-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bitmap.hpp
51 lines (47 loc) · 1.11 KB
/
Bitmap.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
#ifndef BITMAP
#define BITMAP
/**
* This class allows for a dynamic bitmap with optimized space.
* Every 64 bits are represented by an unsigned long.
* */
class Bitmap {
unsigned long * values;
size_t size;
public:
Bitmap( int size): size(size) {
this->values = new unsigned long[size/64 + (size % 64 != 0)];
for(int i=0;i<size/64 + (size % 64 != 0);++i) {
this->values[i] = 0;
}
}
~Bitmap() {
delete(this->values);
}
/**
* Set the i-th bit to val
*/
void set(int i, bool val) {
int block = i/64;
int cell = i%64;
// std::cout << "block " << block << ' ' << "cell " << cell << std::endl;
// number |= 1UL << n;
this->values[block] |= 1UL << cell;
}
/**
* Get the i-th bit
*/
bool get(int i) {
int block = i/64;
int cell = i%64;
return (this->values[block] >> cell) & 1U;
}
/**
* Set all bits to false
*/
void clear() {
for(int i=0;i<size/64 + (size % 64 != 0);++i) {
this->values[i] = 0;
}
}
};
#endif