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.
CompareList
List of comparable generics
Example
function : Example() ~ Nil {
# insert elements
list := Collection.CompareList->New()<FloatRef>;
list->AddBack(33.3);
list->AddFront(66.6);
list->AddBack(99.9);
# get size
list->Size()->PrintLine();
# find value
list->Has(66.6)->PrintLine();
# get first and last item
list->Front()->PrintLine();
list->Back()->PrintLine();
# iterate
while(list->More()) {
list->Get()->PrintLine();
list->Next();
};
# iterate backward
backwards := list->BackwardIterator()<FloatRef>;
while(backwards->More()) {
backwards->Get()->PrintLine();
backwards->Next();
};
}Operations
- New
- AddBack
- AddFront
- Back
- BackwardIterator
- Empty
- Filter
- Find
- Forward
- ForwardIterator
- Front
- Get
- Has
- Insert
- IsBack
- IsEmpty
- IsFront
- Map
- More
- Next
- Previous
- Reduce
- Remove
- RemoveBack
- RemoveFront
- Rest
- Rewind
- Size
- ToArray
- ToString
AddBack # native
Adds a value to the end
method : public : native : AddBack(value:H) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| value | H | value to append |
Example
list := Collection.CompareList->New()<IntRef>;
list->AddBack(IntRef->New(5));
list->AddBack(IntRef->New(10));
list->Back()->PrintLine(); # 10AddFront # native
Adds a value to the front
method : public : native : AddFront(value:H) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| value | H | value to prepend |
Example
list := Collection.CompareList->New()<IntRef>;
list->AddBack(IntRef->New(2));
list->AddFront(IntRef->New(1));
list->Front()->PrintLine(); # 1Back #
Returns the last element in the list
method : public : Back() ~ HReturn
| Type | Description |
|---|---|
| H | last element in the list, Nil if the list is empty |
Example
list := Collection.CompareList->New()<IntRef>;
list->AddBack(IntRef->New(10));
list->AddBack(IntRef->New(20));
list->Back()->PrintLine(); # 20BackwardIterator #
Instance of a backward iterator
method : public : BackwardIterator() ~ CompareBackwardIterator<H>Return
| Type | Description |
|---|---|
| CompareBackwardIterator<H> | Backward iterator |
Example
list := Collection.CompareList->New()<IntRef>;
list->AddBack(IntRef->New(1));
list->AddBack(IntRef->New(2));
it := list->BackwardIterator()<IntRef>;
while(it->More()) {
it->Get()->PrintLine();
it->Next();
};Empty #
Clears the list
method : public : Empty() ~ NilExample
list := Collection.CompareList->New()<IntRef>;
list->AddBack(IntRef->New(1));
list->Empty();
list->IsEmpty()->PrintLine(); # trueFilter #
Uses the given function to filter out values
method : public : Filter(f:(H)~Bool) ~ CompareList<H>Parameters
| Name | Type | Description |
|---|---|---|
| f | (H)~Bool | function to use a filter. If the function evaluates to true the value is added to the collection. |
Return
| Type | Description |
|---|---|
| CompareList<H> | filtered list |
Example
list := Collection.CompareList->New()<IntRef>;
list->AddBack(IntRef->New(1));
list->AddBack(IntRef->New(2));
list->AddBack(IntRef->New(3));
evens := list->Filter(\(IntRef) ~ Bool : (v) => v->Get() % 2 = 0);
evens->Size()->PrintLine(); # 1Find #
Finds a value in the list and sets the pointer
method : public : Find(value:H) ~ HParameters
| Name | Type | Description |
|---|---|---|
| value | H | value to search for |
Return
| Type | Description |
|---|---|
| H | value that's found |
Example
list := Collection.CompareList->New()<IntRef>;
list->AddBack(IntRef->New(1));
list->AddBack(IntRef->New(2));
found := list->Find(IntRef->New(2));
found->PrintLine(); # 2Forward #
Moves the pointer to the end of the list
method : public : Forward() ~ NilExample
list := Collection.CompareList->New()<IntRef>;
list->AddBack(IntRef->New(1));
list->AddBack(IntRef->New(2));
list->Forward();
list->Get()->PrintLine(); # 2ForwardIterator #
Instance of a forward iterator
method : public : ForwardIterator() ~ CompareForwardIterator<H>Return
| Type | Description |
|---|---|
| CompareForwardIterator<H> | forward iterator |
Example
list := Collection.CompareList->New()<IntRef>;
list->AddBack(IntRef->New(1));
list->AddBack(IntRef->New(2));
it := list->ForwardIterator()<IntRef>;
while(it->More()) {
it->Get()->PrintLine();
it->Next();
};Front #
Returns the first element in the list
method : public : Front() ~ HReturn
| Type | Description |
|---|---|
| H | first element in the list, Nil if the list is empty |
Example
list := Collection.CompareList->New()<IntRef>;
list->AddBack(IntRef->New(10));
list->AddBack(IntRef->New(20));
list->Front()->PrintLine(); # 10Get #
Gets the value that's currently pointed to
method : public : Get() ~ HReturn
| Type | Description |
|---|---|
| H | value value |
Example
list := Collection.CompareList->New()<IntRef>;
list->AddBack(IntRef->New(42));
list->Rewind();
list->Get()->PrintLine(); # 42Has #
Searches for a value
method : public : Has(value:H) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| value | H | value to check for |
Return
| Type | Description |
|---|---|
| Bool | true if value is found, false otherwise |
Example
list := Collection.CompareList->New()<IntRef>;
list->AddBack(IntRef->New(5));
list->AddBack(IntRef->New(10));
list->Has(IntRef->New(5))->PrintLine(); # true
list->Has(IntRef->New(99))->PrintLine(); # falseInsert # native
Inserts a value into the list based upon the pointer location
method : public : native : Insert(value:H) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| value | H | value to insert |
Example
list := Collection.CompareList->New()<IntRef>;
list->AddBack(IntRef->New(1));
list->AddBack(IntRef->New(3));
list->Rewind();
list->Insert(IntRef->New(2));
list->Size()->PrintLine(); # 3IsBack #
Checks to see if the pointer is at the end of the list
method : public : IsBack() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if pointer is at the end of the list, false otherwise |
Example
list := Collection.CompareList->New()<IntRef>;
list->AddBack(IntRef->New(1));
list->AddBack(IntRef->New(2));
list->Forward();
list->IsBack()->PrintLine(); # trueIsEmpty #
Checks to see if the list is empty
method : public : IsEmpty() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if empty, false otherwise |
Example
list := Collection.CompareList->New()<IntRef>;
list->IsEmpty()->PrintLine(); # true
list->AddBack(IntRef->New(1));
list->IsEmpty()->PrintLine(); # falseIsFront #
Checks to see if the pointer is at the front of the list
method : public : IsFront() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if pointer is at the front of the list, false otherwise |
Example
list := Collection.CompareList->New()<IntRef>;
list->AddBack(IntRef->New(1));
list->AddBack(IntRef->New(2));
list->Rewind();
list->IsFront()->PrintLine(); # true
list->Next();
list->IsFront()->PrintLine(); # falseMap #
Maps the given function to each value in the list
method : public : Map<R : Compare>(f:(H)~R) ~ CompareList<R>Parameters
| Name | Type | Description |
|---|---|---|
| f | (H)~R | function to apply |
Return
| Type | Description |
|---|---|
| CompareList<R> | newly calculated list |
Example
list := Collection.CompareList->New()<IntRef>;
list->AddBack(IntRef->New(3));
list->AddBack(IntRef->New(4));
doubled := list->Map(\(IntRef) ~ IntRef : (v) => IntRef->New(v->Get() * 2));
doubled->Size()->PrintLine(); # 2More #
Checks to see the pointer can be advanced
method : public : More() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if pointer can be advanced, false otherwise |
Example
list := Collection.CompareList->New()<IntRef>;
list->AddBack(IntRef->New(1));
list->Rewind();
while(list->More()) {
list->Get()->PrintLine();
list->Next();
};New # constructor
Default constructor
New()Example
list := Collection.CompareList->New()<IntRef>;
list->AddBack(IntRef->New(1));
list->AddBack(IntRef->New(2));
list->Size()->PrintLine(); # 2Next #
Advances the pointer
method : public : Next() ~ NilExample
list := Collection.CompareList->New()<IntRef>;
list->AddBack(IntRef->New(1));
list->AddBack(IntRef->New(2));
list->Rewind();
list->Next();
list->Get()->PrintLine(); # 2Previous #
Retreats the pointer
method : public : Previous() ~ NilExample
list := Collection.CompareList->New()<IntRef>;
list->AddBack(IntRef->New(1));
list->AddBack(IntRef->New(2));
list->Forward();
list->Previous();
list->Get()->PrintLine(); # 1Reduce #
Uses the given function to reduce the values
method : public : Reduce(a:H, f:(H,H)~H) ~ HParameters
| Name | Type | Description |
|---|---|---|
| a | H | initial value (i.e. accumulator) |
| f | (H,H)~H | function to use a reduce |
Return
| Type | Description |
|---|---|
| H | reduced vector |
Example
list := Collection.CompareList->New()<IntRef>;
list->AddBack(IntRef->New(1));
list->AddBack(IntRef->New(2));
list->AddBack(IntRef->New(3));
sum := list->Reduce(IntRef->New(0), \(IntRef, IntRef) ~ IntRef : (a, v) => IntRef->New(a->Get() + v->Get()));
sum->PrintLine(); # 6Remove # native
Removes the element at the pointer position
method : public : native : Remove() ~ NilExample
list := Collection.CompareList->New()<IntRef>;
list->AddBack(IntRef->New(1));
list->AddBack(IntRef->New(2));
list->Rewind();
list->Remove();
list->Size()->PrintLine(); # 1Remove # native
Removes the element at the pointer position
method : public : native : Remove(node:CompareListNode<H>) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| node | CompareListNode<H> | node |
RemoveBack #
Removes the last value from the list
method : public : RemoveBack() ~ NilExample
list := Collection.CompareList->New()<IntRef>;
list->AddBack(IntRef->New(1));
list->AddBack(IntRef->New(2));
list->RemoveBack();
list->Back()->PrintLine(); # 1RemoveFront #
Removes the first value from the list
method : public : RemoveFront() ~ NilExample
list := Collection.CompareList->New()<IntRef>;
list->AddBack(IntRef->New(1));
list->AddBack(IntRef->New(2));
list->RemoveFront();
list->Front()->PrintLine(); # 2Rest #
List of all but first element
method : public : Rest() ~ CompareList<H>Return
| Type | Description |
|---|---|
| CompareList<H> | all but first element |
Example
list := Collection.CompareList->New()<IntRef>;
list->AddBack(IntRef->New(1));
list->AddBack(IntRef->New(2));
list->AddBack(IntRef->New(3));
rest := list->Rest();
rest->Size()->PrintLine(); # 2Rewind #
Moves the pointer to the start of the list
method : public : Rewind() ~ NilExample
list := Collection.CompareList->New()<IntRef>;
list->AddBack(IntRef->New(1));
list->AddBack(IntRef->New(2));
list->Forward();
list->Rewind();
list->Get()->PrintLine(); # 1Size #
Size of list
method : public : Size() ~ IntReturn
| Type | Description |
|---|---|
| Int | size of list |
Example
list := Collection.CompareList->New()<IntRef>;
list->AddBack(IntRef->New(1));
list->AddBack(IntRef->New(2));
list->Size()->PrintLine(); # 2ToArray #
Converts the list into an object array
method : public : ToArray() ~ H[]Return
| Type | Description |
|---|---|
| H[] | object array |
Example
list := Collection.CompareList->New()<IntRef>;
list->AddBack(IntRef->New(10));
list->AddBack(IntRef->New(20));
arr := list->ToArray();
arr[0]->PrintLine(); # 10ToString #
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
list := Collection.CompareList->New()<IntRef>;
list->AddBack(IntRef->New(1));
list->AddBack(IntRef->New(2));
list->ToString()->PrintLine(); # [1,2]