5 #include <ccan/compiler/compiler.h>
9 * jbit_new - create a new, empty jbitset.
15 * struct jbitset *set = jbit_new();
17 * errx(1, "Failed to allocate jbitset");
19 struct jbitset *jbit_new(void);
22 * jbit_free - destroy a jbitset.
23 * @set: the set returned from jbit_new.
28 void jbit_free(const struct jbitset *set);
30 /* This is exposed in the header so we can inline. Treat it as private! */
36 const char *COLD_ATTRIBUTE jbit_error_(struct jbitset *set);
39 * jbit_error - test for an error in the a previous jbit_ operation.
40 * @set: the set to test.
42 * Under normal circumstances, return NULL to indicate no error has occurred.
43 * Otherwise, it will return a string containing the error. This string
44 * can only be freed by jbit_free() on the set.
46 * Other than out-of-memory, errors are caused by memory corruption or
50 * struct jbitset *set = jbit_new();
54 * err(1, "allocating jbitset");
55 * errstr = jbit_error(set);
57 * errx(1, "Woah, error on newly created set?! %s", errstr);
59 static inline const char *jbit_error(struct jbitset *set)
61 if (JU_ERRNO(&set->err) <= JU_ERRNO_NFMAX)
63 return jbit_error_(set);
67 * jbit_test - test a bit in the bitset.
68 * @set: bitset from jbit_new
69 * @index: the index to test
71 * Returns true if jbit_set() has been done on this index, false otherwise.
74 * assert(!jbit_test(set, 0));
76 static inline bool jbit_test(const struct jbitset *set, size_t index)
78 return Judy1Test(set->judy, index, (JError_t *)&set->err);
82 * jbit_set - set a bit in the bitset.
83 * @set: bitset from jbit_new
84 * @index: the index to set
86 * Returns false if it was already set (ie. nothing changed)
89 * if (jbit_set(set, 0))
90 * err(1, "Bit 0 was already set?!");
92 static inline bool jbit_set(struct jbitset *set, size_t index)
94 return Judy1Set(&set->judy, index, &set->err);
98 * jbit_clear - clear a bit in the bitset.
99 * @set: bitset from jbit_new
100 * @index: the index to set
102 * Returns the old bit value (ie. false if nothing changed).
105 * if (jbit_clear(set, 0))
106 * err(1, "Bit 0 was already clear?!");
108 static inline bool jbit_clear(struct jbitset *set, size_t index)
110 return Judy1Unset(&set->judy, index, &set->err);
114 * jbit_popcount - get population of (some part of) bitset.
115 * @set: bitset from jbit_new
116 * @start: first index to count
117 * @end_incl: last index to count (use -1 for end).
120 * assert(jbit_popcount(set, 0, 1000) <= jbit_popcount(set, 0, 2000));
122 static inline size_t jbit_popcount(const struct jbitset *set,
123 size_t start, size_t end_incl)
125 return Judy1Count(set->judy, start, end_incl, (JError_t *)&set->err);
129 * jbit_nth - return the index of the nth bit which is set.
130 * @set: bitset from jbit_new
131 * @n: which bit we are interested in (0-based)
132 * @invalid: what to return if n >= set population
134 * This normally returns the nth bit in the set, and often there is a
135 * convenient known-invalid value (ie. something which is never in the
136 * set). Otherwise, and a wrapper function like this can be used:
138 * static bool jbit_nth_index(struct jbitset *set, size_t n, size_t *idx)
140 * // Zero might be valid, if it's first in set.
141 * if (n == 0 && jbit_test(set, 0)) {
145 * *idx = jbit_nth(set, n, 0);
146 * return (*idx != 0);
152 * // We know 0 isn't in set.
153 * assert(!jbit_test(set, 0));
154 * for (i = 0; (val = jbit_nth(set, i, 0)) != 0; i++) {
155 * assert(jbit_popcount(set, 0, val) == i);
156 * printf("Value %zu = %zu\n", i, val);
159 static inline size_t jbit_nth(const struct jbitset *set,
160 size_t n, size_t invalid)
163 if (!Judy1ByCount(set->judy, n+1, &index, (JError_t *)&set->err))
169 * jbit_first - return the first bit which is set.
170 * @set: bitset from jbit_new
171 * @invalid: return value if no bits are set at all.
173 * This is equivalent to jbit_nth(set, 0, invalid).
176 * assert(!jbit_test(set, 0));
177 * printf("Set contents (increasing order):");
178 * for (i = jbit_first(set, 0); i; i = jbit_next(set, i, 0))
182 static inline size_t jbit_first(const struct jbitset *set, size_t invalid)
185 if (!Judy1First(set->judy, &index, (JError_t *)&set->err))
188 assert(index != invalid);
193 * jbit_next - return the next bit which is set.
194 * @set: bitset from jbit_new
195 * @prev: previous index
196 * @invalid: return value if no bits are set at all.
198 * This is usually used to find an adjacent bit which is set, after
201 static inline size_t jbit_next(const struct jbitset *set, size_t prev,
204 if (!Judy1Next(set->judy, (Word_t *)&prev, (JError_t *)&set->err))
207 assert(prev != invalid);
212 * jbit_last - return the last bit which is set.
213 * @set: bitset from jbit_new
214 * @invalid: return value if no bits are set at all.
217 * assert(!jbit_test(set, 0));
218 * printf("Set contents (decreasing order):");
219 * for (i = jbit_last(set, 0); i; i = jbit_prev(set, i, 0))
223 static inline size_t jbit_last(const struct jbitset *set, size_t invalid)
226 if (!Judy1Last(set->judy, &index, (JError_t *)&set->err))
229 assert(index != invalid);
234 * jbit_prev - return the previous bit which is set.
235 * @set: bitset from jbit_new
236 * @prev: previous index
237 * @invalid: return value if no bits are set at all.
239 * This is usually used to find an adjacent bit which is set, after
242 static inline size_t jbit_prev(const struct jbitset *set, size_t prev,
245 if (!Judy1Prev(set->judy, (Word_t *)&prev, (JError_t *)&set->err))
248 assert(prev != invalid);
253 * jbit_first_clear - return the first bit which is unset.
254 * @set: bitset from jbit_new
255 * @invalid: return value if no bits are clear at all.
257 * This allows for iterating the inverse of the bitmap.
259 static inline size_t jbit_first_clear(const struct jbitset *set,
263 if (!Judy1FirstEmpty(set->judy, &index, (JError_t *)&set->err))
266 assert(index != invalid);
270 static inline size_t jbit_next_clear(const struct jbitset *set, size_t prev,
273 if (!Judy1NextEmpty(set->judy, (Word_t *)&prev, (JError_t *)&set->err))
276 assert(prev != invalid);
280 static inline size_t jbit_last_clear(const struct jbitset *set, size_t invalid)
283 if (!Judy1LastEmpty(set->judy, &index, (JError_t *)&set->err))
286 assert(index != invalid);
290 static inline size_t jbit_prev_clear(const struct jbitset *set, size_t prev,
293 if (!Judy1PrevEmpty(set->judy, (Word_t *)&prev, (JError_t *)&set->err))
296 assert(prev != invalid);
299 #endif /* CCAN_JBITSET_H */