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>
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>_*)
16 * NULL values may not be placed into the hash table.
18 * This defines the type hashtable type and an iterator type:
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);
29 * size_t <name>_count(const struct <name> *ht);
31 * Add function only fails if we run out of memory:
32 * bool <name>_add(struct <name> *ht, const <type> *e);
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);
38 * Find and return the (first) matching element, or NULL:
39 * type *<name>_get(const struct @name *ht, const <keytype> *k);
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);
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 * type *<name>_pick(const struct <name> *ht, size_t seed,
52 * struct <name>_iter *i);
53 * It's currently safe to iterate over a changing hashtable, but you might
54 * miss an element. Iteration isn't very efficient, either.
56 * You can use HTABLE_INITIALIZER like so:
57 * struct <name> ht = { HTABLE_INITIALIZER(ht.raw, <name>_hash, NULL) };
59 #define HTABLE_DEFINE_TYPE(type, keyof, hashfn, eqfn, name) \
60 struct name { struct htable raw; }; \
61 struct name##_iter { struct htable_iter i; }; \
62 static inline size_t name##_hash(const void *elem, void *priv) \
65 return hashfn(keyof((const type *)elem)); \
67 static inline UNNEEDED void name##_init(struct name *ht) \
69 htable_init(&ht->raw, name##_hash, NULL); \
71 static inline UNNEEDED bool name##_init_sized(struct name *ht, \
74 return htable_init_sized(&ht->raw, name##_hash, NULL, s); \
76 static inline UNNEEDED size_t name##_count(const struct name *ht) \
78 return htable_count(&ht->raw); \
80 static inline UNNEEDED void name##_clear(struct name *ht) \
82 htable_clear(&ht->raw); \
84 static inline UNNEEDED bool name##_copy(struct name *dst, \
85 const struct name *src) \
87 return htable_copy(&dst->raw, &src->raw); \
89 static inline bool name##_add(struct name *ht, const type *elem) \
91 return htable_add(&ht->raw, hashfn(keyof(elem)), elem); \
93 static inline UNNEEDED bool name##_del(struct name *ht, \
96 return htable_del(&ht->raw, hashfn(keyof(elem)), elem); \
98 static inline UNNEEDED type *name##_get(const struct name *ht, \
99 const HTABLE_KTYPE(keyof, type) k) \
101 struct htable_iter i; \
102 size_t h = hashfn(k); \
105 for (c = htable_firstval(&ht->raw,&i,h); \
107 c = htable_nextval(&ht->raw,&i,h)) { \
113 static inline UNNEEDED type *name##_getmatch_(const struct name *ht, \
114 const HTABLE_KTYPE(keyof, type) k, \
117 struct name##_iter *iter) \
122 v = htable_nextval(&ht->raw, &iter->i, h); \
126 static inline UNNEEDED type *name##_getfirst(const struct name *ht, \
127 const HTABLE_KTYPE(keyof, type) k, \
128 struct name##_iter *iter) \
130 size_t h = hashfn(k); \
131 type *v = htable_firstval(&ht->raw, &iter->i, h); \
132 return name##_getmatch_(ht, k, h, v, iter); \
134 static inline UNNEEDED type *name##_getnext(const struct name *ht, \
135 const HTABLE_KTYPE(keyof, type) k, \
136 struct name##_iter *iter) \
138 size_t h = hashfn(k); \
139 type *v = htable_nextval(&ht->raw, &iter->i, h); \
140 return name##_getmatch_(ht, k, h, v, iter); \
142 static inline UNNEEDED bool name##_delkey(struct name *ht, \
143 const HTABLE_KTYPE(keyof, type) k) \
145 type *elem = name##_get(ht, k); \
147 return name##_del(ht, elem); \
150 static inline UNNEEDED type *name##_pick(const struct name *ht, \
152 struct name##_iter *iter) \
154 /* Note &iter->i == NULL iff iter is NULL */ \
155 return htable_pick(&ht->raw, seed, &iter->i); \
157 static inline UNNEEDED type *name##_first(const struct name *ht, \
158 struct name##_iter *iter) \
160 return htable_first(&ht->raw, &iter->i); \
162 static inline UNNEEDED type *name##_next(const struct name *ht, \
163 struct name##_iter *iter) \
165 return htable_next(&ht->raw, &iter->i); \
167 static inline UNNEEDED type *name##_prev(const struct name *ht, \
168 struct name##_iter *iter) \
170 return htable_prev(&ht->raw, &iter->i); \
174 #define HTABLE_KTYPE(keyof, type) typeof(keyof((const type *)NULL))
176 /* Assumes keys are a pointer: if not, override. */
178 #define HTABLE_KTYPE(keyof, type) void *
181 #endif /* CCAN_HTABLE_TYPE_H */