hash_map.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. typename Allocator = std::allocator<std::pair<const Key, Value> >,
  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. using bucket_type = std::list<pair_type, Allocator>;
  92. using bucket_array_type = std::vector<bucket_type, typename
  93. std::allocator_traits<Allocator>:: template rebind_alloc<bucket_type>>;
  94. using hasher_type = Hasher<Key>;
  95. static constexpr size_type INITIAL_BUCKETS_ALLOCATED = 64;
  96. public:
  97. template <typename Pointer>
  98. class iterator {
  99. public:
  100. using _Value = std::remove_pointer_t<Pointer>;
  101. using Reference = std::add_lvalue_reference_t<_Value>;
  102. friend class hash_map;
  103. public:
  104. constexpr iterator(const iterator& iter) noexcept
  105. : p(iter.p)
  106. {
  107. }
  108. constexpr iterator(iterator&& iter) noexcept
  109. : p(iter.p)
  110. {
  111. iter.p = nullptr;
  112. }
  113. constexpr iterator& operator=(const iterator& iter)
  114. {
  115. p = iter.p;
  116. return *this;
  117. }
  118. explicit constexpr iterator(Pointer p) noexcept
  119. : p(p)
  120. {
  121. }
  122. constexpr bool operator==(const iterator& iter) const noexcept
  123. {
  124. return this->p == iter.p;
  125. }
  126. constexpr bool operator!=(const iterator& iter) const noexcept
  127. {
  128. return !(*this == iter);
  129. }
  130. constexpr operator bool(void)
  131. {
  132. return p != nullptr;
  133. }
  134. constexpr Reference operator*(void) const noexcept
  135. {
  136. return *p;
  137. }
  138. constexpr Pointer operator->(void) const noexcept
  139. {
  140. return p;
  141. }
  142. protected:
  143. Pointer p;
  144. };
  145. private:
  146. bucket_array_type buckets;
  147. protected:
  148. constexpr uint32_t hash_length(void) const
  149. {
  150. switch (buckets.capacity()) {
  151. case 32:
  152. return 5;
  153. case 64:
  154. return 6;
  155. case 128:
  156. return 7;
  157. case 256:
  158. return 8;
  159. // TODO
  160. default:
  161. return 9;
  162. }
  163. }
  164. public:
  165. explicit constexpr hash_map(void)
  166. : buckets(INITIAL_BUCKETS_ALLOCATED) {}
  167. constexpr hash_map(const hash_map& v)
  168. : buckets(v.buckets) {}
  169. constexpr hash_map(hash_map&& v)
  170. : buckets(std::move(v.buckets)) {}
  171. constexpr ~hash_map()
  172. {
  173. buckets.clear();
  174. }
  175. constexpr void emplace(pair_type p)
  176. {
  177. auto hash_value = hasher_type::hash(p.first, hash_length());
  178. buckets.at(hash_value).push_back(std::move(p));
  179. }
  180. template <typename _key_type, typename _value_type>
  181. constexpr void emplace(_key_type&& key, _value_type&& value)
  182. {
  183. emplace(std::make_pair(std::forward<_key_type>(key), std::forward<_value_type>(value)));
  184. }
  185. constexpr void remove(const key_type& key)
  186. {
  187. auto hash_value = hasher_type::hash(key, hash_length());
  188. auto& bucket = buckets.at(hash_value);
  189. for (auto iter = bucket.begin(); iter != bucket.end(); ++iter) {
  190. if (iter->first == key) {
  191. bucket.erase(iter);
  192. return;
  193. }
  194. }
  195. }
  196. constexpr void remove(iterator_type iter)
  197. {
  198. remove(iter->first);
  199. iter.p = nullptr;
  200. }
  201. constexpr void remove(const_iterator_type iter)
  202. {
  203. remove(iter->first);
  204. iter.p = nullptr;
  205. }
  206. constexpr iterator_type find(const key_type& key)
  207. {
  208. auto hash_value = hasher_type::hash(key, hash_length());
  209. auto& bucket = buckets.at(hash_value);
  210. for (auto& item : bucket) {
  211. if (key == item.first)
  212. return iterator_type(&item);
  213. }
  214. return iterator_type(nullptr);
  215. }
  216. constexpr const_iterator_type find(const key_type& key) const
  217. {
  218. auto hash_value = hasher_type::hash(key, hash_length());
  219. const auto& bucket = buckets.at(hash_value);
  220. for (auto iter = bucket.cbegin(); iter != bucket.cend(); ++iter) {
  221. if (key == iter->key)
  222. return const_iterator_type(&iter);
  223. }
  224. return const_iterator_type(nullptr);
  225. }
  226. constexpr void clear(void)
  227. {
  228. for (auto& bucket : buckets)
  229. bucket.clear();
  230. }
  231. };
  232. } // namespace types