tdb2: unify tdb1_traverse into tdb_traverse
[ccan] / ccan / jmap / jmap_type.h
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>
5
6 /**
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>_*)
10  *
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.
13  *
14  * The following wrapper functions are defined; each one is the same as
15  * the jmap.h generic equivalent except where noted:
16  *
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);
21  *
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);
30  *
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,
40  *                                    unsigned long prev,
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,
45  *                                    unsigned long prev,
46  *                                    unsigned long invalid);
47  *
48  *      // Get pointers to values to use.
49  *      type **jmap_@name_getval(const struct jmap_@name *map,
50  *                               unsigned long idx);
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);
62  */
63 #define JMAP_DEFINE_UINTIDX_TYPE(type, name)                            \
64 struct jmap_##name;                                                     \
65 static inline struct jmap_##name *jmap_##name##_new(void)               \
66 {                                                                       \
67         return (struct jmap_##name *)jmap_new();                        \
68 }                                                                       \
69 static inline void jmap_##name##_free(const struct jmap_##name *map)    \
70 {                                                                       \
71         jmap_free((const struct jmap *)map);                            \
72 }                                                                       \
73 static inline const char *jmap_##name##_error(struct jmap_##name *map)  \
74 {                                                                       \
75         return jmap_error((struct jmap *)map);                          \
76 }                                                                       \
77 static inline bool jmap_##name##_add(struct jmap_##name *map,           \
78                                      unsigned long idx, const type *value) \
79 {                                                                       \
80         return jmap_add((struct jmap *)map, idx, (unsigned long)value); \
81 }                                                                       \
82 static inline bool jmap_##name##_set(const struct jmap_##name *map,     \
83                                      unsigned long idx, const type *value) \
84 {                                                                       \
85         return jmap_set((const struct jmap *)map, idx, (unsigned long)value); \
86 }                                                                       \
87 static inline bool jmap_##name##_del(struct jmap_##name *map,           \
88                                      unsigned long idx)                 \
89 {                                                                       \
90         return jmap_del((struct jmap *)map, idx);                       \
91 }                                                                       \
92 static inline bool jmap_##name##_test(const struct jmap_##name *map,    \
93                                       unsigned long idx)                \
94 {                                                                       \
95         return jmap_test((const struct jmap *)map, (unsigned long)idx); \
96 }                                                                       \
97 static inline type *jmap_##name##_get(const struct jmap_##name *map,    \
98                                       unsigned long idx)                \
99 {                                                                       \
100         return (type *)jmap_get((const struct jmap *)map, idx, 0);      \
101 }                                                                       \
102 static inline unsigned long                                             \
103 jmap_##name##_popcount(const struct jmap_##name *map,                   \
104                        unsigned long start, unsigned long end_incl)     \
105 {                                                                       \
106         return jmap_popcount((const struct jmap *)map, start, end_incl); \
107 }                                                                       \
108 static inline unsigned long jmap_##name##_nth(const struct jmap_##name *map, \
109                                               unsigned long n,          \
110                                               unsigned long invalid)    \
111 {                                                                       \
112         return jmap_nth((const struct jmap *)map, n, invalid);          \
113 }                                                                       \
114 static inline unsigned long                                             \
115 jmap_##name##_first(const struct jmap_##name *map,                      \
116                     unsigned long invalid)                              \
117 {                                                                       \
118         return jmap_first((const struct jmap *)map, invalid);           \
119 }                                                                       \
120 static inline unsigned long                                             \
121 jmap_##name##_next(const struct jmap_##name *map,                       \
122                    unsigned long prev, unsigned long invalid)           \
123 {                                                                       \
124         return jmap_next((const struct jmap *)map, prev, invalid);      \
125 }                                                                       \
126 static inline unsigned long                                             \
127 jmap_##name##_last(const struct jmap_##name *map,                       \
128                    unsigned long invalid)                               \
129 {                                                                       \
130         return jmap_last((const struct jmap *)map, invalid);            \
131 }                                                                       \
132 static inline unsigned long                                             \
133 jmap_##name##_prev(const struct jmap_##name *map,                       \
134                    unsigned long prev, unsigned long invalid)           \
135 {                                                                       \
136         return jmap_prev((const struct jmap *)map, prev, invalid);      \
137 }                                                                       \
138 static inline type **jmap_##name##_getval(const struct jmap_##name *map, \
139                                           unsigned long idx)            \
140 {                                                                       \
141         return (type **)jmap_getval((struct jmap *)map, idx);           \
142 }                                                                       \
143 static inline void jmap_##name##_putval(struct jmap_##name *map,        \
144                                           type ***p)                    \
145 {                                                                       \
146         return jmap_putval((struct jmap *)map, (unsigned long **)p);    \
147 }                                                                       \
148 static inline type **jmap_##name##_nthval(struct jmap_##name *map,      \
149                                           unsigned long n,              \
150                                           unsigned long *idx)           \
151 {                                                                       \
152         return (type **)jmap_nthval((struct jmap *)map, n, idx);        \
153 }                                                                       \
154 static inline type **jmap_##name##_firstval(const struct jmap_##name *map, \
155                                             unsigned long *idx)         \
156 {                                                                       \
157         return (type **)jmap_firstval((const struct jmap *)map, idx); \
158 }                                                                       \
159 static inline type **jmap_##name##_nextval(const struct jmap_##name *map, \
160                                            unsigned long *idx)          \
161 {                                                                       \
162         return (type **)jmap_nextval((const struct jmap *)map, idx);    \
163 }                                                                       \
164 static inline type **jmap_##name##_lastval(const struct jmap_##name *map, \
165                                             unsigned long *idx)         \
166 {                                                                       \
167         return (type **)jmap_lastval((const struct jmap *)map, idx);    \
168 }                                                                       \
169 static inline type **jmap_##name##_prevval(const struct jmap_##name *map, \
170                                            unsigned long *idx)          \
171 {                                                                       \
172         return (type **)jmap_prevval((const struct jmap *)map, idx);    \
173 }
174
175 /**
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>_*)
180  *
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).
187  *
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
190  * traversal.
191  *
192  * The following wrapper functions are defined; each one is the same as
193  * the jmap.h generic equivalent:
194  *
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);
198  *
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);
205  *
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);
211  *
212  *      type **jmap_@name_getval(const struct jmap_@name *map,
213  *                               const itype *idx);
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);
219  */
220 #define JMAP_DEFINE_PTRIDX_TYPE(itype, type, name)                      \
221 struct jmap_##name;                                                     \
222 static inline struct jmap_##name *jmap_##name##_new(void)               \
223 {                                                                       \
224         return (struct jmap_##name *)jmap_new();                        \
225 }                                                                       \
226 static inline void jmap_##name##_free(const struct jmap_##name *map)    \
227 {                                                                       \
228         jmap_free((const struct jmap *)map);                            \
229 }                                                                       \
230 static inline const char *jmap_##name##_error(struct jmap_##name *map)  \
231 {                                                                       \
232         return jmap_error((struct jmap *)map);                          \
233 }                                                                       \
234 static inline bool jmap_##name##_add(struct jmap_##name *map,           \
235                                      const itype *idx, const type *value) \
236 {                                                                       \
237         return jmap_add((struct jmap *)map, (unsigned long)idx,         \
238                         (unsigned long)value);                          \
239 }                                                                       \
240 static inline bool jmap_##name##_set(const struct jmap_##name *map,     \
241                                      const itype *idx, const type *value) \
242 {                                                                       \
243         return jmap_set((const struct jmap *)map, (unsigned long)idx,   \
244                         (unsigned long)value);                          \
245 }                                                                       \
246 static inline bool jmap_##name##_del(struct jmap_##name *map,           \
247                                      const itype *idx)                  \
248 {                                                                       \
249         return jmap_del((struct jmap *)map, (unsigned long)idx);        \
250 }                                                                       \
251 static inline bool jmap_##name##_test(const struct jmap_##name *map,    \
252                                       const itype *idx)                 \
253 {                                                                       \
254         return jmap_test((const struct jmap *)map, (unsigned long)idx); \
255 }                                                                       \
256 static inline type *jmap_##name##_get(const struct jmap_##name *map,    \
257                                       const itype *idx)                 \
258 {                                                                       \
259         return (type *)jmap_get((const struct jmap *)map,               \
260                                 (unsigned long)idx, 0);                 \
261 }                                                                       \
262 static inline unsigned long                                             \
263 jmap_##name##_count(const struct jmap_##name *map)                      \
264 {                                                                       \
265         return jmap_popcount((const struct jmap *)map, 0, -1);          \
266 }                                                                       \
267 static inline itype *jmap_##name##_first(const struct jmap_##name *map) \
268 {                                                                       \
269         return (itype *)jmap_first((const struct jmap *)map, 0);        \
270 }                                                                       \
271 static inline itype *jmap_##name##_next(const struct jmap_##name *map,  \
272                                         const itype *prev)              \
273 {                                                                       \
274         return (itype *)jmap_next((const struct jmap *)map,             \
275                                   (unsigned long)prev, 0);              \
276 }                                                                       \
277 static inline type **jmap_##name##_getval(const struct jmap_##name *map, \
278                                           const itype *idx)             \
279 {                                                                       \
280         return (type **)jmap_getval((struct jmap *)map,                 \
281                                     (unsigned long)idx);                \
282 }                                                                       \
283 static inline void jmap_##name##_putval(struct jmap_##name *map,        \
284                                         type ***p)                      \
285 {                                                                       \
286         return jmap_putval((struct jmap *)map, (unsigned long **)p);    \
287 }                                                                       \
288 static inline type **jmap_##name##_firstval(const struct jmap_##name *map, \
289                                             itype **idx)                \
290 {                                                                       \
291         unsigned long i;                                                \
292         type **ret;                                                     \
293         ret = (type **)jmap_firstval((const struct jmap *)map, &i);     \
294         *idx = (void *)i;                                               \
295         return ret;                                                     \
296 }                                                                       \
297 static inline type **jmap_##name##_nextval(const struct jmap_##name *map, \
298                                            itype **idx)         \
299 {                                                                       \
300         return (type **)jmap_nextval((const struct jmap *)map,          \
301                                      (unsigned long *)idx);             \
302 }
303 #endif /* CCAN_JMAP_TYPE_H */