hash_map.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #pragma once
  2. #include <list>
  3. #include <bit>
  4. #include <utility>
  5. #include <type_traits>
  6. #include <stdint.h>
  7. #include <types/allocator.hpp>
  8. #include <types/cplusplus.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(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 <
  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 = std::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 = std::list<pair_type,
  94. types::allocator_adapter<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. constexpr hash_map(const hash_map& v)
  170. : buckets(v.buckets) {}
  171. constexpr hash_map(hash_map&& v)
  172. : buckets(std::move(v.buckets)) {}
  173. constexpr ~hash_map()
  174. {
  175. buckets.clear();
  176. }
  177. constexpr void emplace(pair_type p)
  178. {
  179. auto hash_value = hasher_type::hash(p.first, hash_length());
  180. buckets.at(hash_value).push_back(std::move(p));
  181. }
  182. template <typename _key_type, typename _value_type>
  183. constexpr void emplace(_key_type&& key, _value_type&& value)
  184. {
  185. emplace(std::make_pair(std::forward<_key_type>(key), std::forward<_value_type>(value)));
  186. }
  187. constexpr void remove(const key_type& key)
  188. {
  189. auto hash_value = hasher_type::hash(key, hash_length());
  190. auto& bucket = buckets.at(hash_value);
  191. for (auto iter = bucket.begin(); iter != bucket.end(); ++iter) {
  192. if (iter->first == key) {
  193. bucket.erase(iter);
  194. return;
  195. }
  196. }
  197. }
  198. constexpr void remove(iterator_type iter)
  199. {
  200. remove(iter->first);
  201. iter.p = nullptr;
  202. }
  203. constexpr void remove(const_iterator_type iter)
  204. {
  205. remove(iter->first);
  206. iter.p = nullptr;
  207. }
  208. constexpr iterator_type find(const key_type& key)
  209. {
  210. auto hash_value = hasher_type::hash(key, hash_length());
  211. auto& bucket = buckets.at(hash_value);
  212. for (auto& item : bucket) {
  213. if (key == item.first)
  214. return iterator_type(&item);
  215. }
  216. return iterator_type(nullptr);
  217. }
  218. constexpr const_iterator_type find(const key_type& key) const
  219. {
  220. auto hash_value = hasher_type::hash(key, hash_length());
  221. const auto& bucket = buckets.at(hash_value);
  222. for (auto iter = bucket.cbegin(); iter != bucket.cend(); ++iter) {
  223. if (key == iter->key)
  224. return const_iterator_type(&iter);
  225. }
  226. return const_iterator_type(nullptr);
  227. }
  228. constexpr void clear(void)
  229. {
  230. for (auto& bucket : buckets)
  231. bucket.clear();
  232. }
  233. };
  234. } // namespace types