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.
FenwickTree
Fenwick tree (binary indexed tree) of Int values: point updates and prefix or range sums, both O(log n), in n slots of memory. Indexes are 0-based.
Operations
Add #
Adds to one slot
method : public : Add(index:Int, delta:Int) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| index | Int | slot (0 to Size()-1) |
| delta | Int | amount to add |
Return
| Type | Description |
|---|---|
| Bool | true if added, false for an index out of range |
Example
f := Collection.FenwickTree->New(4);
f->Add(0, 3)->PrintLine(); # trueGet #
Gets the value of one slot
method : public : Get(index:Int) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| index | Int | slot |
Return
| Type | Description |
|---|---|
| Int | value, 0 for an index out of range |
New # constructor
Creates a tree of n zeros
New(n:Int)Parameters
| Name | Type | Description |
|---|---|---|
| n | Int | number of slots (at least 1) |
Example
f := Collection.FenwickTree->New(5);
f->Add(1, 10); f->Add(3, 5);
f->Sum(3)->PrintLine(); # 15RangeSum #
Sums an inclusive range of slots
method : public : RangeSum(lo:Int, hi:Int) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| lo | Int | first slot |
| hi | Int | last slot |
Return
| Type | Description |
|---|---|
| Int | sum, 0 if the range is empty |
Example
f := Collection.FenwickTree->New(5);
f->Add(1, 2); f->Add(2, 3); f->Add(4, 9);
f->RangeSum(1, 2)->PrintLine(); # 5Set #
Sets one slot to a value
method : public : Set(index:Int, value:Int) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| index | Int | slot |
| value | Int | new value |
Return
| Type | Description |
|---|---|
| Bool | true if set, false for an index out of range |
Sum #
Sums the slots from 0 up to and including an index
method : public : Sum(index:Int) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| index | Int | last slot (values past the end are clamped, below 0 gives 0) |
Return
| Type | Description |
|---|---|
| Int | sum |
Example
f := Collection.FenwickTree->New(4);
f->Add(0, 1); f->Add(2, 4);
f->Sum(2)->PrintLine(); # 5