hash.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. #pragma once
  2. #include "fwd.h"
  3. #include "hash_table.h"
  4. template <class Key, class T, class HashFcn, class EqualKey, class Alloc>
  5. class THashMap: public TMapOps<THashMap<Key, T, HashFcn, EqualKey, Alloc>> {
  6. private:
  7. using ht = THashTable<std::pair<const Key, T>, Key, HashFcn, TSelect1st, EqualKey, Alloc>;
  8. ht rep;
  9. public:
  10. using key_type = typename ht::key_type;
  11. using value_type = typename ht::value_type;
  12. using hasher = typename ht::hasher;
  13. using key_equal = typename ht::key_equal;
  14. using allocator_type = typename ht::allocator_type;
  15. using node_allocator_type = typename ht::node_allocator_type;
  16. using mapped_type = T;
  17. using size_type = typename ht::size_type;
  18. using difference_type = typename ht::difference_type;
  19. using pointer = typename ht::pointer;
  20. using const_pointer = typename ht::const_pointer;
  21. using reference = typename ht::reference;
  22. using const_reference = typename ht::const_reference;
  23. using iterator = typename ht::iterator;
  24. using const_iterator = typename ht::const_iterator;
  25. using insert_ctx = typename ht::insert_ctx;
  26. hasher hash_function() const {
  27. return rep.hash_function();
  28. }
  29. key_equal key_eq() const {
  30. return rep.key_eq();
  31. }
  32. public:
  33. THashMap()
  34. : rep(0, hasher(), key_equal())
  35. {
  36. }
  37. template <class TAllocParam>
  38. explicit THashMap(TAllocParam* allocParam, size_type n = 0)
  39. : rep(n, hasher(), key_equal(), allocParam)
  40. {
  41. }
  42. explicit THashMap(size_type n)
  43. : rep(n, hasher(), key_equal())
  44. {
  45. }
  46. THashMap(size_type n, const hasher& hf)
  47. : rep(n, hf, key_equal())
  48. {
  49. }
  50. THashMap(size_type n, const hasher& hf, const key_equal& eql)
  51. : rep(n, hf, eql)
  52. {
  53. }
  54. template <class TAllocParam>
  55. explicit THashMap(size_type n, TAllocParam* allocParam)
  56. : rep(n, hasher(), key_equal(), allocParam)
  57. {
  58. }
  59. template <class InputIterator>
  60. THashMap(InputIterator f, InputIterator l)
  61. : rep(0, hasher(), key_equal())
  62. {
  63. rep.insert_unique(f, l);
  64. }
  65. template <class InputIterator>
  66. THashMap(InputIterator f, InputIterator l, size_type n)
  67. : rep(n, hasher(), key_equal())
  68. {
  69. rep.insert_unique(f, l);
  70. }
  71. template <class InputIterator>
  72. THashMap(InputIterator f, InputIterator l, size_type n,
  73. const hasher& hf)
  74. : rep(n, hf, key_equal())
  75. {
  76. rep.insert_unique(f, l);
  77. }
  78. template <class InputIterator>
  79. THashMap(InputIterator f, InputIterator l, size_type n,
  80. const hasher& hf, const key_equal& eql)
  81. : rep(n, hf, eql)
  82. {
  83. rep.insert_unique(f, l);
  84. }
  85. THashMap(const std::initializer_list<std::pair<Key, T>>& list)
  86. : rep(list.size(), hasher(), key_equal())
  87. {
  88. for (const auto& v : list) {
  89. rep.insert_unique_noresize(v);
  90. }
  91. }
  92. // THashMap has implicit copy/move constructors and copy-/move-assignment operators
  93. // because its implementation is backed by THashTable.
  94. // See hash_ut.cpp
  95. public:
  96. size_type size() const noexcept {
  97. return rep.size();
  98. }
  99. yssize_t ysize() const noexcept {
  100. return (yssize_t)rep.size();
  101. }
  102. size_type max_size() const noexcept {
  103. return rep.max_size();
  104. }
  105. Y_PURE_FUNCTION bool empty() const noexcept {
  106. return rep.empty();
  107. }
  108. explicit operator bool() const noexcept {
  109. return !empty();
  110. }
  111. void swap(THashMap& hs) {
  112. rep.swap(hs.rep);
  113. }
  114. iterator begin() {
  115. return rep.begin();
  116. }
  117. iterator end() {
  118. return rep.end();
  119. }
  120. const_iterator begin() const {
  121. return rep.begin();
  122. }
  123. const_iterator end() const {
  124. return rep.end();
  125. }
  126. const_iterator cbegin() const {
  127. return rep.begin();
  128. }
  129. const_iterator cend() const {
  130. return rep.end();
  131. }
  132. public:
  133. template <class InputIterator>
  134. void insert(InputIterator f, InputIterator l) {
  135. rep.insert_unique(f, l);
  136. }
  137. std::pair<iterator, bool> insert(const value_type& obj) {
  138. return rep.insert_unique(obj);
  139. }
  140. template <class M>
  141. std::pair<iterator, bool> insert_or_assign(const Key& k, M&& value) {
  142. auto result = try_emplace(k, std::forward<M>(value));
  143. if (!result.second) {
  144. result.first->second = std::forward<M>(value);
  145. }
  146. return result;
  147. }
  148. template <class M>
  149. std::pair<iterator, bool> insert_or_assign(Key&& k, M&& value) {
  150. auto result = try_emplace(std::move(k), std::forward<M>(value));
  151. if (!result.second) {
  152. result.first->second = std::forward<M>(value);
  153. }
  154. return result;
  155. }
  156. template <typename... Args>
  157. std::pair<iterator, bool> emplace(Args&&... args) {
  158. return rep.emplace_unique(std::forward<Args>(args)...);
  159. }
  160. std::pair<iterator, bool> insert_noresize(const value_type& obj) {
  161. return rep.insert_unique_noresize(obj);
  162. }
  163. template <typename... Args>
  164. std::pair<iterator, bool> emplace_noresize(Args&&... args) {
  165. return rep.emplace_unique_noresize(std::forward<Args>(args)...);
  166. }
  167. template <class TheObj>
  168. iterator insert_direct(const TheObj& obj, const insert_ctx& ins) {
  169. return rep.insert_direct(obj, ins);
  170. }
  171. template <typename... Args>
  172. iterator emplace_direct(const insert_ctx& ins, Args&&... args) {
  173. return rep.emplace_direct(ins, std::forward<Args>(args)...);
  174. }
  175. template <typename TKey, typename... Args>
  176. std::pair<iterator, bool> try_emplace(TKey&& key, Args&&... args) {
  177. insert_ctx ctx = nullptr;
  178. iterator it = find(key, ctx);
  179. if (it == end()) {
  180. it = rep.emplace_direct(ctx, std::piecewise_construct,
  181. std::forward_as_tuple(std::forward<TKey>(key)),
  182. std::forward_as_tuple(std::forward<Args>(args)...));
  183. return {it, true};
  184. }
  185. return {it, false};
  186. }
  187. template <class TheKey>
  188. iterator find(const TheKey& key) {
  189. return rep.find(key);
  190. }
  191. template <class TheKey>
  192. const_iterator find(const TheKey& key) const {
  193. return rep.find(key);
  194. }
  195. template <class TheKey>
  196. iterator find(const TheKey& key, insert_ctx& ins) {
  197. return rep.find_i(key, ins);
  198. }
  199. template <class TheKey>
  200. bool contains(const TheKey& key) const {
  201. return rep.find(key) != rep.end();
  202. }
  203. bool contains(const key_type& key) const {
  204. return rep.find(key) != rep.end();
  205. }
  206. template <class TheKey>
  207. bool contains(const TheKey& key, insert_ctx& ins) {
  208. return rep.find_i(key, ins) != rep.end();
  209. }
  210. template <class TKey>
  211. T& operator[](const TKey& key) {
  212. insert_ctx ctx = nullptr;
  213. iterator it = find(key, ctx);
  214. if (it != end()) {
  215. return it->second;
  216. }
  217. return rep.emplace_direct(ctx, std::piecewise_construct, std::forward_as_tuple(key), std::forward_as_tuple())->second;
  218. }
  219. template <class TheKey>
  220. const T& at(const TheKey& key) const {
  221. using namespace ::NPrivate;
  222. const_iterator it = find(key);
  223. if (Y_UNLIKELY(it == end())) {
  224. ::NPrivate::ThrowKeyNotFoundInHashTableException(MapKeyToString(key));
  225. }
  226. return it->second;
  227. }
  228. template <class TheKey>
  229. T& at(const TheKey& key) {
  230. using namespace ::NPrivate;
  231. iterator it = find(key);
  232. if (Y_UNLIKELY(it == end())) {
  233. ::NPrivate::ThrowKeyNotFoundInHashTableException(MapKeyToString(key));
  234. }
  235. return it->second;
  236. }
  237. template <class TKey>
  238. size_type count(const TKey& key) const {
  239. return rep.count(key);
  240. }
  241. template <class TKey>
  242. std::pair<iterator, iterator> equal_range(const TKey& key) {
  243. return rep.equal_range(key);
  244. }
  245. template <class TKey>
  246. std::pair<const_iterator, const_iterator> equal_range(const TKey& key) const {
  247. return rep.equal_range(key);
  248. }
  249. template <class TKey>
  250. size_type erase(const TKey& key) {
  251. return rep.erase_one(key);
  252. }
  253. void erase(iterator it) {
  254. rep.erase(it);
  255. }
  256. void erase(iterator f, iterator l) {
  257. rep.erase(f, l);
  258. }
  259. Y_REINITIALIZES_OBJECT void clear() {
  260. rep.clear();
  261. }
  262. Y_REINITIALIZES_OBJECT void clear(size_t downsize_hint) {
  263. rep.clear(downsize_hint);
  264. }
  265. Y_REINITIALIZES_OBJECT void basic_clear() {
  266. rep.basic_clear();
  267. }
  268. void release_nodes() {
  269. rep.release_nodes();
  270. }
  271. // if (stHash != NULL) bucket_count() must be equal to stHash->bucket_count()
  272. template <class KeySaver>
  273. int save_for_st(IOutputStream* stream, KeySaver& ks, sthash<int, int, THash<int>, TEqualTo<int>, typename KeySaver::TSizeType>* stHash = nullptr) const {
  274. return rep.template save_for_st<KeySaver>(stream, ks, stHash);
  275. }
  276. public:
  277. void reserve(size_type hint) {
  278. rep.reserve(hint);
  279. }
  280. size_type bucket_count() const {
  281. return rep.bucket_count();
  282. }
  283. size_type bucket_size(size_type n) const {
  284. return rep.bucket_size(n);
  285. }
  286. node_allocator_type& GetNodeAllocator() {
  287. return rep.GetNodeAllocator();
  288. }
  289. const node_allocator_type& GetNodeAllocator() const {
  290. return rep.GetNodeAllocator();
  291. }
  292. };
  293. template <class Key, class T, class HashFcn, class EqualKey, class Alloc>
  294. inline bool operator==(const THashMap<Key, T, HashFcn, EqualKey, Alloc>& hm1, const THashMap<Key, T, HashFcn, EqualKey, Alloc>& hm2) {
  295. if (hm1.size() != hm2.size()) {
  296. return false;
  297. }
  298. for (const auto& it1 : hm1) {
  299. auto it2 = hm2.find(it1.first);
  300. if ((it2 == hm2.end()) || !(it1 == *it2)) {
  301. return false;
  302. }
  303. }
  304. return true;
  305. }
  306. template <class Key, class T, class HashFcn, class EqualKey, class Alloc>
  307. inline bool operator!=(const THashMap<Key, T, HashFcn, EqualKey, Alloc>& hm1, const THashMap<Key, T, HashFcn, EqualKey, Alloc>& hm2) {
  308. return !(hm1 == hm2);
  309. }