class Vector

namespace sys.core.lang
class Vector

A dynamic array with amortized growth cost.

This is the most commonly used array for dynamically allocated data.

Constructors

this

this{copy: Vector};
this{move copy: Vector};

Brief

Copy/move constructor.

Creates a new instance containing a copy of the input parameter, or moves the data if the input parameter is an expiring value.

Parameters

copy the vector to copy/move from

Methods

@attr

def @attr(copy: Vector);
def @attr(move copy: Vector);

Brief

Assignment/move operator.

Assign the current instance with a copy or a move of the input data.

Parameters

copy the vector to copy/move from

Add

def Add(item: T);
def Add(move item: T);
def Add(items: Vector);
def Add(items: CArray);

Brief

Append a single item or a collection to the end of the vector.

Parameters

item the item to append
items the item collection to append

Fill

def Fill(value: T);
def Fill(items: Vector);
def Fill(items: CArray);

Brief

Copies over all the elements in the static array.

If a single value is provided, all elements will be initialized with it.

If an array is provided, elements will be copied over in sequence. If the source array is exhausted, the reading index will be reset to 0 and the copying resumed.

Parameters

value the value to fill with
items the array to use

FindIndex

func FindIndex(item: T): PtrSize;
func FindIndex(item: T, start: PtrSize): PtrSize;

Brief

Searches for an item in the array and returns the first index at which it was found or -1 if the item was not found.

The search starts on an index given by the `start` parameter if present or from index 0 otherwise.

Parameters

item the item to search for
start the start index for the search

Returns

the index where the item was found

BinaryIndex

func BinaryIndex(item: T): PtrSize;
func BinaryIndex(item: T, start: PtrSize): PtrSize;

Brief

Searches for an item in the array and returns the first index at which it was found or -1 if the item was not found. It uses a binary search algorithm and the contents of the container must be sorted in ascending order. If the elements are not sorted, the result is unpredictable.

The search starts on an index given by the `start` parameter if present or from index 0 otherwise.

Parameters

item the item to search for
start the start index for the search

Returns

the index where the item was found

Insert

def Insert(pos: PtrSize, item: T);
def Insert(pos: PtrSize, items: CArray);
def Insert(pos: PtrSize, items: Vector);

Brief

Inserts an item or a collection of items into the array at a given position.

THe array will grow to accommodate the inserted items.

Parameters

pos the position to insert to
item the item to insert
items the items to insert

Delete

def Delete(item: T): PtrSize;
def Delete(items: CArray): PtrSize;
def Delete(items: Vector): PtrSize;

Brief

Searches for the first occurrence of an item within the array and if found it removes it.

Parameters

item the item to delete
items the items to delete

Returns

the number of deleted items

DeleteAll

def DeleteAll(item: T): PtrSize;
def DeleteAll(item: CArray): PtrSize;
def DeleteAll(item: Vector): PtrSize;

Brief

Searches for all the occurrences of an item within the array and if found removes them all.

Parameters

item the item to remove

Returns

the number of deleted items

DeleteIndex

def DeleteIndex(item: PtrSize): PtrSize;
def DeleteIndex(items: CArray): PtrSize;
def DeleteIndex(items: Vector): PtrSize;

Brief

Deletes an item at a given index from the array.

Parameters

item the index to delete
items an array of indices to delete

Returns

the number of deleted items

Reverse

def Reverse();
def Reverse(start: PtrSize, end: PtrSize);

Brief

Reverses the contents of the array, from beginning to end or between two input indices.

Parameters

start the start index
end the end index

Sum

func Sum(): T;

Brief

Returns the sum of all the items in the array.

Returns

the sum

Sort

def Sort();
def Sort(low: Int, high: Int);

Brief

Sorts the content of the array in ascending order, from beginning to end or between two input indices.

Parameters

low the start index
high the end index

SortDesc

def SortDesc();
def SortDesc(low: Int, high: Int);

Brief

Sorts the content of the array in descending order, from beginning to end or between two input indices.

Parameters

low the start index
high the end index

@write

func @write(ref stream: Stream);

Brief

Writes each element of the array to a stream as an Utf8 text.

The item count is included.

Parameters

stream the output stream

@shl

@shl(ref v: Vector, data: T): ref Vector;

Brief

Appends a vector `v` to the array.

Properties

Length

property Length: PtrSize

Brief

Read and writes the length of the vector.


Capacity

property Capacity: PtrSize

Brief

Read and writes the capacity of the vector.

Capacity can't be set lower than the Length.


@index

property @index: ref T; get;

Brief

Reads and writes a given index from the vector.

Accessing an invalid index is an error.


At

property At: ref T; get;

Brief

Reads and writes a given index from the vector.

Accessing an index greater than Length will cause all the missing values to be default constructed.


SysDataPointer

property SysDataPointer: Ptr; get;

Brief

Returns a pointer to the data.