HashMap

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

require
alias require = getOrAdd

Functions

addIfMissed
bool addIfMissed(K k, T value)

Add key/value to hash if key is not in table. value can be lazy/callable. Returns: true if key were added.

byKey
auto byKey()

iterator by keys

byPair
auto byPair()

iterator by key/value pairs

byValue
auto byValue()

iterator by values

capacity
auto capacity()

when capacity == 0 - next put for new key can trigger resize

clear
void clear()

throw away all keys

dump
string dump()

dump HashMap content to string (for debugging)

fetch
auto fetch(K k)

fetch is safe(do not return pointer) and nogc (do not throw exception) variant of "in" but retuns tuple("ok", "value"). You can check if result.ok == true. It this case you'll find value in "value"

get
V get(K k, T defaultValue)

get with default value it infers @safe, @nogc from user data: do not return ptr and do not thow Returns: value from hash, or defaultValue if key not found (see also getOrAdd). defaultValue can be callable.

getOrAdd
V getOrAdd(K k, T defaultValue)

get value from hash or add if key is not in table. defaultValue can be callable. Returns: ref to value (maybe added)

grow_factor
auto grow_factor()

get current grow factor.

grow_factor
void grow_factor(int gf)

set grow factor (can be between 2, 4 or 8).

length
auto length()

get numter of keys in table

opBinaryRight
auto opBinaryRight(K k)

key in table Returns: pointer to stored value (if key in table) or null

opBinaryRight
auto opBinaryRight(K k)

"in" is unsafe as it can point to arbitrary address after resize

opIndex
auto opIndex(K k)

mapkey Attention: you can't use this method in @nogc code. Usual aakey method. Throws exception if key not found Returns: value for given key

opIndexAssign
void opIndexAssign(V v, K k)

mapk = v;

put
auto put(K k, V v)

put pair (k,v) into hash. inherits @safe and @nogc properties from K and V It can resize table if table is overloaded or has too much deleted entries. Returns: Nullable with old value if value was updated, or empty Nullable if we just stored new value.

remove
bool remove(K k)

remomve key from hash. Returns: true if actually removed, false otherwise.

size
auto size()

get current buckets number

Examples

Example

1 import std.range;
2 import std.algorithm;
3 import std.experimental.logger;
4 HashMap!(string, int) counter;
5 string[] words = [
6     "hello", "this", "simple", "example", "should", "succeed", "or", "it",
7     "should", "fail"
8 ];
9 // count words, simplest and fastest way
10 foreach (word; words) {
11     counter[word] = counter.getOrAdd(word, 0) + 1;
12 }
13 assert(!counter.fetch("world").ok);
14 assert(counter.fetch("hello").value == 1);
15 assert(counter["hello"] == 1);
16 assert(counter["should"] == 2);
17 assert(counter.contains("hello"));
18 assert(counter.length == words.length - 1);
19 // iterators
20 assert(counter.byKey.count == counter.byValue.count);
21 assert(words.all!(w => counter.contains(w))); // all words are in table
22 assert(counter.byValue.sum == words.length); // sum of counters must equals to number of words