roaring.h 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. /*
  2. * An implementation of Roaring Bitmaps in C.
  3. */
  4. #ifndef ROARING_H
  5. #define ROARING_H
  6. #include <stdbool.h>
  7. #include <stddef.h> // for `size_t`
  8. #include <stdint.h>
  9. #include <roaring/roaring_types.h>
  10. // Include other headers after roaring_types.h
  11. #include <roaring/bitset/bitset.h>
  12. #include <roaring/memory.h>
  13. #include <roaring/portability.h>
  14. #include <roaring/roaring_version.h>
  15. #ifdef __cplusplus
  16. extern "C" {
  17. namespace roaring {
  18. namespace api {
  19. #endif
  20. typedef struct roaring_bitmap_s {
  21. roaring_array_t high_low_container;
  22. } roaring_bitmap_t;
  23. /**
  24. * Dynamically allocates a new bitmap (initially empty).
  25. * Returns NULL if the allocation fails.
  26. * Capacity is a performance hint for how many "containers" the data will need.
  27. * Client is responsible for calling `roaring_bitmap_free()`.
  28. */
  29. roaring_bitmap_t *roaring_bitmap_create_with_capacity(uint32_t cap);
  30. /**
  31. * Dynamically allocates a new bitmap (initially empty).
  32. * Returns NULL if the allocation fails.
  33. * Client is responsible for calling `roaring_bitmap_free()`.
  34. */
  35. inline roaring_bitmap_t *roaring_bitmap_create(void) {
  36. return roaring_bitmap_create_with_capacity(0);
  37. }
  38. /**
  39. * Initialize a roaring bitmap structure in memory controlled by client.
  40. * Capacity is a performance hint for how many "containers" the data will need.
  41. * Can return false if auxiliary allocations fail when capacity greater than 0.
  42. */
  43. bool roaring_bitmap_init_with_capacity(roaring_bitmap_t *r, uint32_t cap);
  44. /**
  45. * Initialize a roaring bitmap structure in memory controlled by client.
  46. * The bitmap will be in a "clear" state, with no auxiliary allocations.
  47. * Since this performs no allocations, the function will not fail.
  48. */
  49. inline void roaring_bitmap_init_cleared(roaring_bitmap_t *r) {
  50. roaring_bitmap_init_with_capacity(r, 0);
  51. }
  52. /**
  53. * Add all the values between min (included) and max (excluded) that are at a
  54. * distance k*step from min.
  55. */
  56. roaring_bitmap_t *roaring_bitmap_from_range(uint64_t min, uint64_t max,
  57. uint32_t step);
  58. /**
  59. * Creates a new bitmap from a pointer of uint32_t integers
  60. */
  61. roaring_bitmap_t *roaring_bitmap_of_ptr(size_t n_args, const uint32_t *vals);
  62. /*
  63. * Whether you want to use copy-on-write.
  64. * Saves memory and avoids copies, but needs more care in a threaded context.
  65. * Most users should ignore this flag.
  66. *
  67. * Note: If you do turn this flag to 'true', enabling COW, then ensure that you
  68. * do so for all of your bitmaps, since interactions between bitmaps with and
  69. * without COW is unsafe.
  70. */
  71. inline bool roaring_bitmap_get_copy_on_write(const roaring_bitmap_t *r) {
  72. return r->high_low_container.flags & ROARING_FLAG_COW;
  73. }
  74. inline void roaring_bitmap_set_copy_on_write(roaring_bitmap_t *r, bool cow) {
  75. if (cow) {
  76. r->high_low_container.flags |= ROARING_FLAG_COW;
  77. } else {
  78. r->high_low_container.flags &= ~ROARING_FLAG_COW;
  79. }
  80. }
  81. roaring_bitmap_t *roaring_bitmap_add_offset(const roaring_bitmap_t *bm,
  82. int64_t offset);
  83. /**
  84. * Describe the inner structure of the bitmap.
  85. */
  86. void roaring_bitmap_printf_describe(const roaring_bitmap_t *r);
  87. /**
  88. * Creates a new bitmap from a list of uint32_t integers
  89. *
  90. * This function is deprecated, use `roaring_bitmap_from` instead, which
  91. * doesn't require the number of elements to be passed in.
  92. *
  93. * @see roaring_bitmap_from
  94. */
  95. CROARING_DEPRECATED roaring_bitmap_t *roaring_bitmap_of(size_t n, ...);
  96. #ifdef __cplusplus
  97. /**
  98. * Creates a new bitmap which contains all values passed in as arguments.
  99. *
  100. * To create a bitmap from a variable number of arguments, use the
  101. * `roaring_bitmap_of_ptr` function instead.
  102. */
  103. // Use an immediately invoked closure, capturing by reference
  104. // (in case __VA_ARGS__ refers to context outside the closure)
  105. // Include a 0 at the beginning of the array to make the array length > 0
  106. // (zero sized arrays are not valid in standard c/c++)
  107. #define roaring_bitmap_from(...) \
  108. [&]() { \
  109. const uint32_t roaring_bitmap_from_array[] = {0, __VA_ARGS__}; \
  110. return roaring_bitmap_of_ptr((sizeof(roaring_bitmap_from_array) / \
  111. sizeof(roaring_bitmap_from_array[0])) - \
  112. 1, \
  113. &roaring_bitmap_from_array[1]); \
  114. }()
  115. #else
  116. /**
  117. * Creates a new bitmap which contains all values passed in as arguments.
  118. *
  119. * To create a bitmap from a variable number of arguments, use the
  120. * `roaring_bitmap_of_ptr` function instead.
  121. */
  122. // While __VA_ARGS__ occurs twice in expansion, one of the times is in a sizeof
  123. // expression, which is an unevaluated context, so it's even safe in the case
  124. // where expressions passed have side effects (roaring64_bitmap_from(my_func(),
  125. // ++i))
  126. // Include a 0 at the beginning of the array to make the array length > 0
  127. // (zero sized arrays are not valid in standard c/c++)
  128. #define roaring_bitmap_from(...) \
  129. roaring_bitmap_of_ptr( \
  130. (sizeof((const uint32_t[]){0, __VA_ARGS__}) / sizeof(uint32_t)) - 1, \
  131. &((const uint32_t[]){0, __VA_ARGS__})[1])
  132. #endif
  133. /**
  134. * Copies a bitmap (this does memory allocation).
  135. * The caller is responsible for memory management.
  136. */
  137. roaring_bitmap_t *roaring_bitmap_copy(const roaring_bitmap_t *r);
  138. /**
  139. * Copies a bitmap from src to dest. It is assumed that the pointer dest
  140. * is to an already allocated bitmap. The content of the dest bitmap is
  141. * freed/deleted.
  142. *
  143. * It might be preferable and simpler to call roaring_bitmap_copy except
  144. * that roaring_bitmap_overwrite can save on memory allocations.
  145. *
  146. * Returns true if successful, or false if there was an error. On failure,
  147. * the dest bitmap is left in a valid, empty state (even if it was not empty
  148. * before).
  149. */
  150. bool roaring_bitmap_overwrite(roaring_bitmap_t *dest,
  151. const roaring_bitmap_t *src);
  152. /**
  153. * Print the content of the bitmap.
  154. */
  155. void roaring_bitmap_printf(const roaring_bitmap_t *r);
  156. /**
  157. * Computes the intersection between two bitmaps and returns new bitmap. The
  158. * caller is responsible for memory management.
  159. *
  160. * Performance hint: if you are computing the intersection between several
  161. * bitmaps, two-by-two, it is best to start with the smallest bitmap.
  162. * You may also rely on roaring_bitmap_and_inplace to avoid creating
  163. * many temporary bitmaps.
  164. */
  165. roaring_bitmap_t *roaring_bitmap_and(const roaring_bitmap_t *r1,
  166. const roaring_bitmap_t *r2);
  167. /**
  168. * Computes the size of the intersection between two bitmaps.
  169. */
  170. uint64_t roaring_bitmap_and_cardinality(const roaring_bitmap_t *r1,
  171. const roaring_bitmap_t *r2);
  172. /**
  173. * Check whether two bitmaps intersect.
  174. */
  175. bool roaring_bitmap_intersect(const roaring_bitmap_t *r1,
  176. const roaring_bitmap_t *r2);
  177. /**
  178. * Check whether a bitmap and an open range intersect.
  179. */
  180. bool roaring_bitmap_intersect_with_range(const roaring_bitmap_t *bm, uint64_t x,
  181. uint64_t y);
  182. /**
  183. * Computes the Jaccard index between two bitmaps. (Also known as the Tanimoto
  184. * distance, or the Jaccard similarity coefficient)
  185. *
  186. * The Jaccard index is undefined if both bitmaps are empty.
  187. */
  188. double roaring_bitmap_jaccard_index(const roaring_bitmap_t *r1,
  189. const roaring_bitmap_t *r2);
  190. /**
  191. * Computes the size of the union between two bitmaps.
  192. */
  193. uint64_t roaring_bitmap_or_cardinality(const roaring_bitmap_t *r1,
  194. const roaring_bitmap_t *r2);
  195. /**
  196. * Computes the size of the difference (andnot) between two bitmaps.
  197. */
  198. uint64_t roaring_bitmap_andnot_cardinality(const roaring_bitmap_t *r1,
  199. const roaring_bitmap_t *r2);
  200. /**
  201. * Computes the size of the symmetric difference (xor) between two bitmaps.
  202. */
  203. uint64_t roaring_bitmap_xor_cardinality(const roaring_bitmap_t *r1,
  204. const roaring_bitmap_t *r2);
  205. /**
  206. * Inplace version of `roaring_bitmap_and()`, modifies r1
  207. * r1 == r2 is allowed.
  208. *
  209. * Performance hint: if you are computing the intersection between several
  210. * bitmaps, two-by-two, it is best to start with the smallest bitmap.
  211. */
  212. void roaring_bitmap_and_inplace(roaring_bitmap_t *r1,
  213. const roaring_bitmap_t *r2);
  214. /**
  215. * Computes the union between two bitmaps and returns new bitmap. The caller is
  216. * responsible for memory management.
  217. */
  218. roaring_bitmap_t *roaring_bitmap_or(const roaring_bitmap_t *r1,
  219. const roaring_bitmap_t *r2);
  220. /**
  221. * Inplace version of `roaring_bitmap_or(), modifies r1.
  222. * TODO: decide whether r1 == r2 ok
  223. */
  224. void roaring_bitmap_or_inplace(roaring_bitmap_t *r1,
  225. const roaring_bitmap_t *r2);
  226. /**
  227. * Compute the union of 'number' bitmaps.
  228. * Caller is responsible for freeing the result.
  229. * See also `roaring_bitmap_or_many_heap()`
  230. */
  231. roaring_bitmap_t *roaring_bitmap_or_many(size_t number,
  232. const roaring_bitmap_t **rs);
  233. /**
  234. * Compute the union of 'number' bitmaps using a heap. This can sometimes be
  235. * faster than `roaring_bitmap_or_many() which uses a naive algorithm.
  236. * Caller is responsible for freeing the result.
  237. */
  238. roaring_bitmap_t *roaring_bitmap_or_many_heap(uint32_t number,
  239. const roaring_bitmap_t **rs);
  240. /**
  241. * Computes the symmetric difference (xor) between two bitmaps
  242. * and returns new bitmap. The caller is responsible for memory management.
  243. */
  244. roaring_bitmap_t *roaring_bitmap_xor(const roaring_bitmap_t *r1,
  245. const roaring_bitmap_t *r2);
  246. /**
  247. * Inplace version of roaring_bitmap_xor, modifies r1, r1 != r2.
  248. */
  249. void roaring_bitmap_xor_inplace(roaring_bitmap_t *r1,
  250. const roaring_bitmap_t *r2);
  251. /**
  252. * Compute the xor of 'number' bitmaps.
  253. * Caller is responsible for freeing the result.
  254. */
  255. roaring_bitmap_t *roaring_bitmap_xor_many(size_t number,
  256. const roaring_bitmap_t **rs);
  257. /**
  258. * Computes the difference (andnot) between two bitmaps and returns new bitmap.
  259. * Caller is responsible for freeing the result.
  260. */
  261. roaring_bitmap_t *roaring_bitmap_andnot(const roaring_bitmap_t *r1,
  262. const roaring_bitmap_t *r2);
  263. /**
  264. * Inplace version of roaring_bitmap_andnot, modifies r1, r1 != r2.
  265. */
  266. void roaring_bitmap_andnot_inplace(roaring_bitmap_t *r1,
  267. const roaring_bitmap_t *r2);
  268. /**
  269. * TODO: consider implementing:
  270. *
  271. * "Compute the xor of 'number' bitmaps using a heap. This can sometimes be
  272. * faster than roaring_bitmap_xor_many which uses a naive algorithm. Caller is
  273. * responsible for freeing the result.""
  274. *
  275. * roaring_bitmap_t *roaring_bitmap_xor_many_heap(uint32_t number,
  276. * const roaring_bitmap_t **rs);
  277. */
  278. /**
  279. * Frees the memory.
  280. */
  281. void roaring_bitmap_free(const roaring_bitmap_t *r);
  282. /**
  283. * A bit of context usable with `roaring_bitmap_*_bulk()` functions
  284. *
  285. * Should be initialized with `{0}` (or `memset()` to all zeros).
  286. * Callers should treat it as an opaque type.
  287. *
  288. * A context may only be used with a single bitmap
  289. * (unless re-initialized to zero), and any modification to a bitmap
  290. * (other than modifications performed with `_bulk()` functions with the context
  291. * passed) will invalidate any contexts associated with that bitmap.
  292. */
  293. typedef struct roaring_bulk_context_s {
  294. ROARING_CONTAINER_T *container;
  295. int idx;
  296. uint16_t key;
  297. uint8_t typecode;
  298. } roaring_bulk_context_t;
  299. /**
  300. * Add an item, using context from a previous insert for speed optimization.
  301. *
  302. * `context` will be used to store information between calls to make bulk
  303. * operations faster. `*context` should be zero-initialized before the first
  304. * call to this function.
  305. *
  306. * Modifying the bitmap in any way (other than `-bulk` suffixed functions)
  307. * will invalidate the stored context, calling this function with a non-zero
  308. * context after doing any modification invokes undefined behavior.
  309. *
  310. * In order to exploit this optimization, the caller should call this function
  311. * with values with the same "key" (high 16 bits of the value) consecutively.
  312. */
  313. void roaring_bitmap_add_bulk(roaring_bitmap_t *r,
  314. roaring_bulk_context_t *context, uint32_t val);
  315. /**
  316. * Add value n_args from pointer vals, faster than repeatedly calling
  317. * `roaring_bitmap_add()`
  318. *
  319. * In order to exploit this optimization, the caller should attempt to keep
  320. * values with the same "key" (high 16 bits of the value) as consecutive
  321. * elements in `vals`
  322. */
  323. void roaring_bitmap_add_many(roaring_bitmap_t *r, size_t n_args,
  324. const uint32_t *vals);
  325. /**
  326. * Add value x
  327. */
  328. void roaring_bitmap_add(roaring_bitmap_t *r, uint32_t x);
  329. /**
  330. * Add value x
  331. * Returns true if a new value was added, false if the value already existed.
  332. */
  333. bool roaring_bitmap_add_checked(roaring_bitmap_t *r, uint32_t x);
  334. /**
  335. * Add all values in range [min, max]
  336. */
  337. void roaring_bitmap_add_range_closed(roaring_bitmap_t *r, uint32_t min,
  338. uint32_t max);
  339. /**
  340. * Add all values in range [min, max)
  341. */
  342. inline void roaring_bitmap_add_range(roaring_bitmap_t *r, uint64_t min,
  343. uint64_t max) {
  344. if (max <= min || min > (uint64_t)UINT32_MAX + 1) {
  345. return;
  346. }
  347. roaring_bitmap_add_range_closed(r, (uint32_t)min, (uint32_t)(max - 1));
  348. }
  349. /**
  350. * Remove value x
  351. */
  352. void roaring_bitmap_remove(roaring_bitmap_t *r, uint32_t x);
  353. /**
  354. * Remove all values in range [min, max]
  355. */
  356. void roaring_bitmap_remove_range_closed(roaring_bitmap_t *r, uint32_t min,
  357. uint32_t max);
  358. /**
  359. * Remove all values in range [min, max)
  360. */
  361. inline void roaring_bitmap_remove_range(roaring_bitmap_t *r, uint64_t min,
  362. uint64_t max) {
  363. if (max <= min || min > (uint64_t)UINT32_MAX + 1) {
  364. return;
  365. }
  366. roaring_bitmap_remove_range_closed(r, (uint32_t)min, (uint32_t)(max - 1));
  367. }
  368. /**
  369. * Remove multiple values
  370. */
  371. void roaring_bitmap_remove_many(roaring_bitmap_t *r, size_t n_args,
  372. const uint32_t *vals);
  373. /**
  374. * Remove value x
  375. * Returns true if a new value was removed, false if the value was not existing.
  376. */
  377. bool roaring_bitmap_remove_checked(roaring_bitmap_t *r, uint32_t x);
  378. /**
  379. * Check if value is present
  380. */
  381. bool roaring_bitmap_contains(const roaring_bitmap_t *r, uint32_t val);
  382. /**
  383. * Check whether a range of values from range_start (included)
  384. * to range_end (excluded) is present
  385. */
  386. bool roaring_bitmap_contains_range(const roaring_bitmap_t *r,
  387. uint64_t range_start, uint64_t range_end);
  388. /**
  389. * Check whether a range of values from range_start (included)
  390. * to range_end (included) is present
  391. */
  392. bool roaring_bitmap_contains_range_closed(const roaring_bitmap_t *r,
  393. uint32_t range_start,
  394. uint32_t range_end);
  395. /**
  396. * Check if an items is present, using context from a previous insert or search
  397. * for speed optimization.
  398. *
  399. * `context` will be used to store information between calls to make bulk
  400. * operations faster. `*context` should be zero-initialized before the first
  401. * call to this function.
  402. *
  403. * Modifying the bitmap in any way (other than `-bulk` suffixed functions)
  404. * will invalidate the stored context, calling this function with a non-zero
  405. * context after doing any modification invokes undefined behavior.
  406. *
  407. * In order to exploit this optimization, the caller should call this function
  408. * with values with the same "key" (high 16 bits of the value) consecutively.
  409. */
  410. bool roaring_bitmap_contains_bulk(const roaring_bitmap_t *r,
  411. roaring_bulk_context_t *context,
  412. uint32_t val);
  413. /**
  414. * Get the cardinality of the bitmap (number of elements).
  415. */
  416. uint64_t roaring_bitmap_get_cardinality(const roaring_bitmap_t *r);
  417. /**
  418. * Returns the number of elements in the range [range_start, range_end).
  419. */
  420. uint64_t roaring_bitmap_range_cardinality(const roaring_bitmap_t *r,
  421. uint64_t range_start,
  422. uint64_t range_end);
  423. /**
  424. * Returns the number of elements in the range [range_start, range_end].
  425. */
  426. uint64_t roaring_bitmap_range_cardinality_closed(const roaring_bitmap_t *r,
  427. uint32_t range_start,
  428. uint32_t range_end);
  429. /**
  430. * Returns true if the bitmap is empty (cardinality is zero).
  431. */
  432. bool roaring_bitmap_is_empty(const roaring_bitmap_t *r);
  433. /**
  434. * Empties the bitmap. It will have no auxiliary allocations (so if the bitmap
  435. * was initialized in client memory via roaring_bitmap_init(), then a call to
  436. * roaring_bitmap_clear() would be enough to "free" it)
  437. */
  438. void roaring_bitmap_clear(roaring_bitmap_t *r);
  439. /**
  440. * Convert the bitmap to a sorted array, output in `ans`.
  441. *
  442. * Caller is responsible to ensure that there is enough memory allocated, e.g.
  443. *
  444. * ans = malloc(roaring_bitmap_get_cardinality(bitmap) * sizeof(uint32_t));
  445. */
  446. void roaring_bitmap_to_uint32_array(const roaring_bitmap_t *r, uint32_t *ans);
  447. /**
  448. * Store the bitmap to a bitset. This can be useful for people
  449. * who need the performance and simplicity of a standard bitset.
  450. * We assume that the input bitset is originally empty (does not
  451. * have any set bit).
  452. *
  453. * bitset_t * out = bitset_create();
  454. * // if the bitset has content in it, call "bitset_clear(out)"
  455. * bool success = roaring_bitmap_to_bitset(mybitmap, out);
  456. * // on failure, success will be false.
  457. * // You can then query the bitset:
  458. * bool is_present = bitset_get(out, 10011 );
  459. * // you must free the memory:
  460. * bitset_free(out);
  461. *
  462. */
  463. bool roaring_bitmap_to_bitset(const roaring_bitmap_t *r, bitset_t *bitset);
  464. /**
  465. * Convert the bitmap to a sorted array from `offset` by `limit`, output in
  466. * `ans`.
  467. *
  468. * Caller is responsible to ensure that there is enough memory allocated, e.g.
  469. *
  470. * ans = malloc(roaring_bitmap_get_cardinality(limit) * sizeof(uint32_t));
  471. *
  472. * Return false in case of failure (e.g., insufficient memory)
  473. */
  474. bool roaring_bitmap_range_uint32_array(const roaring_bitmap_t *r, size_t offset,
  475. size_t limit, uint32_t *ans);
  476. /**
  477. * Remove run-length encoding even when it is more space efficient.
  478. * Return whether a change was applied.
  479. */
  480. bool roaring_bitmap_remove_run_compression(roaring_bitmap_t *r);
  481. /**
  482. * Convert array and bitmap containers to run containers when it is more
  483. * efficient; also convert from run containers when more space efficient.
  484. *
  485. * Returns true if the result has at least one run container.
  486. * Additional savings might be possible by calling `shrinkToFit()`.
  487. */
  488. bool roaring_bitmap_run_optimize(roaring_bitmap_t *r);
  489. /**
  490. * If needed, reallocate memory to shrink the memory usage.
  491. * Returns the number of bytes saved.
  492. */
  493. size_t roaring_bitmap_shrink_to_fit(roaring_bitmap_t *r);
  494. /**
  495. * Write the bitmap to an output pointer, this output buffer should refer to
  496. * at least `roaring_bitmap_size_in_bytes(r)` allocated bytes.
  497. *
  498. * See `roaring_bitmap_portable_serialize()` if you want a format that's
  499. * compatible with Java and Go implementations. This format can sometimes be
  500. * more space efficient than the portable form, e.g. when the data is sparse.
  501. *
  502. * Returns how many bytes written, should be `roaring_bitmap_size_in_bytes(r)`.
  503. *
  504. * This function is endian-sensitive. If you have a big-endian system (e.g., a
  505. * mainframe IBM s390x), the data format is going to be big-endian and not
  506. * compatible with little-endian systems.
  507. *
  508. * When serializing data to a file, we recommend that you also use
  509. * checksums so that, at deserialization, you can be confident
  510. * that you are recovering the correct data.
  511. */
  512. size_t roaring_bitmap_serialize(const roaring_bitmap_t *r, char *buf);
  513. /**
  514. * Use with `roaring_bitmap_serialize()`.
  515. *
  516. * (See `roaring_bitmap_portable_deserialize()` if you want a format that's
  517. * compatible with Java and Go implementations).
  518. *
  519. * This function is endian-sensitive. If you have a big-endian system (e.g., a
  520. * mainframe IBM s390x), the data format is going to be big-endian and not
  521. * compatible with little-endian systems.
  522. */
  523. roaring_bitmap_t *roaring_bitmap_deserialize(const void *buf);
  524. /**
  525. * Use with `roaring_bitmap_serialize()`.
  526. *
  527. * (See `roaring_bitmap_portable_deserialize_safe()` if you want a format that's
  528. * compatible with Java and Go implementations).
  529. *
  530. * This function is endian-sensitive. If you have a big-endian system (e.g., a
  531. * mainframe IBM s390x), the data format is going to be big-endian and not
  532. * compatible with little-endian systems.
  533. *
  534. * The difference with `roaring_bitmap_deserialize()` is that this function
  535. * checks that the input buffer is a valid bitmap. If the buffer is too small,
  536. * NULL is returned.
  537. */
  538. roaring_bitmap_t *roaring_bitmap_deserialize_safe(const void *buf,
  539. size_t maxbytes);
  540. /**
  541. * How many bytes are required to serialize this bitmap (NOT compatible
  542. * with Java and Go versions)
  543. */
  544. size_t roaring_bitmap_size_in_bytes(const roaring_bitmap_t *r);
  545. /**
  546. * Read bitmap from a serialized buffer.
  547. * In case of failure, NULL is returned.
  548. *
  549. * This function is unsafe in the sense that if there is no valid serialized
  550. * bitmap at the pointer, then many bytes could be read, possibly causing a
  551. * buffer overflow. See also roaring_bitmap_portable_deserialize_safe().
  552. *
  553. * This is meant to be compatible with the Java and Go versions:
  554. * https://github.com/RoaringBitmap/RoaringFormatSpec
  555. *
  556. * This function is endian-sensitive. If you have a big-endian system (e.g., a
  557. * mainframe IBM s390x), the data format is going to be big-endian and not
  558. * compatible with little-endian systems.
  559. */
  560. roaring_bitmap_t *roaring_bitmap_portable_deserialize(const char *buf);
  561. /**
  562. * Read bitmap from a serialized buffer safely (reading up to maxbytes).
  563. * In case of failure, NULL is returned.
  564. *
  565. * This is meant to be compatible with the Java and Go versions:
  566. * https://github.com/RoaringBitmap/RoaringFormatSpec
  567. *
  568. * The function itself is safe in the sense that it will not cause buffer
  569. * overflows: it will not read beyond the scope of the provided buffer
  570. * (buf,maxbytes).
  571. *
  572. * However, for correct operations, it is assumed that the bitmap
  573. * read was once serialized from a valid bitmap (i.e., it follows the format
  574. * specification). If you provided an incorrect input (garbage), then the bitmap
  575. * read may not be in a valid state and following operations may not lead to
  576. * sensible results. In particular, the serialized array containers need to be
  577. * in sorted order, and the run containers should be in sorted non-overlapping
  578. * order. This is is guaranteed to happen when serializing an existing bitmap,
  579. * but not for random inputs.
  580. *
  581. * You may use roaring_bitmap_internal_validate to check the validity of the
  582. * bitmap prior to using it.
  583. *
  584. * We recommend that you use checksums to check that serialized data corresponds
  585. * to a serialized bitmap.
  586. *
  587. * This function is endian-sensitive. If you have a big-endian system (e.g., a
  588. * mainframe IBM s390x), the data format is going to be big-endian and not
  589. * compatible with little-endian systems.
  590. */
  591. roaring_bitmap_t *roaring_bitmap_portable_deserialize_safe(const char *buf,
  592. size_t maxbytes);
  593. /**
  594. * Read bitmap from a serialized buffer.
  595. * In case of failure, NULL is returned.
  596. *
  597. * Bitmap returned by this function can be used in all readonly contexts.
  598. * Bitmap must be freed as usual, by calling roaring_bitmap_free().
  599. * Underlying buffer must not be freed or modified while it backs any bitmaps.
  600. *
  601. * The function is unsafe in the following ways:
  602. * 1) It may execute unaligned memory accesses.
  603. * 2) A buffer overflow may occur if buf does not point to a valid serialized
  604. * bitmap.
  605. *
  606. * This is meant to be compatible with the Java and Go versions:
  607. * https://github.com/RoaringBitmap/RoaringFormatSpec
  608. *
  609. * This function is endian-sensitive. If you have a big-endian system (e.g., a
  610. * mainframe IBM s390x), the data format is going to be big-endian and not
  611. * compatible with little-endian systems.
  612. */
  613. roaring_bitmap_t *roaring_bitmap_portable_deserialize_frozen(const char *buf);
  614. /**
  615. * Check how many bytes would be read (up to maxbytes) at this pointer if there
  616. * is a bitmap, returns zero if there is no valid bitmap.
  617. *
  618. * This is meant to be compatible with the Java and Go versions:
  619. * https://github.com/RoaringBitmap/RoaringFormatSpec
  620. */
  621. size_t roaring_bitmap_portable_deserialize_size(const char *buf,
  622. size_t maxbytes);
  623. /**
  624. * How many bytes are required to serialize this bitmap.
  625. *
  626. * This is meant to be compatible with the Java and Go versions:
  627. * https://github.com/RoaringBitmap/RoaringFormatSpec
  628. */
  629. size_t roaring_bitmap_portable_size_in_bytes(const roaring_bitmap_t *r);
  630. /**
  631. * Write a bitmap to a char buffer. The output buffer should refer to at least
  632. * `roaring_bitmap_portable_size_in_bytes(r)` bytes of allocated memory.
  633. *
  634. * Returns how many bytes were written which should match
  635. * `roaring_bitmap_portable_size_in_bytes(r)`.
  636. *
  637. * This is meant to be compatible with the Java and Go versions:
  638. * https://github.com/RoaringBitmap/RoaringFormatSpec
  639. *
  640. * This function is endian-sensitive. If you have a big-endian system (e.g., a
  641. * mainframe IBM s390x), the data format is going to be big-endian and not
  642. * compatible with little-endian systems.
  643. *
  644. * When serializing data to a file, we recommend that you also use
  645. * checksums so that, at deserialization, you can be confident
  646. * that you are recovering the correct data.
  647. */
  648. size_t roaring_bitmap_portable_serialize(const roaring_bitmap_t *r, char *buf);
  649. /*
  650. * "Frozen" serialization format imitates memory layout of roaring_bitmap_t.
  651. * Deserialized bitmap is a constant view of the underlying buffer.
  652. * This significantly reduces amount of allocations and copying required during
  653. * deserialization.
  654. * It can be used with memory mapped files.
  655. * Example can be found in benchmarks/frozen_benchmark.c
  656. *
  657. * [#####] const roaring_bitmap_t *
  658. * | | |
  659. * +----+ | +-+
  660. * | | |
  661. * [#####################################] underlying buffer
  662. *
  663. * Note that because frozen serialization format imitates C memory layout
  664. * of roaring_bitmap_t, it is not fixed. It is different on big/little endian
  665. * platforms and can be changed in future.
  666. */
  667. /**
  668. * Returns number of bytes required to serialize bitmap using frozen format.
  669. */
  670. size_t roaring_bitmap_frozen_size_in_bytes(const roaring_bitmap_t *r);
  671. /**
  672. * Serializes bitmap using frozen format.
  673. * Buffer size must be at least roaring_bitmap_frozen_size_in_bytes().
  674. *
  675. * This function is endian-sensitive. If you have a big-endian system (e.g., a
  676. * mainframe IBM s390x), the data format is going to be big-endian and not
  677. * compatible with little-endian systems.
  678. *
  679. * When serializing data to a file, we recommend that you also use
  680. * checksums so that, at deserialization, you can be confident
  681. * that you are recovering the correct data.
  682. */
  683. void roaring_bitmap_frozen_serialize(const roaring_bitmap_t *r, char *buf);
  684. /**
  685. * Creates constant bitmap that is a view of a given buffer.
  686. * Buffer data should have been written by `roaring_bitmap_frozen_serialize()`
  687. * Its beginning must also be aligned by 32 bytes.
  688. * Length must be equal exactly to `roaring_bitmap_frozen_size_in_bytes()`.
  689. * In case of failure, NULL is returned.
  690. *
  691. * Bitmap returned by this function can be used in all readonly contexts.
  692. * Bitmap must be freed as usual, by calling roaring_bitmap_free().
  693. * Underlying buffer must not be freed or modified while it backs any bitmaps.
  694. *
  695. * This function is endian-sensitive. If you have a big-endian system (e.g., a
  696. * mainframe IBM s390x), the data format is going to be big-endian and not
  697. * compatible with little-endian systems.
  698. */
  699. const roaring_bitmap_t *roaring_bitmap_frozen_view(const char *buf,
  700. size_t length);
  701. /**
  702. * Iterate over the bitmap elements. The function iterator is called once for
  703. * all the values with ptr (can be NULL) as the second parameter of each call.
  704. *
  705. * `roaring_iterator` is simply a pointer to a function that returns bool
  706. * (true means that the iteration should continue while false means that it
  707. * should stop), and takes (uint32_t,void*) as inputs.
  708. *
  709. * Returns true if the roaring_iterator returned true throughout (so that all
  710. * data points were necessarily visited).
  711. *
  712. * Iteration is ordered: from the smallest to the largest elements.
  713. */
  714. bool roaring_iterate(const roaring_bitmap_t *r, roaring_iterator iterator,
  715. void *ptr);
  716. bool roaring_iterate64(const roaring_bitmap_t *r, roaring_iterator64 iterator,
  717. uint64_t high_bits, void *ptr);
  718. /**
  719. * Return true if the two bitmaps contain the same elements.
  720. */
  721. bool roaring_bitmap_equals(const roaring_bitmap_t *r1,
  722. const roaring_bitmap_t *r2);
  723. /**
  724. * Return true if all the elements of r1 are also in r2.
  725. */
  726. bool roaring_bitmap_is_subset(const roaring_bitmap_t *r1,
  727. const roaring_bitmap_t *r2);
  728. /**
  729. * Return true if all the elements of r1 are also in r2, and r2 is strictly
  730. * greater than r1.
  731. */
  732. bool roaring_bitmap_is_strict_subset(const roaring_bitmap_t *r1,
  733. const roaring_bitmap_t *r2);
  734. /**
  735. * (For expert users who seek high performance.)
  736. *
  737. * Computes the union between two bitmaps and returns new bitmap. The caller is
  738. * responsible for memory management.
  739. *
  740. * The lazy version defers some computations such as the maintenance of the
  741. * cardinality counts. Thus you must call `roaring_bitmap_repair_after_lazy()`
  742. * after executing "lazy" computations.
  743. *
  744. * It is safe to repeatedly call roaring_bitmap_lazy_or_inplace on the result.
  745. *
  746. * `bitsetconversion` is a flag which determines whether container-container
  747. * operations force a bitset conversion.
  748. */
  749. roaring_bitmap_t *roaring_bitmap_lazy_or(const roaring_bitmap_t *r1,
  750. const roaring_bitmap_t *r2,
  751. const bool bitsetconversion);
  752. /**
  753. * (For expert users who seek high performance.)
  754. *
  755. * Inplace version of roaring_bitmap_lazy_or, modifies r1.
  756. *
  757. * `bitsetconversion` is a flag which determines whether container-container
  758. * operations force a bitset conversion.
  759. */
  760. void roaring_bitmap_lazy_or_inplace(roaring_bitmap_t *r1,
  761. const roaring_bitmap_t *r2,
  762. const bool bitsetconversion);
  763. /**
  764. * (For expert users who seek high performance.)
  765. *
  766. * Execute maintenance on a bitmap created from `roaring_bitmap_lazy_or()`
  767. * or modified with `roaring_bitmap_lazy_or_inplace()`.
  768. */
  769. void roaring_bitmap_repair_after_lazy(roaring_bitmap_t *r1);
  770. /**
  771. * Computes the symmetric difference between two bitmaps and returns new bitmap.
  772. * The caller is responsible for memory management.
  773. *
  774. * The lazy version defers some computations such as the maintenance of the
  775. * cardinality counts. Thus you must call `roaring_bitmap_repair_after_lazy()`
  776. * after executing "lazy" computations.
  777. *
  778. * It is safe to repeatedly call `roaring_bitmap_lazy_xor_inplace()` on
  779. * the result.
  780. */
  781. roaring_bitmap_t *roaring_bitmap_lazy_xor(const roaring_bitmap_t *r1,
  782. const roaring_bitmap_t *r2);
  783. /**
  784. * (For expert users who seek high performance.)
  785. *
  786. * Inplace version of roaring_bitmap_lazy_xor, modifies r1. r1 != r2
  787. */
  788. void roaring_bitmap_lazy_xor_inplace(roaring_bitmap_t *r1,
  789. const roaring_bitmap_t *r2);
  790. /**
  791. * Compute the negation of the bitmap in the interval [range_start, range_end).
  792. * The number of negated values is range_end - range_start.
  793. * Areas outside the range are passed through unchanged.
  794. */
  795. roaring_bitmap_t *roaring_bitmap_flip(const roaring_bitmap_t *r1,
  796. uint64_t range_start, uint64_t range_end);
  797. /**
  798. * Compute the negation of the bitmap in the interval [range_start, range_end].
  799. * The number of negated values is range_end - range_start + 1.
  800. * Areas outside the range are passed through unchanged.
  801. */
  802. roaring_bitmap_t *roaring_bitmap_flip_closed(const roaring_bitmap_t *x1,
  803. uint32_t range_start,
  804. uint32_t range_end);
  805. /**
  806. * compute (in place) the negation of the roaring bitmap within a specified
  807. * interval: [range_start, range_end). The number of negated values is
  808. * range_end - range_start.
  809. * Areas outside the range are passed through unchanged.
  810. */
  811. void roaring_bitmap_flip_inplace(roaring_bitmap_t *r1, uint64_t range_start,
  812. uint64_t range_end);
  813. /**
  814. * compute (in place) the negation of the roaring bitmap within a specified
  815. * interval: [range_start, range_end]. The number of negated values is
  816. * range_end - range_start + 1.
  817. * Areas outside the range are passed through unchanged.
  818. */
  819. void roaring_bitmap_flip_inplace_closed(roaring_bitmap_t *r1,
  820. uint32_t range_start,
  821. uint32_t range_end);
  822. /**
  823. * Selects the element at index 'rank' where the smallest element is at index 0.
  824. * If the size of the roaring bitmap is strictly greater than rank, then this
  825. * function returns true and sets element to the element of given rank.
  826. * Otherwise, it returns false.
  827. */
  828. bool roaring_bitmap_select(const roaring_bitmap_t *r, uint32_t rank,
  829. uint32_t *element);
  830. /**
  831. * roaring_bitmap_rank returns the number of integers that are smaller or equal
  832. * to x. Thus if x is the first element, this function will return 1. If
  833. * x is smaller than the smallest element, this function will return 0.
  834. *
  835. * The indexing convention differs between roaring_bitmap_select and
  836. * roaring_bitmap_rank: roaring_bitmap_select refers to the smallest value
  837. * as having index 0, whereas roaring_bitmap_rank returns 1 when ranking
  838. * the smallest value.
  839. */
  840. uint64_t roaring_bitmap_rank(const roaring_bitmap_t *r, uint32_t x);
  841. /**
  842. * roaring_bitmap_rank_many is an `Bulk` version of `roaring_bitmap_rank`
  843. * it puts rank value of each element in `[begin .. end)` to `ans[]`
  844. *
  845. * the values in `[begin .. end)` must be sorted in Ascending order;
  846. * Caller is responsible to ensure that there is enough memory allocated, e.g.
  847. *
  848. * ans = malloc((end-begin) * sizeof(uint64_t));
  849. */
  850. void roaring_bitmap_rank_many(const roaring_bitmap_t *r, const uint32_t *begin,
  851. const uint32_t *end, uint64_t *ans);
  852. /**
  853. * Returns the index of x in the given roaring bitmap.
  854. * If the roaring bitmap doesn't contain x , this function will return -1.
  855. * The difference with rank function is that this function will return -1 when x
  856. * is not the element of roaring bitmap, but the rank function will return a
  857. * non-negative number.
  858. */
  859. int64_t roaring_bitmap_get_index(const roaring_bitmap_t *r, uint32_t x);
  860. /**
  861. * Returns the smallest value in the set, or UINT32_MAX if the set is empty.
  862. */
  863. uint32_t roaring_bitmap_minimum(const roaring_bitmap_t *r);
  864. /**
  865. * Returns the greatest value in the set, or 0 if the set is empty.
  866. */
  867. uint32_t roaring_bitmap_maximum(const roaring_bitmap_t *r);
  868. /**
  869. * (For advanced users.)
  870. *
  871. * Collect statistics about the bitmap, see roaring_types.h for
  872. * a description of roaring_statistics_t
  873. */
  874. void roaring_bitmap_statistics(const roaring_bitmap_t *r,
  875. roaring_statistics_t *stat);
  876. /**
  877. * Perform internal consistency checks. Returns true if the bitmap is
  878. * consistent. It may be useful to call this after deserializing bitmaps from
  879. * untrusted sources. If roaring_bitmap_internal_validate returns true, then the
  880. * bitmap should be consistent and can be trusted not to cause crashes or memory
  881. * corruption.
  882. *
  883. * Note that some operations intentionally leave bitmaps in an inconsistent
  884. * state temporarily, for example, `roaring_bitmap_lazy_*` functions, until
  885. * `roaring_bitmap_repair_after_lazy` is called.
  886. *
  887. * If reason is non-null, it will be set to a string describing the first
  888. * inconsistency found if any.
  889. */
  890. bool roaring_bitmap_internal_validate(const roaring_bitmap_t *r,
  891. const char **reason);
  892. /*********************
  893. * What follows is code use to iterate through values in a roaring bitmap
  894. roaring_bitmap_t *r =...
  895. roaring_uint32_iterator_t i;
  896. roaring_iterator_create(r, &i);
  897. while(i.has_value) {
  898. printf("value = %d\n", i.current_value);
  899. roaring_uint32_iterator_advance(&i);
  900. }
  901. Obviously, if you modify the underlying bitmap, the iterator
  902. becomes invalid. So don't.
  903. */
  904. /**
  905. * A struct used to keep iterator state. Users should only access
  906. * `current_value` and `has_value`, the rest of the type should be treated as
  907. * opaque.
  908. */
  909. typedef struct roaring_uint32_iterator_s {
  910. const roaring_bitmap_t *parent; // Owner
  911. const ROARING_CONTAINER_T *container; // Current container
  912. uint8_t typecode; // Typecode of current container
  913. int32_t container_index; // Current container index
  914. uint32_t highbits; // High 16 bits of the current value
  915. roaring_container_iterator_t container_it;
  916. uint32_t current_value;
  917. bool has_value;
  918. } roaring_uint32_iterator_t;
  919. /**
  920. * Initialize an iterator object that can be used to iterate through the values.
  921. * If there is a value, then this iterator points to the first value and
  922. * `it->has_value` is true. The value is in `it->current_value`.
  923. */
  924. void roaring_iterator_init(const roaring_bitmap_t *r,
  925. roaring_uint32_iterator_t *newit);
  926. /** DEPRECATED, use `roaring_iterator_init`. */
  927. CROARING_DEPRECATED static inline void roaring_init_iterator(
  928. const roaring_bitmap_t *r, roaring_uint32_iterator_t *newit) {
  929. roaring_iterator_init(r, newit);
  930. }
  931. /**
  932. * Initialize an iterator object that can be used to iterate through the values.
  933. * If there is a value, then this iterator points to the last value and
  934. * `it->has_value` is true. The value is in `it->current_value`.
  935. */
  936. void roaring_iterator_init_last(const roaring_bitmap_t *r,
  937. roaring_uint32_iterator_t *newit);
  938. /** DEPRECATED, use `roaring_iterator_init_last`. */
  939. CROARING_DEPRECATED static inline void roaring_init_iterator_last(
  940. const roaring_bitmap_t *r, roaring_uint32_iterator_t *newit) {
  941. roaring_iterator_init_last(r, newit);
  942. }
  943. /**
  944. * Create an iterator object that can be used to iterate through the values.
  945. * Caller is responsible for calling `roaring_free_iterator()`.
  946. *
  947. * The iterator is initialized (this function calls `roaring_iterator_init()`)
  948. * If there is a value, then this iterator points to the first value and
  949. * `it->has_value` is true. The value is in `it->current_value`.
  950. */
  951. roaring_uint32_iterator_t *roaring_iterator_create(const roaring_bitmap_t *r);
  952. /** DEPRECATED, use `roaring_iterator_create`. */
  953. CROARING_DEPRECATED static inline roaring_uint32_iterator_t *
  954. roaring_create_iterator(const roaring_bitmap_t *r) {
  955. return roaring_iterator_create(r);
  956. }
  957. /**
  958. * Advance the iterator. If there is a new value, then `it->has_value` is true.
  959. * The new value is in `it->current_value`. Values are traversed in increasing
  960. * orders. For convenience, returns `it->has_value`.
  961. *
  962. * Once `it->has_value` is false, `roaring_uint32_iterator_advance` should not
  963. * be called on the iterator again. Calling `roaring_uint32_iterator_previous`
  964. * is allowed.
  965. */
  966. bool roaring_uint32_iterator_advance(roaring_uint32_iterator_t *it);
  967. /** DEPRECATED, use `roaring_uint32_iterator_advance`. */
  968. CROARING_DEPRECATED static inline bool roaring_advance_uint32_iterator(
  969. roaring_uint32_iterator_t *it) {
  970. return roaring_uint32_iterator_advance(it);
  971. }
  972. /**
  973. * Decrement the iterator. If there's a new value, then `it->has_value` is true.
  974. * The new value is in `it->current_value`. Values are traversed in decreasing
  975. * order. For convenience, returns `it->has_value`.
  976. *
  977. * Once `it->has_value` is false, `roaring_uint32_iterator_previous` should not
  978. * be called on the iterator again. Calling `roaring_uint32_iterator_advance` is
  979. * allowed.
  980. */
  981. bool roaring_uint32_iterator_previous(roaring_uint32_iterator_t *it);
  982. /** DEPRECATED, use `roaring_uint32_iterator_previous`. */
  983. CROARING_DEPRECATED static inline bool roaring_previous_uint32_iterator(
  984. roaring_uint32_iterator_t *it) {
  985. return roaring_uint32_iterator_previous(it);
  986. }
  987. /**
  988. * Move the iterator to the first value >= `val`. If there is a such a value,
  989. * then `it->has_value` is true. The new value is in `it->current_value`.
  990. * For convenience, returns `it->has_value`.
  991. */
  992. bool roaring_uint32_iterator_move_equalorlarger(roaring_uint32_iterator_t *it,
  993. uint32_t val);
  994. /** DEPRECATED, use `roaring_uint32_iterator_move_equalorlarger`. */
  995. CROARING_DEPRECATED static inline bool
  996. roaring_move_uint32_iterator_equalorlarger(roaring_uint32_iterator_t *it,
  997. uint32_t val) {
  998. return roaring_uint32_iterator_move_equalorlarger(it, val);
  999. }
  1000. /**
  1001. * Creates a copy of an iterator.
  1002. * Caller must free it.
  1003. */
  1004. roaring_uint32_iterator_t *roaring_uint32_iterator_copy(
  1005. const roaring_uint32_iterator_t *it);
  1006. /** DEPRECATED, use `roaring_uint32_iterator_copy`. */
  1007. CROARING_DEPRECATED static inline roaring_uint32_iterator_t *
  1008. roaring_copy_uint32_iterator(const roaring_uint32_iterator_t *it) {
  1009. return roaring_uint32_iterator_copy(it);
  1010. }
  1011. /**
  1012. * Free memory following `roaring_iterator_create()`
  1013. */
  1014. void roaring_uint32_iterator_free(roaring_uint32_iterator_t *it);
  1015. /** DEPRECATED, use `roaring_uint32_iterator_free`. */
  1016. CROARING_DEPRECATED static inline void roaring_free_uint32_iterator(
  1017. roaring_uint32_iterator_t *it) {
  1018. roaring_uint32_iterator_free(it);
  1019. }
  1020. /*
  1021. * Reads next ${count} values from iterator into user-supplied ${buf}.
  1022. * Returns the number of read elements.
  1023. * This number can be smaller than ${count}, which means that iterator is
  1024. * drained.
  1025. *
  1026. * This function satisfies semantics of iteration and can be used together with
  1027. * other iterator functions.
  1028. * - first value is copied from ${it}->current_value
  1029. * - after function returns, iterator is positioned at the next element
  1030. */
  1031. uint32_t roaring_uint32_iterator_read(roaring_uint32_iterator_t *it,
  1032. uint32_t *buf, uint32_t count);
  1033. /** DEPRECATED, use `roaring_uint32_iterator_read`. */
  1034. CROARING_DEPRECATED static inline uint32_t roaring_read_uint32_iterator(
  1035. roaring_uint32_iterator_t *it, uint32_t *buf, uint32_t count) {
  1036. return roaring_uint32_iterator_read(it, buf, count);
  1037. }
  1038. #ifdef __cplusplus
  1039. }
  1040. }
  1041. } // extern "C" { namespace roaring { namespace api {
  1042. #endif
  1043. #endif /* ROARING_H */
  1044. #ifdef __cplusplus
  1045. /**
  1046. * Best practices for C++ headers is to avoid polluting global scope.
  1047. * But for C compatibility when just `roaring.h` is included building as
  1048. * C++, default to global access for the C public API.
  1049. *
  1050. * BUT when `roaring.hh` is included instead, it sets this flag. That way
  1051. * explicit namespacing must be used to get the C functions.
  1052. *
  1053. * This is outside the include guard so that if you include BOTH headers,
  1054. * the order won't matter; you still get the global definitions.
  1055. */
  1056. #if !defined(ROARING_API_NOT_IN_GLOBAL_NAMESPACE)
  1057. using namespace ::roaring::api;
  1058. #endif
  1059. #endif