Skip to content

Set

1 min read

Import: import system.collections;

Set

A generic open-addressing hash set.

public class Set<T>: Collection<T>

constructor

Allocates an empty set with an initial capacity (defaults to 8, rounded up to power of 2).

public constructor(capacity: int = 8)

reserve

Reserves additional live entries, accounting for load factor and reclaiming tombstones.

public fun reserve(additional: int): Result<Unit, CapacityError>

length

Number of elements currently stored.

public get length(): int

capacity

Current capacity of the backing buffer.

public get capacity(): int

add

Inserts a value into the set. Returns true if it was newly added, false if already present.

public fun add(value: T): bool

contains

True if the set contains the value.

public fun contains(borrow value: T): bool

contains

True when the set holds the string equal to value, without building a string.

public fun contains(borrow value: ReadOnlySpan<char>): bool where T: StringKey

add_all

Adds every element of items, ignoring duplicates.

public fun add_all(borrow items: T[]): void

constructor

Creates a set containing the distinct elements of items. Set literals such as {1, 2, 3} create sets in the same way.

public constructor(borrow items: T[])

remove

Removes a value and returns true if it was removed.

public fun remove(borrow value: T): bool

remove

Removes value; returns whether it was present.

public fun remove(borrow value: T): bool where T: unmanaged

is_empty

True if the set is empty.

public fun is_empty(): bool

clear

Removes all elements, keeping the current capacity. Occupied key slots are zeroed so managed entries release; probe states are reset in place (no capacity realloc).

public fun clear(): void

clear

Removes all entries while keeping storage for reuse.

public fun clear(): void where T: unmanaged

to_array

Gets all elements as an array.

public fun to_array(): T[]

union

Elements present in this set or other.

public fun union(borrow other: Set<T>): Set<T>

intersection

Elements present in both sets.

public fun intersection(borrow other: Set<T>): Set<T>

difference

Elements in this set but not in other.

public fun difference(borrow other: Set<T>): Set<T>

is_subset

True when every element of this set is in other.

public fun is_subset(borrow other: Set<T>): bool

is_disjoint

True when the sets share no elements.

public fun is_disjoint(borrow other: Set<T>): bool

iterator

Gets an iterator for the set.

public fun iterator(): SetIterator<T>

to_string

Returns a text representation of this value.

public override fun to_string(): string