Skip to content

Unordered Map

Spice
import "std/data/unordered-map";

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
public p UnorderedMap.ctor(unsigned long bucketCount = 100l)

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
public p UnorderedMap.upsert(const K& key, const V& value)

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
public f<V&> UnorderedMap.get(const K& key)

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
public f<Result<V>> UnorderedMap.getSafe(const K& key)

Retrieve the value associated with the given key as Result. If the key is not found, the result contains an error.

Parameters

Name Type Description
key const K& The key to look up

Returns: Result<V> — Result, containing the value associated with the key or an error if the key is not found

remove

Spice
public p UnorderedMap.remove(const K& key)

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
public f<bool> UnorderedMap.contains(const K& key)

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
public f<unsigned long> UnorderedMap.getSize()

Get the size of the unordered map.

Returns: unsigned long — The number of key-value pairs in the map

isEmpty

Spice
public f<bool> UnorderedMap.isEmpty()

Check if the unordered map is empty.

Returns: bool — True if empty, false otherwise

clear

Spice
public p UnorderedMap.clear()

Clear the unordered map, removing all key-value pairs.

getIterator

Spice
public f<UnorderedMapIterator<K, V>> UnorderedMap.getIterator()

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
public p UnorderedMapIterator.ctor<K, V>(UnorderedMap<K, V>& unorderedMap)

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
public inline f<Pair<const K&, V&>&> UnorderedMapIterator.get()

Returns the current key-value pair of the unordered map

Returns: Pair<const K&, V&>& — Current key/value pair

getIdx

Spice
public inline f<Pair<unsigned long, Pair<const K&, V&>&>> UnorderedMapIterator.getIdx()

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
public inline f<bool> UnorderedMapIterator.isValid()

Check if the iterator is valid

Returns: bool — true or false

next

Spice
public inline p UnorderedMapIterator.next()

Moves the cursor to the next key/value pair

Operators

operator[]

Spice
public f<V&> operator[]<K, V>(UnorderedMap<K, V>& map, const K& key)

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
public inline p operator++<K, V>(UnorderedMapIterator<K, V>& it)

Advances the cursor by one

Parameters

Name Type Description
it UnorderedMapIterator<K, V>& UnorderedMapIterator