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.
IntVector
Growable array of Int values; nothing is boxed
Example
v := Collection.IntVector->New();
v->AddBack(3); v->AddBack(1); v->AddBack(2);
v->Sort();
v->Get(0)->PrintLine(); # 1
v->Sum()->PrintLine(); # 6
v->Size()->PrintLine(); # 3Operations
- New
- AddBack
- Clear
- Each
- Empty
- Find
- First
- Get
- Has
- IsEmpty
- Last
- Remove
- RemoveBack
- Reserve
- Set
- Size
- Sort
- Sum
- ToArray
- ToString
AddBack # native
Adds a value to the end
method : public : native : AddBack(value:Int) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| value | Int | value |
Example
v := Collection.IntVector->New();
v->AddBack(42);
v->Get(0)->PrintLine(); # 42Clear #
Removes every value; the reserved room is kept
method : public : Clear() ~ NilExample
v := Collection.IntVector->New([1, 2]);
v->Clear();
v->Size()->PrintLine(); # 0Each #
Calls a function for each value
method : public : Each(f:(Int)~Nil) ~ IntVectorParameters
| Name | Type | Description |
|---|---|---|
| f | (Int)~Nil | function called |
Return
| Type | Description |
|---|---|
| IntVector | this vector |
Example
v := Collection.IntVector->New([4, 5]);
v->Each(\(i) => { i->PrintLine(); });Empty #
Removes every value; the same as Clear
method : public : Empty() ~ NilExample
v := Collection.IntVector->New([1, 2]);
v->Empty();
v->IsEmpty()->PrintLine(); # trueFind # native
Finds the first index of a value
method : public : native : Find(value:Int) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| value | Int | value to find |
Return
| Type | Description |
|---|---|
| Int | index, or -1 if not found |
Example
v := Collection.IntVector->New([4, 5, 6]);
v->Find(6)->PrintLine(); # 2First #
Gets the first value
method : public : First() ~ IntReturn
| Type | Description |
|---|---|
| Int | first value, or 0 if empty |
Example
v := Collection.IntVector->New([4, 5]);
v->First()->PrintLine(); # 4Get # native
Gets an indexed value
method : public : native : Get(index:Int) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| index | Int | index |
Return
| Type | Description |
|---|---|
| Int | value, or 0 if the index is out of range |
Example
v := Collection.IntVector->New([4, 5]);
v->Get(1)->PrintLine(); # 5Has #
Checks for a value
method : public : Has(value:Int) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| value | Int | value to find |
Return
| Type | Description |
|---|---|
| Bool | true if found, false otherwise |
Example
v := Collection.IntVector->New([4, 5, 6]);
v->Has(5)->PrintLine(); # trueIsEmpty #
Checks to see if the vector is empty
method : public : IsEmpty() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if empty, false otherwise |
Example
v := Collection.IntVector->New();
v->IsEmpty()->PrintLine(); # trueLast #
Gets the last value
method : public : Last() ~ IntReturn
| Type | Description |
|---|---|
| Int | last value, or 0 if empty |
Example
v := Collection.IntVector->New([4, 5]);
v->Last()->PrintLine(); # 5New # constructor
Default constructor
New()Example
v := Collection.IntVector->New();
v->AddBack(7);
v->Size()->PrintLine(); # 1New # constructor
Constructor that reserves room for a number of values
New(capacity:Int)Parameters
| Name | Type | Description |
|---|---|---|
| capacity | Int | number of values to reserve |
Example
v := Collection.IntVector->New(1000);
v->Size()->PrintLine(); # 0New # constructor
Copy constructor
New(values:Int[])Parameters
| Name | Type | Description |
|---|---|---|
| values | Int | values to copy |
Example
v := Collection.IntVector->New([5, 6, 7]);
v->Get(2)->PrintLine(); # 7Remove #
Removes an indexed value, shifting the later values down
method : public : Remove(index:Int) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| index | Int | index |
Return
| Type | Description |
|---|---|
| Int | removed value, or 0 if the index is out of range |
Example
v := Collection.IntVector->New([4, 5, 6]);
v->Remove(1)->PrintLine(); # 5
v->Get(1)->PrintLine(); # 6RemoveBack #
Removes the last value
method : public : RemoveBack() ~ IntReturn
| Type | Description |
|---|---|
| Int | removed value, or 0 if empty |
Example
v := Collection.IntVector->New([4, 5]);
v->RemoveBack()->PrintLine(); # 5
v->Size()->PrintLine(); # 1Reserve #
Reserves room for at least a number of values
method : public : Reserve(capacity:Int) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| capacity | Int | number of values |
Example
v := Collection.IntVector->New();
v->Reserve(1000000);
v->Size()->PrintLine(); # 0Set # native
Sets an indexed value
method : public : native : Set(value:Int, index:Int) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| value | Int | value |
| index | Int | index |
Return
| Type | Description |
|---|---|
| Bool | true if set, false if the index is out of range |
Example
v := Collection.IntVector->New([4, 5]);
v->Set(9, 0)->PrintLine(); # true
v->Get(0)->PrintLine(); # 9Size #
Size of vector
method : public : Size() ~ IntReturn
| Type | Description |
|---|---|
| Int | number of values |
Example
v := Collection.IntVector->New([1, 2]);
v->Size()->PrintLine(); # 2Sort #
Sorts the values in ascending order (a stable merge sort, O(n log n))
method : public : Sort() ~ NilExample
v := Collection.IntVector->New([3, -1, 2]);
v->Sort();
v->ToString()->PrintLine(); # [-1,2,3]Sum # native
Sums the values
method : public : native : Sum() ~ IntReturn
| Type | Description |
|---|---|
| Int | sum |
Example
v := Collection.IntVector->New([4, 5, 6]);
v->Sum()->PrintLine(); # 15