muwerk ustd Library
A low-resource, minimal implementation of Arrays, Maps and Queues for low-resource avrs.
Loading...
Searching...
No Matches
Public Member Functions | List of all members
ustd::array< T > Class Template Reference

Lightweight c++11 array implementation. More...

#include <ustd_array.h>

Public Member Functions

 array (unsigned int startSize=ARRAY_INIT_SIZE, unsigned int maxSize=ARRAY_MAX_SIZE, unsigned int incSize=ARRAY_INC_SIZE, bool shrink=true)
 
 array (const T initarray[], unsigned int count)
 
 array (const array< T > &ar)
 
 ~array ()
 
arrayIterator< T > begin ()
 
arrayIterator< T > end ()
 
arrayIterator< const T > begin () const
 
arrayIterator< const T > end () const
 
bool resize (unsigned int newSize)
 
void setInvalidValue (T &entryInvalidValue)
 
int add (T &entry)
 
bool erase (unsigned int index)
 
bool erase ()
 
operator[] (unsigned int i) const
 
T & operator[] (unsigned int i)
 
bool isEmpty () const
 
unsigned int length () const
 
unsigned int alloclen () const
 

Detailed Description

template<typename T>
class ustd::array< T >

Lightweight c++11 array implementation.

ustd_array.h is a minimal, yet highly portable array data type implementation that runs well on architectures with very limited resources such as attiny 8kb avr.

The array class either:

The library header-only.

Make sure to provide the required platform define before including ustd headers.

An example for dynamic mode:

#define __ATTINY__ 1 // Platform defines required, see doc, mainpage.
#include <ustd_array.h>
intArray[0] = 13; // Memory for array[0] is allocated
intArray.add(3); // the array is extended, if necessary
int p = intArray[0];
printf("[0]:%d [1]:%d length=%d\n", intArray[0], intArray[1], intArray.length())
Lightweight c++11 array implementation.
Definition: ustd_array.h:123
int add(T &entry)
Definition: ustd_array.h:288
unsigned int length() const
Definition: ustd_array.h:378

An example for static mode

#include <ustd_array.h>
// array length is fixed 5 (startSize==maxSize), no dynamic extensions:
ustd::array<int> intArray = ustd::array<int>(5, 5, 0, false);d

Iterators and initializing with const T[] c-arrays:

#include <ustd_array.h>
const int ci[]={1,2,3,4,5};
for (auto i : ia) {
printf("%d\n",i);
}

Constructor & Destructor Documentation

◆ array() [1/3]

template<typename T >
ustd::array< T >::array ( unsigned int  startSize = ARRAY_INIT_SIZE,
unsigned int  maxSize = ARRAY_MAX_SIZE,
unsigned int  incSize = ARRAY_INC_SIZE,
bool  shrink = true 
)
inline

Constructs an array object. All allocation-hints are optional, the array class will allocate memory as needed during writes, if startSize!=maxSize.

Parameters
startSizeThe number of array entries that are allocated during object creation
maxSizeThe maximal limit of records that will be allocated. If startSize < maxSize, the array size will grow automatically as needed.
incSizeThe number of array entries that are allocated as a chunk if the array needs to grow
shrinkBoolean indicating, if the array should deallocate memory, if the array size shrinks (due to erase()).

◆ array() [2/3]

template<typename T >
ustd::array< T >::array ( const T  initarray[],
unsigned int  count 
)
inline

construct array with const T[] c-array of length count

Parameters
initarrayc-array of type T
countnumber of entries in initarray

◆ array() [3/3]

template<typename T >
ustd::array< T >::array ( const array< T > &  ar)
inline

array copy constructor

◆ ~array()

template<typename T >
ustd::array< T >::~array ( )
inline

Free resources

Member Function Documentation

◆ add()

template<typename T >
int ustd::array< T >::add ( T &  entry)
inline

Append an array element after the current end of the array

Parameters
entryarray element that is appended after the last current entry. The new array size must be smaller than maxSize as defined during array creation. New array memory is automatically allocated if within maxSize boundaries.

◆ alloclen()

template<typename T >
unsigned int ustd::array< T >::alloclen ( ) const
inline

Check the number of allocated array-entries, which can be larger than the length of the array.

Returns
number of allocated entries.

◆ begin() [1/2]

template<typename T >
arrayIterator< T > ustd::array< T >::begin ( )
inline

Iterator support: begin()

◆ begin() [2/2]

template<typename T >
arrayIterator< const T > ustd::array< T >::begin ( ) const
inline

Iterator support: begin()

◆ end() [1/2]

template<typename T >
arrayIterator< T > ustd::array< T >::end ( )
inline

Iterator support: end()

◆ end() [2/2]

template<typename T >
arrayIterator< const T > ustd::array< T >::end ( ) const
inline

Iterator support: end()

◆ erase() [1/2]

template<typename T >
bool ustd::array< T >::erase ( )
inline

Delete all array elements. memory might be freed, if shrink=True during array creation.

◆ erase() [2/2]

template<typename T >
bool ustd::array< T >::erase ( unsigned int  index)
inline

Delete array element at given index

Parameters
indexThe array index of the element to be erased. The array size is reduced by 1, and memory might be freed, if shrink=True during array creation.

◆ isEmpty()

template<typename T >
bool ustd::array< T >::isEmpty ( ) const
inline

Check, if array is empty.

Returns
true if array empty, false otherwise. s

◆ length()

template<typename T >
unsigned int ustd::array< T >::length ( ) const
inline

Check number of array-members.

Returns
number of array entries

◆ operator[]() [1/2]

template<typename T >
T & ustd::array< T >::operator[] ( unsigned int  i)
inline

Assign content of array element at i, e.g. myArray[3]=3

◆ operator[]() [2/2]

template<typename T >
T ustd::array< T >::operator[] ( unsigned int  i) const
inline

Read content of array element at i, a=myArray[3] Note: Since version 0.7.0 a read operation never mutates (e.g. extends) the array, earlier version allowed array-extension via read.

◆ resize()

template<typename T >
bool ustd::array< T >::resize ( unsigned int  newSize)
inline

Change the array allocation size.

Note: Usage of this function is optional for optimization. By default, all necessary allocations (and deallocations, if shrink=true during construction was set) are handled automatically.

Parameters
newSizethe new number of array entries, corresponding memory is allocated/freed as necessary.

◆ setInvalidValue()

template<typename T >
void ustd::array< T >::setInvalidValue ( T &  entryInvalidValue)
inline

Set the value that's given back, if read of an invalid index is requested. By default, an entry all set to zero is given back. Using this function, the value of an invalid read can be configured.

Parameters
entryInvalidValueThe value that is given back in case an invalid operation (e.g. read out of bounds) is tried.

The documentation for this class was generated from the following file: