| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ====================================================================== | ||
| 2 | // \file ArrayMap.hpp | ||
| 3 | // \author bocchino | ||
| 4 | // \brief An array-based map with internal storage | ||
| 5 | // ====================================================================== | ||
| 6 | |||
| 7 | #ifndef Fw_ArrayMap_HPP | ||
| 8 | #define Fw_ArrayMap_HPP | ||
| 9 | |||
| 10 | #include "Fw/DataStructures/ExternalArrayMap.hpp" | ||
| 11 | |||
| 12 | namespace Fw { | ||
| 13 | |||
| 14 | template <typename K, typename V, FwSizeType C> | ||
| 15 | class ArrayMap final : public MapBase<K, V> { | ||
| 16 | // ---------------------------------------------------------------------- | ||
| 17 | // Static assertions | ||
| 18 | // ---------------------------------------------------------------------- | ||
| 19 | |||
| 20 | static_assert(C > 0, "capacity must be greater than zero"); | ||
| 21 | |||
| 22 | // ---------------------------------------------------------------------- | ||
| 23 | // Friend class for testing | ||
| 24 | // ---------------------------------------------------------------------- | ||
| 25 | |||
| 26 | template <typename KK, typename VV, FwSizeType CC> | ||
| 27 | friend class ArrayMapTester; | ||
| 28 | |||
| 29 | public: | ||
| 30 | // ---------------------------------------------------------------------- | ||
| 31 | // Public types | ||
| 32 | // ---------------------------------------------------------------------- | ||
| 33 | |||
| 34 | //! The type of a const iterator | ||
| 35 | using ConstIterator = MapConstIterator<K, V>; | ||
| 36 | |||
| 37 | //! The type of an implementation entry | ||
| 38 | using Entry = SetOrMapImplEntry<K, V>; | ||
| 39 | |||
| 40 | //! The type of the implementation entries | ||
| 41 | using Entries = Entry[C]; | ||
| 42 | |||
| 43 | public: | ||
| 44 | // ---------------------------------------------------------------------- | ||
| 45 | // Public constructors and destructors | ||
| 46 | // ---------------------------------------------------------------------- | ||
| 47 | |||
| 48 | //! Zero-argument constructor | ||
| 49 |
3/3✓ Branch 12 taken 17 times.
✓ Branch 18 taken 16896 times.
✓ Branch 19 taken 17 times.
|
33826 | ArrayMap() : MapBase<K, V>(), m_extMap(m_entries, C) {} |
| 50 | |||
| 51 | //! Copy constructor | ||
| 52 |
4/8✓ Branch 12 taken 1 times.
✓ Branch 18 taken 1024 times.
✓ Branch 19 taken 1 times.
✓ Branch 25 taken 1 times.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 36 not taken.
✗ Branch 37 not taken.
|
1025 | ArrayMap(const ArrayMap<K, V, C>& map) : MapBase<K, V>(), m_extMap(m_entries, C) { *this = map; } |
| 53 | |||
| 54 | //! Destructor | ||
| 55 |
3/4✓ Branch 4 taken 18 times.
✗ Branch 5 not taken.
✓ Branch 11 taken 17920 times.
✓ Branch 12 taken 18 times.
|
35876 | ~ArrayMap() override = default; |
| 56 | |||
| 57 | public: | ||
| 58 | // ---------------------------------------------------------------------- | ||
| 59 | // Public member functions | ||
| 60 | // ---------------------------------------------------------------------- | ||
| 61 | |||
| 62 | //! operator= | ||
| 63 | 2 | ArrayMap<K, V, C>& operator=(const ArrayMap<K, V, C>& map) { | |
| 64 | 2 | this->m_extMap.copyDataFrom(map); | |
| 65 | 2 | return *this; | |
| 66 | } | ||
| 67 | |||
| 68 | //! Get the begin iterator | ||
| 69 | //! \return The iterator | ||
| 70 | 504 | ConstIterator begin() const override { return this->m_extMap.begin(); } | |
| 71 | |||
| 72 | //! Clear the map | ||
| 73 | 154 | void clear() override { this->m_extMap.clear(); } | |
| 74 | |||
| 75 | //! Get the end iterator | ||
| 76 | //! \return The iterator | ||
| 77 | 132 | ConstIterator end() const override { return this->m_extMap.end(); } | |
| 78 | |||
| 79 | //! Find a value associated with a key in the map | ||
| 80 | //! \return SUCCESS if the item was found | ||
| 81 | 4770 | Success find(const K& key, //!< The key | |
| 82 | V& value //!< The value | ||
| 83 | ) const override { | ||
| 84 | 4770 | return this->m_extMap.find(key, value); | |
| 85 | } | ||
| 86 | |||
| 87 | //! Get the capacity of the map (max number of entries) | ||
| 88 | //! \return The capacity | ||
| 89 | 3300 | FwSizeType getCapacity() const override { return this->m_extMap.getCapacity(); } | |
| 90 | |||
| 91 | //! Get the size (number of entries) | ||
| 92 | //! \return The size | ||
| 93 | 12606 | FwSizeType getSize() const override { return this->m_extMap.getSize(); } | |
| 94 | |||
| 95 | //! Insert a (key, value) pair in the map | ||
| 96 | //! \return SUCCESS if there is room in the map | ||
| 97 | 11896 | Success insert(const K& key, //!< The key | |
| 98 | const V& value //!< The value | ||
| 99 | ) override { | ||
| 100 | 11896 | return this->m_extMap.insert(key, value); | |
| 101 | } | ||
| 102 | |||
| 103 | //! Remove a (key, value) pair from the map | ||
| 104 | //! \return SUCCESS if the key was there | ||
| 105 | 324 | Success remove(const K& key, //!< The key | |
| 106 | V& value //!< The value | ||
| 107 | ) override { | ||
| 108 | 324 | return this->m_extMap.remove(key, value); | |
| 109 | } | ||
| 110 | |||
| 111 | private: | ||
| 112 | // ---------------------------------------------------------------------- | ||
| 113 | // Private member variables | ||
| 114 | // ---------------------------------------------------------------------- | ||
| 115 | |||
| 116 | //! The external map implementation | ||
| 117 | ExternalArrayMap<K, V> m_extMap = {}; | ||
| 118 | |||
| 119 | //! The array providing the backing memory for m_extMap | ||
| 120 | Entries m_entries = {}; | ||
| 121 | }; | ||
| 122 | |||
| 123 | } // namespace Fw | ||
| 124 | |||
| 125 | #endif | ||
| 126 |