hash_map.hpp 6.9 KB

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