Merge branch 'judy'
[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  *      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);
19  *
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);
26  *
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,
33  *                              size_t invalid);
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,
37  *                              size_t invalid);
38  *      size_t jmap_@name_prev(const struct jmap_@name *map,
39  *                              size_t prev, size_t invalid);
40  *
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,
46  *                                 size_t *index);
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);
50  */
51 #define JMAP_DEFINE_UINTIDX_TYPE(type, name)                            \
52 struct jmap_##name;                                                     \
53 static inline struct jmap_##name *jmap_##name##_new(void)               \
54 {                                                                       \
55         return (struct jmap_##name *)jmap_new();                        \
56 }                                                                       \
57 static inline void jmap_##name##_free(const struct jmap_##name *map)    \
58 {                                                                       \
59         jmap_free((const struct jmap *)map);                            \
60 }                                                                       \
61 static inline const char *jmap_##name##_error(struct jmap_##name *map)  \
62 {                                                                       \
63         return jmap_error((struct jmap *)map);                          \
64 }                                                                       \
65 static inline bool jmap_##name##_add(struct jmap_##name *map,           \
66                                      size_t index, const type *value)   \
67 {                                                                       \
68         return jmap_add((struct jmap *)map, index, (size_t)value);      \
69 }                                                                       \
70 static inline bool jmap_##name##_set(const struct jmap_##name *map,     \
71                                      size_t index, const type *value)   \
72 {                                                                       \
73         return jmap_set((const struct jmap *)map, index, (size_t)value); \
74 }                                                                       \
75 static inline bool jmap_##name##_del(struct jmap_##name *map, size_t index) \
76 {                                                                       \
77         return jmap_del((struct jmap *)map, index);                     \
78 }                                                                       \
79 static inline bool jmap_##name##_test(const struct jmap_##name *map,  \
80                                       size_t index)                     \
81 {                                                                       \
82         return jmap_test((const struct jmap *)map, (size_t)index);      \
83 }                                                                       \
84 static inline type *jmap_##name##_get(const struct jmap_##name *map,    \
85                                       size_t index)                     \
86 {                                                                       \
87         return (type *)jmap_get((const struct jmap *)map, index, 0);    \
88 }                                                                       \
89 static inline size_t jmap_##name##_popcount(const struct jmap_##name *map, \
90                                             size_t start, size_t end_incl) \
91 {                                                                       \
92         return jmap_popcount((const struct jmap *)map, start, end_incl); \
93 }                                                                       \
94 static inline size_t jmap_##name##_nth(const struct jmap_##name *map,   \
95                                        size_t n, size_t invalid)        \
96 {                                                                       \
97         return jmap_nth((const struct jmap *)map, n, invalid);          \
98 }                                                                       \
99 static inline size_t jmap_##name##_first(const struct jmap_##name *map, \
100                                         size_t invalid)                 \
101 {                                                                       \
102         return jmap_first((const struct jmap *)map, invalid);           \
103 }                                                                       \
104 static inline size_t jmap_##name##_next(const struct jmap_##name *map,  \
105                                         size_t prev, size_t invalid)    \
106 {                                                                       \
107         return jmap_next((const struct jmap *)map, prev, invalid);      \
108 }                                                                       \
109 static inline size_t jmap_##name##_last(const struct jmap_##name *map,  \
110                                         size_t invalid)                 \
111 {                                                                       \
112         return jmap_last((const struct jmap *)map, invalid);            \
113 }                                                                       \
114 static inline size_t jmap_##name##_prev(const struct jmap_##name *map,  \
115                                         size_t prev, size_t invalid)    \
116 {                                                                       \
117         return jmap_prev((const struct jmap *)map, prev, invalid);      \
118 }                                                                       \
119 static inline type **jmap_##name##_getval(const struct jmap_##name *map, \
120                                           size_t index)                 \
121 {                                                                       \
122         return (type **)jmap_getval((struct jmap *)map, index);         \
123 }                                                                       \
124 static inline void jmap_##name##_putval(struct jmap_##name *map,        \
125                                           type ***p)                    \
126 {                                                                       \
127         return jmap_putval((struct jmap *)map, (size_t **)p);           \
128 }                                                                       \
129 static inline type **jmap_##name##_nthval(struct jmap_##name *map,      \
130                                           size_t n, size_t *index)      \
131 {                                                                       \
132         return (type **)jmap_nthval((struct jmap *)map, n, index);      \
133 }                                                                       \
134 static inline type **jmap_##name##_firstval(const struct jmap_##name *map, \
135                                             size_t *index)              \
136 {                                                                       \
137         return (type **)jmap_firstval((const struct jmap *)map, index); \
138 }                                                                       \
139 static inline type **jmap_##name##_nextval(const struct jmap_##name *map, \
140                                            size_t *index)               \
141 {                                                                       \
142         return (type **)jmap_nextval((const struct jmap *)map, index);  \
143 }                                                                       \
144 static inline type **jmap_##name##_lastval(const struct jmap_##name *map, \
145                                             size_t *index)              \
146 {                                                                       \
147         return (type **)jmap_lastval((const struct jmap *)map, index);  \
148 }                                                                       \
149 static inline type **jmap_##name##_prevval(const struct jmap_##name *map, \
150                                            size_t *index)               \
151 {                                                                       \
152         return (type **)jmap_prevval((const struct jmap *)map, index);  \
153 }
154
155 /**
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>_*)
160  *
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).
167  *
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
170  * traversal.
171  *
172  * The following wrapper functions are defined; each one is the same as
173  * the jmap.h generic equivalent:
174  *
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);
178  *
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);
185  *
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);
191  *
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);
199  */
200 #define JMAP_DEFINE_PTRIDX_TYPE(itype, type, name)                      \
201 struct jmap_##name;                                                     \
202 static inline struct jmap_##name *jmap_##name##_new(void)               \
203 {                                                                       \
204         return (struct jmap_##name *)jmap_new();                        \
205 }                                                                       \
206 static inline void jmap_##name##_free(const struct jmap_##name *map)    \
207 {                                                                       \
208         jmap_free((const struct jmap *)map);                            \
209 }                                                                       \
210 static inline const char *jmap_##name##_error(struct jmap_##name *map)  \
211 {                                                                       \
212         return jmap_error((struct jmap *)map);                          \
213 }                                                                       \
214 static inline bool jmap_##name##_add(struct jmap_##name *map,           \
215                                      const itype *index, const type *value) \
216 {                                                                       \
217         return jmap_add((struct jmap *)map, (size_t)index,              \
218                         (size_t)value);                                 \
219 }                                                                       \
220 static inline bool jmap_##name##_set(const struct jmap_##name *map,     \
221                                      const itype *index, const type *value) \
222 {                                                                       \
223         return jmap_set((const struct jmap *)map, (size_t)index,        \
224                         (size_t)value);                                 \
225 }                                                                       \
226 static inline bool jmap_##name##_del(struct jmap_##name *map,           \
227                                      const itype *index)                \
228 {                                                                       \
229         return jmap_del((struct jmap *)map, (size_t)index);             \
230 }                                                                       \
231 static inline bool jmap_##name##_test(const struct jmap_##name *map,    \
232                                       const itype *index)               \
233 {                                                                       \
234         return jmap_test((const struct jmap *)map, (size_t)index);      \
235 }                                                                       \
236 static inline type *jmap_##name##_get(const struct jmap_##name *map,    \
237                                       const itype *index)               \
238 {                                                                       \
239         return (type *)jmap_get((const struct jmap *)map, (size_t)index, 0); \
240 }                                                                       \
241 static inline size_t jmap_##name##_count(const struct jmap_##name *map) \
242 {                                                                       \
243         return jmap_popcount((const struct jmap *)map, 0, -1);          \
244 }                                                                       \
245 static inline itype *jmap_##name##_first(const struct jmap_##name *map) \
246 {                                                                       \
247         return (itype *)jmap_first((const struct jmap *)map, 0);        \
248 }                                                                       \
249 static inline itype *jmap_##name##_next(const struct jmap_##name *map,  \
250                                         const itype *prev)              \
251 {                                                                       \
252         return (itype *)jmap_next((const struct jmap *)map, (size_t)prev, 0); \
253 }                                                                       \
254 static inline type **jmap_##name##_getval(const struct jmap_##name *map, \
255                                           const itype *index)           \
256 {                                                                       \
257         return (type **)jmap_getval((struct jmap *)map, (size_t)index); \
258 }                                                                       \
259 static inline void jmap_##name##_putval(struct jmap_##name *map,        \
260                                         type ***p)                      \
261 {                                                                       \
262         return jmap_putval((struct jmap *)map, (size_t **)p);           \
263 }                                                                       \
264 static inline type **jmap_##name##_firstval(const struct jmap_##name *map, \
265                                             itype **index)              \
266 {                                                                       \
267         return (type **)jmap_firstval((const struct jmap *)map,         \
268                                       (size_t *)index);                 \
269 }                                                                       \
270 static inline type **jmap_##name##_nextval(const struct jmap_##name *map, \
271                                            itype **index)               \
272 {                                                                       \
273         return (type **)jmap_nextval((const struct jmap *)map,          \
274                                      (size_t *)index);                  \
275 }
276 #endif /* CCAN_JMAP_TYPE_H */