-
Notifications
You must be signed in to change notification settings - Fork 3
/
bitset.cpp
102 lines (82 loc) · 2 KB
/
bitset.cpp
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <bits/stdc++.h>
using namespace std;
class BitSet;
class BitSetReference {
friend class BitSet;
BitSetReference() ; // no public constructor
unsigned int *word;
int ind;
public:
operator bool() const ; // convert to bool
BitSetReference(BitSet* bitset, int n);
BitSetReference& operator= (bool x) ; // assign bool
BitSetReference& operator= (const BitSetReference& x) ; // assign bit
};
class BitSet {
friend class BitSetReference;
vector<unsigned int> a;
size_t w;
int n;
size_t _size;
public:
BitSet(): n(0), w(sizeof(unsigned int)), _size(0) {}
BitSet(int n, bool v = 0): n(n), w(sizeof(int)) {
_size = n/w;
if(n%w) _size++;
a.resize(_size, v ? ~1 : 0);
}
void push(bool b) {
n++;
_size = n/w;
if(n%w) _size++;
if(_size!=a.size())
a.resize(_size);
(*this)[n-1] = b;
}
void pop() {
if(!n)return;
n--;
_size = n/w;
if(n%w) _size++;
if(_size!=a.size())
a.resize(_size);
}
BitSetReference back() {
return (*this)[n-1];
}
size_t size() {
return _size;
}
BitSetReference operator[] (int n) {
return BitSetReference(this, n);
}
};
BitSetReference::BitSetReference(BitSet* bitset, int n) {
word = &(bitset->a[(n/bitset->w)]);
ind = n%(bitset->w);
}
BitSetReference& BitSetReference::operator= (bool x) {
if(x)
*word |= (1<<ind);
else
*word &= ~(1<<ind);
return *this;
}
BitSetReference& BitSetReference::operator= (const BitSetReference& x) {
*this = bool(x);
return *this;
}
BitSetReference::operator bool() const {
return (*word & (1<<ind)) != 0;
}
int main() {
BitSet b1(10);
cout<< b1[0]<<endl;
b1[0] = 1;
cout<< b1[0]<<endl;
b1.push(b1[0]);
cout<< b1[10]<<endl;
cout<< b1.back()<<endl;
b1.pop();
cout<< b1.back()<<endl;
}