unordered_set.h 61 KB

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