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. |
MACROS | |
---|---|
DM_ARRAY_SIZE(Array) | get number of elements in C array |
T& Back()
Last element of the array
PARAMETERS
RETURNS
T& |
reference to the last element |
const T& Back()
Last element of the array (const)
PARAMETERS
RETURNS
const T& |
const-reference to the last element |
T* Begin()
Pointer to the start of the backing storage
PARAMETERS
RETURNS
T* |
pointer to start of memory |
const T* Begin()
Pointer to the start of the backing storage
PARAMETERS
RETURNS
const T* |
pointer to start of memory |
uint32_t Capacity()
Capacity is currently allocated storage.
PARAMETERS
RETURNS
uint32_t |
array capacity |
void dmArray()
constructor. empty auto-allocated memory
PARAMETERS
EXAMPLES
dmArray<int> a;
a.Push(1);
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 |
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 |
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 |
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 |
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 |
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 |
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 |
T& Front()
First element of the array
PARAMETERS
RETURNS
T& |
reference to the first element |
const T& Front()
First element of the array (const)
PARAMETERS
RETURNS
const T& |
const-reference to the first element |
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 |
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 |
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 |
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 |
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 |
void Pop()
Remove the last element of the array Only allowed when the size is larger than zero.
PARAMETERS
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 |
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 |
uint32_t Remaining()
Amount of additional elements that can be stored
PARAMETERS
RETURNS
uint32_t |
amount of additional elements that can be stored |
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 |
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 |
uint32_t Size()
Size of the array in elements
PARAMETERS
RETURNS
uint32_t |
array size |
void Swap(dmArray`
Swap the content of two arrays
PARAMETERS
dmArray` |
rhs |
reference to array to swap content with |
void ~dmArray()
Only frees memory when auto-allocated.
PARAMETERS
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).
get number of elements in C array
Array |
[type:] |