2020-09-10 15:29:19 +02:00
|
|
|
#ifndef FSFW_CONTAINER_FIXEDMAP_H_
|
|
|
|
#define FSFW_CONTAINER_FIXEDMAP_H_
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2020-09-07 15:43:48 +02:00
|
|
|
#include <type_traits>
|
2022-02-02 10:29:30 +01:00
|
|
|
#include <utility>
|
|
|
|
|
2022-08-16 12:48:22 +02:00
|
|
|
#include "../returnvalues/returnvalue.h"
|
2022-02-02 10:29:30 +01:00
|
|
|
#include "ArrayList.h"
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2019-08-28 14:50:24 +02:00
|
|
|
/**
|
2020-09-30 18:57:30 +02:00
|
|
|
* @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).
|
2020-09-10 15:29:19 +02:00
|
|
|
* @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
|
2019-08-28 14:50:24 +02:00
|
|
|
*/
|
2022-02-02 10:29:30 +01:00
|
|
|
template <typename key_t, typename T>
|
|
|
|
class FixedMap : public SerializeIF {
|
|
|
|
static_assert(std::is_trivially_copyable<T>::value or std::is_base_of<SerializeIF, T>::value,
|
|
|
|
"Types used in FixedMap must either be trivial copy-able or a "
|
|
|
|
"derived class from SerializeIF to be serialize-able");
|
|
|
|
|
|
|
|
public:
|
|
|
|
static const uint8_t INTERFACE_ID = CLASS_ID::FIXED_MAP;
|
|
|
|
static const ReturnValue_t KEY_ALREADY_EXISTS = MAKE_RETURN_CODE(0x01);
|
|
|
|
static const ReturnValue_t MAP_FULL = MAKE_RETURN_CODE(0x02);
|
|
|
|
static const ReturnValue_t KEY_DOES_NOT_EXIST = MAKE_RETURN_CODE(0x03);
|
|
|
|
|
|
|
|
private:
|
|
|
|
static const key_t EMPTY_SLOT = -1;
|
|
|
|
ArrayList<std::pair<key_t, T>, uint32_t> theMap;
|
|
|
|
uint32_t _size;
|
|
|
|
|
|
|
|
uint32_t findIndex(key_t key) const {
|
|
|
|
if (_size == 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
uint32_t i = 0;
|
|
|
|
for (i = 0; i < _size; ++i) {
|
|
|
|
if (theMap[i].first == key) {
|
2021-01-28 11:50:33 +01:00
|
|
|
return i;
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
2021-01-28 11:50:33 +01:00
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
return i;
|
|
|
|
}
|
2021-01-28 11:50:33 +01:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
public:
|
|
|
|
FixedMap(uint32_t maxSize) : theMap(maxSize), _size(0) {}
|
2021-01-28 11:50:33 +01:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
class Iterator : public ArrayList<std::pair<key_t, T>, uint32_t>::Iterator {
|
|
|
|
public:
|
|
|
|
Iterator() : ArrayList<std::pair<key_t, T>, uint32_t>::Iterator() {}
|
2021-01-28 11:50:33 +01:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
Iterator(std::pair<key_t, T>* pair)
|
|
|
|
: ArrayList<std::pair<key_t, T>, uint32_t>::Iterator(pair) {}
|
|
|
|
};
|
2021-01-28 11:50:33 +01:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
friend bool operator==(const typename FixedMap::Iterator& lhs,
|
|
|
|
const typename FixedMap::Iterator& rhs) {
|
|
|
|
return (lhs.value == rhs.value);
|
|
|
|
}
|
2021-01-28 11:50:33 +01:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
friend bool operator!=(const typename FixedMap::Iterator& lhs,
|
|
|
|
const typename FixedMap::Iterator& rhs) {
|
|
|
|
return not(lhs.value == rhs.value);
|
|
|
|
}
|
2021-01-28 11:50:33 +01:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
Iterator begin() const { return Iterator(&theMap[0]); }
|
2021-01-28 11:50:33 +01:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
Iterator end() const { return Iterator(&theMap[_size]); }
|
2021-01-28 11:50:33 +01:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
uint32_t size() const { return _size; }
|
2021-01-28 11:50:33 +01:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
ReturnValue_t insert(key_t key, T value, Iterator* storedValue = nullptr) {
|
2022-08-15 20:28:16 +02:00
|
|
|
if (exists(key) == returnvalue::OK) {
|
2022-02-02 10:29:30 +01:00
|
|
|
return KEY_ALREADY_EXISTS;
|
2021-01-28 11:50:33 +01:00
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
if (_size == theMap.maxSize()) {
|
|
|
|
return MAP_FULL;
|
2021-01-28 11:50:33 +01:00
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
theMap[_size].first = key;
|
|
|
|
theMap[_size].second = value;
|
|
|
|
if (storedValue != nullptr) {
|
|
|
|
*storedValue = Iterator(&theMap[_size]);
|
2021-01-28 11:50:33 +01:00
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
++_size;
|
2022-08-15 20:28:16 +02:00
|
|
|
return returnvalue::OK;
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t insert(std::pair<key_t, T> pair) { return insert(pair.first, pair.second); }
|
2021-01-28 11:50:33 +01:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
ReturnValue_t exists(key_t key) const {
|
|
|
|
ReturnValue_t result = KEY_DOES_NOT_EXIST;
|
|
|
|
if (findIndex(key) < _size) {
|
2022-08-15 20:28:16 +02:00
|
|
|
result = returnvalue::OK;
|
2021-01-28 11:50:33 +01:00
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
return result;
|
|
|
|
}
|
2021-01-28 11:50:33 +01:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
ReturnValue_t erase(Iterator* iter) {
|
|
|
|
uint32_t i;
|
|
|
|
if ((i = findIndex((*iter).value->first)) >= _size) {
|
|
|
|
return KEY_DOES_NOT_EXIST;
|
2021-01-28 11:50:33 +01:00
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
theMap[i] = theMap[_size - 1];
|
|
|
|
--_size;
|
|
|
|
--((*iter).value);
|
2022-08-15 20:28:16 +02:00
|
|
|
return returnvalue::OK;
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
2021-01-28 11:50:33 +01:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
ReturnValue_t erase(key_t key) {
|
|
|
|
uint32_t i;
|
|
|
|
if ((i = findIndex(key)) >= _size) {
|
|
|
|
return KEY_DOES_NOT_EXIST;
|
2021-01-28 11:50:33 +01:00
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
theMap[i] = theMap[_size - 1];
|
|
|
|
--_size;
|
2022-08-15 20:28:16 +02:00
|
|
|
return returnvalue::OK;
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
T* findValue(key_t key) const { return &theMap[findIndex(key)].second; }
|
2021-01-28 11:50:33 +01:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
Iterator find(key_t key) const {
|
|
|
|
ReturnValue_t result = exists(key);
|
2022-08-15 20:28:16 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-02-02 10:29:30 +01:00
|
|
|
return end();
|
2021-01-28 11:50:33 +01:00
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
return Iterator(&theMap[findIndex(key)]);
|
|
|
|
}
|
2021-01-28 11:50:33 +01:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
ReturnValue_t find(key_t key, T** value) const {
|
|
|
|
ReturnValue_t result = exists(key);
|
2022-08-15 20:28:16 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-02-02 10:29:30 +01:00
|
|
|
return result;
|
2021-01-28 11:50:33 +01:00
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
*value = &theMap[findIndex(key)].second;
|
2022-08-15 20:28:16 +02:00
|
|
|
return returnvalue::OK;
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
2021-01-28 11:50:33 +01:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
bool empty() {
|
|
|
|
if (_size == 0) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2021-01-28 11:50:33 +01:00
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
2021-01-28 11:50:33 +01:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
bool full() {
|
|
|
|
if (_size >= theMap.maxSize()) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2021-01-28 11:50:33 +01:00
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
2021-01-28 11:50:33 +01:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
void clear() { _size = 0; }
|
2021-01-28 11:50:33 +01:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
uint32_t maxSize() const { return theMap.maxSize(); }
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
virtual ReturnValue_t serialize(uint8_t** buffer, size_t* size, size_t maxSize,
|
|
|
|
Endianness streamEndianness) const {
|
|
|
|
ReturnValue_t result =
|
|
|
|
SerializeAdapter::serialize(&this->_size, buffer, size, maxSize, streamEndianness);
|
|
|
|
uint32_t i = 0;
|
2022-08-15 20:28:16 +02:00
|
|
|
while ((result == returnvalue::OK) && (i < this->_size)) {
|
2022-02-02 10:29:30 +01:00
|
|
|
result =
|
|
|
|
SerializeAdapter::serialize(&theMap[i].first, buffer, size, maxSize, streamEndianness);
|
|
|
|
result =
|
|
|
|
SerializeAdapter::serialize(&theMap[i].second, buffer, size, maxSize, streamEndianness);
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual size_t getSerializedSize() const {
|
|
|
|
uint32_t printSize = sizeof(_size);
|
|
|
|
uint32_t i = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < _size; ++i) {
|
|
|
|
printSize += SerializeAdapter::getSerializedSize(&theMap[i].first);
|
|
|
|
printSize += SerializeAdapter::getSerializedSize(&theMap[i].second);
|
|
|
|
}
|
|
|
|
|
|
|
|
return printSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ReturnValue_t deSerialize(const uint8_t** buffer, size_t* size,
|
|
|
|
Endianness streamEndianness) {
|
|
|
|
ReturnValue_t result =
|
|
|
|
SerializeAdapter::deSerialize(&this->_size, buffer, size, streamEndianness);
|
|
|
|
if (this->_size > theMap.maxSize()) {
|
|
|
|
return SerializeIF::TOO_MANY_ELEMENTS;
|
|
|
|
}
|
|
|
|
uint32_t i = 0;
|
2022-08-15 20:28:16 +02:00
|
|
|
while ((result == returnvalue::OK) && (i < this->_size)) {
|
2022-02-02 10:29:30 +01:00
|
|
|
result = SerializeAdapter::deSerialize(&theMap[i].first, buffer, size, streamEndianness);
|
|
|
|
result = SerializeAdapter::deSerialize(&theMap[i].second, buffer, size, streamEndianness);
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2016-06-15 23:48:41 +02:00
|
|
|
};
|
|
|
|
|
2020-09-10 15:29:19 +02:00
|
|
|
#endif /* FSFW_CONTAINER_FIXEDMAP_H_ */
|