1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
2 #ifndef CCAN_JMAP_TYPE_H
3 #define CCAN_JMAP_TYPE_H
4 #include <ccan/jmap/jmap.h>
7 * JMAP_DEFINE_UINTIDX_TYPE - create a set of jmap ops for integer->ptr map
8 * @type: a type whose pointers will be values in the map.
9 * @name: a name for all the functions to define (of form jmap_<name>_*)
11 * It's easiest if NULL values aren't placed in the map: jmap_@name_get will
12 * return NULL if an index isn't valid.
14 * The following wrapper functions are defined; each one is the same as
15 * the jmap.h generic equivalent except where noted:
17 * // Creating, errors and freeing.
18 * struct jmap_@name *jmap_@name_new(void);
19 * void jmap_@name_free(const struct jmap_@name *map);
20 * const char *jmap_@name_error(struct jmap_@name *map);
22 * // Add, set, delete, test and get.
23 * bool jmap_@name_add(struct jmap_@name *map,
24 * unsigned long idx, const type *value);
25 * bool jmap_@name_set(const struct jmap_@name *map,
26 * unsigned long idx, const type *value);
27 * bool jmap_@name_del(struct jmap_@name *map, unsigned long idx);
28 * bool jmap_@name_test(const struct jmap_@name *map, unsigned long idx);
29 * type *jmap_@name_get(const struct jmap_@name *map, unsigned long idx);
31 * // Counting and iteration.
32 * unsigned long jmap_@name_popcount(const struct jmap_@name *map,
33 * unsigned long start,
34 * unsigned long end_incl);
35 * unsigned long jmap_@name_nth(const struct jmap_@name *map,
36 * unsigned long n, unsigned long invalid);
37 * unsigned long jmap_@name_first(const struct jmap_@name *map,
38 * unsigned long invalid);
39 * unsigned long jmap_@name_next(const struct jmap_@name *map,
41 * unsigned long invalid);
42 * unsigned long jmap_@name_last(const struct jmap_@name *map,
43 * unsigned long invalid);
44 * unsigned long jmap_@name_prev(const struct jmap_@name *map,
46 * unsigned long invalid);
48 * // Get pointers to values to use.
49 * type **jmap_@name_getval(const struct jmap_@name *map,
51 * void jmap_@name_putval(struct jmap_@name *map, type ***p);
52 * type **jmap_@name_nthval(struct jmap_@name *map,
53 * unsigned long n, unsigned long *idx);
54 * type **jmap_@name_firstval(const struct jmap_@name *map,
55 * unsigned long *idx);
56 * type **jmap_@name_nextval(const struct jmap_@name *map,
57 * unsigned long *idx);
58 * type **jmap_@name_lastval(const struct jmap_@name *map,
59 * unsigned long *idx);
60 * type **jmap_@name_prevval(const struct jmap_@name *map,
61 * unsigned long *idx);
63 #define JMAP_DEFINE_UINTIDX_TYPE(type, name) \
65 static inline struct jmap_##name *jmap_##name##_new(void) \
67 return (struct jmap_##name *)jmap_new(); \
69 static inline void jmap_##name##_free(const struct jmap_##name *map) \
71 jmap_free((const struct jmap *)map); \
73 static inline const char *jmap_##name##_error(struct jmap_##name *map) \
75 return jmap_error((struct jmap *)map); \
77 static inline bool jmap_##name##_add(struct jmap_##name *map, \
78 unsigned long idx, const type *value) \
80 return jmap_add((struct jmap *)map, idx, (unsigned long)value); \
82 static inline bool jmap_##name##_set(const struct jmap_##name *map, \
83 unsigned long idx, const type *value) \
85 return jmap_set((const struct jmap *)map, idx, (unsigned long)value); \
87 static inline bool jmap_##name##_del(struct jmap_##name *map, \
90 return jmap_del((struct jmap *)map, idx); \
92 static inline bool jmap_##name##_test(const struct jmap_##name *map, \
95 return jmap_test((const struct jmap *)map, (unsigned long)idx); \
97 static inline type *jmap_##name##_get(const struct jmap_##name *map, \
100 return (type *)jmap_get((const struct jmap *)map, idx, 0); \
102 static inline unsigned long \
103 jmap_##name##_popcount(const struct jmap_##name *map, \
104 unsigned long start, unsigned long end_incl) \
106 return jmap_popcount((const struct jmap *)map, start, end_incl); \
108 static inline unsigned long jmap_##name##_nth(const struct jmap_##name *map, \
110 unsigned long invalid) \
112 return jmap_nth((const struct jmap *)map, n, invalid); \
114 static inline unsigned long \
115 jmap_##name##_first(const struct jmap_##name *map, \
116 unsigned long invalid) \
118 return jmap_first((const struct jmap *)map, invalid); \
120 static inline unsigned long \
121 jmap_##name##_next(const struct jmap_##name *map, \
122 unsigned long prev, unsigned long invalid) \
124 return jmap_next((const struct jmap *)map, prev, invalid); \
126 static inline unsigned long \
127 jmap_##name##_last(const struct jmap_##name *map, \
128 unsigned long invalid) \
130 return jmap_last((const struct jmap *)map, invalid); \
132 static inline unsigned long \
133 jmap_##name##_prev(const struct jmap_##name *map, \
134 unsigned long prev, unsigned long invalid) \
136 return jmap_prev((const struct jmap *)map, prev, invalid); \
138 static inline type **jmap_##name##_getval(const struct jmap_##name *map, \
141 return (type **)jmap_getval((struct jmap *)map, idx); \
143 static inline void jmap_##name##_putval(struct jmap_##name *map, \
146 return jmap_putval((struct jmap *)map, (unsigned long **)p); \
148 static inline type **jmap_##name##_nthval(struct jmap_##name *map, \
150 unsigned long *idx) \
152 return (type **)jmap_nthval((struct jmap *)map, n, idx); \
154 static inline type **jmap_##name##_firstval(const struct jmap_##name *map, \
155 unsigned long *idx) \
157 return (type **)jmap_firstval((const struct jmap *)map, idx); \
159 static inline type **jmap_##name##_nextval(const struct jmap_##name *map, \
160 unsigned long *idx) \
162 return (type **)jmap_nextval((const struct jmap *)map, idx); \
164 static inline type **jmap_##name##_lastval(const struct jmap_##name *map, \
165 unsigned long *idx) \
167 return (type **)jmap_lastval((const struct jmap *)map, idx); \
169 static inline type **jmap_##name##_prevval(const struct jmap_##name *map, \
170 unsigned long *idx) \
172 return (type **)jmap_prevval((const struct jmap *)map, idx); \
176 * JMAP_DEFINE_PTRIDX_TYPE - create a map of jmap ops for ptr->ptr map
177 * @itype: a type whose pointers will idx into the map.
178 * @type: a type whose pointers will be values in the map.
179 * @name: a name for all the functions to define (of form jmap_<name>_*)
181 * This macro defines a map of inline functions for typesafe and
182 * convenient usage of a pointer-idxed Judy map of pointers. It is
183 * assumed that a NULL pointer is never an idx in the map, as
184 * various functions return NULL for "invalid idx". Similarly,
185 * jmap_@name_get will return NULL if an idx isn't valid, so NULL indices
186 * are not recommended (though you can tell using jmap_@name_test).
188 * Since the ordering is by idx pointer value, it's generally quite useless.
189 * Thus we don't define order-specific functions, except first/next for
192 * The following wrapper functions are defined; each one is the same as
193 * the jmap.h generic equivalent:
195 * struct jmap_@name *jmap_@name_new(void);
196 * void jmap_@name_free(const struct jmap_@name *map);
197 * const char *jmap_@name_error(struct jmap_@name *map);
199 * bool jmap_@name_add(const struct jmap_@name *map,
200 * const itype *idx, const type *value);
201 * bool jmap_@name_set(const struct jmap_@name *map,
202 * const itype *idx, const type *value);
203 * bool jmap_@name_del(struct jmap_@name *map, const itype *idx);
204 * bool jmap_@name_test(const struct jmap_@name *map, const itype *idx);
206 * type *jmap_@name_get(const struct jmap_@name *map, const itype *idx);
207 * itype *jmap_@name_count(const struct jmap_@name *map);
208 * itype *jmap_@name_first(const struct jmap_@name *map);
209 * itype *jmap_@name_next(const struct jmap_@name *map,
210 * const itype *prev);
212 * type **jmap_@name_getval(const struct jmap_@name *map,
214 * void jmap_@name_putval(struct jmap_@name *map, type ***p);
215 * type **jmap_@name_firstval(const struct jmap_@name *map,
216 * const itype **idx);
217 * type **jmap_@name_nextval(const struct jmap_@name *map,
218 * const itype **idx);
220 #define JMAP_DEFINE_PTRIDX_TYPE(itype, type, name) \
221 struct jmap_##name; \
222 static inline struct jmap_##name *jmap_##name##_new(void) \
224 return (struct jmap_##name *)jmap_new(); \
226 static inline void jmap_##name##_free(const struct jmap_##name *map) \
228 jmap_free((const struct jmap *)map); \
230 static inline const char *jmap_##name##_error(struct jmap_##name *map) \
232 return jmap_error((struct jmap *)map); \
234 static inline bool jmap_##name##_add(struct jmap_##name *map, \
235 const itype *idx, const type *value) \
237 return jmap_add((struct jmap *)map, (unsigned long)idx, \
238 (unsigned long)value); \
240 static inline bool jmap_##name##_set(const struct jmap_##name *map, \
241 const itype *idx, const type *value) \
243 return jmap_set((const struct jmap *)map, (unsigned long)idx, \
244 (unsigned long)value); \
246 static inline bool jmap_##name##_del(struct jmap_##name *map, \
249 return jmap_del((struct jmap *)map, (unsigned long)idx); \
251 static inline bool jmap_##name##_test(const struct jmap_##name *map, \
254 return jmap_test((const struct jmap *)map, (unsigned long)idx); \
256 static inline type *jmap_##name##_get(const struct jmap_##name *map, \
259 return (type *)jmap_get((const struct jmap *)map, \
260 (unsigned long)idx, 0); \
262 static inline unsigned long \
263 jmap_##name##_count(const struct jmap_##name *map) \
265 return jmap_popcount((const struct jmap *)map, 0, -1); \
267 static inline itype *jmap_##name##_first(const struct jmap_##name *map) \
269 return (itype *)jmap_first((const struct jmap *)map, 0); \
271 static inline itype *jmap_##name##_next(const struct jmap_##name *map, \
274 return (itype *)jmap_next((const struct jmap *)map, \
275 (unsigned long)prev, 0); \
277 static inline type **jmap_##name##_getval(const struct jmap_##name *map, \
280 return (type **)jmap_getval((struct jmap *)map, \
281 (unsigned long)idx); \
283 static inline void jmap_##name##_putval(struct jmap_##name *map, \
286 return jmap_putval((struct jmap *)map, (unsigned long **)p); \
288 static inline type **jmap_##name##_firstval(const struct jmap_##name *map, \
293 ret = (type **)jmap_firstval((const struct jmap *)map, &i); \
297 static inline type **jmap_##name##_nextval(const struct jmap_##name *map, \
300 return (type **)jmap_nextval((const struct jmap *)map, \
301 (unsigned long *)idx); \
303 #endif /* CCAN_JMAP_TYPE_H */