roaring64.h 25 KB

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