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.

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) ~ Bool

Parameters

NameTypeDescription
indexIntslot (0 to Size()-1)
deltaIntamount to add

Return

TypeDescription
Booltrue if added, false for an index out of range

Example

f := Collection.FenwickTree->New(4);
f->Add(0, 3)->PrintLine(); # true

Get #

Gets the value of one slot

method : public : Get(index:Int) ~ Int

Parameters

NameTypeDescription
indexIntslot

Return

TypeDescription
Intvalue, 0 for an index out of range

New # constructor

Creates a tree of n zeros

New(n:Int)

Parameters

NameTypeDescription
nIntnumber of slots (at least 1)

Example

f := Collection.FenwickTree->New(5);
f->Add(1, 10); f->Add(3, 5);
f->Sum(3)->PrintLine(); # 15

RangeSum #

Sums an inclusive range of slots

method : public : RangeSum(lo:Int, hi:Int) ~ Int

Parameters

NameTypeDescription
loIntfirst slot
hiIntlast slot

Return

TypeDescription
Intsum, 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(); # 5

Set #

Sets one slot to a value

method : public : Set(index:Int, value:Int) ~ Bool

Parameters

NameTypeDescription
indexIntslot
valueIntnew value

Return

TypeDescription
Booltrue if set, false for an index out of range

Size #

Gets the number of slots

method : public : Size() ~ Int

Return

TypeDescription
Intsize

Sum #

Sums the slots from 0 up to and including an index

method : public : Sum(index:Int) ~ Int

Parameters

NameTypeDescription
indexIntlast slot (values past the end are clamped, below 0 gives 0)

Return

TypeDescription
Intsum

Example

f := Collection.FenwickTree->New(4);
f->Add(0, 1); f->Add(2, 4);
f->Sum(2)->PrintLine(); # 5