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.SetCapacity(1);
a.Push(1);
int b = a[0];

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

Functions

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

Begin

T* Begin()

Pointer to the start of the backing storage

PARAMETERS

RETURNS

T* pointer to start of memory

Begin

const T* Begin()

Pointer to the start of the backing storage

PARAMETERS

RETURNS

const T* pointer to start of memory

Capacity

uint32_t Capacity()

Capacity is currently allocated storage.

PARAMETERS

RETURNS

uint32_t array capacity

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(T* user_array, uint32_t size, uint32_t capacity, bool user_allocated)

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
bool user_allocated If false, the ownership is transferred to the dmArray

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

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

End

const T* End()

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

PARAMETERS

RETURNS

const T* pointer to end of memory

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

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

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

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

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

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

Pop

void Pop()

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

PARAMETERS


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

Remaining

uint32_t Remaining()

Amount of additional elements that can be stored

PARAMETERS

RETURNS

uint32_t amount of additional elements that can be stored

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

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

Size

uint32_t Size()

Size of the array in elements

PARAMETERS

RETURNS

uint32_t array size

Swap

void Swap(dmArray``& rhs)

Swap the content of two arrays

PARAMETERS

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

~dmArray

void ~dmArray()

Only frees memory when auto-allocated.

PARAMETERS


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


Macros

DM_ARRAY_SIZE

get number of elements in C array

Array [type:]