GF2++
Loading...
Searching...
No Matches
BitVec.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
14#include <charconv>
15
16namespace gf2 {
17
22template<Unsigned Word = usize>
23class BitVec {
24private:
25 // The number of bit elements in the bit-vector.
26 usize m_size;
27
28 // The bit elements are packed compactly into this standard vector of unsigned words
29 std::vector<Word> m_store;
30
31public:
33 using word_type = Word;
34
36 static constexpr u8 bits_per_word = BITS<Word>;
37
40
50 constexpr usize size() const { return m_size; }
51
63 constexpr usize words() const { return m_store.size(); }
64
79 constexpr Word word(usize i) const {
80 gf2_debug_assert(i < words(), "Index {} is too large for a bit-vector with {} words.", i, words());
81 return m_store[i];
82 }
83
98 constexpr void set_word(usize i, Word word) {
99 gf2_debug_assert(i < words(), "Index {} is too large for a bit-vector 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
114 constexpr const Word* store() const { return m_store.data(); }
115
126 constexpr Word* store() { return m_store.data(); }
127
131 constexpr u8 offset() const { return 0; }
132
136
148 explicit constexpr BitVec(usize len = 0) : m_size(len), m_store(gf2::words_needed<Word>(len)) {
149 // Empty body -- we now have an underlying vector of words all initialized to 0.
150 // Note: We avoided using uniform initialization on the `std::vector` data member.
151 }
152
164 explicit constexpr BitVec(usize len, Word word) : m_size(len), m_store(gf2::words_needed<Word>(len), word) {
165 // Make sure any excess bits are set to 0.
166 clean();
167 }
168
172
181 static constexpr BitVec with_capacity(usize capacity) {
182 auto result = BitVec<Word>{};
183 result.m_store.reserve(gf2::words_needed<Word>(capacity));
184 return result;
185 }
186
194 static constexpr BitVec zeros(usize n) { return BitVec{n}; }
195
202 static constexpr BitVec ones(usize n) { return BitVec{n, MAX<Word>}; }
203
213 static constexpr BitVec constant(usize n, bool value) { return BitVec{n, value ? MAX<Word> : Word{0}}; }
214
224 static constexpr BitVec unit(usize n, usize i) {
225 gf2_assert(i < n, "Unit axis i = {} should be less than the bit-vector size n = {}", i, n);
226 BitVec result{n};
227 result.set(i);
228 return result;
229 }
230
237 static constexpr BitVec alternating(usize n) { return BitVec{n, gf2::ALTERNATING<Word>}; }
238
256 template<Unsigned Src>
257 static constexpr BitVec from(Src src) {
258 BitVec result{gf2::BITS<Src>};
259 result.copy(src);
260 return result;
261 }
262
282 template<BitStore Src>
283 static constexpr BitVec from(Src const& src) {
284 BitVec result{src.size()};
285 result.copy(src);
286 return result;
287 }
288
299 template<usize N>
300 static constexpr BitVec from(std::bitset<N> const& src) {
301 BitVec result{N};
302 result.copy(src);
303 return result;
304 }
305
317 static constexpr BitVec from(usize len, std::invocable<usize> auto f) {
318 BitVec result{len};
319 result.copy(f);
320 return result;
321 }
322
326
345 static BitVec random(usize len, double p = 0.5, u64 seed = 0) {
346 BitVec result{len};
347 result.random_fill(p, seed);
348 return result;
349 }
350
368 static BitVec seeded_random(usize len, u64 seed) { return random(len, 0.5, seed); }
369
382 static BitVec biased_random(usize len, double p) { return random(len, p, 0); }
383
387
408 static std::optional<BitVec> from_string(std::string_view sv) {
409 // Edge case ...
410 if (sv.empty()) return BitVec<Word>{};
411
412 // Remove any whitespace, commas, single quotes, or underscores characters.
413 std::string s{sv};
414 std::erase_if(s, [](char c) { return c == ' ' || c == ',' || c == '\'' || c == '_'; });
415 bool no_punctuation = true;
416
417 // Check for a binary prefix.
418 if (s.starts_with("0b")) return from_binary_string(s, no_punctuation);
419
420 // Check for a hex prefix.
421 if (s.starts_with("0x") || s.starts_with("0X")) return from_hex_string(s, no_punctuation);
422
423 // No prefix, but perhaps the string only contains '0' and '1' characters.
424 if (std::ranges::all_of(s, [](char c) { return c == '0' || c == '1'; })) {
425 return BitVec::from_binary_string(s, no_punctuation);
426 }
427
428 // Last gasp -- try hex ...
429 return BitVec::from_hex_string(s, no_punctuation);
430 }
431
445 static std::optional<BitVec> from_binary_string(std::string_view sv, bool no_punctuation = false) {
446 // Edge case ...
447 if (sv.empty()) return BitVec<Word>{};
448
449 // Convert to a string & remove the "0b" prefix if it exists.
450 std::string s{sv};
451 if (s.starts_with("0b")) s = s.substr(2);
452
453 // If necessary, also remove any whitespace, commas, single quotes, or underscores.
454 if (!no_punctuation) std::erase_if(s, [](char c) { return c == ' ' || c == ',' || c == '\'' || c == '_'; });
455
456 // The string should now be a sequence of 0's and 1's.
457 if (std::ranges::any_of(s, [](char c) { return c != '0' && c != '1'; })) return std::nullopt;
458
459 // Construct the bit-vector.
460 auto result = BitVec::zeros(s.size());
461 for (auto i = 0uz; i < s.size(); ++i)
462 if (s[i] == '1') result.set(i);
463 return result;
464 }
465
486 static std::optional<BitVec> from_hex_string(std::string_view sv, bool no_punctuation = false) {
487 // Edge case ...
488 if (sv.empty()) return BitVec<Word>{};
489
490 // Convert to a string and remove the optional "0x" prefix if it exists.
491 std::string s{sv};
492 if (s.starts_with("0x") || s.starts_with("0X")) s = s.substr(2);
493
494 // By default, the base of the last digit is 16 just like all the others.
495 // However, there may be a suffix of the form ".base" where `base` is one of 2, 4 or 8.
496 int last_digit_base = 16;
497 if (s.ends_with(".2"))
498 last_digit_base = 2;
499 else if (s.ends_with(".4"))
500 last_digit_base = 4;
501 else if (s.ends_with(".8"))
502 last_digit_base = 8;
503
504 // Remove the suffix if it exists.
505 if (last_digit_base != 16) s = s.substr(0, s.size() - 2);
506
507 // If necessary, also remove any whitespace, commas, single quotes, or underscores.
508 if (!no_punctuation) std::erase_if(s, [](char c) { return c == ' ' || c == ',' || c == '_'; });
509
510 // The string should now be a sequence of 0-9, A-F, a-f.
511 if (std::ranges::any_of(s, [](char c) { return !std::isxdigit(c); })) return std::nullopt;
512
513 // Construct the bit-vector where we allow all the characters to be hex digits.
514 auto result = BitVec::with_capacity(s.size() * 4);
515
516 // Push all but the last character -- those are hex digits for sure.
517 for (auto i = 0uz; i < s.size() - 1; ++i) result.append_hex_digit(s[i]);
518
519 // Push the last character -- it may be a hex digit or a base indicator.
520 result.append_digit(s[s.size() - 1], last_digit_base);
521
522 return result;
523 }
524
528
541 constexpr usize capacity() const { return bits_per_word * m_store.capacity(); }
542
554 constexpr BitVec& shrink_to_fit() {
556 m_store.shrink_to_fit();
557 return *this;
558 }
559
563 constexpr BitVec& clear() {
564 m_store.clear();
565 m_size = 0;
566 return *this;
567 }
568
582 constexpr BitVec& resize(usize n) {
583 if (n != size()) {
584 m_store.resize(gf2::words_needed<Word>(n), 0);
585 auto old_size = size();
586 m_size = n;
587
588 // If we truncated, we need to clean up the last occupied word if necessary.
589 if (n < old_size) clean();
590 }
591 return *this;
592 }
593
597 constexpr void clean() {
598 // NOTE: This works fine even if `size() == 0`.
599 auto shift = m_size % bits_per_word;
600 if (shift != 0) m_store[words() - 1] &= Word(~(MAX<Word> << shift));
601 }
602
606
619 constexpr BitVec& push(bool b) {
620 resize(size() + 1);
621 if (b) this->set(size() - 1, true);
622 return *this;
623 }
624
642 constexpr std::optional<bool> pop() {
643 if (m_size == 0) return std::nullopt;
644 bool b = this->back();
645 resize(m_size - 1);
646 return b;
647 }
648
652
667 template<Unsigned Src>
668 auto append(Src src) {
669 auto old_size = size();
670 resize(old_size + BITS<Src>);
671 this->span(old_size, size()).copy(src);
672 return *this;
673 }
674
689 template<BitStore Src>
690 constexpr BitVec& append(Src const& src) {
691 auto old_size = size();
692 resize(old_size + src.size());
693 this->span(old_size, size()).copy(src);
694 return *this;
695 }
696
706 template<usize N>
707 auto append(std::bitset<N> const& src) {
708 auto old_size = size();
709 resize(old_size + src.size());
710 this->span(old_size, size()).copy(src);
711 return *this;
712 }
713
734 constexpr BitVec& append_digit(char c, int base) {
735 // The known bases are 2, 4, 8, and 16.
736 static constexpr std::array<int, 4> known_bases = {2, 4, 8, 16};
737 if (std::ranges::contains(known_bases, base)) {
738 // Try to convert the character to an integer.
739 usize x;
740 if (std::from_chars(&c, &c + 1, x, base).ec == std::errc{}) {
741 // Success! Push the digits onto the bit-vector.
742 auto digits = static_cast<usize>(std::log2(base));
743 auto old_len = size();
744 resize(old_len + digits);
745 for (auto i = 0uz; i < digits; ++i) this->set(old_len + i, (x >> (digits - i - 1)) & 1);
746 }
747 }
748 return *this;
749 }
750
768 constexpr BitVec& append_hex_digit(char c) {
769 // Try to convert the hex digit to an integer.
770 int x;
771 if (std::from_chars(&c, &c + 1, x, 16).ec == std::errc{}) {
772 // Success! Push the digits onto the bit-vector.
773 auto old_len = size();
774 resize(old_len + 4);
775 for (auto i = 0uz; i < 4; ++i) this->set(old_len + i, (x >> (3 - i)) & 1);
776 }
777 return *this;
778 }
779
783
799 BitVec result;
800 split_off(at, result);
801 return result;
802 }
803
819 constexpr void split_off(usize at, BitVec<Word>& dst) {
820 gf2_assert(at <= m_size, "split point {} is beyond the end of the bit-vector", at);
821 dst.clear();
822 dst.append(this->span(at, m_size));
823 resize(at);
824 }
825
849 template<Unsigned Dst = Word>
850 constexpr std::optional<Dst> split_off_unsigned() {
851 // Edge case ...
852 if (size() == 0) return std::nullopt;
853
854 // Perhaps we can safely cast an `Word` into a `Dst` without any loss of information.
855 constexpr usize bits_per_dst = BITS<Dst>;
856 if constexpr (bits_per_dst <= bits_per_word) {
857 // Easy cases when we can safely cast an `Word` into a `Dst` without any loss of information.
858 // 1. Just one word in the store & we know that all unused bits in that word are zeros.
859 // 2. More than one word in the store but the last word is fully occupied.
860 auto n_words = words();
861 auto shift = size() % bits_per_word;
862 if (n_words == 1 || shift == 0) {
863 auto result = static_cast<Dst>(m_store[n_words - 1]);
864 resize(size() - bits_per_dst);
865 return result;
866 }
867
868 // The last word is not fully occupied so we need to combine it with the next to last word to form a
869 // value that can be cast to a `Dst`.
870 auto lo = m_store[n_words - 2] >> shift;
871 auto hi = m_store[n_words - 1] << (bits_per_word - shift);
872 auto result = static_cast<Dst>(lo | hi);
873 resize(size() - bits_per_dst);
874 return result;
875 } else {
876 // `Dst` is larger than `Word` -- it should be an integer multiple of `bits_per_word` is size.
877 static_assert(bits_per_dst % bits_per_word == 0, "sizeof(Dst) % sizeof(Word) != 0");
878
879 // Pop an appropriate number of `Word` sized chunks off the end of the bit-vector.
880 auto words_per_dst = bits_per_dst / bits_per_word;
881 std::vector<Word> chunks(words_per_dst);
882 for (auto i = 0uz; i < words_per_dst; ++i) chunks[i] = *split_off_unsigned<Word>();
883
884 // Combine the chunks into a `Dst` value.
885 Dst result = 0;
886 for (auto i = words_per_dst; i-- > 0;) result |= static_cast<Dst>(chunks[i] << (i * bits_per_word));
887
888 return result;
889 }
890 }
891
895
907 constexpr bool get(usize i) const { return gf2::get(*this, i); }
908
921 constexpr bool operator[](usize i) const { return gf2::get(*this, i); }
922
934 constexpr bool front() const { return gf2::front(*this); }
935
947 constexpr bool back() const { return gf2::back(*this); }
948
952
965 auto set(usize i, bool value = true) { return gf2::set(*this, i, value); }
966
985 constexpr auto operator[](usize i) { return gf2::ref(*this, i); }
986
1001 auto flip(usize i) { return gf2::flip(*this, i); }
1002
1021 constexpr auto swap(usize i0, usize i1) { return gf2::swap(*this, i0, i1); }
1022
1026
1036 constexpr bool is_empty() const { return gf2::is_empty(*this); }
1037
1049 constexpr bool any() const { return gf2::any(*this); }
1050
1064 constexpr bool all() const { return gf2::all(*this); }
1065
1077 constexpr bool none() const { return gf2::none(*this); }
1078
1082
1093 auto set_all(bool value = true) { return gf2::set_all(*this, value); }
1094
1103 auto flip_all() { return gf2::flip_all(*this); }
1104
1108
1126 template<Unsigned Src>
1127 auto copy(Src src) {
1128 return gf2::copy(src, *this);
1129 }
1130
1144 template<BitStore Src>
1145 auto copy(Src const& src) {
1146 return gf2::copy(src, *this);
1147 }
1148
1160 template<usize N>
1161 auto copy(std::bitset<N> const& src) {
1162 return gf2::copy(src, *this);
1163 }
1164
1168
1178 auto copy(std::invocable<usize> auto f) { return gf2::copy(*this, f); }
1179
1197 auto random_fill(double p = 0.5, u64 seed = 0) { return gf2::random_fill(*this, p, seed); }
1198
1202
1212 constexpr usize count_ones() const { return gf2::count_ones(*this); }
1213
1223 constexpr usize count_zeros() const { return gf2::count_zeros(*this); }
1224
1236 constexpr usize leading_zeros() const { return gf2::leading_zeros(*this); }
1237
1247 constexpr usize trailing_zeros() const { return gf2::trailing_zeros(*this); }
1248
1252
1268 constexpr std::optional<usize> first_set() const { return gf2::first_set(*this); }
1269
1283 constexpr std::optional<usize> last_set() const { return gf2::last_set(*this); }
1284
1297 constexpr std::optional<usize> next_set(usize index) const { return gf2::next_set(*this, index); }
1298
1311 constexpr std::optional<usize> previous_set(usize index) const { return gf2::previous_set(*this, index); }
1312
1316
1332 constexpr std::optional<usize> first_unset() const { return gf2::first_unset(*this); }
1333
1349 constexpr std::optional<usize> last_unset() const { return gf2::last_unset(*this); }
1350
1365 constexpr std::optional<usize> next_unset(usize index) const { return gf2::next_unset(*this, index); }
1366
1381 constexpr std::optional<usize> previous_unset(usize index) const { return gf2::previous_unset(*this, index); }
1382
1386
1399 constexpr auto bits() const { return gf2::bits(*this); }
1400
1414 constexpr auto bits() { return gf2::bits(*this); }
1415
1427 constexpr auto set_bits() const { return gf2::set_bits(*this); }
1428
1440 constexpr auto unset_bits() const { return gf2::unset_bits(*this); }
1441
1459 constexpr auto store_words() const { return gf2::store_words(*this); }
1460
1471 constexpr auto to_words() const { return gf2::to_words(*this); }
1472
1476
1489 constexpr auto span(usize begin, usize end) const { return gf2::span(*this, begin, end); }
1490
1506 constexpr auto span(usize begin, usize end) { return gf2::span(*this, begin, end); }
1507
1511
1525 constexpr auto sub(usize begin, usize end) const { return gf2::sub(*this, begin, end); }
1526
1530
1552 constexpr void split_at(usize at, BitVec<word_type>& left, BitVec<word_type>& right) const {
1553 return gf2::split(*this, at, left, right);
1554 }
1555
1573 constexpr auto split_at(usize at) const { return gf2::split(*this, at); }
1574
1578
1593 constexpr void riffled(BitVec<word_type>& dst) const { return gf2::riffle(*this, dst); }
1594
1607 constexpr auto riffled() const { return gf2::riffle(*this); }
1608
1612
1629 std::string to_binary_string(std::string_view sep = "", std::string_view pre = "",
1630 std::string_view post = "") const {
1631 return gf2::to_binary_string(*this, sep, pre, post);
1632 }
1633
1650 std::string to_string(std::string_view sep = "", std::string_view pre = "", std::string_view post = "") const {
1651 return gf2::to_string(*this, sep, pre, post);
1652 }
1653
1666 std::string to_pretty_string() const { return gf2::to_pretty_string(*this); }
1667
1698 std::string to_hex_string() const { return gf2::to_hex_string(*this); }
1699
1703 std::string describe() const { return gf2::describe(*this); }
1704
1706};
1707
1708} // namespace gf2
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
std::string describe() const
Returns a multi-line string describing the bit-store in some detail.
Definition BitVec.h:1703
static std::optional< BitVec > from_hex_string(std::string_view sv, bool no_punctuation=false)
Factory method to construct a bit-vector from a hex string, returning std::nullopt on failure.
Definition BitVec.h:486
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 BitVec.h:1365
constexpr BitVec(usize len=0)
Constructs a bit-vector of length n with all the bit elements set to 0.
Definition BitVec.h:148
static BitVec biased_random(usize len, double p)
Factory method to generate a bit-vector of size len where the elements are from independent fair coin...
Definition BitVec.h:382
auto copy(Src src)
Copies the bits from an unsigned integral src value and returns a reference to this for chaining.
Definition BitVec.h:1127
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 BitVec.h:1489
constexpr void riffled(BitVec< word_type > &dst) const
Interleaves the bits of this bit-store with zeros storing the result into the bit-vector dst.
Definition BitVec.h:1593
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 BitVec.h:1093
static std::optional< BitVec > from_string(std::string_view sv)
Factory method to construct a bit-vector from a string s, returning std::nullopt on failure.
Definition BitVec.h:408
constexpr Word word(usize i) const
Returns word i from the bit-vector's underlying word store.
Definition BitVec.h:79
constexpr bool all() const
Returns true if all bits in the store are set, false otherwise.
Definition BitVec.h:1064
constexpr bool is_empty() const
Returns true if the store is empty, false otherwise.
Definition BitVec.h:1036
constexpr void clean()
Sets any unused bits in the last occupied word to 0.
Definition BitVec.h:597
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 BitVec.h:1650
constexpr usize count_zeros() const
Returns the number of unset bits in the store.
Definition BitVec.h:1223
constexpr bool operator[](usize i) const
Returns the boolean value of the bit element i.
Definition BitVec.h:921
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 BitVec.h:1381
constexpr usize trailing_zeros() const
Returns the number of trailing zeros in the store.
Definition BitVec.h:1247
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 BitVec.h:1197
constexpr BitVec & append_digit(char c, int base)
Appends a single character c onto the end of bit-vector and returns this for chaining.
Definition BitVec.h:734
auto copy(std::invocable< usize > auto f)
Fills the store by repeatedly calling f(i) and returns a reference to this for chaining.
Definition BitVec.h:1178
constexpr usize size() const
Returns the number of bit-elements in the bit-vector.
Definition BitVec.h:50
constexpr auto bits() const
Returns a const iterator over the bool values of the bits in the const bit-store.
Definition BitVec.h:1399
constexpr BitVec & push(bool b)
Pushes a single bit b onto the bit-vector.
Definition BitVec.h:619
constexpr auto store_words() const
Returns a const iterator over all the words underlying the bit-store.
Definition BitVec.h:1459
constexpr u8 offset() const
Returns the offset (in bits) of the first bit in the store within the first word.
Definition BitVec.h:131
static constexpr BitVec from(Src const &src)
Factory method to construct a bit-vector by copying all the bits from any bit-store instance.
Definition BitVec.h:283
constexpr void split_off(usize at, BitVec< Word > &dst)
Splits a bit-vector into two at the given index, returning the second part in dst.
Definition BitVec.h:819
constexpr usize capacity() const
Definition BitVec.h:541
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 BitVec.h:1525
constexpr BitVec(usize len, Word word)
Constructs a bit-vector with len elements by repeatedly copying all the bits from word.
Definition BitVec.h:164
constexpr auto bits()
Returns a non-const iterator over the values of the bits in the mutable bit-store.
Definition BitVec.h:1414
static constexpr BitVec from(usize len, std::invocable< usize > auto f)
Factory method to construct a bit-vector by repeatedly calling f(i) for i in [0, len).
Definition BitVec.h:317
static constexpr BitVec constant(usize n, bool value)
Factory method to generate a bit-vector of length n where the elements are set to value.
Definition BitVec.h:213
constexpr auto split_at(usize at) const
Views a bit-store as two parts containing the elements [0, at) and [at, size()) respectively.
Definition BitVec.h:1573
std::string to_hex_string() const
Returns the "hex" string representation of the bits in the bit-store.
Definition BitVec.h:1698
constexpr std::optional< bool > pop()
Removes the last bit from the bit-vector and returns it or std::nullopt if the bit-vector is empty.
Definition BitVec.h:642
constexpr Word * store()
Returns a pointer to the underlying store of words.
Definition BitVec.h:126
static constexpr BitVec from(std::bitset< N > const &src)
Factory method to construct a bit-vector from the bits of a std::bitset.
Definition BitVec.h:300
auto copy(Src const &src)
Copies the bits from an equal-sized src store and returns a reference to this for chaining.
Definition BitVec.h:1145
std::string to_pretty_string() const
Returns a "pretty" string representation of the store.
Definition BitVec.h:1666
constexpr bool any() const
Returns true if at least one bit in the store is set, false otherwise.
Definition BitVec.h:1049
constexpr BitVec & shrink_to_fit()
Shrinks the bit-vector's capacity as much as possible.
Definition BitVec.h:554
constexpr usize words() const
Returns the number of words in the bit-vector's underlying word store.
Definition BitVec.h:63
static constexpr BitVec with_capacity(usize capacity)
Factory method to construct an empty bit-vector with at least the specified capacity.
Definition BitVec.h:181
constexpr auto to_words() const
Returns a copy of the words underlying this bit-store.
Definition BitVec.h:1471
constexpr auto riffled() const
Returns a new bit-vector that is the result of riffling the bits in this bit-store with zeros.
Definition BitVec.h:1607
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 BitVec.h:1161
auto set(usize i, bool value=true)
Sets the bit-element i to the specified boolean value & returns this for chaining....
Definition BitVec.h:965
static constexpr BitVec zeros(usize n)
Factory method to generate a bit-vector of length n where the elements are all 0.
Definition BitVec.h:194
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 BitVec.h:1349
constexpr BitVec< Word > split_off(usize at)
Splits a bit-vector into two at the given index, returning a new BitVec.
Definition BitVec.h:798
static constexpr BitVec alternating(usize n)
Factory method to generate a bit-vector of length n looking like 101010....
Definition BitVec.h:237
constexpr usize count_ones() const
Returns the number of set bits in the store.
Definition BitVec.h:1212
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 BitVec.h:1332
constexpr auto set_bits() const
Returns an iterator over the indices of any set bits in the bit-store.
Definition BitVec.h:1427
constexpr bool back() const
Returns true if the last bit element is set, false otherwise.
Definition BitVec.h:947
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 BitVec.h:1552
static BitVec random(usize len, double p=0.5, u64 seed=0)
Factory method to generate a bit-vector of size len where the elements are picked at random.
Definition BitVec.h:345
constexpr BitVec & resize(usize n)
Resize the bit-vector so that its size() is n.
Definition BitVec.h:582
constexpr bool none() const
Returns true if no bits in the store are set, false otherwise.
Definition BitVec.h:1077
static constexpr BitVec ones(usize n)
Factory method to generate a bit-vector of length n where the elements are all 1.
Definition BitVec.h:202
constexpr const Word * store() const
Returns a const pointer to the underlying store of words .
Definition BitVec.h:114
static BitVec seeded_random(usize len, u64 seed)
Factory method to generate a bit-vector of size len where the elements are from independent fair coin...
Definition BitVec.h:368
constexpr BitVec & clear()
Removes all elements from the bit-vector so size()==0.
Definition BitVec.h:563
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 BitVec.h:1268
auto flip(usize i)
Flips the value of the bit-element i and returns this for chaining.
Definition BitVec.h:1001
constexpr bool get(usize i) const
Returns true if the bit at the given index i is set, false otherwise.
Definition BitVec.h:907
static std::optional< BitVec > from_binary_string(std::string_view sv, bool no_punctuation=false)
Factory method to construct a bit-vector from a binary string, returning std::nullopt on failure.
Definition BitVec.h:445
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 BitVec.h:1311
constexpr void set_word(usize i, Word word)
Sets word i in the bit-vector's underlying word store to value (masked if necessary).
Definition BitVec.h:98
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 BitVec.h:1297
constexpr bool front() const
Returns true if the first bit element is set, false otherwise.
Definition BitVec.h:934
constexpr auto operator[](usize i)
Returns a "reference" to the bit element i.
Definition BitVec.h:985
auto append(std::bitset< N > const &src)
Appends all the bits from a std::bitset onto the end of the bit-vector and returns this for chaining.
Definition BitVec.h:707
Word word_type
Definition BitVec.h:33
constexpr std::optional< Dst > split_off_unsigned()
Split off a single arbitrary sized unsigned integer off the end of the bit-vector and returns it or s...
Definition BitVec.h:850
constexpr BitVec & append_hex_digit(char c)
Appends a single hex digit character c onto the end of bit-vector and returns this for chaining.
Definition BitVec.h:768
static constexpr BitVec from(Src src)
Factory method to construct a bit-vector by copying all the bits from any Unsigned instance....
Definition BitVec.h:257
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 BitVec.h:1021
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 BitVec.h:1283
constexpr BitVec & append(Src const &src)
Appends all the bits from any BitStore src onto the end of the bit-vector and returns this for chaini...
Definition BitVec.h:690
static constexpr u8 bits_per_word
Definition BitVec.h:36
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 BitVec.h:1629
constexpr auto span(usize begin, usize end)
Returns a mutable bit-span encompassing the bits in the half-open range [begin, end).
Definition BitVec.h:1506
static constexpr BitVec unit(usize n, usize i)
Factory method to generate a "unit" bit-vector of length n where only element i is set.
Definition BitVec.h:224
auto append(Src src)
Appends all the bits from any unsigned integral src value and returns a reference to this for chainin...
Definition BitVec.h:668
constexpr usize leading_zeros() const
Returns the number of leading zeros in the store.
Definition BitVec.h:1236
auto flip_all()
Flips the value of the bits in the store and returns a reference to this for chaining.
Definition BitVec.h:1103
constexpr auto unset_bits() const
Returns an iterator over the indices of any unset bits in the bit-store.
Definition BitVec.h:1440
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
constexpr usize words_needed(usize n_bits)
Returns the number of Unsigneds needed to store n_bits bits.
Definition Unsigned.h:523
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