1 #ifndef CCAN_JMAP_TYPE_H
2 #define CCAN_JMAP_TYPE_H
3 #include <ccan/jmap/jmap.h>
6 * JMAP_DEFINE_UINTIDX_TYPE - create a set of jmap ops for integer->ptr map
7 * @type: a type whose pointers will be values in the map.
8 * @name: a name for all the functions to define (of form jmap_<name>_*)
10 * It's easiest if NULL values aren't placed in the map: jmap_@name_get will
11 * return NULL if an index isn't valid.
13 * The following wrapper functions are defined; each one is the same as
14 * the jmap.h generic equivalent except where noted:
16 * struct jmap_@name *jmap_@name_new(void);
17 * void jmap_@name_free(const struct jmap_@name *map);
18 * const char *jmap_@name_error(struct jmap_@name *map);
20 * bool jmap_@name_add(const struct jmap_@name *map,
21 * size_t index, const type *value);
22 * bool jmap_@name_set(const struct jmap_@name *map,
23 * size_t index, const type *value);
24 * bool jmap_@name_del(struct jmap_@name *map, size_t index);
25 * bool jmap_@name_test(const struct jmap_@name *map, size_t index);
27 * type *jmap_@name_get(const struct jmap_@name *map, size_t index);
28 * size_t jmap_@name_popcount(const struct jmap_@name *map,
29 * size_t start, size_t end_incl);
30 * size_t jmap_@name_nth(const struct jmap_@name *map,
31 * size_t n, size_t invalid);
32 * size_t jmap_@name_first(const struct jmap_@name *map,
34 * size_t jmap_@name_next(const struct jmap_@name *map,
35 * size_t prev, size_t invalid);
36 * size_t jmap_@name_last(const struct jmap_@name *map,
38 * size_t jmap_@name_prev(const struct jmap_@name *map,
39 * size_t prev, size_t invalid);
41 * type **jmap_@name_getval(const struct jmap_@name *map, size_t index);
42 * void jmap_@name_putval(struct jmap_@name *map, type ***p);
43 * type **jmap_@name_nthval(struct jmap_@name *map,
44 * size_t n, size_t *index);
45 * type **jmap_@name_firstval(const struct jmap_@name *map,
47 * type **jmap_@name_nextval(const struct jmap_@name *map, size_t *index);
48 * type **jmap_@name_lastval(const struct jmap_@name *map, size_t *index);
49 * type **jmap_@name_prevval(const struct jmap_@name *map, size_t *index);
51 #define JMAP_DEFINE_UINTIDX_TYPE(type, name) \
53 static inline struct jmap_##name *jmap_##name##_new(void) \
55 return (struct jmap_##name *)jmap_new(); \
57 static inline void jmap_##name##_free(const struct jmap_##name *map) \
59 jmap_free((const struct jmap *)map); \
61 static inline const char *jmap_##name##_error(struct jmap_##name *map) \
63 return jmap_error((struct jmap *)map); \
65 static inline bool jmap_##name##_add(struct jmap_##name *map, \
66 size_t index, const type *value) \
68 return jmap_add((struct jmap *)map, index, (size_t)value); \
70 static inline bool jmap_##name##_set(const struct jmap_##name *map, \
71 size_t index, const type *value) \
73 return jmap_set((const struct jmap *)map, index, (size_t)value); \
75 static inline bool jmap_##name##_del(struct jmap_##name *map, size_t index) \
77 return jmap_del((struct jmap *)map, index); \
79 static inline bool jmap_##name##_test(const struct jmap_##name *map, \
82 return jmap_test((const struct jmap *)map, (size_t)index); \
84 static inline type *jmap_##name##_get(const struct jmap_##name *map, \
87 return (type *)jmap_get((const struct jmap *)map, index, 0); \
89 static inline size_t jmap_##name##_popcount(const struct jmap_##name *map, \
90 size_t start, size_t end_incl) \
92 return jmap_popcount((const struct jmap *)map, start, end_incl); \
94 static inline size_t jmap_##name##_nth(const struct jmap_##name *map, \
95 size_t n, size_t invalid) \
97 return jmap_nth((const struct jmap *)map, n, invalid); \
99 static inline size_t jmap_##name##_first(const struct jmap_##name *map, \
102 return jmap_first((const struct jmap *)map, invalid); \
104 static inline size_t jmap_##name##_next(const struct jmap_##name *map, \
105 size_t prev, size_t invalid) \
107 return jmap_next((const struct jmap *)map, prev, invalid); \
109 static inline size_t jmap_##name##_last(const struct jmap_##name *map, \
112 return jmap_last((const struct jmap *)map, invalid); \
114 static inline size_t jmap_##name##_prev(const struct jmap_##name *map, \
115 size_t prev, size_t invalid) \
117 return jmap_prev((const struct jmap *)map, prev, invalid); \
119 static inline type **jmap_##name##_getval(const struct jmap_##name *map, \
122 return (type **)jmap_getval((struct jmap *)map, index); \
124 static inline void jmap_##name##_putval(struct jmap_##name *map, \
127 return jmap_putval((struct jmap *)map, (size_t **)p); \
129 static inline type **jmap_##name##_nthval(struct jmap_##name *map, \
130 size_t n, size_t *index) \
132 return (type **)jmap_nthval((struct jmap *)map, n, index); \
134 static inline type **jmap_##name##_firstval(const struct jmap_##name *map, \
137 return (type **)jmap_firstval((const struct jmap *)map, index); \
139 static inline type **jmap_##name##_nextval(const struct jmap_##name *map, \
142 return (type **)jmap_nextval((const struct jmap *)map, index); \
144 static inline type **jmap_##name##_lastval(const struct jmap_##name *map, \
147 return (type **)jmap_lastval((const struct jmap *)map, index); \
149 static inline type **jmap_##name##_prevval(const struct jmap_##name *map, \
152 return (type **)jmap_prevval((const struct jmap *)map, index); \
156 * JMAP_DEFINE_PTRIDX_TYPE - create a map of jmap ops for ptr->ptr map
157 * @itype: a type whose pointers will index into the map.
158 * @type: a type whose pointers will be values in the map.
159 * @name: a name for all the functions to define (of form jmap_<name>_*)
161 * This macro defines a map of inline functions for typesafe and
162 * convenient usage of a pointer-indexed Judy map of pointers. It is
163 * assumed that a NULL pointer is never an index in the map, as
164 * various functions return NULL for "invalid index". Similarly,
165 * jmap_@name_get will return NULL if an index isn't valid, so NULL indices
166 * are not recommended (though you can tell using jmap_@name_test).
168 * Since the ordering is by index pointer value, it's generally quite useless.
169 * Thus we don't define order-specific functions, except first/next for
172 * The following wrapper functions are defined; each one is the same as
173 * the jmap.h generic equivalent:
175 * struct jmap_@name *jmap_@name_new(void);
176 * void jmap_@name_free(const struct jmap_@name *map);
177 * const char *jmap_@name_error(struct jmap_@name *map);
179 * bool jmap_@name_add(const struct jmap_@name *map,
180 * const itype *index, const type *value);
181 * bool jmap_@name_set(const struct jmap_@name *map,
182 * const itype *index, const type *value);
183 * bool jmap_@name_del(struct jmap_@name *map, const itype *index);
184 * bool jmap_@name_test(const struct jmap_@name *map, const itype *index);
186 * type *jmap_@name_get(const struct jmap_@name *map, const itype *index);
187 * itype *jmap_@name_count(const struct jmap_@name *map);
188 * itype *jmap_@name_first(const struct jmap_@name *map);
189 * itype *jmap_@name_next(const struct jmap_@name *map,
190 * const itype *prev);
192 * type **jmap_@name_getval(const struct jmap_@name *map,
193 * const itype *index);
194 * void jmap_@name_putval(struct jmap_@name *map, type ***p);
195 * type **jmap_@name_firstval(const struct jmap_@name *map,
196 * const itype **index);
197 * type **jmap_@name_nextval(const struct jmap_@name *map,
198 * const itype **index);
200 #define JMAP_DEFINE_PTRIDX_TYPE(itype, type, name) \
201 struct jmap_##name; \
202 static inline struct jmap_##name *jmap_##name##_new(void) \
204 return (struct jmap_##name *)jmap_new(); \
206 static inline void jmap_##name##_free(const struct jmap_##name *map) \
208 jmap_free((const struct jmap *)map); \
210 static inline const char *jmap_##name##_error(struct jmap_##name *map) \
212 return jmap_error((struct jmap *)map); \
214 static inline bool jmap_##name##_add(struct jmap_##name *map, \
215 const itype *index, const type *value) \
217 return jmap_add((struct jmap *)map, (size_t)index, \
220 static inline bool jmap_##name##_set(const struct jmap_##name *map, \
221 const itype *index, const type *value) \
223 return jmap_set((const struct jmap *)map, (size_t)index, \
226 static inline bool jmap_##name##_del(struct jmap_##name *map, \
227 const itype *index) \
229 return jmap_del((struct jmap *)map, (size_t)index); \
231 static inline bool jmap_##name##_test(const struct jmap_##name *map, \
232 const itype *index) \
234 return jmap_test((const struct jmap *)map, (size_t)index); \
236 static inline type *jmap_##name##_get(const struct jmap_##name *map, \
237 const itype *index) \
239 return (type *)jmap_get((const struct jmap *)map, (size_t)index, 0); \
241 static inline size_t jmap_##name##_count(const struct jmap_##name *map) \
243 return jmap_popcount((const struct jmap *)map, 0, -1); \
245 static inline itype *jmap_##name##_first(const struct jmap_##name *map) \
247 return (itype *)jmap_first((const struct jmap *)map, 0); \
249 static inline itype *jmap_##name##_next(const struct jmap_##name *map, \
252 return (itype *)jmap_next((const struct jmap *)map, (size_t)prev, 0); \
254 static inline type **jmap_##name##_getval(const struct jmap_##name *map, \
255 const itype *index) \
257 return (type **)jmap_getval((struct jmap *)map, (size_t)index); \
259 static inline void jmap_##name##_putval(struct jmap_##name *map, \
262 return jmap_putval((struct jmap *)map, (size_t **)p); \
264 static inline type **jmap_##name##_firstval(const struct jmap_##name *map, \
269 ret = (type **)jmap_firstval((const struct jmap *)map, &idx); \
270 *index = (void *)idx; \
273 static inline type **jmap_##name##_nextval(const struct jmap_##name *map, \
276 return (type **)jmap_nextval((const struct jmap *)map, \
279 #endif /* CCAN_JMAP_TYPE_H */