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

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

clear
void clear()

throw away all keys

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

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

key in table

opBinaryRight
deprecated 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

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.fetch("world").ok);
assert(counter.fetch("hello").value == 1);
assert(counter["hello"] == 1);
assert(counter["should"] == 2);
assert(counter.contains("hello"));
assert(counter.length == words.length - 1);
// iterators
assert(counter.byKey.count == counter.byValue.count);
assert(words.all!(w => counter.contains(w))); // all words are in table
assert(counter.byValue.sum == words.length); // sum of counters must equals to number of words

Meta