sparrow 0.9.0
Loading...
Searching...
No Matches
bitset_reference.hpp
Go to the documentation of this file.
1
2// Copyright 2024 Man Group Operations Limited
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#pragma once
17
18namespace sparrow
19{
20 template <class B, bool is_const>
21 class bitset_iterator;
22
31 template <class B>
33 {
34 public:
35
37
38 constexpr bitset_reference(const bitset_reference&) noexcept = default;
39 constexpr bitset_reference(bitset_reference&&) noexcept = default;
40
41 constexpr self_type& operator=(const self_type&) noexcept;
42 constexpr self_type& operator=(self_type&&) noexcept;
43 constexpr self_type& operator=(bool) noexcept;
44
45 constexpr operator bool() const noexcept;
46
47 constexpr bool operator~() const noexcept;
48
49 constexpr self_type& operator&=(bool) noexcept;
50 constexpr self_type& operator|=(bool) noexcept;
51 constexpr self_type& operator^=(bool) noexcept;
52
53 private:
54
55 using block_type = typename B::block_type;
56 using bitset_type = B;
57 using size_type = typename B::size_type;
58
59 bitset_reference(bitset_type& bitset, size_type index);
60
61 constexpr void assign(bool) noexcept;
62 constexpr void set() noexcept;
63 constexpr void reset() noexcept;
64
65 bitset_type* p_bitset;
66 size_type m_index;
67
68 friend class bitset_iterator<B, false>;
69 template <typename RAR>
70 requires std::ranges::random_access_range<std::remove_pointer_t<RAR>>
71 friend class dynamic_bitset_base;
72 };
73
74 template <class B1, class B2>
75 bool operator==(const bitset_reference<B1>& lhs, const bitset_reference<B2>& rhs);
76
77 template <class B>
78 bool operator==(const bitset_reference<B>& lhs, bool rhs);
79
80 template <class B>
81 constexpr auto bitset_reference<B>::operator=(const self_type& rhs) noexcept -> self_type&
82 {
83 assign(rhs);
84 return *this;
85 }
86
87 template <class B>
88 constexpr auto bitset_reference<B>::operator=(self_type&& rhs) noexcept -> self_type&
89 {
90 assign(rhs);
91 return *this;
92 }
93
94 template <class B>
95 constexpr auto bitset_reference<B>::operator=(bool rhs) noexcept -> self_type&
96 {
97 assign(rhs);
98 return *this;
99 }
100
101 template <class B>
102 constexpr bitset_reference<B>::operator bool() const noexcept
103 {
104 return p_bitset->test(m_index);
105 }
106
107 template <class B>
108 constexpr bool bitset_reference<B>::operator~() const noexcept
109 {
110 return !p_bitset->test(m_index);
111 }
112
113 template <class B>
114 constexpr auto bitset_reference<B>::operator&=(bool rhs) noexcept -> self_type&
115 {
116 if (!rhs)
117 {
118 reset();
119 }
120 return *this;
121 }
122
123 template <class B>
124 constexpr auto bitset_reference<B>::operator|=(bool rhs) noexcept -> self_type&
125 {
126 if (rhs)
127 {
128 set();
129 }
130 return *this;
131 }
132
133 template <class B>
134 constexpr auto bitset_reference<B>::operator^=(bool rhs) noexcept -> self_type&
135 {
136 if (rhs)
137 {
138 p_bitset->set(m_index, !p_bitset->test(m_index));
139 }
140 return *this;
141 }
142
143 template <class B>
144 bitset_reference<B>::bitset_reference(bitset_type& bitset, size_type index)
145 : p_bitset(&bitset)
146 , m_index(index)
147 {
148 }
149
150 template <class B>
151 constexpr void bitset_reference<B>::assign(bool rhs) noexcept
152 {
153 rhs ? set() : reset();
154 }
155
156 template <class B>
157 constexpr void bitset_reference<B>::set() noexcept
158 {
159 p_bitset->set(m_index, true);
160 }
161
162 template <class B>
163 constexpr void bitset_reference<B>::reset() noexcept
164 {
165 p_bitset->set(m_index, false);
166 }
167
168 template <class B1, class B2>
170 {
171 return bool(lhs) == bool(rhs);
172 }
173
174 template <class B>
175 bool operator==(const bitset_reference<B>& lhs, bool rhs)
176 {
177 return bool(lhs) == rhs;
178 }
179}
180
181#if defined(__cpp_lib_format)
182# include <format>
183
184template <class B>
185struct std::formatter<sparrow::bitset_reference<B>>
186{
187 constexpr auto parse(std::format_parse_context& ctx)
188 {
189 return ctx.begin();
190 }
191
192 auto format(const sparrow::bitset_reference<B>& b, std::format_context& ctx) const
193 {
194 bool val = b;
195 return std::format_to(ctx.out(), "{}", val);
196 }
197};
198
199#endif
Iterator used to iterate over the bits of a dynamic bitset as if they were addressable values.
Reference proxy used by the bitset_iterator class to make it possible to assign a bit of a bitset as ...
constexpr bitset_reference(const bitset_reference &) noexcept=default
constexpr self_type & operator=(const self_type &) noexcept
constexpr self_type & operator|=(bool) noexcept
bitset_reference< B > self_type
constexpr bitset_reference(bitset_reference &&) noexcept=default
constexpr self_type & operator^=(bool) noexcept
constexpr bool operator~() const noexcept
constexpr self_type & operator&=(bool) noexcept
SPARROW_API bool operator==(const array &lhs, const array &rhs)
Compares the content of two arrays.