| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ====================================================================== | ||
| 2 | // \file ExternalArrayMap.hpp | ||
| 3 | // \author bocchino | ||
| 4 | // \brief An array-based map with external storage | ||
| 5 | // ====================================================================== | ||
| 6 | |||
| 7 | #ifndef Fw_ExternalArrayMap_HPP | ||
| 8 | #define Fw_ExternalArrayMap_HPP | ||
| 9 | |||
| 10 | #include "Fw/DataStructures/ArraySetOrMapImpl.hpp" | ||
| 11 | #include "Fw/DataStructures/MapBase.hpp" | ||
| 12 | #include "Fw/Types/Assert.hpp" | ||
| 13 | |||
| 14 | namespace Fw { | ||
| 15 | |||
| 16 | template <typename K, typename V> | ||
| 17 | class ExternalArrayMap final : public MapBase<K, V> { | ||
| 18 | // ---------------------------------------------------------------------- | ||
| 19 | // Friend class for testing | ||
| 20 | // ---------------------------------------------------------------------- | ||
| 21 | |||
| 22 | template <typename KK, typename VV> | ||
| 23 | friend class ExternalArrayMapTester; | ||
| 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 map entry | ||
| 34 | using Entry = SetOrMapImplEntry<K, V>; | ||
| 35 | |||
| 36 | public: | ||
| 37 | // ---------------------------------------------------------------------- | ||
| 38 | // Public constructors and destructors | ||
| 39 | // ---------------------------------------------------------------------- | ||
| 40 | |||
| 41 | //! Zero-argument constructor | ||
| 42 |
1/1✓ Branch 18 taken 2 times.
|
2 | ExternalArrayMap() = default; |
| 43 | |||
| 44 | //! Constructor providing typed backing storage. | ||
| 45 | //! entries must point to at least capacity elements of type Entry. | ||
| 46 | 111 | ExternalArrayMap(Entry* entries, //!< The entries | |
| 47 | FwSizeType capacity //!< The capacity | ||
| 48 | ) | ||
| 49 |
1/1✓ Branch 18 taken 93 times.
|
111 | : MapBase<K, V>() { |
| 50 |
1/1✓ Branch 4 taken 93 times.
|
111 | this->setStorage(entries, capacity); |
| 51 | 111 | } | |
| 52 | |||
| 53 | //! Constructor providing untyped backing storage. | ||
| 54 | //! data must be aligned according to getByteArrayAlignment(). | ||
| 55 | //! data must contain at least getByteArraySize(capacity) bytes. | ||
| 56 | 1 | ExternalArrayMap(ByteArray data, //!< The data, | |
| 57 | FwSizeType capacity //!< The capacity | ||
| 58 | ) | ||
| 59 |
1/1✓ Branch 18 taken 1 times.
|
1 | : MapBase<K, V>() { |
| 60 |
1/1✓ Branch 4 taken 1 times.
|
1 | this->setStorage(data, capacity); |
| 61 | 1 | } | |
| 62 | |||
| 63 | //! Copy constructor | ||
| 64 |
2/2✓ Branch 19 taken 1 times.
✓ Branch 25 taken 1 times.
|
1 | ExternalArrayMap(const ExternalArrayMap<K, V>& map) : MapBase<K, V>() { *this = map; } |
| 65 | |||
| 66 | //! Destructor | ||
| 67 | 194 | ~ExternalArrayMap() override = default; | |
| 68 | |||
| 69 | public: | ||
| 70 | // ---------------------------------------------------------------------- | ||
| 71 | // Public member functions | ||
| 72 | // ---------------------------------------------------------------------- | ||
| 73 | |||
| 74 | //! operator= | ||
| 75 | 2 | ExternalArrayMap<K, V>& operator=(const ExternalArrayMap<K, V>& map) { | |
| 76 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (&map != this) { |
| 77 | 2 | this->m_impl = map.m_impl; | |
| 78 | } | ||
| 79 | 2 | return *this; | |
| 80 | } | ||
| 81 | |||
| 82 | //! Get the begin iterator | ||
| 83 | //! \return The iterator | ||
| 84 |
2/2✓ Branch 4 taken 532 times.
✓ Branch 8 taken 532 times.
|
532 | ConstIterator begin() const override { return ConstIterator(this->m_impl.begin()); } |
| 85 | |||
| 86 | //! Clear the map | ||
| 87 | 283 | void clear() override { this->m_impl.clear(); } | |
| 88 | |||
| 89 | //! Get the end iterator | ||
| 90 | //! \return The iterator | ||
| 91 |
2/2✓ Branch 4 taken 173 times.
✓ Branch 8 taken 173 times.
|
173 | ConstIterator end() const override { return ConstIterator(this->m_impl.end()); } |
| 92 | |||
| 93 | //! Find a value associated with a key in the map | ||
| 94 | //! \return SUCCESS if the item was found | ||
| 95 | 26318 | Success find(const K& key, //!< The key | |
| 96 | V& value //!< The value | ||
| 97 | ) const override { | ||
| 98 | 26318 | return this->m_impl.find(key, value); | |
| 99 | } | ||
| 100 | |||
| 101 | //! Get the capacity of the map (max number of entries) | ||
| 102 | //! \return The capacity | ||
| 103 | 3322 | FwSizeType getCapacity() const override { return this->m_impl.getCapacity(); } | |
| 104 | |||
| 105 | //! Get the size (number of entries) | ||
| 106 | //! \return The size | ||
| 107 | 12958 | FwSizeType getSize() const override { return this->m_impl.getSize(); } | |
| 108 | |||
| 109 | //! Insert a (key, value) pair in the map | ||
| 110 | //! \return SUCCESS if there is room in the map | ||
| 111 | 25014 | Success insert(const K& key, //!< The key | |
| 112 | const V& value //!< The value | ||
| 113 | ) override { | ||
| 114 | 25014 | return this->m_impl.insert(key, value); | |
| 115 | } | ||
| 116 | |||
| 117 | //! Remove a (key, value) pair from the map | ||
| 118 | //! \return SUCCESS if the key was there | ||
| 119 | 5674 | Success remove(const K& key, //!< The key | |
| 120 | V& value //!< The value | ||
| 121 | ) override { | ||
| 122 | 5674 | return this->m_impl.remove(key, value); | |
| 123 | } | ||
| 124 | |||
| 125 | //! Set the backing storage (typed data) | ||
| 126 | //! entries must point to at least capacity elements of type Entry. | ||
| 127 | 111 | void setStorage(Entry* entries, //!< The entries | |
| 128 | FwSizeType capacity //!< The capacity | ||
| 129 | ) { | ||
| 130 | 111 | this->m_impl.setStorage(entries, capacity); | |
| 131 | 111 | } | |
| 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<K, V>::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<K, V>::getByteArraySize(capacity); | ||
| 157 | } | ||
| 158 | |||
| 159 | private: | ||
| 160 | // ---------------------------------------------------------------------- | ||
| 161 | // Private member variables | ||
| 162 | // ---------------------------------------------------------------------- | ||
| 163 | |||
| 164 | //! The map implementation | ||
| 165 | ArraySetOrMapImpl<K, V> m_impl = {}; | ||
| 166 | }; | ||
| 167 | |||
| 168 | } // namespace Fw | ||
| 169 | |||
| 170 | #endif | ||
| 171 |