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