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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
#ifndef CBITSET_BITSET_H
#define CBITSET_BITSET_H
// For compatibility with MSVC with the use of `restrict`
#if (__STDC_VERSION__ >= 199901L) || \
(defined(__GNUC__) && defined(__STDC_VERSION__))
#define CBITSET_RESTRICT restrict
#else
#define CBITSET_RESTRICT
#endif // (__STDC_VERSION__ >= 199901L) || (defined(__GNUC__) &&
// defined(__STDC_VERSION__ ))
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <roaring/portability.h>
#ifdef __cplusplus
extern "C" { namespace roaring { namespace api {
#endif
struct bitset_s {
uint64_t *CBITSET_RESTRICT array;
/* For simplicity and performance, we prefer to have a size and a capacity that is a multiple of 64 bits.
* Thus we only track the size and the capacity in terms of 64-bit words allocated */
size_t arraysize;
size_t capacity;
};
typedef struct bitset_s bitset_t;
/* Create a new bitset. Return NULL in case of failure. */
bitset_t *bitset_create(void);
/* Create a new bitset able to contain size bits. Return NULL in case of
* failure. */
bitset_t *bitset_create_with_capacity(size_t size);
/* Free memory. */
void bitset_free(bitset_t *bitset);
/* Set all bits to zero. */
void bitset_clear(bitset_t *bitset);
/* Set all bits to one. */
void bitset_fill(bitset_t *bitset);
/* Create a copy */
bitset_t *bitset_copy(const bitset_t *bitset);
/* For advanced users: Resize the bitset so that it can support newarraysize * 64 bits.
* Return true in case of success, false for failure. Pad
* with zeroes new buffer areas if requested. */
bool bitset_resize(bitset_t *bitset, size_t newarraysize, bool padwithzeroes);
/* returns how many bytes of memory the backend buffer uses */
inline size_t bitset_size_in_bytes(const bitset_t *bitset) {
return bitset->arraysize * sizeof(uint64_t);
}
/* returns how many bits can be accessed */
inline size_t bitset_size_in_bits(const bitset_t *bitset) {
return bitset->arraysize * 64;
}
/* returns how many words (64-bit) of memory the backend buffer uses */
inline size_t bitset_size_in_words(const bitset_t *bitset) {
return bitset->arraysize;
}
/* For advanced users: Grow the bitset so that it can support newarraysize * 64 bits with padding.
* Return true in case of success, false for failure. */
bool bitset_grow(bitset_t *bitset, size_t newarraysize);
/* attempts to recover unused memory, return false in case of roaring_reallocation
* failure */
bool bitset_trim(bitset_t *bitset);
/* shifts all bits by 's' positions so that the bitset representing values
* 1,2,10 would represent values 1+s, 2+s, 10+s */
void bitset_shift_left(bitset_t *bitset, size_t s);
/* shifts all bits by 's' positions so that the bitset representing values
* 1,2,10 would represent values 1-s, 2-s, 10-s, negative values are deleted */
void bitset_shift_right(bitset_t *bitset, size_t s);
/* Set the ith bit. Attempts to resize the bitset if needed (may silently fail)
*/
inline void bitset_set(bitset_t *bitset, size_t i) {
size_t shiftedi = i / 64;
if (shiftedi >= bitset->arraysize) {
if (!bitset_grow(bitset, shiftedi + 1)) {
return;
}
}
bitset->array[shiftedi] |= ((uint64_t)1) << (i % 64);
}
/* Set the ith bit to the specified value. Attempts to resize the bitset if
* needed (may silently fail) */
inline void bitset_set_to_value(bitset_t *bitset, size_t i, bool flag) {
size_t shiftedi = i / 64;
uint64_t mask = ((uint64_t)1) << (i % 64);
uint64_t dynmask = ((uint64_t)flag) << (i % 64);
if (shiftedi >= bitset->arraysize) {
if (!bitset_grow(bitset, shiftedi + 1)) {
return;
}
}
uint64_t w = bitset->array[shiftedi];
w &= ~mask;
w |= dynmask;
bitset->array[shiftedi] = w;
}
/* Get the value of the ith bit. */
inline bool bitset_get(const bitset_t *bitset, size_t i) {
size_t shiftedi = i / 64;
if (shiftedi >= bitset->arraysize) {
return false;
}
return (bitset->array[shiftedi] & (((uint64_t)1) << (i % 64))) != 0;
}
/* Count number of bits set. */
size_t bitset_count(const bitset_t *bitset);
/* Find the index of the first bit set. Or zero if the bitset is empty. */
size_t bitset_minimum(const bitset_t *bitset);
/* Find the index of the last bit set. Or zero if the bitset is empty. */
size_t bitset_maximum(const bitset_t *bitset);
/* compute the union in-place (to b1), returns true if successful, to generate a
* new bitset first call bitset_copy */
bool bitset_inplace_union(bitset_t *CBITSET_RESTRICT b1,
const bitset_t *CBITSET_RESTRICT b2);
/* report the size of the union (without materializing it) */
size_t bitset_union_count(const bitset_t *CBITSET_RESTRICT b1,
const bitset_t *CBITSET_RESTRICT b2);
/* compute the intersection in-place (to b1), to generate a new bitset first
* call bitset_copy */
void bitset_inplace_intersection(bitset_t *CBITSET_RESTRICT b1,
const bitset_t *CBITSET_RESTRICT b2);
/* report the size of the intersection (without materializing it) */
size_t bitset_intersection_count(const bitset_t *CBITSET_RESTRICT b1,
const bitset_t *CBITSET_RESTRICT b2);
/* returns true if the bitsets contain no common elements */
bool bitsets_disjoint(const bitset_t *CBITSET_RESTRICT b1, const bitset_t *CBITSET_RESTRICT b2);
/* returns true if the bitsets contain any common elements */
bool bitsets_intersect(const bitset_t *CBITSET_RESTRICT b1, const bitset_t *CBITSET_RESTRICT b2);
/* returns true if b1 contains all of the set bits of b2 */
bool bitset_contains_all(const bitset_t *CBITSET_RESTRICT b1, const bitset_t *CBITSET_RESTRICT b2);
/* compute the difference in-place (to b1), to generate a new bitset first call
* bitset_copy */
void bitset_inplace_difference(bitset_t *CBITSET_RESTRICT b1,
const bitset_t *CBITSET_RESTRICT b2);
/* compute the size of the difference */
size_t bitset_difference_count(const bitset_t *CBITSET_RESTRICT b1,
const bitset_t *CBITSET_RESTRICT b2);
/* compute the symmetric difference in-place (to b1), return true if successful,
* to generate a new bitset first call bitset_copy */
bool bitset_inplace_symmetric_difference(bitset_t *CBITSET_RESTRICT b1,
const bitset_t *CBITSET_RESTRICT b2);
/* compute the size of the symmetric difference */
size_t bitset_symmetric_difference_count(const bitset_t *CBITSET_RESTRICT b1,
const bitset_t *CBITSET_RESTRICT b2);
/* iterate over the set bits
like so :
for(size_t i = 0; bitset_next_set_bit(b,&i) ; i++) {
//.....
}
*/
inline bool bitset_next_set_bit(const bitset_t *bitset, size_t *i) {
size_t x = *i / 64;
if (x >= bitset->arraysize) {
return false;
}
uint64_t w = bitset->array[x];
w >>= (*i & 63);
if (w != 0) {
*i += roaring_trailing_zeroes(w);
return true;
}
x++;
while (x < bitset->arraysize) {
w = bitset->array[x];
if (w != 0) {
*i = x * 64 + roaring_trailing_zeroes(w);
return true;
}
x++;
}
return false;
}
/* iterate over the set bits
like so :
size_t buffer[256];
size_t howmany = 0;
for(size_t startfrom = 0; (howmany = bitset_next_set_bits(b,buffer,256, &startfrom)) >
0 ; startfrom++) {
//.....
}
*/
inline size_t bitset_next_set_bits(const bitset_t *bitset, size_t *buffer,
size_t capacity, size_t *startfrom) {
if (capacity == 0) return 0; // sanity check
size_t x = *startfrom / 64;
if (x >= bitset->arraysize) {
return 0; // nothing more to iterate over
}
uint64_t w = bitset->array[x];
w >>= (*startfrom & 63);
size_t howmany = 0;
size_t base = x << 6;
while (howmany < capacity) {
while (w != 0) {
uint64_t t = w & (~w + 1);
int r = roaring_trailing_zeroes(w);
buffer[howmany++] = r + base;
if (howmany == capacity) goto end;
w ^= t;
}
x += 1;
if (x == bitset->arraysize) {
break;
}
base += 64;
w = bitset->array[x];
}
end:
if (howmany > 0) {
*startfrom = buffer[howmany - 1];
}
return howmany;
}
typedef bool (*bitset_iterator)(size_t value, void *param);
// return true if uninterrupted
inline bool bitset_for_each(const bitset_t *b, bitset_iterator iterator,
void *ptr) {
size_t base = 0;
for (size_t i = 0; i < b->arraysize; ++i) {
uint64_t w = b->array[i];
while (w != 0) {
uint64_t t = w & (~w + 1);
int r = roaring_trailing_zeroes(w);
if (!iterator(r + base, ptr)) return false;
w ^= t;
}
base += 64;
}
return true;
}
inline void bitset_print(const bitset_t *b) {
printf("{");
for (size_t i = 0; bitset_next_set_bit(b, &i); i++) {
printf("%zu, ", i);
}
printf("}");
}
#ifdef __cplusplus
} } } // extern "C" { namespace roaring { namespace api {
#endif
#endif
|