GF2++
Loading...
Searching...
No Matches
gf2::BitArray< N, Word >

A fixed-size "vector" over GF(2) with N bit elements compactly stored in a standard array of primitive unsigned words whose type is given by the template parameter Word. The elements in a bit-array are initially all set to 0. More...

#include <BitArray.h>

Public Types

using word_type = Word
 The underlying unsigned word type used to store the bits.

Public Member Functions

Required BitStore Concept Methods.
constexpr usize size () const
 Returns the number of bit-elements in the bit-array.
constexpr usize words () const
 Returns the number of words in the bit-array's underlying word store.
constexpr Word word (usize i) const
 Returns word i from the bit-array's underlying word store.
constexpr void set_word (usize i, Word word)
 Sets word i in the bit-array's underlying word store to value (masked if necessary).
constexpr const Word * store () const
 Returns a const pointer to the underlying store of words .
constexpr Word * store ()
 Returns a pointer to the underlying store of words.
constexpr u8 offset () const
 Returns the offset (in bits) of the first bit in the bit-array within the first word.
Constructors:
constexpr BitArray ()
 Constructs a bit-array of length N with all the bit elements set to 0.
constexpr BitArray (Word word)
 Constructs a bit-array with N elements by repeatedly copying all the bits from word.
Accessors for Individual Bits:
constexpr bool get (usize i) const
 Returns true if the bit at the given index i is set, false otherwise.
constexpr bool operator[] (usize i) const
 Returns the boolean value of the bit element i.
constexpr bool front () const
 Returns true if the first bit element is set, false otherwise.
constexpr bool back () const
 Returns true if the last bit element is set, false otherwise.
Bit Mutators:
constexpr auto set (usize i, bool value=true)
 Sets the bit-element i to the specified boolean value & returns this for chaining. The default value for value is true.
constexpr auto operator[] (usize i)
 Returns a "reference" to the bit element i.
constexpr auto flip (usize i)
 Flips the value of the bit-element i and returns this for chaining.
constexpr auto swap (usize i0, usize i1)
 Swaps the bits in the bit-array at indices i0 and i1 and returns this for chaining.
BitArray Queries:
constexpr bool is_empty () const
 Returns true if the bit-array is empty, false otherwise.
constexpr bool any () const
 Returns true if at least one bit in the bit-array is set, false otherwise.
constexpr bool all () const
 Returns true if all bits in the bit-array are set, false otherwise.
constexpr bool none () const
 Returns true if no bits in the bit-array are set, false otherwise.
BitArray Whole Array Mutators:
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.
constexpr auto flip_all ()
 Flips the value of the bits in the bit-array and returns a reference to this for chaining.
Copying into the BitArray:
template<Unsigned Src>
constexpr auto copy (Src src)
 Copies all the bits from any unsigned integral src value to this equal-sized bit-array. Returns a reference to this for chaining.
template<typename Iter>
requires std::is_unsigned_v<typename std::iterator_traits<Iter>::value_type>
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-array.
template<BitStore Src>
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 this for chaining.
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 for chaining.
BitArray Fills:
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.
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.
Bit Counts:
constexpr usize count_ones () const
 Returns the number of set bits in the bit-array.
constexpr usize count_zeros () const
 Returns the number of unset bits in the bit-array.
constexpr usize leading_zeros () const
 Returns the number of leading zeros in the bit-array.
constexpr usize trailing_zeros () const
 Returns the number of trailing zeros in the bit-array.
Set-bit Indices:
constexpr std::optional< usizefirst_set () const
 Returns the index of the first set bit in the bit-array or {} if no bits are set.
constexpr std::optional< usizelast_set () const
 Returns the index of the last set bit in the bit-array or {} if no bits are set.
constexpr std::optional< usizenext_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.
constexpr std::optional< usizeprevious_set (usize index) const
 Returns the index of the previous set bit before index in the bit-array or {} if there are none.
Unset-bit Indices:
constexpr std::optional< usizefirst_unset () const
 Returns the index of the first unset bit in the bit-array or {} if no bits are unset.
constexpr std::optional< usizelast_unset () const
 Returns the index of the last unset bit in the bit-array or {} if no bits are unset.
constexpr std::optional< usizenext_unset (usize index) const
 Returns the index of the next unset bit after index in the bit-array or {} if no more unset bits exist.
constexpr std::optional< usizeprevious_unset (usize index) const
 Returns the index of the previous unset bit before index in the bit-array or {} if no more unset bits exist.
BitArray Iterators:
constexpr auto bits () const
 Returns a const iterator over the bool values of the bits in the const bit-array.
constexpr auto bits ()
 Returns a non-const iterator over the values of the bits in the mutable bit-array.
constexpr auto set_bits () const
 Returns an iterator over the indices of any set bits in the bit-array.
constexpr auto unset_bits () const
 Returns an iterator over the indices of any unset bits in the bit-array.
constexpr auto store_words () const
 Returns a const iterator over all the words underlying the v.
Exports:
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.
constexpr auto to_words () const
 Returns a copy of the words underlying this bit-array as a std::vector<word_type>.
Spans:
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, end).
constexpr auto span (usize begin, usize end)
 Returns a mutable bit-span encompassing the bit-array's bits in the half-open range [begin, end).
Sub-vectors:
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.
Splits:
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.
constexpr auto split_at (usize at) const
 Views the bit-array as two parts containing the elements [0, at) and [at, size()) respectively.
Riffling:
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.
constexpr auto riffled () const
 Returns a new bit-vector that is the result of riffling the bits in this bit-array with zeros.
String Representations:
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.
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.
std::string to_pretty_string () const
 Returns a "pretty" string representation of the bit-array.
std::string to_hex_string () const
 Returns the "hex" string representation of the bits in the bit-array.
std::string describe () const
 Returns a multi-line string describing the bit-array in some detail.

Static Public Member Functions

Factory Constructors:
static constexpr BitArray zeros ()
 Factory method to generate a bit-array of length N where the elements are all 0.
static constexpr BitArray ones ()
 Factory method to generate a bit-array of length N where the elements are all 1.
static constexpr BitArray constant (bool value)
 Factory method to generate a bit-array of length N where the elements are set to value.
