From bed6b6a9170253e010cbffd59202add4edfd1c2b Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 19 Feb 2024 10:34:40 +0200 Subject: Add string_map buildfile value type This exposes the std::map type to buildfiles. New functions: $size() $keys() Subscript can be used to lookup a value by key. The result is [null] if there is no value associated with the specified key. For example: map = [string_map] a@1 b@2 c@3 b = ($map[b]) # 2 if ($map[z] == [null]) ... Note that append (+=) is overriding (like std::map::insert_or_assign()) while prepend (=+) is not (like std::map::insert()). In a sense, whatever appears last (from left to right) is kept, which is consistent with what we expect to happen when specifying the same key repeatedly in a literal representation. For example: map = [string_map] a@0 b@2 a@1 # a@1 b@2 map += b@0 c@3 # a@1 b@0 c@3 map =+ b@1 d@4 # a@1 b@0 c@3 d@4 Example of iteration: map = [string_map] a@1 b@2 c@3 for p: $map { k = $first($p) v = $second($p) } While the subscript is mapped to key lookup only, index-based access can be implemented (with a bit of overhead) using the $keys() function: map = [string_map] a@1 b@2 c@3 keys = $keys($m) for i: $integer_sequence(0, $size($keys)) { k = ($keys[$i]) v = ($map[$k]) } Also, this commit changes the naming of other template-based value types (not exposed as buildfile value types) to use C++ template id-like names (e.g., map>). --- tests/type/map/buildfile | 4 +++ tests/type/map/testscript | 65 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 tests/type/map/buildfile create mode 100644 tests/type/map/testscript (limited to 'tests/type') diff --git a/tests/type/map/buildfile b/tests/type/map/buildfile new file mode 100644 index 0000000..7f2cdcf --- /dev/null +++ b/tests/type/map/buildfile @@ -0,0 +1,4 @@ +# file : tests/type/map/buildfile +# license : MIT; see accompanying LICENSE file + +./: testscript $b diff --git a/tests/type/map/testscript b/tests/type/map/testscript new file mode 100644 index 0000000..7b90ddd --- /dev/null +++ b/tests/type/map/testscript @@ -0,0 +1,65 @@ +# file : tests/type/map/testscript +# license : MIT; see accompanying LICENSE file + +# See also tests in function/*/ (size(), keys()). + +.include ../../common.testscript + +: basics +: +$* <>EOO +m = [string_map] a@0 b@2 a@1 +print $m +m += c@3 b@0 +print $m +m =+ d@4 b@1 +print $m +EOI +a@1 b@2 +a@1 b@0 c@3 +a@1 b@0 c@3 d@4 +EOO + +: type +: +$* <>EOO +m = [string_map] +print $type($m) +EOI +string_map +EOO + +: subscript +: +$* <>EOO +m = [string_map] a@1 b@2 c@3 +print ($m[b]) +print ($m[z]) +EOI +2 +[null] +EOO + +: iteration +: +$* <>EOO +for p: [string_map] a@1 b@2 c@3 + print $first($p) $second($p) +EOI +a 1 +b 2 +c 3 +EOO + +: iteration-index +: +$* <>EOO +m = [string_map] a@1 b@2 c@3 +k = $keys($m) +for i: $integer_sequence(0, $size($k)) + print $i ($k[$i]) ($m[($k[$i])]) # @@ TMP: nested subscript +EOI +0 a 1 +1 b 2 +2 c 3 +EOO -- cgit v1.1