Initialize the associative array with default value.
Constructs an associative array using keys and values from the builtin associative array Complexity: O(n log(n))
Constructs an associative array using keys and values.
Returns a dynamic array, the elements of which are the values in the associative array. Doesn't allocate a new copy.
Assume that it is safe to append to this associative array. Appends made to this associative array after calling this function may append in place, even if the array was a slice of a larger array to begin with. Use this only when it is certain there are no elements in use beyond the associative array in the memory block. If there are, those elements will be overwritten by appending to this associative array.
Return a range over all elements (key-values pairs) currently stored in the associative array.
(Property) Gets the current capacity of an associative array. The capacity is the size that the underlaynig slices can grow to before the underlying arrays may be reallocated or extended.
Removes all remaining keys and values from an associative array.
Finds position of the key in the associative array .
Looks up key; if it exists returns corresponding value else evaluates and returns defaultValue.
Returns a dynamic array, the elements of which are the keys in the associative array. Doesn't allocate a new copy.
Reset the associative array
Complexity: O(log(s)), where s is the number of the keys with the same length as the input key.
opCmp Implementation. Doesn't depend on order
== implementation. Doesn't depend on order
Complexity: O(log(s)), where s is the number of the keys with the same length as the input key.
Complexity: O(log(s)) (exist) or O(n) (not exist), where s is the count of the strings with the same length as they key.
remove(key) does nothing if the given key does not exist and returns false. If the given key does exist, it removes it from the AA and returns true.
Looks up key; if it exists returns corresponding value else evaluates value, adds it to the associative array and returns it.
Reserves capacity for an associative array. The capacity is the size that the underlaying slices can grow to before the underlying arrays may be reallocated or extended.
Sorts table according to the keys
hashOf Implementation. Doesn't depend on order
Returns a dynamic array, the elements of which are the values in the associative array. Doesn't allocate a new copy.
Converts to a builtin associative array.
mutable value type, can be instance of $(AlgebraicREF Algebraic) for example.
class C { this(int x) { this.x = x; } int x; bool opEquals(scope const C rhs) const scope @safe pure nothrow @nogc { return x == rhs.x; } override size_t toHash() @safe const scope pure nothrow @nogc { return x; } } StringMap!(const C) table; const v0 = new C(42); const v1 = new C(43); table["0"] = v0; table["1"] = v1; assert(table.keys == ["0", "1"]); assert(table.values == [v0, v1]); // TODO: qualify unittest as `pure` when this is inferred `pure` static assert(is(typeof(table.values) == const(C)[]));
StringMap!int table; table["L"] = 3; table["A"] = 2; table["val"] = 1; assert(table.keys == ["L", "A", "val"]); assert(table.values == [3, 2, 1]); assert(table["A"] == 2); table.values[2] += 10; assert(table["A"] == 2); assert(table["L"] == 3); assert(table["val"] == 11); assert(table.keys == ["L", "A", "val"]); assert(table.values == [3, 2, 11]); table.remove("A"); assert(table.keys == ["L", "val"]); assert(table.values == [3, 11]); assert(table["L"] == 3); assert(table["val"] == 11); assert(table == table); // sorting table["A"] = 2; table.sort; assert(table.keys == ["A", "L", "val"]); assert(table.values == [2, 3, 11]); assert(table["A"] == 2); assert(table["L"] == 3); assert(table["val"] == 11);
static void testEquals(X, Y)() { X x; Y y; assert(x == y); x["L"] = 3; assert(x != y); x["A"] = 2; assert(x != y); x["val"] = 1; assert(x != y); y["val"] = 1; assert(x != y); y["L"] = 3; assert(x != y); y["A"] = 2; assert(x == y); x = X.init; assert(x != y); y = Y.init; assert(x == y); } testEquals!(StringMap!int, StringMap!uint)(); testEquals!(StringMap!int, uint[string])(); testEquals!(uint[string], StringMap!int)();
Ordered string-value associative array with extremely fast lookup.