The OpenD Programming Language

HashMap

Associative array / hash map.

Constructors

this
this(Allocator allocator)

Use the given allocator for allocations.

this
this(size_t bucketCount, Allocator allocator)

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

this
this(size_t bucketCount)

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

Destructor

A destructor is present on this object, but not explicitly documented in the source.

Postblit

A postblit is present on this object, but not explicitly documented in the source.

Members

Aliases

opSlice
alias opSlice = byValue

Functions

byKey
auto byKey()
byKeyValue
auto byKeyValue()
byValue
auto byValue()
clear
void clear()

Removes all items from the map

containsKey
bool containsKey(K key)
empty
bool empty()
get
auto get(K key, V defaultValue)

Gets the value for the given key, or returns defaultValue if the given key is not present.

getOrAdd
auto getOrAdd(K key, V defaultValue)

If the given key does not exist in the HashMap, adds it with the value defaultValue.

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

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

opBinaryRight
inout(V)* opBinaryRight(K key)

Supports key in aa syntax.

opIndex
ref opIndex(K key)

Supports aa[key] 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

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);
assert (hm.containsKey("answer"));
hm.remove("answer");
assert (hm.length == 0);
assert ("answer" !in hm);
assert (hm.get("answer", 1000) == 1000);
assert (!hm.containsKey("answer"));
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);

Meta