merged fixed map changes

This commit is contained in:
Robin Müller 2020-09-10 15:51:11 +02:00
parent bbc73a6aaf
commit 96e471add6

View File

@ -1,15 +1,19 @@
#ifndef FIXEDMAP_H_ #ifndef FSFW_CONTAINER_FIXEDMAP_H_
#define FIXEDMAP_H_ #define FSFW_CONTAINER_FIXEDMAP_H_
#include "../container/ArrayList.h" #include "ArrayList.h"
#include "../returnvalues/HasReturnvaluesIF.h" #include "../returnvalues/HasReturnvaluesIF.h"
#include <utility> #include <utility>
#include <type_traits>
/** /**
* @brief Map implementation for maps with a pre-defined size. * @brief Map implementation for maps with a pre-defined size.
* @details Can be initialized with desired maximum size. * @details
* Iterator is used to access <key,value> pair and * Can be initialized with desired maximum size.
* iterate through map entries. Complexity O(n). * Iterator is used to access <key,value> pair and iterate through map entries.
* Complexity O(n).
* @warning Iterators return a non-const key_t in the pair.
* @warning A User is not allowed to change the key, otherwise the map is corrupted.
* @ingroup container * @ingroup container
*/ */
template<typename key_t, typename T> template<typename key_t, typename T>
@ -55,29 +59,8 @@ public:
Iterator(std::pair<key_t, T> *pair) : Iterator(std::pair<key_t, T> *pair) :
ArrayList<std::pair<key_t, T>, uint32_t>::Iterator(pair) { ArrayList<std::pair<key_t, T>, uint32_t>::Iterator(pair) {
} }
T operator*() {
return ArrayList<std::pair<key_t, T>, uint32_t>::Iterator::value->second;
}
// -> operator overloaded, can be used to access value
T *operator->() {
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]);
} }
@ -90,16 +73,16 @@ public:
return _size; return _size;
} }
ReturnValue_t insert(key_t key, T value, Iterator *storedValue = NULL) { ReturnValue_t insert(key_t key, T value, Iterator *storedValue = nullptr) {
if (exists(key) == HasReturnvaluesIF::RETURN_OK) { if (exists(key) == HasReturnvaluesIF::RETURN_OK) {
return FixedMap::KEY_ALREADY_EXISTS; return KEY_ALREADY_EXISTS;
} }
if (_size == theMap.maxSize()) { if (_size == theMap.maxSize()) {
return FixedMap::MAP_FULL; return MAP_FULL;
} }
theMap[_size].first = key; theMap[_size].first = key;
theMap[_size].second = value; theMap[_size].second = value;
if (storedValue != NULL) { if (storedValue != nullptr) {
*storedValue = Iterator(&theMap[_size]); *storedValue = Iterator(&theMap[_size]);
} }
++_size; ++_size;
@ -160,6 +143,15 @@ public:
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
bool full() {
if(_size >= theMap.maxSize()) {
return true;
}
else {
return false;
}
}
void clear() { void clear() {
_size = 0; _size = 0;
} }
@ -168,16 +160,6 @@ public:
return theMap.maxSize(); return theMap.maxSize();
} }
bool full() {
if(_size == theMap.maxSize()) {
return true;
}
else {
return false;
}
}
virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size, virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size,
size_t maxSize, Endianness streamEndianness) const { size_t maxSize, Endianness streamEndianness) const {
ReturnValue_t result = SerializeAdapter::serialize(&this->_size, ReturnValue_t result = SerializeAdapter::serialize(&this->_size,
@ -226,4 +208,4 @@ public:
}; };
#endif /* FIXEDMAP_H_ */ #endif /* FSFW_CONTAINER_FIXEDMAP_H_ */