#pragma once #include #include #include #include #include namespace Void::Bitset { // ── Bitset ───────────────────────────────────────────────────────────────── // Fixed-size bitset stored as an array of uint64_t words. // All operations are O(N/64). template class Bitset { static constexpr std::size_t Words = (N + 63) / 64; public: constexpr Bitset() noexcept { words_.fill(0); } // Set bit at position (throws if out of range). constexpr Bitset& set(std::size_t pos) { check(pos); words_[pos / 64] |= mask(pos); return *this; } // Clear bit at position. constexpr Bitset& reset(std::size_t pos) { check(pos); words_[pos / 64] &= ~mask(pos); return *this; } // Flip bit at position. constexpr Bitset& flip(std::size_t pos) { check(pos); words_[pos / 64] ^= mask(pos); return *this; } // Test bit at position. [[nodiscard]] constexpr bool test(std::size_t pos) const { check(pos); return (words_[pos / 64] & mask(pos)) != 0; } [[nodiscard]] constexpr bool operator[](std::size_t pos) const { return test(pos); } // Set / clear / flip all bits. constexpr Bitset& set_all() noexcept { for (auto& w : words_) w = ~uint64_t{0}; trim(); return *this; } constexpr Bitset& reset_all() noexcept { words_.fill(0); return *this; } constexpr Bitset& flip_all() noexcept { for (auto& w : words_) w = ~w; trim(); return *this; } // Count of set bits. [[nodiscard]] constexpr std::size_t count() const noexcept { std::size_t n = 0; for (auto w : words_) n += popcount(w); return n; } [[nodiscard]] constexpr bool none() const noexcept { for (auto w : words_) if (w) return false; return true; } [[nodiscard]] constexpr bool any() const noexcept { return !none(); } [[nodiscard]] constexpr bool all() const noexcept { return count() == N; } static constexpr std::size_t size() noexcept { return N; } // ── bitwise ops ─────────────────────────────────────────────────────────── [[nodiscard]] constexpr Bitset operator&(const Bitset& o) const noexcept { Bitset r; for (std::size_t i = 0; i < Words; ++i) r.words_[i] = words_[i] & o.words_[i]; return r; } [[nodiscard]] constexpr Bitset operator|(const Bitset& o) const noexcept { Bitset r; for (std::size_t i = 0; i < Words; ++i) r.words_[i] = words_[i] | o.words_[i]; return r; } [[nodiscard]] constexpr Bitset operator^(const Bitset& o) const noexcept { Bitset r; for (std::size_t i = 0; i < Words; ++i) r.words_[i] = words_[i] ^ o.words_[i]; return r; } [[nodiscard]] constexpr Bitset operator~() const noexcept { Bitset r = *this; r.flip_all(); return r; } constexpr Bitset& operator&=(const Bitset& o) noexcept { for (std::size_t i = 0; i < Words; ++i) words_[i] &= o.words_[i]; return *this; } constexpr Bitset& operator|=(const Bitset& o) noexcept { for (std::size_t i = 0; i < Words; ++i) words_[i] |= o.words_[i]; return *this; } constexpr Bitset& operator^=(const Bitset& o) noexcept { for (std::size_t i = 0; i < Words; ++i) words_[i] ^= o.words_[i]; return *this; } // ── shift ───────────────────────────────────────────────────────────────── [[nodiscard]] constexpr Bitset operator<<(std::size_t shift) const noexcept { if (shift >= N) return Bitset{}; Bitset r; const std::size_t word_shift = shift / 64; const std::size_t bit_shift = shift % 64; for (std::size_t i = Words; i-- > 0;) { if (i >= word_shift) r.words_[i] = words_[i - word_shift] << bit_shift; if (bit_shift && i > word_shift) r.words_[i] |= words_[i - word_shift - 1] >> (64 - bit_shift); } r.trim(); return r; } [[nodiscard]] constexpr Bitset operator>>(std::size_t shift) const noexcept { if (shift >= N) return Bitset{}; Bitset r; const std::size_t word_shift = shift / 64; const std::size_t bit_shift = shift % 64; for (std::size_t i = 0; i < Words; ++i) { if (i + word_shift < Words) r.words_[i] = words_[i + word_shift] >> bit_shift; if (bit_shift && i + word_shift + 1 < Words) r.words_[i] |= words_[i + word_shift + 1] << (64 - bit_shift); } return r; } // ── comparison ──────────────────────────────────────────────────────────── [[nodiscard]] constexpr bool operator==(const Bitset& o) const noexcept { return words_ == o.words_; } [[nodiscard]] constexpr bool operator!=(const Bitset& o) const noexcept { return !(*this == o); } // ── string conversion ───────────────────────────────────────────────────── [[nodiscard]] std::string to_string() const { std::string s(N, '0'); for (std::size_t i = 0; i < N; ++i) if (test(N - 1 - i)) s[i] = '1'; return s; } // ── index of first set bit (-1 if none) ────────────────────────────────── [[nodiscard]] constexpr std::size_t find_first() const noexcept { for (std::size_t w = 0; w < Words; ++w) { if (words_[w] == 0) continue; return w * 64 + ctz(words_[w]); } return N; // sentinel: no bit set } private: std::array words_; static constexpr std::uint64_t mask(std::size_t pos) noexcept { return std::uint64_t{1} << (pos % 64); } constexpr void check(std::size_t pos) const { if (pos >= N) throw std::out_of_range{"Bitset: position out of range"}; } // Clear bits above N in the last word. constexpr void trim() noexcept { if constexpr (N % 64 != 0) words_[Words - 1] &= (std::uint64_t{1} << (N % 64)) - 1; } static constexpr std::size_t popcount(std::uint64_t w) noexcept { return static_cast(__builtin_popcountll(w)); } static constexpr std::size_t ctz(std::uint64_t w) noexcept { return static_cast(__builtin_ctzll(w)); } }; } // namespace Void::Bitset