Skip to content

Unordered Set

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

UnorderedSet<V> struct

Implements: IIterable<V>

An unordered set in Spice is a commonly used data structure, which can be used to represent a list of unique values.

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

Construct an empty unordered set with the given number of buckets

Parameters

Name Type Description
bucketCount unsigned long Number of buckets in the underlying hash table (default: 100l)

Methods

insert

Spice
public p UnorderedSet.insert(const V& value)

Insert a value into the set. If the value already exists, nothing happens.

Parameters

Name Type Description
value const V& The value to insert

contains

Spice
public f<bool> UnorderedSet.contains(const V& value)

Check if the set contains the given value.

Parameters

Name Type Description
value const V& The value to check

Returns: bool — true if the value is in the set, false otherwise

remove

Spice
public p UnorderedSet.remove(const V& value)

Remove a value from the set. If the value does not exist, nothing happens.

Parameters

Name Type Description
value const V& The value to remove

clear

Spice
public p UnorderedSet.clear()

Clear all values from the set.

getSize

Spice
public f<unsigned long> UnorderedSet.getSize()

Get the number of elements in the set.

Returns: unsigned long — The number of elements in the set

isEmpty

Spice
public f<bool> UnorderedSet.isEmpty()

Check if the set is empty.

Returns: bool — true if the set is empty, false otherwise

toLinkedList

Spice
public f<LinkedList<V>> UnorderedSet.toLinkedList()

Get all elements in the set as a list.

Returns: LinkedList<V> — A linked list of all elements in the set

getIterator

Spice
public f<UnorderedSetIterator<V>> UnorderedSet.getIterator()

Retrieve a forward iterator for the unordered set

Returns: UnorderedSetIterator<V>

UnorderedSetIterator<V> struct

Implements: IIterator<const V&>

Iterator to iterate over an unordered set data structure

Constructors

ctor

Spice
public p UnorderedSetIterator.ctor<V>(UnorderedSet<V>& unorderedSet)

Construct an unordered set iterator over the given unordered set

Parameters

Name Type Description
unorderedSet UnorderedSet<V>& Unordered set to iterate over

Methods

get

Spice
public inline f<const V&> UnorderedSetIterator.get()

Returns the current value of the unordered set

Returns: const V& — Current value

getIdx

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

Returns the current index and the current item of the unordered set

Returns: Pair<unsigned long, const V&> — Pair of current index and current key/value pair

isValid

Spice
public inline f<bool> UnorderedSetIterator.isValid()

Check if the iterator is valid

Returns: bool — true or false

next

Spice
public inline p UnorderedSetIterator.next()

Moves the cursor to the next key/value pair

Operators

operator++

Spice
public inline p operator++<V>(UnorderedSetIterator<V>& it)

Advances the cursor by one

Parameters

Name Type Description
it UnorderedSetIterator<V>& UnorderedSetIterator