5#include <initializer_list>
16 using mask_t =
typename std::conditional<(MaxBits <= 8), uint8_t,
17 typename std::conditional<(MaxBits <= 16), uint16_t, uint32_t>
::type>
::type;
21 static constexpr unsigned to_bit(ValueType value) {
return static_cast<unsigned>(value); }
23 static constexpr ValueType
from_bit(
unsigned bit) {
return static_cast<ValueType
>(bit); }
56template<
typename ValueType,
typename BitPolicy = DefaultBitPolicy<ValueType, 16>>
class FiniteSetMask {
64 for (
auto value : values) {
70 constexpr void insert(ValueType value) { this->
mask_ |= (
static_cast<bitmask_t>(1) << BitPolicy::to_bit(value)); }
73 constexpr void insert(std::initializer_list<ValueType> values) {
74 for (
auto value : values) {
80 constexpr void erase(ValueType value) { this->
mask_ &= ~(
static_cast<bitmask_t>(1) << BitPolicy::to_bit(value)); }
87 constexpr size_t count(ValueType value)
const {
88 return (this->
mask_ & (
static_cast<bitmask_t>(1) << BitPolicy::to_bit(value))) != 0 ? 1 : 0;
92 constexpr size_t size()
const {
148 return (mask & (
static_cast<bitmask_t>(1) << BitPolicy::to_bit(value))) != 0;
161 while (bit < BitPolicy::MAX_BITS && !(mask & (
static_cast<bitmask_t>(1) << bit))) {
Iterator support for range-based for loops and API encoding Iterates over set bits and converts bit p...
constexpr ValueType operator*() const
constexpr bool operator!=(const Iterator &other) const
constexpr Iterator(bitmask_t mask)
std::ptrdiff_t difference_type
constexpr Iterator & operator++()
std::forward_iterator_tag iterator_category
const ValueType * pointer
constexpr bool operator==(const Iterator &other) const
Generic bitmask for storing a finite set of discrete values efficiently.
static constexpr ValueType first_value_from_mask(bitmask_t mask)
Get the first value from a raw bitmask Used for optimizing intersection logic (e.g....
constexpr size_t count(ValueType value) const
Check if the set contains a specific value (std::set compatibility) Returns 1 if present,...
constexpr void clear()
Clear all values from the set.
constexpr Iterator end() const
constexpr void insert(std::initializer_list< ValueType > values)
Add multiple values from initializer list.
constexpr bitmask_t get_mask() const
Get the raw bitmask value for optimized operations.
constexpr void insert(ValueType value)
Add a single value to the set (std::set compatibility)
constexpr FiniteSetMask()=default
constexpr void erase(ValueType value)
Remove a value from the set (std::set compatibility)
constexpr bool empty() const
Check if the set is empty.
static constexpr int find_next_set_bit(bitmask_t mask, int start_bit)
Find the next set bit in a bitmask starting from a given position Returns the bit position,...
constexpr FiniteSetMask(std::initializer_list< ValueType > values)
Construct from initializer list: {VALUE1, VALUE2, ...}.
constexpr size_t size() const
Count the number of values in the set.
static constexpr bool mask_contains(bitmask_t mask, ValueType value)
Check if a specific value is present in a raw bitmask Useful for checking intersection results withou...
typename BitPolicy::mask_t bitmask_t
constexpr Iterator begin() const
Providing packet encoding functions for exchanging data with a remote host.
Default bit mapping policy for contiguous enums starting at 0 Provides 1:1 mapping where enum value e...
typename std::conditional<(MaxBits<=8), uint8_t, typename std::conditional<(MaxBits<=16), uint16_t, uint32_t >::type >::type mask_t
static constexpr unsigned to_bit(ValueType value)
static constexpr int MAX_BITS
static constexpr ValueType from_bit(unsigned bit)