hash.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. #ifndef __sti__hash_h__
  2. #define __sti__hash_h__
  3. // Public Domain.
  4. #include <stdint.h>
  5. #include <stddef.h>
  6. // super nifty site:
  7. // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
  8. inline static size_t HT_nextPOT(size_t in) {
  9. in--;
  10. in |= in >> 1;
  11. in |= in >> 2;
  12. in |= in >> 4;
  13. in |= in >> 8;
  14. in |= in >> 16;
  15. in++;
  16. return in;
  17. }
  18. struct HT_Pointer_Type { int useless; }; // key's length is stated explicitly
  19. struct HT_IntLiteral_Type { int useless; }; // key's length is determined by sizeof(), and can be passed in an int64
  20. struct HT_Sizeof_Type { int useless; }; // key's length is determined by sizeof()
  21. struct HT_String_Type { int useless; }; // null terminated key pointer
  22. struct HT_Option_None { int useless; };
  23. struct HT_Option_Literal { int useless; }; // keys are literal 64 bit values, not pointers to memory
  24. #define HT_BUILTIN_TYPES(q, z) \
  25. _Bool q: z, \
  26. char q: z, \
  27. signed char q: z, \
  28. short q: z, \
  29. int q: z, \
  30. long q: z, \
  31. long long q: z, \
  32. unsigned char q: z, \
  33. unsigned short q: z, \
  34. unsigned int q: z, \
  35. unsigned long q: z, \
  36. unsigned long long q: z, \
  37. float q: z, \
  38. double q: z, \
  39. void* q: z,
  40. #define HT_INT_TYPES(q, z) \
  41. _Bool q: z, \
  42. char q: z, \
  43. signed char q: z, \
  44. short q: z, \
  45. int q: z, \
  46. long q: z, \
  47. long long q: z, \
  48. unsigned char q: z, \
  49. unsigned short q: z, \
  50. unsigned int q: z, \
  51. unsigned long q: z, \
  52. unsigned long long q: z, \
  53. #define HT_IS_NATIVE_TYPE(x, y, n) _Generic(x, \
  54. HT_BUILTIN_TYPES(,y) \
  55. HT_BUILTIN_TYPES(*,y) \
  56. HT_BUILTIN_TYPES(**,y) \
  57. HT_BUILTIN_TYPES(***,y) \
  58. HT_BUILTIN_TYPES(****,y) \
  59. default: n \
  60. )
  61. // shrink_ratio: set greater than 1.0 to entirely disable, default 99.0
  62. #define HT_ARG_N(_1, _2, _3, _4, _5, _6, N, ...) N
  63. #define HT_RSEQ_N() 6, 5, 4, 3, 2, 1, 0
  64. #define HT_NARG_(...) HT_ARG_N(__VA_ARGS__)
  65. #define HT_NARG(...) HT_NARG_(__VA_ARGS__, HT_RSEQ_N())
  66. #define HT_CAT(a, b) a ## b
  67. #define HT(...) HT_(HT_NARG(__VA_ARGS__), __VA_ARGS__)
  68. #define HT_(n, ...) HT_CAT(HT_,n)(__VA_ARGS__)
  69. // a single argument implies string keys and default options
  70. #define HT_1(ValType) HT_EX(HT_String_Type, char*, ValType, HT_Option_None, HT_Option_None)
  71. // two arguments implies a sizeof()-able literal key type and default options
  72. #define HT_2(KeyType, ValType) HT_EX(HT_Sizeof_Type, KeyType, ValType, HT_Option_None, HT_Option_None)
  73. // three arguments lets you set the key mode flag
  74. #define HT_3(KeyType, ValType, KeyFlag) HT_EX(HT_##KeyFlag##_Type, KeyType, ValType, HT_Option_None, HT_Option_None)
  75. struct HT_base_layout {
  76. size_t alloc_size;
  77. size_t fill;
  78. size_t stride; // storing it once is less memory than passing it in every function call
  79. void* buckets;
  80. uint32_t key_len; // keys longer than 4GB are not supported.
  81. // uint16_t key_width; // number of bytes the key
  82. uint8_t key_mode; // 's' = C string, 'p' = pointer to memory[key_len], 'i' = inline key sizeof() == key_len
  83. };
  84. #define HT_EX(KeyFlag, KeyType, ValType, KeyOpt, ValOpt) \
  85. struct { \
  86. struct HT_base_layout base; \
  87. struct { \
  88. struct KeyFlag keyTypeFlag; \
  89. ValType valType; \
  90. ValType* valTypep; \
  91. ValType** valTypepp; \
  92. KeyType keyType; \
  93. KeyType* keyTypep; \
  94. struct KeyOpt keyOpt; \
  95. struct ValOpt valOpt; \
  96. } meta[]; \
  97. }
  98. // this must be calculated manually because compilers might
  99. // add padding at the end of the struct for small Types
  100. #define HT_STRIDE(h) (sizeof(uint64_t) + sizeof((h)->meta[0].keyType) + sizeof((h)->meta[0].valType))
  101. #define HT_KEYMODE(h) _Generic((h)->meta[0].keyTypeFlag, \
  102. struct HT_String_Type: 's', \
  103. struct HT_Sizeof_Type: 'i', \
  104. struct HT_Pointer_Type: 'p' \
  105. )
  106. #define HT_KEYMODE_LEN(h) _Generic((h)->meta[0].keyTypeFlag, \
  107. struct HT_String_Type: 0, \
  108. struct HT_Sizeof_Type: sizeof((h)->meta[0].keyType), \
  109. struct HT_Pointer_Type: sizeof((h)->meta[0].keyType) \
  110. )
  111. #define HT_KEY_WIDTH(h) _Generic((h)->meta[0].keyTypeFlag, \
  112. struct HT_String_Type: sizeof(char*), \
  113. struct HT_Sizeof_Type: sizeof((h)->meta[0].keyType), \
  114. struct HT_Pointer_Type: sizeof(void*) \
  115. )
  116. #define HT_init(h, sz) \
  117. do { \
  118. (h)->base.alloc_size = HT_nextPOT(sz); \
  119. (h)->base.fill = 0; \
  120. (h)->base.key_len = HT_KEYMODE_LEN(h); \
  121. (h)->base.key_mode = HT_KEYMODE(h); \
  122. (h)->base.stride = sizeof(uint64_t) + HT_KEY_WIDTH(h) + sizeof((h)->meta[0].valType); \
  123. (h)->base.buckets = calloc(1, (h)->base.stride * (h)->base.alloc_size); \
  124. } while(0)
  125. #define HT_destroy(h) \
  126. do { \
  127. (h)->base.alloc_size = 0; \
  128. (h)->base.fill = 0; \
  129. if((h)->base.buckets) free((h)->base.buckets); \
  130. } while(0)
  131. int oaht_resize(struct HT_base_layout* ht, size_t newSize);
  132. // returns 0 if val is set to the value
  133. // *val == NULL && return > 0 means the key was not found;
  134. int oaht_getp_kptr(struct HT_base_layout* ht, void* key, void** valp);
  135. int oaht_get_kptr(struct HT_base_layout* ht, void* key, void* val);
  136. int oaht_get_klit(struct HT_base_layout* ht, uint64_t key, void* val);
  137. #define HT_TYPECHECK(h, a, b) (void*)(1 ? a : (h)->meta[0].b)
  138. #define HT_get(h, key, valp) ({ \
  139. __typeof__((h)->meta[0].keyType) __HT_key = (key); \
  140. oaht_get_kptr(&(h)->base, \
  141. _Generic((h)->meta[0].keyTypeFlag, \
  142. struct HT_String_Type: __HT_key, \
  143. default: &__HT_key \
  144. ), HT_TYPECHECK(h, valp, valTypep)); \
  145. })
  146. /*
  147. #define HT_get(h, key, valp) ({ \
  148. __typeof__((h)->meta[0].keyType) __HT_key = (key); \
  149. _Generic((h)->meta[0].keyTypeFlag, \
  150. struct HT_String_Type: oaht_get_kptr(&(h)->base, __HT_key, HT_TYPECHECK(h, valp, valTypep)), \
  151. default: oaht_get_kptr(&(h)->base, &__HT_key, HT_TYPECHECK(h, valp, valTypep)) \
  152. ); \
  153. })
  154. */
  155. #define HT_getp(h, key, valp) ({ \
  156. __typeof__((h)->meta[0].keyType) __HT_key = (key); \
  157. oaht_getp_kptr(&(h)->base, \
  158. _Generic((h)->meta[0].keyTypeFlag, \
  159. struct HT_String_Type: __HT_key, \
  160. default: &__HT_key \
  161. ), HT_TYPECHECK(h, valp, valTypepp)); \
  162. })
  163. int oaht_set_kptr(struct HT_base_layout* ht, void* key, void* val);
  164. int oaht_set_klit(struct HT_base_layout* ht, uint64_t key, void* val);
  165. #define HT_set(h, key, val) ({ \
  166. __typeof__((h)->meta[0].keyType) __HT_key = (key); \
  167. __typeof__((h)->meta[0].valType) __HT_val = (val); \
  168. oaht_set_kptr(&(h)->base, \
  169. _Generic((h)->meta[0].keyTypeFlag, \
  170. struct HT_String_Type: __HT_key, \
  171. default: &__HT_key \
  172. ), &__HT_val); \
  173. })
  174. /*#define HT_set(h, key, val) ({ \
  175. __typeof__((h)->meta[0].keyType) __HT_key = (key); \
  176. __typeof__((h)->meta[0].valType) __HT_val = (val); \
  177. _Generic((h)->meta[0].keyTypeFlag, \
  178. struct HT_String_Type: oaht_set_kptr(&(h)->base, __HT_key, &__HT_val), \
  179. default: oaht_set_kptr(&(h)->base, &__HT_key, &__HT_val) \
  180. ); \
  181. })*/
  182. //#define HT_setn(h, key, valp) oaht_set_klit(&(h)->base, (uint64_t)(1 ? key : ((h)->meta[0].keyType)), HT_TYPECHECK(h, &valp, valTypep))
  183. /* doesn't work because the compiler is not smart enough to not typecheck the contents of non-chosen _Generic cases...
  184. #define HT_set(h, key, valp) _Generic(key, \
  185. HT_INT_TYPES(, oaht_set_litn(&(h)->base, (uint64_t)(1 ? key : ((h)->meta[0].keyType)), HT_TYPECHECK(h, valp, valTypep))) \
  186. default: oaht_set_kptr(&(h)->base, HT_TYPECHECK(h, key, keyType), HT_TYPECHECK(h, valp, valTypep)) \
  187. )
  188. */
  189. //#define HT_set(h, key, val) oaht_set((char**)&((h)->buckets), HT_STRIDE(h), &(h)->fill, &(h)->alloc_size, key, (char*)(1 ? &(val) : &((h)->buckets->value)))
  190. int oaht_delete(struct HT_base_layout* ht, void* key);
  191. //#define HT_delete(h, key) oaht_delete(&(h)->base, key)
  192. #define HT_delete(h, key) ({ \
  193. __typeof__((h)->meta[0].keyType) __HT_key = (key); \
  194. oaht_delete(&(h)->base, \
  195. _Generic((h)->meta[0].keyTypeFlag, \
  196. struct HT_String_Type: __HT_key, \
  197. default: &__HT_key \
  198. )); \
  199. })
  200. // iteration. no order. results undefined if modified while iterating
  201. // returns 0 when there is none left
  202. // set iter to NULL to start
  203. int oaht_nextp(struct HT_base_layout* ht, void** iter, void** key, void** valp);
  204. #define HT_nextp(h, iter, keyp, valp) oaht_nextp(&(h)->base, iter, (void**)HT_TYPECHECK(h, keyp, keyTypep), (void**)HT_TYPECHECK(h, valp, valTypepp))
  205. int oaht_next(struct HT_base_layout* ht, void** iter, void** key, void* val);
  206. #define HT_next(h, iter, keyp, valp) oaht_next(&(h)->base, iter, (void**)HT_TYPECHECK(h, keyp, keyTypep), HT_TYPECHECK(h, valp, valTypep))
  207. /*
  208. Loop macro magic
  209. https://www.chiark.greenend.org.uk/~sgtatham/mp/
  210. HashTable obj;
  211. HT_LOOP(&obj, key, char*, val) {
  212. printf("loop: %s, %s", key, val);
  213. }
  214. effective source:
  215. #define HT_LOOP(obj, keyname, valtype, valname)
  216. if(0)
  217. finished: ;
  218. else
  219. for(char* keyname;;) // internal declarations, multiple loops to avoid comma op funny business
  220. for(valtype valname;;)
  221. for(void* iter = NULL ;;)
  222. if(HT_next(obj, iter, &keyname, &valname))
  223. goto main_loop;
  224. else
  225. while(1)
  226. if(1) {
  227. // when the user uses break
  228. goto finished;
  229. }
  230. else
  231. while(1)
  232. if(!HT_next(obj, iter, &keyname, &valname)) {
  233. // normal termination
  234. goto finished;
  235. }
  236. else
  237. main_loop:
  238. // { user block; not in macro }
  239. */
  240. #define HASH__PASTEINNER(a, b) a ## b
  241. #define HASH__PASTE(a, b) HASH__PASTEINNER(a, b)
  242. #define HASH__ITER(key, val) HASH__PASTE(hashtable_iter_ ## key ## __ ## val ## __, __LINE__)
  243. #define HASH__FINISHED(key, val) HASH__PASTE(hashtable_finished__ ## key ## __ ## val ## __, __LINE__)
  244. #define HASH__MAINLOOP(key, val) HASH__PASTE(hashtable_main_loop__ ## key ## __ ## val ## __, __LINE__)
  245. #define HT_EACH(obj, keyname, valtype, valname) \
  246. if(0) \
  247. HASH__FINISHED(keyname, val): ; \
  248. else \
  249. for(__typeof__((obj)->meta[0].keyType) keyname ;;) \
  250. for(valtype valname ;;) \
  251. for(void* HASH__ITER(keyname, val) = NULL ;;) \
  252. if(HT_next(obj, &(HASH__ITER(keyname, val)), &keyname, &valname)) \
  253. goto HASH__MAINLOOP(keyname, val); \
  254. else \
  255. while(1) \
  256. if(1) { \
  257. goto HASH__FINISHED(keyname, val); \
  258. } \
  259. else \
  260. while(1) \
  261. if(!HT_next(obj, &(HASH__ITER(keyname, val)), &keyname, &valname)) { \
  262. goto HASH__FINISHED(keyname, val); \
  263. } \
  264. else \
  265. HASH__MAINLOOP(keyname, val) :
  266. // { user block; not in macro }
  267. #define HT_EACHP(obj, keyname, valtype, valname) \
  268. if(0) \
  269. HASH__FINISHED(key, val): ; \
  270. else \
  271. for(__typeof__((obj)->meta[0].keyType) keyname ;;) \
  272. for(valtype* valname ;;) \
  273. for(void* HASH__ITER(key, val) = NULL ;;) \
  274. if(HT_nextp(obj, &(HASH__ITER(key, val)), &keyname, &valname)) \
  275. goto HASH__MAINLOOP(key, val); \
  276. else \
  277. while(1) \
  278. if(1) { \
  279. goto HASH__FINISHED(key, val); \
  280. } \
  281. else \
  282. while(1) \
  283. if(!HT_nextp(obj, &(HASH__ITER(key, val)), &keyname, &valname)) { \
  284. goto HASH__FINISHED(key, val); \
  285. } \
  286. else \
  287. HASH__MAINLOOP(key, val) :
  288. // { user block; not in macro }
  289. //
  290. #endif // __sti__hash_h__