1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
2 #ifndef CCAN_JBITSET_TYPE_H
3 #define CCAN_JBITSET_TYPE_H
4 #include <ccan/jbitset/jbitset.h>
7 * JBIT_DEFINE_TYPE - create a set of jbit ops for a given pointer type
8 * @type: a type whose pointers will go into the bitset.
9 * @name: a name for all the functions to define (of form jbit_<name>_*)
11 * This macro defines a set of inline functions for typesafe and convenient
12 * usage of a Judy bitset for pointers. It is assumed that a NULL pointer
13 * is never set in the bitset.
16 * JBIT_DEFINE_TYPE(char, char);
17 * JBIT_DEFINE_TYPE(struct foo, foo);
19 * static struct jbitset_char *jc;
20 * struct jbitset_foo *jf;
22 * static void add_to_bitsets(const char *p, const struct foo *f)
24 * // Note, this adds the pointer, not the string!
25 * jbit_char_set(jc, p);
26 * jbit_foo_set(jf, f);
29 #define JBIT_DEFINE_TYPE(type, name) \
30 struct jbitset_##name; \
31 static inline struct jbitset_##name *jbit_##name##_new(void) \
33 return (struct jbitset_##name *)jbit_new(); \
35 static inline void jbit_##name##_free(const struct jbitset_##name *set) \
37 jbit_free((const struct jbitset *)set); \
39 static inline const char *jbit_##name##_error(struct jbitset_##name *set) \
41 return jbit_error((struct jbitset *)set); \
43 static inline bool jbit_##name##_test(const struct jbitset_##name *set, \
46 return jbit_test((const struct jbitset *)set, (size_t)index); \
48 static inline bool jbit_##name##_set(struct jbitset_##name *set, \
51 return jbit_set((struct jbitset *)set, (size_t)index); \
53 static inline bool jbit_##name##_clear(struct jbitset_##name *set, \
56 return jbit_clear((struct jbitset *)set, (size_t)index); \
58 static inline size_t jbit_##name##_count(struct jbitset_##name *set) \
60 return jbit_popcount((const struct jbitset *)set, 0, -1); \
62 static inline type *jbit_##name##_nth(const struct jbitset_##name *set, \
65 return (type *)jbit_nth((const struct jbitset *)set, n, 0); \
67 static inline type *jbit_##name##_first(const struct jbitset_##name *set) \
69 return (type *)jbit_first((const struct jbitset *)set, 0); \
71 static inline type *jbit_##name##_next(struct jbitset_##name *set, \
74 return (type *)jbit_next((const struct jbitset *)set, (size_t)prev, 0); \
76 static inline type *jbit_##name##_last(struct jbitset_##name *set) \
78 return (type *)jbit_last((const struct jbitset *)set, 0); \
80 static inline type *jbit_##name##_prev(struct jbitset_##name *set, \
83 return (type *)jbit_prev((const struct jbitset *)set, (size_t)prev, 0); \
85 #endif /* CCAN_JBITSET_TYPE_H */