Coverage Report

Created: 2026-06-10 06:42

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