Merge branch 'master' into mueller/FixedOrdMultimapTpp

This commit is contained in:
Robin Müller 2020-10-01 13:59:42 +02:00
commit 4f91db2ff8
3 changed files with 113 additions and 79 deletions

View File

@ -1,15 +1,15 @@
#ifndef ARRAYLIST_H_ #ifndef FSFW_CONTAINER_ARRAYLIST_H_
#define ARRAYLIST_H_ #define FSFW_CONTAINER_ARRAYLIST_H_
#include "../returnvalues/HasReturnvaluesIF.h" #include "../returnvalues/HasReturnvaluesIF.h"
#include "../serialize/SerializeAdapter.h" #include "../serialize/SerializeAdapter.h"
#include "../serialize/SerializeIF.h" #include "../serialize/SerializeIF.h"
/** /**
* A List that stores its values in an array. * @brief A List that stores its values in an array.
* * @details
* The backend is an array that can be allocated by the class itself or supplied via ctor. * The underlying storage is an array that can be allocated by the class
* * itself or supplied via ctor.
* *
* @ingroup container * @ingroup container
*/ */
@ -20,6 +20,53 @@ public:
static const uint8_t INTERFACE_ID = CLASS_ID::ARRAY_LIST; static const uint8_t INTERFACE_ID = CLASS_ID::ARRAY_LIST;
static const ReturnValue_t FULL = MAKE_RETURN_CODE(0x01); static const ReturnValue_t FULL = MAKE_RETURN_CODE(0x01);
/**
* This is the allocating constructor.
* It allocates an array of the specified size.
* @param maxSize
*/
ArrayList(count_t maxSize) :
size(0), maxSize_(maxSize), allocated(true) {
entries = new T[maxSize];
}
/**
* This is the non-allocating constructor
*
* It expects a pointer to an array of a certain size and initializes
* itself to it.
*
* @param storage the array to use as backend
* @param maxSize size of storage
* @param size size of data already present in storage
*/
ArrayList(T *storage, count_t maxSize, count_t size = 0) :
size(size), entries(storage), maxSize_(maxSize), allocated(false) {
}
/**
* Copying is forbiden by declaring copy ctor and copy assignment deleted
* It is too ambigous in this case.
* (Allocate a new backend? Use the same? What to do in an modifying call?)
*/
ArrayList(const ArrayList& other) = delete;
const ArrayList& operator=(const ArrayList& other) = delete;
/**
* Number of Elements stored in this List
*/
count_t size;
/**
* Destructor, if the allocating constructor was used, it deletes the array.
*/
virtual ~ArrayList() {
if (allocated) {
delete[] entries;
}
}
/** /**
* An Iterator to go trough an ArrayList * An Iterator to go trough an ArrayList
* *
@ -31,10 +78,7 @@ public:
/** /**
* Empty ctor, points to NULL * Empty ctor, points to NULL
*/ */
Iterator() : Iterator(): value(0) {}
value(0) {
}
/** /**
* Initializes the Iterator to point to an element * Initializes the Iterator to point to an element
@ -72,70 +116,31 @@ public:
return tmp; return tmp;
} }
T& operator*(){ T& operator*() {
return *value; return *value;
} }
const T& operator*() const{ const T& operator*() const {
return *value; return *value;
} }
T *operator->(){ T *operator->() {
return value; return value;
} }
const T *operator->() const{ const T *operator->() const {
return value; return value;
} }
//SHOULDDO this should be implemented as non-member
bool operator==(const typename ArrayList<T, count_t>::Iterator& other) const{
return (value == other.value);
}
//SHOULDDO this should be implemented as non-member
bool operator!=(const typename ArrayList<T, count_t>::Iterator& other) const {
return !(*this == other);
}
}; };
/** friend bool operator==(const ArrayList::Iterator& lhs,
* Number of Elements stored in this List const ArrayList::Iterator& rhs) {
*/ return (lhs.value == rhs.value);
count_t size;
/**
* This is the allocating constructor;
*
* It allocates an array of the specified size.
*
* @param maxSize
*/
ArrayList(count_t maxSize) :
size(0), maxSize_(maxSize), allocated(true) {
entries = new T[maxSize];
} }
/** friend bool operator!=(const ArrayList::Iterator& lhs,
* This is the non-allocating constructor const ArrayList::Iterator& rhs) {
* return not (lhs.value == rhs.value);
* It expects a pointer to an array of a certain size and initializes itself to it.
*
* @param storage the array to use as backend
* @param maxSize size of storage
* @param size size of data already present in storage
*/
ArrayList(T *storage, count_t maxSize, count_t size = 0) :
size(size), entries(storage), maxSize_(maxSize), allocated(false) {
}
/**
* Destructor, if the allocating constructor was used, it deletes the array.
*/
virtual ~ArrayList() {
if (allocated) {
delete[] entries;
}
} }
/** /**
@ -191,7 +196,7 @@ public:
* *
* @return maximum number of elements * @return maximum number of elements
*/ */
uint32_t maxSize() const { size_t maxSize() const {
return this->maxSize_; return this->maxSize_;
} }
@ -226,19 +231,7 @@ public:
count_t remaining() { count_t remaining() {
return (maxSize_ - size); return (maxSize_ - size);
} }
private:
/**
* This is the copy constructor
*
* It is private, as copying is too ambigous in this case. (Allocate a new backend? Use the same?
* What to do in an modifying call?)
*
* @param other
*/
ArrayList(const ArrayList& other) :
size(other.size), entries(other.entries), maxSize_(other.maxSize_), allocated(
false) {
}
protected: protected:
/** /**
* pointer to the array in which the entries are stored * pointer to the array in which the entries are stored
@ -247,12 +240,14 @@ protected:
/** /**
* remembering the maximum size * remembering the maximum size
*/ */
uint32_t maxSize_; size_t maxSize_;
/** /**
* true if the array was allocated and needs to be deleted in the destructor. * true if the array was allocated and needs to be deleted in the destructor.
*/ */
bool allocated; bool allocated;
}; };
#endif /* ARRAYLIST_H_ */
#endif /* FSFW_CONTAINER_ARRAYLIST_H_ */

