GCC Code Coverage Report


Directory: ./
File: Fw/DataStructures/SetConstIterator.hpp
Date: 2026-09-03 21:13:48
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 4310 Impl() {}
44 //! Array constructor
45 660 Impl(const ArrayIterator& it) : array(it) {}
46 //! Red-black tree constructor
47 125030 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 130000 ~Impl() {}
54 };
55
56 public:
57 // ----------------------------------------------------------------------
58 // Constructors and destructors
59 // ----------------------------------------------------------------------
60
61 //! Constructor providing an array implementation
62 660 SetConstIterator(const ArrayIterator& it) : m_impl(it), m_implIterator(&m_impl.array) {}
63
64 //! Constructor providing a red-black tree implementation
65 125030 SetConstIterator(const RedBlackTreeIterator& it) : m_impl(it), m_implIterator(&m_impl.redBlackTree) {}
66
67 //! Copy constructor
68 4310 SetConstIterator(const SetConstIterator& it) : m_impl(), m_implIterator() {
69
2/2
✓ Branch 2 taken 4310 times.
✓ Branch 11 taken 4310 times.
4310 const auto implKind = it.getImplIterator().implKind();
70
2/3
✓ Branch 0 taken 2146 times.
✓ Branch 1 taken 2164 times.
✗ Branch 2 not taken.
4310 switch (implKind) {
71 2146 case ImplKind::ARRAY:
72 2146 this->m_implIterator = new (&this->m_impl.array) ArrayIterator(it.m_impl.array);
73 2146 break;
74 2164 case ImplKind::RED_BLACK_TREE:
75 2164 this->m_implIterator = new (&this->m_impl.redBlackTree) RedBlackTreeIterator(it.m_impl.redBlackTree);
76 2164 break;
77 default:
78 FW_ASSERT(false, static_cast<FwAssertArgType>(implKind));
79 break;
80 }
81 4310 }
82
83 //! Destructor
84 130000 ~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 63061 bool operator==(const SetConstIterator& it) const {
96 63061 bool result = false;
97 63061 const auto implKind1 = this->getImplIterator().implKind();
98 63061 const auto implKind2 = it.getImplIterator().implKind();
99
1/2
✓ Branch 0 taken 63061 times.
✗ Branch 1 not taken.
63061 if (implKind1 == implKind2) {
100
2/3
✓ Branch 0 taken 451 times.
✓ Branch 1 taken 62610 times.
✗ Branch 2 not taken.
63061 switch (implKind1) {
101 451 case ImplKind::ARRAY:
102 451 result = this->m_impl.array.compareEqual(it.m_impl.array);
103 451 break;
104 62610 case ImplKind::RED_BLACK_TREE:
105 62610 result = this->m_impl.redBlackTree.compareEqual(it.m_impl.redBlackTree);
106 62610 break;
107 default:
108 FW_ASSERT(false, static_cast<FwAssertArgType>(implKind1));
109 break;
110 }
111 }
112 63061 return result;
113 }
114
115 //! Inequality comparison operator
116 876 bool operator!=(const SetConstIterator& it) const { return !(*this == it); };
117
118 //! Prefix increment
119 4857 SetConstIterator& operator++() {
120 4857 this->getImplIterator().increment();
121 4857 return *this;
122 }
123
124 //! Postfix increment
125 4310 SetConstIterator operator++(int) {
126 4310 SetConstIterator tmp = *this;
127
1/1
✓ Branch 2 taken 4310 times.
4310 ++(*this);
128 4310 return tmp;
129 }
130
131 //! Check whether the iterator is in range
132 816 bool isInRange() const { return this->getImplIterator().isInRange(); }
133
134 //! Dereference
135 67518 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 4857 SetOrMapImplConstIterator<T, Nil>& getImplIterator() {
147 4857 FW_ASSERT(this->m_implIterator != nullptr);
148 4857 return *this->m_implIterator;
149 }
150
151 //! Assert and get the impl iterator (const)
152 198766 const SetOrMapImplConstIterator<T, Nil>& getImplIterator() const {
153 198766 FW_ASSERT(this->m_implIterator != nullptr);
154 198766 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