| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ====================================================================== | ||
| 2 | // \file ExternalRedBlackTreeMap.hpp | ||
| 3 | // \author bocchino | ||
| 4 | // \brief A map based on a red-black tree with external storage | ||
| 5 | // ====================================================================== | ||
| 6 | |||
| 7 | #ifndef Fw_ExternalRedBlackTreeMap_HPP | ||
| 8 | #define Fw_ExternalRedBlackTreeMap_HPP | ||
| 9 | |||
| 10 | #include "Fw/DataStructures/MapBase.hpp" | ||
| 11 | #include "Fw/DataStructures/RedBlackTreeSetOrMapImpl.hpp" | ||
| 12 | #include "Fw/Types/Assert.hpp" | ||
| 13 | |||
| 14 | namespace Fw { | ||
| 15 | |||
| 16 | template <typename K, typename V> | ||
| 17 | class ExternalRedBlackTreeMap final : public MapBase<K, V> { | ||
| 18 | // ---------------------------------------------------------------------- | ||
| 19 | // Friend class for testing | ||
| 20 | // ---------------------------------------------------------------------- | ||
| 21 | |||
| 22 | template <typename KK, typename VV> | ||
| 23 | friend class ExternalRedBlackTreeMapTester; | ||
| 24 | |||
| 25 | public: | ||
| 26 | // ---------------------------------------------------------------------- | ||
| 27 | // Public types | ||
| 28 | // ---------------------------------------------------------------------- | ||
| 29 | |||
| 30 | //! The type of a const iterator | ||
| 31 | using ConstIterator = MapConstIterator<K, V>; | ||
| 32 | |||
| 33 | //! The type of a tree node | ||
| 34 | using Node = typename RedBlackTreeSetOrMapImpl<K, V>::Node; | ||
| 35 | |||
| 36 | //! The type of a tree node index | ||
| 37 | using Index = typename RedBlackTreeSetOrMapImpl<K, V>::Index; | ||
| 38 | |||
| 39 | public: | ||
| 40 | // ---------------------------------------------------------------------- | ||
| 41 | // Public constructors and destructors | ||
| 42 | // ---------------------------------------------------------------------- | ||
| 43 | |||
| 44 | //! Zero-argument constructor | ||
| 45 |
1/1✓ Branch 12 taken 2 times.
|
2 | ExternalRedBlackTreeMap() = default; |
| 46 | |||
| 47 | //! Constructor providing typed backing storage. | ||
| 48 | //! nodes must point to at least capacity elements of type Node. | ||
| 49 | //! freeNodes must point to at least capacity elements of type FwSizeType. | ||
| 50 | 64 | ExternalRedBlackTreeMap(Node* nodes, //!< The nodes | |
| 51 | Index* freeNodes, //!< The free nodes | ||
| 52 | FwSizeType capacity //!< The capacity | ||
| 53 | ) | ||
| 54 |
1/1✓ Branch 12 taken 64 times.
|
64 | : MapBase<K, V>() { |
| 55 |
1/1✓ Branch 4 taken 64 times.
|
64 | this->setStorage(nodes, freeNodes, capacity); |
| 56 | 64 | } | |
| 57 | |||
| 58 | //! Constructor providing untyped backing storage. | ||
| 59 | //! data must be aligned according to getByteArrayAlignment(). | ||
| 60 | //! data must contain at least getByteArraySize(capacity) bytes. | ||
| 61 | 1 | ExternalRedBlackTreeMap(ByteArray data, //!< The data, | |
| 62 | FwSizeType capacity //!< The capacity | ||
| 63 | ) | ||
| 64 |
1/1✓ Branch 12 taken 1 times.
|
1 | : MapBase<K, V>() { |
| 65 |
1/1✓ Branch 4 taken 1 times.
|
1 | this->setStorage(data, capacity); |
| 66 | 1 | } | |
| 67 | |||
| 68 | //! Copy constructor | ||
| 69 |
2/2✓ Branch 13 taken 1 times.
✓ Branch 19 taken 1 times.
|
1 | ExternalRedBlackTreeMap(const ExternalRedBlackTreeMap<K, V>& map) : MapBase<K, V>() { *this = map; } |
| 70 | |||
| 71 | //! Destructor | ||
| 72 | 136 | ~ExternalRedBlackTreeMap() override = default; | |
| 73 | |||
| 74 | public: | ||
| 75 | // ---------------------------------------------------------------------- | ||
| 76 | // Public member functions | ||
| 77 | // ---------------------------------------------------------------------- | ||
| 78 | |||
| 79 | //! operator= | ||
| 80 | 2 | ExternalRedBlackTreeMap<K, V>& operator=(const ExternalRedBlackTreeMap<K, V>& map) { | |
| 81 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (&map != this) { |
| 82 | 2 | this->m_impl = map.m_impl; | |
| 83 | } | ||
| 84 | 2 | return *this; | |
| 85 | } | ||
| 86 | |||
| 87 | //! Get the begin iterator | ||
| 88 | //! \return The iterator | ||
| 89 |
2/2✓ Branch 4 taken 539 times.
✓ Branch 8 taken 539 times.
|
539 | ConstIterator begin() const override { return ConstIterator(this->m_impl.begin()); } |
| 90 | |||
| 91 | //! Clear the map | ||
| 92 | 205 | void clear() override { this->m_impl.clear(); } | |
| 93 | |||
| 94 | //! Get the end iterator | ||
| 95 | //! \return The iterator | ||
| 96 |
2/2✓ Branch 4 taken 190 times.
✓ Branch 8 taken 190 times.
|
190 | ConstIterator end() const override { return ConstIterator(this->m_impl.end()); } |
| 97 | |||
| 98 | //! Find a value associated with a key in the map | ||
| 99 | //! \return SUCCESS if the item was found | ||
| 100 | 3148 | Success find(const K& key, //!< The key | |
| 101 | V& value //!< The value | ||
| 102 | ) const override { | ||
| 103 | 3148 | return this->m_impl.find(key, value); | |
| 104 | } | ||
| 105 | |||
| 106 | //! Get the capacity of the map (max number of entries) | ||
| 107 | //! \return The capacity | ||
| 108 | 3349 | FwSizeType getCapacity() const override { return this->m_impl.getCapacity(); } | |
| 109 | |||
| 110 | //! Get the size (number of entries) | ||
| 111 | //! \return The size | ||
| 112 | 12809 | FwSizeType getSize() const override { return this->m_impl.getSize(); } | |
| 113 | |||
| 114 | //! Insert a (key, value) pair in the map | ||
| 115 | //! \return SUCCESS if there is room in the map | ||
| 116 | 7496 | Success insert(const K& key, //!< The key | |
| 117 | const V& value //!< The value | ||
| 118 | ) override { | ||
| 119 | 7496 | return this->m_impl.insert(key, value); | |
| 120 | } | ||
| 121 | |||
| 122 | //! Remove a (key, value) pair from the map | ||
| 123 | //! \return SUCCESS if the key was there | ||
| 124 | 602 | Success remove(const K& key, //!< The key | |
| 125 | V& value //!< The value | ||
| 126 | ) override { | ||
| 127 | 602 | return this->m_impl.remove(key, value); | |
| 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 | 64 | void setStorage(Node* nodes, //!< The nodes | |
| 134 | Index* freeNodes, //!< The free nodes | ||
| 135 | FwSizeType capacity //!< The capacity | ||
| 136 | ) { | ||
| 137 | 64 | this->m_impl.setStorage(nodes, freeNodes, capacity); | |
| 138 | 64 | } | |
| 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<K, V>::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<K, V>::getByteArraySize(capacity); | ||
| 164 | } | ||
| 165 | |||
| 166 | private: | ||
| 167 | // ---------------------------------------------------------------------- | ||
| 168 | // Private member variables | ||
| 169 | // ---------------------------------------------------------------------- | ||
| 170 | |||
| 171 | //! The map implementation | ||
| 172 | RedBlackTreeSetOrMapImpl<K, V> m_impl = {}; | ||
| 173 | }; | ||
| 174 | |||
| 175 | } // namespace Fw | ||
| 176 | |||
| 177 | #endif | ||
| 178 |