Coverage Report

Created: 2026-06-15 06:49

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