roaring 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2949 @@
1
+ // !!! DO NOT EDIT - THIS IS AN AUTO-GENERATED FILE !!!
2
+ // Created by amalgamation.sh on 2024-05-13T21:29:25Z
3
+
4
+ /*
5
+ * The CRoaring project is under a dual license (Apache/MIT).
6
+ * Users of the library may choose one or the other license.
7
+ */
8
+ /*
9
+ * Copyright 2016-2022 The CRoaring authors
10
+ *
11
+ * Licensed under the Apache License, Version 2.0 (the "License");
12
+ * you may not use this file except in compliance with the License.
13
+ * You may obtain a copy of the License at
14
+ *
15
+ * http://www.apache.org/licenses/LICENSE-2.0
16
+ *
17
+ * Unless required by applicable law or agreed to in writing, software
18
+ * distributed under the License is distributed on an "AS IS" BASIS,
19
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
+ * See the License for the specific language governing permissions and
21
+ * limitations under the License.
22
+ *
23
+ * SPDX-License-Identifier: Apache-2.0
24
+ */
25
+ /*
26
+ * MIT License
27
+ *
28
+ * Copyright 2016-2022 The CRoaring authors
29
+ *
30
+ * Permission is hereby granted, free of charge, to any
31
+ * person obtaining a copy of this software and associated
32
+ * documentation files (the "Software"), to deal in the
33
+ * Software without restriction, including without
34
+ * limitation the rights to use, copy, modify, merge,
35
+ * publish, distribute, sublicense, and/or sell copies of
36
+ * the Software, and to permit persons to whom the Software
37
+ * is furnished to do so, subject to the following
38
+ * conditions:
39
+ *
40
+ * The above copyright notice and this permission notice
41
+ * shall be included in all copies or substantial portions
42
+ * of the Software.
43
+ *
44
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
45
+ * ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
46
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
47
+ * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
48
+ * SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
49
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
50
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
51
+ * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
52
+ * DEALINGS IN THE SOFTWARE.
53
+ *
54
+ * SPDX-License-Identifier: MIT
55
+ */
56
+
57
+ /* begin file include/roaring/roaring_version.h */
58
+ // clang-format off
59
+ // /include/roaring/roaring_version.h automatically generated by release.py, do not change by hand
60
+ #ifndef ROARING_INCLUDE_ROARING_VERSION
61
+ #define ROARING_INCLUDE_ROARING_VERSION
62
+ #define ROARING_VERSION "4.0.0"
63
+ enum {
64
+ ROARING_VERSION_MAJOR = 4,
65
+ ROARING_VERSION_MINOR = 0,
66
+ ROARING_VERSION_REVISION = 0
67
+ };
68
+ #endif // ROARING_INCLUDE_ROARING_VERSION
69
+ // clang-format on/* end file include/roaring/roaring_version.h */
70
+ /* begin file include/roaring/roaring_types.h */
71
+ /*
72
+ Typedefs used by various components
73
+ */
74
+
75
+ #ifndef ROARING_TYPES_H
76
+ #define ROARING_TYPES_H
77
+
78
+ #include <stdbool.h>
79
+ #include <stdint.h>
80
+
81
+ #ifdef __cplusplus
82
+ extern "C" {
83
+ namespace roaring {
84
+ namespace api {
85
+ #endif
86
+
87
+ /**
88
+ * When building .c files as C++, there's added compile-time checking if the
89
+ * container types are derived from a `container_t` base class. So long as
90
+ * such a base class is empty, the struct will behave compatibly with C structs
91
+ * despite the derivation. This is due to the Empty Base Class Optimization:
92
+ *
93
+ * https://en.cppreference.com/w/cpp/language/ebo
94
+ *
95
+ * But since C isn't namespaced, taking `container_t` globally might collide
96
+ * with other projects. So roaring.h uses ROARING_CONTAINER_T, while internal
97
+ * code #undefs that after declaring `typedef ROARING_CONTAINER_T container_t;`
98
+ */
99
+ #if defined(__cplusplus)
100
+ extern "C++" {
101
+ struct container_s {};
102
+ }
103
+ #define ROARING_CONTAINER_T ::roaring::api::container_s
104
+ #else
105
+ #define ROARING_CONTAINER_T void // no compile-time checking
106
+ #endif
107
+
108
+ #define ROARING_FLAG_COW UINT8_C(0x1)
109
+ #define ROARING_FLAG_FROZEN UINT8_C(0x2)
110
+
111
+ /**
112
+ * Roaring arrays are array-based key-value pairs having containers as values
113
+ * and 16-bit integer keys. A roaring bitmap might be implemented as such.
114
+ */
115
+
116
+ // parallel arrays. Element sizes quite different.
117
+ // Alternative is array
118
+ // of structs. Which would have better
119
+ // cache performance through binary searches?
120
+
121
+ typedef struct roaring_array_s {
122
+ int32_t size;
123
+ int32_t allocation_size;
124
+ ROARING_CONTAINER_T **containers; // Use container_t in non-API files!
125
+ uint16_t *keys;
126
+ uint8_t *typecodes;
127
+ uint8_t flags;
128
+ } roaring_array_t;
129
+
130
+ typedef bool (*roaring_iterator)(uint32_t value, void *param);
131
+ typedef bool (*roaring_iterator64)(uint64_t value, void *param);
132
+
133
+ /**
134
+ * (For advanced users.)
135
+ * The roaring_statistics_t can be used to collect detailed statistics about
136
+ * the composition of a roaring bitmap.
137
+ */
138
+ typedef struct roaring_statistics_s {
139
+ uint32_t n_containers; /* number of containers */
140
+
141
+ uint32_t n_array_containers; /* number of array containers */
142
+ uint32_t n_run_containers; /* number of run containers */
143
+ uint32_t n_bitset_containers; /* number of bitmap containers */
144
+
145
+ uint32_t
146
+ n_values_array_containers; /* number of values in array containers */
147
+ uint32_t n_values_run_containers; /* number of values in run containers */
148
+ uint32_t
149
+ n_values_bitset_containers; /* number of values in bitmap containers */
150
+
151
+ uint32_t n_bytes_array_containers; /* number of allocated bytes in array
152
+ containers */
153
+ uint32_t n_bytes_run_containers; /* number of allocated bytes in run
154
+ containers */
155
+ uint32_t n_bytes_bitset_containers; /* number of allocated bytes in bitmap
156
+ containers */
157
+
158
+ uint32_t
159
+ max_value; /* the maximal value, undefined if cardinality is zero */
160
+ uint32_t
161
+ min_value; /* the minimal value, undefined if cardinality is zero */
162
+ uint64_t sum_value; /* deprecated always zero */
163
+
164
+ uint64_t cardinality; /* total number of values stored in the bitmap */
165
+
166
+ // and n_values_arrays, n_values_rle, n_values_bitmap
167
+ } roaring_statistics_t;
168
+
169
+ /**
170
+ * (For advanced users.)
171
+ * The roaring64_statistics_t can be used to collect detailed statistics about
172
+ * the composition of a roaring64 bitmap.
173
+ */
174
+ typedef struct roaring64_statistics_s {
175
+ uint64_t n_containers; /* number of containers */
176
+
177
+ uint64_t n_array_containers; /* number of array containers */
178
+ uint64_t n_run_containers; /* number of run containers */
179
+ uint64_t n_bitset_containers; /* number of bitmap containers */
180
+
181
+ uint64_t
182
+ n_values_array_containers; /* number of values in array containers */
183
+ uint64_t n_values_run_containers; /* number of values in run containers */
184
+ uint64_t
185
+ n_values_bitset_containers; /* number of values in bitmap containers */
186
+
187
+ uint64_t n_bytes_array_containers; /* number of allocated bytes in array
188
+ containers */
189
+ uint64_t n_bytes_run_containers; /* number of allocated bytes in run
190
+ containers */
191
+ uint64_t n_bytes_bitset_containers; /* number of allocated bytes in bitmap
192
+ containers */
193
+
194
+ uint64_t
195
+ max_value; /* the maximal value, undefined if cardinality is zero */
196
+ uint64_t
197
+ min_value; /* the minimal value, undefined if cardinality is zero */
198
+
199
+ uint64_t cardinality; /* total number of values stored in the bitmap */
200
+
201
+ // and n_values_arrays, n_values_rle, n_values_bitmap
202
+ } roaring64_statistics_t;
203
+
204
+ /**
205
+ * Roaring-internal type used to iterate within a roaring container.
206
+ */
207
+ typedef struct roaring_container_iterator_s {
208
+ // For bitset and array containers this is the index of the bit / entry.
209
+ // For run containers this points at the run.
210
+ int32_t index;
211
+ } roaring_container_iterator_t;
212
+
213
+ #ifdef __cplusplus
214
+ }
215
+ }
216
+ } // extern "C" { namespace roaring { namespace api {
217
+ #endif
218
+
219
+ #endif /* ROARING_TYPES_H */
220
+ /* end file include/roaring/roaring_types.h */
221
+ /* begin file include/roaring/portability.h */
222
+ /*
223
+ * portability.h
224
+ *
225
+ */
226
+
227
+ /**
228
+ * All macros should be prefixed with either CROARING or ROARING.
229
+ * The library uses both ROARING_...
230
+ * as well as CROAIRING_ as prefixes. The ROARING_ prefix is for
231
+ * macros that are provided by the build system or that are closely
232
+ * related to the format. The header macros may also use ROARING_.
233
+ * The CROARING_ prefix is for internal macros that a user is unlikely
234
+ * to ever interact with.
235
+ */
236
+
237
+ #ifndef CROARING_INCLUDE_PORTABILITY_H_
238
+ #define CROARING_INCLUDE_PORTABILITY_H_
239
+
240
+ #ifndef _GNU_SOURCE
241
+ #define _GNU_SOURCE 1
242
+ #endif // _GNU_SOURCE
243
+ #ifndef __STDC_FORMAT_MACROS
244
+ #define __STDC_FORMAT_MACROS 1
245
+ #endif // __STDC_FORMAT_MACROS
246
+
247
+ #ifdef _MSC_VER
248
+ #define CROARING_VISUAL_STUDIO 1
249
+ /**
250
+ * We want to differentiate carefully between
251
+ * clang under visual studio and regular visual
252
+ * studio.
253
+ */
254
+ #ifdef __clang__
255
+ // clang under visual studio
256
+ #define CROARING_CLANG_VISUAL_STUDIO 1
257
+ #else
258
+ // just regular visual studio (best guess)
259
+ #define CROARING_REGULAR_VISUAL_STUDIO 1
260
+ #endif // __clang__
261
+ #endif // _MSC_VER
262
+ #ifndef CROARING_VISUAL_STUDIO
263
+ #define CROARING_VISUAL_STUDIO 0
264
+ #endif
265
+ #ifndef CROARING_CLANG_VISUAL_STUDIO
266
+ #define CROARING_CLANG_VISUAL_STUDIO 0
267
+ #endif
268
+ #ifndef CROARING_REGULAR_VISUAL_STUDIO
269
+ #define CROARING_REGULAR_VISUAL_STUDIO 0
270
+ #endif
271
+
272
+ #if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE < 200809L)
273
+ #undef _POSIX_C_SOURCE
274
+ #endif
275
+
276
+ #ifndef _POSIX_C_SOURCE
277
+ #define _POSIX_C_SOURCE 200809L
278
+ #endif // !(defined(_POSIX_C_SOURCE)) || (_POSIX_C_SOURCE < 200809L)
279
+ #if !(defined(_XOPEN_SOURCE)) || (_XOPEN_SOURCE < 700)
280
+ #define _XOPEN_SOURCE 700
281
+ #endif // !(defined(_XOPEN_SOURCE)) || (_XOPEN_SOURCE < 700)
282
+
283
+ #ifdef __illumos__
284
+ #define __EXTENSIONS__
285
+ #endif
286
+
287
+ #include <stdbool.h>
288
+ #include <stdint.h>
289
+ #include <stdlib.h> // will provide posix_memalign with _POSIX_C_SOURCE as defined above
290
+ #ifdef __GLIBC__
291
+ #include <malloc.h> // this should never be needed but there are some reports that it is needed.
292
+ #endif
293
+
294
+ #ifdef __cplusplus
295
+ extern "C" { // portability definitions are in global scope, not a namespace
296
+ #endif
297
+
298
+ #if defined(__SIZEOF_LONG_LONG__) && __SIZEOF_LONG_LONG__ != 8
299
+ #error This code assumes 64-bit long longs (by use of the GCC intrinsics). Your system is not currently supported.
300
+ #endif
301
+
302
+ #if CROARING_REGULAR_VISUAL_STUDIO
303
+ #ifndef __restrict__
304
+ #define __restrict__ __restrict
305
+ #endif // __restrict__
306
+ #endif // CROARING_REGULAR_VISUAL_STUDIO
307
+
308
+ #if defined(__x86_64__) || defined(_M_X64)
309
+ // we have an x64 processor
310
+ #define CROARING_IS_X64 1
311
+
312
+ #if defined(_MSC_VER) && (_MSC_VER < 1910)
313
+ // Old visual studio systems won't support AVX2 well.
314
+ #undef CROARING_IS_X64
315
+ #endif
316
+
317
+ #if defined(__clang_major__) && (__clang_major__ <= 8) && !defined(__AVX2__)
318
+ // Older versions of clang have a bug affecting us
319
+ // https://stackoverflow.com/questions/57228537/how-does-one-use-pragma-clang-attribute-push-with-c-namespaces
320
+ #undef CROARING_IS_X64
321
+ #endif
322
+
323
+ #ifdef ROARING_DISABLE_X64
324
+ #undef CROARING_IS_X64
325
+ #endif
326
+ // we include the intrinsic header
327
+ #if !CROARING_REGULAR_VISUAL_STUDIO
328
+ /* Non-Microsoft C/C++-compatible compiler */
329
+ #include <x86intrin.h> // on some recent GCC, this will declare posix_memalign
330
+
331
+ #if CROARING_CLANG_VISUAL_STUDIO
332
+
333
+ /**
334
+ * You are not supposed, normally, to include these
335
+ * headers directly. Instead you should either include intrin.h
336
+ * or x86intrin.h. However, when compiling with clang
337
+ * under Windows (i.e., when _MSC_VER is set), these headers
338
+ * only get included *if* the corresponding features are detected
339
+ * from macros:
340
+ * e.g., if __AVX2__ is set... in turn, we normally set these
341
+ * macros by compiling against the corresponding architecture
342
+ * (e.g., arch:AVX2, -mavx2, etc.) which compiles the whole
343
+ * software with these advanced instructions. These headers would
344
+ * normally guard against such usage, but we carefully included
345
+ * <x86intrin.h> (or <intrin.h>) before, so the headers
346
+ * are fooled.
347
+ */
348
+ // To avoid reordering imports:
349
+ // clang-format off
350
+ #include <bmiintrin.h> // for _blsr_u64
351
+ #include <lzcntintrin.h> // for __lzcnt64
352
+ #include <immintrin.h> // for most things (AVX2, AVX512, _popcnt64)
353
+ #include <smmintrin.h>
354
+ #include <tmmintrin.h>
355
+ #include <avxintrin.h>
356
+ #include <avx2intrin.h>
357
+ #include <wmmintrin.h>
358
+ #if _MSC_VER >= 1920
359
+ // Important: we need the AVX-512 headers:
360
+ #include <avx512fintrin.h>
361
+ #include <avx512dqintrin.h>
362
+ #include <avx512cdintrin.h>
363
+ #include <avx512bwintrin.h>
364
+ #include <avx512vlintrin.h>
365
+ #include <avx512vbmiintrin.h>
366
+ #include <avx512vbmi2intrin.h>
367
+ #include <avx512vpopcntdqintrin.h>
368
+ // clang-format on
369
+ #endif // _MSC_VER >= 1920
370
+ // unfortunately, we may not get _blsr_u64, but, thankfully, clang
371
+ // has it as a macro.
372
+ #ifndef _blsr_u64
373
+ // we roll our own
374
+ #define _blsr_u64(n) ((n - 1) & n)
375
+ #endif // _blsr_u64
376
+ #endif // SIMDJSON_CLANG_VISUAL_STUDIO
377
+
378
+ #endif // CROARING_REGULAR_VISUAL_STUDIO
379
+ #endif // defined(__x86_64__) || defined(_M_X64)
380
+
381
+ #if !defined(CROARING_USENEON) && !defined(DISABLENEON) && defined(__ARM_NEON)
382
+ #define CROARING_USENEON
383
+ #endif
384
+ #if defined(CROARING_USENEON)
385
+ #include <arm_neon.h>
386
+ #endif
387
+
388
+ #if !CROARING_REGULAR_VISUAL_STUDIO
389
+ /* Non-Microsoft C/C++-compatible compiler, assumes that it supports inline
390
+ * assembly */
391
+ #define CROARING_INLINE_ASM 1
392
+ #endif // _MSC_VER
393
+
394
+ #if CROARING_REGULAR_VISUAL_STUDIO
395
+ /* Microsoft C/C++-compatible compiler */
396
+ #include <intrin.h>
397
+
398
+ #ifndef __clang__ // if one compiles with MSVC *with* clang, then these
399
+ // intrinsics are defined!!!
400
+ #define CROARING_INTRINSICS 1
401
+ // sadly there is no way to check whether we are missing these intrinsics
402
+ // specifically.
403
+
404
+ /* wrappers for Visual Studio built-ins that look like gcc built-ins
405
+ * __builtin_ctzll */
406
+ /** result might be undefined when input_num is zero */
407
+ inline int roaring_trailing_zeroes(unsigned long long input_num) {
408
+ unsigned long index;
409
+ #ifdef _WIN64 // highly recommended!!!
410
+ _BitScanForward64(&index, input_num);
411
+ #else // if we must support 32-bit Windows
412
+ if ((uint32_t)input_num != 0) {
413
+ _BitScanForward(&index, (uint32_t)input_num);
414
+ } else {
415
+ _BitScanForward(&index, (uint32_t)(input_num >> 32));
416
+ index += 32;
417
+ }
418
+ #endif // _WIN64
419
+ return index;
420
+ }
421
+
422
+ /* wrappers for Visual Studio built-ins that look like gcc built-ins
423
+ * __builtin_clzll */
424
+ /** result might be undefined when input_num is zero */
425
+ inline int roaring_leading_zeroes(unsigned long long input_num) {
426
+ unsigned long index;
427
+ #ifdef _WIN64 // highly recommended!!!
428
+ _BitScanReverse64(&index, input_num);
429
+ #else // if we must support 32-bit Windows
430
+ if (input_num > 0xFFFFFFFF) {
431
+ _BitScanReverse(&index, (uint32_t)(input_num >> 32));
432
+ index += 32;
433
+ } else {
434
+ _BitScanReverse(&index, (uint32_t)(input_num));
435
+ }
436
+ #endif // _WIN64
437
+ return 63 - index;
438
+ }
439
+
440
+ /* Use #define so this is effective even under /Ob0 (no inline) */
441
+ #define roaring_unreachable __assume(0)
442
+ #endif // __clang__
443
+
444
+ #endif // CROARING_REGULAR_VISUAL_STUDIO
445
+
446
+ #ifndef CROARING_INTRINSICS
447
+ #define CROARING_INTRINSICS 1
448
+ #define roaring_unreachable __builtin_unreachable()
449
+ /** result might be undefined when input_num is zero */
450
+ inline int roaring_trailing_zeroes(unsigned long long input_num) {
451
+ return __builtin_ctzll(input_num);
452
+ }
453
+ /** result might be undefined when input_num is zero */
454
+ inline int roaring_leading_zeroes(unsigned long long input_num) {
455
+ return __builtin_clzll(input_num);
456
+ }
457
+ #endif
458
+
459
+ #if CROARING_REGULAR_VISUAL_STUDIO
460
+ #define ALIGNED(x) __declspec(align(x))
461
+ #elif defined(__GNUC__) || defined(__clang__)
462
+ #define ALIGNED(x) __attribute__((aligned(x)))
463
+ #else
464
+ #warning "Warning. Unrecognized compiler."
465
+ #define ALIGNED(x)
466
+ #endif
467
+
468
+ #if defined(__GNUC__) || defined(__clang__)
469
+ #define CROARING_WARN_UNUSED __attribute__((warn_unused_result))
470
+ #else
471
+ #define CROARING_WARN_UNUSED
472
+ #endif
473
+
474
+ #define IS_BIG_ENDIAN (*(uint16_t *)"\0\xff" < 0x100)
475
+
476
+ #ifdef CROARING_USENEON
477
+ // we can always compute the popcount fast.
478
+ #elif (defined(_M_ARM) || defined(_M_ARM64)) && \
479
+ ((defined(_WIN64) || defined(_WIN32)) && \
480
+ defined(CROARING_REGULAR_VISUAL_STUDIO) && \
481
+ CROARING_REGULAR_VISUAL_STUDIO)
482
+ // we will need this function:
483
+ static inline int roaring_hamming_backup(uint64_t x) {
484
+ uint64_t c1 = UINT64_C(0x5555555555555555);
485
+ uint64_t c2 = UINT64_C(0x3333333333333333);
486
+ uint64_t c4 = UINT64_C(0x0F0F0F0F0F0F0F0F);
487
+ x -= (x >> 1) & c1;
488
+ x = ((x >> 2) & c2) + (x & c2);
489
+ x = (x + (x >> 4)) & c4;
490
+ x *= UINT64_C(0x0101010101010101);
491
+ return x >> 56;
492
+ }
493
+ #endif
494
+
495
+ static inline int roaring_hamming(uint64_t x) {
496
+ #if defined(_WIN64) && defined(CROARING_REGULAR_VISUAL_STUDIO) && \
497
+ CROARING_REGULAR_VISUAL_STUDIO
498
+ #ifdef CROARING_USENEON
499
+ return vaddv_u8(vcnt_u8(vcreate_u8(input_num)));
500
+ #elif defined(_M_ARM64)
501
+ return roaring_hamming_backup(x);
502
+ // (int) _CountOneBits64(x); is unavailable
503
+ #else // _M_ARM64
504
+ return (int)__popcnt64(x);
505
+ #endif // _M_ARM64
506
+ #elif defined(_WIN32) && defined(CROARING_REGULAR_VISUAL_STUDIO) && \
507
+ CROARING_REGULAR_VISUAL_STUDIO
508
+ #ifdef _M_ARM
509
+ return roaring_hamming_backup(x);
510
+ // _CountOneBits is unavailable
511
+ #else // _M_ARM
512
+ return (int)__popcnt((unsigned int)x) +
513
+ (int)__popcnt((unsigned int)(x >> 32));
514
+ #endif // _M_ARM
515
+ #else
516
+ return __builtin_popcountll(x);
517
+ #endif
518
+ }
519
+
520
+ #ifndef UINT64_C
521
+ #define UINT64_C(c) (c##ULL)
522
+ #endif // UINT64_C
523
+
524
+ #ifndef UINT32_C
525
+ #define UINT32_C(c) (c##UL)
526
+ #endif // UINT32_C
527
+
528
+ #ifdef __cplusplus
529
+ } // extern "C" {
530
+ #endif // __cplusplus
531
+
532
+ // this is almost standard?
533
+ #undef STRINGIFY_IMPLEMENTATION_
534
+ #undef STRINGIFY
535
+ #define STRINGIFY_IMPLEMENTATION_(a) #a
536
+ #define STRINGIFY(a) STRINGIFY_IMPLEMENTATION_(a)
537
+
538
+ // Our fast kernels require 64-bit systems.
539
+ //
540
+ // On 32-bit x86, we lack 64-bit popcnt, lzcnt, blsr instructions.
541
+ // Furthermore, the number of SIMD registers is reduced.
542
+ //
543
+ // On 32-bit ARM, we would have smaller registers.
544
+ //
545
+ // The library should still have the fallback kernel. It is
546
+ // slower, but it should run everywhere.
547
+
548
+ //
549
+ // Enable valid runtime implementations, and select
550
+ // CROARING_BUILTIN_IMPLEMENTATION
551
+ //
552
+
553
+ // We are going to use runtime dispatch.
554
+ #if CROARING_IS_X64
555
+ #ifdef __clang__
556
+ // clang does not have GCC push pop
557
+ // warning: clang attribute push can't be used within a namespace in clang up
558
+ // til 8.0 so CROARING_TARGET_REGION and CROARING_UNTARGET_REGION must be
559
+ // *outside* of a namespace.
560
+ #define CROARING_TARGET_REGION(T) \
561
+ _Pragma(STRINGIFY(clang attribute push(__attribute__((target(T))), \
562
+ apply_to = function)))
563
+ #define CROARING_UNTARGET_REGION _Pragma("clang attribute pop")
564
+ #elif defined(__GNUC__)
565
+ // GCC is easier
566
+ #define CROARING_TARGET_REGION(T) \
567
+ _Pragma("GCC push_options") _Pragma(STRINGIFY(GCC target(T)))
568
+ #define CROARING_UNTARGET_REGION _Pragma("GCC pop_options")
569
+ #endif // clang then gcc
570
+
571
+ #endif // CROARING_IS_X64
572
+
573
+ // Default target region macros don't do anything.
574
+ #ifndef CROARING_TARGET_REGION
575
+ #define CROARING_TARGET_REGION(T)
576
+ #define CROARING_UNTARGET_REGION
577
+ #endif
578
+
579
+ #define CROARING_TARGET_AVX2 \
580
+ CROARING_TARGET_REGION("avx2,bmi,pclmul,lzcnt,popcnt")
581
+ #define CROARING_TARGET_AVX512 \
582
+ CROARING_TARGET_REGION( \
583
+ "avx2,bmi,bmi2,pclmul,lzcnt,popcnt,avx512f,avx512dq,avx512bw," \
584
+ "avx512vbmi2,avx512bitalg,avx512vpopcntdq")
585
+ #define CROARING_UNTARGET_AVX2 CROARING_UNTARGET_REGION
586
+ #define CROARING_UNTARGET_AVX512 CROARING_UNTARGET_REGION
587
+
588
+ #ifdef __AVX2__
589
+ // No need for runtime dispatching.
590
+ // It is unnecessary and harmful to old clang to tag regions.
591
+ #undef CROARING_TARGET_AVX2
592
+ #define CROARING_TARGET_AVX2
593
+ #undef CROARING_UNTARGET_AVX2
594
+ #define CROARING_UNTARGET_AVX2
595
+ #endif
596
+
597
+ #if defined(__AVX512F__) && defined(__AVX512DQ__) && defined(__AVX512BW__) && \
598
+ defined(__AVX512VBMI2__) && defined(__AVX512BITALG__) && \
599
+ defined(__AVX512VPOPCNTDQ__)
600
+ // No need for runtime dispatching.
601
+ // It is unnecessary and harmful to old clang to tag regions.
602
+ #undef CROARING_TARGET_AVX512
603
+ #define CROARING_TARGET_AVX512
604
+ #undef CROARING_UNTARGET_AVX512
605
+ #define CROARING_UNTARGET_AVX512
606
+ #endif
607
+
608
+ // Allow unaligned memory access
609
+ #if defined(__GNUC__) || defined(__clang__)
610
+ #define ALLOW_UNALIGNED __attribute__((no_sanitize("alignment")))
611
+ #else
612
+ #define ALLOW_UNALIGNED
613
+ #endif
614
+
615
+ #if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__)
616
+ #define CROARING_IS_BIG_ENDIAN (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
617
+ #elif defined(_WIN32)
618
+ #define CROARING_IS_BIG_ENDIAN 0
619
+ #else
620
+ #if defined(__APPLE__) || \
621
+ defined(__FreeBSD__) // defined __BYTE_ORDER__ && defined
622
+ // __ORDER_BIG_ENDIAN__
623
+ #include <machine/endian.h>
624
+ #elif defined(sun) || \
625
+ defined(__sun) // defined(__APPLE__) || defined(__FreeBSD__)
626
+ #include <sys/byteorder.h>
627
+ #else // defined(__APPLE__) || defined(__FreeBSD__)
628
+
629
+ #ifdef __has_include
630
+ #if __has_include(<endian.h>)
631
+ #include <endian.h>
632
+ #endif //__has_include(<endian.h>)
633
+ #endif //__has_include
634
+
635
+ #endif // defined(__APPLE__) || defined(__FreeBSD__)
636
+
637
+ #ifndef !defined(__BYTE_ORDER__) || !defined(__ORDER_LITTLE_ENDIAN__)
638
+ #define CROARING_IS_BIG_ENDIAN 0
639
+ #endif
640
+
641
+ #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
642
+ #define CROARING_IS_BIG_ENDIAN 0
643
+ #else // __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
644
+ #define CROARING_IS_BIG_ENDIAN 1
645
+ #endif // __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
646
+ #endif
647
+
648
+ // Host <-> big endian conversion.
649
+ #if CROARING_IS_BIG_ENDIAN
650
+ #define croaring_htobe64(x) (x)
651
+
652
+ #elif defined(_WIN32) || defined(_WIN64) // CROARING_IS_BIG_ENDIAN
653
+ #include <stdlib.h>
654
+ #define croaring_htobe64(x) _byteswap_uint64(x)
655
+
656
+ #elif defined(__APPLE__) // CROARING_IS_BIG_ENDIAN
657
+ #include <libkern/OSByteOrder.h>
658
+ #define croaring_htobe64(x) OSSwapInt64(x)
659
+
660
+ #elif defined(__has_include) && \
661
+ __has_include( \
662
+ <byteswap.h>) && (defined(__linux__) || defined(__FreeBSD__)) // CROARING_IS_BIG_ENDIAN
663
+ #include <byteswap.h>
664
+ #if defined(__linux__)
665
+ #define croaring_htobe64(x) bswap_64(x)
666
+ #elif defined(__FreeBSD__)
667
+ #define croaring_htobe64(x) bswap64(x)
668
+ #else
669
+ #warning "Unknown platform, report as an error"
670
+ #endif
671
+
672
+ #else // CROARING_IS_BIG_ENDIAN
673
+ // Gets compiled to bswap or equivalent on most compilers.
674
+ #define croaring_htobe64(x) \
675
+ (((x & 0x00000000000000FFULL) << 56) | \
676
+ ((x & 0x000000000000FF00ULL) << 40) | \
677
+ ((x & 0x0000000000FF0000ULL) << 24) | \
678
+ ((x & 0x00000000FF000000ULL) << 8) | ((x & 0x000000FF00000000ULL) >> 8) | \
679
+ ((x & 0x0000FF0000000000ULL) >> 24) | \
680
+ ((x & 0x00FF000000000000ULL) >> 40) | \
681
+ ((x & 0xFF00000000000000ULL) >> 56))
682
+ #endif // CROARING_IS_BIG_ENDIAN
683
+ #define croaring_be64toh(x) croaring_htobe64(x)
684
+ // End of host <-> big endian conversion.
685
+
686
+ // Defines for the possible CROARING atomic implementations
687
+ #define CROARING_ATOMIC_IMPL_NONE 1
688
+ #define CROARING_ATOMIC_IMPL_CPP 2
689
+ #define CROARING_ATOMIC_IMPL_C 3
690
+ #define CROARING_ATOMIC_IMPL_C_WINDOWS 4
691
+
692
+ // If the use has forced a specific implementation, use that, otherwise,
693
+ // figure out the best implementation we can use.
694
+ #if !defined(CROARING_ATOMIC_IMPL)
695
+ #if defined(__cplusplus) && __cplusplus >= 201103L
696
+ #ifdef __has_include
697
+ #if __has_include(<atomic>)
698
+ #define CROARING_ATOMIC_IMPL CROARING_ATOMIC_IMPL_CPP
699
+ #endif //__has_include(<atomic>)
700
+ #else
701
+ // We lack __has_include to check:
702
+ #define CROARING_ATOMIC_IMPL CROARING_ATOMIC_IMPL_CPP
703
+ #endif //__has_include
704
+ #elif __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_ATOMICS__)
705
+ #define CROARING_ATOMIC_IMPL CROARING_ATOMIC_IMPL_C
706
+ #elif CROARING_REGULAR_VISUAL_STUDIO
707
+ // https://www.technetworkhub.com/c11-atomics-in-visual-studio-2022-version-17/
708
+ #define CROARING_ATOMIC_IMPL CROARING_ATOMIC_IMPL_C_WINDOWS
709
+ #endif
710
+ #endif // !defined(CROARING_ATOMIC_IMPL)
711
+
712
+ #if CROARING_ATOMIC_IMPL == CROARING_ATOMIC_IMPL_C
713
+ #include <stdatomic.h>
714
+ typedef _Atomic(uint32_t) croaring_refcount_t;
715
+
716
+ static inline void croaring_refcount_inc(croaring_refcount_t *val) {
717
+ // Increasing the reference counter can always be done with
718
+ // memory_order_relaxed: New references to an object can only be formed from
719
+ // an existing reference, and passing an existing reference from one thread
720
+ // to another must already provide any required synchronization.
721
+ atomic_fetch_add_explicit(val, 1, memory_order_relaxed);
722
+ }
723
+
724
+ static inline bool croaring_refcount_dec(croaring_refcount_t *val) {
725
+ // It is important to enforce any possible access to the object in one
726
+ // thread (through an existing reference) to happen before deleting the
727
+ // object in a different thread. This is achieved by a "release" operation
728
+ // after dropping a reference (any access to the object through this
729
+ // reference must obviously happened before), and an "acquire" operation
730
+ // before deleting the object.
731
+ bool is_zero = atomic_fetch_sub_explicit(val, 1, memory_order_release) == 1;
732
+ if (is_zero) {
733
+ atomic_thread_fence(memory_order_acquire);
734
+ }
735
+ return is_zero;
736
+ }
737
+
738
+ static inline uint32_t croaring_refcount_get(const croaring_refcount_t *val) {
739
+ return atomic_load_explicit(val, memory_order_relaxed);
740
+ }
741
+ #elif CROARING_ATOMIC_IMPL == CROARING_ATOMIC_IMPL_CPP
742
+ #include <atomic>
743
+ typedef std::atomic<uint32_t> croaring_refcount_t;
744
+
745
+ static inline void croaring_refcount_inc(croaring_refcount_t *val) {
746
+ val->fetch_add(1, std::memory_order_relaxed);
747
+ }
748
+
749
+ static inline bool croaring_refcount_dec(croaring_refcount_t *val) {
750
+ // See above comments on the c11 atomic implementation for memory ordering
751
+ bool is_zero = val->fetch_sub(1, std::memory_order_release) == 1;
752
+ if (is_zero) {
753
+ std::atomic_thread_fence(std::memory_order_acquire);
754
+ }
755
+ return is_zero;
756
+ }
757
+
758
+ static inline uint32_t croaring_refcount_get(const croaring_refcount_t *val) {
759
+ return val->load(std::memory_order_relaxed);
760
+ }
761
+ #elif CROARING_ATOMIC_IMPL == CROARING_ATOMIC_IMPL_C_WINDOWS
762
+ #include <intrin.h>
763
+ #pragma intrinsic(_InterlockedIncrement)
764
+ #pragma intrinsic(_InterlockedDecrement)
765
+
766
+ // _InterlockedIncrement and _InterlockedDecrement take a (signed) long, and
767
+ // overflow is defined to wrap, so we can pretend it is a uint32_t for our case
768
+ typedef volatile long croaring_refcount_t;
769
+
770
+ static inline void croaring_refcount_inc(croaring_refcount_t *val) {
771
+ _InterlockedIncrement(val);
772
+ }
773
+
774
+ static inline bool croaring_refcount_dec(croaring_refcount_t *val) {
775
+ return _InterlockedDecrement(val) == 0;
776
+ }
777
+
778
+ static inline uint32_t croaring_refcount_get(const croaring_refcount_t *val) {
779
+ // Per
780
+ // https://learn.microsoft.com/en-us/windows/win32/sync/interlocked-variable-access
781
+ // > Simple reads and writes to properly-aligned 32-bit variables are atomic
782
+ // > operations. In other words, you will not end up with only one portion
783
+ // > of the variable updated; all bits are updated in an atomic fashion.
784
+ return *val;
785
+ }
786
+ #elif CROARING_ATOMIC_IMPL == CROARING_ATOMIC_IMPL_NONE
787
+ #include <assert.h>
788
+ typedef uint32_t croaring_refcount_t;
789
+
790
+ static inline void croaring_refcount_inc(croaring_refcount_t *val) {
791
+ *val += 1;
792
+ }
793
+
794
+ static inline bool croaring_refcount_dec(croaring_refcount_t *val) {
795
+ assert(*val > 0);
796
+ *val -= 1;
797
+ return val == 0;
798
+ }
799
+
800
+ static inline uint32_t croaring_refcount_get(const croaring_refcount_t *val) {
801
+ return *val;
802
+ }
803
+ #else
804
+ #error "Unknown atomic implementation"
805
+ #endif
806
+
807
+ #if defined(__GNUC__) || defined(__clang__)
808
+ #define CROARING_DEPRECATED __attribute__((deprecated))
809
+ #else
810
+ #define CROARING_DEPRECATED
811
+ #endif // defined(__GNUC__) || defined(__clang__)
812
+
813
+ // We need portability.h to be included first,
814
+ // but we also always want isadetection.h to be
815
+ // included (right after).
816
+ // See https://github.com/RoaringBitmap/CRoaring/issues/394
817
+ // There is no scenario where we want portability.h to
818
+ // be included, but not isadetection.h: the latter is a
819
+ // strict requirement.
820
+ #endif /* INCLUDE_PORTABILITY_H_ */
821
+ /* end file include/roaring/portability.h */
822
+ /* begin file include/roaring/bitset/bitset.h */
823
+ #ifndef CROARING_CBITSET_BITSET_H
824
+ #define CROARING_CBITSET_BITSET_H
825
+
826
+ // For compatibility with MSVC with the use of `restrict`
827
+ #if (__STDC_VERSION__ >= 199901L) || \
828
+ (defined(__GNUC__) && defined(__STDC_VERSION__))
829
+ #define CROARING_CBITSET_RESTRICT restrict
830
+ #else
831
+ #define CROARING_CBITSET_RESTRICT
832
+ #endif // (__STDC_VERSION__ >= 199901L) || (defined(__GNUC__) &&
833
+ // defined(__STDC_VERSION__ ))
834
+
835
+ #include <stdbool.h>
836
+ #include <stdint.h>
837
+ #include <stdio.h>
838
+ #include <stdlib.h>
839
+ #include <string.h>
840
+
841
+
842
+ #ifdef __cplusplus
843
+ extern "C" {
844
+ namespace roaring {
845
+ namespace api {
846
+ #endif
847
+
848
+ struct bitset_s {
849
+ uint64_t *CROARING_CBITSET_RESTRICT array;
850
+ /* For simplicity and performance, we prefer to have a size and a capacity
851
+ * that is a multiple of 64 bits. Thus we only track the size and the
852
+ * capacity in terms of 64-bit words allocated */
853
+ size_t arraysize;
854
+ size_t capacity;
855
+ };
856
+
857
+ typedef struct bitset_s bitset_t;
858
+
859
+ /* Create a new bitset. Return NULL in case of failure. */
860
+ bitset_t *bitset_create(void);
861
+
862
+ /* Create a new bitset able to contain size bits. Return NULL in case of
863
+ * failure. */
864
+ bitset_t *bitset_create_with_capacity(size_t size);
865
+
866
+ /* Free memory. */
867
+ void bitset_free(bitset_t *bitset);
868
+
869
+ /* Set all bits to zero. */
870
+ void bitset_clear(bitset_t *bitset);
871
+
872
+ /* Set all bits to one. */
873
+ void bitset_fill(bitset_t *bitset);
874
+
875
+ /* Create a copy */
876
+ bitset_t *bitset_copy(const bitset_t *bitset);
877
+
878
+ /* For advanced users: Resize the bitset so that it can support newarraysize *
879
+ * 64 bits. Return true in case of success, false for failure. Pad with zeroes
880
+ * new buffer areas if requested. */
881
+ bool bitset_resize(bitset_t *bitset, size_t newarraysize, bool padwithzeroes);
882
+
883
+ /* returns how many bytes of memory the backend buffer uses */
884
+ inline size_t bitset_size_in_bytes(const bitset_t *bitset) {
885
+ return bitset->arraysize * sizeof(uint64_t);
886
+ }
887
+
888
+ /* returns how many bits can be accessed */
889
+ inline size_t bitset_size_in_bits(const bitset_t *bitset) {
890
+ return bitset->arraysize * 64;
891
+ }
892
+
893
+ /* returns how many words (64-bit) of memory the backend buffer uses */
894
+ inline size_t bitset_size_in_words(const bitset_t *bitset) {
895
+ return bitset->arraysize;
896
+ }
897
+
898
+ /* For advanced users: Grow the bitset so that it can support newarraysize * 64
899
+ * bits with padding. Return true in case of success, false for failure. */
900
+ bool bitset_grow(bitset_t *bitset, size_t newarraysize);
901
+
902
+ /* attempts to recover unused memory, return false in case of
903
+ * roaring_reallocation failure */
904
+ bool bitset_trim(bitset_t *bitset);
905
+
906
+ /* shifts all bits by 's' positions so that the bitset representing values
907
+ * 1,2,10 would represent values 1+s, 2+s, 10+s */
908
+ void bitset_shift_left(bitset_t *bitset, size_t s);
909
+
910
+ /* shifts all bits by 's' positions so that the bitset representing values
911
+ * 1,2,10 would represent values 1-s, 2-s, 10-s, negative values are deleted */
912
+ void bitset_shift_right(bitset_t *bitset, size_t s);
913
+
914
+ /* Set the ith bit. Attempts to resize the bitset if needed (may silently fail)
915
+ */
916
+ inline void bitset_set(bitset_t *bitset, size_t i) {
917
+ size_t shiftedi = i / 64;
918
+ if (shiftedi >= bitset->arraysize) {
919
+ if (!bitset_grow(bitset, shiftedi + 1)) {
920
+ return;
921
+ }
922
+ }
923
+ bitset->array[shiftedi] |= ((uint64_t)1) << (i % 64);
924
+ }
925
+
926
+ /* Set the ith bit to the specified value. Attempts to resize the bitset if
927
+ * needed (may silently fail) */
928
+ inline void bitset_set_to_value(bitset_t *bitset, size_t i, bool flag) {
929
+ size_t shiftedi = i / 64;
930
+ uint64_t mask = ((uint64_t)1) << (i % 64);
931
+ uint64_t dynmask = ((uint64_t)flag) << (i % 64);
932
+ if (shiftedi >= bitset->arraysize) {
933
+ if (!bitset_grow(bitset, shiftedi + 1)) {
934
+ return;
935
+ }
936
+ }
937
+ uint64_t w = bitset->array[shiftedi];
938
+ w &= ~mask;
939
+ w |= dynmask;
940
+ bitset->array[shiftedi] = w;
941
+ }
942
+
943
+ /* Get the value of the ith bit. */
944
+ inline bool bitset_get(const bitset_t *bitset, size_t i) {
945
+ size_t shiftedi = i / 64;
946
+ if (shiftedi >= bitset->arraysize) {
947
+ return false;
948
+ }
949
+ return (bitset->array[shiftedi] & (((uint64_t)1) << (i % 64))) != 0;
950
+ }
951
+
952
+ /* Count number of bits set. */
953
+ size_t bitset_count(const bitset_t *bitset);
954
+
955
+ /* Find the index of the first bit set. Or zero if the bitset is empty. */
956
+ size_t bitset_minimum(const bitset_t *bitset);
957
+
958
+ /* Find the index of the last bit set. Or zero if the bitset is empty. */
959
+ size_t bitset_maximum(const bitset_t *bitset);
960
+
961
+ /* compute the union in-place (to b1), returns true if successful, to generate a
962
+ * new bitset first call bitset_copy */
963
+ bool bitset_inplace_union(bitset_t *CROARING_CBITSET_RESTRICT b1,
964
+ const bitset_t *CROARING_CBITSET_RESTRICT b2);
965
+
966
+ /* report the size of the union (without materializing it) */
967
+ size_t bitset_union_count(const bitset_t *CROARING_CBITSET_RESTRICT b1,
968
+ const bitset_t *CROARING_CBITSET_RESTRICT b2);
969
+
970
+ /* compute the intersection in-place (to b1), to generate a new bitset first
971
+ * call bitset_copy */
972
+ void bitset_inplace_intersection(bitset_t *CROARING_CBITSET_RESTRICT b1,
973
+ const bitset_t *CROARING_CBITSET_RESTRICT b2);
974
+
975
+ /* report the size of the intersection (without materializing it) */
976
+ size_t bitset_intersection_count(const bitset_t *CROARING_CBITSET_RESTRICT b1,
977
+ const bitset_t *CROARING_CBITSET_RESTRICT b2);
978
+
979
+ /* returns true if the bitsets contain no common elements */
980
+ bool bitsets_disjoint(const bitset_t *CROARING_CBITSET_RESTRICT b1,
981
+ const bitset_t *CROARING_CBITSET_RESTRICT b2);
982
+
983
+ /* returns true if the bitsets contain any common elements */
984
+ bool bitsets_intersect(const bitset_t *CROARING_CBITSET_RESTRICT b1,
985
+ const bitset_t *CROARING_CBITSET_RESTRICT b2);
986
+
987
+ /* returns true if b1 contains all of the set bits of b2 */
988
+ bool bitset_contains_all(const bitset_t *CROARING_CBITSET_RESTRICT b1,
989
+ const bitset_t *CROARING_CBITSET_RESTRICT b2);
990
+
991
+ /* compute the difference in-place (to b1), to generate a new bitset first call
992
+ * bitset_copy */
993
+ void bitset_inplace_difference(bitset_t *CROARING_CBITSET_RESTRICT b1,
994
+ const bitset_t *CROARING_CBITSET_RESTRICT b2);
995
+
996
+ /* compute the size of the difference */
997
+ size_t bitset_difference_count(const bitset_t *CROARING_CBITSET_RESTRICT b1,
998
+ const bitset_t *CROARING_CBITSET_RESTRICT b2);
999
+
1000
+ /* compute the symmetric difference in-place (to b1), return true if successful,
1001
+ * to generate a new bitset first call bitset_copy */
1002
+ bool bitset_inplace_symmetric_difference(
1003
+ bitset_t *CROARING_CBITSET_RESTRICT b1,
1004
+ const bitset_t *CROARING_CBITSET_RESTRICT b2);
1005
+
1006
+ /* compute the size of the symmetric difference */
1007
+ size_t bitset_symmetric_difference_count(
1008
+ const bitset_t *CROARING_CBITSET_RESTRICT b1,
1009
+ const bitset_t *CROARING_CBITSET_RESTRICT b2);
1010
+
1011
+ /* iterate over the set bits
1012
+ like so :
1013
+ for(size_t i = 0; bitset_next_set_bit(b,&i) ; i++) {
1014
+ //.....
1015
+ }
1016
+ */
1017
+ inline bool bitset_next_set_bit(const bitset_t *bitset, size_t *i) {
1018
+ size_t x = *i / 64;
1019
+ if (x >= bitset->arraysize) {
1020
+ return false;
1021
+ }
1022
+ uint64_t w = bitset->array[x];
1023
+ w >>= (*i & 63);
1024
+ if (w != 0) {
1025
+ *i += roaring_trailing_zeroes(w);
1026
+ return true;
1027
+ }
1028
+ x++;
1029
+ while (x < bitset->arraysize) {
1030
+ w = bitset->array[x];
1031
+ if (w != 0) {
1032
+ *i = x * 64 + roaring_trailing_zeroes(w);
1033
+ return true;
1034
+ }
1035
+ x++;
1036
+ }
1037
+ return false;
1038
+ }
1039
+
1040
+ /* iterate over the set bits
1041
+ like so :
1042
+ size_t buffer[256];
1043
+ size_t howmany = 0;
1044
+ for(size_t startfrom = 0; (howmany = bitset_next_set_bits(b,buffer,256,
1045
+ &startfrom)) > 0 ; startfrom++) {
1046
+ //.....
1047
+ }
1048
+ */
1049
+ inline size_t bitset_next_set_bits(const bitset_t *bitset, size_t *buffer,
1050
+ size_t capacity, size_t *startfrom) {
1051
+ if (capacity == 0) return 0; // sanity check
1052
+ size_t x = *startfrom / 64;
1053
+ if (x >= bitset->arraysize) {
1054
+ return 0; // nothing more to iterate over
1055
+ }
1056
+ uint64_t w = bitset->array[x];
1057
+ w >>= (*startfrom & 63);
1058
+ size_t howmany = 0;
1059
+ size_t base = x << 6;
1060
+ while (howmany < capacity) {
1061
+ while (w != 0) {
1062
+ uint64_t t = w & (~w + 1);
1063
+ int r = roaring_trailing_zeroes(w);
1064
+ buffer[howmany++] = r + base;
1065
+ if (howmany == capacity) goto end;
1066
+ w ^= t;
1067
+ }
1068
+ x += 1;
1069
+ if (x == bitset->arraysize) {
1070
+ break;
1071
+ }
1072
+ base += 64;
1073
+ w = bitset->array[x];
1074
+ }
1075
+ end:
1076
+ if (howmany > 0) {
1077
+ *startfrom = buffer[howmany - 1];
1078
+ }
1079
+ return howmany;
1080
+ }
1081
+
1082
+ typedef bool (*bitset_iterator)(size_t value, void *param);
1083
+
1084
+ // return true if uninterrupted
1085
+ inline bool bitset_for_each(const bitset_t *b, bitset_iterator iterator,
1086
+ void *ptr) {
1087
+ size_t base = 0;
1088
+ for (size_t i = 0; i < b->arraysize; ++i) {
1089
+ uint64_t w = b->array[i];
1090
+ while (w != 0) {
1091
+ uint64_t t = w & (~w + 1);
1092
+ int r = roaring_trailing_zeroes(w);
1093
+ if (!iterator(r + base, ptr)) return false;
1094
+ w ^= t;
1095
+ }
1096
+ base += 64;
1097
+ }
1098
+ return true;
1099
+ }
1100
+
1101
+ inline void bitset_print(const bitset_t *b) {
1102
+ printf("{");
1103
+ for (size_t i = 0; bitset_next_set_bit(b, &i); i++) {
1104
+ printf("%zu, ", i);
1105
+ }
1106
+ printf("}");
1107
+ }
1108
+
1109
+ #ifdef __cplusplus
1110
+ }
1111
+ }
1112
+ } // extern "C" { namespace roaring { namespace api {
1113
+ #endif
1114
+
1115
+ #endif
1116
+ /* end file include/roaring/bitset/bitset.h */
1117
+ /* begin file include/roaring/roaring.h */
1118
+ /*
1119
+ * An implementation of Roaring Bitmaps in C.
1120
+ */
1121
+
1122
+ #ifndef ROARING_H
1123
+ #define ROARING_H
1124
+
1125
+ #include <stdbool.h>
1126
+ #include <stddef.h> // for `size_t`
1127
+ #include <stdint.h>
1128
+
1129
+
1130
+ // Include other headers after roaring_types.h
1131
+
1132
+ #ifdef __cplusplus
1133
+ extern "C" {
1134
+ namespace roaring {
1135
+ namespace api {
1136
+ #endif
1137
+
1138
+ typedef struct roaring_bitmap_s {
1139
+ roaring_array_t high_low_container;
1140
+ } roaring_bitmap_t;
1141
+
1142
+ /**
1143
+ * Dynamically allocates a new bitmap (initially empty).
1144
+ * Returns NULL if the allocation fails.
1145
+ * Capacity is a performance hint for how many "containers" the data will need.
1146
+ * Client is responsible for calling `roaring_bitmap_free()`.
1147
+ */
1148
+ roaring_bitmap_t *roaring_bitmap_create_with_capacity(uint32_t cap);
1149
+
1150
+ /**
1151
+ * Dynamically allocates a new bitmap (initially empty).
1152
+ * Returns NULL if the allocation fails.
1153
+ * Client is responsible for calling `roaring_bitmap_free()`.
1154
+ */
1155
+ inline roaring_bitmap_t *roaring_bitmap_create(void) {
1156
+ return roaring_bitmap_create_with_capacity(0);
1157
+ }
1158
+
1159
+ /**
1160
+ * Initialize a roaring bitmap structure in memory controlled by client.
1161
+ * Capacity is a performance hint for how many "containers" the data will need.
1162
+ * Can return false if auxiliary allocations fail when capacity greater than 0.
1163
+ */
1164
+ bool roaring_bitmap_init_with_capacity(roaring_bitmap_t *r, uint32_t cap);
1165
+
1166
+ /**
1167
+ * Initialize a roaring bitmap structure in memory controlled by client.
1168
+ * The bitmap will be in a "clear" state, with no auxiliary allocations.
1169
+ * Since this performs no allocations, the function will not fail.
1170
+ */
1171
+ inline void roaring_bitmap_init_cleared(roaring_bitmap_t *r) {
1172
+ roaring_bitmap_init_with_capacity(r, 0);
1173
+ }
1174
+
1175
+ /**
1176
+ * Add all the values between min (included) and max (excluded) that are at a
1177
+ * distance k*step from min.
1178
+ */
1179
+ roaring_bitmap_t *roaring_bitmap_from_range(uint64_t min, uint64_t max,
1180
+ uint32_t step);
1181
+
1182
+ /**
1183
+ * Creates a new bitmap from a pointer of uint32_t integers
1184
+ */
1185
+ roaring_bitmap_t *roaring_bitmap_of_ptr(size_t n_args, const uint32_t *vals);
1186
+
1187
+ /*
1188
+ * Whether you want to use copy-on-write.
1189
+ * Saves memory and avoids copies, but needs more care in a threaded context.
1190
+ * Most users should ignore this flag.
1191
+ *
1192
+ * Note: If you do turn this flag to 'true', enabling COW, then ensure that you
1193
+ * do so for all of your bitmaps, since interactions between bitmaps with and
1194
+ * without COW is unsafe.
1195
+ */
1196
+ inline bool roaring_bitmap_get_copy_on_write(const roaring_bitmap_t *r) {
1197
+ return r->high_low_container.flags & ROARING_FLAG_COW;
1198
+ }
1199
+ inline void roaring_bitmap_set_copy_on_write(roaring_bitmap_t *r, bool cow) {
1200
+ if (cow) {
1201
+ r->high_low_container.flags |= ROARING_FLAG_COW;
1202
+ } else {
1203
+ r->high_low_container.flags &= ~ROARING_FLAG_COW;
1204
+ }
1205
+ }
1206
+
1207
+ roaring_bitmap_t *roaring_bitmap_add_offset(const roaring_bitmap_t *bm,
1208
+ int64_t offset);
1209
+ /**
1210
+ * Describe the inner structure of the bitmap.
1211
+ */
1212
+ void roaring_bitmap_printf_describe(const roaring_bitmap_t *r);
1213
+
1214
+ /**
1215
+ * Creates a new bitmap from a list of uint32_t integers
1216
+ *
1217
+ * This function is deprecated, use `roaring_bitmap_from` instead, which
1218
+ * doesn't require the number of elements to be passed in.
1219
+ *
1220
+ * @see roaring_bitmap_from
1221
+ */
1222
+ CROARING_DEPRECATED roaring_bitmap_t *roaring_bitmap_of(size_t n, ...);
1223
+
1224
+ #ifdef __cplusplus
1225
+ /**
1226
+ * Creates a new bitmap which contains all values passed in as arguments.
1227
+ *
1228
+ * To create a bitmap from a variable number of arguments, use the
1229
+ * `roaring_bitmap_of_ptr` function instead.
1230
+ */
1231
+ // Use an immediately invoked closure, capturing by reference
1232
+ // (in case __VA_ARGS__ refers to context outside the closure)
1233
+ // Include a 0 at the beginning of the array to make the array length > 0
1234
+ // (zero sized arrays are not valid in standard c/c++)
1235
+ #define roaring_bitmap_from(...) \
1236
+ [&]() { \
1237
+ const uint32_t roaring_bitmap_from_array[] = {0, __VA_ARGS__}; \
1238
+ return roaring_bitmap_of_ptr((sizeof(roaring_bitmap_from_array) / \
1239
+ sizeof(roaring_bitmap_from_array[0])) - \
1240
+ 1, \
1241
+ &roaring_bitmap_from_array[1]); \
1242
+ }()
1243
+ #else
1244
+ /**
1245
+ * Creates a new bitmap which contains all values passed in as arguments.
1246
+ *
1247
+ * To create a bitmap from a variable number of arguments, use the
1248
+ * `roaring_bitmap_of_ptr` function instead.
1249
+ */
1250
+ // While __VA_ARGS__ occurs twice in expansion, one of the times is in a sizeof
1251
+ // expression, which is an unevaluated context, so it's even safe in the case
1252
+ // where expressions passed have side effects (roaring64_bitmap_from(my_func(),
1253
+ // ++i))
1254
+ // Include a 0 at the beginning of the array to make the array length > 0
1255
+ // (zero sized arrays are not valid in standard c/c++)
1256
+ #define roaring_bitmap_from(...) \
1257
+ roaring_bitmap_of_ptr( \
1258
+ (sizeof((const uint32_t[]){0, __VA_ARGS__}) / sizeof(uint32_t)) - 1, \
1259
+ &((const uint32_t[]){0, __VA_ARGS__})[1])
1260
+ #endif
1261
+
1262
+ /**
1263
+ * Copies a bitmap (this does memory allocation).
1264
+ * The caller is responsible for memory management.
1265
+ */
1266
+ roaring_bitmap_t *roaring_bitmap_copy(const roaring_bitmap_t *r);
1267
+
1268
+ /**
1269
+ * Copies a bitmap from src to dest. It is assumed that the pointer dest
1270
+ * is to an already allocated bitmap. The content of the dest bitmap is
1271
+ * freed/deleted.
1272
+ *
1273
+ * It might be preferable and simpler to call roaring_bitmap_copy except
1274
+ * that roaring_bitmap_overwrite can save on memory allocations.
1275
+ *
1276
+ * Returns true if successful, or false if there was an error. On failure,
1277
+ * the dest bitmap is left in a valid, empty state (even if it was not empty
1278
+ * before).
1279
+ */
1280
+ bool roaring_bitmap_overwrite(roaring_bitmap_t *dest,
1281
+ const roaring_bitmap_t *src);
1282
+
1283
+ /**
1284
+ * Print the content of the bitmap.
1285
+ */
1286
+ void roaring_bitmap_printf(const roaring_bitmap_t *r);
1287
+
1288
+ /**
1289
+ * Computes the intersection between two bitmaps and returns new bitmap. The
1290
+ * caller is responsible for memory management.
1291
+ *
1292
+ * Performance hint: if you are computing the intersection between several
1293
+ * bitmaps, two-by-two, it is best to start with the smallest bitmap.
1294
+ * You may also rely on roaring_bitmap_and_inplace to avoid creating
1295
+ * many temporary bitmaps.
1296
+ */
1297
+ roaring_bitmap_t *roaring_bitmap_and(const roaring_bitmap_t *r1,
1298
+ const roaring_bitmap_t *r2);
1299
+
1300
+ /**
1301
+ * Computes the size of the intersection between two bitmaps.
1302
+ */
1303
+ uint64_t roaring_bitmap_and_cardinality(const roaring_bitmap_t *r1,
1304
+ const roaring_bitmap_t *r2);
1305
+
1306
+ /**
1307
+ * Check whether two bitmaps intersect.
1308
+ */
1309
+ bool roaring_bitmap_intersect(const roaring_bitmap_t *r1,
1310
+ const roaring_bitmap_t *r2);
1311
+
1312
+ /**
1313
+ * Check whether a bitmap and an open range intersect.
1314
+ */
1315
+ bool roaring_bitmap_intersect_with_range(const roaring_bitmap_t *bm, uint64_t x,
1316
+ uint64_t y);
1317
+
1318
+ /**
1319
+ * Computes the Jaccard index between two bitmaps. (Also known as the Tanimoto
1320
+ * distance, or the Jaccard similarity coefficient)
1321
+ *
1322
+ * The Jaccard index is undefined if both bitmaps are empty.
1323
+ */
1324
+ double roaring_bitmap_jaccard_index(const roaring_bitmap_t *r1,
1325
+ const roaring_bitmap_t *r2);
1326
+
1327
+ /**
1328
+ * Computes the size of the union between two bitmaps.
1329
+ */
1330
+ uint64_t roaring_bitmap_or_cardinality(const roaring_bitmap_t *r1,
1331
+ const roaring_bitmap_t *r2);
1332
+
1333
+ /**
1334
+ * Computes the size of the difference (andnot) between two bitmaps.
1335
+ */
1336
+ uint64_t roaring_bitmap_andnot_cardinality(const roaring_bitmap_t *r1,
1337
+ const roaring_bitmap_t *r2);
1338
+
1339
+ /**
1340
+ * Computes the size of the symmetric difference (xor) between two bitmaps.
1341
+ */
1342
+ uint64_t roaring_bitmap_xor_cardinality(const roaring_bitmap_t *r1,
1343
+ const roaring_bitmap_t *r2);
1344
+
1345
+ /**
1346
+ * Inplace version of `roaring_bitmap_and()`, modifies r1
1347
+ * r1 == r2 is allowed.
1348
+ *
1349
+ * Performance hint: if you are computing the intersection between several
1350
+ * bitmaps, two-by-two, it is best to start with the smallest bitmap.
1351
+ */
1352
+ void roaring_bitmap_and_inplace(roaring_bitmap_t *r1,
1353
+ const roaring_bitmap_t *r2);
1354
+
1355
+ /**
1356
+ * Computes the union between two bitmaps and returns new bitmap. The caller is
1357
+ * responsible for memory management.
1358
+ */
1359
+ roaring_bitmap_t *roaring_bitmap_or(const roaring_bitmap_t *r1,
1360
+ const roaring_bitmap_t *r2);
1361
+
1362
+ /**
1363
+ * Inplace version of `roaring_bitmap_or(), modifies r1.
1364
+ * TODO: decide whether r1 == r2 ok
1365
+ */
1366
+ void roaring_bitmap_or_inplace(roaring_bitmap_t *r1,
1367
+ const roaring_bitmap_t *r2);
1368
+
1369
+ /**
1370
+ * Compute the union of 'number' bitmaps.
1371
+ * Caller is responsible for freeing the result.
1372
+ * See also `roaring_bitmap_or_many_heap()`
1373
+ */
1374
+ roaring_bitmap_t *roaring_bitmap_or_many(size_t number,
1375
+ const roaring_bitmap_t **rs);
1376
+
1377
+ /**
1378
+ * Compute the union of 'number' bitmaps using a heap. This can sometimes be
1379
+ * faster than `roaring_bitmap_or_many() which uses a naive algorithm.
1380
+ * Caller is responsible for freeing the result.
1381
+ */
1382
+ roaring_bitmap_t *roaring_bitmap_or_many_heap(uint32_t number,
1383
+ const roaring_bitmap_t **rs);
1384
+
1385
+ /**
1386
+ * Computes the symmetric difference (xor) between two bitmaps
1387
+ * and returns new bitmap. The caller is responsible for memory management.
1388
+ */
1389
+ roaring_bitmap_t *roaring_bitmap_xor(const roaring_bitmap_t *r1,
1390
+ const roaring_bitmap_t *r2);
1391
+
1392
+ /**
1393
+ * Inplace version of roaring_bitmap_xor, modifies r1, r1 != r2.
1394
+ */
1395
+ void roaring_bitmap_xor_inplace(roaring_bitmap_t *r1,
1396
+ const roaring_bitmap_t *r2);
1397
+
1398
+ /**
1399
+ * Compute the xor of 'number' bitmaps.
1400
+ * Caller is responsible for freeing the result.
1401
+ */
1402
+ roaring_bitmap_t *roaring_bitmap_xor_many(size_t number,
1403
+ const roaring_bitmap_t **rs);
1404
+
1405
+ /**
1406
+ * Computes the difference (andnot) between two bitmaps and returns new bitmap.
1407
+ * Caller is responsible for freeing the result.
1408
+ */
1409
+ roaring_bitmap_t *roaring_bitmap_andnot(const roaring_bitmap_t *r1,
1410
+ const roaring_bitmap_t *r2);
1411
+
1412
+ /**
1413
+ * Inplace version of roaring_bitmap_andnot, modifies r1, r1 != r2.
1414
+ */
1415
+ void roaring_bitmap_andnot_inplace(roaring_bitmap_t *r1,
1416
+ const roaring_bitmap_t *r2);
1417
+
1418
+ /**
1419
+ * TODO: consider implementing:
1420
+ *
1421
+ * "Compute the xor of 'number' bitmaps using a heap. This can sometimes be
1422
+ * faster than roaring_bitmap_xor_many which uses a naive algorithm. Caller is
1423
+ * responsible for freeing the result.""
1424
+ *
1425
+ * roaring_bitmap_t *roaring_bitmap_xor_many_heap(uint32_t number,
1426
+ * const roaring_bitmap_t **rs);
1427
+ */
1428
+
1429
+ /**
1430
+ * Frees the memory.
1431
+ */
1432
+ void roaring_bitmap_free(const roaring_bitmap_t *r);
1433
+
1434
+ /**
1435
+ * A bit of context usable with `roaring_bitmap_*_bulk()` functions
1436
+ *
1437
+ * Should be initialized with `{0}` (or `memset()` to all zeros).
1438
+ * Callers should treat it as an opaque type.
1439
+ *
1440
+ * A context may only be used with a single bitmap
1441
+ * (unless re-initialized to zero), and any modification to a bitmap
1442
+ * (other than modifications performed with `_bulk()` functions with the context
1443
+ * passed) will invalidate any contexts associated with that bitmap.
1444
+ */
1445
+ typedef struct roaring_bulk_context_s {
1446
+ ROARING_CONTAINER_T *container;
1447
+ int idx;
1448
+ uint16_t key;
1449
+ uint8_t typecode;
1450
+ } roaring_bulk_context_t;
1451
+
1452
+ /**
1453
+ * Add an item, using context from a previous insert for speed optimization.
1454
+ *
1455
+ * `context` will be used to store information between calls to make bulk
1456
+ * operations faster. `*context` should be zero-initialized before the first
1457
+ * call to this function.
1458
+ *
1459
+ * Modifying the bitmap in any way (other than `-bulk` suffixed functions)
1460
+ * will invalidate the stored context, calling this function with a non-zero
1461
+ * context after doing any modification invokes undefined behavior.
1462
+ *
1463
+ * In order to exploit this optimization, the caller should call this function
1464
+ * with values with the same "key" (high 16 bits of the value) consecutively.
1465
+ */
1466
+ void roaring_bitmap_add_bulk(roaring_bitmap_t *r,
1467
+ roaring_bulk_context_t *context, uint32_t val);
1468
+
1469
+ /**
1470
+ * Add value n_args from pointer vals, faster than repeatedly calling
1471
+ * `roaring_bitmap_add()`
1472
+ *
1473
+ * In order to exploit this optimization, the caller should attempt to keep
1474
+ * values with the same "key" (high 16 bits of the value) as consecutive
1475
+ * elements in `vals`
1476
+ */
1477
+ void roaring_bitmap_add_many(roaring_bitmap_t *r, size_t n_args,
1478
+ const uint32_t *vals);
1479
+
1480
+ /**
1481
+ * Add value x
1482
+ */
1483
+ void roaring_bitmap_add(roaring_bitmap_t *r, uint32_t x);
1484
+
1485
+ /**
1486
+ * Add value x
1487
+ * Returns true if a new value was added, false if the value already existed.
1488
+ */
1489
+ bool roaring_bitmap_add_checked(roaring_bitmap_t *r, uint32_t x);
1490
+
1491
+ /**
1492
+ * Add all values in range [min, max]
1493
+ */
1494
+ void roaring_bitmap_add_range_closed(roaring_bitmap_t *r, uint32_t min,
1495
+ uint32_t max);
1496
+
1497
+ /**
1498
+ * Add all values in range [min, max)
1499
+ */
1500
+ inline void roaring_bitmap_add_range(roaring_bitmap_t *r, uint64_t min,
1501
+ uint64_t max) {
1502
+ if (max <= min) return;
1503
+ roaring_bitmap_add_range_closed(r, (uint32_t)min, (uint32_t)(max - 1));
1504
+ }
1505
+
1506
+ /**
1507
+ * Remove value x
1508
+ */
1509
+ void roaring_bitmap_remove(roaring_bitmap_t *r, uint32_t x);
1510
+
1511
+ /**
1512
+ * Remove all values in range [min, max]
1513
+ */
1514
+ void roaring_bitmap_remove_range_closed(roaring_bitmap_t *r, uint32_t min,
1515
+ uint32_t max);
1516
+
1517
+ /**
1518
+ * Remove all values in range [min, max)
1519
+ */
1520
+ inline void roaring_bitmap_remove_range(roaring_bitmap_t *r, uint64_t min,
1521
+ uint64_t max) {
1522
+ if (max <= min) return;
1523
+ roaring_bitmap_remove_range_closed(r, (uint32_t)min, (uint32_t)(max - 1));
1524
+ }
1525
+
1526
+ /**
1527
+ * Remove multiple values
1528
+ */
1529
+ void roaring_bitmap_remove_many(roaring_bitmap_t *r, size_t n_args,
1530
+ const uint32_t *vals);
1531
+
1532
+ /**
1533
+ * Remove value x
1534
+ * Returns true if a new value was removed, false if the value was not existing.
1535
+ */
1536
+ bool roaring_bitmap_remove_checked(roaring_bitmap_t *r, uint32_t x);
1537
+
1538
+ /**
1539
+ * Check if value is present
1540
+ */
1541
+ bool roaring_bitmap_contains(const roaring_bitmap_t *r, uint32_t val);
1542
+
1543
+ /**
1544
+ * Check whether a range of values from range_start (included)
1545
+ * to range_end (excluded) is present
1546
+ */
1547
+ bool roaring_bitmap_contains_range(const roaring_bitmap_t *r,
1548
+ uint64_t range_start, uint64_t range_end);
1549
+
1550
+ /**
1551
+ * Check if an items is present, using context from a previous insert or search
1552
+ * for speed optimization.
1553
+ *
1554
+ * `context` will be used to store information between calls to make bulk
1555
+ * operations faster. `*context` should be zero-initialized before the first
1556
+ * call to this function.
1557
+ *
1558
+ * Modifying the bitmap in any way (other than `-bulk` suffixed functions)
1559
+ * will invalidate the stored context, calling this function with a non-zero
1560
+ * context after doing any modification invokes undefined behavior.
1561
+ *
1562
+ * In order to exploit this optimization, the caller should call this function
1563
+ * with values with the same "key" (high 16 bits of the value) consecutively.
1564
+ */
1565
+ bool roaring_bitmap_contains_bulk(const roaring_bitmap_t *r,
1566
+ roaring_bulk_context_t *context,
1567
+ uint32_t val);
1568
+
1569
+ /**
1570
+ * Get the cardinality of the bitmap (number of elements).
1571
+ */
1572
+ uint64_t roaring_bitmap_get_cardinality(const roaring_bitmap_t *r);
1573
+
1574
+ /**
1575
+ * Returns the number of elements in the range [range_start, range_end).
1576
+ */
1577
+ uint64_t roaring_bitmap_range_cardinality(const roaring_bitmap_t *r,
1578
+ uint64_t range_start,
1579
+ uint64_t range_end);
1580
+
1581
+ /**
1582
+ * Returns true if the bitmap is empty (cardinality is zero).
1583
+ */
1584
+ bool roaring_bitmap_is_empty(const roaring_bitmap_t *r);
1585
+
1586
+ /**
1587
+ * Empties the bitmap. It will have no auxiliary allocations (so if the bitmap
1588
+ * was initialized in client memory via roaring_bitmap_init(), then a call to
1589
+ * roaring_bitmap_clear() would be enough to "free" it)
1590
+ */
1591
+ void roaring_bitmap_clear(roaring_bitmap_t *r);
1592
+
1593
+ /**
1594
+ * Convert the bitmap to a sorted array, output in `ans`.
1595
+ *
1596
+ * Caller is responsible to ensure that there is enough memory allocated, e.g.
1597
+ *
1598
+ * ans = malloc(roaring_bitmap_get_cardinality(bitmap) * sizeof(uint32_t));
1599
+ */
1600
+ void roaring_bitmap_to_uint32_array(const roaring_bitmap_t *r, uint32_t *ans);
1601
+
1602
+ /**
1603
+ * Store the bitmap to a bitset. This can be useful for people
1604
+ * who need the performance and simplicity of a standard bitset.
1605
+ * We assume that the input bitset is originally empty (does not
1606
+ * have any set bit).
1607
+ *
1608
+ * bitset_t * out = bitset_create();
1609
+ * // if the bitset has content in it, call "bitset_clear(out)"
1610
+ * bool success = roaring_bitmap_to_bitset(mybitmap, out);
1611
+ * // on failure, success will be false.
1612
+ * // You can then query the bitset:
1613
+ * bool is_present = bitset_get(out, 10011 );
1614
+ * // you must free the memory:
1615
+ * bitset_free(out);
1616
+ *
1617
+ */
1618
+ bool roaring_bitmap_to_bitset(const roaring_bitmap_t *r, bitset_t *bitset);
1619
+
1620
+ /**
1621
+ * Convert the bitmap to a sorted array from `offset` by `limit`, output in
1622
+ * `ans`.
1623
+ *
1624
+ * Caller is responsible to ensure that there is enough memory allocated, e.g.
1625
+ *
1626
+ * ans = malloc(roaring_bitmap_get_cardinality(limit) * sizeof(uint32_t));
1627
+ *
1628
+ * Return false in case of failure (e.g., insufficient memory)
1629
+ */
1630
+ bool roaring_bitmap_range_uint32_array(const roaring_bitmap_t *r, size_t offset,
1631
+ size_t limit, uint32_t *ans);
1632
+
1633
+ /**
1634
+ * Remove run-length encoding even when it is more space efficient.
1635
+ * Return whether a change was applied.
1636
+ */
1637
+ bool roaring_bitmap_remove_run_compression(roaring_bitmap_t *r);
1638
+
1639
+ /**
1640
+ * Convert array and bitmap containers to run containers when it is more
1641
+ * efficient; also convert from run containers when more space efficient.
1642
+ *
1643
+ * Returns true if the result has at least one run container.
1644
+ * Additional savings might be possible by calling `shrinkToFit()`.
1645
+ */
1646
+ bool roaring_bitmap_run_optimize(roaring_bitmap_t *r);
1647
+
1648
+ /**
1649
+ * If needed, reallocate memory to shrink the memory usage.
1650
+ * Returns the number of bytes saved.
1651
+ */
1652
+ size_t roaring_bitmap_shrink_to_fit(roaring_bitmap_t *r);
1653
+
1654
+ /**
1655
+ * Write the bitmap to an output pointer, this output buffer should refer to
1656
+ * at least `roaring_bitmap_size_in_bytes(r)` allocated bytes.
1657
+ *
1658
+ * See `roaring_bitmap_portable_serialize()` if you want a format that's
1659
+ * compatible with Java and Go implementations. This format can sometimes be
1660
+ * more space efficient than the portable form, e.g. when the data is sparse.
1661
+ *
1662
+ * Returns how many bytes written, should be `roaring_bitmap_size_in_bytes(r)`.
1663
+ *
1664
+ * This function is endian-sensitive. If you have a big-endian system (e.g., a
1665
+ * mainframe IBM s390x), the data format is going to be big-endian and not
1666
+ * compatible with little-endian systems.
1667
+ */
1668
+ size_t roaring_bitmap_serialize(const roaring_bitmap_t *r, char *buf);
1669
+
1670
+ /**
1671
+ * Use with `roaring_bitmap_serialize()`.
1672
+ *
1673
+ * (See `roaring_bitmap_portable_deserialize()` if you want a format that's
1674
+ * compatible with Java and Go implementations).
1675
+ *
1676
+ * This function is endian-sensitive. If you have a big-endian system (e.g., a
1677
+ * mainframe IBM s390x), the data format is going to be big-endian and not
1678
+ * compatible with little-endian systems.
1679
+ */
1680
+ roaring_bitmap_t *roaring_bitmap_deserialize(const void *buf);
1681
+
1682
+ /**
1683
+ * Use with `roaring_bitmap_serialize()`.
1684
+ *
1685
+ * (See `roaring_bitmap_portable_deserialize_safe()` if you want a format that's
1686
+ * compatible with Java and Go implementations).
1687
+ *
1688
+ * This function is endian-sensitive. If you have a big-endian system (e.g., a
1689
+ * mainframe IBM s390x), the data format is going to be big-endian and not
1690
+ * compatible with little-endian systems.
1691
+ *
1692
+ * The difference with `roaring_bitmap_deserialize()` is that this function
1693
+ * checks that the input buffer is a valid bitmap. If the buffer is too small,
1694
+ * NULL is returned.
1695
+ */
1696
+ roaring_bitmap_t *roaring_bitmap_deserialize_safe(const void *buf,
1697
+ size_t maxbytes);
1698
+
1699
+ /**
1700
+ * How many bytes are required to serialize this bitmap (NOT compatible
1701
+ * with Java and Go versions)
1702
+ */
1703
+ size_t roaring_bitmap_size_in_bytes(const roaring_bitmap_t *r);
1704
+
1705
+ /**
1706
+ * Read bitmap from a serialized buffer.
1707
+ * In case of failure, NULL is returned.
1708
+ *
1709
+ * This function is unsafe in the sense that if there is no valid serialized
1710
+ * bitmap at the pointer, then many bytes could be read, possibly causing a
1711
+ * buffer overflow. See also roaring_bitmap_portable_deserialize_safe().
1712
+ *
1713
+ * This is meant to be compatible with the Java and Go versions:
1714
+ * https://github.com/RoaringBitmap/RoaringFormatSpec
1715
+ *
1716
+ * This function is endian-sensitive. If you have a big-endian system (e.g., a
1717
+ * mainframe IBM s390x), the data format is going to be big-endian and not
1718
+ * compatible with little-endian systems.
1719
+ */
1720
+ roaring_bitmap_t *roaring_bitmap_portable_deserialize(const char *buf);
1721
+
1722
+ /**
1723
+ * Read bitmap from a serialized buffer safely (reading up to maxbytes).
1724
+ * In case of failure, NULL is returned.
1725
+ *
1726
+ * This is meant to be compatible with the Java and Go versions:
1727
+ * https://github.com/RoaringBitmap/RoaringFormatSpec
1728
+ *
1729
+ * The function itself is safe in the sense that it will not cause buffer
1730
+ * overflows. However, for correct operations, it is assumed that the bitmap
1731
+ * read was once serialized from a valid bitmap (i.e., it follows the format
1732
+ * specification). If you provided an incorrect input (garbage), then the bitmap
1733
+ * read may not be in a valid state and following operations may not lead to
1734
+ * sensible results. In particular, the serialized array containers need to be
1735
+ * in sorted order, and the run containers should be in sorted non-overlapping
1736
+ * order. This is is guaranteed to happen when serializing an existing bitmap,
1737
+ * but not for random inputs.
1738
+ *
1739
+ * You may use roaring_bitmap_internal_validate to check the validity of the
1740
+ * bitmap prior to using it. You may also use other strategies to check for
1741
+ * corrupted inputs (e.g., checksums).
1742
+ *
1743
+ * This function is endian-sensitive. If you have a big-endian system (e.g., a
1744
+ * mainframe IBM s390x), the data format is going to be big-endian and not
1745
+ * compatible with little-endian systems.
1746
+ */
1747
+ roaring_bitmap_t *roaring_bitmap_portable_deserialize_safe(const char *buf,
1748
+ size_t maxbytes);
1749
+
1750
+ /**
1751
+ * Read bitmap from a serialized buffer.
1752
+ * In case of failure, NULL is returned.
1753
+ *
1754
+ * Bitmap returned by this function can be used in all readonly contexts.
1755
+ * Bitmap must be freed as usual, by calling roaring_bitmap_free().
1756
+ * Underlying buffer must not be freed or modified while it backs any bitmaps.
1757
+ *
1758
+ * The function is unsafe in the following ways:
1759
+ * 1) It may execute unaligned memory accesses.
1760
+ * 2) A buffer overflow may occur if buf does not point to a valid serialized
1761
+ * bitmap.
1762
+ *
1763
+ * This is meant to be compatible with the Java and Go versions:
1764
+ * https://github.com/RoaringBitmap/RoaringFormatSpec
1765
+ *
1766
+ * This function is endian-sensitive. If you have a big-endian system (e.g., a
1767
+ * mainframe IBM s390x), the data format is going to be big-endian and not
1768
+ * compatible with little-endian systems.
1769
+ */
1770
+ roaring_bitmap_t *roaring_bitmap_portable_deserialize_frozen(const char *buf);
1771
+
1772
+ /**
1773
+ * Check how many bytes would be read (up to maxbytes) at this pointer if there
1774
+ * is a bitmap, returns zero if there is no valid bitmap.
1775
+ *
1776
+ * This is meant to be compatible with the Java and Go versions:
1777
+ * https://github.com/RoaringBitmap/RoaringFormatSpec
1778
+ */
1779
+ size_t roaring_bitmap_portable_deserialize_size(const char *buf,
1780
+ size_t maxbytes);
1781
+
1782
+ /**
1783
+ * How many bytes are required to serialize this bitmap.
1784
+ *
1785
+ * This is meant to be compatible with the Java and Go versions:
1786
+ * https://github.com/RoaringBitmap/RoaringFormatSpec
1787
+ */
1788
+ size_t roaring_bitmap_portable_size_in_bytes(const roaring_bitmap_t *r);
1789
+
1790
+ /**
1791
+ * Write a bitmap to a char buffer. The output buffer should refer to at least
1792
+ * `roaring_bitmap_portable_size_in_bytes(r)` bytes of allocated memory.
1793
+ *
1794
+ * Returns how many bytes were written which should match
1795
+ * `roaring_bitmap_portable_size_in_bytes(r)`.
1796
+ *
1797
+ * This is meant to be compatible with the Java and Go versions:
1798
+ * https://github.com/RoaringBitmap/RoaringFormatSpec
1799
+ *
1800
+ * This function is endian-sensitive. If you have a big-endian system (e.g., a
1801
+ * mainframe IBM s390x), the data format is going to be big-endian and not
1802
+ * compatible with little-endian systems.
1803
+ */
1804
+ size_t roaring_bitmap_portable_serialize(const roaring_bitmap_t *r, char *buf);
1805
+
1806
+ /*
1807
+ * "Frozen" serialization format imitates memory layout of roaring_bitmap_t.
1808
+ * Deserialized bitmap is a constant view of the underlying buffer.
1809
+ * This significantly reduces amount of allocations and copying required during
1810
+ * deserialization.
1811
+ * It can be used with memory mapped files.
1812
+ * Example can be found in benchmarks/frozen_benchmark.c
1813
+ *
1814
+ * [#####] const roaring_bitmap_t *
1815
+ * | | |
1816
+ * +----+ | +-+
1817
+ * | | |
1818
+ * [#####################################] underlying buffer
1819
+ *
1820
+ * Note that because frozen serialization format imitates C memory layout
1821
+ * of roaring_bitmap_t, it is not fixed. It is different on big/little endian
1822
+ * platforms and can be changed in future.
1823
+ */
1824
+
1825
+ /**
1826
+ * Returns number of bytes required to serialize bitmap using frozen format.
1827
+ */
1828
+ size_t roaring_bitmap_frozen_size_in_bytes(const roaring_bitmap_t *r);
1829
+
1830
+ /**
1831
+ * Serializes bitmap using frozen format.
1832
+ * Buffer size must be at least roaring_bitmap_frozen_size_in_bytes().
1833
+ *
1834
+ * This function is endian-sensitive. If you have a big-endian system (e.g., a
1835
+ * mainframe IBM s390x), the data format is going to be big-endian and not
1836
+ * compatible with little-endian systems.
1837
+ */
1838
+ void roaring_bitmap_frozen_serialize(const roaring_bitmap_t *r, char *buf);
1839
+
1840
+ /**
1841
+ * Creates constant bitmap that is a view of a given buffer.
1842
+ * Buffer data should have been written by `roaring_bitmap_frozen_serialize()`
1843
+ * Its beginning must also be aligned by 32 bytes.
1844
+ * Length must be equal exactly to `roaring_bitmap_frozen_size_in_bytes()`.
1845
+ * In case of failure, NULL is returned.
1846
+ *
1847
+ * Bitmap returned by this function can be used in all readonly contexts.
1848
+ * Bitmap must be freed as usual, by calling roaring_bitmap_free().
1849
+ * Underlying buffer must not be freed or modified while it backs any bitmaps.
1850
+ *
1851
+ * This function is endian-sensitive. If you have a big-endian system (e.g., a
1852
+ * mainframe IBM s390x), the data format is going to be big-endian and not
1853
+ * compatible with little-endian systems.
1854
+ */
1855
+ const roaring_bitmap_t *roaring_bitmap_frozen_view(const char *buf,
1856
+ size_t length);
1857
+
1858
+ /**
1859
+ * Iterate over the bitmap elements. The function iterator is called once for
1860
+ * all the values with ptr (can be NULL) as the second parameter of each call.
1861
+ *
1862
+ * `roaring_iterator` is simply a pointer to a function that returns bool
1863
+ * (true means that the iteration should continue while false means that it
1864
+ * should stop), and takes (uint32_t,void*) as inputs.
1865
+ *
1866
+ * Returns true if the roaring_iterator returned true throughout (so that all
1867
+ * data points were necessarily visited).
1868
+ *
1869
+ * Iteration is ordered: from the smallest to the largest elements.
1870
+ */
1871
+ bool roaring_iterate(const roaring_bitmap_t *r, roaring_iterator iterator,
1872
+ void *ptr);
1873
+
1874
+ bool roaring_iterate64(const roaring_bitmap_t *r, roaring_iterator64 iterator,
1875
+ uint64_t high_bits, void *ptr);
1876
+
1877
+ /**
1878
+ * Return true if the two bitmaps contain the same elements.
1879
+ */
1880
+ bool roaring_bitmap_equals(const roaring_bitmap_t *r1,
1881
+ const roaring_bitmap_t *r2);
1882
+
1883
+ /**
1884
+ * Return true if all the elements of r1 are also in r2.
1885
+ */
1886
+ bool roaring_bitmap_is_subset(const roaring_bitmap_t *r1,
1887
+ const roaring_bitmap_t *r2);
1888
+
1889
+ /**
1890
+ * Return true if all the elements of r1 are also in r2, and r2 is strictly
1891
+ * greater than r1.
1892
+ */
1893
+ bool roaring_bitmap_is_strict_subset(const roaring_bitmap_t *r1,
1894
+ const roaring_bitmap_t *r2);
1895
+
1896
+ /**
1897
+ * (For expert users who seek high performance.)
1898
+ *
1899
+ * Computes the union between two bitmaps and returns new bitmap. The caller is
1900
+ * responsible for memory management.
1901
+ *
1902
+ * The lazy version defers some computations such as the maintenance of the
1903
+ * cardinality counts. Thus you must call `roaring_bitmap_repair_after_lazy()`
1904
+ * after executing "lazy" computations.
1905
+ *
1906
+ * It is safe to repeatedly call roaring_bitmap_lazy_or_inplace on the result.
1907
+ *
1908
+ * `bitsetconversion` is a flag which determines whether container-container
1909
+ * operations force a bitset conversion.
1910
+ */
1911
+ roaring_bitmap_t *roaring_bitmap_lazy_or(const roaring_bitmap_t *r1,
1912
+ const roaring_bitmap_t *r2,
1913
+ const bool bitsetconversion);
1914
+
1915
+ /**
1916
+ * (For expert users who seek high performance.)
1917
+ *
1918
+ * Inplace version of roaring_bitmap_lazy_or, modifies r1.
1919
+ *
1920
+ * `bitsetconversion` is a flag which determines whether container-container
1921
+ * operations force a bitset conversion.
1922
+ */
1923
+ void roaring_bitmap_lazy_or_inplace(roaring_bitmap_t *r1,
1924
+ const roaring_bitmap_t *r2,
1925
+ const bool bitsetconversion);
1926
+
1927
+ /**
1928
+ * (For expert users who seek high performance.)
1929
+ *
1930
+ * Execute maintenance on a bitmap created from `roaring_bitmap_lazy_or()`
1931
+ * or modified with `roaring_bitmap_lazy_or_inplace()`.
1932
+ */
1933
+ void roaring_bitmap_repair_after_lazy(roaring_bitmap_t *r1);
1934
+
1935
+ /**
1936
+ * Computes the symmetric difference between two bitmaps and returns new bitmap.
1937
+ * The caller is responsible for memory management.
1938
+ *
1939
+ * The lazy version defers some computations such as the maintenance of the
1940
+ * cardinality counts. Thus you must call `roaring_bitmap_repair_after_lazy()`
1941
+ * after executing "lazy" computations.
1942
+ *
1943
+ * It is safe to repeatedly call `roaring_bitmap_lazy_xor_inplace()` on
1944
+ * the result.
1945
+ */
1946
+ roaring_bitmap_t *roaring_bitmap_lazy_xor(const roaring_bitmap_t *r1,
1947
+ const roaring_bitmap_t *r2);
1948
+
1949
+ /**
1950
+ * (For expert users who seek high performance.)
1951
+ *
1952
+ * Inplace version of roaring_bitmap_lazy_xor, modifies r1. r1 != r2
1953
+ */
1954
+ void roaring_bitmap_lazy_xor_inplace(roaring_bitmap_t *r1,
1955
+ const roaring_bitmap_t *r2);
1956
+
1957
+ /**
1958
+ * Compute the negation of the bitmap in the interval [range_start, range_end).
1959
+ * The number of negated values is range_end - range_start.
1960
+ * Areas outside the range are passed through unchanged.
1961
+ */
1962
+ roaring_bitmap_t *roaring_bitmap_flip(const roaring_bitmap_t *r1,
1963
+ uint64_t range_start, uint64_t range_end);
1964
+
1965
+ /**
1966
+ * compute (in place) the negation of the roaring bitmap within a specified
1967
+ * interval: [range_start, range_end). The number of negated values is
1968
+ * range_end - range_start.
1969
+ * Areas outside the range are passed through unchanged.
1970
+ */
1971
+ void roaring_bitmap_flip_inplace(roaring_bitmap_t *r1, uint64_t range_start,
1972
+ uint64_t range_end);
1973
+
1974
+ /**
1975
+ * Selects the element at index 'rank' where the smallest element is at index 0.
1976
+ * If the size of the roaring bitmap is strictly greater than rank, then this
1977
+ * function returns true and sets element to the element of given rank.
1978
+ * Otherwise, it returns false.
1979
+ */
1980
+ bool roaring_bitmap_select(const roaring_bitmap_t *r, uint32_t rank,
1981
+ uint32_t *element);
1982
+
1983
+ /**
1984
+ * roaring_bitmap_rank returns the number of integers that are smaller or equal
1985
+ * to x. Thus if x is the first element, this function will return 1. If
1986
+ * x is smaller than the smallest element, this function will return 0.
1987
+ *
1988
+ * The indexing convention differs between roaring_bitmap_select and
1989
+ * roaring_bitmap_rank: roaring_bitmap_select refers to the smallest value
1990
+ * as having index 0, whereas roaring_bitmap_rank returns 1 when ranking
1991
+ * the smallest value.
1992
+ */
1993
+ uint64_t roaring_bitmap_rank(const roaring_bitmap_t *r, uint32_t x);
1994
+
1995
+ /**
1996
+ * roaring_bitmap_rank_many is an `Bulk` version of `roaring_bitmap_rank`
1997
+ * it puts rank value of each element in `[begin .. end)` to `ans[]`
1998
+ *
1999
+ * the values in `[begin .. end)` must be sorted in Ascending order;
2000
+ * Caller is responsible to ensure that there is enough memory allocated, e.g.
2001
+ *
2002
+ * ans = malloc((end-begin) * sizeof(uint64_t));
2003
+ */
2004
+ void roaring_bitmap_rank_many(const roaring_bitmap_t *r, const uint32_t *begin,
2005
+ const uint32_t *end, uint64_t *ans);
2006
+
2007
+ /**
2008
+ * Returns the index of x in the given roaring bitmap.
2009
+ * If the roaring bitmap doesn't contain x , this function will return -1.
2010
+ * The difference with rank function is that this function will return -1 when x
2011
+ * is not the element of roaring bitmap, but the rank function will return a
2012
+ * non-negative number.
2013
+ */
2014
+ int64_t roaring_bitmap_get_index(const roaring_bitmap_t *r, uint32_t x);
2015
+
2016
+ /**
2017
+ * Returns the smallest value in the set, or UINT32_MAX if the set is empty.
2018
+ */
2019
+ uint32_t roaring_bitmap_minimum(const roaring_bitmap_t *r);
2020
+
2021
+ /**
2022
+ * Returns the greatest value in the set, or 0 if the set is empty.
2023
+ */
2024
+ uint32_t roaring_bitmap_maximum(const roaring_bitmap_t *r);
2025
+
2026
+ /**
2027
+ * (For advanced users.)
2028
+ *
2029
+ * Collect statistics about the bitmap, see roaring_types.h for
2030
+ * a description of roaring_statistics_t
2031
+ */
2032
+ void roaring_bitmap_statistics(const roaring_bitmap_t *r,
2033
+ roaring_statistics_t *stat);
2034
+
2035
+ /**
2036
+ * Perform internal consistency checks. Returns true if the bitmap is
2037
+ * consistent. It may be useful to call this after deserializing bitmaps from
2038
+ * untrusted sources. If roaring_bitmap_internal_validate returns true, then the
2039
+ * bitmap should be consistent and can be trusted not to cause crashes or memory
2040
+ * corruption.
2041
+ *
2042
+ * Note that some operations intentionally leave bitmaps in an inconsistent
2043
+ * state temporarily, for example, `roaring_bitmap_lazy_*` functions, until
2044
+ * `roaring_bitmap_repair_after_lazy` is called.
2045
+ *
2046
+ * If reason is non-null, it will be set to a string describing the first
2047
+ * inconsistency found if any.
2048
+ */
2049
+ bool roaring_bitmap_internal_validate(const roaring_bitmap_t *r,
2050
+ const char **reason);
2051
+
2052
+ /*********************
2053
+ * What follows is code use to iterate through values in a roaring bitmap
2054
+
2055
+ roaring_bitmap_t *r =...
2056
+ roaring_uint32_iterator_t i;
2057
+ roaring_iterator_create(r, &i);
2058
+ while(i.has_value) {
2059
+ printf("value = %d\n", i.current_value);
2060
+ roaring_uint32_iterator_advance(&i);
2061
+ }
2062
+
2063
+ Obviously, if you modify the underlying bitmap, the iterator
2064
+ becomes invalid. So don't.
2065
+ */
2066
+
2067
+ /**
2068
+ * A struct used to keep iterator state. Users should only access
2069
+ * `current_value` and `has_value`, the rest of the type should be treated as
2070
+ * opaque.
2071
+ */
2072
+ typedef struct roaring_uint32_iterator_s {
2073
+ const roaring_bitmap_t *parent; // Owner
2074
+ const ROARING_CONTAINER_T *container; // Current container
2075
+ uint8_t typecode; // Typecode of current container
2076
+ int32_t container_index; // Current container index
2077
+ uint32_t highbits; // High 16 bits of the current value
2078
+ roaring_container_iterator_t container_it;
2079
+
2080
+ uint32_t current_value;
2081
+ bool has_value;
2082
+ } roaring_uint32_iterator_t;
2083
+
2084
+ /**
2085
+ * Initialize an iterator object that can be used to iterate through the values.
2086
+ * If there is a value, then this iterator points to the first value and
2087
+ * `it->has_value` is true. The value is in `it->current_value`.
2088
+ */
2089
+ void roaring_iterator_init(const roaring_bitmap_t *r,
2090
+ roaring_uint32_iterator_t *newit);
2091
+
2092
+ /** DEPRECATED, use `roaring_iterator_init`. */
2093
+ CROARING_DEPRECATED static inline void roaring_init_iterator(
2094
+ const roaring_bitmap_t *r, roaring_uint32_iterator_t *newit) {
2095
+ roaring_iterator_init(r, newit);
2096
+ }
2097
+
2098
+ /**
2099
+ * Initialize an iterator object that can be used to iterate through the values.
2100
+ * If there is a value, then this iterator points to the last value and
2101
+ * `it->has_value` is true. The value is in `it->current_value`.
2102
+ */
2103
+ void roaring_iterator_init_last(const roaring_bitmap_t *r,
2104
+ roaring_uint32_iterator_t *newit);
2105
+
2106
+ /** DEPRECATED, use `roaring_iterator_init_last`. */
2107
+ CROARING_DEPRECATED static inline void roaring_init_iterator_last(
2108
+ const roaring_bitmap_t *r, roaring_uint32_iterator_t *newit) {
2109
+ roaring_iterator_init_last(r, newit);
2110
+ }
2111
+
2112
+ /**
2113
+ * Create an iterator object that can be used to iterate through the values.
2114
+ * Caller is responsible for calling `roaring_free_iterator()`.
2115
+ *
2116
+ * The iterator is initialized (this function calls `roaring_iterator_init()`)
2117
+ * If there is a value, then this iterator points to the first value and
2118
+ * `it->has_value` is true. The value is in `it->current_value`.
2119
+ */
2120
+ roaring_uint32_iterator_t *roaring_iterator_create(const roaring_bitmap_t *r);
2121
+
2122
+ /** DEPRECATED, use `roaring_iterator_create`. */
2123
+ CROARING_DEPRECATED static inline roaring_uint32_iterator_t *
2124
+ roaring_create_iterator(const roaring_bitmap_t *r) {
2125
+ return roaring_iterator_create(r);
2126
+ }
2127
+
2128
+ /**
2129
+ * Advance the iterator. If there is a new value, then `it->has_value` is true.
2130
+ * The new value is in `it->current_value`. Values are traversed in increasing
2131
+ * orders. For convenience, returns `it->has_value`.
2132
+ *
2133
+ * Once `it->has_value` is false, `roaring_uint32_iterator_advance` should not
2134
+ * be called on the iterator again. Calling `roaring_uint32_iterator_previous`
2135
+ * is allowed.
2136
+ */
2137
+ bool roaring_uint32_iterator_advance(roaring_uint32_iterator_t *it);
2138
+
2139
+ /** DEPRECATED, use `roaring_uint32_iterator_advance`. */
2140
+ CROARING_DEPRECATED static inline bool roaring_advance_uint32_iterator(
2141
+ roaring_uint32_iterator_t *it) {
2142
+ return roaring_uint32_iterator_advance(it);
2143
+ }
2144
+
2145
+ /**
2146
+ * Decrement the iterator. If there's a new value, then `it->has_value` is true.
2147
+ * The new value is in `it->current_value`. Values are traversed in decreasing
2148
+ * order. For convenience, returns `it->has_value`.
2149
+ *
2150
+ * Once `it->has_value` is false, `roaring_uint32_iterator_previous` should not
2151
+ * be called on the iterator again. Calling `roaring_uint32_iterator_advance` is
2152
+ * allowed.
2153
+ */
2154
+ bool roaring_uint32_iterator_previous(roaring_uint32_iterator_t *it);
2155
+
2156
+ /** DEPRECATED, use `roaring_uint32_iterator_previous`. */
2157
+ CROARING_DEPRECATED static inline bool roaring_previous_uint32_iterator(
2158
+ roaring_uint32_iterator_t *it) {
2159
+ return roaring_uint32_iterator_previous(it);
2160
+ }
2161
+
2162
+ /**
2163
+ * Move the iterator to the first value >= `val`. If there is a such a value,
2164
+ * then `it->has_value` is true. The new value is in `it->current_value`.
2165
+ * For convenience, returns `it->has_value`.
2166
+ */
2167
+ bool roaring_uint32_iterator_move_equalorlarger(roaring_uint32_iterator_t *it,
2168
+ uint32_t val);
2169
+
2170
+ /** DEPRECATED, use `roaring_uint32_iterator_move_equalorlarger`. */
2171
+ CROARING_DEPRECATED static inline bool
2172
+ roaring_move_uint32_iterator_equalorlarger(roaring_uint32_iterator_t *it,
2173
+ uint32_t val) {
2174
+ return roaring_uint32_iterator_move_equalorlarger(it, val);
2175
+ }
2176
+
2177
+ /**
2178
+ * Creates a copy of an iterator.
2179
+ * Caller must free it.
2180
+ */
2181
+ roaring_uint32_iterator_t *roaring_uint32_iterator_copy(
2182
+ const roaring_uint32_iterator_t *it);
2183
+
2184
+ /** DEPRECATED, use `roaring_uint32_iterator_copy`. */
2185
+ CROARING_DEPRECATED static inline roaring_uint32_iterator_t *
2186
+ roaring_copy_uint32_iterator(const roaring_uint32_iterator_t *it) {
2187
+ return roaring_uint32_iterator_copy(it);
2188
+ }
2189
+
2190
+ /**
2191
+ * Free memory following `roaring_iterator_create()`
2192
+ */
2193
+ void roaring_uint32_iterator_free(roaring_uint32_iterator_t *it);
2194
+
2195
+ /** DEPRECATED, use `roaring_uint32_iterator_free`. */
2196
+ CROARING_DEPRECATED static inline void roaring_free_uint32_iterator(
2197
+ roaring_uint32_iterator_t *it) {
2198
+ roaring_uint32_iterator_free(it);
2199
+ }
2200
+
2201
+ /*
2202
+ * Reads next ${count} values from iterator into user-supplied ${buf}.
2203
+ * Returns the number of read elements.
2204
+ * This number can be smaller than ${count}, which means that iterator is
2205
+ * drained.
2206
+ *
2207
+ * This function satisfies semantics of iteration and can be used together with
2208
+ * other iterator functions.
2209
+ * - first value is copied from ${it}->current_value
2210
+ * - after function returns, iterator is positioned at the next element
2211
+ */
2212
+ uint32_t roaring_uint32_iterator_read(roaring_uint32_iterator_t *it,
2213
+ uint32_t *buf, uint32_t count);
2214
+
2215
+ /** DEPRECATED, use `roaring_uint32_iterator_read`. */
2216
+ CROARING_DEPRECATED static inline uint32_t roaring_read_uint32_iterator(
2217
+ roaring_uint32_iterator_t *it, uint32_t *buf, uint32_t count) {
2218
+ return roaring_uint32_iterator_read(it, buf, count);
2219
+ }
2220
+
2221
+ #ifdef __cplusplus
2222
+ }
2223
+ }
2224
+ } // extern "C" { namespace roaring { namespace api {
2225
+ #endif
2226
+
2227
+ #endif /* ROARING_H */
2228
+
2229
+ #ifdef __cplusplus
2230
+ /**
2231
+ * Best practices for C++ headers is to avoid polluting global scope.
2232
+ * But for C compatibility when just `roaring.h` is included building as
2233
+ * C++, default to global access for the C public API.
2234
+ *
2235
+ * BUT when `roaring.hh` is included instead, it sets this flag. That way
2236
+ * explicit namespacing must be used to get the C functions.
2237
+ *
2238
+ * This is outside the include guard so that if you include BOTH headers,
2239
+ * the order won't matter; you still get the global definitions.
2240
+ */
2241
+ #if !defined(ROARING_API_NOT_IN_GLOBAL_NAMESPACE)
2242
+ using namespace ::roaring::api;
2243
+ #endif
2244
+ #endif
2245
+ /* end file include/roaring/roaring.h */
2246
+ /* begin file include/roaring/memory.h */
2247
+ #ifndef INCLUDE_ROARING_MEMORY_H_
2248
+ #define INCLUDE_ROARING_MEMORY_H_
2249
+
2250
+ #ifdef __cplusplus
2251
+ extern "C" {
2252
+ #endif
2253
+
2254
+ #include <stddef.h> // for size_t
2255
+
2256
+ typedef void* (*roaring_malloc_p)(size_t);
2257
+ typedef void* (*roaring_realloc_p)(void*, size_t);
2258
+ typedef void* (*roaring_calloc_p)(size_t, size_t);
2259
+ typedef void (*roaring_free_p)(void*);
2260
+ typedef void* (*roaring_aligned_malloc_p)(size_t, size_t);
2261
+ typedef void (*roaring_aligned_free_p)(void*);
2262
+
2263
+ typedef struct roaring_memory_s {
2264
+ roaring_malloc_p malloc;
2265
+ roaring_realloc_p realloc;
2266
+ roaring_calloc_p calloc;
2267
+ roaring_free_p free;
2268
+ roaring_aligned_malloc_p aligned_malloc;
2269
+ roaring_aligned_free_p aligned_free;
2270
+ } roaring_memory_t;
2271
+
2272
+ void roaring_init_memory_hook(roaring_memory_t memory_hook);
2273
+
2274
+ void* roaring_malloc(size_t);
2275
+ void* roaring_realloc(void*, size_t);
2276
+ void* roaring_calloc(size_t, size_t);
2277
+ void roaring_free(void*);
2278
+ void* roaring_aligned_malloc(size_t, size_t);
2279
+ void roaring_aligned_free(void*);
2280
+
2281
+ #ifdef __cplusplus
2282
+ }
2283
+ #endif
2284
+
2285
+ #endif // INCLUDE_ROARING_MEMORY_H_
2286
+ /* end file include/roaring/memory.h */
2287
+ /* begin file include/roaring/roaring64.h */
2288
+ #ifndef ROARING64_H
2289
+ #define ROARING64_H
2290
+
2291
+ #include <stdbool.h>
2292
+ #include <stddef.h>
2293
+ #include <stdint.h>
2294
+
2295
+
2296
+ #ifdef __cplusplus
2297
+ extern "C" {
2298
+ namespace roaring {
2299
+ namespace api {
2300
+ #endif
2301
+
2302
+ typedef struct roaring64_bitmap_s roaring64_bitmap_t;
2303
+ typedef struct roaring64_leaf_s roaring64_leaf_t;
2304
+ typedef struct roaring64_iterator_s roaring64_iterator_t;
2305
+
2306
+ /**
2307
+ * A bit of context usable with `roaring64_bitmap_*_bulk()` functions.
2308
+ *
2309
+ * Should be initialized with `{0}` (or `memset()` to all zeros).
2310
+ * Callers should treat it as an opaque type.
2311
+ *
2312
+ * A context may only be used with a single bitmap (unless re-initialized to
2313
+ * zero), and any modification to a bitmap (other than modifications performed
2314
+ * with `_bulk()` functions with the context passed) will invalidate any
2315
+ * contexts associated with that bitmap.
2316
+ */
2317
+ typedef struct roaring64_bulk_context_s {
2318
+ uint8_t high_bytes[6];
2319
+ roaring64_leaf_t *leaf;
2320
+ } roaring64_bulk_context_t;
2321
+
2322
+ /**
2323
+ * Dynamically allocates a new bitmap (initially empty).
2324
+ * Client is responsible for calling `roaring64_bitmap_free()`.
2325
+ */
2326
+ roaring64_bitmap_t *roaring64_bitmap_create(void);
2327
+ void roaring64_bitmap_free(roaring64_bitmap_t *r);
2328
+
2329
+ /**
2330
+ * Returns a copy of a bitmap.
2331
+ */
2332
+ roaring64_bitmap_t *roaring64_bitmap_copy(const roaring64_bitmap_t *r);
2333
+
2334
+ /**
2335
+ * Creates a new bitmap of a pointer to N 64-bit integers.
2336
+ */
2337
+ roaring64_bitmap_t *roaring64_bitmap_of_ptr(size_t n_args,
2338
+ const uint64_t *vals);
2339
+
2340
+ #ifdef __cplusplus
2341
+ /**
2342
+ * Creates a new bitmap which contains all values passed in as arguments.
2343
+ *
2344
+ * To create a bitmap from a variable number of arguments, use the
2345
+ * `roaring64_bitmap_of_ptr` function instead.
2346
+ */
2347
+ // Use an immediately invoked closure, capturing by reference
2348
+ // (in case __VA_ARGS__ refers to context outside the closure)
2349
+ // Include a 0 at the beginning of the array to make the array length > 0
2350
+ // (zero sized arrays are not valid in standard c/c++)
2351
+ #define roaring64_bitmap_from(...) \
2352
+ [&]() { \
2353
+ const uint64_t roaring64_bitmap_from_array[] = {0, __VA_ARGS__}; \
2354
+ return roaring64_bitmap_of_ptr( \
2355
+ (sizeof(roaring64_bitmap_from_array) / \
2356
+ sizeof(roaring64_bitmap_from_array[0])) - \
2357
+ 1, \
2358
+ &roaring64_bitmap_from_array[1]); \
2359
+ }()
2360
+ #else
2361
+ /**
2362
+ * Creates a new bitmap which contains all values passed in as arguments.
2363
+ *
2364
+ * To create a bitmap from a variable number of arguments, use the
2365
+ * `roaring64_bitmap_of_ptr` function instead.
2366
+ */
2367
+ // While __VA_ARGS__ occurs twice in expansion, one of the times is in a sizeof
2368
+ // expression, which is an unevaluated context, so it's even safe in the case
2369
+ // where expressions passed have side effects (roaring64_bitmap_from(my_func(),
2370
+ // ++i))
2371
+ // Include a 0 at the beginning of the array to make the array length > 0
2372
+ // (zero sized arrays are not valid in standard c/c++)
2373
+ #define roaring64_bitmap_from(...) \
2374
+ roaring64_bitmap_of_ptr( \
2375
+ (sizeof((const uint64_t[]){0, __VA_ARGS__}) / sizeof(uint64_t)) - 1, \
2376
+ &((const uint64_t[]){0, __VA_ARGS__})[1])
2377
+ #endif
2378
+
2379
+ /**
2380
+ * Create a new bitmap containing all the values in [min, max) that are at a
2381
+ * distance k*step from min.
2382
+ */
2383
+ roaring64_bitmap_t *roaring64_bitmap_from_range(uint64_t min, uint64_t max,
2384
+ uint64_t step);
2385
+
2386
+ /**
2387
+ * Adds the provided value to the bitmap.
2388
+ */
2389
+ void roaring64_bitmap_add(roaring64_bitmap_t *r, uint64_t val);
2390
+
2391
+ /**
2392
+ * Adds the provided value to the bitmap.
2393
+ * Returns true if a new value was added, false if the value already existed.
2394
+ */
2395
+ bool roaring64_bitmap_add_checked(roaring64_bitmap_t *r, uint64_t val);
2396
+
2397
+ /**
2398
+ * Add an item, using context from a previous insert for faster insertion.
2399
+ *
2400
+ * `context` will be used to store information between calls to make bulk
2401
+ * operations faster. `*context` should be zero-initialized before the first
2402
+ * call to this function.
2403
+ *
2404
+ * Modifying the bitmap in any way (other than `-bulk` suffixed functions)
2405
+ * will invalidate the stored context, calling this function with a non-zero
2406
+ * context after doing any modification invokes undefined behavior.
2407
+ *
2408
+ * In order to exploit this optimization, the caller should call this function
2409
+ * with values with the same high 48 bits of the value consecutively.
2410
+ */
2411
+ void roaring64_bitmap_add_bulk(roaring64_bitmap_t *r,
2412
+ roaring64_bulk_context_t *context, uint64_t val);
2413
+
2414
+ /**
2415
+ * Add `n_args` values from `vals`, faster than repeatedly calling
2416
+ * `roaring64_bitmap_add()`
2417
+ *
2418
+ * In order to exploit this optimization, the caller should attempt to keep
2419
+ * values with the same high 48 bits of the value as consecutive elements in
2420
+ * `vals`.
2421
+ */
2422
+ void roaring64_bitmap_add_many(roaring64_bitmap_t *r, size_t n_args,
2423
+ const uint64_t *vals);
2424
+
2425
+ /**
2426
+ * Add all values in range [min, max).
2427
+ */
2428
+ void roaring64_bitmap_add_range(roaring64_bitmap_t *r, uint64_t min,
2429
+ uint64_t max);
2430
+
2431
+ /**
2432
+ * Add all values in range [min, max].
2433
+ */
2434
+ void roaring64_bitmap_add_range_closed(roaring64_bitmap_t *r, uint64_t min,
2435
+ uint64_t max);
2436
+
2437
+ /**
2438
+ * Removes a value from the bitmap if present.
2439
+ */
2440
+ void roaring64_bitmap_remove(roaring64_bitmap_t *r, uint64_t val);
2441
+
2442
+ /**
2443
+ * Removes a value from the bitmap if present, returns true if the value was
2444
+ * removed and false if the value was not present.
2445
+ */
2446
+ bool roaring64_bitmap_remove_checked(roaring64_bitmap_t *r, uint64_t val);
2447
+
2448
+ /**
2449
+ * Remove an item, using context from a previous insert for faster removal.
2450
+ *
2451
+ * `context` will be used to store information between calls to make bulk
2452
+ * operations faster. `*context` should be zero-initialized before the first
2453
+ * call to this function.
2454
+ *
2455
+ * Modifying the bitmap in any way (other than `-bulk` suffixed functions)
2456
+ * will invalidate the stored context, calling this function with a non-zero
2457
+ * context after doing any modification invokes undefined behavior.
2458
+ *
2459
+ * In order to exploit this optimization, the caller should call this function
2460
+ * with values with the same high 48 bits of the value consecutively.
2461
+ */
2462
+ void roaring64_bitmap_remove_bulk(roaring64_bitmap_t *r,
2463
+ roaring64_bulk_context_t *context,
2464
+ uint64_t val);
2465
+
2466
+ /**
2467
+ * Remove `n_args` values from `vals`, faster than repeatedly calling
2468
+ * `roaring64_bitmap_remove()`
2469
+ *
2470
+ * In order to exploit this optimization, the caller should attempt to keep
2471
+ * values with the same high 48 bits of the value as consecutive elements in
2472
+ * `vals`.
2473
+ */
2474
+ void roaring64_bitmap_remove_many(roaring64_bitmap_t *r, size_t n_args,
2475
+ const uint64_t *vals);
2476
+
2477
+ /**
2478
+ * Remove all values in range [min, max).
2479
+ */
2480
+ void roaring64_bitmap_remove_range(roaring64_bitmap_t *r, uint64_t min,
2481
+ uint64_t max);
2482
+
2483
+ /**
2484
+ * Remove all values in range [min, max].
2485
+ */
2486
+ void roaring64_bitmap_remove_range_closed(roaring64_bitmap_t *r, uint64_t min,
2487
+ uint64_t max);
2488
+
2489
+ /**
2490
+ * Returns true if the provided value is present.
2491
+ */
2492
+ bool roaring64_bitmap_contains(const roaring64_bitmap_t *r, uint64_t val);
2493
+
2494
+ /**
2495
+ * Returns true if all values in the range [min, max) are present.
2496
+ */
2497
+ bool roaring64_bitmap_contains_range(const roaring64_bitmap_t *r, uint64_t min,
2498
+ uint64_t max);
2499
+
2500
+ /**
2501
+ * Check if an item is present using context from a previous insert or search
2502
+ * for faster search.
2503
+ *
2504
+ * `context` will be used to store information between calls to make bulk
2505
+ * operations faster. `*context` should be zero-initialized before the first
2506
+ * call to this function.
2507
+ *
2508
+ * Modifying the bitmap in any way (other than `-bulk` suffixed functions)
2509
+ * will invalidate the stored context, calling this function with a non-zero
2510
+ * context after doing any modification invokes undefined behavior.
2511
+ *
2512
+ * In order to exploit this optimization, the caller should call this function
2513
+ * with values with the same high 48 bits of the value consecutively.
2514
+ */
2515
+ bool roaring64_bitmap_contains_bulk(const roaring64_bitmap_t *r,
2516
+ roaring64_bulk_context_t *context,
2517
+ uint64_t val);
2518
+
2519
+ /**
2520
+ * Selects the element at index 'rank' where the smallest element is at index 0.
2521
+ * If the size of the bitmap is strictly greater than rank, then this function
2522
+ * returns true and sets element to the element of given rank. Otherwise, it
2523
+ * returns false.
2524
+ */
2525
+ bool roaring64_bitmap_select(const roaring64_bitmap_t *r, uint64_t rank,
2526
+ uint64_t *element);
2527
+
2528
+ /**
2529
+ * Returns the number of integers that are smaller or equal to x. Thus if x is
2530
+ * the first element, this function will return 1. If x is smaller than the
2531
+ * smallest element, this function will return 0.
2532
+ *
2533
+ * The indexing convention differs between roaring64_bitmap_select and
2534
+ * roaring64_bitmap_rank: roaring_bitmap64_select refers to the smallest value
2535
+ * as having index 0, whereas roaring64_bitmap_rank returns 1 when ranking
2536
+ * the smallest value.
2537
+ */
2538
+ uint64_t roaring64_bitmap_rank(const roaring64_bitmap_t *r, uint64_t val);
2539
+
2540
+ /**
2541
+ * Returns true if the given value is in the bitmap, and sets `out_index` to the
2542
+ * (0-based) index of the value in the bitmap. Returns false if the value is not
2543
+ * in the bitmap.
2544
+ */
2545
+ bool roaring64_bitmap_get_index(const roaring64_bitmap_t *r, uint64_t val,
2546
+ uint64_t *out_index);
2547
+
2548
+ /**
2549
+ * Returns the number of values in the bitmap.
2550
+ */
2551
+ uint64_t roaring64_bitmap_get_cardinality(const roaring64_bitmap_t *r);
2552
+
2553
+ /**
2554
+ * Returns the number of elements in the range [min, max).
2555
+ */
2556
+ uint64_t roaring64_bitmap_range_cardinality(const roaring64_bitmap_t *r,
2557
+ uint64_t min, uint64_t max);
2558
+
2559
+ /**
2560
+ * Returns true if the bitmap is empty (cardinality is zero).
2561
+ */
2562
+ bool roaring64_bitmap_is_empty(const roaring64_bitmap_t *r);
2563
+
2564
+ /**
2565
+ * Returns the smallest value in the set, or UINT64_MAX if the set is empty.
2566
+ */
2567
+ uint64_t roaring64_bitmap_minimum(const roaring64_bitmap_t *r);
2568
+
2569
+ /**
2570
+ * Returns the largest value in the set, or 0 if empty.
2571
+ */
2572
+ uint64_t roaring64_bitmap_maximum(const roaring64_bitmap_t *r);
2573
+
2574
+ /**
2575
+ * Returns true if the result has at least one run container.
2576
+ */
2577
+ bool roaring64_bitmap_run_optimize(roaring64_bitmap_t *r);
2578
+
2579
+ /**
2580
+ * (For advanced users.)
2581
+ * Collect statistics about the bitmap
2582
+ */
2583
+ void roaring64_bitmap_statistics(const roaring64_bitmap_t *r,
2584
+ roaring64_statistics_t *stat);
2585
+
2586
+ /**
2587
+ * Perform internal consistency checks.
2588
+ *
2589
+ * Returns true if the bitmap is consistent. It may be useful to call this
2590
+ * after deserializing bitmaps from untrusted sources. If
2591
+ * roaring64_bitmap_internal_validate returns true, then the bitmap is
2592
+ * consistent and can be trusted not to cause crashes or memory corruption.
2593
+ *
2594
+ * If reason is non-null, it will be set to a string describing the first
2595
+ * inconsistency found if any.
2596
+ */
2597
+ bool roaring64_bitmap_internal_validate(const roaring64_bitmap_t *r,
2598
+ const char **reason);
2599
+
2600
+ /**
2601
+ * Return true if the two bitmaps contain the same elements.
2602
+ */
2603
+ bool roaring64_bitmap_equals(const roaring64_bitmap_t *r1,
2604
+ const roaring64_bitmap_t *r2);
2605
+
2606
+ /**
2607
+ * Return true if all the elements of r1 are also in r2.
2608
+ */
2609
+ bool roaring64_bitmap_is_subset(const roaring64_bitmap_t *r1,
2610
+ const roaring64_bitmap_t *r2);
2611
+
2612
+ /**
2613
+ * Return true if all the elements of r1 are also in r2, and r2 is strictly
2614
+ * greater than r1.
2615
+ */
2616
+ bool roaring64_bitmap_is_strict_subset(const roaring64_bitmap_t *r1,
2617
+ const roaring64_bitmap_t *r2);
2618
+
2619
+ /**
2620
+ * Computes the intersection between two bitmaps and returns new bitmap. The
2621
+ * caller is responsible for free-ing the result.
2622
+ *
2623
+ * Performance hint: if you are computing the intersection between several
2624
+ * bitmaps, two-by-two, it is best to start with the smallest bitmaps. You may
2625
+ * also rely on roaring64_bitmap_and_inplace to avoid creating many temporary
2626
+ * bitmaps.
2627
+ */
2628
+ roaring64_bitmap_t *roaring64_bitmap_and(const roaring64_bitmap_t *r1,
2629
+ const roaring64_bitmap_t *r2);
2630
+
2631
+ /**
2632
+ * Computes the size of the intersection between two bitmaps.
2633
+ */
2634
+ uint64_t roaring64_bitmap_and_cardinality(const roaring64_bitmap_t *r1,
2635
+ const roaring64_bitmap_t *r2);
2636
+
2637
+ /**
2638
+ * In-place version of `roaring64_bitmap_and()`, modifies `r1`. `r1` and `r2`
2639
+ * are allowed to be equal.
2640
+ *
2641
+ * Performance hint: if you are computing the intersection between several
2642
+ * bitmaps, two-by-two, it is best to start with the smallest bitmaps.
2643
+ */
2644
+ void roaring64_bitmap_and_inplace(roaring64_bitmap_t *r1,
2645
+ const roaring64_bitmap_t *r2);
2646
+
2647
+ /**
2648
+ * Check whether two bitmaps intersect.
2649
+ */
2650
+ bool roaring64_bitmap_intersect(const roaring64_bitmap_t *r1,
2651
+ const roaring64_bitmap_t *r2);
2652
+
2653
+ /**
2654
+ * Check whether a bitmap intersects the range [min, max).
2655
+ */
2656
+ bool roaring64_bitmap_intersect_with_range(const roaring64_bitmap_t *r,
2657
+ uint64_t min, uint64_t max);
2658
+
2659
+ /**
2660
+ * Computes the Jaccard index between two bitmaps. (Also known as the Tanimoto
2661
+ * distance, or the Jaccard similarity coefficient)
2662
+ *
2663
+ * The Jaccard index is undefined if both bitmaps are empty.
2664
+ */
2665
+ double roaring64_bitmap_jaccard_index(const roaring64_bitmap_t *r1,
2666
+ const roaring64_bitmap_t *r2);
2667
+
2668
+ /**
2669
+ * Computes the union between two bitmaps and returns new bitmap. The caller is
2670
+ * responsible for free-ing the result.
2671
+ */
2672
+ roaring64_bitmap_t *roaring64_bitmap_or(const roaring64_bitmap_t *r1,
2673
+ const roaring64_bitmap_t *r2);
2674
+
2675
+ /**
2676
+ * Computes the size of the union between two bitmaps.
2677
+ */
2678
+ uint64_t roaring64_bitmap_or_cardinality(const roaring64_bitmap_t *r1,
2679
+ const roaring64_bitmap_t *r2);
2680
+
2681
+ /**
2682
+ * In-place version of `roaring64_bitmap_or(), modifies `r1`.
2683
+ */
2684
+ void roaring64_bitmap_or_inplace(roaring64_bitmap_t *r1,
2685
+ const roaring64_bitmap_t *r2);
2686
+
2687
+ /**
2688
+ * Computes the symmetric difference (xor) between two bitmaps and returns a new
2689
+ * bitmap. The caller is responsible for free-ing the result.
2690
+ */
2691
+ roaring64_bitmap_t *roaring64_bitmap_xor(const roaring64_bitmap_t *r1,
2692
+ const roaring64_bitmap_t *r2);
2693
+
2694
+ /**
2695
+ * Computes the size of the symmetric difference (xor) between two bitmaps.
2696
+ */
2697
+ uint64_t roaring64_bitmap_xor_cardinality(const roaring64_bitmap_t *r1,
2698
+ const roaring64_bitmap_t *r2);
2699
+
2700
+ /**
2701
+ * In-place version of `roaring64_bitmap_xor()`, modifies `r1`. `r1` and `r2`
2702
+ * are not allowed to be equal (that would result in an empty bitmap).
2703
+ */
2704
+ void roaring64_bitmap_xor_inplace(roaring64_bitmap_t *r1,
2705
+ const roaring64_bitmap_t *r2);
2706
+
2707
+ /**
2708
+ * Computes the difference (andnot) between two bitmaps and returns a new
2709
+ * bitmap. The caller is responsible for free-ing the result.
2710
+ */
2711
+ roaring64_bitmap_t *roaring64_bitmap_andnot(const roaring64_bitmap_t *r1,
2712
+ const roaring64_bitmap_t *r2);
2713
+
2714
+ /**
2715
+ * Computes the size of the difference (andnot) between two bitmaps.
2716
+ */
2717
+ uint64_t roaring64_bitmap_andnot_cardinality(const roaring64_bitmap_t *r1,
2718
+ const roaring64_bitmap_t *r2);
2719
+
2720
+ /**
2721
+ * In-place version of `roaring64_bitmap_andnot()`, modifies `r1`. `r1` and `r2`
2722
+ * are not allowed to be equal (that would result in an empty bitmap).
2723
+ */
2724
+ void roaring64_bitmap_andnot_inplace(roaring64_bitmap_t *r1,
2725
+ const roaring64_bitmap_t *r2);
2726
+
2727
+ /**
2728
+ * Compute the negation of the bitmap in the interval [min, max).
2729
+ * The number of negated values is `max - min`. Areas outside the range are
2730
+ * passed through unchanged.
2731
+ */
2732
+ roaring64_bitmap_t *roaring64_bitmap_flip(const roaring64_bitmap_t *r,
2733
+ uint64_t min, uint64_t max);
2734
+
2735
+ /**
2736
+ * Compute the negation of the bitmap in the interval [min, max].
2737
+ * The number of negated values is `max - min + 1`. Areas outside the range are
2738
+ * passed through unchanged.
2739
+ */
2740
+ roaring64_bitmap_t *roaring64_bitmap_flip_closed(const roaring64_bitmap_t *r,
2741
+ uint64_t min, uint64_t max);
2742
+
2743
+ /**
2744
+ * In-place version of `roaring64_bitmap_flip`. Compute the negation of the
2745
+ * bitmap in the interval [min, max). The number of negated values is `max -
2746
+ * min`. Areas outside the range are passed through unchanged.
2747
+ */
2748
+ void roaring64_bitmap_flip_inplace(roaring64_bitmap_t *r, uint64_t min,
2749
+ uint64_t max);
2750
+ /**
2751
+ * In-place version of `roaring64_bitmap_flip_closed`. Compute the negation of
2752
+ * the bitmap in the interval [min, max]. The number of negated values is `max -
2753
+ * min + 1`. Areas outside the range are passed through unchanged.
2754
+ */
2755
+ void roaring64_bitmap_flip_closed_inplace(roaring64_bitmap_t *r, uint64_t min,
2756
+ uint64_t max);
2757
+ /**
2758
+ * How many bytes are required to serialize this bitmap.
2759
+ *
2760
+ * This is meant to be compatible with other languages:
2761
+ * https://github.com/RoaringBitmap/RoaringFormatSpec#extension-for-64-bit-implementations
2762
+ */
2763
+ size_t roaring64_bitmap_portable_size_in_bytes(const roaring64_bitmap_t *r);
2764
+
2765
+ /**
2766
+ * Write a bitmap to a buffer. The output buffer should refer to at least
2767
+ * `roaring64_bitmap_portable_size_in_bytes(r)` bytes of allocated memory.
2768
+ *
2769
+ * Returns how many bytes were written, which should match
2770
+ * `roaring64_bitmap_portable_size_in_bytes(r)`.
2771
+ *
2772
+ * This is meant to be compatible with other languages:
2773
+ * https://github.com/RoaringBitmap/RoaringFormatSpec#extension-for-64-bit-implementations
2774
+ *
2775
+ * This function is endian-sensitive. If you have a big-endian system (e.g., a
2776
+ * mainframe IBM s390x), the data format is going to be big-endian and not
2777
+ * compatible with little-endian systems.
2778
+ */
2779
+ size_t roaring64_bitmap_portable_serialize(const roaring64_bitmap_t *r,
2780
+ char *buf);
2781
+ /**
2782
+ * Check how many bytes would be read (up to maxbytes) at this pointer if there
2783
+ * is a valid bitmap, returns zero if there is no valid bitmap.
2784
+ *
2785
+ * This is meant to be compatible with other languages
2786
+ * https://github.com/RoaringBitmap/RoaringFormatSpec#extension-for-64-bit-implementations
2787
+ */
2788
+ size_t roaring64_bitmap_portable_deserialize_size(const char *buf,
2789
+ size_t maxbytes);
2790
+
2791
+ /**
2792
+ * Read a bitmap from a serialized buffer safely (reading up to maxbytes).
2793
+ * In case of failure, NULL is returned.
2794
+ *
2795
+ * This is meant to be compatible with other languages
2796
+ * https://github.com/RoaringBitmap/RoaringFormatSpec#extension-for-64-bit-implementations
2797
+ *
2798
+ * The function itself is safe in the sense that it will not cause buffer
2799
+ * overflows. However, for correct operations, it is assumed that the bitmap
2800
+ * read was once serialized from a valid bitmap (i.e., it follows the format
2801
+ * specification). If you provided an incorrect input (garbage), then the bitmap
2802
+ * read may not be in a valid state and following operations may not lead to
2803
+ * sensible results. In particular, the serialized array containers need to be
2804
+ * in sorted order, and the run containers should be in sorted non-overlapping
2805
+ * order. This is is guaranteed to happen when serializing an existing bitmap,
2806
+ * but not for random inputs.
2807
+ *
2808
+ * This function is endian-sensitive. If you have a big-endian system (e.g., a
2809
+ * mainframe IBM s390x), the data format is going to be big-endian and not
2810
+ * compatible with little-endian systems.
2811
+ */
2812
+ roaring64_bitmap_t *roaring64_bitmap_portable_deserialize_safe(const char *buf,
2813
+ size_t maxbytes);
2814
+
2815
+ /**
2816
+ * Iterate over the bitmap elements. The function `iterator` is called once for
2817
+ * all the values with `ptr` (can be NULL) as the second parameter of each call.
2818
+ *
2819
+ * `roaring_iterator64` is simply a pointer to a function that returns a bool
2820
+ * and takes `(uint64_t, void*)` as inputs. True means that the iteration should
2821
+ * continue, while false means that it should stop.
2822
+ *
2823
+ * Returns true if the `roaring64_iterator` returned true throughout (so that
2824
+ * all data points were necessarily visited).
2825
+ *
2826
+ * Iteration is ordered from the smallest to the largest elements.
2827
+ */
2828
+ bool roaring64_bitmap_iterate(const roaring64_bitmap_t *r,
2829
+ roaring_iterator64 iterator, void *ptr);
2830
+
2831
+ /**
2832
+ * Convert the bitmap to a sorted array `out`.
2833
+ *
2834
+ * Caller is responsible to ensure that there is enough memory allocated, e.g.
2835
+ * ```
2836
+ * out = malloc(roaring64_bitmap_get_cardinality(bitmap) * sizeof(uint64_t));
2837
+ * ```
2838
+ */
2839
+ void roaring64_bitmap_to_uint64_array(const roaring64_bitmap_t *r,
2840
+ uint64_t *out);
2841
+
2842
+ /**
2843
+ * Create an iterator object that can be used to iterate through the values.
2844
+ * Caller is responsible for calling `roaring64_iterator_free()`.
2845
+ *
2846
+ * The iterator is initialized. If there is a value, then this iterator points
2847
+ * to the first value and `roaring64_iterator_has_value()` returns true. The
2848
+ * value can be retrieved with `roaring64_iterator_value()`.
2849
+ */
2850
+ roaring64_iterator_t *roaring64_iterator_create(const roaring64_bitmap_t *r);
2851
+
2852
+ /**
2853
+ * Create an iterator object that can be used to iterate through the values.
2854
+ * Caller is responsible for calling `roaring64_iterator_free()`.
2855
+ *
2856
+ * The iterator is initialized. If there is a value, then this iterator points
2857
+ * to the last value and `roaring64_iterator_has_value()` returns true. The
2858
+ * value can be retrieved with `roaring64_iterator_value()`.
2859
+ */
2860
+ roaring64_iterator_t *roaring64_iterator_create_last(
2861
+ const roaring64_bitmap_t *r);
2862
+
2863
+ /**
2864
+ * Re-initializes an existing iterator. Functionally the same as
2865
+ * `roaring64_iterator_create` without a allocation.
2866
+ */
2867
+ void roaring64_iterator_reinit(const roaring64_bitmap_t *r,
2868
+ roaring64_iterator_t *it);
2869
+
2870
+ /**
2871
+ * Re-initializes an existing iterator. Functionally the same as
2872
+ * `roaring64_iterator_create_last` without a allocation.
2873
+ */
2874
+ void roaring64_iterator_reinit_last(const roaring64_bitmap_t *r,
2875
+ roaring64_iterator_t *it);
2876
+
2877
+ /**
2878
+ * Creates a copy of the iterator. Caller is responsible for calling
2879
+ * `roaring64_iterator_free()` on the resulting iterator.
2880
+ */
2881
+ roaring64_iterator_t *roaring64_iterator_copy(const roaring64_iterator_t *it);
2882
+
2883
+ /**
2884
+ * Free the iterator.
2885
+ */
2886
+ void roaring64_iterator_free(roaring64_iterator_t *it);
2887
+
2888
+ /**
2889
+ * Returns true if the iterator currently points to a value. If so, calling
2890
+ * `roaring64_iterator_value()` returns the value.
2891
+ */
2892
+ bool roaring64_iterator_has_value(const roaring64_iterator_t *it);
2893
+
2894
+ /**
2895
+ * Returns the value the iterator currently points to. Should only be called if
2896
+ * `roaring64_iterator_has_value()` returns true.
2897
+ */
2898
+ uint64_t roaring64_iterator_value(const roaring64_iterator_t *it);
2899
+
2900
+ /**
2901
+ * Advance the iterator. If there is a new value, then
2902
+ * `roaring64_iterator_has_value()` returns true. Values are traversed in
2903
+ * increasing order. For convenience, returns the result of
2904
+ * `roaring64_iterator_has_value()`.
2905
+ *
2906
+ * Once this returns false, `roaring64_iterator_advance` should not be called on
2907
+ * the iterator again. Calling `roaring64_iterator_previous` is allowed.
2908
+ */
2909
+ bool roaring64_iterator_advance(roaring64_iterator_t *it);
2910
+
2911
+ /**
2912
+ * Decrement the iterator. If there is a new value, then
2913
+ * `roaring64_iterator_has_value()` returns true. Values are traversed in
2914
+ * decreasing order. For convenience, returns the result of
2915
+ * `roaring64_iterator_has_value()`.
2916
+ *
2917
+ * Once this returns false, `roaring64_iterator_previous` should not be called
2918
+ * on the iterator again. Calling `roaring64_iterator_advance` is allowed.
2919
+ */
2920
+ bool roaring64_iterator_previous(roaring64_iterator_t *it);
2921
+
2922
+ /**
2923
+ * Move the iterator to the first value greater than or equal to `val`, if it
2924
+ * exists at or after the current position of the iterator. If there is a new
2925
+ * value, then `roaring64_iterator_has_value()` returns true. Values are
2926
+ * traversed in increasing order. For convenience, returns the result of
2927
+ * `roaring64_iterator_has_value()`.
2928
+ */
2929
+ bool roaring64_iterator_move_equalorlarger(roaring64_iterator_t *it,
2930
+ uint64_t val);
2931
+
2932
+ /**
2933
+ * Reads up to `count` values from the iterator into the given `buf`. Returns
2934
+ * the number of elements read. The number of elements read can be smaller than
2935
+ * `count`, which means that there are no more elements in the bitmap.
2936
+ *
2937
+ * This function can be used together with other iterator functions.
2938
+ */
2939
+ uint64_t roaring64_iterator_read(roaring64_iterator_t *it, uint64_t *buf,
2940
+ uint64_t count);
2941
+
2942
+ #ifdef __cplusplus
2943
+ } // extern "C"
2944
+ } // namespace roaring
2945
+ } // namespace api
2946
+ #endif
2947
+
2948
+ #endif /* ROARING64_H */
2949
+ /* end file include/roaring/roaring64.h */