1 /* Licensed under LGPLv2+ - see LICENSE file for details */
11 * htable_new - allocate a hash tree.
12 * @rehash: hash function to use for rehashing.
13 * @priv: private argument to @rehash function.
15 struct htable *htable_new(size_t (*hash)(const void *elem, void *priv),
19 * htable_free - dellocate a hash tree.
21 * This doesn't do anything to any pointers left in it.
23 void htable_free(const struct htable *);
26 * htable_rehash - use a hashtree's rehash function
27 * @elem: the argument to rehash()
30 size_t htable_rehash(const void *elem);
33 * htable_add - add a pointer into a hash tree.
35 * @hash: the hash value of the object
36 * @p: the non-NULL pointer
38 * Also note that this can only fail due to allocation failure. Otherwise, it
41 bool htable_add(struct htable *ht, size_t hash, const void *p);
44 * htable_del - remove a pointer from a hash tree
46 * @hash: the hash value of the object
49 * Returns true if the pointer was found (and deleted).
51 bool htable_del(struct htable *ht, size_t hash, const void *p);
54 * struct htable_iter - iterator or htable_first or htable_firstval etc.
56 * This refers to a location inside the hashtable.
63 * htable_firstval - find a candidate for a given hash value
64 * @htable: the hashtable
65 * @i: the struct htable_iter to initialize
66 * @hash: the hash value
68 * You'll need to check the value is what you want; returns NULL if none.
72 void *htable_firstval(const struct htable *htable,
73 struct htable_iter *i, size_t hash);
76 * htable_nextval - find another candidate for a given hash value
77 * @htable: the hashtable
78 * @i: the struct htable_iter to initialize
79 * @hash: the hash value
81 * You'll need to check the value is what you want; returns NULL if no more.
83 void *htable_nextval(const struct htable *htable,
84 struct htable_iter *i, size_t hash);
87 * htable_get - find an entry in the hash table
89 * @h: the hash value of the entry
90 * @cmp: the comparison function
91 * @ptr: the pointer to hand to the comparison function.
93 * Convenient inline wrapper for htable_firstval/htable_nextval loop.
95 static inline void *htable_get(const struct htable *ht,
97 bool (*cmp)(const void *candidate, void *ptr),
100 struct htable_iter i;
103 for (c = htable_firstval(ht,&i,h); c; c = htable_nextval(ht,&i,h)) {
104 if (cmp(c, (void *)ptr))
111 * htable_first - find an entry in the hash table
113 * @i: the struct htable_iter to initialize
115 * Get an entry in the hashtable; NULL if empty.
117 void *htable_first(const struct htable *htable, struct htable_iter *i);
120 * htable_next - find another entry in the hash table
122 * @i: the struct htable_iter to use
124 * Get another entry in the hashtable; NULL if all done.
125 * This is usually used after htable_first or prior non-NULL htable_next.
127 void *htable_next(const struct htable *htable, struct htable_iter *i);
130 * htable_delval - remove an iterated pointer from a hash tree
132 * @i: the htable_iter
134 * Usually used to delete a hash entry after it has been found with
135 * htable_firstval etc.
137 void htable_delval(struct htable *ht, struct htable_iter *i);
139 #endif /* CCAN_HTABLE_H */