Hashtable.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. htop - Hashtable.c
  3. (C) 2004-2011 Hisham H. Muhammad
  4. Released under the GNU GPLv2+, see the COPYING file
  5. in the source distribution for its full text.
  6. */
  7. #include "config.h" // IWYU pragma: keep
  8. #include "Hashtable.h"
  9. #include <assert.h>
  10. #include <stdint.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "CRT.h"
  14. #include "Macros.h"
  15. #include "XUtils.h"
  16. #ifndef NDEBUG
  17. #include <stdio.h>
  18. #endif
  19. typedef struct HashtableItem_ {
  20. ht_key_t key;
  21. size_t probe;
  22. void* value;
  23. } HashtableItem;
  24. struct Hashtable_ {
  25. size_t size;
  26. HashtableItem* buckets;
  27. size_t items;
  28. bool owner;
  29. };
  30. #ifndef NDEBUG
  31. static void Hashtable_dump(const Hashtable* this) {
  32. fprintf(stderr, "Hashtable %p: size=%zu items=%zu owner=%s\n",
  33. (const void*)this,
  34. this->size,
  35. this->items,
  36. this->owner ? "yes" : "no");
  37. size_t items = 0;
  38. for (size_t i = 0; i < this->size; i++) {
  39. fprintf(stderr, " item %5zu: key = %5u probe = %2zu value = %p\n",
  40. i,
  41. this->buckets[i].key,
  42. this->buckets[i].probe,
  43. this->buckets[i].value);
  44. if (this->buckets[i].value)
  45. items++;
  46. }
  47. fprintf(stderr, "Hashtable %p: items=%zu counted=%zu\n",
  48. (const void*)this,
  49. this->items,
  50. items);
  51. }
  52. static bool Hashtable_isConsistent(const Hashtable* this) {
  53. size_t items = 0;
  54. for (size_t i = 0; i < this->size; i++) {
  55. if (this->buckets[i].value)
  56. items++;
  57. }
  58. bool res = items == this->items;
  59. if (!res)
  60. Hashtable_dump(this);
  61. return res;
  62. }
  63. size_t Hashtable_count(const Hashtable* this) {
  64. size_t items = 0;
  65. for (size_t i = 0; i < this->size; i++) {
  66. if (this->buckets[i].value)
  67. items++;
  68. }
  69. assert(items == this->items);
  70. return items;
  71. }
  72. #endif /* NDEBUG */
  73. /* https://oeis.org/A014234 */
  74. static const uint64_t OEISprimes[] = {
  75. 7, 13, 31, 61, 127, 251, 509, 1021, 2039, 4093, 8191,
  76. 16381, 32749, 65521, 131071, 262139, 524287, 1048573,
  77. 2097143, 4194301, 8388593, 16777213, 33554393,
  78. 67108859, 134217689, 268435399, 536870909, 1073741789,
  79. 2147483647, 4294967291, 8589934583, 17179869143,
  80. 34359738337, 68719476731, 137438953447
  81. };
  82. static size_t nextPrime(size_t n) {
  83. /* on 32-bit make sure we do not return primes not fitting in size_t */
  84. for (size_t i = 0; i < ARRAYSIZE(OEISprimes) && OEISprimes[i] < SIZE_MAX; i++) {
  85. if (n <= OEISprimes[i])
  86. return OEISprimes[i];
  87. }
  88. CRT_fatalError("Hashtable: no prime found");
  89. }
  90. Hashtable* Hashtable_new(size_t size, bool owner) {
  91. size = size ? nextPrime(size) : 13;
  92. Hashtable* this = xMalloc(sizeof(Hashtable));
  93. *this = (Hashtable) {
  94. .items = 0,
  95. .size = size,
  96. .buckets = xCalloc(size, sizeof(HashtableItem)),
  97. .owner = owner,
  98. };
  99. assert(Hashtable_isConsistent(this));
  100. return this;
  101. }
  102. void Hashtable_delete(Hashtable* this) {
  103. Hashtable_clear(this);
  104. free(this->buckets);
  105. free(this);
  106. }
  107. void Hashtable_clear(Hashtable* this) {
  108. assert(Hashtable_isConsistent(this));
  109. if (this->owner)
  110. for (size_t i = 0; i < this->size; i++)
  111. free(this->buckets[i].value);
  112. memset(this->buckets, 0, this->size * sizeof(HashtableItem));
  113. this->items = 0;
  114. assert(Hashtable_isConsistent(this));
  115. }
  116. static void insert(Hashtable* this, ht_key_t key, void* value) {
  117. size_t index = key % this->size;
  118. size_t probe = 0;
  119. #ifndef NDEBUG
  120. size_t origIndex = index;
  121. #endif
  122. for (;;) {
  123. if (!this->buckets[index].value) {
  124. this->items++;
  125. this->buckets[index].key = key;
  126. this->buckets[index].probe = probe;
  127. this->buckets[index].value = value;
  128. return;
  129. }
  130. if (this->buckets[index].key == key) {
  131. if (this->owner && this->buckets[index].value != value)
  132. free(this->buckets[index].value);
  133. this->buckets[index].value = value;
  134. return;
  135. }
  136. /* Robin Hood swap */
  137. if (probe > this->buckets[index].probe) {
  138. HashtableItem tmp = this->buckets[index];
  139. this->buckets[index].key = key;
  140. this->buckets[index].probe = probe;
  141. this->buckets[index].value = value;
  142. key = tmp.key;
  143. probe = tmp.probe;
  144. value = tmp.value;
  145. }
  146. index = (index + 1) % this->size;
  147. probe++;
  148. assert(index != origIndex);
  149. }
  150. }
  151. void Hashtable_setSize(Hashtable* this, size_t size) {
  152. assert(Hashtable_isConsistent(this));
  153. if (size <= this->items)
  154. return;
  155. size_t newSize = nextPrime(size);
  156. if (newSize == this->size)
  157. return;
  158. HashtableItem* oldBuckets = this->buckets;
  159. size_t oldSize = this->size;
  160. this->size = newSize;
  161. this->buckets = (HashtableItem*) xCalloc(this->size, sizeof(HashtableItem));
  162. this->items = 0;
  163. /* rehash */
  164. for (size_t i = 0; i < oldSize; i++) {
  165. if (!oldBuckets[i].value)
  166. continue;
  167. insert(this, oldBuckets[i].key, oldBuckets[i].value);
  168. }
  169. free(oldBuckets);
  170. assert(Hashtable_isConsistent(this));
  171. }
  172. void Hashtable_put(Hashtable* this, ht_key_t key, void* value) {
  173. assert(Hashtable_isConsistent(this));
  174. assert(this->size > 0);
  175. assert(value);
  176. /* grow on load-factor > 0.7 */
  177. if (10 * this->items > 7 * this->size) {
  178. if (SIZE_MAX / 2 < this->size)
  179. CRT_fatalError("Hashtable: size overflow");
  180. Hashtable_setSize(this, 2 * this->size);
  181. }
  182. insert(this, key, value);
  183. assert(Hashtable_isConsistent(this));
  184. assert(Hashtable_get(this, key) != NULL);
  185. assert(this->size > this->items);
  186. }
  187. void* Hashtable_remove(Hashtable* this, ht_key_t key) {
  188. size_t index = key % this->size;
  189. size_t probe = 0;
  190. #ifndef NDEBUG
  191. size_t origIndex = index;
  192. #endif
  193. assert(Hashtable_isConsistent(this));
  194. void* res = NULL;
  195. while (this->buckets[index].value) {
  196. if (this->buckets[index].key == key) {
  197. if (this->owner) {
  198. free(this->buckets[index].value);
  199. } else {
  200. res = this->buckets[index].value;
  201. }
  202. size_t next = (index + 1) % this->size;
  203. while (this->buckets[next].value && this->buckets[next].probe > 0) {
  204. this->buckets[index] = this->buckets[next];
  205. this->buckets[index].probe -= 1;
  206. index = next;
  207. next = (index + 1) % this->size;
  208. }
  209. /* set empty after backward shifting */
  210. this->buckets[index].value = NULL;
  211. this->items--;
  212. break;
  213. }
  214. if (this->buckets[index].probe < probe)
  215. break;
  216. index = (index + 1) % this->size;
  217. probe++;
  218. assert(index != origIndex);
  219. }
  220. assert(Hashtable_isConsistent(this));
  221. assert(Hashtable_get(this, key) == NULL);
  222. /* shrink on load-factor < 0.125 */
  223. if (8 * this->items < this->size)
  224. Hashtable_setSize(this, this->size / 3); /* account for nextPrime rounding up */
  225. return res;
  226. }
  227. void* Hashtable_get(Hashtable* this, ht_key_t key) {
  228. size_t index = key % this->size;
  229. size_t probe = 0;
  230. void* res = NULL;
  231. #ifndef NDEBUG
  232. size_t origIndex = index;
  233. #endif
  234. assert(Hashtable_isConsistent(this));
  235. while (this->buckets[index].value) {
  236. if (this->buckets[index].key == key) {
  237. res = this->buckets[index].value;
  238. break;
  239. }
  240. if (this->buckets[index].probe < probe)
  241. break;
  242. index = (index + 1) != this->size ? (index + 1) : 0;
  243. probe++;
  244. assert(index != origIndex);
  245. }
  246. return res;
  247. }
  248. void Hashtable_foreach(Hashtable* this, Hashtable_PairFunction f, void* userData) {
  249. assert(Hashtable_isConsistent(this));
  250. for (size_t i = 0; i < this->size; i++) {
  251. HashtableItem* walk = &this->buckets[i];
  252. if (walk->value)
  253. f(walk->key, walk->value, userData);
  254. }
  255. assert(Hashtable_isConsistent(this));
  256. }