10#ifndef BASE_BITSET_TYPE_HPP
11#define BASE_BITSET_TYPE_HPP
21template <typename Timpl, typename Tvalue_type, typename Tstorage, Tstorage Tmask = std::numeric_limits<Tstorage>::max()>
26 static constexpr Tstorage
MASK = Tmask;
37 constexpr auto operator <=>(
const BaseBitSet &)
const noexcept =
default;
43 inline constexpr Timpl &
Set()
46 return static_cast<Timpl&
>(*this);
54 inline constexpr Timpl &
Set(Tvalue_type value)
56 this->
data |= (1ULL << Timpl::DecayValueType(value));
57 return static_cast<Timpl&
>(*this);
65 inline constexpr Timpl &
Set(
const Timpl &other)
67 this->
data |= other.data;
68 return static_cast<Timpl&
>(*this);
77 inline constexpr Timpl &
Set(Tvalue_type value,
bool set)
79 return set ? this->
Set(value) : this->
Reset(value);
89 return static_cast<Timpl &
>(*this);
97 inline constexpr Timpl &
Reset(Tvalue_type value)
99 this->
data &= ~(1ULL << Timpl::DecayValueType(value));
100 return static_cast<Timpl&
>(*this);
108 inline constexpr Timpl &
Reset(
const Timpl &other)
110 this->
data &= ~other.data;
111 return static_cast<Timpl&
>(*this);
121 return static_cast<Timpl &
>(*this);
129 inline constexpr Timpl &
Flip(Tvalue_type value)
131 if (this->
Test(value)) {
132 return this->
Reset(value);
134 return this->
Set(value);
143 inline constexpr Timpl &
Flip(
const Timpl &other)
145 this->
data ^= other.data;
146 return static_cast<Timpl&
>(*this);
154 inline constexpr bool Test(Tvalue_type value)
const
156 return (this->
data & (1ULL << Timpl::DecayValueType(value))) != 0;
164 inline constexpr bool All(
const Timpl &other)
const
166 return (this->
data & other.data) == other.data;
173 inline constexpr bool All()
const
175 return this->
data == Tmask;
183 inline constexpr bool Any(
const Timpl &other)
const
185 return (this->
data & other.data) != 0;
192 inline constexpr bool Any()
const
194 return this->
data != 0;
201 inline constexpr bool None()
const
203 return this->
data == 0;
206 inline constexpr Timpl &operator|=(
const Timpl &other)
208 this->data |= other.data;
209 return static_cast<Timpl &
>(*this);
212 inline constexpr Timpl operator|(
const Timpl &other)
const
214 return Timpl{
static_cast<Tstorage
>(this->data | other.data)};
217 inline constexpr Timpl &operator&=(
const Timpl &other)
219 this->data &= other.data;
220 return static_cast<Timpl &
>(*this);
223 inline constexpr Timpl operator&(
const Timpl &other)
const
225 return Timpl{
static_cast<Tstorage
>(this->data & other.data)};
232 inline constexpr Tstorage
base() const noexcept
243 return (this->
base() & Tmask) == this->
base();
262 for (
auto i : *
this) {
263 if (n == 0)
return i;
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 bool Test(Tenum value) const
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 EnumBitSet< Tenum, Tstorage, Tend_value > & Reset()
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.
static constexpr Tstorage MASK
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.