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() ~ IntReturn
| Type | Description |
|---|---|
| Int | the new element's id |
Example
uf := Collection.UnionFind->New(2);
uf->Add()->PrintLine(); # 2Connected #
Checks whether two elements are in the same set
method : public : Connected(a:Int, b:Int) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| a | Int | first element id |
| b | Int | second element id |
Return
| Type | Description |
|---|---|
| Bool | true if connected (false for an id out of range) |
Count #
Gets the number of disjoint sets
method : public : Count() ~ IntReturn
| Type | Description |
|---|---|
| Int | set count |
Find #
Finds the representative of the set holding an element
method : public : Find(x:Int) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| x | Int | element id |
Return
| Type | Description |
|---|---|
| Int | representative 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(); # trueNew # constructor
Creates n singleton sets, one per element
New(n:Int)Parameters
| Name | Type | Description |
|---|---|---|
| n | Int | number of elements (ids 0 to n-1) |
Example
uf := Collection.UnionFind->New(5);
uf->Union(0, 1); uf->Union(3, 4);
uf->Count()->PrintLine(); # 3Size #
Gets the number of elements
method : public : Size() ~ IntReturn
| Type | Description |
|---|---|
| Int | element count |
SizeOf #
Gets the size of the set holding an element
method : public : SizeOf(x:Int) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| x | Int | element id |
Return
| Type | Description |
|---|---|
| Int | number 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) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| a | Int | first element id |
| b | Int | second element id |
Return
| Type | Description |
|---|---|
| Bool | true 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