jbitset.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #ifndef CCAN_JBITSET_H
  2. #define CCAN_JBITSET_H
  3. #include <Judy.h>
  4. #include <stdbool.h>
  5. #include <ccan/compiler/compiler.h>
  6. #include <assert.h>
  7. /**
  8. * jbit_new - create a new, empty jbitset.
  9. *
  10. * See Also:
  11. * JBIT_DEFINE_TYPE()
  12. *
  13. * Example:
  14. * struct jbitset *set = jbit_new();
  15. * if (!set)
  16. * errx(1, "Failed to allocate jbitset");
  17. */
  18. struct jbitset *jbit_new(void);
  19. /**
  20. * jbit_free - destroy a jbitset.
  21. * @set: the set returned from jbit_new.
  22. *
  23. * Example:
  24. * jbit_free(set);
  25. */
  26. void jbit_free(const struct jbitset *set);
  27. /* This is exposed in the header so we can inline. Treat it as private! */
  28. struct jbitset {
  29. void *judy;
  30. JError_t err;
  31. const char *errstr;
  32. };
  33. const char *COLD_ATTRIBUTE jbit_error_(struct jbitset *set);
  34. /**
  35. * jbit_error - test for an error in the a previous jbit_ operation.
  36. * @set: the set to test.
  37. *
  38. * Under normal circumstances, return NULL to indicate no error has occurred.
  39. * Otherwise, it will return a string containing the error. This string
  40. * can only be freed by jbit_free() on the set.
  41. *
  42. * Other than out-of-memory, errors are caused by memory corruption or
  43. * interface misuse.
  44. *
  45. * Example:
  46. * struct jbitset *set = jbit_new();
  47. * const char *errstr;
  48. *
  49. * if (!set)
  50. * err(1, "allocating jbitset");
  51. * errstr = jbit_error(set);
  52. * if (errstr)
  53. * errx(1, "Woah, error on newly created set?! %s", errstr);
  54. */
  55. static inline const char *jbit_error(struct jbitset *set)
  56. {
  57. if (JU_ERRNO(&set->err) <= JU_ERRNO_NFMAX)
  58. return NULL;
  59. return jbit_error_(set);
  60. }
  61. /**
  62. * jbit_test - test a bit in the bitset.
  63. * @set: bitset from jbit_new
  64. * @index: the index to test
  65. *
  66. * Returns true if jbit_set() has been done on this index, false otherwise.
  67. *
  68. * Example:
  69. * assert(!jbit_test(set, 0));
  70. */
  71. static inline bool jbit_test(const struct jbitset *set, unsigned long index)
  72. {
  73. return Judy1Test(set->judy, index, (JError_t *)&set->err);
  74. }
  75. /**
  76. * jbit_set - set a bit in the bitset.
  77. * @set: bitset from jbit_new
  78. * @index: the index to set
  79. *
  80. * Returns false if it was already set (ie. nothing changed)
  81. *
  82. * Example:
  83. * if (jbit_set(set, 0))
  84. * err(1, "Bit 0 was already set?!");
  85. */
  86. static inline bool jbit_set(struct jbitset *set, unsigned long index)
  87. {
  88. return Judy1Set(&set->judy, index, &set->err);
  89. }
  90. /**
  91. * jbit_clear - clear a bit in the bitset.
  92. * @set: bitset from jbit_new
  93. * @index: the index to set
  94. *
  95. * Returns the old bit value (ie. false if nothing changed).
  96. *
  97. * Example:
  98. * if (jbit_clear(set, 0))
  99. * err(1, "Bit 0 was already clear?!");
  100. */
  101. static inline bool jbit_clear(struct jbitset *set, unsigned long index)
  102. {
  103. return Judy1Unset(&set->judy, index, &set->err);
  104. }
  105. /**
  106. * jbit_popcount - get population of (some part of) bitset.
  107. * @set: bitset from jbit_new
  108. * @start: first index to count
  109. * @end_incl: last index to count (use -1 for end).
  110. *
  111. * Example:
  112. * assert(jbit_popcount(set, 0, 1000) <= jbit_popcount(set, 0, 2000));
  113. */
  114. static inline unsigned long jbit_popcount(const struct jbitset *set,
  115. unsigned long start,
  116. unsigned long end_incl)
  117. {
  118. return Judy1Count(set->judy, start, end_incl, (JError_t *)&set->err);
  119. }
  120. /**
  121. * jbit_nth - return the index of the nth bit which is set.
  122. * @set: bitset from jbit_new
  123. * @n: which bit we are interested in (0-based)
  124. * @invalid: what to return if n >= set population
  125. *
  126. * This normally returns the nth bit in the set, and often there is a
  127. * convenient known-invalid value (ie. something which is never in the
  128. * set). Otherwise, and a wrapper function like this can be used:
  129. *
  130. * static bool jbit_nth_index(struct jbitset *set,
  131. * unsigned long n, unsigned long *idx)
  132. * {
  133. * // Zero might be valid, if it's first in set.
  134. * if (n == 0 && jbit_test(set, 0)) {
  135. * *idx = 0;
  136. * return true;
  137. * }
  138. * *idx = jbit_nth(set, n, 0);
  139. * return (*idx != 0);
  140. * }
  141. *
  142. * Example:
  143. * unsigned long i, val;
  144. *
  145. * // We know 0 isn't in set.
  146. * assert(!jbit_test(set, 0));
  147. * for (i = 0; (val = jbit_nth(set, i, 0)) != 0; i++) {
  148. * assert(jbit_popcount(set, 0, val) == i);
  149. * printf("Value %zu = %zu\n", i, val);
  150. * }
  151. */
  152. static inline unsigned long jbit_nth(const struct jbitset *set,
  153. unsigned long n, unsigned long invalid)
  154. {
  155. unsigned long index;
  156. if (!Judy1ByCount(set->judy, n+1, &index, (JError_t *)&set->err))
  157. index = invalid;
  158. return index;
  159. }
  160. /**
  161. * jbit_first - return the first bit which is set.
  162. * @set: bitset from jbit_new
  163. * @invalid: return value if no bits are set at all.
  164. *
  165. * This is equivalent to jbit_nth(set, 0, invalid).
  166. *
  167. * Example:
  168. * assert(!jbit_test(set, 0));
  169. * printf("Set contents (increasing order):");
  170. * for (i = jbit_first(set, 0); i; i = jbit_next(set, i, 0))
  171. * printf(" %zu", i);
  172. * printf("\n");
  173. */
  174. static inline unsigned long jbit_first(const struct jbitset *set,
  175. unsigned long invalid)
  176. {
  177. unsigned long index = 0;
  178. if (!Judy1First(set->judy, &index, (JError_t *)&set->err))
  179. index = invalid;
  180. else
  181. assert(index != invalid);
  182. return index;
  183. }
  184. /**
  185. * jbit_next - return the next bit which is set.
  186. * @set: bitset from jbit_new
  187. * @prev: previous index
  188. * @invalid: return value if no bits are set at all.
  189. *
  190. * This is usually used to find an adjacent bit which is set, after
  191. * jbit_first.
  192. */
  193. static inline unsigned long jbit_next(const struct jbitset *set,
  194. unsigned long prev,
  195. unsigned long invalid)
  196. {
  197. if (!Judy1Next(set->judy, &prev, (JError_t *)&set->err))
  198. prev = invalid;
  199. else
  200. assert(prev != invalid);
  201. return prev;
  202. }
  203. /**
  204. * jbit_last - return the last bit which is set.
  205. * @set: bitset from jbit_new
  206. * @invalid: return value if no bits are set at all.
  207. *
  208. * Example:
  209. * assert(!jbit_test(set, 0));
  210. * printf("Set contents (decreasing order):");
  211. * for (i = jbit_last(set, 0); i; i = jbit_prev(set, i, 0))
  212. * printf(" %zu", i);
  213. * printf("\n");
  214. */
  215. static inline unsigned long jbit_last(const struct jbitset *set,
  216. unsigned long invalid)
  217. {
  218. unsigned long index = -1;
  219. if (!Judy1Last(set->judy, &index, (JError_t *)&set->err))
  220. index = invalid;
  221. else
  222. assert(index != invalid);
  223. return index;
  224. }
  225. /**
  226. * jbit_prev - return the previous bit which is set.
  227. * @set: bitset from jbit_new
  228. * @prev: previous index
  229. * @invalid: return value if no bits are set at all.
  230. *
  231. * This is usually used to find an adjacent bit which is set, after
  232. * jbit_last.
  233. */
  234. static inline unsigned long jbit_prev(const struct jbitset *set,
  235. unsigned long prev,
  236. unsigned long invalid)
  237. {
  238. if (!Judy1Prev(set->judy, &prev, (JError_t *)&set->err))
  239. prev = invalid;
  240. else
  241. assert(prev != invalid);
  242. return prev;
  243. }
  244. /**
  245. * jbit_first_clear - return the first bit which is unset.
  246. * @set: bitset from jbit_new
  247. * @invalid: return value if no bits are clear at all.
  248. *
  249. * This allows for iterating the inverse of the bitmap.
  250. */
  251. static inline unsigned long jbit_first_clear(const struct jbitset *set,
  252. unsigned long invalid)
  253. {
  254. unsigned long index = 0;
  255. if (!Judy1FirstEmpty(set->judy, &index, (JError_t *)&set->err))
  256. index = invalid;
  257. else
  258. assert(index != invalid);
  259. return index;
  260. }
  261. static inline unsigned long jbit_next_clear(const struct jbitset *set,
  262. unsigned long prev,
  263. unsigned long invalid)
  264. {
  265. if (!Judy1NextEmpty(set->judy, &prev, (JError_t *)&set->err))
  266. prev = invalid;
  267. else
  268. assert(prev != invalid);
  269. return prev;
  270. }
  271. static inline unsigned long jbit_last_clear(const struct jbitset *set,
  272. unsigned long invalid)
  273. {
  274. unsigned long index = -1;
  275. if (!Judy1LastEmpty(set->judy, &index, (JError_t *)&set->err))
  276. index = invalid;
  277. else
  278. assert(index != invalid);
  279. return index;
  280. }
  281. static inline unsigned long jbit_prev_clear(const struct jbitset *set,
  282. unsigned long prev,
  283. unsigned long invalid)
  284. {
  285. if (!Judy1PrevEmpty(set->judy, &prev, (JError_t *)&set->err))
  286. prev = invalid;
  287. else
  288. assert(prev != invalid);
  289. return prev;
  290. }
  291. #endif /* CCAN_JBITSET_H */