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.
OrderedHash
Insertion-ordered hash: key to value with O(1) expected Insert, Find, Has and Remove, and iteration in the order keys were first inserted. Updating an existing key keeps its position. A Hash is unordered and a Map is sorted by key; use this when the order of arrival matters.
Operations
Each #
Calls a function for every entry in insertion order
method : public : Each(f:(K,V)~Nil) ~ OrderedHash<K,V>Parameters
| Name | Type | Description |
|---|---|---|
| f | (K,V)~Nil | function taking the key and value |
Return
| Type | Description |
|---|---|
| OrderedHash<K,V> | this hash |
Example
h := Collection.OrderedHash->New()<String, IntRef>;
h->Insert("a", IntRef->New(1));
h->Each(\(k, v) => k->PrintLine());Find #
Finds the value for a key
method : public : Find(key:K) ~ VParameters
| Name | Type | Description |
|---|---|---|
| key | K | key |
Return
| Type | Description |
|---|---|
| V | value, or Nil if absent |
FirstKey #
Gets the oldest key (the first one inserted that is still present)
method : public : FirstKey() ~ KReturn
| Type | Description |
|---|---|
| K | key, or Nil if empty |
GetKeys #
Gets the keys in insertion order
method : public : GetKeys() ~ Vector<K>Return
| Type | Description |
|---|---|
| Vector<K> | vector of keys |
GetValues #
Gets the values in insertion order of their keys
method : public : GetValues() ~ Vector<V>Return
| Type | Description |
|---|---|
| Vector<V> | vector of values |
Has #
Checks whether a key is present
method : public : Has(key:K) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| key | K | key |
Return
| Type | Description |
|---|---|
| Bool | true if present |
Insert #
Inserts a key or updates its value; a new key goes last, an existing key keeps its place
method : public : Insert(key:K, value:V) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| key | K | key |
| value | V | value |
Example
h := Collection.OrderedHash->New()<String, IntRef>;
h->Insert("a", IntRef->New(1));
h->Insert("a", IntRef->New(5));
h->Find("a")->Get()->PrintLine(); # 5
h->Size()->PrintLine(); # 1IsEmpty #
Checks for no entries
method : public : IsEmpty() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if empty |
LastKey #
Gets the newest key
method : public : LastKey() ~ KReturn
| Type | Description |
|---|---|
| K | key, or Nil if empty |
New # constructor
Default constructor
New()Example
h := Collection.OrderedHash->New()<String, IntRef>;
h->Insert("b", IntRef->New(2)); h->Insert("a", IntRef->New(1));
h->GetKeys()->Get(0)->PrintLine(); # bRemove #
Removes a key
method : public : Remove(key:K) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| key | K | key |
Return
| Type | Description |
|---|---|
| Bool | true if it was present |
Example
h := Collection.OrderedHash->New()<String, IntRef>;
h->Insert("a", IntRef->New(1));
h->Remove("a")->PrintLine(); # trueRemoveFirst #
Removes the oldest key, as a first-in first-out cache or queue-with-lookup would
method : public : RemoveFirst() ~ KReturn
| Type | Description |
|---|---|
| K | the removed key, or Nil if empty |