Set.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #ifndef Set_H
  16. #define Set_H
  17. #include "memory/Allocator.h"
  18. #include "util/Bits.h"
  19. #include "util/UniqueName.h"
  20. #include "util/Linker.h"
  21. Linker_require("util/Set.c")
  22. /*
  23. * This struct is never defined anywhere.
  24. * It's used in place to void* so that casts must be explicit to reduce likelihood of error.
  25. */
  26. struct Set;
  27. struct Set_Iter;
  28. typedef int (* Set_Compare_t)(const void* a, const void* b);
  29. typedef uint32_t (* Set_HashCode_t)(const void* a);
  30. int Set_add(struct Set* as, void* val);
  31. void* Set_remove(struct Set* as, void* val);
  32. void* Set_get(struct Set* as, void* val);
  33. struct Set* Set_new(struct Allocator* alloc, Set_HashCode_t, Set_Compare_t);
  34. void Set_iter(struct Set* _set, struct Set_Iter* iter);
  35. void Set_iterNext(struct Set_Iter* iter);
  36. int Set_addCopy(struct Set* _set, void* val, uint32_t size);
  37. #define Set_FOREACH(name, set, out) \
  38. struct Set_ ## name ## _Iter UniqueName_get(); \
  39. for (Set_ ## name ## _iter(set, &UniqueName_last()); \
  40. ((out) = UniqueName_last().val); \
  41. Set_ ## name ## _iterNext(&UniqueName_last()))
  42. // CHECKFILES_IGNORE expecting a {
  43. #endif // Used multiple times...
  44. #ifndef Set_NOCREATE
  45. #ifndef Set_TYPE
  46. #error Set_TYPE must be specified
  47. #endif
  48. #ifndef Set_NAME
  49. #ifndef Set_FULLNAME
  50. #error Set_NAME must be specified
  51. #endif
  52. #else
  53. #define Set_FULLNAME Set_GLUE(Set, Set_NAME)
  54. #endif
  55. #define Set_FUNCTION(name) \
  56. Set_GLUE(Set_FULLNAME, name)
  57. #define Set_GLUE(x,y) Set_GLUE2(x,y)
  58. #define Set_GLUE2(x,y) x ## _ ## y
  59. struct Set_FULLNAME {
  60. int size;
  61. };
  62. struct Set_FUNCTION(Iter)
  63. {
  64. Set_TYPE* val;
  65. void* internal;
  66. };
  67. #ifdef Set_COMPARE
  68. #ifndef Set_HASHCODE
  69. #error cannot specify Set_COMPARE without Set_HASHCODE
  70. #endif
  71. static inline uint32_t Set_FUNCTION(_hashCode)(const void* a)
  72. {
  73. return Set_HASHCODE((Set_TYPE*)a);
  74. }
  75. static inline int Set_FUNCTION(_compare)(const void* a, const void* b)
  76. {
  77. return Set_COMPARE((Set_TYPE*)a, (Set_TYPE*)b);
  78. }
  79. #else
  80. #ifdef Set_HASHCODE
  81. #error cannot specify Set_HASHCODE without Set_COMPARE
  82. #endif
  83. #include "util/Hash.h"
  84. static inline uint32_t Set_FUNCTION(_hashCode)(const void* a)
  85. {
  86. return Hash_compute((uint8_t*) a, sizeof(Set_TYPE));
  87. }
  88. static inline int Set_FUNCTION(_compare)(const void* a, const void* b)
  89. {
  90. return Bits_memcmp(a, b, sizeof(Set_TYPE));
  91. }
  92. #endif
  93. static inline struct Set_FULLNAME* Set_FUNCTION(new)(struct Allocator* alloc)
  94. {
  95. return (struct Set_FULLNAME*) Set_new(alloc, Set_FUNCTION(_hashCode), Set_FUNCTION(_compare));
  96. }
  97. static inline Set_TYPE* Set_FUNCTION(get)(struct Set_FULLNAME* list, Set_TYPE* val)
  98. {
  99. return (Set_TYPE*) Set_get((struct Set*) list, (void*)val);
  100. }
  101. static inline int Set_FUNCTION(addCopy)(struct Set_FULLNAME* list, Set_TYPE* val)
  102. {
  103. return Set_addCopy((struct Set*) list, (void*) val, sizeof(Set_TYPE));
  104. }
  105. static inline int Set_FUNCTION(add)(struct Set_FULLNAME* list, Set_TYPE* val)
  106. {
  107. return Set_add((struct Set*) list, (void*) val);
  108. }
  109. static inline void Set_FUNCTION(iter)(struct Set_FULLNAME* set, struct Set_FUNCTION(Iter)* iter)
  110. {
  111. Set_iter((struct Set*) set, (struct Set_Iter*) iter);
  112. }
  113. static inline void Set_FUNCTION(iterNext)(struct Set_FUNCTION(Iter)* iter)
  114. {
  115. Set_iterNext((struct Set_Iter*) iter);
  116. }
  117. static inline Set_TYPE* Set_FUNCTION(remove)(struct Set_FULLNAME* list, Set_TYPE* val)
  118. {
  119. return (Set_TYPE*) Set_remove((struct Set*) list, (void*) val);
  120. }
  121. #undef Set_TYPE
  122. #undef Set_NAME
  123. #undef Set_FULLNAME
  124. #undef Set_INITIAL_CAPACITY
  125. #undef Set_COMPARE
  126. #undef Set_FUNCTION
  127. #undef Set_FULLNAME
  128. #undef Set_GLUE
  129. #undef Set_GLUE2
  130. #undef Set_HASHCODE
  131. #undef Set_COMPARE
  132. #endif // not defined Set_NOCREATE