fixed map improvements

This commit is contained in:
Robin Müller 2020-04-06 11:43:37 +02:00
parent cd7e47ccbb
commit fe246b9bca

View File

@ -6,7 +6,11 @@
#include <utility> #include <utility>
/** /**
* \ingroup container * @brief Map implementation for maps with a pre-defined size.
* @details Can be initialized with desired maximum size.
* Iterator is used to access <key,value> pair and
* iterate through map entries. Complexity O(n).
* @ingroup container
*/ */
template<typename key_t, typename T> template<typename key_t, typename T>
class FixedMap: public SerializeIF { class FixedMap: public SerializeIF {
@ -52,12 +56,24 @@ public:
return ArrayList<std::pair<key_t, T>, uint32_t>::Iterator::value->second; return ArrayList<std::pair<key_t, T>, uint32_t>::Iterator::value->second;
} }
// -> operator overloaded, can be used to access value
T *operator->() { T *operator->() {
return &ArrayList<std::pair<key_t, T>, uint32_t>::Iterator::value->second; return &ArrayList<std::pair<key_t, T>, uint32_t>::Iterator::value->second;
} }
// Can be used to access the key of the iterator
key_t first() {
return ArrayList<std::pair<key_t, T>, uint32_t>::Iterator::value->first;
}
// Alternative to access value, similar to std::map implementation
T second() {
return ArrayList<std::pair<key_t, T>, uint32_t>::Iterator::value->second;
}
}; };
Iterator begin() const { Iterator begin() const {
return Iterator(&theMap[0]); return Iterator(&theMap[0]);
} }
@ -72,10 +88,10 @@ public:
ReturnValue_t insert(key_t key, T value, Iterator *storedValue = NULL) { ReturnValue_t insert(key_t key, T value, Iterator *storedValue = NULL) {
if (exists(key) == HasReturnvaluesIF::RETURN_OK) { if (exists(key) == HasReturnvaluesIF::RETURN_OK) {
return KEY_ALREADY_EXISTS; return FixedMap::KEY_ALREADY_EXISTS;
} }
if (_size == theMap.maxSize()) { if (_size == theMap.maxSize()) {
return MAP_FULL; return FixedMap::MAP_FULL;
} }
theMap[_size].first = key; theMap[_size].first = key;
theMap[_size].second = value; theMap[_size].second = value;
@ -87,7 +103,7 @@ public:
} }
ReturnValue_t insert(std::pair<key_t, T> pair) { ReturnValue_t insert(std::pair<key_t, T> pair) {
return insert(pair.fist, pair.second); return insert(pair.first, pair.second);
} }
ReturnValue_t exists(key_t key) const { ReturnValue_t exists(key_t key) const {
@ -148,8 +164,17 @@ public:
return theMap.maxSize(); return theMap.maxSize();
} }
virtual ReturnValue_t serialize(uint8_t** buffer, uint32_t* size, bool full() {
const uint32_t max_size, bool bigEndian) const { if(_size == theMap.maxSize()) {
return true;
}
else {
return false;
}
}
virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size,
const size_t max_size, bool bigEndian) const {
ReturnValue_t result = SerializeAdapter<uint32_t>::serialize(&this->_size, ReturnValue_t result = SerializeAdapter<uint32_t>::serialize(&this->_size,
buffer, size, max_size, bigEndian); buffer, size, max_size, bigEndian);
uint32_t i = 0; uint32_t i = 0;
@ -163,7 +188,7 @@ public:
return result; return result;
} }
virtual uint32_t getSerializedSize() const { virtual size_t getSerializedSize() const {
uint32_t printSize = sizeof(_size); uint32_t printSize = sizeof(_size);
uint32_t i = 0; uint32_t i = 0;
@ -176,7 +201,7 @@ public:
return printSize; return printSize;
} }
virtual ReturnValue_t deSerialize(const uint8_t** buffer, int32_t* size, virtual ReturnValue_t deSerialize(const uint8_t** buffer, ssize_t* size,
bool bigEndian) { bool bigEndian) {
ReturnValue_t result = SerializeAdapter<uint32_t>::deSerialize(&this->_size, ReturnValue_t result = SerializeAdapter<uint32_t>::deSerialize(&this->_size,
buffer, size, bigEndian); buffer, size, bigEndian);