hash_map.hpp 6.8 KB

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