1 /* Licensed under LGPLv2+ - see LICENSE file for details */
10 * struct htable - private definition of a htable.
12 * It's exposed here so you can put it in your structures and so we can
13 * supply inline functions.
16 size_t (*rehash)(const void *elem, void *priv);
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;
27 * htable_init - initialize an empty hash tree.
28 * @ht: the hash table to initialize
29 * @rehash: hash function to use for rehashing.
30 * @priv: private argument to @rehash function.
32 void htable_init(struct htable *ht,
33 size_t (*rehash)(const void *elem, void *priv), void *priv);
36 * htable_clear - empty a hash tree.
37 * @ht: the hash table to clear
39 * This doesn't do anything to any pointers left in it.
41 void htable_clear(struct htable *ht);
44 * htable_rehash - use a hashtree's rehash function
45 * @elem: the argument to rehash()
48 size_t htable_rehash(const void *elem);
51 * htable_add - add a pointer into a hash tree.
53 * @hash: the hash value of the object
54 * @p: the non-NULL pointer
56 * Also note that this can only fail due to allocation failure. Otherwise, it
59 bool htable_add(struct htable *ht, size_t hash, const void *p);
62 * htable_del - remove a pointer from a hash tree
64 * @hash: the hash value of the object
67 * Returns true if the pointer was found (and deleted).
69 bool htable_del(struct htable *ht, size_t hash, const void *p);
72 * struct htable_iter - iterator or htable_first or htable_firstval etc.
74 * This refers to a location inside the hashtable.
81 * htable_firstval - find a candidate for a given hash value
82 * @htable: the hashtable
83 * @i: the struct htable_iter to initialize
84 * @hash: the hash value
86 * You'll need to check the value is what you want; returns NULL if none.
90 void *htable_firstval(const struct htable *htable,
91 struct htable_iter *i, size_t hash);
94 * htable_nextval - find another candidate for a given hash value
95 * @htable: the hashtable
96 * @i: the struct htable_iter to initialize
97 * @hash: the hash value
99 * You'll need to check the value is what you want; returns NULL if no more.
101 void *htable_nextval(const struct htable *htable,
102 struct htable_iter *i, size_t hash);
105 * htable_get - find an entry in the hash table
107 * @h: the hash value of the entry
108 * @cmp: the comparison function
109 * @ptr: the pointer to hand to the comparison function.
111 * Convenient inline wrapper for htable_firstval/htable_nextval loop.
113 static inline void *htable_get(const struct htable *ht,
115 bool (*cmp)(const void *candidate, void *ptr),
118 struct htable_iter i;
121 for (c = htable_firstval(ht,&i,h); c; c = htable_nextval(ht,&i,h)) {
122 if (cmp(c, (void *)ptr))
129 * htable_first - find an entry in the hash table
131 * @i: the struct htable_iter to initialize
133 * Get an entry in the hashtable; NULL if empty.
135 void *htable_first(const struct htable *htable, struct htable_iter *i);
138 * htable_next - find another entry in the hash table
140 * @i: the struct htable_iter to use
142 * Get another entry in the hashtable; NULL if all done.
143 * This is usually used after htable_first or prior non-NULL htable_next.
145 void *htable_next(const struct htable *htable, struct htable_iter *i);
148 * htable_delval - remove an iterated pointer from a hash tree
150 * @i: the htable_iter
152 * Usually used to delete a hash entry after it has been found with
153 * htable_firstval etc.
155 void htable_delval(struct htable *ht, struct htable_iter *i);
157 #endif /* CCAN_HTABLE_H */