htable.h 6.2 KB

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