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