From 17d5de15c990be42ad98da412059d7588ded15bd Mon Sep 17 00:00:00 2001 From: "Robin.Mueller" Date: Tue, 29 Sep 2020 16:10:35 +0200 Subject: [PATCH 1/6] slight changes ,size t replacement --- container/ArrayList.h | 218 +++++++++++++++++++++--------------------- 1 file changed, 107 insertions(+), 111 deletions(-) diff --git a/container/ArrayList.h b/container/ArrayList.h index 19454777..f347da8e 100644 --- a/container/ArrayList.h +++ b/container/ArrayList.h @@ -1,15 +1,15 @@ -#ifndef ARRAYLIST_H_ -#define ARRAYLIST_H_ +#ifndef FSFW_CONTAINER_ARRAYLIST_H_ +#define FSFW_CONTAINER_ARRAYLIST_H_ #include "../returnvalues/HasReturnvaluesIF.h" #include "../serialize/SerializeAdapter.h" #include "../serialize/SerializeIF.h" /** - * A List that stores its values in an array. - * - * The backend is an array that can be allocated by the class itself or supplied via ctor. - * + * @brief A List that stores its values in an array. + * @details + * The underlying storage is an array that can be allocated by the class + * itself or supplied via ctor. * * @ingroup container */ @@ -21,94 +21,8 @@ public: static const ReturnValue_t FULL = MAKE_RETURN_CODE(0x01); /** - * An Iterator to go trough an ArrayList - * - * It stores a pointer to an element and increments the - * pointer when incremented itself. - */ - class Iterator { - public: - /** - * Empty ctor, points to NULL - */ - Iterator() : - value(0) { - - } - - /** - * Initializes the Iterator to point to an element - * - * @param initialize - */ - Iterator(T *initialize) { - value = initialize; - } - - /** - * The current element the iterator points to - */ - T *value; - - Iterator& operator++() { - value++; - return *this; - } - - Iterator operator++(int) { - Iterator tmp(*this); - operator++(); - return tmp; - } - - Iterator& operator--() { - value--; - return *this; - } - - Iterator operator--(int) { - Iterator tmp(*this); - operator--(); - return tmp; - } - - T& operator*(){ - return *value; - } - - const T& operator*() const{ - return *value; - } - - T *operator->(){ - return value; - } - - const T *operator->() const{ - return value; - } - - //SHOULDDO this should be implemented as non-member - bool operator==(const typename ArrayList::Iterator& other) const{ - return (value == other.value); - } - - //SHOULDDO this should be implemented as non-member - bool operator!=(const typename ArrayList::Iterator& other) const { - return !(*this == other); - } - }; - - /** - * Number of Elements stored in this List - */ - count_t size; - - /** - * This is the allocating constructor; - * + * This is the allocating constructor. * It allocates an array of the specified size. - * * @param maxSize */ ArrayList(count_t maxSize) : @@ -119,7 +33,8 @@ public: /** * This is the non-allocating constructor * - * It expects a pointer to an array of a certain size and initializes itself to it. + * 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 @@ -129,6 +44,20 @@ public: 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. */ @@ -138,6 +67,85 @@ public: } } + /** + * An Iterator to go trough an ArrayList + * + * It stores a pointer to an element and increments the + * pointer when incremented itself. + */ + class Iterator { + public: + /** + * Empty ctor, points to NULL + */ + Iterator(): value(0) {} + + /** + * Initializes the Iterator to point to an element + * + * @param initialize + */ + Iterator(T *initialize) { + value = initialize; + } + + /** + * The current element the iterator points to + */ + T *value; + + Iterator& operator++() { + value++; + return *this; + } + + Iterator operator++(int) { + Iterator tmp(*this); + operator++(); + return tmp; + } + + Iterator& operator--() { + value--; + return *this; + } + + Iterator operator--(int) { + Iterator tmp(*this); + operator--(); + return tmp; + } + + T& operator*() { + return *value; + } + + const T& operator*() const { + return *value; + } + + T *operator->() { + return value; + } + + const T *operator->() const { + return value; + } + + + //SHOULDDO this should be implemented as non-member + bool operator==(const typename + ArrayList::Iterator& other) const { + return (value == other.value); + } + + //SHOULDDO this should be implemented as non-member + bool operator!=(const typename + ArrayList::Iterator& other) const { + return !(*this == other); + } + }; + /** * Iterator pointing to the first stored elmement * @@ -191,7 +199,7 @@ public: * * @return maximum number of elements */ - uint32_t maxSize() const { + size_t maxSize() const { return this->maxSize_; } @@ -226,19 +234,7 @@ public: count_t remaining() { 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: /** * pointer to the array in which the entries are stored @@ -247,12 +243,12 @@ protected: /** * remembering the maximum size */ - uint32_t maxSize_; + size_t maxSize_; /** * true if the array was allocated and needs to be deleted in the destructor. */ bool allocated; - }; -#endif /* ARRAYLIST_H_ */ + +#endif /* FSFW_CONTAINER_ARRAYLIST_H_ */ From 4eef7bfc0133f56021914028818107cbe0e41b64 Mon Sep 17 00:00:00 2001 From: "Robin.Mueller" Date: Tue, 29 Sep 2020 16:12:43 +0200 Subject: [PATCH 2/6] changes taken over --- container/ArrayList.h | 144 +++++++++++++++++++++--------------------- 1 file changed, 72 insertions(+), 72 deletions(-) diff --git a/container/ArrayList.h b/container/ArrayList.h index f347da8e..426e02b3 100644 --- a/container/ArrayList.h +++ b/container/ArrayList.h @@ -26,7 +26,7 @@ public: * @param maxSize */ ArrayList(count_t maxSize) : - size(0), maxSize_(maxSize), allocated(true) { + size(0), maxSize_(maxSize), allocated(true) { entries = new T[maxSize]; } @@ -41,14 +41,14 @@ public: * @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) { + 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?) - */ + /** + * 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; @@ -67,84 +67,84 @@ public: } } - /** - * An Iterator to go trough an ArrayList - * - * It stores a pointer to an element and increments the - * pointer when incremented itself. - */ - class Iterator { - public: - /** - * Empty ctor, points to NULL - */ - Iterator(): value(0) {} + /** + * An Iterator to go trough an ArrayList + * + * It stores a pointer to an element and increments the + * pointer when incremented itself. + */ + class Iterator { + public: + /** + * Empty ctor, points to NULL + */ + Iterator(): value(0) {} - /** - * Initializes the Iterator to point to an element - * - * @param initialize - */ - Iterator(T *initialize) { - value = initialize; - } + /** + * Initializes the Iterator to point to an element + * + * @param initialize + */ + Iterator(T *initialize) { + value = initialize; + } - /** - * The current element the iterator points to - */ - T *value; + /** + * The current element the iterator points to + */ + T *value; - Iterator& operator++() { - value++; - return *this; - } + Iterator& operator++() { + value++; + return *this; + } - Iterator operator++(int) { - Iterator tmp(*this); - operator++(); - return tmp; - } + Iterator operator++(int) { + Iterator tmp(*this); + operator++(); + return tmp; + } - Iterator& operator--() { - value--; - return *this; - } + Iterator& operator--() { + value--; + return *this; + } - Iterator operator--(int) { - Iterator tmp(*this); - operator--(); - return tmp; - } + Iterator operator--(int) { + Iterator tmp(*this); + operator--(); + return tmp; + } - T& operator*() { - return *value; - } + T& operator*() { + return *value; + } - const T& operator*() const { - return *value; - } + const T& operator*() const { + return *value; + } - T *operator->() { - return value; - } + T *operator->() { + return value; + } - const T *operator->() const { - return value; - } + const T *operator->() const { + return value; + } - //SHOULDDO this should be implemented as non-member - bool operator==(const typename - ArrayList::Iterator& other) const { - return (value == other.value); - } + //SHOULDDO this should be implemented as non-member + bool operator==(const typename + ArrayList::Iterator& other) const { + return (value == other.value); + } - //SHOULDDO this should be implemented as non-member - bool operator!=(const typename - ArrayList::Iterator& other) const { - return !(*this == other); - } - }; + //SHOULDDO this should be implemented as non-member + bool operator!=(const typename + ArrayList::Iterator& other) const { + return !(*this == other); + } + }; /** * Iterator pointing to the first stored elmement From 6c0bb23ed6f24e84a52a2b37c3bf082a39b0c43d Mon Sep 17 00:00:00 2001 From: "Robin.Mueller" Date: Tue, 29 Sep 2020 17:51:16 +0200 Subject: [PATCH 3/6] non member bool operator --- container/ArrayList.h | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/container/ArrayList.h b/container/ArrayList.h index 426e02b3..6bd5c1d5 100644 --- a/container/ArrayList.h +++ b/container/ArrayList.h @@ -131,21 +131,18 @@ public: const T *operator->() const { return value; } - - - //SHOULDDO this should be implemented as non-member - bool operator==(const typename - ArrayList::Iterator& other) const { - return (value == other.value); - } - - //SHOULDDO this should be implemented as non-member - bool operator!=(const typename - ArrayList::Iterator& other) const { - return !(*this == other); - } }; + friend bool operator==(const ArrayList::Iterator& lhs, + const ArrayList::Iterator& rhs) { + return (lhs.value == rhs.value); + } + + friend bool operator!=(const ArrayList::Iterator& lhs, + const ArrayList::Iterator& rhs) { + return not (lhs.value == rhs.value); + } + /** * Iterator pointing to the first stored elmement * @@ -251,4 +248,6 @@ protected: bool allocated; }; + + #endif /* FSFW_CONTAINER_ARRAYLIST_H_ */ From ecc4bdf11a2144f37e20631c17f3d58132499398 Mon Sep 17 00:00:00 2001 From: Steffen Gaisser Date: Wed, 30 Sep 2020 12:50:52 +0200 Subject: [PATCH 4/6] Added a compile time check for MAX_SIZE Compiler may warn if MAX_SIZE value overflows by itself but this checks gives a more verbose warning --- container/FixedArrayList.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/container/FixedArrayList.h b/container/FixedArrayList.h index 42b59177..505097c9 100644 --- a/container/FixedArrayList.h +++ b/container/FixedArrayList.h @@ -2,11 +2,13 @@ #define FIXEDARRAYLIST_H_ #include "ArrayList.h" +#include /** * \ingroup container */ -template +template class FixedArrayList: public ArrayList { + static_assert(MAX_SIZE <= (pow(2,sizeof(count_t)*8)-1), "count_t is not large enough to hold MAX_SIZE"); private: T data[MAX_SIZE]; public: From 8d8e918aeb299ceb1b1279a93a289c37419cf325 Mon Sep 17 00:00:00 2001 From: Steffen Gaisser Date: Wed, 30 Sep 2020 13:05:55 +0200 Subject: [PATCH 5/6] Fixed copying of FixedArrayList --- container/FixedArrayList.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/container/FixedArrayList.h b/container/FixedArrayList.h index 505097c9..e9e127cf 100644 --- a/container/FixedArrayList.h +++ b/container/FixedArrayList.h @@ -20,11 +20,13 @@ public: ArrayList(data, MAX_SIZE) { memcpy(this->data, other.data, sizeof(this->data)); this->entries = data; + this->size = other.size; } FixedArrayList& operator=(FixedArrayList other) { memcpy(this->data, other.data, sizeof(this->data)); this->entries = data; + this->size = other.size; return *this; } From a0098e8b17e42d43e64d7d9ef7a78f8cc66b137b Mon Sep 17 00:00:00 2001 From: "Robin.Mueller" Date: Wed, 30 Sep 2020 18:57:30 +0200 Subject: [PATCH 6/6] non member comp operator for iterators --- container/FixedMap.h | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/container/FixedMap.h b/container/FixedMap.h index d1cc31ff..7a5220fa 100644 --- a/container/FixedMap.h +++ b/container/FixedMap.h @@ -7,14 +7,21 @@ #include /** + * @brief Map implementation for maps with a pre-defined size. + * @details + * Can be initialized with desired maximum size. + * Iterator is used to access 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 */ template class FixedMap: public SerializeIF { - static_assert (std::is_trivially_copyable::value or std::is_base_of::value, - "Types used in FixedMap must either be trivial copy-able or a derived Class from SerializeIF to be serialize-able"); + static_assert (std::is_trivially_copyable::value or + std::is_base_of::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); @@ -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 { return Iterator(&theMap[0]); } @@ -136,6 +153,24 @@ public: 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() { _size = 0; }