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.
IntMap
Hash table from Int keys to Int values: open addressing over plain arrays, so nothing is boxed. Every Int is a valid key, including 0 and the Int64 extremes.
Example
counts := Collection.IntMap->New();
counts->Add(7, 1); counts->Add(7, 1); counts->Add(3, 1);
counts->Find(7)->PrintLine(); # 2
counts->Find(9, -1)->PrintLine(); # -1
counts->Has(3)->PrintLine(); # true
counts->Size()->PrintLine(); # 2Operations
Add # native
Adds to the value of a key; a missing key starts at 0
method : public : native : Add(key:Int, delta:Int) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| key | Int | key |
| delta | Int | amount to add |
Return
| Type | Description |
|---|---|
| Int | new value |
Example
map := Collection.IntMap->New();
map->Add(1, 5);
map->Add(1, 2)->PrintLine(); # 7Each #
Calls a function for each key and value. The function must not change the map.
method : public : Each(f:(Int,Int)~Nil) ~ IntMapParameters
| Name | Type | Description |
|---|---|---|
| f | (Int,Int)~Nil | function called |
Return
| Type | Description |
|---|---|
| IntMap | this map |
Example
map := Collection.IntMap->New();
map->Insert(1, 10);
map->Each(\(k, v) => { "{$k}={$v}"->PrintLine(); });Empty #
Removes every entry
method : public : Empty() ~ NilExample
map := Collection.IntMap->New();
map->Insert(1, 1);
map->Empty();
map->Size()->PrintLine(); # 0Find #
Finds the value of a key
method : public : Find(key:Int) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| key | Int | key |
Return
| Type | Description |
|---|---|
| Int | value, or 0 if the key is missing (use Has or Find(key, missing) to tell) |
Example
map := Collection.IntMap->New();
map->Insert(1, 10);
map->Find(1)->PrintLine(); # 10
map->Find(2)->PrintLine(); # 0Find # native
Finds the value of a key
method : public : native : Find(key:Int, missing:Int) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| key | Int | key |
| missing | Int | value returned when the key is missing |
Return
| Type | Description |
|---|---|
| Int | value, or missing |
Example
map := Collection.IntMap->New();
map->Find(2, -1)->PrintLine(); # -1GetKeys # native
Gets the keys. GetKeys and GetValues list the entries in the same order.
method : public : native : GetKeys() ~ Int[]Return
| Type | Description |
|---|---|
| Int | array of keys |
Example
map := Collection.IntMap->New();
map->Insert(1, 10); map->Insert(2, 20);
map->GetKeys()->Size()->PrintLine(); # 2GetValues # native
Gets the values, in the same order as GetKeys
method : public : native : GetValues() ~ Int[]Return
| Type | Description |
|---|---|
| Int | array of values |
Example
map := Collection.IntMap->New();
map->Insert(1, 10); map->Insert(2, 20);
map->GetValues()->Size()->PrintLine(); # 2Has # native
Checks for a key
method : public : native : Has(key:Int) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| key | Int | key |
Return
| Type | Description |
|---|---|
| Bool | true if found, false otherwise |
Example
map := Collection.IntMap->New();
map->Insert(0, 0);
map->Has(0)->PrintLine(); # true
map->Has(1)->PrintLine(); # falseInsert # native
Inserts a value, replacing the value of an existing key
method : public : native : Insert(key:Int, value:Int) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| key | Int | key |
| value | Int | value |
Example
map := Collection.IntMap->New();
map->Insert(-5, 25);
map->Find(-5)->PrintLine(); # 25IsEmpty #
Checks to see if the map is empty
method : public : IsEmpty() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if empty, false otherwise |
Example
map := Collection.IntMap->New();
map->IsEmpty()->PrintLine(); # trueNew # constructor
Default constructor
New()Example
map := Collection.IntMap->New();
map->Insert(1, 100);
map->Size()->PrintLine(); # 1New # constructor
Constructor that reserves room for a number of entries
New(capacity:Int)Parameters
| Name | Type | Description |
|---|---|---|
| capacity | Int | number of entries expected |
Example
map := Collection.IntMap->New(100000);
map->Size()->PrintLine(); # 0Remove # native
Removes a key
method : public : native : Remove(key:Int) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| key | Int | key |
Return
| Type | Description |
|---|---|
| Bool | true if removed, false if the key was missing |
Example
map := Collection.IntMap->New();
map->Insert(4, 16);
map->Remove(4)->PrintLine(); # true
map->Has(4)->PrintLine(); # false