]> git.ozlabs.org Git - ccan/blob - ccan/htable/htable_type.h
htable: add htable_count().
[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  *      bool <name>_init_sized(struct <name> *, size_t);
25  *      void <name>_clear(struct <name> *);
26  *      bool <name>_copy(struct <name> *dst, const struct <name> *src);
27  *
28  * Count entries:
29  *      size_t <name>_count(const struct <name> *ht);
30  *
31  * Add function only fails if we run out of memory:
32  *      bool <name>_add(struct <name> *ht, const <type> *e);
33  *
34  * Delete and delete-by key return true if it was in the set:
35  *      bool <name>_del(struct <name> *ht, const <type> *e);
36  *      bool <name>_delkey(struct <name> *ht, const <keytype> *k);
37  *
38  * Find and return the (first) matching element, or NULL:
39  *      type *<name>_get(const struct @name *ht, const <keytype> *k);
40  *
41  * Find and return all matching elements, or NULL:
42  *      type *<name>_getfirst(const struct @name *ht, const <keytype> *k,
43  *                            struct <name>_iter *i);
44  *      type *<name>_getnext(const struct @name *ht, const <keytype> *k,
45  *                           struct <name>_iter *i);
46  *
47  * Iteration over hashtable is also supported:
48  *      type *<name>_first(const struct <name> *ht, struct <name>_iter *i);
49  *      type *<name>_next(const struct <name> *ht, struct <name>_iter *i);
50  *      type *<name>_prev(const struct <name> *ht, struct <name>_iter *i);
51  *
52  * It's currently safe to iterate over a changing hashtable, but you might
53  * miss an element.  Iteration isn't very efficient, either.
54  *
55  * You can use HTABLE_INITIALIZER like so:
56  *      struct <name> ht = { HTABLE_INITIALIZER(ht.raw, <name>_hash, NULL) };
57  */
58 #define HTABLE_DEFINE_TYPE(type, keyof, hashfn, eqfn, name)             \
59         struct name { struct htable raw; };                             \
60         struct name##_iter { struct htable_iter i; };                   \
61         static inline size_t name##_hash(const void *elem, void *priv)  \
62         {                                                               \
63                 (void)priv;                                             \
64                 return hashfn(keyof((const type *)elem));               \
65         }                                                               \
66         static inline UNNEEDED void name##_init(struct name *ht)        \
67         {                                                               \
68                 htable_init(&ht->raw, name##_hash, NULL);               \
69         }                                                               \
70         static inline UNNEEDED bool name##_init_sized(struct name *ht,  \
71                                                       size_t s)         \
72         {                                                               \
73                 return htable_init_sized(&ht->raw, name##_hash, NULL, s); \
74         }                                                               \
75         static inline UNNEEDED size_t name##_count(const struct name *ht) \
76         {                                                               \
77                 return htable_count(&ht->raw);                          \
78         }                                                               \
79         static inline UNNEEDED void name##_clear(struct name *ht)       \
80         {                                                               \
81                 htable_clear(&ht->raw);                                 \
82         }                                                               \
83         static inline UNNEEDED bool name##_copy(struct name *dst,       \
84                                                 const struct name *src) \
85         {                                                               \
86                 return htable_copy(&dst->raw, &src->raw);               \
87         }                                                               \
88         static inline bool name##_add(struct name *ht, const type *elem) \
89         {                                                               \
90                 return htable_add(&ht->raw, hashfn(keyof(elem)), elem); \
91         }                                                               \
92         static inline UNNEEDED bool name##_del(struct name *ht,         \
93                                                const type *elem)        \
94         {                                                               \
95                 return htable_del(&ht->raw, hashfn(keyof(elem)), elem); \
96         }                                                               \
97         static inline UNNEEDED type *name##_get(const struct name *ht,  \
98                                        const HTABLE_KTYPE(keyof, type) k) \
99         {                                                               \
100                 struct htable_iter i;                                   \
101                 size_t h = hashfn(k);                                   \
102                 void *c;                                                \
103                                                                         \
104                 for (c = htable_firstval(&ht->raw,&i,h);                \
105                      c;                                                 \
106                      c = htable_nextval(&ht->raw,&i,h)) {               \
107                         if (eqfn(c, k))                                 \
108                                 return c;                               \
109                 }                                                       \
110                 return NULL;                                            \
111         }                                                               \
112         static inline UNNEEDED type *name##_getmatch_(const struct name *ht, \
113                                          const HTABLE_KTYPE(keyof, type) k, \
114                                          size_t h,                      \
115                                          type *v,                       \
116                                          struct name##_iter *iter)      \
117         {                                                               \
118                 while (v) {                                             \
119                         if (eqfn(v, k))                                 \
120                                 break;                                  \
121                         v = htable_nextval(&ht->raw, &iter->i, h);      \
122                 }                                                       \
123                 return v;                                               \
124         }                                                               \
125         static inline UNNEEDED type *name##_getfirst(const struct name *ht, \
126                                          const HTABLE_KTYPE(keyof, type) k, \
127                                          struct name##_iter *iter)      \
128         {                                                               \
129                 size_t h = hashfn(k);                                   \
130                 type *v = htable_firstval(&ht->raw, &iter->i, h);       \
131                 return name##_getmatch_(ht, k, h, v, iter);                     \
132         }                                                               \
133         static inline UNNEEDED type *name##_getnext(const struct name *ht, \
134                                          const HTABLE_KTYPE(keyof, type) k, \
135                                          struct name##_iter *iter)      \
136         {                                                               \
137                 size_t h = hashfn(k);                                   \
138                 type *v = htable_nextval(&ht->raw, &iter->i, h);        \
139                 return name##_getmatch_(ht, k, h, v, iter);             \
140         }                                                               \
141         static inline UNNEEDED bool name##_delkey(struct name *ht,      \
142                                          const HTABLE_KTYPE(keyof, type) k) \
143         {                                                               \
144                 type *elem = name##_get(ht, k);                         \
145                 if (elem)                                               \
146                         return name##_del(ht, elem);                    \
147                 return false;                                           \
148         }                                                               \
149         static inline UNNEEDED type *name##_first(const struct name *ht, \
150                                          struct name##_iter *iter)      \
151         {                                                               \
152                 return htable_first(&ht->raw, &iter->i);                \
153         }                                                               \
154         static inline UNNEEDED type *name##_next(const struct name *ht, \
155                                         struct name##_iter *iter)       \
156         {                                                               \
157                 return htable_next(&ht->raw, &iter->i);                 \
158         }                                                               \
159         static inline UNNEEDED type *name##_prev(const struct name *ht, \
160                                         struct name##_iter *iter)       \
161         {                                                               \
162                 return htable_prev(&ht->raw, &iter->i);                 \
163         }
164
165 #if HAVE_TYPEOF
166 #define HTABLE_KTYPE(keyof, type) typeof(keyof((const type *)NULL))
167 #else
168 /* Assumes keys are a pointer: if not, override. */
169 #ifndef HTABLE_KTYPE
170 #define HTABLE_KTYPE(keyof, type) void *
171 #endif
172 #endif
173 #endif /* CCAN_HTABLE_TYPE_H */