GCC Code Coverage Report


Directory: Fw/DataStructures/
File: ExternalRedBlackTreeMap.hpp
Date: 2026-09-03 21:14:50
Exec Total Coverage
Lines: 32 32 100.0%
Functions: 16 16 100.0%
Branches: 12 13 92.3%

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 34 ExternalRedBlackTreeMap(Node* nodes, //!< The nodes
51 Index* freeNodes, //!< The free nodes
52 FwSizeType capacity //!< The capacity
53 )
54
1/1
✓ Branch 12 taken 34 times.
34 : MapBase<K, V>() {
55
1/1
✓ Branch 4 taken 34 times.
34 this->setStorage(nodes, freeNodes, capacity);
56 34 }
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 76 ~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 479 times.
✓ Branch 8 taken 479 times.
479 ConstIterator begin() const override { return ConstIterator(this->m_impl.begin()); }
90
91 //! Clear the map
92 185 void clear() override { this->m_impl.clear(); }
93
94 //! Get the end iterator
95 //! \return The iterator
96
2/2
✓ Branch 4 taken 164 times.
✓ Branch 8 taken 164 times.
164 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 2801 Success find(const K& key, //!< The key
101 V& value //!< The value
102 ) const override {
103 2801 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 3326 FwSizeType getCapacity() const override { return this->m_impl.getCapacity(); }
109
110 //! Get the size (number of entries)
111 //! \return The size
112 12624 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 7310 Success insert(const K& key, //!< The key
117 const V& value //!< The value
118 ) override {
119 7310 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 606 Success remove(const K& key, //!< The key
125 V& value //!< The value
126 ) override {
127 606 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 34 void setStorage(Node* nodes, //!< The nodes
134 Index* freeNodes, //!< The free nodes
135 FwSizeType capacity //!< The capacity
136 ) {
137 34 this->m_impl.setStorage(nodes, freeNodes, capacity);
138 34 }
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