-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16_code_snippet.cpp
59 lines (47 loc) · 1.01 KB
/
16_code_snippet.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
#include <iostream>
using namespace std;
// boolean bitset for large size
// 35,000 * 50,000 bytes == 208 MB
// 20,000 * 50,000 bytes == 119 MB
const unsigned int MAX_N = 30000 * 50000;
unsigned char large[(MAX_N + 7) / 8];
inline bool bitIsTrue(unsigned int k) {
return large[k >> 3] & (1 << (k & 7));
}
inline void bitSetTrue(unsigned int k) {
large[k >> 3] |= 1 << (k & 7);
}
inline void bitSetFalse(unsigned int k) {
large[k >> 3] &= ~(1 << (k & 7));
}
bool check(int n) {
for (int i = 0; i < n; i++) {
bitSetTrue(i);
}
for (int i = 0; i < n; i++) {
if (!bitIsTrue(i)) {
cout << "Something wrong!" << '\n';
return false;
}
}
for (int i = 0; i < n; i++) {
bitSetFalse(i);
}
for (int i = 0; i < n; i++) {
if (bitIsTrue(i)) {
cout << "Something wrong!" << '\n';
return false;
}
}
cout << "Passed! (size: " << n << ")\n";
return true;
}
int main(void) {
ios_base::sync_with_stdio(false);
cout.tie(NULL);
for (int i = 1; i < MAX_N; i *= 1000) {
if (!check(i))
break;
}
return 0;
}