jbitset_type.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #ifndef CCAN_JBITSET_TYPE_H
  2. #define CCAN_JBITSET_TYPE_H
  3. #include <ccan/jbitset/jbitset.h>
  4. /**
  5. * JBIT_DEFINE_TYPE - create a set of jbit ops for a given pointer type
  6. * @type: a type whose pointers will go into the bitset.
  7. * @name: a name for all the functions to define (of form jbit_<name>_*)
  8. *
  9. * This macro defines a set of inline functions for typesafe and convenient
  10. * usage of a Judy bitset for pointers. It is assumed that a NULL pointer
  11. * is never set in the bitset.
  12. *
  13. * Example:
  14. * JBIT_DEFINE_TYPE(char, char);
  15. * JBIT_DEFINE_TYPE(struct foo, foo);
  16. *
  17. * static struct jbitset_char *jc;
  18. * struct jbitset_foo *jf;
  19. *
  20. * static void add_to_bitsets(const char *p, const struct foo *f)
  21. * {
  22. * // Note, this adds the pointer, not the string!
  23. * jbit_char_set(jc, p);
  24. * jbit_foo_set(jf, f);
  25. * }
  26. */
  27. #define JBIT_DEFINE_TYPE(type, name) \
  28. struct jbitset_##name; \
  29. static inline struct jbitset_##name *jbit_##name##_new(void) \
  30. { \
  31. return (struct jbitset_##name *)jbit_new(); \
  32. } \
  33. static inline void jbit_##name##_free(const struct jbitset_##name *set) \
  34. { \
  35. jbit_free((const struct jbitset *)set); \
  36. } \
  37. static inline const char *jbit_##name##_error(struct jbitset_##name *set) \
  38. { \
  39. return jbit_error((struct jbitset *)set); \
  40. } \
  41. static inline bool jbit_##name##_test(const struct jbitset_##name *set, \
  42. const type *index) \
  43. { \
  44. return jbit_test((const struct jbitset *)set, (size_t)index); \
  45. } \
  46. static inline bool jbit_##name##_set(struct jbitset_##name *set, \
  47. const type *index) \
  48. { \
  49. return jbit_set((struct jbitset *)set, (size_t)index); \
  50. } \
  51. static inline bool jbit_##name##_clear(struct jbitset_##name *set, \
  52. const type *index) \
  53. { \
  54. return jbit_clear((struct jbitset *)set, (size_t)index); \
  55. } \
  56. static inline size_t jbit_##name##_count(struct jbitset_##name *set) \
  57. { \
  58. return jbit_popcount((const struct jbitset *)set, 0, -1); \
  59. } \
  60. static inline type *jbit_##name##_nth(const struct jbitset_##name *set, \
  61. size_t n) \
  62. { \
  63. return (type *)jbit_nth((const struct jbitset *)set, n, 0); \
  64. } \
  65. static inline type *jbit_##name##_first(const struct jbitset_##name *set) \
  66. { \
  67. return (type *)jbit_first((const struct jbitset *)set, 0); \
  68. } \
  69. static inline type *jbit_##name##_next(struct jbitset_##name *set, \
  70. const type *prev) \
  71. { \
  72. return (type *)jbit_next((const struct jbitset *)set, (size_t)prev, 0); \
  73. } \
  74. static inline type *jbit_##name##_last(struct jbitset_##name *set) \
  75. { \
  76. return (type *)jbit_last((const struct jbitset *)set, 0); \
  77. } \
  78. static inline type *jbit_##name##_prev(struct jbitset_##name *set, \
  79. const type *prev) \
  80. { \
  81. return (type *)jbit_prev((const struct jbitset *)set, (size_t)prev, 0); \
  82. }
  83. #endif /* CCAN_JBITSET_TYPE_H */