static constexpr BitArray unit (usize i)
 Factory method to generate a "unit" bit-array of length N where only element i is set.
static constexpr BitArray alternating ()
 Factory method to generate a bit-array of length N looking like 101010....
Construct BitArrays with Random Fills:
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.
static BitArray seeded_random (u64 seed)
 Factory method to generate a bit-array of size N where the elements are from independent fair coin flips generated from an RNG seeded with the given seed.
static BitArray biased_random (double p)
 Factory method to generate a bit-array of size N where the elements are from independent fair coin flips and where each bit is 1 with probability p.

Static Public Attributes

static constexpr u8 bits_per_word = BITS<Word>
 The number of bits per Word.

Helper Method:

constexpr void clean ()
 Sets any unused bits in the last occupied word to 0.
template<Unsigned Src>
requires (BITS<Src> == N)
static constexpr BitArray from (Src src)
 Factory method to construct a bit-array by copying all the bits from any Unsigned instance.
template<typename Iter>
requires std::is_unsigned_v<typename std::iterator_traits<Iter>::value_type>
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.
static constexpr BitArray from (std::bitset< N > const &src)
 Factory method to construct a bit-array from the bits of a std::bitset.
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).

Detailed Description

template<usize N, Unsigned Word = usize>
class gf2::BitArray< N, Word >

A fixed-size "vector" over GF(2) with N bit elements compactly stored in a standard array of primitive unsigned words whose type is given by the template parameter Word. The elements in a bit-array are initially all set to 0.

BitArray<N> is similar to std::bitset<N> but has a richer set of functionality.

The BitArray class satisfies the BitStore concept.

Constructor & Destructor Documentation

◆ BitArray() [1/2]

template<usize N, Unsigned Word = usize>
gf2::BitArray< N, Word >::BitArray ( )
inlineexplicitconstexpr

Constructs a bit-array of length N with all the bit elements set to 0.

Example

assert_eq(u.to_string(), "");
assert_eq(v.to_string(), "0000000000");
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 BitArray()
Constructs a bit-array of length N with all the bit elements set to 0.
Definition BitArray.h:148

◆ BitArray() [2/2]

template<usize N, Unsigned Word = usize>
gf2::BitArray< N, Word >::BitArray ( Word word)
inlineexplicitconstexpr

Constructs a bit-array with N elements by repeatedly copying all the bits from word.

The final copy of word may be truncated and padded with zeros (unused bit slots are always set to zero).

Example

