hash_map.hpp 7.1 KB

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