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