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);
119 inline constexpr Timpl &
Flip(Tvalue_type value)
121 if (this->
Test(value)) {
122 return this->
Reset(value);
124 return this->
Set(value);
133 inline constexpr Timpl &
Flip(
const Timpl &other)
135 this->
data ^= other.data;
136 return static_cast<Timpl&
>(*this);
144 inline constexpr bool Test(Tvalue_type value)
const
146 return (this->
data & (1ULL << Timpl::DecayValueType(value))) != 0;
154 inline constexpr bool All(
const Timpl &other)
const
156 return (this->
data & other.data) == other.data;
163 inline constexpr bool All()
const
165 return this->
data == Tmask;
173 inline constexpr bool Any(
const Timpl &other)
const
175 return (this->
data & other.data) != 0;
182 inline constexpr bool Any()
const
184 return this->
data != 0;
191 inline constexpr bool None()
const
193 return this->
data == 0;
196 inline constexpr Timpl &operator|=(
const Timpl &other)
198 this->data |= other.data;
199 return static_cast<Timpl &
>(*this);
202 inline constexpr Timpl operator|(
const Timpl &other)
const
204 return Timpl{
static_cast<Tstorage
>(this->data | other.data)};
207 inline constexpr Timpl &operator&=(
const Timpl &other)
209 this->data &= other.data;
210 return static_cast<Timpl &
>(*this);
213 inline constexpr Timpl operator&(
const Timpl &other)
const
215 return Timpl{
static_cast<Tstorage
>(this->data & other.data)};
222 inline constexpr Tstorage
base() const noexcept
233 return (this->
base() & Tmask) == this->
base();
252 for (
auto i : *
this) {
253 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 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.