Templatized array

Templatized array with bounds checking. 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);
int b = a[0];

Include: #include <dmsdk/dlib/array.h>
STRUCTS
class dmArray<T> Templatized array with bounds checking.
FUNCTIONS
void dmArray() constructor. empty auto-allocated memory
void dmArray(T* user_array, uint32_t size, uint32_t capacity) constructor. user-allocated memory
void ~dmArray() array destructor
T* Begin() array begin
T* End() array end
T& Front() array front
const T& Front() array front (const)
T& Back() array back
const T& Back() array back (const)
uint32_t Size() size of array
uint32_t Capacity() capacity of array
boolean Full() array full
boolean Empty() array empty
uint32_t Remaining() remaining size of array
T& operator[](uint32_t index) array operator[]
const T& operator[](uint32_t index) array operator[] (const)
void SetCapacity(uint32_t capacity) array set capacity
void OffsetCapacity(uint32_t offset) array offset capacity
void SetSize(uint32_t size) array set size
T& EraseSwap(uint32_t index) array eraseswap
T& EraseSwapRef(T& element) array reference eraseswap
void Push(const T& element) array push
void PushArray(const T& array, uint32_t count) array push array
void Pop() array pop
void Swap(dmArray``& rhs) array swap
void Map( fn,  ctx) map a function on all values

Functions

dmArray

void dmArray()

constructor. empty auto-allocated memory

PARAMETERS

EXAMPLES

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

dmArray

void dmArray(T* user_array, uint32_t size, uint32_t capacity)

user-allocated array with initial size and capacity

PARAMETERS

T* user_array User-allocated array to be used as storage.
uint32_t size Initial size
uint32_t capacity Initial capacity

~dmArray

void ~dmArray()

Only frees memory when auto-allocated.

PARAMETERS


Begin

T* Begin()

Pointer to the start of the backing storage

PARAMETERS

RETURNS

T* pointer to start of memory

End

T* End()

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

PARAMETERS

RETURNS

T* pointer to end of memory

Front

T& Front()

First element of the array

PARAMETERS

RETURNS

T& reference to the first element

Front

const T& Front()

First element of the array (const)

PARAMETERS

RETURNS

const T& const-reference to the first element

Back

T& Back()

Last element of the array

PARAMETERS

RETURNS

T& reference to the last element

Back

const T& Back()

Last element of the array (const)

PARAMETERS

RETURNS

const T& const-reference to the last element

Size

uint32_t Size()

Size of the array in elements

PARAMETERS

RETURNS

uint32_t array size

Capacity

uint32_t Capacity()

Capacity is currently allocated storage.

PARAMETERS

RETURNS

uint32_t array capacity

Full

boolean Full()

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

PARAMETERS

RETURNS

boolean true if the array is full

Empty

boolean Empty()

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

PARAMETERS

RETURNS

boolean true if the array is empty

Remaining

uint32_t Remaining()

Amount of additional elements that can be stored

PARAMETERS

RETURNS

uint32_t amount of additional elements that can be stored

operator[]

T& operator[](uint32_t index)

Retrieve an element by index

PARAMETERS

uint32_t index array index

RETURNS

T& reference to the element at the specified index

operator[]

const T& operator[](uint32_t index)

Retrieve an element by index (const)

PARAMETERS

uint32_t index array index

RETURNS

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

SetCapacity

void SetCapacity(uint32_t 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

uint32_t capacity capacity of the array

OffsetCapacity

void OffsetCapacity(uint32_t offset)

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

uint32_t offset relative amount of elements to change the capacity

SetSize

void SetSize(uint32_t size)

Set size of the array

PARAMETERS

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

EraseSwap

T& EraseSwap(uint32_t index)

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

uint32_t index index of the element to remove

RETURNS

T& reference to the new element at index

EraseSwapRef

T& EraseSwapRef(T& element)

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

T& element reference to the element to remove.

RETURNS

T& reference to the new referenced element

Push

void Push(const T& element)

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

PARAMETERS

const T& element element element to add

PushArray

void PushArray(const T& array, uint32_t count)

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

PARAMETERS

const T& array array of elements to add
uint32_t count amount of elements in the array

Pop

void Pop()

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

PARAMETERS


Swap

void Swap(dmArray``& rhs)

Swap the content of two arrays

PARAMETERS

dmArray``& rhs reference to array to swap content with

Map

void Map( fn,  ctx)

map a function on all values

PARAMETERS

fn function that will be called for each element
ctx user defined context that will be passed in with each callback

Structs

dmArray

TYPE

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).