htable_type.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 "config.h"
  6. /**
  7. * HTABLE_DEFINE_TYPE - create a set of htable ops for a type
  8. * @type: a type whose pointers will be values in the hash.
  9. * @keyof: a function/macro to extract a key: <keytype> @keyof(const type *elem)
  10. * @hashfn: a hash function for a @key: size_t @hashfn(const <keytype> *)
  11. * @eqfn: an equality function keys: bool @eqfn(const type *, const <keytype> *)
  12. * @prefix: a prefix for all the functions to define (of form <name>_*)
  13. *
  14. * NULL values may not be placed into the hash table.
  15. *
  16. * This defines the type hashtable type and an iterator type:
  17. * struct <name>;
  18. * struct <name>_iter;
  19. *
  20. * It also defines initialization and freeing functions:
  21. * void <name>_init(struct <name> *);
  22. * void <name>_clear(struct <name> *);
  23. *
  24. * Add function only fails if we run out of memory:
  25. * bool <name>_add(struct <name> *ht, const <type> *e);
  26. *
  27. * Delete and delete-by key return true if it was in the set:
  28. * bool <name>_del(struct <name> *ht, const <type> *e);
  29. * bool <name>_delkey(struct <name> *ht, const <keytype> *k);
  30. *
  31. * Find function return the matching element, or NULL:
  32. * type *<name>_get(const struct @name *ht, const <keytype> *k);
  33. *
  34. * Iteration over hashtable is also supported:
  35. * type *<name>_first(const struct <name> *ht, struct <name>_iter *i);
  36. * type *<name>_next(const struct <name> *ht, struct <name>_iter *i);
  37. *
  38. * It's currently safe to iterate over a changing hashtable, but you might
  39. * miss an element. Iteration isn't very efficient, either.
  40. *
  41. * You can use HTABLE_INITIALIZER like so:
  42. * struct <name> ht = { HTABLE_INITIALIZER(ht.raw, <name>_hash, NULL) };
  43. */
  44. #define HTABLE_DEFINE_TYPE(type, keyof, hashfn, eqfn, name) \
  45. struct name { struct htable raw; }; \
  46. struct name##_iter { struct htable_iter i; }; \
  47. static inline size_t name##_hash(const void *elem, void *priv) \
  48. { \
  49. return hashfn(keyof((const type *)elem)); \
  50. } \
  51. static inline void name##_init(struct name *ht) \
  52. { \
  53. htable_init(&ht->raw, name##_hash, NULL); \
  54. } \
  55. static inline void name##_clear(struct name *ht) \
  56. { \
  57. htable_clear(&ht->raw); \
  58. } \
  59. static inline bool name##_add(struct name *ht, const type *elem) \
  60. { \
  61. return htable_add(&ht->raw, hashfn(keyof(elem)), elem); \
  62. } \
  63. static inline bool name##_del(struct name *ht, const type *elem) \
  64. { \
  65. return htable_del(&ht->raw, hashfn(keyof(elem)), elem); \
  66. } \
  67. static inline type *name##_get(const struct name *ht, \
  68. const HTABLE_KTYPE(keyof) k) \
  69. { \
  70. /* Typecheck for eqfn */ \
  71. (void)sizeof(eqfn((const type *)NULL, \
  72. keyof((const type *)NULL))); \
  73. return htable_get(&ht->raw, \
  74. hashfn(k), \
  75. (bool (*)(const void *, void *))(eqfn), \
  76. k); \
  77. } \
  78. static inline bool name##_delkey(struct name *ht, \
  79. const HTABLE_KTYPE(keyof) k) \
  80. { \
  81. type *elem = name##_get(ht, k); \
  82. if (elem) \
  83. return name##_del(ht, elem); \
  84. return false; \
  85. } \
  86. static inline type *name##_first(const struct name *ht, \
  87. struct name##_iter *iter) \
  88. { \
  89. return htable_first(&ht->raw, &iter->i); \
  90. } \
  91. static inline type *name##_next(const struct name *ht, \
  92. struct name##_iter *iter) \
  93. { \
  94. return htable_next(&ht->raw, &iter->i); \
  95. }
  96. #if HAVE_TYPEOF
  97. #define HTABLE_KTYPE(keyof) typeof(keyof(NULL))
  98. #else
  99. #define HTABLE_KTYPE(keyof) void *
  100. #endif
  101. #endif /* CCAN_HTABLE_TYPE_H */