roaring64.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. #ifndef ROARING64_H
  2. #define ROARING64_H
  3. #include <stdbool.h>
  4. #include <stddef.h>
  5. #include <stdint.h>
  6. #include <roaring/memory.h>
  7. #include <roaring/portability.h>
  8. #include <roaring/roaring.h>
  9. #include <roaring/roaring_types.h>
  10. #ifdef __cplusplus
  11. extern "C" {
  12. namespace roaring {
  13. namespace api {
  14. #endif
  15. typedef struct roaring64_bitmap_s roaring64_bitmap_t;
  16. typedef struct roaring64_leaf_s roaring64_leaf_t;
  17. typedef struct roaring64_iterator_s roaring64_iterator_t;
  18. /**
  19. * A bit of context usable with `roaring64_bitmap_*_bulk()` functions.
  20. *
  21. * Should be initialized with `{0}` (or `memset()` to all zeros).
  22. * Callers should treat it as an opaque type.
  23. *
  24. * A context may only be used with a single bitmap (unless re-initialized to
  25. * zero), and any modification to a bitmap (other than modifications performed
  26. * with `_bulk()` functions with the context passed) will invalidate any
  27. * contexts associated with that bitmap.
  28. */
  29. typedef struct roaring64_bulk_context_s {
  30. uint8_t high_bytes[6];
  31. roaring64_leaf_t *leaf;
  32. } roaring64_bulk_context_t;
  33. /**
  34. * Dynamically allocates a new bitmap (initially empty).
  35. * Client is responsible for calling `roaring64_bitmap_free()`.
  36. */
  37. roaring64_bitmap_t *roaring64_bitmap_create(void);
  38. void roaring64_bitmap_free(roaring64_bitmap_t *r);
  39. /**
  40. * Returns a copy of a bitmap.
  41. */
  42. roaring64_bitmap_t *roaring64_bitmap_copy(const roaring64_bitmap_t *r);
  43. /**
  44. * Creates a new bitmap of a pointer to N 64-bit integers.
  45. */
  46. roaring64_bitmap_t *roaring64_bitmap_of_ptr(size_t n_args,
  47. const uint64_t *vals);
  48. #ifdef __cplusplus
  49. /**
  50. * Creates a new bitmap which contains all values passed in as arguments.
  51. *
  52. * To create a bitmap from a variable number of arguments, use the
  53. * `roaring64_bitmap_of_ptr` function instead.
  54. */
  55. // Use an immediately invoked closure, capturing by reference
  56. // (in case __VA_ARGS__ refers to context outside the closure)
  57. // Include a 0 at the beginning of the array to make the array length > 0
  58. // (zero sized arrays are not valid in standard c/c++)
  59. #define roaring64_bitmap_from(...) \
  60. [&]() { \
  61. const uint64_t roaring64_bitmap_from_array[] = {0, __VA_ARGS__}; \
  62. return roaring64_bitmap_of_ptr( \
  63. (sizeof(roaring64_bitmap_from_array) / \
  64. sizeof(roaring64_bitmap_from_array[0])) - \
  65. 1, \
  66. &roaring64_bitmap_from_array[1]); \
  67. }()
  68. #else
  69. /**
  70. * Creates a new bitmap which contains all values passed in as arguments.
  71. *
  72. * To create a bitmap from a variable number of arguments, use the
  73. * `roaring64_bitmap_of_ptr` function instead.
  74. */
  75. // While __VA_ARGS__ occurs twice in expansion, one of the times is in a sizeof
  76. // expression, which is an unevaluated context, so it's even safe in the case
  77. // where expressions passed have side effects (roaring64_bitmap_from(my_func(),
  78. // ++i))
  79. // Include a 0 at the beginning of the array to make the array length > 0
  80. // (zero sized arrays are not valid in standard c/c++)
  81. #define roaring64_bitmap_from(...) \
  82. roaring64_bitmap_of_ptr( \
  83. (sizeof((const uint64_t[]){0, __VA_ARGS__}) / sizeof(uint64_t)) - 1, \
  84. &((const uint64_t[]){0, __VA_ARGS__})[1])
  85. #endif
  86. /**
  87. * Create a new bitmap by moving containers from a 32 bit roaring bitmap.
  88. *
  89. * After calling this function, the original bitmap will be empty, and the
  90. * returned bitmap will contain all the values from the original bitmap.
  91. */
  92. roaring64_bitmap_t *roaring64_bitmap_move_from_roaring32(roaring_bitmap_t *r);
  93. /**
  94. * Create a new bitmap containing all the values in [min, max) that are at a
  95. * distance k*step from min.
  96. */
  97. roaring64_bitmap_t *roaring64_bitmap_from_range(uint64_t min, uint64_t max,
  98. uint64_t step);
  99. /**
  100. * Adds the provided value to the bitmap.
  101. */
  102. void roaring64_bitmap_add(roaring64_bitmap_t *r, uint64_t val);
  103. /**
  104. * Adds the provided value to the bitmap.
  105. * Returns true if a new value was added, false if the value already existed.
  106. */
  107. bool roaring64_bitmap_add_checked(roaring64_bitmap_t *r, uint64_t val);
  108. /**
  109. * Add an item, using context from a previous insert for faster insertion.
  110. *
  111. * `context` will be used to store information between calls to make bulk
  112. * operations faster. `*context` should be zero-initialized before the first
  113. * call to this function.
  114. *
  115. * Modifying the bitmap in any way (other than `-bulk` suffixed functions)
  116. * will invalidate the stored context, calling this function with a non-zero
  117. * context after doing any modification invokes undefined behavior.
  118. *
  119. * In order to exploit this optimization, the caller should call this function
  120. * with values with the same high 48 bits of the value consecutively.
  121. */
  122. void roaring64_bitmap_add_bulk(roaring64_bitmap_t *r,
  123. roaring64_bulk_context_t *context, uint64_t val);
  124. /**
  125. * Add `n_args` values from `vals`, faster than repeatedly calling
  126. * `roaring64_bitmap_add()`
  127. *
  128. * In order to exploit this optimization, the caller should attempt to keep
  129. * values with the same high 48 bits of the value as consecutive elements in
  130. * `vals`.
  131. */
  132. void roaring64_bitmap_add_many(roaring64_bitmap_t *r, size_t n_args,
  133. const uint64_t *vals);
  134. /**
  135. * Add all values in range [min, max).
  136. */
  137. void roaring64_bitmap_add_range(roaring64_bitmap_t *r, uint64_t min,
  138. uint64_t max);
  139. /**
  140. * Add all values in range [min, max].
  141. */
  142. void roaring64_bitmap_add_range_closed(roaring64_bitmap_t *r, uint64_t min,
  143. uint64_t max);
  144. /**
  145. * Removes a value from the bitmap if present.
  146. */
  147. void roaring64_bitmap_remove(roaring64_bitmap_t *r, uint64_t val);
  148. /**
  149. * Removes a value from the bitmap if present, returns true if the value was
  150. * removed and false if the value was not present.
  151. */
  152. bool roaring64_bitmap_remove_checked(roaring64_bitmap_t *r, uint64_t val);
  153. /**
  154. * Remove an item, using context from a previous insert for faster removal.
  155. *
  156. * `context` will be used to store information between calls to make bulk
  157. * operations faster. `*context` should be zero-initialized before the first
  158. * call to this function.
  159. *
  160. * Modifying the bitmap in any way (other than `-bulk` suffixed functions)
  161. * will invalidate the stored context, calling this function with a non-zero
  162. * context after doing any modification invokes undefined behavior.
  163. *
  164. * In order to exploit this optimization, the caller should call this function
  165. * with values with the same high 48 bits of the value consecutively.
  166. */
  167. void roaring64_bitmap_remove_bulk(roaring64_bitmap_t *r,
  168. roaring64_bulk_context_t *context,
  169. uint64_t val);
  170. /**
  171. * Remove `n_args` values from `vals`, faster than repeatedly calling
  172. * `roaring64_bitmap_remove()`
  173. *
  174. * In order to exploit this optimization, the caller should attempt to keep
  175. * values with the same high 48 bits of the value as consecutive elements in
  176. * `vals`.
  177. */
  178. void roaring64_bitmap_remove_many(roaring64_bitmap_t *r, size_t n_args,
  179. const uint64_t *vals);
  180. /**
  181. * Remove all values in range [min, max).
  182. */
  183. void roaring64_bitmap_remove_range(roaring64_bitmap_t *r, uint64_t min,
  184. uint64_t max);
  185. /**
  186. * Remove all values in range [min, max].
  187. */
  188. void roaring64_bitmap_remove_range_closed(roaring64_bitmap_t *r, uint64_t min,
  189. uint64_t max);
  190. /**
  191. * Empties the bitmap.
  192. */
  193. void roaring64_bitmap_clear(roaring64_bitmap_t *r);
  194. /**
  195. * Returns true if the provided value is present.
  196. */
  197. bool roaring64_bitmap_contains(const roaring64_bitmap_t *r, uint64_t val);
  198. /**
  199. * Returns true if all values in the range [min, max) are present.
  200. */
  201. bool roaring64_bitmap_contains_range(const roaring64_bitmap_t *r, uint64_t min,
  202. uint64_t max);
  203. /**
  204. * Check if an item is present using context from a previous insert or search
  205. * for faster search.
  206. *
  207. * `context` will be used to store information between calls to make bulk
  208. * operations faster. `*context` should be zero-initialized before the first
  209. * call to this function.
  210. *
  211. * Modifying the bitmap in any way (other than `-bulk` suffixed functions)
  212. * will invalidate the stored context, calling this function with a non-zero
  213. * context after doing any modification invokes undefined behavior.
  214. *
  215. * In order to exploit this optimization, the caller should call this function
  216. * with values with the same high 48 bits of the value consecutively.
  217. */
  218. bool roaring64_bitmap_contains_bulk(const roaring64_bitmap_t *r,
  219. roaring64_bulk_context_t *context,
  220. uint64_t val);
  221. /**
  222. * Selects the element at index 'rank' where the smallest element is at index 0.
  223. * If the size of the bitmap is strictly greater than rank, then this function
  224. * returns true and sets element to the element of given rank. Otherwise, it
  225. * returns false.
  226. */
  227. bool roaring64_bitmap_select(const roaring64_bitmap_t *r, uint64_t rank,
  228. uint64_t *element);
  229. /**
  230. * Returns the number of integers that are smaller or equal to x. Thus if x is
  231. * the first element, this function will return 1. If x is smaller than the
  232. * smallest element, this function will return 0.
  233. *
  234. * The indexing convention differs between roaring64_bitmap_select and
  235. * roaring64_bitmap_rank: roaring_bitmap64_select refers to the smallest value
  236. * as having index 0, whereas roaring64_bitmap_rank returns 1 when ranking
  237. * the smallest value.
  238. */
  239. uint64_t roaring64_bitmap_rank(const roaring64_bitmap_t *r, uint64_t val);
  240. /**
  241. * Returns true if the given value is in the bitmap, and sets `out_index` to the
  242. * (0-based) index of the value in the bitmap. Returns false if the value is not
  243. * in the bitmap.
  244. */
  245. bool roaring64_bitmap_get_index(const roaring64_bitmap_t *r, uint64_t val,
  246. uint64_t *out_index);
  247. /**
  248. * Returns the number of values in the bitmap.
  249. */
  250. uint64_t roaring64_bitmap_get_cardinality(const roaring64_bitmap_t *r);
  251. /**
  252. * Returns the number of elements in the range [min, max).
  253. */
  254. uint64_t roaring64_bitmap_range_cardinality(const roaring64_bitmap_t *r,
  255. uint64_t min, uint64_t max);
  256. /**
  257. * Returns the number of elements in the range [min, max]
  258. */
  259. uint64_t roaring64_bitmap_range_closed_cardinality(const roaring64_bitmap_t *r,
  260. uint64_t min, uint64_t max);
  261. /**
  262. * Returns true if the bitmap is empty (cardinality is zero).
  263. */
  264. bool roaring64_bitmap_is_empty(const roaring64_bitmap_t *r);
  265. /**
  266. * Returns the smallest value in the set, or UINT64_MAX if the set is empty.
  267. */
  268. uint64_t roaring64_bitmap_minimum(const roaring64_bitmap_t *r);
  269. /**
  270. * Returns the largest value in the set, or 0 if empty.
  271. */
  272. uint64_t roaring64_bitmap_maximum(const roaring64_bitmap_t *r);
  273. /**
  274. * Returns true if the result has at least one run container.
  275. */
  276. bool roaring64_bitmap_run_optimize(roaring64_bitmap_t *r);
  277. /**
  278. * (For advanced users.)
  279. * Collect statistics about the bitmap
  280. */
  281. void roaring64_bitmap_statistics(const roaring64_bitmap_t *r,
  282. roaring64_statistics_t *stat);
  283. /**
  284. * Perform internal consistency checks.
  285. *
  286. * Returns true if the bitmap is consistent. It may be useful to call this
  287. * after deserializing bitmaps from untrusted sources. If
  288. * roaring64_bitmap_internal_validate returns true, then the bitmap is
  289. * consistent and can be trusted not to cause crashes or memory corruption.
  290. *
  291. * If reason is non-null, it will be set to a string describing the first
  292. * inconsistency found if any.
  293. */
  294. bool roaring64_bitmap_internal_validate(const roaring64_bitmap_t *r,
  295. const char **reason);
  296. /**
  297. * Return true if the two bitmaps contain the same elements.
  298. */
  299. bool roaring64_bitmap_equals(const roaring64_bitmap_t *r1,
  300. const roaring64_bitmap_t *r2);
  301. /**
  302. * Return true if all the elements of r1 are also in r2.
  303. */
  304. bool roaring64_bitmap_is_subset(const roaring64_bitmap_t *r1,
  305. const roaring64_bitmap_t *r2);
  306. /**
  307. * Return true if all the elements of r1 are also in r2, and r2 is strictly
  308. * greater than r1.
  309. */
  310. bool roaring64_bitmap_is_strict_subset(const roaring64_bitmap_t *r1,
  311. const roaring64_bitmap_t *r2);
  312. /**
  313. * Computes the intersection between two bitmaps and returns new bitmap. The
  314. * caller is responsible for free-ing the result.
  315. *
  316. * Performance hint: if you are computing the intersection between several
  317. * bitmaps, two-by-two, it is best to start with the smallest bitmaps. You may
  318. * also rely on roaring64_bitmap_and_inplace to avoid creating many temporary
  319. * bitmaps.
  320. */
  321. roaring64_bitmap_t *roaring64_bitmap_and(const roaring64_bitmap_t *r1,
  322. const roaring64_bitmap_t *r2);
  323. /**
  324. * Computes the size of the intersection between two bitmaps.
  325. */
  326. uint64_t roaring64_bitmap_and_cardinality(const roaring64_bitmap_t *r1,
  327. const roaring64_bitmap_t *r2);
  328. /**
  329. * In-place version of `roaring64_bitmap_and()`, modifies `r1`. `r1` and `r2`
  330. * are allowed to be equal.
  331. *
  332. * Performance hint: if you are computing the intersection between several
  333. * bitmaps, two-by-two, it is best to start with the smallest bitmaps.
  334. */
  335. void roaring64_bitmap_and_inplace(roaring64_bitmap_t *r1,
  336. const roaring64_bitmap_t *r2);
  337. /**
  338. * Check whether two bitmaps intersect.
  339. */
  340. bool roaring64_bitmap_intersect(const roaring64_bitmap_t *r1,
  341. const roaring64_bitmap_t *r2);
  342. /**
  343. * Check whether a bitmap intersects the range [min, max).
  344. */
  345. bool roaring64_bitmap_intersect_with_range(const roaring64_bitmap_t *r,
  346. uint64_t min, uint64_t max);
  347. /**
  348. * Computes the Jaccard index between two bitmaps. (Also known as the Tanimoto
  349. * distance, or the Jaccard similarity coefficient)
  350. *
  351. * The Jaccard index is undefined if both bitmaps are empty.
  352. */
  353. double roaring64_bitmap_jaccard_index(const roaring64_bitmap_t *r1,
  354. const roaring64_bitmap_t *r2);
  355. /**
  356. * Computes the union between two bitmaps and returns new bitmap. The caller is
  357. * responsible for free-ing the result.
  358. */
  359. roaring64_bitmap_t *roaring64_bitmap_or(const roaring64_bitmap_t *r1,
  360. const roaring64_bitmap_t *r2);
  361. /**
  362. * Computes the size of the union between two bitmaps.
  363. */
  364. uint64_t roaring64_bitmap_or_cardinality(const roaring64_bitmap_t *r1,
  365. const roaring64_bitmap_t *r2);
  366. /**
  367. * In-place version of `roaring64_bitmap_or(), modifies `r1`.
  368. */
  369. void roaring64_bitmap_or_inplace(roaring64_bitmap_t *r1,
  370. const roaring64_bitmap_t *r2);
  371. /**
  372. * Computes the symmetric difference (xor) between two bitmaps and returns a new
  373. * bitmap. The caller is responsible for free-ing the result.
  374. */
  375. roaring64_bitmap_t *roaring64_bitmap_xor(const roaring64_bitmap_t *r1,
  376. const roaring64_bitmap_t *r2);
  377. /**
  378. * Computes the size of the symmetric difference (xor) between two bitmaps.
  379. */
  380. uint64_t roaring64_bitmap_xor_cardinality(const roaring64_bitmap_t *r1,
  381. const roaring64_bitmap_t *r2);
  382. /**
  383. * In-place version of `roaring64_bitmap_xor()`, modifies `r1`. `r1` and `r2`
  384. * are not allowed to be equal (that would result in an empty bitmap).
  385. */
  386. void roaring64_bitmap_xor_inplace(roaring64_bitmap_t *r1,
  387. const roaring64_bitmap_t *r2);
  388. /**
  389. * Computes the difference (andnot) between two bitmaps and returns a new
  390. * bitmap. The caller is responsible for free-ing the result.
  391. */
  392. roaring64_bitmap_t *roaring64_bitmap_andnot(const roaring64_bitmap_t *r1,
  393. const roaring64_bitmap_t *r2);
  394. /**
  395. * Computes the size of the difference (andnot) between two bitmaps.
  396. */
  397. uint64_t roaring64_bitmap_andnot_cardinality(const roaring64_bitmap_t *r1,
  398. const roaring64_bitmap_t *r2);
  399. /**
  400. * In-place version of `roaring64_bitmap_andnot()`, modifies `r1`. `r1` and `r2`
  401. * are not allowed to be equal (that would result in an empty bitmap).
  402. */
  403. void roaring64_bitmap_andnot_inplace(roaring64_bitmap_t *r1,
  404. const roaring64_bitmap_t *r2);
  405. /**
  406. * Compute the negation of the bitmap in the interval [min, max).
  407. * The number of negated values is `max - min`. Areas outside the range are
  408. * passed through unchanged.
  409. */
  410. roaring64_bitmap_t *roaring64_bitmap_flip(const roaring64_bitmap_t *r,
  411. uint64_t min, uint64_t max);
  412. /**
  413. * Compute the negation of the bitmap in the interval [min, max].
  414. * The number of negated values is `max - min + 1`. Areas outside the range are
  415. * passed through unchanged.
  416. */
  417. roaring64_bitmap_t *roaring64_bitmap_flip_closed(const roaring64_bitmap_t *r,
  418. uint64_t min, uint64_t max);
  419. /**
  420. * In-place version of `roaring64_bitmap_flip`. Compute the negation of the
  421. * bitmap in the interval [min, max). The number of negated values is `max -
  422. * min`. Areas outside the range are passed through unchanged.
  423. */
  424. void roaring64_bitmap_flip_inplace(roaring64_bitmap_t *r, uint64_t min,
  425. uint64_t max);
  426. /**
  427. * In-place version of `roaring64_bitmap_flip_closed`. Compute the negation of
  428. * the bitmap in the interval [min, max]. The number of negated values is `max -
  429. * min + 1`. Areas outside the range are passed through unchanged.
  430. */
  431. void roaring64_bitmap_flip_closed_inplace(roaring64_bitmap_t *r, uint64_t min,
  432. uint64_t max);
  433. /**
  434. * How many bytes are required to serialize this bitmap.
  435. *
  436. * This is meant to be compatible with other languages:
  437. * https://github.com/RoaringBitmap/RoaringFormatSpec#extension-for-64-bit-implementations
  438. */
  439. size_t roaring64_bitmap_portable_size_in_bytes(const roaring64_bitmap_t *r);
  440. /**
  441. * Write a bitmap to a buffer. The output buffer should refer to at least
  442. * `roaring64_bitmap_portable_size_in_bytes(r)` bytes of allocated memory.
  443. *
  444. * Returns how many bytes were written, which should match
  445. * `roaring64_bitmap_portable_size_in_bytes(r)`.
  446. *
  447. * This is meant to be compatible with other languages:
  448. * https://github.com/RoaringBitmap/RoaringFormatSpec#extension-for-64-bit-implementations
  449. *
  450. * This function is endian-sensitive. If you have a big-endian system (e.g., a
  451. * mainframe IBM s390x), the data format is going to be big-endian and not
  452. * compatible with little-endian systems.
  453. *
  454. * When serializing data to a file, we recommend that you also use
  455. * checksums so that, at deserialization, you can be confident
  456. * that you are recovering the correct data.
  457. */
  458. size_t roaring64_bitmap_portable_serialize(const roaring64_bitmap_t *r,
  459. char *buf);
  460. /**
  461. * Check how many bytes would be read (up to maxbytes) at this pointer if there
  462. * is a valid bitmap, returns zero if there is no valid bitmap.
  463. *
  464. * This is meant to be compatible with other languages
  465. * https://github.com/RoaringBitmap/RoaringFormatSpec#extension-for-64-bit-implementations
  466. */
  467. size_t roaring64_bitmap_portable_deserialize_size(const char *buf,
  468. size_t maxbytes);
  469. /**
  470. * Read a bitmap from a serialized buffer (reading up to maxbytes).
  471. * In case of failure, NULL is returned.
  472. *
  473. * This is meant to be compatible with other languages
  474. * https://github.com/RoaringBitmap/RoaringFormatSpec#extension-for-64-bit-implementations
  475. *
  476. * The function itself is safe in the sense that it will not cause buffer
  477. * overflows: it will not read beyond the scope of the provided buffer
  478. * (buf,maxbytes).
  479. *
  480. * However, for correct operations, it is assumed that the bitmap
  481. * read was once serialized from a valid bitmap (i.e., it follows the format
  482. * specification). If you provided an incorrect input (garbage), then the bitmap
  483. * read may not be in a valid state and following operations may not lead to
  484. * sensible results. In particular, the serialized array containers need to be
  485. * in sorted order, and the run containers should be in sorted non-overlapping
  486. * order. This is is guaranteed to happen when serializing an existing bitmap,
  487. * but not for random inputs.
  488. *
  489. * You may use roaring64_bitmap_internal_validate to check the validity of the
  490. * bitmap prior to using it.
  491. *
  492. * We recommend that you use checksums to check that serialized data corresponds
  493. * to a serialized bitmap.
  494. *
  495. * This function is endian-sensitive. If you have a big-endian system (e.g., a
  496. * mainframe IBM s390x), the data format is going to be big-endian and not
  497. * compatible with little-endian systems.
  498. */
  499. roaring64_bitmap_t *roaring64_bitmap_portable_deserialize_safe(const char *buf,
  500. size_t maxbytes);
  501. /**
  502. * Iterate over the bitmap elements. The function `iterator` is called once for
  503. * all the values with `ptr` (can be NULL) as the second parameter of each call.
  504. *
  505. * `roaring_iterator64` is simply a pointer to a function that returns a bool
  506. * and takes `(uint64_t, void*)` as inputs. True means that the iteration should
  507. * continue, while false means that it should stop.
  508. *
  509. * Returns true if the `roaring64_iterator` returned true throughout (so that
  510. * all data points were necessarily visited).
  511. *
  512. * Iteration is ordered from the smallest to the largest elements.
  513. */
  514. bool roaring64_bitmap_iterate(const roaring64_bitmap_t *r,
  515. roaring_iterator64 iterator, void *ptr);
  516. /**
  517. * Convert the bitmap to a sorted array `out`.
  518. *
  519. * Caller is responsible to ensure that there is enough memory allocated, e.g.
  520. * ```
  521. * out = malloc(roaring64_bitmap_get_cardinality(bitmap) * sizeof(uint64_t));
  522. * ```
  523. */
  524. void roaring64_bitmap_to_uint64_array(const roaring64_bitmap_t *r,
  525. uint64_t *out);
  526. /**
  527. * Create an iterator object that can be used to iterate through the values.
  528. * Caller is responsible for calling `roaring64_iterator_free()`.
  529. *
  530. * The iterator is initialized. If there is a value, then this iterator points
  531. * to the first value and `roaring64_iterator_has_value()` returns true. The
  532. * value can be retrieved with `roaring64_iterator_value()`.
  533. */
  534. roaring64_iterator_t *roaring64_iterator_create(const roaring64_bitmap_t *r);
  535. /**
  536. * Create an iterator object that can be used to iterate through the values.
  537. * Caller is responsible for calling `roaring64_iterator_free()`.
  538. *
  539. * The iterator is initialized. If there is a value, then this iterator points
  540. * to the last value and `roaring64_iterator_has_value()` returns true. The
  541. * value can be retrieved with `roaring64_iterator_value()`.
  542. */
  543. roaring64_iterator_t *roaring64_iterator_create_last(
  544. const roaring64_bitmap_t *r);
  545. /**
  546. * Re-initializes an existing iterator. Functionally the same as
  547. * `roaring64_iterator_create` without a allocation.
  548. */
  549. void roaring64_iterator_reinit(const roaring64_bitmap_t *r,
  550. roaring64_iterator_t *it);
  551. /**
  552. * Re-initializes an existing iterator. Functionally the same as
  553. * `roaring64_iterator_create_last` without a allocation.
  554. */
  555. void roaring64_iterator_reinit_last(const roaring64_bitmap_t *r,
  556. roaring64_iterator_t *it);
  557. /**
  558. * Creates a copy of the iterator. Caller is responsible for calling
  559. * `roaring64_iterator_free()` on the resulting iterator.
  560. */
  561. roaring64_iterator_t *roaring64_iterator_copy(const roaring64_iterator_t *it);
  562. /**
  563. * Free the iterator.
  564. */
  565. void roaring64_iterator_free(roaring64_iterator_t *it);
  566. /**
  567. * Returns true if the iterator currently points to a value. If so, calling
  568. * `roaring64_iterator_value()` returns the value.
  569. */
  570. bool roaring64_iterator_has_value(const roaring64_iterator_t *it);
  571. /**
  572. * Returns the value the iterator currently points to. Should only be called if
  573. * `roaring64_iterator_has_value()` returns true.
  574. */
  575. uint64_t roaring64_iterator_value(const roaring64_iterator_t *it);
  576. /**
  577. * Advance the iterator. If there is a new value, then
  578. * `roaring64_iterator_has_value()` returns true. Values are traversed in
  579. * increasing order. For convenience, returns the result of
  580. * `roaring64_iterator_has_value()`.
  581. *
  582. * Once this returns false, `roaring64_iterator_advance` should not be called on
  583. * the iterator again. Calling `roaring64_iterator_previous` is allowed.
  584. */
  585. bool roaring64_iterator_advance(roaring64_iterator_t *it);
  586. /**
  587. * Decrement the iterator. If there is a new value, then
  588. * `roaring64_iterator_has_value()` returns true. Values are traversed in
  589. * decreasing order. For convenience, returns the result of
  590. * `roaring64_iterator_has_value()`.
  591. *
  592. * Once this returns false, `roaring64_iterator_previous` should not be called
  593. * on the iterator again. Calling `roaring64_iterator_advance` is allowed.
  594. */
  595. bool roaring64_iterator_previous(roaring64_iterator_t *it);
  596. /**
  597. * Move the iterator to the first value greater than or equal to `val`, if it
  598. * exists at or after the current position of the iterator. If there is a new
  599. * value, then `roaring64_iterator_has_value()` returns true. Values are
  600. * traversed in increasing order. For convenience, returns the result of
  601. * `roaring64_iterator_has_value()`.
  602. */
  603. bool roaring64_iterator_move_equalorlarger(roaring64_iterator_t *it,
  604. uint64_t val);
  605. /**
  606. * Reads up to `count` values from the iterator into the given `buf`. Returns
  607. * the number of elements read. The number of elements read can be smaller than
  608. * `count`, which means that there are no more elements in the bitmap.
  609. *
  610. * This function can be used together with other iterator functions.
  611. */
  612. uint64_t roaring64_iterator_read(roaring64_iterator_t *it, uint64_t *buf,
  613. uint64_t count);
  614. #ifdef __cplusplus
  615. } // extern "C"
  616. } // namespace roaring
  617. } // namespace api
  618. #endif
  619. #endif /* ROARING64_H */