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