THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

344 lines
13KB

  1. /* The MIT License
  2. Copyright (c) 2008, 2009, 2011 by Attractive Chaos <attractor@live.co.uk>
  3. Permission is hereby granted, free of charge, to any person obtaining
  4. a copy of this software and associated documentation files (the
  5. "Software"), to deal in the Software without restriction, including
  6. without limitation the rights to use, copy, modify, merge, publish,
  7. distribute, sublicense, and/or sell copies of the Software, and to
  8. permit persons to whom the Software is furnished to do so, subject to
  9. the following conditions:
  10. The above copyright notice and this permission notice shall be
  11. included in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  16. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  17. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  18. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. SOFTWARE.
  20. */
  21. #ifndef __AC_KHASH_H
  22. #define __AC_KHASH_H
  23. #include "cache.h"
  24. #include "hashmap.h"
  25. #define AC_VERSION_KHASH_H "0.2.8"
  26. typedef uint32_t khint32_t;
  27. typedef uint64_t khint64_t;
  28. typedef khint32_t khint_t;
  29. typedef khint_t khiter_t;
  30. #define __ac_isempty(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&2)
  31. #define __ac_isdel(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&1)
  32. #define __ac_iseither(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&3)
  33. #define __ac_set_isdel_false(flag, i) (flag[i>>4]&=~(1ul<<((i&0xfU)<<1)))
  34. #define __ac_set_isempty_false(flag, i) (flag[i>>4]&=~(2ul<<((i&0xfU)<<1)))
  35. #define __ac_set_isboth_false(flag, i) (flag[i>>4]&=~(3ul<<((i&0xfU)<<1)))
  36. #define __ac_set_isdel_true(flag, i) (flag[i>>4]|=1ul<<((i&0xfU)<<1))
  37. #define __ac_fsize(m) ((m) < 16? 1 : (m)>>4)
  38. #define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
  39. static inline khint_t __ac_X31_hash_string(const char *s)
  40. {
  41. khint_t h = (khint_t)*s;
  42. if (h) for (++s ; *s; ++s) h = (h << 5) - h + (khint_t)*s;
  43. return h;
  44. }
  45. #define kh_str_hash_func(key) __ac_X31_hash_string(key)
  46. #define kh_str_hash_equal(a, b) (strcmp(a, b) == 0)
  47. static const double __ac_HASH_UPPER = 0.77;
  48. #define __KHASH_TYPE(name, khkey_t, khval_t) \
  49. typedef struct { \
  50. khint_t n_buckets, size, n_occupied, upper_bound; \
  51. khint32_t *flags; \
  52. khkey_t *keys; \
  53. khval_t *vals; \
  54. } kh_##name##_t;
  55. #define __KHASH_PROTOTYPES(name, khkey_t, khval_t) \
  56. kh_##name##_t *kh_init_##name(void); \
  57. void kh_destroy_##name(kh_##name##_t *h); \
  58. void kh_clear_##name(kh_##name##_t *h); \
  59. khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key); \
  60. int kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets); \
  61. khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret); \
  62. void kh_del_##name(kh_##name##_t *h, khint_t x);
  63. #define __KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
  64. SCOPE kh_##name##_t *kh_init_##name(void) { \
  65. return (kh_##name##_t*)xcalloc(1, sizeof(kh_##name##_t)); \
  66. } \
  67. SCOPE void kh_release_##name(kh_##name##_t *h) \
  68. { \
  69. free(h->flags); \
  70. free((void *)h->keys); \
  71. free((void *)h->vals); \
  72. } \
  73. SCOPE void kh_destroy_##name(kh_##name##_t *h) \
  74. { \
  75. if (h) { \
  76. kh_release_##name(h); \
  77. free(h); \
  78. } \
  79. } \
  80. SCOPE void kh_clear_##name(kh_##name##_t *h) \
  81. { \
  82. if (h && h->flags) { \
  83. memset(h->flags, 0xaa, __ac_fsize(h->n_buckets) * sizeof(khint32_t)); \
  84. h->size = h->n_occupied = 0; \
  85. } \
  86. } \
  87. SCOPE khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key) \
  88. { \
  89. if (h->n_buckets) { \
  90. khint_t k, i, last, mask, step = 0; \
  91. mask = h->n_buckets - 1; \
  92. k = __hash_func(key); i = k & mask; \
  93. last = i; \
  94. while (!__ac_isempty(h->flags, i) && (__ac_isdel(h->flags, i) || !__hash_equal(h->keys[i], key))) { \
  95. i = (i + (++step)) & mask; \
  96. if (i == last) return h->n_buckets; \
  97. } \
  98. return __ac_iseither(h->flags, i)? h->n_buckets : i; \
  99. } else return 0; \
  100. } \
  101. SCOPE int kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets) \
  102. { /* This function uses 0.25*n_buckets bytes of working space instead of [sizeof(key_t+val_t)+.25]*n_buckets. */ \
  103. khint32_t *new_flags = NULL; \
  104. khint_t j = 1; \
  105. { \
  106. kroundup32(new_n_buckets); \
  107. if (new_n_buckets < 4) new_n_buckets = 4; \
  108. if (h->size >= (khint_t)(new_n_buckets * __ac_HASH_UPPER + 0.5)) j = 0; /* requested size is too small */ \
  109. else { /* hash table size to be changed (shrink or expand); rehash */ \
  110. ALLOC_ARRAY(new_flags, __ac_fsize(new_n_buckets)); \
  111. if (!new_flags) return -1; \
  112. memset(new_flags, 0xaa, __ac_fsize(new_n_buckets) * sizeof(khint32_t)); \
  113. if (h->n_buckets < new_n_buckets) { /* expand */ \
  114. REALLOC_ARRAY(h->keys, new_n_buckets); \
  115. if (kh_is_map) { \
  116. REALLOC_ARRAY(h->vals, new_n_buckets); \
  117. } \
  118. } /* otherwise shrink */ \
  119. } \
  120. } \
  121. if (j) { /* rehashing is needed */ \
  122. for (j = 0; j != h->n_buckets; ++j) { \
  123. if (__ac_iseither(h->flags, j) == 0) { \
  124. khkey_t key = h->keys[j]; \
  125. khval_t val; \
  126. khint_t new_mask; \
  127. new_mask = new_n_buckets - 1; \
  128. if (kh_is_map) val = h->vals[j]; \
  129. __ac_set_isdel_true(h->flags, j); \
  130. while (1) { /* kick-out process; sort of like in Cuckoo hashing */ \
  131. khint_t k, i, step = 0; \
  132. k = __hash_func(key); \
  133. i = k & new_mask; \
  134. while (!__ac_isempty(new_flags, i)) i = (i + (++step)) & new_mask; \
  135. __ac_set_isempty_false(new_flags, i); \
  136. if (i < h->n_buckets && __ac_iseither(h->flags, i) == 0) { /* kick out the existing element */ \
  137. { khkey_t tmp = h->keys[i]; h->keys[i] = key; key = tmp; } \
  138. if (kh_is_map) { khval_t tmp = h->vals[i]; h->vals[i] = val; val = tmp; } \
  139. __ac_set_isdel_true(h->flags, i); /* mark it as deleted in the old hash table */ \
  140. } else { /* write the element and jump out of the loop */ \
  141. h->keys[i] = key; \
  142. if (kh_is_map) h->vals[i] = val; \
  143. break; \
  144. } \
  145. } \
  146. } \
  147. } \
  148. if (h->n_buckets > new_n_buckets) { /* shrink the hash table */ \
  149. REALLOC_ARRAY(h->keys, new_n_buckets); \
  150. if (kh_is_map) REALLOC_ARRAY(h->vals, new_n_buckets); \
  151. } \
  152. free(h->flags); /* free the working space */ \
  153. h->flags = new_flags; \
  154. h->n_buckets = new_n_buckets; \
  155. h->n_occupied = h->size; \
  156. h->upper_bound = (khint_t)(h->n_buckets * __ac_HASH_UPPER + 0.5); \
  157. } \
  158. return 0; \
  159. } \
  160. SCOPE khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret) \
  161. { \
  162. khint_t x; \
  163. if (h->n_occupied >= h->upper_bound) { /* update the hash table */ \
  164. if (h->n_buckets > (h->size<<1)) { \
  165. if (kh_resize_##name(h, h->n_buckets - 1) < 0) { /* clear "deleted" elements */ \
  166. *ret = -1; return h->n_buckets; \
  167. } \
  168. } else if (kh_resize_##name(h, h->n_buckets + 1) < 0) { /* expand the hash table */ \
  169. *ret = -1; return h->n_buckets; \
  170. } \
  171. } /* TODO: to implement automatically shrinking; resize() already support shrinking */ \
  172. { \
  173. khint_t k, i, site, last, mask = h->n_buckets - 1, step = 0; \
  174. x = site = h->n_buckets; k = __hash_func(key); i = k & mask; \
  175. if (__ac_isempty(h->flags, i)) x = i; /* for speed up */ \
  176. else { \
  177. last = i; \
  178. while (!__ac_isempty(h->flags, i) && (__ac_isdel(h->flags, i) || !__hash_equal(h->keys[i], key))) { \
  179. if (__ac_isdel(h->flags, i)) site = i; \
  180. i = (i + (++step)) & mask; \
  181. if (i == last) { x = site; break; } \
  182. } \
  183. if (x == h->n_buckets) { \
  184. if (__ac_isempty(h->flags, i) && site != h->n_buckets) x = site; \
  185. else x = i; \
  186. } \
  187. } \
  188. } \
  189. if (__ac_isempty(h->flags, x)) { /* not present at all */ \
  190. h->keys[x] = key; \
  191. __ac_set_isboth_false(h->flags, x); \
  192. ++h->size; ++h->n_occupied; \
  193. *ret = 1; \
  194. } else if (__ac_isdel(h->flags, x)) { /* deleted */ \
  195. h->keys[x] = key; \
  196. __ac_set_isboth_false(h->flags, x); \
  197. ++h->size; \
  198. *ret = 2; \
  199. } else *ret = 0; /* Don't touch h->keys[x] if present and not deleted */ \
  200. return x; \
  201. } \
  202. SCOPE void kh_del_##name(kh_##name##_t *h, khint_t x) \
  203. { \
  204. if (x != h->n_buckets && !__ac_iseither(h->flags, x)) { \
  205. __ac_set_isdel_true(h->flags, x); \
  206. --h->size; \
  207. } \
  208. }
  209. #define KHASH_DECLARE(name, khkey_t, khval_t) \
  210. __KHASH_TYPE(name, khkey_t, khval_t) \
  211. __KHASH_PROTOTYPES(name, khkey_t, khval_t)
  212. #define KHASH_INIT2(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
  213. __KHASH_TYPE(name, khkey_t, khval_t) \
  214. __KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal)
  215. #define KHASH_INIT(name, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
  216. KHASH_INIT2(name, MAYBE_UNUSED static inline, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal)
  217. /* Other convenient macros... */
  218. /*! @function
  219. @abstract Test whether a bucket contains data.
  220. @param h Pointer to the hash table [khash_t(name)*]
  221. @param x Iterator to the bucket [khint_t]
  222. @return 1 if containing data; 0 otherwise [int]
  223. */
  224. #define kh_exist(h, x) (!__ac_iseither((h)->flags, (x)))
  225. /*! @function
  226. @abstract Get key given an iterator
  227. @param h Pointer to the hash table [khash_t(name)*]
  228. @param x Iterator to the bucket [khint_t]
  229. @return Key [type of keys]
  230. */
  231. #define kh_key(h, x) ((h)->keys[x])
  232. /*! @function
  233. @abstract Get value given an iterator
  234. @param h Pointer to the hash table [khash_t(name)*]
  235. @param x Iterator to the bucket [khint_t]
  236. @return Value [type of values]
  237. @discussion For hash sets, calling this results in segfault.
  238. */
  239. #define kh_val(h, x) ((h)->vals[x])
  240. /*! @function
  241. @abstract Alias of kh_val()
  242. */
  243. #define kh_value(h, x) ((h)->vals[x])
  244. /*! @function
  245. @abstract Get the start iterator
  246. @param h Pointer to the hash table [khash_t(name)*]
  247. @return The start iterator [khint_t]
  248. */
  249. #define kh_begin(h) (khint_t)(0)
  250. /*! @function
  251. @abstract Get the end iterator
  252. @param h Pointer to the hash table [khash_t(name)*]
  253. @return The end iterator [khint_t]
  254. */
  255. #define kh_end(h) ((h)->n_buckets)
  256. /*! @function
  257. @abstract Get the number of elements in the hash table
  258. @param h Pointer to the hash table [khash_t(name)*]
  259. @return Number of elements in the hash table [khint_t]
  260. */
  261. #define kh_size(h) ((h)->size)
  262. /*! @function
  263. @abstract Get the number of buckets in the hash table
  264. @param h Pointer to the hash table [khash_t(name)*]
  265. @return Number of buckets in the hash table [khint_t]
  266. */
  267. #define kh_n_buckets(h) ((h)->n_buckets)
  268. /*! @function
  269. @abstract Iterate over the entries in the hash table
  270. @param h Pointer to the hash table [khash_t(name)*]
  271. @param kvar Variable to which key will be assigned
  272. @param vvar Variable to which value will be assigned
  273. @param code Block of code to execute
  274. */
  275. #define kh_foreach(h, kvar, vvar, code) { khint_t __i; \
  276. for (__i = kh_begin(h); __i != kh_end(h); ++__i) { \
  277. if (!kh_exist(h,__i)) continue; \
  278. (kvar) = kh_key(h,__i); \
  279. (vvar) = kh_val(h,__i); \
  280. code; \
  281. } }
  282. /*! @function
  283. @abstract Iterate over the values in the hash table
  284. @param h Pointer to the hash table [khash_t(name)*]
  285. @param vvar Variable to which value will be assigned
  286. @param code Block of code to execute
  287. */
  288. #define kh_foreach_value(h, vvar, code) { khint_t __i; \
  289. for (__i = kh_begin(h); __i != kh_end(h); ++__i) { \
  290. if (!kh_exist(h,__i)) continue; \
  291. (vvar) = kh_val(h,__i); \
  292. code; \
  293. } }
  294. static inline unsigned int oidhash_by_value(struct object_id oid)
  295. {
  296. return oidhash(&oid);
  297. }
  298. static inline int oideq_by_value(struct object_id a, struct object_id b)
  299. {
  300. return oideq(&a, &b);
  301. }
  302. KHASH_INIT(oid_set, struct object_id, int, 0, oidhash_by_value, oideq_by_value)
  303. KHASH_INIT(oid_map, struct object_id, void *, 1, oidhash_by_value, oideq_by_value)
  304. KHASH_INIT(oid_pos, struct object_id, int, 1, oidhash_by_value, oideq_by_value)
  305. #endif /* __AC_KHASH_H */