htable.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. * return *(size_t *)elem;
  37. * }
  38. * static struct htable ht = HTABLE_INITIALIZER(ht, rehash, NULL);
  39. */
  40. #define HTABLE_INITIALIZER(name, rehash, priv) \
  41. { rehash, priv, 0, 0, 0, 0, 0, -1, 0, 0, &name.perfect_bit }
  42. /**
  43. * htable_init - initialize an empty hash table.
  44. * @ht: the hash table to initialize
  45. * @rehash: hash function to use for rehashing.
  46. * @priv: private argument to @rehash function.
  47. */
  48. void htable_init(struct htable *ht,
  49. size_t (*rehash)(const void *elem, void *priv), void *priv);
  50. /**
  51. * htable_clear - empty a hash table.
  52. * @ht: the hash table to clear
  53. *
  54. * This doesn't do anything to any pointers left in it.
  55. */
  56. void htable_clear(struct htable *ht);
  57. /**
  58. * htable_rehash - use a hashtree's rehash function
  59. * @elem: the argument to rehash()
  60. *
  61. */
  62. size_t htable_rehash(const void *elem);
  63. /**
  64. * htable_add - add a pointer into a hash table.
  65. * @ht: the htable
  66. * @hash: the hash value of the object
  67. * @p: the non-NULL pointer
  68. *
  69. * Also note that this can only fail due to allocation failure. Otherwise, it
  70. * returns true.
  71. */
  72. bool htable_add(struct htable *ht, size_t hash, const void *p);
  73. /**
  74. * htable_del - remove a pointer from a hash table
  75. * @ht: the htable
  76. * @hash: the hash value of the object
  77. * @p: the pointer
  78. *
  79. * Returns true if the pointer was found (and deleted).
  80. */
  81. bool htable_del(struct htable *ht, size_t hash, const void *p);
  82. /**
  83. * struct htable_iter - iterator or htable_first or htable_firstval etc.
  84. *
  85. * This refers to a location inside the hashtable.
  86. */
  87. struct htable_iter {
  88. size_t off;
  89. };
  90. /**
  91. * htable_firstval - find a candidate for a given hash value
  92. * @htable: the hashtable
  93. * @i: the struct htable_iter to initialize
  94. * @hash: the hash value
  95. *
  96. * You'll need to check the value is what you want; returns NULL if none.
  97. * See Also:
  98. * htable_delval()
  99. */
  100. void *htable_firstval(const struct htable *htable,
  101. struct htable_iter *i, size_t hash);
  102. /**
  103. * htable_nextval - find another candidate for a given hash value
  104. * @htable: the hashtable
  105. * @i: the struct htable_iter to initialize
  106. * @hash: the hash value
  107. *
  108. * You'll need to check the value is what you want; returns NULL if no more.
  109. */
  110. void *htable_nextval(const struct htable *htable,
  111. struct htable_iter *i, size_t hash);
  112. /**
  113. * htable_get - find an entry in the hash table
  114. * @ht: the hashtable
  115. * @h: the hash value of the entry
  116. * @cmp: the comparison function
  117. * @ptr: the pointer to hand to the comparison function.
  118. *
  119. * Convenient inline wrapper for htable_firstval/htable_nextval loop.
  120. */
  121. static inline void *htable_get(const struct htable *ht,
  122. size_t h,
  123. bool (*cmp)(const void *candidate, void *ptr),
  124. const void *ptr)
  125. {
  126. struct htable_iter i;
  127. void *c;
  128. for (c = htable_firstval(ht,&i,h); c; c = htable_nextval(ht,&i,h)) {
  129. if (cmp(c, (void *)ptr))
  130. return c;
  131. }
  132. return NULL;
  133. }
  134. /**
  135. * htable_first - find an entry in the hash table
  136. * @ht: the hashtable
  137. * @i: the struct htable_iter to initialize
  138. *
  139. * Get an entry in the hashtable; NULL if empty.
  140. */
  141. void *htable_first(const struct htable *htable, struct htable_iter *i);
  142. /**
  143. * htable_next - find another entry in the hash table
  144. * @ht: the hashtable
  145. * @i: the struct htable_iter to use
  146. *
  147. * Get another entry in the hashtable; NULL if all done.
  148. * This is usually used after htable_first or prior non-NULL htable_next.
  149. */
  150. void *htable_next(const struct htable *htable, struct htable_iter *i);
  151. /**
  152. * htable_delval - remove an iterated pointer from a hash table
  153. * @ht: the htable
  154. * @i: the htable_iter
  155. *
  156. * Usually used to delete a hash entry after it has been found with
  157. * htable_firstval etc.
  158. */
  159. void htable_delval(struct htable *ht, struct htable_iter *i);
  160. #endif /* CCAN_HTABLE_H */