unordered_map.h 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215
  1. // unordered_map implementation -*- C++ -*-
  2. // Copyright (C) 2010-2022 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 bits/unordered_map.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{unordered_map}
  23. */
  24. #ifndef _UNORDERED_MAP_H
  25. #define _UNORDERED_MAP_H
  26. namespace std _GLIBCXX_VISIBILITY(default)
  27. {
  28. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  29. _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
  30. /// Base types for unordered_map.
  31. template<bool _Cache>
  32. using __umap_traits = __detail::_Hashtable_traits<_Cache, false, true>;
  33. template<typename _Key,
  34. typename _Tp,
  35. typename _Hash = hash<_Key>,
  36. typename _Pred = std::equal_to<_Key>,
  37. typename _Alloc = std::allocator<std::pair<const _Key, _Tp> >,
  38. typename _Tr = __umap_traits<__cache_default<_Key, _Hash>::value>>
  39. using __umap_hashtable = _Hashtable<_Key, std::pair<const _Key, _Tp>,
  40. _Alloc, __detail::_Select1st,
  41. _Pred, _Hash,
  42. __detail::_Mod_range_hashing,
  43. __detail::_Default_ranged_hash,
  44. __detail::_Prime_rehash_policy, _Tr>;
  45. /// Base types for unordered_multimap.
  46. template<bool _Cache>
  47. using __ummap_traits = __detail::_Hashtable_traits<_Cache, false, false>;
  48. template<typename _Key,
  49. typename _Tp,
  50. typename _Hash = hash<_Key>,
  51. typename _Pred = std::equal_to<_Key>,
  52. typename _Alloc = std::allocator<std::pair<const _Key, _Tp> >,
  53. typename _Tr = __ummap_traits<__cache_default<_Key, _Hash>::value>>
  54. using __ummap_hashtable = _Hashtable<_Key, std::pair<const _Key, _Tp>,
  55. _Alloc, __detail::_Select1st,
  56. _Pred, _Hash,
  57. __detail::_Mod_range_hashing,
  58. __detail::_Default_ranged_hash,
  59. __detail::_Prime_rehash_policy, _Tr>;
  60. template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  61. class unordered_multimap;
  62. /**
  63. * @brief A standard container composed of unique keys (containing
  64. * at most one of each key value) that associates values of another type
  65. * with the keys.
  66. *
  67. * @ingroup unordered_associative_containers
  68. *
  69. * @tparam _Key Type of key objects.
  70. * @tparam _Tp Type of mapped objects.
  71. * @tparam _Hash Hashing function object type, defaults to hash<_Value>.
  72. * @tparam _Pred Predicate function object type, defaults
  73. * to equal_to<_Value>.
  74. * @tparam _Alloc Allocator type, defaults to
  75. * std::allocator<std::pair<const _Key, _Tp>>.
  76. *
  77. * Meets the requirements of a <a href="tables.html#65">container</a>, and
  78. * <a href="tables.html#xx">unordered associative container</a>
  79. *
  80. * The resulting value type of the container is std::pair<const _Key, _Tp>.
  81. *
  82. * Base is _Hashtable, dispatched at compile time via template
  83. * alias __umap_hashtable.
  84. */
  85. template<typename _Key, typename _Tp,
  86. typename _Hash = hash<_Key>,
  87. typename _Pred = equal_to<_Key>,
  88. typename _Alloc = allocator<std::pair<const _Key, _Tp>>>
  89. class unordered_map
  90. {
  91. typedef __umap_hashtable<_Key, _Tp, _Hash, _Pred, _Alloc> _Hashtable;
  92. _Hashtable _M_h;
  93. public:
  94. // typedefs:
  95. ///@{
  96. /// Public typedefs.
  97. typedef typename _Hashtable::key_type key_type;
  98. typedef typename _Hashtable::value_type value_type;
  99. typedef typename _Hashtable::mapped_type mapped_type;
  100. typedef typename _Hashtable::hasher hasher;
  101. typedef typename _Hashtable::key_equal key_equal;
  102. typedef typename _Hashtable::allocator_type allocator_type;
  103. ///@}
  104. ///@{
  105. /// Iterator-related typedefs.
  106. typedef typename _Hashtable::pointer pointer;
  107. typedef typename _Hashtable::const_pointer const_pointer;
  108. typedef typename _Hashtable::reference reference;
  109. typedef typename _Hashtable::const_reference const_reference;
  110. typedef typename _Hashtable::iterator iterator;
  111. typedef typename _Hashtable::const_iterator const_iterator;
  112. typedef typename _Hashtable::local_iterator local_iterator;
  113. typedef typename _Hashtable::const_local_iterator const_local_iterator;
  114. typedef typename _Hashtable::size_type size_type;
  115. typedef typename _Hashtable::difference_type difference_type;
  116. ///@}
  117. #if __cplusplus > 201402L
  118. using node_type = typename _Hashtable::node_type;
  119. using insert_return_type = typename _Hashtable::insert_return_type;
  120. #endif
  121. //construct/destroy/copy
  122. /// Default constructor.
  123. unordered_map() = default;
  124. /**
  125. * @brief Default constructor creates no elements.
  126. * @param __n Minimal initial number of buckets.
  127. * @param __hf A hash functor.
  128. * @param __eql A key equality functor.
  129. * @param __a An allocator object.
  130. */
  131. explicit
  132. unordered_map(size_type __n,
  133. const hasher& __hf = hasher(),
  134. const key_equal& __eql = key_equal(),
  135. const allocator_type& __a = allocator_type())
  136. : _M_h(__n, __hf, __eql, __a)
  137. { }
  138. /**
  139. * @brief Builds an %unordered_map from a range.
  140. * @param __first An input iterator.
  141. * @param __last An input iterator.
  142. * @param __n Minimal initial number of buckets.
  143. * @param __hf A hash functor.
  144. * @param __eql A key equality functor.
  145. * @param __a An allocator object.
  146. *
  147. * Create an %unordered_map consisting of copies of the elements from
  148. * [__first,__last). This is linear in N (where N is
  149. * distance(__first,__last)).
  150. */
  151. template<typename _InputIterator>
  152. unordered_map(_InputIterator __first, _InputIterator __last,
  153. size_type __n = 0,
  154. const hasher& __hf = hasher(),
  155. const key_equal& __eql = key_equal(),
  156. const allocator_type& __a = allocator_type())
  157. : _M_h(__first, __last, __n, __hf, __eql, __a)
  158. { }
  159. /// Copy constructor.
  160. unordered_map(const unordered_map&) = default;
  161. /// Move constructor.
  162. unordered_map(unordered_map&&) = default;
  163. /**
  164. * @brief Creates an %unordered_map with no elements.
  165. * @param __a An allocator object.
  166. */
  167. explicit
  168. unordered_map(const allocator_type& __a)
  169. : _M_h(__a)
  170. { }
  171. /*
  172. * @brief Copy constructor with allocator argument.
  173. * @param __uset Input %unordered_map to copy.
  174. * @param __a An allocator object.
  175. */
  176. unordered_map(const unordered_map& __umap,
  177. const allocator_type& __a)
  178. : _M_h(__umap._M_h, __a)
  179. { }
  180. /*
  181. * @brief Move constructor with allocator argument.
  182. * @param __uset Input %unordered_map to move.
  183. * @param __a An allocator object.
  184. */
  185. unordered_map(unordered_map&& __umap,
  186. const allocator_type& __a)
  187. noexcept( noexcept(_Hashtable(std::move(__umap._M_h), __a)) )
  188. : _M_h(std::move(__umap._M_h), __a)
  189. { }
  190. /**
  191. * @brief Builds an %unordered_map from an initializer_list.
  192. * @param __l An initializer_list.
  193. * @param __n Minimal initial number of buckets.
  194. * @param __hf A hash functor.
  195. * @param __eql A key equality functor.
  196. * @param __a An allocator object.
  197. *
  198. * Create an %unordered_map consisting of copies of the elements in the
  199. * list. This is linear in N (where N is @a __l.size()).
  200. */
  201. unordered_map(initializer_list<value_type> __l,
  202. size_type __n = 0,
  203. const hasher& __hf = hasher(),
  204. const key_equal& __eql = key_equal(),
  205. const allocator_type& __a = allocator_type())
  206. : _M_h(__l, __n, __hf, __eql, __a)
  207. { }
  208. unordered_map(size_type __n, const allocator_type& __a)
  209. : unordered_map(__n, hasher(), key_equal(), __a)
  210. { }
  211. unordered_map(size_type __n, const hasher& __hf,
  212. const allocator_type& __a)
  213. : unordered_map(__n, __hf, key_equal(), __a)
  214. { }
  215. template<typename _InputIterator>
  216. unordered_map(_InputIterator __first, _InputIterator __last,
  217. size_type __n,
  218. const allocator_type& __a)
  219. : unordered_map(__first, __last, __n, hasher(), key_equal(), __a)
  220. { }
  221. template<typename _InputIterator>
  222. unordered_map(_InputIterator __first, _InputIterator __last,
  223. size_type __n, const hasher& __hf,
  224. const allocator_type& __a)
  225. : unordered_map(__first, __last, __n, __hf, key_equal(), __a)
  226. { }
  227. unordered_map(initializer_list<value_type> __l,
  228. size_type __n,
  229. const allocator_type& __a)
  230. : unordered_map(__l, __n, hasher(), key_equal(), __a)
  231. { }
  232. unordered_map(initializer_list<value_type> __l,
  233. size_type __n, const hasher& __hf,
  234. const allocator_type& __a)
  235. : unordered_map(__l, __n, __hf, key_equal(), __a)
  236. { }
  237. /// Copy assignment operator.
  238. unordered_map&
  239. operator=(const unordered_map&) = default;
  240. /// Move assignment operator.
  241. unordered_map&
  242. operator=(unordered_map&&) = default;
  243. /**
  244. * @brief %Unordered_map list assignment operator.
  245. * @param __l An initializer_list.
  246. *
  247. * This function fills an %unordered_map with copies of the elements in
  248. * the initializer list @a __l.
  249. *
  250. * Note that the assignment completely changes the %unordered_map and
  251. * that the resulting %unordered_map's size is the same as the number
  252. * of elements assigned.
  253. */
  254. unordered_map&
  255. operator=(initializer_list<value_type> __l)
  256. {
  257. _M_h = __l;
  258. return *this;
  259. }
  260. /// Returns the allocator object used by the %unordered_map.
  261. allocator_type
  262. get_allocator() const noexcept
  263. { return _M_h.get_allocator(); }
  264. // size and capacity:
  265. /// Returns true if the %unordered_map is empty.
  266. _GLIBCXX_NODISCARD bool
  267. empty() const noexcept
  268. { return _M_h.empty(); }
  269. /// Returns the size of the %unordered_map.
  270. size_type
  271. size() const noexcept
  272. { return _M_h.size(); }
  273. /// Returns the maximum size of the %unordered_map.
  274. size_type
  275. max_size() const noexcept
  276. { return _M_h.max_size(); }
  277. // iterators.
  278. /**
  279. * Returns a read/write iterator that points to the first element in the
  280. * %unordered_map.
  281. */
  282. iterator
  283. begin() noexcept
  284. { return _M_h.begin(); }
  285. ///@{
  286. /**
  287. * Returns a read-only (constant) iterator that points to the first
  288. * element in the %unordered_map.
  289. */
  290. const_iterator
  291. begin() const noexcept
  292. { return _M_h.begin(); }
  293. const_iterator
  294. cbegin() const noexcept
  295. { return _M_h.begin(); }
  296. ///@}
  297. /**
  298. * Returns a read/write iterator that points one past the last element in
  299. * the %unordered_map.
  300. */
  301. iterator
  302. end() noexcept
  303. { return _M_h.end(); }
  304. ///@{
  305. /**
  306. * Returns a read-only (constant) iterator that points one past the last
  307. * element in the %unordered_map.
  308. */
  309. const_iterator
  310. end() const noexcept
  311. { return _M_h.end(); }
  312. const_iterator
  313. cend() const noexcept
  314. { return _M_h.end(); }
  315. ///@}
  316. // modifiers.
  317. /**
  318. * @brief Attempts to build and insert a std::pair into the
  319. * %unordered_map.
  320. *
  321. * @param __args Arguments used to generate a new pair instance (see
  322. * std::piecewise_contruct for passing arguments to each
  323. * part of the pair constructor).
  324. *
  325. * @return A pair, of which the first element is an iterator that points
  326. * to the possibly inserted pair, and the second is a bool that
  327. * is true if the pair was actually inserted.
  328. *
  329. * This function attempts to build and insert a (key, value) %pair into
  330. * the %unordered_map.
  331. * An %unordered_map relies on unique keys and thus a %pair is only
  332. * inserted if its first element (the key) is not already present in the
  333. * %unordered_map.
  334. *
  335. * Insertion requires amortized constant time.
  336. */
  337. template<typename... _Args>
  338. std::pair<iterator, bool>
  339. emplace(_Args&&... __args)
  340. { return _M_h.emplace(std::forward<_Args>(__args)...); }
  341. /**
  342. * @brief Attempts to build and insert a std::pair into the
  343. * %unordered_map.
  344. *
  345. * @param __pos An iterator that serves as a hint as to where the pair
  346. * should be inserted.
  347. * @param __args Arguments used to generate a new pair instance (see
  348. * std::piecewise_contruct for passing arguments to each
  349. * part of the pair constructor).
  350. * @return An iterator that points to the element with key of the
  351. * std::pair built from @a __args (may or may not be that
  352. * std::pair).
  353. *
  354. * This function is not concerned about whether the insertion took place,
  355. * and thus does not return a boolean like the single-argument emplace()
  356. * does.
  357. * Note that the first parameter is only a hint and can potentially
  358. * improve the performance of the insertion process. A bad hint would
  359. * cause no gains in efficiency.
  360. *
  361. * See
  362. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
  363. * for more on @a hinting.
  364. *
  365. * Insertion requires amortized constant time.
  366. */
  367. template<typename... _Args>
  368. iterator
  369. emplace_hint(const_iterator __pos, _Args&&... __args)
  370. { return _M_h.emplace_hint(__pos, std::forward<_Args>(__args)...); }
  371. #if __cplusplus > 201402L
  372. /// Extract a node.
  373. node_type
  374. extract(const_iterator __pos)
  375. {
  376. __glibcxx_assert(__pos != end());
  377. return _M_h.extract(__pos);
  378. }
  379. /// Extract a node.
  380. node_type
  381. extract(const key_type& __key)
  382. { return _M_h.extract(__key); }
  383. /// Re-insert an extracted node.
  384. insert_return_type
  385. insert(node_type&& __nh)
  386. { return _M_h._M_reinsert_node(std::move(__nh)); }
  387. /// Re-insert an extracted node.
  388. iterator
  389. insert(const_iterator, node_type&& __nh)
  390. { return _M_h._M_reinsert_node(std::move(__nh)).position; }
  391. #define __cpp_lib_unordered_map_try_emplace 201411L
  392. /**
  393. * @brief Attempts to build and insert a std::pair into the
  394. * %unordered_map.
  395. *
  396. * @param __k Key to use for finding a possibly existing pair in
  397. * the unordered_map.
  398. * @param __args Arguments used to generate the .second for a
  399. * new pair instance.
  400. *
  401. * @return A pair, of which the first element is an iterator that points
  402. * to the possibly inserted pair, and the second is a bool that
  403. * is true if the pair was actually inserted.
  404. *
  405. * This function attempts to build and insert a (key, value) %pair into
  406. * the %unordered_map.
  407. * An %unordered_map relies on unique keys and thus a %pair is only
  408. * inserted if its first element (the key) is not already present in the
  409. * %unordered_map.
  410. * If a %pair is not inserted, this function has no effect.
  411. *
  412. * Insertion requires amortized constant time.
  413. */
  414. template <typename... _Args>
  415. pair<iterator, bool>
  416. try_emplace(const key_type& __k, _Args&&... __args)
  417. {
  418. return _M_h.try_emplace(cend(), __k, std::forward<_Args>(__args)...);
  419. }
  420. // move-capable overload
  421. template <typename... _Args>
  422. pair<iterator, bool>
  423. try_emplace(key_type&& __k, _Args&&... __args)
  424. {
  425. return _M_h.try_emplace(cend(), std::move(__k),
  426. std::forward<_Args>(__args)...);
  427. }
  428. /**
  429. * @brief Attempts to build and insert a std::pair into the
  430. * %unordered_map.
  431. *
  432. * @param __hint An iterator that serves as a hint as to where the pair
  433. * should be inserted.
  434. * @param __k Key to use for finding a possibly existing pair in
  435. * the unordered_map.
  436. * @param __args Arguments used to generate the .second for a
  437. * new pair instance.
  438. * @return An iterator that points to the element with key of the
  439. * std::pair built from @a __args (may or may not be that
  440. * std::pair).
  441. *
  442. * This function is not concerned about whether the insertion took place,
  443. * and thus does not return a boolean like the single-argument emplace()
  444. * does. However, if insertion did not take place,
  445. * this function has no effect.
  446. * Note that the first parameter is only a hint and can potentially
  447. * improve the performance of the insertion process. A bad hint would
  448. * cause no gains in efficiency.
  449. *
  450. * See
  451. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
  452. * for more on @a hinting.
  453. *
  454. * Insertion requires amortized constant time.
  455. */
  456. template <typename... _Args>
  457. iterator
  458. try_emplace(const_iterator __hint, const key_type& __k,
  459. _Args&&... __args)
  460. {
  461. return _M_h.try_emplace(__hint, __k,
  462. std::forward<_Args>(__args)...).first;
  463. }
  464. // move-capable overload
  465. template <typename... _Args>
  466. iterator
  467. try_emplace(const_iterator __hint, key_type&& __k, _Args&&... __args)
  468. {
  469. return _M_h.try_emplace(__hint, std::move(__k),
  470. std::forward<_Args>(__args)...).first;
  471. }
  472. #endif // C++17
  473. ///@{
  474. /**
  475. * @brief Attempts to insert a std::pair into the %unordered_map.
  476. * @param __x Pair to be inserted (see std::make_pair for easy
  477. * creation of pairs).
  478. *
  479. * @return A pair, of which the first element is an iterator that
  480. * points to the possibly inserted pair, and the second is
  481. * a bool that is true if the pair was actually inserted.
  482. *
  483. * This function attempts to insert a (key, value) %pair into the
  484. * %unordered_map. An %unordered_map relies on unique keys and thus a
  485. * %pair is only inserted if its first element (the key) is not already
  486. * present in the %unordered_map.
  487. *
  488. * Insertion requires amortized constant time.
  489. */
  490. std::pair<iterator, bool>
  491. insert(const value_type& __x)
  492. { return _M_h.insert(__x); }
  493. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  494. // 2354. Unnecessary copying when inserting into maps with braced-init
  495. std::pair<iterator, bool>
  496. insert(value_type&& __x)
  497. { return _M_h.insert(std::move(__x)); }
  498. template<typename _Pair>
  499. __enable_if_t<is_constructible<value_type, _Pair&&>::value,
  500. pair<iterator, bool>>
  501. insert(_Pair&& __x)
  502. { return _M_h.emplace(std::forward<_Pair>(__x)); }
  503. ///@}
  504. ///@{
  505. /**
  506. * @brief Attempts to insert a std::pair into the %unordered_map.
  507. * @param __hint An iterator that serves as a hint as to where the
  508. * pair should be inserted.
  509. * @param __x Pair to be inserted (see std::make_pair for easy creation
  510. * of pairs).
  511. * @return An iterator that points to the element with key of
  512. * @a __x (may or may not be the %pair passed in).
  513. *
  514. * This function is not concerned about whether the insertion took place,
  515. * and thus does not return a boolean like the single-argument insert()
  516. * does. Note that the first parameter is only a hint and can
  517. * potentially improve the performance of the insertion process. A bad
  518. * hint would cause no gains in efficiency.
  519. *
  520. * See
  521. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
  522. * for more on @a hinting.
  523. *
  524. * Insertion requires amortized constant time.
  525. */
  526. iterator
  527. insert(const_iterator __hint, const value_type& __x)
  528. { return _M_h.insert(__hint, __x); }
  529. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  530. // 2354. Unnecessary copying when inserting into maps with braced-init
  531. iterator
  532. insert(const_iterator __hint, value_type&& __x)
  533. { return _M_h.insert(__hint, std::move(__x)); }
  534. template<typename _Pair>
  535. __enable_if_t<is_constructible<value_type, _Pair&&>::value, iterator>
  536. insert(const_iterator __hint, _Pair&& __x)
  537. { return _M_h.emplace_hint(__hint, std::forward<_Pair>(__x)); }
  538. ///@}
  539. /**
  540. * @brief A template function that attempts to insert a range of
  541. * elements.
  542. * @param __first Iterator pointing to the start of the range to be
  543. * inserted.
  544. * @param __last Iterator pointing to the end of the range.
  545. *
  546. * Complexity similar to that of the range constructor.
  547. */
  548. template<typename _InputIterator>
  549. void
  550. insert(_InputIterator __first, _InputIterator __last)
  551. { _M_h.insert(__first, __last); }
  552. /**
  553. * @brief Attempts to insert a list of elements into the %unordered_map.
  554. * @param __l A std::initializer_list<value_type> of elements
  555. * to be inserted.
  556. *
  557. * Complexity similar to that of the range constructor.
  558. */
  559. void
  560. insert(initializer_list<value_type> __l)
  561. { _M_h.insert(__l); }
  562. #if __cplusplus > 201402L
  563. /**
  564. * @brief Attempts to insert a std::pair into the %unordered_map.
  565. * @param __k Key to use for finding a possibly existing pair in
  566. * the map.
  567. * @param __obj Argument used to generate the .second for a pair
  568. * instance.
  569. *
  570. * @return A pair, of which the first element is an iterator that
  571. * points to the possibly inserted pair, and the second is
  572. * a bool that is true if the pair was actually inserted.
  573. *
  574. * This function attempts to insert a (key, value) %pair into the
  575. * %unordered_map. An %unordered_map relies on unique keys and thus a
  576. * %pair is only inserted if its first element (the key) is not already
  577. * present in the %unordered_map.
  578. * If the %pair was already in the %unordered_map, the .second of
  579. * the %pair is assigned from __obj.
  580. *
  581. * Insertion requires amortized constant time.
  582. */
  583. template <typename _Obj>
  584. pair<iterator, bool>
  585. insert_or_assign(const key_type& __k, _Obj&& __obj)
  586. {
  587. auto __ret = _M_h.try_emplace(cend(), __k,
  588. std::forward<_Obj>(__obj));
  589. if (!__ret.second)
  590. __ret.first->second = std::forward<_Obj>(__obj);
  591. return __ret;
  592. }
  593. // move-capable overload
  594. template <typename _Obj>
  595. pair<iterator, bool>
  596. insert_or_assign(key_type&& __k, _Obj&& __obj)
  597. {
  598. auto __ret = _M_h.try_emplace(cend(), std::move(__k),
  599. std::forward<_Obj>(__obj));
  600. if (!__ret.second)
  601. __ret.first->second = std::forward<_Obj>(__obj);
  602. return __ret;
  603. }
  604. /**
  605. * @brief Attempts to insert a std::pair into the %unordered_map.
  606. * @param __hint An iterator that serves as a hint as to where the
  607. * pair should be inserted.
  608. * @param __k Key to use for finding a possibly existing pair in
  609. * the unordered_map.
  610. * @param __obj Argument used to generate the .second for a pair
  611. * instance.
  612. * @return An iterator that points to the element with key of
  613. * @a __x (may or may not be the %pair passed in).
  614. *
  615. * This function is not concerned about whether the insertion took place,
  616. * and thus does not return a boolean like the single-argument insert()
  617. * does.
  618. * If the %pair was already in the %unordered map, the .second of
  619. * the %pair is assigned from __obj.
  620. * Note that the first parameter is only a hint and can
  621. * potentially improve the performance of the insertion process. A bad
  622. * hint would cause no gains in efficiency.
  623. *
  624. * See
  625. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
  626. * for more on @a hinting.
  627. *
  628. * Insertion requires amortized constant time.
  629. */
  630. template <typename _Obj>
  631. iterator
  632. insert_or_assign(const_iterator __hint, const key_type& __k,
  633. _Obj&& __obj)
  634. {
  635. auto __ret = _M_h.try_emplace(__hint, __k, std::forward<_Obj>(__obj));
  636. if (!__ret.second)
  637. __ret.first->second = std::forward<_Obj>(__obj);
  638. return __ret.first;
  639. }
  640. // move-capable overload
  641. template <typename _Obj>
  642. iterator
  643. insert_or_assign(const_iterator __hint, key_type&& __k, _Obj&& __obj)
  644. {
  645. auto __ret = _M_h.try_emplace(__hint, std::move(__k),
  646. std::forward<_Obj>(__obj));
  647. if (!__ret.second)
  648. __ret.first->second = std::forward<_Obj>(__obj);
  649. return __ret.first;
  650. }
  651. #endif
  652. ///@{
  653. /**
  654. * @brief Erases an element from an %unordered_map.
  655. * @param __position An iterator pointing to the element to be erased.
  656. * @return An iterator pointing to the element immediately following
  657. * @a __position prior to the element being erased. If no such
  658. * element exists, end() is returned.
  659. *
  660. * This function erases an element, pointed to by the given iterator,
  661. * from an %unordered_map.
  662. * Note that this function only erases the element, and that if the
  663. * element is itself a pointer, the pointed-to memory is not touched in
  664. * any way. Managing the pointer is the user's responsibility.
  665. */
  666. iterator
  667. erase(const_iterator __position)
  668. { return _M_h.erase(__position); }
  669. // LWG 2059.
  670. iterator
  671. erase(iterator __position)
  672. { return _M_h.erase(__position); }
  673. ///@}
  674. /**
  675. * @brief Erases elements according to the provided key.
  676. * @param __x Key of element to be erased.
  677. * @return The number of elements erased.
  678. *
  679. * This function erases all the elements located by the given key from
  680. * an %unordered_map. For an %unordered_map the result of this function
  681. * can only be 0 (not present) or 1 (present).
  682. * Note that this function only erases the element, and that if the
  683. * element is itself a pointer, the pointed-to memory is not touched in
  684. * any way. Managing the pointer is the user's responsibility.
  685. */
  686. size_type
  687. erase(const key_type& __x)
  688. { return _M_h.erase(__x); }
  689. /**
  690. * @brief Erases a [__first,__last) range of elements from an
  691. * %unordered_map.
  692. * @param __first Iterator pointing to the start of the range to be
  693. * erased.
  694. * @param __last Iterator pointing to the end of the range to
  695. * be erased.
  696. * @return The iterator @a __last.
  697. *
  698. * This function erases a sequence of elements from an %unordered_map.
  699. * Note that this function only erases the elements, and that if
  700. * the element is itself a pointer, the pointed-to memory is not touched
  701. * in any way. Managing the pointer is the user's responsibility.
  702. */
  703. iterator
  704. erase(const_iterator __first, const_iterator __last)
  705. { return _M_h.erase(__first, __last); }
  706. /**
  707. * Erases all elements in an %unordered_map.
  708. * Note that this function only erases the elements, and that if the
  709. * elements themselves are pointers, the pointed-to memory is not touched
  710. * in any way. Managing the pointer is the user's responsibility.
  711. */
  712. void
  713. clear() noexcept
  714. { _M_h.clear(); }
  715. /**
  716. * @brief Swaps data with another %unordered_map.
  717. * @param __x An %unordered_map of the same element and allocator
  718. * types.
  719. *
  720. * This exchanges the elements between two %unordered_map in constant
  721. * time.
  722. * Note that the global std::swap() function is specialized such that
  723. * std::swap(m1,m2) will feed to this function.
  724. */
  725. void
  726. swap(unordered_map& __x)
  727. noexcept( noexcept(_M_h.swap(__x._M_h)) )
  728. { _M_h.swap(__x._M_h); }
  729. #if __cplusplus > 201402L
  730. template<typename, typename, typename>
  731. friend class std::_Hash_merge_helper;
  732. template<typename _H2, typename _P2>
  733. void
  734. merge(unordered_map<_Key, _Tp, _H2, _P2, _Alloc>& __source)
  735. {
  736. using _Merge_helper = _Hash_merge_helper<unordered_map, _H2, _P2>;
  737. _M_h._M_merge_unique(_Merge_helper::_S_get_table(__source));
  738. }
  739. template<typename _H2, typename _P2>
  740. void
  741. merge(unordered_map<_Key, _Tp, _H2, _P2, _Alloc>&& __source)
  742. { merge(__source); }
  743. template<typename _H2, typename _P2>
  744. void
  745. merge(unordered_multimap<_Key, _Tp, _H2, _P2, _Alloc>& __source)
  746. {
  747. using _Merge_helper = _Hash_merge_helper<unordered_map, _H2, _P2>;
  748. _M_h._M_merge_unique(_Merge_helper::_S_get_table(__source));
  749. }
  750. template<typename _H2, typename _P2>
  751. void
  752. merge(unordered_multimap<_Key, _Tp, _H2, _P2, _Alloc>&& __source)
  753. { merge(__source); }
  754. #endif // C++17
  755. // observers.
  756. /// Returns the hash functor object with which the %unordered_map was
  757. /// constructed.
  758. hasher
  759. hash_function() const
  760. { return _M_h.hash_function(); }
  761. /// Returns the key comparison object with which the %unordered_map was
  762. /// constructed.
  763. key_equal
  764. key_eq() const
  765. { return _M_h.key_eq(); }
  766. // lookup.
  767. ///@{
  768. /**
  769. * @brief Tries to locate an element in an %unordered_map.
  770. * @param __x Key to be located.
  771. * @return Iterator pointing to sought-after element, or end() if not
  772. * found.
  773. *
  774. * This function takes a key and tries to locate the element with which
  775. * the key matches. If successful the function returns an iterator
  776. * pointing to the sought after element. If unsuccessful it returns the
  777. * past-the-end ( @c end() ) iterator.
  778. */
  779. iterator
  780. find(const key_type& __x)
  781. { return _M_h.find(__x); }
  782. #if __cplusplus > 201703L
  783. template<typename _Kt>
  784. auto
  785. find(const _Kt& __x) -> decltype(_M_h._M_find_tr(__x))
  786. { return _M_h._M_find_tr(__x); }
  787. #endif
  788. const_iterator
  789. find(const key_type& __x) const
  790. { return _M_h.find(__x); }
  791. #if __cplusplus > 201703L
  792. template<typename _Kt>
  793. auto
  794. find(const _Kt& __x) const -> decltype(_M_h._M_find_tr(__x))
  795. { return _M_h._M_find_tr(__x); }
  796. #endif
  797. ///@}
  798. ///@{
  799. /**
  800. * @brief Finds the number of elements.
  801. * @param __x Key to count.
  802. * @return Number of elements with specified key.
  803. *
  804. * This function only makes sense for %unordered_multimap; for
  805. * %unordered_map the result will either be 0 (not present) or 1
  806. * (present).
  807. */
  808. size_type
  809. count(const key_type& __x) const
  810. { return _M_h.count(__x); }
  811. #if __cplusplus > 201703L
  812. template<typename _Kt>
  813. auto
  814. count(const _Kt& __x) const -> decltype(_M_h._M_count_tr(__x))
  815. { return _M_h._M_count_tr(__x); }
  816. #endif
  817. ///@}
  818. #if __cplusplus > 201703L
  819. ///@{
  820. /**
  821. * @brief Finds whether an element with the given key exists.
  822. * @param __x Key of elements to be located.
  823. * @return True if there is any element with the specified key.
  824. */
  825. bool
  826. contains(const key_type& __x) const
  827. { return _M_h.find(__x) != _M_h.end(); }
  828. template<typename _Kt>
  829. auto
  830. contains(const _Kt& __x) const
  831. -> decltype(_M_h._M_find_tr(__x), void(), true)
  832. { return _M_h._M_find_tr(__x) != _M_h.end(); }
  833. ///@}
  834. #endif
  835. ///@{
  836. /**
  837. * @brief Finds a subsequence matching given key.
  838. * @param __x Key to be located.
  839. * @return Pair of iterators that possibly points to the subsequence
  840. * matching given key.
  841. *
  842. * This function probably only makes sense for %unordered_multimap.
  843. */
  844. std::pair<iterator, iterator>
  845. equal_range(const key_type& __x)
  846. { return _M_h.equal_range(__x); }
  847. #if __cplusplus > 201703L
  848. template<typename _Kt>
  849. auto
  850. equal_range(const _Kt& __x)
  851. -> decltype(_M_h._M_equal_range_tr(__x))
  852. { return _M_h._M_equal_range_tr(__x); }
  853. #endif
  854. std::pair<const_iterator, const_iterator>
  855. equal_range(const key_type& __x) const
  856. { return _M_h.equal_range(__x); }
  857. #if __cplusplus > 201703L
  858. template<typename _Kt>
  859. auto
  860. equal_range(const _Kt& __x) const
  861. -> decltype(_M_h._M_equal_range_tr(__x))
  862. { return _M_h._M_equal_range_tr(__x); }
  863. #endif
  864. ///@}
  865. ///@{
  866. /**
  867. * @brief Subscript ( @c [] ) access to %unordered_map data.
  868. * @param __k The key for which data should be retrieved.
  869. * @return A reference to the data of the (key,data) %pair.
  870. *
  871. * Allows for easy lookup with the subscript ( @c [] )operator. Returns
  872. * data associated with the key specified in subscript. If the key does
  873. * not exist, a pair with that key is created using default values, which
  874. * is then returned.
  875. *
  876. * Lookup requires constant time.
  877. */
  878. mapped_type&
  879. operator[](const key_type& __k)
  880. { return _M_h[__k]; }
  881. mapped_type&
  882. operator[](key_type&& __k)
  883. { return _M_h[std::move(__k)]; }
  884. ///@}
  885. ///@{
  886. /**
  887. * @brief Access to %unordered_map data.
  888. * @param __k The key for which data should be retrieved.
  889. * @return A reference to the data whose key is equal to @a __k, if
  890. * such a data is present in the %unordered_map.
  891. * @throw std::out_of_range If no such data is present.
  892. */
  893. mapped_type&
  894. at(const key_type& __k)
  895. { return _M_h.at(__k); }
  896. const mapped_type&
  897. at(const key_type& __k) const
  898. { return _M_h.at(__k); }
  899. ///@}
  900. // bucket interface.
  901. /// Returns the number of buckets of the %unordered_map.
  902. size_type
  903. bucket_count() const noexcept
  904. { return _M_h.bucket_count(); }
  905. /// Returns the maximum number of buckets of the %unordered_map.
  906. size_type
  907. max_bucket_count() const noexcept
  908. { return _M_h.max_bucket_count(); }
  909. /*
  910. * @brief Returns the number of elements in a given bucket.
  911. * @param __n A bucket index.
  912. * @return The number of elements in the bucket.
  913. */
  914. size_type
  915. bucket_size(size_type __n) const
  916. { return _M_h.bucket_size(__n); }
  917. /*
  918. * @brief Returns the bucket index of a given element.
  919. * @param __key A key instance.
  920. * @return The key bucket index.
  921. */
  922. size_type
  923. bucket(const key_type& __key) const
  924. { return _M_h.bucket(__key); }
  925. /**
  926. * @brief Returns a read/write iterator pointing to the first bucket
  927. * element.
  928. * @param __n The bucket index.
  929. * @return A read/write local iterator.
  930. */
  931. local_iterator
  932. begin(size_type __n)
  933. { return _M_h.begin(__n); }
  934. ///@{
  935. /**
  936. * @brief Returns a read-only (constant) iterator pointing to the first
  937. * bucket element.
  938. * @param __n The bucket index.
  939. * @return A read-only local iterator.
  940. */
  941. const_local_iterator
  942. begin(size_type __n) const
  943. { return _M_h.begin(__n); }
  944. const_local_iterator
  945. cbegin(size_type __n) const
  946. { return _M_h.cbegin(__n); }
  947. ///@}
  948. /**
  949. * @brief Returns a read/write iterator pointing to one past the last
  950. * bucket elements.
  951. * @param __n The bucket index.
  952. * @return A read/write local iterator.
  953. */
  954. local_iterator
  955. end(size_type __n)
  956. { return _M_h.end(__n); }
  957. ///@{
  958. /**
  959. * @brief Returns a read-only (constant) iterator pointing to one past
  960. * the last bucket elements.
  961. * @param __n The bucket index.
  962. * @return A read-only local iterator.
  963. */
  964. const_local_iterator
  965. end(size_type __n) const
  966. { return _M_h.end(__n); }
  967. const_local_iterator
  968. cend(size_type __n) const
  969. { return _M_h.cend(__n); }
  970. ///@}
  971. // hash policy.
  972. /// Returns the average number of elements per bucket.
  973. float
  974. load_factor() const noexcept
  975. { return _M_h.load_factor(); }
  976. /// Returns a positive number that the %unordered_map tries to keep the
  977. /// load factor less than or equal to.
  978. float
  979. max_load_factor() const noexcept
  980. { return _M_h.max_load_factor(); }
  981. /**
  982. * @brief Change the %unordered_map maximum load factor.
  983. * @param __z The new maximum load factor.
  984. */
  985. void
  986. max_load_factor(float __z)
  987. { _M_h.max_load_factor(__z); }
  988. /**
  989. * @brief May rehash the %unordered_map.
  990. * @param __n The new number of buckets.
  991. *
  992. * Rehash will occur only if the new number of buckets respect the
  993. * %unordered_map maximum load factor.
  994. */
  995. void
  996. rehash(size_type __n)
  997. { _M_h.rehash(__n); }
  998. /**
  999. * @brief Prepare the %unordered_map for a specified number of
  1000. * elements.
  1001. * @param __n Number of elements required.
  1002. *
  1003. * Same as rehash(ceil(n / max_load_factor())).
  1004. */
  1005. void
  1006. reserve(size_type __n)
  1007. { _M_h.reserve(__n); }
  1008. template<typename _Key1, typename _Tp1, typename _Hash1, typename _Pred1,
  1009. typename _Alloc1>
  1010. friend bool
  1011. operator==(const unordered_map<_Key1, _Tp1, _Hash1, _Pred1, _Alloc1>&,
  1012. const unordered_map<_Key1, _Tp1, _Hash1, _Pred1, _Alloc1>&);
  1013. };
  1014. #if __cpp_deduction_guides >= 201606
  1015. template<typename _InputIterator,
  1016. typename _Hash = hash<__iter_key_t<_InputIterator>>,
  1017. typename _Pred = equal_to<__iter_key_t<_InputIterator>>,
  1018. typename _Allocator = allocator<__iter_to_alloc_t<_InputIterator>>,
  1019. typename = _RequireInputIter<_InputIterator>,
  1020. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  1021. typename = _RequireNotAllocator<_Pred>,
  1022. typename = _RequireAllocator<_Allocator>>
  1023. unordered_map(_InputIterator, _InputIterator,
  1024. typename unordered_map<int, int>::size_type = {},
  1025. _Hash = _Hash(), _Pred = _Pred(), _Allocator = _Allocator())
  1026. -> unordered_map<__iter_key_t<_InputIterator>,
  1027. __iter_val_t<_InputIterator>,
  1028. _Hash, _Pred, _Allocator>;
  1029. template<typename _Key, typename _Tp, typename _Hash = hash<_Key>,
  1030. typename _Pred = equal_to<_Key>,
  1031. typename _Allocator = allocator<pair<const _Key, _Tp>>,
  1032. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  1033. typename = _RequireNotAllocator<_Pred>,
  1034. typename = _RequireAllocator<_Allocator>>
  1035. unordered_map(initializer_list<pair<_Key, _Tp>>,
  1036. typename unordered_map<int, int>::size_type = {},
  1037. _Hash = _Hash(), _Pred = _Pred(), _Allocator = _Allocator())
  1038. -> unordered_map<_Key, _Tp, _Hash, _Pred, _Allocator>;
  1039. template<typename _InputIterator, typename _Allocator,
  1040. typename = _RequireInputIter<_InputIterator>,
  1041. typename = _RequireAllocator<_Allocator>>
  1042. unordered_map(_InputIterator, _InputIterator,
  1043. typename unordered_map<int, int>::size_type, _Allocator)
  1044. -> unordered_map<__iter_key_t<_InputIterator>,
  1045. __iter_val_t<_InputIterator>,
  1046. hash<__iter_key_t<_InputIterator>>,
  1047. equal_to<__iter_key_t<_InputIterator>>,
  1048. _Allocator>;
  1049. template<typename _InputIterator, typename _Allocator,
  1050. typename = _RequireInputIter<_InputIterator>,
  1051. typename = _RequireAllocator<_Allocator>>
  1052. unordered_map(_InputIterator, _InputIterator, _Allocator)
  1053. -> unordered_map<__iter_key_t<_InputIterator>,
  1054. __iter_val_t<_InputIterator>,
  1055. hash<__iter_key_t<_InputIterator>>,
  1056. equal_to<__iter_key_t<_InputIterator>>,
  1057. _Allocator>;
  1058. template<typename _InputIterator, typename _Hash, typename _Allocator,
  1059. typename = _RequireInputIter<_InputIterator>,
  1060. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  1061. typename = _RequireAllocator<_Allocator>>
  1062. unordered_map(_InputIterator, _InputIterator,
  1063. typename unordered_map<int, int>::size_type,
  1064. _Hash, _Allocator)
  1065. -> unordered_map<__iter_key_t<_InputIterator>,
  1066. __iter_val_t<_InputIterator>, _Hash,
  1067. equal_to<__iter_key_t<_InputIterator>>, _Allocator>;
  1068. template<typename _Key, typename _Tp, typename _Allocator,
  1069. typename = _RequireAllocator<_Allocator>>
  1070. unordered_map(initializer_list<pair<_Key, _Tp>>,
  1071. typename unordered_map<int, int>::size_type,
  1072. _Allocator)
  1073. -> unordered_map<_Key, _Tp, hash<_Key>, equal_to<_Key>, _Allocator>;
  1074. template<typename _Key, typename _Tp, typename _Allocator,
  1075. typename = _RequireAllocator<_Allocator>>
  1076. unordered_map(initializer_list<pair<_Key, _Tp>>, _Allocator)
  1077. -> unordered_map<_Key, _Tp, hash<_Key>, equal_to<_Key>, _Allocator>;
  1078. template<typename _Key, typename _Tp, typename _Hash, typename _Allocator,
  1079. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  1080. typename = _RequireAllocator<_Allocator>>
  1081. unordered_map(initializer_list<pair<_Key, _Tp>>,
  1082. typename unordered_map<int, int>::size_type,
  1083. _Hash, _Allocator)
  1084. -> unordered_map<_Key, _Tp, _Hash, equal_to<_Key>, _Allocator>;
  1085. #endif
  1086. /**
  1087. * @brief A standard container composed of equivalent keys
  1088. * (possibly containing multiple of each key value) that associates
  1089. * values of another type with the keys.
  1090. *
  1091. * @ingroup unordered_associative_containers
  1092. *
  1093. * @tparam _Key Type of key objects.
  1094. * @tparam _Tp Type of mapped objects.
  1095. * @tparam _Hash Hashing function object type, defaults to hash<_Value>.
  1096. * @tparam _Pred Predicate function object type, defaults
  1097. * to equal_to<_Value>.
  1098. * @tparam _Alloc Allocator type, defaults to
  1099. * std::allocator<std::pair<const _Key, _Tp>>.
  1100. *
  1101. * Meets the requirements of a <a href="tables.html#65">container</a>, and
  1102. * <a href="tables.html#xx">unordered associative container</a>
  1103. *
  1104. * The resulting value type of the container is std::pair<const _Key, _Tp>.
  1105. *
  1106. * Base is _Hashtable, dispatched at compile time via template
  1107. * alias __ummap_hashtable.
  1108. */
  1109. template<typename _Key, typename _Tp,
  1110. typename _Hash = hash<_Key>,
  1111. typename _Pred = equal_to<_Key>,
  1112. typename _Alloc = allocator<std::pair<const _Key, _Tp>>>
  1113. class unordered_multimap
  1114. {
  1115. typedef __ummap_hashtable<_Key, _Tp, _Hash, _Pred, _Alloc> _Hashtable;
  1116. _Hashtable _M_h;
  1117. public:
  1118. // typedefs:
  1119. ///@{
  1120. /// Public typedefs.
  1121. typedef typename _Hashtable::key_type key_type;
  1122. typedef typename _Hashtable::value_type value_type;
  1123. typedef typename _Hashtable::mapped_type mapped_type;
  1124. typedef typename _Hashtable::hasher hasher;
  1125. typedef typename _Hashtable::key_equal key_equal;
  1126. typedef typename _Hashtable::allocator_type allocator_type;
  1127. ///@}
  1128. ///@{
  1129. /// Iterator-related typedefs.
  1130. typedef typename _Hashtable::pointer pointer;
  1131. typedef typename _Hashtable::const_pointer const_pointer;
  1132. typedef typename _Hashtable::reference reference;
  1133. typedef typename _Hashtable::const_reference const_reference;
  1134. typedef typename _Hashtable::iterator iterator;
  1135. typedef typename _Hashtable::const_iterator const_iterator;
  1136. typedef typename _Hashtable::local_iterator local_iterator;
  1137. typedef typename _Hashtable::const_local_iterator const_local_iterator;
  1138. typedef typename _Hashtable::size_type size_type;
  1139. typedef typename _Hashtable::difference_type difference_type;
  1140. ///@}
  1141. #if __cplusplus > 201402L
  1142. using node_type = typename _Hashtable::node_type;
  1143. #endif
  1144. //construct/destroy/copy
  1145. /// Default constructor.
  1146. unordered_multimap() = default;
  1147. /**
  1148. * @brief Default constructor creates no elements.
  1149. * @param __n Mnimal initial number of buckets.
  1150. * @param __hf A hash functor.
  1151. * @param __eql A key equality functor.
  1152. * @param __a An allocator object.
  1153. */
  1154. explicit
  1155. unordered_multimap(size_type __n,
  1156. const hasher& __hf = hasher(),
  1157. const key_equal& __eql = key_equal(),
  1158. const allocator_type& __a = allocator_type())
  1159. : _M_h(__n, __hf, __eql, __a)
  1160. { }
  1161. /**
  1162. * @brief Builds an %unordered_multimap from a range.
  1163. * @param __first An input iterator.
  1164. * @param __last An input iterator.
  1165. * @param __n Minimal initial number of buckets.
  1166. * @param __hf A hash functor.
  1167. * @param __eql A key equality functor.
  1168. * @param __a An allocator object.
  1169. *
  1170. * Create an %unordered_multimap consisting of copies of the elements
  1171. * from [__first,__last). This is linear in N (where N is
  1172. * distance(__first,__last)).
  1173. */
  1174. template<typename _InputIterator>
  1175. unordered_multimap(_InputIterator __first, _InputIterator __last,
  1176. size_type __n = 0,
  1177. const hasher& __hf = hasher(),
  1178. const key_equal& __eql = key_equal(),
  1179. const allocator_type& __a = allocator_type())
  1180. : _M_h(__first, __last, __n, __hf, __eql, __a)
  1181. { }
  1182. /// Copy constructor.
  1183. unordered_multimap(const unordered_multimap&) = default;
  1184. /// Move constructor.
  1185. unordered_multimap(unordered_multimap&&) = default;
  1186. /**
  1187. * @brief Creates an %unordered_multimap with no elements.
  1188. * @param __a An allocator object.
  1189. */
  1190. explicit
  1191. unordered_multimap(const allocator_type& __a)
  1192. : _M_h(__a)
  1193. { }
  1194. /*
  1195. * @brief Copy constructor with allocator argument.
  1196. * @param __uset Input %unordered_multimap to copy.
  1197. * @param __a An allocator object.
  1198. */
  1199. unordered_multimap(const unordered_multimap& __ummap,
  1200. const allocator_type& __a)
  1201. : _M_h(__ummap._M_h, __a)
  1202. { }
  1203. /*
  1204. * @brief Move constructor with allocator argument.
  1205. * @param __uset Input %unordered_multimap to move.
  1206. * @param __a An allocator object.
  1207. */
  1208. unordered_multimap(unordered_multimap&& __ummap,
  1209. const allocator_type& __a)
  1210. noexcept( noexcept(_Hashtable(std::move(__ummap._M_h), __a)) )
  1211. : _M_h(std::move(__ummap._M_h), __a)
  1212. { }
  1213. /**
  1214. * @brief Builds an %unordered_multimap from an initializer_list.
  1215. * @param __l An initializer_list.
  1216. * @param __n Minimal initial number of buckets.
  1217. * @param __hf A hash functor.
  1218. * @param __eql A key equality functor.
  1219. * @param __a An allocator object.
  1220. *
  1221. * Create an %unordered_multimap consisting of copies of the elements in
  1222. * the list. This is linear in N (where N is @a __l.size()).
  1223. */
  1224. unordered_multimap(initializer_list<value_type> __l,
  1225. size_type __n = 0,
  1226. const hasher& __hf = hasher(),
  1227. const key_equal& __eql = key_equal(),
  1228. const allocator_type& __a = allocator_type())
  1229. : _M_h(__l, __n, __hf, __eql, __a)
  1230. { }
  1231. unordered_multimap(size_type __n, const allocator_type& __a)
  1232. : unordered_multimap(__n, hasher(), key_equal(), __a)
  1233. { }
  1234. unordered_multimap(size_type __n, const hasher& __hf,
  1235. const allocator_type& __a)
  1236. : unordered_multimap(__n, __hf, key_equal(), __a)
  1237. { }
  1238. template<typename _InputIterator>
  1239. unordered_multimap(_InputIterator __first, _InputIterator __last,
  1240. size_type __n,
  1241. const allocator_type& __a)
  1242. : unordered_multimap(__first, __last, __n, hasher(), key_equal(), __a)
  1243. { }
  1244. template<typename _InputIterator>
  1245. unordered_multimap(_InputIterator __first, _InputIterator __last,
  1246. size_type __n, const hasher& __hf,
  1247. const allocator_type& __a)
  1248. : unordered_multimap(__first, __last, __n, __hf, key_equal(), __a)
  1249. { }
  1250. unordered_multimap(initializer_list<value_type> __l,
  1251. size_type __n,
  1252. const allocator_type& __a)
  1253. : unordered_multimap(__l, __n, hasher(), key_equal(), __a)
  1254. { }
  1255. unordered_multimap(initializer_list<value_type> __l,
  1256. size_type __n, const hasher& __hf,
  1257. const allocator_type& __a)
  1258. : unordered_multimap(__l, __n, __hf, key_equal(), __a)
  1259. { }
  1260. /// Copy assignment operator.
  1261. unordered_multimap&
  1262. operator=(const unordered_multimap&) = default;
  1263. /// Move assignment operator.
  1264. unordered_multimap&
  1265. operator=(unordered_multimap&&) = default;
  1266. /**
  1267. * @brief %Unordered_multimap list assignment operator.
  1268. * @param __l An initializer_list.
  1269. *
  1270. * This function fills an %unordered_multimap with copies of the
  1271. * elements in the initializer list @a __l.
  1272. *
  1273. * Note that the assignment completely changes the %unordered_multimap
  1274. * and that the resulting %unordered_multimap's size is the same as the
  1275. * number of elements assigned.
  1276. */
  1277. unordered_multimap&
  1278. operator=(initializer_list<value_type> __l)
  1279. {
  1280. _M_h = __l;
  1281. return *this;
  1282. }
  1283. /// Returns the allocator object used by the %unordered_multimap.
  1284. allocator_type
  1285. get_allocator() const noexcept
  1286. { return _M_h.get_allocator(); }
  1287. // size and capacity:
  1288. /// Returns true if the %unordered_multimap is empty.
  1289. _GLIBCXX_NODISCARD bool
  1290. empty() const noexcept
  1291. { return _M_h.empty(); }
  1292. /// Returns the size of the %unordered_multimap.
  1293. size_type
  1294. size() const noexcept
  1295. { return _M_h.size(); }
  1296. /// Returns the maximum size of the %unordered_multimap.
  1297. size_type
  1298. max_size() const noexcept
  1299. { return _M_h.max_size(); }
  1300. // iterators.
  1301. /**
  1302. * Returns a read/write iterator that points to the first element in the
  1303. * %unordered_multimap.
  1304. */
  1305. iterator
  1306. begin() noexcept
  1307. { return _M_h.begin(); }
  1308. ///@{
  1309. /**
  1310. * Returns a read-only (constant) iterator that points to the first
  1311. * element in the %unordered_multimap.
  1312. */
  1313. const_iterator
  1314. begin() const noexcept
  1315. { return _M_h.begin(); }
  1316. const_iterator
  1317. cbegin() const noexcept
  1318. { return _M_h.begin(); }
  1319. ///@}
  1320. /**
  1321. * Returns a read/write iterator that points one past the last element in
  1322. * the %unordered_multimap.
  1323. */
  1324. iterator
  1325. end() noexcept
  1326. { return _M_h.end(); }
  1327. ///@{
  1328. /**
  1329. * Returns a read-only (constant) iterator that points one past the last
  1330. * element in the %unordered_multimap.
  1331. */
  1332. const_iterator
  1333. end() const noexcept
  1334. { return _M_h.end(); }
  1335. const_iterator
  1336. cend() const noexcept
  1337. { return _M_h.end(); }
  1338. ///@}
  1339. // modifiers.
  1340. /**
  1341. * @brief Attempts to build and insert a std::pair into the
  1342. * %unordered_multimap.
  1343. *
  1344. * @param __args Arguments used to generate a new pair instance (see
  1345. * std::piecewise_contruct for passing arguments to each
  1346. * part of the pair constructor).
  1347. *
  1348. * @return An iterator that points to the inserted pair.
  1349. *
  1350. * This function attempts to build and insert a (key, value) %pair into
  1351. * the %unordered_multimap.
  1352. *
  1353. * Insertion requires amortized constant time.
  1354. */
  1355. template<typename... _Args>
  1356. iterator
  1357. emplace(_Args&&... __args)
  1358. { return _M_h.emplace(std::forward<_Args>(__args)...); }
  1359. /**
  1360. * @brief Attempts to build and insert a std::pair into the
  1361. * %unordered_multimap.
  1362. *
  1363. * @param __pos An iterator that serves as a hint as to where the pair
  1364. * should be inserted.
  1365. * @param __args Arguments used to generate a new pair instance (see
  1366. * std::piecewise_contruct for passing arguments to each
  1367. * part of the pair constructor).
  1368. * @return An iterator that points to the element with key of the
  1369. * std::pair built from @a __args.
  1370. *
  1371. * Note that the first parameter is only a hint and can potentially
  1372. * improve the performance of the insertion process. A bad hint would
  1373. * cause no gains in efficiency.
  1374. *
  1375. * See
  1376. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
  1377. * for more on @a hinting.
  1378. *
  1379. * Insertion requires amortized constant time.
  1380. */
  1381. template<typename... _Args>
  1382. iterator
  1383. emplace_hint(const_iterator __pos, _Args&&... __args)
  1384. { return _M_h.emplace_hint(__pos, std::forward<_Args>(__args)...); }
  1385. ///@{
  1386. /**
  1387. * @brief Inserts a std::pair into the %unordered_multimap.
  1388. * @param __x Pair to be inserted (see std::make_pair for easy
  1389. * creation of pairs).
  1390. *
  1391. * @return An iterator that points to the inserted pair.
  1392. *
  1393. * Insertion requires amortized constant time.
  1394. */
  1395. iterator
  1396. insert(const value_type& __x)
  1397. { return _M_h.insert(__x); }
  1398. iterator
  1399. insert(value_type&& __x)
  1400. { return _M_h.insert(std::move(__x)); }
  1401. template<typename _Pair>
  1402. __enable_if_t<is_constructible<value_type, _Pair&&>::value, iterator>
  1403. insert(_Pair&& __x)
  1404. { return _M_h.emplace(std::forward<_Pair>(__x)); }
  1405. ///@}
  1406. ///@{
  1407. /**
  1408. * @brief Inserts a std::pair into the %unordered_multimap.
  1409. * @param __hint An iterator that serves as a hint as to where the
  1410. * pair should be inserted.
  1411. * @param __x Pair to be inserted (see std::make_pair for easy creation
  1412. * of pairs).
  1413. * @return An iterator that points to the element with key of
  1414. * @a __x (may or may not be the %pair passed in).
  1415. *
  1416. * Note that the first parameter is only a hint and can potentially
  1417. * improve the performance of the insertion process. A bad hint would
  1418. * cause no gains in efficiency.
  1419. *
  1420. * See
  1421. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
  1422. * for more on @a hinting.
  1423. *
  1424. * Insertion requires amortized constant time.
  1425. */
  1426. iterator
  1427. insert(const_iterator __hint, const value_type& __x)
  1428. { return _M_h.insert(__hint, __x); }
  1429. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1430. // 2354. Unnecessary copying when inserting into maps with braced-init
  1431. iterator
  1432. insert(const_iterator __hint, value_type&& __x)
  1433. { return _M_h.insert(__hint, std::move(__x)); }
  1434. template<typename _Pair>
  1435. __enable_if_t<is_constructible<value_type, _Pair&&>::value, iterator>
  1436. insert(const_iterator __hint, _Pair&& __x)
  1437. { return _M_h.emplace_hint(__hint, std::forward<_Pair>(__x)); }
  1438. ///@}
  1439. /**
  1440. * @brief A template function that attempts to insert a range of
  1441. * elements.
  1442. * @param __first Iterator pointing to the start of the range to be
  1443. * inserted.
  1444. * @param __last Iterator pointing to the end of the range.
  1445. *
  1446. * Complexity similar to that of the range constructor.
  1447. */
  1448. template<typename _InputIterator>
  1449. void
  1450. insert(_InputIterator __first, _InputIterator __last)
  1451. { _M_h.insert(__first, __last); }
  1452. /**
  1453. * @brief Attempts to insert a list of elements into the
  1454. * %unordered_multimap.
  1455. * @param __l A std::initializer_list<value_type> of elements
  1456. * to be inserted.
  1457. *
  1458. * Complexity similar to that of the range constructor.
  1459. */
  1460. void
  1461. insert(initializer_list<value_type> __l)
  1462. { _M_h.insert(__l); }
  1463. #if __cplusplus > 201402L
  1464. /// Extract a node.
  1465. node_type
  1466. extract(const_iterator __pos)
  1467. {
  1468. __glibcxx_assert(__pos != end());
  1469. return _M_h.extract(__pos);
  1470. }
  1471. /// Extract a node.
  1472. node_type
  1473. extract(const key_type& __key)
  1474. { return _M_h.extract(__key); }
  1475. /// Re-insert an extracted node.
  1476. iterator
  1477. insert(node_type&& __nh)
  1478. { return _M_h._M_reinsert_node_multi(cend(), std::move(__nh)); }
  1479. /// Re-insert an extracted node.
  1480. iterator
  1481. insert(const_iterator __hint, node_type&& __nh)
  1482. { return _M_h._M_reinsert_node_multi(__hint, std::move(__nh)); }
  1483. #endif // C++17
  1484. ///@{
  1485. /**
  1486. * @brief Erases an element from an %unordered_multimap.
  1487. * @param __position An iterator pointing to the element to be erased.
  1488. * @return An iterator pointing to the element immediately following
  1489. * @a __position prior to the element being erased. If no such
  1490. * element exists, end() is returned.
  1491. *
  1492. * This function erases an element, pointed to by the given iterator,
  1493. * from an %unordered_multimap.
  1494. * Note that this function only erases the element, and that if the
  1495. * element is itself a pointer, the pointed-to memory is not touched in
  1496. * any way. Managing the pointer is the user's responsibility.
  1497. */
  1498. iterator
  1499. erase(const_iterator __position)
  1500. { return _M_h.erase(__position); }
  1501. // LWG 2059.
  1502. iterator
  1503. erase(iterator __position)
  1504. { return _M_h.erase(__position); }
  1505. ///@}
  1506. /**
  1507. * @brief Erases elements according to the provided key.
  1508. * @param __x Key of elements to be erased.
  1509. * @return The number of elements erased.
  1510. *
  1511. * This function erases all the elements located by the given key from
  1512. * an %unordered_multimap.
  1513. * Note that this function only erases the element, and that if the
  1514. * element is itself a pointer, the pointed-to memory is not touched in
  1515. * any way. Managing the pointer is the user's responsibility.
  1516. */
  1517. size_type
  1518. erase(const key_type& __x)
  1519. { return _M_h.erase(__x); }
  1520. /**
  1521. * @brief Erases a [__first,__last) range of elements from an
  1522. * %unordered_multimap.
  1523. * @param __first Iterator pointing to the start of the range to be
  1524. * erased.
  1525. * @param __last Iterator pointing to the end of the range to
  1526. * be erased.
  1527. * @return The iterator @a __last.
  1528. *
  1529. * This function erases a sequence of elements from an
  1530. * %unordered_multimap.
  1531. * Note that this function only erases the elements, and that if
  1532. * the element is itself a pointer, the pointed-to memory is not touched
  1533. * in any way. Managing the pointer is the user's responsibility.
  1534. */
  1535. iterator
  1536. erase(const_iterator __first, const_iterator __last)
  1537. { return _M_h.erase(__first, __last); }
  1538. /**
  1539. * Erases all elements in an %unordered_multimap.
  1540. * Note that this function only erases the elements, and that if the
  1541. * elements themselves are pointers, the pointed-to memory is not touched
  1542. * in any way. Managing the pointer is the user's responsibility.
  1543. */
  1544. void
  1545. clear() noexcept
  1546. { _M_h.clear(); }
  1547. /**
  1548. * @brief Swaps data with another %unordered_multimap.
  1549. * @param __x An %unordered_multimap of the same element and allocator
  1550. * types.
  1551. *
  1552. * This exchanges the elements between two %unordered_multimap in
  1553. * constant time.
  1554. * Note that the global std::swap() function is specialized such that
  1555. * std::swap(m1,m2) will feed to this function.
  1556. */
  1557. void
  1558. swap(unordered_multimap& __x)
  1559. noexcept( noexcept(_M_h.swap(__x._M_h)) )
  1560. { _M_h.swap(__x._M_h); }
  1561. #if __cplusplus > 201402L
  1562. template<typename, typename, typename>
  1563. friend class std::_Hash_merge_helper;
  1564. template<typename _H2, typename _P2>
  1565. void
  1566. merge(unordered_multimap<_Key, _Tp, _H2, _P2, _Alloc>& __source)
  1567. {
  1568. using _Merge_helper
  1569. = _Hash_merge_helper<unordered_multimap, _H2, _P2>;
  1570. _M_h._M_merge_multi(_Merge_helper::_S_get_table(__source));
  1571. }
  1572. template<typename _H2, typename _P2>
  1573. void
  1574. merge(unordered_multimap<_Key, _Tp, _H2, _P2, _Alloc>&& __source)
  1575. { merge(__source); }
  1576. template<typename _H2, typename _P2>
  1577. void
  1578. merge(unordered_map<_Key, _Tp, _H2, _P2, _Alloc>& __source)
  1579. {
  1580. using _Merge_helper
  1581. = _Hash_merge_helper<unordered_multimap, _H2, _P2>;
  1582. _M_h._M_merge_multi(_Merge_helper::_S_get_table(__source));
  1583. }
  1584. template<typename _H2, typename _P2>
  1585. void
  1586. merge(unordered_map<_Key, _Tp, _H2, _P2, _Alloc>&& __source)
  1587. { merge(__source); }
  1588. #endif // C++17
  1589. // observers.
  1590. /// Returns the hash functor object with which the %unordered_multimap
  1591. /// was constructed.
  1592. hasher
  1593. hash_function() const
  1594. { return _M_h.hash_function(); }
  1595. /// Returns the key comparison object with which the %unordered_multimap
  1596. /// was constructed.
  1597. key_equal
  1598. key_eq() const
  1599. { return _M_h.key_eq(); }
  1600. // lookup.
  1601. ///@{
  1602. /**
  1603. * @brief Tries to locate an element in an %unordered_multimap.
  1604. * @param __x Key to be located.
  1605. * @return Iterator pointing to sought-after element, or end() if not
  1606. * found.
  1607. *
  1608. * This function takes a key and tries to locate the element with which
  1609. * the key matches. If successful the function returns an iterator
  1610. * pointing to the sought after element. If unsuccessful it returns the
  1611. * past-the-end ( @c end() ) iterator.
  1612. */
  1613. iterator
  1614. find(const key_type& __x)
  1615. { return _M_h.find(__x); }
  1616. #if __cplusplus > 201703L
  1617. template<typename _Kt>
  1618. auto
  1619. find(const _Kt& __x) -> decltype(_M_h._M_find_tr(__x))
  1620. { return _M_h._M_find_tr(__x); }
  1621. #endif
  1622. const_iterator
  1623. find(const key_type& __x) const
  1624. { return _M_h.find(__x); }
  1625. #if __cplusplus > 201703L
  1626. template<typename _Kt>
  1627. auto
  1628. find(const _Kt& __x) const -> decltype(_M_h._M_find_tr(__x))
  1629. { return _M_h._M_find_tr(__x); }
  1630. #endif
  1631. ///@}
  1632. ///@{
  1633. /**
  1634. * @brief Finds the number of elements.
  1635. * @param __x Key to count.
  1636. * @return Number of elements with specified key.
  1637. */
  1638. size_type
  1639. count(const key_type& __x) const
  1640. { return _M_h.count(__x); }
  1641. #if __cplusplus > 201703L
  1642. template<typename _Kt>
  1643. auto
  1644. count(const _Kt& __x) const -> decltype(_M_h._M_count_tr(__x))
  1645. { return _M_h._M_count_tr(__x); }
  1646. #endif
  1647. ///@}
  1648. #if __cplusplus > 201703L
  1649. ///@{
  1650. /**
  1651. * @brief Finds whether an element with the given key exists.
  1652. * @param __x Key of elements to be located.
  1653. * @return True if there is any element with the specified key.
  1654. */
  1655. bool
  1656. contains(const key_type& __x) const
  1657. { return _M_h.find(__x) != _M_h.end(); }
  1658. template<typename _Kt>
  1659. auto
  1660. contains(const _Kt& __x) const
  1661. -> decltype(_M_h._M_find_tr(__x), void(), true)
  1662. { return _M_h._M_find_tr(__x) != _M_h.end(); }
  1663. ///@}
  1664. #endif
  1665. ///@{
  1666. /**
  1667. * @brief Finds a subsequence matching given key.
  1668. * @param __x Key to be located.
  1669. * @return Pair of iterators that possibly points to the subsequence
  1670. * matching given key.
  1671. */
  1672. std::pair<iterator, iterator>
  1673. equal_range(const key_type& __x)
  1674. { return _M_h.equal_range(__x); }
  1675. #if __cplusplus > 201703L
  1676. template<typename _Kt>
  1677. auto
  1678. equal_range(const _Kt& __x)
  1679. -> decltype(_M_h._M_equal_range_tr(__x))
  1680. { return _M_h._M_equal_range_tr(__x); }
  1681. #endif
  1682. std::pair<const_iterator, const_iterator>
  1683. equal_range(const key_type& __x) const
  1684. { return _M_h.equal_range(__x); }
  1685. #if __cplusplus > 201703L
  1686. template<typename _Kt>
  1687. auto
  1688. equal_range(const _Kt& __x) const
  1689. -> decltype(_M_h._M_equal_range_tr(__x))
  1690. { return _M_h._M_equal_range_tr(__x); }
  1691. #endif
  1692. ///@}
  1693. // bucket interface.
  1694. /// Returns the number of buckets of the %unordered_multimap.
  1695. size_type
  1696. bucket_count() const noexcept
  1697. { return _M_h.bucket_count(); }
  1698. /// Returns the maximum number of buckets of the %unordered_multimap.
  1699. size_type
  1700. max_bucket_count() const noexcept
  1701. { return _M_h.max_bucket_count(); }
  1702. /*
  1703. * @brief Returns the number of elements in a given bucket.
  1704. * @param __n A bucket index.
  1705. * @return The number of elements in the bucket.
  1706. */
  1707. size_type
  1708. bucket_size(size_type __n) const
  1709. { return _M_h.bucket_size(__n); }
  1710. /*
  1711. * @brief Returns the bucket index of a given element.
  1712. * @param __key A key instance.
  1713. * @return The key bucket index.
  1714. */
  1715. size_type
  1716. bucket(const key_type& __key) const
  1717. { return _M_h.bucket(__key); }
  1718. /**
  1719. * @brief Returns a read/write iterator pointing to the first bucket
  1720. * element.
  1721. * @param __n The bucket index.
  1722. * @return A read/write local iterator.
  1723. */
  1724. local_iterator
  1725. begin(size_type __n)
  1726. { return _M_h.begin(__n); }
  1727. ///@{
  1728. /**
  1729. * @brief Returns a read-only (constant) iterator pointing to the first
  1730. * bucket element.
  1731. * @param __n The bucket index.
  1732. * @return A read-only local iterator.
  1733. */
  1734. const_local_iterator
  1735. begin(size_type __n) const
  1736. { return _M_h.begin(__n); }
  1737. const_local_iterator
  1738. cbegin(size_type __n) const
  1739. { return _M_h.cbegin(__n); }
  1740. ///@}
  1741. /**
  1742. * @brief Returns a read/write iterator pointing to one past the last
  1743. * bucket elements.
  1744. * @param __n The bucket index.
  1745. * @return A read/write local iterator.
  1746. */
  1747. local_iterator
  1748. end(size_type __n)
  1749. { return _M_h.end(__n); }
  1750. ///@{
  1751. /**
  1752. * @brief Returns a read-only (constant) iterator pointing to one past
  1753. * the last bucket elements.
  1754. * @param __n The bucket index.
  1755. * @return A read-only local iterator.
  1756. */
  1757. const_local_iterator
  1758. end(size_type __n) const
  1759. { return _M_h.end(__n); }
  1760. const_local_iterator
  1761. cend(size_type __n) const
  1762. { return _M_h.cend(__n); }
  1763. ///@}
  1764. // hash policy.
  1765. /// Returns the average number of elements per bucket.
  1766. float
  1767. load_factor() const noexcept
  1768. { return _M_h.load_factor(); }
  1769. /// Returns a positive number that the %unordered_multimap tries to keep
  1770. /// the load factor less than or equal to.
  1771. float
  1772. max_load_factor() const noexcept
  1773. { return _M_h.max_load_factor(); }
  1774. /**
  1775. * @brief Change the %unordered_multimap maximum load factor.
  1776. * @param __z The new maximum load factor.
  1777. */
  1778. void
  1779. max_load_factor(float __z)
  1780. { _M_h.max_load_factor(__z); }
  1781. /**
  1782. * @brief May rehash the %unordered_multimap.
  1783. * @param __n The new number of buckets.
  1784. *
  1785. * Rehash will occur only if the new number of buckets respect the
  1786. * %unordered_multimap maximum load factor.
  1787. */
  1788. void
  1789. rehash(size_type __n)
  1790. { _M_h.rehash(__n); }
  1791. /**
  1792. * @brief Prepare the %unordered_multimap for a specified number of
  1793. * elements.
  1794. * @param __n Number of elements required.
  1795. *
  1796. * Same as rehash(ceil(n / max_load_factor())).
  1797. */
  1798. void
  1799. reserve(size_type __n)
  1800. { _M_h.reserve(__n); }
  1801. template<typename _Key1, typename _Tp1, typename _Hash1, typename _Pred1,
  1802. typename _Alloc1>
  1803. friend bool
  1804. operator==(const unordered_multimap<_Key1, _Tp1,
  1805. _Hash1, _Pred1, _Alloc1>&,
  1806. const unordered_multimap<_Key1, _Tp1,
  1807. _Hash1, _Pred1, _Alloc1>&);
  1808. };
  1809. #if __cpp_deduction_guides >= 201606
  1810. template<typename _InputIterator,
  1811. typename _Hash = hash<__iter_key_t<_InputIterator>>,
  1812. typename _Pred = equal_to<__iter_key_t<_InputIterator>>,
  1813. typename _Allocator = allocator<__iter_to_alloc_t<_InputIterator>>,
  1814. typename = _RequireInputIter<_InputIterator>,
  1815. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  1816. typename = _RequireNotAllocator<_Pred>,
  1817. typename = _RequireAllocator<_Allocator>>
  1818. unordered_multimap(_InputIterator, _InputIterator,
  1819. unordered_multimap<int, int>::size_type = {},
  1820. _Hash = _Hash(), _Pred = _Pred(),
  1821. _Allocator = _Allocator())
  1822. -> unordered_multimap<__iter_key_t<_InputIterator>,
  1823. __iter_val_t<_InputIterator>, _Hash, _Pred,
  1824. _Allocator>;
  1825. template<typename _Key, typename _Tp, typename _Hash = hash<_Key>,
  1826. typename _Pred = equal_to<_Key>,
  1827. typename _Allocator = allocator<pair<const _Key, _Tp>>,
  1828. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  1829. typename = _RequireNotAllocator<_Pred>,
  1830. typename = _RequireAllocator<_Allocator>>
  1831. unordered_multimap(initializer_list<pair<_Key, _Tp>>,
  1832. unordered_multimap<int, int>::size_type = {},
  1833. _Hash = _Hash(), _Pred = _Pred(),
  1834. _Allocator = _Allocator())
  1835. -> unordered_multimap<_Key, _Tp, _Hash, _Pred, _Allocator>;
  1836. template<typename _InputIterator, typename _Allocator,
  1837. typename = _RequireInputIter<_InputIterator>,
  1838. typename = _RequireAllocator<_Allocator>>
  1839. unordered_multimap(_InputIterator, _InputIterator,
  1840. unordered_multimap<int, int>::size_type, _Allocator)
  1841. -> unordered_multimap<__iter_key_t<_InputIterator>,
  1842. __iter_val_t<_InputIterator>,
  1843. hash<__iter_key_t<_InputIterator>>,
  1844. equal_to<__iter_key_t<_InputIterator>>, _Allocator>;
  1845. template<typename _InputIterator, typename _Allocator,
  1846. typename = _RequireInputIter<_InputIterator>,
  1847. typename = _RequireAllocator<_Allocator>>
  1848. unordered_multimap(_InputIterator, _InputIterator, _Allocator)
  1849. -> unordered_multimap<__iter_key_t<_InputIterator>,
  1850. __iter_val_t<_InputIterator>,
  1851. hash<__iter_key_t<_InputIterator>>,
  1852. equal_to<__iter_key_t<_InputIterator>>, _Allocator>;
  1853. template<typename _InputIterator, typename _Hash, typename _Allocator,
  1854. typename = _RequireInputIter<_InputIterator>,
  1855. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  1856. typename = _RequireAllocator<_Allocator>>
  1857. unordered_multimap(_InputIterator, _InputIterator,
  1858. unordered_multimap<int, int>::size_type, _Hash,
  1859. _Allocator)
  1860. -> unordered_multimap<__iter_key_t<_InputIterator>,
  1861. __iter_val_t<_InputIterator>, _Hash,
  1862. equal_to<__iter_key_t<_InputIterator>>, _Allocator>;
  1863. template<typename _Key, typename _Tp, typename _Allocator,
  1864. typename = _RequireAllocator<_Allocator>>
  1865. unordered_multimap(initializer_list<pair<_Key, _Tp>>,
  1866. unordered_multimap<int, int>::size_type,
  1867. _Allocator)
  1868. -> unordered_multimap<_Key, _Tp, hash<_Key>, equal_to<_Key>, _Allocator>;
  1869. template<typename _Key, typename _Tp, typename _Allocator,
  1870. typename = _RequireAllocator<_Allocator>>
  1871. unordered_multimap(initializer_list<pair<_Key, _Tp>>, _Allocator)
  1872. -> unordered_multimap<_Key, _Tp, hash<_Key>, equal_to<_Key>, _Allocator>;
  1873. template<typename _Key, typename _Tp, typename _Hash, typename _Allocator,
  1874. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  1875. typename = _RequireAllocator<_Allocator>>
  1876. unordered_multimap(initializer_list<pair<_Key, _Tp>>,
  1877. unordered_multimap<int, int>::size_type,
  1878. _Hash, _Allocator)
  1879. -> unordered_multimap<_Key, _Tp, _Hash, equal_to<_Key>, _Allocator>;
  1880. #endif
  1881. template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  1882. inline void
  1883. swap(unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  1884. unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  1885. noexcept(noexcept(__x.swap(__y)))
  1886. { __x.swap(__y); }
  1887. template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  1888. inline void
  1889. swap(unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  1890. unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  1891. noexcept(noexcept(__x.swap(__y)))
  1892. { __x.swap(__y); }
  1893. template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  1894. inline bool
  1895. operator==(const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  1896. const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  1897. { return __x._M_h._M_equal(__y._M_h); }
  1898. #if __cpp_impl_three_way_comparison < 201907L
  1899. template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  1900. inline bool
  1901. operator!=(const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  1902. const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  1903. { return !(__x == __y); }
  1904. #endif
  1905. template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  1906. inline bool
  1907. operator==(const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  1908. const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  1909. { return __x._M_h._M_equal(__y._M_h); }
  1910. #if __cpp_impl_three_way_comparison < 201907L
  1911. template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  1912. inline bool
  1913. operator!=(const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  1914. const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  1915. { return !(__x == __y); }
  1916. #endif
  1917. _GLIBCXX_END_NAMESPACE_CONTAINER
  1918. #if __cplusplus > 201402L
  1919. // Allow std::unordered_map access to internals of compatible maps.
  1920. template<typename _Key, typename _Val, typename _Hash1, typename _Eq1,
  1921. typename _Alloc, typename _Hash2, typename _Eq2>
  1922. struct _Hash_merge_helper<
  1923. _GLIBCXX_STD_C::unordered_map<_Key, _Val, _Hash1, _Eq1, _Alloc>,
  1924. _Hash2, _Eq2>
  1925. {
  1926. private:
  1927. template<typename... _Tp>
  1928. using unordered_map = _GLIBCXX_STD_C::unordered_map<_Tp...>;
  1929. template<typename... _Tp>
  1930. using unordered_multimap = _GLIBCXX_STD_C::unordered_multimap<_Tp...>;
  1931. friend unordered_map<_Key, _Val, _Hash1, _Eq1, _Alloc>;
  1932. static auto&
  1933. _S_get_table(unordered_map<_Key, _Val, _Hash2, _Eq2, _Alloc>& __map)
  1934. { return __map._M_h; }
  1935. static auto&
  1936. _S_get_table(unordered_multimap<_Key, _Val, _Hash2, _Eq2, _Alloc>& __map)
  1937. { return __map._M_h; }
  1938. };
  1939. // Allow std::unordered_multimap access to internals of compatible maps.
  1940. template<typename _Key, typename _Val, typename _Hash1, typename _Eq1,
  1941. typename _Alloc, typename _Hash2, typename _Eq2>
  1942. struct _Hash_merge_helper<
  1943. _GLIBCXX_STD_C::unordered_multimap<_Key, _Val, _Hash1, _Eq1, _Alloc>,
  1944. _Hash2, _Eq2>
  1945. {
  1946. private:
  1947. template<typename... _Tp>
  1948. using unordered_map = _GLIBCXX_STD_C::unordered_map<_Tp...>;
  1949. template<typename... _Tp>
  1950. using unordered_multimap = _GLIBCXX_STD_C::unordered_multimap<_Tp...>;
  1951. friend unordered_multimap<_Key, _Val, _Hash1, _Eq1, _Alloc>;
  1952. static auto&
  1953. _S_get_table(unordered_map<_Key, _Val, _Hash2, _Eq2, _Alloc>& __map)
  1954. { return __map._M_h; }
  1955. static auto&
  1956. _S_get_table(unordered_multimap<_Key, _Val, _Hash2, _Eq2, _Alloc>& __map)
  1957. { return __map._M_h; }
  1958. };
  1959. #endif // C++17
  1960. _GLIBCXX_END_NAMESPACE_VERSION
  1961. } // namespace std
  1962. #endif /* _UNORDERED_MAP_H */