v2026.5.3
All Bundles
Bundle Generic collections library: Vector (dynamic array), Map (hash map), Set, MultiMap, Stack, Queue, and Pair. Provides typed iteration, sorting, filtering, and functional operations (Reduce, Any, All). Compile with -lib gen_collect.

CompareVector

Growable array of comparable generics

function : Example() ~ Nil {
   # insert elements
   vector := Collection.CompareVector->New()<IntRef>;
   vector->AddBack(4);
   vector->AddBack(1);
   vector->AddBack(5);
   vector->AddBack(9);
   vector->AddBack(2);
   vector->AddBack(5);

   # remove last item
   vector->RemoveBack();

   # get size
   vector->Size()->PrintLine();
   
   # get elements
   (vector->Get(0) + vector->Get(1))->PrintLine();

   # sort elements
   vector->Sort();

   # print all items with a loop
   each(item := vector) {
   item->PrintLine();
   };

   # print all items with a function
   vector->Each(Show(IntRef) ~ Nil);
}

function : Show(value : IntRef) ~ Nil {
   value->PrintLine();
}

Operations

AddBack #

Adds a value to the end

method : public : AddBack(value:H) ~ Nil

Parameters

NameTypeDescription
valueHvalue to append

Example

v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(10));
v->AddBack(IntRef->New(20));
v->Size()->PrintLine(); # 2

All #

Checks if all elements match the predicate

method : public : All(f:(H)~Bool) ~ Bool

Parameters

NameTypeDescription
f(H)~Boolpredicate function

Return

TypeDescription
Booltrue if all elements match, false otherwise

Example

v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(2)); v->AddBack(IntRef->New(4)); v->AddBack(IntRef->New(6));
v->All(\(x : IntRef) ~ Bool { return x->Get() % 2 = 0; })->PrintLine(); # true
v->All(\(x : IntRef) ~ Bool { return x->Get() > 3; })->PrintLine();     # false

Any #

Checks if any element matches the predicate

method : public : Any(f:(H)~Bool) ~ Bool

Parameters

NameTypeDescription
f(H)~Boolpredicate function

Return

TypeDescription
Booltrue if any element matches, false otherwise

Example

v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(4)); v->AddBack(IntRef->New(9));
v->Any(\(x : IntRef) ~ Bool { return x->Get() > 5; })->PrintLine(); # true
v->Any(\(x : IntRef) ~ Bool { return x->Get() > 10; })->PrintLine(); # false

BinarySearch # native

Performs a binary search O(log n)

method : public : native : BinarySearch(value:H) ~ Int

Parameters

NameTypeDescription
valueHvalue to search for

Return

TypeDescription
Intindex of found value, -1 if not found

Example

v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(3)); v->AddBack(IntRef->New(5));
v->Sort();
v->BinarySearch(IntRef->New(3))->PrintLine(); # 1

Compress #

Compresses the Vector freeing unused memory

method : public : Compress() ~ Nil

Example

v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(2));
v->Compress();
v->Size()->PrintLine(); # 2

Count #

Counts elements matching the predicate

method : public : Count(f:(H)~Bool) ~ Int

Parameters

NameTypeDescription
f(H)~Boolpredicate function

Return

TypeDescription
Intcount of matching elements

Example

v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(2)); v->AddBack(IntRef->New(3));
v->Count(\(x : IntRef) ~ Bool { return x->Get() > 1; })->PrintLine(); # 2

Distinct #

Returns a new vector with duplicate elements removed

method : public : Distinct() ~ CompareVector<H>

Return

TypeDescription
CompareVector<H>vector with unique elements only

Example

v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(2)); v->AddBack(IntRef->New(1));
unique := v->Distinct()<IntRef>;
unique->Size()->PrintLine(); # 2

Each #

Function called for each element

method : public : Each(f:(H)~Nil) ~ CompareVector<H>

Parameters

NameTypeDescription
f(H)~Nilfunction called

Example

v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(2));
v->Each(\(n : IntRef) ~ Nil { n->PrintLine(); });

