/src/croaring/include/roaring/roaring64.h
Line | Count | Source |
1 | | /* |
2 | | * roaring64.h |
3 | | * |
4 | | * This file declares the 64-bit Roaring bitmap API. A roaring64 bitmap stores |
5 | | * sets of 64-bit unsigned integers by partitioning the value space by high |
6 | | * bits and using Roaring containers for the lower bits inside each partition. |
7 | | * This keeps the structure compact while preserving fast membership tests, |
8 | | * insertions, iteration, and set operations over large sparse integer sets. |
9 | | */ |
10 | | #ifndef ROARING64_H |
11 | | #define ROARING64_H |
12 | | |
13 | | #include <stdbool.h> |
14 | | #include <stddef.h> |
15 | | #include <stdint.h> |
16 | | |
17 | | #include <roaring/memory.h> |
18 | | #include <roaring/portability.h> |
19 | | #include <roaring/roaring.h> |
20 | | #include <roaring/roaring_types.h> |
21 | | |
22 | | #ifdef __cplusplus |
23 | | extern "C" { |
24 | | namespace roaring { |
25 | | namespace api { |
26 | | #endif |
27 | | |
28 | | /** An opaque 64-bit Roaring bitmap. Create one with `roaring64_bitmap_create()` |
29 | | * and release it with `roaring64_bitmap_free()`. */ |
30 | | typedef struct roaring64_bitmap_s roaring64_bitmap_t; |
31 | | /** Internal leaf type, exposed only for use inside `roaring64_bulk_context_t`. |
32 | | * Callers should treat it as opaque. */ |
33 | | typedef uint64_t roaring64_leaf_t; |
34 | | /** An opaque iterator over a 64-bit bitmap. See `roaring64_iterator_create()`. |
35 | | */ |
36 | | typedef struct roaring64_iterator_s roaring64_iterator_t; |
37 | | |
38 | | /** The leading members of `roaring64_iterator_t`, so that |
39 | | * `roaring64_iterator_value()` and `roaring64_iterator_has_value()` can be |
40 | | * read without a call. The iterator itself stays opaque; do not declare one of |
41 | | * these, and do not rely on the layout beyond these two members. */ |
42 | | typedef struct roaring64_iterator_public_s { |
43 | | uint64_t value; |
44 | | bool has_value; |
45 | | } roaring64_iterator_public_t; |
46 | | |
47 | | /** |
48 | | * A bit of context usable with `roaring64_bitmap_*_bulk()` functions. |
49 | | * |
50 | | * Should be initialized with `{0}` (or `memset()` to all zeros). |
51 | | * Callers should treat it as an opaque type. |
52 | | * |
53 | | * A context may only be used with a single bitmap (unless re-initialized to |
54 | | * zero), and any modification to a bitmap (other than modifications performed |
55 | | * with `_bulk()` functions with the context passed) will invalidate any |
56 | | * contexts associated with that bitmap. |
57 | | */ |
58 | | typedef struct roaring64_bulk_context_s { |
59 | | uint8_t high_bytes[6]; |
60 | | roaring64_leaf_t *leaf; |
61 | | } roaring64_bulk_context_t; |
62 | | |
63 | | /** |
64 | | * Dynamically allocates a new bitmap (initially empty). |
65 | | * Client is responsible for calling `roaring64_bitmap_free()`. |
66 | | * The returned pointer may be NULL in case of errors. |
67 | | */ |
68 | | roaring64_bitmap_t *roaring64_bitmap_create(void); |
69 | | void roaring64_bitmap_free(roaring64_bitmap_t *r); |
70 | | |
71 | | /** |
72 | | * Returns a copy of a bitmap. |
73 | | * The returned pointer may be NULL in case of errors. |
74 | | * |
75 | | * The copy may keep some of the free space that removals left in `r`; call |
76 | | * `roaring64_bitmap_shrink_to_fit` on it for a compact bitmap. |
77 | | */ |
78 | | roaring64_bitmap_t *roaring64_bitmap_copy(const roaring64_bitmap_t *r); |
79 | | |
80 | | /** |
81 | | * Copies a bitmap from src to dest. It is assumed that the pointer dest |
82 | | * is to an already allocated bitmap. The content of the dest bitmap is |
83 | | * freed/deleted. dest == src leaves it unchanged. |
84 | | * |
85 | | * It might be preferable and simpler to call roaring64_bitmap_copy except |
86 | | * that roaring64_bitmap_overwrite can save on memory allocations. |
87 | | * |
88 | | * As with roaring64_bitmap_copy, dest may keep some of the free space that |
89 | | * removals left in src. |
90 | | */ |
91 | | void roaring64_bitmap_overwrite(roaring64_bitmap_t *dest, |
92 | | const roaring64_bitmap_t *src); |
93 | | |
94 | | /** |
95 | | * Creates a new bitmap of a pointer to N 64-bit integers. |
96 | | */ |
97 | | roaring64_bitmap_t *roaring64_bitmap_of_ptr(size_t n_args, |
98 | | const uint64_t *vals); |
99 | | |
100 | | #ifdef __cplusplus |
101 | | /** |
102 | | * Creates a new bitmap which contains all values passed in as arguments. |
103 | | * |
104 | | * To create a bitmap from a variable number of arguments, use the |
105 | | * `roaring64_bitmap_of_ptr` function instead. |
106 | | */ |
107 | | // Use an immediately invoked closure, capturing by reference |
108 | | // (in case __VA_ARGS__ refers to context outside the closure) |
109 | | // Include a 0 at the beginning of the array to make the array length > 0 |
110 | | // (zero sized arrays are not valid in standard c/c++) |
111 | | #define roaring64_bitmap_from(...) \ |
112 | | [&]() { \ |
113 | | const uint64_t roaring64_bitmap_from_array[] = {0, __VA_ARGS__}; \ |
114 | | return roaring64_bitmap_of_ptr( \ |
115 | | (sizeof(roaring64_bitmap_from_array) / \ |
116 | | sizeof(roaring64_bitmap_from_array[0])) - \ |
117 | | 1, \ |
118 | | &roaring64_bitmap_from_array[1]); \ |
119 | | }() |
120 | | #else |
121 | | /** |
122 | | * Creates a new bitmap which contains all values passed in as arguments. |
123 | | * |
124 | | * To create a bitmap from a variable number of arguments, use the |
125 | | * `roaring64_bitmap_of_ptr` function instead. |
126 | | */ |
127 | | // While __VA_ARGS__ occurs twice in expansion, one of the times is in a sizeof |
128 | | // expression, which is an unevaluated context, so it's even safe in the case |
129 | | // where expressions passed have side effects (roaring64_bitmap_from(my_func(), |
130 | | // ++i)) |
131 | | // Include a 0 at the beginning of the array to make the array length > 0 |
132 | | // (zero sized arrays are not valid in standard c/c++) |
133 | | #define roaring64_bitmap_from(...) \ |
134 | | roaring64_bitmap_of_ptr( \ |
135 | | (sizeof((const uint64_t[]){0, __VA_ARGS__}) / sizeof(uint64_t)) - 1, \ |
136 | | &((const uint64_t[]){0, __VA_ARGS__})[1]) |
137 | | #endif |
138 | | |
139 | | /** |
140 | | * Create a new bitmap by moving containers from a 32 bit roaring bitmap. |
141 | | * Containers shared with other copy-on-write bitmaps are copied. |
142 | | * |
143 | | * After calling this function, the original bitmap will be empty, and the |
144 | | * returned bitmap will contain all the values from the original bitmap. |
145 | | */ |
146 | | roaring64_bitmap_t *roaring64_bitmap_move_from_roaring32(roaring_bitmap_t *r); |
147 | | |
148 | | /** |
149 | | * Create a new bitmap containing all the values in [min, max) that are at a |
150 | | * distance k*step from min. |
151 | | * The returned pointer may be NULL in case of errors. |
152 | | */ |
153 | | roaring64_bitmap_t *roaring64_bitmap_from_range(uint64_t min, uint64_t max, |
154 | | uint64_t step); |
155 | | |
156 | | /** |
157 | | * Adds the provided value to the bitmap. |
158 | | */ |
159 | | void roaring64_bitmap_add(roaring64_bitmap_t *r, uint64_t val); |
160 | | |
161 | | /** |
162 | | * Adds the provided value to the bitmap. |
163 | | * Returns true if a new value was added, false if the value already existed. |
164 | | */ |
165 | | bool roaring64_bitmap_add_checked(roaring64_bitmap_t *r, uint64_t val); |
166 | | |
167 | | /** |
168 | | * Add an item, using context from a previous insert for faster insertion. |
169 | | * |
170 | | * `context` will be used to store information between calls to make bulk |
171 | | * operations faster. `*context` should be zero-initialized before the first |
172 | | * call to this function. |
173 | | * |
174 | | * Modifying the bitmap in any way (other than `-bulk` suffixed functions) |
175 | | * will invalidate the stored context, calling this function with a non-zero |
176 | | * context after doing any modification invokes undefined behavior. |
177 | | * |
178 | | * In order to exploit this optimization, the caller should call this function |
179 | | * with values with the same high 48 bits of the value consecutively. |
180 | | */ |
181 | | void roaring64_bitmap_add_bulk(roaring64_bitmap_t *r, |
182 | | roaring64_bulk_context_t *context, uint64_t val); |
183 | | |
184 | | /** |
185 | | * Add `n_args` values from `vals`, faster than repeatedly calling |
186 | | * `roaring64_bitmap_add()` |
187 | | * |
188 | | * In order to exploit this optimization, the caller should attempt to keep |
189 | | * values with the same high 48 bits of the value as consecutive elements in |
190 | | * `vals`. |
191 | | */ |
192 | | void roaring64_bitmap_add_many(roaring64_bitmap_t *r, size_t n_args, |
193 | | const uint64_t *vals); |
194 | | |
195 | | /** |
196 | | * Add all values in range [min, max). |
197 | | */ |
198 | | void roaring64_bitmap_add_range(roaring64_bitmap_t *r, uint64_t min, |
199 | | uint64_t max); |
200 | | |
201 | | /** |
202 | | * Add all values in range [min, max]. |
203 | | */ |
204 | | void roaring64_bitmap_add_range_closed(roaring64_bitmap_t *r, uint64_t min, |
205 | | uint64_t max); |
206 | | |
207 | | /** |
208 | | * Removes a value from the bitmap if present. |
209 | | */ |
210 | | void roaring64_bitmap_remove(roaring64_bitmap_t *r, uint64_t val); |
211 | | |
212 | | /** |
213 | | * Removes a value from the bitmap if present, returns true if the value was |
214 | | * removed and false if the value was not present. |
215 | | */ |
216 | | bool roaring64_bitmap_remove_checked(roaring64_bitmap_t *r, uint64_t val); |
217 | | |
218 | | /** |
219 | | * Remove an item, using context from a previous insert for faster removal. |
220 | | * |
221 | | * `context` will be used to store information between calls to make bulk |
222 | | * operations faster. `*context` should be zero-initialized before the first |
223 | | * call to this function. |
224 | | * |
225 | | * Modifying the bitmap in any way (other than `-bulk` suffixed functions) |
226 | | * will invalidate the stored context, calling this function with a non-zero |
227 | | * context after doing any modification invokes undefined behavior. |
228 | | * |
229 | | * In order to exploit this optimization, the caller should call this function |
230 | | * with values with the same high 48 bits of the value consecutively. |
231 | | */ |
232 | | void roaring64_bitmap_remove_bulk(roaring64_bitmap_t *r, |
233 | | roaring64_bulk_context_t *context, |
234 | | uint64_t val); |
235 | | |
236 | | /** |
237 | | * Remove `n_args` values from `vals`, faster than repeatedly calling |
238 | | * `roaring64_bitmap_remove()` |
239 | | * |
240 | | * In order to exploit this optimization, the caller should attempt to keep |
241 | | * values with the same high 48 bits of the value as consecutive elements in |
242 | | * `vals`. |
243 | | */ |
244 | | void roaring64_bitmap_remove_many(roaring64_bitmap_t *r, size_t n_args, |
245 | | const uint64_t *vals); |
246 | | |
247 | | /** |
248 | | * Remove all values in range [min, max). |
249 | | */ |
250 | | void roaring64_bitmap_remove_range(roaring64_bitmap_t *r, uint64_t min, |
251 | | uint64_t max); |
252 | | |
253 | | /** |
254 | | * Remove all values in range [min, max]. |
255 | | */ |
256 | | void roaring64_bitmap_remove_range_closed(roaring64_bitmap_t *r, uint64_t min, |
257 | | uint64_t max); |
258 | | |
259 | | /** |
260 | | * Empties the bitmap. |
261 | | */ |
262 | | void roaring64_bitmap_clear(roaring64_bitmap_t *r); |
263 | | |
264 | | /** |
265 | | * Returns true if the provided value is present. |
266 | | */ |
267 | | bool roaring64_bitmap_contains(const roaring64_bitmap_t *r, uint64_t val); |
268 | | |
269 | | /** |
270 | | * Returns true if all values in the range [min, max) are present. |
271 | | */ |
272 | | bool roaring64_bitmap_contains_range(const roaring64_bitmap_t *r, uint64_t min, |
273 | | uint64_t max); |
274 | | |
275 | | /** |
276 | | * Returns true if all values in the range [min, max] are present. |
277 | | */ |
278 | | bool roaring64_bitmap_contains_range_closed(const roaring64_bitmap_t *r, |
279 | | uint64_t min, uint64_t max); |
280 | | |
281 | | /** |
282 | | * Check if an item is present using context from a previous insert or search |
283 | | * for faster search. |
284 | | * |
285 | | * `context` will be used to store information between calls to make bulk |
286 | | * operations faster. `*context` should be zero-initialized before the first |
287 | | * call to this function. |
288 | | * |
289 | | * Modifying the bitmap in any way (other than `-bulk` suffixed functions) |
290 | | * will invalidate the stored context, calling this function with a non-zero |
291 | | * context after doing any modification invokes undefined behavior. |
292 | | * |
293 | | * In order to exploit this optimization, the caller should call this function |
294 | | * with values with the same high 48 bits of the value consecutively. |
295 | | */ |
296 | | bool roaring64_bitmap_contains_bulk(const roaring64_bitmap_t *r, |
297 | | roaring64_bulk_context_t *context, |
298 | | uint64_t val); |
299 | | |
300 | | /** |
301 | | * Selects the element at index 'rank' where the smallest element is at index 0. |
302 | | * If the size of the bitmap is strictly greater than rank, then this function |
303 | | * returns true and sets element to the element of given rank. Otherwise, it |
304 | | * returns false. |
305 | | */ |
306 | | bool roaring64_bitmap_select(const roaring64_bitmap_t *r, uint64_t rank, |
307 | | uint64_t *element); |
308 | | |
309 | | /** |
310 | | * Returns the number of integers that are smaller or equal to x. Thus if x is |
311 | | * the first element, this function will return 1. If x is smaller than the |
312 | | * smallest element, this function will return 0. |
313 | | * |
314 | | * The indexing convention differs between roaring64_bitmap_select and |
315 | | * roaring64_bitmap_rank: roaring_bitmap64_select refers to the smallest value |
316 | | * as having index 0, whereas roaring64_bitmap_rank returns 1 when ranking |
317 | | * the smallest value. |
318 | | */ |
319 | | uint64_t roaring64_bitmap_rank(const roaring64_bitmap_t *r, uint64_t val); |
320 | | |
321 | | /** |
322 | | * Returns true if the given value is in the bitmap, and sets `out_index` to the |
323 | | * (0-based) index of the value in the bitmap. Returns false if the value is not |
324 | | * in the bitmap. |
325 | | */ |
326 | | bool roaring64_bitmap_get_index(const roaring64_bitmap_t *r, uint64_t val, |
327 | | uint64_t *out_index); |
328 | | |
329 | | /** |
330 | | * Returns the number of values in the bitmap. |
331 | | */ |
332 | | uint64_t roaring64_bitmap_get_cardinality(const roaring64_bitmap_t *r); |
333 | | |
334 | | /** |
335 | | * Returns the number of elements in the range [min, max). |
336 | | */ |
337 | | uint64_t roaring64_bitmap_range_cardinality(const roaring64_bitmap_t *r, |
338 | | uint64_t min, uint64_t max); |
339 | | |
340 | | /** |
341 | | * Returns the number of elements in the range [min, max] |
342 | | */ |
343 | | uint64_t roaring64_bitmap_range_closed_cardinality(const roaring64_bitmap_t *r, |
344 | | uint64_t min, uint64_t max); |
345 | | |
346 | | /** |
347 | | * Returns true if the bitmap is empty (cardinality is zero). |
348 | | */ |
349 | | bool roaring64_bitmap_is_empty(const roaring64_bitmap_t *r); |
350 | | |
351 | | /** |
352 | | * Returns the smallest value in the set, or UINT64_MAX if the set is empty. |
353 | | */ |
354 | | uint64_t roaring64_bitmap_minimum(const roaring64_bitmap_t *r); |
355 | | |
356 | | /** |
357 | | * Returns the largest value in the set, or 0 if empty. |
358 | | */ |
359 | | uint64_t roaring64_bitmap_maximum(const roaring64_bitmap_t *r); |
360 | | |
361 | | /** |
362 | | * Remove run-length encoding even when it is more space efficient. |
363 | | * Return whether a change was applied. |
364 | | */ |
365 | | bool roaring64_bitmap_remove_run_compression(roaring64_bitmap_t *r); |
366 | | |
367 | | /** |
368 | | * Convert array and bitmap containers to run containers when it is more |
369 | | * efficient; also convert from run containers when more space efficient. |
370 | | * |
371 | | * Adding or removing single values (for example with roaring64_bitmap_add or |
372 | | * roaring64_bitmap_remove) does not convert a run container back to an array or |
373 | | * bitmap container when its runs stop being the most compact encoding. Such a |
374 | | * container can then use more memory and serialized space than needed: in the |
375 | | * worst case, the runs of one 65536-value chunk can take about 128 KiB where a |
376 | | * bitmap container takes 8 KiB. Calling this function again after such |
377 | | * modifications recovers that space. |
378 | | * |
379 | | * Returns true if the result has at least one run container. |
380 | | */ |
381 | | bool roaring64_bitmap_run_optimize(roaring64_bitmap_t *r); |
382 | | |
383 | | /** |
384 | | * Shrinks internal arrays to eliminate any unused capacity. Returns the number |
385 | | * of bytes freed. |
386 | | */ |
387 | | size_t roaring64_bitmap_shrink_to_fit(roaring64_bitmap_t *r); |
388 | | |
389 | | /** |
390 | | * (For advanced users.) |
391 | | * Collect statistics about the bitmap |
392 | | */ |
393 | | void roaring64_bitmap_statistics(const roaring64_bitmap_t *r, |
394 | | roaring64_statistics_t *stat); |
395 | | |
396 | | /** |
397 | | * Perform internal consistency checks. |
398 | | * |
399 | | * Returns true if the bitmap is consistent. It may be useful to call this |
400 | | * after deserializing bitmaps from untrusted sources. If |
401 | | * roaring64_bitmap_internal_validate returns true, then the bitmap is |
402 | | * consistent and can be trusted not to cause crashes or memory corruption. |
403 | | * |
404 | | * If reason is non-null, it will be set to a string describing the first |
405 | | * inconsistency found if any. |
406 | | */ |
407 | | bool roaring64_bitmap_internal_validate(const roaring64_bitmap_t *r, |
408 | | const char **reason); |
409 | | |
410 | | /** |
411 | | * Return true if the two bitmaps contain the same elements. |
412 | | */ |
413 | | bool roaring64_bitmap_equals(const roaring64_bitmap_t *r1, |
414 | | const roaring64_bitmap_t *r2); |
415 | | |
416 | | /** |
417 | | * Return true if all the elements of r1 are also in r2. |
418 | | */ |
419 | | bool roaring64_bitmap_is_subset(const roaring64_bitmap_t *r1, |
420 | | const roaring64_bitmap_t *r2); |
421 | | |
422 | | /** |
423 | | * Return true if all the elements of r1 are also in r2, and r2 is strictly |
424 | | * greater than r1. |
425 | | */ |
426 | | bool roaring64_bitmap_is_strict_subset(const roaring64_bitmap_t *r1, |
427 | | const roaring64_bitmap_t *r2); |
428 | | |
429 | | /** |
430 | | * Computes the intersection between two bitmaps and returns new bitmap. The |
431 | | * caller is responsible for free-ing the result. |
432 | | * |
433 | | * Performance hint: if you are computing the intersection between several |
434 | | * bitmaps, two-by-two, it is best to start with the smallest bitmaps. You may |
435 | | * also rely on roaring64_bitmap_and_inplace to avoid creating many temporary |
436 | | * bitmaps. |
437 | | * |
438 | | * The returned pointer may be NULL in case of errors. |
439 | | */ |
440 | | roaring64_bitmap_t *roaring64_bitmap_and(const roaring64_bitmap_t *r1, |
441 | | const roaring64_bitmap_t *r2); |
442 | | |
443 | | /** |
444 | | * Computes the size of the intersection between two bitmaps. |
445 | | */ |
446 | | uint64_t roaring64_bitmap_and_cardinality(const roaring64_bitmap_t *r1, |
447 | | const roaring64_bitmap_t *r2); |
448 | | |
449 | | /** |
450 | | * In-place version of `roaring64_bitmap_and()`, modifies `r1`. `r1` and `r2` |
451 | | * are allowed to be equal. |
452 | | * |
453 | | * Performance hint: if you are computing the intersection between several |
454 | | * bitmaps, two-by-two, it is best to start with the smallest bitmaps. |
455 | | */ |
456 | | void roaring64_bitmap_and_inplace(roaring64_bitmap_t *r1, |
457 | | const roaring64_bitmap_t *r2); |
458 | | |
459 | | /** |
460 | | * Check whether two bitmaps intersect. |
461 | | */ |
462 | | bool roaring64_bitmap_intersect(const roaring64_bitmap_t *r1, |
463 | | const roaring64_bitmap_t *r2); |
464 | | |
465 | | /** |
466 | | * Check whether a bitmap intersects the range [min, max). |
467 | | */ |
468 | | bool roaring64_bitmap_intersect_with_range(const roaring64_bitmap_t *r, |
469 | | uint64_t min, uint64_t max); |
470 | | |
471 | | /** |
472 | | * Computes the Jaccard index between two bitmaps. (Also known as the Tanimoto |
473 | | * distance, or the Jaccard similarity coefficient) |
474 | | * |
475 | | * The Jaccard index is undefined if both bitmaps are empty. |
476 | | */ |
477 | | double roaring64_bitmap_jaccard_index(const roaring64_bitmap_t *r1, |
478 | | const roaring64_bitmap_t *r2); |
479 | | |
480 | | /** |
481 | | * Computes the union between two bitmaps and returns new bitmap. The caller is |
482 | | * responsible for free-ing the result. |
483 | | * The returned pointer may be NULL in case of errors. |
484 | | */ |
485 | | roaring64_bitmap_t *roaring64_bitmap_or(const roaring64_bitmap_t *r1, |
486 | | const roaring64_bitmap_t *r2); |
487 | | |
488 | | /** |
489 | | * Computes the size of the union between two bitmaps. |
490 | | */ |
491 | | uint64_t roaring64_bitmap_or_cardinality(const roaring64_bitmap_t *r1, |
492 | | const roaring64_bitmap_t *r2); |
493 | | |
494 | | /** |
495 | | * In-place version of `roaring64_bitmap_or(), modifies `r1`. `r1` and `r2` |
496 | | * are allowed to be equal. |
497 | | */ |
498 | | void roaring64_bitmap_or_inplace(roaring64_bitmap_t *r1, |
499 | | const roaring64_bitmap_t *r2); |
500 | | |
501 | | /** |
502 | | * Computes the symmetric difference (xor) between two bitmaps and returns a new |
503 | | * bitmap. The caller is responsible for free-ing the result. |
504 | | * The returned pointer may be NULL in case of errors. |
505 | | */ |
506 | | roaring64_bitmap_t *roaring64_bitmap_xor(const roaring64_bitmap_t *r1, |
507 | | const roaring64_bitmap_t *r2); |
508 | | |
509 | | /** |
510 | | * Computes the size of the symmetric difference (xor) between two bitmaps. |
511 | | */ |
512 | | uint64_t roaring64_bitmap_xor_cardinality(const roaring64_bitmap_t *r1, |
513 | | const roaring64_bitmap_t *r2); |
514 | | |
515 | | /** |
516 | | * In-place version of `roaring64_bitmap_xor()`, modifies `r1` (emptied when |
517 | | * `r1 == r2`). |
518 | | */ |
519 | | void roaring64_bitmap_xor_inplace(roaring64_bitmap_t *r1, |
520 | | const roaring64_bitmap_t *r2); |
521 | | |
522 | | /** |
523 | | * Computes the difference (andnot) between two bitmaps and returns a new |
524 | | * bitmap. The caller is responsible for free-ing the result. |
525 | | * The returned pointer may be NULL in case of errors. |
526 | | */ |
527 | | roaring64_bitmap_t *roaring64_bitmap_andnot(const roaring64_bitmap_t *r1, |
528 | | const roaring64_bitmap_t *r2); |
529 | | |
530 | | /** |
531 | | * Computes the size of the difference (andnot) between two bitmaps. |
532 | | */ |
533 | | uint64_t roaring64_bitmap_andnot_cardinality(const roaring64_bitmap_t *r1, |
534 | | const roaring64_bitmap_t *r2); |
535 | | |
536 | | /** |
537 | | * In-place version of `roaring64_bitmap_andnot()`, modifies `r1` (emptied when |
538 | | * `r1 == r2`). |
539 | | */ |
540 | | void roaring64_bitmap_andnot_inplace(roaring64_bitmap_t *r1, |
541 | | const roaring64_bitmap_t *r2); |
542 | | |
543 | | /** |
544 | | * Compute the negation of the bitmap in the interval [min, max). |
545 | | * The number of negated values is `max - min`. Areas outside the range are |
546 | | * passed through unchanged. |
547 | | * The returned pointer may be NULL in case of errors. |
548 | | */ |
549 | | roaring64_bitmap_t *roaring64_bitmap_flip(const roaring64_bitmap_t *r, |
550 | | uint64_t min, uint64_t max); |
551 | | |
552 | | /** |
553 | | * Compute the negation of the bitmap in the interval [min, max]. |
554 | | * The number of negated values is `max - min + 1`. Areas outside the range are |
555 | | * passed through unchanged. |
556 | | * The returned pointer may be NULL in case of errors. |
557 | | */ |
558 | | roaring64_bitmap_t *roaring64_bitmap_flip_closed(const roaring64_bitmap_t *r, |
559 | | uint64_t min, uint64_t max); |
560 | | |
561 | | /** |
562 | | * In-place version of `roaring64_bitmap_flip`. Compute the negation of the |
563 | | * bitmap in the interval [min, max). The number of negated values is `max - |
564 | | * min`. Areas outside the range are passed through unchanged. |
565 | | */ |
566 | | void roaring64_bitmap_flip_inplace(roaring64_bitmap_t *r, uint64_t min, |
567 | | uint64_t max); |
568 | | /** |
569 | | * In-place version of `roaring64_bitmap_flip_closed`. Compute the negation of |
570 | | * the bitmap in the interval [min, max]. The number of negated values is `max - |
571 | | * min + 1`. Areas outside the range are passed through unchanged. |
572 | | */ |
573 | | void roaring64_bitmap_flip_closed_inplace(roaring64_bitmap_t *r, uint64_t min, |
574 | | uint64_t max); |
575 | | /** |
576 | | * Return a copy of the bitmap with all values shifted by offset. |
577 | | * |
578 | | * If `positive` is true, the shift is added, otherwise subtracted. Values that |
579 | | * overflow or underflow uint64_t are dropped. The caller is responsible for |
580 | | * freeing the returned bitmap. |
581 | | */ |
582 | | roaring64_bitmap_t *roaring64_bitmap_add_offset_signed( |
583 | | const roaring64_bitmap_t *r, bool positive, uint64_t offset); |
584 | | |
585 | | /** |
586 | | * Return a copy of the bitmap with all values shifted up by offset. |
587 | | * |
588 | | * Values that overflow or underflow uint64_t are dropped. The caller is |
589 | | * responsible for freeing the returned bitmap. |
590 | | */ |
591 | | static inline roaring64_bitmap_t *roaring64_bitmap_add_offset( |
592 | 0 | const roaring64_bitmap_t *r, uint64_t offset) { |
593 | 0 | return roaring64_bitmap_add_offset_signed(r, true, offset); |
594 | 0 | } Unexecuted instantiation: croaring_fuzzer_cc.cc:roaring::api::roaring64_bitmap_add_offset(roaring::api::roaring64_bitmap_s const*, unsigned long) Unexecuted instantiation: roaring.c:roaring64_bitmap_add_offset |
595 | | |
596 | | /** |
597 | | * Return a copy of the bitmap with all values shifted down by offset. |
598 | | * |
599 | | * Values that overflow or underflow uint64_t are dropped. The caller is |
600 | | * responsible for freeing the returned bitmap. |
601 | | */ |
602 | | static inline roaring64_bitmap_t *roaring64_bitmap_sub_offset( |
603 | 0 | const roaring64_bitmap_t *r, uint64_t offset) { |
604 | 0 | return roaring64_bitmap_add_offset_signed(r, false, offset); |
605 | 0 | } Unexecuted instantiation: croaring_fuzzer_cc.cc:roaring::api::roaring64_bitmap_sub_offset(roaring::api::roaring64_bitmap_s const*, unsigned long) Unexecuted instantiation: roaring.c:roaring64_bitmap_sub_offset |
606 | | |
607 | | /** |
608 | | * How many bytes are required to serialize this bitmap. |
609 | | * |
610 | | * This is meant to be compatible with other languages: |
611 | | * https://github.com/RoaringBitmap/RoaringFormatSpec#extension-for-64-bit-implementations |
612 | | */ |
613 | | size_t roaring64_bitmap_portable_size_in_bytes(const roaring64_bitmap_t *r); |
614 | | |
615 | | /** |
616 | | * Write a bitmap to a buffer. The output buffer should refer to at least |
617 | | * `roaring64_bitmap_portable_size_in_bytes(r)` bytes of allocated memory. |
618 | | * |
619 | | * Returns how many bytes were written, which should match |
620 | | * `roaring64_bitmap_portable_size_in_bytes(r)`. |
621 | | * |
622 | | * This is meant to be compatible with other languages: |
623 | | * https://github.com/RoaringBitmap/RoaringFormatSpec#extension-for-64-bit-implementations |
624 | | * |
625 | | * When serializing data to a file, we recommend that you also use |
626 | | * checksums so that, at deserialization, you can be confident |
627 | | * that you are recovering the correct data. |
628 | | */ |
629 | | size_t roaring64_bitmap_portable_serialize(const roaring64_bitmap_t *r, |
630 | | char *buf); |
631 | | /** |
632 | | * Check how many bytes would be read (up to maxbytes) at this pointer if there |
633 | | * is a valid bitmap, returns zero if there is no valid bitmap. |
634 | | * |
635 | | * This is meant to be compatible with other languages |
636 | | * https://github.com/RoaringBitmap/RoaringFormatSpec#extension-for-64-bit-implementations |
637 | | */ |
638 | | size_t roaring64_bitmap_portable_deserialize_size(const char *buf, |
639 | | size_t maxbytes); |
640 | | |
641 | | /** |
642 | | * Read a bitmap from a serialized buffer (reading up to maxbytes). |
643 | | * In case of failure, NULL is returned. |
644 | | * |
645 | | * This is meant to be compatible with other languages |
646 | | * https://github.com/RoaringBitmap/RoaringFormatSpec#extension-for-64-bit-implementations |
647 | | * |
648 | | * The function itself is safe in the sense that it will not cause buffer |
649 | | * overflows: it will not read beyond the scope of the provided buffer |
650 | | * (buf,maxbytes). |
651 | | * |
652 | | * However, for correct operations, it is assumed that the bitmap |
653 | | * read was once serialized from a valid bitmap (i.e., it follows the format |
654 | | * specification). If you provided an incorrect input (garbage), then the bitmap |
655 | | * read may not be in a valid state and following operations may not lead to |
656 | | * sensible results. In particular, the serialized array containers need to be |
657 | | * in sorted order, and the run containers should be in sorted non-overlapping |
658 | | * order. This is is guaranteed to happen when serializing an existing bitmap, |
659 | | * but not for random inputs. |
660 | | * |
661 | | * If the source is untrusted, you should call |
662 | | * roaring64_bitmap_internal_validate to check the validity of the |
663 | | * bitmap prior to using it. Only after calling |
664 | | * roaring64_bitmap_internal_validate is the bitmap considered safe for use. |
665 | | * |
666 | | * We also recommend that you use checksums to check that serialized data |
667 | | * corresponds to the serialized bitmap. The CRoaring library does not provide |
668 | | * checksumming. |
669 | | */ |
670 | | roaring64_bitmap_t *roaring64_bitmap_portable_deserialize_safe(const char *buf, |
671 | | size_t maxbytes); |
672 | | |
673 | | /** |
674 | | * Read a bitmap from a portable serialized buffer as a read-only view of the |
675 | | * container payloads. Headers and the ART index are allocated; bitset/array/run |
676 | | * payloads alias `buf` and are not copied. |
677 | | * |
678 | | * In case of failure, NULL is returned. The function will not read beyond |
679 | | * `maxbytes`. |
680 | | * |
681 | | * The returned bitmap must only be used in a readonly manner. It must be |
682 | | * freed with `roaring64_bitmap_free()`. The backing buffer must outlive the |
683 | | * bitmap and must not be freed or modified while it backs it. Calling any |
684 | | * mutating function on the result is undefined behavior: its container array |
685 | | * and headers live in a single allocation, so growing it would reallocate an |
686 | | * interior pointer. |
687 | | * |
688 | | * The function itself is safe in the sense that it will not read beyond |
689 | | * (buf, maxbytes). However, as with |
690 | | * `roaring64_bitmap_portable_deserialize_safe()`, a bitmap read from garbage |
691 | | * may not be in a valid state, and subsequent operations on it may not lead |
692 | | * to sensible results: array containers must be sorted, and run containers |
693 | | * sorted and non-overlapping, which is guaranteed only when the input came |
694 | | * from a real serialized bitmap. |
695 | | * |
696 | | * If the source is untrusted, you should call |
697 | | * `roaring64_bitmap_internal_validate` on the result before using it. Only |
698 | | * after that is the bitmap considered safe for use. We also recommend |
699 | | * checksumming the serialized data; CRoaring does not provide checksumming. |
700 | | * |
701 | | * Returns NULL on a big-endian system (e.g., a mainframe IBM s390x). The |
702 | | * portable format is little-endian and this function uses the payload bytes |
703 | | * where they sit, so there is no correct in-place view of them there; use |
704 | | * `roaring64_bitmap_portable_deserialize_safe()`, which converts as it copies. |
705 | | * |
706 | | * Container payloads are used where they sit in the buffer, so they may be |
707 | | * unaligned. Every access path is either SIMD with unaligned loads or marked |
708 | | * `CROARING_ALLOW_UNALIGNED`. |
709 | | */ |
710 | | roaring64_bitmap_t *roaring64_bitmap_portable_deserialize_frozen( |
711 | | const char *buf, size_t maxbytes); |
712 | | |
713 | | /** |
714 | | * Returns the number of bytes required to serialize this bitmap in a "frozen" |
715 | | * format. This is not compatible with any other serialization formats. |
716 | | * |
717 | | * `roaring64_bitmap_shrink_to_fit()` must be called before this method. |
718 | | */ |
719 | | size_t roaring64_bitmap_frozen_size_in_bytes(const roaring64_bitmap_t *r); |
720 | | |
721 | | /** |
722 | | * Serializes the bitmap in a "frozen" format. The given buffer must be at least |
723 | | * `roaring64_bitmap_frozen_size_in_bytes()` in size. Returns the number of |
724 | | * bytes used for serialization. |
725 | | * |
726 | | * `roaring64_bitmap_shrink_to_fit()` must be called before this method. |
727 | | * |
728 | | * The frozen format is optimized for speed of (de)serialization, as well as |
729 | | * allowing the user to create a bitmap based on a memory mapped file, which is |
730 | | * possible because the format mimics the memory layout of the bitmap. |
731 | | * |
732 | | * Because the format mimics the memory layout of the bitmap, the format is not |
733 | | * fixed across releases of Roaring Bitmaps, and may change in future releases. |
734 | | * |
735 | | * This function is endian-sensitive. If you have a big-endian system (e.g., a |
736 | | * mainframe IBM s390x), the data format is going to be big-endian and not |
737 | | * compatible with little-endian systems. This is not a bug, it is by design, |
738 | | * since the format imitates C memory layout of roaring64_bitmap_t. |
739 | | */ |
740 | | size_t roaring64_bitmap_frozen_serialize(const roaring64_bitmap_t *r, |
741 | | char *buf); |
742 | | |
743 | | /** |
744 | | * Creates a readonly bitmap that is a view of the given buffer. The buffer |
745 | | * must be created with `roaring64_bitmap_frozen_serialize()`, and must be |
746 | | * aligned by 64 bytes. |
747 | | * |
748 | | * Returns NULL if deserialization fails. |
749 | | * |
750 | | * The returned bitmap must only be used in a readonly manner. The bitmap must |
751 | | * be freed using `roaring64_bitmap_free()` as normal. The backing buffer must |
752 | | * only be freed after the bitmap. |
753 | | * |
754 | | * This function is endian-sensitive. If you have a big-endian system (e.g., a |
755 | | * mainframe IBM s390x), the data format is going to be big-endian and not |
756 | | * compatible with little-endian systems. This is not a bug, it is by design, |
757 | | * since the format imitates C memory layout of roaring64_bitmap_t. |
758 | | */ |
759 | | roaring64_bitmap_t *roaring64_bitmap_frozen_view(const char *buf, |
760 | | size_t maxbytes); |
761 | | |
762 | | /** |
763 | | * Iterate over the bitmap elements. The function `iterator` is called once for |
764 | | * all the values with `ptr` (can be NULL) as the second parameter of each call. |
765 | | * |
766 | | * `roaring_iterator64` is simply a pointer to a function that returns a bool |
767 | | * and takes `(uint64_t, void*)` as inputs. True means that the iteration should |
768 | | * continue, while false means that it should stop. |
769 | | * |
770 | | * Returns true if the `roaring64_iterator` returned true throughout (so that |
771 | | * all data points were necessarily visited). |
772 | | * |
773 | | * Iteration is ordered from the smallest to the largest elements. |
774 | | */ |
775 | | bool roaring64_bitmap_iterate(const roaring64_bitmap_t *r, |
776 | | roaring_iterator64 iterator, void *ptr); |
777 | | |
778 | | /** |
779 | | * Convert the bitmap to a sorted array `out`. |
780 | | * |
781 | | * Caller is responsible to ensure that there is enough memory allocated, e.g. |
782 | | * ``` |
783 | | * out = malloc(roaring64_bitmap_get_cardinality(bitmap) * sizeof(uint64_t)); |
784 | | * ``` |
785 | | */ |
786 | | void roaring64_bitmap_to_uint64_array(const roaring64_bitmap_t *r, |
787 | | uint64_t *out); |
788 | | |
789 | | /** |
790 | | * Create an iterator object that can be used to iterate through the values. |
791 | | * Caller is responsible for calling `roaring64_iterator_free()`. |
792 | | * |
793 | | * The iterator is initialized. If there is a value, then this iterator points |
794 | | * to the first value and `roaring64_iterator_has_value()` returns true. The |
795 | | * value can be retrieved with `roaring64_iterator_value()`. |
796 | | */ |
797 | | roaring64_iterator_t *roaring64_iterator_create(const roaring64_bitmap_t *r); |
798 | | |
799 | | /** |
800 | | * Create an iterator object that can be used to iterate through the values. |
801 | | * Caller is responsible for calling `roaring64_iterator_free()`. |
802 | | * |
803 | | * The iterator is initialized. If there is a value, then this iterator points |
804 | | * to the last value and `roaring64_iterator_has_value()` returns true. The |
805 | | * value can be retrieved with `roaring64_iterator_value()`. |
806 | | */ |
807 | | roaring64_iterator_t *roaring64_iterator_create_last( |
808 | | const roaring64_bitmap_t *r); |
809 | | |
810 | | /** |
811 | | * Re-initializes an existing iterator. Functionally the same as |
812 | | * `roaring64_iterator_create` without a allocation. |
813 | | */ |
814 | | void roaring64_iterator_reinit(const roaring64_bitmap_t *r, |
815 | | roaring64_iterator_t *it); |
816 | | |
817 | | /** |
818 | | * Re-initializes an existing iterator. Functionally the same as |
819 | | * `roaring64_iterator_create_last` without a allocation. |
820 | | */ |
821 | | void roaring64_iterator_reinit_last(const roaring64_bitmap_t *r, |
822 | | roaring64_iterator_t *it); |
823 | | |
824 | | /** |
825 | | * Creates a copy of the iterator. Caller is responsible for calling |
826 | | * `roaring64_iterator_free()` on the resulting iterator. |
827 | | */ |
828 | | roaring64_iterator_t *roaring64_iterator_copy(const roaring64_iterator_t *it); |
829 | | |
830 | | /** |
831 | | * Free the iterator. |
832 | | */ |
833 | | void roaring64_iterator_free(roaring64_iterator_t *it); |
834 | | |
835 | | /** |
836 | | * Returns true if the iterator currently points to a value. If so, calling |
837 | | * `roaring64_iterator_value()` returns the value. |
838 | | * |
839 | | * A pointer to a structure, suitably converted, points to its initial member |
840 | | * (C17 6.7.2.1p15), and `roaring64_iterator_public_t` is the initial member of |
841 | | * `roaring64_iterator_t`, so this reads the field directly. |
842 | | */ |
843 | 0 | inline bool roaring64_iterator_has_value(const roaring64_iterator_t *it) { |
844 | 0 | return ((const roaring64_iterator_public_t *)it)->has_value; |
845 | 0 | } |
846 | | |
847 | | /** |
848 | | * Returns the value the iterator currently points to. Should only be called if |
849 | | * `roaring64_iterator_has_value()` returns true. |
850 | | */ |
851 | 0 | inline uint64_t roaring64_iterator_value(const roaring64_iterator_t *it) { |
852 | 0 | return ((const roaring64_iterator_public_t *)it)->value; |
853 | 0 | } |
854 | | |
855 | | /** |
856 | | * Advance the iterator. If there is a new value, then |
857 | | * `roaring64_iterator_has_value()` returns true. Values are traversed in |
858 | | * increasing order. For convenience, returns the result of |
859 | | * `roaring64_iterator_has_value()`. |
860 | | * |
861 | | * Once this returns false, `roaring64_iterator_advance` should not be called on |
862 | | * the iterator again. Calling `roaring64_iterator_previous` is allowed. |
863 | | */ |
864 | | bool roaring64_iterator_advance(roaring64_iterator_t *it); |
865 | | |
866 | | /** |
867 | | * Decrement the iterator. If there is a new value, then |
868 | | * `roaring64_iterator_has_value()` returns true. Values are traversed in |
869 | | * decreasing order. For convenience, returns the result of |
870 | | * `roaring64_iterator_has_value()`. |
871 | | * |
872 | | * Once this returns false, `roaring64_iterator_previous` should not be called |
873 | | * on the iterator again. Calling `roaring64_iterator_advance` is allowed. |
874 | | */ |
875 | | bool roaring64_iterator_previous(roaring64_iterator_t *it); |
876 | | |
877 | | /** |
878 | | * Move the iterator to the first value greater than or equal to `val`, if it |
879 | | * exists at or after the current position of the iterator. If there is a new |
880 | | * value, then `roaring64_iterator_has_value()` returns true. Values are |
881 | | * traversed in increasing order. For convenience, returns the result of |
882 | | * `roaring64_iterator_has_value()`. |
883 | | */ |
884 | | bool roaring64_iterator_move_equalorlarger(roaring64_iterator_t *it, |
885 | | uint64_t val); |
886 | | |
887 | | /** |
888 | | * Reads up to `count` values from the iterator into the given `buf`. Returns |
889 | | * the number of elements read. The number of elements read can be smaller than |
890 | | * `count`, which means that there are no more elements in the bitmap. |
891 | | * |
892 | | * This function can be used together with other iterator functions. |
893 | | */ |
894 | | uint64_t roaring64_iterator_read(roaring64_iterator_t *it, uint64_t *buf, |
895 | | uint64_t count); |
896 | | |
897 | | /** |
898 | | * Reads previous ${count} values from iterator into user-supplied ${buf}. |
899 | | * Returns the number of read elements. |
900 | | * This number can be smaller than ${count}, which means that iterator is |
901 | | * drained. |
902 | | * |
903 | | * Values are written in descending order: buf[0] is the highest (current) |
904 | | * value, buf[ret-1] is the lowest value read. |
905 | | * |
906 | | * This function satisfies semantics of reverse iteration and can be used |
907 | | * together with other iterator functions. |
908 | | * - first value is copied from the current iterator value |
909 | | * - after function returns, iterator is positioned at the previous element |
910 | | */ |
911 | | uint64_t roaring64_iterator_read_backward(roaring64_iterator_t *it, |
912 | | uint64_t *buf, uint64_t count); |
913 | | |
914 | | typedef struct roaring64_range_closed_s { |
915 | | uint64_t min; |
916 | | uint64_t max; |
917 | | } roaring64_range_closed_t; |
918 | | |
919 | | /** |
920 | | * Reads next ${count} ranges from iterator into user-supplied ${buf}. |
921 | | * A range is defined as a maximal interval of consecutive values. |
922 | | * For example, the set {1,2,3,5,6} contains two ranges: [1..3] and [5..6]. |
923 | | * Each range is represented as a struct {min,max}, both endpoints included. |
924 | | * Consecutive values that span internal container boundaries are merged into |
925 | | * a single range. |
926 | | * |
927 | | * Returns the number of read ranges. |
928 | | * This number can be smaller than ${count}, which means that the iterator is |
929 | | * drained. |
930 | | * |
931 | | * This function can be used together with other iterator functions. |
932 | | * - first range will start with the current iterator value |
933 | | * - after the function returns, the iterator is positioned at the next element |
934 | | * after the end of the last returned range, or has_value is false if |
935 | | * the bitmap is exhausted. |
936 | | */ |
937 | | size_t roaring64_iterator_read_ranges(roaring64_iterator_t *it, |
938 | | roaring64_range_closed_t *buf, |
939 | | size_t count); |
940 | | |
941 | | /** |
942 | | * Reads previous ${count} ranges from iterator into user-supplied ${buf}. |
943 | | * A range is defined as a maximal interval of consecutive values. |
944 | | * For example, the set {1,2,3,5,6} contains two ranges: [1..3] and [5..6]. |
945 | | * Each range is represented as a struct {min,max}, both endpoints included. |
946 | | * Consecutive values that span internal container boundaries are merged into |
947 | | * a single range. |
948 | | * |
949 | | * Returns the number of read ranges. |
950 | | * This number can be smaller than ${count}, which means that the iterator is |
951 | | * drained. |
952 | | * |
953 | | * Ranges are returned in reverse order, e.g. the first range returned is the |
954 | | * highest range (ending at the current value). |
955 | | * |
956 | | * This function can be used together with other iterator functions. |
957 | | * - first range will end with the current iterator value |
958 | | * - after the function returns, the iterator is positioned at the element |
959 | | * before the beginning of the last returned range, or has_value is false if |
960 | | * the bitmap is exhausted. |
961 | | */ |
962 | | size_t roaring64_iterator_read_prev_ranges(roaring64_iterator_t *it, |
963 | | roaring64_range_closed_t *buf, |
964 | | size_t count); |
965 | | |
966 | | #ifdef __cplusplus |
967 | | } // extern "C" |
968 | | } // namespace roaring |
969 | | } // namespace api |
970 | | #endif |
971 | | |
972 | | #endif /* ROARING64_H */ |