22#include <spot/misc/hashfunc.hh>
23#include <spot/misc/common.hh>
24#include <spot/misc/clz.hh>
31 [[noreturn]] SPOT_API
void report_bit_shift_too_big();
32 [[noreturn]] SPOT_API
void report_bit_out_of_bounds();
41 using word_t = unsigned;
43 static_assert(8*N*
sizeof(word_t) < -1U,
"too many bits in bitset");
45 std::array<word_t, N> data;
48 struct minus_one_tag {};
49 explicit bitset(minus_one_tag)
55 constexpr explicit bitset(word_t s)
58 SPOT_ASSERT(s == 0
U || s == 1U);
74 explicit operator bool()
const
76 for (
const auto& v : data)
85 return fnv_hash(data.begin(), data.end());
92 for (
unsigned i = 0; i != N; ++i)
93 if (data[i] != other.data[i])
107 for (
unsigned i = 0; i != N; ++i)
108 if (data[i] < other.data[i])
110 else if (data[i] > other.data[i])
118 for (
unsigned i = 0; i != N; ++i)
119 if (data[i] < other.data[i])
121 else if (data[i] > other.data[i])
129 return other.operator<(*this);
135 return other.operator<=(*this);
141#if SPOT_DEBUG || defined(SWIGPYTHON)
142 if (SPOT_UNLIKELY(s >= 8 * N *
sizeof(word_t)))
143 internal::report_bit_out_of_bounds();
145 SPOT_ASSUME(s < 8 * N *
sizeof(word_t));
147 data[s / (8*
sizeof(word_t))] |= 1U << (s % (8*
sizeof(word_t)));
153#if SPOT_DEBUG || defined(SWIGPYTHON)
154 if (SPOT_UNLIKELY(s >= 8 * N *
sizeof(word_t)))
155 internal::report_bit_out_of_bounds();
157 SPOT_ASSUME(s < 8 * N *
sizeof(word_t));
159 data[s / (8*
sizeof(word_t))] &= ~(1U << (s % (8*
sizeof(word_t))));
180#if SPOT_DEBUG || defined(SWIGPYTHON)
181 if (SPOT_UNLIKELY(s >= 8 * N *
sizeof(word_t)))
182 internal::report_bit_shift_too_big();
184 SPOT_ASSUME(s < 8 * N *
sizeof(word_t));
198 const unsigned wshift = s / (8 *
sizeof(word_t));
199 const unsigned offset = s % (8 *
sizeof(word_t));
203 for (
unsigned i = N - 1; i >= wshift; --i)
204 data[i] = data[i - wshift];
208 const unsigned sub_offset = 8 *
sizeof(word_t) - offset;
209 for (
unsigned i = N - 1; i > wshift; --i)
210 data[i] = ((data[i - wshift] << offset) |
211 (data[i - wshift - 1] >> sub_offset));
212 data[wshift] = data[0] << offset;
214 std::fill(data.begin(), data.begin() + wshift, word_t(0));
221#if SPOT_DEBUG || defined(SWIGPYTHON)
222 if (SPOT_UNLIKELY(s >= 8 * N *
sizeof(word_t)))
223 internal::report_bit_shift_too_big();
225 SPOT_ASSUME(s < 8 * N *
sizeof(word_t));
238 const unsigned wshift = s / (8 *
sizeof(word_t));
239 const unsigned offset = s % (8 *
sizeof(word_t));
240 const unsigned limit = N - wshift - 1;
244 for (
unsigned i = 0; i <= limit; ++i)
245 data[i] = data[i + wshift];
249 const unsigned sub_offset = 8 *
sizeof(word_t) - offset;
250 for (
unsigned i = 0; i < limit; ++i)
251 data[i] = ((data[i + wshift] >> offset) |
252 (data[i + wshift + 1] << sub_offset));
253 data[limit] = data[N - 1] >> offset;
255 std::fill(data.begin() + limit + 1, data.end(), word_t(0));
263 for (
auto& v : r.data)
295 for (
unsigned i = 0; i != N; ++i)
296 data[i] &= other.data[i];
302 for (
unsigned i = 0; i != N; ++i)
303 data[i] |= other.data[i];
309 for (
unsigned i = 0; i != N; ++i)
310 data[i] ^= other.data[i];
344 for (
auto& v : res.data)
360 c += __builtin_popcount(v);
375 unsigned res = (N-1)*8*
sizeof(word_t);
382 res -= CHAR_BIT*
sizeof(word_t);
385 return res + CHAR_BIT*
sizeof(word_t) -
clz(v) - 1;
402 res += __builtin_ctz(v);
A fixed-size bitset backed by N unsigned words.
Definition bitset.hh:40
static bitset mone()
the -1 (all bits are set to 1)
Definition bitset.hh:71
size_t hash() const
Return a hash of the bitset.
Definition bitset.hh:83
bitset operator|(const bitset &other) const
Return the bitwise OR of *this and other.
Definition bitset.hh:277
bitset & operator-=(word_t s)
Subtract s from the bitset in place.
Definition bitset.hh:322
bool operator<(const bitset &other) const
Lexicographic less-than comparison.
Definition bitset.hh:105
bitset operator>>(unsigned s) const
Return the bitset shifted right by s positions.
Definition bitset.hh:170
bool operator!=(const bitset &other) const
Inequality comparison.
Definition bitset.hh:99
void set(unsigned s)
Set bit s to one.
Definition bitset.hh:139
static constexpr bitset zero()
the 0
Definition bitset.hh:67
bitset operator-(word_t s) const
Return the result of subtracting s from the bitset.
Definition bitset.hh:315
bitset & operator|=(const bitset &other)
In-place bitwise OR with other.
Definition bitset.hh:300
unsigned highest() const
Return the position of the highest set bit.
Definition bitset.hh:373
bitset & operator>>=(unsigned s)
Shift right in place by s positions.
Definition bitset.hh:219
bool operator>(const bitset &other) const
Lexicographic greater-than comparison.
Definition bitset.hh:127
bool operator==(const bitset &other) const
Equality comparison.
Definition bitset.hh:89
unsigned count() const
Return the number of set bits (popcount).
Definition bitset.hh:354
bitset & operator<<=(unsigned s)
Shift left in place by s positions.
Definition bitset.hh:178
bool operator<=(const bitset &other) const
Lexicographic less-than-or-equal comparison.
Definition bitset.hh:116
bitset operator-() const
Return the arithmetic negation (two's complement).
Definition bitset.hh:340
bool operator>=(const bitset &other) const
Lexicographic greater-than-or-equal comparison.
Definition bitset.hh:133
bitset operator&(const bitset &other) const
Return the bitwise AND of *this and other.
Definition bitset.hh:269
bitset operator^(const bitset &other) const
Return the bitwise XOR of *this and other.
Definition bitset.hh:285
bitset operator<<(unsigned s) const
Return the bitset shifted left by s positions.
Definition bitset.hh:163
static constexpr bitset one()
the 1
Definition bitset.hh:69
unsigned lowest() const
Return the position of the lowest set bit.
Definition bitset.hh:391
bitset & operator^=(const bitset &other)
In-place bitwise XOR with other.
Definition bitset.hh:307
bitset & operator&=(const bitset &other)
In-place bitwise AND with other.
Definition bitset.hh:293
void clear(unsigned s)
Clear bit s (set it to zero).
Definition bitset.hh:151
bitset operator~() const
Return the bitwise complement.
Definition bitset.hh:260
size_t fnv_hash(It begin, It end)
Fowler-Noll-Vo hash function.
Definition hashfunc.hh:95
Definition automata.hh:26
constexpr bool operator==(trival a, trival b)
Equality comparison of two trival values.
Definition trival.hh:134
size_t operator()(const spot::bitset< N > &b) const
Compute the hash of b.
Definition bitset.hh:425