Hash Table¶
| Spice | |
|---|---|
HashTable<K, V> struct¶
Implements: IIterable<Pair<K, V>>
A hash table in Spice is a commonly used data structure, which stores key-value pairs and allows fast access to a value by its key. Collisions are resolved by open addressing with linear probing over a contiguous slot array, and the table grows once it reaches its maximum load factor.
Time complexity:
Insert: O(1) (amortized average case), O(n) (worst case)
Delete: O(1) (average case), O(n) (worst case)
Lookup: O(1) (average case), O(n) (worst case)
The average case assumes a good hash distribution. In the worst case, all keys probe the same chain of slots, degrading every operation to a linear scan.
The entries themselves live in their own allocations and are never relocated, only the slot array is rebuilt when the table grows. A reference obtained from get() therefore stays valid until that particular key is removed, the table is cleared or the table is destructed - inserting other keys never invalidates it.
Constructors¶
ctor¶
| Spice | |
|---|---|
Construct an empty hash table with room for the given number of entries. Passing 0 keeps the hash table in an unallocated state; the slot array is materialized lazily on the first insertion.
Parameters
| Name | Type | Description |
|---|---|---|
initialCapacity |
unsigned long |
Number of entries to pre-allocate slots for (default: 0l) |
ctor¶
| Spice | |
|---|---|
Construct a hash table as a deep copy of another hash table
Parameters
| Name | Type | Description |
|---|---|---|
original |
const HashTable<K, V>& |
Hash table to copy |
dtor¶
| Spice | |
|---|---|
Destructs the hash table, destructing all entries and freeing the slot array
Methods¶
upsert¶
| Spice | |
|---|---|
Insert a key-value pair into the hash table. If the key already exists, the value is updated.
Parameters
| Name | Type | Description |
|---|---|---|
key |
const K& |
The key to insert |
value |
const V& |
The value to insert |
get¶
| Spice | |
|---|---|
Retrieve the value associated with the given key. If the key is not found, panic.
Parameters
| Name | Type | Description |
|---|---|---|
key |
const K& |
The key to look up |
Returns: V& — The value associated with the key
getSafe¶
| Spice | |
|---|---|
Retrieve the value associated with the given key as Result
Parameters
| Name | Type | Description |
|---|---|---|
key |
const K& |
The key to look up |
Returns: Result<V> — Result
remove¶
| Spice | |
|---|---|
Remove the key-value pair associated with the given key. If the key is not found, do nothing.
Parameters
| Name | Type | Description |
|---|---|---|
key |
const K& |
The key to remove |
contains¶
| Spice | |
|---|---|
Check if the hash table contains the given key.
Parameters
| Name | Type | Description |
|---|---|---|
key |
const K& |
The key to check for |
Returns: bool — True if the key is found, false otherwise
getSize¶
| Spice | |
|---|---|
Get the size of the hash table.
Returns: unsigned long — The number of key-value pairs in the hash table
isEmpty¶
| Spice | |
|---|---|
Checks if the hash table is empty.
Returns: bool — True if empty, false otherwise.
clear¶
| Spice | |
|---|---|
Clear the hash table, removing all key-value pairs. The slot array is kept for reuse.
getIterator¶
| Spice | |
|---|---|
Retrieve a forward iterator for the hash table
Returns: HashTableIterator<K, V>
HashTableIterator<K, V> struct¶
Implements: IIterator<Pair<const K&, V&>>
Iterator to iterate over a hash table data structure
Constructors¶
ctor¶
| Spice | |
|---|---|
Construct a hash table iterator over the given hash table
Parameters
| Name | Type | Description |
|---|---|---|
hashTable |
HashTable<K, V>& |
Hash table to iterate over |
Methods¶
get¶
| Spice | |
|---|---|
Returns the current key-value pair of the hash table
Returns: Pair<const K&, V&>& — Current key/value pair
getIdx¶
| Spice | |
|---|---|
Returns the current index and the current item of the hash table
Returns: Pair<unsigned long, Pair<const K&, V&>&> — Pair of current index and current key/value pair
isValid¶
| Spice | |
|---|---|
Check if the iterator is valid
The iterator maintains the invariant that its slot index either addresses an occupied slot or has run past the last one, so validity is a single comparison.
Returns: bool — true or false
next¶
| Spice | |
|---|---|
Moves the cursor to the next key/value pair
Operators¶
operator=¶
| Spice | |
|---|---|
Copy-assign the contents of another hash table into this one
Parameters
| Name | Type | Description |
|---|---|---|
newValue |
const HashTable<K, V>& |
Hash table to copy from |