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.
Set
Ordered set of generic objects
Example
# insert elements
set := Collection.Set->New()<String>;
set->Insert("San Francisco");
set->Insert("Oakland");
set->Insert("East Bay");
# get size
set->Size()->PrintLine();
# get value by key
set->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.Set->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:Set<K>) ~ Set<K>Parameters
| Name | Type | Description |
|---|---|---|
| that | Set<K> | other set (Nil counts as empty) |
Return
| Type | Description |
|---|---|
| Set<K> | new set |
Example
a := Collection.Set->New()<IntRef>;
a->Insert(1); a->Insert(2); a->Insert(3);
b := Collection.Set->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) ~ Set<K>Parameters
| Name | Type | Description |
|---|---|---|
| f | (K)~Nil | function called |
Return
| Type | Description |
|---|---|
| Set<K> | this set |
Example
set := Collection.Set->New()<String>;
set->Insert("x"); set->Insert("y");
set->Each(\(s) => { s->PrintLine(); });Empty #
Clears the set
method : public : Empty() ~ NilExample
set := Collection.Set->New()<String>;
set->Insert("item");
set->Empty();
set->IsEmpty()->PrintLine(); # trueFilter #
Keeps the elements a function accepts. The set is not changed.
method : public : Filter(f:(K)~Bool) ~ Set<K>Parameters
| Name | Type | Description |
|---|---|---|
| f | (K)~Bool | function that returns true for each element to keep |
Return
| Type | Description |
|---|---|
| Set<K> | new set |
Example
set := Collection.Set->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
set := Collection.Set->New()<String>;
set->Insert("a"); set->Insert("b");
keys := set->GetKeys()<String>;
each(k := keys) { k->PrintLine(); }Has #
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
set := Collection.Set->New()<String>;
set->Insert("found");
set->Has("found")->PrintLine(); # true
set->Has("missing")->PrintLine(); # falseInsert #
Inserts a key into the set
method : public : Insert(key:K) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| key | K | key |
Example
set := Collection.Set->New()<String>;
set->Insert("apple");
set->Insert("apple"); # duplicate ignored
set->Size()->PrintLine(); # 1Intersect #
Elements in both this set and the other set. Neither set is changed.
method : public : Intersect(that:Set<K>) ~ Set<K>Parameters
| Name | Type | Description |
|---|---|---|
| that | Set<K> | other set (Nil counts as empty) |
Return
| Type | Description |
|---|---|
| Set<K> | new set |
Example
a := Collection.Set->New()<IntRef>;
a->Insert(1); a->Insert(2); a->Insert(3);
b := Collection.Set->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
set := Collection.Set->New()<String>;
set->IsEmpty()->PrintLine(); # true
set->Insert("z");
set->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:Set<K>) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| that | Set<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.Set->New()<IntRef>;
a->Insert(1); a->Insert(2); a->Insert(3);
b := Collection.Set->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:Set<K>) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| that | Set<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.Set->New()<IntRef>;
a->Insert(1); a->Insert(2); a->Insert(3);
b := Collection.Set->New()<IntRef>;
b->Insert(2); b->Insert(3); b->Insert(4);
a->Union(b)->IsSuperset(a)->PrintLine(); # trueNew # constructor
Default constructor
New()Example
set := Collection.Set->New()<String>;
set->Insert("hello");
set->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
set := Collection.Set->New()<String>;
set->Insert("go");
set->Remove("go")->PrintLine(); # true
set->Has("go")->PrintLine(); # falseSize #
Size of map
method : public : Size() ~ IntReturn
| Type | Description |
|---|---|
| Int | size of queue |
Example
set := Collection.Set->New()<String>;
set->Insert("a"); set->Insert("b"); set->Insert("c");
set->Size()->PrintLine(); # 3SymmetricDifference #
Elements in exactly one of the two sets. Neither set is changed.
method : public : SymmetricDifference(that:Set<K>) ~ Set<K>Parameters
| Name | Type | Description |
|---|---|---|
| that | Set<K> | other set (Nil counts as empty) |
Return
| Type | Description |
|---|---|
| Set<K> | new set |
Example
a := Collection.Set->New()<IntRef>;
a->Insert(1); a->Insert(2); a->Insert(3);
b := Collection.Set->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.Set->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
set := Collection.Set->New()<String>;
set->Insert("x"); set->Insert("y");
set->ToString()->PrintLine(); # [x,y]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.Set->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:Set<K>) ~ Set<K>Parameters
| Name | Type | Description |
|---|---|---|
| that | Set<K> | other set (Nil counts as empty) |
Return
| Type | Description |
|---|---|
| Set<K> | new set |
Example
a := Collection.Set->New()<IntRef>;
a->Insert(1); a->Insert(2); a->Insert(3);
b := Collection.Set->New()<IntRef>;
b->Insert(2); b->Insert(3); b->Insert(4);
a->Union(b)->Size()->PrintLine(); # 4