]> git.ozlabs.org Git - ccan/blob - ccan/htable/htable_type.h
ba33f139d72a89387c73847b80bab73f47e1ce15
[ccan] / ccan / htable / htable_type.h
1 /* Licensed under LGPLv2+ - see LICENSE file for details */
2 #ifndef CCAN_HTABLE_TYPE_H
3 #define CCAN_HTABLE_TYPE_H
4 #include <ccan/htable/htable.h>
5 #include <ccan/compiler/compiler.h>
6 #include "config.h"
7
8 /**
9  * HTABLE_DEFINE_TYPE - create a set of htable ops for a type
10  * @type: a type whose pointers will be values in the hash.
11  * @keyof: a function/macro to extract a key: <keytype> @keyof(const type *elem)
12  * @hashfn: a hash function for a @key: size_t @hashfn(const <keytype> *)
13  * @eqfn: an equality function keys: bool @eqfn(const type *, const <keytype> *)
14  * @prefix: a prefix for all the functions to define (of form <name>_*)
15  *
16  * NULL values may not be placed into the hash table.
17  *
18  * This defines the type hashtable type and an iterator type:
19  *      struct <name>;
20  *      struct <name>_iter;
21  *
22  * It also defines initialization and freeing functions:
23  *      void <name>_init(struct <name> *);
24  *      void <name>_init_sized(struct <name> *, size_t);
25  *      void <name>_clear(struct <name> *);
26  *
27  * Add function only fails if we run out of memory:
28  *      bool <name>_add(struct <name> *ht, const <type> *e);
29  *
30  * Delete and delete-by key return true if it was in the set:
31  *      bool <name>_del(struct <name> *ht, const <type> *e);
32  *      bool <name>_delkey(struct <name> *ht, const <keytype> *k);
33  *
34  * Find and return the (first) matching element, or NULL:
35  *      type *<name>_get(const struct @name *ht, const <keytype> *k);
36  *
37  * Find and return all matching elements, or NULL:
38  *      type *<name>_getfirst(const struct @name *ht, const <keytype> *k,
39  *                            struct <name>_iter *i);
40  *      type *<name>_getnext(const struct @name *ht, const <keytype> *k,
41  *                           struct <name>_iter *i);
42  *
43  * Iteration over hashtable is also supported:
44  *      type *<name>_first(const struct <name> *ht, struct <name>_iter *i);
45  *      type *<name>_next(const struct <name> *ht, struct <name>_iter *i);
46  *      type *<name>_prev(const struct <name> *ht, struct <name>_iter *i);
47  *
48  * It's currently safe to iterate over a changing hashtable, but you might
49  * miss an element.  Iteration isn't very efficient, either.
50  *
51  * You can use HTABLE_INITIALIZER like so:
52  *      struct <name> ht = { HTABLE_INITIALIZER(ht.raw, <name>_hash, NULL) };
53  */
54 #define HTABLE_DEFINE_TYPE(type, keyof, hashfn, eqfn, name)             \
55         struct name { struct htable raw; };                             \
56         struct name##_iter { struct htable_iter i; };                   \
57         static inline size_t name##_hash(const void *elem, void *priv)  \
58         {                                                               \
59                 (void)priv;                                             \
60                 return hashfn(keyof((const type *)elem));               \
61         }                                                               \
62         static inline UNNEEDED void name##_init(struct name *ht)        \
63         {                                                               \
64                 htable_init(&ht->raw, name##_hash, NULL);               \
65         }                                                               \
66         static inline UNNEEDED void name##_init_sized(struct name *ht,  \
67                                                       size_t s)         \
68         {                                                               \
69                 htable_init_sized(&ht->raw, name##_hash, NULL, s);      \
70         }                                                               \
71         static inline UNNEEDED void name##_clear(struct name *ht)       \
72         {                                                               \
73                 htable_clear(&ht->raw);                                 \
74         }                                                               \
75         static inline bool name##_add(struct name *ht, const type *elem) \
76         {                                                               \
77                 return htable_add(&ht->raw, hashfn(keyof(elem)), elem); \
78         }                                                               \
79         static inline UNNEEDED bool name##_del(struct name *ht,         \
80                                                const type *elem)        \
81         {                                                               \
82                 return htable_del(&ht->raw, hashfn(keyof(elem)), elem); \
83         }                                                               \
84         static inline UNNEEDED type *name##_get(const struct name *ht,  \
85                                        const HTABLE_KTYPE(keyof, type) k) \
86         {                                                               \
87                 /* Typecheck for eqfn */                                \
88                 (void)sizeof(eqfn((const type *)NULL,                   \
89                                   keyof((const type *)NULL)));          \
90                 return htable_get(&ht->raw,                             \
91                                   hashfn(k),                            \
92                                   (bool (*)(const void *, void *))(eqfn), \
93                                   k);                                   \
94         }                                                               \
95         static inline UNNEEDED type *name##_getmatch_(const struct name *ht, \
96                                          const HTABLE_KTYPE(keyof, type) k, \
97                                          size_t h,                      \
98                                          type *v,                       \
99                                          struct name##_iter *iter)      \
100         {                                                               \
101                 while (v) {                                             \
102                         if (eqfn(v, k))                                 \
103                                 break;                                  \
104                         v = htable_nextval(&ht->raw, &iter->i, h);      \
105                 }                                                       \
106                 return v;                                               \
107         }                                                               \
108         static inline UNNEEDED type *name##_getfirst(const struct name *ht, \
109                                          const HTABLE_KTYPE(keyof, type) k, \
110                                          struct name##_iter *iter)      \
111         {                                                               \
112                 size_t h = hashfn(k);                                   \
113                 type *v = htable_firstval(&ht->raw, &iter->i, h);       \
114                 return name##_getmatch_(ht, k, h, v, iter);                     \
115         }                                                               \
116         static inline UNNEEDED type *name##_getnext(const struct name *ht, \
117                                          const HTABLE_KTYPE(keyof, type) k, \
118                                          struct name##_iter *iter)      \
119         {                                                               \
120                 size_t h = hashfn(k);                                   \
121                 type *v = htable_nextval(&ht->raw, &iter->i, h);        \
122                 return name##_getmatch_(ht, k, h, v, iter);             \
123         }                                                               \
124         static inline UNNEEDED bool name##_delkey(struct name *ht,      \
125                                          const HTABLE_KTYPE(keyof, type) k) \
126         {                                                               \
127                 type *elem = name##_get(ht, k);                         \
128                 if (elem)                                               \
129                         return name##_del(ht, elem);                    \
130                 return false;                                           \
131         }                                                               \
132         static inline UNNEEDED type *name##_first(const struct name *ht, \
133                                          struct name##_iter *iter)      \
134         {                                                               \
135                 return htable_first(&ht->raw, &iter->i);                \
136         }                                                               \
137         static inline UNNEEDED type *name##_next(const struct name *ht, \
138                                         struct name##_iter *iter)       \
139         {                                                               \
140                 return htable_next(&ht->raw, &iter->i);                 \
141         }                                                               \
142         static inline UNNEEDED type *name##_prev(const struct name *ht, \
143                                         struct name##_iter *iter)       \
144         {                                                               \
145                 return htable_prev(&ht->raw, &iter->i);                 \
146         }
147
148 #if HAVE_TYPEOF
149 #define HTABLE_KTYPE(keyof, type) typeof(keyof((const type *)NULL))
150 #else
151 #define HTABLE_KTYPE(keyof, type) void *
152 #endif
153 #endif /* CCAN_HTABLE_TYPE_H */