Coverage Report

Created: 2026-10-17 07:19

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