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.Parameters: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
, UassocArr
)
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.Parameters:A allocator
a allocator.html#.RCIAllocator">std.experimental. allocator
.RCIAllocator or allocator.html#.RCISharedAllocator">std.experimental.allocator
.RCISharedAllocatorallocator
objectU 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; 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)(UassocArr
)
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 ofkeys
Complexity Ο(size).
- Array!V
values
(this _)(); - Get an array with the existing
values
in the hashtable.Returns:an Array!V array ofvalues
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 _)(Kkey
, VnullValue
); - Get the value corresponding to the given
key
; if it doesn't exist returnnullValue
.Parameters:K key
the key
corresponding to a valueV nullValue
a value that will be returned if there is no such key
-value pairReturns:the correspondingkey
value, ornullValue
.Complexity Ο(n), where n is the number of elements in the corresponding
key
bucket. - Nullable!V
opIndex
(this _)(Kkey
); - Get a Nullable!V corresponding to the given
key
index.Parameters:K key
the key
corresponding to a valueReturns:a nullable valueComplexity Ο(n), where n is the number of elements in the corresponding
key
bucket. - Nullable!V
opIndexUnary
(string op)(Kkey
); - 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 valueReturns: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)(Uval
, Kkey
)
if (isImplicitlyConvertible!(U, V)); - Assign
val
to the element corresponding tokey
. 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 valueReturns: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)(Uval
, Kkey
)
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 valueReturns: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 torhs
, unlessrhs
is null, in which case the current hashtable will become empty. Ifrhs
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 hashtableComplexity Ο(length).
- void
clear
(); - Removes all the elements in the current hashtable.
Complexity Ο(length).