GCC Code Coverage Report


Directory: ./
File: Fw/DataStructures/MapConstIterator.hpp
Date: 2026-09-23 21:11:01
Exec Total Coverage
Lines: 47 54 87.0%
Functions: 51 63 81.0%
Branches: 8 11 72.7%

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