hash.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. Copyright (C) 2012 Bitsquid AB
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #pragma once
  23. #include "array.h"
  24. #include "container_types.h"
  25. namespace crown {
  26. /// The hash function stores its data in a "list-in-an-array" where
  27. /// indices are used instead of pointers.
  28. ///
  29. /// When items are removed, the array-list is repacked to always keep
  30. /// it tightly ordered.
  31. ///
  32. /// @ingroup Containers
  33. namespace hash
  34. {
  35. /// Returns true if the specified key exists in the hash.
  36. template<typename T> bool has(const Hash<T> &h, uint64_t key);
  37. /// Returns the value stored for the specified key, or deffault if the key
  38. /// does not exist in the hash.
  39. template<typename T> const T &get(const Hash<T> &h, uint64_t key, const T &deffault);
  40. /// Sets the value for the key.
  41. template<typename T> void set(Hash<T> &h, uint64_t key, const T &value);
  42. /// Removes the key from the hash if it exists.
  43. template<typename T> void remove(Hash<T> &h, uint64_t key);
  44. /// Resizes the hash lookup table to the specified size.
  45. /// (The table will grow automatically when 70 % full.)
  46. template<typename T> void reserve(Hash<T> &h, uint32_t size);
  47. /// Remove all elements from the hash.
  48. template<typename T> void clear(Hash<T> &h);
  49. /// Returns a pointer to the first entry in the hash table, can be used to
  50. /// efficiently iterate over the elements (in random order).
  51. template<typename T> const typename Hash<T>::Entry *begin(const Hash<T> &h);
  52. template<typename T> const typename Hash<T>::Entry *end(const Hash<T> &h);
  53. }
  54. /// Functions to manipulate Hash as a multi-hash.
  55. ///
  56. /// @ingroup Containers
  57. namespace multi_hash
  58. {
  59. /// Finds the first entry with the specified key.
  60. template<typename T> const typename Hash<T>::Entry *find_first(const Hash<T> &h, uint64_t key);
  61. /// Finds the next entry with the same key as e.
  62. template<typename T> const typename Hash<T>::Entry *find_next(const Hash<T> &h, const typename Hash<T>::Entry *e);
  63. /// Returns the number of entries with the key.
  64. template<typename T> uint32_t count(const Hash<T> &h, uint64_t key);
  65. /// Returns all the entries with the specified key.
  66. /// Use a TempAllocator for the array to avoid allocating memory.
  67. template<typename T> void get(const Hash<T> &h, uint64_t key, Array<T> &items);
  68. /// Inserts the value as an aditional value for the key.
  69. template<typename T> void insert(Hash<T> &h, uint64_t key, const T &value);
  70. /// Removes the specified entry.
  71. template<typename T> void remove(Hash<T> &h, const typename Hash<T>::Entry *e);
  72. /// Removes all entries with the specified key.
  73. template<typename T> void remove_all(Hash<T> &h, uint64_t key);
  74. }
  75. namespace hash_internal
  76. {
  77. const uint32_t END_OF_LIST = 0xffffffffu;
  78. struct FindResult
  79. {
  80. uint32_t hash_i;
  81. uint32_t data_prev;
  82. uint32_t data_i;
  83. };
  84. template<typename T> uint32_t add_entry(Hash<T> &h, uint64_t key)
  85. {
  86. typename Hash<T>::Entry e;
  87. e.key = key;
  88. e.next = END_OF_LIST;
  89. uint32_t ei = array::size(h._data);
  90. array::push_back(h._data, e);
  91. return ei;
  92. }
  93. template<typename T> FindResult find(const Hash<T> &h, uint64_t key)
  94. {
  95. FindResult fr;
  96. fr.hash_i = END_OF_LIST;
  97. fr.data_prev = END_OF_LIST;
  98. fr.data_i = END_OF_LIST;
  99. if (array::size(h._hash) == 0)
  100. return fr;
  101. fr.hash_i = key % array::size(h._hash);
  102. fr.data_i = h._hash[fr.hash_i];
  103. while (fr.data_i != END_OF_LIST) {
  104. if (h._data[fr.data_i].key == key)
  105. return fr;
  106. fr.data_prev = fr.data_i;
  107. fr.data_i = h._data[fr.data_i].next;
  108. }
  109. return fr;
  110. }
  111. template<typename T> FindResult find(const Hash<T> &h, const typename Hash<T>::Entry *e)
  112. {
  113. FindResult fr;
  114. fr.hash_i = END_OF_LIST;
  115. fr.data_prev = END_OF_LIST;
  116. fr.data_i = END_OF_LIST;
  117. if (array::size(h._hash) == 0)
  118. return fr;
  119. fr.hash_i = e->key % array::size(h._hash);
  120. fr.data_i = h._hash[fr.hash_i];
  121. while (fr.data_i != END_OF_LIST) {
  122. if (&h._data[fr.data_i] == e)
  123. return fr;
  124. fr.data_prev = fr.data_i;
  125. fr.data_i = h._data[fr.data_i].next;
  126. }
  127. return fr;
  128. }
  129. template<typename T> void erase(Hash<T> &h, const FindResult &fr)
  130. {
  131. if (fr.data_prev == END_OF_LIST)
  132. h._hash[fr.hash_i] = h._data[fr.data_i].next;
  133. else
  134. h._data[fr.data_prev].next = h._data[fr.data_i].next;
  135. if (fr.data_i == array::size(h._data) - 1) {
  136. array::pop_back(h._data);
  137. return;
  138. }
  139. h._data[fr.data_i] = h._data[array::size(h._data) - 1];
  140. FindResult last = find(h, h._data[fr.data_i].key);
  141. if (last.data_prev != END_OF_LIST)
  142. h._data[last.data_prev].next = fr.data_i;
  143. else
  144. h._hash[last.hash_i] = fr.data_i;
  145. }
  146. template<typename T> uint32_t find_or_fail(const Hash<T> &h, uint64_t key)
  147. {
  148. return find(h, key).data_i;
  149. }
  150. template<typename T> uint32_t find_or_make(Hash<T> &h, uint64_t key)
  151. {
  152. const FindResult fr = find(h, key);
  153. if (fr.data_i != END_OF_LIST)
  154. return fr.data_i;
  155. uint32_t i = add_entry(h, key);
  156. if (fr.data_prev == END_OF_LIST)
  157. h._hash[fr.hash_i] = i;
  158. else
  159. h._data[fr.data_prev].next = i;
  160. return i;
  161. }
  162. template<typename T> uint32_t make(Hash<T> &h, uint64_t key)
  163. {
  164. const FindResult fr = find(h, key);
  165. const uint32_t i = add_entry(h, key);
  166. if (fr.data_prev == END_OF_LIST)
  167. h._hash[fr.hash_i] = i;
  168. else
  169. h._data[fr.data_prev].next = i;
  170. h._data[i].next = fr.data_i;
  171. return i;
  172. }
  173. template<typename T> void find_and_erase(Hash<T> &h, uint64_t key)
  174. {
  175. const FindResult fr = find(h, key);
  176. if (fr.data_i != END_OF_LIST)
  177. erase(h, fr);
  178. }
  179. template<typename T> void rehash(Hash<T> &h, uint32_t new_size)
  180. {
  181. Hash<T> nh(*h._hash.m_allocator);
  182. array::resize(nh._hash, new_size);
  183. array::reserve(nh._data, array::size(h._data));
  184. for (uint32_t i=0; i<new_size; ++i)
  185. nh._hash[i] = END_OF_LIST;
  186. for (uint32_t i=0; i<array::size(h._data); ++i) {
  187. const typename Hash<T>::Entry &e = h._data[i];
  188. multi_hash::insert(nh, e.key, e.value);
  189. }
  190. Hash<T> empty(*h._hash.m_allocator);
  191. h.~Hash<T>();
  192. memcpy(&h, &nh, sizeof(Hash<T>));
  193. memcpy(&nh, &empty, sizeof(Hash<T>));
  194. }
  195. template<typename T> bool full(const Hash<T> &h)
  196. {
  197. const float max_load_factor = 0.7f;
  198. return array::size(h._data) >= array::size(h._hash) * max_load_factor;
  199. }
  200. template<typename T> void grow(Hash<T> &h)
  201. {
  202. const uint32_t new_size = array::size(h._data) * 2 + 10;
  203. rehash(h, new_size);
  204. }
  205. }
  206. namespace hash
  207. {
  208. template<typename T> bool has(const Hash<T> &h, uint64_t key)
  209. {
  210. return hash_internal::find_or_fail(h, key) != hash_internal::END_OF_LIST;
  211. }
  212. template<typename T> const T &get(const Hash<T> &h, uint64_t key, const T &deffault)
  213. {
  214. const uint32_t i = hash_internal::find_or_fail(h, key);
  215. return i == hash_internal::END_OF_LIST ? deffault : h._data[i].value;
  216. }
  217. template<typename T> void set(Hash<T> &h, uint64_t key, const T &value)
  218. {
  219. if (array::size(h._hash) == 0)
  220. hash_internal::grow(h);
  221. const uint32_t i = hash_internal::find_or_make(h, key);
  222. h._data[i].value = value;
  223. if (hash_internal::full(h))
  224. hash_internal::grow(h);
  225. }
  226. template<typename T> void remove(Hash<T> &h, uint64_t key)
  227. {
  228. hash_internal::find_and_erase(h, key);
  229. }
  230. template<typename T> void reserve(Hash<T> &h, uint32_t size)
  231. {
  232. hash_internal::rehash(h, size);
  233. }
  234. template<typename T> void clear(Hash<T> &h)
  235. {
  236. array::clear(h._data);
  237. array::clear(h._hash);
  238. }
  239. template<typename T> const typename Hash<T>::Entry *begin(const Hash<T> &h)
  240. {
  241. return array::begin(h._data);
  242. }
  243. template<typename T> const typename Hash<T>::Entry *end(const Hash<T> &h)
  244. {
  245. return array::end(h._data);
  246. }
  247. }
  248. namespace multi_hash
  249. {
  250. template<typename T> const typename Hash<T>::Entry *find_first(const Hash<T> &h, uint64_t key)
  251. {
  252. const uint32_t i = hash_internal::find_or_fail(h, key);
  253. return i == hash_internal::END_OF_LIST ? 0 : &h._data[i];
  254. }
  255. template<typename T> const typename Hash<T>::Entry *find_next(const Hash<T> &h, const typename Hash<T>::Entry *e)
  256. {
  257. uint32_t i = e->next;
  258. while (i != hash_internal::END_OF_LIST) {
  259. if (h._data[i].key == e->key)
  260. return &h._data[i];
  261. i = h._data[i].next;
  262. }
  263. return 0;
  264. }
  265. template<typename T> uint32_t count(const Hash<T> &h, uint64_t key)
  266. {
  267. uint32_t i = 0;
  268. const typename Hash<T>::Entry *e = find_first(h, key);
  269. while (e) {
  270. ++i;
  271. e = find_next(h, e);
  272. }
  273. return i;
  274. }
  275. template<typename T> void get(const Hash<T> &h, uint64_t key, Array<T> &items)
  276. {
  277. const typename Hash<T>::Entry *e = find_first(h, key);
  278. while (e) {
  279. array::push_back(items, e->value);
  280. e = find_next(h, e);
  281. }
  282. }
  283. template<typename T> void insert(Hash<T> &h, uint64_t key, const T &value)
  284. {
  285. if (array::size(h._hash) == 0)
  286. hash_internal::grow(h);
  287. const uint32_t i = hash_internal::make(h, key);
  288. h._data[i].value = value;
  289. if (hash_internal::full(h))
  290. hash_internal::grow(h);
  291. }
  292. template<typename T> void remove(Hash<T> &h, const typename Hash<T>::Entry *e)
  293. {
  294. const hash_internal::FindResult fr = hash_internal::find(h, e);
  295. if (fr.data_i != hash_internal::END_OF_LIST)
  296. hash_internal::erase(h, fr);
  297. }
  298. template<typename T> void remove_all(Hash<T> &h, uint64_t key)
  299. {
  300. while (hash::has(h, key))
  301. hash::remove(h, key);
  302. }
  303. }
  304. template <typename T> Hash<T>::Hash(Allocator &a) :
  305. _hash(a), _data(a)
  306. {
  307. }
  308. } // namespace crown