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