-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathbitvector.hpp
58 lines (50 loc) · 1.07 KB
/
bitvector.hpp
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
// LGPL 3 or higher Robert Burner Schadek [email protected]
#ifndef SWEETHPP_BITVECTOR
#define SWEETHPP_BITVECTOR
#include <vector>
#include <iostream>
class BitVector {
public:
unsigned int threeTwo;
std::vector<bool> rest;
public:
static bool _bittest(unsigned int base, unsigned int offset) {
return base & (1u << offset);
}
inline bool operator[](const size_t idx) const {
if(idx < 32) {
return _bittest(threeTwo, idx);
} else {
const size_t nIdx = idx-32;
if(nIdx >= rest.size()) {
return false;
}
return rest[nIdx];
}
}
inline void set(const size_t idx) {
if(idx < 32u) {
threeTwo |= (1u<<idx);
} else {
const size_t nIdx = idx-32u;
if(nIdx >= rest.size()) {
rest.resize(nIdx+1, false);
}
rest[nIdx] = true;
}
}
inline void unset(const size_t idx) {
if(idx < 32u) {
threeTwo &= ~(1u<<idx);
} else {
const size_t nIdx = idx-32u;
if(nIdx >= rest.size()) {
rest.resize(nIdx+1u, false);
}
rest[nIdx-32u] = false;
}
}
public:
inline BitVector(unsigned int b = 0) : threeTwo(b) {}
};
#endif