unordered_map 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590
  1. // Debugging unordered_map/unordered_multimap implementation -*- C++ -*-
  2. // Copyright (C) 2003-2023 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file debug/unordered_map
  21. * This file is a GNU debug extension to the Standard C++ Library.
  22. */
  23. #ifndef _GLIBCXX_DEBUG_UNORDERED_MAP
  24. #define _GLIBCXX_DEBUG_UNORDERED_MAP 1
  25. #pragma GCC system_header
  26. #if __cplusplus < 201103L
  27. # include <bits/c++0x_warning.h>
  28. #else
  29. # include <bits/c++config.h>
  30. namespace std _GLIBCXX_VISIBILITY(default) { namespace __debug {
  31. template<typename _Key, typename _Tp, typename _Hash, typename _Pred,
  32. typename _Allocator>
  33. class unordered_map;
  34. template<typename _Key, typename _Tp, typename _Hash, typename _Pred,
  35. typename _Allocator>
  36. class unordered_multimap;
  37. } } // namespace std::__debug
  38. # include <unordered_map>
  39. #include <debug/safe_unordered_container.h>
  40. #include <debug/safe_container.h>
  41. #include <debug/safe_iterator.h>
  42. #include <debug/safe_local_iterator.h>
  43. namespace std _GLIBCXX_VISIBILITY(default)
  44. {
  45. namespace __debug
  46. {
  47. /// Class std::unordered_map with safety/checking/debug instrumentation.
  48. template<typename _Key, typename _Tp,
  49. typename _Hash = std::hash<_Key>,
  50. typename _Pred = std::equal_to<_Key>,
  51. typename _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
  52. class unordered_map
  53. : public __gnu_debug::_Safe_container<
  54. unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>, _Alloc,
  55. __gnu_debug::_Safe_unordered_container>,
  56. public _GLIBCXX_STD_C::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>
  57. {
  58. typedef _GLIBCXX_STD_C::unordered_map<_Key, _Tp, _Hash,
  59. _Pred, _Alloc> _Base;
  60. typedef __gnu_debug::_Safe_container<unordered_map,
  61. _Alloc, __gnu_debug::_Safe_unordered_container> _Safe;
  62. typedef typename _Base::const_iterator _Base_const_iterator;
  63. typedef typename _Base::iterator _Base_iterator;
  64. typedef typename _Base::const_local_iterator
  65. _Base_const_local_iterator;
  66. typedef typename _Base::local_iterator _Base_local_iterator;
  67. template<typename _ItT, typename _SeqT, typename _CatT>
  68. friend class ::__gnu_debug::_Safe_iterator;
  69. template<typename _ItT, typename _SeqT>
  70. friend class ::__gnu_debug::_Safe_local_iterator;
  71. // Reference wrapper for base class. See PR libstdc++/90102.
  72. struct _Base_ref
  73. {
  74. _Base_ref(const _Base& __r) : _M_ref(__r) { }
  75. const _Base& _M_ref;
  76. };
  77. public:
  78. typedef typename _Base::size_type size_type;
  79. typedef typename _Base::hasher hasher;
  80. typedef typename _Base::key_equal key_equal;
  81. typedef typename _Base::allocator_type allocator_type;
  82. typedef typename _Base::key_type key_type;
  83. typedef typename _Base::value_type value_type;
  84. typedef typename _Base::mapped_type mapped_type;
  85. typedef typename _Base::pointer pointer;
  86. typedef typename _Base::const_pointer const_pointer;
  87. typedef typename _Base::reference reference;
  88. typedef typename _Base::const_reference const_reference;
  89. typedef __gnu_debug::_Safe_iterator<
  90. _Base_iterator, unordered_map> iterator;
  91. typedef __gnu_debug::_Safe_iterator<
  92. _Base_const_iterator, unordered_map> const_iterator;
  93. typedef __gnu_debug::_Safe_local_iterator<
  94. _Base_local_iterator, unordered_map> local_iterator;
  95. typedef __gnu_debug::_Safe_local_iterator<
  96. _Base_const_local_iterator, unordered_map> const_local_iterator;
  97. typedef typename _Base::difference_type difference_type;
  98. unordered_map() = default;
  99. explicit
  100. unordered_map(size_type __n,
  101. const hasher& __hf = hasher(),
  102. const key_equal& __eql = key_equal(),
  103. const allocator_type& __a = allocator_type())
  104. : _Base(__n, __hf, __eql, __a) { }
  105. template<typename _InputIterator>
  106. unordered_map(_InputIterator __first, _InputIterator __last,
  107. size_type __n = 0,
  108. const hasher& __hf = hasher(),
  109. const key_equal& __eql = key_equal(),
  110. const allocator_type& __a = allocator_type())
  111. : _Base(__gnu_debug::__base(
  112. __glibcxx_check_valid_constructor_range(__first, __last)),
  113. __gnu_debug::__base(__last), __n,
  114. __hf, __eql, __a) { }
  115. unordered_map(const unordered_map&) = default;
  116. unordered_map(_Base_ref __x)
  117. : _Base(__x._M_ref) { }
  118. unordered_map(unordered_map&&) = default;
  119. explicit
  120. unordered_map(const allocator_type& __a)
  121. : _Base(__a) { }
  122. unordered_map(const unordered_map& __umap,
  123. const allocator_type& __a)
  124. : _Base(__umap, __a) { }
  125. unordered_map(unordered_map&& __umap,
  126. const allocator_type& __a)
  127. noexcept( noexcept(_Base(std::move(__umap), __a)) )
  128. : _Safe(std::move(__umap), __a),
  129. _Base(std::move(__umap), __a) { }
  130. unordered_map(initializer_list<value_type> __l,
  131. size_type __n = 0,
  132. const hasher& __hf = hasher(),
  133. const key_equal& __eql = key_equal(),
  134. const allocator_type& __a = allocator_type())
  135. : _Base(__l, __n, __hf, __eql, __a) { }
  136. unordered_map(size_type __n, const allocator_type& __a)
  137. : unordered_map(__n, hasher(), key_equal(), __a)
  138. { }
  139. unordered_map(size_type __n,
  140. const hasher& __hf,
  141. const allocator_type& __a)
  142. : unordered_map(__n, __hf, key_equal(), __a)
  143. { }
  144. template<typename _InputIterator>
  145. unordered_map(_InputIterator __first, _InputIterator __last,
  146. size_type __n,
  147. const allocator_type& __a)
  148. : unordered_map(__first, __last, __n, hasher(), key_equal(), __a)
  149. { }
  150. template<typename _InputIterator>
  151. unordered_map(_InputIterator __first, _InputIterator __last,
  152. size_type __n,
  153. const hasher& __hf,
  154. const allocator_type& __a)
  155. : unordered_map(__first, __last, __n, __hf, key_equal(), __a)
  156. { }
  157. unordered_map(initializer_list<value_type> __l,
  158. size_type __n,
  159. const allocator_type& __a)
  160. : unordered_map(__l, __n, hasher(), key_equal(), __a)
  161. { }
  162. unordered_map(initializer_list<value_type> __l,
  163. size_type __n,
  164. const hasher& __hf,
  165. const allocator_type& __a)
  166. : unordered_map(__l, __n, __hf, key_equal(), __a)
  167. { }
  168. ~unordered_map() = default;
  169. unordered_map&
  170. operator=(const unordered_map&) = default;
  171. unordered_map&
  172. operator=(unordered_map&&) = default;
  173. unordered_map&
  174. operator=(initializer_list<value_type> __l)
  175. {
  176. _Base::operator=(__l);
  177. this->_M_invalidate_all();
  178. return *this;
  179. }
  180. using _Base::get_allocator;
  181. using _Base::empty;
  182. using _Base::size;
  183. using _Base::max_size;
  184. void
  185. swap(unordered_map& __x)
  186. noexcept( noexcept(declval<_Base&>().swap(__x)) )
  187. {
  188. _Safe::_M_swap(__x);
  189. _Base::swap(__x);
  190. }
  191. void
  192. clear() noexcept
  193. {
  194. _Base::clear();
  195. this->_M_invalidate_all();
  196. }
  197. iterator
  198. begin() noexcept
  199. { return { _Base::begin(), this }; }
  200. const_iterator
  201. begin() const noexcept
  202. { return { _Base::begin(), this }; }
  203. iterator
  204. end() noexcept
  205. { return { _Base::end(), this }; }
  206. const_iterator
  207. end() const noexcept
  208. { return { _Base::end(), this }; }
  209. const_iterator
  210. cbegin() const noexcept
  211. { return { _Base::cbegin(), this }; }
  212. const_iterator
  213. cend() const noexcept
  214. { return { _Base::cend(), this }; }
  215. // local versions
  216. local_iterator
  217. begin(size_type __b)
  218. {
  219. __glibcxx_check_bucket_index(__b);
  220. return { _Base::begin(__b), this };
  221. }
  222. local_iterator
  223. end(size_type __b)
  224. {
  225. __glibcxx_check_bucket_index(__b);
  226. return { _Base::end(__b), this };
  227. }
  228. const_local_iterator
  229. begin(size_type __b) const
  230. {
  231. __glibcxx_check_bucket_index(__b);
  232. return { _Base::begin(__b), this };
  233. }
  234. const_local_iterator
  235. end(size_type __b) const
  236. {
  237. __glibcxx_check_bucket_index(__b);
  238. return { _Base::end(__b), this };
  239. }
  240. const_local_iterator
  241. cbegin(size_type __b) const
  242. {
  243. __glibcxx_check_bucket_index(__b);
  244. return { _Base::cbegin(__b), this };
  245. }
  246. const_local_iterator
  247. cend(size_type __b) const
  248. {
  249. __glibcxx_check_bucket_index(__b);
  250. return { _Base::cend(__b), this };
  251. }
  252. using _Base::bucket_count;
  253. using _Base::max_bucket_count;
  254. using _Base::bucket;
  255. size_type
  256. bucket_size(size_type __b) const
  257. {
  258. __glibcxx_check_bucket_index(__b);
  259. return _Base::bucket_size(__b);
  260. }
  261. using _Base::load_factor;
  262. float
  263. max_load_factor() const noexcept
  264. { return _Base::max_load_factor(); }
  265. void
  266. max_load_factor(float __f)
  267. {
  268. __glibcxx_check_max_load_factor(__f);
  269. _Base::max_load_factor(__f);
  270. }
  271. template<typename... _Args>
  272. std::pair<iterator, bool>
  273. emplace(_Args&&... __args)
  274. {
  275. size_type __bucket_count = this->bucket_count();
  276. auto __res = _Base::emplace(std::forward<_Args>(__args)...);
  277. _M_check_rehashed(__bucket_count);
  278. return { { __res.first, this }, __res.second };
  279. }
  280. template<typename... _Args>
  281. iterator
  282. emplace_hint(const_iterator __hint, _Args&&... __args)
  283. {
  284. __glibcxx_check_insert(__hint);
  285. size_type __bucket_count = this->bucket_count();
  286. auto __it = _Base::emplace_hint(__hint.base(),
  287. std::forward<_Args>(__args)...);
  288. _M_check_rehashed(__bucket_count);
  289. return { __it, this };
  290. }
  291. std::pair<iterator, bool>
  292. insert(const value_type& __obj)
  293. {
  294. size_type __bucket_count = this->bucket_count();
  295. auto __res = _Base::insert(__obj);
  296. _M_check_rehashed(__bucket_count);
  297. return { { __res.first, this }, __res.second };
  298. }
  299. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  300. // 2354. Unnecessary copying when inserting into maps with braced-init
  301. std::pair<iterator, bool>
  302. insert(value_type&& __x)
  303. {
  304. size_type __bucket_count = this->bucket_count();
  305. auto __res = _Base::insert(std::move(__x));
  306. _M_check_rehashed(__bucket_count);
  307. return { { __res.first, this }, __res.second };
  308. }
  309. template<typename _Pair, typename = typename
  310. std::enable_if<std::is_constructible<value_type,
  311. _Pair&&>::value>::type>
  312. std::pair<iterator, bool>
  313. insert(_Pair&& __obj)
  314. {
  315. size_type __bucket_count = this->bucket_count();
  316. auto __res = _Base::insert(std::forward<_Pair>(__obj));
  317. _M_check_rehashed(__bucket_count);
  318. return { { __res.first, this }, __res.second };
  319. }
  320. iterator
  321. insert(const_iterator __hint, const value_type& __obj)
  322. {
  323. __glibcxx_check_insert(__hint);
  324. size_type __bucket_count = this->bucket_count();
  325. auto __it = _Base::insert(__hint.base(), __obj);
  326. _M_check_rehashed(__bucket_count);
  327. return { __it, this };
  328. }
  329. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  330. // 2354. Unnecessary copying when inserting into maps with braced-init
  331. iterator
  332. insert(const_iterator __hint, value_type&& __x)
  333. {
  334. __glibcxx_check_insert(__hint);
  335. size_type __bucket_count = this->bucket_count();
  336. auto __it = _Base::insert(__hint.base(), std::move(__x));
  337. _M_check_rehashed(__bucket_count);
  338. return { __it, this };
  339. }
  340. template<typename _Pair, typename = typename
  341. std::enable_if<std::is_constructible<value_type,
  342. _Pair&&>::value>::type>
  343. iterator
  344. insert(const_iterator __hint, _Pair&& __obj)
  345. {
  346. __glibcxx_check_insert(__hint);
  347. size_type __bucket_count = this->bucket_count();
  348. auto __it = _Base::insert(__hint.base(), std::forward<_Pair>(__obj));
  349. _M_check_rehashed(__bucket_count);
  350. return { __it, this };
  351. }
  352. void
  353. insert(std::initializer_list<value_type> __l)
  354. {
  355. size_type __bucket_count = this->bucket_count();
  356. _Base::insert(__l);
  357. _M_check_rehashed(__bucket_count);
  358. }
  359. template<typename _InputIterator>
  360. void
  361. insert(_InputIterator __first, _InputIterator __last)
  362. {
  363. typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
  364. __glibcxx_check_valid_range2(__first, __last, __dist);
  365. size_type __bucket_count = this->bucket_count();
  366. if (__dist.second >= __gnu_debug::__dp_sign)
  367. _Base::insert(__gnu_debug::__unsafe(__first),
  368. __gnu_debug::__unsafe(__last));
  369. else
  370. _Base::insert(__first, __last);
  371. _M_check_rehashed(__bucket_count);
  372. }
  373. #if __cplusplus > 201402L
  374. template <typename... _Args>
  375. pair<iterator, bool>
  376. try_emplace(const key_type& __k, _Args&&... __args)
  377. {
  378. auto __res = _Base::try_emplace(__k,
  379. std::forward<_Args>(__args)...);
  380. return { { __res.first, this }, __res.second };
  381. }
  382. template <typename... _Args>
  383. pair<iterator, bool>
  384. try_emplace(key_type&& __k, _Args&&... __args)
  385. {
  386. auto __res = _Base::try_emplace(std::move(__k),
  387. std::forward<_Args>(__args)...);
  388. return { { __res.first, this }, __res.second };
  389. }
  390. template <typename... _Args>
  391. iterator
  392. try_emplace(const_iterator __hint, const key_type& __k,
  393. _Args&&... __args)
  394. {
  395. __glibcxx_check_insert(__hint);
  396. return { _Base::try_emplace(__hint.base(), __k,
  397. std::forward<_Args>(__args)...),
  398. this };
  399. }
  400. template <typename... _Args>
  401. iterator
  402. try_emplace(const_iterator __hint, key_type&& __k, _Args&&... __args)
  403. {
  404. __glibcxx_check_insert(__hint);
  405. return { _Base::try_emplace(__hint.base(), std::move(__k),
  406. std::forward<_Args>(__args)...),
  407. this };
  408. }
  409. template <typename _Obj>
  410. pair<iterator, bool>
  411. insert_or_assign(const key_type& __k, _Obj&& __obj)
  412. {
  413. auto __res = _Base::insert_or_assign(__k,
  414. std::forward<_Obj>(__obj));
  415. return { { __res.first, this }, __res.second };
  416. }
  417. template <typename _Obj>
  418. pair<iterator, bool>
  419. insert_or_assign(key_type&& __k, _Obj&& __obj)
  420. {
  421. auto __res = _Base::insert_or_assign(std::move(__k),
  422. std::forward<_Obj>(__obj));
  423. return { { __res.first, this }, __res.second };
  424. }
  425. template <typename _Obj>
  426. iterator
  427. insert_or_assign(const_iterator __hint, const key_type& __k,
  428. _Obj&& __obj)
  429. {
  430. __glibcxx_check_insert(__hint);
  431. return { _Base::insert_or_assign(__hint.base(), __k,
  432. std::forward<_Obj>(__obj)),
  433. this };
  434. }
  435. template <typename _Obj>
  436. iterator
  437. insert_or_assign(const_iterator __hint, key_type&& __k, _Obj&& __obj)
  438. {
  439. __glibcxx_check_insert(__hint);
  440. return { _Base::insert_or_assign(__hint.base(), std::move(__k),
  441. std::forward<_Obj>(__obj)),
  442. this };
  443. }
  444. #endif // C++17
  445. #if __cplusplus > 201402L
  446. using node_type = typename _Base::node_type;
  447. using insert_return_type = _Node_insert_return<iterator, node_type>;
  448. node_type
  449. extract(const_iterator __position)
  450. {
  451. __glibcxx_check_erase(__position);
  452. return _M_extract(__position.base());
  453. }
  454. node_type
  455. extract(const key_type& __key)
  456. {
  457. const auto __position = _Base::find(__key);
  458. if (__position != _Base::end())
  459. return _M_extract(__position);
  460. return {};
  461. }
  462. insert_return_type
  463. insert(node_type&& __nh)
  464. {
  465. auto __ret = _Base::insert(std::move(__nh));
  466. return
  467. { { __ret.position, this }, __ret.inserted, std::move(__ret.node) };
  468. }
  469. iterator
  470. insert(const_iterator __hint, node_type&& __nh)
  471. {
  472. __glibcxx_check_insert(__hint);
  473. return { _Base::insert(__hint.base(), std::move(__nh)), this };
  474. }
  475. template<typename _H2, typename _P2>
  476. void
  477. merge(unordered_map<_Key, _Tp, _H2, _P2, _Alloc>& __source)
  478. {
  479. auto __guard
  480. = _Safe::_S_uc_guard(std::__detail::_Select1st{}, __source);
  481. _Base::merge(__source);
  482. }
  483. template<typename _H2, typename _P2>
  484. void
  485. merge(unordered_map<_Key, _Tp, _H2, _P2, _Alloc>&& __source)
  486. { merge(__source); }
  487. template<typename _H2, typename _P2>
  488. void
  489. merge(unordered_multimap<_Key, _Tp, _H2, _P2, _Alloc>& __source)
  490. {
  491. auto __guard
  492. = _Safe::_S_umc_guard(std::__detail::_Select1st{}, __source);
  493. _Base::merge(__source);
  494. }
  495. template<typename _H2, typename _P2>
  496. void
  497. merge(unordered_multimap<_Key, _Tp, _H2, _P2, _Alloc>&& __source)
  498. { merge(__source); }
  499. #endif // C++17
  500. using _Base::hash_function;
  501. using _Base::key_eq;
  502. iterator
  503. find(const key_type& __key)
  504. { return { _Base::find(__key), this }; }
  505. #if __cplusplus > 201703L
  506. template<typename _Kt,
  507. typename = std::__has_is_transparent_t<_Hash, _Kt>,
  508. typename = std::__has_is_transparent_t<_Pred, _Kt>>
  509. iterator
  510. find(const _Kt& __k)
  511. { return { _Base::find(__k), this }; }
  512. #endif
  513. const_iterator
  514. find(const key_type& __key) const
  515. { return { _Base::find(__key), this }; }
  516. #if __cplusplus > 201703L
  517. template<typename _Kt,
  518. typename = std::__has_is_transparent_t<_Hash, _Kt>,
  519. typename = std::__has_is_transparent_t<_Pred, _Kt>>
  520. const_iterator
  521. find(const _Kt& __k) const
  522. { return { _Base::find(__k), this }; }
  523. #endif
  524. using _Base::count;
  525. #if __cplusplus > 201703L
  526. using _Base::contains;
  527. #endif
  528. std::pair<iterator, iterator>
  529. equal_range(const key_type& __key)
  530. {
  531. auto __res = _Base::equal_range(__key);
  532. return { { __res.first, this }, { __res.second, this } };
  533. }
  534. #if __cplusplus > 201703L
  535. template<typename _Kt,
  536. typename = std::__has_is_transparent_t<_Hash, _Kt>,
  537. typename = std::__has_is_transparent_t<_Pred, _Kt>>
  538. std::pair<iterator, iterator>
  539. equal_range(const _Kt& __k)
  540. {
  541. auto __res = _Base::equal_range(__k);
  542. return { { __res.first, this }, { __res.second, this } };
  543. }
  544. #endif
  545. std::pair<const_iterator, const_iterator>
  546. equal_range(const key_type& __key) const
  547. {
  548. auto __res = _Base::equal_range(__key);
  549. return { { __res.first, this }, { __res.second, this } };
  550. }
  551. #if __cplusplus > 201703L
  552. template<typename _Kt,
  553. typename = std::__has_is_transparent_t<_Hash, _Kt>,
  554. typename = std::__has_is_transparent_t<_Pred, _Kt>>
  555. std::pair<const_iterator, const_iterator>
  556. equal_range(const _Kt& __k) const
  557. {
  558. auto __res = _Base::equal_range(__k);
  559. return { { __res.first, this }, { __res.second, this } };
  560. }
  561. #endif
  562. using _Base::operator[];
  563. using _Base::at;
  564. size_type
  565. erase(const key_type& __key)
  566. {
  567. size_type __ret(0);
  568. auto __victim = _Base::find(__key);
  569. if (__victim != _Base::end())
  570. {
  571. _M_erase(__victim);
  572. __ret = 1;
  573. }
  574. return __ret;
  575. }
  576. iterator
  577. erase(const_iterator __it)
  578. {
  579. __glibcxx_check_erase(__it);
  580. return { _M_erase(__it.base()), this };
  581. }
  582. _Base_iterator
  583. erase(_Base_const_iterator __it)
  584. {
  585. __glibcxx_check_erase2(__it);
  586. return _M_erase(__it);
  587. }
  588. iterator
  589. erase(iterator __it)
  590. {
  591. __glibcxx_check_erase(__it);
  592. return { _M_erase(__it.base()), this };
  593. }
  594. iterator
  595. erase(const_iterator __first, const_iterator __last)
  596. {
  597. __glibcxx_check_erase_range(__first, __last);
  598. for (auto __tmp = __first.base(); __tmp != __last.base(); ++__tmp)
  599. {
  600. _GLIBCXX_DEBUG_VERIFY(__tmp != _Base::cend(),
  601. _M_message(__gnu_debug::__msg_valid_range)
  602. ._M_iterator(__first, "first")
  603. ._M_iterator(__last, "last"));
  604. _M_invalidate(__tmp);
  605. }
  606. size_type __bucket_count = this->bucket_count();
  607. auto __next = _Base::erase(__first.base(), __last.base());
  608. _M_check_rehashed(__bucket_count);
  609. return { __next, this };
  610. }
  611. using _Base::rehash;
  612. using _Base::reserve;
  613. _Base&
  614. _M_base() noexcept { return *this; }
  615. const _Base&
  616. _M_base() const noexcept { return *this; }
  617. private:
  618. void
  619. _M_check_rehashed(size_type __prev_count)
  620. {
  621. if (__prev_count != this->bucket_count())
  622. this->_M_invalidate_all();
  623. }
  624. void
  625. _M_invalidate(_Base_const_iterator __victim)
  626. {
  627. this->_M_invalidate_if(
  628. [__victim](_Base_const_iterator __it) { return __it == __victim; });
  629. this->_M_invalidate_local_if(
  630. [__victim](_Base_const_local_iterator __it)
  631. { return __it == __victim; });
  632. }
  633. _Base_iterator
  634. _M_erase(_Base_const_iterator __victim)
  635. {
  636. _M_invalidate(__victim);
  637. size_type __bucket_count = this->bucket_count();
  638. _Base_iterator __next = _Base::erase(__victim);
  639. _M_check_rehashed(__bucket_count);
  640. return __next;
  641. }
  642. #if __cplusplus > 201402L
  643. node_type
  644. _M_extract(_Base_const_iterator __victim)
  645. {
  646. _M_invalidate(__victim);
  647. return _Base::extract(__victim);
  648. }
  649. #endif
  650. };
  651. #if __cpp_deduction_guides >= 201606
  652. template<typename _InputIterator,
  653. typename _Hash = hash<__iter_key_t<_InputIterator>>,
  654. typename _Pred = equal_to<__iter_key_t<_InputIterator>>,
  655. typename _Allocator = allocator<__iter_to_alloc_t<_InputIterator>>,
  656. typename = _RequireInputIter<_InputIterator>,
  657. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  658. typename = _RequireNotAllocator<_Pred>,
  659. typename = _RequireAllocator<_Allocator>>
  660. unordered_map(_InputIterator, _InputIterator,
  661. typename unordered_map<int, int>::size_type = {},
  662. _Hash = _Hash(), _Pred = _Pred(), _Allocator = _Allocator())
  663. -> unordered_map<__iter_key_t<_InputIterator>,
  664. __iter_val_t<_InputIterator>,
  665. _Hash, _Pred, _Allocator>;
  666. template<typename _Key, typename _Tp, typename _Hash = hash<_Key>,
  667. typename _Pred = equal_to<_Key>,
  668. typename _Allocator = allocator<pair<const _Key, _Tp>>,
  669. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  670. typename = _RequireNotAllocator<_Pred>,
  671. typename = _RequireAllocator<_Allocator>>
  672. unordered_map(initializer_list<pair<_Key, _Tp>>,
  673. typename unordered_map<int, int>::size_type = {},
  674. _Hash = _Hash(), _Pred = _Pred(), _Allocator = _Allocator())
  675. -> unordered_map<_Key, _Tp, _Hash, _Pred, _Allocator>;
  676. template<typename _InputIterator, typename _Allocator,
  677. typename = _RequireInputIter<_InputIterator>,
  678. typename = _RequireAllocator<_Allocator>>
  679. unordered_map(_InputIterator, _InputIterator,
  680. typename unordered_map<int, int>::size_type, _Allocator)
  681. -> unordered_map<__iter_key_t<_InputIterator>,
  682. __iter_val_t<_InputIterator>,
  683. hash<__iter_key_t<_InputIterator>>,
  684. equal_to<__iter_key_t<_InputIterator>>,
  685. _Allocator>;
  686. template<typename _InputIterator, typename _Allocator,
  687. typename = _RequireInputIter<_InputIterator>,
  688. typename = _RequireAllocator<_Allocator>>
  689. unordered_map(_InputIterator, _InputIterator, _Allocator)
  690. -> unordered_map<__iter_key_t<_InputIterator>,
  691. __iter_val_t<_InputIterator>,
  692. hash<__iter_key_t<_InputIterator>>,
  693. equal_to<__iter_key_t<_InputIterator>>,
  694. _Allocator>;
  695. template<typename _InputIterator, typename _Hash, typename _Allocator,
  696. typename = _RequireInputIter<_InputIterator>,
  697. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  698. typename = _RequireAllocator<_Allocator>>
  699. unordered_map(_InputIterator, _InputIterator,
  700. typename unordered_map<int, int>::size_type,
  701. _Hash, _Allocator)
  702. -> unordered_map<__iter_key_t<_InputIterator>,
  703. __iter_val_t<_InputIterator>, _Hash,
  704. equal_to<__iter_key_t<_InputIterator>>, _Allocator>;
  705. template<typename _Key, typename _Tp, typename _Allocator,
  706. typename = _RequireAllocator<_Allocator>>
  707. unordered_map(initializer_list<pair<_Key, _Tp>>,
  708. typename unordered_map<int, int>::size_type,
  709. _Allocator)
  710. -> unordered_map<_Key, _Tp, hash<_Key>, equal_to<_Key>, _Allocator>;
  711. template<typename _Key, typename _Tp, typename _Allocator,
  712. typename = _RequireAllocator<_Allocator>>
  713. unordered_map(initializer_list<pair<_Key, _Tp>>, _Allocator)
  714. -> unordered_map<_Key, _Tp, hash<_Key>, equal_to<_Key>, _Allocator>;
  715. template<typename _Key, typename _Tp, typename _Hash, typename _Allocator,
  716. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  717. typename = _RequireAllocator<_Allocator>>
  718. unordered_map(initializer_list<pair<_Key, _Tp>>,
  719. typename unordered_map<int, int>::size_type,
  720. _Hash, _Allocator)
  721. -> unordered_map<_Key, _Tp, _Hash, equal_to<_Key>, _Allocator>;
  722. #endif
  723. template<typename _Key, typename _Tp, typename _Hash,
  724. typename _Pred, typename _Alloc>
  725. inline void
  726. swap(unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  727. unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  728. noexcept(noexcept(__x.swap(__y)))
  729. { __x.swap(__y); }
  730. template<typename _Key, typename _Tp, typename _Hash,
  731. typename _Pred, typename _Alloc>
  732. inline bool
  733. operator==(const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  734. const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  735. { return __x._M_base() == __y._M_base(); }
  736. #if __cpp_impl_three_way_comparison < 201907L
  737. template<typename _Key, typename _Tp, typename _Hash,
  738. typename _Pred, typename _Alloc>
  739. inline bool
  740. operator!=(const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  741. const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  742. { return !(__x == __y); }
  743. #endif
  744. /// Class std::unordered_multimap with safety/checking/debug instrumentation.
  745. template<typename _Key, typename _Tp,
  746. typename _Hash = std::hash<_Key>,
  747. typename _Pred = std::equal_to<_Key>,
  748. typename _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
  749. class unordered_multimap
  750. : public __gnu_debug::_Safe_container<
  751. unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>, _Alloc,
  752. __gnu_debug::_Safe_unordered_container>,
  753. public _GLIBCXX_STD_C::unordered_multimap<
  754. _Key, _Tp, _Hash, _Pred, _Alloc>
  755. {
  756. typedef _GLIBCXX_STD_C::unordered_multimap<_Key, _Tp, _Hash,
  757. _Pred, _Alloc> _Base;
  758. typedef __gnu_debug::_Safe_container<unordered_multimap,
  759. _Alloc, __gnu_debug::_Safe_unordered_container> _Safe;
  760. typedef typename _Base::const_iterator _Base_const_iterator;
  761. typedef typename _Base::iterator _Base_iterator;
  762. typedef typename _Base::const_local_iterator _Base_const_local_iterator;
  763. typedef typename _Base::local_iterator _Base_local_iterator;
  764. template<typename _ItT, typename _SeqT, typename _CatT>
  765. friend class ::__gnu_debug::_Safe_iterator;
  766. template<typename _ItT, typename _SeqT>
  767. friend class ::__gnu_debug::_Safe_local_iterator;
  768. // Reference wrapper for base class. See PR libstdc++/90102.
  769. struct _Base_ref
  770. {
  771. _Base_ref(const _Base& __r) : _M_ref(__r) { }
  772. const _Base& _M_ref;
  773. };
  774. public:
  775. typedef typename _Base::size_type size_type;
  776. typedef typename _Base::hasher hasher;
  777. typedef typename _Base::key_equal key_equal;
  778. typedef typename _Base::allocator_type allocator_type;
  779. typedef typename _Base::key_type key_type;
  780. typedef typename _Base::value_type value_type;
  781. typedef typename _Base::mapped_type mapped_type;
  782. typedef typename _Base::pointer pointer;
  783. typedef typename _Base::const_pointer const_pointer;
  784. typedef typename _Base::reference reference;
  785. typedef typename _Base::const_reference const_reference;
  786. typedef __gnu_debug::_Safe_iterator<
  787. _Base_iterator, unordered_multimap> iterator;
  788. typedef __gnu_debug::_Safe_iterator<
  789. _Base_const_iterator, unordered_multimap> const_iterator;
  790. typedef __gnu_debug::_Safe_local_iterator<
  791. _Base_local_iterator, unordered_multimap> local_iterator;
  792. typedef __gnu_debug::_Safe_local_iterator<
  793. _Base_const_local_iterator, unordered_multimap> const_local_iterator;
  794. typedef typename _Base::difference_type difference_type;
  795. unordered_multimap() = default;
  796. explicit
  797. unordered_multimap(size_type __n,
  798. const hasher& __hf = hasher(),
  799. const key_equal& __eql = key_equal(),
  800. const allocator_type& __a = allocator_type())
  801. : _Base(__n, __hf, __eql, __a) { }
  802. template<typename _InputIterator>
  803. unordered_multimap(_InputIterator __first, _InputIterator __last,
  804. size_type __n = 0,
  805. const hasher& __hf = hasher(),
  806. const key_equal& __eql = key_equal(),
  807. const allocator_type& __a = allocator_type())
  808. : _Base(__gnu_debug::__base(
  809. __glibcxx_check_valid_constructor_range(__first, __last)),
  810. __gnu_debug::__base(__last), __n,
  811. __hf, __eql, __a) { }
  812. unordered_multimap(const unordered_multimap&) = default;
  813. unordered_multimap(_Base_ref __x)
  814. : _Base(__x._M_ref) { }
  815. unordered_multimap(unordered_multimap&&) = default;
  816. explicit
  817. unordered_multimap(const allocator_type& __a)
  818. : _Base(__a) { }
  819. unordered_multimap(const unordered_multimap& __umap,
  820. const allocator_type& __a)
  821. : _Base(__umap, __a) { }
  822. unordered_multimap(unordered_multimap&& __umap,
  823. const allocator_type& __a)
  824. noexcept( noexcept(_Base(std::move(__umap), __a)) )
  825. : _Safe(std::move(__umap), __a),
  826. _Base(std::move(__umap), __a) { }
  827. unordered_multimap(initializer_list<value_type> __l,
  828. size_type __n = 0,
  829. const hasher& __hf = hasher(),
  830. const key_equal& __eql = key_equal(),
  831. const allocator_type& __a = allocator_type())
  832. : _Base(__l, __n, __hf, __eql, __a) { }
  833. unordered_multimap(size_type __n, const allocator_type& __a)
  834. : unordered_multimap(__n, hasher(), key_equal(), __a)
  835. { }
  836. unordered_multimap(size_type __n, const hasher& __hf,
  837. const allocator_type& __a)
  838. : unordered_multimap(__n, __hf, key_equal(), __a)
  839. { }
  840. template<typename _InputIterator>
  841. unordered_multimap(_InputIterator __first, _InputIterator __last,
  842. size_type __n,
  843. const allocator_type& __a)
  844. : unordered_multimap(__first, __last, __n, hasher(), key_equal(), __a)
  845. { }
  846. template<typename _InputIterator>
  847. unordered_multimap(_InputIterator __first, _InputIterator __last,
  848. size_type __n, const hasher& __hf,
  849. const allocator_type& __a)
  850. : unordered_multimap(__first, __last, __n, __hf, key_equal(), __a)
  851. { }
  852. unordered_multimap(initializer_list<value_type> __l,
  853. size_type __n,
  854. const allocator_type& __a)
  855. : unordered_multimap(__l, __n, hasher(), key_equal(), __a)
  856. { }
  857. unordered_multimap(initializer_list<value_type> __l,
  858. size_type __n, const hasher& __hf,
  859. const allocator_type& __a)
  860. : unordered_multimap(__l, __n, __hf, key_equal(), __a)
  861. { }
  862. ~unordered_multimap() = default;
  863. unordered_multimap&
  864. operator=(const unordered_multimap&) = default;
  865. unordered_multimap&
  866. operator=(unordered_multimap&&) = default;
  867. unordered_multimap&
  868. operator=(initializer_list<value_type> __l)
  869. {
  870. _Base::operator=(__l);
  871. this->_M_invalidate_all();
  872. return *this;
  873. }
  874. using _Base::get_allocator;
  875. using _Base::empty;
  876. using _Base::size;
  877. using _Base::max_size;
  878. void
  879. swap(unordered_multimap& __x)
  880. noexcept( noexcept(declval<_Base&>().swap(__x)) )
  881. {
  882. _Safe::_M_swap(__x);
  883. _Base::swap(__x);
  884. }
  885. void
  886. clear() noexcept
  887. {
  888. _Base::clear();
  889. this->_M_invalidate_all();
  890. }
  891. iterator
  892. begin() noexcept
  893. { return { _Base::begin(), this }; }
  894. const_iterator
  895. begin() const noexcept
  896. { return { _Base::begin(), this }; }
  897. iterator
  898. end() noexcept
  899. { return { _Base::end(), this }; }
  900. const_iterator
  901. end() const noexcept
  902. { return { _Base::end(), this }; }
  903. const_iterator
  904. cbegin() const noexcept
  905. { return { _Base::cbegin(), this }; }
  906. const_iterator
  907. cend() const noexcept
  908. { return { _Base::cend(), this }; }
  909. // local versions
  910. local_iterator
  911. begin(size_type __b)
  912. {
  913. __glibcxx_check_bucket_index(__b);
  914. return { _Base::begin(__b), this };
  915. }
  916. local_iterator
  917. end(size_type __b)
  918. {
  919. __glibcxx_check_bucket_index(__b);
  920. return { _Base::end(__b), this };
  921. }
  922. const_local_iterator
  923. begin(size_type __b) const
  924. {
  925. __glibcxx_check_bucket_index(__b);
  926. return { _Base::begin(__b), this };
  927. }
  928. const_local_iterator
  929. end(size_type __b) const
  930. {
  931. __glibcxx_check_bucket_index(__b);
  932. return { _Base::end(__b), this };
  933. }
  934. const_local_iterator
  935. cbegin(size_type __b) const
  936. {
  937. __glibcxx_check_bucket_index(__b);
  938. return { _Base::cbegin(__b), this };
  939. }
  940. const_local_iterator
  941. cend(size_type __b) const
  942. {
  943. __glibcxx_check_bucket_index(__b);
  944. return { _Base::cend(__b), this };
  945. }
  946. using _Base::bucket_count;
  947. using _Base::max_bucket_count;
  948. using _Base::bucket;
  949. size_type
  950. bucket_size(size_type __b) const
  951. {
  952. __glibcxx_check_bucket_index(__b);
  953. return _Base::bucket_size(__b);
  954. }
  955. float
  956. max_load_factor() const noexcept
  957. { return _Base::max_load_factor(); }
  958. void
  959. max_load_factor(float __f)
  960. {
  961. __glibcxx_check_max_load_factor(__f);
  962. _Base::max_load_factor(__f);
  963. }
  964. template<typename... _Args>
  965. iterator
  966. emplace(_Args&&... __args)
  967. {
  968. size_type __bucket_count = this->bucket_count();
  969. auto __it = _Base::emplace(std::forward<_Args>(__args)...);
  970. _M_check_rehashed(__bucket_count);
  971. return { __it, this };
  972. }
  973. template<typename... _Args>
  974. iterator
  975. emplace_hint(const_iterator __hint, _Args&&... __args)
  976. {
  977. __glibcxx_check_insert(__hint);
  978. size_type __bucket_count = this->bucket_count();
  979. auto __it = _Base::emplace_hint(__hint.base(),
  980. std::forward<_Args>(__args)...);
  981. _M_check_rehashed(__bucket_count);
  982. return { __it, this };
  983. }
  984. iterator
  985. insert(const value_type& __obj)
  986. {
  987. size_type __bucket_count = this->bucket_count();
  988. auto __it = _Base::insert(__obj);
  989. _M_check_rehashed(__bucket_count);
  990. return { __it, this };
  991. }
  992. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  993. // 2354. Unnecessary copying when inserting into maps with braced-init
  994. iterator
  995. insert(value_type&& __x)
  996. {
  997. size_type __bucket_count = this->bucket_count();
  998. auto __it = _Base::insert(std::move(__x));
  999. _M_check_rehashed(__bucket_count);
  1000. return { __it, this };
  1001. }
  1002. iterator
  1003. insert(const_iterator __hint, const value_type& __obj)
  1004. {
  1005. __glibcxx_check_insert(__hint);
  1006. size_type __bucket_count = this->bucket_count();
  1007. auto __it = _Base::insert(__hint.base(), __obj);
  1008. _M_check_rehashed(__bucket_count);
  1009. return { __it, this };
  1010. }
  1011. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1012. // 2354. Unnecessary copying when inserting into maps with braced-init
  1013. iterator
  1014. insert(const_iterator __hint, value_type&& __x)
  1015. {
  1016. __glibcxx_check_insert(__hint);
  1017. size_type __bucket_count = this->bucket_count();
  1018. auto __it = _Base::insert(__hint.base(), std::move(__x));
  1019. _M_check_rehashed(__bucket_count);
  1020. return { __it, this };
  1021. }
  1022. template<typename _Pair, typename = typename
  1023. std::enable_if<std::is_constructible<value_type,
  1024. _Pair&&>::value>::type>
  1025. iterator
  1026. insert(_Pair&& __obj)
  1027. {
  1028. size_type __bucket_count = this->bucket_count();
  1029. auto __it = _Base::insert(std::forward<_Pair>(__obj));
  1030. _M_check_rehashed(__bucket_count);
  1031. return { __it, this };
  1032. }
  1033. template<typename _Pair, typename = typename
  1034. std::enable_if<std::is_constructible<value_type,
  1035. _Pair&&>::value>::type>
  1036. iterator
  1037. insert(const_iterator __hint, _Pair&& __obj)
  1038. {
  1039. __glibcxx_check_insert(__hint);
  1040. size_type __bucket_count = this->bucket_count();
  1041. auto __it = _Base::insert(__hint.base(), std::forward<_Pair>(__obj));
  1042. _M_check_rehashed(__bucket_count);
  1043. return { __it, this };
  1044. }
  1045. void
  1046. insert(std::initializer_list<value_type> __l)
  1047. { _Base::insert(__l); }
  1048. template<typename _InputIterator>
  1049. void
  1050. insert(_InputIterator __first, _InputIterator __last)
  1051. {
  1052. typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
  1053. __glibcxx_check_valid_range2(__first, __last, __dist);
  1054. size_type __bucket_count = this->bucket_count();
  1055. if (__dist.second >= __gnu_debug::__dp_sign)
  1056. _Base::insert(__gnu_debug::__unsafe(__first),
  1057. __gnu_debug::__unsafe(__last));
  1058. else
  1059. _Base::insert(__first, __last);
  1060. _M_check_rehashed(__bucket_count);
  1061. }
  1062. #if __cplusplus > 201402L
  1063. using node_type = typename _Base::node_type;
  1064. node_type
  1065. extract(const_iterator __position)
  1066. {
  1067. __glibcxx_check_erase(__position);
  1068. return _M_extract(__position.base());
  1069. }
  1070. node_type
  1071. extract(const key_type& __key)
  1072. {
  1073. const auto __position = _Base::find(__key);
  1074. if (__position != _Base::end())
  1075. return _M_extract(__position);
  1076. return {};
  1077. }
  1078. iterator
  1079. insert(node_type&& __nh)
  1080. { return { _Base::insert(std::move(__nh)), this }; }
  1081. iterator
  1082. insert(const_iterator __hint, node_type&& __nh)
  1083. {
  1084. __glibcxx_check_insert(__hint);
  1085. return { _Base::insert(__hint.base(), std::move(__nh)), this };
  1086. }
  1087. template<typename _H2, typename _P2>
  1088. void
  1089. merge(unordered_multimap<_Key, _Tp, _H2, _P2, _Alloc>& __source)
  1090. {
  1091. auto __guard
  1092. = _Safe::_S_umc_guard(std::__detail::_Select1st{}, __source);
  1093. _Base::merge(__source);
  1094. }
  1095. template<typename _H2, typename _P2>
  1096. void
  1097. merge(unordered_multimap<_Key, _Tp, _H2, _P2, _Alloc>&& __source)
  1098. { merge(__source); }
  1099. template<typename _H2, typename _P2>
  1100. void
  1101. merge(unordered_map<_Key, _Tp, _H2, _P2, _Alloc>& __source)
  1102. {
  1103. auto __guard
  1104. = _Safe::_S_uc_guard(std::__detail::_Select1st{}, __source);
  1105. _Base::merge(__source);
  1106. }
  1107. template<typename _H2, typename _P2>
  1108. void
  1109. merge(unordered_map<_Key, _Tp, _H2, _P2, _Alloc>&& __source)
  1110. { merge(__source); }
  1111. #endif // C++17
  1112. using _Base::hash_function;
  1113. using _Base::key_eq;
  1114. iterator
  1115. find(const key_type& __key)
  1116. { return { _Base::find(__key), this }; }
  1117. #if __cplusplus > 201703L
  1118. template<typename _Kt,
  1119. typename = std::__has_is_transparent_t<_Hash, _Kt>,
  1120. typename = std::__has_is_transparent_t<_Pred, _Kt>>
  1121. iterator
  1122. find(const _Kt& __k)
  1123. { return { _Base::find(__k), this }; }
  1124. #endif
  1125. const_iterator
  1126. find(const key_type& __key) const
  1127. { return { _Base::find(__key), this }; }
  1128. #if __cplusplus > 201703L
  1129. template<typename _Kt,
  1130. typename = std::__has_is_transparent_t<_Hash, _Kt>,
  1131. typename = std::__has_is_transparent_t<_Pred, _Kt>>
  1132. const_iterator
  1133. find(const _Kt& __k) const
  1134. { return { _Base::find(__k), this }; }
  1135. #endif
  1136. using _Base::count;
  1137. #if __cplusplus > 201703L
  1138. using _Base::contains;
  1139. #endif
  1140. std::pair<iterator, iterator>
  1141. equal_range(const key_type& __key)
  1142. {
  1143. auto __res = _Base::equal_range(__key);
  1144. return { { __res.first, this }, { __res.second, this } };
  1145. }
  1146. #if __cplusplus > 201703L
  1147. template<typename _Kt,
  1148. typename = std::__has_is_transparent_t<_Hash, _Kt>,
  1149. typename = std::__has_is_transparent_t<_Pred, _Kt>>
  1150. std::pair<iterator, iterator>
  1151. equal_range(const _Kt& __k)
  1152. {
  1153. auto __res = _Base::equal_range(__k);
  1154. return { { __res.first, this }, { __res.second, this } };
  1155. }
  1156. #endif
  1157. std::pair<const_iterator, const_iterator>
  1158. equal_range(const key_type& __key) const
  1159. {
  1160. auto __res = _Base::equal_range(__key);
  1161. return { { __res.first, this }, { __res.second, this } };
  1162. }
  1163. #if __cplusplus > 201703L
  1164. template<typename _Kt,
  1165. typename = std::__has_is_transparent_t<_Hash, _Kt>,
  1166. typename = std::__has_is_transparent_t<_Pred, _Kt>>
  1167. std::pair<const_iterator, const_iterator>
  1168. equal_range(const _Kt& __k) const
  1169. {
  1170. auto __res = _Base::equal_range(__k);
  1171. return { { __res.first, this }, { __res.second, this } };
  1172. }
  1173. #endif
  1174. size_type
  1175. erase(const key_type& __key)
  1176. {
  1177. size_type __ret(0);
  1178. size_type __bucket_count = this->bucket_count();
  1179. auto __pair = _Base::equal_range(__key);
  1180. for (auto __victim = __pair.first; __victim != __pair.second;)
  1181. {
  1182. _M_invalidate(__victim);
  1183. __victim = _Base::erase(__victim);
  1184. ++__ret;
  1185. }
  1186. _M_check_rehashed(__bucket_count);
  1187. return __ret;
  1188. }
  1189. iterator
  1190. erase(const_iterator __it)
  1191. {
  1192. __glibcxx_check_erase(__it);
  1193. return { _M_erase(__it.base()), this };
  1194. }
  1195. _Base_iterator
  1196. erase(_Base_const_iterator __it)
  1197. {
  1198. __glibcxx_check_erase2(__it);
  1199. return _M_erase(__it);
  1200. }
  1201. iterator
  1202. erase(iterator __it)
  1203. {
  1204. __glibcxx_check_erase(__it);
  1205. return { _M_erase(__it.base()), this };
  1206. }
  1207. iterator
  1208. erase(const_iterator __first, const_iterator __last)
  1209. {
  1210. __glibcxx_check_erase_range(__first, __last);
  1211. for (auto __tmp = __first.base(); __tmp != __last.base(); ++__tmp)
  1212. {
  1213. _GLIBCXX_DEBUG_VERIFY(__tmp != _Base::cend(),
  1214. _M_message(__gnu_debug::__msg_valid_range)
  1215. ._M_iterator(__first, "first")
  1216. ._M_iterator(__last, "last"));
  1217. _M_invalidate(__tmp);
  1218. }
  1219. size_type __bucket_count = this->bucket_count();
  1220. auto __next = _Base::erase(__first.base(), __last.base());
  1221. _M_check_rehashed(__bucket_count);
  1222. return { __next, this };
  1223. }
  1224. using _Base::rehash;
  1225. using _Base::reserve;
  1226. _Base&
  1227. _M_base() noexcept { return *this; }
  1228. const _Base&
  1229. _M_base() const noexcept { return *this; }
  1230. private:
  1231. void
  1232. _M_check_rehashed(size_type __prev_count)
  1233. {
  1234. if (__prev_count != this->bucket_count())
  1235. this->_M_invalidate_all();
  1236. }
  1237. void
  1238. _M_invalidate(_Base_const_iterator __victim)
  1239. {
  1240. this->_M_invalidate_if(
  1241. [__victim](_Base_const_iterator __it) { return __it == __victim; });
  1242. this->_M_invalidate_local_if(
  1243. [__victim](_Base_const_local_iterator __it)
  1244. { return __it == __victim; });
  1245. }
  1246. _Base_iterator
  1247. _M_erase(_Base_const_iterator __victim)
  1248. {
  1249. _M_invalidate(__victim);
  1250. size_type __bucket_count = this->bucket_count();
  1251. _Base_iterator __next = _Base::erase(__victim);
  1252. _M_check_rehashed(__bucket_count);
  1253. return __next;
  1254. }
  1255. #if __cplusplus > 201402L
  1256. node_type
  1257. _M_extract(_Base_const_iterator __victim)
  1258. {
  1259. _M_invalidate(__victim);
  1260. return _Base::extract(__victim);
  1261. }
  1262. #endif
  1263. };
  1264. #if __cpp_deduction_guides >= 201606
  1265. template<typename _InputIterator,
  1266. typename _Hash = hash<__iter_key_t<_InputIterator>>,
  1267. typename _Pred = equal_to<__iter_key_t<_InputIterator>>,
  1268. typename _Allocator = allocator<__iter_to_alloc_t<_InputIterator>>,
  1269. typename = _RequireInputIter<_InputIterator>,
  1270. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  1271. typename = _RequireNotAllocator<_Pred>,
  1272. typename = _RequireAllocator<_Allocator>>
  1273. unordered_multimap(_InputIterator, _InputIterator,
  1274. unordered_multimap<int, int>::size_type = {},
  1275. _Hash = _Hash(), _Pred = _Pred(),
  1276. _Allocator = _Allocator())
  1277. -> unordered_multimap<__iter_key_t<_InputIterator>,
  1278. __iter_val_t<_InputIterator>, _Hash, _Pred,
  1279. _Allocator>;
  1280. template<typename _Key, typename _Tp, typename _Hash = hash<_Key>,
  1281. typename _Pred = equal_to<_Key>,
  1282. typename _Allocator = allocator<pair<const _Key, _Tp>>,
  1283. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  1284. typename = _RequireNotAllocator<_Pred>,
  1285. typename = _RequireAllocator<_Allocator>>
  1286. unordered_multimap(initializer_list<pair<_Key, _Tp>>,
  1287. unordered_multimap<int, int>::size_type = {},
  1288. _Hash = _Hash(), _Pred = _Pred(),
  1289. _Allocator = _Allocator())
  1290. -> unordered_multimap<_Key, _Tp, _Hash, _Pred, _Allocator>;
  1291. template<typename _InputIterator, typename _Allocator,
  1292. typename = _RequireInputIter<_InputIterator>,
  1293. typename = _RequireAllocator<_Allocator>>
  1294. unordered_multimap(_InputIterator, _InputIterator,
  1295. unordered_multimap<int, int>::size_type, _Allocator)
  1296. -> unordered_multimap<__iter_key_t<_InputIterator>,
  1297. __iter_val_t<_InputIterator>,
  1298. hash<__iter_key_t<_InputIterator>>,
  1299. equal_to<__iter_key_t<_InputIterator>>, _Allocator>;
  1300. template<typename _InputIterator, typename _Allocator,
  1301. typename = _RequireInputIter<_InputIterator>,
  1302. typename = _RequireAllocator<_Allocator>>
  1303. unordered_multimap(_InputIterator, _InputIterator, _Allocator)
  1304. -> unordered_multimap<__iter_key_t<_InputIterator>,
  1305. __iter_val_t<_InputIterator>,
  1306. hash<__iter_key_t<_InputIterator>>,
  1307. equal_to<__iter_key_t<_InputIterator>>, _Allocator>;
  1308. template<typename _InputIterator, typename _Hash, typename _Allocator,
  1309. typename = _RequireInputIter<_InputIterator>,
  1310. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  1311. typename = _RequireAllocator<_Allocator>>
  1312. unordered_multimap(_InputIterator, _InputIterator,
  1313. unordered_multimap<int, int>::size_type, _Hash,
  1314. _Allocator)
  1315. -> unordered_multimap<__iter_key_t<_InputIterator>,
  1316. __iter_val_t<_InputIterator>, _Hash,
  1317. equal_to<__iter_key_t<_InputIterator>>, _Allocator>;
  1318. template<typename _Key, typename _Tp, typename _Allocator,
  1319. typename = _RequireAllocator<_Allocator>>
  1320. unordered_multimap(initializer_list<pair<_Key, _Tp>>,
  1321. unordered_multimap<int, int>::size_type,
  1322. _Allocator)
  1323. -> unordered_multimap<_Key, _Tp, hash<_Key>, equal_to<_Key>, _Allocator>;
  1324. template<typename _Key, typename _Tp, typename _Allocator,
  1325. typename = _RequireAllocator<_Allocator>>
  1326. unordered_multimap(initializer_list<pair<_Key, _Tp>>, _Allocator)
  1327. -> unordered_multimap<_Key, _Tp, hash<_Key>, equal_to<_Key>, _Allocator>;
  1328. template<typename _Key, typename _Tp, typename _Hash, typename _Allocator,
  1329. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  1330. typename = _RequireAllocator<_Allocator>>
  1331. unordered_multimap(initializer_list<pair<_Key, _Tp>>,
  1332. unordered_multimap<int, int>::size_type,
  1333. _Hash, _Allocator)
  1334. -> unordered_multimap<_Key, _Tp, _Hash, equal_to<_Key>, _Allocator>;
  1335. #endif
  1336. template<typename _Key, typename _Tp, typename _Hash,
  1337. typename _Pred, typename _Alloc>
  1338. inline void
  1339. swap(unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  1340. unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  1341. noexcept(noexcept(__x.swap(__y)))
  1342. { __x.swap(__y); }
  1343. template<typename _Key, typename _Tp, typename _Hash,
  1344. typename _Pred, typename _Alloc>
  1345. inline bool
  1346. operator==(const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  1347. const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  1348. { return __x._M_base() == __y._M_base(); }
  1349. #if __cpp_impl_three_way_comparison < 201907L
  1350. template<typename _Key, typename _Tp, typename _Hash,
  1351. typename _Pred, typename _Alloc>
  1352. inline bool
  1353. operator!=(const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  1354. const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  1355. { return !(__x == __y); }
  1356. #endif
  1357. } // namespace __debug
  1358. } // namespace std
  1359. #endif // C++11
  1360. #endif