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 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, unsigned long 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, unsigned long 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, unsigned long 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 unsigned long jbit_popcount(const struct jbitset *set,
124 unsigned long end_incl)
126 return Judy1Count(set->judy, start, end_incl, (JError_t *)&set->err);
130 * jbit_nth - return the index of the nth bit which is set.
131 * @set: bitset from jbit_new
132 * @n: which bit we are interested in (0-based)
133 * @invalid: what to return if n >= set population
135 * This normally returns the nth bit in the set, and often there is a
136 * convenient known-invalid value (ie. something which is never in the
137 * set). Otherwise, and a wrapper function like this can be used:
139 * static bool jbit_nth_index(struct jbitset *set,
140 * unsigned long n, unsigned long *idx)
142 * // Zero might be valid, if it's first in set.
143 * if (n == 0 && jbit_test(set, 0)) {
147 * *idx = jbit_nth(set, n, 0);
148 * return (*idx != 0);
152 * unsigned long i, val;
154 * // We know 0 isn't in set.
155 * assert(!jbit_test(set, 0));
156 * for (i = 0; (val = jbit_nth(set, i, 0)) != 0; i++) {
157 * assert(jbit_popcount(set, 0, val) == i);
158 * printf("Value %zu = %zu\n", i, val);
161 static inline unsigned long jbit_nth(const struct jbitset *set,
162 unsigned long n, unsigned long invalid)
165 if (!Judy1ByCount(set->judy, n+1, &index, (JError_t *)&set->err))
171 * jbit_first - return the first bit which is set.
172 * @set: bitset from jbit_new
173 * @invalid: return value if no bits are set at all.
175 * This is equivalent to jbit_nth(set, 0, invalid).
178 * assert(!jbit_test(set, 0));
179 * printf("Set contents (increasing order):");
180 * for (i = jbit_first(set, 0); i; i = jbit_next(set, i, 0))
184 static inline unsigned long jbit_first(const struct jbitset *set,
185 unsigned long invalid)
187 unsigned long index = 0;
188 if (!Judy1First(set->judy, &index, (JError_t *)&set->err))
191 assert(index != invalid);
196 * jbit_next - return the next bit which is set.
197 * @set: bitset from jbit_new
198 * @prev: previous index
199 * @invalid: return value if no bits are set at all.
201 * This is usually used to find an adjacent bit which is set, after
204 static inline unsigned long jbit_next(const struct jbitset *set,
206 unsigned long invalid)
208 if (!Judy1Next(set->judy, &prev, (JError_t *)&set->err))
211 assert(prev != invalid);
216 * jbit_last - return the last bit which is set.
217 * @set: bitset from jbit_new
218 * @invalid: return value if no bits are set at all.
221 * assert(!jbit_test(set, 0));
222 * printf("Set contents (decreasing order):");
223 * for (i = jbit_last(set, 0); i; i = jbit_prev(set, i, 0))
227 static inline unsigned long jbit_last(const struct jbitset *set,
228 unsigned long invalid)
230 unsigned long index = -1;
231 if (!Judy1Last(set->judy, &index, (JError_t *)&set->err))
234 assert(index != invalid);
239 * jbit_prev - return the previous bit which is set.
240 * @set: bitset from jbit_new
241 * @prev: previous index
242 * @invalid: return value if no bits are set at all.
244 * This is usually used to find an adjacent bit which is set, after
247 static inline unsigned long jbit_prev(const struct jbitset *set,
249 unsigned long invalid)
251 if (!Judy1Prev(set->judy, &prev, (JError_t *)&set->err))
254 assert(prev != invalid);
259 * jbit_first_clear - return the first bit which is unset.
260 * @set: bitset from jbit_new
261 * @invalid: return value if no bits are clear at all.
263 * This allows for iterating the inverse of the bitmap.
265 static inline unsigned long jbit_first_clear(const struct jbitset *set,
266 unsigned long invalid)
268 unsigned long index = 0;
269 if (!Judy1FirstEmpty(set->judy, &index, (JError_t *)&set->err))
272 assert(index != invalid);
276 static inline unsigned long jbit_next_clear(const struct jbitset *set,
278 unsigned long invalid)
280 if (!Judy1NextEmpty(set->judy, &prev, (JError_t *)&set->err))
283 assert(prev != invalid);
287 static inline unsigned long jbit_last_clear(const struct jbitset *set,
288 unsigned long invalid)
290 unsigned long index = -1;
291 if (!Judy1LastEmpty(set->judy, &index, (JError_t *)&set->err))
294 assert(index != invalid);
298 static inline unsigned long jbit_prev_clear(const struct jbitset *set,
300 unsigned long invalid)
302 if (!Judy1PrevEmpty(set->judy, &prev, (JError_t *)&set->err))
305 assert(prev != invalid);
308 #endif /* CCAN_JBITSET_H */