HashMap

Associative array / hash map.

Constructors

this
this(size_t bucketCount)

Constructs an HashMap with an initial bucket count of bucketCount. bucketCount must be a power of two.

Destructor

~this
~this()
Undocumented in source.

Postblit

this(this)
this(this)
Undocumented in source.

Members

Functions

keys
K[] keys()
length
size_t length()
opApply
int opApply(int delegate(ref K, ref V) del)

Support for foreach(key, value; aa) { ... } syntax;

opBinaryRight
bool opBinaryRight(K key)

Supports key in aa syntax.

opIndex
auto opIndex(K key)

Supports aakey syntax.

opIndexAssign
void opIndexAssign(V value, K key)

Supports aakey = value; syntax.

remove
bool remove(K key)

Removes the value associated with the given key

values
auto values()

Parameters

K

the key type

V

the value type

hashFunction

the hash function to use on the keys

Examples

import std.uuid : randomUUID;
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);
foreach (i; 0 .. 1000)
{
	hm[randomUUID().toString] = i;
}
assert (hm.length == 1001);
assert (hm.keys().length == hm.length);
assert (hm.values().length == hm.length);
foreach (ref string k, ref int v; hm) {}

auto hm2 = HashMap!(char, char)(4);
hm2['a'] = 'a';

Meta