GF2++
Loading...
Searching...
No Matches
BitSet.h
Go to the documentation of this file.
1#pragma once
2// SPDX-FileCopyrightText: 2025 Nessan Fitzmaurice <nzznfitz+gh@icloud.com>
3// SPDX-License-Identifier: MIT
4
8
9#include <gf2/BitStore.h>
10#include <gf2/Iterators.h>
11#include <gf2/BitRef.h>
12#include <gf2/BitSpan.h>
13#include <gf2/BitSet.h>
14
15namespace gf2 {
16
23template<usize N, Unsigned Word = usize>
24class BitSet {
25private:
26 // The bit elements are packed compactly into this standard array of unsigned words
27 std::array<Word, words_needed<Word>(N)> m_store{};
28
29public:
31 using word_type = Word;
32
34 static constexpr u8 bits_per_word = BITS<Word>;
35
38
46 constexpr usize size() const { return N; }
47
59 constexpr usize words() const { return m_store.size(); }
60
77 constexpr Word word(usize i) const {
78 gf2_debug_assert(i < words(), "Index {} is too large for a bitset with {} words.", i, words());
79 return m_store[i];
80 }
81
96 constexpr void set_word(usize i, Word word) {
97 gf2_debug_assert(i < words(), "Index {} is too large for a bitset with {} words.", i, words());
98
99 // Set the value and if it's the last word, make sure it is kept clean
100 m_store[i] = word;
101 if (i == words() - 1) clean();
102 }
103
113 constexpr const Word* store() const { return m_store.data(); }
114
126 constexpr Word* store() { return m_store.data(); }
127
131 constexpr u8 offset() const { return 0; }
132
136
146 explicit constexpr BitSet() {
147 for (usize i = 0; i < words(); ++i) m_store[i] = 0;
148 }
149
159 explicit constexpr BitSet(Word word) {
160 // Fill the store with the given word & then clean up any excess bits
161 for (usize i = 0; i < words(); ++i) m_store[i] = word;
162 clean();
163 }
164
168
176 static constexpr BitSet zeros() { return BitSet{}; }
177
185 static constexpr BitSet ones() { return BitSet{MAX<Word>}; }
186
196 static constexpr BitSet constant(bool value) { return BitSet{value ? MAX<Word> : Word{0}}; }
197
207 static constexpr BitSet unit(usize i) {
208 gf2_assert(i < N, "Unit axis i = {} should be less than the bit-set size n = {}", i, N);
209 BitSet result{};
210 result.set(i);
211 return result;
212 }
213
221 static constexpr BitSet alternating() { return BitSet{gf2::ALTERNATING<Word>}; }
222
226
230 constexpr void clean() {
231 // NOTE: This works fine even if `size() == 0`.
232 auto shift = N % bits_per_word;
233 if (shift != 0) m_store[words() - 1] &= Word(~(MAX<Word> << shift));
234 }
235
246 static constexpr BitSet from(std::bitset<N> const& src) {
247 BitSet result{};
248 result.copy(src);
249 return result;
250 }
251
261 static constexpr BitSet from(std::invocable<usize> auto f) {
262 BitSet result{};
263 result.copy(f);
264 return result;
265 }
266
270
288 static BitSet random(double p = 0.5, u64 seed = 0) {
289 BitSet result{};
290 result.random_fill(p, seed);
291 return result;
292 }
293
310 static BitSet seeded_random(u64 seed) { return random(0.5, seed); }
311
323 static BitSet biased_random(double p) { return random(p, 0); }
324
328
340 constexpr bool get(usize i) const { return gf2::get(*this, i); }
341
354 constexpr bool operator[](usize i) const { return gf2::get(*this, i); }
355
367 constexpr bool front() const { return gf2::front(*this); }
368
380 constexpr bool back() const { return gf2::back(*this); }
381
385
398 auto set(usize i, bool value = true) { return gf2::set(*this, i, value); }
399
419 constexpr auto operator[](usize i) { return gf2::ref(*this, i); }
420
435 auto flip(usize i) { return gf2::flip(*this, i); }
436
455 constexpr auto swap(usize i0, usize i1) { return gf2::swap(*this, i0, i1); }
456
460
470 constexpr bool is_empty() const { return gf2::is_empty(*this); }
471
483 constexpr bool any() const { return gf2::any(*this); }
484
498 constexpr bool all() const { return gf2::all(*this); }
499
511 constexpr bool none() const { return gf2::none(*this); }
512
516
527 auto set_all(bool value = true) { return gf2::set_all(*this, value); }
528
537 auto flip_all() { return gf2::flip_all(*this); }
538
542
560 template<Unsigned Src>
561 auto copy(Src src) {
562 return gf2::copy(src, *this);
563 }
564
578 template<BitStore Src>
579 auto copy(Src const& src) {
580 return gf2::copy(src, *this);
581 }
582
594 auto copy(std::bitset<N> const& src) { return gf2::copy(src, *this); }
595
599
609 auto copy(std::invocable<usize> auto f) { return gf2::copy(*this, f); }
610
628 auto random_fill(double p = 0.5, u64 seed = 0) { return gf2::random_fill(*this, p, seed); }
629
633
643 constexpr usize count_ones() const { return gf2::count_ones(*this); }
644
654 constexpr usize count_zeros() const { return gf2::count_zeros(*this); }
655
668 constexpr usize leading_zeros() const { return gf2::leading_zeros(*this); }
669
679 constexpr usize trailing_zeros() const { return gf2::trailing_zeros(*this); }
680
684
700 constexpr std::optional<usize> first_set() const { return gf2::first_set(*this); }
701
715 constexpr std::optional<usize> last_set() const { return gf2::last_set(*this); }
716
729 constexpr std::optional<usize> next_set(usize index) const { return gf2::next_set(*this, index); }
730
743 constexpr std::optional<usize> previous_set(usize index) const { return gf2::previous_set(*this, index); }
744
748
764 constexpr std::optional<usize> first_unset() const { return gf2::first_unset(*this); }
765
781 constexpr std::optional<usize> last_unset() const { return gf2::last_unset(*this); }
782
797 constexpr std::optional<usize> next_unset(usize index) const { return gf2::next_unset(*this, index); }
798
813 constexpr std::optional<usize> previous_unset(usize index) const { return gf2::previous_unset(*this, index); }
814
818
831 constexpr auto bits() const { return gf2::bits(*this); }
832
846 constexpr auto bits() { return gf2::bits(*this); }
847
860 constexpr auto set_bits() const { return gf2::set_bits(*this); }
861
874 constexpr auto unset_bits() const { return gf2::unset_bits(*this); }
875
893 constexpr auto store_words() const { return gf2::store_words(*this); }
894
905 constexpr auto to_words() const { return gf2::to_words(*this); }
906
910
924 constexpr auto span(usize begin, usize end) const { return gf2::span(*this, begin, end); }
925
942 constexpr auto span(usize begin, usize end) { return gf2::span(*this, begin, end); }
943
947
962 constexpr auto sub(usize begin, usize end) const { return gf2::sub(*this, begin, end); }
963
967
990 constexpr void split_at(usize at, BitVec<word_type>& left, BitVec<word_type>& right) const {
991 return gf2::split(*this, at, left, right);
992 }
993
1012 constexpr auto split_at(usize at) const { return gf2::split(*this, at); }
1013
1017
1032 constexpr void riffled(BitVec<word_type>& dst) const { return gf2::riffle(*this, dst); }
1033
1046 constexpr auto riffled() const { return gf2::riffle(*this); }
1047
1051
1068 std::string to_binary_string(std::string_view sep = "", std::string_view pre = "",
1069 std::string_view post = "") const {
1070 return gf2::to_binary_string(*this, sep, pre, post);
1071 }
1072
1089 std::string to_string(std::string_view sep = "", std::string_view pre = "", std::string_view post = "") const {
1090 return gf2::to_string(*this, sep, pre, post);
1091 }
1092
1106 std::string to_pretty_string() const { return gf2::to_pretty_string(*this); }
1107
1139 std::string to_hex_string() const { return gf2::to_hex_string(*this); }
1140
1144 std::string describe() const { return gf2::describe(*this); }
1145
1147};
1148
1149} // namespace gf2
A BitRef is a proxy type that references a single bit in a bit-store. See the BitRef page for more ...
Fixed-size vectors over GF(2) with compactly stored bit-elements in a standard array of primitive uns...
Non-owning views of a range of bits from some underlying contiguous unsigned words....
A concept for types that can access individual bits packed into contiguous primitive unsigned words,...
Five bit-store iterators that serve different purposes. See the Iterators page for more details.
#define gf2_debug_assert(cond,...)
A confirmation macro that checks a boolean condition cond. On failure, the confirmation prints an e...
Definition assert.h:68
#define gf2_assert(cond,...)
A confirmation macro that checks a boolean condition cond. On failure, the confirmation prints an e...
Definition assert.h:98
std::string to_hex_string() const
Returns the "hex" string representation of the bits in the bit-store.
Definition BitSet.h:1139
constexpr bool front() const
Returns true if the first bit element is set, false otherwise.
Definition BitSet.h:367
constexpr std::optional< usize > last_unset() const
Returns the index of the last unset bit in the bit-store or {} if no bits are unset.
Definition BitSet.h:781
std::string to_string(std::string_view sep="", std::string_view pre="", std::string_view post="") const
Returns a binary string representation of the store.
Definition BitSet.h:1089
Word word_type
The underlying unsigned word type used to store the bits.
Definition BitSet.h:31
constexpr BitSet(Word word)
Constructs a bit-set with N elements by repeatedly copying all the bits from word.
Definition BitSet.h:159
constexpr std::optional< usize > first_unset() const
Returns the index of the first unset bit in the bit-store or {} if no bits are unset.
Definition BitSet.h:764
constexpr bool back() const
Returns true if the last bit element is set, false otherwise.
Definition BitSet.h:380
auto copy(Src src)
Copies the bits from an unsigned integral src value and returns a reference to this for chaining.
Definition BitSet.h:561
static constexpr BitSet alternating()
Factory method to generate a bit-set of length N looking like 101010....
Definition BitSet.h:221
constexpr Word * store()
Returns a pointer to the underlying store of words.
Definition BitSet.h:126
constexpr auto riffled() const
Returns a new bit-set that is the result of riffling the bits in this bit-store with zeros.
Definition BitSet.h:1046
static BitSet random(double p=0.5, u64 seed=0)
Factory method to generate a bit-set of size N where the elements are picked at random.
Definition BitSet.h:288
std::string to_binary_string(std::string_view sep="", std::string_view pre="", std::string_view post="") const
Returns a binary string representation of the store.
Definition BitSet.h:1068
static constexpr BitSet ones()
Factory method to generate a bit-set of length N where the elements are all 1.
Definition BitSet.h:185
auto flip(usize i)
Flips the value of the bit-element i and returns this for chaining.
Definition BitSet.h:435
constexpr const Word * store() const
Returns a const pointer to the underlying store of words .
Definition BitSet.h:113
constexpr auto span(usize begin, usize end) const
Returns an immutable bit-span encompassing the store's bits in the half-open range [begin,...
Definition BitSet.h:924
constexpr std::optional< usize > next_unset(usize index) const
Returns the index of the next unset bit after index in the store or {} if no more unset bits exist.
Definition BitSet.h:797
constexpr std::optional< usize > first_set() const
Returns the index of the first set bit in the bit-store or {} if no bits are set.
Definition BitSet.h:700
constexpr void split_at(usize at, BitVec< word_type > &left, BitVec< word_type > &right) const
Views a bit-store as two parts containing the elements [0, at) and [at, size()) respectively.
Definition BitSet.h:990
auto copy(std::bitset< N > const &src)
Copies the bits of an equal-sized std::bitset and returns a reference to this for chaining.
Definition BitSet.h:594
static constexpr BitSet constant(bool value)
Factory method to generate a bit-set of length N where the elements are set to value.
Definition BitSet.h:196
auto copy(Src const &src)
Copies the bits from an equal-sized src store and returns a reference to this for chaining.
Definition BitSet.h:579
constexpr bool is_empty() const
Returns true if the store is empty, false otherwise.
Definition BitSet.h:470
constexpr auto operator[](usize i)
Returns a "reference" to the bit element i.
Definition BitSet.h:419
static constexpr BitSet unit(usize i)
Factory method to generate a "unit" bit-set of length N where only element i is set.
Definition BitSet.h:207
constexpr bool any() const
Returns true if at least one bit in the store is set, false otherwise.
Definition BitSet.h:483
constexpr Word word(usize i) const
Returns word i from the bitset's underlying word store.
Definition BitSet.h:77
constexpr auto sub(usize begin, usize end) const
Returns a clone of the elements in the half-open range [begin, end) as a new bit-set.
Definition BitSet.h:962
static BitSet biased_random(double p)
Factory method to generate a bit-set of size N where the elements are from independent fair coin flip...
Definition BitSet.h:323
auto random_fill(double p=0.5, u64 seed=0)
Fill the store with random bits and returns a reference to this for chaining.
Definition BitSet.h:628
static constexpr u8 bits_per_word
The number of bits per Word.
Definition BitSet.h:34
static constexpr BitSet zeros()
Factory method to generate a bit-set of length N where the elements are all 0.
Definition BitSet.h:176
constexpr bool all() const
Returns true if all bits in the store are set, false otherwise.
Definition BitSet.h:498
constexpr auto store_words() const
Returns a const iterator over all the words underlying the bit-store.
Definition BitSet.h:893
constexpr std::optional< usize > next_set(usize index) const
Returns the index of the next set bit after index in the store or {} if no more set bits exist.
Definition BitSet.h:729
std::string describe() const
Returns a multi-line string describing the bit-store in some detail.
Definition BitSet.h:1144
auto flip_all()
Flips the value of the bits in the store and returns a reference to this for chaining.
Definition BitSet.h:537
auto set_all(bool value=true)
Sets the bits in the store to the boolean value and returns a reference to this for chaining.
Definition BitSet.h:527
static constexpr BitSet from(std::invocable< usize > auto f)
Factory method to construct a bit-set by repeatedly calling f(i) for i in [0, N).
Definition BitSet.h:261
constexpr auto split_at(usize at) const
Views a bit-store as two parts containing the elements [0, at) and [at, size()) respectively.
Definition BitSet.h:1012
constexpr auto swap(usize i0, usize i1)
Swaps the bits in the bit-store at indices i0 and i1 and returns this for chaining.
Definition BitSet.h:455
static BitSet seeded_random(u64 seed)
Factory method to generate a bit-set of size N where the elements are from independent fair coin flip...
Definition BitSet.h:310
constexpr usize count_ones() const
Returns the number of set bits in the store.
Definition BitSet.h:643
constexpr void set_word(usize i, Word word)
Sets word i in the bitset's underlying word store to value (masked if necessary).
Definition BitSet.h:96
constexpr std::optional< usize > previous_set(usize index) const
Returns the index of the previous set bit before index in the store or {} if there are none.
Definition BitSet.h:743
constexpr auto to_words() const
Returns a copy of the words underlying this bit-store.
Definition BitSet.h:905
auto copy(std::invocable< usize > auto f)
Fill the store by repeatedly calling f(i) and returns a reference to this for chaining.
Definition BitSet.h:609
constexpr auto span(usize begin, usize end)
Returns a mutable bit-span encompassing the bits in the half-open range [begin, end).
Definition BitSet.h:942
constexpr std::optional< usize > last_set() const
Returns the index of the last set bit in the bit-store or {} if no bits are set.
Definition BitSet.h:715
constexpr void clean()
Sets any unused bits in the last occupied word to 0.
Definition BitSet.h:230
constexpr bool none() const
Returns true if no bits in the store are set, false otherwise.
Definition BitSet.h:511
constexpr usize trailing_zeros() const
Returns the number of trailing zeros in the store.
Definition BitSet.h:679
constexpr usize words() const
Returns the number of words in the bitset's underlying word store.
Definition BitSet.h:59
constexpr auto bits()
Returns a non-const iterator over the values of the bits in the mutable bit-store.
Definition BitSet.h:846
constexpr auto unset_bits() const
Returns an iterator over the indices of any unset bits in the bit-store.
Definition BitSet.h:874
constexpr u8 offset() const
Returns the offset (in bits) of the first bit in the store within the first word.
Definition BitSet.h:131
constexpr bool get(usize i) const
Returns true if the bit at the given index i is set, false otherwise.
Definition BitSet.h:340
constexpr auto bits() const
Returns a const iterator over the bool values of the bits in the const bit-store.
Definition BitSet.h:831
constexpr usize size() const
Returns the number of bit-elements in the bitset.
Definition BitSet.h:46
static constexpr BitSet from(std::bitset< N > const &src)
Factory method to construct a bit-set from the bits of a std::bitset.
Definition BitSet.h:246
constexpr usize leading_zeros() const
Returns the number of leading zeros in the store.
Definition BitSet.h:668
constexpr bool operator[](usize i) const
Returns the boolean value of the bit element i.
Definition BitSet.h:354
constexpr usize count_zeros() const
Returns the number of unset bits in the store.
Definition BitSet.h:654
std::string to_pretty_string() const
Returns a "pretty" string representation of the store.
Definition BitSet.h:1106
auto set(usize i, bool value=true)
Sets the bit-element i to the specified boolean value & returns this for chaining....
Definition BitSet.h:398
constexpr void riffled(BitVec< word_type > &dst) const
Interleaves the bits of this bit-store with zeros storing the result into the bit-set dst.
Definition BitSet.h:1032
constexpr BitSet()
Constructs a bit-set of length N with all the bit elements set to 0.
Definition BitSet.h:146
constexpr auto set_bits() const
Returns an iterator over the indices of any set bits in the bit-store.
Definition BitSet.h:860
constexpr std::optional< usize > previous_unset(usize index) const
Returns the index of the previous unset bit before index in the store or {} if no more unset bits exi...
Definition BitSet.h:813
A dynamically-sized vector over GF(2) with bit elements compactly stored in a standard vector of prim...
Definition BitVec.h:23
The namespace for the gf2 library.
Definition assert.h:119
constexpr std::optional< usize > first_unset(Store const &store)
Returns the index of the first unset bit in the bit-store or {} if no bits are unset.
Definition BitStore.h:903
constexpr std::optional< usize > first_set(Store const &store)
Returns the index of the first set bit in the bit-store or {} if no bits are set.
Definition BitStore.h:770
constexpr bool is_empty(Store const &store)
Returns true if the store is empty, false otherwise.
Definition BitStore.h:337
constexpr std::optional< usize > next_unset(Store const &store, usize index)
Returns the index of the next unset bit after index in the store or {} if no more unset bits exist.
Definition BitStore.h:968
constexpr Word ALTERNATING
The Unsigned instance with alternating bits set to 0 and 1.
Definition Unsigned.h:91
static std::string to_string(Store const &store, std::string_view sep="", std::string_view pre="", std::string_view post="")
Returns a binary string representation of a store.
Definition BitStore.h:1482
constexpr auto & flip_all(Store &store)
Flips the value of the bits in the store and returns the store for chaining.
Definition BitStore.h:445
constexpr auto ref(Store &store, usize i)
Returns a "reference" to the bit element i in the given bit-store.
Definition BitStore.h:189
constexpr usize count_zeros(Store const &store)
Returns the number of unset bits in the store.
Definition BitStore.h:691
constexpr auto & flip(Store &store, usize i)
Flips the value of the bit-element at the given index and returns the store for chaining.
Definition BitStore.h:269
constexpr bool get(Store const &store, usize i)
Returns the bool value of the bit at index i in the given bit-store.
Definition BitStore.h:163
constexpr auto set_bits(Store const &store)
Returns an iterator over the indices of any set bits in the bit-store.
Definition BitStore.h:1097
constexpr bool back(Store const &store)
Returns true if the final bit element is set, false otherwise.
Definition BitStore.h:225
constexpr auto & random_fill(Store &store, double p=0.5, u64 seed=0)
Fill the store with random bits and returns the store for chaining.
Definition BitStore.h:631
static std::string to_pretty_string(Store const &store)
Returns a "pretty" string representation of a store.
Definition BitStore.h:1500
std::uint64_t u64
Word type alias for a 64-bit unsigned integer.
Definition Unsigned.h:39
constexpr std::optional< usize > previous_set(Store const &store, usize index)
Returns the index of the previous set bit before index in the store or {} if there are none.
Definition BitStore.h:857
constexpr auto store_words(Store const &store)
Returns a const iterator over all the words underlying the bit-store.
Definition BitStore.h:1137
constexpr std::optional< usize > next_set(Store const &store, usize index)
Returns the index of the next set bit after index in the store or {} if no more set bits exist.
Definition BitStore.h:819
constexpr auto & set_all(Store &store, bool value=true)
Sets the bits in the store to the boolean value and returns the store for chaining.
Definition BitStore.h:428
std::uint8_t u8
Word type alias for an 8-bit unsigned integer.
Definition Unsigned.h:30
constexpr bool all(Store const &store)
Returns true if all bits in the store are set, false otherwise.
Definition BitStore.h:375
constexpr auto sub(Store const &store, usize begin, usize end)
Returns a clone of the elements in the half-open range [begin, end) as a new bit-vector.
Definition BitStore.h:1253
static constexpr auto span(Store const &store, usize begin, usize end)
Constructs a read-only bit-span over the const bit-store store for its bits in the range [begin,...
Definition BitStore.h:1176
constexpr auto to_words(Store const &store)
Returns a copy of the words underlying this bit-store.
Definition BitStore.h:1153
constexpr void split(Store const &store, usize at, BitVec< typename Store::word_type > &left, BitVec< typename Store::word_type > &right)
Views a bit-store as two parts containing the elements [0, at) and [at, size()) respectively....
Definition BitStore.h:1283
constexpr auto bits(Store const &store)
Returns a const iterator over the bool values of the bits in the const bit-store.
Definition BitStore.h:1061
constexpr auto unset_bits(Store const &store)
Returns an iterator over the indices of any unset bits in the bit-store.
Definition BitStore.h:1114
constexpr auto & set(Store &store, usize i, bool value=true)
Sets the bit-element at the given index to the specified boolean value and returns the store for chai...
Definition BitStore.h:244
static std::string to_hex_string(Store const &store)
Returns the "hex" string representation of the bits in the bit-store.
Definition BitStore.h:1536
constexpr usize count_ones(Store const &store)
Returns the number of set bits in the store.
Definition BitStore.h:674
constexpr usize leading_zeros(Store const &store)
Returns the number of leading zeros in the store.
Definition BitStore.h:708
constexpr std::optional< usize > previous_unset(Store const &store, usize index)
Returns the index of the previous unset bit before index in the store or {} if no more unset bits exi...
Definition BitStore.h:1013
constexpr auto & copy(Src src, Store &store)
Copies the bits from an unsigned integral src value and returns the store for chaining.
Definition BitStore.h:474
constexpr bool front(Store const &store)
Returns true if the first bit element is set, false otherwise.
Definition BitStore.h:207
constexpr usize trailing_zeros(Store const &store)
Returns the number of trailing zeros in the store.
Definition BitStore.h:729
std::size_t usize
Word type alias for the platform's "native"-sized unsigned integer.
Definition Unsigned.h:42
constexpr bool none(Store const &store)
Returns true if no bits in the store are set, false otherwise.
Definition BitStore.h:408
constexpr Word MAX
The Unsigned instance with all its bits set to 1.
Definition Unsigned.h:82
constexpr u8 BITS
The number of bits in an Unsigned returned as a u8.
Definition Unsigned.h:55
constexpr bool any(Store const &store)
Returns true if at least one bit in the store is set, false otherwise.
Definition BitStore.h:354
constexpr std::optional< usize > last_unset(Store const &store)
Returns the index of the last unset bit in the bit-store or {} if no bits are unset.
Definition BitStore.h:936
static std::string describe(Store const &store)
Detailed Description of a Bit-Store.
Definition BitStore.h:1579
constexpr void riffle(Store const &store, BitVec< typename Store::word_type > &dst)
Interleaves the bits of a bit-store with zeros storing the result into the passed bit-vector dst.
Definition BitStore.h:1362
static std::string to_binary_string(Store const &store, std::string_view sep="", std::string_view pre="", std::string_view post="")
Returns a binary string representation of a store.
Definition BitStore.h:1431
constexpr auto & swap(Store &store, usize i0, usize i1)
Swaps the bits in the bit-store at indices i0 and i1 and returns the store for chaining.
Definition BitStore.h:297
constexpr std::optional< usize > last_set(Store const &store)
Returns the index of the last set bit in the bit-store or {} if no bits are set.
Definition BitStore.h:795