containers.hashmap
Hash Map
License
-
Declaration
structHashMap(K, V, Allocator = Mallocator, alias hashFunction = generateHash!K, bool supportGC = shouldAddGCRange!K || shouldAddGCRange!V);Associative array / hash map.
Parameters
Kthe key type
Vthe value type
Allocatorthe allocator type to use. Defaults to
MallocatorhashFunctionthe hash function to use on the keys
supportGCtrueif the container should support holding references to GC-allocated memory.Examples
import std.uuid : randomUUID; import std.range.primitives : walkLength; auto hm = HashMap!(string, int)(16); assert (hm.length == 0); assert (!hm.remove("abc")); hm["answer"] = 42; assert (hm.length == 1); assert ("answer" in hm); hm.remove("answer"); assert (hm.length == 0); hm["one"] = 1; hm["one"] = 1; assert (hm.length == 1); assert (hm["one"] == 1); hm["one"] = 2; assert(hm["one"] == 2); foreach (i; 0 .. 1000) { hm[randomUUID().toString] = i; } assert (hm.length == 1001); assert (hm.keys().length == hm.length); assert (hm.values().length == hm.length); () @nogc { assert (hm.byKey().walkLength == hm.length); assert (hm.byValue().walkLength == hm.length); assert (hm[].walkLength == hm.length); assert (hm.byKeyValue().walkLength == hm.length); }(); foreach (v; hm) {} auto hm2 = HashMap!(char, char)(4); hm2['a'] = 'a'; HashMap!(int, int) hm3; assert (hm3.get(100, 20) == 20); hm3[100] = 1; assert (hm3.get(100, 20) == 1); auto pValue = 100 in hm3; assert(*pValue == 1);
-
Declaration
pure nothrow @nogc @safe this(Allocatorallocator);Use the given
for allocations.allocator -
Declaration
this(size_tbucketCount, Allocatorallocator);Constructs an HashMap with an initial bucket count of
bucketCount.bucketCountmust be a power of two. -
Declaration
voidclear();Removes all items from the map
-
Declaration
ref autoopIndex(this This)(Kkey);Supports
aa[syntax.key] -
Declaration
inout pure @nogc @safe boolcontainsKey(this This)(Kkey);Return Value
trueif there is an entry in this map for the given,keyfalseotherwise. -
Declaration
autoget(this This)(Kkey, lazy VdefaultValue);Gets the value for the given
key, or returnsif the givendefaultValuekeyis not present.Parameters
Kkeythe
keyto look upvaluethe default value
Return Value
the value indexed by
, if present, orkeyotherwise.defaultValue -
Declaration
autogetOrAdd(this This)(Kkey, lazy VdefaultValue);If the given
keydoes not exist in the HashMap, adds it with the value.defaultValueParameters
Kkeythe
keyto look upvaluethe default value
Return Value
a pointer to the value stored in the HashMap with the given
key. The pointer is guaranteed to be valid only until the next HashMap modification. -
Declaration
voidopIndexAssign(Vvalue, const Kkey);Supports aa[
key] =value; syntax. -
Declaration
inout nothrow @trusted inout(V)*opBinaryRight(string op)(const Kkey) if (op == "in");Supports
keyin aa syntax.Return Value
pointer to the value corresponding to the given
key, ornullif thekeyis not present in the HashMap. -
Declaration
boolremove(Kkey);Removes the value associated with the given
keyReturn Value
trueif a value was actually removed. -
Declaration
const pure nothrow @nogc @property @safe size_tlength();Return Value
the number of key/value pairs in this container.
-
Declaration
const pure nothrow @nogc @property @safe boolempty();Return Value
trueif there are no items in this container. -
Declaration
inout @trusted autobyKey(this This)();Return Value
a range of the keys in this map.
-
Declaration
const @property K[]keys();Return Value
a GC-allocated array filled with the
keyscontained in this map. -
Declaration
inout @trusted autobyValue(this This)();
aliasopSlice= byValue;Return Value
a range of the values in this map.
-
Declaration
const @property autovalues(this This)();Return Value
a GC-allocated array containing the
valuescontained in this map. -
Declaration
inout @trusted autobyKeyValue(this This)();Return Value
a range of the kev/value pairs in this map. The element type of this range is a struct with
keyandvaluefields.