Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

1076 linhas
37KB

  1. // Multiset implementation -*- C++ -*-
  2. // Copyright (C) 2001-2020 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /*
  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, const allocator_type& __a)
  225. : _M_t(__m._M_t, _Key_alloc_type(__a)) { }
  226. /// Allocator-extended move constructor.
  227. multiset(multiset&& __m, const allocator_type& __a)
  228. noexcept(is_nothrow_copy_constructible<_Compare>::value
  229. && _Alloc_traits::_S_always_equal())
  230. : _M_t(std::move(__m._M_t), _Key_alloc_type(__a)) { }
  231. /// Allocator-extended initialier-list constructor.
  232. multiset(initializer_list<value_type> __l, const allocator_type& __a)
  233. : _M_t(_Key_alloc_type(__a))
  234. { _M_t._M_insert_range_equal(__l.begin(), __l.end()); }
  235. /// Allocator-extended range constructor.
  236. template<typename _InputIterator>
  237. multiset(_InputIterator __first, _InputIterator __last,
  238. const allocator_type& __a)
  239. : _M_t(_Key_alloc_type(__a))
  240. { _M_t._M_insert_range_equal(__first, __last); }
  241. /**
  242. * The dtor only erases the elements, and note that if the elements
  243. * themselves are pointers, the pointed-to memory is not touched in any
  244. * way. Managing the pointer is the user's responsibility.
  245. */
  246. ~multiset() = default;
  247. #endif
  248. /**
  249. * @brief %Multiset assignment operator.
  250. *
  251. * Whether the allocator is copied depends on the allocator traits.
  252. */
  253. #if __cplusplus < 201103L
  254. multiset&
  255. operator=(const multiset& __x)
  256. {
  257. _M_t = __x._M_t;
  258. return *this;
  259. }
  260. #else
  261. multiset&
  262. operator=(const multiset&) = default;
  263. /// Move assignment operator.
  264. multiset&
  265. operator=(multiset&&) = default;
  266. /**
  267. * @brief %Multiset list assignment operator.
  268. * @param __l An initializer_list.
  269. *
  270. * This function fills a %multiset with copies of the elements in the
  271. * initializer list @a __l.
  272. *
  273. * Note that the assignment completely changes the %multiset and
  274. * that the resulting %multiset's size is the same as the number
  275. * of elements assigned.
  276. */
  277. multiset&
  278. operator=(initializer_list<value_type> __l)
  279. {
  280. _M_t._M_assign_equal(__l.begin(), __l.end());
  281. return *this;
  282. }
  283. #endif
  284. // accessors:
  285. /// Returns the comparison object.
  286. key_compare
  287. key_comp() const
  288. { return _M_t.key_comp(); }
  289. /// Returns the comparison object.
  290. value_compare
  291. value_comp() const
  292. { return _M_t.key_comp(); }
  293. /// Returns the memory allocation object.
  294. allocator_type
  295. get_allocator() const _GLIBCXX_NOEXCEPT
  296. { return allocator_type(_M_t.get_allocator()); }
  297. /**
  298. * Returns a read-only (constant) iterator that points to the first
  299. * element in the %multiset. Iteration is done in ascending order
  300. * according to the keys.
  301. */
  302. iterator
  303. begin() const _GLIBCXX_NOEXCEPT
  304. { return _M_t.begin(); }
  305. /**
  306. * Returns a read-only (constant) iterator that points one past the last
  307. * element in the %multiset. Iteration is done in ascending order
  308. * according to the keys.
  309. */
  310. iterator
  311. end() const _GLIBCXX_NOEXCEPT
  312. { return _M_t.end(); }
  313. /**
  314. * Returns a read-only (constant) reverse iterator that points to the
  315. * last element in the %multiset. Iteration is done in descending order
  316. * according to the keys.
  317. */
  318. reverse_iterator
  319. rbegin() const _GLIBCXX_NOEXCEPT
  320. { return _M_t.rbegin(); }
  321. /**
  322. * Returns a read-only (constant) reverse iterator that points to the
  323. * last element in the %multiset. Iteration is done in descending order
  324. * according to the keys.
  325. */
  326. reverse_iterator
  327. rend() const _GLIBCXX_NOEXCEPT
  328. { return _M_t.rend(); }
  329. #if __cplusplus >= 201103L
  330. /**
  331. * Returns a read-only (constant) iterator that points to the first
  332. * element in the %multiset. Iteration is done in ascending order
  333. * according to the keys.
  334. */
  335. iterator
  336. cbegin() const noexcept
  337. { return _M_t.begin(); }
  338. /**
  339. * Returns a read-only (constant) iterator that points one past the last
  340. * element in the %multiset. Iteration is done in ascending order
  341. * according to the keys.
  342. */
  343. iterator
  344. cend() const noexcept
  345. { return _M_t.end(); }
  346. /**
  347. * Returns a read-only (constant) reverse iterator that points to the
  348. * last element in the %multiset. Iteration is done in descending order
  349. * according to the keys.
  350. */
  351. reverse_iterator
  352. crbegin() const noexcept
  353. { return _M_t.rbegin(); }
  354. /**
  355. * Returns a read-only (constant) reverse iterator that points to the
  356. * last element in the %multiset. Iteration is done in descending order
  357. * according to the keys.
  358. */
  359. reverse_iterator
  360. crend() const noexcept
  361. { return _M_t.rend(); }
  362. #endif
  363. /// Returns true if the %set is empty.
  364. _GLIBCXX_NODISCARD bool
  365. empty() const _GLIBCXX_NOEXCEPT
  366. { return _M_t.empty(); }
  367. /// Returns the size of the %set.
  368. size_type
  369. size() const _GLIBCXX_NOEXCEPT
  370. { return _M_t.size(); }
  371. /// Returns the maximum size of the %set.
  372. size_type
  373. max_size() const _GLIBCXX_NOEXCEPT
  374. { return _M_t.max_size(); }
  375. /**
  376. * @brief Swaps data with another %multiset.
  377. * @param __x A %multiset of the same element and allocator types.
  378. *
  379. * This exchanges the elements between two multisets in constant time.
  380. * (It is only swapping a pointer, an integer, and an instance of the @c
  381. * Compare type (which itself is often stateless and empty), so it should
  382. * be quite fast.)
  383. * Note that the global std::swap() function is specialized such that
  384. * std::swap(s1,s2) will feed to this function.
  385. *
  386. * Whether the allocators are swapped depends on the allocator traits.
  387. */
  388. void
  389. swap(multiset& __x)
  390. _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value)
  391. { _M_t.swap(__x._M_t); }
  392. // insert/erase
  393. #if __cplusplus >= 201103L
  394. /**
  395. * @brief Builds and inserts an element into the %multiset.
  396. * @param __args Arguments used to generate the element instance to be
  397. * inserted.
  398. * @return An iterator that points to the inserted element.
  399. *
  400. * This function inserts an element into the %multiset. Contrary
  401. * to a std::set the %multiset does not rely on unique keys and thus
  402. * multiple copies of the same element can be inserted.
  403. *
  404. * Insertion requires logarithmic time.
  405. */
  406. template<typename... _Args>
  407. iterator
  408. emplace(_Args&&... __args)
  409. { return _M_t._M_emplace_equal(std::forward<_Args>(__args)...); }
  410. /**
  411. * @brief Builds and inserts an element into the %multiset.
  412. * @param __pos An iterator that serves as a hint as to where the
  413. * element should be inserted.
  414. * @param __args Arguments used to generate the element instance to be
  415. * inserted.
  416. * @return An iterator that points to the inserted element.
  417. *
  418. * This function inserts an element into the %multiset. Contrary
  419. * to a std::set the %multiset does not rely on unique keys and thus
  420. * multiple copies of the same element can be inserted.
  421. *
  422. * Note that the first parameter is only a hint and can potentially
  423. * improve the performance of the insertion process. A bad hint would
  424. * cause no gains in efficiency.
  425. *
  426. * See https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
  427. * for more on @a hinting.
  428. *
  429. * Insertion requires logarithmic time (if the hint is not taken).
  430. */
  431. template<typename... _Args>
  432. iterator
  433. emplace_hint(const_iterator __pos, _Args&&... __args)
  434. {
  435. return _M_t._M_emplace_hint_equal(__pos,
  436. std::forward<_Args>(__args)...);
  437. }
  438. #endif
  439. /**
  440. * @brief Inserts an element into the %multiset.
  441. * @param __x Element to be inserted.
  442. * @return An iterator that points to the inserted element.
  443. *
  444. * This function inserts an element into the %multiset. Contrary
  445. * to a std::set the %multiset does not rely on unique keys and thus
  446. * multiple copies of the same element can be inserted.
  447. *
  448. * Insertion requires logarithmic time.
  449. */
  450. iterator
  451. insert(const value_type& __x)
  452. { return _M_t._M_insert_equal(__x); }
  453. #if __cplusplus >= 201103L
  454. iterator
  455. insert(value_type&& __x)
  456. { return _M_t._M_insert_equal(std::move(__x)); }
  457. #endif
  458. /**
  459. * @brief Inserts an element into the %multiset.
  460. * @param __position An iterator that serves as a hint as to where the
  461. * element should be inserted.
  462. * @param __x Element to be inserted.
  463. * @return An iterator that points to the inserted element.
  464. *
  465. * This function inserts an element into the %multiset. Contrary
  466. * to a std::set the %multiset does not rely on unique keys and thus
  467. * multiple copies of the same element can be inserted.
  468. *
  469. * Note that the first parameter is only a hint and can potentially
  470. * improve the performance of the insertion process. A bad hint would
  471. * cause no gains in efficiency.
  472. *
  473. * See https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
  474. * for more on @a hinting.
  475. *
  476. * Insertion requires logarithmic time (if the hint is not taken).
  477. */
  478. iterator
  479. insert(const_iterator __position, const value_type& __x)
  480. { return _M_t._M_insert_equal_(__position, __x); }
  481. #if __cplusplus >= 201103L
  482. iterator
  483. insert(const_iterator __position, value_type&& __x)
  484. { return _M_t._M_insert_equal_(__position, std::move(__x)); }
  485. #endif
  486. /**
  487. * @brief A template function that tries to insert a range of elements.
  488. * @param __first Iterator pointing to the start of the range to be
  489. * inserted.
  490. * @param __last Iterator pointing to the end of the range.
  491. *
  492. * Complexity similar to that of the range constructor.
  493. */
  494. template<typename _InputIterator>
  495. void
  496. insert(_InputIterator __first, _InputIterator __last)
  497. { _M_t._M_insert_range_equal(__first, __last); }
  498. #if __cplusplus >= 201103L
  499. /**
  500. * @brief Attempts to insert a list of elements into the %multiset.
  501. * @param __l A std::initializer_list<value_type> of elements
  502. * to be inserted.
  503. *
  504. * Complexity similar to that of the range constructor.
  505. */
  506. void
  507. insert(initializer_list<value_type> __l)
  508. { this->insert(__l.begin(), __l.end()); }
  509. #endif
  510. #if __cplusplus > 201402L
  511. /// Extract a node.
  512. node_type
  513. extract(const_iterator __pos)
  514. {
  515. __glibcxx_assert(__pos != end());
  516. return _M_t.extract(__pos);
  517. }
  518. /// Extract a node.
  519. node_type
  520. extract(const key_type& __x)
  521. { return _M_t.extract(__x); }
  522. /// Re-insert an extracted node.
  523. iterator
  524. insert(node_type&& __nh)
  525. { return _M_t._M_reinsert_node_equal(std::move(__nh)); }
  526. /// Re-insert an extracted node.
  527. iterator
  528. insert(const_iterator __hint, node_type&& __nh)
  529. { return _M_t._M_reinsert_node_hint_equal(__hint, std::move(__nh)); }
  530. template<typename, typename>
  531. friend class std::_Rb_tree_merge_helper;
  532. template<typename _Compare1>
  533. void
  534. merge(multiset<_Key, _Compare1, _Alloc>& __source)
  535. {
  536. using _Merge_helper = _Rb_tree_merge_helper<multiset, _Compare1>;
  537. _M_t._M_merge_equal(_Merge_helper::_S_get_tree(__source));
  538. }
  539. template<typename _Compare1>
  540. void
  541. merge(multiset<_Key, _Compare1, _Alloc>&& __source)
  542. { merge(__source); }
  543. template<typename _Compare1>
  544. void
  545. merge(set<_Key, _Compare1, _Alloc>& __source)
  546. {
  547. using _Merge_helper = _Rb_tree_merge_helper<multiset, _Compare1>;
  548. _M_t._M_merge_equal(_Merge_helper::_S_get_tree(__source));
  549. }
  550. template<typename _Compare1>
  551. void
  552. merge(set<_Key, _Compare1, _Alloc>&& __source)
  553. { merge(__source); }
  554. #endif // C++17
  555. #if __cplusplus >= 201103L
  556. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  557. // DR 130. Associative erase should return an iterator.
  558. /**
  559. * @brief Erases an element from a %multiset.
  560. * @param __position An iterator pointing to the element to be erased.
  561. * @return An iterator pointing to the element immediately following
  562. * @a position prior to the element being erased. If no such
  563. * element exists, end() is returned.
  564. *
  565. * This function erases an element, pointed to by the given iterator,
  566. * from a %multiset. Note that this function only erases the element,
  567. * and that if the element is itself a pointer, the pointed-to memory is
  568. * not touched in any way. Managing the pointer is the user's
  569. * responsibility.
  570. */
  571. _GLIBCXX_ABI_TAG_CXX11
  572. iterator
  573. erase(const_iterator __position)
  574. { return _M_t.erase(__position); }
  575. #else
  576. /**
  577. * @brief Erases an element from a %multiset.
  578. * @param __position An iterator pointing to the element to be erased.
  579. *
  580. * This function erases an element, pointed to by the given iterator,
  581. * from a %multiset. Note that this function only erases the element,
  582. * and that if the element is itself a pointer, the pointed-to memory is
  583. * not touched in any way. Managing the pointer is the user's
  584. * responsibility.
  585. */
  586. void
  587. erase(iterator __position)
  588. { _M_t.erase(__position); }
  589. #endif
  590. /**
  591. * @brief Erases elements according to the provided key.
  592. * @param __x Key of element to be erased.
  593. * @return The number of elements erased.
  594. *
  595. * This function erases all elements located by the given key from a
  596. * %multiset.
  597. * Note that this function only erases the element, and that if
  598. * the element is itself a pointer, the pointed-to memory is not touched
  599. * in any way. Managing the pointer is the user's responsibility.
  600. */
  601. size_type
  602. erase(const key_type& __x)
  603. { return _M_t.erase(__x); }
  604. #if __cplusplus >= 201103L
  605. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  606. // DR 130. Associative erase should return an iterator.
  607. /**
  608. * @brief Erases a [first,last) range of elements from a %multiset.
  609. * @param __first Iterator pointing to the start of the range to be
  610. * erased.
  611. * @param __last Iterator pointing to the end of the range to
  612. * be erased.
  613. * @return The iterator @a last.
  614. *
  615. * This function erases a sequence of elements from a %multiset.
  616. * Note that this function only erases the elements, and that if
  617. * the elements themselves are pointers, the pointed-to memory is not
  618. * touched in any way. Managing the pointer is the user's
  619. * responsibility.
  620. */
  621. _GLIBCXX_ABI_TAG_CXX11
  622. iterator
  623. erase(const_iterator __first, const_iterator __last)
  624. { return _M_t.erase(__first, __last); }
  625. #else
  626. /**
  627. * @brief Erases a [first,last) range of elements from a %multiset.
  628. * @param first Iterator pointing to the start of the range to be
  629. * erased.
  630. * @param last Iterator pointing to the end of the range to be erased.
  631. *
  632. * This function erases a sequence of elements from a %multiset.
  633. * Note that this function only erases the elements, and that if
  634. * the elements themselves are pointers, the pointed-to memory is not
  635. * touched in any way. Managing the pointer is the user's
  636. * responsibility.
  637. */
  638. void
  639. erase(iterator __first, iterator __last)
  640. { _M_t.erase(__first, __last); }
  641. #endif
  642. /**
  643. * Erases all elements in a %multiset. Note that this function only
  644. * erases the elements, and that if the elements themselves are pointers,
  645. * the pointed-to memory is not touched in any way. Managing the pointer
  646. * is the user's responsibility.
  647. */
  648. void
  649. clear() _GLIBCXX_NOEXCEPT
  650. { _M_t.clear(); }
  651. // multiset operations:
  652. //@{
  653. /**
  654. * @brief Finds the number of elements with given key.
  655. * @param __x Key of elements to be located.
  656. * @return Number of elements with specified key.
  657. */
  658. size_type
  659. count(const key_type& __x) const
  660. { return _M_t.count(__x); }
  661. #if __cplusplus > 201103L
  662. template<typename _Kt>
  663. auto
  664. count(const _Kt& __x) const -> decltype(_M_t._M_count_tr(__x))
  665. { return _M_t._M_count_tr(__x); }
  666. #endif
  667. //@}
  668. #if __cplusplus > 201703L
  669. //@{
  670. /**
  671. * @brief Finds whether an element with the given key exists.
  672. * @param __x Key of elements to be located.
  673. * @return True if there is any element with the specified key.
  674. */
  675. bool
  676. contains(const key_type& __x) const
  677. { return _M_t.find(__x) != _M_t.end(); }
  678. template<typename _Kt>
  679. auto
  680. contains(const _Kt& __x) const
  681. -> decltype(_M_t._M_find_tr(__x), void(), true)
  682. { return _M_t._M_find_tr(__x) != _M_t.end(); }
  683. //@}
  684. #endif
  685. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  686. // 214. set::find() missing const overload
  687. //@{
  688. /**
  689. * @brief Tries to locate an element in a %set.
  690. * @param __x Element to be located.
  691. * @return Iterator pointing to sought-after element, or end() if not
  692. * found.
  693. *
  694. * This function takes a key and tries to locate the element with which
  695. * the key matches. If successful the function returns an iterator
  696. * pointing to the sought after element. If unsuccessful it returns the
  697. * past-the-end ( @c end() ) iterator.
  698. */
  699. iterator
  700. find(const key_type& __x)
  701. { return _M_t.find(__x); }
  702. const_iterator
  703. find(const key_type& __x) const
  704. { return _M_t.find(__x); }
  705. #if __cplusplus > 201103L
  706. template<typename _Kt>
  707. auto
  708. find(const _Kt& __x)
  709. -> decltype(iterator{_M_t._M_find_tr(__x)})
  710. { return iterator{_M_t._M_find_tr(__x)}; }
  711. template<typename _Kt>
  712. auto
  713. find(const _Kt& __x) const
  714. -> decltype(const_iterator{_M_t._M_find_tr(__x)})
  715. { return const_iterator{_M_t._M_find_tr(__x)}; }
  716. #endif
  717. //@}
  718. //@{
  719. /**
  720. * @brief Finds the beginning of a subsequence matching given key.
  721. * @param __x Key to be located.
  722. * @return Iterator pointing to first element equal to or greater
  723. * than key, or end().
  724. *
  725. * This function returns the first element of a subsequence of elements
  726. * that matches the given key. If unsuccessful it returns an iterator
  727. * pointing to the first element that has a greater value than given key
  728. * or end() if no such element exists.
  729. */
  730. iterator
  731. lower_bound(const key_type& __x)
  732. { return _M_t.lower_bound(__x); }
  733. const_iterator
  734. lower_bound(const key_type& __x) const
  735. { return _M_t.lower_bound(__x); }
  736. #if __cplusplus > 201103L
  737. template<typename _Kt>
  738. auto
  739. lower_bound(const _Kt& __x)
  740. -> decltype(iterator(_M_t._M_lower_bound_tr(__x)))
  741. { return iterator(_M_t._M_lower_bound_tr(__x)); }
  742. template<typename _Kt>
  743. auto
  744. lower_bound(const _Kt& __x) const
  745. -> decltype(iterator(_M_t._M_lower_bound_tr(__x)))
  746. { return iterator(_M_t._M_lower_bound_tr(__x)); }
  747. #endif
  748. //@}
  749. //@{
  750. /**
  751. * @brief Finds the end of a subsequence matching given key.
  752. * @param __x Key to be located.
  753. * @return Iterator pointing to the first element
  754. * greater than key, or end().
  755. */
  756. iterator
  757. upper_bound(const key_type& __x)
  758. { return _M_t.upper_bound(__x); }
  759. const_iterator
  760. upper_bound(const key_type& __x) const
  761. { return _M_t.upper_bound(__x); }
  762. #if __cplusplus > 201103L
  763. template<typename _Kt>
  764. auto
  765. upper_bound(const _Kt& __x)
  766. -> decltype(iterator(_M_t._M_upper_bound_tr(__x)))
  767. { return iterator(_M_t._M_upper_bound_tr(__x)); }
  768. template<typename _Kt>
  769. auto
  770. upper_bound(const _Kt& __x) const
  771. -> decltype(iterator(_M_t._M_upper_bound_tr(__x)))
  772. { return iterator(_M_t._M_upper_bound_tr(__x)); }
  773. #endif
  774. //@}
  775. //@{
  776. /**
  777. * @brief Finds a subsequence matching given key.
  778. * @param __x Key to be located.
  779. * @return Pair of iterators that possibly points to the subsequence
  780. * matching given key.
  781. *
  782. * This function is equivalent to
  783. * @code
  784. * std::make_pair(c.lower_bound(val),
  785. * c.upper_bound(val))
  786. * @endcode
  787. * (but is faster than making the calls separately).
  788. *
  789. * This function probably only makes sense for multisets.
  790. */
  791. std::pair<iterator, iterator>
  792. equal_range(const key_type& __x)
  793. { return _M_t.equal_range(__x); }
  794. std::pair<const_iterator, const_iterator>
  795. equal_range(const key_type& __x) const
  796. { return _M_t.equal_range(__x); }
  797. #if __cplusplus > 201103L
  798. template<typename _Kt>
  799. auto
  800. equal_range(const _Kt& __x)
  801. -> decltype(pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)))
  802. { return pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)); }
  803. template<typename _Kt>
  804. auto
  805. equal_range(const _Kt& __x) const
  806. -> decltype(pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)))
  807. { return pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)); }
  808. #endif
  809. //@}
  810. template<typename _K1, typename _C1, typename _A1>
  811. friend bool
  812. operator==(const multiset<_K1, _C1, _A1>&,
  813. const multiset<_K1, _C1, _A1>&);
  814. #if __cpp_lib_three_way_comparison
  815. template<typename _K1, typename _C1, typename _A1>
  816. friend __detail::__synth3way_t<_K1>
  817. operator<=>(const multiset<_K1, _C1, _A1>&,
  818. const multiset<_K1, _C1, _A1>&);
  819. #else
  820. template<typename _K1, typename _C1, typename _A1>
  821. friend bool
  822. operator< (const multiset<_K1, _C1, _A1>&,
  823. const multiset<_K1, _C1, _A1>&);
  824. #endif
  825. };
  826. #if __cpp_deduction_guides >= 201606
  827. template<typename _InputIterator,
  828. typename _Compare =
  829. less<typename iterator_traits<_InputIterator>::value_type>,
  830. typename _Allocator =
  831. allocator<typename iterator_traits<_InputIterator>::value_type>,
  832. typename = _RequireInputIter<_InputIterator>,
  833. typename = _RequireNotAllocator<_Compare>,
  834. typename = _RequireAllocator<_Allocator>>
  835. multiset(_InputIterator, _InputIterator,
  836. _Compare = _Compare(), _Allocator = _Allocator())
  837. -> multiset<typename iterator_traits<_InputIterator>::value_type,
  838. _Compare, _Allocator>;
  839. template<typename _Key,
  840. typename _Compare = less<_Key>,
  841. typename _Allocator = allocator<_Key>,
  842. typename = _RequireNotAllocator<_Compare>,
  843. typename = _RequireAllocator<_Allocator>>
  844. multiset(initializer_list<_Key>,
  845. _Compare = _Compare(), _Allocator = _Allocator())
  846. -> multiset<_Key, _Compare, _Allocator>;
  847. template<typename _InputIterator, typename _Allocator,
  848. typename = _RequireInputIter<_InputIterator>,
  849. typename = _RequireAllocator<_Allocator>>
  850. multiset(_InputIterator, _InputIterator, _Allocator)
  851. -> multiset<typename iterator_traits<_InputIterator>::value_type,
  852. less<typename iterator_traits<_InputIterator>::value_type>,
  853. _Allocator>;
  854. template<typename _Key, typename _Allocator,
  855. typename = _RequireAllocator<_Allocator>>
  856. multiset(initializer_list<_Key>, _Allocator)
  857. -> multiset<_Key, less<_Key>, _Allocator>;
  858. #endif // deduction guides
  859. /**
  860. * @brief Multiset equality comparison.
  861. * @param __x A %multiset.
  862. * @param __y A %multiset of the same type as @a __x.
  863. * @return True iff the size and elements of the multisets are equal.
  864. *
  865. * This is an equivalence relation. It is linear in the size of the
  866. * multisets.
  867. * Multisets are considered equivalent if their sizes are equal, and if
  868. * corresponding elements compare equal.
  869. */
  870. template<typename _Key, typename _Compare, typename _Alloc>
  871. inline bool
  872. operator==(const multiset<_Key, _Compare, _Alloc>& __x,
  873. const multiset<_Key, _Compare, _Alloc>& __y)
  874. { return __x._M_t == __y._M_t; }
  875. #if __cpp_lib_three_way_comparison
  876. /**
  877. * @brief Multiset ordering relation.
  878. * @param __x A `multiset`.
  879. * @param __y A `multiset` of the same type as `x`.
  880. * @return A value indicating whether `__x` is less than, equal to,
  881. * greater than, or incomparable with `__y`.
  882. *
  883. * This is a total ordering relation. It is linear in the size of the
  884. * maps. The elements must be comparable with @c <.
  885. *
  886. * See `std::lexicographical_compare_three_way()` for how the determination
  887. * is made. This operator is used to synthesize relational operators like
  888. * `<` and `>=` etc.
  889. */
  890. template<typename _Key, typename _Compare, typename _Alloc>
  891. inline __detail::__synth3way_t<_Key>
  892. operator<=>(const multiset<_Key, _Compare, _Alloc>& __x,
  893. const multiset<_Key, _Compare, _Alloc>& __y)
  894. { return __x._M_t <=> __y._M_t; }
  895. #else
  896. /**
  897. * @brief Multiset ordering relation.
  898. * @param __x A %multiset.
  899. * @param __y A %multiset of the same type as @a __x.
  900. * @return True iff @a __x is lexicographically less than @a __y.
  901. *
  902. * This is a total ordering relation. It is linear in the size of the
  903. * sets. The elements must be comparable with @c <.
  904. *
  905. * See std::lexicographical_compare() for how the determination is made.
  906. */
  907. template<typename _Key, typename _Compare, typename _Alloc>
  908. inline bool
  909. operator<(const multiset<_Key, _Compare, _Alloc>& __x,
  910. const multiset<_Key, _Compare, _Alloc>& __y)
  911. { return __x._M_t < __y._M_t; }
  912. /// Returns !(x == y).
  913. template<typename _Key, typename _Compare, typename _Alloc>
  914. inline bool
  915. operator!=(const multiset<_Key, _Compare, _Alloc>& __x,
  916. const multiset<_Key, _Compare, _Alloc>& __y)
  917. { return !(__x == __y); }
  918. /// Returns y < x.
  919. template<typename _Key, typename _Compare, typename _Alloc>
  920. inline bool
  921. operator>(const multiset<_Key,_Compare,_Alloc>& __x,
  922. const multiset<_Key,_Compare,_Alloc>& __y)
  923. { return __y < __x; }
  924. /// Returns !(y < x)
  925. template<typename _Key, typename _Compare, typename _Alloc>
  926. inline bool
  927. operator<=(const multiset<_Key, _Compare, _Alloc>& __x,
  928. const multiset<_Key, _Compare, _Alloc>& __y)
  929. { return !(__y < __x); }
  930. /// Returns !(x < y)
  931. template<typename _Key, typename _Compare, typename _Alloc>
  932. inline bool
  933. operator>=(const multiset<_Key, _Compare, _Alloc>& __x,
  934. const multiset<_Key, _Compare, _Alloc>& __y)
  935. { return !(__x < __y); }
  936. #endif // three-way comparison
  937. /// See std::multiset::swap().
  938. template<typename _Key, typename _Compare, typename _Alloc>
  939. inline void
  940. swap(multiset<_Key, _Compare, _Alloc>& __x,
  941. multiset<_Key, _Compare, _Alloc>& __y)
  942. _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
  943. { __x.swap(__y); }
  944. _GLIBCXX_END_NAMESPACE_CONTAINER
  945. #if __cplusplus > 201402L
  946. // Allow std::multiset access to internals of compatible sets.
  947. template<typename _Val, typename _Cmp1, typename _Alloc, typename _Cmp2>
  948. struct
  949. _Rb_tree_merge_helper<_GLIBCXX_STD_C::multiset<_Val, _Cmp1, _Alloc>,
  950. _Cmp2>
  951. {
  952. private:
  953. friend class _GLIBCXX_STD_C::multiset<_Val, _Cmp1, _Alloc>;
  954. static auto&
  955. _S_get_tree(_GLIBCXX_STD_C::set<_Val, _Cmp2, _Alloc>& __set)
  956. { return __set._M_t; }
  957. static auto&
  958. _S_get_tree(_GLIBCXX_STD_C::multiset<_Val, _Cmp2, _Alloc>& __set)
  959. { return __set._M_t; }
  960. };
  961. #endif // C++17
  962. _GLIBCXX_END_NAMESPACE_VERSION
  963. } // namespace std
  964. #endif /* _STL_MULTISET_H */