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