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.

MultiSet

Multiset (bag, counter): counts how many times each key was added. Backed by a Hash, so Add, Remove and Count are O(1) expected. Keys with a count of zero are not kept.

Operations

Add #

Adds one occurrence of a key

method : public : Add(key:K) ~ Int

Parameters

NameTypeDescription
keyKkey to count

Return

TypeDescription
Intthe key's new count

Example

s := Collection.MultiSet->New()<String>;
s->Add("x")->PrintLine(); # 1
s->Add("x")->PrintLine(); # 2

AddMany #

Adds several occurrences of a key

method : public : AddMany(key:K, n:Int) ~ Int

Parameters

NameTypeDescription
keyKkey to count
nIntoccurrences to add (values below 1 change nothing)

Return

TypeDescription
Intthe key's new count

Example

s := Collection.MultiSet->New()<String>;
s->AddMany("x", 5)->PrintLine(); # 5

Count #

Gets the number of occurrences of a key

method : public : Count(key:K) ~ Int

Parameters

NameTypeDescription
keyKkey to look up

Return

TypeDescription
Intcount, 0 if absent

Example

s := Collection.MultiSet->New()<String>;
s->Add("x");
s->Count("x")->PrintLine(); # 1
s->Count("y")->PrintLine(); # 0

Distinct #

Gets the number of distinct keys

method : public : Distinct() ~ Int

Return

TypeDescription
Intdistinct count

Empty #

Removes everything

method : public : Empty() ~ Nil

GetKeys #

Gets the distinct keys, order unspecified

method : public : GetKeys() ~ Vector<K>

Return

TypeDescription
Vector<K>vector of keys

Has #

Checks whether a key has at least one occurrence

method : public : Has(key:K) ~ Bool

Parameters

NameTypeDescription
keyKkey to look up

Return

TypeDescription
Booltrue if present

IsEmpty #

Checks for no occurrences

method : public : IsEmpty() ~ Bool

Return

TypeDescription
Booltrue if empty

MostCommon #

Gets the keys with the highest counts

method : public : MostCommon(n:Int) ~ Vector<K>

Parameters

NameTypeDescription
nInthow many keys to return at most

Return

TypeDescription
Vector<K>keys, highest count first (ties in unspecified order)

Example

s := Collection.MultiSet->New()<String>;
s->AddMany("a", 1); s->AddMany("b", 5); s->AddMany("c", 3);
s->MostCommon(2)->Get(0)->PrintLine(); # b

New # constructor

Default constructor

New()

Example

words := Collection.MultiSet->New()<String>;
words->Add("a"); words->Add("b"); words->Add("a");
words->Count("a")->PrintLine(); # 2

Remove #

Removes one occurrence of a key

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

Parameters

NameTypeDescription
keyKkey to remove

Return

TypeDescription
Booltrue if an occurrence was removed, false if the key was not present

Example

s := Collection.MultiSet->New()<String>;
s->Add("x"); s->Add("x");
s->Remove("x")->PrintLine(); # true
s->Count("x")->PrintLine();  # 1

RemoveAll #

Removes every occurrence of a key

method : public : RemoveAll(key:K) ~ Int

Parameters

NameTypeDescription
keyKkey to remove

Return

TypeDescription
Inthow many occurrences were removed

Example

s := Collection.MultiSet->New()<String>;
s->AddMany("x", 3);
s->RemoveAll("x")->PrintLine(); # 3

Size #

Gets the total number of occurrences, counting repeats

method : public : Size() ~ Int

Return

TypeDescription
Inttotal count