| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ====================================================================== | ||
| 2 | // \file ExternalArraySet.hpp | ||
| 3 | // \author bocchino | ||
| 4 | // \brief An array-based set with external storage | ||
| 5 | // ====================================================================== | ||
| 6 | |||
| 7 | #ifndef Fw_ExternalArraySet_HPP | ||
| 8 | #define Fw_ExternalArraySet_HPP | ||
| 9 | |||
| 10 | #include "Fw/DataStructures/ArraySetOrMapImpl.hpp" | ||
| 11 | #include "Fw/DataStructures/Nil.hpp" | ||
| 12 | #include "Fw/DataStructures/SetBase.hpp" | ||
| 13 | #include "Fw/Types/Assert.hpp" | ||
| 14 | |||
| 15 | namespace Fw { | ||
| 16 | |||
| 17 | template <typename T> | ||
| 18 | class ExternalArraySet final : public SetBase<T> { | ||
| 19 | // ---------------------------------------------------------------------- | ||
| 20 | // Friend class for testing | ||
| 21 | // ---------------------------------------------------------------------- | ||
| 22 | |||
| 23 | template <typename TT> | ||
| 24 | friend class ExternalArraySetTester; | ||
| 25 | |||
| 26 | public: | ||
| 27 | // ---------------------------------------------------------------------- | ||
| 28 | // Public types | ||
| 29 | // ---------------------------------------------------------------------- | ||
| 30 | |||
| 31 | //! The type of a const iterator | ||
| 32 | using ConstIterator = SetConstIterator<T>; | ||
| 33 | |||
| 34 | //! The type of a set entry | ||
| 35 | using Entry = SetOrMapImplEntry<T, Nil>; | ||
| 36 | |||
| 37 | public: | ||
| 38 | // ---------------------------------------------------------------------- | ||
| 39 | // Public constructors and destructors | ||
| 40 | // ---------------------------------------------------------------------- | ||
| 41 | |||
| 42 | //! Zero-argument constructor | ||
| 43 |
1/1✓ Branch 18 taken 2 times.
|
2 | ExternalArraySet() = default; |
| 44 | |||
| 45 | //! Constructor providing typed backing storage. | ||
| 46 | //! entries must point to at least capacity elements of type ImplEntry. | ||
| 47 | 34 | ExternalArraySet(Entry* entries, //!< The entries | |
| 48 | FwSizeType capacity //!< The capacity | ||
| 49 | ) | ||
| 50 |
1/1✓ Branch 18 taken 34 times.
|
34 | : SetBase<T>() { |
| 51 |
1/1✓ Branch 4 taken 34 times.
|
34 | this->setStorage(entries, capacity); |
| 52 | 34 | } | |
| 53 | |||
| 54 | //! Constructor providing untyped backing storage. | ||
| 55 | //! data must be aligned according to getByteArrayAlignment(). | ||
| 56 | //! data must contain at least getByteArraySize(capacity) bytes. | ||
| 57 | 1 | ExternalArraySet(ByteArray data, //!< The data, | |
| 58 | FwSizeType capacity //!< The capacity | ||
| 59 | ) | ||
| 60 |
1/1✓ Branch 18 taken 1 times.
|
1 | : SetBase<T>() { |
| 61 |
1/1✓ Branch 4 taken 1 times.
|
1 | this->setStorage(data, capacity); |
| 62 | 1 | } | |
| 63 | |||
| 64 | //! Copy constructor | ||
| 65 |
2/2✓ Branch 19 taken 1 times.
✓ Branch 25 taken 1 times.
|
1 | ExternalArraySet(const ExternalArraySet<T>& set) : SetBase<T>() { *this = set; } |
| 66 | |||
| 67 | //! Destructor | ||
| 68 | 76 | ~ExternalArraySet() override = default; | |
| 69 | |||
| 70 | public: | ||
| 71 | // ---------------------------------------------------------------------- | ||
| 72 | // Public member functions | ||
| 73 | // ---------------------------------------------------------------------- | ||
| 74 | |||
| 75 | //! operator= | ||
| 76 | 2 | ExternalArraySet<T>& operator=(const ExternalArraySet<T>& set) { | |
| 77 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (&set != this) { |
| 78 | 2 | this->m_impl = set.m_impl; | |
| 79 | } | ||
| 80 | 2 | return *this; | |
| 81 | } | ||
| 82 | |||
| 83 | //! Get the begin iterator | ||
| 84 | //! \return The iterator | ||
| 85 |
2/2✓ Branch 4 taken 560 times.
✓ Branch 8 taken 560 times.
|
560 | ConstIterator begin() const override { return ConstIterator(this->m_impl.begin()); } |
| 86 | |||
| 87 | //! Clear the set | ||
| 88 | 183 | void clear() override { this->m_impl.clear(); } | |
| 89 | |||
| 90 | //! Get the end iterator | ||
| 91 | //! \return The iterator | ||
| 92 |
2/2✓ Branch 4 taken 184 times.
✓ Branch 8 taken 184 times.
|
184 | ConstIterator end() const override { return ConstIterator(this->m_impl.end()); } |
| 93 | |||
| 94 | //! Find a value associated with an element in the set | ||
| 95 | //! \return SUCCESS if the item was found | ||
| 96 | 2832 | Success find(const T& element //!< The element | |
| 97 | ) const override { | ||
| 98 | 2832 | Nil nil = {}; | |
| 99 |
1/1✓ Branch 4 taken 2832 times.
|
5664 | return this->m_impl.find(element, nil); |
| 100 | } | ||
| 101 | |||
| 102 | //! Get the capacity of the set (max number of entries) | ||
| 103 | //! \return The capacity | ||
| 104 | 3284 | FwSizeType getCapacity() const override { return this->m_impl.getCapacity(); } | |
| 105 | |||
| 106 | //! Get the size (number of entries) | ||
| 107 | //! \return The size | ||
| 108 | 12742 | FwSizeType getSize() const override { return this->m_impl.getSize(); } | |
| 109 | |||
| 110 | //! Insert an element in the set | ||
| 111 | //! \return SUCCESS if there is room in the set | ||
| 112 | 7352 | Success insert(const T& element //!< The element | |
| 113 | ) override { | ||
| 114 |
1/1✓ Branch 5 taken 7352 times.
|
14704 | return this->m_impl.insert(element, Nil()); |
| 115 | } | ||
| 116 | |||
| 117 | //! Remove an element from the set | ||
| 118 | //! \return SUCCESS if the element was there | ||
| 119 | 564 | Success remove(const T& element //!< The element | |
| 120 | ) override { | ||
| 121 | 564 | Nil nil = {}; | |
| 122 |
1/1✓ Branch 4 taken 564 times.
|
1128 | return this->m_impl.remove(element, nil); |
| 123 | } | ||
| 124 | |||
| 125 | //! Set the backing storage (typed data) | ||
| 126 | //! entries must point to at least capacity elements of type ImplEntry. | ||
| 127 | 34 | void setStorage(Entry* entries, //!< The entries | |
| 128 | FwSizeType capacity //!< The capacity | ||
| 129 | ) { | ||
| 130 | 34 | this->m_impl.setStorage(entries, capacity); | |
| 131 | 34 | } | |
| 132 | |||
| 133 | //! Set the backing storage (untyped data) | ||
| 134 | //! data must be aligned according to getByteArrayAlignment(). | ||
| 135 | //! data must contain at least getByteArraySize(capacity) bytes. | ||
| 136 | 1 | void setStorage(ByteArray data, //!< The data | |
| 137 | FwSizeType capacity //!< The capacity | ||
| 138 | ) { | ||
| 139 | 1 | this->m_impl.setStorage(data, capacity); | |
| 140 | 1 | } | |
| 141 | |||
| 142 | public: | ||
| 143 | // ---------------------------------------------------------------------- | ||
| 144 | // Public static functions | ||
| 145 | // ---------------------------------------------------------------------- | ||
| 146 | |||
| 147 | //! Get the alignment of the storage for an ArraySetOrMapImpl | ||
| 148 | //! \return The alignment | ||
| 149 | static constexpr U8 getByteArrayAlignment() { return ArraySetOrMapImpl<T, Nil>::getByteArrayAlignment(); } | ||
| 150 | |||
| 151 | //! Get the size of the storage for an ExternalArray of the specified capacity, | ||
| 152 | //! as a byte array | ||
| 153 | //! \return The byte array size | ||
| 154 | static constexpr FwSizeType getByteArraySize(FwSizeType capacity //!< The capacity | ||
| 155 | ) { | ||
| 156 | return ArraySetOrMapImpl<T, Nil>::getByteArraySize(capacity); | ||
| 157 | } | ||
| 158 | |||
| 159 | private: | ||
| 160 | // ---------------------------------------------------------------------- | ||
| 161 | // Private member variables | ||
| 162 | // ---------------------------------------------------------------------- | ||
| 163 | |||
| 164 | //! The set implementation | ||
| 165 | ArraySetOrMapImpl<T, Nil> m_impl = {}; | ||
| 166 | }; | ||
| 167 | |||
| 168 | } // namespace Fw | ||
| 169 | |||
| 170 | #endif | ||
| 171 |