unordered_map.h 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171
  1. // unordered_map implementation -*- C++ -*-
  2. // Copyright (C) 2010-2020 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. _GLIBCXX_NODISCARD 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. /**
  592. * @brief Attempts to insert a std::pair into the %unordered_map.
  593. * @param __k Key to use for finding a possibly existing pair in
  594. * the map.
  595. * @param __obj Argument used to generate the .second for a pair
  596. * instance.
  597. *
  598. * @return A pair, of which the first element is an iterator that
  599. * points to the possibly inserted pair, and the second is
  600. * a bool that is true if the pair was actually inserted.
  601. *
  602. * This function attempts to insert a (key, value) %pair into the
  603. * %unordered_map. An %unordered_map relies on unique keys and thus a
  604. * %pair is only inserted if its first element (the key) is not already
  605. * present in the %unordered_map.
  606. * If the %pair was already in the %unordered_map, the .second of
  607. * the %pair is assigned from __obj.
  608. *
  609. * Insertion requires amortized constant time.
  610. */
  611. template <typename _Obj>
  612. pair<iterator, bool>
  613. insert_or_assign(const key_type& __k, _Obj&& __obj)
  614. {
  615. iterator __i = find(__k);
  616. if (__i == end())
  617. {
  618. __i = emplace(std::piecewise_construct,
  619. std::forward_as_tuple(__k),
  620. std::forward_as_tuple(std::forward<_Obj>(__obj)))
  621. .first;
  622. return {__i, true};
  623. }
  624. (*__i).second = std::forward<_Obj>(__obj);
  625. return {__i, false};
  626. }
  627. // move-capable overload
  628. template <typename _Obj>
  629. pair<iterator, bool>
  630. insert_or_assign(key_type&& __k, _Obj&& __obj)
  631. {
  632. iterator __i = find(__k);
  633. if (__i == end())
  634. {
  635. __i = emplace(std::piecewise_construct,
  636. std::forward_as_tuple(std::move(__k)),
  637. std::forward_as_tuple(std::forward<_Obj>(__obj)))
  638. .first;
  639. return {__i, true};
  640. }
  641. (*__i).second = std::forward<_Obj>(__obj);
  642. return {__i, false};
  643. }
  644. /**
  645. * @brief Attempts to insert a std::pair into the %unordered_map.
  646. * @param __hint An iterator that serves as a hint as to where the
  647. * pair should be inserted.
  648. * @param __k Key to use for finding a possibly existing pair in
  649. * the unordered_map.
  650. * @param __obj Argument used to generate the .second for a pair
  651. * instance.
  652. * @return An iterator that points to the element with key of
  653. * @a __x (may or may not be the %pair passed in).
  654. *
  655. * This function is not concerned about whether the insertion took place,
  656. * and thus does not return a boolean like the single-argument insert()
  657. * does.
  658. * If the %pair was already in the %unordered map, the .second of
  659. * the %pair is assigned from __obj.
  660. * Note that the first parameter is only a hint and can
  661. * potentially improve the performance of the insertion process. A bad
  662. * hint would cause no gains in efficiency.
  663. *
  664. * See
  665. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
  666. * for more on @a hinting.
  667. *
  668. * Insertion requires amortized constant time.
  669. */
  670. template <typename _Obj>
  671. iterator
  672. insert_or_assign(const_iterator __hint, const key_type& __k,
  673. _Obj&& __obj)
  674. {
  675. iterator __i = find(__k);
  676. if (__i == end())
  677. {
  678. return emplace_hint(__hint, std::piecewise_construct,
  679. std::forward_as_tuple(__k),
  680. std::forward_as_tuple(
  681. std::forward<_Obj>(__obj)));
  682. }
  683. (*__i).second = std::forward<_Obj>(__obj);
  684. return __i;
  685. }
  686. // move-capable overload
  687. template <typename _Obj>
  688. iterator
  689. insert_or_assign(const_iterator __hint, key_type&& __k, _Obj&& __obj)
  690. {
  691. iterator __i = find(__k);
  692. if (__i == end())
  693. {
  694. return emplace_hint(__hint, std::piecewise_construct,
  695. std::forward_as_tuple(std::move(__k)),
  696. std::forward_as_tuple(
  697. std::forward<_Obj>(__obj)));
  698. }
  699. (*__i).second = std::forward<_Obj>(__obj);
  700. return __i;
  701. }
  702. #endif
  703. //@{
  704. /**
  705. * @brief Erases an element from an %unordered_map.
  706. * @param __position An iterator pointing to the element to be erased.
  707. * @return An iterator pointing to the element immediately following
  708. * @a __position prior to the element being erased. If no such
  709. * element exists, end() is returned.
  710. *
  711. * This function erases an element, pointed to by the given iterator,
  712. * from an %unordered_map.
  713. * Note that this function only erases the element, and that if the
  714. * element is itself a pointer, the pointed-to memory is not touched in
  715. * any way. Managing the pointer is the user's responsibility.
  716. */
  717. iterator
  718. erase(const_iterator __position)
  719. { return _M_h.erase(__position); }
  720. // LWG 2059.
  721. iterator
  722. erase(iterator __position)
  723. { return _M_h.erase(__position); }
  724. //@}
  725. /**
  726. * @brief Erases elements according to the provided key.
  727. * @param __x Key of element to be erased.
  728. * @return The number of elements erased.
  729. *
  730. * This function erases all the elements located by the given key from
  731. * an %unordered_map. For an %unordered_map the result of this function
  732. * can only be 0 (not present) or 1 (present).
  733. * Note that this function only erases the element, and that if the
  734. * element is itself a pointer, the pointed-to memory is not touched in
  735. * any way. Managing the pointer is the user's responsibility.
  736. */
  737. size_type
  738. erase(const key_type& __x)
  739. { return _M_h.erase(__x); }
  740. /**
  741. * @brief Erases a [__first,__last) range of elements from an
  742. * %unordered_map.
  743. * @param __first Iterator pointing to the start of the range to be
  744. * erased.
  745. * @param __last Iterator pointing to the end of the range to
  746. * be erased.
  747. * @return The iterator @a __last.
  748. *
  749. * This function erases a sequence of elements from an %unordered_map.
  750. * Note that this function only erases the elements, and that if
  751. * the element is itself a pointer, the pointed-to memory is not touched
  752. * in any way. Managing the pointer is the user's responsibility.
  753. */
  754. iterator
  755. erase(const_iterator __first, const_iterator __last)
  756. { return _M_h.erase(__first, __last); }
  757. /**
  758. * Erases all elements in an %unordered_map.
  759. * Note that this function only erases the elements, and that if the
  760. * elements themselves are pointers, the pointed-to memory is not touched
  761. * in any way. Managing the pointer is the user's responsibility.
  762. */
  763. void
  764. clear() noexcept
  765. { _M_h.clear(); }
  766. /**
  767. * @brief Swaps data with another %unordered_map.
  768. * @param __x An %unordered_map of the same element and allocator
  769. * types.
  770. *
  771. * This exchanges the elements between two %unordered_map in constant
  772. * time.
  773. * Note that the global std::swap() function is specialized such that
  774. * std::swap(m1,m2) will feed to this function.
  775. */
  776. void
  777. swap(unordered_map& __x)
  778. noexcept( noexcept(_M_h.swap(__x._M_h)) )
  779. { _M_h.swap(__x._M_h); }
  780. #if __cplusplus > 201402L
  781. template<typename, typename, typename>
  782. friend class std::_Hash_merge_helper;
  783. template<typename _H2, typename _P2>
  784. void
  785. merge(unordered_map<_Key, _Tp, _H2, _P2, _Alloc>& __source)
  786. {
  787. using _Merge_helper = _Hash_merge_helper<unordered_map, _H2, _P2>;
  788. _M_h._M_merge_unique(_Merge_helper::_S_get_table(__source));
  789. }
  790. template<typename _H2, typename _P2>
  791. void
  792. merge(unordered_map<_Key, _Tp, _H2, _P2, _Alloc>&& __source)
  793. { merge(__source); }
  794. template<typename _H2, typename _P2>
  795. void
  796. merge(unordered_multimap<_Key, _Tp, _H2, _P2, _Alloc>& __source)
  797. {
  798. using _Merge_helper = _Hash_merge_helper<unordered_map, _H2, _P2>;
  799. _M_h._M_merge_unique(_Merge_helper::_S_get_table(__source));
  800. }
  801. template<typename _H2, typename _P2>
  802. void
  803. merge(unordered_multimap<_Key, _Tp, _H2, _P2, _Alloc>&& __source)
  804. { merge(__source); }
  805. #endif // C++17
  806. // observers.
  807. /// Returns the hash functor object with which the %unordered_map was
  808. /// constructed.
  809. hasher
  810. hash_function() const
  811. { return _M_h.hash_function(); }
  812. /// Returns the key comparison object with which the %unordered_map was
  813. /// constructed.
  814. key_equal
  815. key_eq() const
  816. { return _M_h.key_eq(); }
  817. // lookup.
  818. //@{
  819. /**
  820. * @brief Tries to locate an element in an %unordered_map.
  821. * @param __x Key to be located.
  822. * @return Iterator pointing to sought-after element, or end() if not
  823. * found.
  824. *
  825. * This function takes a key and tries to locate the element with which
  826. * the key matches. If successful the function returns an iterator
  827. * pointing to the sought after element. If unsuccessful it returns the
  828. * past-the-end ( @c end() ) iterator.
  829. */
  830. iterator
  831. find(const key_type& __x)
  832. { return _M_h.find(__x); }
  833. const_iterator
  834. find(const key_type& __x) const
  835. { return _M_h.find(__x); }
  836. //@}
  837. /**
  838. * @brief Finds the number of elements.
  839. * @param __x Key to count.
  840. * @return Number of elements with specified key.
  841. *
  842. * This function only makes sense for %unordered_multimap; for
  843. * %unordered_map the result will either be 0 (not present) or 1
  844. * (present).
  845. */
  846. size_type
  847. count(const key_type& __x) const
  848. { return _M_h.count(__x); }
  849. #if __cplusplus > 201703L
  850. /**
  851. * @brief Finds whether an element with the given key exists.
  852. * @param __x Key of elements to be located.
  853. * @return True if there is any element with the specified key.
  854. */
  855. bool
  856. contains(const key_type& __x) const
  857. { return _M_h.find(__x) != _M_h.end(); }
  858. #endif
  859. //@{
  860. /**
  861. * @brief Finds a subsequence matching given key.
  862. * @param __x Key to be located.
  863. * @return Pair of iterators that possibly points to the subsequence
  864. * matching given key.
  865. *
  866. * This function probably only makes sense for %unordered_multimap.
  867. */
  868. std::pair<iterator, iterator>
  869. equal_range(const key_type& __x)
  870. { return _M_h.equal_range(__x); }
  871. std::pair<const_iterator, const_iterator>
  872. equal_range(const key_type& __x) const
  873. { return _M_h.equal_range(__x); }
  874. //@}
  875. //@{
  876. /**
  877. * @brief Subscript ( @c [] ) access to %unordered_map data.
  878. * @param __k The key for which data should be retrieved.
  879. * @return A reference to the data of the (key,data) %pair.
  880. *
  881. * Allows for easy lookup with the subscript ( @c [] )operator. Returns
  882. * data associated with the key specified in subscript. If the key does
  883. * not exist, a pair with that key is created using default values, which
  884. * is then returned.
  885. *
  886. * Lookup requires constant time.
  887. */
  888. mapped_type&
  889. operator[](const key_type& __k)
  890. { return _M_h[__k]; }
  891. mapped_type&
  892. operator[](key_type&& __k)
  893. { return _M_h[std::move(__k)]; }
  894. //@}
  895. //@{
  896. /**
  897. * @brief Access to %unordered_map data.
  898. * @param __k The key for which data should be retrieved.
  899. * @return A reference to the data whose key is equal to @a __k, if
  900. * such a data is present in the %unordered_map.
  901. * @throw std::out_of_range If no such data is present.
  902. */
  903. mapped_type&
  904. at(const key_type& __k)
  905. { return _M_h.at(__k); }
  906. const mapped_type&
  907. at(const key_type& __k) const
  908. { return _M_h.at(__k); }
  909. //@}
  910. // bucket interface.
  911. /// Returns the number of buckets of the %unordered_map.
  912. size_type
  913. bucket_count() const noexcept
  914. { return _M_h.bucket_count(); }
  915. /// Returns the maximum number of buckets of the %unordered_map.
  916. size_type
  917. max_bucket_count() const noexcept
  918. { return _M_h.max_bucket_count(); }
  919. /*
  920. * @brief Returns the number of elements in a given bucket.
  921. * @param __n A bucket index.
  922. * @return The number of elements in the bucket.
  923. */
  924. size_type
  925. bucket_size(size_type __n) const
  926. { return _M_h.bucket_size(__n); }
  927. /*
  928. * @brief Returns the bucket index of a given element.
  929. * @param __key A key instance.
  930. * @return The key bucket index.
  931. */
  932. size_type
  933. bucket(const key_type& __key) const
  934. { return _M_h.bucket(__key); }
  935. /**
  936. * @brief Returns a read/write iterator pointing to the first bucket
  937. * element.
  938. * @param __n The bucket index.
  939. * @return A read/write local iterator.
  940. */
  941. local_iterator
  942. begin(size_type __n)
  943. { return _M_h.begin(__n); }
  944. //@{
  945. /**
  946. * @brief Returns a read-only (constant) iterator pointing to the first
  947. * bucket element.
  948. * @param __n The bucket index.
  949. * @return A read-only local iterator.
  950. */
  951. const_local_iterator
  952. begin(size_type __n) const
  953. { return _M_h.begin(__n); }
  954. const_local_iterator
  955. cbegin(size_type __n) const
  956. { return _M_h.cbegin(__n); }
  957. //@}
  958. /**
  959. * @brief Returns a read/write iterator pointing to one past the last
  960. * bucket elements.
  961. * @param __n The bucket index.
  962. * @return A read/write local iterator.
  963. */
  964. local_iterator
  965. end(size_type __n)
  966. { return _M_h.end(__n); }
  967. //@{
  968. /**
  969. * @brief Returns a read-only (constant) iterator pointing to one past
  970. * the last bucket elements.
  971. * @param __n The bucket index.
  972. * @return A read-only local iterator.
  973. */
  974. const_local_iterator
  975. end(size_type __n) const
  976. { return _M_h.end(__n); }
  977. const_local_iterator
  978. cend(size_type __n) const
  979. { return _M_h.cend(__n); }
  980. //@}
  981. // hash policy.
  982. /// Returns the average number of elements per bucket.
  983. float
  984. load_factor() const noexcept
  985. { return _M_h.load_factor(); }
  986. /// Returns a positive number that the %unordered_map tries to keep the
  987. /// load factor less than or equal to.
  988. float
  989. max_load_factor() const noexcept
  990. { return _M_h.max_load_factor(); }
  991. /**
  992. * @brief Change the %unordered_map maximum load factor.
  993. * @param __z The new maximum load factor.
  994. */
  995. void
  996. max_load_factor(float __z)
  997. { _M_h.max_load_factor(__z); }
  998. /**
  999. * @brief May rehash the %unordered_map.
  1000. * @param __n The new number of buckets.
  1001. *
  1002. * Rehash will occur only if the new number of buckets respect the
  1003. * %unordered_map maximum load factor.
  1004. */
  1005. void
  1006. rehash(size_type __n)
  1007. { _M_h.rehash(__n); }
  1008. /**
  1009. * @brief Prepare the %unordered_map for a specified number of
  1010. * elements.
  1011. * @param __n Number of elements required.
  1012. *
  1013. * Same as rehash(ceil(n / max_load_factor())).
  1014. */
  1015. void
  1016. reserve(size_type __n)
  1017. { _M_h.reserve(__n); }
  1018. template<typename _Key1, typename _Tp1, typename _Hash1, typename _Pred1,
  1019. typename _Alloc1>
  1020. friend bool
  1021. operator==(const unordered_map<_Key1, _Tp1, _Hash1, _Pred1, _Alloc1>&,
  1022. const unordered_map<_Key1, _Tp1, _Hash1, _Pred1, _Alloc1>&);
  1023. };
  1024. #if __cpp_deduction_guides >= 201606
  1025. template<typename _InputIterator,
  1026. typename _Hash = hash<__iter_key_t<_InputIterator>>,
  1027. typename _Pred = equal_to<__iter_key_t<_InputIterator>>,
  1028. typename _Allocator = allocator<__iter_to_alloc_t<_InputIterator>>,
  1029. typename = _RequireInputIter<_InputIterator>,
  1030. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  1031. typename = _RequireNotAllocator<_Pred>,
  1032. typename = _RequireAllocator<_Allocator>>
  1033. unordered_map(_InputIterator, _InputIterator,
  1034. typename unordered_map<int, int>::size_type = {},
  1035. _Hash = _Hash(), _Pred = _Pred(), _Allocator = _Allocator())
  1036. -> unordered_map<__iter_key_t<_InputIterator>,
  1037. __iter_val_t<_InputIterator>,
  1038. _Hash, _Pred, _Allocator>;
  1039. template<typename _Key, typename _Tp, typename _Hash = hash<_Key>,
  1040. typename _Pred = equal_to<_Key>,
  1041. typename _Allocator = allocator<pair<const _Key, _Tp>>,
  1042. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  1043. typename = _RequireNotAllocator<_Pred>,
  1044. typename = _RequireAllocator<_Allocator>>
  1045. unordered_map(initializer_list<pair<_Key, _Tp>>,
  1046. typename unordered_map<int, int>::size_type = {},
  1047. _Hash = _Hash(), _Pred = _Pred(), _Allocator = _Allocator())
  1048. -> unordered_map<_Key, _Tp, _Hash, _Pred, _Allocator>;
  1049. template<typename _InputIterator, typename _Allocator,
  1050. typename = _RequireInputIter<_InputIterator>,
  1051. typename = _RequireAllocator<_Allocator>>
  1052. unordered_map(_InputIterator, _InputIterator,
  1053. typename unordered_map<int, int>::size_type, _Allocator)
  1054. -> unordered_map<__iter_key_t<_InputIterator>,
  1055. __iter_val_t<_InputIterator>,
  1056. hash<__iter_key_t<_InputIterator>>,
  1057. equal_to<__iter_key_t<_InputIterator>>,
  1058. _Allocator>;
  1059. template<typename _InputIterator, typename _Allocator,
  1060. typename = _RequireInputIter<_InputIterator>,
  1061. typename = _RequireAllocator<_Allocator>>
  1062. unordered_map(_InputIterator, _InputIterator, _Allocator)
  1063. -> unordered_map<__iter_key_t<_InputIterator>,
  1064. __iter_val_t<_InputIterator>,
  1065. hash<__iter_key_t<_InputIterator>>,
  1066. equal_to<__iter_key_t<_InputIterator>>,
  1067. _Allocator>;
  1068. template<typename _InputIterator, typename _Hash, typename _Allocator,
  1069. typename = _RequireInputIter<_InputIterator>,
  1070. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  1071. typename = _RequireAllocator<_Allocator>>
  1072. unordered_map(_InputIterator, _InputIterator,
  1073. typename unordered_map<int, int>::size_type,
  1074. _Hash, _Allocator)
  1075. -> unordered_map<__iter_key_t<_InputIterator>,
  1076. __iter_val_t<_InputIterator>, _Hash,
  1077. equal_to<__iter_key_t<_InputIterator>>, _Allocator>;
  1078. template<typename _Key, typename _Tp, typename _Allocator,
  1079. typename = _RequireAllocator<_Allocator>>
  1080. unordered_map(initializer_list<pair<_Key, _Tp>>,
  1081. typename unordered_map<int, int>::size_type,
  1082. _Allocator)
  1083. -> unordered_map<_Key, _Tp, hash<_Key>, equal_to<_Key>, _Allocator>;
  1084. template<typename _Key, typename _Tp, typename _Allocator,
  1085. typename = _RequireAllocator<_Allocator>>
  1086. unordered_map(initializer_list<pair<_Key, _Tp>>, _Allocator)
  1087. -> unordered_map<_Key, _Tp, hash<_Key>, equal_to<_Key>, _Allocator>;
  1088. template<typename _Key, typename _Tp, typename _Hash, typename _Allocator,
  1089. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  1090. typename = _RequireAllocator<_Allocator>>
  1091. unordered_map(initializer_list<pair<_Key, _Tp>>,
  1092. typename unordered_map<int, int>::size_type,
  1093. _Hash, _Allocator)
  1094. -> unordered_map<_Key, _Tp, _Hash, equal_to<_Key>, _Allocator>;
  1095. #endif
  1096. /**
  1097. * @brief A standard container composed of equivalent keys
  1098. * (possibly containing multiple of each key value) that associates
  1099. * values of another type with the keys.
  1100. *
  1101. * @ingroup unordered_associative_containers
  1102. *
  1103. * @tparam _Key Type of key objects.
  1104. * @tparam _Tp Type of mapped objects.
  1105. * @tparam _Hash Hashing function object type, defaults to hash<_Value>.
  1106. * @tparam _Pred Predicate function object type, defaults
  1107. * to equal_to<_Value>.
  1108. * @tparam _Alloc Allocator type, defaults to
  1109. * std::allocator<std::pair<const _Key, _Tp>>.
  1110. *
  1111. * Meets the requirements of a <a href="tables.html#65">container</a>, and
  1112. * <a href="tables.html#xx">unordered associative container</a>
  1113. *
  1114. * The resulting value type of the container is std::pair<const _Key, _Tp>.
  1115. *
  1116. * Base is _Hashtable, dispatched at compile time via template
  1117. * alias __ummap_hashtable.
  1118. */
  1119. template<typename _Key, typename _Tp,
  1120. typename _Hash = hash<_Key>,
  1121. typename _Pred = equal_to<_Key>,
  1122. typename _Alloc = allocator<std::pair<const _Key, _Tp>>>
  1123. class unordered_multimap
  1124. {
  1125. typedef __ummap_hashtable<_Key, _Tp, _Hash, _Pred, _Alloc> _Hashtable;
  1126. _Hashtable _M_h;
  1127. public:
  1128. // typedefs:
  1129. //@{
  1130. /// Public typedefs.
  1131. typedef typename _Hashtable::key_type key_type;
  1132. typedef typename _Hashtable::value_type value_type;
  1133. typedef typename _Hashtable::mapped_type mapped_type;
  1134. typedef typename _Hashtable::hasher hasher;
  1135. typedef typename _Hashtable::key_equal key_equal;
  1136. typedef typename _Hashtable::allocator_type allocator_type;
  1137. //@}
  1138. //@{
  1139. /// Iterator-related typedefs.
  1140. typedef typename _Hashtable::pointer pointer;
  1141. typedef typename _Hashtable::const_pointer const_pointer;
  1142. typedef typename _Hashtable::reference reference;
  1143. typedef typename _Hashtable::const_reference const_reference;
  1144. typedef typename _Hashtable::iterator iterator;
  1145. typedef typename _Hashtable::const_iterator const_iterator;
  1146. typedef typename _Hashtable::local_iterator local_iterator;
  1147. typedef typename _Hashtable::const_local_iterator const_local_iterator;
  1148. typedef typename _Hashtable::size_type size_type;
  1149. typedef typename _Hashtable::difference_type difference_type;
  1150. //@}
  1151. #if __cplusplus > 201402L
  1152. using node_type = typename _Hashtable::node_type;
  1153. #endif
  1154. //construct/destroy/copy
  1155. /// Default constructor.
  1156. unordered_multimap() = default;
  1157. /**
  1158. * @brief Default constructor creates no elements.
  1159. * @param __n Mnimal initial number of buckets.
  1160. * @param __hf A hash functor.
  1161. * @param __eql A key equality functor.
  1162. * @param __a An allocator object.
  1163. */
  1164. explicit
  1165. unordered_multimap(size_type __n,
  1166. const hasher& __hf = hasher(),
  1167. const key_equal& __eql = key_equal(),
  1168. const allocator_type& __a = allocator_type())
  1169. : _M_h(__n, __hf, __eql, __a)
  1170. { }
  1171. /**
  1172. * @brief Builds an %unordered_multimap from a range.
  1173. * @param __first An input iterator.
  1174. * @param __last An input iterator.
  1175. * @param __n Minimal initial number of buckets.
  1176. * @param __hf A hash functor.
  1177. * @param __eql A key equality functor.
  1178. * @param __a An allocator object.
  1179. *
  1180. * Create an %unordered_multimap consisting of copies of the elements
  1181. * from [__first,__last). This is linear in N (where N is
  1182. * distance(__first,__last)).
  1183. */
  1184. template<typename _InputIterator>
  1185. unordered_multimap(_InputIterator __first, _InputIterator __last,
  1186. size_type __n = 0,
  1187. const hasher& __hf = hasher(),
  1188. const key_equal& __eql = key_equal(),
  1189. const allocator_type& __a = allocator_type())
  1190. : _M_h(__first, __last, __n, __hf, __eql, __a)
  1191. { }
  1192. /// Copy constructor.
  1193. unordered_multimap(const unordered_multimap&) = default;
  1194. /// Move constructor.
  1195. unordered_multimap(unordered_multimap&&) = default;
  1196. /**
  1197. * @brief Creates an %unordered_multimap with no elements.
  1198. * @param __a An allocator object.
  1199. */
  1200. explicit
  1201. unordered_multimap(const allocator_type& __a)
  1202. : _M_h(__a)
  1203. { }
  1204. /*
  1205. * @brief Copy constructor with allocator argument.
  1206. * @param __uset Input %unordered_multimap to copy.
  1207. * @param __a An allocator object.
  1208. */
  1209. unordered_multimap(const unordered_multimap& __ummap,
  1210. const allocator_type& __a)
  1211. : _M_h(__ummap._M_h, __a)
  1212. { }
  1213. /*
  1214. * @brief Move constructor with allocator argument.
  1215. * @param __uset Input %unordered_multimap to move.
  1216. * @param __a An allocator object.
  1217. */
  1218. unordered_multimap(unordered_multimap&& __ummap,
  1219. const allocator_type& __a)
  1220. : _M_h(std::move(__ummap._M_h), __a)
  1221. { }
  1222. /**
  1223. * @brief Builds an %unordered_multimap from an initializer_list.
  1224. * @param __l An initializer_list.
  1225. * @param __n Minimal initial number of buckets.
  1226. * @param __hf A hash functor.
  1227. * @param __eql A key equality functor.
  1228. * @param __a An allocator object.
  1229. *
  1230. * Create an %unordered_multimap consisting of copies of the elements in
  1231. * the list. This is linear in N (where N is @a __l.size()).
  1232. */
  1233. unordered_multimap(initializer_list<value_type> __l,
  1234. size_type __n = 0,
  1235. const hasher& __hf = hasher(),
  1236. const key_equal& __eql = key_equal(),
  1237. const allocator_type& __a = allocator_type())
  1238. : _M_h(__l, __n, __hf, __eql, __a)
  1239. { }
  1240. unordered_multimap(size_type __n, const allocator_type& __a)
  1241. : unordered_multimap(__n, hasher(), key_equal(), __a)
  1242. { }
  1243. unordered_multimap(size_type __n, const hasher& __hf,
  1244. const allocator_type& __a)
  1245. : unordered_multimap(__n, __hf, key_equal(), __a)
  1246. { }
  1247. template<typename _InputIterator>
  1248. unordered_multimap(_InputIterator __first, _InputIterator __last,
  1249. size_type __n,
  1250. const allocator_type& __a)
  1251. : unordered_multimap(__first, __last, __n, hasher(), key_equal(), __a)
  1252. { }
  1253. template<typename _InputIterator>
  1254. unordered_multimap(_InputIterator __first, _InputIterator __last,
  1255. size_type __n, const hasher& __hf,
  1256. const allocator_type& __a)
  1257. : unordered_multimap(__first, __last, __n, __hf, key_equal(), __a)
  1258. { }
  1259. unordered_multimap(initializer_list<value_type> __l,
  1260. size_type __n,
  1261. const allocator_type& __a)
  1262. : unordered_multimap(__l, __n, hasher(), key_equal(), __a)
  1263. { }
  1264. unordered_multimap(initializer_list<value_type> __l,
  1265. size_type __n, const hasher& __hf,
  1266. const allocator_type& __a)
  1267. : unordered_multimap(__l, __n, __hf, key_equal(), __a)
  1268. { }
  1269. /// Copy assignment operator.
  1270. unordered_multimap&
  1271. operator=(const unordered_multimap&) = default;
  1272. /// Move assignment operator.
  1273. unordered_multimap&
  1274. operator=(unordered_multimap&&) = default;
  1275. /**
  1276. * @brief %Unordered_multimap list assignment operator.
  1277. * @param __l An initializer_list.
  1278. *
  1279. * This function fills an %unordered_multimap with copies of the
  1280. * elements in the initializer list @a __l.
  1281. *
  1282. * Note that the assignment completely changes the %unordered_multimap
  1283. * and that the resulting %unordered_multimap's size is the same as the
  1284. * number of elements assigned.
  1285. */
  1286. unordered_multimap&
  1287. operator=(initializer_list<value_type> __l)
  1288. {
  1289. _M_h = __l;
  1290. return *this;
  1291. }
  1292. /// Returns the allocator object used by the %unordered_multimap.
  1293. allocator_type
  1294. get_allocator() const noexcept
  1295. { return _M_h.get_allocator(); }
  1296. // size and capacity:
  1297. /// Returns true if the %unordered_multimap is empty.
  1298. _GLIBCXX_NODISCARD bool
  1299. empty() const noexcept
  1300. { return _M_h.empty(); }
  1301. /// Returns the size of the %unordered_multimap.
  1302. size_type
  1303. size() const noexcept
  1304. { return _M_h.size(); }
  1305. /// Returns the maximum size of the %unordered_multimap.
  1306. size_type
  1307. max_size() const noexcept
  1308. { return _M_h.max_size(); }
  1309. // iterators.
  1310. /**
  1311. * Returns a read/write iterator that points to the first element in the
  1312. * %unordered_multimap.
  1313. */
  1314. iterator
  1315. begin() noexcept
  1316. { return _M_h.begin(); }
  1317. //@{
  1318. /**
  1319. * Returns a read-only (constant) iterator that points to the first
  1320. * element in the %unordered_multimap.
  1321. */
  1322. const_iterator
  1323. begin() const noexcept
  1324. { return _M_h.begin(); }
  1325. const_iterator
  1326. cbegin() const noexcept
  1327. { return _M_h.begin(); }
  1328. //@}
  1329. /**
  1330. * Returns a read/write iterator that points one past the last element in
  1331. * the %unordered_multimap.
  1332. */
  1333. iterator
  1334. end() noexcept
  1335. { return _M_h.end(); }
  1336. //@{
  1337. /**
  1338. * Returns a read-only (constant) iterator that points one past the last
  1339. * element in the %unordered_multimap.
  1340. */
  1341. const_iterator
  1342. end() const noexcept
  1343. { return _M_h.end(); }
  1344. const_iterator
  1345. cend() const noexcept
  1346. { return _M_h.end(); }
  1347. //@}
  1348. // modifiers.
  1349. /**
  1350. * @brief Attempts to build and insert a std::pair into the
  1351. * %unordered_multimap.
  1352. *
  1353. * @param __args Arguments used to generate a new pair instance (see
  1354. * std::piecewise_contruct for passing arguments to each
  1355. * part of the pair constructor).
  1356. *
  1357. * @return An iterator that points to the inserted pair.
  1358. *
  1359. * This function attempts to build and insert a (key, value) %pair into
  1360. * the %unordered_multimap.
  1361. *
  1362. * Insertion requires amortized constant time.
  1363. */
  1364. template<typename... _Args>
  1365. iterator
  1366. emplace(_Args&&... __args)
  1367. { return _M_h.emplace(std::forward<_Args>(__args)...); }
  1368. /**
  1369. * @brief Attempts to build and insert a std::pair into the
  1370. * %unordered_multimap.
  1371. *
  1372. * @param __pos An iterator that serves as a hint as to where the pair
  1373. * should be inserted.
  1374. * @param __args Arguments used to generate a new pair instance (see
  1375. * std::piecewise_contruct for passing arguments to each
  1376. * part of the pair constructor).
  1377. * @return An iterator that points to the element with key of the
  1378. * std::pair built from @a __args.
  1379. *
  1380. * Note that the first parameter is only a hint and can potentially
  1381. * improve the performance of the insertion process. A bad hint would
  1382. * cause no gains in efficiency.
  1383. *
  1384. * See
  1385. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
  1386. * for more on @a hinting.
  1387. *
  1388. * Insertion requires amortized constant time.
  1389. */
  1390. template<typename... _Args>
  1391. iterator
  1392. emplace_hint(const_iterator __pos, _Args&&... __args)
  1393. { return _M_h.emplace_hint(__pos, std::forward<_Args>(__args)...); }
  1394. //@{
  1395. /**
  1396. * @brief Inserts a std::pair into the %unordered_multimap.
  1397. * @param __x Pair to be inserted (see std::make_pair for easy
  1398. * creation of pairs).
  1399. *
  1400. * @return An iterator that points to the inserted pair.
  1401. *
  1402. * Insertion requires amortized constant time.
  1403. */
  1404. iterator
  1405. insert(const value_type& __x)
  1406. { return _M_h.insert(__x); }
  1407. iterator
  1408. insert(value_type&& __x)
  1409. { return _M_h.insert(std::move(__x)); }
  1410. template<typename _Pair>
  1411. __enable_if_t<is_constructible<value_type, _Pair&&>::value, iterator>
  1412. insert(_Pair&& __x)
  1413. { return _M_h.emplace(std::forward<_Pair>(__x)); }
  1414. //@}
  1415. //@{
  1416. /**
  1417. * @brief Inserts a std::pair into the %unordered_multimap.
  1418. * @param __hint An iterator that serves as a hint as to where the
  1419. * pair should be inserted.
  1420. * @param __x Pair to be inserted (see std::make_pair for easy creation
  1421. * of pairs).
  1422. * @return An iterator that points to the element with key of
  1423. * @a __x (may or may not be the %pair passed in).
  1424. *
  1425. * Note that the first parameter is only a hint and can potentially
  1426. * improve the performance of the insertion process. A bad hint would
  1427. * cause no gains in efficiency.
  1428. *
  1429. * See
  1430. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
  1431. * for more on @a hinting.
  1432. *
  1433. * Insertion requires amortized constant time.
  1434. */
  1435. iterator
  1436. insert(const_iterator __hint, const value_type& __x)
  1437. { return _M_h.insert(__hint, __x); }
  1438. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1439. // 2354. Unnecessary copying when inserting into maps with braced-init
  1440. iterator
  1441. insert(const_iterator __hint, value_type&& __x)
  1442. { return _M_h.insert(__hint, std::move(__x)); }
  1443. template<typename _Pair>
  1444. __enable_if_t<is_constructible<value_type, _Pair&&>::value, iterator>
  1445. insert(const_iterator __hint, _Pair&& __x)
  1446. { return _M_h.emplace_hint(__hint, std::forward<_Pair>(__x)); }
  1447. //@}
  1448. /**
  1449. * @brief A template function that attempts to insert a range of
  1450. * elements.
  1451. * @param __first Iterator pointing to the start of the range to be
  1452. * inserted.
  1453. * @param __last Iterator pointing to the end of the range.
  1454. *
  1455. * Complexity similar to that of the range constructor.
  1456. */
  1457. template<typename _InputIterator>
  1458. void
  1459. insert(_InputIterator __first, _InputIterator __last)
  1460. { _M_h.insert(__first, __last); }
  1461. /**
  1462. * @brief Attempts to insert a list of elements into the
  1463. * %unordered_multimap.
  1464. * @param __l A std::initializer_list<value_type> of elements
  1465. * to be inserted.
  1466. *
  1467. * Complexity similar to that of the range constructor.
  1468. */
  1469. void
  1470. insert(initializer_list<value_type> __l)
  1471. { _M_h.insert(__l); }
  1472. #if __cplusplus > 201402L
  1473. /// Extract a node.
  1474. node_type
  1475. extract(const_iterator __pos)
  1476. {
  1477. __glibcxx_assert(__pos != end());
  1478. return _M_h.extract(__pos);
  1479. }
  1480. /// Extract a node.
  1481. node_type
  1482. extract(const key_type& __key)
  1483. { return _M_h.extract(__key); }
  1484. /// Re-insert an extracted node.
  1485. iterator
  1486. insert(node_type&& __nh)
  1487. { return _M_h._M_reinsert_node_multi(cend(), std::move(__nh)); }
  1488. /// Re-insert an extracted node.
  1489. iterator
  1490. insert(const_iterator __hint, node_type&& __nh)
  1491. { return _M_h._M_reinsert_node_multi(__hint, std::move(__nh)); }
  1492. #endif // C++17
  1493. //@{
  1494. /**
  1495. * @brief Erases an element from an %unordered_multimap.
  1496. * @param __position An iterator pointing to the element to be erased.
  1497. * @return An iterator pointing to the element immediately following
  1498. * @a __position prior to the element being erased. If no such
  1499. * element exists, end() is returned.
  1500. *
  1501. * This function erases an element, pointed to by the given iterator,
  1502. * from an %unordered_multimap.
  1503. * Note that this function only erases the element, and that if the
  1504. * element is itself a pointer, the pointed-to memory is not touched in
  1505. * any way. Managing the pointer is the user's responsibility.
  1506. */
  1507. iterator
  1508. erase(const_iterator __position)
  1509. { return _M_h.erase(__position); }
  1510. // LWG 2059.
  1511. iterator
  1512. erase(iterator __position)
  1513. { return _M_h.erase(__position); }
  1514. //@}
  1515. /**
  1516. * @brief Erases elements according to the provided key.
  1517. * @param __x Key of elements to be erased.
  1518. * @return The number of elements erased.
  1519. *
  1520. * This function erases all the elements located by the given key from
  1521. * an %unordered_multimap.
  1522. * Note that this function only erases the element, and that if the
  1523. * element is itself a pointer, the pointed-to memory is not touched in
  1524. * any way. Managing the pointer is the user's responsibility.
  1525. */
  1526. size_type
  1527. erase(const key_type& __x)
  1528. { return _M_h.erase(__x); }
  1529. /**
  1530. * @brief Erases a [__first,__last) range of elements from an
  1531. * %unordered_multimap.
  1532. * @param __first Iterator pointing to the start of the range to be
  1533. * erased.
  1534. * @param __last Iterator pointing to the end of the range to
  1535. * be erased.
  1536. * @return The iterator @a __last.
  1537. *
  1538. * This function erases a sequence of elements from an
  1539. * %unordered_multimap.
  1540. * Note that this function only erases the elements, and that if
  1541. * the element is itself a pointer, the pointed-to memory is not touched
  1542. * in any way. Managing the pointer is the user's responsibility.
  1543. */
  1544. iterator
  1545. erase(const_iterator __first, const_iterator __last)
  1546. { return _M_h.erase(__first, __last); }
  1547. /**
  1548. * Erases all elements in an %unordered_multimap.
  1549. * Note that this function only erases the elements, and that if the
  1550. * elements themselves are pointers, the pointed-to memory is not touched
  1551. * in any way. Managing the pointer is the user's responsibility.
  1552. */
  1553. void
  1554. clear() noexcept
  1555. { _M_h.clear(); }
  1556. /**
  1557. * @brief Swaps data with another %unordered_multimap.
  1558. * @param __x An %unordered_multimap of the same element and allocator
  1559. * types.
  1560. *
  1561. * This exchanges the elements between two %unordered_multimap in
  1562. * constant time.
  1563. * Note that the global std::swap() function is specialized such that
  1564. * std::swap(m1,m2) will feed to this function.
  1565. */
  1566. void
  1567. swap(unordered_multimap& __x)
  1568. noexcept( noexcept(_M_h.swap(__x._M_h)) )
  1569. { _M_h.swap(__x._M_h); }
  1570. #if __cplusplus > 201402L
  1571. template<typename, typename, typename>
  1572. friend class std::_Hash_merge_helper;
  1573. template<typename _H2, typename _P2>
  1574. void
  1575. merge(unordered_multimap<_Key, _Tp, _H2, _P2, _Alloc>& __source)
  1576. {
  1577. using _Merge_helper
  1578. = _Hash_merge_helper<unordered_multimap, _H2, _P2>;
  1579. _M_h._M_merge_multi(_Merge_helper::_S_get_table(__source));
  1580. }
  1581. template<typename _H2, typename _P2>
  1582. void
  1583. merge(unordered_multimap<_Key, _Tp, _H2, _P2, _Alloc>&& __source)
  1584. { merge(__source); }
  1585. template<typename _H2, typename _P2>
  1586. void
  1587. merge(unordered_map<_Key, _Tp, _H2, _P2, _Alloc>& __source)
  1588. {
  1589. using _Merge_helper
  1590. = _Hash_merge_helper<unordered_multimap, _H2, _P2>;
  1591. _M_h._M_merge_multi(_Merge_helper::_S_get_table(__source));
  1592. }
  1593. template<typename _H2, typename _P2>
  1594. void
  1595. merge(unordered_map<_Key, _Tp, _H2, _P2, _Alloc>&& __source)
  1596. { merge(__source); }
  1597. #endif // C++17
  1598. // observers.
  1599. /// Returns the hash functor object with which the %unordered_multimap
  1600. /// was constructed.
  1601. hasher
  1602. hash_function() const
  1603. { return _M_h.hash_function(); }
  1604. /// Returns the key comparison object with which the %unordered_multimap
  1605. /// was constructed.
  1606. key_equal
  1607. key_eq() const
  1608. { return _M_h.key_eq(); }
  1609. // lookup.
  1610. //@{
  1611. /**
  1612. * @brief Tries to locate an element in an %unordered_multimap.
  1613. * @param __x Key to be located.
  1614. * @return Iterator pointing to sought-after element, or end() if not
  1615. * found.
  1616. *
  1617. * This function takes a key and tries to locate the element with which
  1618. * the key matches. If successful the function returns an iterator
  1619. * pointing to the sought after element. If unsuccessful it returns the
  1620. * past-the-end ( @c end() ) iterator.
  1621. */
  1622. iterator
  1623. find(const key_type& __x)
  1624. { return _M_h.find(__x); }
  1625. const_iterator
  1626. find(const key_type& __x) const
  1627. { return _M_h.find(__x); }
  1628. //@}
  1629. /**
  1630. * @brief Finds the number of elements.
  1631. * @param __x Key to count.
  1632. * @return Number of elements with specified key.
  1633. */
  1634. size_type
  1635. count(const key_type& __x) const
  1636. { return _M_h.count(__x); }
  1637. #if __cplusplus > 201703L
  1638. /**
  1639. * @brief Finds whether an element with the given key exists.
  1640. * @param __x Key of elements to be located.
  1641. * @return True if there is any element with the specified key.
  1642. */
  1643. bool
  1644. contains(const key_type& __x) const
  1645. { return _M_h.find(__x) != _M_h.end(); }
  1646. #endif
  1647. //@{
  1648. /**
  1649. * @brief Finds a subsequence matching given key.
  1650. * @param __x Key to be located.
  1651. * @return Pair of iterators that possibly points to the subsequence
  1652. * matching given key.
  1653. */
  1654. std::pair<iterator, iterator>
  1655. equal_range(const key_type& __x)
  1656. { return _M_h.equal_range(__x); }
  1657. std::pair<const_iterator, const_iterator>
  1658. equal_range(const key_type& __x) const
  1659. { return _M_h.equal_range(__x); }
  1660. //@}
  1661. // bucket interface.
  1662. /// Returns the number of buckets of the %unordered_multimap.
  1663. size_type
  1664. bucket_count() const noexcept
  1665. { return _M_h.bucket_count(); }
  1666. /// Returns the maximum number of buckets of the %unordered_multimap.
  1667. size_type
  1668. max_bucket_count() const noexcept
  1669. { return _M_h.max_bucket_count(); }
  1670. /*
  1671. * @brief Returns the number of elements in a given bucket.
  1672. * @param __n A bucket index.
  1673. * @return The number of elements in the bucket.
  1674. */
  1675. size_type
  1676. bucket_size(size_type __n) const
  1677. { return _M_h.bucket_size(__n); }
  1678. /*
  1679. * @brief Returns the bucket index of a given element.
  1680. * @param __key A key instance.
  1681. * @return The key bucket index.
  1682. */
  1683. size_type
  1684. bucket(const key_type& __key) const
  1685. { return _M_h.bucket(__key); }
  1686. /**
  1687. * @brief Returns a read/write iterator pointing to the first bucket
  1688. * element.
  1689. * @param __n The bucket index.
  1690. * @return A read/write local iterator.
  1691. */
  1692. local_iterator
  1693. begin(size_type __n)
  1694. { return _M_h.begin(__n); }
  1695. //@{
  1696. /**
  1697. * @brief Returns a read-only (constant) iterator pointing to the first
  1698. * bucket element.
  1699. * @param __n The bucket index.
  1700. * @return A read-only local iterator.
  1701. */
  1702. const_local_iterator
  1703. begin(size_type __n) const
  1704. { return _M_h.begin(__n); }
  1705. const_local_iterator
  1706. cbegin(size_type __n) const
  1707. { return _M_h.cbegin(__n); }
  1708. //@}
  1709. /**
  1710. * @brief Returns a read/write iterator pointing to one past the last
  1711. * bucket elements.
  1712. * @param __n The bucket index.
  1713. * @return A read/write local iterator.
  1714. */
  1715. local_iterator
  1716. end(size_type __n)
  1717. { return _M_h.end(__n); }
  1718. //@{
  1719. /**
  1720. * @brief Returns a read-only (constant) iterator pointing to one past
  1721. * the last bucket elements.
  1722. * @param __n The bucket index.
  1723. * @return A read-only local iterator.
  1724. */
  1725. const_local_iterator
  1726. end(size_type __n) const
  1727. { return _M_h.end(__n); }
  1728. const_local_iterator
  1729. cend(size_type __n) const
  1730. { return _M_h.cend(__n); }
  1731. //@}
  1732. // hash policy.
  1733. /// Returns the average number of elements per bucket.
  1734. float
  1735. load_factor() const noexcept
  1736. { return _M_h.load_factor(); }
  1737. /// Returns a positive number that the %unordered_multimap tries to keep
  1738. /// the load factor less than or equal to.
  1739. float
  1740. max_load_factor() const noexcept
  1741. { return _M_h.max_load_factor(); }
  1742. /**
  1743. * @brief Change the %unordered_multimap maximum load factor.
  1744. * @param __z The new maximum load factor.
  1745. */
  1746. void
  1747. max_load_factor(float __z)
  1748. { _M_h.max_load_factor(__z); }
  1749. /**
  1750. * @brief May rehash the %unordered_multimap.
  1751. * @param __n The new number of buckets.
  1752. *
  1753. * Rehash will occur only if the new number of buckets respect the
  1754. * %unordered_multimap maximum load factor.
  1755. */
  1756. void
  1757. rehash(size_type __n)
  1758. { _M_h.rehash(__n); }
  1759. /**
  1760. * @brief Prepare the %unordered_multimap for a specified number of
  1761. * elements.
  1762. * @param __n Number of elements required.
  1763. *
  1764. * Same as rehash(ceil(n / max_load_factor())).
  1765. */
  1766. void
  1767. reserve(size_type __n)
  1768. { _M_h.reserve(__n); }
  1769. template<typename _Key1, typename _Tp1, typename _Hash1, typename _Pred1,
  1770. typename _Alloc1>
  1771. friend bool
  1772. operator==(const unordered_multimap<_Key1, _Tp1,
  1773. _Hash1, _Pred1, _Alloc1>&,
  1774. const unordered_multimap<_Key1, _Tp1,
  1775. _Hash1, _Pred1, _Alloc1>&);
  1776. };
  1777. #if __cpp_deduction_guides >= 201606
  1778. template<typename _InputIterator,
  1779. typename _Hash = hash<__iter_key_t<_InputIterator>>,
  1780. typename _Pred = equal_to<__iter_key_t<_InputIterator>>,
  1781. typename _Allocator = allocator<__iter_to_alloc_t<_InputIterator>>,
  1782. typename = _RequireInputIter<_InputIterator>,
  1783. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  1784. typename = _RequireNotAllocator<_Pred>,
  1785. typename = _RequireAllocator<_Allocator>>
  1786. unordered_multimap(_InputIterator, _InputIterator,
  1787. unordered_multimap<int, int>::size_type = {},
  1788. _Hash = _Hash(), _Pred = _Pred(),
  1789. _Allocator = _Allocator())
  1790. -> unordered_multimap<__iter_key_t<_InputIterator>,
  1791. __iter_val_t<_InputIterator>, _Hash, _Pred,
  1792. _Allocator>;
  1793. template<typename _Key, typename _Tp, typename _Hash = hash<_Key>,
  1794. typename _Pred = equal_to<_Key>,
  1795. typename _Allocator = allocator<pair<const _Key, _Tp>>,
  1796. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  1797. typename = _RequireNotAllocator<_Pred>,
  1798. typename = _RequireAllocator<_Allocator>>
  1799. unordered_multimap(initializer_list<pair<_Key, _Tp>>,
  1800. unordered_multimap<int, int>::size_type = {},
  1801. _Hash = _Hash(), _Pred = _Pred(),
  1802. _Allocator = _Allocator())
  1803. -> unordered_multimap<_Key, _Tp, _Hash, _Pred, _Allocator>;
  1804. template<typename _InputIterator, typename _Allocator,
  1805. typename = _RequireInputIter<_InputIterator>,
  1806. typename = _RequireAllocator<_Allocator>>
  1807. unordered_multimap(_InputIterator, _InputIterator,
  1808. unordered_multimap<int, int>::size_type, _Allocator)
  1809. -> unordered_multimap<__iter_key_t<_InputIterator>,
  1810. __iter_val_t<_InputIterator>,
  1811. hash<__iter_key_t<_InputIterator>>,
  1812. equal_to<__iter_key_t<_InputIterator>>, _Allocator>;
  1813. template<typename _InputIterator, typename _Allocator,
  1814. typename = _RequireInputIter<_InputIterator>,
  1815. typename = _RequireAllocator<_Allocator>>
  1816. unordered_multimap(_InputIterator, _InputIterator, _Allocator)
  1817. -> unordered_multimap<__iter_key_t<_InputIterator>,
  1818. __iter_val_t<_InputIterator>,
  1819. hash<__iter_key_t<_InputIterator>>,
  1820. equal_to<__iter_key_t<_InputIterator>>, _Allocator>;
  1821. template<typename _InputIterator, typename _Hash, typename _Allocator,
  1822. typename = _RequireInputIter<_InputIterator>,
  1823. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  1824. typename = _RequireAllocator<_Allocator>>
  1825. unordered_multimap(_InputIterator, _InputIterator,
  1826. unordered_multimap<int, int>::size_type, _Hash,
  1827. _Allocator)
  1828. -> unordered_multimap<__iter_key_t<_InputIterator>,
  1829. __iter_val_t<_InputIterator>, _Hash,
  1830. equal_to<__iter_key_t<_InputIterator>>, _Allocator>;
  1831. template<typename _Key, typename _Tp, typename _Allocator,
  1832. typename = _RequireAllocator<_Allocator>>
  1833. unordered_multimap(initializer_list<pair<_Key, _Tp>>,
  1834. unordered_multimap<int, int>::size_type,
  1835. _Allocator)
  1836. -> unordered_multimap<_Key, _Tp, hash<_Key>, equal_to<_Key>, _Allocator>;
  1837. template<typename _Key, typename _Tp, typename _Allocator,
  1838. typename = _RequireAllocator<_Allocator>>
  1839. unordered_multimap(initializer_list<pair<_Key, _Tp>>, _Allocator)
  1840. -> unordered_multimap<_Key, _Tp, hash<_Key>, equal_to<_Key>, _Allocator>;
  1841. template<typename _Key, typename _Tp, typename _Hash, typename _Allocator,
  1842. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  1843. typename = _RequireAllocator<_Allocator>>
  1844. unordered_multimap(initializer_list<pair<_Key, _Tp>>,
  1845. unordered_multimap<int, int>::size_type,
  1846. _Hash, _Allocator)
  1847. -> unordered_multimap<_Key, _Tp, _Hash, equal_to<_Key>, _Allocator>;
  1848. #endif
  1849. template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  1850. inline void
  1851. swap(unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  1852. unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  1853. noexcept(noexcept(__x.swap(__y)))
  1854. { __x.swap(__y); }
  1855. template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  1856. inline void
  1857. swap(unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  1858. unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  1859. noexcept(noexcept(__x.swap(__y)))
  1860. { __x.swap(__y); }
  1861. template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  1862. inline bool
  1863. operator==(const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  1864. const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  1865. { return __x._M_h._M_equal(__y._M_h); }
  1866. #if __cpp_impl_three_way_comparison < 201907L
  1867. template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  1868. inline bool
  1869. operator!=(const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  1870. const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  1871. { return !(__x == __y); }
  1872. #endif
  1873. template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  1874. inline bool
  1875. operator==(const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  1876. const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  1877. { return __x._M_h._M_equal(__y._M_h); }
  1878. #if __cpp_impl_three_way_comparison < 201907L
  1879. template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
  1880. inline bool
  1881. operator!=(const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
  1882. const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
  1883. { return !(__x == __y); }
  1884. #endif
  1885. _GLIBCXX_END_NAMESPACE_CONTAINER
  1886. #if __cplusplus > 201402L
  1887. // Allow std::unordered_map access to internals of compatible maps.
  1888. template<typename _Key, typename _Val, typename _Hash1, typename _Eq1,
  1889. typename _Alloc, typename _Hash2, typename _Eq2>
  1890. struct _Hash_merge_helper<
  1891. _GLIBCXX_STD_C::unordered_map<_Key, _Val, _Hash1, _Eq1, _Alloc>,
  1892. _Hash2, _Eq2>
  1893. {
  1894. private:
  1895. template<typename... _Tp>
  1896. using unordered_map = _GLIBCXX_STD_C::unordered_map<_Tp...>;
  1897. template<typename... _Tp>
  1898. using unordered_multimap = _GLIBCXX_STD_C::unordered_multimap<_Tp...>;
  1899. friend unordered_map<_Key, _Val, _Hash1, _Eq1, _Alloc>;
  1900. static auto&
  1901. _S_get_table(unordered_map<_Key, _Val, _Hash2, _Eq2, _Alloc>& __map)
  1902. { return __map._M_h; }
  1903. static auto&
  1904. _S_get_table(unordered_multimap<_Key, _Val, _Hash2, _Eq2, _Alloc>& __map)
  1905. { return __map._M_h; }
  1906. };
  1907. // Allow std::unordered_multimap access to internals of compatible maps.
  1908. template<typename _Key, typename _Val, typename _Hash1, typename _Eq1,
  1909. typename _Alloc, typename _Hash2, typename _Eq2>
  1910. struct _Hash_merge_helper<
  1911. _GLIBCXX_STD_C::unordered_multimap<_Key, _Val, _Hash1, _Eq1, _Alloc>,
  1912. _Hash2, _Eq2>
  1913. {
  1914. private:
  1915. template<typename... _Tp>
  1916. using unordered_map = _GLIBCXX_STD_C::unordered_map<_Tp...>;
  1917. template<typename... _Tp>
  1918. using unordered_multimap = _GLIBCXX_STD_C::unordered_multimap<_Tp...>;
  1919. friend unordered_multimap<_Key, _Val, _Hash1, _Eq1, _Alloc>;
  1920. static auto&
  1921. _S_get_table(unordered_map<_Key, _Val, _Hash2, _Eq2, _Alloc>& __map)
  1922. { return __map._M_h; }
  1923. static auto&
  1924. _S_get_table(unordered_multimap<_Key, _Val, _Hash2, _Eq2, _Alloc>& __map)
  1925. { return __map._M_h; }
  1926. };
  1927. #endif // C++17
  1928. _GLIBCXX_END_NAMESPACE_VERSION
  1929. } // namespace std
  1930. #endif /* _UNORDERED_MAP_H */