]> git.ozlabs.org Git - ccan/blob - ccan/htable/htable_type.h
6764c3f24ae103c8bccbcc8949c283ff56dcca1e
[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 function return the matching element, or NULL:
35  *      type *<name>_get(const struct @name *ht, const <keytype> *k);
36  *
37  * Iteration over hashtable is also supported:
38  *      type *<name>_first(const struct <name> *ht, struct <name>_iter *i);
39  *      type *<name>_next(const struct <name> *ht, struct <name>_iter *i);
40  *
41  * It's currently safe to iterate over a changing hashtable, but you might
42  * miss an element.  Iteration isn't very efficient, either.
43  *
44  * You can use HTABLE_INITIALIZER like so:
45  *      struct <name> ht = { HTABLE_INITIALIZER(ht.raw, <name>_hash, NULL) };
46  */
47 #define HTABLE_DEFINE_TYPE(type, keyof, hashfn, eqfn, name)             \
48         struct name { struct htable raw; };                             \
49         struct name##_iter { struct htable_iter i; };                   \
50         static inline size_t name##_hash(const void *elem, void *priv)  \
51         {                                                               \
52                 return hashfn(keyof((const type *)elem));               \
53         }                                                               \
54         static inline UNNEEDED void name##_init(struct name *ht)        \
55         {                                                               \
56                 htable_init(&ht->raw, name##_hash, NULL);               \
57         }                                                               \
58         static inline UNNEEDED void name##_init_sized(struct name *ht,  \
59                                                       size_t s)         \
60         {                                                               \
61                 htable_init_sized(&ht->raw, name##_hash, NULL, s);      \
62         }                                                               \
63         static inline UNNEEDED void name##_clear(struct name *ht)       \
64         {                                                               \
65                 htable_clear(&ht->raw);                                 \
66         }                                                               \
67         static inline bool name##_add(struct name *ht, const type *elem) \
68         {                                                               \
69                 return htable_add(&ht->raw, hashfn(keyof(elem)), elem); \
70         }                                                               \
71         static inline UNNEEDED bool name##_del(struct name *ht,         \
72                                                const type *elem)        \
73         {                                                               \
74                 return htable_del(&ht->raw, hashfn(keyof(elem)), elem); \
75         }                                                               \
76         static inline UNNEEDED type *name##_get(const struct name *ht,  \
77                                        const HTABLE_KTYPE(keyof) k)     \
78         {                                                               \
79                 /* Typecheck for eqfn */                                \
80                 (void)sizeof(eqfn((const type *)NULL,                   \
81                                   keyof((const type *)NULL)));          \
82                 return htable_get(&ht->raw,                             \
83                                   hashfn(k),                            \
84                                   (bool (*)(const void *, void *))(eqfn), \
85                                   k);                                   \
86         }                                                               \
87         static inline UNNEEDED bool name##_delkey(struct name *ht,      \
88                                          const HTABLE_KTYPE(keyof) k)   \
89         {                                                               \
90                 type *elem = name##_get(ht, k);                         \
91                 if (elem)                                               \
92                         return name##_del(ht, elem);                    \
93                 return false;                                           \
94         }                                                               \
95         static inline UNNEEDED type *name##_first(const struct name *ht, \
96                                          struct name##_iter *iter)      \
97         {                                                               \
98                 return htable_first(&ht->raw, &iter->i);                \
99         }                                                               \
100         static inline UNNEEDED type *name##_next(const struct name *ht, \
101                                         struct name##_iter *iter)       \
102         {                                                               \
103                 return htable_next(&ht->raw, &iter->i);                 \
104         }
105
106 #if HAVE_TYPEOF
107 #define HTABLE_KTYPE(keyof) typeof(keyof(NULL))
108 #else
109 #define HTABLE_KTYPE(keyof) void *
110 #endif
111 #endif /* CCAN_HTABLE_TYPE_H */