25 using storage_t = uint32_t;
27 static constexpr uint16_t
storageSize = (N +
sizeof(storage_t) * 8 - 1) / (
sizeof(storage_t) * 8);
28 static constexpr uint16_t bitsPerStorage =
sizeof(storage_t) * 8;
31 std::array<storage_t, storageSize> _data;
34 static constexpr uint16_t word_index(uint16_t i)
noexcept {
return i / bitsPerStorage; }
37 static constexpr storage_t bit_mask(uint16_t i)
noexcept {
return static_cast<storage_t
>(1) << (i % bitsPerStorage); }
40 static constexpr storage_t last_word_mask() noexcept
42 const uint16_t lastWordBits = N % bitsPerStorage;
43 if (lastWordBits == 0)
44 return static_cast<storage_t
>(~storage_t {0});
45 storage_t mask =
static_cast<storage_t
>(1) << lastWordBits;
57 explicit constexpr BitSet(storage_t value) : _data {}
67 storage_t fullMask =
static_cast<storage_t
>(~storage_t {0});
76 for (
auto& word: _data)
84 void set(uint16_t pos)
noexcept
88 _data[word_index(pos)] |= bit_mask(pos);
96 void set(uint16_t pos,
bool value)
noexcept
101 _data[word_index(pos)] |= bit_mask(pos);
103 _data[word_index(pos)] &= ~bit_mask(pos);
114 _data[word_index(pos)] &= ~bit_mask(pos);
121 void flip(uint16_t pos)
noexcept
125 _data[word_index(pos)] ^= bit_mask(pos);
133 constexpr bool test(uint16_t pos)
const noexcept
137 return (_data[word_index(pos)] & bit_mask(pos)) != 0;
151 if (pos >= parent->
size())
154 parent->_data[parent->word_index(pos)] |= parent->bit_mask(pos);
156 parent->_data[parent->word_index(pos)] &= ~parent->bit_mask(pos);
162 if (pos >= parent->
size())
166 parent->_data[parent->word_index(pos)] |= parent->bit_mask(pos);
168 parent->_data[parent->word_index(pos)] &= ~parent->bit_mask(pos);
172 constexpr operator bool()
const noexcept
174 if (pos >= parent->
size())
176 return (parent->_data[parent->word_index(pos)] & parent->bit_mask(pos)) != 0;
181 if (pos >= parent->
size())
183 parent->_data[parent->word_index(pos)] ^= parent->bit_mask(pos);
195 constexpr uint16_t
count() const noexcept
198 for (
const auto& word: _data)
203 total +=
static_cast<uint16_t
>(w & 1);
211 constexpr bool any() const noexcept
213 for (
const auto& word: _data)
220 constexpr bool all() const noexcept
222 storage_t fullMask =
static_cast<storage_t
>(~storage_t {0});
224 if (_data[i] != fullMask)
226 return (_data[
storageSize - 1] & last_word_mask()) == last_word_mask();
230 constexpr bool none() const noexcept
232 for (
const auto& word: _data)
241 storage_t fullMask =
static_cast<storage_t
>(~storage_t {0});
243 _data[i] ^= fullMask;
256 result._data[i] = _data[i] & other._data[i];
269 result._data[i] = _data[i] | other._data[i];
282 result._data[i] = _data[i] & ~other._data[i];
293 storage_t fullMask =
static_cast<storage_t
>(~storage_t {0});
295 result._data[i] = ~_data[i];
304 _data[i] &= other._data[i];
312 _data[i] |= other._data[i];
320 _data[i] ^= other._data[i];
328 if (_data[i] != other._data[i])
337 static constexpr uint16_t
size() noexcept {
return N; }
A compact, zero-allocation bitset for compile-time sized bit arrays.
Definition: bitset.h:23
void set(uint16_t pos, bool value) noexcept
Set the bit at position pos to the given value.
Definition: bitset.h:96
void flip() noexcept
Flip all bits.
Definition: bitset.h:239
constexpr bool operator==(const BitSet< N > &other) const noexcept
Check equality with another bitset.
Definition: bitset.h:325
constexpr bool all() const noexcept
Return true if all bits are set.
Definition: bitset.h:220
constexpr bool operator[](uint16_t pos) const noexcept
Alias for test(pos)
Definition: bitset.h:141
void set() noexcept
Set all bits to 1.
Definition: bitset.h:65
constexpr BitSet(storage_t value)
Construct with a single storage value repeated for all words.
Definition: bitset.h:57
BitSet< N > & operator|=(const BitSet< N > &other) noexcept
In-place bitwise OR.
Definition: bitset.h:309
void flip(uint16_t pos) noexcept
Flip (invert) the bit at position pos.
Definition: bitset.h:121
BitSet< N > & operator&=(const BitSet< N > &other) noexcept
In-place bitwise AND.
Definition: bitset.h:301
BitSet< N > operator~() const noexcept
Compute the bitwise NOT of this bitset.
Definition: bitset.h:290
constexpr bool test(uint16_t pos) const noexcept
Get the value of the bit at position pos.
Definition: bitset.h:133
constexpr BitSet< N > operator|(const BitSet< N > &other) const noexcept
Compute the union of this bitset with another.
Definition: bitset.h:265
void set(uint16_t pos) noexcept
Set the bit at position pos to 1.
Definition: bitset.h:84
BitSet< N > & operator^=(const BitSet< N > &other) noexcept
In-place bitwise XOR.
Definition: bitset.h:317
constexpr BitSet< N > operator-(const BitSet< N > &other) const noexcept
Compute the difference of this bitset with another.
Definition: bitset.h:278
constexpr BitSet< N > operator&(const BitSet< N > &other) const noexcept
Compute the intersection of this bitset with another.
Definition: bitset.h:252
constexpr bool none() const noexcept
Return true if no bits are set.
Definition: bitset.h:230
constexpr uint16_t count() const noexcept
Count the number of bits set to 1.
Definition: bitset.h:195
void reset(uint16_t pos) noexcept
Clear the bit at position pos.
Definition: bitset.h:110
constexpr BitSet()
Default constructor: all bits set to zero.
Definition: bitset.h:51
static constexpr uint16_t size() noexcept
Return the number of bits this bitset can hold.
Definition: bitset.h:337
void reset() noexcept
Set all bits to 0.
Definition: bitset.h:74
constexpr bool any() const noexcept
Return true if any bit is set.
Definition: bitset.h:211
static constexpr uint16_t storageSize
The underlying storage type, sized to hold at least N bits.
Definition: bitset.h:27
static constexpr uint16_t size_words() noexcept
Return the number of storage words used.
Definition: bitset.h:340
reference operator[](uint16_t pos) noexcept
Non-const operator[] returning a proxy for bit manipulation.
Definition: bitset.h:189
constexpr bool operator!=(const BitSet< N > &other) const noexcept
Check inequality with another bitset.
Definition: bitset.h:334
Program scope.
Definition: control_fixed_modes.hpp:12
Non-const operator[]: returns a reference-like proxy that allows set/reset.
Definition: bitset.h:145