You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1087 lines
37KB

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