BitArray<10, u8> v{u8{0b0101'0101}};
assert_eq(v.to_string(), "1010101010");
std::uint8_t u8
Word type alias for an 8-bit unsigned integer.
Definition Unsigned.h:30

Member Function Documentation

◆ all()

template<usize N, Unsigned Word = usize>
bool gf2::BitArray< N, Word >::all ( ) const
inlineconstexpr

Returns true if all bits in the bit-array are set, false otherwise.

Note: Empty bit-arrays have no set bits (logical connective for all is AND with identity true).

Example

assert_eq(v.all(), false);
v.set(0);
v.set(1);
v.set(2);
assert_eq(v.all(), true);
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 bool all() const
Returns true if all bits in the bit-array are set, false otherwise.
Definition BitArray.h:573

◆ alternating()

template<usize N, Unsigned Word = usize>
constexpr BitArray gf2::BitArray< N, Word >::alternating ( )
inlinestaticconstexpr

Factory method to generate a bit-array of length N looking like 101010....

Example

assert_eq(v.to_string(), "1010101010");
static constexpr BitArray alternating()
Factory method to generate a bit-array of length N looking like 101010....
Definition BitArray.h:223

◆ any()

template<usize N, Unsigned Word = usize>
bool gf2::BitArray< N, Word >::any ( ) const
inlineconstexpr

Returns true if at least one bit in the bit-array is set, false otherwise.

Note: Empty bit-arrays have no set bits (logical connective for any is OR with identity false).

Example

assert_eq(v.any(), false);
v.set(0);
assert_eq(v.any(), true);
constexpr bool any() const
Returns true if at least one bit in the bit-array is set, false otherwise.
Definition BitArray.h:558

◆ back()

template<usize N, Unsigned Word = usize>
bool gf2::BitArray< N, Word >::back ( ) const
inlineconstexpr

Returns true if the last bit element is set, false otherwise.

Panics

In debug mode the method panics if the bit-array is empty.

Example

assert_eq(v.back(), false);
v.set_all();
assert_eq(v.back(), true);
constexpr bool back() const
Returns true if the last bit element is set, false otherwise.
Definition BitArray.h:440
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

◆ biased_random()

template<usize N, Unsigned Word = usize>
BitArray gf2::BitArray< N, Word >::biased_random ( double p)
inlinestatic

Factory method to generate a bit-array of size N where the elements are from independent fair coin flips and where each bit is 1 with probability p.

Parameters
pThe probability of the elements being 1.

Example

assert_eq(u.size(), v.size());
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

◆ bits() [1/2]

template<usize N, Unsigned Word = usize>
auto gf2::BitArray< N, Word >::bits ( )
inlineconstexpr

Returns a non-const iterator over the values of the bits in the mutable bit-array.

You can use this iterator to iterate over the bits in the store to get or set the value of each bit.

Note

For the most part, try to avoid iterating through individual bits. It is much more efficient to use methods that work on whole words of bits at a time.

Example

for (auto&& bit : v.bits()) bit = true;
assert_eq(v.to_string(), "1111111111");
constexpr auto bits() const
Returns a const iterator over the bool values of the bits in the const bit-array.
Definition BitArray.h:961

◆ bits() [2/2]

template<usize N, Unsigned Word = usize>
auto gf2::BitArray< N, Word >::bits ( ) const
inlineconstexpr

Returns a const iterator over the bool values of the bits in the const bit-array.

You can use this iterator to iterate over the bits in the store and get the values of each bit as a bool.

Note

For the most part, try to avoid iterating through individual bits. It is much more efficient to use methods that work on whole words of bits at a time.

Example

for (auto&& bit : v.bits()) assert_eq(bit, true);

◆ clean()

template<usize N, Unsigned Word = usize>
void gf2::BitArray< N, Word >::clean ( )
inlineconstexpr

Sets any unused bits in the last occupied word to 0.

You can use this helper method to enforce the guarantee that unused bits in the store are always set to 0.

◆ constant()

template<usize N, Unsigned Word = usize>
constexpr BitArray gf2::BitArray< N, Word >::constant ( bool value)
inlinestaticconstexpr

Factory method to generate a bit-array of length N where the elements are set to value.

Example

auto v = BitArray<10>::constant(true);
assert_eq(v.to_string(), "1111111111");
auto w = BitArray<10>::constant(false);
assert_eq(w.to_string(), "0000000000");
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

◆ copy() [1/5]

template<usize N, Unsigned Word = usize>
template<typename Iter>
requires std::is_unsigned_v<typename std::iterator_traits<Iter>::value_type>
void gf2::BitArray< N, Word >::copy ( Iter src_begin,
Iter src_end )
inlineconstexpr

Copies all the bits from an iteration of any unsigned integral src values to this equal-sized bit-array.

Note

We allow any unsigned integral source, e.g. copying u64 words into a BitArray<N,u8> of the correct size.

Panics

Panics if the size of this bit-array does not match the number of bits in the source iteration.

Example

std::vector<u16> src = { 0b1010101010101010, 0b1010101010101010, 0b1111111111111111 };
v.copy(src.begin(), src.end());
assert_eq(v.to_string(), "010101010101010101010101010101011111111111111111");
w.copy(src.begin(), src.end());
assert_eq(w.to_string(), "010101010101010101010101010101011111111111111111");
A fixed-size "vector" over GF(2) with N bit elements compactly stored in a standard array of primitiv...
Definition BitArray.h:24
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

◆ copy() [2/5]

template<usize N, Unsigned Word = usize>
template<BitStore Src>
auto gf2::BitArray< N, Word >::copy ( Src const & src)
inlineconstexpr

Copies all the bits from any src bit-store to this equal-sized bit-array and returns a reference to this for chaining.

Note

This is one of the few methods in the library that doesn't require the two stores to have the same word_type. You can use it to convert between different word_type stores (e.g., from BitArray<u32> to BitArray<u8>) as long as the sizes match.

Panics

Panics if the sizes of this bit-array and the src bit-store do not match.

Example

assert_eq(v.to_string(), "0000000000");
assert_eq(v.to_string(), "1010101010");
static constexpr BitVector alternating(usize n)
Factory method to generate a bit-vector of length n looking like 101010....
Definition BitVector.h:239

◆ copy() [3/5]

template<usize N, Unsigned Word = usize>
template<Unsigned Src>
auto gf2::BitArray< N, Word >::copy ( Src src)
inlineconstexpr

Copies all the bits from any unsigned integral src value to this equal-sized bit-array. Returns a reference to this for chaining.

Notes:

  1. We allow any unsigned integral source, e.g. copying a single u64 into a BitArray<u8> of size 64.
  2. The least-significant bit of the source becomes the bit at index 0 in the bit-array.

Panics

Panics if the size of the bit-array does not match the number of bits in the source integer type.

Example

u16 src = 0b1010101010101010;
v.copy(src);
assert_eq(v.to_string(), "0101010101010101");
w.copy(src);
assert_eq(w.to_string(), "0101010101010101");
std::uint16_t u16
Word type alias for a 16-bit unsigned integer.
Definition Unsigned.h:33

◆ copy() [4/5]

template<usize N, Unsigned Word = usize>
auto gf2::BitArray< N, Word >::copy ( std::bitset< N > const & src)
inlineconstexpr

Copies all the bits from a std::bitset to this equal-sized bit-array and returns a reference to this for chaining.

Note

A std::bitset prints its bit elements in bit-order which is the reverse of our convention.

Panics

Panics if the size of the bit-array does not match the number of bits in the source std::bitset.

Example

std::bitset<10> src{0b1010101010};
v.copy(src);
assert_eq(v.to_string(), "0101010101");

◆ copy() [5/5]

template<usize N, Unsigned Word = usize>
auto gf2::BitArray< N, Word >::copy ( std::invocable< usize > auto f)
inlineconstexpr

Fill the bit-array by repeatedly calling f(i) and returns a reference to this for chaining.

Example

v.copy([](usize i) { return i % 2 == 0; });
assert_eq(v.size(), 10);
assert_eq(v.to_string(), "1010101010");
constexpr usize size() const
Returns the number of bit-elements in the bit-array.
Definition BitArray.h:46
std::size_t usize
Word type alias for the platform's "native"-sized unsigned integer.
Definition Unsigned.h:42

◆ count_ones()

template<usize N, Unsigned Word = usize>
usize gf2::BitArray< N, Word >::count_ones ( ) const
inlineconstexpr

Returns the number of set bits in the bit-array.

Example

assert_eq(v.count_ones(), 0);
v.set(0);
assert_eq(v.count_ones(), 1);
constexpr usize count_ones() const
Returns the number of set bits in the bit-array.
Definition BitArray.h:772

◆ count_zeros()

template<usize N, Unsigned Word = usize>
usize gf2::BitArray< N, Word >::count_zeros ( ) const
inlineconstexpr

Returns the number of unset bits in the bit-array.

Example

assert_eq(v.count_zeros(), 10);
v.set(0);
assert_eq(v.count_zeros(), 9);
constexpr usize count_zeros() const
Returns the number of unset bits in the bit-array.
Definition BitArray.h:783

◆ describe()

template<usize N, Unsigned Word = usize>
std::string gf2::BitArray< N, Word >::describe ( ) const
inline

Returns a multi-line string describing the bit-array in some detail.

This method is useful for debugging but you should not rely on the output format which may change.

◆ fill_random()

template<usize N, Unsigned Word = usize>
auto gf2::BitArray< N, Word >::fill_random ( double p = 0.5,
u64 seed = 0 )
inlineconstexpr

Fill the bit-array with random bits and returns a reference to this for chaining.

The default call fill_random() sets each bit to 1 with probability 0.5 (fair coin).

Parameters
pThe probability of the elements being 1 (defaults to a fair coin, i.e. 50-50).
seedThe seed to use for the random number generator (defaults to 0, which means use entropy).

If p < 0 then the fill is all zeros, if p > 1 then the fill is all ones.

Example

u64 seed = 1234567890;
u.fill_random(0.5, seed);
v.fill_random(0.5, seed);
assert(u == v);
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::uint64_t u64
Word type alias for a 64-bit unsigned integer.
Definition Unsigned.h:39

◆ first_set()

template<usize N, Unsigned Word = usize>
std::optional< usize > gf2::BitArray< N, Word >::first_set ( ) const
inlineconstexpr

Returns the index of the first set bit in the bit-array or {} if no bits are set.

Example

assert(v.first_set() == std::optional<usize>{});
v.set(2);
assert(v.first_set() == std::optional<usize>{2});
v.set(2, false);
assert(v.first_set() == std::optional<usize>{});
v.set(27);
assert(v.first_set() == std::optional<usize>{27});
assert(empty.first_set() == std::optional<usize>{});
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

◆ first_unset()

template<usize N, Unsigned Word = usize>
std::optional< usize > gf2::BitArray< N, Word >::first_unset ( ) const
inlineconstexpr

Returns the index of the first unset bit in the bit-array or {} if no bits are unset.

Example

assert(v.first_unset() == std::optional<usize>{});
v.set(2, false);
assert(v.first_unset() == std::optional<usize>{2});
v.set(2);
assert(v.first_unset() == std::optional<usize>{});
v.set(27, false);
assert(v.first_unset() == std::optional<usize>{27});
assert(empty.first_unset() == std::optional<usize>{});
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

◆ flip()

template<usize N, Unsigned Word = usize>
auto gf2::BitArray< N, Word >::flip ( usize i)
inlineconstexpr

Flips the value of the bit-element i and returns this for chaining.

Panics

In debug mode the index is bounds-checked.

Example

v.flip(0);
assert_eq(v.to_string(), "0111111111");
v.flip(1);
assert_eq(v.to_string(), "0011111111");
v.flip(9);
assert_eq(v.to_string(), "0011111110");
static constexpr BitArray ones()
Factory method to generate a bit-array of length N where the elements are all 1.
Definition BitArray.h:187

◆ flip_all()

template<usize N, Unsigned Word = usize>
auto gf2::BitArray< N, Word >::flip_all ( )
inlineconstexpr

Flips the value of the bits in the bit-array and returns a reference to this for chaining.

Example

assert_eq(v.to_string(), "1111111111");
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

◆ from() [1/4]

template<usize N, Unsigned Word = usize>
template<typename Iter>
requires std::is_unsigned_v<typename std::iterator_traits<Iter>::value_type>
constexpr BitArray gf2::BitArray< N, Word >::from ( Iter src_begin,
Iter src_end )
inlinestaticconstexpr

Factory method to construct a bit-array by copying all the bits from an iteration of any Unsigneds.

Note

We allow any unsigned integral source, e.g. copying u64 words into a BitArray<N, u8>.

Panics

Panics if the number of bits in the source iteration does not match N.

Example

std::vector<u16> src = { 0b1010101010101010, 0b1010101010101010, 0b1111111111111111 };
auto v = BitArray<48, u8>::from(src.begin(), src.end());
assert_eq(v.to_string(), "010101010101010101010101010101011111111111111111");
auto w = BitArray<48, u32>::from(src.begin(), src.end());
assert_eq(w.to_string(), "010101010101010101010101010101011111111111111111");
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

◆ from() [2/4]

template<usize N, Unsigned Word = usize>
template<Unsigned Src>
requires (BITS<Src> == N)
constexpr BitArray gf2::BitArray< N, Word >::from ( Src src)
inlinestaticconstexpr

Factory method to construct a bit-array by copying all the bits from any Unsigned instance.

Note

The source type Src must have exactly N bits.

Example

u8 s8 = 0b01010101;
auto u = BitArray<8, u8>::from(s8);
assert_eq(u.to_string(), "10101010");
u16 s16 = 0b0101010101010101;
auto v = BitArray<16, u8>::from(s16);
assert_eq(v.to_string(), "1010101010101010");
assert_eq(w.to_string(), "10101010");

◆ from() [3/4]

template<usize N, Unsigned Word = usize>
constexpr BitArray gf2::BitArray< N, Word >::from ( std::bitset< N > const & src)
inlinestaticconstexpr

Factory method to construct a bit-array from the bits of a std::bitset.

Note

A std::bitset prints its bit elements in bit-order ...b2b1b0., we print in vector-order b0b1b2...

Example

std::bitset<10> src{0b1010101010};
auto v = BitArray<10>::from(src);
assert_eq(v.to_string(), "0101010101");

◆ from() [4/4]

template<usize N, Unsigned Word = usize>
constexpr BitArray gf2::BitArray< N, Word >::from ( std::invocable< usize > auto f)
inlinestaticconstexpr

Factory method to construct a bit-array by repeatedly calling f(i) for i in [0, N).

Parameters
fThe function to call for each index i in [0, N).

Example

auto v = BitArray<10, u8>::from([](usize i) { return i % 2 == 0; });
assert_eq(v.to_string(), "1010101010");

◆ front()

template<usize N, Unsigned Word = usize>
bool gf2::BitArray< N, Word >::front ( ) const
inlineconstexpr

Returns true if the first bit element is set, false otherwise.

Panics

In debug mode the method panics if the bit-array is empty.

Example

assert_eq(v.front(), false);
v.set_all();
assert_eq(v.front(), true);
constexpr bool front() const
Returns true if the first bit element is set, false otherwise.
Definition BitArray.h:426

◆ get()

template<usize N, Unsigned Word = usize>
bool gf2::BitArray< N, Word >::get ( usize i) const
inlineconstexpr

Returns true if the bit at the given index i is set, false otherwise.

Panics

In debug mode the index is bounds checked.

Example

assert_eq(v.get(0), false);
v.set(0);
assert_eq(v.get(0), true);
constexpr bool get(usize i) const
Returns true if the bit at the given index i is set, false otherwise.
Definition BitArray.h:397

◆ is_empty()

template<usize N, Unsigned Word = usize>
bool gf2::BitArray< N, Word >::is_empty ( ) const
inlineconstexpr

Returns true if the bit-array is empty, false otherwise.

Example

assert_eq(v.is_empty(), true);
assert_eq(u.is_empty(), false);
constexpr bool is_empty() const
Returns true if the bit-array is empty, false otherwise.
Definition BitArray.h:545

◆ last_set()

template<usize N, Unsigned Word = usize>
std::optional< usize > gf2::BitArray< N, Word >::last_set ( ) const
inlineconstexpr

Returns the index of the last set bit in the bit-array or {} if no bits are set.

Example

assert(v.last_set() == std::optional<usize>{});
v.set(2);
assert(v.last_set() == std::optional<usize>{2});
v.set(27);
assert(v.last_set() == std::optional<usize>{27});
assert(empty.last_set() == std::optional<usize>{});
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

◆ last_unset()

template<usize N, Unsigned Word = usize>
std::optional< usize > gf2::BitArray< N, Word >::last_unset ( ) const
inlineconstexpr

Returns the index of the last unset bit in the bit-array or {} if no bits are unset.

Example

assert(v.last_unset() == std::optional<usize>{});
v.set(2, false);
assert(v.last_unset() == std::optional<usize>{2});
v.set(2);
assert(v.last_unset() == std::optional<usize>{});
v.set(27, false);
assert(v.last_unset() == std::optional<usize>{27});
assert(empty.last_unset() == std::optional<usize>{});
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

◆ leading_zeros()

template<usize N, Unsigned Word = usize>
usize gf2::BitArray< N, Word >::leading_zeros ( ) const
inlineconstexpr

Returns the number of leading zeros in the bit-array.

Example

assert_eq(v.leading_zeros(), 37);
v.set(27);
assert_eq(v.leading_zeros(), 27);
w.set_all();
assert_eq(w.leading_zeros(), 0);
constexpr usize leading_zeros() const
Returns the number of leading zeros in the bit-array.
Definition BitArray.h:797

◆ next_set()

template<usize N, Unsigned Word = usize>
std::optional< usize > gf2::BitArray< N, Word >::next_set ( usize index) const
inlineconstexpr

Returns the index of the next set bit after index in the bit-array or {} if no more set bits exist.

Example

assert(v.next_set(0) == std::optional<usize>{});
v.set(2);
v.set(27);
assert(v.next_set(0) == std::optional<usize>{2});
assert(v.next_set(2) == std::optional<usize>{27});
assert(v.next_set(27) == std::optional<usize>{});
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

◆ next_unset()

template<usize N, Unsigned Word = usize>
std::optional< usize > gf2::BitArray< N, Word >::next_unset ( usize index) const
inlineconstexpr

Returns the index of the next unset bit after index in the bit-array or {} if no more unset bits exist.

Example

assert(v.next_unset(0) == std::optional<usize>{});
v.set(2, false);
v.set(27, false);
assert(v.next_unset(0) == std::optional<usize>{2});
assert(v.next_unset(2) == std::optional<usize>{27});
assert(v.next_unset(27) == std::optional<usize>{});
assert(empty.next_unset(0) == std::optional<usize>{});
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

◆ none()

template<usize N, Unsigned Word = usize>
bool gf2::BitArray< N, Word >::none ( ) const
inlineconstexpr

Returns true if no bits in the bit-array are set, false otherwise.

Note: Empty bit-arrays have no set bits (logical connective for none is AND with identity true).

Example

assert_eq(v.none(), true);
v.set(0);
assert_eq(v.none(), false);
constexpr bool none() const
Returns true if no bits in the bit-array are set, false otherwise.
Definition BitArray.h:586

◆ offset()

template<usize N, Unsigned Word = usize>
u8 gf2::BitArray< N, Word >::offset ( ) const
inlineconstexpr

Returns the offset (in bits) of the first bit in the bit-array within the first word.

This is always zero for BitArray.

◆ ones()

template<usize N, Unsigned Word = usize>
constexpr BitArray gf2::BitArray< N, Word >::ones ( )
inlinestaticconstexpr

Factory method to generate a bit-array of length N where the elements are all 1.

Example

assert_eq(v.to_string(), "1111111111");

◆ operator[]() [1/2]

template<usize N, Unsigned Word = usize>
auto gf2::BitArray< N, Word >::operator[] ( usize i)
inlineconstexpr

Returns a "reference" to the bit element i.

The returned object is a BitRef reference for the bit element at index rather than a true reference.

Note

The referenced bit-array must continue to exist while the BitRef is in use.

Panics

In debug mode the index is bounds-checked.

Example

v[2] = true;
assert(v.to_string() == "0010000000");
w.set_all();
v[3] = w[3];
assert(v.to_string() == "0011000000");
v[4] |= w[4];
assert(v.to_string() == "0011100000");

◆ operator[]() [2/2]

template<usize N, Unsigned Word = usize>
bool gf2::BitArray< N, Word >::operator[] ( usize i) const
inlineconstexpr

Returns the boolean value of the bit element i.

Panics

In debug mode the index is bounds checked.

Example

assert(v[2] == false);
v[2] = true;
assert(v[2] == true);
assert(v.to_string() == "0010000000");

◆ previous_set()

template<usize N, Unsigned Word = usize>
std::optional< usize > gf2::BitArray< N, Word >::previous_set ( usize index) const
inlineconstexpr

Returns the index of the previous set bit before index in the bit-array or {} if there are none.

Example

assert(v.previous_set(36) == std::optional<usize>{});
v.set(2);
v.set(27);
assert(v.previous_set(36) == std::optional<usize>{27});
assert(v.previous_set(27) == std::optional<usize>{2});
assert(v.previous_set(2) == std::optional<usize>{});
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

◆ previous_unset()

template<usize N, Unsigned Word = usize>
std::optional< usize > gf2::BitArray< N, Word >::previous_unset ( usize index) const
inlineconstexpr

Returns the index of the previous unset bit before index in the bit-array or {} if no more unset bits exist.

Example

assert(v.previous_unset(0) == std::optional<usize>{});
v.set(2, false);
v.set(27, false);
assert(v.previous_unset(36) == std::optional<usize>{27});
assert(v.previous_unset(27) == std::optional<usize>{2});
assert(v.previous_unset(2) == std::optional<usize>{});
assert(empty.previous_unset(0) == std::optional<usize>{});
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

◆ random()

template<usize N, Unsigned Word = usize>
BitArray gf2::BitArray< N, Word >::random ( double p = 0.5,
u64 seed = 0 )
inlinestatic

Factory method to generate a bit-array of size N where the elements are picked at random.

The default call BitArray<>::random() produces a random bit-array with each bit being 1 with probability 0.5 and where the RNG is seeded from entropy.

Parameters
pThe probability of the elements being 1 (defaults to a fair coin, i.e. 50-50).
seedThe seed to use for the random number generator (defaults to 0, which means use entropy).

If p < 0 then the bit-array is all zeros, if p > 1 then the bit-array is all ones.

Example

u64 seed = 1234567890;
auto u = BitArray<10>::random(0.5, seed);
auto v = BitArray<10>::random(0.5, seed);
assert(u == v);
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

◆ riffled() [1/2]

template<usize N, Unsigned Word = usize>
auto gf2::BitArray< N, Word >::riffled ( ) const
inlineconstexpr

Returns a new bit-vector that is the result of riffling the bits in this bit-array with zeros.

If the bit-array has the bits abcde then the output bit-vector will have the bits a0b0c0d0e.

Note: There is no last zero bit in dst.

Example

auto dst = v.riffled();
assert_eq(dst.to_string(), "1010101010101010101");
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

◆ riffled() [2/2]

template<usize N, Unsigned Word = usize>
void gf2::BitArray< N, Word >::riffled ( BitVector< word_type > & dst) const
inlineconstexpr

Interleaves the bits of this bit-array with zeros storing the result into the bit-vector dst.

On return, dst will have the bits of this bit-array interleaved with zeros. For example, if this bit-array has the bits abcde then dst will have the bits a0b0c0d0e.

Note: There is no last zero bit in dst.

Example

v.riffled(dst);
assert_eq(dst.to_string(), "1010101010101010101");
A dynamically-sized vector over GF(2) with bit elements compactly stored in a standard vector of prim...
Definition BitVector.h:23
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-vector.
Definition BitVector.h:1820

◆ seeded_random()

template<usize N, Unsigned Word = usize>
BitArray gf2::BitArray< N, Word >::seeded_random ( u64 seed)
inlinestatic

Factory method to generate a bit-array of size N where the elements are from independent fair coin flips generated from an RNG seeded with the given seed.

This allows one to have reproducible random bit-arrays, which is useful for testing and debugging.

Parameters
seedThe seed to use for the random number generator (if you set this to 0 then entropy is used).

If p < 0 then the bit-array is all zeros, if p > 1 then the bit-array is all ones.

Example

u64 seed = 1234567890;
assert(u == v);
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

◆ set()

template<usize N, Unsigned Word = usize>
auto gf2::BitArray< N, Word >::set ( usize i,
bool value = true )
inlineconstexpr

Sets the bit-element i to the specified boolean value & returns this for chaining. The default value for value is true.

Panics

In debug mode the index is bounds-checked.

Example

assert_eq(v[0], false);
v.set(0);
assert_eq(v[0], true);

◆ set_all()

template<usize N, Unsigned Word = usize>
auto gf2::BitArray< N, Word >::set_all ( bool value = true)
inlineconstexpr

Sets all the bits in the bit-array to the boolean value and returns a reference to this for chaining.

By default, all bits are set to true.

Example

v.set_all();
assert_eq(v.to_string(), "1111111111");

◆ set_bits()

template<usize N, Unsigned Word = usize>
auto gf2::BitArray< N, Word >::set_bits ( ) const
inlineconstexpr

Returns an iterator over the indices of any set bits in the bit-array.

You can use this iterator to iterate over the set bits in the bit-array and get the index of each bit.

Example

v.copy([](usize i) { return i % 2 == 0; });
assert_eq(v.to_string(), "1010101010");
auto indices = std::ranges::to<std::vector>(v.set_bits());
assert_eq(indices, (std::vector<usize>{0, 2, 4, 6, 8}));
constexpr auto set_bits() const
Returns an iterator over the indices of any set bits in the bit-array.
Definition BitArray.h:991

◆ set_word()

template<usize N, Unsigned Word = usize>
void gf2::BitArray< N, Word >::set_word ( usize i,
Word word )
inlineconstexpr

Sets word i in the bit-array's underlying word store to value (masked if necessary).

The final word in the store may not be fully occupied but we ensure that unused bits remain set to 0.

Panics

In debug mode the index is bounds checked.

Example

assert_eq(v.to_string(), "0000000000");
v.set_word(1, 0b1111'1111);
assert_eq(v.to_string(), "0000000011");
assert_eq(v.count_ones(), 2);
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

◆ size()

template<usize N, Unsigned Word = usize>
usize gf2::BitArray< N, Word >::size ( ) const
inlineconstexpr

Returns the number of bit-elements in the bit-array.

Example

assert_eq(v.size(), 10);

◆ span() [1/2]

template<usize N, Unsigned Word = usize>
auto gf2::BitArray< N, Word >::span ( usize begin,
usize end )
inlineconstexpr

Returns a mutable bit-span encompassing the bit-array's bits in the half-open range [begin, end).

Mutability here is deep – the interior pointer in the returned span is to non-const words.

Panics

This method panics if the span range is not valid.

Example

v.copy([](usize i) { return i % 2 == 0; });
auto s = v.span(1,5);
assert_eq(s.to_string(), "0101");
s.set_all();
assert_eq(s.to_string(), "1111");
assert_eq(v.to_string(), "1111101010");
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

◆ span() [2/2]

template<usize N, Unsigned Word = usize>
auto gf2::BitArray< N, Word >::span ( usize begin,
usize end ) const
inlineconstexpr

Returns an immutable bit-span encompassing the bit-array's bits in the half-open range [begin, end).

Immutability here is deep – the interior pointer in the returned span is to const words.

Panics

This method panics if the span range is not valid.

Example

v.copy([](usize i) { return i % 2 == 0; });
auto s = v.span(1,5);
assert_eq(s.to_string(), "0101");

◆ split_at() [1/2]

template<usize N, Unsigned Word = usize>
auto gf2::BitArray< N, Word >::split_at ( usize at) const
inlineconstexpr

Views the bit-array as two parts containing the elements [0, at) and [at, size()) respectively.

Clones of the parts are returned as a pair of bit-vectors [left, right].

On return, left is a clone of the bits from the start of the bit-array up to but not including at and right contains the bits from at to the end of the bit-array. This bit-array itself is not modified.

Panics

This method panics if the split point is beyond the end of the bit-array.

Example

v.copy([](usize i) { return i % 2 == 0; });
auto [left, right] = v.split_at(5);
assert_eq(left.to_string(), "10101");
assert_eq(right.to_string(), "01010");
assert_eq(v.to_string(), "1010101010");
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

◆ split_at() [2/2]

template<usize N, Unsigned Word = usize>
void gf2::BitArray< N, Word >::split_at ( usize at,
BitVector< word_type > & left,
BitVector< word_type > & right ) const
inlineconstexpr

Views the bit-array as two parts containing the elements [0, at) and [at, size()) respectively.

Clones of the parts are stored in the passed bit-vectors left and right.

On return, left contains the bits from the start of the bit-array up to but not including at and right contains the bits from at to the end of the bit-array. This bit-array itself is not modified.

This lets one reuse the left and right destinations without having to allocate new bit-vectors. This is useful when implementing iterative algorithms that need to split a bit-array into two parts repeatedly.

Panics

This method panics if the split point is beyond the end of the bit-array.

Example

v.copy([](usize i) { return i % 2 == 0; });
BitVector<u8> left, right;
v.split_at(5, left, right);
assert_eq(left.to_string(), "10101");
assert_eq(right.to_string(), "01010");
assert_eq(v.to_string(), "1010101010");

◆ store() [1/2]

template<usize N, Unsigned Word = usize>
Word * gf2::BitArray< N, Word >::store ( )
inlineconstexpr

Returns a pointer to the underlying store of words.

Note: The pointer is non-const but you should be careful about using it to modify the words in the store.

Example

v.set_all();
auto ptr = v.store();
assert_eq(*ptr, 0b1111'1111);
constexpr const Word * store() const
Returns a const pointer to the underlying store of words .
Definition BitArray.h:115

◆ store() [2/2]

template<usize N, Unsigned Word = usize>
const Word * gf2::BitArray< N, Word >::store ( ) const
inlineconstexpr

Returns a const pointer to the underlying store of words .

Example

v.set_all();
auto ptr = v.store();
assert_eq(*ptr, 0b1111'1111);

◆ store_words()

template<usize N, Unsigned Word = usize>
auto gf2::BitArray< N, Word >::store_words ( ) const
inlineconstexpr

Returns a const iterator over all the words underlying the v.

You can use this iterator to iterate over the words in the bit-array and read the Word value of each word. You cannot use this iterator to modify the words in the store.

Example

assert_eq(v.to_string(), "1111111111");
auto words = std::ranges::to<std::vector>(v.store_words());
assert_eq(words, (std::vector<u8>{0b1111'1111, 0b0000'0011}));
constexpr usize words() const
Returns the number of words in the bit-array's underlying word store.
Definition BitArray.h:59

◆ sub()

template<usize N, Unsigned Word = usize>
auto gf2::BitArray< N, Word >::sub ( usize begin,
usize end ) const
inlineconstexpr

Returns a clone of the elements in the half-open range [begin, end) as a new bit-vector.

Panics

This method panics if the range is not valid.

Example

v.copy([](usize i) { return i % 2 == 0; });
auto s = v.sub(1,5);
assert_eq(s.to_string(), "0101");
s.set_all();
assert_eq(s.to_string(), "1111");
assert_eq(v.to_string(), "1010101010");
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

◆ swap()

template<usize N, Unsigned Word = usize>
auto gf2::BitArray< N, Word >::swap ( usize i0,
usize i1 )
inlineconstexpr

Swaps the bits in the bit-array at indices i0 and i1 and returns this for chaining.

Panics

In debug mode the indices are bounds-checked.

Example

v.set(0);
assert_eq(v.to_string(), "1000000000");
v.swap(0, 1);
assert_eq(v.to_string(), "0100000000");
v.swap(0, 1);
assert_eq(v.to_string(), "1000000000");
v.swap(0, 9);
assert_eq(v.to_string(), "0000000001");
v.swap(0, 9);
assert_eq(v.to_string(), "1000000000");
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

◆ to_binary_string()

template<usize N, Unsigned Word = usize>
std::string gf2::BitArray< N, Word >::to_binary_string ( std::string_view sep = "",
std::string_view pre = "",
std::string_view post = "" ) const
inline

Returns a binary string representation of the bit-array.

The string is formatted as a sequence of 0s and 1s with the least significant bit on the right.

Parameters
sepThe separator between bit elements which defaults to no separator.
preThe prefix to add to the string which defaults to no prefix.
postThe postfix to add to the string which defaults to no postfix.

Example

assert_eq(v.to_binary_string(), "0000000000");
v.set(0);
assert_eq(v.to_binary_string(), "1000000000");
assert_eq(v.to_binary_string(",", "[", "]"), "[1,0,0,0,0,0,0,0,0,0]");
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

◆ to_hex_string()

template<usize N, Unsigned Word = usize>
std::string gf2::BitArray< N, Word >::to_hex_string ( ) const
inline

Returns the "hex" string representation of the bits in the bit-array.

The output is a string of hex characters without any spaces, commas, or other formatting.

The string may have a two character suffix of the form ".base" where base is one of 2, 4 or 8.
All hex characters encode 4 bits: "0X0" -> 0b0000, "0X1" -> 0b0001, ..., "0XF" -> 0b1111.
The three possible ".base" suffixes allow for bit-vectors whose length is not a multiple of 4.
Empty bit-vectors are represented as the empty string.

  • 0X1 is the hex representation of the bit-array 0001 => length 4.
  • 0X1.8 is the hex representation of the bit-array 001 => length 3.
  • 0X1.4 is the hex representation of the bit-array 01 => length 2.
  • 0X1.2 is the hex representation of the bit-array 1 => length 1.

The output is in vector-order. If "h0" is the first hex digit in the output string, you can print it as four binary digits v_0v_1v_2v_3. For example, if h0 = "A" which is 1010 in binary, then v = 1010.

Example

assert_eq(v0.to_hex_string(), "");
assert_eq(v1.to_hex_string(), "F");
assert_eq(v2.to_hex_string(), "F1.2");
v3.copy([](usize i) { return i % 2 == 0; });
assert_eq(v3.to_binary_string(), "10101010");
assert_eq(v3.to_hex_string(), "AA");
std::string to_hex_string() const
Returns the "hex" string representation of the bits in the bit-array.
Definition BitArray.h:1294

◆ to_pretty_string()

template<usize N, Unsigned Word = usize>
std::string gf2::BitArray< N, Word >::to_pretty_string ( ) const
inline

Returns a "pretty" string representation of the bit-array.

The output is a string of 0's and 1's with spaces between each bit, and the whole thing enclosed in square brackets.

Example

v.copy([](usize i) { return i % 2 == 0; });
assert_eq(v.to_pretty_string(), "[1,0,1,0,1,0,1,0,1,0]");
assert_eq(empty.to_pretty_string(), "[]");
std::string to_pretty_string() const
Returns a "pretty" string representation of the bit-array.
Definition BitArray.h:1261

◆ to_string()

template<usize N, Unsigned Word = usize>
std::string gf2::BitArray< N, Word >::to_string ( std::string_view sep = "",
std::string_view pre = "",
std::string_view post = "" ) const
inline

Returns a binary string representation of the bit-array.

The string is formatted as a sequence of 0s and 1s with the least significant bit on the right.

Parameters
sepThe separator between bit elements which defaults to no separator.
preThe prefix to add to the string which defaults to no prefix.
postThe postfix to add to the string which defaults to no postfix.

Example

assert_eq(v.to_string(), "0000000000");
v.set(0);
assert_eq(v.to_string(), "1000000000");
assert_eq(v.to_string(",", "[", "]"), "[1,0,0,0,0,0,0,0,0,0]");

◆ to_words() [1/2]

template<usize N, Unsigned Word = usize>
auto gf2::BitArray< N, Word >::to_words ( ) const
inlineconstexpr

Returns a copy of the words underlying this bit-array as a std::vector<word_type>.

Note: The last word in the bit-array may not be fully occupied but unused slots will be all zeros.

Example

auto words = v.to_words();
assert_eq(words, (std::vector<u8>{0b1111'1111, 0b0000'0011}));

◆ to_words() [2/2]

template<usize N, Unsigned Word = usize>
void gf2::BitArray< N, Word >::to_words ( std::output_iterator< word_type > auto out)
inlineconstexpr

Returns a copy of the words underlying this bit-array and puts them into the passed output iterator.

Note

  • The last word in the bit-array may not be fully occupied but unused slots will be all zeros.
  • The output iterator must be able to accept values of the bit-arrays's word_type.
  • The output iterator must have enough space to accept all the words in the bit-array.
  • If there is extra space in the output iterator, those extra slots are left unchanged.

Example

std::vector<u8> out8(v.words());
v.to_words(out8.begin());
assert_eq(out8, (std::vector<u8>{0b1111'1111, 0b0000'0011}));
std::vector<u16> out16(v.words());
v.to_words(out16.begin());
assert_eq(out16, (std::vector<u16>{0b1111'1111, 0b0000'0011}));

◆ trailing_zeros()

template<usize N, Unsigned Word = usize>
usize gf2::BitArray< N, Word >::trailing_zeros ( ) const
inlineconstexpr

Returns the number of trailing zeros in the bit-array.

Example

assert_eq(v.trailing_zeros(), 27);
v.set(0);
assert_eq(v.trailing_zeros(), 26);
constexpr usize trailing_zeros() const
Returns the number of trailing zeros in the bit-array.
Definition BitArray.h:808

◆ unit()

template<usize N, Unsigned Word = usize>
constexpr BitArray gf2::BitArray< N, Word >::unit ( usize i)
inlinestaticconstexpr

Factory method to generate a "unit" bit-array of length N where only element i is set.

This method panics if the condition i < N is not met.

Example

assert_eq(BitArray<10>::unit(0).to_string(), "1000000000");
assert_eq(BitArray<10>::unit(9).to_string(), "0000000001");
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

◆ unset_bits()

template<usize N, Unsigned Word = usize>
auto gf2::BitArray< N, Word >::unset_bits ( ) const
inlineconstexpr

Returns an iterator over the indices of any unset bits in the bit-array.

You can use this iterator to iterate over the unset bits in the bit-array and get the index of each bit.

Example

v.copy([](usize i) { return i % 2 == 0; });
assert_eq(v.to_string(), "1010101010");
auto indices = std::ranges::to<std::vector>(v.unset_bits());
assert_eq(indices, (std::vector<usize>{1, 3, 5, 7, 9}));
constexpr auto unset_bits() const
Returns an iterator over the indices of any unset bits in the bit-array.
Definition BitArray.h:1005

◆ word()

template<usize N, Unsigned Word = usize>
Word gf2::BitArray< N, Word >::word ( usize i) const
inlineconstexpr

Returns word i from the bit-array's underlying word store.

The final word in the store may not be fully occupied but we guarantee that unused bits are set to 0.

Panics

In debug mode the index is bounds checked.

Example

assert_eq(v.to_string(), "0000000000");
v.set_all();
assert_eq(v.to_string(), "1111111111");
assert_eq(v.words(), 2);
assert_eq(v.word(0), 0b1111'1111);
assert_eq(v.word(1), 0b0000'0011);
constexpr Word word(usize i) const
Returns word i from the bit-array's underlying word store.
Definition BitArray.h:78

◆ words()

template<usize N, Unsigned Word = usize>
usize gf2::BitArray< N, Word >::words ( ) const
inlineconstexpr

Returns the number of words in the bit-array's underlying word store.

The bit-elements are packed into a standard array with this number of words.

Example

assert_eq(v0.words(), 2);
assert_eq(v1.words(), 1);

◆ zeros()

template<usize N, Unsigned Word = usize>
constexpr BitArray gf2::BitArray< N, Word >::zeros ( )
inlinestaticconstexpr

Factory method to generate a bit-array of length N where the elements are all 0.

Example

assert_eq(v.to_string(), "0000000000");
static constexpr BitArray zeros()
Factory method to generate a bit-array of length N where the elements are all 0.
Definition BitArray.h:178