Unordered Map¶
| Spice | |
|---|---|
UnorderedMap<K, V> struct¶
Implements: IIterable<Pair<K, V>>
An unordered map in Spice is a commonly used data structure, which can be used to represent a list of key value pairs.
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)
Constructors¶
ctor¶
| Spice | |
|---|---|
Construct an empty unordered map with the given number of buckets
Parameters
| Name | Type | Description |
|---|---|---|
bucketCount |
unsigned long |
Number of buckets in the underlying hash table (default: 100l) |
Methods¶
upsert¶
| Spice | |
|---|---|
Insert a key-value pair into the map 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 | |
|---|---|
Check if the map contains the given key.
Parameters
| Name | Type | Description |
|---|---|---|
key |
const K& |
The key to check for |
Returns: True if the key is found, false otherwise
contains¶
| Spice | |
|---|---|
Check if the map 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 unordered map.
Returns: unsigned long — The number of key-value pairs in the map
isEmpty¶
| Spice | |
|---|---|
Check if the unordered map is empty.
Returns: bool — True if empty, false otherwise
clear¶
| Spice | |
|---|---|
Clear the unordered map, removing all key-value pairs.
getIterator¶
| Spice | |
|---|---|
Retrieve a forward iterator for the unordered map
Returns: UnorderedMapIterator<K, V>
UnorderedMapIterator<K, V> struct¶
Implements: IIterator<Pair<const K&, V&>>
Iterator to iterate over an unordered map data structure
Constructors¶
ctor¶
| Spice | |
|---|---|
Construct an unordered map iterator over the given unordered map
Parameters
| Name | Type | Description |
|---|---|---|
unorderedMap |
UnorderedMap<K, V>& |
Unordered map to iterate over |
Methods¶
get¶
| Spice | |
|---|---|
Returns the current key-value pair of the unordered map
Returns: Pair<const K&, V&>& — Current key/value pair
getIdx¶
| Spice | |
|---|---|
Returns the current index and the current item of the unordered map
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
Operators¶
operator[]¶
| Spice | |
|---|---|
Retrieve the value associated with the given key. If the key is not found, panic.
Parameters
| Name | Type | Description |
|---|---|---|
map |
UnorderedMap<K, V>& |
|
key |
const K& |
The key to look up |
Returns: V& — The value associated with the key
operator++¶
| Spice | |
|---|---|
Advances the cursor by one
Parameters
| Name | Type | Description |
|---|---|---|
it |
UnorderedMapIterator<K, V>& |
UnorderedMapIterator |