GCC Code Coverage Report


Directory: ./
File: Fw/DataStructures/SetConstIterator.hpp
Date: 2026-09-03 22:12:29
Exec Total Coverage
Lines: 0 33 0.0%
Functions: 0 17 0.0%
Branches: 0 5 0.0%

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