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.
MultiHash
Hash table that holds multiple values with the same key: a MultiMap whose keys are unordered, with constant-time lookups
Example
mmap := Collection.MultiHash->New()<String, IntRef>;
mmap->Insert("fruits", 1);
mmap->Insert("fruits", 2);
mmap->Insert("vegetables", 3);
values := mmap->Find("fruits")<IntRef>;
each(v := values) {
v->PrintLine();
};Operations
Each #
Function called for each element
method : public : Each(f:(K,V)~Nil) ~ MultiHash<K,V>Parameters
| Name | Type | Description |
|---|---|---|
| f | (K,V)~Nil | function called |
Example
mmap := Collection.MultiHash->New()<String, IntRef>;
mmap->Insert("a", IntRef->New(1)); mmap->Insert("a", IntRef->New(2));
mmap->Each(\(k, v) => { "{$k}={$v}"->PrintLine(); });Empty #
Clears the map
method : public : Empty() ~ NilExample
mmap := Collection.MultiHash->New()<String, IntRef>;
mmap->Insert("a", IntRef->New(1));
mmap->Empty();
mmap->IsEmpty()->PrintLine(); # trueFilter #
Uses the given function to filter out values
method : public : Filter(f:(K)~Bool) ~ MultiHash<K,V>Parameters
| Name | Type | Description |
|---|---|---|
| f | (K)~Bool | function to use a filter. If the function evaluates to true the value is added to the collection. |
Return
| Type | Description |
|---|---|
| MultiHash<K,V> | filtered MultiHash |
Example
mmap := Collection.MultiHash->New()<String, IntRef>;
mmap->Insert("a", IntRef->New(1)); mmap->Insert("b", IntRef->New(2));
filtered := mmap->Filter(\(k) => k->Equals("a"))<String, IntRef>;
filtered->Size()->PrintLine(); # 1Find #
Finds all values for a key in the map
method : public : Find(key:K) ~ Vector<V>Parameters
| Name | Type | Description |
|---|---|---|
| key | K | search key |
Return
| Type | Description |
|---|---|
| Vector<V> | vector of values, or Nil if not found |
Example
mmap := Collection.MultiHash->New()<String, IntRef>;
mmap->Insert("k", IntRef->New(10)); mmap->Insert("k", IntRef->New(20));
vals := mmap->Find("k")<IntRef>;
each(v := vals) { v->PrintLine(); } # 10, 20GetKeys #
Get a collection of keys
method : public : GetKeys() ~ Vector<K>Return
| Type | Description |
|---|---|
| Vector<K> | vector of keys |
Example
mmap := Collection.MultiHash->New()<String, IntRef>;
mmap->Insert("a", IntRef->New(1)); mmap->Insert("b", IntRef->New(2));
keys := mmap->GetKeys()<String>;
each(k := keys) { k->PrintLine(); }GetValues #
Gets a collection of all values across all keys
method : public : GetValues() ~ Vector<V>Return
| Type | Description |
|---|---|
| Vector<V> | vector of values |
Example
mmap := Collection.MultiHash->New()<String, IntRef>;
mmap->Insert("a", IntRef->New(1)); mmap->Insert("b", IntRef->New(2));
all_values := mmap->GetValues()<IntRef>;
all_values->Size()->PrintLine(); # 2Has #
Checks for a value in a map
method : public : Has(key:K) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| key | K | search key |
Return
| Type | Description |
|---|---|
| Bool | true if found, false otherwise |
Example
mmap := Collection.MultiHash->New()<String, IntRef>;
mmap->Insert("x", IntRef->New(5));
mmap->Has("x")->PrintLine(); # true
mmap->Has("y")->PrintLine(); # falseInsert #
Inserts a value into the map
method : public : Insert(key:K, value:V) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| key | K | key |
| value | V | value |
Example
mmap := Collection.MultiHash->New()<String, IntRef>;
mmap->Insert("fruits", IntRef->New(1));
mmap->Insert("fruits", IntRef->New(2));
mmap->Find("fruits")->Size()->PrintLine(); # 2IsEmpty #
Checks to see if the map is empty
method : public : IsEmpty() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if empty, false otherwise |
Example
mmap := Collection.MultiHash->New()<String, IntRef>;
mmap->IsEmpty()->PrintLine(); # true
mmap->Insert("a", IntRef->New(1));
mmap->IsEmpty()->PrintLine(); # falseNew # constructor
Default constructor
New()Example
mmap := Collection.MultiHash->New()<String, IntRef>;
mmap->Insert("a", IntRef->New(1));
mmap->Size()->PrintLine(); # 1Reduce #
Uses the given function to reduce the values
method : public : Reduce(a:K, f:(K,K)~K) ~ KParameters
| Name | Type | Description |
|---|---|---|
| a | K | initial value (i.e. accumulator) |
| f | (K,K)~K | function to use a reduce |
Return
| Type | Description |
|---|---|
| K | reduced value |
Example
mmap := Collection.MultiHash->New()<String, IntRef>;
mmap->Insert("a", IntRef->New(0)); mmap->Insert("b", IntRef->New(0));
concat := mmap->Reduce("", \(String, String) ~ String : (acc, k) => "{$acc}{$k}");
concat->PrintLine(); # abRemove #
Removes a set of values from the map
method : public : Remove(key:K) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| key | K | key for values to remove |
Example
mmap := Collection.MultiHash->New()<String, IntRef>;
mmap->Insert("del", IntRef->New(1)); mmap->Insert("del", IntRef->New(2));
mmap->Remove("del")->PrintLine(); # true
mmap->Has("del")->PrintLine(); # falseSize #
Size of unique keys
method : public : Size() ~ IntReturn
| Type | Description |
|---|---|
| Int | size of unique keys |
Example
mmap := Collection.MultiHash->New()<String, IntRef>;
mmap->Insert("a", IntRef->New(1)); mmap->Insert("b", IntRef->New(2));
mmap->Size()->PrintLine(); # 2ToString #
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
mmap := Collection.MultiHash->New()<String, IntRef>;
mmap->Insert("a", IntRef->New(1)); mmap->Insert("a", IntRef->New(2));
mmap->ToString()->PrintLine(); # [(a:12)]