hash_map.hpp 6.6 KB

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