]> git.ozlabs.org Git - ccan/blob - ccan/hashtable/hashtable.h
gitify the tree, especially the web makefile.
[ccan] / ccan / hashtable / hashtable.h
1 #ifndef CCAN_HASHTABLE_H
2 #define CCAN_HASHTABLE_H
3 #include "config.h"
4 #include <stdbool.h>
5 #include <ccan/typesafe_cb/typesafe_cb.h>
6
7 struct hashtable;
8
9 /**
10  * hashtable_new - allocate a hash tree.
11  * @rehash: hash function to use for rehashing.
12  * @priv: private argument to @rehash function.
13  */
14 struct hashtable *hashtable_new(unsigned long (*rehash)(const void *elem,
15                                                         void *priv),
16                                 void *priv);
17
18 /**
19  * hashtable_free - dellocate a hash tree.
20  *
21  * This doesn't do anything to any pointers left in it.
22  */
23 void hashtable_free(struct hashtable *);
24
25 /**
26  * hashtable_find - look for an object in a hash tree.
27  * @ht: the hashtable
28  * @hash: the hash value of the object you are seeking
29  * @cmp: a comparison function: returns true if the object is found
30  * @cmpdata: the data to hand as second arg to @cmp
31  *
32  * Note that you can do all the work inside the cmp function if you want to:
33  * hashtable_find will return @htelem if @cmp returns true.
34  */
35 void *hashtable_find(struct hashtable *ht,
36                      unsigned long hash,
37                      bool (*cmp)(const void *htelem, void *cmpdata),
38                      void *cmpdata);
39
40 /**
41  * hashtable_add - add an (aligned) pointer into a hash tree.
42  * @ht: the hashtable
43  * @hash: the hash value of the object
44  * @p: the non-NULL pointer, usually from malloc or equiv.
45  *
46  * Note that this implementation (ab)uses the lower bits of the pointer, so
47  * it will abort if the pointer is not aligned to 8 bytes.
48  *
49  * Also note that this can only fail due to allocation failure.  Otherwise, it
50  * returns true.
51  */
52 bool hashtable_add(struct hashtable *ht, unsigned long hash, const void *p);
53
54 /**
55  * hashtable_del - remove a pointer from a hash tree
56  * @ht: the hashtable
57  * @hash: the hash value of the object
58  * @p: the pointer
59  *
60  * Returns true if the pointer was found (and deleted).
61  */
62 bool hashtable_del(struct hashtable *ht, unsigned long hash, const void *p);
63
64 /**
65  * hashtable_traverse - call a function on every pointer in hash tree
66  * @ht: the hashtable
67  * @type: the type of the element in the hashtable.
68  * @cb: the callback: returns true to abort traversal.
69  * @cbarg: the argument to the callback
70  *
71  * Note that your traversal callback may delete any entry (it won't crash),
72  * but it may make the traverse unreliable.
73  */
74 #define hashtable_traverse(ht, type, cb, cbarg)                         \
75         _hashtable_traverse(ht, cast_if_type(bool (*)(void *, void *),  \
76                                              cast_if_any(bool (*)(void *, \
77                                                                   void *), \
78                                                          (cb), &*(cb),  \
79                                                          bool (*)(const type *, \
80                                                                   const typeof(*cbarg) *), \
81                                                          bool (*)(type *, \
82                                                                   const typeof(*cbarg) *), \
83                                                          bool (*)(const type *, \
84                                                                   typeof(*cbarg) *)), \
85                                              &*(cb),            \
86                                              bool (*)(type *, typeof(*cbarg) *)), \
87                             (cbarg))
88
89 void _hashtable_traverse(struct hashtable *ht,
90                          bool (*cb)(void *p, void *cbarg), void *cbarg);
91 #endif /* CCAN_HASHTABLE_H */