hash_map.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. pair(const key_type& _key, const value_type& _val)
  97. : key(_key)
  98. , value(_val)
  99. {
  100. }
  101. pair(key_type&& _key, const value_type& _val)
  102. : key(move(_key))
  103. , value(_val)
  104. {
  105. }
  106. pair(const key_type& _key, value_type&& _val)
  107. : key(_key)
  108. , value(move(_val))
  109. {
  110. }
  111. pair(key_type&& _key, value_type&& _val)
  112. : key(move(_key))
  113. , value(move(_val))
  114. {
  115. }
  116. bool operator==(const pair& p)
  117. {
  118. return key == p.key;
  119. }
  120. key_type key;
  121. value_type value;
  122. };
  123. template <typename Pointer>
  124. class iterator {
  125. public:
  126. using _Value = typename traits::remove_pointer<Pointer>::type;
  127. using Reference = typename traits::add_reference<_Value>::type;
  128. public:
  129. iterator(const iterator& iter) noexcept
  130. : p(iter.p)
  131. {
  132. }
  133. iterator(iterator&& iter) noexcept
  134. : p(iter.p)
  135. {
  136. iter.p = nullptr;
  137. }
  138. iterator& operator=(const iterator& iter)
  139. {
  140. p = iter.p;
  141. return *this;
  142. }
  143. explicit iterator(Pointer p) noexcept
  144. : p(p)
  145. {
  146. }
  147. bool operator==(const iterator& iter) noexcept
  148. {
  149. return this->p == iter.p;
  150. }
  151. bool operator!=(const iterator& iter) noexcept
  152. {
  153. return !(*this == iter);
  154. }
  155. Reference operator*() const noexcept
  156. {
  157. return *p;
  158. }
  159. Pointer operator->() const noexcept
  160. {
  161. return p;
  162. }
  163. protected:
  164. Pointer p;
  165. public:
  166. static inline iterator npos = iterator((Pointer)-1);
  167. };
  168. private:
  169. bucket_array_type buckets;
  170. protected:
  171. constexpr uint32_t hash_length(void) const
  172. {
  173. switch (buckets.capacity()) {
  174. case 32:
  175. return 5;
  176. case 64:
  177. return 6;
  178. case 128:
  179. return 7;
  180. case 256:
  181. return 8;
  182. // TODO
  183. default:
  184. return 9;
  185. }
  186. }
  187. public:
  188. explicit hash_map(void)
  189. : buckets(INITIAL_BUCKETS_ALLOCATED)
  190. {
  191. for (size_type i = 0; i < INITIAL_BUCKETS_ALLOCATED; ++i)
  192. buckets.emplace_back();
  193. }
  194. hash_map(const hash_map& v)
  195. : buckets(v.buckets)
  196. {
  197. }
  198. hash_map(hash_map&& v)
  199. : buckets(move(v))
  200. {
  201. }
  202. ~hash_map()
  203. {
  204. clear();
  205. }
  206. void insert(const pair& p)
  207. {
  208. auto hash_value = hasher_traits<Hasher, key_type>::hash(p.key, hash_length());
  209. buckets.at(hash_value).push_back(p);
  210. }
  211. void insert(pair&& p)
  212. {
  213. auto hash_value = hasher_traits<Hasher, key_type>::hash(p.key, hash_length());
  214. buckets.at(hash_value).push_back(move(p));
  215. }
  216. void insert(const key_type& key, const value_type& val)
  217. {
  218. insert(pair { key, val });
  219. }
  220. void remove(const key_type& key)
  221. {
  222. auto hash_value = hasher_traits<Hasher, key_type>::hash(key, hash_length());
  223. auto& bucket = buckets.at(hash_value);
  224. for (auto iter = bucket.begin(); iter != bucket.end(); ++iter) {
  225. if (iter->key == key) {
  226. bucket.erase(iter);
  227. return;
  228. }
  229. }
  230. }
  231. iterator_type find(const key_type& key)
  232. {
  233. auto hash_value = hasher_traits<Hasher, key_type>::hash(key, hash_length());
  234. auto& bucket = buckets.at(hash_value);
  235. for (auto& item : bucket) {
  236. if (key == item.key)
  237. return iterator_type(&item);
  238. }
  239. return iterator_type::npos;
  240. }
  241. const_iterator_type find(const key_type& key) const
  242. {
  243. auto hash_value = hasher_traits<Hasher, key_type>::hash(key, hash_length());
  244. const auto& bucket = buckets.at(hash_value);
  245. for (const auto& item : bucket) {
  246. if (key == item.key)
  247. return const_iterator_type(&(*item));
  248. }
  249. return const_iterator_type::npos;
  250. }
  251. void clear(void)
  252. {
  253. buckets.clear();
  254. }
  255. };
  256. } // namespace types