namespace sys.core.lang
class CArray
A class representing a simple C array, a continuous indexable memory block. It uses static memory allocation. It does not store anywhere in memory the actual size of the block or the number of elements in the array.
In C using such arrays behaves mostly like using a pointer, since the array length is not available. Z2 makes the array length available, even though it is not stored, by using compile time information when possible and passing the actual length in though a hidden parameter, when not.
this{};
this{item: T};
Creates a new static array, filling each element of the array.
The elements of the array are either default constructed or copied over from the provided parameter.
item | items will be copied from this value |
---|
def Fill(value: T);
def Fill(items: Vector);
def Fill(items: CArray);
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.
value | the value to fill with |
---|---|
items | the array to use |
func FindIndex(item: T): PtrSize;
func FindIndex(item: T, start: PtrSize): PtrSize;
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.
item | the item to search for |
---|---|
start | the start index for the search |
the index where the item was found
func BinaryIndex(item: T): PtrSize;
func BinaryIndex(item: T, start: PtrSize): PtrSize;
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.
item | the item to search for |
---|---|
start | the start index for the search |
the index where the item was found
def Insert(pos: PtrSize, item: T);
Inserts an item into the array at a given position.
Since the array is static, it can not grow in size. Instead elements are pushed out of the array and destroyed.
pos | the position to insert to |
---|---|
item | the item to insert |
def Delete(item: T): PtrSize;
Searches for the first occurrence of an item within the array and if found it removes it.
Since the array is static, it can not shrink in size. Instead elements are copied around and the free spaces are default constructed.
item | the item to delete |
---|
the number of deleted items
def DeleteAll(item: T): PtrSize;
Searches for all the occurrences of an item within the array and if found removes them all.
Since the array is static, it can not shrink in size. Instead elements are copied around and the free spaces are default constructed.
item | the item to delete |
---|
the number of deleted items
def DeleteIndex(pos: PtrSize): PtrSize;
Deletes an item at a given index from the array.
Since the array is static, it can not shrink in size. Instead elements are copied around and the free spaces are default constructed.
pos | the index to delete |
---|
the number of deleted items
def Reverse();
def Reverse(start: PtrSize, end: PtrSize);
Reverses the contents of the array, from beginning to end or between two input indices.
start | the start index |
---|---|
end | the end index |
func Sum(): T;
Returns the sum of all the items in the array.
the sum
def Sort();
def Sort(low: Int, high: Int);
Sorts the content of the array in ascending order, from beginning to end or only sorting the values between two input indices.
low | the start index |
---|---|
high | the end index |
def SortDesc();
def SortDesc(low: Int, high: Int);
Sorts the content of the array in descending order, from beginning to end or only sorting the values between two input indices.
low | the start index |
---|---|
high | the end index |
func @write(ref stream: Stream);
Writes each element of the array to a stream as an Utf8 text.
The item count is not included.
stream | the output stream |
---|
property IsEmpty: Bool; get;
Returns `false` because the array is static and always has a non-zero length.