stl_multiset.h 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  1. // Multiset implementation -*- C++ -*-
  2. // Copyright (C) 2001-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /*
  21. *
  22. * Copyright (c) 1994
  23. * Hewlett-Packard Company
  24. *
  25. * Permission to use, copy, modify, distribute and sell this software
  26. * and its documentation for any purpose is hereby granted without fee,
  27. * provided that the above copyright notice appear in all copies and
  28. * that both that copyright notice and this permission notice appear
  29. * in supporting documentation. Hewlett-Packard Company makes no
  30. * representations about the suitability of this software for any
  31. * purpose. It is provided "as is" without express or implied warranty.
  32. *
  33. *
  34. * Copyright (c) 1996
  35. * Silicon Graphics Computer Systems, Inc.
  36. *
  37. * Permission to use, copy, modify, distribute and sell this software
  38. * and its documentation for any purpose is hereby granted without fee,
  39. * provided that the above copyright notice appear in all copies and
  40. * that both that copyright notice and this permission notice appear
  41. * in supporting documentation. Silicon Graphics makes no
  42. * representations about the suitability of this software for any
  43. * purpose. It is provided "as is" without express or implied warranty.
  44. */
  45. /** @file bits/stl_multiset.h
  46. * This is an internal header file, included by other library headers.
  47. * Do not attempt to use it directly. @headername{set}
  48. */
  49. #ifndef _STL_MULTISET_H
  50. #define _STL_MULTISET_H 1
  51. #include <bits/concept_check.h>
  52. #if __cplusplus >= 201103L
  53. #include <initializer_list>
  54. #endif
  55. namespace std _GLIBCXX_VISIBILITY(default)
  56. {
  57. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  58. _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
  59. template<typename _Key, typename _Compare, typename _Alloc>
  60. class set;
  61. /**
  62. * @brief A standard container made up of elements, which can be retrieved
  63. * in logarithmic time.
  64. *
  65. * @ingroup associative_containers
  66. *
  67. *
  68. * @tparam _Key Type of key objects.
  69. * @tparam _Compare Comparison function object type, defaults to less<_Key>.
  70. * @tparam _Alloc Allocator type, defaults to allocator<_Key>.
  71. *
  72. * Meets the requirements of a <a href="tables.html#65">container</a>, a
  73. * <a href="tables.html#66">reversible container</a>, and an
  74. * <a href="tables.html#69">associative container</a> (using equivalent
  75. * keys). For a @c multiset<Key> the key_type and value_type are Key.
  76. *
  77. * Multisets support bidirectional iterators.
  78. *
  79. * The private tree data is declared exactly the same way for set and
  80. * multiset; the distinction is made entirely in how the tree functions are
  81. * called (*_unique versus *_equal, same as the standard).
  82. */
  83. template <typename _Key, typename _Compare = std::less<_Key>,
  84. typename _Alloc = std::allocator<_Key> >
  85. class multiset
  86. {
  87. #ifdef _GLIBCXX_CONCEPT_CHECKS
  88. // concept requirements
  89. typedef typename _Alloc::value_type _Alloc_value_type;
  90. # if __cplusplus < 201103L
  91. __glibcxx_class_requires(_Key, _SGIAssignableConcept)
  92. # endif
  93. __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
  94. _BinaryFunctionConcept)
  95. __glibcxx_class_requires2(_Key, _Alloc_value_type, _SameTypeConcept)
  96. #endif
  97. #if __cplusplus >= 201103L
  98. static_assert(is_same<typename remove_cv<_Key>::type, _Key>::value,
  99. "std::multiset must have a non-const, non-volatile value_type");
  100. # if __cplusplus > 201703L || defined __STRICT_ANSI__
  101. static_assert(is_same<typename _Alloc::value_type, _Key>::value,
  102. "std::multiset must have the same value_type as its allocator");
  103. # endif
  104. #endif
  105. public:
  106. // typedefs:
  107. typedef _Key key_type;
  108. typedef _Key value_type;
  109. typedef _Compare key_compare;
  110. typedef _Compare value_compare;
  111. typedef _Alloc allocator_type;
  112. private:
  113. /// This turns a red-black tree into a [multi]set.
  114. typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
  115. rebind<_Key>::other _Key_alloc_type;
  116. typedef _Rb_tree<key_type, value_type, _Identity<value_type>,
  117. key_compare, _Key_alloc_type> _Rep_type;
  118. /// The actual tree structure.
  119. _Rep_type _M_t;
  120. typedef __gnu_cxx::__alloc_traits<_Key_alloc_type> _Alloc_traits;
  121. public:
  122. typedef typename _Alloc_traits::pointer pointer;
  123. typedef typename _Alloc_traits::const_pointer const_pointer;
  124. typedef typename _Alloc_traits::reference reference;
  125. typedef typename _Alloc_traits::const_reference const_reference;
  126. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  127. // DR 103. set::iterator is required to be modifiable,
  128. // but this allows modification of keys.
  129. typedef typename _Rep_type::const_iterator iterator;
  130. typedef typename _Rep_type::const_iterator const_iterator;
  131. typedef typename _Rep_type::const_reverse_iterator reverse_iterator;
  132. typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
  133. typedef typename _Rep_type::size_type size_type;
  134. typedef typename _Rep_type::difference_type difference_type;
  135. #if __cplusplus > 201402L
  136. using node_type = typename _Rep_type::node_type;
  137. #endif
  138. // allocation/deallocation
  139. /**
  140. * @brief Default constructor creates no elements.
  141. */
  142. #if __cplusplus < 201103L
  143. multiset() : _M_t() { }
  144. #else
  145. multiset() = default;
  146. #endif
  147. /**
  148. * @brief Creates a %multiset with no elements.
  149. * @param __comp Comparator to use.
  150. * @param __a An allocator object.
  151. */
  152. explicit
  153. multiset(const _Compare& __comp,
  154. const allocator_type& __a = allocator_type())
  155. : _M_t(__comp, _Key_alloc_type(__a)) { }
  156. /**
  157. * @brief Builds a %multiset from a range.
  158. * @param __first An input iterator.
  159. * @param __last An input iterator.
  160. *
  161. * Create a %multiset consisting of copies of the elements from
  162. * [first,last). This is linear in N if the range is already sorted,
  163. * and NlogN otherwise (where N is distance(__first,__last)).
  164. */
  165. template<typename _InputIterator>
  166. multiset(_InputIterator __first, _InputIterator __last)
  167. : _M_t()
  168. { _M_t._M_insert_range_equal(__first, __last); }
  169. /**
  170. * @brief Builds a %multiset from a range.
  171. * @param __first An input iterator.
  172. * @param __last An input iterator.
  173. * @param __comp A comparison functor.
  174. * @param __a An allocator object.
  175. *
  176. * Create a %multiset consisting of copies of the elements from
  177. * [__first,__last). This is linear in N if the range is already sorted,
  178. * and NlogN otherwise (where N is distance(__first,__last)).
  179. */
  180. template<typename _InputIterator>
  181. multiset(_InputIterator __first, _InputIterator __last,
  182. const _Compare& __comp,
  183. const allocator_type& __a = allocator_type())
  184. : _M_t(__comp, _Key_alloc_type(__a))
  185. { _M_t._M_insert_range_equal(__first, __last); }
  186. /**
  187. * @brief %Multiset copy constructor.
  188. *
  189. * Whether the allocator is copied depends on the allocator traits.
  190. */
  191. #if __cplusplus < 201103L
  192. multiset(const multiset& __x)
  193. : _M_t(__x._M_t) { }
  194. #else
  195. multiset(const multiset&) = default;
  196. /**
  197. * @brief %Multiset move constructor.
  198. *
  199. * The newly-created %multiset contains the exact contents of the
  200. * moved instance. The moved instance is a valid, but unspecified
  201. * %multiset.
  202. */
  203. multiset(multiset&&) = default;
  204. /**
  205. * @brief Builds a %multiset from an initializer_list.
  206. * @param __l An initializer_list.
  207. * @param __comp A comparison functor.
  208. * @param __a An allocator object.
  209. *
  210. * Create a %multiset consisting of copies of the elements from
  211. * the list. This is linear in N if the list is already sorted,
  212. * and NlogN otherwise (where N is @a __l.size()).
  213. */
  214. multiset(initializer_list<value_type> __l,
  215. const _Compare& __comp = _Compare(),
  216. const allocator_type& __a = allocator_type())
  217. : _M_t(__comp, _Key_alloc_type(__a))
  218. { _M_t._M_insert_range_equal(__l.begin(), __l.end()); }
  219. /// Allocator-extended default constructor.
  220. explicit
  221. multiset(const allocator_type& __a)
  222. : _M_t(_Key_alloc_type(__a)) { }
  223. /// Allocator-extended copy constructor.
  224. multiset(const multiset& __m,
  225. const __type_identity_t<allocator_type>& __a)
  226. : _M_t(__m._M_t, _Key_alloc_type(__a)) { }
  227. /// Allocator-extended move constructor.
  228. multiset(multiset&& __m, const __type_identity_t<allocator_type>& __a)
  229. noexcept(is_nothrow_copy_constructible<_Compare>::value
  230. && _Alloc_traits::_S_always_equal())
  231. : _M_t(std::move(__m._M_t), _Key_alloc_type(__a)) { }
  232. /// Allocator-extended initialier-list constructor.
  233. multiset(initializer_list<value_type> __l, const allocator_type& __a)
  234. : _M_t(_Key_alloc_type(__a))
  235. { _M_t._M_insert_range_equal(__l.begin(), __l.end()); }
  236. /// Allocator-extended range constructor.
  237. template<typename _InputIterator>
  238. multiset(_InputIterator __first, _InputIterator __last,
  239. const allocator_type& __a)
  240. : _M_t(_Key_alloc_type(__a))
  241. { _M_t._M_insert_range_equal(__first, __last); }
  242. /**
  243. * The dtor only erases the elements, and note that if the elements
  244. * themselves are pointers, the pointed-to memory is not touched in any
  245. * way. Managing the pointer is the user's responsibility.
  246. */
  247. ~multiset() = default;
  248. #endif
  249. /**
  250. * @brief %Multiset assignment operator.
  251. *
  252. * Whether the allocator is copied depends on the allocator traits.
  253. */
  254. #if __cplusplus < 201103L
  255. multiset&
  256. operator=(const multiset& __x)
  257. {
  258. _M_t = __x._M_t;
  259. return *this;
  260. }
  261. #else
  262. multiset&
  263. operator=(const multiset&) = default;
  264. /// Move assignment operator.
  265. multiset&
  266. operator=(multiset&&) = default;
  267. /**
  268. * @brief %Multiset list assignment operator.
  269. * @param __l An initializer_list.
  270. *
  271. * This function fills a %multiset with copies of the elements in the
  272. * initializer list @a __l.
  273. *
  274. * Note that the assignment completely changes the %multiset and
  275. * that the resulting %multiset's size is the same as the number
  276. * of elements assigned.
  277. */
  278. multiset&
  279. operator=(initializer_list<value_type> __l)
  280. {
  281. _M_t._M_assign_equal(__l.begin(), __l.end());
  282. return *this;
  283. }
  284. #endif
  285. // accessors:
  286. /// Returns the comparison object.
  287. key_compare
  288. key_comp() const
  289. { return _M_t.key_comp(); }
  290. /// Returns the comparison object.
  291. value_compare
  292. value_comp() const
  293. { return _M_t.key_comp(); }
  294. /// Returns the memory allocation object.
  295. allocator_type
  296. get_allocator() const _GLIBCXX_NOEXCEPT
  297. { return allocator_type(_M_t.get_allocator()); }
  298. /**
  299. * Returns a read-only (constant) iterator that points to the first
  300. * element in the %multiset. Iteration is done in ascending order
  301. * according to the keys.
  302. */
  303. iterator
  304. begin() const _GLIBCXX_NOEXCEPT
  305. { return _M_t.begin(); }
  306. /**
  307. * Returns a read-only (constant) iterator that points one past the last
  308. * element in the %multiset. Iteration is done in ascending order
  309. * according to the keys.
  310. */
  311. iterator
  312. end() const _GLIBCXX_NOEXCEPT
  313. { return _M_t.end(); }
  314. /**
  315. * Returns a read-only (constant) reverse iterator that points to the
  316. * last element in the %multiset. Iteration is done in descending order
  317. * according to the keys.
  318. */
  319. reverse_iterator
  320. rbegin() const _GLIBCXX_NOEXCEPT
  321. { return _M_t.rbegin(); }
  322. /**
  323. * Returns a read-only (constant) reverse iterator that points to the
  324. * last element in the %multiset. Iteration is done in descending order
  325. * according to the keys.
  326. */
  327. reverse_iterator
  328. rend() const _GLIBCXX_NOEXCEPT
  329. { return _M_t.rend(); }
  330. #if __cplusplus >= 201103L
  331. /**
  332. * Returns a read-only (constant) iterator that points to the first
  333. * element in the %multiset. Iteration is done in ascending order
  334. * according to the keys.
  335. */
  336. iterator
  337. cbegin() const noexcept
  338. { return _M_t.begin(); }
  339. /**
  340. * Returns a read-only (constant) iterator that points one past the last
  341. * element in the %multiset. Iteration is done in ascending order
  342. * according to the keys.
  343. */
  344. iterator
  345. cend() const noexcept
  346. { return _M_t.end(); }
  347. /**
  348. * Returns a read-only (constant) reverse iterator that points to the
  349. * last element in the %multiset. Iteration is done in descending order
  350. * according to the keys.
  351. */
  352. reverse_iterator
  353. crbegin() const noexcept
  354. { return _M_t.rbegin(); }
  355. /**
  356. * Returns a read-only (constant) reverse iterator that points to the
  357. * last element in the %multiset. Iteration is done in descending order
  358. * according to the keys.
  359. */
  360. reverse_iterator
  361. crend() const noexcept
  362. { return _M_t.rend(); }
  363. #endif
  364. /// Returns true if the %set is empty.
  365. _GLIBCXX_NODISCARD bool
  366. empty() const _GLIBCXX_NOEXCEPT
  367. { return _M_t.empty(); }
  368. /// Returns the size of the %set.
  369. size_type
  370. size() const _GLIBCXX_NOEXCEPT
  371. { return _M_t.size(); }
  372. /// Returns the maximum size of the %set.
  373. size_type
  374. max_size() const _GLIBCXX_NOEXCEPT
  375. { return _M_t.max_size(); }
  376. /**
  377. * @brief Swaps data with another %multiset.
  378. * @param __x A %multiset of the same element and allocator types.
  379. *
  380. * This exchanges the elements between two multisets in constant time.
  381. * (It is only swapping a pointer, an integer, and an instance of the @c
  382. * Compare type (which itself is often stateless and empty), so it should
  383. * be quite fast.)
  384. * Note that the global std::swap() function is specialized such that
  385. * std::swap(s1,s2) will feed to this function.
  386. *
  387. * Whether the allocators are swapped depends on the allocator traits.
  388. */
  389. void
  390. swap(multiset& __x)
  391. _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value)
  392. { _M_t.swap(__x._M_t); }
  393. // insert/erase
  394. #if __cplusplus >= 201103L
  395. /**
  396. * @brief Builds and inserts an element into the %multiset.
  397. * @param __args Arguments used to generate the element instance to be
  398. * inserted.
  399. * @return An iterator that points to the inserted element.
  400. *
  401. * This function inserts an element into the %multiset. Contrary
  402. * to a std::set the %multiset does not rely on unique keys and thus
  403. * multiple copies of the same element can be inserted.
  404. *
  405. * Insertion requires logarithmic time.
  406. */
  407. template<typename... _Args>
  408. iterator
  409. emplace(_Args&&... __args)
  410. { return _M_t._M_emplace_equal(std::forward<_Args>(__args)...); }
  411. /**
  412. * @brief Builds and inserts an element into the %multiset.
  413. * @param __pos An iterator that serves as a hint as to where the
  414. * element should be inserted.
  415. * @param __args Arguments used to generate the element instance to be
  416. * inserted.
  417. * @return An iterator that points to the inserted element.
  418. *
  419. * This function inserts an element into the %multiset. Contrary
  420. * to a std::set the %multiset does not rely on unique keys and thus
  421. * multiple copies of the same element can be inserted.
  422. *
  423. * Note that the first parameter is only a hint and can potentially
  424. * improve the performance of the insertion process. A bad hint would
  425. * cause no gains in efficiency.
  426. *
  427. * See https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
  428. * for more on @a hinting.
  429. *
  430. * Insertion requires logarithmic time (if the hint is not taken).
  431. */
  432. template<typename... _Args>
  433. iterator
  434. emplace_hint(const_iterator __pos, _Args&&... __args)
  435. {
  436. return _M_t._M_emplace_hint_equal(__pos,
  437. std::forward<_Args>(__args)...);
  438. }
  439. #endif
  440. /**
  441. * @brief Inserts an element into the %multiset.
  442. * @param __x Element to be inserted.
  443. * @return An iterator that points to the inserted element.
  444. *
  445. * This function inserts an element into the %multiset. Contrary
  446. * to a std::set the %multiset does not rely on unique keys and thus
  447. * multiple copies of the same element can be inserted.
  448. *
  449. * Insertion requires logarithmic time.
  450. */
  451. iterator
  452. insert(const value_type& __x)
  453. { return _M_t._M_insert_equal(__x); }
  454. #if __cplusplus >= 201103L
  455. iterator
  456. insert(value_type&& __x)
  457. { return _M_t._M_insert_equal(std::move(__x)); }
  458. #endif
  459. /**
  460. * @brief Inserts an element into the %multiset.
  461. * @param __position An iterator that serves as a hint as to where the
  462. * element should be inserted.
  463. * @param __x Element to be inserted.
  464. * @return An iterator that points to the inserted element.
  465. *
  466. * This function inserts an element into the %multiset. Contrary
  467. * to a std::set the %multiset does not rely on unique keys and thus
  468. * multiple copies of the same element can be inserted.
  469. *
  470. * Note that the first parameter is only a hint and can potentially
  471. * improve the performance of the insertion process. A bad hint would
  472. * cause no gains in efficiency.
  473. *
  474. * See https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
  475. * for more on @a hinting.
  476. *
  477. * Insertion requires logarithmic time (if the hint is not taken).
  478. */
  479. iterator
  480. insert(const_iterator __position, const value_type& __x)
  481. { return _M_t._M_insert_equal_(__position, __x); }
  482. #if __cplusplus >= 201103L
  483. iterator
  484. insert(const_iterator __position, value_type&& __x)
  485. { return _M_t._M_insert_equal_(__position, std::move(__x)); }
  486. #endif
  487. /**
  488. * @brief A template function that tries to insert a range of elements.
  489. * @param __first Iterator pointing to the start of the range to be
  490. * inserted.
  491. * @param __last Iterator pointing to the end of the range.
  492. *
  493. * Complexity similar to that of the range constructor.
  494. */
  495. template<typename _InputIterator>
  496. void
  497. insert(_InputIterator __first, _InputIterator __last)
  498. { _M_t._M_insert_range_equal(__first, __last); }
  499. #if __cplusplus >= 201103L
  500. /**
  501. * @brief Attempts to insert a list of elements into the %multiset.
  502. * @param __l A std::initializer_list<value_type> of elements
  503. * to be inserted.
  504. *
  505. * Complexity similar to that of the range constructor.
  506. */
  507. void
  508. insert(initializer_list<value_type> __l)
  509. { this->insert(__l.begin(), __l.end()); }
  510. #endif
  511. #if __cplusplus > 201402L
  512. /// Extract a node.
  513. node_type
  514. extract(const_iterator __pos)
  515. {
  516. __glibcxx_assert(__pos != end());
  517. return _M_t.extract(__pos);
  518. }
  519. /// Extract a node.
  520. node_type
  521. extract(const key_type& __x)
  522. { return _M_t.extract(__x); }
  523. /// Re-insert an extracted node.
  524. iterator
  525. insert(node_type&& __nh)
  526. { return _M_t._M_reinsert_node_equal(std::move(__nh)); }
  527. /// Re-insert an extracted node.
  528. iterator
  529. insert(const_iterator __hint, node_type&& __nh)
  530. { return _M_t._M_reinsert_node_hint_equal(__hint, std::move(__nh)); }
  531. template<typename, typename>
  532. friend struct std::_Rb_tree_merge_helper;
  533. template<typename _Compare1>
  534. void
  535. merge(multiset<_Key, _Compare1, _Alloc>& __source)
  536. {
  537. using _Merge_helper = _Rb_tree_merge_helper<multiset, _Compare1>;
  538. _M_t._M_merge_equal(_Merge_helper::_S_get_tree(__source));
  539. }
  540. template<typename _Compare1>
  541. void
  542. merge(multiset<_Key, _Compare1, _Alloc>&& __source)
  543. { merge(__source); }
  544. template<typename _Compare1>
  545. void
  546. merge(set<_Key, _Compare1, _Alloc>& __source)
  547. {
  548. using _Merge_helper = _Rb_tree_merge_helper<multiset, _Compare1>;
  549. _M_t._M_merge_equal(_Merge_helper::_S_get_tree(__source));
  550. }
  551. template<typename _Compare1>
  552. void
  553. merge(set<_Key, _Compare1, _Alloc>&& __source)
  554. { merge(__source); }
  555. #endif // C++17
  556. #if __cplusplus >= 201103L
  557. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  558. // DR 130. Associative erase should return an iterator.
  559. /**
  560. * @brief Erases an element from a %multiset.
  561. * @param __position An iterator pointing to the element to be erased.
  562. * @return An iterator pointing to the element immediately following
  563. * @a position prior to the element being erased. If no such
  564. * element exists, end() is returned.
  565. *
  566. * This function erases an element, pointed to by the given iterator,
  567. * from a %multiset. Note that this function only erases the element,
  568. * and that if the element is itself a pointer, the pointed-to memory is
  569. * not touched in any way. Managing the pointer is the user's
  570. * responsibility.
  571. */
  572. _GLIBCXX_ABI_TAG_CXX11
  573. iterator
  574. erase(const_iterator __position)
  575. { return _M_t.erase(__position); }
  576. #else
  577. /**
  578. * @brief Erases an element from a %multiset.
  579. * @param __position An iterator pointing to the element to be erased.
  580. *
  581. * This function erases an element, pointed to by the given iterator,
  582. * from a %multiset. Note that this function only erases the element,
  583. * and that if the element is itself a pointer, the pointed-to memory is
  584. * not touched in any way. Managing the pointer is the user's
  585. * responsibility.
  586. */
  587. void
  588. erase(iterator __position)
  589. { _M_t.erase(__position); }
  590. #endif
  591. /**
  592. * @brief Erases elements according to the provided key.
  593. * @param __x Key of element to be erased.
  594. * @return The number of elements erased.
  595. *
  596. * This function erases all elements located by the given key from a
  597. * %multiset.
  598. * Note that this function only erases the element, and that if
  599. * the element is itself a pointer, the pointed-to memory is not touched
  600. * in any way. Managing the pointer is the user's responsibility.
  601. */
  602. size_type
  603. erase(const key_type& __x)
  604. { return _M_t.erase(__x); }
  605. #if __cplusplus >= 201103L
  606. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  607. // DR 130. Associative erase should return an iterator.
  608. /**
  609. * @brief Erases a [first,last) range of elements from a %multiset.
  610. * @param __first Iterator pointing to the start of the range to be
  611. * erased.
  612. * @param __last Iterator pointing to the end of the range to
  613. * be erased.
  614. * @return The iterator @a last.
  615. *
  616. * This function erases a sequence of elements from a %multiset.
  617. * Note that this function only erases the elements, and that if
  618. * the elements themselves are pointers, the pointed-to memory is not
  619. * touched in any way. Managing the pointer is the user's
  620. * responsibility.
  621. */
  622. _GLIBCXX_ABI_TAG_CXX11
  623. iterator
  624. erase(const_iterator __first, const_iterator __last)
  625. { return _M_t.erase(__first, __last); }
  626. #else
  627. /**
  628. * @brief Erases a [first,last) range of elements from a %multiset.
  629. * @param first Iterator pointing to the start of the range to be
  630. * erased.
  631. * @param last Iterator pointing to the end of the range to be erased.
  632. *
  633. * This function erases a sequence of elements from a %multiset.
  634. * Note that this function only erases the elements, and that if
  635. * the elements themselves are pointers, the pointed-to memory is not
  636. * touched in any way. Managing the pointer is the user's
  637. * responsibility.
  638. */
  639. void
  640. erase(iterator __first, iterator __last)
  641. { _M_t.erase(__first, __last); }
  642. #endif
  643. /**
  644. * Erases all elements in a %multiset. Note that this function only
  645. * erases the elements, and that if the elements themselves are pointers,
  646. * the pointed-to memory is not touched in any way. Managing the pointer
  647. * is the user's responsibility.
  648. */
  649. void
  650. clear() _GLIBCXX_NOEXCEPT
  651. { _M_t.clear(); }
  652. // multiset operations:
  653. ///@{
  654. /**
  655. * @brief Finds the number of elements with given key.
  656. * @param __x Key of elements to be located.
  657. * @return Number of elements with specified key.
  658. */
  659. size_type
  660. count(const key_type& __x) const
  661. { return _M_t.count(__x); }
  662. #if __cplusplus > 201103L
  663. template<typename _Kt>
  664. auto
  665. count(const _Kt& __x) const -> decltype(_M_t._M_count_tr(__x))
  666. { return _M_t._M_count_tr(__x); }
  667. #endif
  668. ///@}
  669. #if __cplusplus > 201703L
  670. ///@{
  671. /**
  672. * @brief Finds whether an element with the given key exists.
  673. * @param __x Key of elements to be located.
  674. * @return True if there is any element with the specified key.
  675. */
  676. bool
  677. contains(const key_type& __x) const
  678. { return _M_t.find(__x) != _M_t.end(); }
  679. template<typename _Kt>
  680. auto
  681. contains(const _Kt& __x) const
  682. -> decltype(_M_t._M_find_tr(__x), void(), true)
  683. { return _M_t._M_find_tr(__x) != _M_t.end(); }
  684. ///@}
  685. #endif
  686. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  687. // 214. set::find() missing const overload
  688. ///@{
  689. /**
  690. * @brief Tries to locate an element in a %set.
  691. * @param __x Element to be located.
  692. * @return Iterator pointing to sought-after element, or end() if not
  693. * found.
  694. *
  695. * This function takes a key and tries to locate the element with which
  696. * the key matches. If successful the function returns an iterator
  697. * pointing to the sought after element. If unsuccessful it returns the
  698. * past-the-end ( @c end() ) iterator.
  699. */
  700. iterator
  701. find(const key_type& __x)
  702. { return _M_t.find(__x); }
  703. const_iterator
  704. find(const key_type& __x) const
  705. { return _M_t.find(__x); }
  706. #if __cplusplus > 201103L
  707. template<typename _Kt>
  708. auto
  709. find(const _Kt& __x)
  710. -> decltype(iterator{_M_t._M_find_tr(__x)})
  711. { return iterator{_M_t._M_find_tr(__x)}; }
  712. template<typename _Kt>
  713. auto
  714. find(const _Kt& __x) const
  715. -> decltype(const_iterator{_M_t._M_find_tr(__x)})
  716. { return const_iterator{_M_t._M_find_tr(__x)}; }
  717. #endif
  718. ///@}
  719. ///@{
  720. /**
  721. * @brief Finds the beginning of a subsequence matching given key.
  722. * @param __x Key to be located.
  723. * @return Iterator pointing to first element equal to or greater
  724. * than key, or end().
  725. *
  726. * This function returns the first element of a subsequence of elements
  727. * that matches the given key. If unsuccessful it returns an iterator
  728. * pointing to the first element that has a greater value than given key
  729. * or end() if no such element exists.
  730. */
  731. iterator
  732. lower_bound(const key_type& __x)
  733. { return _M_t.lower_bound(__x); }
  734. const_iterator
  735. lower_bound(const key_type& __x) const
  736. { return _M_t.lower_bound(__x); }
  737. #if __cplusplus > 201103L
  738. template<typename _Kt>
  739. auto
  740. lower_bound(const _Kt& __x)
  741. -> decltype(iterator(_M_t._M_lower_bound_tr(__x)))
  742. { return iterator(_M_t._M_lower_bound_tr(__x)); }
  743. template<typename _Kt>
  744. auto
  745. lower_bound(const _Kt& __x) const
  746. -> decltype(iterator(_M_t._M_lower_bound_tr(__x)))
  747. { return iterator(_M_t._M_lower_bound_tr(__x)); }
  748. #endif
  749. ///@}
  750. ///@{
  751. /**
  752. * @brief Finds the end of a subsequence matching given key.
  753. * @param __x Key to be located.
  754. * @return Iterator pointing to the first element
  755. * greater than key, or end().
  756. */
  757. iterator
  758. upper_bound(const key_type& __x)
  759. { return _M_t.upper_bound(__x); }
  760. const_iterator
  761. upper_bound(const key_type& __x) const
  762. { return _M_t.upper_bound(__x); }
  763. #if __cplusplus > 201103L
  764. template<typename _Kt>
  765. auto
  766. upper_bound(const _Kt& __x)
  767. -> decltype(iterator(_M_t._M_upper_bound_tr(__x)))
  768. { return iterator(_M_t._M_upper_bound_tr(__x)); }
  769. template<typename _Kt>
  770. auto
  771. upper_bound(const _Kt& __x) const
  772. -> decltype(iterator(_M_t._M_upper_bound_tr(__x)))
  773. { return iterator(_M_t._M_upper_bound_tr(__x)); }
  774. #endif
  775. ///@}
  776. ///@{
  777. /**
  778. * @brief Finds a subsequence matching given key.
  779. * @param __x Key to be located.
  780. * @return Pair of iterators that possibly points to the subsequence
  781. * matching given key.
  782. *
  783. * This function is equivalent to
  784. * @code
  785. * std::make_pair(c.lower_bound(val),
  786. * c.upper_bound(val))
  787. * @endcode
  788. * (but is faster than making the calls separately).
  789. *
  790. * This function probably only makes sense for multisets.
  791. */
  792. std::pair<iterator, iterator>
  793. equal_range(const key_type& __x)
  794. { return _M_t.equal_range(__x); }
  795. std::pair<const_iterator, const_iterator>
  796. equal_range(const key_type& __x) const
  797. { return _M_t.equal_range(__x); }
  798. #if __cplusplus > 201103L
  799. template<typename _Kt>
  800. auto
  801. equal_range(const _Kt& __x)
  802. -> decltype(pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)))
  803. { return pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)); }
  804. template<typename _Kt>
  805. auto
  806. equal_range(const _Kt& __x) const
  807. -> decltype(pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)))
  808. { return pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)); }
  809. #endif
  810. ///@}
  811. template<typename _K1, typename _C1, typename _A1>
  812. friend bool
  813. operator==(const multiset<_K1, _C1, _A1>&,
  814. const multiset<_K1, _C1, _A1>&);
  815. #if __cpp_lib_three_way_comparison
  816. template<typename _K1, typename _C1, typename _A1>
  817. friend __detail::__synth3way_t<_K1>
  818. operator<=>(const multiset<_K1, _C1, _A1>&,
  819. const multiset<_K1, _C1, _A1>&);
  820. #else
  821. template<typename _K1, typename _C1, typename _A1>
  822. friend bool
  823. operator< (const multiset<_K1, _C1, _A1>&,
  824. const multiset<_K1, _C1, _A1>&);
  825. #endif
  826. };
  827. #if __cpp_deduction_guides >= 201606
  828. template<typename _InputIterator,
  829. typename _Compare =
  830. less<typename iterator_traits<_InputIterator>::value_type>,
  831. typename _Allocator =
  832. allocator<typename iterator_traits<_InputIterator>::value_type>,
  833. typename = _RequireInputIter<_InputIterator>,
  834. typename = _RequireNotAllocator<_Compare>,
  835. typename = _RequireAllocator<_Allocator>>
  836. multiset(_InputIterator, _InputIterator,
  837. _Compare = _Compare(), _Allocator = _Allocator())
  838. -> multiset<typename iterator_traits<_InputIterator>::value_type,
  839. _Compare, _Allocator>;
  840. template<typename _Key,
  841. typename _Compare = less<_Key>,
  842. typename _Allocator = allocator<_Key>,
  843. typename = _RequireNotAllocator<_Compare>,
  844. typename = _RequireAllocator<_Allocator>>
  845. multiset(initializer_list<_Key>,
  846. _Compare = _Compare(), _Allocator = _Allocator())
  847. -> multiset<_Key, _Compare, _Allocator>;
  848. template<typename _InputIterator, typename _Allocator,
  849. typename = _RequireInputIter<_InputIterator>,
  850. typename = _RequireAllocator<_Allocator>>
  851. multiset(_InputIterator, _InputIterator, _Allocator)
  852. -> multiset<typename iterator_traits<_InputIterator>::value_type,
  853. less<typename iterator_traits<_InputIterator>::value_type>,
  854. _Allocator>;
  855. template<typename _Key, typename _Allocator,
  856. typename = _RequireAllocator<_Allocator>>
  857. multiset(initializer_list<_Key>, _Allocator)
  858. -> multiset<_Key, less<_Key>, _Allocator>;
  859. #endif // deduction guides
  860. /**
  861. * @brief Multiset equality comparison.
  862. * @param __x A %multiset.
  863. * @param __y A %multiset of the same type as @a __x.
  864. * @return True iff the size and elements of the multisets are equal.
  865. *
  866. * This is an equivalence relation. It is linear in the size of the
  867. * multisets.
  868. * Multisets are considered equivalent if their sizes are equal, and if
  869. * corresponding elements compare equal.
  870. */
  871. template<typename _Key, typename _Compare, typename _Alloc>
  872. inline bool
  873. operator==(const multiset<_Key, _Compare, _Alloc>& __x,
  874. const multiset<_Key, _Compare, _Alloc>& __y)
  875. { return __x._M_t == __y._M_t; }
  876. #if __cpp_lib_three_way_comparison
  877. /**
  878. * @brief Multiset ordering relation.
  879. * @param __x A `multiset`.
  880. * @param __y A `multiset` of the same type as `x`.
  881. * @return A value indicating whether `__x` is less than, equal to,
  882. * greater than, or incomparable with `__y`.
  883. *
  884. * This is a total ordering relation. It is linear in the size of the
  885. * maps. The elements must be comparable with @c <.
  886. *
  887. * See `std::lexicographical_compare_three_way()` for how the determination
  888. * is made. This operator is used to synthesize relational operators like
  889. * `<` and `>=` etc.
  890. */
  891. template<typename _Key, typename _Compare, typename _Alloc>
  892. inline __detail::__synth3way_t<_Key>
  893. operator<=>(const multiset<_Key, _Compare, _Alloc>& __x,
  894. const multiset<_Key, _Compare, _Alloc>& __y)
  895. { return __x._M_t <=> __y._M_t; }
  896. #else
  897. /**
  898. * @brief Multiset ordering relation.
  899. * @param __x A %multiset.
  900. * @param __y A %multiset of the same type as @a __x.
  901. * @return True iff @a __x is lexicographically less than @a __y.
  902. *
  903. * This is a total ordering relation. It is linear in the size of the
  904. * sets. The elements must be comparable with @c <.
  905. *
  906. * See std::lexicographical_compare() for how the determination is made.
  907. */
  908. template<typename _Key, typename _Compare, typename _Alloc>
  909. inline bool
  910. operator<(const multiset<_Key, _Compare, _Alloc>& __x,
  911. const multiset<_Key, _Compare, _Alloc>& __y)
  912. { return __x._M_t < __y._M_t; }
  913. /// Returns !(x == y).
  914. template<typename _Key, typename _Compare, typename _Alloc>
  915. inline bool
  916. operator!=(const multiset<_Key, _Compare, _Alloc>& __x,
  917. const multiset<_Key, _Compare, _Alloc>& __y)
  918. { return !(__x == __y); }
  919. /// Returns y < x.
  920. template<typename _Key, typename _Compare, typename _Alloc>
  921. inline bool
  922. operator>(const multiset<_Key,_Compare,_Alloc>& __x,
  923. const multiset<_Key,_Compare,_Alloc>& __y)
  924. { return __y < __x; }
  925. /// Returns !(y < x)
  926. template<typename _Key, typename _Compare, typename _Alloc>
  927. inline bool
  928. operator<=(const multiset<_Key, _Compare, _Alloc>& __x,
  929. const multiset<_Key, _Compare, _Alloc>& __y)
  930. { return !(__y < __x); }
  931. /// Returns !(x < y)
  932. template<typename _Key, typename _Compare, typename _Alloc>
  933. inline bool
  934. operator>=(const multiset<_Key, _Compare, _Alloc>& __x,
  935. const multiset<_Key, _Compare, _Alloc>& __y)
  936. { return !(__x < __y); }
  937. #endif // three-way comparison
  938. /// See std::multiset::swap().
  939. template<typename _Key, typename _Compare, typename _Alloc>
  940. inline void
  941. swap(multiset<_Key, _Compare, _Alloc>& __x,
  942. multiset<_Key, _Compare, _Alloc>& __y)
  943. _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
  944. { __x.swap(__y); }
  945. _GLIBCXX_END_NAMESPACE_CONTAINER
  946. #if __cplusplus > 201402L
  947. // Allow std::multiset access to internals of compatible sets.
  948. template<typename _Val, typename _Cmp1, typename _Alloc, typename _Cmp2>
  949. struct
  950. _Rb_tree_merge_helper<_GLIBCXX_STD_C::multiset<_Val, _Cmp1, _Alloc>,
  951. _Cmp2>
  952. {
  953. private:
  954. friend class _GLIBCXX_STD_C::multiset<_Val, _Cmp1, _Alloc>;
  955. static auto&
  956. _S_get_tree(_GLIBCXX_STD_C::set<_Val, _Cmp2, _Alloc>& __set)
  957. { return __set._M_t; }
  958. static auto&
  959. _S_get_tree(_GLIBCXX_STD_C::multiset<_Val, _Cmp2, _Alloc>& __set)
  960. { return __set._M_t; }
  961. };
  962. #endif // C++17
  963. _GLIBCXX_END_NAMESPACE_VERSION
  964. } // namespace std
  965. #endif /* _STL_MULTISET_H */