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.
SetHash
Unordered set of generic objects
Example
# insert elements
set_hash := Collection.SetHash->New()<String>;
set_hash->Insert("San Francisco");
set_hash->Insert("Oakland");
set_hash->Insert("East Bay");
# get size
set_hash->Size()->PrintLine();
# get value by key
set_hash->Has("Oakland")->PrintLine();Operations
- New
- Add
- Difference
- Each
- Empty
- Filter
- GetKeys
- Has
- Insert
- Intersect
- IsEmpty
- IsSubset
- IsSuperset
- Remove
- Size
- SymmetricDifference
- ToArray
- ToString
- ToVector
- Union
Add #
Adds a key to the set; the same as Insert, but says whether it was new
method : public : Add(key:K) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| key | K | key |
Return
| Type | Description |
|---|---|
| Bool | true if added, false if it was already in the set (or is Nil) |
Example
set := Collection.SetHash->New()<String>;
set->Add("apple")->PrintLine(); # true
set->Add("apple")->PrintLine(); # falseDifference #
Elements in this set that are not in the other set. Neither set is changed.
method : public : Difference(that:SetHash<K>) ~ SetHash<K>Parameters
| Name | Type | Description |
|---|---|---|
| that | SetHash<K> | other set (Nil counts as empty) |
Return
| Type | Description |
|---|---|
| SetHash<K> | new set |
Example
a := Collection.SetHash->New()<IntRef>;
a->Insert(1); a->Insert(2); a->Insert(3);
b := Collection.SetHash->New()<IntRef>;
b->Insert(2); b->Insert(3); b->Insert(4);
a->Difference(b)->Has(1)->PrintLine(); # trueEach #
Calls a function for each element
method : public : Each(f:(K)~Nil) ~ SetHash<K>Parameters
| Name | Type | Description |
|---|---|---|
| f | (K)~Nil | function called |
Return
| Type | Description |
|---|---|
| SetHash<K> | this set |
Example
set := Collection.SetHash->New()<String>;
set->Insert("x"); set->Insert("y");
set->Each(\(s) => { s->PrintLine(); });Empty #
Clears the set
method : public : Empty() ~ NilExample
sh := Collection.SetHash->New()<String>;
sh->Insert("item");
sh->Empty();
sh->IsEmpty()->PrintLine(); # trueFilter #
Keeps the elements a function accepts. The set is not changed.
method : public : Filter(f:(K)~Bool) ~ SetHash<K>Parameters
| Name | Type | Description |
|---|---|---|
| f | (K)~Bool | function that returns true for each element to keep |
Return
| Type | Description |
|---|---|
| SetHash<K> | new set |
Example
set := Collection.SetHash->New()<String>;
set->Insert("apple"); set->Insert("kiwi");
set->Filter(\(s) => s->Size() > 4)->Size()->PrintLine(); # 1GetKeys #
Get a collection of keys
method : public : GetKeys() ~ Vector<K>Return
| Type | Description |
|---|---|
| Vector<K> | vector of keys |
Example
sh := Collection.SetHash->New()<String>;
sh->Insert("x"); sh->Insert("y");
keys := sh->GetKeys()<String>;
keys->Size()->PrintLine(); # 2Has #
Checks for key in set
method : public : Has(key:K) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| key | K | search key |
Return
| Type | Description |
|---|---|
| Bool | true if found, false otherwise |
Example
sh := Collection.SetHash->New()<String>;
sh->Insert("present");
sh->Has("present")->PrintLine(); # true
sh->Has("absent")->PrintLine(); # falseInsert #
Inserts a key into the set
method : public : Insert(key:K) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| key | K | key |
Example
sh := Collection.SetHash->New()<String>;
sh->Insert("blue");
sh->Has("blue")->PrintLine(); # trueIntersect #
Elements in both this set and the other set. Neither set is changed.
method : public : Intersect(that:SetHash<K>) ~ SetHash<K>Parameters
| Name | Type | Description |
|---|---|---|
| that | SetHash<K> | other set (Nil counts as empty) |
Return
| Type | Description |
|---|---|
| SetHash<K> | new set |
Example
a := Collection.SetHash->New()<IntRef>;
a->Insert(1); a->Insert(2); a->Insert(3);
b := Collection.SetHash->New()<IntRef>;
b->Insert(2); b->Insert(3); b->Insert(4);
a->Intersect(b)->Size()->PrintLine(); # 2IsEmpty #
Checks to see if the set is empty
method : public : IsEmpty() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if empty, false otherwise |
Example
sh := Collection.SetHash->New()<String>;
sh->IsEmpty()->PrintLine(); # true
sh->Insert("z");
sh->IsEmpty()->PrintLine(); # falseIsSubset #
Checks that every element of this set is in the other set. The empty set is a subset of every set.
method : public : IsSubset(that:SetHash<K>) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| that | SetHash<K> | other set (Nil counts as empty) |
Return
| Type | Description |
|---|---|
| Bool | true if this set is a subset of the other, false otherwise |
Example
a := Collection.SetHash->New()<IntRef>;
a->Insert(1); a->Insert(2); a->Insert(3);
b := Collection.SetHash->New()<IntRef>;
b->Insert(2); b->Insert(3); b->Insert(4);
a->Intersect(b)->IsSubset(b)->PrintLine(); # true
a->IsSubset(b)->PrintLine(); # falseIsSuperset #
Checks that every element of the other set is in this set
method : public : IsSuperset(that:SetHash<K>) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| that | SetHash<K> | other set (Nil counts as empty) |
Return
| Type | Description |
|---|---|
| Bool | true if this set is a superset of the other, false otherwise |
Example
a := Collection.SetHash->New()<IntRef>;
a->Insert(1); a->Insert(2); a->Insert(3);
b := Collection.SetHash->New()<IntRef>;
b->Insert(2); b->Insert(3); b->Insert(4);
a->Union(b)->IsSuperset(a)->PrintLine(); # trueNew # constructor
Default constructor
New()Example
sh := Collection.SetHash->New()<String>;
sh->Insert("hello");
sh->Size()->PrintLine(); # 1Remove #
Removes a key from the set
method : public : Remove(key:K) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| key | K | key for value to remove |
Return
| Type | Description |
|---|---|
| Bool | true if removed, false otherwise |
Example
sh := Collection.SetHash->New()<String>;
sh->Insert("tmp");
sh->Remove("tmp")->PrintLine(); # true
sh->Has("tmp")->PrintLine(); # falseSize #
Size of set
method : public : Size() ~ IntReturn
| Type | Description |
|---|---|
| Int | number of elements |
Example
sh := Collection.SetHash->New()<String>;
sh->Insert("a"); sh->Insert("b");
sh->Size()->PrintLine(); # 2SymmetricDifference #
Elements in exactly one of the two sets. Neither set is changed.
method : public : SymmetricDifference(that:SetHash<K>) ~ SetHash<K>Parameters
| Name | Type | Description |
|---|---|---|
| that | SetHash<K> | other set (Nil counts as empty) |
Return
| Type | Description |
|---|---|
| SetHash<K> | new set |
Example
a := Collection.SetHash->New()<IntRef>;
a->Insert(1); a->Insert(2); a->Insert(3);
b := Collection.SetHash->New()<IntRef>;
b->Insert(2); b->Insert(3); b->Insert(4);
a->SymmetricDifference(b)->Size()->PrintLine(); # 2 (1 and 4)ToArray #
Gets all elements as an array
method : public : ToArray() ~ K[]Return
| Type | Description |
|---|---|
| K[] | array of elements |
Example
set := Collection.SetHash->New()<String>;
set->Insert("p"); set->Insert("q");
set->ToArray()->Size()->PrintLine(); # 2ToString #
Formats the collection into a string. If an element implements the 'Stringify' interface, it's 'ToString()' is called.
method : public : ToString() ~ StringReturn
| Type | Description |
|---|---|
| String | string representation |
Example
sh := Collection.SetHash->New()<String>;
sh->Insert("m"); sh->Insert("n");
sh->ToString()->PrintLine();ToVector #
Gets all elements as a vector; the same as GetKeys
method : public : ToVector() ~ Vector<K>Return
| Type | Description |
|---|---|
| Vector<K> | vector of elements |
Example
set := Collection.SetHash->New()<String>;
set->Insert("x"); set->Insert("y");
set->ToVector()->Size()->PrintLine(); # 2Union #
Elements in this set, the other set, or both. Neither set is changed.
method : public : Union(that:SetHash<K>) ~ SetHash<K>Parameters
| Name | Type | Description |
|---|---|---|
| that | SetHash<K> | other set (Nil counts as empty) |
Return
| Type | Description |
|---|---|
| SetHash<K> | new set |
Example
a := Collection.SetHash->New()<IntRef>;
a->Insert(1); a->Insert(2); a->Insert(3);
b := Collection.SetHash->New()<IntRef>;
b->Insert(2); b->Insert(3); b->Insert(4);
a->Union(b)->Size()->PrintLine(); # 4