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() noexcept;
65 constexpr void decrement() noexcept;
66 constexpr void advance(difference_type n) noexcept;
67 [[nodiscard]] constexpr difference_type
68 distance_to(const self_type& rhs) const noexcept(!SPARROW_CONTRACTS_THROW_ON_FAILURE);
69 [[nodiscard]] constexpr bool
70 equal(const self_type& rhs) const noexcept(!SPARROW_CONTRACTS_THROW_ON_FAILURE);
71 [[nodiscard]] constexpr bool
72 less_than(const self_type& rhs) const noexcept(!SPARROW_CONTRACTS_THROW_ON_FAILURE);
73
74 [[nodiscard]] static constexpr difference_type as_signed(size_type i) noexcept;
75 [[nodiscard]] static constexpr size_type as_unsigned(difference_type i) noexcept;
76
77 bitset_type* p_bitset = nullptr;
78 size_type m_index;
79
80 friend class iterator_access;
81 };
82
83 template <class B, bool is_const>
84 constexpr bitset_iterator<B, is_const>::bitset_iterator(bitset_type* bitset, size_type index)
85 : p_bitset(bitset)
86 , m_index(index)
87 {
88 }
89
90 template <class B, bool is_const>
91 constexpr auto bitset_iterator<B, is_const>::dereference() const noexcept -> reference
92 {
93 if constexpr (is_const)
94 {
95 if (p_bitset->data() == nullptr)
96 {
97 return true;
98 }
99 return p_bitset->test(m_index);
100 }
101 else
102 {
103 return bitset_reference<B>(*p_bitset, m_index);
104 }
105 }
106
107 template <class B, bool is_const>
108 constexpr void bitset_iterator<B, is_const>::increment() noexcept
109 {
110 ++m_index;
111 }
112
113 template <class B, bool is_const>
114 constexpr void bitset_iterator<B, is_const>::decrement() noexcept
115 {
116 --m_index;
117 }
118
119 template <class B, bool is_const>
121 {
122 if (n < 0 && static_cast<size_type>(-n) > m_index)
123 {
124 // Prevent underflow by clamping m_index to 0
125 m_index = 0;
126 }
127 else
128 {
129 m_index = as_unsigned(as_signed(m_index) + n);
130 }
131 }
132
133 template <class B, bool is_const>
134 constexpr auto bitset_iterator<B, is_const>::distance_to(const self_type& rhs) const noexcept(
136 ) -> difference_type
137 {
138 SPARROW_ASSERT_TRUE(p_bitset == rhs.p_bitset);
139 return as_signed(rhs.m_index) - as_signed(m_index);
140 }
141
142 template <class B, bool is_const>
143 constexpr bool
145 {
146 SPARROW_ASSERT_TRUE(p_bitset == rhs.p_bitset);
147 return m_index == rhs.m_index;
148 }
149
150 template <class B, bool is_const>
151 constexpr bool
153 {
154 SPARROW_ASSERT_TRUE(p_bitset == rhs.p_bitset);
155 return m_index < rhs.m_index;
156 }
157
158 template <class B, bool is_const>
160 {
161 return static_cast<difference_type>(i);
162 }
163
164 template <class B, bool is_const>
166 {
167 return static_cast<size_type>(i);
168 }
169}
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__)
#define SPARROW_CONTRACTS_THROW_ON_FAILURE
Definition contracts.hpp:24
typename constify< T, is_const >::type constify_t
Definition mp_utils.hpp:457