stl_set.h 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. // Set implementation -*- C++ -*-
  2. // Copyright (C) 2001-2018 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /*
  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. # ifdef __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_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_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_unique(__l.begin(), __l.end()); }
  223. /// Allocator-extended default constructor.
  224. explicit
  225. set(const allocator_type& __a)
  226. : _M_t(_Compare(), _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(_Compare(), _Key_alloc_type(__a))
  238. { _M_t._M_insert_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(_Compare(), _Key_alloc_type(__a))
  244. { _M_t._M_insert_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. 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_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. // _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(const_iterator(_M_t._M_lower_bound_tr(__x)))
  747. { return const_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 const_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 set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&);
  814. template<typename _K1, typename _C1, typename _A1>
  815. friend bool
  816. operator<(const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&);
  817. };
  818. #if __cpp_deduction_guides >= 201606
  819. template<typename _InputIterator,
  820. typename _Compare =
  821. less<typename iterator_traits<_InputIterator>::value_type>,
  822. typename _Allocator =
  823. allocator<typename iterator_traits<_InputIterator>::value_type>,
  824. typename = _RequireInputIter<_InputIterator>,
  825. typename = _RequireAllocator<_Allocator>>
  826. set(_InputIterator, _InputIterator,
  827. _Compare = _Compare(), _Allocator = _Allocator())
  828. -> set<typename iterator_traits<_InputIterator>::value_type,
  829. _Compare, _Allocator>;
  830. template<typename _Key, typename _Compare = less<_Key>,
  831. typename _Allocator = allocator<_Key>,
  832. typename = _RequireAllocator<_Allocator>>
  833. set(initializer_list<_Key>,
  834. _Compare = _Compare(), _Allocator = _Allocator())
  835. -> set<_Key, _Compare, _Allocator>;
  836. template<typename _InputIterator, typename _Allocator,
  837. typename = _RequireInputIter<_InputIterator>,
  838. typename = _RequireAllocator<_Allocator>>
  839. set(_InputIterator, _InputIterator, _Allocator)
  840. -> set<typename iterator_traits<_InputIterator>::value_type,
  841. less<typename iterator_traits<_InputIterator>::value_type>,
  842. _Allocator>;
  843. template<typename _Key, typename _Allocator,
  844. typename = _RequireAllocator<_Allocator>>
  845. set(initializer_list<_Key>, _Allocator)
  846. -> set<_Key, less<_Key>, _Allocator>;
  847. #endif
  848. /**
  849. * @brief Set equality comparison.
  850. * @param __x A %set.
  851. * @param __y A %set of the same type as @a x.
  852. * @return True iff the size and elements of the sets are equal.
  853. *
  854. * This is an equivalence relation. It is linear in the size of the sets.
  855. * Sets are considered equivalent if their sizes are equal, and if
  856. * corresponding elements compare equal.
  857. */
  858. template<typename _Key, typename _Compare, typename _Alloc>
  859. inline bool
  860. operator==(const set<_Key, _Compare, _Alloc>& __x,
  861. const set<_Key, _Compare, _Alloc>& __y)
  862. { return __x._M_t == __y._M_t; }
  863. /**
  864. * @brief Set ordering relation.
  865. * @param __x A %set.
  866. * @param __y A %set of the same type as @a x.
  867. * @return True iff @a __x is lexicographically less than @a __y.
  868. *
  869. * This is a total ordering relation. It is linear in the size of the
  870. * sets. The elements must be comparable with @c <.
  871. *
  872. * See std::lexicographical_compare() for how the determination is made.
  873. */
  874. template<typename _Key, typename _Compare, typename _Alloc>
  875. inline bool
  876. operator<(const set<_Key, _Compare, _Alloc>& __x,
  877. const set<_Key, _Compare, _Alloc>& __y)
  878. { return __x._M_t < __y._M_t; }
  879. /// Returns !(x == y).
  880. template<typename _Key, typename _Compare, typename _Alloc>
  881. inline bool
  882. operator!=(const set<_Key, _Compare, _Alloc>& __x,
  883. const set<_Key, _Compare, _Alloc>& __y)
  884. { return !(__x == __y); }
  885. /// Returns y < x.
  886. template<typename _Key, typename _Compare, typename _Alloc>
  887. inline bool
  888. operator>(const set<_Key, _Compare, _Alloc>& __x,
  889. const set<_Key, _Compare, _Alloc>& __y)
  890. { return __y < __x; }
  891. /// Returns !(y < x)
  892. template<typename _Key, typename _Compare, typename _Alloc>
  893. inline bool
  894. operator<=(const set<_Key, _Compare, _Alloc>& __x,
  895. const set<_Key, _Compare, _Alloc>& __y)
  896. { return !(__y < __x); }
  897. /// Returns !(x < y)
  898. template<typename _Key, typename _Compare, typename _Alloc>
  899. inline bool
  900. operator>=(const set<_Key, _Compare, _Alloc>& __x,
  901. const set<_Key, _Compare, _Alloc>& __y)
  902. { return !(__x < __y); }
  903. /// See std::set::swap().
  904. template<typename _Key, typename _Compare, typename _Alloc>
  905. inline void
  906. swap(set<_Key, _Compare, _Alloc>& __x, set<_Key, _Compare, _Alloc>& __y)
  907. _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
  908. { __x.swap(__y); }
  909. _GLIBCXX_END_NAMESPACE_CONTAINER
  910. #if __cplusplus > 201402L
  911. // Allow std::set access to internals of compatible sets.
  912. template<typename _Val, typename _Cmp1, typename _Alloc, typename _Cmp2>
  913. struct
  914. _Rb_tree_merge_helper<_GLIBCXX_STD_C::set<_Val, _Cmp1, _Alloc>, _Cmp2>
  915. {
  916. private:
  917. friend class _GLIBCXX_STD_C::set<_Val, _Cmp1, _Alloc>;
  918. static auto&
  919. _S_get_tree(_GLIBCXX_STD_C::set<_Val, _Cmp2, _Alloc>& __set)
  920. { return __set._M_t; }
  921. static auto&
  922. _S_get_tree(_GLIBCXX_STD_C::multiset<_Val, _Cmp2, _Alloc>& __set)
  923. { return __set._M_t; }
  924. };
  925. #endif // C++17
  926. _GLIBCXX_END_NAMESPACE_VERSION
  927. } //namespace std
  928. #endif /* _STL_SET_H */