hash_map.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. constexpr pair(void) = delete;
  98. constexpr pair(const key_type _key, value_type _val)
  99. : key(_key)
  100. , value(_val)
  101. {
  102. }
  103. constexpr 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. friend class hash_map;
  114. public:
  115. constexpr iterator(const iterator& iter) noexcept
  116. : p(iter.p)
  117. {
  118. }
  119. constexpr iterator(iterator&& iter) noexcept
  120. : p(iter.p)
  121. {
  122. iter.p = nullptr;
  123. }
  124. constexpr iterator& operator=(const iterator& iter)
  125. {
  126. p = iter.p;
  127. return *this;
  128. }
  129. explicit constexpr iterator(Pointer p) noexcept
  130. : p(p)
  131. {
  132. }
  133. constexpr bool operator==(const iterator& iter) const noexcept
  134. {
  135. return this->p == iter.p;
  136. }
  137. constexpr bool operator!=(const iterator& iter) const noexcept
  138. {
  139. return !(*this == iter);
  140. }
  141. constexpr operator bool()
  142. {
  143. return p != nullptr;
  144. }
  145. constexpr Reference operator*() const noexcept
  146. {
  147. return *p;
  148. }
  149. constexpr 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 constexpr 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. constexpr hash_map(const hash_map& v)
  183. : buckets(v.buckets)
  184. {
  185. }
  186. constexpr hash_map(hash_map&& v)
  187. : buckets(move(v.buckets))
  188. {
  189. }
  190. constexpr ~hash_map()
  191. {
  192. buckets.clear();
  193. }
  194. constexpr void insert(const pair& p)
  195. {
  196. auto hash_value = _Hasher::hash(p.key, hash_length());
  197. buckets.at(hash_value).push_back(p);
  198. }
  199. constexpr void insert(pair&& p)
  200. {
  201. auto hash_value = _Hasher::hash(p.key, hash_length());
  202. buckets.at(hash_value).push_back(move(p));
  203. }
  204. constexpr void insert(const key_type& key, const value_type& val)
  205. {
  206. insert(pair { key, val });
  207. }
  208. constexpr void remove(const key_type& key)
  209. {
  210. auto hash_value = _Hasher::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. constexpr void remove(iterator_type iter)
  220. {
  221. remove(iter->key);
  222. iter.p = nullptr;
  223. }
  224. constexpr void remove(const_iterator_type iter)
  225. {
  226. remove(iter->key);
  227. iter.p = nullptr;
  228. }
  229. constexpr iterator_type find(const key_type& key)
  230. {
  231. auto hash_value = _Hasher::hash(key, hash_length());
  232. auto& bucket = buckets.at(hash_value);
  233. for (auto& item : bucket) {
  234. if (key == item.key)
  235. return iterator_type(&item);
  236. }
  237. return iterator_type(nullptr);
  238. }
  239. constexpr const_iterator_type find(const key_type& key) const
  240. {
  241. auto hash_value = _Hasher::hash(key, hash_length());
  242. const auto& bucket = buckets.at(hash_value);
  243. for (const auto& item : bucket) {
  244. if (key == item.key)
  245. return const_iterator_type(&(*item));
  246. }
  247. return const_iterator_type(nullptr);
  248. }
  249. constexpr void clear(void)
  250. {
  251. for (size_t i = 0; i < buckets.size(); ++i)
  252. buckets.at(i).clear();
  253. }
  254. };
  255. } // namespace types