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