unordered_map.h 74 KB

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