v2026.5.3
All Bundles
Bundle Generic collections library: Vector (dynamic array), Map (hash map), Set, MultiMap, Stack, Queue, and Pair. Provides typed iteration, sorting, filtering, and functional operations (Reduce, Any, All). Compile with -lib gen_collect.

Heap

Generic binary heap

heap := Collection.Heap->New(Collection.Heap->Order->MIN)<IntRef>;
        
heap->Insert(IntRef->New(3));
heap->Insert(IntRef->New(1));
heap->Insert(IntRef->New(9));
heap->Insert(IntRef->New(8));
heap->Insert(IntRef->New(6));
heap->Insert(IntRef->New(2));

while(<>heap->IsEmpty()) {
   heap->Pop()->PrintLine();
};

Operations

Capacity #

Returns the heap capacity

method : public : Capacity() ~ Int

Return

TypeDescription
Intheap capacity

Example

heap := Collection.Heap->New(Collection.Heap->Order->MIN)<IntRef>;
heap->Capacity()->PrintLine(); # initial capacity (16)

Insert #

Inserts a value into the heap and extends the heap size if necessary

method : public : Insert(value:H) ~ Nil

Parameters

NameTypeDescription
valueHvalue to insert

Example

heap := Collection.Heap->New(Collection.Heap->Order->MIN)<IntRef>;
heap->Insert(IntRef->New(7));
heap->Insert(IntRef->New(2));
heap->Insert(IntRef->New(5));
heap->Top()->PrintLine(); # 2

IsEmpty #

Checks to see if the heap is empty

method : public : IsEmpty() ~ Bool

Return

TypeDescription
Booltrue if empty, false otherwise

Example

heap := Collection.Heap->New(Collection.Heap->Order->MIN)<IntRef>;
heap->IsEmpty()->PrintLine(); # true
heap->Insert(IntRef->New(1));
heap->IsEmpty()->PrintLine(); # false

New # constructor

Constructor

New(array:H[], order:Heap->Order)

Parameters

NameTypeDescription
arrayH[]array of values to insert
orderHeap->Ordersort order

Example

values := IntRef->New[3];
values[0] := 5; values[1] := 2; values[2] := 8;
heap := Collection.Heap->New(values, Collection.Heap->Order->MIN)<IntRef>;
heap->Pop()->PrintLine(); # 2

New # constructor

Constructor

New(order:Heap->Order)

Parameters

NameTypeDescription
orderHeap->Ordersort order

Example

heap := Collection.Heap->New(Collection.Heap->Order->MAX)<IntRef>;
heap->Insert(IntRef->New(5));
heap->Insert(IntRef->New(9));
heap->Pop()->PrintLine(); # 9

Pop #

Pop either the smallest or largest value depending upon the sort order

method : public : Pop() ~ H

Return

TypeDescription
Hsmallest or largest value

Example

heap := Collection.Heap->New(Collection.Heap->Order->MIN)<IntRef>;
heap->Insert(IntRef->New(4));
heap->Insert(IntRef->New(1));
heap->Pop()->PrintLine(); # 1
heap->Pop()->PrintLine(); # 4

Pop #

Searches and pops the given value

method : public : Pop(value:H) ~ Bool

Parameters

NameTypeDescription
valueHvalue to remove

Return

TypeDescription
Booltrue if successful, false otherwise

Example

heap := Collection.Heap->New(Collection.Heap->Order->MIN)<IntRef>;
heap->Insert(IntRef->New(3));
heap->Insert(IntRef->New(7));
heap->Pop(IntRef->New(7))->PrintLine(); # true
heap->Size()->PrintLine();              # 1

Size #

Size of heap, including Nil values

method : public : Size() ~ Int

Return

TypeDescription
Intsize of heap

Example

heap := Collection.Heap->New(Collection.Heap->Order->MIN)<IntRef>;
heap->Insert(IntRef->New(1));
heap->Insert(IntRef->New(2));
heap->Size()->PrintLine(); # 2

ToArray #

Returns a list of non-Nil values

method : public : ToArray() ~ H[]

Return

TypeDescription
H[]list of values

Example

heap := Collection.Heap->New(Collection.Heap->Order->MIN)<IntRef>;
heap->Insert(IntRef->New(3));
heap->Insert(IntRef->New(1));
arr := heap->ToArray();
arr[0]->PrintLine(); # smallest element

ToString #

Formats the collection into a string. If an element implements the 'Stringify' interface, it's 'ToString()' is called.

method : public : ToString() ~ String

Return

TypeDescription
Stringstring representation

Example

heap := Collection.Heap->New(Collection.Heap->Order->MIN)<IntRef>;
heap->Insert(IntRef->New(3));
heap->Insert(IntRef->New(1));
heap->ToString()->PrintLine();

Top #

Check the top of the stack

method : public : Top() ~ H

Return

TypeDescription
Hvalue on the top of stack, Nil if stack is empty

Example

heap := Collection.Heap->New(Collection.Heap->Order->MIN)<IntRef>;
heap->Insert(IntRef->New(5));
heap->Insert(IntRef->New(2));
heap->Top()->PrintLine(); # 2