Empty #

Clears the vector

method : public : Empty() ~ Nil

Example

v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1));
v->Empty();
v->IsEmpty()->PrintLine(); # true

Filter #

Uses the given function to filter out values

method : public : Filter(f:(H)~Bool) ~ CompareVector<H>

Parameters

NameTypeDescription
f(H)~Boolfunction to use a filter. If the function evaluates to true the value is added to the collection.

Return

TypeDescription
CompareVector<H>filtered vector

Example

v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(2)); v->AddBack(IntRef->New(3));
evens := v->Filter(\(x : IntRef) ~ Bool { return x->Get() % 2 = 0; })<IntRef>;
evens->Size()->PrintLine(); # 1

Find #

Finds a given value in the vector via linear search

method : public : Find(value:H) ~ Int

Parameters

NameTypeDescription
valueHvalue to search for

Return

TypeDescription
Intindex of found value, -1 if not found

Example

v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(10)); v->AddBack(IntRef->New(20));
v->Find(IntRef->New(20))->PrintLine();  # 1
v->Find(IntRef->New(99))->PrintLine();  # -1

FindFirst #

Finds the first element matching the predicate

method : public : FindFirst(f:(H)~Bool) ~ H

Parameters

NameTypeDescription
f(H)~Boolpredicate function

Return

TypeDescription
Hfirst matching element, or Nil if not found

Example

v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(3)); v->AddBack(IntRef->New(7)); v->AddBack(IntRef->New(2));
found := v->FindFirst(\(x : IntRef) ~ Bool { return x->Get() > 4; });
found->PrintLine(); # 7

FindIndex #

Finds the index of the first element matching the predicate

method : public : FindIndex(f:(H)~Bool) ~ Int

Parameters

NameTypeDescription
f(H)~Boolpredicate function

Return

TypeDescription
Intindex of first matching element, or -1 if not found

Example

v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(3)); v->AddBack(IntRef->New(7)); v->AddBack(IntRef->New(2));
v->FindIndex(\(x : IntRef) ~ Bool { return x->Get() > 4; })->PrintLine(); # 1

First #

Gets the first value

method : public : First() ~ H

Return

TypeDescription
Hfirst value, or Nil if not set

Example

v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(5)); v->AddBack(IntRef->New(10));
v->First()->PrintLine(); # 5

Get #

Gets an indexed value

method : public : Get(index:Int) ~ H

Parameters

NameTypeDescription
indexIntindex

Return

TypeDescription
Hvalue

Example

v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(10)); v->AddBack(IntRef->New(20));
v->Get(0)->PrintLine(); # 10
v->Get(1)->PrintLine(); # 20

Has #

Check of the given value is in the vector

method : public : Has(value:H) ~ Bool

Parameters

NameTypeDescription
valueHvalue to check for

Return

TypeDescription
Booltrue if found, false otherwise

Example

v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(5)); v->AddBack(IntRef->New(10));
v->Has(IntRef->New(5))->PrintLine();  # true
v->Has(IntRef->New(99))->PrintLine(); # false

IsEmpty #

Checks to see if the vector is empty

method : public : IsEmpty() ~ Bool

Return

TypeDescription
Booltrue if empty, false otherwise

Example

v := Collection.CompareVector->New()<IntRef>;
v->IsEmpty()->PrintLine(); # true
v->AddBack(IntRef->New(1));
v->IsEmpty()->PrintLine(); # false

Last #

Gets the last value

method : public : Last() ~ H

Return

TypeDescription
Hlast value, or Nil if not set

Example

v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(2));
v->Last()->PrintLine(); # 2

Limit #

Returns a limited list

method : public : Limit(l:Int) ~ CompareVector<H>

Parameters

NameTypeDescription
lIntlimit

Return

TypeDescription
CompareVector<H>limited list

Example

v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(2)); v->AddBack(IntRef->New(3));
first2 := v->Limit(2);
first2->Size()->PrintLine(); # 2

Map #

Maps the given function to each value in the vector

