HashTable.insert - multiple declarations
Function HashTable.insert
Inserts a new element in the hash table.
If the element with the same key was already in the table, it reassigns
it with the new value, but insert returns 0
. Otherwise
1
is returned.
Parameters
Name | Description |
---|---|
keyValue | Key/value pair. |
Returns
The number of the inserted elements with a unique key.
Example
HashTable!(string, int) hashTable;
assert(hashTable .insert(hashTable .KeyValue("number", 1)) == 1);
assert(hashTable["number"] == 1);
assert(hashTable .insert(hashTable .KeyValue("number", 2)) == 0);
assert(hashTable["number"] == 2);
Function HashTable.insert
Inserts a forward range of key/value pairs into the hash table.
size_t insert(R)
(
scope R range
)
if (isForwardRange!R && is(ElementType!R == KeyValue) && !isInfinite!R);
If some of the elements in the range have the same key, they are reassigned but are not counted as inserted elements. So the value returned by this function will be less than the range length.
Parameters
Name | Description |
---|---|
R | Range type. |
range | Forward range. |
Returns
The number of the inserted elements with a unique key.
Example
HashTable!(string, int) hashTable;
hashTable .KeyValue[2] range = [
hashTable .KeyValue("one", 1),
hashTable .KeyValue("two", 2),
];
assert(hashTable .insert(range[]) == 2);
assert(hashTable["one"] == 1);
assert(hashTable["two"] == 2);