GCC Code Coverage Report


Directory: ./
File: Fw/DataStructures/ExternalRedBlackTreeSet.hpp
Date: 2026-09-03 22:12:29
Exec Total Coverage
Lines: 9 19 47.4%
Functions: 4 11 36.4%
Branches: 2 9 22.2%

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 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 1 ExternalRedBlackTreeSet(Node* nodes, //!< The nodes
52 Index* freeNodes, //!< The free nodes
53 FwSizeType capacity //!< The capacity
54 )
55
1/1
✓ Branch 2 taken 1 times.
1 : SetBase<T>() {
56
1/1
✓ Branch 1 taken 1 times.
1 this->setStorage(nodes, freeNodes, capacity);
57 1 }
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 ExternalRedBlackTreeSet(ByteArray data, //!< The data,
63 FwSizeType capacity //!< The capacity
64 )
65 : SetBase<T>() {
66 this->setStorage(data, capacity);
67 }
68
69 //! Copy constructor
70 ExternalRedBlackTreeSet(const ExternalRedBlackTreeSet<T>& set) : SetBase<T>() { *this = set; }
71
72 //! Destructor
73 2 ~ExternalRedBlackTreeSet() override = default;
74
75 public:
76 // ----------------------------------------------------------------------
77 // Public member functions
78 // ----------------------------------------------------------------------
79
80 //! operator=
81 ExternalRedBlackTreeSet<T>& operator=(const ExternalRedBlackTreeSet<T>& set) {
82 if (&set != this) {
83 this->m_impl = set.m_impl;
84 }
85 return *this;
86 }
87
88 //! Get the begin iterator
89 //! \return The iterator
90 ConstIterator begin() const override { return ConstIterator(this->m_impl.begin()); }
91
92 //! Clear the set
93 1 void clear() override { this->m_impl.clear(); }
94
95 //! Get the end iterator
96 //! \return The iterator
97 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 Success find(const T& element //!< The element
102 ) const override {
103 Nil nil = {};
104 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 FwSizeType getCapacity() const override { return this->m_impl.getCapacity(); }
110
111 //! Get the size (number of entries)
112 //! \return The size
113 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 Success insert(const T& element //!< The element
118 ) override {
119 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 Success remove(const T& element //!< The element
125 ) override {
126 Nil nil = {};
127 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 1 void setStorage(Node* nodes, //!< The nodes
134 Index* freeNodes, //!< The free nodes
135 FwSizeType capacity //!< The capacity
136 ) {
137 1 this->m_impl.setStorage(nodes, freeNodes, capacity);
138 1 }
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 void setStorage(ByteArray data, //!< The data
144 FwSizeType capacity //!< The capacity
145 ) {
146 this->m_impl.setStorage(data, capacity);
147 }
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