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.
Hash
Hash unordered table of generics. Open addressing (linear probing over parallel key, value and hash arrays) that doubles at 70% load with no upper limit. Lookups change nothing, so concurrent readers do not interfere; writers still need a lock. Keys of different classes are never equal, and the iteration order is unspecified.
Example
# insert elements
hash := Collection.Hash->New()<IntRef, String>;
hash->Insert(415, "San Francisco");
hash->Insert(510, "Oakland");
hash->Insert(925, "East Bay");
# get size
hash->Size()->PrintLine();
# get value by key
hash->Find(510)->PrintLine();
# get key/values
key_values := hash->GetKeyValues()<Pair<IntRef, String>>;
each(key_value := key_values) {
key_value->GetFirst()->PrintLine();
};
# get values
values := hash->GetValues()<String>;
each(value := values) {
value->PrintLine();
};
# check for key
hash->Has(408)->PrintLine();Operations
- New
- Copy
- Dict
- Each
- Empty
- Filter
- Find
- GetCapacity
- GetKeyValues
- GetKeys
- GetValues
- Has
- Insert
- InsertIfAbsent
- IsAutoResize
- IsEmpty
- Merge
- Remove
- Resize
- SetAutoResize
- SetCapacity
- Size
- ToString
Copy #
Copies the hash. The keys and values are shared, not cloned.
method : public : Copy() ~ Hash<K,V>Return
| Type | Description |
|---|---|
| Hash<K,V> | new hash with the same entries |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->Insert("a", 1);
copy := hash->Copy();
copy->Insert("b", 2);
hash->Size()->PrintLine(); # 1
copy->Size()->PrintLine(); # 2Dict # function
Converts a vector pairs to a hash
function : Dict(pairs:Vector<Collection.Tuple.Pair<K,V>>) ~ Hash<K,V>Parameters
| Name | Type | Description |
|---|---|---|
| pairs | Vector<CollectionTuplePair<K,V>> | vector of pairs |
Return
| Type | Description |
|---|---|
| Hash<K,V> | hashed pairs |
Example
pairs := Collection.Vector->New()<Collection.Tuple.Pair<String, IntRef>>;
pairs->AddBack(Collection.Tuple.Pair->New("a", IntRef->New(1))<String, IntRef>);
pairs->AddBack(Collection.Tuple.Pair->New("b", IntRef->New(2))<String, IntRef>);
hash := Collection.Hash->Dict(pairs)<String, IntRef>;
hash->Find("a")->PrintLine(); # 1Each #
Calls a function for each key and value. The function may change the hash: it is called on a snapshot of the entries.
method : public : Each(f:(K,V)~Nil) ~ Hash<K,V>Parameters
| Name | Type | Description |
|---|---|---|
| f | (K,V)~Nil | function called |
Return
| Type | Description |
|---|---|
| Hash<K,V> | this hash |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->Insert("a", 1); hash->Insert("b", 2);
hash->Each(\(k, v) => { "{$k}={$v}"->PrintLine(); });Empty #
Clears the map
method : public : Empty() ~ NilExample
hash := Collection.Hash->New()<String, IntRef>;
hash->Insert("a", 1);
hash->Empty();
hash->IsEmpty()->PrintLine(); # trueFilter #
Keeps the entries whose keys a function accepts, as Map->Filter does. The hash is not changed.
method : public : Filter(f:(K)~Bool) ~ Hash<K,V>Parameters
| Name | Type | Description |
|---|---|---|
| f | (K)~Bool | function that returns true for each key to keep |
Return
| Type | Description |
|---|---|
| Hash<K,V> | new hash |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->Insert("apple", 1); hash->Insert("kiwi", 2);
hash->Filter(\(k) => k->Size() > 4)->Size()->PrintLine(); # 1Find # native
Searches for a value in a hash. A lookup changes nothing in the hash, so concurrent readers do not interfere.
method : public : native : Find(key:K) ~ VParameters
| Name | Type | Description |
|---|---|---|
| key | K | search key |
Return
| Type | Description |
|---|---|
| V | found value, Nil if not found |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->Insert("price", 99);
hash->Find("price")->PrintLine(); # 99GetCapacity #
Gets the capacity hint the hash was created or last resized with
method : public : GetCapacity() ~ Hash->CapacityReturn
| Type | Description |
|---|---|
| Hash->Capacity | hash table capacity hint |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->GetCapacity()->PrintLine();GetKeyValues #
Gets a collection of key/value pairs
method : public : GetKeyValues() ~ Vector<Pair<K,V>>Return
| Type | Description |
|---|---|
| Vector<Pair<K,V>> | vector of key/value pairs |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->Insert("m", 100);
kvs := hash->GetKeyValues()<Collection.Pair<String, IntRef>>;
each(kv := kvs) {
"{kv->GetFirst()}: {kv->GetSecond()}"->PrintLine();
};GetKeys # native
Get a collection of keys. GetKeys, GetValues and GetKeyValues list the entries in the same order.
method : public : native : GetKeys() ~ Vector<K>Return
| Type | Description |
|---|---|
| Vector<K> | vector of keys |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->Insert("a", 1);
hash->Insert("b", 2);
keys := hash->GetKeys()<String>;
each(k := keys) {
k->PrintLine();
};GetValues # native
Gets a collection of values
method : public : native : GetValues() ~ Vector<V>Return
| Type | Description |
|---|---|
| Vector<V> | vector of values |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->Insert("x", 10);
hash->Insert("y", 20);
vals := hash->GetValues()<IntRef>;
each(v := vals) {
v->PrintLine();
};Has # native
Checks for a value in a hash
method : public : native : Has(key:K) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| key | K | search key |
Return
| Type | Description |
|---|---|
| Bool | true if found, false otherwise |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->Insert("k", 1);
hash->Has("k")->PrintLine(); # true
hash->Has("z")->PrintLine(); # falseInsert # native
Inserts a value into the hash, replacing the value of an equal key. Keys of different classes are never equal.
method : public : native : Insert(key:K, value:V) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| key | K | key |
| value | V | value |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->Insert("count", 42);
hash->Find("count")->PrintLine(); # 42InsertIfAbsent # native
Inserts a value only when the key is not already in the hash
method : public : native : InsertIfAbsent(key:K, value:V) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| key | K | key |
| value | V | value |
Return
| Type | Description |
|---|---|
| Bool | true if inserted, false if the key was already present (or is Nil) |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->InsertIfAbsent("a", 1)->PrintLine(); # true
hash->InsertIfAbsent("a", 2)->PrintLine(); # false
hash->Find("a")->PrintLine(); # 1IsAutoResize #
Gets auto-resize setting
method : public : IsAutoResize() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if auto-resize is set, false otherwise |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->IsAutoResize()->PrintLine(); # trueIsEmpty #
Checks to see if the hash table is empty
method : public : IsEmpty() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if empty, false otherwise |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->IsEmpty()->PrintLine(); # true
hash->Insert("a", 1);
hash->IsEmpty()->PrintLine(); # falseMerge #
Inserts every entry of another hash into this one. Where both have a key, that hash's value wins.
method : public : Merge(that:Hash<K,V>) ~ Hash<K,V>Parameters
| Name | Type | Description |
|---|---|---|
| that | Hash<K,V> | hash to merge in (Nil changes nothing) |
Return
| Type | Description |
|---|---|
| Hash<K,V> | this hash |
Example
a := Collection.Hash->New()<String, IntRef>;
a->Insert("x", 1); a->Insert("y", 2);
b := Collection.Hash->New()<String, IntRef>;
b->Insert("y", 20); b->Insert("z", 30);
a->Merge(b);
a->Find("y")->PrintLine(); # 20
a->Size()->PrintLine(); # 3New # constructor
Default constructor
New(auto_resize:Bool)Parameters
| Name | Type | Description |
|---|---|---|
| auto_resize | Bool | kept for compatibility; the table always grows when it needs to |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->Insert("apples", 3);
hash->Insert("bananas", 5);
hash->Size()->PrintLine(); # 2New # constructor
Default constructor
New(capacity:Hash->Capacity, auto_resize:Bool)Parameters
| Name | Type | Description |
|---|---|---|
| capacity | Hash->Capacity | expected number of slots, rounded up to a power of two |
| auto_resize | Bool | kept for compatibility; the table always grows when it needs to |
Example
hash := Collection.Hash->New(Collection.Hash->Capacity->MEDIUM)<String, IntRef>;
hash->Insert("key", 42);
hash->Find("key")->PrintLine(); # 42Remove # native
Removes a value from the hash
method : public : native : Remove(key:K) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| key | K | key for value to remove |
Return
| Type | Description |
|---|---|
| Bool | true if removed, false if the key was not found |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->Insert("del", 9);
hash->Remove("del")->PrintLine(); # true
hash->Has("del")->PrintLine(); # falseResize #
Resizes the hash table to at least the given capacity, and to more if the entries need it
method : public : Resize(capacity:Hash->Capacity, auto_resize:Bool) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| capacity | Hash->Capacity | table capacity |
| auto_resize | Bool | true for hash table auto resizing, false otherwise |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->Insert("a", 1);
hash->Resize(Collection.Hash->Capacity->LARGE);
hash->Find("a")->PrintLine(); # 1SetAutoResize #
Set auto-resize setting. The table grows whenever it is 70% full either way; the flag is kept for compatibility.
method : public : SetAutoResize(auto_resize:Bool) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| auto_resize | Bool | true if auto-resize is enable, false otherwise |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->SetAutoResize(false);
hash->IsAutoResize()->PrintLine(); # falseSetCapacity #
Sets the hash table capacity hint; use Resize to resize the table
method : public : SetCapacity(capacity:Hash->Capacity) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| capacity | Hash->Capacity | hash table capacity |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->SetCapacity(Collection.Hash->Capacity->MEDIUM);
hash->GetCapacity()->PrintLine();Size #
Size of map
method : public : Size() ~ IntReturn
| Type | Description |
|---|---|
| Int | size of map |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->Insert("a", 1);
hash->Insert("b", 2);
hash->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
hash := Collection.Hash->New()<String, IntRef>;
hash->Insert("x", 10);
hash->Insert("y", 20);
hash->ToString()->PrintLine();