| 80 | }; |
| 81 | /** Iterator type returned by begin(), which efficiently iterates all 1 positions. */ |
| 82 | class Iterator |
| 83 | { |
| 84 | friend class IntBitSet; |
| 85 | I m_val; /**< The original integer's remaining bits. */ |
| 86 | unsigned m_pos; /** Last reported 1 position (if m_pos != 0). */ |
| 87 | constexpr Iterator(I val) noexcept : m_val(val), m_pos(0) |
| 88 | { |
| 89 | if (m_val != 0) m_pos = std::countr_zero(m_val); |
| 90 | } |
| 91 | public: |
| 92 | /** Do not allow external code to construct an Iterator. */ |
| 93 | Iterator() = delete; |
| 94 | // Copying is allowed. |
| 95 | constexpr Iterator(const Iterator&) noexcept = default; |
| 96 | constexpr Iterator& operator=(const Iterator&) noexcept = default; |
| 97 | /** Test whether we are done (can only compare with IteratorEnd). */ |
| 98 | constexpr friend bool operator==(const Iterator& a, const IteratorEnd&) noexcept |
| 99 | { |
| 100 | return a.m_val == 0; |
| 101 | } |
| 102 | /** Progress to the next 1 bit (only if != IteratorEnd). */ |
| 103 | constexpr Iterator& operator++() noexcept |
| 104 | { |
| 105 | Assume(m_val != 0); |
| 106 | m_val &= m_val - I{1U}; |
| 107 | if (m_val != 0) m_pos = std::countr_zero(m_val); |
| 108 | return *this; |
| 109 | } |
| 110 | /** Get the current bit position (only if != IteratorEnd). */ |
| 111 | constexpr unsigned operator*() const noexcept |
| 112 | { |
| 113 | Assume(m_val != 0); |
| 114 | return m_pos; |
| 115 | } |
| 116 | }; |
| 117 | |
| 118 | public: |
| 119 | /** Construct an all-zero bitset. */ |