Map¶
| Spice | |
|---|---|
Map<K, V> struct¶
Implements: IIterable<Pair<K, V>>
A 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(log n)
Delete: O(log n)
Lookup: O(log n)
Methods¶
insert¶
| Spice | |
|---|---|
Inserts a key value pair into the map.
Parameters
| Name | Type | Description |
|---|---|---|
key |
const K& |
The key to insert. |
value |
const V& |
The value to insert. |
remove¶
| Spice | |
|---|---|
Removes a key value pair from the map.
Parameters
| Name | Type | Description |
|---|---|---|
key |
const K& |
The key to remove. |
get¶
| Spice | |
|---|---|
Retrieve the value associated with the given key. Note: If the key is not found in the map, this function will panic. To avoid this, use getSafe instead.
Parameters
| Name | Type | Description |
|---|---|---|
key |
const K& |
The key to get. |
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
contains¶
| Spice | |
|---|---|
Checks if the map contains a key.
Parameters
| Name | Type | Description |
|---|---|---|
key |
const K& |
The key to check. |
Returns: bool — True if the map contains the key, false otherwise.
getSize¶
| Spice | |
|---|---|
Gets the number of elements in the map.
Returns: unsigned long — The number of elements in the map.
isEmpty¶
| Spice | |
|---|---|
Checks if the map is empty.
Returns: bool — True if the map is empty, false otherwise.
clear¶
| Spice | |
|---|---|
Removes all elements from the map.
getIterator¶
| Spice | |
|---|---|
Retrieve a forward iterator for the map
Returns: MapIterator<K, V>
MapIterator<K, V> struct¶
Implements: IIterator<Pair<const K&, V&>>
Iterator to iterate over a map data structure
Constructors¶
ctor¶
| Spice | |
|---|---|
Construct a map iterator over the given map
Parameters
| Name | Type | Description |
|---|---|---|
map |
Map<K, V>& |
Map to iterate over |
Methods¶
get¶
| Spice | |
|---|---|
Returns the current key-value pair of the map
Returns: Pair<const K&, V&>& — Current key/value pair
getIdx¶
| Spice | |
|---|---|
Returns the current index and the current item of the 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. Note: If the key is not found in the map, this function will panic. To avoid this, use getSafe instead.
Parameters
| Name | Type | Description |
|---|---|---|
map |
Map<K, V>& |
|
key |
const K& |
The key to get. |
Returns: V& — The value associated with the key.
operator++¶
| Spice | |
|---|---|
Advances the cursor by one
Parameters
| Name | Type | Description |
|---|---|---|
it |
MapIterator<K, V>& |
MapIterator |