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>
8 * HTABLE_DEFINE_TYPE - create a set of htable ops for a type
9 * @type: a type whose pointers will be values in the hash.
10 * @keyof: a function/macro to extract a key: <keytype> @keyof(const type *elem)
11 * @hashfn: a hash function for a @key: size_t @hashfn(const <keytype> *)
12 * @eqfn: an equality function keys: bool @eqfn(const type *, const <keytype> *)
13 * @prefix: a prefix for all the functions to define (of form <name>_*)
15 * NULL values may not be placed into the hash table.
17 * This defines the type hashtable type and an iterator type:
21 * It also defines initialization and freeing functions:
22 * void <name>_init(struct <name> *);
23 * void <name>_init_sized(struct <name> *, size_t);
24 * void <name>_clear(struct <name> *);
26 * Add function only fails if we run out of memory:
27 * bool <name>_add(struct <name> *ht, const <type> *e);
29 * Delete and delete-by key return true if it was in the set:
30 * bool <name>_del(struct <name> *ht, const <type> *e);
31 * bool <name>_delkey(struct <name> *ht, const <keytype> *k);
33 * Find function return the matching element, or NULL:
34 * type *<name>_get(const struct @name *ht, const <keytype> *k);
36 * Iteration over hashtable is also supported:
37 * type *<name>_first(const struct <name> *ht, struct <name>_iter *i);
38 * type *<name>_next(const struct <name> *ht, struct <name>_iter *i);
40 * It's currently safe to iterate over a changing hashtable, but you might
41 * miss an element. Iteration isn't very efficient, either.
43 * You can use HTABLE_INITIALIZER like so:
44 * struct <name> ht = { HTABLE_INITIALIZER(ht.raw, <name>_hash, NULL) };
46 #define HTABLE_DEFINE_TYPE(type, keyof, hashfn, eqfn, name) \
47 struct name { struct htable raw; }; \
48 struct name##_iter { struct htable_iter i; }; \
49 static inline size_t name##_hash(const void *elem, void *priv) \
51 return hashfn(keyof((const type *)elem)); \
53 static inline void name##_init(struct name *ht) \
55 htable_init(&ht->raw, name##_hash, NULL); \
57 static inline void name##_init_sized(struct name *ht, size_t s) \
59 htable_init_sized(&ht->raw, name##_hash, NULL, s); \
61 static inline void name##_clear(struct name *ht) \
63 htable_clear(&ht->raw); \
65 static inline bool name##_add(struct name *ht, const type *elem) \
67 return htable_add(&ht->raw, hashfn(keyof(elem)), elem); \
69 static inline bool name##_del(struct name *ht, const type *elem) \
71 return htable_del(&ht->raw, hashfn(keyof(elem)), elem); \
73 static inline type *name##_get(const struct name *ht, \
74 const HTABLE_KTYPE(keyof) k) \
76 /* Typecheck for eqfn */ \
77 (void)sizeof(eqfn((const type *)NULL, \
78 keyof((const type *)NULL))); \
79 return htable_get(&ht->raw, \
81 (bool (*)(const void *, void *))(eqfn), \
84 static inline bool name##_delkey(struct name *ht, \
85 const HTABLE_KTYPE(keyof) k) \
87 type *elem = name##_get(ht, k); \
89 return name##_del(ht, elem); \
92 static inline type *name##_first(const struct name *ht, \
93 struct name##_iter *iter) \
95 return htable_first(&ht->raw, &iter->i); \
97 static inline type *name##_next(const struct name *ht, \
98 struct name##_iter *iter) \
100 return htable_next(&ht->raw, &iter->i); \
104 #define HTABLE_KTYPE(keyof) typeof(keyof(NULL))
106 #define HTABLE_KTYPE(keyof) void *
108 #endif /* CCAN_HTABLE_TYPE_H */