containers.hashmap
Hash Map
License
-
Declaration
struct
HashMap
(K, V, Allocator = Mallocator, alias hashFunction = generateHash!K, bool supportGC = shouldAddGCRange!K || shouldAddGCRange!V);Associative array / hash map.
Parameters
K
the key type
V
the value type
Allocator
the allocator type to use. Defaults to
Mallocator
hashFunction
the hash function to use on the keys
supportGC
true
if 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(Allocator
allocator
);Use the given
for allocations.allocator
-
Declaration
this(size_t
bucketCount
, Allocatorallocator
);Constructs an HashMap with an initial bucket count of
bucketCount
.bucketCount
must be a power of two. -
Declaration
void
clear
();Removes all items from the map
-
Declaration
ref auto
opIndex
(this This)(Kkey
);Supports
aa[
syntax.key
] -
Declaration
inout pure @nogc @safe bool
containsKey
(this This)(Kkey
);Return Value
true
if there is an entry in this map for the given
,key
false
otherwise. -
Declaration
auto
get
(this This)(Kkey
, lazy VdefaultValue
);Gets the value for the given
key
, or returns
if the givendefaultValue
key
is not present.Parameters
K
key
the
key
to look upvalue
the default value
Return Value
the value indexed by
, if present, orkey
otherwise.defaultValue
-
Declaration
auto
getOrAdd
(this This)(Kkey
, lazy VdefaultValue
);If the given
key
does not exist in the HashMap, adds it with the value
.defaultValue
Parameters
K
key
the
key
to look upvalue
the 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
void
opIndexAssign
(Vvalue
, const Kkey
);Supports aa[
key
] =value
; syntax. -
Declaration
inout nothrow @trusted inout(V)*
opBinaryRight
(string op)(const Kkey
) if (op == "in");Supports
key
in aa syntax.Return Value
pointer to the value corresponding to the given
key
, ornull
if thekey
is not present in the HashMap. -
Declaration
bool
remove
(Kkey
);Removes the value associated with the given
key
Return Value
true
if a value was actually removed. -
Declaration
const pure nothrow @nogc @property @safe size_t
length
();Return Value
the number of key/value pairs in this container.
-
Declaration
const pure nothrow @nogc @property @safe bool
empty
();Return Value
true
if there are no items in this container. -
Declaration
inout @trusted auto
byKey
(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
keys
contained in this map. -
Declaration
inout @trusted auto
byValue
(this This)();
aliasopSlice
= byValue;Return Value
a range of the values in this map.
-
Declaration
const @property auto
values
(this This)();Return Value
a GC-allocated array containing the
values
contained in this map. -
Declaration
inout @trusted auto
byKeyValue
(this This)();Return Value
a range of the kev/value pairs in this map. The element type of this range is a struct with
key
andvalue
fields.