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.
BitSet
Growable set of bits packed 64 to an Int
Example
bits := Collection.BitSet->New(128);
bits->Set(3); bits->Set(64); bits->Set(100);
bits->Get(64)->PrintLine(); # true
bits->Cardinality()->PrintLine(); # 3
bits->NextSetBit(4)->PrintLine(); # 64Operations
And #
Keeps only the bits also set in another set (in place)
method : public : And(that:BitSet) ~ BitSetParameters
| Name | Type | Description |
|---|---|---|
| that | BitSet | other set (Nil clears every bit) |
Return
| Type | Description |
|---|---|
| BitSet | this set |
Example
a := Collection.BitSet->New(8); a->Set(1); a->Set(2);
b := Collection.BitSet->New(8); b->Set(2);
a->And(b)->Cardinality()->PrintLine(); # 1AndNot #
Clears the bits set in another set (in place)
method : public : AndNot(that:BitSet) ~ BitSetParameters
| Name | Type | Description |
|---|---|---|
| that | BitSet | other set (Nil changes nothing) |
Return
| Type | Description |
|---|---|
| BitSet | this set |
Example
a := Collection.BitSet->New(8); a->Set(1); a->Set(2);
b := Collection.BitSet->New(8); b->Set(2);
a->AndNot(b)->Get(2)->PrintLine(); # falseCardinality # native
Counts the set bits
method : public : native : Cardinality() ~ IntReturn
| Type | Description |
|---|---|
| Int | number of set bits |
Example
bits := Collection.BitSet->New(8);
bits->Set(1); bits->Set(2);
bits->Cardinality()->PrintLine(); # 2Clear # native
Clears a bit
method : public : native : Clear(index:Int) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| index | Int | bit index (indexes past the size are already clear) |
Example
bits := Collection.BitSet->New(8);
bits->Set(2);
bits->Clear(2);
bits->Get(2)->PrintLine(); # falseCopy #
Copies the set
method : public : Copy() ~ BitSetReturn
| Type | Description |
|---|---|
| BitSet | new set with the same bits |
Example
a := Collection.BitSet->New(8); a->Set(1);
b := a->Copy();
b->Set(2);
a->Cardinality()->PrintLine(); # 1Empty #
Clears every bit; the size is kept
method : public : Empty() ~ NilExample
bits := Collection.BitSet->New(8);
bits->Set(1);
bits->Empty();
bits->Cardinality()->PrintLine(); # 0Get # native
Gets a bit
method : public : native : Get(index:Int) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| index | Int | bit index |
Return
| Type | Description |
|---|---|
| Bool | true if set, false if clear or out of range |
Example
bits := Collection.BitSet->New(8);
bits->Set(5);
bits->Get(5)->PrintLine(); # true
bits->Get(6)->PrintLine(); # falseGetWords #
Gets the backing words; bit i is bit (i and 63) of word (i >> 6). The array is shared, not copied.
method : public : GetWords() ~ Int[]Return
| Type | Description |
|---|---|
| Int | words |
Example
bits := Collection.BitSet->New(8);
bits->Set(0);
bits->GetWords()[0]->PrintLine(); # 1New # constructor
Default constructor
New()Example
bits := Collection.BitSet->New();
bits->Set(10);
bits->Size()->PrintLine(); # 11New # constructor
Constructor for a number of bits, all clear
New(size:Int)Parameters
| Name | Type | Description |
|---|---|---|
| size | Int | number of bits |
Example
bits := Collection.BitSet->New(65);
bits->Size()->PrintLine(); # 65NextSetBit # native
Finds the next set bit at or after an index
method : public : native : NextSetBit(start:Int) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| start | Int | index to start at |
Return
| Type | Description |
|---|---|
| Int | index of the next set bit, or -1 if there is none |
Example
bits := Collection.BitSet->New(200);
bits->Set(70); bits->Set(130);
bits->NextSetBit(0)->PrintLine(); # 70
bits->NextSetBit(71)->PrintLine(); # 130
bits->NextSetBit(131)->PrintLine(); # -1Or #
Adds the bits set in another set (in place)
method : public : Or(that:BitSet) ~ BitSetParameters
| Name | Type | Description |
|---|---|---|
| that | BitSet | other set (Nil changes nothing) |
Return
| Type | Description |
|---|---|
| BitSet | this set |
Example
a := Collection.BitSet->New(8); a->Set(1);
b := Collection.BitSet->New(8); b->Set(2);
a->Or(b)->Cardinality()->PrintLine(); # 2Set # native
Sets a bit, growing the set when the index is past its size
method : public : native : Set(index:Int) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| index | Int | bit index (negative indexes are ignored) |
Example
bits := Collection.BitSet->New();
bits->Set(200);
bits->Get(200)->PrintLine(); # trueSet #
Sets or clears a bit
method : public : Set(index:Int, value:Bool) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| index | Int | bit index (negative indexes are ignored) |
| value | Bool | true to set, false to clear |
Example
bits := Collection.BitSet->New(8);
bits->Set(2, true);
bits->Get(2)->PrintLine(); # trueSize #
Number of bits: the size given to the constructor, or one past the highest bit ever set if that is larger
method : public : Size() ~ IntReturn
| Type | Description |
|---|---|
| Int | number of bits |
Example
bits := Collection.BitSet->New(10);
bits->Size()->PrintLine(); # 10ToString #
Formats the set into a string of the indexes of its set bits
method : public : ToString() ~ StringReturn
| Type | Description |
|---|---|
| String | string representation |
Example
bits := Collection.BitSet->New(8);
bits->Set(1); bits->Set(5);
bits->ToString()->PrintLine(); # {1,5}Xor #
Flips the bits set in another set (in place)
method : public : Xor(that:BitSet) ~ BitSetParameters
| Name | Type | Description |
|---|---|---|
| that | BitSet | other set (Nil changes nothing) |
Return
| Type | Description |
|---|---|
| BitSet | this set |
Example
a := Collection.BitSet->New(8); a->Set(1); a->Set(2);
b := Collection.BitSet->New(8); b->Set(2); b->Set(3);
a->Xor(b)->Cardinality()->PrintLine(); # 2