HashMap

Destructor

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

Members

Aliases

require
alias require = getOrAdd

Functions

byKey
auto byKey()

iterator by keys

byPair
auto byPair()

iterator by key/value pairs

byValue
auto byValue()

iterator by values

clear
void clear()

throw away all keys

get
V get(K k, T defaultValue)

get

getOrAdd
V getOrAdd(K k, T defaultValue)

get value from hash or add if key is not in table. defaultValue can be callable.

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).

keyPointer
KeyPointer keyPointer(K key)
length
auto length()

get numter of keys in table

opBinaryRight
V* opBinaryRight(in K k)

key in table

opIndex
V opIndex(in K k)

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

opIndexAssign
void opIndexAssign(V v, K k)

mapk = v;

put
V* put(K k, V v)

put pair (k,v) into hash.

remove
bool remove(K k)

remomve key from hash.

size
auto size()

get current buckets number

Structs

KeyPointer
struct KeyPointer

Examples

Example

1 import std.range;
2 import std.algorithm;
3 
4 HashMap!(string, int) counter;
5 string[] words = ["hello", "this", "simple", "example", "should", "succeed", "or", "it", "should", "fail"];
6 // count words, simplest and fastest way
7 foreach (word; words)
8 {
9     counter.getOrAdd(word, 0)++;
10 }
11 assert("world" !in counter);
12 assert(counter["hello"] == 1);
13 assert(counter["should"] == 2);
14 assert(counter.length == words.length - 1);
15 // clear counter
16 counter.clear;
17 assert(counter.length == 0);
18 // more verbose way to count
19 foreach (word; words)
20 {
21     auto w = word in counter;
22     if (w)
23     {
24         (*w)++;
25     }
26     else
27     {
28         counter[word] = 1;
29     }
30 }
31 assert("world" !in counter);
32 assert(counter["hello"] == 1);
33 assert(counter["should"] == 2);
34 assert(counter.length == words.length - 1);
35 // iterators
36 assert(counter.byKey.count == counter.byValue.count);
37 assert(words.all!(w => w in counter));          // all words are in table
38 assert(counter.byValue.sum == words.length);    // sum of counters must equals to number of words

Meta