GCC Code Coverage Report


Directory: ./
File: Fw/DataStructures/SetConstIterator.hpp
Date: 2026-10-14 21:15:02
Exec Total Coverage
Lines: 46 53 86.8%
Functions: 23 23 100.0%
Branches: 8 11 72.7%

Line Branch Exec Source
1 // ======================================================================
2 // \title SetConstIterator
3 // \author bocchino
4 // \brief An abstract class template representing a const iterator for a set
5 // ======================================================================
6
7 #ifndef Fw_SetConstIterator_HPP
8 #define Fw_SetConstIterator_HPP
9
10 #include <new>
11
12 #include "Fw/DataStructures/ArraySetOrMapImpl.hpp"
13 #include "Fw/DataStructures/Nil.hpp"
14 #include "Fw/DataStructures/RedBlackTreeSetOrMapImpl.hpp"
15 #include "Fw/FPrimeBasicTypes.hpp"
16
17 namespace Fw {
18
19 template <typename T>
20 class SetConstIterator {
21 public:
22 // ----------------------------------------------------------------------
23 // Public types
24 // ----------------------------------------------------------------------
25
26 //! The type of an array iterator
27 using ArrayIterator = typename ArraySetOrMapImpl<T, Nil>::ConstIterator;
28
29 //! The type of a red-black tree iterator
30 using RedBlackTreeIterator = typename RedBlackTreeSetOrMapImpl<T, Nil>::ConstIterator;
31
32 private:
33 // ----------------------------------------------------------------------
34 // Private types
35 // ----------------------------------------------------------------------
36
37 //! The type of an implementation kind
38 using ImplKind = typename SetOrMapImplConstIterator<T, Nil>::ImplKind;
39
40 //! The type of an implementation
41 union Impl {
42 //! Default constructor
43 4307 Impl() {}
44 //! Array constructor
45 691 Impl(const ArrayIterator& it) : array(it) {}
46 //! Red-black tree constructor
47 124603 Impl(const RedBlackTreeIterator& it) : redBlackTree(it) {}
48 //! An array iterator
49 ArrayIterator array;
50 //! A red-black tree iterator
51 RedBlackTreeIterator redBlackTree;
52 // ! Destructor
53 129601 ~Impl() {}
54 };
55
56 public:
57 // ----------------------------------------------------------------------
58 // Constructors and destructors
59 // ----------------------------------------------------------------------
60
61 //! Constructor providing an array implementation
62 691 SetConstIterator(const ArrayIterator& it) : m_impl(it), m_implIterator(&m_impl.array) {}
63
64 //! Constructor providing a red-black tree implementation
65 124603 SetConstIterator(const RedBlackTreeIterator& it) : m_impl(it), m_implIterator(&m_impl.redBlackTree) {}
66
67 //! Copy constructor
68 4307 SetConstIterator(const SetConstIterator& it) : m_impl(), m_implIterator() {
69
2/2
✓ Branch 2 taken 4307 times.
✓ Branch 11 taken 4307 times.
4307 const auto implKind = it.getImplIterator().implKind();
70
2/3
✓ Branch 0 taken 2145 times.
✓ Branch 1 taken 2162 times.
✗ Branch 2 not taken.
4307 switch (implKind) {
71 2145 case ImplKind::ARRAY:
72 2145 this->m_implIterator = new (&this->m_impl.array) ArrayIterator(it.m_impl.array);
73 2145 break;
74 2162 case ImplKind::RED_BLACK_TREE:
75 2162 this->m_implIterator = new (&this->m_impl.redBlackTree) RedBlackTreeIterator(it.m_impl.redBlackTree);
76 2162 break;
77 ✗ default:
78 ✗ FW_ASSERT(false, static_cast<FwAssertArgType>(implKind));
79 ✗ break;
80 }
81 4307 }
82
83 //! Destructor
84 129601 ~SetConstIterator() {}
85
86 public:
87 // ----------------------------------------------------------------------
88 // Public member functions
89 // ----------------------------------------------------------------------
90
91 //! Copy assignment operator
92 SetConstIterator& operator=(const SetConstIterator&) = default;
93
94 //! Equality comparison operator
95 62831 bool operator==(const SetConstIterator& it) const {
96 62831 bool result = false;
97 62831 const auto implKind1 = this->getImplIterator().implKind();
98 62831 const auto implKind2 = it.getImplIterator().implKind();
99
1/2
✓ Branch 0 taken 62831 times.
✗ Branch 1 not taken.
62831 if (implKind1 == implKind2) {
100
2/3
✓ Branch 0 taken 443 times.
✓ Branch 1 taken 62388 times.
✗ Branch 2 not taken.
62831 switch (implKind1) {
101 443 case ImplKind::ARRAY:
102 443 result = this->m_impl.array.compareEqual(it.m_impl.array);
103 443 break;
104 62388 case ImplKind::RED_BLACK_TREE:
105 62388 result = this->m_impl.redBlackTree.compareEqual(it.m_impl.redBlackTree);
106 62388 break;
107 ✗ default:
108 ✗ FW_ASSERT(false, static_cast<FwAssertArgType>(implKind1));
109 ✗ break;
110 }
111 }
112 62831 return result;
113 }
114
115 //! Inequality comparison operator
116 864 bool operator!=(const SetConstIterator& it) const { return !(*this == it); };
117
118 //! Prefix increment
119 4838 SetConstIterator& operator++() {
120 4838 this->getImplIterator().increment();
121 4838 return *this;
122 }
123
124 //! Postfix increment
125 4307 SetConstIterator operator++(int) {
126 4307 SetConstIterator tmp = *this;
127
1/1
✓ Branch 2 taken 4307 times.
4307 ++(*this);
128 4307 return tmp;
129 ✗ }
130
131 //! Check whether the iterator is in range
132 845 bool isInRange() const { return this->getImplIterator().isInRange(); }
133
134 //! Dereference
135 67316 const T& operator*() const { return this->getImplIterator().getEntry().getKeyOrElement(); }
136
137 //! Pointer
138 const T* operator->() const { return &this->getImplIterator().getEntry().getKeyOrElement(); }
139
140 private:
141 // ----------------------------------------------------------------------
142 // Private helper functions
143 // ----------------------------------------------------------------------
144
145 //! Assert and get the impl iterator
146 4838 SetOrMapImplConstIterator<T, Nil>& getImplIterator() {
147 4838 FW_ASSERT(this->m_implIterator != nullptr);
148 4838 return *this->m_implIterator;
149 }
150
151 //! Assert and get the impl iterator (const)
152 198130 const SetOrMapImplConstIterator<T, Nil>& getImplIterator() const {
153 198130 FW_ASSERT(this->m_implIterator != nullptr);
154 198130 return *this->m_implIterator;
155 }
156
157 private:
158 // ----------------------------------------------------------------------
159 // Private member variables
160 // ----------------------------------------------------------------------
161
162 //! The implementation
163 Impl m_impl;
164
165 //! The impl iterator
166 SetOrMapImplConstIterator<T, Nil>* m_implIterator = nullptr;
167 };
168
169 } // namespace Fw
170
171 #endif
172