Module imm/set

imm/set

Persistent immutable hash set using a Hash Array Mapped Trie (HAMT).

Set(T) is a thin wrapper around Map(T, bool), backed by atomic object nodes for thread-safe structural sharing. All mutations return new sets, leaving the original unchanged.

Elements must implement Eq, Hash, and Send.

Examples

{ Set } :: import "std/imm/set";

s := Set(i32).new();
s = s.insert(i32(1));
s = s.insert(i32(2));
assert(s.contains(i32(1)), "set contains 1");
assert((s.len() == usize(2)), "two elements");

Types

Set type-function
fn(comptime(T) : Type) -> (comptime(fn_return_yoe5187c8f_id_17) : Type)

Persistent immutable hash set backed by a HAMT.

Wraps Map(T, bool) internally. The bool values are always true.

Type Parameters

NameTypeNotes
TTypecomptime

Trait Implementations

Eq
impl(forall(T : Type), where(T <: (Eq(T), Hash, Send)), Set(T), ...)
new : (fn() -> Self)

Create an empty set.

Returns: Self

len : (fn(self: Self) -> usize)

Number of elements.

Returns: usize

is_empty : (fn(self: Self) -> bool)

Check if the set is empty.

Returns: bool

contains : (fn(self: Self, elem: T) -> bool)

Check if the set contains elem.

Returns: bool

insert : (fn(self: Self, elem: T) -> Self)

Return a new set with elem added.

Returns: Self

remove : (fn(self: Self, elem: T) -> Self)

Return a new set with elem removed.

Returns: Self

union : (fn(self: Self, other: Self) -> Self)

Return the union of two sets (self ∪ other).

Returns: Self

intersection : (fn(self: Self, other: Self) -> Self)

Return the intersection of two sets (self ∩ other).

Returns: Self

difference : (fn(self: Self, other: Self) -> Self)

Return the difference (self \ other).

Returns: Self

is_subset : (fn(self: Self, other: Self) -> bool)

Check if self is a subset of other.

Returns: bool

is_disjoint : (fn(self: Self, other: Self) -> bool)

Check if two sets are disjoint.

Returns: bool

to_list : (fn(self: Self) -> List(T))

Collect all elements into a List.

Returns: List(T)

from_slice : (fn(s: Slice(T)) -> Self)

Create a set from a slice of elements.

Returns: Self

impl(forall(T : Type), where(T <: (Eq(T), Hash, Send)), Set(T), Eq(Set(T))(...))