hash_map.hpp 7.1 KB

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