Array

<dmsdk/dlib/array.h>

Templatized array with bounds checking.
template <typename T> class dmArray

The backing storage is either auto-allocated (dynamically allocated) or user-allocated (supplied by user). With exception of changing the size and capacity, all operations are guaranteed to be O(1).

dmArray<int> a;
a.Push(1);

dmArray::Back()

array back

Last element of the array

RETURN

reference -

T& reference to the last element


dmArray::Back()

array back (const)

Last element of the array (const)

RETURN

reference -

const T& const-reference to the last element


dmArray::Begin()

array begin

Pointer to the start of the backing storage

RETURN

pointer -

T* pointer to start of memory


dmArray::Capacity()

capacity of array

Capacity is currently allocated storage.

RETURN

number -

uint32_t array capacity


dmArray::Empty()

array empty

Check if the array is empty. The array is empty when the size is zero.

RETURN

boolean -

boolean true if the array is empty


dmArray::End()

array end

Pointer to the end of the backing storage The end is essentially outside of the used storage.

RETURN

pointer -

T* pointer to end of memory


dmArray::EraseSwap()

array eraseswap

Remove the element at the specified index. The removed element is replaced by the element at the end (if any), thus potentially altering the order. While operation changes the array size, it is guaranteed to be O(1).

PARAMETERS

index -

uint32_t index of the element to remove

RETURN

reference -

T& reference to the new element at index


dmArray::EraseSwapRef()

array reference eraseswap

Remove the element by reference The removed element is replaced by the element at the end (if any), thus potentially altering the order. While operation changes the array size, it is guaranteed to be O(1).

PARAMETERS

element -

T& reference to the element to remove.

RETURN

reference -

T& reference to the new referenced element


dmArray::Front()

array front

First element of the array

RETURN

reference -

T& reference to the first element


dmArray::Front()

array front (const)

First element of the array (const)

RETURN

reference -

const T& const-reference to the first element


dmArray::Full()

array full

Check if the array is full. The array is full when the size is equal to the capacity.

RETURN

boolean -

boolean true if the array is full


dmArray::OffsetCapacity()

array offset capacity

Relative change of capacity Equivalent to SetCapacity(Capacity() + offset). Only allowed for auto-allocated arrays and will result in a new dynamic allocation followed by memcpy of the elements.

PARAMETERS

offset -

uint32_t relative amount of elements to change the capacity


dmArray::Pop()

array pop

Remove the last element of the array Only allowed when the size is larger than zero.


dmArray::Push()

array push

Add an element to the end of the array Only allowed when the capacity is larger than size.

PARAMETERS

element -

const T& element element to add


dmArray::PushArray()

array push array

Add an array of elements to the end of the array Only allowed when the capacity is larger than size + count

PARAMETERS

array -

const T& array of elements to add

count -

uint32_t amount of elements in the array


dmArray::Remaining()

remaining size of array

Amount of additional elements that can be stored

RETURN

number -

uint32_t amount of additional elements that can be stored


dmArray::SetCapacity()

array set capacity

Set the capacity of the array. If the size is less than the capacity, the array is truncated. If it is larger, the array is extended. Only allowed for auto-allocated arrays and will result in a new dynamic allocation followed by memcpy of the elements.

PARAMETERS

capacity -

uint32_t capacity of the array


dmArray::SetSize()

array set size

Set size of the array

PARAMETERS

size -

uint32_t size of the array, must be less or equal to the capacity


dmArray::Size()

size of array

Size of the array in elements

RETURN

number -

uint32_t array size


dmArray::Swap()

array swap

Swap the content of two arrays

PARAMETERS

rhs -

dmArray<T>& reference to array to swap content with


dmArray::dmArray()

empty auto-allocated array

EXAMPLES

dmArray<int>* a = new dmArray<int>();


dmArray::dmArray()

user-allocated array

user-allocated array with initial size and capacity

PARAMETERS

user_array -

T* User-allocated array to be used as storage.

size -

uint32_t Initial size

capacity -

uint32_t Initial capacity


dmArray::operator[]()

array operator[]

Retrieve an element by index

PARAMETERS

index -

uint32_t array index

RETURN

reference -

T& reference to the element at the specified index


dmArray::operator[]()

array operator[] (const)

Retrieve an element by index (const)

PARAMETERS

index -

uint32_t array index

RETURN

reference -

const T& const-reference to the element at the specified index


dmArray::~dmArray()

array destructor

Only frees memory when auto-allocated.