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.
Heap
Generic binary heap
Example
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() ~ IntReturn
| Type | Description |
|---|---|
| Int | heap 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) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| value | H | value 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(); # 2IsEmpty #
Checks to see if the heap is empty
method : public : IsEmpty() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true 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(); # falseNew # constructor
Constructor
New(array:H[], order:Heap->Order)Parameters
| Name | Type | Description |
|---|---|---|
| array | H[] | array of values to insert |
| order | Heap->Order | sort 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(); # 2New # constructor
Constructor
New(order:Heap->Order)Parameters
| Name | Type | Description |
|---|---|---|
| order | Heap->Order | sort order |
Example
heap := Collection.Heap->New(Collection.Heap->Order->MAX)<IntRef>;
heap->Insert(IntRef->New(5));
heap->Insert(IntRef->New(9));
heap->Pop()->PrintLine(); # 9New # constructor
Constructor that orders the values with a comparator instead of their Compare method
New(order:Heap->Order, compare:(H,H)~Int)Parameters
| Name | Type | Description |
|---|---|---|
| order | Heap->Order | sort order: MIN pops the value the comparator ranks lowest first |
| compare | (H,H)~Int | comparator: negative if the first value ranks lower, 0 if equal, positive if higher |
Example
# longest string first
heap := Collection.Heap->New(Collection.Heap->Order->MAX,
\(String, String) ~ Int : (a, b) => a->Size() - b->Size())<String>;
heap->Insert("fig"); heap->Insert("banana"); heap->Insert("kiwi");
heap->Pop()->PrintLine(); # bananaNew # constructor
Constructor for a min-heap ordered by a comparator: Pop returns the value the comparator ranks lowest
New(compare:(H,H)~Int)Parameters
| Name | Type | Description |
|---|---|---|
| compare | (H,H)~Int | comparator: negative if the first value ranks lower, 0 if equal, positive if higher |
Example
heap := Collection.Heap->New(\(IntRef, IntRef) ~ Int : (a, b) => b->Get() - a->Get())<IntRef>;
heap->Insert(IntRef->New(1)); heap->Insert(IntRef->New(5));
heap->Pop()->PrintLine(); # 5: the comparator reverses the orderPeek #
Returns the value Pop would return, without removing it; the same as Top
method : public : Peek() ~ HReturn
| Type | Description |
|---|---|
| H | smallest or largest value, Nil if the heap is empty |
Example
heap := Collection.Heap->New(Collection.Heap->Order->MIN)<IntRef>;
heap->Insert(IntRef->New(5)); heap->Insert(IntRef->New(2));
heap->Peek()->PrintLine(); # 2
heap->Size()->PrintLine(); # 2Pop #
Pop either the smallest or largest value depending upon the sort order
method : public : Pop() ~ HReturn
| Type | Description |
|---|---|
| H | smallest 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(); # 4Pop #
Searches and pops the given value
method : public : Pop(value:H) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| value | H | value to remove |
Return
| Type | Description |
|---|---|
| Bool | true 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(); # 1Size #
Size of heap, including Nil values
method : public : Size() ~ IntReturn
| Type | Description |
|---|---|
| Int | size 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(); # 2ToArray #
Returns a list of non-Nil values
method : public : ToArray() ~ H[]Return
| Type | Description |
|---|---|
| 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 elementToString #
Formats the collection into a string. If an element implements the 'Stringify' interface, it's 'ToString()' is called.
method : public : ToString() ~ StringReturn
| Type | Description |
|---|---|
| String | string 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() ~ HReturn
| Type | Description |
|---|---|
| H | value 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