-
Notifications
You must be signed in to change notification settings - Fork 2
/
bitset.hpp
229 lines (181 loc) · 4.15 KB
/
bitset.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#ifndef BITSET_HPP
#define BITSET_HPP
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <initializer_list>
#include <set>
namespace sopang
{
template<int N>
class BitSet
{
public:
BitSet(int maxCount);
BitSet(int maxCount, const std::initializer_list<int> &other);
BitSet(const std::initializer_list<int> &other);
std::set<int> toSet() const;
bool operator==(const BitSet &other) const;
bool operator==(const std::set<int> &other) const;
BitSet<N> operator&(const BitSet<N> &other) const;
BitSet<N> &operator|=(const BitSet<N> &other);
int count() const;
bool empty() const;
bool any() const;
bool test(int n) const;
void set();
void set(int n);
void reset();
void reset(int n);
private:
int maxCount;
int bufferSize, bufferSizeBytes;
uint64_t buffer[(N + 63) / 64];
};
template<int N>
BitSet<N>::BitSet(int maxCount)
:maxCount(maxCount),
bufferSize((maxCount + 63) / 64),
bufferSizeBytes(bufferSize * sizeof(uint64_t))
{
assert(maxCount <= N);
__builtin_memset(buffer, 0, bufferSizeBytes);
}
template<int N>
BitSet<N>::BitSet(int maxCount, const std::initializer_list<int> &list)
:BitSet(maxCount)
{
for (const int n : list)
{
set(n);
}
}
template<int N>
BitSet<N>::BitSet(const std::initializer_list<int> &list)
:BitSet(N)
{
maxCount = 0;
for (const int n : list)
{
maxCount = std::max(maxCount - 1, n) + 1;
set(n);
}
bufferSize = (maxCount + 63) / 64;
bufferSizeBytes = bufferSize * sizeof(uint64_t);
}
template <int N>
std::set<int> BitSet<N>::toSet() const
{
std::set<int> ret;
for (int i = 0; i < bufferSize; ++i)
{
const int offset = i * 64;
for (int b = 0; b < 64; ++b)
{
if (buffer[i] & (0x1ULL << b))
{
ret.insert(offset + b);
}
}
}
return ret;
}
template <int N>
bool BitSet<N>::operator==(const BitSet &other) const
{
return __builtin_memcmp(this->buffer, other.buffer, bufferSizeBytes) == 0;
}
template <int N>
bool BitSet<N>::operator==(const std::set<int> &other) const
{
return this->toSet() == other;
}
template <int N>
BitSet<N> BitSet<N>::operator&(const BitSet<N> &other) const
{
assert(other.maxCount >= this->maxCount);
BitSet<N> ret(maxCount);
for (int i = 0; i < bufferSize; ++i)
{
ret.buffer[i] = this->buffer[i] & other.buffer[i];
}
return ret;
}
template <int N>
BitSet<N> &BitSet<N>::operator|=(const BitSet<N> &other)
{
assert(other.maxCount >= this->maxCount);
for (int i = 0; i < bufferSize; ++i)
{
this->buffer[i] |= other.buffer[i];
}
return *this;
}
template <int N>
int BitSet<N>::count() const
{
int ret = 0;
for (int i = 0; i < bufferSize; ++i)
{
ret += __builtin_popcountll(buffer[i]);
}
return ret;
}
template <int N>
bool BitSet<N>::empty() const
{
for (int i = 0; i < bufferSize; ++i)
{
if (__builtin_popcountll(buffer[i]))
return false;
}
return true;
}
template <int N>
bool BitSet<N>::any() const
{
for (int i = 0; i < bufferSize; ++i)
{
if (__builtin_popcountll(buffer[i]))
return true;
}
return false;
}
namespace
{
// https://stackoverflow.com/questions/33333363/built-in-mod-vs-custom-mod-function-improve-the-performance-of-modulus-op/33333636
inline int modulo(const int input, const int ceil)
{
return input >= ceil ? input % ceil : input;
}
} // namespace
template <int N>
bool BitSet<N>::test(int n) const
{
assert(n < maxCount);
return (buffer[n / 64] & (0x1ULL << (modulo(n, 64))));
}
template <int N>
void BitSet<N>::set()
{
__builtin_memset(buffer, ~0x0, bufferSizeBytes);
}
template <int N>
void BitSet<N>::set(int n)
{
assert(n < maxCount);
buffer[n / 64] |= (0x1ULL << (modulo(n, 64)));
}
template <int N>
void BitSet<N>::reset()
{
__builtin_memset(buffer, 0, bufferSizeBytes);
}
template <int N>
void BitSet<N>::reset(int n)
{
assert(n < maxCount);
buffer[n / 64] &= (~(0x1ULL << (modulo(n, 64))));
}
} // namespace sopang
#endif // BITSET_HPP