Report a bug
If you spot a problem with this page, click here to create a Github issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

stdx.collections.hashtable

struct Hashtable(K, V);
this(A, this Q)(A allocator)
if (!is(Q == shared) && (is(A == RCISharedAllocator) || !is(Q == immutable)) && (is(A == RCIAllocator) || is(A == RCISharedAllocator)));
Constructs a qualified hashtable that will use the provided allocator object. For immutable objects, a RCISharedAllocator must be supplied.

Complexity Ο(1)

Examples:
import std.experimental.allocator : theAllocator, processAllocator;

auto h = Hashtable!(int, int)(theAllocator);
auto ch = const Hashtable!(int, int)(processAllocator);
auto ih = immutable Hashtable!(int, int)(processAllocator);
this(U, this Q)(U assocArr)
if (is(U == Value[Key], Value : V, Key : K));
Constructs a qualified hashtable out of an associative array. Because no allocator was provided, the hashtable will use the GCAllocator.std.experimental.allocator.gc_allocator.
Parameters:
U assocArr an associative array

Complexity Ο(m), where m is the number of (key, value) pairs in the associative array.

Examples:
import std.algorithm.comparison : equal;

auto h = Hashtable!(int, int)([1 : 10]);
assert(equal(h.keys(), [1]));
assert(equal(h.values(), [10]));
this(A, U, this Q)(A allocator, U assocArr)
if (!is(Q == shared) && (is(A == RCISharedAllocator) || !is(Q == immutable)) && (is(A == RCIAllocator) || is(A == RCISharedAllocator)) && is(U == Value[Key], Value : V, Key : K));
Constructs a qualified hashtable out of an associative array that will use the provided allocator object. For immutable objects, a RCISharedAllocator must be supplied.

Complexity Ο(m), where m is the number of (key, value) pairs in the associative array.

Examples:
import std.algorithm.comparison : equal;
import stdx.collections.array : Array;

{
    auto h = Hashtable!(int, int)([1 : 10]);
    assert(equal(h.keys(), [1]));
    assert(equal(h.values(), [10]));
}

{
    auto h = immutable Hashtable!(int, int)([1 : 10]);
    assert(equal(h.values(), Array!int([10])));
}
size_t insert(U)(U assocArr)
if (is(U == Value[Key], Value : V, Key : K));
Insert the (key, value) pairs of an associative array into the hashtable.
If no allocator was provided when the list was created, the GCAllocator.std.experimental.allocator.gc_allocator will be used.
Parameters:
U assocArr an associative array

Complexity Ο(m), where m is the number of (key, value) pairs in the associative array.

Examples:
import std.algorithm.comparison : equal;

auto h = Hashtable!(int, int)();
assert(h.length == 0);
h.insert([1 : 10]);
assert(equal(h.keys(), [1]));
assert(equal(h.values(), [10]));
const pure nothrow @nogc @safe size_t length();
Returns number of key-value pairs.
Returns:
a positive integer.

Complexity Ο(1).

Examples:
import std.algorithm.comparison : equal;

auto h = Hashtable!(int, int)();
assert(h.length == 0);
h.insert([1 : 10]);
assert(h.length == 1);
const pure nothrow @nogc @safe size_t size();
Returns number of buckets.
Returns:
a positive integer.

Complexity Ο(1).

const pure nothrow @nogc @safe bool empty();
Check if the hashtable is empty.
Returns:
true if there are no elements in the hashtable; false otherwise.

Complexity Ο(1).

ref auto front(this _)();
Provide access to the first value in the hashtable. The user must check that the hashtable isn't empty, prior to calling this function. There is no guarantee of the order of the values, as they are placed in the hashtable based on the result of the hash function.
Returns:
a reference to the first found value.

Complexity Ο(length).

Array!K keys(this _)();
Get an array with the existing keys in the hashtable.
Returns:
an Array!K array of keys

Complexity Ο(size).

Array!V values(this _)();
Get an array with the existing values in the hashtable.
Returns:
an Array!V array of values

Complexity Ο(length).

Array!(Tuple!(K, "key", V, "value")) keyValuePairs(this _)();
Get an array with the existing key-value pairs in the hashtable.
Returns:
an Array!(Tuple!(K, "key", V, "value")) array of key-value pairs

Complexity Ο(length).

V get(this _)(K key, V nullValue);
Get the value corresponding to the given key; if it doesn't exist return nullValue.
Parameters:
K key the key corresponding to a value
V nullValue a value that will be returned if there is no such key-value pair
Returns:
the corresponding key value, or nullValue.

Complexity Ο(n), where n is the number of elements in the corresponding key bucket.

Nullable!V opIndex(this _)(K key);
Get a Nullable!V corresponding to the given key index.
Parameters:
K key the key corresponding to a value
Returns:
a nullable value

Complexity Ο(n), where n is the number of elements in the corresponding key bucket.

Nullable!V opIndexUnary(string op)(K key);
Apply a unary operation to the value corresponding to key. If there isn't such a value, return a V.init wrapped inside a Nullable.
Parameters:
K key the key corresponding to a value
Returns:
a nullable value corresponding to the result or V.init.

Complexity Ο(n), where n is the number of elements in the corresponding key bucket.

Nullable!V opIndexAssign(U)(U val, K key)
if (isImplicitlyConvertible!(U, V));
Assign val to the element corresponding to key. If there isn't such a value, return a V.init wrapped inside a Nullable.
Parameters:
U val the value to be set
K key the key corresponding to a value
Returns:
a nullable value corresponding to the result or V.init.

Complexity Ο(n), where n is the number of elements in the corresponding key bucket.

Nullable!V opIndexOpAssign(string op, U)(U val, K key)
if (isImplicitlyConvertible!(U, V));
Assign to the element corresponding to key the result of applying op to the current value. If there isn't such a value, return a V.init wrapped inside a Nullable.
Parameters:
U val the value to be used with op
K key the key corresponding to a value
Returns:
a nullable value corresponding to the result or V.init.

Complexity Ο(n), where n is the number of elements in the corresponding key bucket.

ref auto opAssign()(auto ref typeof(this) rhs);
Assign rhs to this hashtable. The current hashtable will now become another reference to rhs, unless rhs is null, in which case the current hashtable will become empty. If rhs refers to the current hashtable nothing will happen.
If there are no more references to the previous hashtable the previous hashtable will be destroyed; this leads to a Ο(length) complexity.
Parameters:
typeof(this) rhs a reference to a hashtable
Returns:
a reference to this hashtable

Complexity Ο(length).

void clear();
Removes all the elements in the current hashtable.

Complexity Ο(length).