Module: collections.set

Source: ./collections/set.reef


Overview

collections/set - Generic Set implementation

Provides a dynamic set that can hold any number of elements (unlike setof which is limited to 32 elements). Uses linear search for simplicity. Capacity grows automatically on set_add when full (double capacity, min 1). set_add returns false only if the value already exists (or on hard failure) — not because the set was full. set_try_add returns Result[bool, Error]: Ok(true) added, Ok(false) already present, Err on hard failure after grow retries.

Usage: import collections.set let s = create_int_set() set_add[:int](s, 42) if set_contains[:int](s, 42) println("found!") end if

Factory functions: create_int_set() -> Set[int] create_string_set() -> Set[string] create_float_set() -> Set[float] create_bool_set() -> Set[bool]

create_*_set_cap(capacity) sets the initial capacity; the set still grows.


Types

Set

Fields:

Name Type
elements [T]
count int
cap int

Functions

fn create_int_set(): Set[int]

fn create_int_set_cap(cap: int): Set[int]

fn create_string_set(): Set[string]

fn create_string_set_cap(cap: int): Set[string]

fn create_float_set(): Set[float]

fn create_bool_set(): Set[bool]

fn set_try_add(s: Set[T], value: T): result.Result[bool, error.Error]

Result-shaped add. Ok(true) = newly inserted, Ok(false) = already present, Err = hard failure after grow retries.

fn set_add(s: Set[T], value: T): bool

Add an element to the set. Auto-grows when full. Returns true if added, false if already present (or on hard failure) — not because the set was full. Compatibility wrapper over set_try_add.

fn set_remove(s: Set[T], value: T): bool

Remove an element from the set Returns true if removed, false if not found

fn set_contains(s: Set[T], value: T): bool

Check if the set contains an element

fn set_size(s: Set[T]): int

Get the number of elements in the set

fn set_capacity(s: Set[T]): int

Get the capacity of the set

fn set_is_empty(s: Set[T]): bool

Check if the set is empty

fn set_to_array(s: Set[T], result: [T], max: int): int

Copy elements to an array Returns the number of elements copied

fn set_union_int(a: Set[int], b: Set[int]): Set[int]

Union: elements in either a or b Uses set_try_add (Result) as the insert path exemplar; growable sets should not Err.

fn set_intersection_int(a: Set[int], b: Set[int]): Set[int]

Intersection: elements in both a and b

fn set_difference_int(a: Set[int], b: Set[int]): Set[int]

Difference: elements in a but not in b

fn set_is_subset_int(a: Set[int], b: Set[int]): bool

Subset: all elements of a are in b

fn string_set_contains(s: Set[string], value: string): bool

Helper: check if string set contains a value (uses strcmp for comparison)

fn set_union_string(a: Set[string], b: Set[string]): Set[string]

Union: elements in either a or b

fn set_intersection_string(a: Set[string], b: Set[string]): Set[string]

Intersection: elements in both a and b

fn set_difference_string(a: Set[string], b: Set[string]): Set[string]

Difference: elements in a but not in b

fn set_is_subset_string(a: Set[string], b: Set[string]): bool

Subset: all elements of a are in b


Procedures

proc grow_set(s: Set[T])

Grow backing storage to at least 2x current capacity (min 1).

proc set_clear(s: Set[T])

Remove all elements from the set


Generated by reefc doc