htable_type.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* Licensed under LGPLv2+ - see LICENSE file for details */
  2. #ifndef CCAN_HTABLE_TYPE_H
  3. #define CCAN_HTABLE_TYPE_H
  4. #include <ccan/htable/htable.h>
  5. #include <ccan/compiler/compiler.h>
  6. #include "config.h"
  7. /**
  8. * HTABLE_DEFINE_TYPE - create a set of htable ops for a type
  9. * @type: a type whose pointers will be values in the hash.
  10. * @keyof: a function/macro to extract a key: <keytype> @keyof(const type *elem)
  11. * @hashfn: a hash function for a @key: size_t @hashfn(const <keytype> *)
  12. * @eqfn: an equality function keys: bool @eqfn(const type *, const <keytype> *)
  13. * @prefix: a prefix for all the functions to define (of form <name>_*)
  14. *
  15. * NULL values may not be placed into the hash table.
  16. *
  17. * This defines the type hashtable type and an iterator type:
  18. * struct <name>;
  19. * struct <name>_iter;
  20. *
  21. * It also defines initialization and freeing functions:
  22. * void <name>_init(struct <name> *);
  23. * void <name>_init_sized(struct <name> *, size_t);
  24. * void <name>_clear(struct <name> *);
  25. *
  26. * Add function only fails if we run out of memory:
  27. * bool <name>_add(struct <name> *ht, const <type> *e);
  28. *
  29. * Delete and delete-by key return true if it was in the set:
  30. * bool <name>_del(struct <name> *ht, const <type> *e);
  31. * bool <name>_delkey(struct <name> *ht, const <keytype> *k);
  32. *
  33. * Find and return the (first) matching element, or NULL:
  34. * type *<name>_get(const struct @name *ht, const <keytype> *k);
  35. *
  36. * Find and return all matching elements, or NULL:
  37. * type *<name>_getfirst(const struct @name *ht, const <keytype> *k,
  38. * struct <name>_iter *i);
  39. * type *<name>_getnext(const struct @name *ht, const <keytype> *k,
  40. * struct <name>_iter *i);
  41. *
  42. * Iteration over hashtable is also supported:
  43. * type *<name>_first(const struct <name> *ht, struct <name>_iter *i);
  44. * type *<name>_next(const struct <name> *ht, struct <name>_iter *i);
  45. *
  46. * It's currently safe to iterate over a changing hashtable, but you might
  47. * miss an element. Iteration isn't very efficient, either.
  48. *
  49. * You can use HTABLE_INITIALIZER like so:
  50. * struct <name> ht = { HTABLE_INITIALIZER(ht.raw, <name>_hash, NULL) };
  51. */
  52. #define HTABLE_DEFINE_TYPE(type, keyof, hashfn, eqfn, name) \
  53. struct name { struct htable raw; }; \
  54. struct name##_iter { struct htable_iter i; }; \
  55. static inline size_t name##_hash(const void *elem, void *priv) \
  56. { \
  57. return hashfn(keyof((const type *)elem)); \
  58. } \
  59. static inline UNNEEDED void name##_init(struct name *ht) \
  60. { \
  61. htable_init(&ht->raw, name##_hash, NULL); \
  62. } \
  63. static inline UNNEEDED void name##_init_sized(struct name *ht, \
  64. size_t s) \
  65. { \
  66. htable_init_sized(&ht->raw, name##_hash, NULL, s); \
  67. } \
  68. static inline UNNEEDED void name##_clear(struct name *ht) \
  69. { \
  70. htable_clear(&ht->raw); \
  71. } \
  72. static inline bool name##_add(struct name *ht, const type *elem) \
  73. { \
  74. return htable_add(&ht->raw, hashfn(keyof(elem)), elem); \
  75. } \
  76. static inline UNNEEDED bool name##_del(struct name *ht, \
  77. const type *elem) \
  78. { \
  79. return htable_del(&ht->raw, hashfn(keyof(elem)), elem); \
  80. } \
  81. static inline UNNEEDED type *name##_get(const struct name *ht, \
  82. const HTABLE_KTYPE(keyof) k) \
  83. { \
  84. /* Typecheck for eqfn */ \
  85. (void)sizeof(eqfn((const type *)NULL, \
  86. keyof((const type *)NULL))); \
  87. return htable_get(&ht->raw, \
  88. hashfn(k), \
  89. (bool (*)(const void *, void *))(eqfn), \
  90. k); \
  91. } \
  92. static inline UNNEEDED type *name##_getmatch_(const struct name *ht, \
  93. const HTABLE_KTYPE(keyof) k, \
  94. size_t h, \
  95. type *v, \
  96. struct name##_iter *iter) \
  97. { \
  98. while (v) { \
  99. if (eqfn(v, k)) \
  100. break; \
  101. v = htable_nextval(&ht->raw, &iter->i, h); \
  102. } \
  103. return v; \
  104. } \
  105. static inline UNNEEDED type *name##_getfirst(const struct name *ht, \
  106. const HTABLE_KTYPE(keyof) k, \
  107. struct name##_iter *iter) \
  108. { \
  109. size_t h = hashfn(k); \
  110. type *v = htable_firstval(&ht->raw, &iter->i, h); \
  111. return name##_getmatch_(ht, k, h, v, iter); \
  112. } \
  113. static inline UNNEEDED type *name##_getnext(const struct name *ht, \
  114. const HTABLE_KTYPE(keyof) k, \
  115. struct name##_iter *iter) \
  116. { \
  117. size_t h = hashfn(k); \
  118. type *v = htable_nextval(&ht->raw, &iter->i, h); \
  119. return name##_getmatch_(ht, k, h, v, iter); \
  120. } \
  121. static inline UNNEEDED bool name##_delkey(struct name *ht, \
  122. const HTABLE_KTYPE(keyof) k) \
  123. { \
  124. type *elem = name##_get(ht, k); \
  125. if (elem) \
  126. return name##_del(ht, elem); \
  127. return false; \
  128. } \
  129. static inline UNNEEDED type *name##_first(const struct name *ht, \
  130. struct name##_iter *iter) \
  131. { \
  132. return htable_first(&ht->raw, &iter->i); \
  133. } \
  134. static inline UNNEEDED type *name##_next(const struct name *ht, \
  135. struct name##_iter *iter) \
  136. { \
  137. return htable_next(&ht->raw, &iter->i); \
  138. }
  139. #if HAVE_TYPEOF
  140. #define HTABLE_KTYPE(keyof) typeof(keyof(NULL))
  141. #else
  142. #define HTABLE_KTYPE(keyof) void *
  143. #endif
  144. #endif /* CCAN_HTABLE_TYPE_H */