Template Class BitArray

Class Documentation

template<std::size_t Bits>
class BitArray

Fixed-size packed bit array.

Storage uses the smallest fixed-width integer that can hold the requested bits without exceeding the platform’s native word size. Larger arrays are split across native words.

Bit zero is the least-significant bit. Mutable indexing returns a proxy so individual bits can be assigned as though they were ordinary booleans. words() exposes the same packed storage as a contiguous word array. Indices must be below Bits; assertions are not checked release-mode errors. Spans/proxies borrow this array. Access is unsynchronized, including writes to different bits in one word; whole-word access can modify unused high bits. Native storage/byte order is not a portable serialized representation.

Template Parameters:

Bits – Positive logical bit count, enforced by static_assert; starts with all bits zero.

Public Types

using word_type = ::CE::Math::Detail::bit_word_t<Bits>

Smallest supported backing word not wider than native size_t.

using Reference = ::CE::Math::Detail::BitReference<word_type>

Borrowed write-through bit proxy; see mutable operator[] for assignment and lifetime rules.

Public Functions

Reference operator[](std::size_t index) noexcept

Borrows a writable bit proxy; indexing performs no allocation.

Parameters:

index – Zero-based bit index, strictly below Bits; asserted, not a checked release error.

Returns:

Proxy borrowing this array’s backing word. Assignment from bool writes that bit; assignment from another proxy copies its current bit value, not its binding. Explicit bool conversion samples the bit. Keep this array alive through proxy use; copied/moved arrays have separate storage and do not retarget existing proxies. Writes, including different bits sharing a word, require caller synchronization.

bool operator[](std::size_t index) const noexcept

Reads one bit as an independent bool value.

Parameters:

index – Zero-based bit index below Bits; violations are assertion-only.

Returns:

Current bit value; no pointer/proxy or synchronization is retained.

std::span<word_type, word_count> words() noexcept

Borrows contiguous mutable backing words in least-significant-bit index order.

Returns:

Fixed-extent word span borrowing this array. It does not mask unused high bits. Caller mutations and synchronization operate on whole words; this is native storage, not a portable byte serialization. Keep the owning array alive through use.

std::span<const word_type, word_count> words() const noexcept

Borrows a read-only span of contiguous native backing words.

Returns:

Fixed-extent view valid while this array lives, including any unused high bits. It is a live view, not a snapshot; reads must not race mutation of the same words.

Public Static Attributes

static constexpr std::size_t word_bits = ::CE::Math::Detail::bit_word_bits<Bits>

Bits per backing word.

static constexpr std::size_t word_count = ::CE::Math::Detail::bit_word_count<Bits>

Words required for Bits, rounding up.