sparrow 0.3.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
58 bitset_reference(bitset_type& bitset, block_type& block, block_type mask);
59
60 constexpr void assign(bool) noexcept;
61 constexpr void set() noexcept;
62 constexpr void reset() noexcept;
63
64 bitset_type& m_bitset;
65 block_type& m_block;
66 block_type m_mask;
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 if (m_bitset.data() == nullptr)
105 {
106 return true;
107 }
108 return (m_block & m_mask) != 0;
109 }
110
111 template <class B>
112 constexpr bool bitset_reference<B>::operator~() const noexcept
113 {
114 return (m_block & m_mask) == 0;
115 }
116
117 template <class B>
118 constexpr auto bitset_reference<B>::operator&=(bool rhs) noexcept -> self_type&
119 {
120 if (!rhs)
121 {
122 reset();
123 }
124 return *this;
125 }
126
127 template <class B>
128 constexpr auto bitset_reference<B>::operator|=(bool rhs) noexcept -> self_type&
129 {
130 if (rhs)
131 {
132 set();
133 }
134 return *this;
135 }
136
137 template <class B>
138 constexpr auto bitset_reference<B>::operator^=(bool rhs) noexcept -> self_type&
139 {
140 if (rhs)
141 {
142 bool old_value = m_block & m_mask;
143 m_block ^= m_mask;
144 m_bitset.update_null_count(old_value, !old_value);
145 }
146 return *this;
147 }
148
149 template <class B>
150 bitset_reference<B>::bitset_reference(bitset_type& bitset, block_type& block, block_type mask)
151 : m_bitset(bitset)
152 , m_block(block)
153 , m_mask(mask)
154 {
155 }
156
157 template <class B>
158 constexpr void bitset_reference<B>::assign(bool rhs) noexcept
159 {
160 rhs ? set() : reset();
161 }
162
163 template <class B>
164 constexpr void bitset_reference<B>::set() noexcept
165 {
166 bool old_value = m_block & m_mask;
167 m_block |= m_mask;
168 m_bitset.update_null_count(old_value, m_block & m_mask);
169 }
170
171 template <class B>
172 constexpr void bitset_reference<B>::reset() noexcept
173 {
174 bool old_value = m_block & m_mask;
175 m_block &= ~m_mask;
176 m_bitset.update_null_count(old_value, m_block & m_mask);
177 }
178
179 template <class B1, class B2>
181 {
182 return bool(lhs) == bool(rhs);
183 }
184
185 template <class B>
186 bool operator==(const bitset_reference<B>& lhs, bool rhs)
187 {
188 return bool(lhs) == rhs;
189 }
190}
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.