Struct OrderedMap

An associative array that permits access to ordered keys.

struct OrderedMap(KeyT, ValueT) ;

This is a thin wrapper around D's built-in [Associative Arrays](https://dlang.org/spec/hash-map.html) couple with an [Array](https://dlang.org/spec/arrays.html) that keeps track of key order.

The interface is identical to an Associative Array with one exception: an [OrderedMap] may not be initialized from a literal, because the order of keys in a literal is not known. E.g. OrderedMap!(string, int) = ["a": 2, "b": 1]; is not permitted.

Fields

NameTypeDescription
map ValueT[KeyT]The underlying associative array used.
orderedKeys KeyT[]A maintained list of keys in the order they were added.

Methods

NameDescription
clear () Removes all map keys and clears the orderedKeys.
isEmpty () Returns whether the OrderedMap is empty or not.
opAssign (map) Hide this method because the order of the initial map is unknown.
opIndexAssign (value, key) Assign a value to the map and add it to the orderedKeys if it is new.
remove (key) Removes a single item from the map and the orderedKeys.

Example

Initializing from an unordered map is not allowed.

import std.exception : assertThrown;
OrderedMap!(string, int) omap;
assertThrown!AssertError(omap = ["a": 1, "b": 2]);

Example

Values can be read and written just like an associative array.

import std.exception : assertThrown;
OrderedMap!(string, int) omap;
omap["b"] = 1;
assert(omap["b"] == 1);

Example

A new property orderedKeys is available to use.

OrderedMap!(string, int) omap;
omap["b"] = 1;
omap["a"] = 2;
omap["c"] = 3;
assert(omap.orderedKeys == ["b", "a", "c"]);