bitset.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #ifndef CBITSET_BITSET_H
  2. #define 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 CBITSET_RESTRICT restrict
  7. #else
  8. #define 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 *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. /* Find the index of the first bit set. Or zero if the bitset is empty. */
  110. size_t bitset_minimum(const bitset_t *bitset);
  111. /* Find the index of the last bit set. Or zero if the bitset is empty. */
  112. size_t bitset_maximum(const bitset_t *bitset);
  113. /* compute the union in-place (to b1), returns true if successful, to generate a
  114. * new bitset first call bitset_copy */
  115. bool bitset_inplace_union(bitset_t *CBITSET_RESTRICT b1,
  116. const bitset_t *CBITSET_RESTRICT b2);
  117. /* report the size of the union (without materializing it) */
  118. size_t bitset_union_count(const bitset_t *CBITSET_RESTRICT b1,
  119. const bitset_t *CBITSET_RESTRICT b2);
  120. /* compute the intersection in-place (to b1), to generate a new bitset first
  121. * call bitset_copy */
  122. void bitset_inplace_intersection(bitset_t *CBITSET_RESTRICT b1,
  123. const bitset_t *CBITSET_RESTRICT b2);
  124. /* report the size of the intersection (without materializing it) */
  125. size_t bitset_intersection_count(const bitset_t *CBITSET_RESTRICT b1,
  126. const bitset_t *CBITSET_RESTRICT b2);
  127. /* returns true if the bitsets contain no common elements */
  128. bool bitsets_disjoint(const bitset_t *CBITSET_RESTRICT b1,
  129. const bitset_t *CBITSET_RESTRICT b2);
  130. /* returns true if the bitsets contain any common elements */
  131. bool bitsets_intersect(const bitset_t *CBITSET_RESTRICT b1,
  132. const bitset_t *CBITSET_RESTRICT b2);
  133. /* returns true if b1 contains all of the set bits of b2 */
  134. bool bitset_contains_all(const bitset_t *CBITSET_RESTRICT b1,
  135. const bitset_t *CBITSET_RESTRICT b2);
  136. /* compute the difference in-place (to b1), to generate a new bitset first call
  137. * bitset_copy */
  138. void bitset_inplace_difference(bitset_t *CBITSET_RESTRICT b1,
  139. const bitset_t *CBITSET_RESTRICT b2);
  140. /* compute the size of the difference */
  141. size_t bitset_difference_count(const bitset_t *CBITSET_RESTRICT b1,
  142. const bitset_t *CBITSET_RESTRICT b2);
  143. /* compute the symmetric difference in-place (to b1), return true if successful,
  144. * to generate a new bitset first call bitset_copy */
  145. bool bitset_inplace_symmetric_difference(bitset_t *CBITSET_RESTRICT b1,
  146. const bitset_t *CBITSET_RESTRICT b2);
  147. /* compute the size of the symmetric difference */
  148. size_t bitset_symmetric_difference_count(const bitset_t *CBITSET_RESTRICT b1,
  149. const bitset_t *CBITSET_RESTRICT b2);
  150. /* iterate over the set bits
  151. like so :
  152. for(size_t i = 0; bitset_next_set_bit(b,&i) ; i++) {
  153. //.....
  154. }
  155. */
  156. inline bool bitset_next_set_bit(const bitset_t *bitset, size_t *i) {
  157. size_t x = *i / 64;
  158. if (x >= bitset->arraysize) {
  159. return false;
  160. }
  161. uint64_t w = bitset->array[x];
  162. w >>= (*i & 63);
  163. if (w != 0) {
  164. *i += roaring_trailing_zeroes(w);
  165. return true;
  166. }
  167. x++;
  168. while (x < bitset->arraysize) {
  169. w = bitset->array[x];
  170. if (w != 0) {
  171. *i = x * 64 + roaring_trailing_zeroes(w);
  172. return true;
  173. }
  174. x++;
  175. }
  176. return false;
  177. }
  178. /* iterate over the set bits
  179. like so :
  180. size_t buffer[256];
  181. size_t howmany = 0;
  182. for(size_t startfrom = 0; (howmany = bitset_next_set_bits(b,buffer,256,
  183. &startfrom)) > 0 ; startfrom++) {
  184. //.....
  185. }
  186. */
  187. inline size_t bitset_next_set_bits(const bitset_t *bitset, size_t *buffer,
  188. size_t capacity, size_t *startfrom) {
  189. if (capacity == 0) return 0; // sanity check
  190. size_t x = *startfrom / 64;
  191. if (x >= bitset->arraysize) {
  192. return 0; // nothing more to iterate over
  193. }
  194. uint64_t w = bitset->array[x];
  195. w >>= (*startfrom & 63);
  196. size_t howmany = 0;
  197. size_t base = x << 6;
  198. while (howmany < capacity) {
  199. while (w != 0) {
  200. uint64_t t = w & (~w + 1);
  201. int r = roaring_trailing_zeroes(w);
  202. buffer[howmany++] = r + base;
  203. if (howmany == capacity) goto end;
  204. w ^= t;
  205. }
  206. x += 1;
  207. if (x == bitset->arraysize) {
  208. break;
  209. }
  210. base += 64;
  211. w = bitset->array[x];
  212. }
  213. end:
  214. if (howmany > 0) {
  215. *startfrom = buffer[howmany - 1];
  216. }
  217. return howmany;
  218. }
  219. typedef bool (*bitset_iterator)(size_t value, void *param);
  220. // return true if uninterrupted
  221. inline bool bitset_for_each(const bitset_t *b, bitset_iterator iterator,
  222. void *ptr) {
  223. size_t base = 0;
  224. for (size_t i = 0; i < b->arraysize; ++i) {
  225. uint64_t w = b->array[i];
  226. while (w != 0) {
  227. uint64_t t = w & (~w + 1);
  228. int r = roaring_trailing_zeroes(w);
  229. if (!iterator(r + base, ptr)) return false;
  230. w ^= t;
  231. }
  232. base += 64;
  233. }
  234. return true;
  235. }
  236. inline void bitset_print(const bitset_t *b) {
  237. printf("{");
  238. for (size_t i = 0; bitset_next_set_bit(b, &i); i++) {
  239. printf("%zu, ", i);
  240. }
  241. printf("}");
  242. }
  243. #ifdef __cplusplus
  244. }
  245. }
  246. } // extern "C" { namespace roaring { namespace api {
  247. #endif
  248. #endif