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) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| key | K | key to count |
Return
| Type | Description |
|---|---|
| Int | the key's new count |
Example
s := Collection.MultiSet->New()<String>;
s->Add("x")->PrintLine(); # 1
s->Add("x")->PrintLine(); # 2AddMany #
Adds several occurrences of a key
method : public : AddMany(key:K, n:Int) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| key | K | key to count |
| n | Int | occurrences to add (values below 1 change nothing) |
Return
| Type | Description |
|---|---|
| Int | the key's new count |
Example
s := Collection.MultiSet->New()<String>;
s->AddMany("x", 5)->PrintLine(); # 5Count #
Gets the number of occurrences of a key
method : public : Count(key:K) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| key | K | key to look up |
Return
| Type | Description |
|---|---|
| Int | count, 0 if absent |
Example
s := Collection.MultiSet->New()<String>;
s->Add("x");
s->Count("x")->PrintLine(); # 1
s->Count("y")->PrintLine(); # 0Distinct #
Gets the number of distinct keys
method : public : Distinct() ~ IntReturn
| Type | Description |
|---|---|
| Int | distinct count |
GetKeys #
Gets the distinct keys, order unspecified
method : public : GetKeys() ~ Vector<K>Return
| Type | Description |
|---|---|
| Vector<K> | vector of keys |
Has #
Checks whether a key has at least one occurrence
method : public : Has(key:K) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| key | K | key to look up |
Return
| Type | Description |
|---|---|
| Bool | true if present |
IsEmpty #
Checks for no occurrences
method : public : IsEmpty() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if empty |
MostCommon #
Gets the keys with the highest counts
method : public : MostCommon(n:Int) ~ Vector<K>Parameters
| Name | Type | Description |
|---|---|---|
| n | Int | how many keys to return at most |
Return
| Type | Description |
|---|---|
| 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(); # bNew # constructor
Default constructor
New()Example
words := Collection.MultiSet->New()<String>;
words->Add("a"); words->Add("b"); words->Add("a");
words->Count("a")->PrintLine(); # 2Remove #
Removes one occurrence of a key
method : public : Remove(key:K) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| key | K | key to remove |
Return
| Type | Description |
|---|---|
| Bool | true 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