OpenTTD Source 20260721-master-g25ec12c62d
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
55 template <typename Treturn_type = Timpl> requires std::is_base_of_v<BaseBitSet<Timpl, Tvalue_type, Tstorage, Tmask>, Treturn_type>
56 inline constexpr Treturn_type &Set(Tvalue_type value)
57 {
58 this->data |= (1ULL << Timpl::DecayValueType(value));
59 return static_cast<Treturn_type &>(*this);
60 }
61
67 inline constexpr Timpl &Set(const Timpl &other)
68 {
69 this->data |= other.data;
70 return static_cast<Timpl&>(*this);
71 }
72
79 inline constexpr Timpl &Set(Tvalue_type value, bool set)
80 {
81 return set ? this->Set(value) : this->Reset(value);
82 }
83
88 inline constexpr Timpl &Reset()
89 {
90 this->data = 0;
91 return static_cast<Timpl &>(*this);
92 }
93
99 inline constexpr Timpl &Reset(Tvalue_type value)
100 {
101 this->data &= ~(1ULL << Timpl::DecayValueType(value));
102 return static_cast<Timpl&>(*this);
103 }
104
110 inline constexpr Timpl &Reset(const Timpl &other)
111 {
112 this->data &= ~other.data;
113 return static_cast<Timpl&>(*this);
114 }
115
120 inline constexpr Timpl &Flip()
121 {
122 this->data ^= Tmask;
123 return static_cast<Timpl &>(*this);
124 }
125
131 inline constexpr Timpl &Flip(Tvalue_type value)
132 {
133 if (this->Test(value)) {
134 return this->Reset(value);
135 } else {
136 return this->Set(value);
137 }
138 }
139
145 inline constexpr Timpl &Flip(const Timpl &other)
146 {
147 this->data ^= other.data;
148 return static_cast<Timpl&>(*this);
149 }
150
156 inline constexpr bool Test(Tvalue_type value) const
157 {
158 return (this->data & (1ULL << Timpl::DecayValueType(value))) != 0;
159 }
160
166 inline constexpr bool All(const Timpl &other) const
167 {
168 return (this->data & other.data) == other.data;
169 }
170
175 inline constexpr bool All() const
176 {
177 return this->data == Tmask;
178 }
179
185 inline constexpr bool Any(const Timpl &other) const
186 {
187 return (this->data & other.data) != 0;
188 }
189
194 inline constexpr bool Any() const
195 {
196 return this->data != 0;
197 }
198
203 inline constexpr bool None() const
204 {
205 return this->data == 0;
206 }
207
208 inline constexpr Timpl &operator|=(const Timpl &other)
209 {
210 this->data |= other.data;
211 return static_cast<Timpl &>(*this);
212 }
213
214 inline constexpr Timpl operator|(const Timpl &other) const
215 {
216 return Timpl{static_cast<Tstorage>(this->data | other.data)};
217 }
218
219 inline constexpr Timpl &operator&=(const Timpl &other)
220 {
221 this->data &= other.data;
222 return static_cast<Timpl &>(*this);
223 }
224
225 inline constexpr Timpl operator&(const Timpl &other) const
226 {
227 return Timpl{static_cast<Tstorage>(this->data & other.data)};
228 }
229
234 inline constexpr Tstorage base() const noexcept
235 {
236 return this->data;
237 }
238
243 inline constexpr bool IsValid() const
244 {
245 return (this->base() & Tmask) == this->base();
246 }
247
252 inline uint Count() const
253 {
254 return CountBits(this->base());
255 }
256
262 std::optional<Tvalue_type> GetNthSetBit(uint n) const
263 {
264 for (auto i : *this) {
265 if (n == 0) return i;
266 --n;
267 }
268
269 return std::nullopt;
270 }
271
276 auto begin() const { return SetBitIterator<Tvalue_type, Tstorage>(this->data).begin(); }
277
282 auto end() const { return SetBitIterator<Tvalue_type, Tstorage>(this->data).end(); }
283
284private:
285 Tstorage data;
286};
287
288#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 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.
constexpr Treturn_type & Set(Tvalue_type value)
Set the value-th bit.
std::optional< Tvalue_type > GetNthSetBit(uint n) const
Get the value of the Nth set bit.
constexpr VarType operator|(VarFileType file, VarMemType mem)
Transitional helper function to combine a file and memory storage configuration.
Definition saveload.h:718
Iterable ensemble of each set bit in a value.