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;
28 constexpr BaseBitSet() :
data(0) {}
29 explicit constexpr BaseBitSet(Tstorage data) : data(data & Tmask) {}
31 constexpr auto operator <=>(
const BaseBitSet &)
const noexcept =
default;
37 inline constexpr Timpl &
Set()
40 return static_cast<Timpl&
>(*this);
48 inline constexpr Timpl &
Set(Tvalue_type value)
50 this->
data |= (1ULL << Timpl::DecayValueType(value));
51 return static_cast<Timpl&
>(*this);
59 inline constexpr Timpl &
Set(
const Timpl &other)
61 this->
data |= other.data;
62 return static_cast<Timpl&
>(*this);
71 inline constexpr Timpl &
Set(Tvalue_type value,
bool set)
73 return set ? this->
Set(value) : this->
Reset(value);
83 return static_cast<Timpl &
>(*this);
91 inline constexpr Timpl &
Reset(Tvalue_type value)
93 this->
data &= ~(1ULL << Timpl::DecayValueType(value));
94 return static_cast<Timpl&
>(*this);
102 inline constexpr Timpl &
Reset(
const Timpl &other)
104 this->
data &= ~other.data;
105 return static_cast<Timpl&
>(*this);
113 inline constexpr Timpl &
Flip(Tvalue_type value)
115 if (this->
Test(value)) {
116 return this->
Reset(value);
118 return this->
Set(value);
127 inline constexpr Timpl &
Flip(
const Timpl &other)
129 this->
data ^= other.data;
130 return static_cast<Timpl&
>(*this);
138 inline constexpr bool Test(Tvalue_type value)
const
140 return (this->
data & (1ULL << Timpl::DecayValueType(value))) != 0;
148 inline constexpr bool All(
const Timpl &other)
const
150 return (this->
data & other.data) == other.data;
157 inline constexpr bool All()
const
159 return this->
data == Tmask;
167 inline constexpr bool Any(
const Timpl &other)
const
169 return (this->
data & other.data) != 0;
176 inline constexpr bool Any()
const
178 return this->
data != 0;
185 inline constexpr bool None()
const
187 return this->
data == 0;
190 inline constexpr Timpl &operator|=(
const Timpl &other)
192 this->data |= other.data;
193 return static_cast<Timpl &
>(*this);
196 inline constexpr Timpl operator|(
const Timpl &other)
const
198 return Timpl{
static_cast<Tstorage
>(this->data | other.data)};
201 inline constexpr Timpl &operator&=(
const Timpl &other)
203 this->data &= other.data;
204 return static_cast<Timpl &
>(*this);
207 inline constexpr Timpl operator&(
const Timpl &other)
const
209 return Timpl{
static_cast<Tstorage
>(this->data & other.data)};
216 inline constexpr Tstorage
base() const noexcept
227 return (this->
base() & Tmask) == this->
base();
246 for (
auto i : *
this) {
247 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.
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 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.
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.
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.
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.