10inline uint64_t begin_mask(uint64_t i) {
return -1_u64 << (i % 64_u64); }
11inline uint64_t end_mask(uint64_t i) {
return ~begin_mask(i); }
14void BitSet::dealloc()
const {
15 if (num_words_ != 1)
delete[] words_;
21 for (
size_t i = 0, e = num_words(); i != e; ++i) result += std::popcount(w[i]);
26 auto n = std::min(this->num_words(), other.num_words());
27 for (
size_t i = 0; i != n; ++i)
28 if (this->words()[i] != other.words()[i])
return false;
32 if (this->num_words() > other.num_words()) {
34 m = this->num_words();
37 m = other.num_words();
40 for (
size_t i = n; i != m; ++i)
41 if (w[i] != 0)
return false;
47 if (begin >= end)
return false;
49 end = std::min(end, num_bits());
50 size_t i = begin / 64_s;
51 size_t e = end / 64_s;
52 auto bmask = begin_mask(begin);
53 auto emask = end_mask(end);
56 if (i == e)
return words()[i] & bmask & emask;
59 bool result = (words()[i++] & bmask);
62 for (; !result && i != e; ++i) result |= words()[i];
65 return result || (emask && (words_[i] & emask));
69 uint64_t div = shift / 64_u64;
70 uint64_t
rem = shift % 64_u64;
73 if (div >= num_words())
76 for (
size_t i = 0, e = num_words() - div; i != e; ++i) w[i] = w[i + div];
77 std::fill(w + num_words() - div, w + num_words(), 0);
80 for (
size_t i = num_words() - div; i-- != 0;) {
81 uint64_t new_carry = w[i] << (64_u64 -
rem);
82 w[i] = (w[i] >>
rem) | carry;
90void BitSet::ensure_capacity(
size_t i)
const {
91 size_t num_new_words = (i + 64_s) / 64_s;
92 if (num_new_words > num_words_) {
93 num_new_words = std::bit_ceil(num_new_words);
94 assert(num_new_words >= num_words_ * 2_s
95 &&
"num_new_words must be a power of two at least twice of num_words_");
96 uint64_t* new_words =
new uint64_t[num_new_words];
99 std::fill(std::copy_n(words(), num_words_, new_words), new_words + num_new_words, 0);
103 num_words_ = num_new_words;
BitSet & operator>>=(uint64_t shift)
bool operator==(const BitSet &) const
void clear()
clears all bits
bool any_range(const size_t begin, const size_t end) const
size_t count() const
number of bits set