v2026.11.3
All Bundles
Bundle

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

Copy #

Copies the hash. The keys and values are shared, not cloned.

method : public : Copy() ~ Hash<K,V>

Return

TypeDescription
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(); # 2

Dict # function

Converts a vector pairs to a hash

function : Dict(pairs:Vector<Collection.Tuple.Pair<K,V>>) ~ Hash<K,V>

Parameters

NameTypeDescription
pairsVector<CollectionTuplePair<K,V>>vector of pairs

Return

TypeDescription
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(); # 1

Each #

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

NameTypeDescription
f(K,V)~Nilfunction called

Return

TypeDescription
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() ~ Nil

Example

hash := Collection.Hash->New()<String, IntRef>;
hash->Insert("a", 1);
hash->Empty();
hash->IsEmpty()->PrintLine(); # true

Filter #

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

NameTypeDescription
f(K)~Boolfunction that returns true for each key to keep

Return

TypeDescription
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(); # 1

Find # 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) ~ V

Parameters

NameTypeDescription
keyKsearch key

Return

TypeDescription
Vfound value, Nil if not found

Example

hash := Collection.Hash->New()<String, IntRef>;
hash->Insert("price", 99);
hash->Find("price")->PrintLine(); # 99

GetCapacity #

Gets the capacity hint the hash was created or last resized with

method : public : GetCapacity() ~ Hash->Capacity

Return

TypeDescription
Hash->Capacityhash 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

TypeDescription
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

TypeDescription
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

TypeDescription
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) ~ Bool

Parameters

NameTypeDescription
keyKsearch key

Return

TypeDescription
Booltrue if found, false otherwise

Example

hash := Collection.Hash->New()<String, IntRef>;
hash->Insert("k", 1);
hash->Has("k")->PrintLine();   # true
hash->Has("z")->PrintLine();   # false

Insert # 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) ~ Nil

Parameters

NameTypeDescription
keyKkey
valueVvalue

Example

hash := Collection.Hash->New()<String, IntRef>;
hash->Insert("count", 42);
hash->Find("count")->PrintLine(); # 42

InsertIfAbsent # native

Inserts a value only when the key is not already in the hash

method : public : native : InsertIfAbsent(key:K, value:V) ~ Bool

Parameters

NameTypeDescription
keyKkey
valueVvalue

Return

TypeDescription
Booltrue 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();              # 1

IsAutoResize #

Gets auto-resize setting

method : public : IsAutoResize() ~ Bool

Return

TypeDescription
Booltrue if auto-resize is set, false otherwise

Example

hash := Collection.Hash->New()<String, IntRef>;
hash->IsAutoResize()->PrintLine(); # true

IsEmpty #

Checks to see if the hash table is empty

method : public : IsEmpty() ~ Bool

Return

TypeDescription
Booltrue if empty, false otherwise

Example

hash := Collection.Hash->New()<String, IntRef>;
hash->IsEmpty()->PrintLine(); # true
hash->Insert("a", 1);
hash->IsEmpty()->PrintLine(); # false

Merge #

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

NameTypeDescription
thatHash<K,V>hash to merge in (Nil changes nothing)

Return

TypeDescription
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();    # 3

New # constructor

Default constructor

New(auto_resize:Bool)

Parameters

NameTypeDescription
auto_resizeBoolkept 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(); # 2

New # constructor

Default constructor

New(capacity:Hash->Capacity, auto_resize:Bool)

Parameters

NameTypeDescription
capacityHash->Capacityexpected number of slots, rounded up to a power of two
auto_resizeBoolkept 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(); # 42

Remove # native

Removes a value from the hash

method : public : native : Remove(key:K) ~ Bool

Parameters

NameTypeDescription
keyKkey for value to remove

Return

TypeDescription
Booltrue 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();    # false

Resize #

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) ~ Nil

Parameters

NameTypeDescription
capacityHash->Capacitytable capacity
auto_resizeBooltrue 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(); # 1

SetAutoResize #

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) ~ Nil

Parameters

NameTypeDescription
auto_resizeBooltrue if auto-resize is enable, false otherwise

Example

hash := Collection.Hash->New()<String, IntRef>;
hash->SetAutoResize(false);
hash->IsAutoResize()->PrintLine(); # false

SetCapacity #

Sets the hash table capacity hint; use Resize to resize the table

method : public : SetCapacity(capacity:Hash->Capacity) ~ Nil

Parameters

NameTypeDescription
capacityHash->Capacityhash 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() ~ Int

Return

TypeDescription
Intsize of map

Example

hash := Collection.Hash->New()<String, IntRef>;
hash->Insert("a", 1);
hash->Insert("b", 2);
hash->Size()->PrintLine(); # 2

ToString #

Formats the collection into a string. If an element implements the 'Stringify' interface, it's 'ToString()' is called.

method : public : ToString() ~ String

Return

TypeDescription
Stringstring representation

Example

hash := Collection.Hash->New()<String, IntRef>;
hash->Insert("x", 10);
hash->Insert("y", 20);
hash->ToString()->PrintLine();