roaring_array.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #ifndef INCLUDE_ROARING_ARRAY_H
  2. #define INCLUDE_ROARING_ARRAY_H
  3. #include <assert.h>
  4. #include <stdbool.h>
  5. #include <stdint.h>
  6. #include <roaring/array_util.h>
  7. #include <roaring/containers/containers.h> // get_writable_copy_if_shared()
  8. #ifdef __cplusplus
  9. extern "C" {
  10. namespace roaring {
  11. // Note: in pure C++ code, you should avoid putting `using` in header files
  12. using api::roaring_array_t;
  13. namespace internal {
  14. #endif
  15. enum {
  16. SERIAL_COOKIE_NO_RUNCONTAINER = 12346,
  17. SERIAL_COOKIE = 12347,
  18. FROZEN_COOKIE = 13766,
  19. NO_OFFSET_THRESHOLD = 4
  20. };
  21. /**
  22. * Create a new roaring array
  23. */
  24. roaring_array_t *ra_create(void);
  25. /**
  26. * Initialize an existing roaring array with the specified capacity (in number
  27. * of containers)
  28. */
  29. bool ra_init_with_capacity(roaring_array_t *new_ra, uint32_t cap);
  30. /**
  31. * Initialize with zero capacity
  32. */
  33. void ra_init(roaring_array_t *t);
  34. /**
  35. * Copies this roaring array, we assume that dest is not initialized
  36. */
  37. bool ra_copy(const roaring_array_t *source, roaring_array_t *dest,
  38. bool copy_on_write);
  39. /*
  40. * Shrinks the capacity, returns the number of bytes saved.
  41. */
  42. int ra_shrink_to_fit(roaring_array_t *ra);
  43. /**
  44. * Copies this roaring array, we assume that dest is initialized
  45. */
  46. bool ra_overwrite(const roaring_array_t *source, roaring_array_t *dest,
  47. bool copy_on_write);
  48. /**
  49. * Frees the memory used by a roaring array
  50. */
  51. void ra_clear(roaring_array_t *r);
  52. /**
  53. * Frees the memory used by a roaring array, but does not free the containers
  54. */
  55. void ra_clear_without_containers(roaring_array_t *r);
  56. /**
  57. * Frees just the containers
  58. */
  59. void ra_clear_containers(roaring_array_t *ra);
  60. /**
  61. * Get the index corresponding to a 16-bit key
  62. */
  63. inline int32_t ra_get_index(const roaring_array_t *ra, uint16_t x) {
  64. if ((ra->size == 0) || ra->keys[ra->size - 1] == x) return ra->size - 1;
  65. return binarySearch(ra->keys, (int32_t)ra->size, x);
  66. }
  67. /**
  68. * Retrieves the container at index i, filling in the typecode
  69. */
  70. inline container_t *ra_get_container_at_index(const roaring_array_t *ra,
  71. uint16_t i, uint8_t *typecode) {
  72. *typecode = ra->typecodes[i];
  73. return ra->containers[i];
  74. }
  75. /**
  76. * Retrieves the key at index i
  77. */
  78. inline uint16_t ra_get_key_at_index(const roaring_array_t *ra, uint16_t i) {
  79. return ra->keys[i];
  80. }
  81. /**
  82. * Add a new key-value pair at index i
  83. */
  84. void ra_insert_new_key_value_at(roaring_array_t *ra, int32_t i, uint16_t key,
  85. container_t *c, uint8_t typecode);
  86. /**
  87. * Append a new key-value pair
  88. */
  89. void ra_append(roaring_array_t *ra, uint16_t key, container_t *c,
  90. uint8_t typecode);
  91. /**
  92. * Append a new key-value pair to ra, cloning (in COW sense) a value from sa
  93. * at index index
  94. */
  95. void ra_append_copy(roaring_array_t *ra, const roaring_array_t *sa,
  96. uint16_t index, bool copy_on_write);
  97. /**
  98. * Append new key-value pairs to ra, cloning (in COW sense) values from sa
  99. * at indexes
  100. * [start_index, end_index)
  101. */
  102. void ra_append_copy_range(roaring_array_t *ra, const roaring_array_t *sa,
  103. int32_t start_index, int32_t end_index,
  104. bool copy_on_write);
  105. /** appends from sa to ra, ending with the greatest key that is
  106. * is less or equal stopping_key
  107. */
  108. void ra_append_copies_until(roaring_array_t *ra, const roaring_array_t *sa,
  109. uint16_t stopping_key, bool copy_on_write);
  110. /** appends from sa to ra, starting with the smallest key that is
  111. * is strictly greater than before_start
  112. */
  113. void ra_append_copies_after(roaring_array_t *ra, const roaring_array_t *sa,
  114. uint16_t before_start, bool copy_on_write);
  115. /**
  116. * Move the key-value pairs to ra from sa at indexes
  117. * [start_index, end_index), old array should not be freed
  118. * (use ra_clear_without_containers)
  119. **/
  120. void ra_append_move_range(roaring_array_t *ra, roaring_array_t *sa,
  121. int32_t start_index, int32_t end_index);
  122. /**
  123. * Append new key-value pairs to ra, from sa at indexes
  124. * [start_index, end_index)
  125. */
  126. void ra_append_range(roaring_array_t *ra, roaring_array_t *sa,
  127. int32_t start_index, int32_t end_index,
  128. bool copy_on_write);
  129. /**
  130. * Set the container at the corresponding index using the specified
  131. * typecode.
  132. */
  133. inline void ra_set_container_at_index(const roaring_array_t *ra, int32_t i,
  134. container_t *c, uint8_t typecode) {
  135. assert(i < ra->size);
  136. ra->containers[i] = c;
  137. ra->typecodes[i] = typecode;
  138. }
  139. container_t *ra_get_container(roaring_array_t *ra, uint16_t x,
  140. uint8_t *typecode);
  141. /**
  142. * If needed, increase the capacity of the array so that it can fit k values
  143. * (at
  144. * least);
  145. */
  146. bool extend_array(roaring_array_t *ra, int32_t k);
  147. inline int32_t ra_get_size(const roaring_array_t *ra) { return ra->size; }
  148. static inline int32_t ra_advance_until(const roaring_array_t *ra, uint16_t x,
  149. int32_t pos) {
  150. return advanceUntil(ra->keys, pos, ra->size, x);
  151. }
  152. int32_t ra_advance_until_freeing(roaring_array_t *ra, uint16_t x, int32_t pos);
  153. void ra_downsize(roaring_array_t *ra, int32_t new_length);
  154. inline void ra_replace_key_and_container_at_index(roaring_array_t *ra,
  155. int32_t i, uint16_t key,
  156. container_t *c,
  157. uint8_t typecode) {
  158. assert(i < ra->size);
  159. ra->keys[i] = key;
  160. ra->containers[i] = c;
  161. ra->typecodes[i] = typecode;
  162. }
  163. // write set bits to an array
  164. void ra_to_uint32_array(const roaring_array_t *ra, uint32_t *ans);
  165. bool ra_range_uint32_array(const roaring_array_t *ra, size_t offset,
  166. size_t limit, uint32_t *ans);
  167. /**
  168. * write a bitmap to a buffer. This is meant to be compatible with
  169. * the
  170. * Java and Go versions. Return the size in bytes of the serialized
  171. * output (which should be ra_portable_size_in_bytes(ra)).
  172. */
  173. size_t ra_portable_serialize(const roaring_array_t *ra, char *buf);
  174. /**
  175. * read a bitmap from a serialized version. This is meant to be compatible
  176. * with the Java and Go versions.
  177. * maxbytes indicates how many bytes available from buf.
  178. * When the function returns true, roaring_array_t is populated with the data
  179. * and *readbytes indicates how many bytes were read. In all cases, if the
  180. * function returns true, then maxbytes >= *readbytes.
  181. */
  182. bool ra_portable_deserialize(roaring_array_t *ra, const char *buf,
  183. const size_t maxbytes, size_t *readbytes);
  184. /**
  185. * Quickly checks whether there is a serialized bitmap at the pointer,
  186. * not exceeding size "maxbytes" in bytes. This function does not allocate
  187. * memory dynamically.
  188. *
  189. * This function returns 0 if and only if no valid bitmap is found.
  190. * Otherwise, it returns how many bytes are occupied by the bitmap data.
  191. */
  192. size_t ra_portable_deserialize_size(const char *buf, const size_t maxbytes);
  193. /**
  194. * How many bytes are required to serialize this bitmap (meant to be
  195. * compatible
  196. * with Java and Go versions)
  197. */
  198. size_t ra_portable_size_in_bytes(const roaring_array_t *ra);
  199. /**
  200. * return true if it contains at least one run container.
  201. */
  202. bool ra_has_run_container(const roaring_array_t *ra);
  203. /**
  204. * Size of the header when serializing (meant to be compatible
  205. * with Java and Go versions)
  206. */
  207. uint32_t ra_portable_header_size(const roaring_array_t *ra);
  208. /**
  209. * If the container at the index i is share, unshare it (creating a local
  210. * copy if needed).
  211. */
  212. static inline void ra_unshare_container_at_index(roaring_array_t *ra,
  213. uint16_t i) {
  214. assert(i < ra->size);
  215. ra->containers[i] =
  216. get_writable_copy_if_shared(ra->containers[i], &ra->typecodes[i]);
  217. }
  218. /**
  219. * remove at index i, sliding over all entries after i
  220. */
  221. void ra_remove_at_index(roaring_array_t *ra, int32_t i);
  222. /**
  223. * clears all containers, sets the size at 0 and shrinks the memory usage.
  224. */
  225. void ra_reset(roaring_array_t *ra);
  226. /**
  227. * remove at index i, sliding over all entries after i. Free removed container.
  228. */
  229. void ra_remove_at_index_and_free(roaring_array_t *ra, int32_t i);
  230. /**
  231. * remove a chunk of indices, sliding over entries after it
  232. */
  233. // void ra_remove_index_range(roaring_array_t *ra, int32_t begin, int32_t end);
  234. // used in inplace andNot only, to slide left the containers from
  235. // the mutated RoaringBitmap that are after the largest container of
  236. // the argument RoaringBitmap. It is followed by a call to resize.
  237. //
  238. void ra_copy_range(roaring_array_t *ra, uint32_t begin, uint32_t end,
  239. uint32_t new_begin);
  240. /**
  241. * Shifts rightmost $count containers to the left (distance < 0) or
  242. * to the right (distance > 0).
  243. * Allocates memory if necessary.
  244. * This function doesn't free or create new containers.
  245. * Caller is responsible for that.
  246. */
  247. void ra_shift_tail(roaring_array_t *ra, int32_t count, int32_t distance);
  248. #ifdef __cplusplus
  249. } // namespace internal
  250. }
  251. } // extern "C" { namespace roaring {
  252. #endif
  253. #endif