unordered_set.h 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745
  1. // unordered_set implementation -*- C++ -*-
  2. // Copyright (C) 2010-2018 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/unordered_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. 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. //@{
  591. /**
  592. * @brief Finds a subsequence matching given key.
  593. * @param __x Key to be located.
  594. * @return Pair of iterators that possibly points to the subsequence
  595. * matching given key.
  596. *
  597. * This function probably only makes sense for multisets.
  598. */
  599. std::pair<iterator, iterator>
  600. equal_range(const key_type& __x)
  601. { return _M_h.equal_range(__x); }
  602. std::pair<const_iterator, const_iterator>
  603. equal_range(const key_type& __x) const
  604. { return _M_h.equal_range(__x); }
  605. //@}
  606. // bucket interface.
  607. /// Returns the number of buckets of the %unordered_set.
  608. size_type
  609. bucket_count() const noexcept
  610. { return _M_h.bucket_count(); }
  611. /// Returns the maximum number of buckets of the %unordered_set.
  612. size_type
  613. max_bucket_count() const noexcept
  614. { return _M_h.max_bucket_count(); }
  615. /*
  616. * @brief Returns the number of elements in a given bucket.
  617. * @param __n A bucket index.
  618. * @return The number of elements in the bucket.
  619. */
  620. size_type
  621. bucket_size(size_type __n) const
  622. { return _M_h.bucket_size(__n); }
  623. /*
  624. * @brief Returns the bucket index of a given element.
  625. * @param __key A key instance.
  626. * @return The key bucket index.
  627. */
  628. size_type
  629. bucket(const key_type& __key) const
  630. { return _M_h.bucket(__key); }
  631. //@{
  632. /**
  633. * @brief Returns a read-only (constant) iterator pointing to the first
  634. * bucket element.
  635. * @param __n The bucket index.
  636. * @return A read-only local iterator.
  637. */
  638. local_iterator
  639. begin(size_type __n)
  640. { return _M_h.begin(__n); }
  641. const_local_iterator
  642. begin(size_type __n) const
  643. { return _M_h.begin(__n); }
  644. const_local_iterator
  645. cbegin(size_type __n) const
  646. { return _M_h.cbegin(__n); }
  647. //@}
  648. //@{
  649. /**
  650. * @brief Returns a read-only (constant) iterator pointing to one past
  651. * the last bucket elements.
  652. * @param __n The bucket index.
  653. * @return A read-only local iterator.
  654. */
  655. local_iterator
  656. end(size_type __n)
  657. { return _M_h.end(__n); }
  658. const_local_iterator
  659. end(size_type __n) const
  660. { return _M_h.end(__n); }
  661. const_local_iterator
  662. cend(size_type __n) const
  663. { return _M_h.cend(__n); }
  664. //@}
  665. // hash policy.
  666. /// Returns the average number of elements per bucket.
  667. float
  668. load_factor() const noexcept
  669. { return _M_h.load_factor(); }
  670. /// Returns a positive number that the %unordered_set tries to keep the
  671. /// load factor less than or equal to.
  672. float
  673. max_load_factor() const noexcept
  674. { return _M_h.max_load_factor(); }
  675. /**
  676. * @brief Change the %unordered_set maximum load factor.
  677. * @param __z The new maximum load factor.
  678. */
  679. void
  680. max_load_factor(float __z)
  681. { _M_h.max_load_factor(__z); }
  682. /**
  683. * @brief May rehash the %unordered_set.
  684. * @param __n The new number of buckets.
  685. *
  686. * Rehash will occur only if the new number of buckets respect the
  687. * %unordered_set maximum load factor.
  688. */
  689. void
  690. rehash(size_type __n)
  691. { _M_h.rehash(__n); }
  692. /**
  693. * @brief Prepare the %unordered_set for a specified number of
  694. * elements.
  695. * @param __n Number of elements required.
  696. *
  697. * Same as rehash(ceil(n / max_load_factor())).
  698. */
  699. void
  700. reserve(size_type __n)
  701. { _M_h.reserve(__n); }
  702. template<typename _Value1, typename _Hash1, typename _Pred1,
  703. typename _Alloc1>
  704. friend bool
  705. operator==(const unordered_set<_Value1, _Hash1, _Pred1, _Alloc1>&,
  706. const unordered_set<_Value1, _Hash1, _Pred1, _Alloc1>&);
  707. };
  708. #if __cpp_deduction_guides >= 201606
  709. template<typename _InputIterator,
  710. typename _Hash =
  711. hash<typename iterator_traits<_InputIterator>::value_type>,
  712. typename _Pred =
  713. equal_to<typename iterator_traits<_InputIterator>::value_type>,
  714. typename _Allocator =
  715. allocator<typename iterator_traits<_InputIterator>::value_type>,
  716. typename = _RequireInputIter<_InputIterator>,
  717. typename = _RequireAllocator<_Allocator>>
  718. unordered_set(_InputIterator, _InputIterator,
  719. unordered_set<int>::size_type = {},
  720. _Hash = _Hash(), _Pred = _Pred(), _Allocator = _Allocator())
  721. -> unordered_set<typename iterator_traits<_InputIterator>::value_type,
  722. _Hash, _Pred, _Allocator>;
  723. template<typename _Tp, typename _Hash = hash<_Tp>,
  724. typename _Pred = equal_to<_Tp>,
  725. typename _Allocator = allocator<_Tp>,
  726. typename = _RequireAllocator<_Allocator>>
  727. unordered_set(initializer_list<_Tp>,
  728. unordered_set<int>::size_type = {},
  729. _Hash = _Hash(), _Pred = _Pred(), _Allocator = _Allocator())
  730. -> unordered_set<_Tp, _Hash, _Pred, _Allocator>;
  731. template<typename _InputIterator, typename _Allocator,
  732. typename = _RequireInputIter<_InputIterator>,
  733. typename = _RequireAllocator<_Allocator>>
  734. unordered_set(_InputIterator, _InputIterator,
  735. unordered_set<int>::size_type, _Allocator)
  736. -> unordered_set<typename iterator_traits<_InputIterator>::value_type,
  737. hash<
  738. typename iterator_traits<_InputIterator>::value_type>,
  739. equal_to<
  740. typename iterator_traits<_InputIterator>::value_type>,
  741. _Allocator>;
  742. template<typename _InputIterator, typename _Hash, typename _Allocator,
  743. typename = _RequireInputIter<_InputIterator>,
  744. typename = _RequireAllocator<_Allocator>>
  745. unordered_set(_InputIterator, _InputIterator,
  746. unordered_set<int>::size_type,
  747. _Hash, _Allocator)
  748. -> unordered_set<typename iterator_traits<_InputIterator>::value_type,
  749. _Hash,
  750. equal_to<
  751. typename iterator_traits<_InputIterator>::value_type>,
  752. _Allocator>;
  753. template<typename _Tp, typename _Allocator,
  754. typename = _RequireAllocator<_Allocator>>
  755. unordered_set(initializer_list<_Tp>,
  756. unordered_set<int>::size_type, _Allocator)
  757. -> unordered_set<_Tp, hash<_Tp>, equal_to<_Tp>, _Allocator>;
  758. template<typename _Tp, typename _Hash, typename _Allocator,
  759. typename = _RequireAllocator<_Allocator>>
  760. unordered_set(initializer_list<_Tp>,
  761. unordered_set<int>::size_type, _Hash, _Allocator)
  762. -> unordered_set<_Tp, _Hash, equal_to<_Tp>, _Allocator>;
  763. #endif
  764. /**
  765. * @brief A standard container composed of equivalent keys
  766. * (possibly containing multiple of each key value) in which the
  767. * elements' keys are the elements themselves.
  768. *
  769. * @ingroup unordered_associative_containers
  770. *
  771. * @tparam _Value Type of key objects.
  772. * @tparam _Hash Hashing function object type, defaults to hash<_Value>.
  773. * @tparam _Pred Predicate function object type, defaults
  774. * to equal_to<_Value>.
  775. * @tparam _Alloc Allocator type, defaults to allocator<_Key>.
  776. *
  777. * Meets the requirements of a <a href="tables.html#65">container</a>, and
  778. * <a href="tables.html#xx">unordered associative container</a>
  779. *
  780. * Base is _Hashtable, dispatched at compile time via template
  781. * alias __umset_hashtable.
  782. */
  783. template<typename _Value,
  784. typename _Hash = hash<_Value>,
  785. typename _Pred = equal_to<_Value>,
  786. typename _Alloc = allocator<_Value>>
  787. class unordered_multiset
  788. {
  789. typedef __umset_hashtable<_Value, _Hash, _Pred, _Alloc> _Hashtable;
  790. _Hashtable _M_h;
  791. public:
  792. // typedefs:
  793. //@{
  794. /// Public typedefs.
  795. typedef typename _Hashtable::key_type key_type;
  796. typedef typename _Hashtable::value_type value_type;
  797. typedef typename _Hashtable::hasher hasher;
  798. typedef typename _Hashtable::key_equal key_equal;
  799. typedef typename _Hashtable::allocator_type allocator_type;
  800. //@}
  801. //@{
  802. /// Iterator-related typedefs.
  803. typedef typename _Hashtable::pointer pointer;
  804. typedef typename _Hashtable::const_pointer const_pointer;
  805. typedef typename _Hashtable::reference reference;
  806. typedef typename _Hashtable::const_reference const_reference;
  807. typedef typename _Hashtable::iterator iterator;
  808. typedef typename _Hashtable::const_iterator const_iterator;
  809. typedef typename _Hashtable::local_iterator local_iterator;
  810. typedef typename _Hashtable::const_local_iterator const_local_iterator;
  811. typedef typename _Hashtable::size_type size_type;
  812. typedef typename _Hashtable::difference_type difference_type;
  813. //@}
  814. #if __cplusplus > 201402L
  815. using node_type = typename _Hashtable::node_type;
  816. #endif
  817. // construct/destroy/copy
  818. /// Default constructor.
  819. unordered_multiset() = default;
  820. /**
  821. * @brief Default constructor creates no elements.
  822. * @param __n Minimal initial number of buckets.
  823. * @param __hf A hash functor.
  824. * @param __eql A key equality functor.
  825. * @param __a An allocator object.
  826. */
  827. explicit
  828. unordered_multiset(size_type __n,
  829. const hasher& __hf = hasher(),
  830. const key_equal& __eql = key_equal(),
  831. const allocator_type& __a = allocator_type())
  832. : _M_h(__n, __hf, __eql, __a)
  833. { }
  834. /**
  835. * @brief Builds an %unordered_multiset from a range.
  836. * @param __first An input iterator.
  837. * @param __last An input iterator.
  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. * Create an %unordered_multiset consisting of copies of the elements
  844. * from [__first,__last). This is linear in N (where N is
  845. * distance(__first,__last)).
  846. */
  847. template<typename _InputIterator>
  848. unordered_multiset(_InputIterator __first, _InputIterator __last,
  849. size_type __n = 0,
  850. const hasher& __hf = hasher(),
  851. const key_equal& __eql = key_equal(),
  852. const allocator_type& __a = allocator_type())
  853. : _M_h(__first, __last, __n, __hf, __eql, __a)
  854. { }
  855. /// Copy constructor.
  856. unordered_multiset(const unordered_multiset&) = default;
  857. /// Move constructor.
  858. unordered_multiset(unordered_multiset&&) = default;
  859. /**
  860. * @brief Builds an %unordered_multiset from an initializer_list.
  861. * @param __l An initializer_list.
  862. * @param __n Minimal initial number of buckets.
  863. * @param __hf A hash functor.
  864. * @param __eql A key equality functor.
  865. * @param __a An allocator object.
  866. *
  867. * Create an %unordered_multiset consisting of copies of the elements in
  868. * the list. This is linear in N (where N is @a __l.size()).
  869. */
  870. unordered_multiset(initializer_list<value_type> __l,
  871. size_type __n = 0,
  872. const hasher& __hf = hasher(),
  873. const key_equal& __eql = key_equal(),
  874. const allocator_type& __a = allocator_type())
  875. : _M_h(__l, __n, __hf, __eql, __a)
  876. { }
  877. /// Copy assignment operator.
  878. unordered_multiset&
  879. operator=(const unordered_multiset&) = default;
  880. /// Move assignment operator.
  881. unordered_multiset&
  882. operator=(unordered_multiset&&) = default;
  883. /**
  884. * @brief Creates an %unordered_multiset with no elements.
  885. * @param __a An allocator object.
  886. */
  887. explicit
  888. unordered_multiset(const allocator_type& __a)
  889. : _M_h(__a)
  890. { }
  891. /*
  892. * @brief Copy constructor with allocator argument.
  893. * @param __uset Input %unordered_multiset to copy.
  894. * @param __a An allocator object.
  895. */
  896. unordered_multiset(const unordered_multiset& __umset,
  897. const allocator_type& __a)
  898. : _M_h(__umset._M_h, __a)
  899. { }
  900. /*
  901. * @brief Move constructor with allocator argument.
  902. * @param __umset Input %unordered_multiset to move.
  903. * @param __a An allocator object.
  904. */
  905. unordered_multiset(unordered_multiset&& __umset,
  906. const allocator_type& __a)
  907. : _M_h(std::move(__umset._M_h), __a)
  908. { }
  909. unordered_multiset(size_type __n, const allocator_type& __a)
  910. : unordered_multiset(__n, hasher(), key_equal(), __a)
  911. { }
  912. unordered_multiset(size_type __n, const hasher& __hf,
  913. const allocator_type& __a)
  914. : unordered_multiset(__n, __hf, key_equal(), __a)
  915. { }
  916. template<typename _InputIterator>
  917. unordered_multiset(_InputIterator __first, _InputIterator __last,
  918. size_type __n,
  919. const allocator_type& __a)
  920. : unordered_multiset(__first, __last, __n, hasher(), key_equal(), __a)
  921. { }
  922. template<typename _InputIterator>
  923. unordered_multiset(_InputIterator __first, _InputIterator __last,
  924. size_type __n, const hasher& __hf,
  925. const allocator_type& __a)
  926. : unordered_multiset(__first, __last, __n, __hf, key_equal(), __a)
  927. { }
  928. unordered_multiset(initializer_list<value_type> __l,
  929. size_type __n,
  930. const allocator_type& __a)
  931. : unordered_multiset(__l, __n, hasher(), key_equal(), __a)
  932. { }
  933. unordered_multiset(initializer_list<value_type> __l,
  934. size_type __n, const hasher& __hf,
  935. const allocator_type& __a)
  936. : unordered_multiset(__l, __n, __hf, key_equal(), __a)
  937. { }
  938. /**
  939. * @brief %Unordered_multiset list assignment operator.
  940. * @param __l An initializer_list.
  941. *
  942. * This function fills an %unordered_multiset with copies of the elements
  943. * in the initializer list @a __l.
  944. *
  945. * Note that the assignment completely changes the %unordered_multiset
  946. * and that the resulting %unordered_multiset's size is the same as the
  947. * number of elements assigned.
  948. */
  949. unordered_multiset&
  950. operator=(initializer_list<value_type> __l)
  951. {
  952. _M_h = __l;
  953. return *this;
  954. }
  955. /// Returns the allocator object used by the %unordered_multiset.
  956. allocator_type
  957. get_allocator() const noexcept
  958. { return _M_h.get_allocator(); }
  959. // size and capacity:
  960. /// Returns true if the %unordered_multiset is empty.
  961. bool
  962. empty() const noexcept
  963. { return _M_h.empty(); }
  964. /// Returns the size of the %unordered_multiset.
  965. size_type
  966. size() const noexcept
  967. { return _M_h.size(); }
  968. /// Returns the maximum size of the %unordered_multiset.
  969. size_type
  970. max_size() const noexcept
  971. { return _M_h.max_size(); }
  972. // iterators.
  973. //@{
  974. /**
  975. * Returns a read-only (constant) iterator that points to the first
  976. * element in the %unordered_multiset.
  977. */
  978. iterator
  979. begin() noexcept
  980. { return _M_h.begin(); }
  981. const_iterator
  982. begin() const noexcept
  983. { return _M_h.begin(); }
  984. //@}
  985. //@{
  986. /**
  987. * Returns a read-only (constant) iterator that points one past the last
  988. * element in the %unordered_multiset.
  989. */
  990. iterator
  991. end() noexcept
  992. { return _M_h.end(); }
  993. const_iterator
  994. end() const noexcept
  995. { return _M_h.end(); }
  996. //@}
  997. /**
  998. * Returns a read-only (constant) iterator that points to the first
  999. * element in the %unordered_multiset.
  1000. */
  1001. const_iterator
  1002. cbegin() const noexcept
  1003. { return _M_h.begin(); }
  1004. /**
  1005. * Returns a read-only (constant) iterator that points one past the last
  1006. * element in the %unordered_multiset.
  1007. */
  1008. const_iterator
  1009. cend() const noexcept
  1010. { return _M_h.end(); }
  1011. // modifiers.
  1012. /**
  1013. * @brief Builds and insert an element into the %unordered_multiset.
  1014. * @param __args Arguments used to generate an element.
  1015. * @return An iterator that points to the inserted element.
  1016. *
  1017. * Insertion requires amortized constant time.
  1018. */
  1019. template<typename... _Args>
  1020. iterator
  1021. emplace(_Args&&... __args)
  1022. { return _M_h.emplace(std::forward<_Args>(__args)...); }
  1023. /**
  1024. * @brief Inserts an element into the %unordered_multiset.
  1025. * @param __pos An iterator that serves as a hint as to where the
  1026. * element should be inserted.
  1027. * @param __args Arguments used to generate the element to be
  1028. * inserted.
  1029. * @return An iterator that points to the inserted element.
  1030. *
  1031. * Note that the first parameter is only a hint and can potentially
  1032. * improve the performance of the insertion process. A bad hint would
  1033. * cause no gains in efficiency.
  1034. *
  1035. * For more on @a hinting, see:
  1036. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
  1037. *
  1038. * Insertion requires amortized constant time.
  1039. */
  1040. template<typename... _Args>
  1041. iterator
  1042. emplace_hint(const_iterator __pos, _Args&&... __args)
  1043. { return _M_h.emplace_hint(__pos, std::forward<_Args>(__args)...); }
  1044. //@{
  1045. /**
  1046. * @brief Inserts an element into the %unordered_multiset.
  1047. * @param __x Element to be inserted.
  1048. * @return An iterator that points to the inserted element.
  1049. *
  1050. * Insertion requires amortized constant time.
  1051. */
  1052. iterator
  1053. insert(const value_type& __x)
  1054. { return _M_h.insert(__x); }
  1055. iterator
  1056. insert(value_type&& __x)
  1057. { return _M_h.insert(std::move(__x)); }
  1058. //@}
  1059. //@{
  1060. /**
  1061. * @brief Inserts an element into the %unordered_multiset.
  1062. * @param __hint An iterator that serves as a hint as to where the
  1063. * element should be inserted.
  1064. * @param __x Element to be inserted.
  1065. * @return An iterator that points to the inserted element.
  1066. *
  1067. * Note that the first parameter is only a hint and can potentially
  1068. * improve the performance of the insertion process. A bad hint would
  1069. * cause no gains in efficiency.
  1070. *
  1071. * For more on @a hinting, see:
  1072. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
  1073. *
  1074. * Insertion requires amortized constant.
  1075. */
  1076. iterator
  1077. insert(const_iterator __hint, const value_type& __x)
  1078. { return _M_h.insert(__hint, __x); }
  1079. iterator
  1080. insert(const_iterator __hint, value_type&& __x)
  1081. { return _M_h.insert(__hint, std::move(__x)); }
  1082. //@}
  1083. /**
  1084. * @brief A template function that inserts a range of elements.
  1085. * @param __first Iterator pointing to the start of the range to be
  1086. * inserted.
  1087. * @param __last Iterator pointing to the end of the range.
  1088. *
  1089. * Complexity similar to that of the range constructor.
  1090. */
  1091. template<typename _InputIterator>
  1092. void
  1093. insert(_InputIterator __first, _InputIterator __last)
  1094. { _M_h.insert(__first, __last); }
  1095. /**
  1096. * @brief Inserts a list of elements into the %unordered_multiset.
  1097. * @param __l A std::initializer_list<value_type> of elements to be
  1098. * inserted.
  1099. *
  1100. * Complexity similar to that of the range constructor.
  1101. */
  1102. void
  1103. insert(initializer_list<value_type> __l)
  1104. { _M_h.insert(__l); }
  1105. #if __cplusplus > 201402L
  1106. /// Extract a node.
  1107. node_type
  1108. extract(const_iterator __pos)
  1109. {
  1110. __glibcxx_assert(__pos != end());
  1111. return _M_h.extract(__pos);
  1112. }
  1113. /// Extract a node.
  1114. node_type
  1115. extract(const key_type& __key)
  1116. { return _M_h.extract(__key); }
  1117. /// Re-insert an extracted node.
  1118. iterator
  1119. insert(node_type&& __nh)
  1120. { return _M_h._M_reinsert_node_multi(cend(), std::move(__nh)); }
  1121. /// Re-insert an extracted node.
  1122. iterator
  1123. insert(const_iterator __hint, node_type&& __nh)
  1124. { return _M_h._M_reinsert_node_multi(__hint, std::move(__nh)); }
  1125. #endif // C++17
  1126. //@{
  1127. /**
  1128. * @brief Erases an element from an %unordered_multiset.
  1129. * @param __position An iterator pointing to the element to be erased.
  1130. * @return An iterator pointing to the element immediately following
  1131. * @a __position prior to the element being erased. If no such
  1132. * element exists, end() is returned.
  1133. *
  1134. * This function erases an element, pointed to by the given iterator,
  1135. * from an %unordered_multiset.
  1136. *
  1137. * Note that this function only erases the element, and that if the
  1138. * element is itself a pointer, the pointed-to memory is not touched in
  1139. * any way. Managing the pointer is the user's responsibility.
  1140. */
  1141. iterator
  1142. erase(const_iterator __position)
  1143. { return _M_h.erase(__position); }
  1144. // LWG 2059.
  1145. iterator
  1146. erase(iterator __position)
  1147. { return _M_h.erase(__position); }
  1148. //@}
  1149. /**
  1150. * @brief Erases elements according to the provided key.
  1151. * @param __x Key of element to be erased.
  1152. * @return The number of elements erased.
  1153. *
  1154. * This function erases all the elements located by the given key from
  1155. * an %unordered_multiset.
  1156. *
  1157. * Note that this function only erases the element, and that if the
  1158. * element is itself a pointer, the pointed-to memory is not touched in
  1159. * any way. Managing the pointer is the user's responsibility.
  1160. */
  1161. size_type
  1162. erase(const key_type& __x)
  1163. { return _M_h.erase(__x); }
  1164. /**
  1165. * @brief Erases a [__first,__last) range of elements from an
  1166. * %unordered_multiset.
  1167. * @param __first Iterator pointing to the start of the range to be
  1168. * erased.
  1169. * @param __last Iterator pointing to the end of the range to
  1170. * be erased.
  1171. * @return The iterator @a __last.
  1172. *
  1173. * This function erases a sequence of elements from an
  1174. * %unordered_multiset.
  1175. *
  1176. * Note that this function only erases the element, and that if
  1177. * the element is itself a pointer, the pointed-to memory is not touched
  1178. * in any way. Managing the pointer is the user's responsibility.
  1179. */
  1180. iterator
  1181. erase(const_iterator __first, const_iterator __last)
  1182. { return _M_h.erase(__first, __last); }
  1183. /**
  1184. * Erases all elements in an %unordered_multiset.
  1185. *
  1186. * Note that this function only erases the elements, and that if the
  1187. * elements themselves are pointers, the pointed-to memory is not touched
  1188. * in any way. Managing the pointer is the user's responsibility.
  1189. */
  1190. void
  1191. clear() noexcept
  1192. { _M_h.clear(); }
  1193. /**
  1194. * @brief Swaps data with another %unordered_multiset.
  1195. * @param __x An %unordered_multiset of the same element and allocator
  1196. * types.
  1197. *
  1198. * This exchanges the elements between two sets in constant time.
  1199. * Note that the global std::swap() function is specialized such that
  1200. * std::swap(s1,s2) will feed to this function.
  1201. */
  1202. void
  1203. swap(unordered_multiset& __x)
  1204. noexcept( noexcept(_M_h.swap(__x._M_h)) )
  1205. { _M_h.swap(__x._M_h); }
  1206. #if __cplusplus > 201402L
  1207. template<typename, typename, typename>
  1208. friend class std::_Hash_merge_helper;
  1209. template<typename _H2, typename _P2>
  1210. void
  1211. merge(unordered_multiset<_Value, _H2, _P2, _Alloc>& __source)
  1212. {
  1213. using _Merge_helper
  1214. = _Hash_merge_helper<unordered_multiset, _H2, _P2>;
  1215. _M_h._M_merge_multi(_Merge_helper::_S_get_table(__source));
  1216. }
  1217. template<typename _H2, typename _P2>
  1218. void
  1219. merge(unordered_multiset<_Value, _H2, _P2, _Alloc>&& __source)
  1220. { merge(__source); }
  1221. template<typename _H2, typename _P2>
  1222. void
  1223. merge(unordered_set<_Value, _H2, _P2, _Alloc>& __source)
  1224. {
  1225. using _Merge_helper
  1226. = _Hash_merge_helper<unordered_multiset, _H2, _P2>;
  1227. _M_h._M_merge_multi(_Merge_helper::_S_get_table(__source));
  1228. }
  1229. template<typename _H2, typename _P2>
  1230. void
  1231. merge(unordered_set<_Value, _H2, _P2, _Alloc>&& __source)
  1232. { merge(__source); }
  1233. #endif // C++17
  1234. // observers.
  1235. /// Returns the hash functor object with which the %unordered_multiset
  1236. /// was constructed.
  1237. hasher
  1238. hash_function() const
  1239. { return _M_h.hash_function(); }
  1240. /// Returns the key comparison object with which the %unordered_multiset
  1241. /// was constructed.
  1242. key_equal
  1243. key_eq() const
  1244. { return _M_h.key_eq(); }
  1245. // lookup.
  1246. //@{
  1247. /**
  1248. * @brief Tries to locate an element in an %unordered_multiset.
  1249. * @param __x Element to be located.
  1250. * @return Iterator pointing to sought-after element, or end() if not
  1251. * found.
  1252. *
  1253. * This function takes a key and tries to locate the element with which
  1254. * the key matches. If successful the function returns an iterator
  1255. * pointing to the sought after element. If unsuccessful it returns the
  1256. * past-the-end ( @c end() ) iterator.
  1257. */
  1258. iterator
  1259. find(const key_type& __x)
  1260. { return _M_h.find(__x); }
  1261. const_iterator
  1262. find(const key_type& __x) const
  1263. { return _M_h.find(__x); }
  1264. //@}
  1265. /**
  1266. * @brief Finds the number of elements.
  1267. * @param __x Element to located.
  1268. * @return Number of elements with specified key.
  1269. */
  1270. size_type
  1271. count(const key_type& __x) const
  1272. { return _M_h.count(__x); }
  1273. //@{
  1274. /**
  1275. * @brief Finds a subsequence matching given key.
  1276. * @param __x Key to be located.
  1277. * @return Pair of iterators that possibly points to the subsequence
  1278. * matching given key.
  1279. */
  1280. std::pair<iterator, iterator>
  1281. equal_range(const key_type& __x)
  1282. { return _M_h.equal_range(__x); }
  1283. std::pair<const_iterator, const_iterator>
  1284. equal_range(const key_type& __x) const
  1285. { return _M_h.equal_range(__x); }
  1286. //@}
  1287. // bucket interface.
  1288. /// Returns the number of buckets of the %unordered_multiset.
  1289. size_type
  1290. bucket_count() const noexcept
  1291. { return _M_h.bucket_count(); }
  1292. /// Returns the maximum number of buckets of the %unordered_multiset.
  1293. size_type
  1294. max_bucket_count() const noexcept
  1295. { return _M_h.max_bucket_count(); }
  1296. /*
  1297. * @brief Returns the number of elements in a given bucket.
  1298. * @param __n A bucket index.
  1299. * @return The number of elements in the bucket.
  1300. */
  1301. size_type
  1302. bucket_size(size_type __n) const
  1303. { return _M_h.bucket_size(__n); }
  1304. /*
  1305. * @brief Returns the bucket index of a given element.
  1306. * @param __key A key instance.
  1307. * @return The key bucket index.
  1308. */
  1309. size_type
  1310. bucket(const key_type& __key) const
  1311. { return _M_h.bucket(__key); }
  1312. //@{
  1313. /**
  1314. * @brief Returns a read-only (constant) iterator pointing to the first
  1315. * bucket element.
  1316. * @param __n The bucket index.
  1317. * @return A read-only local iterator.
  1318. */
  1319. local_iterator
  1320. begin(size_type __n)
  1321. { return _M_h.begin(__n); }
  1322. const_local_iterator
  1323. begin(size_type __n) const
  1324. { return _M_h.begin(__n); }
  1325. const_local_iterator
  1326. cbegin(size_type __n) const
  1327. { return _M_h.cbegin(__n); }
  1328. //@}
  1329. //@{
  1330. /**
  1331. * @brief Returns a read-only (constant) iterator pointing to one past
  1332. * the last bucket elements.
  1333. * @param __n The bucket index.
  1334. * @return A read-only local iterator.
  1335. */
  1336. local_iterator
  1337. end(size_type __n)
  1338. { return _M_h.end(__n); }
  1339. const_local_iterator
  1340. end(size_type __n) const
  1341. { return _M_h.end(__n); }
  1342. const_local_iterator
  1343. cend(size_type __n) const
  1344. { return _M_h.cend(__n); }
  1345. //@}
  1346. // hash policy.
  1347. /// Returns the average number of elements per bucket.
  1348. float
  1349. load_factor() const noexcept
  1350. { return _M_h.load_factor(); }
  1351. /// Returns a positive number that the %unordered_multiset tries to keep the
  1352. /// load factor less than or equal to.
  1353. float
  1354. max_load_factor() const noexcept
  1355. { return _M_h.max_load_factor(); }
  1356. /**
  1357. * @brief Change the %unordered_multiset maximum load factor.
  1358. * @param __z The new maximum load factor.
  1359. */
  1360. void
  1361. max_load_factor(float __z)
  1362. { _M_h.max_load_factor(__z); }
  1363. /**
  1364. * @brief May rehash the %unordered_multiset.
  1365. * @param __n The new number of buckets.
  1366. *
  1367. * Rehash will occur only if the new number of buckets respect the
  1368. * %unordered_multiset maximum load factor.
  1369. */
  1370. void
  1371. rehash(size_type __n)
  1372. { _M_h.rehash(__n); }
  1373. /**
  1374. * @brief Prepare the %unordered_multiset for a specified number of
  1375. * elements.
  1376. * @param __n Number of elements required.
  1377. *
  1378. * Same as rehash(ceil(n / max_load_factor())).
  1379. */
  1380. void
  1381. reserve(size_type __n)
  1382. { _M_h.reserve(__n); }
  1383. template<typename _Value1, typename _Hash1, typename _Pred1,
  1384. typename _Alloc1>
  1385. friend bool
  1386. operator==(const unordered_multiset<_Value1, _Hash1, _Pred1, _Alloc1>&,
  1387. const unordered_multiset<_Value1, _Hash1, _Pred1, _Alloc1>&);
  1388. };
  1389. #if __cpp_deduction_guides >= 201606
  1390. template<typename _InputIterator,
  1391. typename _Hash =
  1392. hash<typename iterator_traits<_InputIterator>::value_type>,
  1393. typename _Pred =
  1394. equal_to<typename iterator_traits<_InputIterator>::value_type>,
  1395. typename _Allocator =
  1396. allocator<typename iterator_traits<_InputIterator>::value_type>,
  1397. typename = _RequireInputIter<_InputIterator>,
  1398. typename = _RequireAllocator<_Allocator>>
  1399. unordered_multiset(_InputIterator, _InputIterator,
  1400. unordered_multiset<int>::size_type = {},
  1401. _Hash = _Hash(), _Pred = _Pred(),
  1402. _Allocator = _Allocator())
  1403. -> unordered_multiset<typename iterator_traits<_InputIterator>::value_type,
  1404. _Hash, _Pred, _Allocator>;
  1405. template<typename _Tp, typename _Hash = hash<_Tp>,
  1406. typename _Pred = equal_to<_Tp>,
  1407. typename _Allocator = allocator<_Tp>,
  1408. typename = _RequireAllocator<_Allocator>>
  1409. unordered_multiset(initializer_list<_Tp>,
  1410. unordered_multiset<int>::size_type = {},
  1411. _Hash = _Hash(), _Pred = _Pred(),
  1412. _Allocator = _Allocator())
  1413. -> unordered_multiset<_Tp, _Hash, _Pred, _Allocator>;
  1414. template<typename _InputIterator, typename _Allocator,
  1415. typename = _RequireInputIter<_InputIterator>,
  1416. typename = _RequireAllocator<_Allocator>>
  1417. unordered_multiset(_InputIterator, _InputIterator,
  1418. unordered_multiset<int>::size_type, _Allocator)
  1419. -> unordered_multiset<typename iterator_traits<_InputIterator>::value_type,
  1420. hash<typename
  1421. iterator_traits<_InputIterator>::value_type>,
  1422. equal_to<typename
  1423. iterator_traits<_InputIterator>::value_type>,
  1424. _Allocator>;
  1425. template<typename _InputIterator, typename _Hash, typename _Allocator,
  1426. typename = _RequireInputIter<_InputIterator>,
  1427. typename = _RequireAllocator<_Allocator>>
  1428. unordered_multiset(_InputIterator, _InputIterator,
  1429. unordered_multiset<int>::size_type,
  1430. _Hash, _Allocator)
  1431. -> unordered_multiset<typename
  1432. iterator_traits<_InputIterator>::value_type,
  1433. _Hash,
  1434. equal_to<
  1435. typename
  1436. iterator_traits<_InputIterator>::value_type>,
  1437. _Allocator>;
  1438. template<typename _Tp, typename _Allocator,
  1439. typename = _RequireAllocator<_Allocator>>
  1440. unordered_multiset(initializer_list<_Tp>,
  1441. unordered_multiset<int>::size_type, _Allocator)
  1442. -> unordered_multiset<_Tp, hash<_Tp>, equal_to<_Tp>, _Allocator>;
  1443. template<typename _Tp, typename _Hash, typename _Allocator,
  1444. typename = _RequireAllocator<_Allocator>>
  1445. unordered_multiset(initializer_list<_Tp>,
  1446. unordered_multiset<int>::size_type, _Hash, _Allocator)
  1447. -> unordered_multiset<_Tp, _Hash, equal_to<_Tp>, _Allocator>;
  1448. #endif
  1449. template<class _Value, class _Hash, class _Pred, class _Alloc>
  1450. inline void
  1451. swap(unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
  1452. unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
  1453. noexcept(noexcept(__x.swap(__y)))
  1454. { __x.swap(__y); }
  1455. template<class _Value, class _Hash, class _Pred, class _Alloc>
  1456. inline void
  1457. swap(unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
  1458. unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
  1459. noexcept(noexcept(__x.swap(__y)))
  1460. { __x.swap(__y); }
  1461. template<class _Value, class _Hash, class _Pred, class _Alloc>
  1462. inline bool
  1463. operator==(const unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
  1464. const unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
  1465. { return __x._M_h._M_equal(__y._M_h); }
  1466. template<class _Value, class _Hash, class _Pred, class _Alloc>
  1467. inline bool
  1468. operator!=(const unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
  1469. const unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
  1470. { return !(__x == __y); }
  1471. template<class _Value, class _Hash, class _Pred, class _Alloc>
  1472. inline bool
  1473. operator==(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
  1474. const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
  1475. { return __x._M_h._M_equal(__y._M_h); }
  1476. template<class _Value, class _Hash, class _Pred, class _Alloc>
  1477. inline bool
  1478. operator!=(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
  1479. const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
  1480. { return !(__x == __y); }
  1481. _GLIBCXX_END_NAMESPACE_CONTAINER
  1482. #if __cplusplus > 201402L
  1483. // Allow std::unordered_set access to internals of compatible sets.
  1484. template<typename _Val, typename _Hash1, typename _Eq1, typename _Alloc,
  1485. typename _Hash2, typename _Eq2>
  1486. struct _Hash_merge_helper<
  1487. _GLIBCXX_STD_C::unordered_set<_Val, _Hash1, _Eq1, _Alloc>, _Hash2, _Eq2>
  1488. {
  1489. private:
  1490. template<typename... _Tp>
  1491. using unordered_set = _GLIBCXX_STD_C::unordered_set<_Tp...>;
  1492. template<typename... _Tp>
  1493. using unordered_multiset = _GLIBCXX_STD_C::unordered_multiset<_Tp...>;
  1494. friend unordered_set<_Val, _Hash1, _Eq1, _Alloc>;
  1495. static auto&
  1496. _S_get_table(unordered_set<_Val, _Hash2, _Eq2, _Alloc>& __set)
  1497. { return __set._M_h; }
  1498. static auto&
  1499. _S_get_table(unordered_multiset<_Val, _Hash2, _Eq2, _Alloc>& __set)
  1500. { return __set._M_h; }
  1501. };
  1502. // Allow std::unordered_multiset access to internals of compatible sets.
  1503. template<typename _Val, typename _Hash1, typename _Eq1, typename _Alloc,
  1504. typename _Hash2, typename _Eq2>
  1505. struct _Hash_merge_helper<
  1506. _GLIBCXX_STD_C::unordered_multiset<_Val, _Hash1, _Eq1, _Alloc>,
  1507. _Hash2, _Eq2>
  1508. {
  1509. private:
  1510. template<typename... _Tp>
  1511. using unordered_set = _GLIBCXX_STD_C::unordered_set<_Tp...>;
  1512. template<typename... _Tp>
  1513. using unordered_multiset = _GLIBCXX_STD_C::unordered_multiset<_Tp...>;
  1514. friend unordered_multiset<_Val, _Hash1, _Eq1, _Alloc>;
  1515. static auto&
  1516. _S_get_table(unordered_set<_Val, _Hash2, _Eq2, _Alloc>& __set)
  1517. { return __set._M_h; }
  1518. static auto&
  1519. _S_get_table(unordered_multiset<_Val, _Hash2, _Eq2, _Alloc>& __set)
  1520. { return __set._M_h; }
  1521. };
  1522. #endif // C++17
  1523. _GLIBCXX_END_NAMESPACE_VERSION
  1524. } // namespace std
  1525. #endif /* _UNORDERED_SET_H */