Files
Library/include/Void/Containers/Bitset.hpp
T
faumarayandClaude Fable 5 28b94883fd Restructure into umbrella modules (~55 -> 16 targets)
Headers move, CMake targets merge, C++ namespaces stay unchanged:
- Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant
- Functional: Pipeline, Either, Result, Retry
- Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval
- Math: +Matrix, Numeric, Random, Histogram
- Text: Str, Format, Tokenizer, Diff, Encoding, Semver
- Data: Json, Csv, Ini
- Async: Coro, Concurrency, EventBus, Observable
- Time: +Bench
- Log: Log facade + Logging framework (facade now header-only, breaking
  the Stacktrace<->Log link cycle the merge would have created)
- System: FileSystem + Process
- Configuration: +Argparse
- Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail

Module system fixes:
- OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no
  longer depends on the alphabetical discovery order of src/ subdirs
  (Testing -> Text would have been silently dropped).
- Every header-only module now has a tracked module.cmake; previously the
  empty src/<Name> marker dirs were untracked, so a fresh clone would have
  silently skipped those modules.
- VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT.

Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files
prefixed: ResultBasic etc.). Test count verified identical (831) before and
after. README/CLAUDE.md module tables updated.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 18:50:02 +03:00

220 lines
7.0 KiB
C++

#pragma once
#include <array>
#include <cstddef>
#include <cstdint>
#include <stdexcept>
#include <string>
namespace Void::Bitset
{
// ── Bitset<N> ─────────────────────────────────────────────────────────────────
// Fixed-size bitset stored as an array of uint64_t words.
// All operations are O(N/64).
template <std::size_t N>
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<std::uint64_t, Words> 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<std::size_t>(__builtin_popcountll(w));
}
static constexpr std::size_t ctz(std::uint64_t w) noexcept
{
return static_cast<std::size_t>(__builtin_ctzll(w));
}
};
} // namespace Void::Bitset