hash_map 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_HASH_MAP
  10. #define _LIBCPP_HASH_MAP
  11. /*
  12. hash_map synopsis
  13. namespace __gnu_cxx
  14. {
  15. template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
  16. class Alloc = allocator<pair<const Key, T>>>
  17. class hash_map
  18. {
  19. public:
  20. // types
  21. typedef Key key_type;
  22. typedef T mapped_type;
  23. typedef Hash hasher;
  24. typedef Pred key_equal;
  25. typedef Alloc allocator_type;
  26. typedef pair<const key_type, mapped_type> value_type;
  27. typedef value_type& reference;
  28. typedef const value_type& const_reference;
  29. typedef typename allocator_traits<allocator_type>::pointer pointer;
  30. typedef typename allocator_traits<allocator_type>::const_pointer const_pointer;
  31. typedef typename allocator_traits<allocator_type>::size_type size_type;
  32. typedef typename allocator_traits<allocator_type>::difference_type difference_type;
  33. typedef /unspecified/ iterator;
  34. typedef /unspecified/ const_iterator;
  35. hash_map();
  36. explicit hash_map(size_type n, const hasher& hf = hasher(),
  37. const key_equal& eql = key_equal(),
  38. const allocator_type& a = allocator_type());
  39. template <class InputIterator>
  40. hash_map(InputIterator f, InputIterator l);
  41. template <class InputIterator>
  42. hash_map(InputIterator f, InputIterator l,
  43. size_type n, const hasher& hf = hasher(),
  44. const key_equal& eql = key_equal(),
  45. const allocator_type& a = allocator_type());
  46. hash_map(const hash_map&);
  47. ~hash_map();
  48. hash_map& operator=(const hash_map&);
  49. allocator_type get_allocator() const;
  50. bool empty() const;
  51. size_type size() const;
  52. size_type max_size() const;
  53. iterator begin();
  54. iterator end();
  55. const_iterator begin() const;
  56. const_iterator end() const;
  57. pair<iterator, bool> insert(const value_type& obj);
  58. template <class InputIterator>
  59. void insert(InputIterator first, InputIterator last);
  60. void erase(const_iterator position);
  61. size_type erase(const key_type& k);
  62. void erase(const_iterator first, const_iterator last);
  63. void clear();
  64. void swap(hash_map&);
  65. hasher hash_funct() const;
  66. key_equal key_eq() const;
  67. iterator find(const key_type& k);
  68. const_iterator find(const key_type& k) const;
  69. size_type count(const key_type& k) const;
  70. pair<iterator, iterator> equal_range(const key_type& k);
  71. pair<const_iterator, const_iterator> equal_range(const key_type& k) const;
  72. mapped_type& operator[](const key_type& k);
  73. size_type bucket_count() const;
  74. size_type max_bucket_count() const;
  75. size_type elems_in_bucket(size_type n) const;
  76. void resize(size_type n);
  77. };
  78. template <class Key, class T, class Hash, class Pred, class Alloc>
  79. void swap(hash_map<Key, T, Hash, Pred, Alloc>& x,
  80. hash_map<Key, T, Hash, Pred, Alloc>& y);
  81. template <class Key, class T, class Hash, class Pred, class Alloc>
  82. bool
  83. operator==(const hash_map<Key, T, Hash, Pred, Alloc>& x,
  84. const hash_map<Key, T, Hash, Pred, Alloc>& y);
  85. template <class Key, class T, class Hash, class Pred, class Alloc>
  86. bool
  87. operator!=(const hash_map<Key, T, Hash, Pred, Alloc>& x,
  88. const hash_map<Key, T, Hash, Pred, Alloc>& y);
  89. template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
  90. class Alloc = allocator<pair<const Key, T>>>
  91. class hash_multimap
  92. {
  93. public:
  94. // types
  95. typedef Key key_type;
  96. typedef T mapped_type;
  97. typedef Hash hasher;
  98. typedef Pred key_equal;
  99. typedef Alloc allocator_type;
  100. typedef pair<const key_type, mapped_type> value_type;
  101. typedef value_type& reference;
  102. typedef const value_type& const_reference;
  103. typedef typename allocator_traits<allocator_type>::pointer pointer;
  104. typedef typename allocator_traits<allocator_type>::const_pointer const_pointer;
  105. typedef typename allocator_traits<allocator_type>::size_type size_type;
  106. typedef typename allocator_traits<allocator_type>::difference_type difference_type;
  107. typedef /unspecified/ iterator;
  108. typedef /unspecified/ const_iterator;
  109. explicit hash_multimap(size_type n = 193, const hasher& hf = hasher(),
  110. const key_equal& eql = key_equal(),
  111. const allocator_type& a = allocator_type());
  112. template <class InputIterator>
  113. hash_multimap(InputIterator f, InputIterator l,
  114. size_type n = 193, const hasher& hf = hasher(),
  115. const key_equal& eql = key_equal(),
  116. const allocator_type& a = allocator_type());
  117. explicit hash_multimap(const allocator_type&);
  118. hash_multimap(const hash_multimap&);
  119. ~hash_multimap();
  120. hash_multimap& operator=(const hash_multimap&);
  121. allocator_type get_allocator() const;
  122. bool empty() const;
  123. size_type size() const;
  124. size_type max_size() const;
  125. iterator begin();
  126. iterator end();
  127. const_iterator begin() const;
  128. const_iterator end() const;
  129. iterator insert(const value_type& obj);
  130. template <class InputIterator>
  131. void insert(InputIterator first, InputIterator last);
  132. void erase(const_iterator position);
  133. size_type erase(const key_type& k);
  134. void erase(const_iterator first, const_iterator last);
  135. void clear();
  136. void swap(hash_multimap&);
  137. hasher hash_funct() const;
  138. key_equal key_eq() const;
  139. iterator find(const key_type& k);
  140. const_iterator find(const key_type& k) const;
  141. size_type count(const key_type& k) const;
  142. pair<iterator, iterator> equal_range(const key_type& k);
  143. pair<const_iterator, const_iterator> equal_range(const key_type& k) const;
  144. size_type bucket_count() const;
  145. size_type max_bucket_count() const;
  146. size_type elems_in_bucket(size_type n) const;
  147. void resize(size_type n);
  148. };
  149. template <class Key, class T, class Hash, class Pred, class Alloc>
  150. void swap(hash_multimap<Key, T, Hash, Pred, Alloc>& x,
  151. hash_multimap<Key, T, Hash, Pred, Alloc>& y);
  152. template <class Key, class T, class Hash, class Pred, class Alloc>
  153. bool
  154. operator==(const hash_multimap<Key, T, Hash, Pred, Alloc>& x,
  155. const hash_multimap<Key, T, Hash, Pred, Alloc>& y);
  156. template <class Key, class T, class Hash, class Pred, class Alloc>
  157. bool
  158. operator!=(const hash_multimap<Key, T, Hash, Pred, Alloc>& x,
  159. const hash_multimap<Key, T, Hash, Pred, Alloc>& y);
  160. } // __gnu_cxx
  161. */
  162. #include <__config>
  163. #include <__hash_table>
  164. #include <algorithm>
  165. #include <ext/__hash>
  166. #include <functional>
  167. #if defined(__DEPRECATED) && __DEPRECATED
  168. # if defined(_LIBCPP_WARNING)
  169. _LIBCPP_WARNING("Use of the header <ext/hash_map> is deprecated. Migrate to <unordered_map>")
  170. # else
  171. # warning Use of the header <ext/hash_map> is deprecated. Migrate to <unordered_map>
  172. # endif
  173. #endif
  174. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  175. # pragma GCC system_header
  176. #endif
  177. namespace __gnu_cxx {
  178. template <class _Tp, class _Hash, bool = std::is_empty<_Hash>::value && !std::__libcpp_is_final<_Hash>::value >
  179. class __hash_map_hasher : private _Hash {
  180. public:
  181. _LIBCPP_HIDE_FROM_ABI __hash_map_hasher() : _Hash() {}
  182. _LIBCPP_HIDE_FROM_ABI __hash_map_hasher(const _Hash& __h) : _Hash(__h) {}
  183. _LIBCPP_HIDE_FROM_ABI const _Hash& hash_function() const { return *this; }
  184. _LIBCPP_HIDE_FROM_ABI size_t operator()(const _Tp& __x) const { return static_cast<const _Hash&>(*this)(__x.first); }
  185. _LIBCPP_HIDE_FROM_ABI size_t operator()(const typename _Tp::first_type& __x) const {
  186. return static_cast<const _Hash&>(*this)(__x);
  187. }
  188. };
  189. template <class _Tp, class _Hash>
  190. class __hash_map_hasher<_Tp, _Hash, false> {
  191. _Hash __hash_;
  192. public:
  193. _LIBCPP_HIDE_FROM_ABI __hash_map_hasher() : __hash_() {}
  194. _LIBCPP_HIDE_FROM_ABI __hash_map_hasher(const _Hash& __h) : __hash_(__h) {}
  195. _LIBCPP_HIDE_FROM_ABI const _Hash& hash_function() const { return __hash_; }
  196. _LIBCPP_HIDE_FROM_ABI size_t operator()(const _Tp& __x) const { return __hash_(__x.first); }
  197. _LIBCPP_HIDE_FROM_ABI size_t operator()(const typename _Tp::first_type& __x) const { return __hash_(__x); }
  198. };
  199. template <class _Tp, class _Pred, bool = std::is_empty<_Pred>::value && !std::__libcpp_is_final<_Pred>::value >
  200. class __hash_map_equal : private _Pred {
  201. public:
  202. _LIBCPP_HIDE_FROM_ABI __hash_map_equal() : _Pred() {}
  203. _LIBCPP_HIDE_FROM_ABI __hash_map_equal(const _Pred& __p) : _Pred(__p) {}
  204. _LIBCPP_HIDE_FROM_ABI const _Pred& key_eq() const { return *this; }
  205. _LIBCPP_HIDE_FROM_ABI bool operator()(const _Tp& __x, const _Tp& __y) const {
  206. return static_cast<const _Pred&>(*this)(__x.first, __y.first);
  207. }
  208. _LIBCPP_HIDE_FROM_ABI bool operator()(const typename _Tp::first_type& __x, const _Tp& __y) const {
  209. return static_cast<const _Pred&>(*this)(__x, __y.first);
  210. }
  211. _LIBCPP_HIDE_FROM_ABI bool operator()(const _Tp& __x, const typename _Tp::first_type& __y) const {
  212. return static_cast<const _Pred&>(*this)(__x.first, __y);
  213. }
  214. _LIBCPP_HIDE_FROM_ABI bool
  215. operator()(const typename _Tp::first_type& __x, const typename _Tp::first_type& __y) const {
  216. return static_cast<const _Pred&>(*this)(__x, __y);
  217. }
  218. };
  219. template <class _Tp, class _Pred>
  220. class __hash_map_equal<_Tp, _Pred, false> {
  221. _Pred __pred_;
  222. public:
  223. _LIBCPP_HIDE_FROM_ABI __hash_map_equal() : __pred_() {}
  224. _LIBCPP_HIDE_FROM_ABI __hash_map_equal(const _Pred& __p) : __pred_(__p) {}
  225. _LIBCPP_HIDE_FROM_ABI const _Pred& key_eq() const { return __pred_; }
  226. _LIBCPP_HIDE_FROM_ABI bool operator()(const _Tp& __x, const _Tp& __y) const { return __pred_(__x.first, __y.first); }
  227. _LIBCPP_HIDE_FROM_ABI bool operator()(const typename _Tp::first_type& __x, const _Tp& __y) const {
  228. return __pred_(__x, __y.first);
  229. }
  230. _LIBCPP_HIDE_FROM_ABI bool operator()(const _Tp& __x, const typename _Tp::first_type& __y) const {
  231. return __pred_(__x.first, __y);
  232. }
  233. _LIBCPP_HIDE_FROM_ABI bool
  234. operator()(const typename _Tp::first_type& __x, const typename _Tp::first_type& __y) const {
  235. return __pred_(__x, __y);
  236. }
  237. };
  238. template <class _Alloc>
  239. class __hash_map_node_destructor {
  240. typedef _Alloc allocator_type;
  241. typedef std::allocator_traits<allocator_type> __alloc_traits;
  242. typedef typename __alloc_traits::value_type::__node_value_type value_type;
  243. public:
  244. typedef typename __alloc_traits::pointer pointer;
  245. private:
  246. typedef typename value_type::first_type first_type;
  247. typedef typename value_type::second_type second_type;
  248. allocator_type& __na_;
  249. public:
  250. bool __first_constructed;
  251. bool __second_constructed;
  252. _LIBCPP_HIDE_FROM_ABI __hash_map_node_destructor(__hash_map_node_destructor const&) = default;
  253. __hash_map_node_destructor& operator=(const __hash_map_node_destructor&) = delete;
  254. _LIBCPP_HIDE_FROM_ABI explicit __hash_map_node_destructor(allocator_type& __na)
  255. : __na_(__na), __first_constructed(false), __second_constructed(false) {}
  256. #ifndef _LIBCPP_CXX03_LANG
  257. _LIBCPP_HIDE_FROM_ABI __hash_map_node_destructor(std::__hash_node_destructor<allocator_type>&& __x)
  258. : __na_(__x.__na_), __first_constructed(__x.__value_constructed), __second_constructed(__x.__value_constructed) {
  259. __x.__value_constructed = false;
  260. }
  261. #else // _LIBCPP_CXX03_LANG
  262. _LIBCPP_HIDE_FROM_ABI __hash_map_node_destructor(const std::__hash_node_destructor<allocator_type>& __x)
  263. : __na_(__x.__na_), __first_constructed(__x.__value_constructed), __second_constructed(__x.__value_constructed) {
  264. const_cast<bool&>(__x.__value_constructed) = false;
  265. }
  266. #endif // _LIBCPP_CXX03_LANG
  267. _LIBCPP_HIDE_FROM_ABI void operator()(pointer __p) {
  268. if (__second_constructed)
  269. __alloc_traits::destroy(__na_, std::addressof(__p->__get_value().second));
  270. if (__first_constructed)
  271. __alloc_traits::destroy(__na_, std::addressof(__p->__get_value().first));
  272. if (__p)
  273. __alloc_traits::deallocate(__na_, __p, 1);
  274. }
  275. };
  276. template <class _HashIterator>
  277. class _LIBCPP_TEMPLATE_VIS __hash_map_iterator {
  278. _HashIterator __i_;
  279. typedef const typename _HashIterator::value_type::first_type key_type;
  280. typedef typename _HashIterator::value_type::second_type mapped_type;
  281. public:
  282. typedef std::forward_iterator_tag iterator_category;
  283. typedef std::pair<key_type, mapped_type> value_type;
  284. typedef typename _HashIterator::difference_type difference_type;
  285. typedef value_type& reference;
  286. typedef std::__rebind_pointer_t<typename _HashIterator::pointer, value_type> pointer;
  287. _LIBCPP_HIDE_FROM_ABI __hash_map_iterator() {}
  288. _LIBCPP_HIDE_FROM_ABI __hash_map_iterator(_HashIterator __i) : __i_(__i) {}
  289. _LIBCPP_HIDE_FROM_ABI reference operator*() const { return *operator->(); }
  290. _LIBCPP_HIDE_FROM_ABI pointer operator->() const { return (pointer)__i_.operator->(); }
  291. _LIBCPP_HIDE_FROM_ABI __hash_map_iterator& operator++() {
  292. ++__i_;
  293. return *this;
  294. }
  295. _LIBCPP_HIDE_FROM_ABI __hash_map_iterator operator++(int) {
  296. __hash_map_iterator __t(*this);
  297. ++(*this);
  298. return __t;
  299. }
  300. friend _LIBCPP_HIDE_FROM_ABI bool operator==(const __hash_map_iterator& __x, const __hash_map_iterator& __y) {
  301. return __x.__i_ == __y.__i_;
  302. }
  303. friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const __hash_map_iterator& __x, const __hash_map_iterator& __y) {
  304. return __x.__i_ != __y.__i_;
  305. }
  306. template <class, class, class, class, class>
  307. friend class _LIBCPP_TEMPLATE_VIS hash_map;
  308. template <class, class, class, class, class>
  309. friend class _LIBCPP_TEMPLATE_VIS hash_multimap;
  310. template <class>
  311. friend class _LIBCPP_TEMPLATE_VIS __hash_const_iterator;
  312. template <class>
  313. friend class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator;
  314. template <class>
  315. friend class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator;
  316. };
  317. template <class _HashIterator>
  318. class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator {
  319. _HashIterator __i_;
  320. typedef const typename _HashIterator::value_type::first_type key_type;
  321. typedef typename _HashIterator::value_type::second_type mapped_type;
  322. public:
  323. typedef std::forward_iterator_tag iterator_category;
  324. typedef std::pair<key_type, mapped_type> value_type;
  325. typedef typename _HashIterator::difference_type difference_type;
  326. typedef const value_type& reference;
  327. typedef std::__rebind_pointer_t<typename _HashIterator::pointer, const value_type> pointer;
  328. _LIBCPP_HIDE_FROM_ABI __hash_map_const_iterator() {}
  329. _LIBCPP_HIDE_FROM_ABI __hash_map_const_iterator(_HashIterator __i) : __i_(__i) {}
  330. _LIBCPP_HIDE_FROM_ABI __hash_map_const_iterator(__hash_map_iterator<typename _HashIterator::__non_const_iterator> __i)
  331. : __i_(__i.__i_) {}
  332. _LIBCPP_HIDE_FROM_ABI reference operator*() const { return *operator->(); }
  333. _LIBCPP_HIDE_FROM_ABI pointer operator->() const { return (pointer)__i_.operator->(); }
  334. _LIBCPP_HIDE_FROM_ABI __hash_map_const_iterator& operator++() {
  335. ++__i_;
  336. return *this;
  337. }
  338. _LIBCPP_HIDE_FROM_ABI __hash_map_const_iterator operator++(int) {
  339. __hash_map_const_iterator __t(*this);
  340. ++(*this);
  341. return __t;
  342. }
  343. friend _LIBCPP_HIDE_FROM_ABI bool
  344. operator==(const __hash_map_const_iterator& __x, const __hash_map_const_iterator& __y) {
  345. return __x.__i_ == __y.__i_;
  346. }
  347. friend _LIBCPP_HIDE_FROM_ABI bool
  348. operator!=(const __hash_map_const_iterator& __x, const __hash_map_const_iterator& __y) {
  349. return __x.__i_ != __y.__i_;
  350. }
  351. template <class, class, class, class, class>
  352. friend class _LIBCPP_TEMPLATE_VIS hash_map;
  353. template <class, class, class, class, class>
  354. friend class _LIBCPP_TEMPLATE_VIS hash_multimap;
  355. template <class>
  356. friend class _LIBCPP_TEMPLATE_VIS __hash_const_iterator;
  357. template <class>
  358. friend class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator;
  359. };
  360. template <class _Key,
  361. class _Tp,
  362. class _Hash = hash<_Key>,
  363. class _Pred = std::equal_to<_Key>,
  364. class _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
  365. class _LIBCPP_TEMPLATE_VIS hash_map {
  366. public:
  367. // types
  368. typedef _Key key_type;
  369. typedef _Tp mapped_type;
  370. typedef _Tp data_type;
  371. typedef _Hash hasher;
  372. typedef _Pred key_equal;
  373. typedef _Alloc allocator_type;
  374. typedef std::pair<const key_type, mapped_type> value_type;
  375. typedef value_type& reference;
  376. typedef const value_type& const_reference;
  377. private:
  378. typedef std::pair<key_type, mapped_type> __value_type;
  379. typedef __hash_map_hasher<__value_type, hasher> __hasher;
  380. typedef __hash_map_equal<__value_type, key_equal> __key_equal;
  381. typedef std::__rebind_alloc<std::allocator_traits<allocator_type>, __value_type> __allocator_type;
  382. typedef std::__hash_table<__value_type, __hasher, __key_equal, __allocator_type> __table;
  383. __table __table_;
  384. typedef typename __table::__node_pointer __node_pointer;
  385. typedef typename __table::__node_const_pointer __node_const_pointer;
  386. typedef typename __table::__node_traits __node_traits;
  387. typedef typename __table::__node_allocator __node_allocator;
  388. typedef typename __table::__node __node;
  389. typedef __hash_map_node_destructor<__node_allocator> _Dp;
  390. typedef std::unique_ptr<__node, _Dp> __node_holder;
  391. typedef std::allocator_traits<allocator_type> __alloc_traits;
  392. public:
  393. typedef typename __alloc_traits::pointer pointer;
  394. typedef typename __alloc_traits::const_pointer const_pointer;
  395. typedef typename __alloc_traits::size_type size_type;
  396. typedef typename __alloc_traits::difference_type difference_type;
  397. typedef __hash_map_iterator<typename __table::iterator> iterator;
  398. typedef __hash_map_const_iterator<typename __table::const_iterator> const_iterator;
  399. _LIBCPP_HIDE_FROM_ABI hash_map() {}
  400. explicit _LIBCPP_HIDE_FROM_ABI
  401. hash_map(size_type __n, const hasher& __hf = hasher(), const key_equal& __eql = key_equal());
  402. _LIBCPP_HIDE_FROM_ABI hash_map(size_type __n, const hasher& __hf, const key_equal& __eql, const allocator_type& __a);
  403. template <class _InputIterator>
  404. _LIBCPP_HIDE_FROM_ABI hash_map(_InputIterator __first, _InputIterator __last);
  405. template <class _InputIterator>
  406. _LIBCPP_HIDE_FROM_ABI
  407. hash_map(_InputIterator __first,
  408. _InputIterator __last,
  409. size_type __n,
  410. const hasher& __hf = hasher(),
  411. const key_equal& __eql = key_equal());
  412. template <class _InputIterator>
  413. _LIBCPP_HIDE_FROM_ABI
  414. hash_map(_InputIterator __first,
  415. _InputIterator __last,
  416. size_type __n,
  417. const hasher& __hf,
  418. const key_equal& __eql,
  419. const allocator_type& __a);
  420. _LIBCPP_HIDE_FROM_ABI hash_map(const hash_map& __u);
  421. _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const { return allocator_type(__table_.__node_alloc()); }
  422. _LIBCPP_HIDE_FROM_ABI bool empty() const { return __table_.size() == 0; }
  423. _LIBCPP_HIDE_FROM_ABI size_type size() const { return __table_.size(); }
  424. _LIBCPP_HIDE_FROM_ABI size_type max_size() const { return __table_.max_size(); }
  425. _LIBCPP_HIDE_FROM_ABI iterator begin() { return __table_.begin(); }
  426. _LIBCPP_HIDE_FROM_ABI iterator end() { return __table_.end(); }
  427. _LIBCPP_HIDE_FROM_ABI const_iterator begin() const { return __table_.begin(); }
  428. _LIBCPP_HIDE_FROM_ABI const_iterator end() const { return __table_.end(); }
  429. _LIBCPP_HIDE_FROM_ABI std::pair<iterator, bool> insert(const value_type& __x) {
  430. return __table_.__insert_unique(__x);
  431. }
  432. _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator, const value_type& __x) { return insert(__x).first; }
  433. template <class _InputIterator>
  434. _LIBCPP_HIDE_FROM_ABI void insert(_InputIterator __first, _InputIterator __last);
  435. _LIBCPP_HIDE_FROM_ABI void erase(const_iterator __p) { __table_.erase(__p.__i_); }
  436. _LIBCPP_HIDE_FROM_ABI size_type erase(const key_type& __k) { return __table_.__erase_unique(__k); }
  437. _LIBCPP_HIDE_FROM_ABI void erase(const_iterator __first, const_iterator __last) {
  438. __table_.erase(__first.__i_, __last.__i_);
  439. }
  440. _LIBCPP_HIDE_FROM_ABI void clear() { __table_.clear(); }
  441. _LIBCPP_HIDE_FROM_ABI void swap(hash_map& __u) { __table_.swap(__u.__table_); }
  442. _LIBCPP_HIDE_FROM_ABI hasher hash_funct() const { return __table_.hash_function().hash_function(); }
  443. _LIBCPP_HIDE_FROM_ABI key_equal key_eq() const { return __table_.key_eq().key_eq(); }
  444. _LIBCPP_HIDE_FROM_ABI iterator find(const key_type& __k) { return __table_.find(__k); }
  445. _LIBCPP_HIDE_FROM_ABI const_iterator find(const key_type& __k) const { return __table_.find(__k); }
  446. _LIBCPP_HIDE_FROM_ABI size_type count(const key_type& __k) const { return __table_.__count_unique(__k); }
  447. _LIBCPP_HIDE_FROM_ABI std::pair<iterator, iterator> equal_range(const key_type& __k) {
  448. return __table_.__equal_range_unique(__k);
  449. }
  450. _LIBCPP_HIDE_FROM_ABI std::pair<const_iterator, const_iterator> equal_range(const key_type& __k) const {
  451. return __table_.__equal_range_unique(__k);
  452. }
  453. _LIBCPP_HIDE_FROM_ABI mapped_type& operator[](const key_type& __k);
  454. _LIBCPP_HIDE_FROM_ABI size_type bucket_count() const { return __table_.bucket_count(); }
  455. _LIBCPP_HIDE_FROM_ABI size_type max_bucket_count() const { return __table_.max_bucket_count(); }
  456. _LIBCPP_HIDE_FROM_ABI size_type elems_in_bucket(size_type __n) const { return __table_.bucket_size(__n); }
  457. _LIBCPP_HIDE_FROM_ABI void resize(size_type __n) { __table_.__rehash_unique(__n); }
  458. private:
  459. _LIBCPP_HIDE_FROM_ABI __node_holder __construct_node(const key_type& __k);
  460. };
  461. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  462. hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(size_type __n, const hasher& __hf, const key_equal& __eql)
  463. : __table_(__hf, __eql) {
  464. __table_.__rehash_unique(__n);
  465. }
  466. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  467. hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
  468. size_type __n, const hasher& __hf, const key_equal& __eql, const allocator_type& __a)
  469. : __table_(__hf, __eql, __a) {
  470. __table_.__rehash_unique(__n);
  471. }
  472. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  473. template <class _InputIterator>
  474. hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(_InputIterator __first, _InputIterator __last) {
  475. insert(__first, __last);
  476. }
  477. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  478. template <class _InputIterator>
  479. hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
  480. _InputIterator __first, _InputIterator __last, size_type __n, const hasher& __hf, const key_equal& __eql)
  481. : __table_(__hf, __eql) {
  482. __table_.__rehash_unique(__n);
  483. insert(__first, __last);
  484. }
  485. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  486. template <class _InputIterator>
  487. hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
  488. _InputIterator __first,
  489. _InputIterator __last,
  490. size_type __n,
  491. const hasher& __hf,
  492. const key_equal& __eql,
  493. const allocator_type& __a)
  494. : __table_(__hf, __eql, __a) {
  495. __table_.__rehash_unique(__n);
  496. insert(__first, __last);
  497. }
  498. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  499. hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(const hash_map& __u) : __table_(__u.__table_) {
  500. __table_.__rehash_unique(__u.bucket_count());
  501. insert(__u.begin(), __u.end());
  502. }
  503. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  504. typename hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::__node_holder
  505. hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::__construct_node(const key_type& __k) {
  506. __node_allocator& __na = __table_.__node_alloc();
  507. __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
  508. __node_traits::construct(__na, std::addressof(__h->__get_value().first), __k);
  509. __h.get_deleter().__first_constructed = true;
  510. __node_traits::construct(__na, std::addressof(__h->__get_value().second));
  511. __h.get_deleter().__second_constructed = true;
  512. return __h;
  513. }
  514. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  515. template <class _InputIterator>
  516. inline void hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::insert(_InputIterator __first, _InputIterator __last) {
  517. for (; __first != __last; ++__first)
  518. __table_.__insert_unique(*__first);
  519. }
  520. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  521. _Tp& hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::operator[](const key_type& __k) {
  522. iterator __i = find(__k);
  523. if (__i != end())
  524. return __i->second;
  525. __node_holder __h = __construct_node(__k);
  526. std::pair<iterator, bool> __r = __table_.__node_insert_unique(__h.get());
  527. __h.release();
  528. return __r.first->second;
  529. }
  530. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  531. inline _LIBCPP_HIDE_FROM_ABI void
  532. swap(hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x, hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y) {
  533. __x.swap(__y);
  534. }
  535. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  536. _LIBCPP_HIDE_FROM_ABI bool
  537. operator==(const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x, const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y) {
  538. if (__x.size() != __y.size())
  539. return false;
  540. typedef typename hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::const_iterator const_iterator;
  541. for (const_iterator __i = __x.begin(), __ex = __x.end(), __ey = __y.end(); __i != __ex; ++__i) {
  542. const_iterator __j = __y.find(__i->first);
  543. if (__j == __ey || !(*__i == *__j))
  544. return false;
  545. }
  546. return true;
  547. }
  548. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  549. inline _LIBCPP_HIDE_FROM_ABI bool
  550. operator!=(const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x, const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y) {
  551. return !(__x == __y);
  552. }
  553. template <class _Key,
  554. class _Tp,
  555. class _Hash = hash<_Key>,
  556. class _Pred = std::equal_to<_Key>,
  557. class _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
  558. class _LIBCPP_TEMPLATE_VIS hash_multimap {
  559. public:
  560. // types
  561. typedef _Key key_type;
  562. typedef _Tp mapped_type;
  563. typedef _Tp data_type;
  564. typedef _Hash hasher;
  565. typedef _Pred key_equal;
  566. typedef _Alloc allocator_type;
  567. typedef std::pair<const key_type, mapped_type> value_type;
  568. typedef value_type& reference;
  569. typedef const value_type& const_reference;
  570. private:
  571. typedef std::pair<key_type, mapped_type> __value_type;
  572. typedef __hash_map_hasher<__value_type, hasher> __hasher;
  573. typedef __hash_map_equal<__value_type, key_equal> __key_equal;
  574. typedef std::__rebind_alloc<std::allocator_traits<allocator_type>, __value_type> __allocator_type;
  575. typedef std::__hash_table<__value_type, __hasher, __key_equal, __allocator_type> __table;
  576. __table __table_;
  577. typedef typename __table::__node_traits __node_traits;
  578. typedef typename __table::__node_allocator __node_allocator;
  579. typedef typename __table::__node __node;
  580. typedef __hash_map_node_destructor<__node_allocator> _Dp;
  581. typedef std::unique_ptr<__node, _Dp> __node_holder;
  582. typedef std::allocator_traits<allocator_type> __alloc_traits;
  583. public:
  584. typedef typename __alloc_traits::pointer pointer;
  585. typedef typename __alloc_traits::const_pointer const_pointer;
  586. typedef typename __alloc_traits::size_type size_type;
  587. typedef typename __alloc_traits::difference_type difference_type;
  588. typedef __hash_map_iterator<typename __table::iterator> iterator;
  589. typedef __hash_map_const_iterator<typename __table::const_iterator> const_iterator;
  590. _LIBCPP_HIDE_FROM_ABI hash_multimap() {}
  591. explicit _LIBCPP_HIDE_FROM_ABI
  592. hash_multimap(size_type __n, const hasher& __hf = hasher(), const key_equal& __eql = key_equal());
  593. _LIBCPP_HIDE_FROM_ABI
  594. hash_multimap(size_type __n, const hasher& __hf, const key_equal& __eql, const allocator_type& __a);
  595. template <class _InputIterator>
  596. _LIBCPP_HIDE_FROM_ABI hash_multimap(_InputIterator __first, _InputIterator __last);
  597. template <class _InputIterator>
  598. _LIBCPP_HIDE_FROM_ABI
  599. hash_multimap(_InputIterator __first,
  600. _InputIterator __last,
  601. size_type __n,
  602. const hasher& __hf = hasher(),
  603. const key_equal& __eql = key_equal());
  604. template <class _InputIterator>
  605. _LIBCPP_HIDE_FROM_ABI hash_multimap(
  606. _InputIterator __first,
  607. _InputIterator __last,
  608. size_type __n,
  609. const hasher& __hf,
  610. const key_equal& __eql,
  611. const allocator_type& __a);
  612. _LIBCPP_HIDE_FROM_ABI hash_multimap(const hash_multimap& __u);
  613. _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const { return allocator_type(__table_.__node_alloc()); }
  614. _LIBCPP_HIDE_FROM_ABI bool empty() const { return __table_.size() == 0; }
  615. _LIBCPP_HIDE_FROM_ABI size_type size() const { return __table_.size(); }
  616. _LIBCPP_HIDE_FROM_ABI size_type max_size() const { return __table_.max_size(); }
  617. _LIBCPP_HIDE_FROM_ABI iterator begin() { return __table_.begin(); }
  618. _LIBCPP_HIDE_FROM_ABI iterator end() { return __table_.end(); }
  619. _LIBCPP_HIDE_FROM_ABI const_iterator begin() const { return __table_.begin(); }
  620. _LIBCPP_HIDE_FROM_ABI const_iterator end() const { return __table_.end(); }
  621. _LIBCPP_HIDE_FROM_ABI iterator insert(const value_type& __x) { return __table_.__insert_multi(__x); }
  622. _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator, const value_type& __x) { return insert(__x); }
  623. template <class _InputIterator>
  624. _LIBCPP_HIDE_FROM_ABI void insert(_InputIterator __first, _InputIterator __last);
  625. _LIBCPP_HIDE_FROM_ABI void erase(const_iterator __p) { __table_.erase(__p.__i_); }
  626. _LIBCPP_HIDE_FROM_ABI size_type erase(const key_type& __k) { return __table_.__erase_multi(__k); }
  627. _LIBCPP_HIDE_FROM_ABI void erase(const_iterator __first, const_iterator __last) {
  628. __table_.erase(__first.__i_, __last.__i_);
  629. }
  630. _LIBCPP_HIDE_FROM_ABI void clear() { __table_.clear(); }
  631. _LIBCPP_HIDE_FROM_ABI void swap(hash_multimap& __u) { __table_.swap(__u.__table_); }
  632. _LIBCPP_HIDE_FROM_ABI hasher hash_funct() const { return __table_.hash_function().hash_function(); }
  633. _LIBCPP_HIDE_FROM_ABI key_equal key_eq() const { return __table_.key_eq().key_eq(); }
  634. _LIBCPP_HIDE_FROM_ABI iterator find(const key_type& __k) { return __table_.find(__k); }
  635. _LIBCPP_HIDE_FROM_ABI const_iterator find(const key_type& __k) const { return __table_.find(__k); }
  636. _LIBCPP_HIDE_FROM_ABI size_type count(const key_type& __k) const { return __table_.__count_multi(__k); }
  637. _LIBCPP_HIDE_FROM_ABI std::pair<iterator, iterator> equal_range(const key_type& __k) {
  638. return __table_.__equal_range_multi(__k);
  639. }
  640. _LIBCPP_HIDE_FROM_ABI std::pair<const_iterator, const_iterator> equal_range(const key_type& __k) const {
  641. return __table_.__equal_range_multi(__k);
  642. }
  643. _LIBCPP_HIDE_FROM_ABI size_type bucket_count() const { return __table_.bucket_count(); }
  644. _LIBCPP_HIDE_FROM_ABI size_type max_bucket_count() const { return __table_.max_bucket_count(); }
  645. _LIBCPP_HIDE_FROM_ABI size_type elems_in_bucket(size_type __n) const { return __table_.bucket_size(__n); }
  646. _LIBCPP_HIDE_FROM_ABI void resize(size_type __n) { __table_.__rehash_multi(__n); }
  647. };
  648. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  649. hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(size_type __n, const hasher& __hf, const key_equal& __eql)
  650. : __table_(__hf, __eql) {
  651. __table_.__rehash_multi(__n);
  652. }
  653. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  654. hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
  655. size_type __n, const hasher& __hf, const key_equal& __eql, const allocator_type& __a)
  656. : __table_(__hf, __eql, __a) {
  657. __table_.__rehash_multi(__n);
  658. }
  659. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  660. template <class _InputIterator>
  661. hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(_InputIterator __first, _InputIterator __last) {
  662. insert(__first, __last);
  663. }
  664. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  665. template <class _InputIterator>
  666. hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
  667. _InputIterator __first, _InputIterator __last, size_type __n, const hasher& __hf, const key_equal& __eql)
  668. : __table_(__hf, __eql) {
  669. __table_.__rehash_multi(__n);
  670. insert(__first, __last);
  671. }
  672. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  673. template <class _InputIterator>
  674. hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
  675. _InputIterator __first,
  676. _InputIterator __last,
  677. size_type __n,
  678. const hasher& __hf,
  679. const key_equal& __eql,
  680. const allocator_type& __a)
  681. : __table_(__hf, __eql, __a) {
  682. __table_.__rehash_multi(__n);
  683. insert(__first, __last);
  684. }
  685. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  686. hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(const hash_multimap& __u) : __table_(__u.__table_) {
  687. __table_.__rehash_multi(__u.bucket_count());
  688. insert(__u.begin(), __u.end());
  689. }
  690. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  691. template <class _InputIterator>
  692. inline void hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::insert(_InputIterator __first, _InputIterator __last) {
  693. for (; __first != __last; ++__first)
  694. __table_.__insert_multi(*__first);
  695. }
  696. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  697. inline _LIBCPP_HIDE_FROM_ABI void
  698. swap(hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x, hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y) {
  699. __x.swap(__y);
  700. }
  701. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  702. _LIBCPP_HIDE_FROM_ABI bool operator==(const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  703. const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y) {
  704. if (__x.size() != __y.size())
  705. return false;
  706. typedef typename hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::const_iterator const_iterator;
  707. typedef std::pair<const_iterator, const_iterator> _EqRng;
  708. for (const_iterator __i = __x.begin(), __ex = __x.end(); __i != __ex;) {
  709. _EqRng __xeq = __x.equal_range(__i->first);
  710. _EqRng __yeq = __y.equal_range(__i->first);
  711. if (std::distance(__xeq.first, __xeq.second) != std::distance(__yeq.first, __yeq.second) ||
  712. !std::is_permutation(__xeq.first, __xeq.second, __yeq.first))
  713. return false;
  714. __i = __xeq.second;
  715. }
  716. return true;
  717. }
  718. template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  719. inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  720. const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y) {
  721. return !(__x == __y);
  722. }
  723. } // namespace __gnu_cxx
  724. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  725. # include <concepts>
  726. # include <iterator>
  727. # include <type_traits>
  728. #endif
  729. #endif // _LIBCPP_HASH_MAP