]> git.ozlabs.org Git - ccan/blob - ccan/htable/htable.h
htable: add htable_copy.
[ccan] / ccan / htable / htable.h
1 /* Licensed under LGPLv2+ - see LICENSE file for details */
2 #ifndef CCAN_HTABLE_H
3 #define CCAN_HTABLE_H
4 #include "config.h"
5 #include <stdint.h>
6 #include <stdbool.h>
7 #include <stdlib.h>
8
9 /**
10  * struct htable - private definition of a htable.
11  *
12  * It's exposed here so you can put it in your structures and so we can
13  * supply inline functions.
14  */
15 struct htable {
16         size_t (*rehash)(const void *elem, void *priv);
17         void *priv;
18         unsigned int bits;
19         size_t elems, deleted, max, max_with_deleted;
20         /* These are the bits which are the same in all pointers. */
21         uintptr_t common_mask, common_bits;
22         uintptr_t perfect_bit;
23         uintptr_t *table;
24 };
25
26 /**
27  * HTABLE_INITIALIZER - static initialization for a hash table.
28  * @name: name of this htable.
29  * @rehash: hash function to use for rehashing.
30  * @priv: private argument to @rehash function.
31  *
32  * This is useful for setting up static and global hash tables.
33  *
34  * Example:
35  *      // For simplicity's sake, say hash value is contents of elem.
36  *      static size_t rehash(const void *elem, void *unused)
37  *      {
38  *              return *(size_t *)elem;
39  *      }
40  *      static struct htable ht = HTABLE_INITIALIZER(ht, rehash, NULL);
41  */
42 #define HTABLE_INITIALIZER(name, rehash, priv)                          \
43         { rehash, priv, 0, 0, 0, 0, 0, -1, 0, 0, &name.perfect_bit }
44
45 /**
46  * htable_init - initialize an empty hash table.
47  * @ht: the hash table to initialize
48  * @rehash: hash function to use for rehashing.
49  * @priv: private argument to @rehash function.
50  */
51 void htable_init(struct htable *ht,
52                  size_t (*rehash)(const void *elem, void *priv), void *priv);
53
54 /**
55  * htable_init_sized - initialize an empty hash table of given size.
56  * @ht: the hash table to initialize
57  * @rehash: hash function to use for rehashing.
58  * @priv: private argument to @rehash function.
59  * @size: the number of element.
60  *
61  * If this returns false, @ht is still usable, but may need to do reallocation
62  * upon an add.  If this returns true, it will not need to reallocate within
63  * @size htable_adds.
64  */
65 bool htable_init_sized(struct htable *ht,
66                        size_t (*rehash)(const void *elem, void *priv),
67                        void *priv, size_t size);
68
69 /**
70  * htable_clear - empty a hash table.
71  * @ht: the hash table to clear
72  *
73  * This doesn't do anything to any pointers left in it.
74  */
75 void htable_clear(struct htable *ht);
76
77 /**
78  * htable_copy - duplicate a hash table.
79  * @dst: the hash table to overwrite
80  * @src: the hash table to copy
81  *
82  * Only fails on out-of-memory.
83  *
84  * Equivalent to (but faster than):
85  *    if (!htable_init_sized(dst, src->rehash, src->priv, 1U << src->bits))
86  *         return false;
87  *    v = htable_first(src, &i);
88  *    while (v) {
89  *              htable_add(dst, v);
90  *              v = htable_next(src, i);
91  *    }
92  *    return true;
93  */
94 bool htable_copy(struct htable *dst, const struct htable *src);
95
96 /**
97  * htable_rehash - use a hashtree's rehash function
98  * @elem: the argument to rehash()
99  *
100  */
101 size_t htable_rehash(const void *elem);
102
103 /**
104  * htable_add - add a pointer into a hash table.
105  * @ht: the htable
106  * @hash: the hash value of the object
107  * @p: the non-NULL pointer
108  *
109  * Also note that this can only fail due to allocation failure.  Otherwise, it
110  * returns true.
111  */
112 bool htable_add(struct htable *ht, size_t hash, const void *p);
113
114 /**
115  * htable_del - remove a pointer from a hash table
116  * @ht: the htable
117  * @hash: the hash value of the object
118  * @p: the pointer
119  *
120  * Returns true if the pointer was found (and deleted).
121  */
122 bool htable_del(struct htable *ht, size_t hash, const void *p);
123
124 /**
125  * struct htable_iter - iterator or htable_first or htable_firstval etc.
126  *
127  * This refers to a location inside the hashtable.
128  */
129 struct htable_iter {
130         size_t off;
131 };
132
133 /**
134  * htable_firstval - find a candidate for a given hash value
135  * @htable: the hashtable
136  * @i: the struct htable_iter to initialize
137  * @hash: the hash value
138  *
139  * You'll need to check the value is what you want; returns NULL if none.
140  * See Also:
141  *      htable_delval()
142  */
143 void *htable_firstval(const struct htable *htable,
144                       struct htable_iter *i, size_t hash);
145
146 /**
147  * htable_nextval - find another candidate for a given hash value
148  * @htable: the hashtable
149  * @i: the struct htable_iter to initialize
150  * @hash: the hash value
151  *
152  * You'll need to check the value is what you want; returns NULL if no more.
153  */
154 void *htable_nextval(const struct htable *htable,
155                      struct htable_iter *i, size_t hash);
156
157 /**
158  * htable_get - find an entry in the hash table
159  * @ht: the hashtable
160  * @h: the hash value of the entry
161  * @cmp: the comparison function
162  * @ptr: the pointer to hand to the comparison function.
163  *
164  * Convenient inline wrapper for htable_firstval/htable_nextval loop.
165  */
166 static inline void *htable_get(const struct htable *ht,
167                                size_t h,
168                                bool (*cmp)(const void *candidate, void *ptr),
169                                const void *ptr)
170 {
171         struct htable_iter i;
172         void *c;
173
174         for (c = htable_firstval(ht,&i,h); c; c = htable_nextval(ht,&i,h)) {
175                 if (cmp(c, (void *)ptr))
176                         return c;
177         }
178         return NULL;
179 }
180
181 /**
182  * htable_first - find an entry in the hash table
183  * @ht: the hashtable
184  * @i: the struct htable_iter to initialize
185  *
186  * Get an entry in the hashtable; NULL if empty.
187  */
188 void *htable_first(const struct htable *htable, struct htable_iter *i);
189
190 /**
191  * htable_next - find another entry in the hash table
192  * @ht: the hashtable
193  * @i: the struct htable_iter to use
194  *
195  * Get another entry in the hashtable; NULL if all done.
196  * This is usually used after htable_first or prior non-NULL htable_next.
197  */
198 void *htable_next(const struct htable *htable, struct htable_iter *i);
199
200 /**
201  * htable_prev - find the previous entry in the hash table
202  * @ht: the hashtable
203  * @i: the struct htable_iter to use
204  *
205  * Get previous entry in the hashtable; NULL if all done.
206  *
207  * "previous" here only means the item that would have been returned by
208  * htable_next() before the item it returned most recently.
209  *
210  * This is usually used in the middle of (or after) a htable_next iteration and
211  * to "unwind" actions taken.
212  */
213 void *htable_prev(const struct htable *htable, struct htable_iter *i);
214
215 /**
216  * htable_delval - remove an iterated pointer from a hash table
217  * @ht: the htable
218  * @i: the htable_iter
219  *
220  * Usually used to delete a hash entry after it has been found with
221  * htable_firstval etc.
222  */
223 void htable_delval(struct htable *ht, struct htable_iter *i);
224
225 #endif /* CCAN_HTABLE_H */