sparrow 0.9.0
Loading...
Searching...
No Matches
bitset_iterator.hpp
Go to the documentation of this file.
1// Copyright 2024 Man Group Operations Limited
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
20
21namespace sparrow
22{
23 template <class B>
24 class bitset_reference;
25
36 template <class B, bool is_const>
38 bitset_iterator<B, is_const>,
39 mpl::constify_t<typename B::value_type, is_const>,
40 std::random_access_iterator_tag,
41 std::conditional_t<is_const, bool, bitset_reference<B>>>
42 {
43 public:
44
49 std::contiguous_iterator_tag,
50 std::conditional_t<is_const, bool, bitset_reference<B>>>;
51 using reference = typename base_type::reference;
52 using difference_type = typename base_type::difference_type;
53
56 using size_type = typename B::size_type;
57
58 constexpr bitset_iterator() noexcept = default;
59 constexpr bitset_iterator(bitset_type* bitset, size_type index);
60
61 private:
62
63 constexpr reference dereference() const noexcept;
64 constexpr void increment();
65 constexpr void decrement();
66 constexpr void advance(difference_type n);
67 [[nodiscard]] constexpr difference_type distance_to(const self_type& rhs) const noexcept;
68 [[nodiscard]] constexpr bool equal(const self_type& rhs) const noexcept;
69 [[nodiscard]] constexpr bool less_than(const self_type& rhs) const noexcept;
70
71 [[nodiscard]] static constexpr difference_type as_signed(size_type i);
72 [[nodiscard]] static constexpr size_type as_unsigned(difference_type i);
73
74 bitset_type* p_bitset = nullptr;
75 size_type m_index;
76
77 friend class iterator_access;
78 };
79
80 template <class B, bool is_const>
81 constexpr bitset_iterator<B, is_const>::bitset_iterator(bitset_type* bitset, size_type index)
82 : p_bitset(bitset)
83 , m_index(index)
84 {
85 }
86
87 template <class B, bool is_const>
88 constexpr auto bitset_iterator<B, is_const>::dereference() const noexcept -> reference
89 {
90 if constexpr (is_const)
91 {
92 if (p_bitset->data() == nullptr)
93 {
94 return true;
95 }
96 return p_bitset->test(m_index);
97 }
98 else
99 {
100 return bitset_reference<B>(*p_bitset, m_index);
101 }
102 }
103
104 template <class B, bool is_const>
106 {
107 ++m_index;
108 }
109
110 template <class B, bool is_const>
112 {
113 --m_index;
114 }
115
116 template <class B, bool is_const>
118 {
119 if (n < 0 && static_cast<size_type>(-n) > m_index)
120 {
121 // Prevent underflow by clamping m_index to 0
122 m_index = 0;
123 }
124 else
125 {
126 m_index = as_unsigned(as_signed(m_index) + n);
127 }
128 }
129
130 template <class B, bool is_const>
131 constexpr auto bitset_iterator<B, is_const>::distance_to(const self_type& rhs) const noexcept
133 {
134 SPARROW_ASSERT_TRUE(p_bitset == rhs.p_bitset);
135 return as_signed(rhs.m_index) - as_signed(m_index);
136 }
137
138 template <class B, bool is_const>
139 constexpr bool bitset_iterator<B, is_const>::equal(const self_type& rhs) const noexcept
140 {
141 SPARROW_ASSERT_TRUE(p_bitset == rhs.p_bitset);
142 return m_index == rhs.m_index;
143 }
144
145 template <class B, bool is_const>
146 constexpr bool bitset_iterator<B, is_const>::less_than(const self_type& rhs) const noexcept
147 {
148 SPARROW_ASSERT_TRUE(p_bitset == rhs.p_bitset);
149 return m_index < rhs.m_index;
150 }
151
152 template <class B, bool is_const>
154 {
155 return static_cast<difference_type>(i);
156 }
157
158 template <class B, bool is_const>
160 {
161 return static_cast<size_type>(i);
162 }
163}
typename base_type::reference reference
constexpr bitset_iterator() noexcept=default
mpl::constify_t< B, is_const > bitset_type
iterator_base< self_type, mpl::constify_t< typename B::value_type, is_const >, std::contiguous_iterator_tag, std::conditional_t< is_const, bool, bitset_reference< B > > > base_type
mpl::constify_t< typename B::block_type, is_const > block_type
typename B::size_type size_type
bitset_iterator< B, is_const > self_type
typename base_type::difference_type difference_type
Reference proxy used by the bitset_iterator class to make it possible to assign a bit of a bitset as ...
#define SPARROW_ASSERT_TRUE(expr__)
typename constify< T, is_const >::type constify_t
Definition mp_utils.hpp:390