OpenTTD Source 20250220-master-gf89924a727
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 <http://www.gnu.org/licenses/>.
6 */
7
13#ifndef BASE_BITSET_TYPE_HPP
14#define BASE_BITSET_TYPE_HPP
15
16#include "bitmath_func.hpp"
17
24template <typename Timpl, typename Tvalue_type, typename Tstorage, Tstorage Tmask = std::numeric_limits<Tstorage>::max()>
26public:
27 using ValueType = Tvalue_type;
28 using BaseType = Tstorage;
29 static constexpr Tstorage MASK = Tmask;
30
31 constexpr BaseBitSet() : data(0) {}
32 explicit constexpr BaseBitSet(Tstorage data) : data(data & Tmask) {}
33
34 constexpr auto operator <=>(const BaseBitSet &) const noexcept = default;
35
40 inline constexpr Timpl &Set()
41 {
42 this->data = Tmask;
43 return static_cast<Timpl&>(*this);
44 }
45
51 inline constexpr Timpl &Set(Tvalue_type value)
52 {
53 this->data |= (1ULL << Timpl::DecayValueType(value));
54 return static_cast<Timpl&>(*this);
55 }
56
62 inline constexpr Timpl &Set(const Timpl &other)
63 {
64 this->data |= other.data;
65 return static_cast<Timpl&>(*this);
66 }
67
74 inline constexpr Timpl &Set(Tvalue_type value, bool set)
75 {
76 return set ? this->Set(value) : this->Reset(value);
77 }
78
84 inline constexpr Timpl &Reset(Tvalue_type value)
85 {
86 this->data &= ~(1ULL << Timpl::DecayValueType(value));
87 return static_cast<Timpl&>(*this);
88 }
89
95 inline constexpr Timpl &Reset(const Timpl &other)
96 {
97 this->data &= ~other.data;
98 return static_cast<Timpl&>(*this);
99 }
100
106 inline constexpr Timpl &Flip(Tvalue_type value)
107 {
108 if (this->Test(value)) {
109 return this->Reset(value);
110 } else {
111 return this->Set(value);
112 }
113 }
114
120 inline constexpr Timpl &Flip(const Timpl &other)
121 {
122 this->data ^= other.data;
123 return static_cast<Timpl&>(*this);
124 }
125
131 inline constexpr bool Test(Tvalue_type value) const
132 {
133 return (this->data & (1ULL << Timpl::DecayValueType(value))) != 0;
134 }
135
141 inline constexpr bool All(const Timpl &other) const
142 {
143 return (this->data & other.data) == other.data;
144 }
145
150 inline constexpr bool All() const
151 {
152 return this->data == Tmask;
153 }
154
160 inline constexpr bool Any(const Timpl &other) const
161 {
162 return (this->data & other.data) != 0;
163 }
164
169 inline constexpr bool Any() const
170 {
171 return this->data != 0;
172 }
173
178 inline constexpr bool None() const
179 {
180 return this->data == 0;
181 }
182
183 inline constexpr Timpl operator |(const Timpl &other) const
184 {
185 return Timpl{static_cast<Tstorage>(this->data | other.data)};
186 }
187
188 inline constexpr Timpl operator &(const Timpl &other) const
189 {
190 return Timpl{static_cast<Tstorage>(this->data & other.data)};
191 }
192
197 inline constexpr Tstorage base() const noexcept
198 {
199 return this->data;
200 }
201
206 inline constexpr bool IsValid() const
207 {
208 return (this->base() & Tmask) == this->base();
209 }
210
211 auto begin() const { return SetBitIterator<Tvalue_type>(this->data).begin(); }
212 auto end() const { return SetBitIterator<Tvalue_type>(this->data).end(); }
213
214private:
215 Tstorage data;
216};
217
218#endif /* BASE_BITSET_TYPE_HPP */
Functions related to bit mathematics.
Base for bit set wrapper.
constexpr Timpl & Set(Tvalue_type value, bool set)
Assign the value-th bit.
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.
Tstorage data
Bitmask of values.
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
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.
Tvalue_type ValueType
Value type of this BaseBitSet.
constexpr Timpl & Set()
Set all bits.
Tstorage BaseType
Storage type of this BaseBitSet, be ConvertibleThroughBase.
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.
Iterable ensemble of each set bit in a value.