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.
HashSet
Hash-based set for O(1) membership testing without storing values
Example
set := Collection.HashSet->New()<IntRef>;
set->Add(IntRef->New(1));
set->Add(IntRef->New(2));
set->Add(IntRef->New(3));
set->Has(IntRef->New(2))->PrintLine(); # true
set->Has(IntRef->New(5))->PrintLine(); # false
set->Remove(IntRef->New(2));
set->Has(IntRef->New(2))->PrintLine(); # falseOperations
- New
- Add
- Difference
- Each
- Empty
- Filter
- GetKeys
- Has
- Insert
- Intersect
- IsEmpty
- IsSubset
- IsSuperset
- Remove
- Size
- SymmetricDifference
- ToArray
- ToString
- ToVector
- Union
Add #
Adds a value to the set
method : public : Add(value:H) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| value | H | value to add |
Return
| Type | Description |
|---|---|
| Bool | true if added, false if already exists |
Example
set := Collection.HashSet->New()<String>;
set->Add("apple")->PrintLine(); # true
set->Add("apple")->PrintLine(); # false (duplicate)
set->Size()->PrintLine(); # 1Difference #
Elements in this set that are not in the other set. Neither set is changed.
method : public : Difference(that:HashSet<H>) ~ HashSet<H>Parameters
| Name | Type | Description |
|---|---|---|
| that | HashSet<H> | other set (Nil counts as empty) |
Return
| Type | Description |
|---|---|
| HashSet<H> | new set |
Example
a := Collection.HashSet->New()<IntRef>;
a->Insert(1); a->Insert(2); a->Insert(3);
b := Collection.HashSet->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:(H)~Nil) ~ HashSet<H>Parameters
| Name | Type | Description |
|---|---|---|
| f | (H)~Nil | function called |
Return
| Type | Description |
|---|---|
| HashSet<H> | this set |
Example
set := Collection.HashSet->New()<String>;
set->Insert("x"); set->Insert("y");
set->Each(\(s) => { s->PrintLine(); });Empty #
Clears the set
method : public : Empty() ~ NilExample
set := Collection.HashSet->New()<String>;
set->Add("a");
set->Empty();
set->IsEmpty()->PrintLine(); # trueFilter #
Keeps the elements a function accepts. The set is not changed.
method : public : Filter(f:(H)~Bool) ~ HashSet<H>Parameters
| Name | Type | Description |
|---|---|---|
| f | (H)~Bool | function that returns true for each element to keep |
Return
| Type | Description |
|---|---|
| HashSet<H> | new set |
Example
set := Collection.HashSet->New()<String>;
set->Insert("apple"); set->Insert("kiwi");
set->Filter(\(s) => s->Size() > 4)->Size()->PrintLine(); # 1GetKeys #
Gets all values as a vector; the same as ToVector, for the method name Set and SetHash use
method : public : GetKeys() ~ Vector<H>Return
| Type | Description |
|---|---|
| Vector<H> | vector of values |
Example
set := Collection.HashSet->New()<String>;
set->Insert("x"); set->Insert("y");
set->GetKeys()->Size()->PrintLine(); # 2Has #
Checks if a value exists in the set
method : public : Has(value:H) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| value | H | value to check |
Return
| Type | Description |
|---|---|
| Bool | true if found, false otherwise |
Example
set := Collection.HashSet->New()<String>;
set->Add("hello");
set->Has("hello")->PrintLine(); # true
set->Has("world")->PrintLine(); # falseInsert #
Inserts a value into the set; the same as Add, for the method name Set and SetHash use
method : public : Insert(value:H) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| value | H | value to insert |
Example
set := Collection.HashSet->New()<String>;
set->Insert("apple");
set->Has("apple")->PrintLine(); # trueIntersect #
Elements in both this set and the other set. Neither set is changed.
method : public : Intersect(that:HashSet<H>) ~ HashSet<H>Parameters
| Name | Type | Description |
|---|---|---|
| that | HashSet<H> | other set (Nil counts as empty) |
Return
| Type | Description |
|---|---|
| HashSet<H> | new set |
Example
a := Collection.HashSet->New()<IntRef>;
a->Insert(1); a->Insert(2); a->Insert(3);
b := Collection.HashSet->New()<IntRef>;
b->Insert(2); b->Insert(3); b->Insert(4);
a->Intersect(b)->Size()->PrintLine(); # 2IsEmpty #
Checks if set is empty
method : public : IsEmpty() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if empty, false otherwise |
Example
set := Collection.HashSet->New()<String>;
set->IsEmpty()->PrintLine(); # true
set->Add("x");
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:HashSet<H>) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| that | HashSet<H> | 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.HashSet->New()<IntRef>;
a->Insert(1); a->Insert(2); a->Insert(3);
b := Collection.HashSet->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:HashSet<H>) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| that | HashSet<H> | 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.HashSet->New()<IntRef>;
a->Insert(1); a->Insert(2); a->Insert(3);
b := Collection.HashSet->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.HashSet->New()<String>;
set->Add("apple");
set->Add("banana");
set->Size()->PrintLine(); # 2Remove #
Removes a value from the set
method : public : Remove(value:H) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| value | H | value to remove |
Return
| Type | Description |
|---|---|
| Bool | true if removed, false if not found |
Example
set := Collection.HashSet->New()<String>;
set->Add("hello");
set->Remove("hello")->PrintLine(); # true
set->Has("hello")->PrintLine(); # falseSize #
Size of set
method : public : Size() ~ IntReturn
| Type | Description |
|---|---|
| Int | number of elements |
Example
set := Collection.HashSet->New()<String>;
set->Add("a");
set->Add("b");
set->Size()->PrintLine(); # 2SymmetricDifference #
Elements in exactly one of the two sets. Neither set is changed.
method : public : SymmetricDifference(that:HashSet<H>) ~ HashSet<H>Parameters
| Name | Type | Description |
|---|---|---|
| that | HashSet<H> | other set (Nil counts as empty) |
Return
| Type | Description |
|---|---|
| HashSet<H> | new set |
Example
a := Collection.HashSet->New()<IntRef>;
a->Insert(1); a->Insert(2); a->Insert(3);
b := Collection.HashSet->New()<IntRef>;
b->Insert(2); b->Insert(3); b->Insert(4);
a->SymmetricDifference(b)->Size()->PrintLine(); # 2 (1 and 4)ToArray #
Gets all values as an array
method : public : ToArray() ~ H[]Return
| Type | Description |
|---|---|
| H[] | array of values |
Example
set := Collection.HashSet->New()<String>;
set->Add("p");
set->Add("q");
arr := set->ToArray();
arr->Size()->PrintLine(); # 2ToString #
Formats the set into a string
method : public : ToString() ~ StringReturn
| Type | Description |
|---|---|
| String | string representation |
Example
set := Collection.HashSet->New()<String>;
set->Add("a");
set->Add("b");
set->ToString()->PrintLine();ToVector #
Gets all values as a vector
method : public : ToVector() ~ Vector<H>Return
| Type | Description |
|---|---|
| Vector<H> | vector of values |
Example
set := Collection.HashSet->New()<String>;
set->Add("x");
set->Add("y");
v := set->ToVector()<String>;
v->Size()->PrintLine(); # 2Union #
Elements in this set, the other set, or both. Neither set is changed.
method : public : Union(that:HashSet<H>) ~ HashSet<H>Parameters
| Name | Type | Description |
|---|---|---|
| that | HashSet<H> | other set (Nil counts as empty) |
Return
| Type | Description |
|---|---|
| HashSet<H> | new set |
Example
a := Collection.HashSet->New()<IntRef>;
a->Insert(1); a->Insert(2); a->Insert(3);
b := Collection.HashSet->New()<IntRef>;
b->Insert(2); b->Insert(3); b->Insert(4);
a->Union(b)->Size()->PrintLine(); # 4