Skip to content

Set

Spice
import "std/data/set";

Set<V> struct

Implements: IIterable<V>

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

Time complexity:
Insert: O(log n)
Delete: O(log n)
Lookup: O(log n)

Methods

insert

Spice
public p Set.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> Set.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 Set.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 Set.clear()

Clear all values from the set.

getSize

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

Get the number of elements in the set.

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

isEmpty

Spice
public f<bool> Set.isEmpty()

Check if the set is empty.

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

toLinkedList

Spice
public f<LinkedList<V>> Set.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<SetIterator<V>> Set.getIterator()

Retrieve a forward iterator for the set

Returns: SetIterator<V>

SetIterator<V> struct

Implements: IIterator<const V&>

Iterator to iterate over an set data structure

Constructors

ctor

Spice
public p SetIterator.ctor<V>(Set<V>& set)

Construct a set iterator over the given set

Parameters

Name Type Description
set Set<V>& Set to iterate over

Methods

get

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

Returns the current value of the set

Returns: const V& — Current value

getIdx

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

Returns the current index and the current item of the set

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

isValid

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

Check if the iterator is valid

Returns: bool — true or false

next

Spice
public inline p SetIterator.next()

Moves the cursor to the next key/value pair

Operators

operator++

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

Advances the cursor by one

Parameters

Name Type Description
it SetIterator<V>& SetIterator