RehashingAA

A wrapper around a native associative array that you can controllably set to automatically rehash as entries are added.

Constructors

this
this(V[K] aa)

Constructor.

Alias This

aa

alias this with regards to aa.

Members

Functions

aaOf
inout(V[K]) aaOf()

Returns the internal associative array, for when the wrapper is insufficient.

clear
void clear()

Clears the internal associative array and all counters.

dup
auto dup()

Duplicates this. Explicitly copies the internal associative array.

length
auto length()

Returns the length of the internal associative array.

maybeRehash
auto maybeRehash()

Bumps the internal counter of new keys since the last rehash, and depending on the resulting value of it, maybe rehashes.

newKeysSinceLastRehash
auto newKeysSinceLastRehash()

The number of new entries that has been added since the last rehash. Accessor.

numRehashes
auto numRehashes()

The number of times this instance has rehashed itself. Accessor.

opAssign
void opAssign(V[K] aa)

Inherit a native associative array into aa.

opBinaryRight
auto opBinaryRight(K key)

Wraps key in aa to the internal associative array.

opCast
auto opCast()

Allows for casting this into the base associative array type.

opIndexAssign
void opIndexAssign(V value, K key)

Assigns a value into the internal associative array. If it created a new entry, then call maybeRehash to bump the internal counter and maybe rehash.

rehash
auto ref rehash()

Rehashes the internal associative array, bumping the rehash counter and zeroing the keys-added counter. Additionally invokes the onRehashDg delegate.

remove
auto remove(K key)

Removes a key from the aa associative array by merely invoking .remove.

Variables

minimumNeededForRehash
uint minimumNeededForRehash;

The minimum number of additions needed before the first rehash takes place.

onRehashDg
void delegate() onRehashDg;

Delegate called when rehashing takes place.

rehashThresholdMultiplier
double rehashThresholdMultiplier;

The modifier by how much more entries must be added before another rehash takes place, with regards to the current aa length.

Parameters

K

Key type.

V

Value type.

Examples

import std.conv : to;

RehashingAA!(string, int) aa;
aa.minimumNeededForRehash = 2;

aa["abc"] = 123;
aa["def"] = 456;
assert((aa.newKeysSinceLastRehash == 2), aa.newKeysSinceLastRehash.to!string);
assert((aa.numRehashes == 0), aa.numRehashes.to!string);
aa["ghi"] = 789;
assert((aa.numRehashes == 1), aa.numRehashes.to!string);
assert((aa.newKeysSinceLastRehash == 0), aa.newKeysSinceLastRehash.to!string);
aa.rehash();
assert((aa.numRehashes == 2), aa.numRehashes.to!string);

auto realAA = cast(int[string])aa;
assert("abc" in realAA);
assert("def" in realAA);
assert("ghi" in realAA);
assert("jkl" !in realAA);

auto aa2 = aa.dup;
aa2["jkl"] = 123;
assert("jkl" in aa2);
assert("jkl" !in aa);

Meta