hash_multi_map.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 THashMultiMap {
  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 mapped_type = T;
  15. using allocator_type = typename ht::allocator_type;
  16. using size_type = typename ht::size_type;
  17. using difference_type = typename ht::difference_type;
  18. using pointer = typename ht::pointer;
  19. using const_pointer = typename ht::const_pointer;
  20. using reference = typename ht::reference;
  21. using const_reference = typename ht::const_reference;
  22. using iterator = typename ht::iterator;
  23. using const_iterator = typename ht::const_iterator;
  24. using insert_ctx = typename ht::insert_ctx;
  25. hasher hash_function() const {
  26. return rep.hash_function();
  27. }
  28. key_equal key_eq() const {
  29. return rep.key_eq();
  30. }
  31. public:
  32. THashMultiMap()
  33. : rep(0, hasher(), key_equal())
  34. {
  35. }
  36. template <typename TAllocParam>
  37. explicit THashMultiMap(TAllocParam* allocParam)
  38. : rep(0, hasher(), key_equal(), allocParam)
  39. {
  40. }
  41. explicit THashMultiMap(size_type n)
  42. : rep(n, hasher(), key_equal())
  43. {
  44. }
  45. THashMultiMap(size_type n, const hasher& hf)
  46. : rep(n, hf, key_equal())
  47. {
  48. }
  49. THashMultiMap(size_type n, const hasher& hf, const key_equal& eql)
  50. : rep(n, hf, eql)
  51. {
  52. }
  53. template <class InputIterator>
  54. THashMultiMap(InputIterator f, InputIterator l)
  55. : rep(0, hasher(), key_equal())
  56. {
  57. rep.insert_equal(f, l);
  58. }
  59. template <class InputIterator>
  60. THashMultiMap(InputIterator f, InputIterator l, size_type n)
  61. : rep(n, hasher(), key_equal())
  62. {
  63. rep.insert_equal(f, l);
  64. }
  65. template <class InputIterator>
  66. THashMultiMap(InputIterator f, InputIterator l, size_type n, const hasher& hf)
  67. : rep(n, hf, key_equal())
  68. {
  69. rep.insert_equal(f, l);
  70. }
  71. template <class InputIterator>
  72. THashMultiMap(InputIterator f, InputIterator l, size_type n, const hasher& hf, const key_equal& eql)
  73. : rep(n, hf, eql)
  74. {
  75. rep.insert_equal(f, l);
  76. }
  77. THashMultiMap(std::initializer_list<std::pair<Key, T>> list)
  78. : rep(list.size(), hasher(), key_equal())
  79. {
  80. for (const auto& v : list) {
  81. rep.emplace_equal_noresize(v);
  82. }
  83. }
  84. // THashMultiMap has implicit copy/move constructors and copy-/move-assignment operators
  85. // because its implementation is backed by THashTable.
  86. // See hash_ut.cpp
  87. public:
  88. size_type size() const {
  89. return rep.size();
  90. }
  91. yssize_t ysize() const {
  92. return (yssize_t)rep.size();
  93. }
  94. size_type max_size() const {
  95. return rep.max_size();
  96. }
  97. Y_PURE_FUNCTION bool empty() const {
  98. return rep.empty();
  99. }
  100. explicit operator bool() const noexcept {
  101. return !empty();
  102. }
  103. void swap(THashMultiMap& hs) {
  104. rep.swap(hs.rep);
  105. }
  106. iterator begin() {
  107. return rep.begin();
  108. }
  109. iterator end() {
  110. return rep.end();
  111. }
  112. const_iterator begin() const {
  113. return rep.begin();
  114. }
  115. const_iterator end() const {
  116. return rep.end();
  117. }
  118. const_iterator cbegin() const {
  119. return rep.begin();
  120. }
  121. const_iterator cend() const {
  122. return rep.end();
  123. }
  124. public:
  125. template <class InputIterator>
  126. void insert(InputIterator f, InputIterator l) {
  127. rep.insert_equal(f, l);
  128. }
  129. iterator insert(const value_type& obj) {
  130. return rep.insert_equal(obj);
  131. }
  132. template <typename... Args>
  133. iterator emplace(Args&&... args) {
  134. return rep.emplace_equal(std::forward<Args>(args)...);
  135. }
  136. iterator insert_noresize(const value_type& obj) {
  137. return rep.emplace_equal_noresize(obj);
  138. }
  139. template <typename... Args>
  140. iterator emplace_noresize(Args&&... args) {
  141. return rep.emplace_equal_noresize(std::forward<Args>(args)...);
  142. }
  143. template <class TheObj>
  144. iterator insert_direct(const TheObj& obj, const insert_ctx& ins) {
  145. return rep.insert_direct(obj, ins);
  146. }
  147. template <typename... Args>
  148. iterator emplace_direct(const insert_ctx& ins, Args&&... args) {
  149. return rep.emplace_direct(ins, std::forward<Args>(args)...);
  150. }
  151. template <class TKey>
  152. const_iterator find(const TKey& key) const {
  153. return rep.find(key);
  154. }
  155. template <class TKey>
  156. iterator find(const TKey& key) {
  157. return rep.find(key);
  158. }
  159. template <class TheKey>
  160. iterator find(const TheKey& key, insert_ctx& ins) {
  161. return rep.find_i(key, ins);
  162. }
  163. template <class TheKey>
  164. bool contains(const TheKey& key) const {
  165. return rep.find(key) != rep.end();
  166. }
  167. template <class TKey>
  168. size_type count(const TKey& key) const {
  169. return rep.count(key);
  170. }
  171. template <class TKey>
  172. std::pair<iterator, iterator> equal_range(const TKey& key) {
  173. return rep.equal_range(key);
  174. }
  175. template <class TKey>
  176. std::pair<iterator, iterator> equal_range_i(const TKey& key, insert_ctx& ins) {
  177. return rep.equal_range_i(key, ins);
  178. }
  179. template <class TKey>
  180. std::pair<const_iterator, const_iterator> equal_range(const TKey& key) const {
  181. return rep.equal_range(key);
  182. }
  183. size_type erase(const key_type& key) {
  184. return rep.erase(key);
  185. }
  186. void erase(iterator it) {
  187. rep.erase(it);
  188. }
  189. void erase(iterator f, iterator l) {
  190. rep.erase(f, l);
  191. }
  192. Y_REINITIALIZES_OBJECT void clear() {
  193. rep.clear();
  194. }
  195. Y_REINITIALIZES_OBJECT void clear(size_t downsize_hint) {
  196. rep.clear(downsize_hint);
  197. }
  198. Y_REINITIALIZES_OBJECT void basic_clear() {
  199. rep.basic_clear();
  200. }
  201. void release_nodes() {
  202. rep.release_nodes();
  203. }
  204. // if (stHash != NULL) bucket_count() must be equal to stHash->bucket_count()
  205. template <class KeySaver>
  206. int save_for_st(IOutputStream* stream, KeySaver& ks, sthash<int, int, THash<int>, TEqualTo<int>, typename KeySaver::TSizeType>* stHash = nullptr) const {
  207. return rep.template save_for_st<KeySaver>(stream, ks, stHash);
  208. }
  209. public:
  210. void reserve(size_type hint) {
  211. rep.reserve(hint);
  212. }
  213. size_type bucket_count() const {
  214. return rep.bucket_count();
  215. }
  216. size_type bucket_size(size_type n) const {
  217. return rep.bucket_size(n);
  218. }
  219. };
  220. template <class Key, class T, class HF, class EqKey, class Alloc>
  221. inline bool operator==(const THashMultiMap<Key, T, HF, EqKey, Alloc>& hm1, const THashMultiMap<Key, T, HF, EqKey, Alloc>& hm2) {
  222. // NOTE: copy-pasted from
  223. // contrib/libs/cxxsupp/libcxx/include/unordered_map
  224. // and adapted to THashMultiMap
  225. if (hm1.size() != hm2.size()) {
  226. return false;
  227. }
  228. using const_iterator = typename THashMultiMap<Key, T, HF, EqKey, Alloc>::const_iterator;
  229. using TEqualRange = std::pair<const_iterator, const_iterator>;
  230. for (const_iterator it = hm1.begin(), end = hm1.end(); it != end;) {
  231. TEqualRange eq1 = hm1.equal_range(it->first);
  232. TEqualRange eq2 = hm2.equal_range(it->first);
  233. if (std::distance(eq1.first, eq1.second) != std::distance(eq2.first, eq2.second) ||
  234. !std::is_permutation(eq1.first, eq1.second, eq2.first))
  235. {
  236. return false;
  237. }
  238. it = eq1.second;
  239. }
  240. return true;
  241. }
  242. template <class Key, class T, class HF, class EqKey, class Alloc>
  243. inline bool operator!=(const THashMultiMap<Key, T, HF, EqKey, Alloc>& hm1, const THashMultiMap<Key, T, HF, EqKey, Alloc>& hm2) {
  244. return !(hm1 == hm2);
  245. }