v2026.11.3
All Bundles
Bundle

Generic collections library: Vector (dynamic array), List (linked list), Map (ordered binary tree, O(log n)), Hash (hash table), Set, HashSet, SetHash, MultiMap, MultiHash, Stack, Queue, and Pair. Provides typed iteration, sorting, filtering, and functional operations (Reduce, Any, All). Compile with -lib gen_collect.

UnionFind

Union-find (disjoint set) over the integers 0..n-1, with union by size and path halving: Find and Union run in near-constant (inverse Ackermann) amortised time.

Operations

Add #

Adds a new element in its own set

method : public : Add() ~ Int

Return

TypeDescription
Intthe new element's id

Example

uf := Collection.UnionFind->New(2);
uf->Add()->PrintLine(); # 2

Connected #

Checks whether two elements are in the same set

method : public : Connected(a:Int, b:Int) ~ Bool

Parameters

NameTypeDescription
aIntfirst element id
bIntsecond element id

Return

TypeDescription
Booltrue if connected (false for an id out of range)

Count #

Gets the number of disjoint sets

method : public : Count() ~ Int

Return

TypeDescription
Intset count

Find #

Finds the representative of the set holding an element

method : public : Find(x:Int) ~ Int

Parameters

NameTypeDescription
xIntelement id

Return

TypeDescription
Intrepresentative id, or -1 for an id out of range

Example

uf := Collection.UnionFind->New(3);
uf->Union(0, 2);
(uf->Find(0) = uf->Find(2))->PrintLine(); # true

New # constructor

Creates n singleton sets, one per element

New(n:Int)

Parameters

NameTypeDescription
nIntnumber of elements (ids 0 to n-1)

Example

uf := Collection.UnionFind->New(5);
uf->Union(0, 1); uf->Union(3, 4);
uf->Count()->PrintLine(); # 3

Size #

Gets the number of elements

method : public : Size() ~ Int

Return

TypeDescription
Intelement count

SizeOf #

Gets the size of the set holding an element

method : public : SizeOf(x:Int) ~ Int

Parameters

NameTypeDescription
xIntelement id

Return

TypeDescription
Intnumber of elements in the set, 0 for an id out of range

Union #

Merges the sets holding two elements

method : public : Union(a:Int, b:Int) ~ Bool

Parameters

NameTypeDescription
aIntfirst element id
bIntsecond element id

Return

TypeDescription
Booltrue if two sets were merged, false if already together or an id is out of range

Example

uf := Collection.UnionFind->New(3);
uf->Union(0, 1)->PrintLine(); # true
uf->Union(0, 1)->PrintLine(); # false