sparrow 2.2.1
C++20 idiomatic APIs for the Apache Arrow Columnar Format
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
18
19namespace sparrow
20{
21 template <class B, bool is_const>
22 class bitset_iterator;
23
57 template <class B>
59 {
60 public:
61
63
64 constexpr bitset_reference(const bitset_reference&) noexcept = default;
65 constexpr bitset_reference(bitset_reference&&) noexcept = default;
66
74 constexpr self_type& operator=(const self_type& rhs) noexcept;
75
84 constexpr self_type& operator=(self_type&& rhs) noexcept;
85
93 constexpr self_type& operator=(bool value) noexcept;
94
100 constexpr operator bool() const noexcept;
101
108 constexpr bool operator~() const noexcept;
109
118 constexpr self_type& operator&=(bool rhs) noexcept;
119
128 constexpr self_type& operator|=(bool rhs) noexcept;
129
138 constexpr self_type& operator^=(bool rhs) noexcept;
139
140 private:
141
142 using block_type = typename B::block_type;
143 using bitset_type = B;
144 using size_type = typename B::size_type;
145
154 bitset_reference(bitset_type& bitset, size_type index);
155
161 constexpr void assign(bool value) noexcept;
162
167 constexpr void set() noexcept;
168
173 constexpr void reset() noexcept;
174
175 bitset_type* p_bitset;
176 size_type m_index;
177
178 friend class bitset_iterator<B, false>;
179 template <typename RAR, null_count_policy NCP>
180 requires std::ranges::random_access_range<std::remove_pointer_t<RAR>>
181 friend class dynamic_bitset_base;
182 };
183
193 template <class B1, class B2>
194 bool operator==(const bitset_reference<B1>& lhs, const bitset_reference<B2>& rhs);
195
204 template <class B>
205 bool operator==(const bitset_reference<B>& lhs, bool rhs);
206
207 template <class B>
208 constexpr auto bitset_reference<B>::operator=(const self_type& rhs) noexcept -> self_type&
209 {
210 assign(rhs);
211 return *this;
212 }
213
214 template <class B>
215 constexpr auto bitset_reference<B>::operator=(self_type&& rhs) noexcept -> self_type&
216 {
217 assign(rhs);
218 return *this;
219 }
220
221 template <class B>
222 constexpr auto bitset_reference<B>::operator=(bool rhs) noexcept -> self_type&
223 {
224 assign(rhs);
225 return *this;
226 }
227
228 template <class B>
229 constexpr bitset_reference<B>::operator bool() const noexcept
230 {
231 return p_bitset->test(m_index);
232 }
233
234 template <class B>
235 constexpr bool bitset_reference<B>::operator~() const noexcept
236 {
237 return !p_bitset->test(m_index);
238 }
239
240 template <class B>
241 constexpr auto bitset_reference<B>::operator&=(bool rhs) noexcept -> self_type&
242 {
243 if (!rhs)
244 {
245 reset();
246 }
247 return *this;
248 }
249
250 template <class B>
251 constexpr auto bitset_reference<B>::operator|=(bool rhs) noexcept -> self_type&
252 {
253 if (rhs)
254 {
255 set();
256 }
257 return *this;
258 }
259
260 template <class B>
261 constexpr auto bitset_reference<B>::operator^=(bool rhs) noexcept -> self_type&
262 {
263 if (rhs)
264 {
265 p_bitset->set(m_index, !p_bitset->test(m_index));
266 }
267 return *this;
268 }
269
270 template <class B>
271 bitset_reference<B>::bitset_reference(bitset_type& bitset, size_type index)
272 : p_bitset(&bitset)
273 , m_index(index)
274 {
275 }
276
277 template <class B>
278 constexpr void bitset_reference<B>::assign(bool rhs) noexcept
279 {
280 rhs ? set() : reset();
281 }
282
283 template <class B>
284 constexpr void bitset_reference<B>::set() noexcept
285 {
286 p_bitset->set(m_index, true);
287 }
288
289 template <class B>
290 constexpr void bitset_reference<B>::reset() noexcept
291 {
292 p_bitset->set(m_index, false);
293 }
294
295 template <class B1, class B2>
297 {
298 return bool(lhs) == bool(rhs);
299 }
300
301 template <class B>
302 bool operator==(const bitset_reference<B>& lhs, bool rhs)
303 {
304 return bool(lhs) == rhs;
305 }
306}
307
308#if defined(__cpp_lib_format)
309# include <format>
310
311template <class B>
312struct std::formatter<sparrow::bitset_reference<B>>
313{
314 constexpr auto parse(std::format_parse_context& ctx)
315 {
316 return ctx.begin();
317 }
318
319 auto format(const sparrow::bitset_reference<B>& b, std::format_context& ctx) const
320 {
321 bool val = b;
322 return std::format_to(ctx.out(), "{}", val);
323 }
324};
325
326#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.
Concept that checks if a type is a valid null count policy.
SPARROW_API bool operator==(const array &lhs, const array &rhs)
Compares the content of two arrays.
Extensions to the C++ standard library.