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