/src/croaring/include/roaring/roaring.h
Line | Count | Source |
1 | | /* |
2 | | * An implementation of Roaring Bitmaps in C. |
3 | | */ |
4 | | |
5 | | #ifndef ROARING_H |
6 | | #define ROARING_H |
7 | | |
8 | | #include <stdbool.h> |
9 | | #include <stddef.h> // for `size_t` |
10 | | #include <stdint.h> |
11 | | |
12 | | #include <roaring/roaring_types.h> |
13 | | |
14 | | // Include other headers after roaring_types.h |
15 | | #include <roaring/bitset/bitset.h> |
16 | | #include <roaring/containers/containers.h> |
17 | | #include <roaring/memory.h> |
18 | | #include <roaring/portability.h> |
19 | | #include <roaring/roaring_array.h> |
20 | | #include <roaring/roaring_version.h> |
21 | | |
22 | | #ifdef __cplusplus |
23 | | extern "C" { |
24 | | namespace roaring { |
25 | | namespace api { |
26 | | #endif |
27 | | |
28 | | typedef struct roaring_bitmap_s { |
29 | | roaring_array_t high_low_container; |
30 | | } roaring_bitmap_t; |
31 | | |
32 | | /** |
33 | | * Dynamically allocates a new bitmap (initially empty). |
34 | | * Returns NULL if the allocation fails. |
35 | | * Capacity is a performance hint for how many "containers" the data will need. |
36 | | * Client is responsible for calling `roaring_bitmap_free()`. |
37 | | */ |
38 | | roaring_bitmap_t *roaring_bitmap_create_with_capacity(uint32_t cap); |
39 | | |
40 | | /** |
41 | | * Dynamically allocates a new bitmap (initially empty). |
42 | | * Returns NULL if the allocation fails. |
43 | | * Client is responsible for calling `roaring_bitmap_free()`. |
44 | | */ |
45 | 0 | inline roaring_bitmap_t *roaring_bitmap_create(void) { |
46 | 0 | return roaring_bitmap_create_with_capacity(0); |
47 | 0 | } |
48 | | |
49 | | /** |
50 | | * Initialize a roaring bitmap structure in memory controlled by client. |
51 | | * Capacity is a performance hint for how many "containers" the data will need. |
52 | | * Can return false if auxiliary allocations fail when capacity greater than 0. |
53 | | */ |
54 | | bool roaring_bitmap_init_with_capacity(roaring_bitmap_t *r, uint32_t cap); |
55 | | |
56 | | /** |
57 | | * Initialize a roaring bitmap structure in memory controlled by client. |
58 | | * The bitmap will be in a "clear" state, with no auxiliary allocations. |
59 | | * Since this performs no allocations, the function will not fail. |
60 | | */ |
61 | 27.1k | inline void roaring_bitmap_init_cleared(roaring_bitmap_t *r) { |
62 | 27.1k | roaring_bitmap_init_with_capacity(r, 0); |
63 | 27.1k | } |
64 | | |
65 | | /** |
66 | | * Add all the values between min (included) and max (excluded) that are at a |
67 | | * distance k*step from min. |
68 | | * The returned pointer may be NULL in case of errors. |
69 | | */ |
70 | | roaring_bitmap_t *roaring_bitmap_from_range(uint64_t min, uint64_t max, |
71 | | uint32_t step); |
72 | | |
73 | | /** |
74 | | * Creates a new bitmap from a pointer of uint32_t integers |
75 | | * The returned pointer may be NULL in case of errors. |
76 | | */ |
77 | | roaring_bitmap_t *roaring_bitmap_of_ptr(size_t n_args, const uint32_t *vals); |
78 | | |
79 | | /** |
80 | | * Check if the bitmap contains any shared containers. |
81 | | */ |
82 | | bool roaring_contains_shared(const roaring_bitmap_t *r); |
83 | | |
84 | | /** |
85 | | * Unshare all shared containers. |
86 | | * Returns true if any unsharing was performed, false if there were no shared |
87 | | * containers. |
88 | | */ |
89 | | bool roaring_unshare_all(roaring_bitmap_t *r); |
90 | | |
91 | | /* |
92 | | * Whether you want to use copy-on-write. |
93 | | * Saves memory and avoids copies, but needs more care in a threaded context. |
94 | | * Most users should ignore this flag. |
95 | | * |
96 | | * Note: If you do turn this flag to 'true', enabling COW, then ensure that you |
97 | | * do so for all of your bitmaps, since interactions between bitmaps with and |
98 | | * without COW is unsafe. |
99 | | * |
100 | | * When setting this flag to false, if any containers are shared, they |
101 | | * are unshared (cloned) immediately. |
102 | | */ |
103 | 64.9k | inline bool roaring_bitmap_get_copy_on_write(const roaring_bitmap_t *r) { |
104 | 64.9k | return r->high_low_container.flags & ROARING_FLAG_COW; |
105 | 64.9k | } |
106 | 53.5k | inline void roaring_bitmap_set_copy_on_write(roaring_bitmap_t *r, bool cow) { |
107 | 53.5k | if (cow) { |
108 | 0 | r->high_low_container.flags |= ROARING_FLAG_COW; |
109 | 53.5k | } else { |
110 | 53.5k | if (roaring_bitmap_get_copy_on_write(r)) { |
111 | 0 | roaring_unshare_all(r); |
112 | 0 | } |
113 | 53.5k | r->high_low_container.flags &= ~ROARING_FLAG_COW; |
114 | 53.5k | } |
115 | 53.5k | } |
116 | | |
117 | | /** |
118 | | * Return a copy of the bitmap with all values shifted by offset. |
119 | | * The returned pointer may be NULL in case of errors. The caller is responsible |
120 | | * for freeing the return bitmap. |
121 | | */ |
122 | | roaring_bitmap_t *roaring_bitmap_add_offset(const roaring_bitmap_t *bm, |
123 | | int64_t offset); |
124 | | /** |
125 | | * Describe the inner structure of the bitmap. |
126 | | */ |
127 | | void roaring_bitmap_printf_describe(const roaring_bitmap_t *r); |
128 | | |
129 | | /** |
130 | | * Creates a new bitmap from a list of uint32_t integers |
131 | | * |
132 | | * This function is deprecated, use `roaring_bitmap_from` instead, which |
133 | | * doesn't require the number of elements to be passed in. |
134 | | * |
135 | | * @see roaring_bitmap_from |
136 | | */ |
137 | | CROARING_DEPRECATED roaring_bitmap_t *roaring_bitmap_of(size_t n, ...); |
138 | | |
139 | | #ifdef __cplusplus |
140 | | /** |
141 | | * Creates a new bitmap which contains all values passed in as arguments. |
142 | | * |
143 | | * To create a bitmap from a variable number of arguments, use the |
144 | | * `roaring_bitmap_of_ptr` function instead. |
145 | | */ |
146 | | // Use an immediately invoked closure, capturing by reference |
147 | | // (in case __VA_ARGS__ refers to context outside the closure) |
148 | | // Include a 0 at the beginning of the array to make the array length > 0 |
149 | | // (zero sized arrays are not valid in standard c/c++) |
150 | | #define roaring_bitmap_from(...) \ |
151 | | [&]() { \ |
152 | | const uint32_t roaring_bitmap_from_array[] = {0, __VA_ARGS__}; \ |
153 | | return roaring_bitmap_of_ptr((sizeof(roaring_bitmap_from_array) / \ |
154 | | sizeof(roaring_bitmap_from_array[0])) - \ |
155 | | 1, \ |
156 | | &roaring_bitmap_from_array[1]); \ |
157 | | }() |
158 | | #else |
159 | | /** |
160 | | * Creates a new bitmap which contains all values passed in as arguments. |
161 | | * |
162 | | * To create a bitmap from a variable number of arguments, use the |
163 | | * `roaring_bitmap_of_ptr` function instead. |
164 | | */ |
165 | | // While __VA_ARGS__ occurs twice in expansion, one of the times is in a sizeof |
166 | | // expression, which is an unevaluated context, so it's even safe in the case |
167 | | // where expressions passed have side effects (roaring64_bitmap_from(my_func(), |
168 | | // ++i)) |
169 | | // Include a 0 at the beginning of the array to make the array length > 0 |
170 | | // (zero sized arrays are not valid in standard c/c++) |
171 | | #define roaring_bitmap_from(...) \ |
172 | | roaring_bitmap_of_ptr( \ |
173 | | (sizeof((const uint32_t[]){0, __VA_ARGS__}) / sizeof(uint32_t)) - 1, \ |
174 | | &((const uint32_t[]){0, __VA_ARGS__})[1]) |
175 | | #endif |
176 | | |
177 | | /** |
178 | | * Copies a bitmap (this does memory allocation). |
179 | | * The caller is responsible for memory management. |
180 | | * The returned pointer may be NULL in case of errors. |
181 | | */ |
182 | | roaring_bitmap_t *roaring_bitmap_copy(const roaring_bitmap_t *r); |
183 | | |
184 | | /** |
185 | | * Copies a bitmap from src to dest. It is assumed that the pointer dest |
186 | | * is to an already allocated bitmap. The content of the dest bitmap is |
187 | | * freed/deleted. |
188 | | * |
189 | | * It might be preferable and simpler to call roaring_bitmap_copy except |
190 | | * that roaring_bitmap_overwrite can save on memory allocations. |
191 | | * |
192 | | * Returns true if successful, or false if there was an error. On failure, |
193 | | * the dest bitmap is left in a valid, empty state (even if it was not empty |
194 | | * before). |
195 | | */ |
196 | | bool roaring_bitmap_overwrite(roaring_bitmap_t *dest, |
197 | | const roaring_bitmap_t *src); |
198 | | |
199 | | /** |
200 | | * Print the content of the bitmap. |
201 | | */ |
202 | | void roaring_bitmap_printf(const roaring_bitmap_t *r); |
203 | | |
204 | | /** |
205 | | * Computes the intersection between two bitmaps and returns new bitmap. The |
206 | | * caller is responsible for memory management. |
207 | | * |
208 | | * Performance hint: if you are computing the intersection between several |
209 | | * bitmaps, two-by-two, it is best to start with the smallest bitmap. |
210 | | * You may also rely on roaring_bitmap_and_inplace to avoid creating |
211 | | * many temporary bitmaps. |
212 | | * The returned pointer may be NULL in case of errors. |
213 | | */ |
214 | | roaring_bitmap_t *roaring_bitmap_and(const roaring_bitmap_t *r1, |
215 | | const roaring_bitmap_t *r2); |
216 | | |
217 | | /** |
218 | | * Computes the size of the intersection between two bitmaps. |
219 | | */ |
220 | | uint64_t roaring_bitmap_and_cardinality(const roaring_bitmap_t *r1, |
221 | | const roaring_bitmap_t *r2); |
222 | | |
223 | | /** |
224 | | * Check whether two bitmaps intersect. |
225 | | */ |
226 | | bool roaring_bitmap_intersect(const roaring_bitmap_t *r1, |
227 | | const roaring_bitmap_t *r2); |
228 | | |
229 | | /** |
230 | | * Check whether a bitmap and an open range intersect. |
231 | | */ |
232 | | bool roaring_bitmap_intersect_with_range(const roaring_bitmap_t *bm, uint64_t x, |
233 | | uint64_t y); |
234 | | |
235 | | /** |
236 | | * Computes the Jaccard index between two bitmaps. (Also known as the Tanimoto |
237 | | * distance, or the Jaccard similarity coefficient) |
238 | | * |
239 | | * The Jaccard index is undefined if both bitmaps are empty. |
240 | | */ |
241 | | double roaring_bitmap_jaccard_index(const roaring_bitmap_t *r1, |
242 | | const roaring_bitmap_t *r2); |
243 | | |
244 | | /** |
245 | | * Computes the size of the union between two bitmaps. |
246 | | */ |
247 | | uint64_t roaring_bitmap_or_cardinality(const roaring_bitmap_t *r1, |
248 | | const roaring_bitmap_t *r2); |
249 | | |
250 | | /** |
251 | | * Computes the size of the difference (andnot) between two bitmaps. |
252 | | */ |
253 | | uint64_t roaring_bitmap_andnot_cardinality(const roaring_bitmap_t *r1, |
254 | | const roaring_bitmap_t *r2); |
255 | | |
256 | | /** |
257 | | * Computes the size of the symmetric difference (xor) between two bitmaps. |
258 | | */ |
259 | | uint64_t roaring_bitmap_xor_cardinality(const roaring_bitmap_t *r1, |
260 | | const roaring_bitmap_t *r2); |
261 | | |
262 | | /** |
263 | | * Inplace version of `roaring_bitmap_and()`, modifies r1 |
264 | | * r1 == r2 is allowed. |
265 | | * |
266 | | * Performance hint: if you are computing the intersection between several |
267 | | * bitmaps, two-by-two, it is best to start with the smallest bitmap. |
268 | | */ |
269 | | void roaring_bitmap_and_inplace(roaring_bitmap_t *r1, |
270 | | const roaring_bitmap_t *r2); |
271 | | |
272 | | /** |
273 | | * Computes the union between two bitmaps and returns new bitmap. The caller is |
274 | | * responsible for memory management. |
275 | | * The returned pointer may be NULL in case of errors. |
276 | | */ |
277 | | roaring_bitmap_t *roaring_bitmap_or(const roaring_bitmap_t *r1, |
278 | | const roaring_bitmap_t *r2); |
279 | | |
280 | | /** |
281 | | * Inplace version of `roaring_bitmap_or(), modifies r1. |
282 | | * TODO: decide whether r1 == r2 ok |
283 | | */ |
284 | | void roaring_bitmap_or_inplace(roaring_bitmap_t *r1, |
285 | | const roaring_bitmap_t *r2); |
286 | | |
287 | | /** |
288 | | * Compute the union of 'number' bitmaps. |
289 | | * Caller is responsible for freeing the result. |
290 | | * See also `roaring_bitmap_or_many_heap()` |
291 | | * The returned pointer may be NULL in case of errors. |
292 | | */ |
293 | | roaring_bitmap_t *roaring_bitmap_or_many(size_t number, |
294 | | const roaring_bitmap_t **rs); |
295 | | |
296 | | /** |
297 | | * Compute the union of 'number' bitmaps using a heap. This can sometimes be |
298 | | * faster than `roaring_bitmap_or_many() which uses a naive algorithm. |
299 | | * Caller is responsible for freeing the result. |
300 | | */ |
301 | | roaring_bitmap_t *roaring_bitmap_or_many_heap(uint32_t number, |
302 | | const roaring_bitmap_t **rs); |
303 | | |
304 | | /** |
305 | | * Computes the symmetric difference (xor) between two bitmaps |
306 | | * and returns new bitmap. The caller is responsible for memory management. |
307 | | * The returned pointer may be NULL in case of errors. |
308 | | */ |
309 | | roaring_bitmap_t *roaring_bitmap_xor(const roaring_bitmap_t *r1, |
310 | | const roaring_bitmap_t *r2); |
311 | | |
312 | | /** |
313 | | * Inplace version of roaring_bitmap_xor, modifies r1, r1 != r2. |
314 | | */ |
315 | | void roaring_bitmap_xor_inplace(roaring_bitmap_t *r1, |
316 | | const roaring_bitmap_t *r2); |
317 | | |
318 | | /** |
319 | | * Compute the xor of 'number' bitmaps. |
320 | | * Caller is responsible for freeing the result. |
321 | | * The returned pointer may be NULL in case of errors. |
322 | | */ |
323 | | roaring_bitmap_t *roaring_bitmap_xor_many(size_t number, |
324 | | const roaring_bitmap_t **rs); |
325 | | |
326 | | /** |
327 | | * Computes the difference (andnot) between two bitmaps and returns new bitmap. |
328 | | * Caller is responsible for freeing the result. |
329 | | * The returned pointer may be NULL in case of errors. |
330 | | */ |
331 | | roaring_bitmap_t *roaring_bitmap_andnot(const roaring_bitmap_t *r1, |
332 | | const roaring_bitmap_t *r2); |
333 | | |
334 | | /** |
335 | | * Inplace version of roaring_bitmap_andnot, modifies r1, r1 != r2. |
336 | | */ |
337 | | void roaring_bitmap_andnot_inplace(roaring_bitmap_t *r1, |
338 | | const roaring_bitmap_t *r2); |
339 | | |
340 | | /** |
341 | | * TODO: consider implementing: |
342 | | * |
343 | | * "Compute the xor of 'number' bitmaps using a heap. This can sometimes be |
344 | | * faster than roaring_bitmap_xor_many which uses a naive algorithm. Caller is |
345 | | * responsible for freeing the result."" |
346 | | * |
347 | | * roaring_bitmap_t *roaring_bitmap_xor_many_heap(uint32_t number, |
348 | | * const roaring_bitmap_t **rs); |
349 | | */ |
350 | | |
351 | | /** |
352 | | * Frees the memory. |
353 | | */ |
354 | | void roaring_bitmap_free(const roaring_bitmap_t *r); |
355 | | |
356 | | /** |
357 | | * A bit of context usable with `roaring_bitmap_*_bulk()` functions |
358 | | * |
359 | | * Should be initialized with `{0}` (or `memset()` to all zeros). |
360 | | * Callers should treat it as an opaque type. |
361 | | * |
362 | | * A context may only be used with a single bitmap |
363 | | * (unless re-initialized to zero), and any modification to a bitmap |
364 | | * (other than modifications performed with `_bulk()` functions with the context |
365 | | * passed) will invalidate any contexts associated with that bitmap. |
366 | | */ |
367 | | typedef struct roaring_bulk_context_s { |
368 | | ROARING_CONTAINER_T *container; |
369 | | int idx; |
370 | | uint16_t key; |
371 | | uint8_t typecode; |
372 | | } roaring_bulk_context_t; |
373 | | |
374 | | /** |
375 | | * Add an item, using context from a previous insert for speed optimization. |
376 | | * |
377 | | * `context` will be used to store information between calls to make bulk |
378 | | * operations faster. `*context` should be zero-initialized before the first |
379 | | * call to this function. |
380 | | * |
381 | | * Modifying the bitmap in any way (other than `-bulk` suffixed functions) |
382 | | * will invalidate the stored context, calling this function with a non-zero |
383 | | * context after doing any modification invokes undefined behavior. |
384 | | * |
385 | | * In order to exploit this optimization, the caller should call this function |
386 | | * with values with the same "key" (high 16 bits of the value) consecutively. |
387 | | */ |
388 | | void roaring_bitmap_add_bulk(roaring_bitmap_t *r, |
389 | | roaring_bulk_context_t *context, uint32_t val); |
390 | | |
391 | | /** |
392 | | * Add value n_args from pointer vals, faster than repeatedly calling |
393 | | * `roaring_bitmap_add()` |
394 | | * |
395 | | * In order to exploit this optimization, the caller should attempt to keep |
396 | | * values with the same "key" (high 16 bits of the value) as consecutive |
397 | | * elements in `vals` |
398 | | */ |
399 | | void roaring_bitmap_add_many(roaring_bitmap_t *r, size_t n_args, |
400 | | const uint32_t *vals); |
401 | | |
402 | | /** |
403 | | * Add value x |
404 | | */ |
405 | | void roaring_bitmap_add(roaring_bitmap_t *r, uint32_t x); |
406 | | |
407 | | /** |
408 | | * Add value x |
409 | | * Returns true if a new value was added, false if the value already existed. |
410 | | */ |
411 | | bool roaring_bitmap_add_checked(roaring_bitmap_t *r, uint32_t x); |
412 | | |
413 | | /** |
414 | | * Add all values in range [min, max] |
415 | | */ |
416 | | void roaring_bitmap_add_range_closed(roaring_bitmap_t *r, uint32_t min, |
417 | | uint32_t max); |
418 | | |
419 | | /** |
420 | | * Add all values in range [min, max) |
421 | | */ |
422 | | inline void roaring_bitmap_add_range(roaring_bitmap_t *r, uint64_t min, |
423 | 5.43k | uint64_t max) { |
424 | 5.43k | if (max <= min || min > (uint64_t)UINT32_MAX + 1) { |
425 | 2.96k | return; |
426 | 2.96k | } |
427 | 2.46k | roaring_bitmap_add_range_closed(r, (uint32_t)min, (uint32_t)(max - 1)); |
428 | 2.46k | } |
429 | | |
430 | | /** |
431 | | * Remove value x |
432 | | */ |
433 | | void roaring_bitmap_remove(roaring_bitmap_t *r, uint32_t x); |
434 | | |
435 | | /** |
436 | | * Remove all values in range [min, max] |
437 | | */ |
438 | | void roaring_bitmap_remove_range_closed(roaring_bitmap_t *r, uint32_t min, |
439 | | uint32_t max); |
440 | | |
441 | | /** |
442 | | * Remove all values in range [min, max) |
443 | | */ |
444 | | inline void roaring_bitmap_remove_range(roaring_bitmap_t *r, uint64_t min, |
445 | 5.43k | uint64_t max) { |
446 | 5.43k | if (max <= min || min > (uint64_t)UINT32_MAX + 1) { |
447 | 4.55k | return; |
448 | 4.55k | } |
449 | 883 | roaring_bitmap_remove_range_closed(r, (uint32_t)min, (uint32_t)(max - 1)); |
450 | 883 | } |
451 | | |
452 | | /** |
453 | | * Remove multiple values |
454 | | */ |
455 | | void roaring_bitmap_remove_many(roaring_bitmap_t *r, size_t n_args, |
456 | | const uint32_t *vals); |
457 | | |
458 | | /** |
459 | | * Remove value x |
460 | | * Returns true if a new value was removed, false if the value was not existing. |
461 | | */ |
462 | | bool roaring_bitmap_remove_checked(roaring_bitmap_t *r, uint32_t x); |
463 | | |
464 | | /** |
465 | | * Check if value is present |
466 | | */ |
467 | 355k | inline bool roaring_bitmap_contains(const roaring_bitmap_t *r, uint32_t val) { |
468 | | // For performance reasons, this function is inline and uses internal |
469 | | // functions directly. |
470 | 355k | #ifdef __cplusplus |
471 | 355k | using namespace ::roaring::internal; |
472 | 355k | #endif |
473 | 355k | const uint16_t hb = val >> 16; |
474 | | /* |
475 | | * the next function call involves a binary search and lots of branching. |
476 | | */ |
477 | 355k | int32_t i = ra_get_index(&r->high_low_container, hb); |
478 | 355k | if (i < 0) return false; |
479 | | |
480 | 354k | uint8_t typecode; |
481 | | // next call ought to be cheap |
482 | 354k | container_t *container = ra_get_container_at_index(&r->high_low_container, |
483 | 354k | (uint16_t)i, &typecode); |
484 | | // rest might be a tad expensive, possibly involving another round of binary |
485 | | // search |
486 | 354k | return container_contains(container, val & 0xFFFF, typecode); |
487 | 355k | } |
488 | | |
489 | | /** |
490 | | * Check whether a range of values from range_start (included) |
491 | | * to range_end (excluded) is present |
492 | | */ |
493 | | bool roaring_bitmap_contains_range(const roaring_bitmap_t *r, |
494 | | uint64_t range_start, uint64_t range_end); |
495 | | |
496 | | /** |
497 | | * Check whether a range of values from range_start (included) |
498 | | * to range_end (included) is present |
499 | | */ |
500 | | bool roaring_bitmap_contains_range_closed(const roaring_bitmap_t *r, |
501 | | uint32_t range_start, |
502 | | uint32_t range_end); |
503 | | |
504 | | /** |
505 | | * Check if an items is present, using context from a previous insert or search |
506 | | * for speed optimization. |
507 | | * |
508 | | * `context` will be used to store information between calls to make bulk |
509 | | * operations faster. `*context` should be zero-initialized before the first |
510 | | * call to this function. |
511 | | * |
512 | | * Modifying the bitmap in any way (other than `-bulk` suffixed functions) |
513 | | * will invalidate the stored context, calling this function with a non-zero |
514 | | * context after doing any modification invokes undefined behavior. |
515 | | * |
516 | | * In order to exploit this optimization, the caller should call this function |
517 | | * with values with the same "key" (high 16 bits of the value) consecutively. |
518 | | */ |
519 | | bool roaring_bitmap_contains_bulk(const roaring_bitmap_t *r, |
520 | | roaring_bulk_context_t *context, |
521 | | uint32_t val); |
522 | | |
523 | | /** |
524 | | * Get the cardinality of the bitmap (number of elements). |
525 | | */ |
526 | | uint64_t roaring_bitmap_get_cardinality(const roaring_bitmap_t *r); |
527 | | |
528 | | /** |
529 | | * Returns the number of elements in the range [range_start, range_end). |
530 | | */ |
531 | | uint64_t roaring_bitmap_range_cardinality(const roaring_bitmap_t *r, |
532 | | uint64_t range_start, |
533 | | uint64_t range_end); |
534 | | |
535 | | /** |
536 | | * Returns the number of elements in the range [range_start, range_end]. |
537 | | */ |
538 | | uint64_t roaring_bitmap_range_cardinality_closed(const roaring_bitmap_t *r, |
539 | | uint32_t range_start, |
540 | | uint32_t range_end); |
541 | | /** |
542 | | * Returns true if the bitmap is empty (cardinality is zero). |
543 | | */ |
544 | | bool roaring_bitmap_is_empty(const roaring_bitmap_t *r); |
545 | | |
546 | | /** |
547 | | * Empties the bitmap. It will have no auxiliary allocations (so if the bitmap |
548 | | * was initialized in client memory via roaring_bitmap_init(), then a call to |
549 | | * roaring_bitmap_clear() would be enough to "free" it) |
550 | | */ |
551 | | void roaring_bitmap_clear(roaring_bitmap_t *r); |
552 | | |
553 | | /** |
554 | | * Convert the bitmap to a sorted array, output in `ans`. |
555 | | * |
556 | | * Caller is responsible to ensure that there is enough memory allocated, e.g. |
557 | | * |
558 | | * ans = malloc(roaring_bitmap_get_cardinality(bitmap) * sizeof(uint32_t)); |
559 | | */ |
560 | | void roaring_bitmap_to_uint32_array(const roaring_bitmap_t *r, uint32_t *ans); |
561 | | |
562 | | /** |
563 | | * Store the bitmap to a bitset. This can be useful for people |
564 | | * who need the performance and simplicity of a standard bitset. |
565 | | * We assume that the input bitset is originally empty (does not |
566 | | * have any set bit). |
567 | | * |
568 | | * bitset_t * out = bitset_create(); |
569 | | * // if the bitset has content in it, call "bitset_clear(out)" |
570 | | * bool success = roaring_bitmap_to_bitset(mybitmap, out); |
571 | | * // on failure, success will be false. |
572 | | * // You can then query the bitset: |
573 | | * bool is_present = bitset_get(out, 10011 ); |
574 | | * // you must free the memory: |
575 | | * bitset_free(out); |
576 | | * |
577 | | */ |
578 | | bool roaring_bitmap_to_bitset(const roaring_bitmap_t *r, bitset_t *bitset); |
579 | | |
580 | | /** |
581 | | * Convert the bitmap to a sorted array from `offset` by `limit`, output in |
582 | | * `ans`. |
583 | | * |
584 | | * Caller is responsible to ensure that there is enough memory allocated, e.g. |
585 | | * |
586 | | * ans = malloc(roaring_bitmap_get_cardinality(limit) * sizeof(uint32_t)); |
587 | | * |
588 | | * This function always returns `true` |
589 | | * |
590 | | * For more control, see `roaring_uint32_iterator_skip` and |
591 | | * `roaring_uint32_iterator_read`, which can be used to e.g. tell how many |
592 | | * values were actually read. |
593 | | */ |
594 | | bool roaring_bitmap_range_uint32_array(const roaring_bitmap_t *r, size_t offset, |
595 | | size_t limit, uint32_t *ans); |
596 | | |
597 | | /** |
598 | | * Remove run-length encoding even when it is more space efficient. |
599 | | * Return whether a change was applied. |
600 | | */ |
601 | | bool roaring_bitmap_remove_run_compression(roaring_bitmap_t *r); |
602 | | |
603 | | /** |
604 | | * Convert array and bitmap containers to run containers when it is more |
605 | | * efficient; also convert from run containers when more space efficient. |
606 | | * |
607 | | * Returns true if the result has at least one run container. |
608 | | * Additional savings might be possible by calling `shrinkToFit()`. |
609 | | */ |
610 | | bool roaring_bitmap_run_optimize(roaring_bitmap_t *r); |
611 | | |
612 | | /** |
613 | | * If needed, reallocate memory to shrink the memory usage. |
614 | | * Returns the number of bytes saved. |
615 | | */ |
616 | | size_t roaring_bitmap_shrink_to_fit(roaring_bitmap_t *r); |
617 | | |
618 | | /** |
619 | | * Write the bitmap to an output pointer, this output buffer should refer to |
620 | | * at least `roaring_bitmap_size_in_bytes(r)` allocated bytes. |
621 | | * |
622 | | * See `roaring_bitmap_portable_serialize()` if you want a format that's |
623 | | * compatible with Java and Go implementations. This format can sometimes be |
624 | | * more space efficient than the portable form, e.g. when the data is sparse. |
625 | | * |
626 | | * Returns how many bytes written, should be `roaring_bitmap_size_in_bytes(r)`. |
627 | | * |
628 | | * This function is endian-sensitive. If you have a big-endian system (e.g., a |
629 | | * mainframe IBM s390x), the data format is going to be big-endian and not |
630 | | * compatible with little-endian systems. |
631 | | * |
632 | | * When serializing data to a file, we recommend that you also use |
633 | | * checksums so that, at deserialization, you can be confident |
634 | | * that you are recovering the correct data. |
635 | | */ |
636 | | size_t roaring_bitmap_serialize(const roaring_bitmap_t *r, char *buf); |
637 | | |
638 | | /** |
639 | | * Use with `roaring_bitmap_serialize()`. |
640 | | * |
641 | | * (See `roaring_bitmap_portable_deserialize()` if you want a format that's |
642 | | * compatible with Java and Go implementations). |
643 | | * |
644 | | * This function is endian-sensitive. If you have a big-endian system (e.g., a |
645 | | * mainframe IBM s390x), the data format is going to be big-endian and not |
646 | | * compatible with little-endian systems. |
647 | | * |
648 | | * The returned pointer may be NULL in case of errors. |
649 | | */ |
650 | | roaring_bitmap_t *roaring_bitmap_deserialize(const void *buf); |
651 | | |
652 | | /** |
653 | | * Use with `roaring_bitmap_serialize()`. |
654 | | * |
655 | | * (See `roaring_bitmap_portable_deserialize_safe()` if you want a format that's |
656 | | * compatible with Java and Go implementations). |
657 | | * |
658 | | * This function is endian-sensitive. If you have a big-endian system (e.g., a |
659 | | * mainframe IBM s390x), the data format is going to be big-endian and not |
660 | | * compatible with little-endian systems. |
661 | | * |
662 | | * The difference with `roaring_bitmap_deserialize()` is that this function |
663 | | * checks that the input buffer is a valid bitmap. If the buffer is too small, |
664 | | * NULL is returned. |
665 | | * |
666 | | * The returned pointer may be NULL in case of errors. |
667 | | */ |
668 | | roaring_bitmap_t *roaring_bitmap_deserialize_safe(const void *buf, |
669 | | size_t maxbytes); |
670 | | |
671 | | /** |
672 | | * How many bytes are required to serialize this bitmap (NOT compatible |
673 | | * with Java and Go versions) |
674 | | */ |
675 | | size_t roaring_bitmap_size_in_bytes(const roaring_bitmap_t *r); |
676 | | |
677 | | /** |
678 | | * Read bitmap from a serialized buffer. |
679 | | * In case of failure, NULL is returned. |
680 | | * |
681 | | * This function is unsafe in the sense that if there is no valid serialized |
682 | | * bitmap at the pointer, then many bytes could be read, possibly causing a |
683 | | * buffer overflow. See also roaring_bitmap_portable_deserialize_safe(). |
684 | | * |
685 | | * This is meant to be compatible with the Java and Go versions: |
686 | | * https://github.com/RoaringBitmap/RoaringFormatSpec |
687 | | * |
688 | | * This function is endian-sensitive. If you have a big-endian system (e.g., a |
689 | | * mainframe IBM s390x), the data format is going to be big-endian and not |
690 | | * compatible with little-endian systems. |
691 | | * |
692 | | * The returned pointer may be NULL in case of errors. |
693 | | */ |
694 | | roaring_bitmap_t *roaring_bitmap_portable_deserialize(const char *buf); |
695 | | |
696 | | /** |
697 | | * Read bitmap from a serialized buffer safely (reading up to maxbytes). |
698 | | * In case of failure, NULL is returned. |
699 | | * |
700 | | * This is meant to be compatible with the Java and Go versions: |
701 | | * https://github.com/RoaringBitmap/RoaringFormatSpec |
702 | | * |
703 | | * The function itself is safe in the sense that it will not cause buffer |
704 | | * overflows: it will not read beyond the scope of the provided buffer |
705 | | * (buf,maxbytes). |
706 | | * |
707 | | * However, for correct operations, it is assumed that the bitmap |
708 | | * read was once serialized from a valid bitmap (i.e., it follows the format |
709 | | * specification). If you provided an incorrect input (garbage), then the bitmap |
710 | | * read may not be in a valid state and following operations may not lead to |
711 | | * sensible results. In particular, the serialized array containers need to be |
712 | | * in sorted order, and the run containers should be in sorted non-overlapping |
713 | | * order. This is is guaranteed to happen when serializing an existing bitmap, |
714 | | * but not for random inputs. |
715 | | * |
716 | | * If the source is untrusted, you should call |
717 | | * roaring_bitmap_internal_validate to check the validity of the |
718 | | * bitmap prior to using it. Only after calling roaring_bitmap_internal_validate |
719 | | * is the bitmap considered safe for use. |
720 | | * |
721 | | * We also recommend that you use checksums to check that serialized data |
722 | | * corresponds to the serialized bitmap. The CRoaring library does not provide |
723 | | * checksumming. |
724 | | * |
725 | | * This function is endian-sensitive. If you have a big-endian system (e.g., a |
726 | | * mainframe IBM s390x), the data format is going to be big-endian and not |
727 | | * compatible with little-endian systems. |
728 | | * |
729 | | * The returned pointer may be NULL in case of errors. |
730 | | */ |
731 | | roaring_bitmap_t *roaring_bitmap_portable_deserialize_safe(const char *buf, |
732 | | size_t maxbytes); |
733 | | |
734 | | /** |
735 | | * Read bitmap from a serialized buffer. |
736 | | * In case of failure, NULL is returned. |
737 | | * |
738 | | * Bitmap returned by this function can be used in all readonly contexts. |
739 | | * Bitmap must be freed as usual, by calling roaring_bitmap_free(). |
740 | | * Underlying buffer must not be freed or modified while it backs any bitmaps. |
741 | | * |
742 | | * The function is unsafe in the following ways: |
743 | | * 1) It may execute unaligned memory accesses. |
744 | | * 2) A buffer overflow may occur if buf does not point to a valid serialized |
745 | | * bitmap. |
746 | | * |
747 | | * This is meant to be compatible with the Java and Go versions: |
748 | | * https://github.com/RoaringBitmap/RoaringFormatSpec |
749 | | * |
750 | | * This function is endian-sensitive. If you have a big-endian system (e.g., a |
751 | | * mainframe IBM s390x), the data format is going to be big-endian and not |
752 | | * compatible with little-endian systems. |
753 | | * |
754 | | * The returned pointer may be NULL in case of errors. |
755 | | */ |
756 | | roaring_bitmap_t *roaring_bitmap_portable_deserialize_frozen(const char *buf); |
757 | | |
758 | | /** |
759 | | * Check how many bytes would be read (up to maxbytes) at this pointer if there |
760 | | * is a bitmap, returns zero if there is no valid bitmap. |
761 | | * |
762 | | * This is meant to be compatible with the Java and Go versions: |
763 | | * https://github.com/RoaringBitmap/RoaringFormatSpec |
764 | | */ |
765 | | size_t roaring_bitmap_portable_deserialize_size(const char *buf, |
766 | | size_t maxbytes); |
767 | | |
768 | | /** |
769 | | * How many bytes are required to serialize this bitmap. |
770 | | * |
771 | | * This is meant to be compatible with the Java and Go versions: |
772 | | * https://github.com/RoaringBitmap/RoaringFormatSpec |
773 | | */ |
774 | | size_t roaring_bitmap_portable_size_in_bytes(const roaring_bitmap_t *r); |
775 | | |
776 | | /** |
777 | | * Write a bitmap to a char buffer. The output buffer should refer to at least |
778 | | * `roaring_bitmap_portable_size_in_bytes(r)` bytes of allocated memory. |
779 | | * |
780 | | * Returns how many bytes were written which should match |
781 | | * `roaring_bitmap_portable_size_in_bytes(r)`. |
782 | | * |
783 | | * This is meant to be compatible with the Java and Go versions: |
784 | | * https://github.com/RoaringBitmap/RoaringFormatSpec |
785 | | * |
786 | | * This function is endian-sensitive. If you have a big-endian system (e.g., a |
787 | | * mainframe IBM s390x), the data format is going to be big-endian and not |
788 | | * compatible with little-endian systems. |
789 | | * |
790 | | * When serializing data to a file, we recommend that you also use |
791 | | * checksums so that, at deserialization, you can be confident |
792 | | * that you are recovering the correct data. |
793 | | */ |
794 | | size_t roaring_bitmap_portable_serialize(const roaring_bitmap_t *r, char *buf); |
795 | | |
796 | | /* |
797 | | * "Frozen" serialization format imitates memory layout of roaring_bitmap_t. |
798 | | * Deserialized bitmap is a constant view of the underlying buffer. |
799 | | * This significantly reduces amount of allocations and copying required during |
800 | | * deserialization. |
801 | | * It can be used with memory mapped files. |
802 | | * Example can be found in benchmarks/frozen_benchmark.c |
803 | | * |
804 | | * [#####] const roaring_bitmap_t * |
805 | | * | | | |
806 | | * +----+ | +-+ |
807 | | * | | | |
808 | | * [#####################################] underlying buffer |
809 | | * |
810 | | * Note that because frozen serialization format imitates C memory layout |
811 | | * of roaring_bitmap_t, it is not fixed. It is different on big/little endian |
812 | | * platforms and can be changed in future. |
813 | | */ |
814 | | |
815 | | /** |
816 | | * Returns number of bytes required to serialize bitmap using frozen format. |
817 | | */ |
818 | | size_t roaring_bitmap_frozen_size_in_bytes(const roaring_bitmap_t *r); |
819 | | |
820 | | /** |
821 | | * Serializes bitmap using frozen format. |
822 | | * Buffer size must be at least roaring_bitmap_frozen_size_in_bytes(). |
823 | | * |
824 | | * This function is endian-sensitive. If you have a big-endian system (e.g., a |
825 | | * mainframe IBM s390x), the data format is going to be big-endian and not |
826 | | * compatible with little-endian systems. |
827 | | * |
828 | | * When serializing data to a file, we recommend that you also use |
829 | | * checksums so that, at deserialization, you can be confident |
830 | | * that you are recovering the correct data. |
831 | | */ |
832 | | void roaring_bitmap_frozen_serialize(const roaring_bitmap_t *r, char *buf); |
833 | | |
834 | | /** |
835 | | * Creates constant bitmap that is a view of a given buffer. |
836 | | * Buffer data should have been written by `roaring_bitmap_frozen_serialize()` |
837 | | * Its beginning must also be aligned by 32 bytes. |
838 | | * Length must be equal exactly to `roaring_bitmap_frozen_size_in_bytes()`. |
839 | | * In case of failure, NULL is returned. |
840 | | * |
841 | | * Bitmap returned by this function can be used in all readonly contexts. |
842 | | * Bitmap must be freed as usual, by calling roaring_bitmap_free(). |
843 | | * Underlying buffer must not be freed or modified while it backs any bitmaps. |
844 | | * |
845 | | * This function is endian-sensitive. If you have a big-endian system (e.g., a |
846 | | * mainframe IBM s390x), the data format is going to be big-endian and not |
847 | | * compatible with little-endian systems. |
848 | | */ |
849 | | const roaring_bitmap_t *roaring_bitmap_frozen_view(const char *buf, |
850 | | size_t length); |
851 | | |
852 | | /** |
853 | | * Iterate over the bitmap elements. The function iterator is called once for |
854 | | * all the values with ptr (can be NULL) as the second parameter of each call. |
855 | | * |
856 | | * `roaring_iterator` is simply a pointer to a function that returns bool |
857 | | * (true means that the iteration should continue while false means that it |
858 | | * should stop), and takes (uint32_t,void*) as inputs. |
859 | | * |
860 | | * Returns true if the roaring_iterator returned true throughout (so that all |
861 | | * data points were necessarily visited). |
862 | | * |
863 | | * Iteration is ordered: from the smallest to the largest elements. |
864 | | */ |
865 | | bool roaring_iterate(const roaring_bitmap_t *r, roaring_iterator iterator, |
866 | | void *ptr); |
867 | | |
868 | | bool roaring_iterate64(const roaring_bitmap_t *r, roaring_iterator64 iterator, |
869 | | uint64_t high_bits, void *ptr); |
870 | | |
871 | | /** |
872 | | * Return true if the two bitmaps contain the same elements. |
873 | | */ |
874 | | bool roaring_bitmap_equals(const roaring_bitmap_t *r1, |
875 | | const roaring_bitmap_t *r2); |
876 | | |
877 | | /** |
878 | | * Return true if all the elements of r1 are also in r2. |
879 | | */ |
880 | | bool roaring_bitmap_is_subset(const roaring_bitmap_t *r1, |
881 | | const roaring_bitmap_t *r2); |
882 | | |
883 | | /** |
884 | | * Return true if all the elements of r1 are also in r2, and r2 is strictly |
885 | | * greater than r1. |
886 | | */ |
887 | | bool roaring_bitmap_is_strict_subset(const roaring_bitmap_t *r1, |
888 | | const roaring_bitmap_t *r2); |
889 | | |
890 | | /** |
891 | | * (For expert users who seek high performance.) |
892 | | * |
893 | | * Computes the union between two bitmaps and returns new bitmap. The caller is |
894 | | * responsible for memory management. |
895 | | * |
896 | | * The lazy version defers some computations such as the maintenance of the |
897 | | * cardinality counts. Thus you must call `roaring_bitmap_repair_after_lazy()` |
898 | | * after executing "lazy" computations. |
899 | | * |
900 | | * It is safe to repeatedly call roaring_bitmap_lazy_or_inplace on the result. |
901 | | * |
902 | | * `bitsetconversion` is a flag which determines whether container-container |
903 | | * operations force a bitset conversion. |
904 | | * |
905 | | * The returned pointer may be NULL in case of errors. |
906 | | */ |
907 | | roaring_bitmap_t *roaring_bitmap_lazy_or(const roaring_bitmap_t *r1, |
908 | | const roaring_bitmap_t *r2, |
909 | | const bool bitsetconversion); |
910 | | |
911 | | /** |
912 | | * (For expert users who seek high performance.) |
913 | | * |
914 | | * Inplace version of roaring_bitmap_lazy_or, modifies r1. |
915 | | * |
916 | | * `bitsetconversion` is a flag which determines whether container-container |
917 | | * operations force a bitset conversion. |
918 | | */ |
919 | | void roaring_bitmap_lazy_or_inplace(roaring_bitmap_t *r1, |
920 | | const roaring_bitmap_t *r2, |
921 | | const bool bitsetconversion); |
922 | | |
923 | | /** |
924 | | * (For expert users who seek high performance.) |
925 | | * |
926 | | * Execute maintenance on a bitmap created from `roaring_bitmap_lazy_or()` |
927 | | * or modified with `roaring_bitmap_lazy_or_inplace()`. |
928 | | */ |
929 | | void roaring_bitmap_repair_after_lazy(roaring_bitmap_t *r1); |
930 | | |
931 | | /** |
932 | | * Computes the symmetric difference between two bitmaps and returns new bitmap. |
933 | | * The caller is responsible for memory management. |
934 | | * |
935 | | * The lazy version defers some computations such as the maintenance of the |
936 | | * cardinality counts. Thus you must call `roaring_bitmap_repair_after_lazy()` |
937 | | * after executing "lazy" computations. |
938 | | * |
939 | | * It is safe to repeatedly call `roaring_bitmap_lazy_xor_inplace()` on |
940 | | * the result. |
941 | | * |
942 | | * The returned pointer may be NULL in case of errors. |
943 | | */ |
944 | | roaring_bitmap_t *roaring_bitmap_lazy_xor(const roaring_bitmap_t *r1, |
945 | | const roaring_bitmap_t *r2); |
946 | | |
947 | | /** |
948 | | * (For expert users who seek high performance.) |
949 | | * |
950 | | * Inplace version of roaring_bitmap_lazy_xor, modifies r1. r1 != r2 |
951 | | */ |
952 | | void roaring_bitmap_lazy_xor_inplace(roaring_bitmap_t *r1, |
953 | | const roaring_bitmap_t *r2); |
954 | | |
955 | | /** |
956 | | * Compute the negation of the bitmap in the interval [range_start, range_end). |
957 | | * The number of negated values is range_end - range_start. |
958 | | * Areas outside the range are passed through unchanged. |
959 | | * The returned pointer may be NULL in case of errors. |
960 | | */ |
961 | | roaring_bitmap_t *roaring_bitmap_flip(const roaring_bitmap_t *r1, |
962 | | uint64_t range_start, uint64_t range_end); |
963 | | |
964 | | /** |
965 | | * Compute the negation of the bitmap in the interval [range_start, range_end]. |
966 | | * The number of negated values is range_end - range_start + 1. |
967 | | * Areas outside the range are passed through unchanged. |
968 | | * The returned pointer may be NULL in case of errors. |
969 | | */ |
970 | | roaring_bitmap_t *roaring_bitmap_flip_closed(const roaring_bitmap_t *x1, |
971 | | uint32_t range_start, |
972 | | uint32_t range_end); |
973 | | /** |
974 | | * compute (in place) the negation of the roaring bitmap within a specified |
975 | | * interval: [range_start, range_end). The number of negated values is |
976 | | * range_end - range_start. |
977 | | * Areas outside the range are passed through unchanged. |
978 | | */ |
979 | | void roaring_bitmap_flip_inplace(roaring_bitmap_t *r1, uint64_t range_start, |
980 | | uint64_t range_end); |
981 | | |
982 | | /** |
983 | | * compute (in place) the negation of the roaring bitmap within a specified |
984 | | * interval: [range_start, range_end]. The number of negated values is |
985 | | * range_end - range_start + 1. |
986 | | * Areas outside the range are passed through unchanged. |
987 | | */ |
988 | | void roaring_bitmap_flip_inplace_closed(roaring_bitmap_t *r1, |
989 | | uint32_t range_start, |
990 | | uint32_t range_end); |
991 | | |
992 | | /** |
993 | | * Selects the element at index 'rank' where the smallest element is at index 0. |
994 | | * If the size of the roaring bitmap is strictly greater than rank, then this |
995 | | * function returns true and sets element to the element of given rank. |
996 | | * Otherwise, it returns false. |
997 | | */ |
998 | | bool roaring_bitmap_select(const roaring_bitmap_t *r, uint32_t rank, |
999 | | uint32_t *element); |
1000 | | |
1001 | | /** |
1002 | | * roaring_bitmap_rank returns the number of integers that are smaller or equal |
1003 | | * to x. Thus if x is the first element, this function will return 1. If |
1004 | | * x is smaller than the smallest element, this function will return 0. |
1005 | | * |
1006 | | * The indexing convention differs between roaring_bitmap_select and |
1007 | | * roaring_bitmap_rank: roaring_bitmap_select refers to the smallest value |
1008 | | * as having index 0, whereas roaring_bitmap_rank returns 1 when ranking |
1009 | | * the smallest value. |
1010 | | */ |
1011 | | uint64_t roaring_bitmap_rank(const roaring_bitmap_t *r, uint32_t x); |
1012 | | |
1013 | | /** |
1014 | | * roaring_bitmap_rank_many is an `Bulk` version of `roaring_bitmap_rank` |
1015 | | * it puts rank value of each element in `[begin .. end)` to `ans[]` |
1016 | | * |
1017 | | * the values in `[begin .. end)` must be sorted in Ascending order; |
1018 | | * Caller is responsible to ensure that there is enough memory allocated, e.g. |
1019 | | * |
1020 | | * ans = malloc((end-begin) * sizeof(uint64_t)); |
1021 | | */ |
1022 | | void roaring_bitmap_rank_many(const roaring_bitmap_t *r, const uint32_t *begin, |
1023 | | const uint32_t *end, uint64_t *ans); |
1024 | | |
1025 | | /** |
1026 | | * Returns the index of x in the given roaring bitmap. |
1027 | | * If the roaring bitmap doesn't contain x , this function will return -1. |
1028 | | * The difference with rank function is that this function will return -1 when x |
1029 | | * is not the element of roaring bitmap, but the rank function will return a |
1030 | | * non-negative number. |
1031 | | */ |
1032 | | int64_t roaring_bitmap_get_index(const roaring_bitmap_t *r, uint32_t x); |
1033 | | |
1034 | | /** |
1035 | | * Returns the smallest value in the set, or UINT32_MAX if the set is empty. |
1036 | | */ |
1037 | | uint32_t roaring_bitmap_minimum(const roaring_bitmap_t *r); |
1038 | | |
1039 | | /** |
1040 | | * Returns the greatest value in the set, or 0 if the set is empty. |
1041 | | */ |
1042 | | uint32_t roaring_bitmap_maximum(const roaring_bitmap_t *r); |
1043 | | |
1044 | | /** |
1045 | | * (For advanced users.) |
1046 | | * |
1047 | | * Collect statistics about the bitmap, see roaring_types.h for |
1048 | | * a description of roaring_statistics_t |
1049 | | */ |
1050 | | void roaring_bitmap_statistics(const roaring_bitmap_t *r, |
1051 | | roaring_statistics_t *stat); |
1052 | | |
1053 | | /** |
1054 | | * Perform internal consistency checks. Returns true if the bitmap is |
1055 | | * consistent. It may be useful to call this after deserializing bitmaps from |
1056 | | * untrusted sources. If roaring_bitmap_internal_validate returns true, then the |
1057 | | * bitmap should be consistent and can be trusted not to cause crashes or memory |
1058 | | * corruption. |
1059 | | * |
1060 | | * Note that some operations intentionally leave bitmaps in an inconsistent |
1061 | | * state temporarily, for example, `roaring_bitmap_lazy_*` functions, until |
1062 | | * `roaring_bitmap_repair_after_lazy` is called. |
1063 | | * |
1064 | | * If reason is non-null, it will be set to a string describing the first |
1065 | | * inconsistency found if any. |
1066 | | */ |
1067 | | bool roaring_bitmap_internal_validate(const roaring_bitmap_t *r, |
1068 | | const char **reason); |
1069 | | |
1070 | | /********************* |
1071 | | * What follows is code use to iterate through values in a roaring bitmap |
1072 | | |
1073 | | roaring_bitmap_t *r =... |
1074 | | roaring_uint32_iterator_t i; |
1075 | | roaring_iterator_create(r, &i); |
1076 | | while(i.has_value) { |
1077 | | printf("value = %d\n", i.current_value); |
1078 | | roaring_uint32_iterator_advance(&i); |
1079 | | } |
1080 | | roaring_uint32_iterator_free(&i); |
1081 | | |
1082 | | Obviously, if you modify the underlying bitmap, the iterator |
1083 | | becomes invalid. So don't. |
1084 | | */ |
1085 | | |
1086 | | /** |
1087 | | * A struct used to keep iterator state. Users should only access |
1088 | | * `current_value` and `has_value`, the rest of the type should be treated as |
1089 | | * opaque. |
1090 | | */ |
1091 | | typedef struct roaring_uint32_iterator_s { |
1092 | | const roaring_bitmap_t *parent; // Owner |
1093 | | const ROARING_CONTAINER_T *container; // Current container |
1094 | | uint8_t typecode; // Typecode of current container |
1095 | | int32_t container_index; // Current container index |
1096 | | uint32_t highbits; // High 16 bits of the current value |
1097 | | roaring_container_iterator_t container_it; |
1098 | | |
1099 | | uint32_t current_value; |
1100 | | bool has_value; |
1101 | | } roaring_uint32_iterator_t; |
1102 | | |
1103 | | /** |
1104 | | * Initialize an iterator object that can be used to iterate through the values. |
1105 | | * If there is a value, then this iterator points to the first value and |
1106 | | * `it->has_value` is true. The value is in `it->current_value`. |
1107 | | */ |
1108 | | void roaring_iterator_init(const roaring_bitmap_t *r, |
1109 | | roaring_uint32_iterator_t *newit); |
1110 | | |
1111 | | /** DEPRECATED, use `roaring_iterator_init`. */ |
1112 | | CROARING_DEPRECATED static inline void roaring_init_iterator( |
1113 | 0 | const roaring_bitmap_t *r, roaring_uint32_iterator_t *newit) { |
1114 | 0 | roaring_iterator_init(r, newit); |
1115 | 0 | } Unexecuted instantiation: croaring_fuzzer_cc.cc:roaring::api::roaring_init_iterator(roaring::api::roaring_bitmap_s const*, roaring::api::roaring_uint32_iterator_s*) Unexecuted instantiation: roaring.c:roaring_init_iterator Unexecuted instantiation: croaring_fuzzer.c:roaring_init_iterator Unexecuted instantiation: roaring64.c:roaring_init_iterator |
1116 | | |
1117 | | /** |
1118 | | * Initialize an iterator object that can be used to iterate through the values. |
1119 | | * If there is a value, then this iterator points to the last value and |
1120 | | * `it->has_value` is true. The value is in `it->current_value`. |
1121 | | */ |
1122 | | void roaring_iterator_init_last(const roaring_bitmap_t *r, |
1123 | | roaring_uint32_iterator_t *newit); |
1124 | | |
1125 | | /** DEPRECATED, use `roaring_iterator_init_last`. */ |
1126 | | CROARING_DEPRECATED static inline void roaring_init_iterator_last( |
1127 | 0 | const roaring_bitmap_t *r, roaring_uint32_iterator_t *newit) { |
1128 | 0 | roaring_iterator_init_last(r, newit); |
1129 | 0 | } Unexecuted instantiation: croaring_fuzzer_cc.cc:roaring::api::roaring_init_iterator_last(roaring::api::roaring_bitmap_s const*, roaring::api::roaring_uint32_iterator_s*) Unexecuted instantiation: roaring.c:roaring_init_iterator_last Unexecuted instantiation: croaring_fuzzer.c:roaring_init_iterator_last Unexecuted instantiation: roaring64.c:roaring_init_iterator_last |
1130 | | |
1131 | | /** |
1132 | | * Create an iterator object that can be used to iterate through the values. |
1133 | | * Caller is responsible for calling `roaring_uint32_iterator_free()`. |
1134 | | * |
1135 | | * The iterator is initialized (this function calls `roaring_iterator_init()`) |
1136 | | * If there is a value, then this iterator points to the first value and |
1137 | | * `it->has_value` is true. The value is in `it->current_value`. |
1138 | | */ |
1139 | | roaring_uint32_iterator_t *roaring_iterator_create(const roaring_bitmap_t *r); |
1140 | | |
1141 | | /** DEPRECATED, use `roaring_iterator_create`. */ |
1142 | | CROARING_DEPRECATED static inline roaring_uint32_iterator_t * |
1143 | 0 | roaring_create_iterator(const roaring_bitmap_t *r) { |
1144 | 0 | return roaring_iterator_create(r); |
1145 | 0 | } Unexecuted instantiation: croaring_fuzzer_cc.cc:roaring::api::roaring_create_iterator(roaring::api::roaring_bitmap_s const*) Unexecuted instantiation: roaring.c:roaring_create_iterator Unexecuted instantiation: croaring_fuzzer.c:roaring_create_iterator Unexecuted instantiation: roaring64.c:roaring_create_iterator |
1146 | | |
1147 | | /** |
1148 | | * Advance the iterator. If there is a new value, then `it->has_value` is true. |
1149 | | * The new value is in `it->current_value`. Values are traversed in increasing |
1150 | | * orders. For convenience, returns `it->has_value`. |
1151 | | * |
1152 | | * Once `it->has_value` is false, `roaring_uint32_iterator_advance` should not |
1153 | | * be called on the iterator again. Calling `roaring_uint32_iterator_previous` |
1154 | | * is allowed. |
1155 | | */ |
1156 | | bool roaring_uint32_iterator_advance(roaring_uint32_iterator_t *it); |
1157 | | |
1158 | | /** DEPRECATED, use `roaring_uint32_iterator_advance`. */ |
1159 | | CROARING_DEPRECATED static inline bool roaring_advance_uint32_iterator( |
1160 | 0 | roaring_uint32_iterator_t *it) { |
1161 | 0 | return roaring_uint32_iterator_advance(it); |
1162 | 0 | } Unexecuted instantiation: croaring_fuzzer_cc.cc:roaring::api::roaring_advance_uint32_iterator(roaring::api::roaring_uint32_iterator_s*) Unexecuted instantiation: roaring.c:roaring_advance_uint32_iterator Unexecuted instantiation: croaring_fuzzer.c:roaring_advance_uint32_iterator Unexecuted instantiation: roaring64.c:roaring_advance_uint32_iterator |
1163 | | |
1164 | | /** |
1165 | | * Decrement the iterator. If there's a new value, then `it->has_value` is true. |
1166 | | * The new value is in `it->current_value`. Values are traversed in decreasing |
1167 | | * order. For convenience, returns `it->has_value`. |
1168 | | * |
1169 | | * Once `it->has_value` is false, `roaring_uint32_iterator_previous` should not |
1170 | | * be called on the iterator again. Calling `roaring_uint32_iterator_advance` is |
1171 | | * allowed. |
1172 | | */ |
1173 | | bool roaring_uint32_iterator_previous(roaring_uint32_iterator_t *it); |
1174 | | |
1175 | | /** DEPRECATED, use `roaring_uint32_iterator_previous`. */ |
1176 | | CROARING_DEPRECATED static inline bool roaring_previous_uint32_iterator( |
1177 | 0 | roaring_uint32_iterator_t *it) { |
1178 | 0 | return roaring_uint32_iterator_previous(it); |
1179 | 0 | } Unexecuted instantiation: croaring_fuzzer_cc.cc:roaring::api::roaring_previous_uint32_iterator(roaring::api::roaring_uint32_iterator_s*) Unexecuted instantiation: roaring.c:roaring_previous_uint32_iterator Unexecuted instantiation: croaring_fuzzer.c:roaring_previous_uint32_iterator Unexecuted instantiation: roaring64.c:roaring_previous_uint32_iterator |
1180 | | |
1181 | | /** |
1182 | | * Move the iterator to the first value >= `val`. If there is a such a value, |
1183 | | * then `it->has_value` is true. The new value is in `it->current_value`. |
1184 | | * For convenience, returns `it->has_value`. |
1185 | | */ |
1186 | | bool roaring_uint32_iterator_move_equalorlarger(roaring_uint32_iterator_t *it, |
1187 | | uint32_t val); |
1188 | | |
1189 | | /** DEPRECATED, use `roaring_uint32_iterator_move_equalorlarger`. */ |
1190 | | CROARING_DEPRECATED static inline bool |
1191 | | roaring_move_uint32_iterator_equalorlarger(roaring_uint32_iterator_t *it, |
1192 | 0 | uint32_t val) { |
1193 | 0 | return roaring_uint32_iterator_move_equalorlarger(it, val); |
1194 | 0 | } Unexecuted instantiation: croaring_fuzzer_cc.cc:roaring::api::roaring_move_uint32_iterator_equalorlarger(roaring::api::roaring_uint32_iterator_s*, unsigned int) Unexecuted instantiation: roaring.c:roaring_move_uint32_iterator_equalorlarger Unexecuted instantiation: croaring_fuzzer.c:roaring_move_uint32_iterator_equalorlarger Unexecuted instantiation: roaring64.c:roaring_move_uint32_iterator_equalorlarger |
1195 | | |
1196 | | /** |
1197 | | * Creates a copy of an iterator. |
1198 | | * Caller must free it. |
1199 | | */ |
1200 | | roaring_uint32_iterator_t *roaring_uint32_iterator_copy( |
1201 | | const roaring_uint32_iterator_t *it); |
1202 | | |
1203 | | /** DEPRECATED, use `roaring_uint32_iterator_copy`. */ |
1204 | | CROARING_DEPRECATED static inline roaring_uint32_iterator_t * |
1205 | 0 | roaring_copy_uint32_iterator(const roaring_uint32_iterator_t *it) { |
1206 | 0 | return roaring_uint32_iterator_copy(it); |
1207 | 0 | } Unexecuted instantiation: croaring_fuzzer_cc.cc:roaring::api::roaring_copy_uint32_iterator(roaring::api::roaring_uint32_iterator_s const*) Unexecuted instantiation: roaring.c:roaring_copy_uint32_iterator Unexecuted instantiation: croaring_fuzzer.c:roaring_copy_uint32_iterator Unexecuted instantiation: roaring64.c:roaring_copy_uint32_iterator |
1208 | | |
1209 | | /** |
1210 | | * Free memory following `roaring_iterator_create()` |
1211 | | */ |
1212 | | void roaring_uint32_iterator_free(roaring_uint32_iterator_t *it); |
1213 | | |
1214 | | /** DEPRECATED, use `roaring_uint32_iterator_free`. */ |
1215 | | CROARING_DEPRECATED static inline void roaring_free_uint32_iterator( |
1216 | 0 | roaring_uint32_iterator_t *it) { |
1217 | 0 | roaring_uint32_iterator_free(it); |
1218 | 0 | } Unexecuted instantiation: croaring_fuzzer_cc.cc:roaring::api::roaring_free_uint32_iterator(roaring::api::roaring_uint32_iterator_s*) Unexecuted instantiation: roaring.c:roaring_free_uint32_iterator Unexecuted instantiation: croaring_fuzzer.c:roaring_free_uint32_iterator Unexecuted instantiation: roaring64.c:roaring_free_uint32_iterator |
1219 | | |
1220 | | /** |
1221 | | * Reads next ${count} values from iterator into user-supplied ${buf}. |
1222 | | * Returns the number of read elements. |
1223 | | * This number can be smaller than ${count}, which means that iterator is |
1224 | | * drained. |
1225 | | * |
1226 | | * This function satisfies semantics of iteration and can be used together with |
1227 | | * other iterator functions. |
1228 | | * - first value is copied from ${it}->current_value |
1229 | | * - after function returns, iterator is positioned at the next element |
1230 | | */ |
1231 | | uint32_t roaring_uint32_iterator_read(roaring_uint32_iterator_t *it, |
1232 | | uint32_t *buf, uint32_t count); |
1233 | | |
1234 | | /** DEPRECATED, use `roaring_uint32_iterator_read`. */ |
1235 | | CROARING_DEPRECATED static inline uint32_t roaring_read_uint32_iterator( |
1236 | 0 | roaring_uint32_iterator_t *it, uint32_t *buf, uint32_t count) { |
1237 | 0 | return roaring_uint32_iterator_read(it, buf, count); |
1238 | 0 | } Unexecuted instantiation: croaring_fuzzer_cc.cc:roaring::api::roaring_read_uint32_iterator(roaring::api::roaring_uint32_iterator_s*, unsigned int*, unsigned int) Unexecuted instantiation: roaring.c:roaring_read_uint32_iterator Unexecuted instantiation: croaring_fuzzer.c:roaring_read_uint32_iterator Unexecuted instantiation: roaring64.c:roaring_read_uint32_iterator |
1239 | | |
1240 | | /** |
1241 | | * Skip the next ${count} values from iterator. |
1242 | | * Returns the number of values actually skipped. |
1243 | | * The number can be smaller than ${count}, which means that iterator is |
1244 | | * drained. |
1245 | | * |
1246 | | * This function is equivalent to calling `roaring_uint32_iterator_advance()` |
1247 | | * ${count} times but is much more efficient. |
1248 | | */ |
1249 | | uint32_t roaring_uint32_iterator_skip(roaring_uint32_iterator_t *it, |
1250 | | uint32_t count); |
1251 | | |
1252 | | /** |
1253 | | * Skip the previous ${count} values from iterator (move backwards). |
1254 | | * Returns the number of values actually skipped backwards. |
1255 | | * The number can be smaller than ${count}, which means that iterator reached |
1256 | | * the beginning. |
1257 | | * |
1258 | | * This function is equivalent to calling `roaring_uint32_iterator_previous()` |
1259 | | * ${count} times but is much more efficient. |
1260 | | */ |
1261 | | uint32_t roaring_uint32_iterator_skip_backward(roaring_uint32_iterator_t *it, |
1262 | | uint32_t count); |
1263 | | |
1264 | | #ifdef __cplusplus |
1265 | | } |
1266 | | } |
1267 | | } // extern "C" { namespace roaring { namespace api { |
1268 | | #endif |
1269 | | |
1270 | | #endif /* ROARING_H */ |
1271 | | |
1272 | | #ifdef __cplusplus |
1273 | | /** |
1274 | | * Best practices for C++ headers is to avoid polluting global scope. |
1275 | | * But for C compatibility when just `roaring.h` is included building as |
1276 | | * C++, default to global access for the C public API. |
1277 | | * |
1278 | | * BUT when `roaring.hh` is included instead, it sets this flag. That way |
1279 | | * explicit namespacing must be used to get the C functions. |
1280 | | * |
1281 | | * This is outside the include guard so that if you include BOTH headers, |
1282 | | * the order won't matter; you still get the global definitions. |
1283 | | */ |
1284 | | #if !defined(ROARING_API_NOT_IN_GLOBAL_NAMESPACE) |
1285 | | using namespace ::roaring::api; |
1286 | | #endif |
1287 | | #endif |
1288 | | |
1289 | | // roaring64 will include roaring.h, but we would |
1290 | | // prefer to avoid having our users include roaring64.h |
1291 | | // in addition to roaring.h. |
1292 | | #include <roaring/roaring64.h> |