Skip to content

Map

Spice
import "std/data/map";

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

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
public p Map.remove(const K& key)

Removes a key value pair from the map.

Parameters

Name Type Description
key const K& The key to remove.

get

Spice
public f<V&> Map.get(const K& key)

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
public f<Result<V>> Map.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

contains

Spice
public f<bool> Map.contains(const K& key)

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

Gets the number of elements in the map.

Returns: unsigned long — The number of elements in the map.

isEmpty

Spice
public f<bool> Map.isEmpty()

Checks if the map is empty.

Returns: bool — True if the map is empty, false otherwise.

clear

Spice
public p Map.clear()

Removes all elements from the map.

getIterator

Spice
public f<MapIterator<K, V>> Map.getIterator()

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
public p MapIterator.ctor<K, V>(Map<K, V>& map)

Construct a map iterator over the given map

Parameters

Name Type Description
map Map<K, V>& Map to iterate over

Methods

get

Spice
public inline f<Pair<const K&, V&>&> MapIterator.get()

Returns the current key-value pair of the map

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

getIdx

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

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

Check if the iterator is valid

Returns: bool — true or false

next

Spice
public inline p MapIterator.next()

Moves the cursor to the next key/value pair

Operators

operator[]

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

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

Advances the cursor by one

Parameters

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