htable.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #ifndef CCAN_HTABLE_H
  2. #define CCAN_HTABLE_H
  3. #include "config.h"
  4. #include <stdbool.h>
  5. #include <stdlib.h>
  6. struct htable;
  7. /**
  8. * htable_new - allocate a hash tree.
  9. * @rehash: hash function to use for rehashing.
  10. * @priv: private argument to @rehash function.
  11. */
  12. struct htable *htable_new(size_t (*hash)(const void *elem, void *priv),
  13. void *priv);
  14. /**
  15. * htable_free - dellocate a hash tree.
  16. *
  17. * This doesn't do anything to any pointers left in it.
  18. */
  19. void htable_free(const struct htable *);
  20. /**
  21. * htable_rehash - use a hashtree's rehash function
  22. * @elem: the argument to rehash()
  23. *
  24. */
  25. size_t htable_rehash(const void *elem);
  26. /**
  27. * htable_add - add a pointer into a hash tree.
  28. * @ht: the htable
  29. * @hash: the hash value of the object
  30. * @p: the non-NULL pointer
  31. *
  32. * Also note that this can only fail due to allocation failure. Otherwise, it
  33. * returns true.
  34. */
  35. bool htable_add(struct htable *ht, size_t hash, const void *p);
  36. /**
  37. * htable_del - remove a pointer from a hash tree
  38. * @ht: the htable
  39. * @hash: the hash value of the object
  40. * @p: the pointer
  41. *
  42. * Returns true if the pointer was found (and deleted).
  43. */
  44. bool htable_del(struct htable *ht, size_t hash, const void *p);
  45. /**
  46. * struct htable_iter - iterator or htable_first or htable_firstval etc.
  47. *
  48. * This refers to a location inside the hashtable.
  49. */
  50. struct htable_iter {
  51. size_t off;
  52. };
  53. /**
  54. * htable_firstval - find a candidate for a given hash value
  55. * @htable: the hashtable
  56. * @i: the struct htable_iter to initialize
  57. * @hash: the hash value
  58. *
  59. * You'll need to check the value is what you want; returns NULL if none.
  60. * See Also:
  61. * htable_delval()
  62. */
  63. void *htable_firstval(const struct htable *htable,
  64. struct htable_iter *i, size_t hash);
  65. /**
  66. * htable_nextval - find another candidate for a given hash value
  67. * @htable: the hashtable
  68. * @i: the struct htable_iter to initialize
  69. * @hash: the hash value
  70. *
  71. * You'll need to check the value is what you want; returns NULL if no more.
  72. */
  73. void *htable_nextval(const struct htable *htable,
  74. struct htable_iter *i, size_t hash);
  75. /**
  76. * htable_get - find an entry in the hash table
  77. * @ht: the hashtable
  78. * @h: the hash value of the entry
  79. * @cmp: the comparison function
  80. * @ptr: the pointer to hand to the comparison function.
  81. *
  82. * Convenient inline wrapper for htable_firstval/htable_nextval loop.
  83. */
  84. static inline void *htable_get(const struct htable *ht,
  85. size_t h,
  86. bool (*cmp)(const void *candidate, void *ptr),
  87. const void *ptr)
  88. {
  89. struct htable_iter i;
  90. void *c;
  91. for (c = htable_firstval(ht,&i,h); c; c = htable_nextval(ht,&i,h)) {
  92. if (cmp(c, (void *)ptr))
  93. return c;
  94. }
  95. return NULL;
  96. }
  97. /**
  98. * htable_first - find an entry in the hash table
  99. * @ht: the hashtable
  100. * @i: the struct htable_iter to initialize
  101. *
  102. * Get an entry in the hashtable; NULL if empty.
  103. */
  104. void *htable_first(const struct htable *htable, struct htable_iter *i);
  105. /**
  106. * htable_next - find another entry in the hash table
  107. * @ht: the hashtable
  108. * @i: the struct htable_iter to use
  109. *
  110. * Get another entry in the hashtable; NULL if all done.
  111. * This is usually used after htable_first or prior non-NULL htable_next.
  112. */
  113. void *htable_next(const struct htable *htable, struct htable_iter *i);
  114. /**
  115. * htable_delval - remove an iterated pointer from a hash tree
  116. * @ht: the htable
  117. * @i: the htable_iter
  118. *
  119. * Usually used to delete a hash entry after it has been found with
  120. * htable_firstval etc.
  121. */
  122. void htable_delval(struct htable *ht, struct htable_iter *i);
  123. #endif /* CCAN_HTABLE_H */