isl_hash.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright 2008-2009 Katholieke Universiteit Leuven
  3. *
  4. * Use of this software is governed by the MIT license
  5. *
  6. * Written by Sven Verdoolaege, K.U.Leuven, Departement
  7. * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
  8. */
  9. #include <stdlib.h>
  10. #include <isl/hash.h>
  11. #include <isl/ctx.h>
  12. #include "isl_config.h"
  13. uint32_t isl_hash_string(uint32_t hash, const char *s)
  14. {
  15. for (; *s; s++)
  16. isl_hash_byte(hash, *s);
  17. return hash;
  18. }
  19. uint32_t isl_hash_mem(uint32_t hash, const void *p, size_t len)
  20. {
  21. int i;
  22. const char *s = p;
  23. for (i = 0; i < len; ++i)
  24. isl_hash_byte(hash, s[i]);
  25. return hash;
  26. }
  27. static unsigned int round_up(unsigned int v)
  28. {
  29. int old_v = v;
  30. while (v) {
  31. old_v = v;
  32. v ^= v & -v;
  33. }
  34. return old_v << 1;
  35. }
  36. int isl_hash_table_init(struct isl_ctx *ctx, struct isl_hash_table *table,
  37. int min_size)
  38. {
  39. size_t size;
  40. if (!table)
  41. return -1;
  42. if (min_size < 2)
  43. min_size = 2;
  44. table->bits = ffs(round_up(4 * (min_size + 1) / 3 - 1)) - 1;
  45. table->n = 0;
  46. size = 1 << table->bits;
  47. table->entries = isl_calloc_array(ctx, struct isl_hash_table_entry,
  48. size);
  49. if (!table->entries)
  50. return -1;
  51. return 0;
  52. }
  53. /* Dummy comparison function that always returns false.
  54. */
  55. static isl_bool no(const void *entry, const void *val)
  56. {
  57. return isl_bool_false;
  58. }
  59. /* Extend "table" to twice its size.
  60. * Return 0 on success and -1 on error.
  61. *
  62. * We reuse isl_hash_table_find to create entries in the extended table.
  63. * Since all entries in the original table are assumed to be different,
  64. * there is no need to compare them against each other.
  65. */
  66. static int grow_table(struct isl_ctx *ctx, struct isl_hash_table *table)
  67. {
  68. int n;
  69. size_t old_size, size;
  70. struct isl_hash_table_entry *entries;
  71. uint32_t h;
  72. entries = table->entries;
  73. old_size = 1 << table->bits;
  74. size = 2 * old_size;
  75. table->entries = isl_calloc_array(ctx, struct isl_hash_table_entry,
  76. size);
  77. if (!table->entries) {
  78. table->entries = entries;
  79. return -1;
  80. }
  81. n = table->n;
  82. table->n = 0;
  83. table->bits++;
  84. for (h = 0; h < old_size; ++h) {
  85. struct isl_hash_table_entry *entry;
  86. if (!entries[h].data)
  87. continue;
  88. entry = isl_hash_table_find(ctx, table, entries[h].hash,
  89. &no, NULL, 1);
  90. if (!entry) {
  91. table->bits--;
  92. free(table->entries);
  93. table->entries = entries;
  94. table->n = n;
  95. return -1;
  96. }
  97. *entry = entries[h];
  98. }
  99. free(entries);
  100. return 0;
  101. }
  102. struct isl_hash_table *isl_hash_table_alloc(struct isl_ctx *ctx, int min_size)
  103. {
  104. struct isl_hash_table *table = NULL;
  105. table = isl_alloc_type(ctx, struct isl_hash_table);
  106. if (isl_hash_table_init(ctx, table, min_size))
  107. goto error;
  108. return table;
  109. error:
  110. isl_hash_table_free(ctx, table);
  111. return NULL;
  112. }
  113. void isl_hash_table_clear(struct isl_hash_table *table)
  114. {
  115. if (!table)
  116. return;
  117. free(table->entries);
  118. }
  119. void isl_hash_table_free(struct isl_ctx *ctx, struct isl_hash_table *table)
  120. {
  121. if (!table)
  122. return;
  123. isl_hash_table_clear(table);
  124. free(table);
  125. }
  126. /* A dummy entry that is used by isl_hash_table_find
  127. * to make a distinction between a missing entry and an error condition.
  128. */
  129. static struct isl_hash_table_entry none = { 0, NULL };
  130. struct isl_hash_table_entry *isl_hash_table_entry_none = &none;
  131. struct isl_hash_table_entry *isl_hash_table_find(struct isl_ctx *ctx,
  132. struct isl_hash_table *table,
  133. uint32_t key_hash,
  134. isl_bool (*eq)(const void *entry, const void *val),
  135. const void *val, int reserve)
  136. {
  137. size_t size;
  138. uint32_t h, key_bits;
  139. key_bits = isl_hash_bits(key_hash, table->bits);
  140. size = 1 << table->bits;
  141. for (h = key_bits; table->entries[h].data; h = (h+1) % size) {
  142. isl_bool equal;
  143. if (table->entries[h].hash != key_hash)
  144. continue;
  145. equal = eq(table->entries[h].data, val);
  146. if (equal < 0)
  147. return NULL;
  148. if (equal)
  149. return &table->entries[h];
  150. }
  151. if (!reserve)
  152. return isl_hash_table_entry_none;
  153. if (4 * table->n >= 3 * size) {
  154. if (grow_table(ctx, table) < 0)
  155. return NULL;
  156. return isl_hash_table_find(ctx, table, key_hash, eq, val, 1);
  157. }
  158. table->n++;
  159. table->entries[h].hash = key_hash;
  160. return &table->entries[h];
  161. }
  162. isl_stat isl_hash_table_foreach(isl_ctx *ctx, struct isl_hash_table *table,
  163. isl_stat (*fn)(void **entry, void *user), void *user)
  164. {
  165. size_t size;
  166. uint32_t h;
  167. if (!table->entries)
  168. return isl_stat_error;
  169. size = 1 << table->bits;
  170. for (h = 0; h < size; ++ h)
  171. if (table->entries[h].data &&
  172. fn(&table->entries[h].data, user) < 0)
  173. return isl_stat_error;
  174. return isl_stat_ok;
  175. }
  176. void isl_hash_table_remove(struct isl_ctx *ctx,
  177. struct isl_hash_table *table,
  178. struct isl_hash_table_entry *entry)
  179. {
  180. int h, h2;
  181. size_t size;
  182. if (!table || !entry)
  183. return;
  184. size = 1 << table->bits;
  185. h = entry - table->entries;
  186. isl_assert(ctx, h >= 0 && h < size, return);
  187. for (h2 = h+1; table->entries[h2 % size].data; h2++) {
  188. uint32_t bits = isl_hash_bits(table->entries[h2 % size].hash,
  189. table->bits);
  190. uint32_t offset = (size + bits - (h+1)) % size;
  191. if (offset <= h2 - (h+1))
  192. continue;
  193. *entry = table->entries[h2 % size];
  194. h = h2;
  195. entry = &table->entries[h % size];
  196. }
  197. entry->hash = 0;
  198. entry->data = NULL;
  199. table->n--;
  200. }