HashMap

Destructor

~this
~this()
Undocumented in source.

Postblit

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

Members

Aliases

StoredKeyType
alias StoredKeyType = StoredType!K
Undocumented in source.
StoredValueType
alias StoredValueType = StoredType!V
Undocumented in source.
allocator
alias allocator = Allocator.instance
Undocumented in source.
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.

byKey
auto byKey()

iterator by keys

byPair
auto byPair()

iterator by key/value pairs

byValue
auto byValue()

iterator by values

capacity
auto capacity()
Undocumented in source. Be warned that the author may not have intended to support it.
clear
void clear()

throw away all keys

fetch
auto fetch(K k)

fetch is safe(do not return pointer) and nogc (do not throw exception) variant of "in" but in exchange of the cost of returning value instead of pointer we return ok = true and value if key in map, ok = false otherwise

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

get
V get(K k, T defaultValue)
Undocumented in source. Be warned that the author may not have intended to support it.
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).

length
auto length()

get numter of keys in table

opAssign
void opAssign(typeof(this) rhs)
Undocumented in source. Be warned that the author may not have intended to support it.
opBinaryRight
auto opBinaryRight(K k)

key in table

opBinaryRight
auto opBinaryRight(K k)
Undocumented in source. Be warned that the author may not have intended to support it.
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

opIndexAssign
void opIndexAssign(V v, K k)

mapk = v;

put
auto 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

toString
string toString()
Undocumented in source. Be warned that the author may not have intended to support it.

Structs

_Bucket
struct _Bucket
Undocumented in source.

Variables

_allocated
int _allocated;
Undocumented in source.
_buckets
BucketStorage _buckets;
Undocumented in source.
_buckets_num
int _buckets_num;
Undocumented in source.
_deleted
int _deleted;
Undocumented in source.
_empty
int _empty;
Undocumented in source.
_grow_factor
int _grow_factor;
Undocumented in source.
_mask
int _mask;
Undocumented in source.

Examples

Example

import std.range;
import std.algorithm;
import std.experimental.logger;
HashMap!(string, int) counter;
string[] words = [
    "hello", "this", "simple", "example", "should", "succeed", "or", "it",
    "should", "fail"
];
// count words, simplest and fastest way
foreach (word; words) {
    counter[word] = counter.getOrAdd(word, 0) + 1;
}
assert(counter.capacity ==  32*4/5 - counter.length);
assert(!counter.fetch("world").ok);
assert(counter["hello"] == 1);
assert(counter["should"] == 2);
assert(counter.length == words.length - 1);
// clear counter
counter.clear;
assert(counter.length == 0);
// more verbose way to count
foreach (word; words) {
    auto w = word in counter;
    if (w) {
        (*w)++;
    }
    else {
        counter[word] = 1;
    }
}
assert(!counter.fetch("world").ok);
assert(counter["hello"] == 1);
assert(counter["should"] == 2);
assert(counter.length == words.length - 1);
// iterators
assert(counter.byKey.count == counter.byValue.count);
assert(words.all!(w => w in counter)); // all words are in table
assert(counter.byValue.sum == words.length); // sum of counters must equals to number of words

Meta