@head @module std @title Class std::Map<KT, VT> @index Map @implements Iterable<(KT, VT)> @supertypes

The Map class implements a mapping from keys to values. Keys may be any objects that provide a hash value using the @ref{std::Hash} function and support the == operator, while values may be arbitrary objects. At most a single object can be associated with a specific key. If several values are assigned to a key, the later assignments replace the earlier values. @class Map(... as Pair) @desc Construct a Map object. The constructor accepts an arbitrary number of Pair objects that define the initial (key, value) pairs to be added to the map. The left item of each pair is the key, and the right item is the value. Example: @example var m = Map("one": 1, "three": 33) m["one"] -- Result: 1 @end @end

Map methods

@fun get(key as KT, default as VT) as VT @desc Return the value associated with key if the key is in the map. Otherwise, return default. @end @fun length() as Int @desc Return the number of keys in a map. @end @fun hasKey(key as Object) as Boolean @desc Return True if the key is in the map, or False otherwise. @end @fun remove(key as KT) @desc Remove a key and the corresponding value from the map. Raise @ref{KeyError} if the key is not in the map. @end @fun keys() as Array @desc Return an array of all the keys in the map. The keys are returned in an arbitrary order. @end @fun values() as Array @desc Return an array of all the values in the map (one per key). The values are returned in an arbitrary order. @end @fun items() as Array<(KT, VT)> @desc Return an array of all the (key, value) pairs in the map. Each pair is represented as a tuple. The items are returned in an arbitrary order. @end @fun iterator() as Iterator<(KT, VT)> @desc Return an iterator object that returns all the (key, value) pairs in the map, in an arbitrary order. Each item in the iteration is a tuple with two items, key and value. @note You should not modify the map object during iteration, as the iterator assumes that the items in the map remain fixed. @end @end

Map operations

Map objects support the following operations: @op map[key] @optype{Map[KT] -> VT} @desc Return the value associated with a key, or raise @ref{KeyError} if the key is not in the map. @end @op map[key] = value @optype{Map[KT] = VT} @desc Add a new association to the map, or replace an existing association, if the key already exists. @end @op for key, value in map @optype{for KT, VT in Map} @desc Map contents can be iterated with a for loop. Each item in the iteration is a tuple (key, value). The items are iterated in an arbitrary order. @note You should not modify the map object during iteration, as the iterator assumes that the items in the map remain fixed. @end @end @op Str(map) @desc Return a string representation of a map. @end