Vector
Growable generic array
function : Example() ~ Nil {
# insert elements
vector := Collection.Vector->New()<IntRef>;
vector->AddBack(4);
vector->AddBack(1);
vector->AddBack(5);
vector->AddBack(1);
vector->AddBack(0);
# remove last item
vector->RemoveBack();
# get size
vector->Size()->PrintLine();
# get elements
(vector->Get(0) + vector->Get(1))->PrintLine();
# 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
- New
- AddBack
- Compress
- Each
- Empty
- First
- Get
- IsEmpty
- Last
- Limit
- Map
- Remove
- RemoveBack
- Reverse
- Set
- Size
- Swap
- ToArray
- ToString
AddBack
Adds a value to the end
method : public : AddBack(value:H) ~ Nil
Parameters
Name | Type | Description |
---|---|---|
value | H | value to append |
Each
Function called for each element
method : public : Each(f:(H)~Nil) ~ Vector<H>
Parameters
Name | Type | Description |
---|---|---|
f | (H)~Nil | function called |
First
Gets the first value
method : public : First() ~ H
Return
Type | Description |
---|---|
H | first value, or Nil if not set |
Get
Gets an indexed value
method : public : Get(index:Int) ~ H
Parameters
Name | Type | Description |
---|---|---|
index | Int | index |
Return
Type | Description |
---|---|
H | value, or Nil if invalid index |
IsEmpty
Checks to see if the vector is empty
method : public : IsEmpty() ~ Bool
Return
Type | Description |
---|---|
Bool | true if empty, false otherwise |
Last
Gets the last value
method : public : Last() ~ H
Return
Type | Description |
---|---|
H | last value, or Nil if not set |
Limit
Returns a limited list
method : public : Limit(l:Int) ~ Vector<H>
Parameters
Name | Type | Description |
---|---|---|
l | Int | limit |
Return
Type | Description |
---|---|
Vector<H> | limited list |
Map
Maps the given function to each value in the vector
method : public : Map(f:(H)~H) ~ Vector<H>
Parameters
Name | Type | Description |
---|---|---|
f | (H)~H | function to apply |
Return
Type | Description |
---|---|
Vector<H> | newly calculated vector |
Remove
Removes an indexed value
method : public : Remove(i:Int) ~ H
Parameters
Name | Type | Description |
---|---|---|
i | Int | index |
Return
Type | Description |
---|---|
H | value |
Reverse
Reverses element order
method : public : Reverse() ~ Vector<H>
Return
Type | Description |
---|---|
Vector<H> | reversed vector, if the vector is empty or hold 1 item then the original list is returned |
Set
Sets an indexed value
method : public : Set(value:H, index:Int) ~ Bool
Parameters
Name | Type | Description |
---|---|---|
value | H | value |
index | Int | index |
Swap
Swap two values in the vector
method : public : Swap(a:Int, b:Int) ~ Bool
Parameters
Name | Type | Description |
---|---|---|
a | Int | first value |
b | Int | second value |
Return
Type | Description |
---|---|
Bool | true if values were swapped |
ToArray
Converts the vector into an object array
method : public : ToArray() ~ H[]
Return
Type | Description |
---|---|
H[] | object array |
ToString
Formats the collection into a string. If an element implements the 'Stringify' interface, it's 'ToString()' is called.
method : public : ToString() ~ String
Return
Type | Description |
---|---|
String | string representation |