method : public : Map(f:(H)~H) ~ CompareVector<H>

Parameters

NameTypeDescription
f(H)~Hfunction to apply

Return

TypeDescription
CompareVector<H>newly calculated vector

Example

v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(2));
doubled := v->Map(\(n : IntRef) ~ IntRef { return IntRef->New(n->Get() * 2); });
doubled->Get(0)->PrintLine(); # 2

New # constructor

Default constructor

New()

Example

v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1));
v->Size()->PrintLine(); # 1

New # constructor

Copy constructor

New(values:H[])

Parameters

NameTypeDescription
valuesH[]values to copy

Example

arr := IntRef->New[2];
arr[0] := IntRef->New(1); arr[1] := IntRef->New(2);
v := Collection.CompareVector->New(arr)<IntRef>;
v->Size()->PrintLine(); # 2

New # constructor

Copy constructor

New(values:CompareVector<H>)

Parameters

NameTypeDescription
valuesCompareVector<H>values to copy

Example

src := Collection.CompareVector->New()<IntRef>;
src->AddBack(IntRef->New(5));
copy := Collection.CompareVector->New(src)<IntRef>;
copy->Get(0)->PrintLine(); # 5

Reduce #

Uses the given function to reduce the values

method : public : Reduce(a:H, f:(H,H)~H) ~ H

Parameters

NameTypeDescription
aHinitial value (i.e. accumulator)
f(H,H)~Hfunction to use a reduce

Return

TypeDescription
Hreduced vector

Example

v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(2)); v->AddBack(IntRef->New(3));
sum := v->Reduce(IntRef->New(0), \(a : IntRef, b : IntRef) ~ IntRef { return IntRef->New(a->Get() + b->Get()); });
sum->PrintLine(); # 6

Remove #

Removes an indexed value

method : public : Remove(i:Int) ~ H

Parameters

NameTypeDescription
iIntindex

Return

TypeDescription
Hvalue

Example

v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(2)); v->AddBack(IntRef->New(3));
v->Remove(1)->PrintLine(); # 2
v->Size()->PrintLine();    # 2

RemoveBack #

Removes the last value

method : public : RemoveBack() ~ H

Return

TypeDescription
Hvalue

Example

v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(2));
v->RemoveBack()->PrintLine(); # 2
v->Size()->PrintLine();       # 1

Reverse #

Reverses element order

method : public : Reverse() ~ CompareVector<H>

Return

TypeDescription
CompareVector<H>reversed vector, if the vector is empty or hold 1 item then the original list is returned

Example

v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(2)); v->AddBack(IntRef->New(3));
rev := v->Reverse();
rev->Get(0)->PrintLine(); # 3

Set #

Sets an indexed value

method : public : Set(value:H, index:Int) ~ Bool

Parameters

NameTypeDescription
valueHvalue
indexIntindex

Example

v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1));
v->Set(IntRef->New(99), 0)->PrintLine(); # true
v->Get(0)->PrintLine();                  # 99

Size #

Size of vector

method : public : Size() ~ Int

Return

TypeDescription
Intsize of vector

Example

v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(2));
v->Size()->PrintLine(); # 2

Sort # native

Sorts the values in the vector

method : public : native : Sort() ~ Nil

Example

v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(3)); v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(2));
v->Sort();
v->Get(0)->PrintLine(); # 1

ToArray #

Converts the vector into an object array

method : public : ToArray() ~ H[]

Return

TypeDescription
H[]object array

Example

v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(7)); v->AddBack(IntRef->New(8));
arr := v->ToArray()<IntRef>;
arr[0]->PrintLine(); # 7
arr[1]->PrintLine(); # 8

ToString #

Formats the collection into a string. If an element implements the 'Stringify' interface, it's 'ToString()' is called.

method : public : ToString() ~ String

Return

TypeDescription
Stringstring representation

Example

v := Collection.CompareVector->New()<IntRef>;
v->AddBack(IntRef->New(1)); v->AddBack(IntRef->New(2));
v->ToString()->PrintLine(); # [1,2]