OpenTTD Source 20260218-master-g2123fca5ea
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
119 inline constexpr Timpl &Flip(Tvalue_type value)
120 {
121 if (this->Test(value)) {
122 return this->Reset(value);
123 } else {
124 return this->Set(value);
125 }
126 }
127
133 inline constexpr Timpl &Flip(const Timpl &other)
134 {
135 this->data ^= other.data;
136 return static_cast<Timpl&>(*this);
137 }
138
144 inline constexpr bool Test(Tvalue_type value) const
145 {
146 return (this->data & (1ULL << Timpl::DecayValueType(value))) != 0;
147 }
148
154 inline constexpr bool All(const Timpl &other) const
155 {
156 return (this->data & other.data) == other.data;
157 }
158
163 inline constexpr bool All() const
164 {
165 return this->data == Tmask;
166 }
167
173 inline constexpr bool Any(const Timpl &other) const
174 {
175 return (this->data & other.data) != 0;
176 }
177
182 inline constexpr bool Any() const
183 {
184 return this->data != 0;
185 }
186
191 inline constexpr bool None() const
192 {
193 return this->data == 0;
194 }
195
196 inline constexpr Timpl &operator|=(const Timpl &other)
197 {
198 this->data |= other.data;
199 return static_cast<Timpl &>(*this);
200 }
201
202 inline constexpr Timpl operator|(const Timpl &other) const
203 {
204 return Timpl{static_cast<Tstorage>(this->data | other.data)};
205 }
206
207 inline constexpr Timpl &operator&=(const Timpl &other)
208 {
209 this->data &= other.data;
210 return static_cast<Timpl &>(*this);
211 }
212
213 inline constexpr Timpl operator&(const Timpl &other) const
214 {
215 return Timpl{static_cast<Tstorage>(this->data & other.data)};
216 }
217
222 inline constexpr Tstorage base() const noexcept
223 {
224 return this->data;
225 }
226
231 inline constexpr bool IsValid() const
232 {
233 return (this->base() & Tmask) == this->base();
234 }
235
240 inline uint Count() const
241 {
242 return CountBits(this->base());
243 }
244
250 std::optional<Tvalue_type> GetNthSetBit(uint n) const
251 {
252 for (auto i : *this) {
253 if (n == 0) return i;
254 --n;
255 }
256
257 return std::nullopt;
258 }
259
264 auto begin() const { return SetBitIterator<Tvalue_type, Tstorage>(this->data).begin(); }
265
270 auto end() const { return SetBitIterator<Tvalue_type, Tstorage>(this->data).end(); }
271
272private:
273 Tstorage data;
274};
275
276#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 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.