unordered_set.h 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779
  1. // unordered_set implementation -*- C++ -*-
  2. // Copyright (C) 2010-2019 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. template<class _Value, class _Hash, class _Pred, class _Alloc>
  1499. inline bool
  1500. operator!=(const unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
  1501. const unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
  1502. { return !(__x == __y); }
  1503. template<class _Value, class _Hash, class _Pred, class _Alloc>
  1504. inline bool
  1505. operator==(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
  1506. const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
  1507. { return __x._M_h._M_equal(__y._M_h); }
  1508. template<class _Value, class _Hash, class _Pred, class _Alloc>
  1509. inline bool
  1510. operator!=(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
  1511. const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
  1512. { return !(__x == __y); }
  1513. _GLIBCXX_END_NAMESPACE_CONTAINER
  1514. #if __cplusplus > 201402L
  1515. // Allow std::unordered_set access to internals of compatible sets.
  1516. template<typename _Val, typename _Hash1, typename _Eq1, typename _Alloc,
  1517. typename _Hash2, typename _Eq2>
  1518. struct _Hash_merge_helper<
  1519. _GLIBCXX_STD_C::unordered_set<_Val, _Hash1, _Eq1, _Alloc>, _Hash2, _Eq2>
  1520. {
  1521. private:
  1522. template<typename... _Tp>
  1523. using unordered_set = _GLIBCXX_STD_C::unordered_set<_Tp...>;
  1524. template<typename... _Tp>
  1525. using unordered_multiset = _GLIBCXX_STD_C::unordered_multiset<_Tp...>;
  1526. friend unordered_set<_Val, _Hash1, _Eq1, _Alloc>;
  1527. static auto&
  1528. _S_get_table(unordered_set<_Val, _Hash2, _Eq2, _Alloc>& __set)
  1529. { return __set._M_h; }
  1530. static auto&
  1531. _S_get_table(unordered_multiset<_Val, _Hash2, _Eq2, _Alloc>& __set)
  1532. { return __set._M_h; }
  1533. };
  1534. // Allow std::unordered_multiset access to internals of compatible sets.
  1535. template<typename _Val, typename _Hash1, typename _Eq1, typename _Alloc,
  1536. typename _Hash2, typename _Eq2>
  1537. struct _Hash_merge_helper<
  1538. _GLIBCXX_STD_C::unordered_multiset<_Val, _Hash1, _Eq1, _Alloc>,
  1539. _Hash2, _Eq2>
  1540. {
  1541. private:
  1542. template<typename... _Tp>
  1543. using unordered_set = _GLIBCXX_STD_C::unordered_set<_Tp...>;
  1544. template<typename... _Tp>
  1545. using unordered_multiset = _GLIBCXX_STD_C::unordered_multiset<_Tp...>;
  1546. friend unordered_multiset<_Val, _Hash1, _Eq1, _Alloc>;
  1547. static auto&
  1548. _S_get_table(unordered_set<_Val, _Hash2, _Eq2, _Alloc>& __set)
  1549. { return __set._M_h; }
  1550. static auto&
  1551. _S_get_table(unordered_multiset<_Val, _Hash2, _Eq2, _Alloc>& __set)
  1552. { return __set._M_h; }
  1553. };
  1554. #endif // C++17
  1555. _GLIBCXX_END_NAMESPACE_VERSION
  1556. } // namespace std
  1557. #endif /* _UNORDERED_SET_H */