-
Notifications
You must be signed in to change notification settings - Fork 3
/
bitset.h
63 lines (49 loc) · 1.38 KB
/
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
63
#ifndef BITSET_H
#define BITSET_H
#include <stdint.h>
#include <string.h>
#define BITSIZE (1.0/8.0)
class BitSet{
public:
BitSet(uint32_t _LENGTH):LENGTH(_LENGTH){
uint32_t size = (LENGTH + 7) >> 3;
bitset = new uint8_t[size];
memset(bitset, 0, size * sizeof(uint8_t));
}
~BitSet(){
delete [] bitset;
}
inline void Set(uint32_t index){
Set(index >> 3, index & 0x7);
}
inline void Set(uint32_t position, uint32_t offset){
bitset[position] |= (1 << offset);
}
inline bool Get(uint32_t index){
return Get(index >> 3, index & 0x7);
}
inline bool Get(uint32_t position, uint32_t offset){
return ((bitset[position] & (1 << offset)) != 0);
}
inline bool SetByte(uint32_t position, uint32_t match){
bool ret = !(bitset[position] & match);
bitset[position] |= match;
return ret;
}
inline bool SetNGet(uint32_t index){
return SetNGet(index >> 3, index & 0x7);
}
inline bool SetNGet(uint32_t position, uint32_t offset){
bool ret = (bitset[position] & (1 << offset));
bitset[position] |= (1 << offset);
return ret;
}
inline void Clear(){
uint32_t size = (LENGTH + 7) >> 3;
memset(bitset, 0, size * sizeof(uint8_t));
}
private:
const uint32_t LENGTH;
uint8_t* bitset;
};
#endif //BITSET_H