sparrow 0.9.0
Loading...
Searching...
No Matches
bitset_reference.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
17namespace sparrow
18{
19 template <class B, bool is_const>
20 class bitset_iterator;
21
55 template <class B>
57 {
58 public:
59
61
62 constexpr bitset_reference(const bitset_reference&) noexcept = default;
63 constexpr bitset_reference(bitset_reference&&) noexcept = default;
64
72 constexpr self_type& operator=(const self_type& rhs) noexcept;
73
82 constexpr self_type& operator=(self_type&& rhs) noexcept;
83
91 constexpr self_type& operator=(bool value) noexcept;
92
98 constexpr operator bool() const noexcept;
99
106 constexpr bool operator~() const noexcept;
107
116 constexpr self_type& operator&=(bool rhs) noexcept;
117
126 constexpr self_type& operator|=(bool rhs) noexcept;
127
136 constexpr self_type& operator^=(bool rhs) noexcept;
137
138 private:
139
140 using block_type = typename B::block_type;
141 using bitset_type = B;
142 using size_type = typename B::size_type;
143
152 bitset_reference(bitset_type& bitset, size_type index);
153
159 constexpr void assign(bool value) noexcept;
160
165 constexpr void set() noexcept;
166
171 constexpr void reset() noexcept;
172
173 bitset_type* p_bitset;
174 size_type m_index;
175
176 friend class bitset_iterator<B, false>;
177 template <typename RAR>
178 requires std::ranges::random_access_range<std::remove_pointer_t<RAR>>
179 friend class dynamic_bitset_base;
180 };
181
191 template <class B1, class B2>
192 bool operator==(const bitset_reference<B1>& lhs, const bitset_reference<B2>& rhs);
193
202 template <class B>
203 bool operator==(const bitset_reference<B>& lhs, bool rhs);
204
205 template <class B>
206 constexpr auto bitset_reference<B>::operator=(const self_type& rhs) noexcept -> self_type&
207 {
208 assign(rhs);
209 return *this;
210 }
211
212 template <class B>
213 constexpr auto bitset_reference<B>::operator=(self_type&& rhs) noexcept -> self_type&
214 {
215 assign(rhs);
216 return *this;
217 }
218
219 template <class B>
220 constexpr auto bitset_reference<B>::operator=(bool rhs) noexcept -> self_type&
221 {
222 assign(rhs);
223 return *this;
224 }
225
226 template <class B>
227 constexpr bitset_reference<B>::operator bool() const noexcept
228 {
229 return p_bitset->test(m_index);
230 }
231
232 template <class B>
233 constexpr bool bitset_reference<B>::operator~() const noexcept
234 {
235 return !p_bitset->test(m_index);
236 }
237
238 template <class B>
239 constexpr auto bitset_reference<B>::operator&=(bool rhs) noexcept -> self_type&
240 {
241 if (!rhs)
242 {
243 reset();
244 }
245 return *this;
246 }
247
248 template <class B>
249 constexpr auto bitset_reference<B>::operator|=(bool rhs) noexcept -> self_type&
250 {
251 if (rhs)
252 {
253 set();
254 }
255 return *this;
256 }
257
258 template <class B>
259 constexpr auto bitset_reference<B>::operator^=(bool rhs) noexcept -> self_type&
260 {
261 if (rhs)
262 {
263 p_bitset->set(m_index, !p_bitset->test(m_index));
264 }
265 return *this;
266 }
267
268 template <class B>
269 bitset_reference<B>::bitset_reference(bitset_type& bitset, size_type index)
270 : p_bitset(&bitset)
271 , m_index(index)
272 {
273 }
274
275 template <class B>
276 constexpr void bitset_reference<B>::assign(bool rhs) noexcept
277 {
278 rhs ? set() : reset();
279 }
280
281 template <class B>
282 constexpr void bitset_reference<B>::set() noexcept
283 {
284 p_bitset->set(m_index, true);
285 }
286
287 template <class B>
288 constexpr void bitset_reference<B>::reset() noexcept
289 {
290 p_bitset->set(m_index, false);
291 }
292
293 template <class B1, class B2>
295 {
296 return bool(lhs) == bool(rhs);
297 }
298
299 template <class B>
300 bool operator==(const bitset_reference<B>& lhs, bool rhs)
301 {
302 return bool(lhs) == rhs;
303 }
304}
305
306#if defined(__cpp_lib_format)
307# include <format>
308
309template <class B>
310struct std::formatter<sparrow::bitset_reference<B>>
311{
312 constexpr auto parse(std::format_parse_context& ctx)
313 {
314 return ctx.begin();
315 }
316
317 auto format(const sparrow::bitset_reference<B>& b, std::format_context& ctx) const
318 {
319 bool val = b;
320 return std::format_to(ctx.out(), "{}", val);
321 }
322};
323
324#endif
Iterator used to iterate over the bits of a dynamic bitset as if they were addressable values.
A proxy reference class that provides mutable access to individual bits in a bitset.
constexpr bitset_reference(const bitset_reference &) noexcept=default
constexpr self_type & operator^=(bool rhs) noexcept
Bitwise XOR assignment.
bitset_reference< B > self_type
This class type for convenience.
constexpr self_type & operator|=(bool rhs) noexcept
Bitwise OR assignment.
constexpr self_type & operator&=(bool rhs) noexcept
Bitwise AND assignment.
constexpr self_type & operator=(const self_type &rhs) noexcept
Copy assignment from another bitset_reference.
constexpr bitset_reference(bitset_reference &&) noexcept=default
constexpr bool operator~() const noexcept
Bitwise NOT operator.
SPARROW_API bool operator==(const array &lhs, const array &rhs)
Compares the content of two arrays.