GF2++
Loading...
Searching...
No Matches
BitSet.h
Go to the documentation of this file.
1#pragma once
2// SPDX-FileCopyrightText: 2025 Nessan Fitzmaurice <nzznfitz+gh@icloud.com>
3// SPDX-License-Identifier: MIT
4
8
9#include <gf2/BitStore.h>
10#include <gf2/BitSpan.h>
11
12namespace gf2 {
13
23template<usize N, unsigned_word Word = usize>
24class BitSet : public BitStore<BitSet<N, Word>> {
25private:
26 // The bit elements are packed compactly into this standard array of unsigned words
27 std::array<Word, words_needed<Word>(N)> m_data{};
28
29public:
31 using word_type = Word;
32
34 static constexpr u8 bits_per_word = BITS<Word>;
35
38
46 constexpr usize size() const { return N; }
47
59 constexpr usize words() const { return m_data.size(); }
60
77 constexpr Word word(usize i) const {
78 gf2_debug_assert(i < words(), "Index {} is too large for a bitset with {} words.", i, words());
79 return m_data[i];
80 }
81
96 constexpr void set_word(usize i, Word word) {
97 gf2_debug_assert(i < words(), "Index {} is too large for a bitset with {} words.", i, words());
98
99 // Set the value and if it's the last word, make sure it is kept clean
100 m_data[i] = word;
101 if (i == words() - 1) clean();
102 }
103
113 constexpr const Word* data() const { return m_data.data(); }
114
126 constexpr Word* data() { return m_data.data(); }
127
131 constexpr u8 offset() const { return 0; }
132
136
140 constexpr void clean() {
141 // NOTE: This works fine even if `size() == 0`.
142 auto shift = N % bits_per_word;
143 if (shift != 0) m_data[words() - 1] &= Word(~(MAX<Word> << shift));
144 }
145
146};
147
148} // namespace gf2
149
150// --------------------------------------------------------------------------------------------------------------------
151// Specialises `std::formatter` for `BitSet` -- defers to the version for `BitStore` ...
152// -------------------------------------------------------------------------------------------------------------------
153template<gf2::usize N, gf2::unsigned_word Word>
154struct std::formatter<gf2::BitSet<N, Word>> : std::formatter<gf2::BitStore<gf2::BitSet<N, Word>>> {
155 // Inherits all functionality from `BitStore` formatter
156};
Non-owning views of a contiguous range of bits from some underlying store of unsigned words....
The base class for the vector-like types in the gf2 library. See the BitStore 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
A fixed-size vector over GF(2) with N bit elements compactly stored in a standard vector of primitive...
Definition BitSet.h:24
Word word_type
The underlying unsigned word type used to store the bits.
Definition BitSet.h:31
constexpr Word * data()
Returns a pointer to the underlying store of words.
Definition BitSet.h:126
constexpr Word word(usize i) const
Returns word i from the bitset's underlying word store.
Definition BitSet.h:77
static constexpr u8 bits_per_word
The number of bits per Word.
Definition BitSet.h:34
constexpr const Word * data() const
Returns a const pointer to the underlying store of words .
Definition BitSet.h:113
constexpr void set_word(usize i, Word word)
Sets word i in the bitset's underlying word store to value (masked if necessary).
Definition BitSet.h:96
constexpr void clean()
Sets any unused bits in the last occupied word to 0.
Definition BitSet.h:140
constexpr usize words() const
Returns the number of words in the bitset's underlying word store.
Definition BitSet.h:59
constexpr u8 offset() const
Returns the offset (in bits) of the first bit in the store within the first word.
Definition BitSet.h:131
constexpr usize size() const
Returns the number of bit-elements in the bitset.
Definition BitSet.h:46
The namespace for the gf2 library.
Definition assert.h:119
std::uint8_t u8
Word type alias for an 8-bit unsigned integer.
Definition unsigned_word.h:29
std::size_t usize
Word type alias for the platform's "native"-sized unsigned integer.
Definition unsigned_word.h:41
constexpr Word MAX
The unsigned_word instance with all its bits set to 1.
Definition unsigned_word.h:81
constexpr u8 BITS
The number of bits in an unsigned_word returned as a u8.
Definition unsigned_word.h:54