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.
CompareVector
Growable array of comparable generics
Example
function : Example() ~ Nil {
# insert elements
vector := Collection.CompareVector->New()<IntRef>;
vector->AddBack(4);
vector->AddBack(1);
vector->AddBack(5);
vector->AddBack(9);
vector->AddBack(2);
vector->AddBack(5);
# remove last item
vector->RemoveBack();
# get size
vector->Size()->PrintLine();
# get elements
(vector->Get(0) + vector->Get(1))->PrintLine();
# sort elements
vector->Sort();
# print all items with a loop
each(item := vector) {
item->PrintLine();
};
# print all items with a function
vector->Each(Show(IntRef) ~ Nil);
}
function : Show(value : IntRef) ~ Nil {
value->PrintLine();
}Operations
- New
- AddAll
- AddBack
- All
- Any
- BinarySearch
- Compress
- Count
- Distinct
- Each
- Empty
- Filter
- Find
- FindFirst
- FindIndex
- First
- Get
- Has
- Insert
- IsEmpty
- Last
- Limit
- Map
- Reduce
- Remove
- RemoveBack
- Reverse
- Set
- Size
- Slice
- Sort
- Swap
- ToArray
- ToString
AddAll #
Appends every value of another vector (the values are shared, not copied)
method : public : AddAll(that:CompareVector<H>) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| that | CompareVector<H> | vector to append, may be this vector |
Example
v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1));
v->AddAll(v);
v->Size()->PrintLine(); # 2AddBack #
Adds a value to the end
method : public : AddBack(value:H) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| value | H | value to append |
Example
v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(10));
v->AddBack(IntRef->New(20));
v->Size()->PrintLine(); # 2All #
Checks if all elements match the predicate
method : public : All(f:(H)~Bool) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| f | (H)~Bool | predicate function |
Return
| Type | Description |
|---|---|
| Bool | true if all elements match, false otherwise |
Example
v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(2)); v->AddBack(IntRef->New(4)); v->AddBack(IntRef->New(6));
v->All(\(IntRef) ~ Bool : (x) => x->Get() % 2 = 0)->PrintLine(); # true
v->All(\(IntRef) ~ Bool : (x) => x->Get() > 3)->PrintLine(); # falseAny #
Checks if any element matches the predicate
method : public : Any(f:(H)~Bool) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| f | (H)~Bool | predicate function |
Return
| Type | Description |
|---|---|
| Bool | true if any element matches, false otherwise |
Example
v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(4)); v->AddBack(IntRef->New(9));
v->Any(\(IntRef) ~ Bool : (x) => x->Get() > 5)->PrintLine(); # true
v->Any(\(IntRef) ~ Bool : (x) => x->Get() > 10)->PrintLine(); # falseBinarySearch # native
Performs a binary search O(log n)
method : public : native : BinarySearch(value:H) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| value | H | value to search for |
Return
| Type | Description |
|---|---|
| Int | index of found value, -1 if not found |
Example
v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(3)); v->AddBack(IntRef->New(5));
v->Sort();
v->BinarySearch(IntRef->New(3))->PrintLine(); # 1Compress #
Compresses the Vector freeing unused memory
method : public : Compress() ~ NilExample
v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(2));
v->Compress();
v->Size()->PrintLine(); # 2Count #
Counts elements matching the predicate
method : public : Count(f:(H)~Bool) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| f | (H)~Bool | predicate function |
Return
| Type | Description |
|---|---|
| Int | count of matching elements |
Example
v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(2)); v->AddBack(IntRef->New(3));
v->Count(\(IntRef) ~ Bool : (x) => x->Get() > 1)->PrintLine(); # 2Distinct #
Returns a new vector with duplicate elements removed
method : public : Distinct() ~ CompareVector<H>Return
| Type | Description |
|---|---|
| CompareVector<H> | vector with unique elements only |
Example
v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(2)); v->AddBack(IntRef->New(1));
unique := v->Distinct()<IntRef>;
unique->Size()->PrintLine(); # 2Each #
Function called for each element
method : public : Each(f:(H)~Nil) ~ CompareVector<H>Parameters
| Name | Type | Description |
|---|---|---|
| f | (H)~Nil | function called |
Example
v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(2));
v->Each(\(IntRef) ~ Nil : (n) => n->PrintLine());Empty #
Clears the vector
method : public : Empty() ~ NilExample
v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1));
v->Empty();
v->IsEmpty()->PrintLine(); # trueFilter #
Uses the given function to filter out values
method : public : Filter(f:(H)~Bool) ~ CompareVector<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 |
|---|---|
| CompareVector<H> | filtered vector |
Example
v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(2)); v->AddBack(IntRef->New(3));
evens := v->Filter(\(IntRef) ~ Bool : (x) => x->Get() % 2 = 0)<IntRef>;
evens->Size()->PrintLine(); # 1Find #
Finds a given value in the vector via linear search
method : public : Find(value:H) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| value | H | value to search for |
Return
| Type | Description |
|---|---|
| Int | index of found value, -1 if not found |
Example
v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(10)); v->AddBack(IntRef->New(20));
v->Find(IntRef->New(20))->PrintLine(); # 1
v->Find(IntRef->New(99))->PrintLine(); # -1FindFirst #
Finds the first element matching the predicate
method : public : FindFirst(f:(H)~Bool) ~ HParameters
| Name | Type | Description |
|---|---|---|
| f | (H)~Bool | predicate function |
Return
| Type | Description |
|---|---|
| H | first matching element, or Nil if not found |
Example
v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(3)); v->AddBack(IntRef->New(7)); v->AddBack(IntRef->New(2));
found := v->FindFirst(\(IntRef) ~ Bool : (x) => x->Get() > 4);
found->PrintLine(); # 7FindIndex #
Finds the index of the first element matching the predicate
method : public : FindIndex(f:(H)~Bool) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| f | (H)~Bool | predicate function |
Return
| Type | Description |
|---|---|
| Int | index of first matching element, or -1 if not found |
Example
v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(3)); v->AddBack(IntRef->New(7)); v->AddBack(IntRef->New(2));
v->FindIndex(\(IntRef) ~ Bool : (x) => x->Get() > 4)->PrintLine(); # 1First #
Gets the first value
method : public : First() ~ HReturn
| Type | Description |
|---|---|
| H | first value, or Nil if not set |
Example
v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(5)); v->AddBack(IntRef->New(10));
v->First()->PrintLine(); # 5Get #
Gets an indexed value
method : public : Get(index:Int) ~ HParameters
| Name | Type | Description |
|---|---|---|
| index | Int | index |
Return
| Type | Description |
|---|---|
| H | value |
Example
v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(10)); v->AddBack(IntRef->New(20));
v->Get(0)->PrintLine(); # 10
v->Get(1)->PrintLine(); # 20Has #
Check of the given value is in the vector
method : public : Has(value:H) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| value | H | value to check for |
Return
| Type | Description |
|---|---|
| Bool | true if found, false otherwise |
Example
v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(5)); v->AddBack(IntRef->New(10));
v->Has(IntRef->New(5))->PrintLine(); # true
v->Has(IntRef->New(99))->PrintLine(); # falseInsert #
Inserts a value at an index, shifting the values at and after it up by one
method : public : Insert(index:Int, value:H) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| index | Int | position to insert at (0 to Size(); Size() appends) |
| value | H | value to insert |
Return
| Type | Description |
|---|---|
| Bool | true if inserted, false if the index is out of range |
Example
v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(3));
v->Insert(1, IntRef->New(2))->PrintLine(); # true
v->Size()->PrintLine(); # 3IsEmpty #
Checks to see if the vector is empty
method : public : IsEmpty() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if empty, false otherwise |
Example
v := Collection.CompareVector->New()<IntRef>;
v->IsEmpty()->PrintLine(); # true
v->AddBack(IntRef->New(1));
v->IsEmpty()->PrintLine(); # falseLast #
Gets the last value
method : public : Last() ~ HReturn
| Type | Description |
|---|---|
| H | last value, or Nil if not set |
Example
v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(2));
v->Last()->PrintLine(); # 2Limit #
Returns a limited list
method : public : Limit(l:Int) ~ CompareVector<H>Parameters
| Name | Type | Description |
|---|---|---|
| l | Int | limit |
Return
| Type | Description |
|---|---|
| CompareVector<H> | limited list |
Example
v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(2)); v->AddBack(IntRef->New(3));
first2 := v->Limit(2);
first2->Size()->PrintLine(); # 2Map #
Maps the given function to each value in the vector
method : public : Map(f:(H)~H) ~ CompareVector<H>Parameters
| Name | Type | Description |
|---|---|---|
| f | (H)~H | function to apply |
Return
| Type | Description |
|---|---|
| CompareVector<H> | newly calculated vector |
Example
v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(2));
doubled := v->Map(\(IntRef) ~ IntRef : (n) => IntRef->New(n->Get() * 2));
doubled->Get(0)->PrintLine(); # 2New # constructor
Default constructor
New()Example
v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1));
v->Size()->PrintLine(); # 1New # constructor
Copy constructor
New(values:H[])Parameters
| Name | Type | Description |
|---|---|---|
| values | H[] | values to copy |
Example
arr := IntRef->New[2];
arr[0] := IntRef->New(1); arr[1] := IntRef->New(2);
v := Collection.CompareVector->New(arr)<IntRef>;
v->Size()->PrintLine(); # 2New # constructor
Copy constructor
New(values:CompareVector<H>)Parameters
| Name | Type | Description |
|---|---|---|
| values | CompareVector<H> | values to copy |
Example
src := Collection.CompareVector->New()<IntRef>;
src->AddBack(IntRef->New(5));
copy := Collection.CompareVector->New(src)<IntRef>;
copy->Get(0)->PrintLine(); # 5Reduce #
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
v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(2)); v->AddBack(IntRef->New(3));
sum := v->Reduce(IntRef->New(0), \(IntRef, IntRef) ~ IntRef : (a, b) => IntRef->New(a->Get() + b->Get()));
sum->PrintLine(); # 6Remove #
Removes an indexed value
method : public : Remove(i:Int) ~ HParameters
| Name | Type | Description |
|---|---|---|
| i | Int | index |
Return
| Type | Description |
|---|---|
| H | value |
Example
v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(2)); v->AddBack(IntRef->New(3));
v->Remove(1)->PrintLine(); # 2
v->Size()->PrintLine(); # 2RemoveBack #
Removes the last value
method : public : RemoveBack() ~ HReturn
| Type | Description |
|---|---|
| H | value |
Example
v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(2));
v->RemoveBack()->PrintLine(); # 2
v->Size()->PrintLine(); # 1Reverse #
Reverses element order
method : public : Reverse() ~ CompareVector<H>Return
| Type | Description |
|---|---|
| CompareVector<H> | reversed vector, if the vector is empty or hold 1 item then the original list is returned |
Example
v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(2)); v->AddBack(IntRef->New(3));
rev := v->Reverse();
rev->Get(0)->PrintLine(); # 3Set #
Sets an indexed value
method : public : Set(value:H, index:Int) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| value | H | value |
| index | Int | index |
Example
v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1));
v->Set(IntRef->New(99), 0)->PrintLine(); # true
v->Get(0)->PrintLine(); # 99Size #
Size of vector
method : public : Size() ~ IntReturn
| Type | Description |
|---|---|
| Int | size of vector |
Example
v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(2));
v->Size()->PrintLine(); # 2Slice #
Copies a range into a new vector
method : public : Slice(lo:Int, hi:Int) ~ CompareVector<H>Parameters
| Name | Type | Description |
|---|---|---|
| lo | Int | first index (inclusive, clamped to 0) |
| hi | Int | last index (exclusive, clamped to Size()) |
Return
| Type | Description |
|---|---|
| CompareVector<H> | new vector holding the range, empty if the range is empty |
Example
v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(2)); v->AddBack(IntRef->New(3));
v->Slice(1, 3)->Size()->PrintLine(); # 2Sort # native
Sorts the values in the vector
method : public : native : Sort() ~ NilExample
v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(3)); v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(2));
v->Sort();
v->Get(0)->PrintLine(); # 1Sort #
Sorts the values in place with a comparator. The sort is stable: values the comparator calls equal keep their order.
method : public : Sort(compare:(H,H)~Int) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| compare | (H,H)~Int | comparator: negative if the first value goes first, 0 if equal, positive if the second goes first |
Example
v := Collection.CompareVector->New()<String>;
v->AddBack("pear"); v->AddBack("fig"); v->AddBack("banana");
v->Sort(\(a, b) => a->Size() - b->Size());
v->Get(0)->PrintLine(); # figSwap #
Swaps two values
method : public : Swap(a:Int, b:Int) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| a | Int | first index |
| b | Int | second index |
Return
| Type | Description |
|---|---|
| Bool | true if values were swapped |
Example
v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(2));
v->Swap(0, 1)->PrintLine(); # trueToArray #
Converts the vector into an object array
method : public : ToArray() ~ H[]Return
| Type | Description |
|---|---|
| H[] | object array |
Example
v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(7)); v->AddBack(IntRef->New(8));
arr := v->ToArray()<IntRef>;
arr[0]->PrintLine(); # 7
arr[1]->PrintLine(); # 8ToString #
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
v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(2));
v->ToString()->PrintLine(); # [1,2]