Coverage Report

Created: 2026-12-01 06:38

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