unordered_map.h 75 KB

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