]> git.ozlabs.org Git - ccan/blob - ccan/htable/htable_type.h
htable: clean up interface, document htable_type better.
[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 "config.h"
6
7 /**
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>_*)
14  *
15  * NULL values may not be placed into the hash table.
16  *
17  * This defines the type hashtable type and an iterator type:
18  *      struct <name>;
19  *      struct <name>_iter;
20  *
21  * It also defines initialization and freeing functions:
22  *      void <name>_init(struct <name> *);
23  *      void <name>_clear(struct <name> *);
24  *
25  * Add function only fails if we run out of memory:
26  *      bool <name>_add(struct <name> *ht, const <type> *e);
27  *
28  * Delete and delete-by key return true if it was in the set:
29  *      bool <name>_del(struct <name> *ht, const <type> *e);
30  *      bool <name>_delkey(struct <name> *ht, const <keytype> *k);
31  *
32  * Find function return the matching element, or NULL:
33  *      type *<name>_get(const struct @name *ht, const <keytype> *k);
34  *
35  * Iteration over hashtable is also supported:
36  *      type *<name>_first(const struct <name> *ht, struct <name>_iter *i);
37  *      type *<name>_next(const struct <name> *ht, struct <name>_iter *i);
38  *
39  * It's currently safe to iterate over a changing hashtable, but you might
40  * miss an element.  Iteration isn't very efficient, either.
41  */
42 #define HTABLE_DEFINE_TYPE(type, keyof, hashfn, eqfn, name)             \
43         struct name { struct htable raw; };                             \
44         struct name##_iter { struct htable_iter i; };                   \
45         static inline size_t name##_hash(const void *elem, void *priv)  \
46         {                                                               \
47                 return hashfn(keyof((const type *)elem));               \
48         }                                                               \
49         static inline void name##_init(struct name *ht)                 \
50         {                                                               \
51                 htable_init(&ht->raw, name##_hash, NULL);               \
52         }                                                               \
53         static inline void name##_clear(struct name *ht)                \
54         {                                                               \
55                 htable_clear(&ht->raw);                                 \
56         }                                                               \
57         static inline bool name##_add(struct name *ht, const type *elem) \
58         {                                                               \
59                 return htable_add(&ht->raw, hashfn(keyof(elem)), elem); \
60         }                                                               \
61         static inline bool name##_del(struct name *ht, const type *elem) \
62         {                                                               \
63                 return htable_del(&ht->raw, hashfn(keyof(elem)), elem); \
64         }                                                               \
65         static inline type *name##_get(const struct name *ht,           \
66                                        const HTABLE_KTYPE(keyof) k)     \
67         {                                                               \
68                 /* Typecheck for eqfn */                                \
69                 (void)sizeof(eqfn((const type *)NULL,                   \
70                                   keyof((const type *)NULL)));          \
71                 return htable_get(&ht->raw,                             \
72                                   hashfn(k),                            \
73                                   (bool (*)(const void *, void *))(eqfn), \
74                                   k);                                   \
75         }                                                               \
76         static inline bool name##_delkey(struct name *ht,               \
77                                          const HTABLE_KTYPE(keyof) k)   \
78         {                                                               \
79                 type *elem = name##_get(ht, k);                         \
80                 if (elem)                                               \
81                         return name##_del(ht, elem);                    \
82                 return false;                                           \
83         }                                                               \
84         static inline type *name##_first(const struct name *ht,         \
85                                          struct name##_iter *iter)      \
86         {                                                               \
87                 return htable_first(&ht->raw, &iter->i);                \
88         }                                                               \
89         static inline type *name##_next(const struct name *ht,          \
90                                         struct name##_iter *iter)       \
91         {                                                               \
92                 return htable_next(&ht->raw, &iter->i);                 \
93         }
94
95 #if HAVE_TYPEOF
96 #define HTABLE_KTYPE(keyof) typeof(keyof(NULL))
97 #else
98 #define HTABLE_KTYPE(keyof) void *
99 #endif
100 #endif /* CCAN_HTABLE_TYPE_H */