tap: spelling fix
[ccan] / ccan / jmap / jmap_type.h
1 #ifndef CCAN_JMAP_TYPE_H
2 #define CCAN_JMAP_TYPE_H
3 #include <ccan/jmap/jmap.h>
4
5 /**
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>_*)
9  *
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.
12  *
13  * The following wrapper functions are defined; each one is the same as
14  * the jmap.h generic equivalent except where noted:
15  *
16  *      // Creating, errors and freeing.
17  *      struct jmap_@name *jmap_@name_new(void);
18  *      void jmap_@name_free(const struct jmap_@name *map);
19  *      const char *jmap_@name_error(struct jmap_@name *map);
20  *
21  *      // Add, set, delete, test and get.
22  *      bool jmap_@name_add(struct jmap_@name *map,
23  *                          unsigned long idx, const type *value);
24  *      bool jmap_@name_set(const struct jmap_@name *map,
25  *                         unsigned long idx, const type *value);
26  *      bool jmap_@name_del(struct jmap_@name *map, unsigned long idx);
27  *      bool jmap_@name_test(const struct jmap_@name *map, unsigned long idx);
28  *      type *jmap_@name_get(const struct jmap_@name *map, unsigned long idx);
29  *
30  *      // Counting and iteration.
31  *      unsigned long jmap_@name_popcount(const struct jmap_@name *map,
32  *                                        unsigned long start,
33  *                                        unsigned long end_incl);
34  *      unsigned long jmap_@name_nth(const struct jmap_@name *map,
35  *                                   unsigned long n, unsigned long invalid);
36  *      unsigned long jmap_@name_first(const struct jmap_@name *map,
37  *                                     unsigned long invalid);
38  *      unsigned long jmap_@name_next(const struct jmap_@name *map,
39  *                                    unsigned long prev,
40  *                                    unsigned long invalid);
41  *      unsigned long jmap_@name_last(const struct jmap_@name *map,
42  *                                    unsigned long invalid);
43  *      unsigned long jmap_@name_prev(const struct jmap_@name *map,
44  *                                    unsigned long prev,
45  *                                    unsigned long invalid);
46  *
47  *      // Get pointers to values to use.
48  *      type **jmap_@name_getval(const struct jmap_@name *map,
49  *                               unsigned long idx);
50  *      void jmap_@name_putval(struct jmap_@name *map, type ***p);
51  *      type **jmap_@name_nthval(struct jmap_@name *map,
52  *                               unsigned long n, unsigned long *idx);
53  *      type **jmap_@name_firstval(const struct jmap_@name *map,
54  *                                 unsigned long *idx);
55  *      type **jmap_@name_nextval(const struct jmap_@name *map,
56  *                               unsigned long *idx);
57  *      type **jmap_@name_lastval(const struct jmap_@name *map,
58  *                                unsigned long *idx);
59  *      type **jmap_@name_prevval(const struct jmap_@name *map,
60  *                                unsigned long *idx);
61  */
62 #define JMAP_DEFINE_UINTIDX_TYPE(type, name)                            \
63 struct jmap_##name;                                                     \
64 static inline struct jmap_##name *jmap_##name##_new(void)               \
65 {                                                                       \
66         return (struct jmap_##name *)jmap_new();                        \
67 }                                                                       \
68 static inline void jmap_##name##_free(const struct jmap_##name *map)    \
69 {                                                                       \
70         jmap_free((const struct jmap *)map);                            \
71 }                                                                       \
72 static inline const char *jmap_##name##_error(struct jmap_##name *map)  \
73 {                                                                       \
74         return jmap_error((struct jmap *)map);                          \
75 }                                                                       \
76 static inline bool jmap_##name##_add(struct jmap_##name *map,           \
77                                      unsigned long idx, const type *value) \
78 {                                                                       \
79         return jmap_add((struct jmap *)map, idx, (unsigned long)value); \
80 }                                                                       \
81 static inline bool jmap_##name##_set(const struct jmap_##name *map,     \
82                                      unsigned long idx, const type *value) \
83 {                                                                       \
84         return jmap_set((const struct jmap *)map, idx, (unsigned long)value); \
85 }                                                                       \
86 static inline bool jmap_##name##_del(struct jmap_##name *map,           \
87                                      unsigned long idx)                 \
88 {                                                                       \
89         return jmap_del((struct jmap *)map, idx);                       \
90 }                                                                       \
91 static inline bool jmap_##name##_test(const struct jmap_##name *map,    \
92                                       unsigned long idx)                \
93 {                                                                       \
94         return jmap_test((const struct jmap *)map, (unsigned long)idx); \
95 }                                                                       \
96 static inline type *jmap_##name##_get(const struct jmap_##name *map,    \
97                                       unsigned long idx)                \
98 {                                                                       \
99         return (type *)jmap_get((const struct jmap *)map, idx, 0);      \
100 }                                                                       \
101 static inline unsigned long                                             \
102 jmap_##name##_popcount(const struct jmap_##name *map,                   \
103                        unsigned long start, unsigned long end_incl)     \
104 {                                                                       \
105         return jmap_popcount((const struct jmap *)map, start, end_incl); \
106 }                                                                       \
107 static inline unsigned long jmap_##name##_nth(const struct jmap_##name *map, \
108                                               unsigned long n,          \
109                                               unsigned long invalid)    \
110 {                                                                       \
111         return jmap_nth((const struct jmap *)map, n, invalid);          \
112 }                                                                       \
113 static inline unsigned long                                             \
114 jmap_##name##_first(const struct jmap_##name *map,                      \
115                     unsigned long invalid)                              \
116 {                                                                       \
117         return jmap_first((const struct jmap *)map, invalid);           \
118 }                                                                       \
119 static inline unsigned long                                             \
120 jmap_##name##_next(const struct jmap_##name *map,                       \
121                    unsigned long prev, unsigned long invalid)           \
122 {                                                                       \
123         return jmap_next((const struct jmap *)map, prev, invalid);      \
124 }                                                                       \
125 static inline unsigned long                                             \
126 jmap_##name##_last(const struct jmap_##name *map,                       \
127                    unsigned long invalid)                               \
128 {                                                                       \
129         return jmap_last((const struct jmap *)map, invalid);            \
130 }                                                                       \
131 static inline unsigned long                                             \
132 jmap_##name##_prev(const struct jmap_##name *map,                       \
133                    unsigned long prev, unsigned long invalid)           \
134 {                                                                       \
135         return jmap_prev((const struct jmap *)map, prev, invalid);      \
136 }                                                                       \
137 static inline type **jmap_##name##_getval(const struct jmap_##name *map, \
138                                           unsigned long idx)            \
139 {                                                                       \
140         return (type **)jmap_getval((struct jmap *)map, idx);           \
141 }                                                                       \
142 static inline void jmap_##name##_putval(struct jmap_##name *map,        \
143                                           type ***p)                    \
144 {                                                                       \
145         return jmap_putval((struct jmap *)map, (unsigned long **)p);    \
146 }                                                                       \
147 static inline type **jmap_##name##_nthval(struct jmap_##name *map,      \
148                                           unsigned long n,              \
149                                           unsigned long *idx)           \
150 {                                                                       \
151         return (type **)jmap_nthval((struct jmap *)map, n, idx);        \
152 }                                                                       \
153 static inline type **jmap_##name##_firstval(const struct jmap_##name *map, \
154                                             unsigned long *idx)         \
155 {                                                                       \
156         return (type **)jmap_firstval((const struct jmap *)map, idx); \
157 }                                                                       \
158 static inline type **jmap_##name##_nextval(const struct jmap_##name *map, \
159                                            unsigned long *idx)          \
160 {                                                                       \
161         return (type **)jmap_nextval((const struct jmap *)map, idx);    \
162 }                                                                       \
163 static inline type **jmap_##name##_lastval(const struct jmap_##name *map, \
164                                             unsigned long *idx)         \
165 {                                                                       \
166         return (type **)jmap_lastval((const struct jmap *)map, idx);    \
167 }                                                                       \
168 static inline type **jmap_##name##_prevval(const struct jmap_##name *map, \
169                                            unsigned long *idx)          \
170 {                                                                       \
171         return (type **)jmap_prevval((const struct jmap *)map, idx);    \
172 }
173
174 /**
175  * JMAP_DEFINE_PTRIDX_TYPE - create a map of jmap ops for ptr->ptr map
176  * @itype: a type whose pointers will idx into the map.
177  * @type: a type whose pointers will be values in the map.
178  * @name: a name for all the functions to define (of form jmap_<name>_*)
179  *
180  * This macro defines a map of inline functions for typesafe and
181  * convenient usage of a pointer-idxed Judy map of pointers.  It is
182  * assumed that a NULL pointer is never an idx in the map, as
183  * various functions return NULL for "invalid idx".  Similarly,
184  * jmap_@name_get will return NULL if an idx isn't valid, so NULL indices
185  * are not recommended (though you can tell using jmap_@name_test).
186  *
187  * Since the ordering is by idx pointer value, it's generally quite useless.
188  * Thus we don't define order-specific functions, except first/next for
189  * traversal.
190  *
191  * The following wrapper functions are defined; each one is the same as
192  * the jmap.h generic equivalent:
193  *
194  *      struct jmap_@name *jmap_@name_new(void);
195  *      void jmap_@name_free(const struct jmap_@name *map);
196  *      const char *jmap_@name_error(struct jmap_@name *map);
197  *
198  *      bool jmap_@name_add(const struct jmap_@name *map,
199  *                          const itype *idx, const type *value);
200  *      bool jmap_@name_set(const struct jmap_@name *map,
201  *                         const itype *idx, const type *value);
202  *      bool jmap_@name_del(struct jmap_@name *map, const itype *idx);
203  *      bool jmap_@name_test(const struct jmap_@name *map, const itype *idx);
204  *
205  *      type *jmap_@name_get(const struct jmap_@name *map, const itype *idx);
206  *      itype *jmap_@name_count(const struct jmap_@name *map);
207  *      itype *jmap_@name_first(const struct jmap_@name *map);
208  *      itype *jmap_@name_next(const struct jmap_@name *map,
209  *                             const itype *prev);
210  *
211  *      type **jmap_@name_getval(const struct jmap_@name *map,
212  *                               const itype *idx);
213  *      void jmap_@name_putval(struct jmap_@name *map, type ***p);
214  *      type **jmap_@name_firstval(const struct jmap_@name *map,
215  *                                 const itype **idx);
216  *      type **jmap_@name_nextval(const struct jmap_@name *map,
217  *                                const itype **idx);
218  */
219 #define JMAP_DEFINE_PTRIDX_TYPE(itype, type, name)                      \
220 struct jmap_##name;                                                     \
221 static inline struct jmap_##name *jmap_##name##_new(void)               \
222 {                                                                       \
223         return (struct jmap_##name *)jmap_new();                        \
224 }                                                                       \
225 static inline void jmap_##name##_free(const struct jmap_##name *map)    \
226 {                                                                       \
227         jmap_free((const struct jmap *)map);                            \
228 }                                                                       \
229 static inline const char *jmap_##name##_error(struct jmap_##name *map)  \
230 {                                                                       \
231         return jmap_error((struct jmap *)map);                          \
232 }                                                                       \
233 static inline bool jmap_##name##_add(struct jmap_##name *map,           \
234                                      const itype *idx, const type *value) \
235 {                                                                       \
236         return jmap_add((struct jmap *)map, (unsigned long)idx,         \
237                         (unsigned long)value);                          \
238 }                                                                       \
239 static inline bool jmap_##name##_set(const struct jmap_##name *map,     \
240                                      const itype *idx, const type *value) \
241 {                                                                       \
242         return jmap_set((const struct jmap *)map, (unsigned long)idx,   \
243                         (unsigned long)value);                          \
244 }                                                                       \
245 static inline bool jmap_##name##_del(struct jmap_##name *map,           \
246                                      const itype *idx)                  \
247 {                                                                       \
248         return jmap_del((struct jmap *)map, (unsigned long)idx);        \
249 }                                                                       \
250 static inline bool jmap_##name##_test(const struct jmap_##name *map,    \
251                                       const itype *idx)                 \
252 {                                                                       \
253         return jmap_test((const struct jmap *)map, (unsigned long)idx); \
254 }                                                                       \
255 static inline type *jmap_##name##_get(const struct jmap_##name *map,    \
256                                       const itype *idx)                 \
257 {                                                                       \
258         return (type *)jmap_get((const struct jmap *)map,               \
259                                 (unsigned long)idx, 0);                 \
260 }                                                                       \
261 static inline unsigned long                                             \
262 jmap_##name##_count(const struct jmap_##name *map)                      \
263 {                                                                       \
264         return jmap_popcount((const struct jmap *)map, 0, -1);          \
265 }                                                                       \
266 static inline itype *jmap_##name##_first(const struct jmap_##name *map) \
267 {                                                                       \
268         return (itype *)jmap_first((const struct jmap *)map, 0);        \
269 }                                                                       \
270 static inline itype *jmap_##name##_next(const struct jmap_##name *map,  \
271                                         const itype *prev)              \
272 {                                                                       \
273         return (itype *)jmap_next((const struct jmap *)map,             \
274                                   (unsigned long)prev, 0);              \
275 }                                                                       \
276 static inline type **jmap_##name##_getval(const struct jmap_##name *map, \
277                                           const itype *idx)             \
278 {                                                                       \
279         return (type **)jmap_getval((struct jmap *)map,                 \
280                                     (unsigned long)idx);                \
281 }                                                                       \
282 static inline void jmap_##name##_putval(struct jmap_##name *map,        \
283                                         type ***p)                      \
284 {                                                                       \
285         return jmap_putval((struct jmap *)map, (unsigned long **)p);    \
286 }                                                                       \
287 static inline type **jmap_##name##_firstval(const struct jmap_##name *map, \
288                                             itype **idx)                \
289 {                                                                       \
290         unsigned long i;                                                \
291         type **ret;                                                     \
292         ret = (type **)jmap_firstval((const struct jmap *)map, &i);     \
293         *idx = (void *)i;                                               \
294         return ret;                                                     \
295 }                                                                       \
296 static inline type **jmap_##name##_nextval(const struct jmap_##name *map, \
297                                            itype **idx)         \
298 {                                                                       \
299         return (type **)jmap_nextval((const struct jmap *)map,          \
300                                      (unsigned long *)idx);             \
301 }
302 #endif /* CCAN_JMAP_TYPE_H */