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

Lightweight c++11 dictionary map implementation. More...

#include <ustd_map.h>

Public Member Functions

 map (unsigned int startSize=ARRAY_INIT_SIZE, unsigned int maxSize=ARRAY_MAX_SIZE, unsigned int incSize=ARRAY_INC_SIZE, bool shrink=true)
 
 ~map ()
 
operator[] (K key) const
 
V & operator[] (K key)
 
int find (K key)
 
int erase (K key)
 
void setInvalidValue (V &entryInvalidValue)
 
bool isEmpty ()
 
const ustd::array< K > & keysArray ()
 
unsigned int length ()
 
unsigned int peak ()
 

Public Attributes

ustd::array< V > values
 

Detailed Description

template<class K, class V>
class ustd::map< K, V >

Lightweight c++11 dictionary map implementation.

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

The map 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 // Appropriate platform define required
#include <ustd_map.h>
myMap[0] = 13.3; // Memory for map is allocated, if necessary
double p = myMap[0];
Lightweight c++11 dictionary map implementation.
Definition: ustd_map.h:62

An example for static mode

#define __ATTINY__ 1 // Appropriate platform define required
#include <ustd_map.h>
// map size is fixed 5 (startSize==maxSize), no dynamic extensions:

Iteration over keys

myMap[0]=1.1;
myMap[1]=1.2;
for (auto i : myMap.keysArray()) {
printf("%d->%f\n",i,myMap[i]);
}
const ustd::array< K > & keysArray()
Definition: ustd_map.h:198

Constructor & Destructor Documentation

◆ map()

template<class K , class V >
ustd::map< K, V >::map ( unsigned int  startSize = ARRAY_INIT_SIZE,
unsigned int  maxSize = ARRAY_MAX_SIZE,
unsigned int  incSize = ARRAY_INC_SIZE,
bool  shrink = true 
)
inline

Array of values

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

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

◆ ~map()

template<class K , class V >
ustd::map< K, V >::~map ( )
inline

Free resources

Member Function Documentation

◆ erase()

template<class K , class V >
int ustd::map< K, V >::erase ( key)
inline

Delete the entry corresponding to map-key. This might lead to memory-deallocation, if shrink=True during map creation

Parameters
keyMap-key of entry to be deleted
Returns
index of entry been deleted or -1 on error

◆ find()

template<class K , class V >
int ustd::map< K, V >::find ( key)
inline

Get the index of the key and value arrays of the map

Parameters
keyMap-key.
Returns
index, if found, -1 on error

◆ isEmpty()

template<class K , class V >
bool ustd::map< K, V >::isEmpty ( )
inline

Check, if map is empty.

Returns
boolean true on empty map

◆ keysArray()

template<class K , class V >
const ustd::array< K > & ustd::map< K, V >::keysArray ( )
inline

Reference to array of keys

Returns
const reference to array of keys, e.g. for iteration.

◆ length()

template<class K , class V >
unsigned int ustd::map< K, V >::length ( )
inline

Check number of map-members.

Returns
number of map entries

◆ operator[]() [1/2]

template<class K , class V >
V & ustd::map< K, V >::operator[] ( key)
inline

Write a map value for a given key

Parameters
keymap-key
Returns
value on success, or setInvalidValue() on error (e.g. map full)

◆ operator[]() [2/2]

template<class K , class V >
V ustd::map< K, V >::operator[] ( key) const
inline

Read value of map for given key, a=myMap[3].

Parameters
keymap-key
Returns
Corresponding value. The value set be setInvalidValue() is given back for invalid reads (or by default a value set to zero)

◆ peak()

template<class K , class V >
unsigned int ustd::map< K, V >::peak ( )
inline

Check peak number of map-members.

Returns
maximum number members the map had since creation

◆ setInvalidValue()

template<class K , class V >
void ustd::map< K, V >::setInvalidValue ( V &  entryInvalidValue)
inline

Set the value that's given back, if read of an invalid key 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 invalid key) is tried.

Member Data Documentation

◆ values

template<class K , class V >
ustd::array<V> ustd::map< K, V >::values

Array of keys


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