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.
Trie
Trie (prefix tree) holding a set of strings. Add, Has, HasPrefix and Remove cost O(length of the string) times the log of the branching at each node; WithPrefix lists matches in sorted order. Characters are compared as the string's own character values.
Operations
Add #
Adds a string
method : public : Add(word:String) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| word | String | string to add |
Return
| Type | Description |
|---|---|
| Bool | true if added, false if it was already present or is Nil |
Example
t := Collection.Trie->New();
t->Add("car")->PrintLine(); # true
t->Add("car")->PrintLine(); # falseHas #
Checks whether a whole string was added
method : public : Has(word:String) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| word | String | string to look for |
Return
| Type | Description |
|---|---|
| Bool | true if present |
Example
t := Collection.Trie->New();
t->Add("car");
t->Has("car")->PrintLine(); # true
t->Has("ca")->PrintLine(); # falseHasPrefix #
Checks whether any added string starts with a prefix
method : public : HasPrefix(prefix:String) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| prefix | String | prefix to look for (an empty prefix matches if the trie is not empty) |
Return
| Type | Description |
|---|---|
| Bool | true if some string starts with it |
Example
t := Collection.Trie->New();
t->Add("car");
t->HasPrefix("ca")->PrintLine(); # trueIsEmpty #
Checks for no strings
method : public : IsEmpty() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if empty |
New # constructor
Default constructor
New()Example
t := Collection.Trie->New();
t->Add("car"); t->Add("cat");
t->Size()->PrintLine(); # 2Remove #
Removes a string, freeing the nodes only it used
method : public : Remove(word:String) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| word | String | string to remove |
Return
| Type | Description |
|---|---|
| Bool | true if it was present |
Example
t := Collection.Trie->New();
t->Add("car"); t->Add("cart");
t->Remove("cart")->PrintLine(); # true
t->Has("car")->PrintLine(); # trueWithPrefix #
Lists the added strings that start with a prefix
method : public : WithPrefix(prefix:String) ~ Vector<String>Parameters
| Name | Type | Description |
|---|---|---|
| prefix | String | prefix to match (an empty prefix lists everything) |
Return
| Type | Description |
|---|---|
| Vector<String> | matching strings in sorted order, empty if none |
Example
t := Collection.Trie->New();
t->Add("cat"); t->Add("car"); t->Add("dog");
t->WithPrefix("ca")->Get(0)->PrintLine(); # car