-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic_bitset.h
executable file
·62 lines (52 loc) · 1.17 KB
/
dynamic_bitset.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#ifndef DYNAMIC_BITSET_H
#define DYNAMIC_BITSET_H
#include <core/error_macros.h>
#include <vector>
// STL's bitset is fixed size, and I don't want to depend on Boost
class DynamicBitset {
public:
inline unsigned int size() const {
return _size;
}
inline void resize(unsigned int size) {
// non-initializing resize
_bits.resize((size + 63) / 64);
_size = size;
}
void fill(bool v) {
// Note: padding bits will also be set
uint64_t m = v ? 0xffffffffffffffff : 0;
for (auto it = _bits.begin(); it != _bits.end(); ++it) {
*it = m;
}
}
inline bool get(uint64_t i) const {
#ifdef DEBUG_ENABLED
CRASH_COND(i >= _size);
#endif
return _bits[i >> 6] & (uint64_t(1) << (i & uint64_t(63)));
}
inline void set(uint64_t i) {
#ifdef DEBUG_ENABLED
CRASH_COND(i >= _size);
#endif
_bits[i >> 6] |= uint64_t(1) << (i & uint64_t(63));
}
inline void unset(uint64_t i) {
#ifdef DEBUG_ENABLED
CRASH_COND(i >= _size);
#endif
_bits[i >> 6] &= ~(uint64_t(1) << (i & uint64_t(63)));
}
inline void set(uint64_t i, bool v) {
if (v) {
set(i);
} else {
unset(i);
}
}
private:
std::vector<uint64_t> _bits;
unsigned int _size = 0;
};
#endif // DYNAMIC_BITSET_H