Struct DynamicArray

GC-free dynamic array implementation. Very efficient for small-sized arrays.

struct DynamicArray(T, ulong chunkSize = 32) ;

Methods

NameDescription
data () Get slice of data
free () Free dynamically allocated memory used by array.
insertBack (c) Append single element c to the end.
insertBack (s) Append all elements of slice s to the end.
insertFront (c) Append element to the start.
insertFront (s) Append all elements of slice s to the start.
insertKey (i, v) Inserts an element by a given index (resizing an array and shifting elements).
length () Get number of elements in array.
opApply (dg) Iterating over array via foreach.
opApply (dg) Iterating over array via foreach.
opApplyReverse (dg) Iterating over array via foreach_reverse.
opApplyReverse (dg) Iterating over array via foreach_reverse.
opIndex (index) Access element by index.
opIndexAssign (t, index) Set element t for index.
opOpAssign (c) Same as insertBack, but in operator form.
opOpAssign (s) Same as insertBack, but in operator form.
opSlice (start, end) Access by slice.
removeBack (n) Remove n of elements from the end.
removeFirst (obj) If obj is in array, remove its first occurence and return true. Otherwise do nothing and return false.
removeFront (n) Remove n of elements from the start.
removeKey (i) Removes an element by a given index.
reserve (amount) Preallocate memory without resizing.
resize (newLen, initValue) Resize array and initialize newly added elements with initValue.
shiftLeft (n) Shift contents of array to the left by n positions. Does not change the size of array. n of last elements becomes default initialized.
shiftRight () Shift contents of array to the right. It inreases the size of array by 1. The first element becomes default initialized.
storage () Get pointer to stored data

Example

auto arr = New!(int[])(3);
arr[0] = 1; arr[1] = 2; arr[2] = 3;

reallocateArray(arr, 2);
assert(arr.length == 2);
assert(arr[0] == 1);
assert(arr[1] == 2);

reallocateArray(arr, 4);
assert(arr.length == 4);
assert(arr[0] == 1);
assert(arr[1] == 2);