hash_map.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #pragma once
  2. #include <types/allocator.hpp>
  3. #include <types/cplusplus.hpp>
  4. #include <types/list.hpp>
  5. #include <types/stdint.h>
  6. #include <types/string.hpp>
  7. #include <types/types.h>
  8. #include <types/vector.hpp>
  9. namespace types {
  10. // taken from linux
  11. constexpr uint32_t GOLDEN_RATIO_32 = 0x61C88647;
  12. // constexpr uint64_t GOLDEN_RATIO_64 = 0x61C8864680B583EBull;
  13. static constexpr uint32_t _hash32(uint32_t val)
  14. {
  15. return val * GOLDEN_RATIO_32;
  16. }
  17. static constexpr uint32_t hash32(uint32_t val, uint32_t bits)
  18. {
  19. // higher bits are more random
  20. return _hash32(val) >> (32 - bits);
  21. }
  22. template <typename T>
  23. struct linux_hasher {
  24. static constexpr uint32_t hash(const T& val, uint32_t bits)
  25. {
  26. return hash32(val, bits);
  27. }
  28. };
  29. template <typename T>
  30. struct linux_hasher<T*> {
  31. static constexpr uint32_t hash(T* val, uint32_t bits)
  32. {
  33. return hash32(reinterpret_cast<uint32_t>(val), bits);
  34. }
  35. };
  36. template <typename T>
  37. struct string_hasher {
  38. static constexpr uint32_t hash(T, uint32_t)
  39. {
  40. static_assert(types::template_false_type<T>::value, "string hasher does not support this type");
  41. return (uint32_t)0;
  42. }
  43. };
  44. template <>
  45. struct string_hasher<const char*> {
  46. static constexpr uint32_t hash(const char* str, uint32_t bits)
  47. {
  48. constexpr uint32_t seed = 131;
  49. uint32_t hash = 0;
  50. while (*str)
  51. hash = hash * seed + (*str++);
  52. return hash32(hash, bits);
  53. }
  54. };
  55. template <template <typename> class Allocator>
  56. struct string_hasher<const types::string<Allocator>&> {
  57. static inline constexpr uint32_t hash(const types::string<Allocator>& str, uint32_t bits)
  58. {
  59. return string_hasher<const char*>::hash(str.c_str(), bits);
  60. }
  61. };
  62. template <template <typename> class Allocator>
  63. struct string_hasher<types::string<Allocator>&&> {
  64. static inline constexpr uint32_t hash(types::string<Allocator>&& str, uint32_t bits)
  65. {
  66. return string_hasher<const char*>::hash(str.c_str(), bits);
  67. }
  68. };
  69. template <class Hasher, typename Value>
  70. struct hasher_traits {
  71. using hash_t = size_t;
  72. using length_t = size_t;
  73. static constexpr hash_t hash(Value val, length_t bits)
  74. {
  75. return Hasher::hash(val, bits);
  76. }
  77. };
  78. template <typename Key, typename Value, typename Hasher, template <typename _T> class Allocator = types::kernel_allocator>
  79. class hash_map {
  80. public:
  81. struct pair;
  82. template <typename Pointer>
  83. class iterator;
  84. using key_type = Key;
  85. using value_type = Value;
  86. using pair_type = pair;
  87. using size_type = size_t;
  88. using difference_type = ssize_t;
  89. using iterator_type = iterator<pair_type*>;
  90. using const_iterator_type = iterator<const pair_type*>;
  91. using bucket_type = list<pair, Allocator>;
  92. using bucket_array_type = vector<bucket_type, Allocator>;
  93. static constexpr size_type INITIAL_BUCKETS_ALLOCATED = 64;
  94. public:
  95. struct pair {
  96. const key_type key;
  97. value_type value;
  98. pair(void) = delete;
  99. pair(const key_type _key, value_type _val)
  100. : key(_key)
  101. , value(_val)
  102. {
  103. }
  104. bool operator==(const pair& p)
  105. {
  106. return key == p.key;
  107. }
  108. };
  109. template <typename Pointer>
  110. class iterator {
  111. public:
  112. using _Value = typename traits::remove_pointer<Pointer>::type;
  113. using Reference = typename traits::add_reference<_Value>::type;
  114. public:
  115. iterator(const iterator& iter) noexcept
  116. : p(iter.p)
  117. {
  118. }
  119. iterator(iterator&& iter) noexcept
  120. : p(iter.p)
  121. {
  122. iter.p = nullptr;
  123. }
  124. iterator& operator=(const iterator& iter)
  125. {
  126. p = iter.p;
  127. return *this;
  128. }
  129. explicit iterator(Pointer p) noexcept
  130. : p(p)
  131. {
  132. }
  133. bool operator==(const iterator& iter) noexcept
  134. {
  135. return this->p == iter.p;
  136. }
  137. bool operator!=(const iterator& iter) noexcept
  138. {
  139. return !(*this == iter);
  140. }
  141. bool operator!()
  142. {
  143. return !p;
  144. }
  145. Reference operator*() const noexcept
  146. {
  147. return *p;
  148. }
  149. Pointer operator->() const noexcept
  150. {
  151. return p;
  152. }
  153. protected:
  154. Pointer p;
  155. };
  156. private:
  157. bucket_array_type buckets;
  158. protected:
  159. constexpr uint32_t hash_length(void) const
  160. {
  161. switch (buckets.capacity()) {
  162. case 32:
  163. return 5;
  164. case 64:
  165. return 6;
  166. case 128:
  167. return 7;
  168. case 256:
  169. return 8;
  170. // TODO
  171. default:
  172. return 9;
  173. }
  174. }
  175. public:
  176. explicit hash_map(void)
  177. : buckets(INITIAL_BUCKETS_ALLOCATED)
  178. {
  179. for (size_type i = 0; i < INITIAL_BUCKETS_ALLOCATED; ++i)
  180. buckets.emplace_back();
  181. }
  182. hash_map(const hash_map& v)
  183. : buckets(v.buckets)
  184. {
  185. }
  186. hash_map(hash_map&& v)
  187. : buckets(move(v.buckets))
  188. {
  189. }
  190. ~hash_map()
  191. {
  192. buckets.clear();
  193. }
  194. void insert(const pair& p)
  195. {
  196. auto hash_value = hasher_traits<Hasher, key_type>::hash(p.key, hash_length());
  197. buckets.at(hash_value).push_back(p);
  198. }
  199. void insert(pair&& p)
  200. {
  201. auto hash_value = hasher_traits<Hasher, key_type>::hash(p.key, hash_length());
  202. buckets.at(hash_value).push_back(move(p));
  203. }
  204. void insert(const key_type& key, const value_type& val)
  205. {
  206. insert(pair { key, val });
  207. }
  208. void remove(const key_type& key)
  209. {
  210. auto hash_value = hasher_traits<Hasher, key_type>::hash(key, hash_length());
  211. auto& bucket = buckets.at(hash_value);
  212. for (auto iter = bucket.begin(); iter != bucket.end(); ++iter) {
  213. if (iter->key == key) {
  214. bucket.erase(iter);
  215. return;
  216. }
  217. }
  218. }
  219. iterator_type find(const key_type& key)
  220. {
  221. auto hash_value = hasher_traits<Hasher, key_type>::hash(key, hash_length());
  222. auto& bucket = buckets.at(hash_value);
  223. for (auto& item : bucket) {
  224. if (key == item.key)
  225. return iterator_type(&item);
  226. }
  227. return iterator_type(nullptr);
  228. }
  229. const_iterator_type find(const key_type& key) const
  230. {
  231. auto hash_value = hasher_traits<Hasher, key_type>::hash(key, hash_length());
  232. const auto& bucket = buckets.at(hash_value);
  233. for (const auto& item : bucket) {
  234. if (key == item.key)
  235. return const_iterator_type(&(*item));
  236. }
  237. return const_iterator_type(nullptr);
  238. }
  239. void clear(void)
  240. {
  241. for (size_t i = 0; i < buckets.size(); ++i)
  242. buckets.at(i).clear();
  243. }
  244. };
  245. } // namespace types