hash_map.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #pragma once
  2. #include <list>
  3. #include <vector>
  4. #include <bit>
  5. #include <utility>
  6. #include <type_traits>
  7. #include <stdint.h>
  8. #include <types/allocator.hpp>
  9. #include <types/cplusplus.hpp>
  10. #include <types/string.hpp>
  11. #include <types/types.h>
  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(std::bit_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 <template <typename> typename String, typename Allocator>
  58. struct linux_hasher<String<Allocator>,
  59. std::enable_if_t<
  60. std::is_same_v<
  61. std::decay_t<String<Allocator>>, types::string<Allocator>
  62. >
  63. >
  64. > {
  65. static inline constexpr hash_t hash(types::string<Allocator>&& str, uint32_t bits)
  66. {
  67. return linux_hasher<const char*>::hash(str.c_str(), bits);
  68. }
  69. static inline constexpr hash_t hash(const types::string<Allocator>& str, uint32_t bits)
  70. {
  71. return linux_hasher<const char*>::hash(str.c_str(), bits);
  72. }
  73. };
  74. template <typename Key, typename Value,
  75. template <typename _Key, typename...> class Hasher = types::linux_hasher,
  76. template <typename _T> class Allocator = types::kernel_allocator,
  77. std::enable_if_t<std::is_convertible_v<hash_t, decltype(
  78. Hasher<Key>::hash(std::declval<Key>(), std::declval<uint32_t>())
  79. )>, bool> = true>
  80. class hash_map {
  81. public:
  82. template <typename Pointer>
  83. class iterator;
  84. using key_type = std::add_const_t<Key>;
  85. using value_type = Value;
  86. using pair_type = std::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. template <typename T>
  92. using adapted_allocator = types::allocator_adapter<T, Allocator>;
  93. using bucket_type = std::list<pair_type, adapted_allocator<pair_type>>;
  94. using bucket_array_type = std::vector<bucket_type, adapted_allocator<bucket_type>>;
  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. constexpr hash_map(const hash_map& v)
  169. : buckets(v.buckets) {}
  170. constexpr hash_map(hash_map&& v)
  171. : buckets(std::move(v.buckets)) {}
  172. constexpr ~hash_map()
  173. {
  174. buckets.clear();
  175. }
  176. constexpr void emplace(pair_type p)
  177. {
  178. auto hash_value = hasher_type::hash(p.first, hash_length());
  179. buckets.at(hash_value).push_back(std::move(p));
  180. }
  181. template <typename _key_type, typename _value_type>
  182. constexpr void emplace(_key_type&& key, _value_type&& value)
  183. {
  184. emplace(std::make_pair(std::forward<_key_type>(key), std::forward<_value_type>(value)));
  185. }
  186. constexpr void remove(const key_type& key)
  187. {
  188. auto hash_value = hasher_type::hash(key, hash_length());
  189. auto& bucket = buckets.at(hash_value);
  190. for (auto iter = bucket.begin(); iter != bucket.end(); ++iter) {
  191. if (iter->first == key) {
  192. bucket.erase(iter);
  193. return;
  194. }
  195. }
  196. }
  197. constexpr void remove(iterator_type iter)
  198. {
  199. remove(iter->first);
  200. iter.p = nullptr;
  201. }
  202. constexpr void remove(const_iterator_type iter)
  203. {
  204. remove(iter->first);
  205. iter.p = nullptr;
  206. }
  207. constexpr iterator_type find(const key_type& key)
  208. {
  209. auto hash_value = hasher_type::hash(key, hash_length());
  210. auto& bucket = buckets.at(hash_value);
  211. for (auto& item : bucket) {
  212. if (key == item.first)
  213. return iterator_type(&item);
  214. }
  215. return iterator_type(nullptr);
  216. }
  217. constexpr const_iterator_type find(const key_type& key) const
  218. {
  219. auto hash_value = hasher_type::hash(key, hash_length());
  220. const auto& bucket = buckets.at(hash_value);
  221. for (auto iter = bucket.cbegin(); iter != bucket.cend(); ++iter) {
  222. if (key == iter->key)
  223. return const_iterator_type(&iter);
  224. }
  225. return const_iterator_type(nullptr);
  226. }
  227. constexpr void clear(void)
  228. {
  229. for (auto& bucket : buckets)
  230. bucket.clear();
  231. }
  232. };
  233. } // namespace types