unordered_set.h 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783
  1. // unordered_set 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_set.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{unordered_set}
  23. */
  24. #ifndef _UNORDERED_SET_H
  25. #define _UNORDERED_SET_H
  26. namespace std _GLIBCXX_VISIBILITY(default)
  27. {
  28. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  29. _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
  30. /// Base types for unordered_set.
  31. template<bool _Cache>
  32. using __uset_traits = __detail::_Hashtable_traits<_Cache, true, true>;
  33. template<typename _Value,
  34. typename _Hash = hash<_Value>,
  35. typename _Pred = std::equal_to<_Value>,
  36. typename _Alloc = std::allocator<_Value>,
  37. typename _Tr = __uset_traits<__cache_default<_Value, _Hash>::value>>
  38. using __uset_hashtable = _Hashtable<_Value, _Value, _Alloc,
  39. __detail::_Identity, _Pred, _Hash,
  40. __detail::_Mod_range_hashing,
  41. __detail::_Default_ranged_hash,
  42. __detail::_Prime_rehash_policy, _Tr>;
  43. /// Base types for unordered_multiset.
  44. template<bool _Cache>
  45. using __umset_traits = __detail::_Hashtable_traits<_Cache, true, false>;
  46. template<typename _Value,
  47. typename _Hash = hash<_Value>,
  48. typename _Pred = std::equal_to<_Value>,
  49. typename _Alloc = std::allocator<_Value>,
  50. typename _Tr = __umset_traits<__cache_default<_Value, _Hash>::value>>
  51. using __umset_hashtable = _Hashtable<_Value, _Value, _Alloc,
  52. __detail::_Identity,
  53. _Pred, _Hash,
  54. __detail::_Mod_range_hashing,
  55. __detail::_Default_ranged_hash,
  56. __detail::_Prime_rehash_policy, _Tr>;
  57. template<class _Value, class _Hash, class _Pred, class _Alloc>
  58. class unordered_multiset;
  59. /**
  60. * @brief A standard container composed of unique keys (containing
  61. * at most one of each key value) in which the elements' keys are
  62. * the elements themselves.
  63. *
  64. * @ingroup unordered_associative_containers
  65. *
  66. * @tparam _Value Type of key objects.
  67. * @tparam _Hash Hashing function object type, defaults to hash<_Value>.
  68. * @tparam _Pred Predicate function object type, defaults to
  69. * equal_to<_Value>.
  70. *
  71. * @tparam _Alloc Allocator type, defaults to allocator<_Key>.
  72. *
  73. * Meets the requirements of a <a href="tables.html#65">container</a>, and
  74. * <a href="tables.html#xx">unordered associative container</a>
  75. *
  76. * Base is _Hashtable, dispatched at compile time via template
  77. * alias __uset_hashtable.
  78. */
  79. template<typename _Value,
  80. typename _Hash = hash<_Value>,
  81. typename _Pred = equal_to<_Value>,
  82. typename _Alloc = allocator<_Value>>
  83. class unordered_set
  84. {
  85. typedef __uset_hashtable<_Value, _Hash, _Pred, _Alloc> _Hashtable;
  86. _Hashtable _M_h;
  87. public:
  88. // typedefs:
  89. //@{
  90. /// Public typedefs.
  91. typedef typename _Hashtable::key_type key_type;
  92. typedef typename _Hashtable::value_type value_type;
  93. typedef typename _Hashtable::hasher hasher;
  94. typedef typename _Hashtable::key_equal key_equal;
  95. typedef typename _Hashtable::allocator_type allocator_type;
  96. //@}
  97. //@{
  98. /// Iterator-related typedefs.
  99. typedef typename _Hashtable::pointer pointer;
  100. typedef typename _Hashtable::const_pointer const_pointer;
  101. typedef typename _Hashtable::reference reference;
  102. typedef typename _Hashtable::const_reference const_reference;
  103. typedef typename _Hashtable::iterator iterator;
  104. typedef typename _Hashtable::const_iterator const_iterator;
  105. typedef typename _Hashtable::local_iterator local_iterator;
  106. typedef typename _Hashtable::const_local_iterator const_local_iterator;
  107. typedef typename _Hashtable::size_type size_type;
  108. typedef typename _Hashtable::difference_type difference_type;
  109. //@}
  110. #if __cplusplus > 201402L
  111. using node_type = typename _Hashtable::node_type;
  112. using insert_return_type = typename _Hashtable::insert_return_type;
  113. #endif
  114. // construct/destroy/copy
  115. /// Default constructor.
  116. unordered_set() = default;
  117. /**
  118. * @brief Default constructor creates no elements.
  119. * @param __n Minimal initial number of buckets.
  120. * @param __hf A hash functor.
  121. * @param __eql A key equality functor.
  122. * @param __a An allocator object.
  123. */
  124. explicit
  125. unordered_set(size_type __n,
  126. const hasher& __hf = hasher(),
  127. const key_equal& __eql = key_equal(),
  128. const allocator_type& __a = allocator_type())
  129. : _M_h(__n, __hf, __eql, __a)
  130. { }
  131. /**
  132. * @brief Builds an %unordered_set from a range.
  133. * @param __first An input iterator.
  134. * @param __last An input iterator.
  135. * @param __n Minimal initial number of buckets.
  136. * @param __hf A hash functor.
  137. * @param __eql A key equality functor.
  138. * @param __a An allocator object.
  139. *
  140. * Create an %unordered_set consisting of copies of the elements from
  141. * [__first,__last). This is linear in N (where N is
  142. * distance(__first,__last)).
  143. */
  144. template<typename _InputIterator>
  145. unordered_set(_InputIterator __first, _InputIterator __last,
  146. size_type __n = 0,
  147. const hasher& __hf = hasher(),
  148. const key_equal& __eql = key_equal(),
  149. const allocator_type& __a = allocator_type())
  150. : _M_h(__first, __last, __n, __hf, __eql, __a)
  151. { }
  152. /// Copy constructor.
  153. unordered_set(const unordered_set&) = default;
  154. /// Move constructor.
  155. unordered_set(unordered_set&&) = default;
  156. /**
  157. * @brief Creates an %unordered_set with no elements.
  158. * @param __a An allocator object.
  159. */
  160. explicit
  161. unordered_set(const allocator_type& __a)
  162. : _M_h(__a)
  163. { }
  164. /*
  165. * @brief Copy constructor with allocator argument.
  166. * @param __uset Input %unordered_set to copy.
  167. * @param __a An allocator object.
  168. */
  169. unordered_set(const unordered_set& __uset,
  170. const allocator_type& __a)
  171. : _M_h(__uset._M_h, __a)
  172. { }
  173. /*
  174. * @brief Move constructor with allocator argument.
  175. * @param __uset Input %unordered_set to move.
  176. * @param __a An allocator object.
  177. */
  178. unordered_set(unordered_set&& __uset,
  179. const allocator_type& __a)
  180. : _M_h(std::move(__uset._M_h), __a)
  181. { }
  182. /**
  183. * @brief Builds an %unordered_set from an initializer_list.
  184. * @param __l An initializer_list.
  185. * @param __n Minimal initial number of buckets.
  186. * @param __hf A hash functor.
  187. * @param __eql A key equality functor.
  188. * @param __a An allocator object.
  189. *
  190. * Create an %unordered_set consisting of copies of the elements in the
  191. * list. This is linear in N (where N is @a __l.size()).
  192. */
  193. unordered_set(initializer_list<value_type> __l,
  194. size_type __n = 0,
  195. const hasher& __hf = hasher(),
  196. const key_equal& __eql = key_equal(),
  197. const allocator_type& __a = allocator_type())
  198. : _M_h(__l, __n, __hf, __eql, __a)
  199. { }
  200. unordered_set(size_type __n, const allocator_type& __a)
  201. : unordered_set(__n, hasher(), key_equal(), __a)
  202. { }
  203. unordered_set(size_type __n, const hasher& __hf,
  204. const allocator_type& __a)
  205. : unordered_set(__n, __hf, key_equal(), __a)
  206. { }
  207. template<typename _InputIterator>
  208. unordered_set(_InputIterator __first, _InputIterator __last,
  209. size_type __n,
  210. const allocator_type& __a)
  211. : unordered_set(__first, __last, __n, hasher(), key_equal(), __a)
  212. { }
  213. template<typename _InputIterator>
  214. unordered_set(_InputIterator __first, _InputIterator __last,
  215. size_type __n, const hasher& __hf,
  216. const allocator_type& __a)
  217. : unordered_set(__first, __last, __n, __hf, key_equal(), __a)
  218. { }
  219. unordered_set(initializer_list<value_type> __l,
  220. size_type __n,
  221. const allocator_type& __a)
  222. : unordered_set(__l, __n, hasher(), key_equal(), __a)
  223. { }
  224. unordered_set(initializer_list<value_type> __l,
  225. size_type __n, const hasher& __hf,
  226. const allocator_type& __a)
  227. : unordered_set(__l, __n, __hf, key_equal(), __a)
  228. { }
  229. /// Copy assignment operator.
  230. unordered_set&
  231. operator=(const unordered_set&) = default;
  232. /// Move assignment operator.
  233. unordered_set&
  234. operator=(unordered_set&&) = default;
  235. /**
  236. * @brief %Unordered_set list assignment operator.
  237. * @param __l An initializer_list.
  238. *
  239. * This function fills an %unordered_set with copies of the elements in
  240. * the initializer list @a __l.
  241. *
  242. * Note that the assignment completely changes the %unordered_set and
  243. * that the resulting %unordered_set's size is the same as the number
  244. * of elements assigned.
  245. */
  246. unordered_set&
  247. operator=(initializer_list<value_type> __l)
  248. {
  249. _M_h = __l;
  250. return *this;
  251. }
  252. /// Returns the allocator object used by the %unordered_set.
  253. allocator_type
  254. get_allocator() const noexcept
  255. { return _M_h.get_allocator(); }
  256. // size and capacity:
  257. /// Returns true if the %unordered_set is empty.
  258. _GLIBCXX_NODISCARD bool
  259. empty() const noexcept
  260. { return _M_h.empty(); }
  261. /// Returns the size of the %unordered_set.
  262. size_type
  263. size() const noexcept
  264. { return _M_h.size(); }
  265. /// Returns the maximum size of the %unordered_set.
  266. size_type
  267. max_size() const noexcept
  268. { return _M_h.max_size(); }
  269. // iterators.
  270. //@{
  271. /**
  272. * Returns a read-only (constant) iterator that points to the first
  273. * element in the %unordered_set.
  274. */
  275. iterator
  276. begin() noexcept
  277. { return _M_h.begin(); }
  278. const_iterator
  279. begin() const noexcept
  280. { return _M_h.begin(); }
  281. //@}
  282. //@{
  283. /**
  284. * Returns a read-only (constant) iterator that points one past the last
  285. * element in the %unordered_set.
  286. */
  287. iterator
  288. end() noexcept
  289. { return _M_h.end(); }
  290. const_iterator
  291. end() const noexcept
  292. { return _M_h.end(); }
  293. //@}
  294. /**
  295. * Returns a read-only (constant) iterator that points to the first
  296. * element in the %unordered_set.
  297. */
  298. const_iterator
  299. cbegin() const noexcept
  300. { return _M_h.begin(); }
  301. /**
  302. * Returns a read-only (constant) iterator that points one past the last
  303. * element in the %unordered_set.
  304. */
  305. const_iterator
  306. cend() const noexcept
  307. { return _M_h.end(); }
  308. // modifiers.
  309. /**
  310. * @brief Attempts to build and insert an element into the
  311. * %unordered_set.
  312. * @param __args Arguments used to generate an element.
  313. * @return A pair, of which the first element is an iterator that points
  314. * to the possibly inserted element, and the second is a bool
  315. * that is true if the element was actually inserted.
  316. *
  317. * This function attempts to build and insert an element into the
  318. * %unordered_set. An %unordered_set relies on unique keys and thus an
  319. * element is only inserted if it is not already present in the
  320. * %unordered_set.
  321. *
  322. * Insertion requires amortized constant time.
  323. */
  324. template<typename... _Args>
  325. std::pair<iterator, bool>
  326. emplace(_Args&&... __args)
  327. { return _M_h.emplace(std::forward<_Args>(__args)...); }
  328. /**
  329. * @brief Attempts to insert an element into the %unordered_set.
  330. * @param __pos An iterator that serves as a hint as to where the
  331. * element should be inserted.
  332. * @param __args Arguments used to generate the element to be
  333. * inserted.
  334. * @return An iterator that points to the element with key equivalent to
  335. * the one generated from @a __args (may or may not be the
  336. * element itself).
  337. *
  338. * This function is not concerned about whether the insertion took place,
  339. * and thus does not return a boolean like the single-argument emplace()
  340. * does. Note that the first parameter is only a hint and can
  341. * potentially improve the performance of the insertion process. A bad
  342. * hint would cause no gains in efficiency.
  343. *
  344. * For more on @a hinting, see:
  345. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
  346. *
  347. * Insertion requires amortized constant time.
  348. */
  349. template<typename... _Args>
  350. iterator
  351. emplace_hint(const_iterator __pos, _Args&&... __args)
  352. { return _M_h.emplace_hint(__pos, std::forward<_Args>(__args)...); }
  353. //@{
  354. /**
  355. * @brief Attempts to insert an element into the %unordered_set.
  356. * @param __x Element to be inserted.
  357. * @return A pair, of which the first element is an iterator that points
  358. * to the possibly inserted element, and the second is a bool
  359. * that is true if the element was actually inserted.
  360. *
  361. * This function attempts to insert an element into the %unordered_set.
  362. * An %unordered_set relies on unique keys and thus an element is only
  363. * inserted if it is not already present in the %unordered_set.
  364. *
  365. * Insertion requires amortized constant time.
  366. */
  367. std::pair<iterator, bool>
  368. insert(const value_type& __x)
  369. { return _M_h.insert(__x); }
  370. std::pair<iterator, bool>
  371. insert(value_type&& __x)
  372. { return _M_h.insert(std::move(__x)); }
  373. //@}
  374. //@{
  375. /**
  376. * @brief Attempts to insert an element into the %unordered_set.
  377. * @param __hint An iterator that serves as a hint as to where the
  378. * element should be inserted.
  379. * @param __x Element to be inserted.
  380. * @return An iterator that points to the element with key of
  381. * @a __x (may or may not be the element passed in).
  382. *
  383. * This function is not concerned about whether the insertion took place,
  384. * and thus does not return a boolean like the single-argument insert()
  385. * does. Note that the first parameter is only a hint and can
  386. * potentially improve the performance of the insertion process. A bad
  387. * hint would cause no gains in efficiency.
  388. *
  389. * For more on @a hinting, see:
  390. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
  391. *
  392. * Insertion requires amortized constant.
  393. */
  394. iterator
  395. insert(const_iterator __hint, const value_type& __x)
  396. { return _M_h.insert(__hint, __x); }
  397. iterator
  398. insert(const_iterator __hint, value_type&& __x)
  399. { return _M_h.insert(__hint, std::move(__x)); }
  400. //@}
  401. /**
  402. * @brief A template function that attempts to insert a range of
  403. * elements.
  404. * @param __first Iterator pointing to the start of the range to be
  405. * inserted.
  406. * @param __last Iterator pointing to the end of the range.
  407. *
  408. * Complexity similar to that of the range constructor.
  409. */
  410. template<typename _InputIterator>
  411. void
  412. insert(_InputIterator __first, _InputIterator __last)
  413. { _M_h.insert(__first, __last); }
  414. /**
  415. * @brief Attempts to insert a list of elements into the %unordered_set.
  416. * @param __l A std::initializer_list<value_type> of elements
  417. * to be inserted.
  418. *
  419. * Complexity similar to that of the range constructor.
  420. */
  421. void
  422. insert(initializer_list<value_type> __l)
  423. { _M_h.insert(__l); }
  424. #if __cplusplus > 201402L
  425. /// Extract a node.
  426. node_type
  427. extract(const_iterator __pos)
  428. {
  429. __glibcxx_assert(__pos != end());
  430. return _M_h.extract(__pos);
  431. }
  432. /// Extract a node.
  433. node_type
  434. extract(const key_type& __key)
  435. { return _M_h.extract(__key); }
  436. /// Re-insert an extracted node.
  437. insert_return_type
  438. insert(node_type&& __nh)
  439. { return _M_h._M_reinsert_node(std::move(__nh)); }
  440. /// Re-insert an extracted node.
  441. iterator
  442. insert(const_iterator, node_type&& __nh)
  443. { return _M_h._M_reinsert_node(std::move(__nh)).position; }
  444. #endif // C++17
  445. //@{
  446. /**
  447. * @brief Erases an element from an %unordered_set.
  448. * @param __position An iterator pointing to the element to be erased.
  449. * @return An iterator pointing to the element immediately following
  450. * @a __position prior to the element being erased. If no such
  451. * element exists, end() is returned.
  452. *
  453. * This function erases an element, pointed to by the given iterator,
  454. * from an %unordered_set. Note that this function only erases the
  455. * element, and that if the element is itself a pointer, the pointed-to
  456. * memory is not touched in any way. Managing the pointer is the user's
  457. * responsibility.
  458. */
  459. iterator
  460. erase(const_iterator __position)
  461. { return _M_h.erase(__position); }
  462. // LWG 2059.
  463. iterator
  464. erase(iterator __position)
  465. { return _M_h.erase(__position); }
  466. //@}
  467. /**
  468. * @brief Erases elements according to the provided key.
  469. * @param __x Key of element to be erased.
  470. * @return The number of elements erased.
  471. *
  472. * This function erases all the elements located by the given key from
  473. * an %unordered_set. For an %unordered_set the result of this function
  474. * can only be 0 (not present) or 1 (present).
  475. * Note that this function only erases the element, and that if
  476. * the element is itself a pointer, the pointed-to memory is not touched
  477. * in any way. Managing the pointer is the user's responsibility.
  478. */
  479. size_type
  480. erase(const key_type& __x)
  481. { return _M_h.erase(__x); }
  482. /**
  483. * @brief Erases a [__first,__last) range of elements from an
  484. * %unordered_set.
  485. * @param __first Iterator pointing to the start of the range to be
  486. * erased.
  487. * @param __last Iterator pointing to the end of the range to
  488. * be erased.
  489. * @return The iterator @a __last.
  490. *
  491. * This function erases a sequence of elements from an %unordered_set.
  492. * Note that this function only erases the element, and that if
  493. * the element is itself a pointer, the pointed-to memory is not touched
  494. * in any way. Managing the pointer is the user's responsibility.
  495. */
  496. iterator
  497. erase(const_iterator __first, const_iterator __last)
  498. { return _M_h.erase(__first, __last); }
  499. /**
  500. * Erases all elements in an %unordered_set. Note that this function only
  501. * erases the elements, and that if the elements themselves are pointers,
  502. * the pointed-to memory is not touched in any way. Managing the pointer
  503. * is the user's responsibility.
  504. */
  505. void
  506. clear() noexcept
  507. { _M_h.clear(); }
  508. /**
  509. * @brief Swaps data with another %unordered_set.
  510. * @param __x An %unordered_set of the same element and allocator
  511. * types.
  512. *
  513. * This exchanges the elements between two sets in constant time.
  514. * Note that the global std::swap() function is specialized such that
  515. * std::swap(s1,s2) will feed to this function.
  516. */
  517. void
  518. swap(unordered_set& __x)
  519. noexcept( noexcept(_M_h.swap(__x._M_h)) )
  520. { _M_h.swap(__x._M_h); }
  521. #if __cplusplus > 201402L
  522. template<typename, typename, typename>
  523. friend class std::_Hash_merge_helper;
  524. template<typename _H2, typename _P2>
  525. void
  526. merge(unordered_set<_Value, _H2, _P2, _Alloc>& __source)
  527. {
  528. using _Merge_helper = _Hash_merge_helper<unordered_set, _H2, _P2>;
  529. _M_h._M_merge_unique(_Merge_helper::_S_get_table(__source));
  530. }
  531. template<typename _H2, typename _P2>
  532. void
  533. merge(unordered_set<_Value, _H2, _P2, _Alloc>&& __source)
  534. { merge(__source); }
  535. template<typename _H2, typename _P2>
  536. void
  537. merge(unordered_multiset<_Value, _H2, _P2, _Alloc>& __source)
  538. {
  539. using _Merge_helper = _Hash_merge_helper<unordered_set, _H2, _P2>;
  540. _M_h._M_merge_unique(_Merge_helper::_S_get_table(__source));
  541. }
  542. template<typename _H2, typename _P2>
  543. void
  544. merge(unordered_multiset<_Value, _H2, _P2, _Alloc>&& __source)
  545. { merge(__source); }
  546. #endif // C++17
  547. // observers.
  548. /// Returns the hash functor object with which the %unordered_set was
  549. /// constructed.
  550. hasher
  551. hash_function() const
  552. { return _M_h.hash_function(); }
  553. /// Returns the key comparison object with which the %unordered_set was
  554. /// constructed.
  555. key_equal
  556. key_eq() const
  557. { return _M_h.key_eq(); }
  558. // lookup.
  559. //@{
  560. /**
  561. * @brief Tries to locate an element in an %unordered_set.
  562. * @param __x Element to be located.
  563. * @return Iterator pointing to sought-after element, or end() if not
  564. * found.
  565. *
  566. * This function takes a key and tries to locate the element with which
  567. * the key matches. If successful the function returns an iterator
  568. * pointing to the sought after element. If unsuccessful it returns the
  569. * past-the-end ( @c end() ) iterator.
  570. */
  571. iterator
  572. find(const key_type& __x)
  573. { return _M_h.find(__x); }
  574. const_iterator
  575. find(const key_type& __x) const
  576. { return _M_h.find(__x); }
  577. //@}
  578. /**
  579. * @brief Finds the number of elements.
  580. * @param __x Element to located.
  581. * @return Number of elements with specified key.
  582. *
  583. * This function only makes sense for unordered_multisets; for
  584. * unordered_set the result will either be 0 (not present) or 1
  585. * (present).
  586. */
  587. size_type
  588. count(const key_type& __x) const
  589. { return _M_h.count(__x); }
  590. #if __cplusplus > 201703L
  591. /**
  592. * @brief Finds whether an element with the given key exists.
  593. * @param __x Key of elements to be located.
  594. * @return True if there is any element with the specified key.
  595. */
  596. bool
  597. contains(const key_type& __x) const
  598. { return _M_h.find(__x) != _M_h.end(); }
  599. #endif
  600. //@{
  601. /**
  602. * @brief Finds a subsequence matching given key.
  603. * @param __x Key to be located.
  604. * @return Pair of iterators that possibly points to the subsequence
  605. * matching given key.
  606. *
  607. * This function probably only makes sense for multisets.
  608. */
  609. std::pair<iterator, iterator>
  610. equal_range(const key_type& __x)
  611. { return _M_h.equal_range(__x); }
  612. std::pair<const_iterator, const_iterator>
  613. equal_range(const key_type& __x) const
  614. { return _M_h.equal_range(__x); }
  615. //@}
  616. // bucket interface.
  617. /// Returns the number of buckets of the %unordered_set.
  618. size_type
  619. bucket_count() const noexcept
  620. { return _M_h.bucket_count(); }
  621. /// Returns the maximum number of buckets of the %unordered_set.
  622. size_type
  623. max_bucket_count() const noexcept
  624. { return _M_h.max_bucket_count(); }
  625. /*
  626. * @brief Returns the number of elements in a given bucket.
  627. * @param __n A bucket index.
  628. * @return The number of elements in the bucket.
  629. */
  630. size_type
  631. bucket_size(size_type __n) const
  632. { return _M_h.bucket_size(__n); }
  633. /*
  634. * @brief Returns the bucket index of a given element.
  635. * @param __key A key instance.
  636. * @return The key bucket index.
  637. */
  638. size_type
  639. bucket(const key_type& __key) const
  640. { return _M_h.bucket(__key); }
  641. //@{
  642. /**
  643. * @brief Returns a read-only (constant) iterator pointing to the first
  644. * bucket element.
  645. * @param __n The bucket index.
  646. * @return A read-only local iterator.
  647. */
  648. local_iterator
  649. begin(size_type __n)
  650. { return _M_h.begin(__n); }
  651. const_local_iterator
  652. begin(size_type __n) const
  653. { return _M_h.begin(__n); }
  654. const_local_iterator
  655. cbegin(size_type __n) const
  656. { return _M_h.cbegin(__n); }
  657. //@}
  658. //@{
  659. /**
  660. * @brief Returns a read-only (constant) iterator pointing to one past
  661. * the last bucket elements.
  662. * @param __n The bucket index.
  663. * @return A read-only local iterator.
  664. */
  665. local_iterator
  666. end(size_type __n)
  667. { return _M_h.end(__n); }
  668. const_local_iterator
  669. end(size_type __n) const
  670. { return _M_h.end(__n); }
  671. const_local_iterator
  672. cend(size_type __n) const
  673. { return _M_h.cend(__n); }
  674. //@}
  675. // hash policy.
  676. /// Returns the average number of elements per bucket.
  677. float
  678. load_factor() const noexcept
  679. { return _M_h.load_factor(); }
  680. /// Returns a positive number that the %unordered_set tries to keep the
  681. /// load factor less than or equal to.
  682. float
  683. max_load_factor() const noexcept
  684. { return _M_h.max_load_factor(); }
  685. /**
  686. * @brief Change the %unordered_set maximum load factor.
  687. * @param __z The new maximum load factor.
  688. */
  689. void
  690. max_load_factor(float __z)
  691. { _M_h.max_load_factor(__z); }
  692. /**
  693. * @brief May rehash the %unordered_set.
  694. * @param __n The new number of buckets.
  695. *
  696. * Rehash will occur only if the new number of buckets respect the
  697. * %unordered_set maximum load factor.
  698. */
  699. void
  700. rehash(size_type __n)
  701. { _M_h.rehash(__n); }
  702. /**
  703. * @brief Prepare the %unordered_set for a specified number of
  704. * elements.
  705. * @param __n Number of elements required.
  706. *
  707. * Same as rehash(ceil(n / max_load_factor())).
  708. */
  709. void
  710. reserve(size_type __n)
  711. { _M_h.reserve(__n); }
  712. template<typename _Value1, typename _Hash1, typename _Pred1,
  713. typename _Alloc1>
  714. friend bool
  715. operator==(const unordered_set<_Value1, _Hash1, _Pred1, _Alloc1>&,
  716. const unordered_set<_Value1, _Hash1, _Pred1, _Alloc1>&);
  717. };
  718. #if __cpp_deduction_guides >= 201606
  719. template<typename _InputIterator,
  720. typename _Hash =
  721. hash<typename iterator_traits<_InputIterator>::value_type>,
  722. typename _Pred =
  723. equal_to<typename iterator_traits<_InputIterator>::value_type>,
  724. typename _Allocator =
  725. allocator<typename iterator_traits<_InputIterator>::value_type>,
  726. typename = _RequireInputIter<_InputIterator>,
  727. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  728. typename = _RequireNotAllocator<_Pred>,
  729. typename = _RequireAllocator<_Allocator>>
  730. unordered_set(_InputIterator, _InputIterator,
  731. unordered_set<int>::size_type = {},
  732. _Hash = _Hash(), _Pred = _Pred(), _Allocator = _Allocator())
  733. -> unordered_set<typename iterator_traits<_InputIterator>::value_type,
  734. _Hash, _Pred, _Allocator>;
  735. template<typename _Tp, typename _Hash = hash<_Tp>,
  736. typename _Pred = equal_to<_Tp>,
  737. typename _Allocator = allocator<_Tp>,
  738. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  739. typename = _RequireNotAllocator<_Pred>,
  740. typename = _RequireAllocator<_Allocator>>
  741. unordered_set(initializer_list<_Tp>,
  742. unordered_set<int>::size_type = {},
  743. _Hash = _Hash(), _Pred = _Pred(), _Allocator = _Allocator())
  744. -> unordered_set<_Tp, _Hash, _Pred, _Allocator>;
  745. template<typename _InputIterator, typename _Allocator,
  746. typename = _RequireInputIter<_InputIterator>,
  747. typename = _RequireAllocator<_Allocator>>
  748. unordered_set(_InputIterator, _InputIterator,
  749. unordered_set<int>::size_type, _Allocator)
  750. -> unordered_set<typename iterator_traits<_InputIterator>::value_type,
  751. hash<
  752. typename iterator_traits<_InputIterator>::value_type>,
  753. equal_to<
  754. typename iterator_traits<_InputIterator>::value_type>,
  755. _Allocator>;
  756. template<typename _InputIterator, typename _Hash, typename _Allocator,
  757. typename = _RequireInputIter<_InputIterator>,
  758. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  759. typename = _RequireAllocator<_Allocator>>
  760. unordered_set(_InputIterator, _InputIterator,
  761. unordered_set<int>::size_type,
  762. _Hash, _Allocator)
  763. -> unordered_set<typename iterator_traits<_InputIterator>::value_type,
  764. _Hash,
  765. equal_to<
  766. typename iterator_traits<_InputIterator>::value_type>,
  767. _Allocator>;
  768. template<typename _Tp, typename _Allocator,
  769. typename = _RequireAllocator<_Allocator>>
  770. unordered_set(initializer_list<_Tp>,
  771. unordered_set<int>::size_type, _Allocator)
  772. -> unordered_set<_Tp, hash<_Tp>, equal_to<_Tp>, _Allocator>;
  773. template<typename _Tp, typename _Hash, typename _Allocator,
  774. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  775. typename = _RequireAllocator<_Allocator>>
  776. unordered_set(initializer_list<_Tp>,
  777. unordered_set<int>::size_type, _Hash, _Allocator)
  778. -> unordered_set<_Tp, _Hash, equal_to<_Tp>, _Allocator>;
  779. #endif
  780. /**
  781. * @brief A standard container composed of equivalent keys
  782. * (possibly containing multiple of each key value) in which the
  783. * elements' keys are the elements themselves.
  784. *
  785. * @ingroup unordered_associative_containers
  786. *
  787. * @tparam _Value Type of key objects.
  788. * @tparam _Hash Hashing function object type, defaults to hash<_Value>.
  789. * @tparam _Pred Predicate function object type, defaults
  790. * to equal_to<_Value>.
  791. * @tparam _Alloc Allocator type, defaults to allocator<_Key>.
  792. *
  793. * Meets the requirements of a <a href="tables.html#65">container</a>, and
  794. * <a href="tables.html#xx">unordered associative container</a>
  795. *
  796. * Base is _Hashtable, dispatched at compile time via template
  797. * alias __umset_hashtable.
  798. */
  799. template<typename _Value,
  800. typename _Hash = hash<_Value>,
  801. typename _Pred = equal_to<_Value>,
  802. typename _Alloc = allocator<_Value>>
  803. class unordered_multiset
  804. {
  805. typedef __umset_hashtable<_Value, _Hash, _Pred, _Alloc> _Hashtable;
  806. _Hashtable _M_h;
  807. public:
  808. // typedefs:
  809. //@{
  810. /// Public typedefs.
  811. typedef typename _Hashtable::key_type key_type;
  812. typedef typename _Hashtable::value_type value_type;
  813. typedef typename _Hashtable::hasher hasher;
  814. typedef typename _Hashtable::key_equal key_equal;
  815. typedef typename _Hashtable::allocator_type allocator_type;
  816. //@}
  817. //@{
  818. /// Iterator-related typedefs.
  819. typedef typename _Hashtable::pointer pointer;
  820. typedef typename _Hashtable::const_pointer const_pointer;
  821. typedef typename _Hashtable::reference reference;
  822. typedef typename _Hashtable::const_reference const_reference;
  823. typedef typename _Hashtable::iterator iterator;
  824. typedef typename _Hashtable::const_iterator const_iterator;
  825. typedef typename _Hashtable::local_iterator local_iterator;
  826. typedef typename _Hashtable::const_local_iterator const_local_iterator;
  827. typedef typename _Hashtable::size_type size_type;
  828. typedef typename _Hashtable::difference_type difference_type;
  829. //@}
  830. #if __cplusplus > 201402L
  831. using node_type = typename _Hashtable::node_type;
  832. #endif
  833. // construct/destroy/copy
  834. /// Default constructor.
  835. unordered_multiset() = default;
  836. /**
  837. * @brief Default constructor creates no elements.
  838. * @param __n Minimal initial number of buckets.
  839. * @param __hf A hash functor.
  840. * @param __eql A key equality functor.
  841. * @param __a An allocator object.
  842. */
  843. explicit
  844. unordered_multiset(size_type __n,
  845. const hasher& __hf = hasher(),
  846. const key_equal& __eql = key_equal(),
  847. const allocator_type& __a = allocator_type())
  848. : _M_h(__n, __hf, __eql, __a)
  849. { }
  850. /**
  851. * @brief Builds an %unordered_multiset from a range.
  852. * @param __first An input iterator.
  853. * @param __last An input iterator.
  854. * @param __n Minimal initial number of buckets.
  855. * @param __hf A hash functor.
  856. * @param __eql A key equality functor.
  857. * @param __a An allocator object.
  858. *
  859. * Create an %unordered_multiset consisting of copies of the elements
  860. * from [__first,__last). This is linear in N (where N is
  861. * distance(__first,__last)).
  862. */
  863. template<typename _InputIterator>
  864. unordered_multiset(_InputIterator __first, _InputIterator __last,
  865. size_type __n = 0,
  866. const hasher& __hf = hasher(),
  867. const key_equal& __eql = key_equal(),
  868. const allocator_type& __a = allocator_type())
  869. : _M_h(__first, __last, __n, __hf, __eql, __a)
  870. { }
  871. /// Copy constructor.
  872. unordered_multiset(const unordered_multiset&) = default;
  873. /// Move constructor.
  874. unordered_multiset(unordered_multiset&&) = default;
  875. /**
  876. * @brief Builds an %unordered_multiset from an initializer_list.
  877. * @param __l An initializer_list.
  878. * @param __n Minimal initial number of buckets.
  879. * @param __hf A hash functor.
  880. * @param __eql A key equality functor.
  881. * @param __a An allocator object.
  882. *
  883. * Create an %unordered_multiset consisting of copies of the elements in
  884. * the list. This is linear in N (where N is @a __l.size()).
  885. */
  886. unordered_multiset(initializer_list<value_type> __l,
  887. size_type __n = 0,
  888. const hasher& __hf = hasher(),
  889. const key_equal& __eql = key_equal(),
  890. const allocator_type& __a = allocator_type())
  891. : _M_h(__l, __n, __hf, __eql, __a)
  892. { }
  893. /// Copy assignment operator.
  894. unordered_multiset&
  895. operator=(const unordered_multiset&) = default;
  896. /// Move assignment operator.
  897. unordered_multiset&
  898. operator=(unordered_multiset&&) = default;
  899. /**
  900. * @brief Creates an %unordered_multiset with no elements.
  901. * @param __a An allocator object.
  902. */
  903. explicit
  904. unordered_multiset(const allocator_type& __a)
  905. : _M_h(__a)
  906. { }
  907. /*
  908. * @brief Copy constructor with allocator argument.
  909. * @param __uset Input %unordered_multiset to copy.
  910. * @param __a An allocator object.
  911. */
  912. unordered_multiset(const unordered_multiset& __umset,
  913. const allocator_type& __a)
  914. : _M_h(__umset._M_h, __a)
  915. { }
  916. /*
  917. * @brief Move constructor with allocator argument.
  918. * @param __umset Input %unordered_multiset to move.
  919. * @param __a An allocator object.
  920. */
  921. unordered_multiset(unordered_multiset&& __umset,
  922. const allocator_type& __a)
  923. : _M_h(std::move(__umset._M_h), __a)
  924. { }
  925. unordered_multiset(size_type __n, const allocator_type& __a)
  926. : unordered_multiset(__n, hasher(), key_equal(), __a)
  927. { }
  928. unordered_multiset(size_type __n, const hasher& __hf,
  929. const allocator_type& __a)
  930. : unordered_multiset(__n, __hf, key_equal(), __a)
  931. { }
  932. template<typename _InputIterator>
  933. unordered_multiset(_InputIterator __first, _InputIterator __last,
  934. size_type __n,
  935. const allocator_type& __a)
  936. : unordered_multiset(__first, __last, __n, hasher(), key_equal(), __a)
  937. { }
  938. template<typename _InputIterator>
  939. unordered_multiset(_InputIterator __first, _InputIterator __last,
  940. size_type __n, const hasher& __hf,
  941. const allocator_type& __a)
  942. : unordered_multiset(__first, __last, __n, __hf, key_equal(), __a)
  943. { }
  944. unordered_multiset(initializer_list<value_type> __l,
  945. size_type __n,
  946. const allocator_type& __a)
  947. : unordered_multiset(__l, __n, hasher(), key_equal(), __a)
  948. { }
  949. unordered_multiset(initializer_list<value_type> __l,
  950. size_type __n, const hasher& __hf,
  951. const allocator_type& __a)
  952. : unordered_multiset(__l, __n, __hf, key_equal(), __a)
  953. { }
  954. /**
  955. * @brief %Unordered_multiset list assignment operator.
  956. * @param __l An initializer_list.
  957. *
  958. * This function fills an %unordered_multiset with copies of the elements
  959. * in the initializer list @a __l.
  960. *
  961. * Note that the assignment completely changes the %unordered_multiset
  962. * and that the resulting %unordered_multiset's size is the same as the
  963. * number of elements assigned.
  964. */
  965. unordered_multiset&
  966. operator=(initializer_list<value_type> __l)
  967. {
  968. _M_h = __l;
  969. return *this;
  970. }
  971. /// Returns the allocator object used by the %unordered_multiset.
  972. allocator_type
  973. get_allocator() const noexcept
  974. { return _M_h.get_allocator(); }
  975. // size and capacity:
  976. /// Returns true if the %unordered_multiset is empty.
  977. _GLIBCXX_NODISCARD bool
  978. empty() const noexcept
  979. { return _M_h.empty(); }
  980. /// Returns the size of the %unordered_multiset.
  981. size_type
  982. size() const noexcept
  983. { return _M_h.size(); }
  984. /// Returns the maximum size of the %unordered_multiset.
  985. size_type
  986. max_size() const noexcept
  987. { return _M_h.max_size(); }
  988. // iterators.
  989. //@{
  990. /**
  991. * Returns a read-only (constant) iterator that points to the first
  992. * element in the %unordered_multiset.
  993. */
  994. iterator
  995. begin() noexcept
  996. { return _M_h.begin(); }
  997. const_iterator
  998. begin() const noexcept
  999. { return _M_h.begin(); }
  1000. //@}
  1001. //@{
  1002. /**
  1003. * Returns a read-only (constant) iterator that points one past the last
  1004. * element in the %unordered_multiset.
  1005. */
  1006. iterator
  1007. end() noexcept
  1008. { return _M_h.end(); }
  1009. const_iterator
  1010. end() const noexcept
  1011. { return _M_h.end(); }
  1012. //@}
  1013. /**
  1014. * Returns a read-only (constant) iterator that points to the first
  1015. * element in the %unordered_multiset.
  1016. */
  1017. const_iterator
  1018. cbegin() const noexcept
  1019. { return _M_h.begin(); }
  1020. /**
  1021. * Returns a read-only (constant) iterator that points one past the last
  1022. * element in the %unordered_multiset.
  1023. */
  1024. const_iterator
  1025. cend() const noexcept
  1026. { return _M_h.end(); }
  1027. // modifiers.
  1028. /**
  1029. * @brief Builds and insert an element into the %unordered_multiset.
  1030. * @param __args Arguments used to generate an element.
  1031. * @return An iterator that points to the inserted element.
  1032. *
  1033. * Insertion requires amortized constant time.
  1034. */
  1035. template<typename... _Args>
  1036. iterator
  1037. emplace(_Args&&... __args)
  1038. { return _M_h.emplace(std::forward<_Args>(__args)...); }
  1039. /**
  1040. * @brief Inserts an element into the %unordered_multiset.
  1041. * @param __pos An iterator that serves as a hint as to where the
  1042. * element should be inserted.
  1043. * @param __args Arguments used to generate the element to be
  1044. * inserted.
  1045. * @return An iterator that points to the inserted element.
  1046. *
  1047. * Note that the first parameter is only a hint and can potentially
  1048. * improve the performance of the insertion process. A bad hint would
  1049. * cause no gains in efficiency.
  1050. *
  1051. * For more on @a hinting, see:
  1052. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
  1053. *
  1054. * Insertion requires amortized constant time.
  1055. */
  1056. template<typename... _Args>
  1057. iterator
  1058. emplace_hint(const_iterator __pos, _Args&&... __args)
  1059. { return _M_h.emplace_hint(__pos, std::forward<_Args>(__args)...); }
  1060. //@{
  1061. /**
  1062. * @brief Inserts an element into the %unordered_multiset.
  1063. * @param __x Element to be inserted.
  1064. * @return An iterator that points to the inserted element.
  1065. *
  1066. * Insertion requires amortized constant time.
  1067. */
  1068. iterator
  1069. insert(const value_type& __x)
  1070. { return _M_h.insert(__x); }
  1071. iterator
  1072. insert(value_type&& __x)
  1073. { return _M_h.insert(std::move(__x)); }
  1074. //@}
  1075. //@{
  1076. /**
  1077. * @brief Inserts an element into the %unordered_multiset.
  1078. * @param __hint An iterator that serves as a hint as to where the
  1079. * element should be inserted.
  1080. * @param __x Element to be inserted.
  1081. * @return An iterator that points to the inserted element.
  1082. *
  1083. * Note that the first parameter is only a hint and can potentially
  1084. * improve the performance of the insertion process. A bad hint would
  1085. * cause no gains in efficiency.
  1086. *
  1087. * For more on @a hinting, see:
  1088. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
  1089. *
  1090. * Insertion requires amortized constant.
  1091. */
  1092. iterator
  1093. insert(const_iterator __hint, const value_type& __x)
  1094. { return _M_h.insert(__hint, __x); }
  1095. iterator
  1096. insert(const_iterator __hint, value_type&& __x)
  1097. { return _M_h.insert(__hint, std::move(__x)); }
  1098. //@}
  1099. /**
  1100. * @brief A template function that inserts a range of elements.
  1101. * @param __first Iterator pointing to the start of the range to be
  1102. * inserted.
  1103. * @param __last Iterator pointing to the end of the range.
  1104. *
  1105. * Complexity similar to that of the range constructor.
  1106. */
  1107. template<typename _InputIterator>
  1108. void
  1109. insert(_InputIterator __first, _InputIterator __last)
  1110. { _M_h.insert(__first, __last); }
  1111. /**
  1112. * @brief Inserts a list of elements into the %unordered_multiset.
  1113. * @param __l A std::initializer_list<value_type> of elements to be
  1114. * inserted.
  1115. *
  1116. * Complexity similar to that of the range constructor.
  1117. */
  1118. void
  1119. insert(initializer_list<value_type> __l)
  1120. { _M_h.insert(__l); }
  1121. #if __cplusplus > 201402L
  1122. /// Extract a node.
  1123. node_type
  1124. extract(const_iterator __pos)
  1125. {
  1126. __glibcxx_assert(__pos != end());
  1127. return _M_h.extract(__pos);
  1128. }
  1129. /// Extract a node.
  1130. node_type
  1131. extract(const key_type& __key)
  1132. { return _M_h.extract(__key); }
  1133. /// Re-insert an extracted node.
  1134. iterator
  1135. insert(node_type&& __nh)
  1136. { return _M_h._M_reinsert_node_multi(cend(), std::move(__nh)); }
  1137. /// Re-insert an extracted node.
  1138. iterator
  1139. insert(const_iterator __hint, node_type&& __nh)
  1140. { return _M_h._M_reinsert_node_multi(__hint, std::move(__nh)); }
  1141. #endif // C++17
  1142. //@{
  1143. /**
  1144. * @brief Erases an element from an %unordered_multiset.
  1145. * @param __position An iterator pointing to the element to be erased.
  1146. * @return An iterator pointing to the element immediately following
  1147. * @a __position prior to the element being erased. If no such
  1148. * element exists, end() is returned.
  1149. *
  1150. * This function erases an element, pointed to by the given iterator,
  1151. * from an %unordered_multiset.
  1152. *
  1153. * Note that this function only erases the element, and that if the
  1154. * element is itself a pointer, the pointed-to memory is not touched in
  1155. * any way. Managing the pointer is the user's responsibility.
  1156. */
  1157. iterator
  1158. erase(const_iterator __position)
  1159. { return _M_h.erase(__position); }
  1160. // LWG 2059.
  1161. iterator
  1162. erase(iterator __position)
  1163. { return _M_h.erase(__position); }
  1164. //@}
  1165. /**
  1166. * @brief Erases elements according to the provided key.
  1167. * @param __x Key of element to be erased.
  1168. * @return The number of elements erased.
  1169. *
  1170. * This function erases all the elements located by the given key from
  1171. * an %unordered_multiset.
  1172. *
  1173. * Note that this function only erases the element, and that if the
  1174. * element is itself a pointer, the pointed-to memory is not touched in
  1175. * any way. Managing the pointer is the user's responsibility.
  1176. */
  1177. size_type
  1178. erase(const key_type& __x)
  1179. { return _M_h.erase(__x); }
  1180. /**
  1181. * @brief Erases a [__first,__last) range of elements from an
  1182. * %unordered_multiset.
  1183. * @param __first Iterator pointing to the start of the range to be
  1184. * erased.
  1185. * @param __last Iterator pointing to the end of the range to
  1186. * be erased.
  1187. * @return The iterator @a __last.
  1188. *
  1189. * This function erases a sequence of elements from an
  1190. * %unordered_multiset.
  1191. *
  1192. * Note that this function only erases the element, and that if
  1193. * the element is itself a pointer, the pointed-to memory is not touched
  1194. * in any way. Managing the pointer is the user's responsibility.
  1195. */
  1196. iterator
  1197. erase(const_iterator __first, const_iterator __last)
  1198. { return _M_h.erase(__first, __last); }
  1199. /**
  1200. * Erases all elements in an %unordered_multiset.
  1201. *
  1202. * Note that this function only erases the elements, and that if the
  1203. * elements themselves are pointers, the pointed-to memory is not touched
  1204. * in any way. Managing the pointer is the user's responsibility.
  1205. */
  1206. void
  1207. clear() noexcept
  1208. { _M_h.clear(); }
  1209. /**
  1210. * @brief Swaps data with another %unordered_multiset.
  1211. * @param __x An %unordered_multiset of the same element and allocator
  1212. * types.
  1213. *
  1214. * This exchanges the elements between two sets in constant time.
  1215. * Note that the global std::swap() function is specialized such that
  1216. * std::swap(s1,s2) will feed to this function.
  1217. */
  1218. void
  1219. swap(unordered_multiset& __x)
  1220. noexcept( noexcept(_M_h.swap(__x._M_h)) )
  1221. { _M_h.swap(__x._M_h); }
  1222. #if __cplusplus > 201402L
  1223. template<typename, typename, typename>
  1224. friend class std::_Hash_merge_helper;
  1225. template<typename _H2, typename _P2>
  1226. void
  1227. merge(unordered_multiset<_Value, _H2, _P2, _Alloc>& __source)
  1228. {
  1229. using _Merge_helper
  1230. = _Hash_merge_helper<unordered_multiset, _H2, _P2>;
  1231. _M_h._M_merge_multi(_Merge_helper::_S_get_table(__source));
  1232. }
  1233. template<typename _H2, typename _P2>
  1234. void
  1235. merge(unordered_multiset<_Value, _H2, _P2, _Alloc>&& __source)
  1236. { merge(__source); }
  1237. template<typename _H2, typename _P2>
  1238. void
  1239. merge(unordered_set<_Value, _H2, _P2, _Alloc>& __source)
  1240. {
  1241. using _Merge_helper
  1242. = _Hash_merge_helper<unordered_multiset, _H2, _P2>;
  1243. _M_h._M_merge_multi(_Merge_helper::_S_get_table(__source));
  1244. }
  1245. template<typename _H2, typename _P2>
  1246. void
  1247. merge(unordered_set<_Value, _H2, _P2, _Alloc>&& __source)
  1248. { merge(__source); }
  1249. #endif // C++17
  1250. // observers.
  1251. /// Returns the hash functor object with which the %unordered_multiset
  1252. /// was constructed.
  1253. hasher
  1254. hash_function() const
  1255. { return _M_h.hash_function(); }
  1256. /// Returns the key comparison object with which the %unordered_multiset
  1257. /// was constructed.
  1258. key_equal
  1259. key_eq() const
  1260. { return _M_h.key_eq(); }
  1261. // lookup.
  1262. //@{
  1263. /**
  1264. * @brief Tries to locate an element in an %unordered_multiset.
  1265. * @param __x Element to be located.
  1266. * @return Iterator pointing to sought-after element, or end() if not
  1267. * found.
  1268. *
  1269. * This function takes a key and tries to locate the element with which
  1270. * the key matches. If successful the function returns an iterator
  1271. * pointing to the sought after element. If unsuccessful it returns the
  1272. * past-the-end ( @c end() ) iterator.
  1273. */
  1274. iterator
  1275. find(const key_type& __x)
  1276. { return _M_h.find(__x); }
  1277. const_iterator
  1278. find(const key_type& __x) const
  1279. { return _M_h.find(__x); }
  1280. //@}
  1281. /**
  1282. * @brief Finds the number of elements.
  1283. * @param __x Element to located.
  1284. * @return Number of elements with specified key.
  1285. */
  1286. size_type
  1287. count(const key_type& __x) const
  1288. { return _M_h.count(__x); }
  1289. #if __cplusplus > 201703L
  1290. /**
  1291. * @brief Finds whether an element with the given key exists.
  1292. * @param __x Key of elements to be located.
  1293. * @return True if there is any element with the specified key.
  1294. */
  1295. bool
  1296. contains(const key_type& __x) const
  1297. { return _M_h.find(__x) != _M_h.end(); }
  1298. #endif
  1299. //@{
  1300. /**
  1301. * @brief Finds a subsequence matching given key.
  1302. * @param __x Key to be located.
  1303. * @return Pair of iterators that possibly points to the subsequence
  1304. * matching given key.
  1305. */
  1306. std::pair<iterator, iterator>
  1307. equal_range(const key_type& __x)
  1308. { return _M_h.equal_range(__x); }
  1309. std::pair<const_iterator, const_iterator>
  1310. equal_range(const key_type& __x) const
  1311. { return _M_h.equal_range(__x); }
  1312. //@}
  1313. // bucket interface.
  1314. /// Returns the number of buckets of the %unordered_multiset.
  1315. size_type
  1316. bucket_count() const noexcept
  1317. { return _M_h.bucket_count(); }
  1318. /// Returns the maximum number of buckets of the %unordered_multiset.
  1319. size_type
  1320. max_bucket_count() const noexcept
  1321. { return _M_h.max_bucket_count(); }
  1322. /*
  1323. * @brief Returns the number of elements in a given bucket.
  1324. * @param __n A bucket index.
  1325. * @return The number of elements in the bucket.
  1326. */
  1327. size_type
  1328. bucket_size(size_type __n) const
  1329. { return _M_h.bucket_size(__n); }
  1330. /*
  1331. * @brief Returns the bucket index of a given element.
  1332. * @param __key A key instance.
  1333. * @return The key bucket index.
  1334. */
  1335. size_type
  1336. bucket(const key_type& __key) const
  1337. { return _M_h.bucket(__key); }
  1338. //@{
  1339. /**
  1340. * @brief Returns a read-only (constant) iterator pointing to the first
  1341. * bucket element.
  1342. * @param __n The bucket index.
  1343. * @return A read-only local iterator.
  1344. */
  1345. local_iterator
  1346. begin(size_type __n)
  1347. { return _M_h.begin(__n); }
  1348. const_local_iterator
  1349. begin(size_type __n) const
  1350. { return _M_h.begin(__n); }
  1351. const_local_iterator
  1352. cbegin(size_type __n) const
  1353. { return _M_h.cbegin(__n); }
  1354. //@}
  1355. //@{
  1356. /**
  1357. * @brief Returns a read-only (constant) iterator pointing to one past
  1358. * the last bucket elements.
  1359. * @param __n The bucket index.
  1360. * @return A read-only local iterator.
  1361. */
  1362. local_iterator
  1363. end(size_type __n)
  1364. { return _M_h.end(__n); }
  1365. const_local_iterator
  1366. end(size_type __n) const
  1367. { return _M_h.end(__n); }
  1368. const_local_iterator
  1369. cend(size_type __n) const
  1370. { return _M_h.cend(__n); }
  1371. //@}
  1372. // hash policy.
  1373. /// Returns the average number of elements per bucket.
  1374. float
  1375. load_factor() const noexcept
  1376. { return _M_h.load_factor(); }
  1377. /// Returns a positive number that the %unordered_multiset tries to keep the
  1378. /// load factor less than or equal to.
  1379. float
  1380. max_load_factor() const noexcept
  1381. { return _M_h.max_load_factor(); }
  1382. /**
  1383. * @brief Change the %unordered_multiset maximum load factor.
  1384. * @param __z The new maximum load factor.
  1385. */
  1386. void
  1387. max_load_factor(float __z)
  1388. { _M_h.max_load_factor(__z); }
  1389. /**
  1390. * @brief May rehash the %unordered_multiset.
  1391. * @param __n The new number of buckets.
  1392. *
  1393. * Rehash will occur only if the new number of buckets respect the
  1394. * %unordered_multiset maximum load factor.
  1395. */
  1396. void
  1397. rehash(size_type __n)
  1398. { _M_h.rehash(__n); }
  1399. /**
  1400. * @brief Prepare the %unordered_multiset for a specified number of
  1401. * elements.
  1402. * @param __n Number of elements required.
  1403. *
  1404. * Same as rehash(ceil(n / max_load_factor())).
  1405. */
  1406. void
  1407. reserve(size_type __n)
  1408. { _M_h.reserve(__n); }
  1409. template<typename _Value1, typename _Hash1, typename _Pred1,
  1410. typename _Alloc1>
  1411. friend bool
  1412. operator==(const unordered_multiset<_Value1, _Hash1, _Pred1, _Alloc1>&,
  1413. const unordered_multiset<_Value1, _Hash1, _Pred1, _Alloc1>&);
  1414. };
  1415. #if __cpp_deduction_guides >= 201606
  1416. template<typename _InputIterator,
  1417. typename _Hash =
  1418. hash<typename iterator_traits<_InputIterator>::value_type>,
  1419. typename _Pred =
  1420. equal_to<typename iterator_traits<_InputIterator>::value_type>,
  1421. typename _Allocator =
  1422. allocator<typename iterator_traits<_InputIterator>::value_type>,
  1423. typename = _RequireInputIter<_InputIterator>,
  1424. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  1425. typename = _RequireNotAllocator<_Pred>,
  1426. typename = _RequireAllocator<_Allocator>>
  1427. unordered_multiset(_InputIterator, _InputIterator,
  1428. unordered_multiset<int>::size_type = {},
  1429. _Hash = _Hash(), _Pred = _Pred(),
  1430. _Allocator = _Allocator())
  1431. -> unordered_multiset<typename iterator_traits<_InputIterator>::value_type,
  1432. _Hash, _Pred, _Allocator>;
  1433. template<typename _Tp, typename _Hash = hash<_Tp>,
  1434. typename _Pred = equal_to<_Tp>,
  1435. typename _Allocator = allocator<_Tp>,
  1436. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  1437. typename = _RequireNotAllocator<_Pred>,
  1438. typename = _RequireAllocator<_Allocator>>
  1439. unordered_multiset(initializer_list<_Tp>,
  1440. unordered_multiset<int>::size_type = {},
  1441. _Hash = _Hash(), _Pred = _Pred(),
  1442. _Allocator = _Allocator())
  1443. -> unordered_multiset<_Tp, _Hash, _Pred, _Allocator>;
  1444. template<typename _InputIterator, typename _Allocator,
  1445. typename = _RequireInputIter<_InputIterator>,
  1446. typename = _RequireAllocator<_Allocator>>
  1447. unordered_multiset(_InputIterator, _InputIterator,
  1448. unordered_multiset<int>::size_type, _Allocator)
  1449. -> unordered_multiset<typename iterator_traits<_InputIterator>::value_type,
  1450. hash<typename
  1451. iterator_traits<_InputIterator>::value_type>,
  1452. equal_to<typename
  1453. iterator_traits<_InputIterator>::value_type>,
  1454. _Allocator>;
  1455. template<typename _InputIterator, typename _Hash, typename _Allocator,
  1456. typename = _RequireInputIter<_InputIterator>,
  1457. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  1458. typename = _RequireAllocator<_Allocator>>
  1459. unordered_multiset(_InputIterator, _InputIterator,
  1460. unordered_multiset<int>::size_type,
  1461. _Hash, _Allocator)
  1462. -> unordered_multiset<typename
  1463. iterator_traits<_InputIterator>::value_type,
  1464. _Hash,
  1465. equal_to<
  1466. typename
  1467. iterator_traits<_InputIterator>::value_type>,
  1468. _Allocator>;
  1469. template<typename _Tp, typename _Allocator,
  1470. typename = _RequireAllocator<_Allocator>>
  1471. unordered_multiset(initializer_list<_Tp>,
  1472. unordered_multiset<int>::size_type, _Allocator)
  1473. -> unordered_multiset<_Tp, hash<_Tp>, equal_to<_Tp>, _Allocator>;
  1474. template<typename _Tp, typename _Hash, typename _Allocator,
  1475. typename = _RequireNotAllocatorOrIntegral<_Hash>,
  1476. typename = _RequireAllocator<_Allocator>>
  1477. unordered_multiset(initializer_list<_Tp>,
  1478. unordered_multiset<int>::size_type, _Hash, _Allocator)
  1479. -> unordered_multiset<_Tp, _Hash, equal_to<_Tp>, _Allocator>;
  1480. #endif
  1481. template<class _Value, class _Hash, class _Pred, class _Alloc>
  1482. inline void
  1483. swap(unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
  1484. unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
  1485. noexcept(noexcept(__x.swap(__y)))
  1486. { __x.swap(__y); }
  1487. template<class _Value, class _Hash, class _Pred, class _Alloc>
  1488. inline void
  1489. swap(unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
  1490. unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
  1491. noexcept(noexcept(__x.swap(__y)))
  1492. { __x.swap(__y); }
  1493. template<class _Value, class _Hash, class _Pred, class _Alloc>
  1494. inline bool
  1495. operator==(const unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
  1496. const unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
  1497. { return __x._M_h._M_equal(__y._M_h); }
  1498. #if __cpp_impl_three_way_comparison < 201907L
  1499. template<class _Value, class _Hash, class _Pred, class _Alloc>
  1500. inline bool
  1501. operator!=(const unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
  1502. const unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
  1503. { return !(__x == __y); }
  1504. #endif
  1505. template<class _Value, class _Hash, class _Pred, class _Alloc>
  1506. inline bool
  1507. operator==(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
  1508. const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
  1509. { return __x._M_h._M_equal(__y._M_h); }
  1510. #if __cpp_impl_three_way_comparison < 201907L
  1511. template<class _Value, class _Hash, class _Pred, class _Alloc>
  1512. inline bool
  1513. operator!=(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
  1514. const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
  1515. { return !(__x == __y); }
  1516. #endif
  1517. _GLIBCXX_END_NAMESPACE_CONTAINER
  1518. #if __cplusplus > 201402L
  1519. // Allow std::unordered_set access to internals of compatible sets.
  1520. template<typename _Val, typename _Hash1, typename _Eq1, typename _Alloc,
  1521. typename _Hash2, typename _Eq2>
  1522. struct _Hash_merge_helper<
  1523. _GLIBCXX_STD_C::unordered_set<_Val, _Hash1, _Eq1, _Alloc>, _Hash2, _Eq2>
  1524. {
  1525. private:
  1526. template<typename... _Tp>
  1527. using unordered_set = _GLIBCXX_STD_C::unordered_set<_Tp...>;
  1528. template<typename... _Tp>
  1529. using unordered_multiset = _GLIBCXX_STD_C::unordered_multiset<_Tp...>;
  1530. friend unordered_set<_Val, _Hash1, _Eq1, _Alloc>;
  1531. static auto&
  1532. _S_get_table(unordered_set<_Val, _Hash2, _Eq2, _Alloc>& __set)
  1533. { return __set._M_h; }
  1534. static auto&
  1535. _S_get_table(unordered_multiset<_Val, _Hash2, _Eq2, _Alloc>& __set)
  1536. { return __set._M_h; }
  1537. };
  1538. // Allow std::unordered_multiset access to internals of compatible sets.
  1539. template<typename _Val, typename _Hash1, typename _Eq1, typename _Alloc,
  1540. typename _Hash2, typename _Eq2>
  1541. struct _Hash_merge_helper<
  1542. _GLIBCXX_STD_C::unordered_multiset<_Val, _Hash1, _Eq1, _Alloc>,
  1543. _Hash2, _Eq2>
  1544. {
  1545. private:
  1546. template<typename... _Tp>
  1547. using unordered_set = _GLIBCXX_STD_C::unordered_set<_Tp...>;
  1548. template<typename... _Tp>
  1549. using unordered_multiset = _GLIBCXX_STD_C::unordered_multiset<_Tp...>;
  1550. friend unordered_multiset<_Val, _Hash1, _Eq1, _Alloc>;
  1551. static auto&
  1552. _S_get_table(unordered_set<_Val, _Hash2, _Eq2, _Alloc>& __set)
  1553. { return __set._M_h; }
  1554. static auto&
  1555. _S_get_table(unordered_multiset<_Val, _Hash2, _Eq2, _Alloc>& __set)
  1556. { return __set._M_h; }
  1557. };
  1558. #endif // C++17
  1559. _GLIBCXX_END_NAMESPACE_VERSION
  1560. } // namespace std
  1561. #endif /* _UNORDERED_SET_H */