bitset.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #ifndef CROARING_CBITSET_BITSET_H
  2. #define CROARING_CBITSET_BITSET_H
  3. // For compatibility with MSVC with the use of `restrict`
  4. #if (__STDC_VERSION__ >= 199901L) || \
  5. (defined(__GNUC__) && defined(__STDC_VERSION__))
  6. #define CROARING_CBITSET_RESTRICT restrict
  7. #else
  8. #define CROARING_CBITSET_RESTRICT
  9. #endif // (__STDC_VERSION__ >= 199901L) || (defined(__GNUC__) &&
  10. // defined(__STDC_VERSION__ ))
  11. #include <stdbool.h>
  12. #include <stdint.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <roaring/portability.h>
  17. #ifdef __cplusplus
  18. extern "C" {
  19. namespace roaring {
  20. namespace api {
  21. #endif
  22. struct bitset_s {
  23. uint64_t *CROARING_CBITSET_RESTRICT array;
  24. /* For simplicity and performance, we prefer to have a size and a capacity
  25. * that is a multiple of 64 bits. Thus we only track the size and the
  26. * capacity in terms of 64-bit words allocated */
  27. size_t arraysize;
  28. size_t capacity;
  29. };
  30. typedef struct bitset_s bitset_t;
  31. /* Create a new bitset. Return NULL in case of failure. */
  32. bitset_t *bitset_create(void);
  33. /* Create a new bitset able to contain size bits. Return NULL in case of
  34. * failure. */
  35. bitset_t *bitset_create_with_capacity(size_t size);
  36. /* Free memory. */
  37. void bitset_free(bitset_t *bitset);
  38. /* Set all bits to zero. */
  39. void bitset_clear(bitset_t *bitset);
  40. /* Set all bits to one. */
  41. void bitset_fill(bitset_t *bitset);
  42. /* Create a copy */
  43. bitset_t *bitset_copy(const bitset_t *bitset);
  44. /* For advanced users: Resize the bitset so that it can support newarraysize *
  45. * 64 bits. Return true in case of success, false for failure. Pad with zeroes
  46. * new buffer areas if requested. */
  47. bool bitset_resize(bitset_t *bitset, size_t newarraysize, bool padwithzeroes);
  48. /* returns how many bytes of memory the backend buffer uses */
  49. inline size_t bitset_size_in_bytes(const bitset_t *bitset) {
  50. return bitset->arraysize * sizeof(uint64_t);
  51. }
  52. /* returns how many bits can be accessed */
  53. inline size_t bitset_size_in_bits(const bitset_t *bitset) {
  54. return bitset->arraysize * 64;
  55. }
  56. /* returns how many words (64-bit) of memory the backend buffer uses */
  57. inline size_t bitset_size_in_words(const bitset_t *bitset) {
  58. return bitset->arraysize;
  59. }
  60. /* For advanced users: Grow the bitset so that it can support newarraysize * 64
  61. * bits with padding. Return true in case of success, false for failure. */
  62. bool bitset_grow(bitset_t *bitset, size_t newarraysize);
  63. /* attempts to recover unused memory, return false in case of
  64. * roaring_reallocation failure */
  65. bool bitset_trim(bitset_t *bitset);
  66. /* shifts all bits by 's' positions so that the bitset representing values
  67. * 1,2,10 would represent values 1+s, 2+s, 10+s */
  68. void bitset_shift_left(bitset_t *bitset, size_t s);
  69. /* shifts all bits by 's' positions so that the bitset representing values
  70. * 1,2,10 would represent values 1-s, 2-s, 10-s, negative values are deleted */
  71. void bitset_shift_right(bitset_t *bitset, size_t s);
  72. /* Set the ith bit. Attempts to resize the bitset if needed (may silently fail)
  73. */
  74. inline void bitset_set(bitset_t *bitset, size_t i) {
  75. size_t shiftedi = i / 64;
  76. if (shiftedi >= bitset->arraysize) {
  77. if (!bitset_grow(bitset, shiftedi + 1)) {
  78. return;
  79. }
  80. }
  81. bitset->array[shiftedi] |= ((uint64_t)1) << (i % 64);
  82. }
  83. /* Set the ith bit to the specified value. Attempts to resize the bitset if
  84. * needed (may silently fail) */
  85. inline void bitset_set_to_value(bitset_t *bitset, size_t i, bool flag) {
  86. size_t shiftedi = i / 64;
  87. uint64_t mask = ((uint64_t)1) << (i % 64);
  88. uint64_t dynmask = ((uint64_t)flag) << (i % 64);
  89. if (shiftedi >= bitset->arraysize) {
  90. if (!bitset_grow(bitset, shiftedi + 1)) {
  91. return;
  92. }
  93. }
  94. uint64_t w = bitset->array[shiftedi];
  95. w &= ~mask;
  96. w |= dynmask;
  97. bitset->array[shiftedi] = w;
  98. }
  99. /* Get the value of the ith bit. */
  100. inline bool bitset_get(const bitset_t *bitset, size_t i) {
  101. size_t shiftedi = i / 64;
  102. if (shiftedi >= bitset->arraysize) {
  103. return false;
  104. }
  105. return (bitset->array[shiftedi] & (((uint64_t)1) << (i % 64))) != 0;
  106. }
  107. /* Count number of bits set. */
  108. size_t bitset_count(const bitset_t *bitset);
  109. /* Returns true if no bit is set. */
  110. bool bitset_empty(const bitset_t *bitset);
  111. /* Find the index of the first bit set. Or SIZE_MAX if the bitset is empty. */
  112. size_t bitset_minimum(const bitset_t *bitset);
  113. /* Find the index of the last bit set. Or zero if the bitset is empty. */
  114. size_t bitset_maximum(const bitset_t *bitset);
  115. /* compute the union in-place (to b1), returns true if successful, to generate a
  116. * new bitset first call bitset_copy */
  117. bool bitset_inplace_union(bitset_t *CROARING_CBITSET_RESTRICT b1,
  118. const bitset_t *CROARING_CBITSET_RESTRICT b2);
  119. /* report the size of the union (without materializing it) */
  120. size_t bitset_union_count(const bitset_t *CROARING_CBITSET_RESTRICT b1,
  121. const bitset_t *CROARING_CBITSET_RESTRICT b2);
  122. /* compute the intersection in-place (to b1), to generate a new bitset first
  123. * call bitset_copy */
  124. void bitset_inplace_intersection(bitset_t *CROARING_CBITSET_RESTRICT b1,
  125. const bitset_t *CROARING_CBITSET_RESTRICT b2);
  126. /* report the size of the intersection (without materializing it) */
  127. size_t bitset_intersection_count(const bitset_t *CROARING_CBITSET_RESTRICT b1,
  128. const bitset_t *CROARING_CBITSET_RESTRICT b2);
  129. /* returns true if the bitsets contain no common elements */
  130. bool bitsets_disjoint(const bitset_t *CROARING_CBITSET_RESTRICT b1,
  131. const bitset_t *CROARING_CBITSET_RESTRICT b2);
  132. /* returns true if the bitsets contain any common elements */
  133. bool bitsets_intersect(const bitset_t *CROARING_CBITSET_RESTRICT b1,
  134. const bitset_t *CROARING_CBITSET_RESTRICT b2);
  135. /* returns true if b1 contains all of the set bits of b2 */
  136. bool bitset_contains_all(const bitset_t *CROARING_CBITSET_RESTRICT b1,
  137. const bitset_t *CROARING_CBITSET_RESTRICT b2);
  138. /* compute the difference in-place (to b1), to generate a new bitset first call
  139. * bitset_copy */
  140. void bitset_inplace_difference(bitset_t *CROARING_CBITSET_RESTRICT b1,
  141. const bitset_t *CROARING_CBITSET_RESTRICT b2);
  142. /* compute the size of the difference */
  143. size_t bitset_difference_count(const bitset_t *CROARING_CBITSET_RESTRICT b1,
  144. const bitset_t *CROARING_CBITSET_RESTRICT b2);
  145. /* compute the symmetric difference in-place (to b1), return true if successful,
  146. * to generate a new bitset first call bitset_copy */
  147. bool bitset_inplace_symmetric_difference(
  148. bitset_t *CROARING_CBITSET_RESTRICT b1,
  149. const bitset_t *CROARING_CBITSET_RESTRICT b2);
  150. /* compute the size of the symmetric difference */
  151. size_t bitset_symmetric_difference_count(
  152. const bitset_t *CROARING_CBITSET_RESTRICT b1,
  153. const bitset_t *CROARING_CBITSET_RESTRICT b2);
  154. /* iterate over the set bits
  155. like so :
  156. for(size_t i = 0; bitset_next_set_bit(b,&i) ; i++) {
  157. //.....
  158. }
  159. */
  160. inline bool bitset_next_set_bit(const bitset_t *bitset, size_t *i) {
  161. size_t x = *i / 64;
  162. if (x >= bitset->arraysize) {
  163. return false;
  164. }
  165. uint64_t w = bitset->array[x];
  166. w >>= (*i & 63);
  167. if (w != 0) {
  168. *i += roaring_trailing_zeroes(w);
  169. return true;
  170. }
  171. x++;
  172. while (x < bitset->arraysize) {
  173. w = bitset->array[x];
  174. if (w != 0) {
  175. *i = x * 64 + roaring_trailing_zeroes(w);
  176. return true;
  177. }
  178. x++;
  179. }
  180. return false;
  181. }
  182. /* iterate over the set bits
  183. like so :
  184. size_t buffer[256];
  185. size_t howmany = 0;
  186. for(size_t startfrom = 0; (howmany = bitset_next_set_bits(b,buffer,256,
  187. &startfrom)) > 0 ; startfrom++) {
  188. //.....
  189. }
  190. */
  191. inline size_t bitset_next_set_bits(const bitset_t *bitset, size_t *buffer,
  192. size_t capacity, size_t *startfrom) {
  193. if (capacity == 0) return 0; // sanity check
  194. size_t x = *startfrom / 64;
  195. if (x >= bitset->arraysize) {
  196. return 0; // nothing more to iterate over
  197. }
  198. uint64_t w = bitset->array[x];
  199. w >>= (*startfrom & 63);
  200. size_t howmany = 0;
  201. size_t base = x << 6;
  202. while (howmany < capacity) {
  203. while (w != 0) {
  204. uint64_t t = w & (~w + 1);
  205. int r = roaring_trailing_zeroes(w);
  206. buffer[howmany++] = r + base;
  207. if (howmany == capacity) goto end;
  208. w ^= t;
  209. }
  210. x += 1;
  211. if (x == bitset->arraysize) {
  212. break;
  213. }
  214. base += 64;
  215. w = bitset->array[x];
  216. }
  217. end:
  218. if (howmany > 0) {
  219. *startfrom = buffer[howmany - 1];
  220. }
  221. return howmany;
  222. }
  223. typedef bool (*bitset_iterator)(size_t value, void *param);
  224. // return true if uninterrupted
  225. inline bool bitset_for_each(const bitset_t *b, bitset_iterator iterator,
  226. void *ptr) {
  227. size_t base = 0;
  228. for (size_t i = 0; i < b->arraysize; ++i) {
  229. uint64_t w = b->array[i];
  230. while (w != 0) {
  231. uint64_t t = w & (~w + 1);
  232. int r = roaring_trailing_zeroes(w);
  233. if (!iterator(r + base, ptr)) return false;
  234. w ^= t;
  235. }
  236. base += 64;
  237. }
  238. return true;
  239. }
  240. inline void bitset_print(const bitset_t *b) {
  241. printf("{");
  242. for (size_t i = 0; bitset_next_set_bit(b, &i); i++) {
  243. printf("%zu, ", i);
  244. }
  245. printf("}");
  246. }
  247. #ifdef __cplusplus
  248. }
  249. }
  250. } // extern "C" { namespace roaring { namespace api {
  251. #endif
  252. #endif