GF2++
Loading...
Searching...
No Matches
BitArray.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/BitArray.h>
14
15namespace gf2 {
16
23template<usize N, Unsigned Word = usize>
24class BitArray {
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
78 constexpr Word word(usize i) const {
79 gf2_debug_assert(i < words(), "Index {} is too large for a bitset with {} words.", i, words());
80 return m_store[i];
81 }
82
98 constexpr void set_word(usize i, Word word) {
99 gf2_debug_assert(i < words(), "Index {} is too large for a bitset with {} words.", i, words());
100
101 // Set the value and if it's the last word, make sure it is kept clean
102 m_store[i] = word;
103 if (i == words() - 1) clean();
104 }
105
115 constexpr const Word* store() const { return m_store.data(); }
116
128 constexpr Word* store() { return m_store.data(); }
129
133 constexpr u8 offset() const { return 0; }
134
138
148 explicit constexpr BitArray() {
149 m_store.fill(Word{0});
150 }
151
161 explicit constexpr BitArray(Word word) {
162 // Fill the store with the given word & then clean up any excess bits
163 for (usize i = 0; i < words(); ++i) m_store[i] = word;
164 clean();
165 }
166
170
178 static constexpr BitArray zeros() { return BitArray{}; }
179
187 static constexpr BitArray ones() { return BitArray{MAX<Word>}; }
188
198 static constexpr BitArray constant(bool value) { return BitArray{value ? MAX<Word> : Word{0}}; }
199
209 static constexpr BitArray unit(usize i) {
210 gf2_assert(i < N, "Unit axis i = {} should be less than the bit-array size n = {}", i, N);
211 BitArray result{};
212 result.set(i);
213 return result;
214 }
215
223 static constexpr BitArray alternating() { return BitArray{gf2::ALTERNATING<Word>}; }
224
228
232 constexpr void clean() {
233 // NOTE: This works fine even if `size() == 0`.
234 auto shift = N % bits_per_word;
235 if (shift != 0) m_store[words() - 1] &= Word(~(MAX<Word> << shift));
236 }
237
254 template<Unsigned Src>
255 requires (BITS<Src> == N)
256 static constexpr BitArray from(Src src) {
257 BitArray result;
258 result.copy(src);
259 return result;
260 }
261
278 template<typename Iter>
279 requires std::is_unsigned_v<typename std::iterator_traits<Iter>::value_type>
280 static constexpr BitArray from(Iter src_begin, Iter src_end) {
281 // How many bits are we copying?
282 using src_type = typename std::iterator_traits<Iter>::value_type;
283 auto src_words = static_cast<std::size_t>(std::distance(src_begin, src_end));
284 auto src_bits = src_words * BITS<src_type>;
285 gf2_always_assert(src_bits == N, "Lengths do not match: {} != {}.", N, src_bits);
286 BitArray result;
287 result.copy(src_begin, src_end);
288 return result;
289 }
290
302 static constexpr BitArray from(std::bitset<N> const& src) {
303 BitArray result{};
304 result.copy(src);
305 return result;
306 }
307
317 static constexpr BitArray from(std::invocable<usize> auto f) {
318 BitArray result{};
319 result.copy(f);
320 return result;
321 }
322
326
344 static BitArray random(double p = 0.5, u64 seed = 0) {
345 BitArray result{};
346 result.fill_random(p, seed);
347 return result;
348 }
349
366 static BitArray seeded_random(u64 seed) { return random(0.5, seed); }
367
379 static BitArray biased_random(double p) { return random(p, 0); }
380
384
397 constexpr bool get(usize i) const { return gf2::get(*this, i); }
398
412 constexpr bool operator[](usize i) const { return gf2::get(*this, i); }
413
426 constexpr bool front() const { return gf2::front(*this); }
427
440 constexpr bool back() const { return gf2::back(*this); }
441
445
459 constexpr auto set(usize i, bool value = true) {
460 gf2::set(*this, i, value);
461 return *this;
462 }
463
486 constexpr auto operator[](usize i) { return gf2::ref(*this, i); }
487
503 constexpr auto flip(usize i) {
504 gf2::flip(*this, i);
505 return *this;
506 }
507
527 constexpr auto swap(usize i0, usize i1) {
528 gf2::swap(*this, i0, i1);
529 return *this;
530 }
531
535
545 constexpr bool is_empty() const { return gf2::is_empty(*this); }
546
558 constexpr bool any() const { return gf2::any(*this); }
559
573 constexpr bool all() const { return gf2::all(*this); }
574
586 constexpr bool none() const { return gf2::none(*this); }
587
591
602 constexpr auto set_all(bool value = true) {
603 gf2::set_all(*this, value);
604 return *this;
605 }
606
615 constexpr auto flip_all() {
616 gf2::flip_all(*this);
617 return *this;
618 }
619
623
644 template<Unsigned Src>
645 constexpr auto copy(Src src) {
646 gf2::copy(src, *this);
647 return *this;
648 }
649
668 template<typename Iter>
669 requires std::is_unsigned_v<typename std::iterator_traits<Iter>::value_type>
670 constexpr void copy(Iter src_begin, Iter src_end) {
671 gf2::copy(src_begin, src_end, *this);
672 }
673
692 template<BitStore Src>
693 constexpr auto copy(Src const& src) {
694 gf2::copy(src, *this);
695 return *this;
696 }
697
714 constexpr auto copy(std::bitset<N> const& src) {
715 gf2::copy(src, *this);
716 return *this;
717 }
718
722
732 constexpr auto copy(std::invocable<usize> auto f) {
733 gf2::copy(*this, f);
734 return *this;
735 }
736
754 constexpr auto fill_random(double p = 0.5, u64 seed = 0) {
755 gf2::fill_random(*this, p, seed);
756 return *this;
757 }
758
762
772 constexpr usize count_ones() const { return gf2::count_ones(*this); }
773
783 constexpr usize count_zeros() const { return gf2::count_zeros(*this); }
784
797 constexpr usize leading_zeros() const { return gf2::leading_zeros(*this); }
798
808 constexpr usize trailing_zeros() const { return gf2::trailing_zeros(*this); }
809
813
829 constexpr std::optional<usize> first_set() const { return gf2::first_set(*this); }
830
844 constexpr std::optional<usize> last_set() const { return gf2::last_set(*this); }
845
858 constexpr std::optional<usize> next_set(usize index) const { return gf2::next_set(*this, index); }
859
872 constexpr std::optional<usize> previous_set(usize index) const { return gf2::previous_set(*this, index); }
873
877
893 constexpr std::optional<usize> first_unset() const { return gf2::first_unset(*this); }
894
910 constexpr std::optional<usize> last_unset() const { return gf2::last_unset(*this); }
911
926 constexpr std::optional<usize> next_unset(usize index) const { return gf2::next_unset(*this, index); }
927
942 constexpr std::optional<usize> previous_unset(usize index) const { return gf2::previous_unset(*this, index); }
943
947
961 constexpr auto bits() const { return gf2::bits(*this); }
962
977 constexpr auto bits() { return gf2::bits(*this); }
978
991 constexpr auto set_bits() const { return gf2::set_bits(*this); }
992
1005 constexpr auto unset_bits() const { return gf2::unset_bits(*this); }
1006
1019 constexpr auto store_words() const { return gf2::store_words(*this); }
1020
1024
1043 constexpr void to_words(std::output_iterator<word_type> auto out) { gf2::to_words(*this, out); }
1044
1055 constexpr auto to_words() const { return gf2::to_words(*this); }
1056
1060
1075 constexpr auto span(usize begin, usize end) const { return gf2::span(*this, begin, end); }
1076
1094 constexpr auto span(usize begin, usize end) { return gf2::span(*this, begin, end); }
1095
1099
1115 constexpr auto sub(usize begin, usize end) const { return gf2::sub(*this, begin, end); }
1116
1120
1144 constexpr void split_at(usize at, BitVector<word_type>& left, BitVector<word_type>& right) const {
1145 return gf2::split(*this, at, left, right);
1146 }
1147
1167 constexpr auto split_at(usize at) const { return gf2::split(*this, at); }
1168
1172
1187 constexpr void riffled(BitVector<word_type>& dst) const { return gf2::riffle(*this, dst); }
1188
1201 constexpr auto riffled() const { return gf2::riffle(*this); }
1202
1206
1223 std::string to_binary_string(std::string_view sep = "", std::string_view pre = "",
1224 std::string_view post = "") const {
1225 return gf2::to_binary_string(*this, sep, pre, post);
1226 }
1227
1244 std::string to_string(std::string_view sep = "", std::string_view pre = "", std::string_view post = "") const {
1245 return gf2::to_string(*this, sep, pre, post);
1246 }
1247
1261 std::string to_pretty_string() const { return gf2::to_pretty_string(*this); }
1262
1294 std::string to_hex_string() const { return gf2::to_hex_string(*this); }
1295
1299 std::string describe() const { return gf2::describe(*this); }
1300
1302};
1303
1304} // namespace gf2
Fixed-size vectors over GF(2) with compactly stored bit-elements in a standard array of primitive uns...
A BitRef is a proxy type that references a single bit in a bit-store. See the BitRef page for more ...
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
#define gf2_always_assert(expr,...)
A confirmation macro that checks a boolean condition cond. On failure, the confirmation prints an e...
Definition assert.h:41
constexpr bool back() const
Returns true if the last bit element is set, false otherwise.
Definition BitArray.h:440
static constexpr BitArray constant(bool value)
Factory method to generate a bit-array of length N where the elements are set to value.
Definition BitArray.h:198
constexpr auto copy(std::bitset< N > const &src)
Copies all the bits from a std::bitset to this equal-sized bit-array and returns a reference to this ...
Definition BitArray.h:714
constexpr auto set(usize i, bool value=true)
Sets the bit-element i to the specified boolean value & returns this for chaining....
Definition BitArray.h:459
constexpr usize leading_zeros() const
Returns the number of leading zeros in the bit-array.
Definition BitArray.h:797
constexpr auto store_words() const
Returns a const iterator over all the words underlying the v.
Definition BitArray.h:1019
static constexpr BitArray zeros()
Factory method to generate a bit-array of length N where the elements are all 0.
Definition BitArray.h:178
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-vector.
Definition BitArray.h:1115
constexpr auto flip(usize i)
Flips the value of the bit-element i and returns this for chaining.
Definition BitArray.h:503
constexpr Word word(usize i) const
Returns word i from the bit-array's underlying word store.
Definition BitArray.h:78
constexpr auto operator[](usize i)
Returns a "reference" to the bit element i.
Definition BitArray.h:486
constexpr bool operator[](usize i) const
Returns the boolean value of the bit element i.
Definition BitArray.h:412
constexpr void riffled(BitVector< word_type > &dst) const
Interleaves the bits of this bit-array with zeros storing the result into the bit-vector dst.
Definition BitArray.h:1187
constexpr usize count_zeros() const
Returns the number of unset bits in the bit-array.
Definition BitArray.h:783
constexpr auto set_bits() const
Returns an iterator over the indices of any set bits in the bit-array.
Definition BitArray.h:991
constexpr auto to_words() const
Returns a copy of the words underlying this bit-array as a std::vector<word_type>.
Definition BitArray.h:1055
constexpr std::optional< usize > previous_unset(usize index) const
Returns the index of the previous unset bit before index in the bit-array or {} if no more unset bits...
Definition BitArray.h:942
static constexpr u8 bits_per_word
The number of bits per Word.
Definition BitArray.h:34
constexpr bool front() const
Returns true if the first bit element is set, false otherwise.
Definition BitArray.h:426
constexpr auto unset_bits() const
Returns an iterator over the indices of any unset bits in the bit-array.
Definition BitArray.h:1005
constexpr bool all() const
Returns true if all bits in the bit-array are set, false otherwise.
Definition BitArray.h:573
constexpr auto copy(std::invocable< usize > auto f)
Fill the bit-array by repeatedly calling f(i) and returns a reference to this for chaining.
Definition BitArray.h:732
static constexpr BitArray from(std::bitset< N > const &src)
Factory method to construct a bit-array from the bits of a std::bitset.
Definition BitArray.h:302
constexpr auto span(usize begin, usize end)
Returns a mutable bit-span encompassing the bit-array's bits in the half-open range [begin,...
Definition BitArray.h:1094
constexpr std::optional< usize > first_unset() const
Returns the index of the first unset bit in the bit-array or {} if no bits are unset.
Definition BitArray.h:893
constexpr auto span(usize begin, usize end) const
Returns an immutable bit-span encompassing the bit-array's bits in the half-open range [begin,...
Definition BitArray.h:1075
constexpr BitArray(Word word)
Constructs a bit-array with N elements by repeatedly copying all the bits from word.
Definition BitArray.h:161
constexpr std::optional< usize > next_set(usize index) const
Returns the index of the next set bit after index in the bit-array or {} if no more set bits exist.
Definition BitArray.h:858
std::string to_hex_string() const
Returns the "hex" string representation of the bits in the bit-array.
Definition BitArray.h:1294
static constexpr BitArray unit(usize i)
Factory method to generate a "unit" bit-array of length N where only element i is set.
Definition BitArray.h:209
static constexpr BitArray from(Src src)
Factory method to construct a bit-array by copying all the bits from any Unsigned instance.
Definition BitArray.h:256
constexpr usize trailing_zeros() const
Returns the number of trailing zeros in the bit-array.
Definition BitArray.h:808
constexpr auto fill_random(double p=0.5, u64 seed=0)
Fill the bit-array with random bits and returns a reference to this for chaining.
Definition BitArray.h:754
std::string to_pretty_string() const
Returns a "pretty" string representation of the bit-array.
Definition BitArray.h:1261
static constexpr BitArray ones()
Factory method to generate a bit-array of length N where the elements are all 1.
Definition BitArray.h:187
constexpr auto copy(Src src)
Copies all the bits from any unsigned integral src value to this equal-sized bit-array....
Definition BitArray.h:645
static constexpr BitArray from(std::invocable< usize > auto f)
Factory method to construct a bit-array by repeatedly calling f(i) for i in [0, N).
Definition BitArray.h:317
constexpr std::optional< usize > last_set() const
Returns the index of the last set bit in the bit-array or {} if no bits are set.
Definition BitArray.h:844
constexpr std::optional< usize > previous_set(usize index) const
Returns the index of the previous set bit before index in the bit-array or {} if there are none.
Definition BitArray.h:872
constexpr bool any() const
Returns true if at least one bit in the bit-array is set, false otherwise.
Definition BitArray.h:558
static BitArray biased_random(double p)
Factory method to generate a bit-array of size N where the elements are from independent fair coin fl...
Definition BitArray.h:379
constexpr std::optional< usize > first_set() const
Returns the index of the first set bit in the bit-array or {} if no bits are set.
Definition BitArray.h:829
constexpr void clean()
Sets any unused bits in the last occupied word to 0.
Definition BitArray.h:232
constexpr usize size() const
Returns the number of bit-elements in the bit-array.
Definition BitArray.h:46
constexpr u8 offset() const
Returns the offset (in bits) of the first bit in the bit-array within the first word.
Definition BitArray.h:133
std::string to_string(std::string_view sep="", std::string_view pre="", std::string_view post="") const
Returns a binary string representation of the bit-array.
Definition BitArray.h:1244
constexpr void to_words(std::output_iterator< word_type > auto out)
Returns a copy of the words underlying this bit-array and puts them into the passed output iterator.
Definition BitArray.h:1043
Word word_type
The underlying unsigned word type used to store the bits.
Definition BitArray.h:31
constexpr auto swap(usize i0, usize i1)
Swaps the bits in the bit-array at indices i0 and i1 and returns this for chaining.
Definition BitArray.h:527
constexpr auto split_at(usize at) const
Views the bit-array as two parts containing the elements [0, at) and [at, size()) respectively.
Definition BitArray.h:1167
constexpr bool none() const
Returns true if no bits in the bit-array are set, false otherwise.
Definition BitArray.h:586
constexpr auto bits()
Returns a non-const iterator over the values of the bits in the mutable bit-array.
Definition BitArray.h:977
constexpr auto riffled() const
Returns a new bit-vector that is the result of riffling the bits in this bit-array with zeros.
Definition BitArray.h:1201
static constexpr BitArray from(Iter src_begin, Iter src_end)
Factory method to construct a bit-array by copying all the bits from an iteration of any Unsigneds.
Definition BitArray.h:280
constexpr void copy(Iter src_begin, Iter src_end)
Copies all the bits from an iteration of any unsigned integral src values to this equal-sized bit-arr...
Definition BitArray.h:670
constexpr const Word * store() const
Returns a const pointer to the underlying store of words .
Definition BitArray.h:115
constexpr bool get(usize i) const
Returns true if the bit at the given index i is set, false otherwise.
Definition BitArray.h:397
constexpr auto set_all(bool value=true)
Sets all the bits in the bit-array to the boolean value and returns a reference to this for chaining.
Definition BitArray.h:602
constexpr auto copy(Src const &src)
Copies all the bits from any src bit-store to this equal-sized bit-array and returns a reference to t...
Definition BitArray.h:693
constexpr std::optional< usize > last_unset() const
Returns the index of the last unset bit in the bit-array or {} if no bits are unset.
Definition BitArray.h:910
constexpr auto flip_all()
Flips the value of the bits in the bit-array and returns a reference to this for chaining.
Definition BitArray.h:615
constexpr Word * store()
Returns a pointer to the underlying store of words.
Definition BitArray.h:128
static BitArray seeded_random(u64 seed)
Factory method to generate a bit-array of size N where the elements are from independent fair coin fl...
Definition BitArray.h:366
constexpr std::optional< usize > next_unset(usize index) const
Returns the index of the next unset bit after index in the bit-array or {} if no more unset bits exis...
Definition BitArray.h:926
static BitArray random(double p=0.5, u64 seed=0)
Factory method to generate a bit-array of size N where the elements are picked at random.
Definition BitArray.h:344
constexpr void split_at(usize at, BitVector< word_type > &left, BitVector< word_type > &right) const
Views the bit-array as two parts containing the elements [0, at) and [at, size()) respectively.
Definition BitArray.h:1144
static constexpr BitArray alternating()
Factory method to generate a bit-array of length N looking like 101010....
Definition BitArray.h:223
constexpr bool is_empty() const
Returns true if the bit-array is empty, false otherwise.
Definition BitArray.h:545
constexpr BitArray()
Constructs a bit-array of length N with all the bit elements set to 0.
Definition BitArray.h:148
constexpr auto bits() const
Returns a const iterator over the bool values of the bits in the const bit-array.
Definition BitArray.h:961
constexpr void set_word(usize i, Word word)
Sets word i in the bit-array's underlying word store to value (masked if necessary).
Definition BitArray.h:98
constexpr usize count_ones() const
Returns the number of set bits in the bit-array.
Definition BitArray.h:772
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 bit-array.
Definition BitArray.h:1223
std::string describe() const
Returns a multi-line string describing the bit-array in some detail.
Definition BitArray.h:1299
constexpr usize words() const
Returns the number of words in the bit-array's underlying word store.
Definition BitArray.h:59
A dynamically-sized vector over GF(2) with bit elements compactly stored in a standard vector of prim...
Definition BitVector.h:23
The namespace for the gf2 library.
Definition assert.h:119
constexpr void set(Store &store, usize i, bool value=true)
Sets the bit-element at the given index to the specified boolean value (default value is true).
Definition BitStore.h:254
constexpr void set_all(Store &store, bool value=true)
Sets the bits in the store to the boolean value.
Definition BitStore.h:439
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:974
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:841
constexpr bool is_empty(Store const &store)
Returns true if the store is empty, false otherwise.
Definition BitStore.h:345
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:1039
constexpr void swap(Store &store, usize i0, usize i1)
Swaps the bits in the bit-store at indices i0 and i1.
Definition BitStore.h:307
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:1588
constexpr auto ref(Store &store, usize i)
Returns a "reference" to the bit element i in the given bit-store.
Definition BitStore.h:197
constexpr usize count_zeros(Store const &store)
Returns the number of unset bits in the store.
Definition BitStore.h:762
constexpr void split(Store const &store, usize at, BitVector< typename Store::word_type > &left, BitVector< typename Store::word_type > &right)
Views a bit-store as two parts containing the elements [0, at) and [at, size()) respectively....
Definition BitStore.h:1387
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:168
constexpr auto set_bits(Store const &store)
Returns an iterator over the indices of any set bits in the bit-store.
Definition BitStore.h:1170
constexpr bool back(Store const &store)
Returns true if the final bit element is set, false otherwise.
Definition BitStore.h:235
static std::string to_pretty_string(Store const &store)
Returns a "pretty" string representation of a store.
Definition BitStore.h:1606
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:928
constexpr auto store_words(Store const &store)
Returns a const iterator over all the words underlying the bit-store.
Definition BitStore.h:1211
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:890
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:385
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:1356
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:1277
constexpr void flip_all(Store &store)
Flips the value of the bits in the store.
Definition BitStore.h:455
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:1133
constexpr auto unset_bits(Store const &store)
Returns an iterator over the indices of any unset bits in the bit-store.
Definition BitStore.h:1187
constexpr void flip(Store &store, usize i)
Flips the value of the bit-element at the given index.
Definition BitStore.h:279
static std::string to_hex_string(Store const &store)
Returns the "hex" string representation of the bits in the bit-store.
Definition BitStore.h:1642
constexpr usize count_ones(Store const &store)
Returns the number of set bits in the store.
Definition BitStore.h:745
constexpr usize leading_zeros(Store const &store)
Returns the number of leading zeros in the store.
Definition BitStore.h:779
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:1084
constexpr void copy(Src src, Store &store)
Copies all the bits from any unsigned integral src value to an equal-sized bit-store.
Definition BitStore.h:485
constexpr bool front(Store const &store)
Returns true if the first bit element is set, false otherwise.
Definition BitStore.h:216
constexpr void to_words(Store const &store, std::output_iterator< typename Store::word_type > auto out)
Returns a copy of the words underlying this bit-store and puts them into the passed output iterator.
Definition BitStore.h:1235
constexpr usize trailing_zeros(Store const &store)
Returns the number of trailing zeros in the store.
Definition BitStore.h:800
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:419
constexpr void riffle(Store const &store, BitVector< 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:1468
constexpr void fill_random(Store &store, double p=0.5, u64 seed=0)
Fill the store with random bits based on an optional probability p and an optional seed for the RNG.
Definition BitStore.h:697
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:363
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:1007
static std::string describe(Store const &store)
Detailed Description of a Bit-Store.
Definition BitStore.h:1685
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:1537
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:866