-
Notifications
You must be signed in to change notification settings - Fork 0
/
bpf-bitset.h
265 lines (215 loc) · 5.42 KB
/
bpf-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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// -*- C++ -*-
// Copyright (C) 2016 Red Hat Inc.
//
// This file is part of systemtap, and is free software. You can
// redistribute it and/or modify it under the terms of the GNU General
// Public License (GPL); either version 2, or (at your option) any
// later version.
// This differs from std::bitset in being runtime sized, and from
// boost::dynamic_bitset in being space efficient in multiple dimensions.
//
// ??? Could be templatized to n-dimensions.
#ifndef BPF_BITSET_H
#define BPF_BITSET_H
#include <stdexcept>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <climits>
namespace bpf {
void throw_out_of_range(const char *) __attribute__((noreturn));
namespace bitset {
typedef size_t word_t;
static size_t const bits_per_word = sizeof(word_t) * CHAR_BIT;
inline size_t round_words(size_t bits)
{
return (bits + bits_per_word - 1) / bits_per_word;
}
class bit_ref
{
private:
word_t * const word;
size_t const index;
bit_ref(); // not present
public:
bit_ref(word_t *w, size_t i) : word(w), index(i) { }
void reset() { *word &= ~((word_t)1 << index); }
void set() { *word |= (word_t)1 << index; }
void flip() { *word ^= (word_t)1 << index; }
void set(bool v) { if (v) set(); else reset(); }
bool test() const { return (*word >> index) & 1; }
operator bool() const { return test(); }
bool operator!() const { return !test(); }
bool operator|= (bool o)
{
if (test())
return true;
else if (o)
{
set();
return true;
}
return false;
}
bool operator&= (bool o)
{
if (test())
{
if (o)
return true;
reset();
}
return false;
}
bool operator-= (bool o)
{
if (test())
{
if (!o)
return true;
reset();
}
return false;
}
bool operator^= (bool o)
{
if (o)
flip();
return test();
}
};
class set1;
class set1_ref;
class set1_const_ref
{
friend class set1;
friend class set1_ref;
private:
set1_const_ref(); // not present
set1_const_ref& operator= (const set1_const_ref &); // not present
protected:
word_t *data;
size_t words;
public:
static const size_t npos = -1;
set1_const_ref(word_t *d, size_t w) : data(d), words(w) { }
bool operator!= (const set1_const_ref &o) const
{
return !(*this == o);
}
size_t size() const { return words * bits_per_word; }
bool test(size_t i) const
{
size_t w = i / bits_per_word;
size_t o = i % bits_per_word;
if (w >= words)
throw_out_of_range("bpf::bitset::set1_ref::test");
return (data[w] >> o) & 1;
}
bool operator[] (size_t i) const { return test(i); }
bool empty() const;
bool operator== (const set1_const_ref &o) const;
bool is_subset_of(const set1_const_ref &o) const;
size_t find_first() const;
size_t find_next(size_t i) const;
size_t find_next_zero(size_t i) const;
};
class set1_ref : public set1_const_ref
{
private:
set1_ref(); // not present
public:
set1_ref(size_t *d, size_t w) : set1_const_ref(d, w) { }
bit_ref operator[] (size_t i)
{
size_t w = i / bits_per_word;
size_t o = i % bits_per_word;
if (w >= words)
throw_out_of_range("bpf::bitset::set1_ref::operator[]");
return bit_ref(data + w, o);
}
set1_ref& operator= (const set1_const_ref &o)
{
if (words != o.words)
throw_out_of_range("bpf::bitset::set1_ref::operator=");
memcpy(data, o.data, words * sizeof(*data));
return *this;
}
set1_ref& operator= (const set1_ref &o)
{
return operator=(static_cast<const set1_const_ref &>(o));
}
set1_ref& operator|= (const set1_const_ref &o)
{
if (words != o.words)
throw_out_of_range("bpf::bitset::set1_ref::operator|=");
for (size_t i = 0; i < words; ++i)
data[i] |= o.data[i];
return *this;
}
set1_ref& operator&= (const set1_const_ref &o)
{
if (words != o.words)
throw_out_of_range("bpf::bitset::set1_ref::operator&=");
for (size_t i = 0; i < words; ++i)
data[i] &= o.data[i];
return *this;
}
set1_ref& operator-= (const set1_const_ref &o)
{
if (words != o.words)
throw_out_of_range("bpf::bitset::set1_ref::operator-=");
for (size_t i = 0; i < words; ++i)
data[i] &= ~o.data[i];
return *this;
}
void clear() { memset(data, 0, words * sizeof(*data)); }
void reset(size_t i) { (*this)[i].reset(); }
void set(size_t i) { (*this)[i].set(); }
void set(size_t i, bool v) { (*this)[i].set(v); }
};
class set1 : public set1_ref
{
private:
set1(); // not present
public:
set1(size_t bits);
set1(const set1_const_ref &o);
~set1();
};
class set2
{
private:
size_t n1;
size_t w2;
word_t *data;
set2(); // not present
public:
set2(size_t m1, size_t m2);
set2(const set2 &o);
~set2();
size_t size() const { return n1; }
set2& operator= (const set2 &o)
{
if (n1 != o.n1 || w2 != o.w2)
throw_out_of_range("bpf::bitset::set2::operator=");
memcpy(data, o.data, n1 * w2 * sizeof(word_t));
}
set1_ref operator[] (size_t i)
{
if (i >= n1)
throw_out_of_range("set2::operator[]");
return set1_ref(data + w2 * i, w2);
}
set1_const_ref operator[] (size_t i) const
{
if (i >= n1)
throw_out_of_range("set2::operator[]");
return set1_const_ref(data + w2 * i, w2);
}
void clear() { memset(data, 0, n1 * w2 * sizeof(*data)); }
};
std::ostream& operator<< (std::ostream &o, const set1_const_ref &s);
} // namespace bitset
} // namespace bpf
#endif // BPF_BITSET_H