The OpenD Programming Language

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.

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

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

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

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