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 via separate chaining, where each bucket holds a linked list of entries that hash to the same index.
Time complexity:
Insert: O(1) (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 hash to the same bucket, degrading every operation to a linear scan of that bucket.
Constructors¶
ctor¶
| Spice | |
|---|---|
Construct an empty hash table with the given number of buckets
Parameters
| Name | Type | Description |
|---|---|---|
bucketCount |
unsigned long |
Number of buckets to allocate (default: 100l) |
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 Optional
Parameters
| Name | Type | Description |
|---|---|---|
key |
const K& |
The key to look up |
Returns: Result<V> — Optional
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.
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
Returns: bool — true or false
next¶
| Spice | |
|---|---|
Moves the cursor to the next key/value pair
Functions¶
ctor¶
| Spice | |
|---|---|
Construct a hash entry as a copy of another hash entry
Parameters
| Name | Type | Description |
|---|---|---|
original |
const HashEntry<K, V>& |
Hash entry to copy |