View File

@ -2,11 +2,13 @@
#define FIXEDARRAYLIST_H_ #define FIXEDARRAYLIST_H_
#include "ArrayList.h" #include "ArrayList.h"
#include <cmath>
/** /**
* \ingroup container * \ingroup container
*/ */
template<typename T, uint32_t MAX_SIZE, typename count_t = uint8_t> template<typename T, size_t MAX_SIZE, typename count_t = uint8_t>
class FixedArrayList: public ArrayList<T, count_t> { class FixedArrayList: public ArrayList<T, count_t> {
static_assert(MAX_SIZE <= (pow(2,sizeof(count_t)*8)-1), "count_t is not large enough to hold MAX_SIZE");
private: private:
T data[MAX_SIZE]; T data[MAX_SIZE];
public: public:
@ -18,11 +20,13 @@ public:
ArrayList<T, count_t>(data, MAX_SIZE) { ArrayList<T, count_t>(data, MAX_SIZE) {
memcpy(this->data, other.data, sizeof(this->data)); memcpy(this->data, other.data, sizeof(this->data));
this->entries = data; this->entries = data;
this->size = other.size;
} }
FixedArrayList& operator=(FixedArrayList other) { FixedArrayList& operator=(FixedArrayList other) {
memcpy(this->data, other.data, sizeof(this->data)); memcpy(this->data, other.data, sizeof(this->data));
this->entries = data; this->entries = data;
this->size = other.size;
return *this; return *this;
} }

View File

@ -7,14 +7,21 @@
#include <type_traits> #include <type_traits>
/** /**
* @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).
* @warning Iterators return a non-const key_t in the pair. * @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. * @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>
class FixedMap: public SerializeIF { class FixedMap: public SerializeIF {
static_assert (std::is_trivially_copyable<T>::value or std::is_base_of<SerializeIF, T>::value, static_assert (std::is_trivially_copyable<T>::value or
"Types used in FixedMap must either be trivial copy-able or a derived Class from SerializeIF to be serialize-able"); 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: public:
static const uint8_t INTERFACE_ID = CLASS_ID::FIXED_MAP; 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 KEY_ALREADY_EXISTS = MAKE_RETURN_CODE(0x01);
@ -54,6 +61,16 @@ public:
} }
}; };
friend bool operator==(const typename FixedMap::Iterator& lhs,
const typename FixedMap::Iterator& rhs) {
return (lhs.value == rhs.value);
}
friend bool operator!=(const typename FixedMap::Iterator& lhs,
const typename FixedMap::Iterator& rhs) {
return not (lhs.value == rhs.value);
}
Iterator begin() const { Iterator begin() const {
return Iterator(&theMap[0]); return Iterator(&theMap[0]);
} }
@ -136,6 +153,24 @@ public:
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
bool empty() {
if(_size == 0) {
return true;
}
else {
return false;
}
}
bool full() {
if(_size >= theMap.maxSize()) {
return true;
}
else {
return false;
}
}
void clear() { void clear() {
_size = 0; _size = 0;
} }