OpenTTD Source 20260512-master-g20b387b91f
base_bitset_type.hpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef BASE_BITSET_TYPE_HPP
11#define BASE_BITSET_TYPE_HPP
12
13#include "bitmath_func.hpp"
14
21template <typename Timpl, typename Tvalue_type, typename Tstorage, Tstorage Tmask = std::numeric_limits<Tstorage>::max()>
23public:
24 using ValueType = Tvalue_type;
25 using BaseType = Tstorage;
26 static constexpr Tstorage MASK = Tmask;
27
29 constexpr BaseBitSet() : data(0) {}
30
35 explicit constexpr BaseBitSet(Tstorage data) : data(data & Tmask) {}
36
37 constexpr auto operator <=>(const BaseBitSet &) const noexcept = default;
38
43 inline constexpr Timpl &Set()
44 {
45 this->data = Tmask;
46 return static_cast<Timpl&>(*this);
47 }
48
54 inline constexpr Timpl &Set(Tvalue_type value)
55 {
56 this->data |= (1ULL << Timpl::DecayValueType(value));
57 return static_cast<Timpl&>(*this);
58 }
59
65 inline constexpr Timpl &Set(const Timpl &other)
66 {
67 this->data |= other.data;
68 return static_cast<Timpl&>(*this);
69 }
70
77 inline constexpr Timpl &Set(Tvalue_type value, bool set)
78 {
79 return set ? this->Set(value) : this->Reset(value);
80 }
81
86 inline constexpr Timpl &Reset()
87 {
88 this->data = 0;
89 return static_cast<Timpl &>(*this);
90 }
91
97 inline constexpr Timpl &Reset(Tvalue_type value)
98 {
99 this->data &= ~(1ULL << Timpl::DecayValueType(value));
100 return static_cast<Timpl&>(*this);
101 }
102
108 inline constexpr Timpl &Reset(const Timpl &other)
109 {
110 this->data &= ~other.data;
111 return static_cast<Timpl&>(*this);
112 }
113
118 inline constexpr Timpl &Flip()
119 {
120 this->data ^= Tmask;
121 return static_cast<Timpl &>(*this);
122 }
123
129 inline constexpr Timpl &Flip(Tvalue_type value)
130 {
131 if (this->Test(value)) {
132 return this->Reset(value);
133 } else {
134 return this->Set(value);
135 }
136 }
137
143 inline constexpr Timpl &Flip(const Timpl &other)
144 {
145 this->data ^= other.data;
146 return static_cast<Timpl&>(*this);
147 }
148
154 inline constexpr bool Test(Tvalue_type value) const
155 {
156 return (this->data & (1ULL << Timpl::DecayValueType(value))) != 0;
157 }
158
164 inline constexpr bool All(const Timpl &other) const
165 {
166 return (this->data & other.data) == other.data;
167 }
168
173 inline constexpr bool All() const
174 {
175 return this->data == Tmask;
176 }
177
183 inline constexpr bool Any(const Timpl &other) const
184 {
185 return (this->data & other.data) != 0;
186 }
187
192 inline constexpr bool Any() const
193 {
194 return this->data != 0;
195 }
196
201 inline constexpr bool None() const
202 {
203 return this->data == 0;
204 }
205
206 inline constexpr Timpl &operator|=(const Timpl &other)
207 {
208 this->data |= other.data;
209 return static_cast<Timpl &>(*this);
210 }
211
212 inline constexpr Timpl operator|(const Timpl &other) const
213 {
214 return Timpl{static_cast<Tstorage>(this->data | other.data)};
215 }
216
217 inline constexpr Timpl &operator&=(const Timpl &other)
218 {
219 this->data &= other.data;
220 return static_cast<Timpl &>(*this);
221 }
222
223 inline constexpr Timpl operator&(const Timpl &other) const
224 {
225 return Timpl{static_cast<Tstorage>(this->data & other.data)};
226 }
227
232 inline constexpr Tstorage base() const noexcept
233 {
234 return this->data;
235 }
236
241 inline constexpr bool IsValid() const
242 {
243 return (this->base() & Tmask) == this->base();
244 }
245
250 inline uint Count() const
251 {
252 return CountBits(this->base());
253 }
254
260 std::optional<Tvalue_type> GetNthSetBit(uint n) const
261 {
262 for (auto i : *this) {
263 if (n == 0) return i;
264 --n;
265 }
266
267 return std::nullopt;
268 }
269
274 auto begin() const { return SetBitIterator<Tvalue_type, Tstorage>(this->data).begin(); }
275
280 auto end() const { return SetBitIterator<Tvalue_type, Tstorage>(this->data).end(); }
281
282private:
283 Tstorage data;
284};
285
286#endif /* BASE_BITSET_TYPE_HPP */
Functions related to bit mathematics.
constexpr uint CountBits(T value)
Counts the number of set bits in a variable.
Base for bit set wrapper.
uint Count() const
Count the number of set bits.
constexpr Timpl & Set(Tvalue_type value, bool set)
Assign the value-th bit.
auto end() const
Returns an iterator to the end of the set bits.
constexpr bool All(const Timpl &other) const
Test if all of the values are set.
constexpr Timpl & Flip(const Timpl &other)
Flip values from another bitset.
constexpr Tstorage base() const noexcept
Retrieve the raw value behind this bit set.
constexpr bool None() const
Test if none of the values are set.
constexpr Timpl & Flip()
Flip all bits.
constexpr bool Any() const
Test if any of the values are set.
constexpr Timpl & Set(const Timpl &other)
Set values from another bitset.
constexpr bool IsValid() const
Test that the raw value of this bit set is valid.
constexpr Timpl & Reset(const Timpl &other)
Reset values from another bitset.
constexpr Timpl & Flip(Tvalue_type value)
Flip the value-th bit.
constexpr BaseBitSet()
Create an empty bitset.
Tvalue_type ValueType
Value type of this BaseBitSet.
constexpr Timpl & Set()
Set all bits.
Tstorage BaseType
Storage type of this BaseBitSet, be ConvertibleThroughBase.
auto begin() const
Returns an iterator to begin of the set bits.
constexpr bool Any(const Timpl &other) const
Test if any of the given values are set.
constexpr Timpl & Set(Tvalue_type value)
Set the value-th bit.
constexpr bool All() const
Test if all of the values are set.
constexpr Timpl & Reset(Tvalue_type value)
Reset the value-th bit.
constexpr BaseBitSet(Tstorage data)
Create a bitset with a given bits that are within the mask of valid values.
std::optional< Tvalue_type > GetNthSetBit(uint n) const
Get the value of the Nth set bit.
Iterable ensemble of each set bit in a value.