unordered_map 43 KB

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