2016-10-26 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / libstdc++-v3 / include / bits / stl_set.h
blob5ed9672de5891354deff2c6184298baeda0bf7ea
1 // Set implementation -*- C++ -*-
3 // Copyright (C) 2001-2016 Free Software Foundation, Inc.
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
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
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.
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.
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/>.
27 * Copyright (c) 1994
28 * Hewlett-Packard Company
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.
39 * Copyright (c) 1996,1997
40 * Silicon Graphics Computer Systems, Inc.
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.
51 /** @file bits/stl_set.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{set}
56 #ifndef _STL_SET_H
57 #define _STL_SET_H 1
59 #include <bits/concept_check.h>
60 #if __cplusplus >= 201103L
61 #include <initializer_list>
62 #endif
64 namespace std _GLIBCXX_VISIBILITY(default)
66 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
68 template<typename _Key, typename _Compare, typename _Alloc>
69 class multiset;
71 /**
72 * @brief A standard container made up of unique keys, which can be
73 * retrieved in logarithmic time.
75 * @ingroup associative_containers
77 * @tparam _Key Type of key objects.
78 * @tparam _Compare Comparison function object type, defaults to less<_Key>.
79 * @tparam _Alloc Allocator type, defaults to allocator<_Key>.
81 * Meets the requirements of a <a href="tables.html#65">container</a>, a
82 * <a href="tables.html#66">reversible container</a>, and an
83 * <a href="tables.html#69">associative container</a> (using unique keys).
85 * Sets support bidirectional iterators.
87 * The private tree data is declared exactly the same way for set and
88 * multiset; the distinction is made entirely in how the tree functions are
89 * called (*_unique versus *_equal, same as the standard).
91 template<typename _Key, typename _Compare = std::less<_Key>,
92 typename _Alloc = std::allocator<_Key> >
93 class set
95 // concept requirements
96 typedef typename _Alloc::value_type _Alloc_value_type;
97 __glibcxx_class_requires(_Key, _SGIAssignableConcept)
98 __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
99 _BinaryFunctionConcept)
100 __glibcxx_class_requires2(_Key, _Alloc_value_type, _SameTypeConcept)
102 public:
103 // typedefs:
104 //@{
105 /// Public typedefs.
106 typedef _Key key_type;
107 typedef _Key value_type;
108 typedef _Compare key_compare;
109 typedef _Compare value_compare;
110 typedef _Alloc allocator_type;
111 //@}
113 private:
114 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
115 rebind<_Key>::other _Key_alloc_type;
117 typedef _Rb_tree<key_type, value_type, _Identity<value_type>,
118 key_compare, _Key_alloc_type> _Rep_type;
119 _Rep_type _M_t; // Red-black tree representing set.
121 typedef __gnu_cxx::__alloc_traits<_Key_alloc_type> _Alloc_traits;
123 public:
124 //@{
125 /// Iterator-related typedefs.
126 typedef typename _Alloc_traits::pointer pointer;
127 typedef typename _Alloc_traits::const_pointer const_pointer;
128 typedef typename _Alloc_traits::reference reference;
129 typedef typename _Alloc_traits::const_reference const_reference;
130 // _GLIBCXX_RESOLVE_LIB_DEFECTS
131 // DR 103. set::iterator is required to be modifiable,
132 // but this allows modification of keys.
133 typedef typename _Rep_type::const_iterator iterator;
134 typedef typename _Rep_type::const_iterator const_iterator;
135 typedef typename _Rep_type::const_reverse_iterator reverse_iterator;
136 typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
137 typedef typename _Rep_type::size_type size_type;
138 typedef typename _Rep_type::difference_type difference_type;
139 //@}
141 #if __cplusplus > 201402L
142 using node_type = typename _Rep_type::node_type;
143 using insert_return_type = typename _Rep_type::insert_return_type;
144 #endif
146 // allocation/deallocation
148 * @brief Default constructor creates no elements.
150 #if __cplusplus < 201103L
151 set() : _M_t() { }
152 #else
153 set() = default;
154 #endif
157 * @brief Creates a %set with no elements.
158 * @param __comp Comparator to use.
159 * @param __a An allocator object.
161 explicit
162 set(const _Compare& __comp,
163 const allocator_type& __a = allocator_type())
164 : _M_t(__comp, _Key_alloc_type(__a)) { }
167 * @brief Builds a %set from a range.
168 * @param __first An input iterator.
169 * @param __last An input iterator.
171 * Create a %set consisting of copies of the elements from
172 * [__first,__last). This is linear in N if the range is
173 * already sorted, and NlogN otherwise (where N is
174 * distance(__first,__last)).
176 template<typename _InputIterator>
177 set(_InputIterator __first, _InputIterator __last)
178 : _M_t()
179 { _M_t._M_insert_unique(__first, __last); }
182 * @brief Builds a %set from a range.
183 * @param __first An input iterator.
184 * @param __last An input iterator.
185 * @param __comp A comparison functor.
186 * @param __a An allocator object.
188 * Create a %set consisting of copies of the elements from
189 * [__first,__last). This is linear in N if the range is
190 * already sorted, and NlogN otherwise (where N is
191 * distance(__first,__last)).
193 template<typename _InputIterator>
194 set(_InputIterator __first, _InputIterator __last,
195 const _Compare& __comp,
196 const allocator_type& __a = allocator_type())
197 : _M_t(__comp, _Key_alloc_type(__a))
198 { _M_t._M_insert_unique(__first, __last); }
201 * @brief %Set copy constructor.
202 * @param __x A %set of identical element and allocator types.
204 * The newly-created %set uses a copy of the allocator object used
205 * by @a __x (unless the allocator traits dictate a different object).
207 set(const set& __x)
208 : _M_t(__x._M_t) { }
210 #if __cplusplus >= 201103L
212 * @brief %Set move constructor
213 * @param __x A %set of identical element and allocator types.
215 * The newly-created %set contains the exact contents of @a x.
216 * The contents of @a x are a valid, but unspecified %set.
218 set(set&& __x)
219 noexcept(is_nothrow_copy_constructible<_Compare>::value)
220 : _M_t(std::move(__x._M_t)) { }
223 * @brief Builds a %set from an initializer_list.
224 * @param __l An initializer_list.
225 * @param __comp A comparison functor.
226 * @param __a An allocator object.
228 * Create a %set consisting of copies of the elements in the list.
229 * This is linear in N if the list is already sorted, and NlogN
230 * otherwise (where N is @a __l.size()).
232 set(initializer_list<value_type> __l,
233 const _Compare& __comp = _Compare(),
234 const allocator_type& __a = allocator_type())
235 : _M_t(__comp, _Key_alloc_type(__a))
236 { _M_t._M_insert_unique(__l.begin(), __l.end()); }
238 /// Allocator-extended default constructor.
239 explicit
240 set(const allocator_type& __a)
241 : _M_t(_Compare(), _Key_alloc_type(__a)) { }
243 /// Allocator-extended copy constructor.
244 set(const set& __x, const allocator_type& __a)
245 : _M_t(__x._M_t, _Key_alloc_type(__a)) { }
247 /// Allocator-extended move constructor.
248 set(set&& __x, const allocator_type& __a)
249 noexcept(is_nothrow_copy_constructible<_Compare>::value
250 && _Alloc_traits::_S_always_equal())
251 : _M_t(std::move(__x._M_t), _Key_alloc_type(__a)) { }
253 /// Allocator-extended initialier-list constructor.
254 set(initializer_list<value_type> __l, const allocator_type& __a)
255 : _M_t(_Compare(), _Key_alloc_type(__a))
256 { _M_t._M_insert_unique(__l.begin(), __l.end()); }
258 /// Allocator-extended range constructor.
259 template<typename _InputIterator>
260 set(_InputIterator __first, _InputIterator __last,
261 const allocator_type& __a)
262 : _M_t(_Compare(), _Key_alloc_type(__a))
263 { _M_t._M_insert_unique(__first, __last); }
264 #endif
267 * @brief %Set assignment operator.
268 * @param __x A %set of identical element and allocator types.
270 * All the elements of @a __x are copied.
272 * Whether the allocator is copied depends on the allocator traits.
274 set&
275 operator=(const set& __x)
277 _M_t = __x._M_t;
278 return *this;
281 #if __cplusplus >= 201103L
282 /// Move assignment operator.
283 set&
284 operator=(set&&) = default;
287 * @brief %Set list assignment operator.
288 * @param __l An initializer_list.
290 * This function fills a %set with copies of the elements in the
291 * initializer list @a __l.
293 * Note that the assignment completely changes the %set and
294 * that the resulting %set's size is the same as the number
295 * of elements assigned.
297 set&
298 operator=(initializer_list<value_type> __l)
300 _M_t._M_assign_unique(__l.begin(), __l.end());
301 return *this;
303 #endif
305 // accessors:
307 /// Returns the comparison object with which the %set was constructed.
308 key_compare
309 key_comp() const
310 { return _M_t.key_comp(); }
311 /// Returns the comparison object with which the %set was constructed.
312 value_compare
313 value_comp() const
314 { return _M_t.key_comp(); }
315 /// Returns the allocator object with which the %set was constructed.
316 allocator_type
317 get_allocator() const _GLIBCXX_NOEXCEPT
318 { return allocator_type(_M_t.get_allocator()); }
321 * Returns a read-only (constant) iterator that points to the first
322 * element in the %set. Iteration is done in ascending order according
323 * to the keys.
325 iterator
326 begin() const _GLIBCXX_NOEXCEPT
327 { return _M_t.begin(); }
330 * Returns a read-only (constant) iterator that points one past the last
331 * element in the %set. Iteration is done in ascending order according
332 * to the keys.
334 iterator
335 end() const _GLIBCXX_NOEXCEPT
336 { return _M_t.end(); }
339 * Returns a read-only (constant) iterator that points to the last
340 * element in the %set. Iteration is done in descending order according
341 * to the keys.
343 reverse_iterator
344 rbegin() const _GLIBCXX_NOEXCEPT
345 { return _M_t.rbegin(); }
348 * Returns a read-only (constant) reverse iterator that points to the
349 * last pair in the %set. Iteration is done in descending order
350 * according to the keys.
352 reverse_iterator
353 rend() const _GLIBCXX_NOEXCEPT
354 { return _M_t.rend(); }
356 #if __cplusplus >= 201103L
358 * Returns a read-only (constant) iterator that points to the first
359 * element in the %set. Iteration is done in ascending order according
360 * to the keys.
362 iterator
363 cbegin() const noexcept
364 { return _M_t.begin(); }
367 * Returns a read-only (constant) iterator that points one past the last
368 * element in the %set. Iteration is done in ascending order according
369 * to the keys.
371 iterator
372 cend() const noexcept
373 { return _M_t.end(); }
376 * Returns a read-only (constant) iterator that points to the last
377 * element in the %set. Iteration is done in descending order according
378 * to the keys.
380 reverse_iterator
381 crbegin() const noexcept
382 { return _M_t.rbegin(); }
385 * Returns a read-only (constant) reverse iterator that points to the
386 * last pair in the %set. Iteration is done in descending order
387 * according to the keys.
389 reverse_iterator
390 crend() const noexcept
391 { return _M_t.rend(); }
392 #endif
394 /// Returns true if the %set is empty.
395 bool
396 empty() const _GLIBCXX_NOEXCEPT
397 { return _M_t.empty(); }
399 /// Returns the size of the %set.
400 size_type
401 size() const _GLIBCXX_NOEXCEPT
402 { return _M_t.size(); }
404 /// Returns the maximum size of the %set.
405 size_type
406 max_size() const _GLIBCXX_NOEXCEPT
407 { return _M_t.max_size(); }
410 * @brief Swaps data with another %set.
411 * @param __x A %set of the same element and allocator types.
413 * This exchanges the elements between two sets in constant
414 * time. (It is only swapping a pointer, an integer, and an
415 * instance of the @c Compare type (which itself is often
416 * stateless and empty), so it should be quite fast.) Note
417 * that the global std::swap() function is specialized such
418 * that std::swap(s1,s2) will feed to this function.
420 * Whether the allocators are swapped depends on the allocator traits.
422 void
423 swap(set& __x)
424 _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value)
425 { _M_t.swap(__x._M_t); }
427 // insert/erase
428 #if __cplusplus >= 201103L
430 * @brief Attempts to build and insert an element into the %set.
431 * @param __args Arguments used to generate an element.
432 * @return A pair, of which the first element is an iterator that points
433 * to the possibly inserted element, and the second is a bool
434 * that is true if the element was actually inserted.
436 * This function attempts to build and insert an element into the %set.
437 * A %set relies on unique keys and thus an element is only inserted if
438 * it is not already present in the %set.
440 * Insertion requires logarithmic time.
442 template<typename... _Args>
443 std::pair<iterator, bool>
444 emplace(_Args&&... __args)
445 { return _M_t._M_emplace_unique(std::forward<_Args>(__args)...); }
448 * @brief Attempts to insert an element into the %set.
449 * @param __pos An iterator that serves as a hint as to where the
450 * element should be inserted.
451 * @param __args Arguments used to generate the element to be
452 * inserted.
453 * @return An iterator that points to the element with key equivalent to
454 * the one generated from @a __args (may or may not be the
455 * element itself).
457 * This function is not concerned about whether the insertion took place,
458 * and thus does not return a boolean like the single-argument emplace()
459 * does. Note that the first parameter is only a hint and can
460 * potentially improve the performance of the insertion process. A bad
461 * hint would cause no gains in efficiency.
463 * For more on @a hinting, see:
464 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
466 * Insertion requires logarithmic time (if the hint is not taken).
468 template<typename... _Args>
469 iterator
470 emplace_hint(const_iterator __pos, _Args&&... __args)
472 return _M_t._M_emplace_hint_unique(__pos,
473 std::forward<_Args>(__args)...);
475 #endif
478 * @brief Attempts to insert an element into the %set.
479 * @param __x Element to be inserted.
480 * @return A pair, of which the first element is an iterator that points
481 * to the possibly inserted element, and the second is a bool
482 * that is true if the element was actually inserted.
484 * This function attempts to insert an element into the %set. A %set
485 * relies on unique keys and thus an element is only inserted if it is
486 * not already present in the %set.
488 * Insertion requires logarithmic time.
490 std::pair<iterator, bool>
491 insert(const value_type& __x)
493 std::pair<typename _Rep_type::iterator, bool> __p =
494 _M_t._M_insert_unique(__x);
495 return std::pair<iterator, bool>(__p.first, __p.second);
498 #if __cplusplus >= 201103L
499 std::pair<iterator, bool>
500 insert(value_type&& __x)
502 std::pair<typename _Rep_type::iterator, bool> __p =
503 _M_t._M_insert_unique(std::move(__x));
504 return std::pair<iterator, bool>(__p.first, __p.second);
506 #endif
509 * @brief Attempts to insert an element into the %set.
510 * @param __position An iterator that serves as a hint as to where the
511 * element should be inserted.
512 * @param __x Element to be inserted.
513 * @return An iterator that points to the element with key of
514 * @a __x (may or may not be the element passed in).
516 * This function is not concerned about whether the insertion took place,
517 * and thus does not return a boolean like the single-argument insert()
518 * does. Note that the first parameter is only a hint and can
519 * potentially improve the performance of the insertion process. A bad
520 * hint would cause no gains in efficiency.
522 * For more on @a hinting, see:
523 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
525 * Insertion requires logarithmic time (if the hint is not taken).
527 iterator
528 insert(const_iterator __position, const value_type& __x)
529 { return _M_t._M_insert_unique_(__position, __x); }
531 #if __cplusplus >= 201103L
532 iterator
533 insert(const_iterator __position, value_type&& __x)
534 { return _M_t._M_insert_unique_(__position, std::move(__x)); }
535 #endif
538 * @brief A template function that attempts to insert a range
539 * of elements.
540 * @param __first Iterator pointing to the start of the range to be
541 * inserted.
542 * @param __last Iterator pointing to the end of the range.
544 * Complexity similar to that of the range constructor.
546 template<typename _InputIterator>
547 void
548 insert(_InputIterator __first, _InputIterator __last)
549 { _M_t._M_insert_unique(__first, __last); }
551 #if __cplusplus >= 201103L
553 * @brief Attempts to insert a list of elements into the %set.
554 * @param __l A std::initializer_list<value_type> of elements
555 * to be inserted.
557 * Complexity similar to that of the range constructor.
559 void
560 insert(initializer_list<value_type> __l)
561 { this->insert(__l.begin(), __l.end()); }
562 #endif
564 #if __cplusplus > 201402L
565 /// Extract a node.
566 node_type
567 extract(const_iterator __pos)
569 __glibcxx_assert(__pos != end());
570 return _M_t.extract(__pos);
573 /// Extract a node.
574 node_type
575 extract(const key_type& __x)
576 { return _M_t.extract(__x); }
578 /// Re-insert an extracted node.
579 insert_return_type
580 insert(node_type&& __nh)
581 { return _M_t._M_reinsert_node_unique(std::move(__nh)); }
583 /// Re-insert an extracted node.
584 iterator
585 insert(const_iterator __hint, node_type&& __nh)
586 { return _M_t._M_reinsert_node_hint_unique(__hint, std::move(__nh)); }
588 template<typename, typename>
589 friend class _Rb_tree_merge_helper;
591 template<typename _Compare1>
592 void
593 merge(set<_Key, _Compare1, _Alloc>& __source)
595 using _Merge_helper = _Rb_tree_merge_helper<set, _Compare1>;
596 _M_t._M_merge_unique(_Merge_helper::_S_get_tree(__source));
599 template<typename _Compare1>
600 void
601 merge(set<_Key, _Compare1, _Alloc>&& __source)
602 { merge(__source); }
604 template<typename _Compare1>
605 void
606 merge(multiset<_Key, _Compare1, _Alloc>& __source)
608 using _Merge_helper = _Rb_tree_merge_helper<set, _Compare1>;
609 _M_t._M_merge_unique(_Merge_helper::_S_get_tree(__source));
612 template<typename _Compare1>
613 void
614 merge(multiset<_Key, _Compare1, _Alloc>&& __source)
615 { merge(__source); }
616 #endif // C++17
618 #if __cplusplus >= 201103L
619 // _GLIBCXX_RESOLVE_LIB_DEFECTS
620 // DR 130. Associative erase should return an iterator.
622 * @brief Erases an element from a %set.
623 * @param __position An iterator pointing to the element to be erased.
624 * @return An iterator pointing to the element immediately following
625 * @a __position prior to the element being erased. If no such
626 * element exists, end() is returned.
628 * This function erases an element, pointed to by the given iterator,
629 * from a %set. Note that this function only erases the element, and
630 * that if the element is itself a pointer, the pointed-to memory is not
631 * touched in any way. Managing the pointer is the user's
632 * responsibility.
634 _GLIBCXX_ABI_TAG_CXX11
635 iterator
636 erase(const_iterator __position)
637 { return _M_t.erase(__position); }
638 #else
640 * @brief Erases an element from a %set.
641 * @param position An iterator pointing to the element to be erased.
643 * This function erases an element, pointed to by the given iterator,
644 * from a %set. Note that this function only erases the element, and
645 * that if the element is itself a pointer, the pointed-to memory is not
646 * touched in any way. Managing the pointer is the user's
647 * responsibility.
649 void
650 erase(iterator __position)
651 { _M_t.erase(__position); }
652 #endif
655 * @brief Erases elements according to the provided key.
656 * @param __x Key of element to be erased.
657 * @return The number of elements erased.
659 * This function erases all the elements located by the given key from
660 * a %set.
661 * Note that this function only erases the element, and that if
662 * the element is itself a pointer, the pointed-to memory is not touched
663 * in any way. Managing the pointer is the user's responsibility.
665 size_type
666 erase(const key_type& __x)
667 { return _M_t.erase(__x); }
669 #if __cplusplus >= 201103L
670 // _GLIBCXX_RESOLVE_LIB_DEFECTS
671 // DR 130. Associative erase should return an iterator.
673 * @brief Erases a [__first,__last) range of elements from a %set.
674 * @param __first Iterator pointing to the start of the range to be
675 * erased.
677 * @param __last Iterator pointing to the end of the range to
678 * be erased.
679 * @return The iterator @a __last.
681 * This function erases a sequence of elements from a %set.
682 * Note that this function only erases the element, and that if
683 * the element is itself a pointer, the pointed-to memory is not touched
684 * in any way. Managing the pointer is the user's responsibility.
686 _GLIBCXX_ABI_TAG_CXX11
687 iterator
688 erase(const_iterator __first, const_iterator __last)
689 { return _M_t.erase(__first, __last); }
690 #else
692 * @brief Erases a [first,last) range of elements from a %set.
693 * @param __first Iterator pointing to the start of the range to be
694 * erased.
695 * @param __last Iterator pointing to the end of the range to
696 * be erased.
698 * This function erases a sequence of elements from a %set.
699 * Note that this function only erases the element, and that if
700 * the element is itself a pointer, the pointed-to memory is not touched
701 * in any way. Managing the pointer is the user's responsibility.
703 void
704 erase(iterator __first, iterator __last)
705 { _M_t.erase(__first, __last); }
706 #endif
709 * Erases all elements in a %set. Note that this function only erases
710 * the elements, and that if the elements themselves are pointers, the
711 * pointed-to memory is not touched in any way. Managing the pointer is
712 * the user's responsibility.
714 void
715 clear() _GLIBCXX_NOEXCEPT
716 { _M_t.clear(); }
718 // set operations:
720 //@{
722 * @brief Finds the number of elements.
723 * @param __x Element to located.
724 * @return Number of elements with specified key.
726 * This function only makes sense for multisets; for set the result will
727 * either be 0 (not present) or 1 (present).
729 size_type
730 count(const key_type& __x) const
731 { return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
733 #if __cplusplus > 201103L
734 template<typename _Kt>
735 auto
736 count(const _Kt& __x) const
737 -> decltype(_M_t._M_count_tr(__x))
738 { return _M_t._M_find_tr(__x) == _M_t.end() ? 0 : 1; }
739 #endif
740 //@}
742 // _GLIBCXX_RESOLVE_LIB_DEFECTS
743 // 214. set::find() missing const overload
744 //@{
746 * @brief Tries to locate an element in a %set.
747 * @param __x Element to be located.
748 * @return Iterator pointing to sought-after element, or end() if not
749 * found.
751 * This function takes a key and tries to locate the element with which
752 * the key matches. If successful the function returns an iterator
753 * pointing to the sought after element. If unsuccessful it returns the
754 * past-the-end ( @c end() ) iterator.
756 iterator
757 find(const key_type& __x)
758 { return _M_t.find(__x); }
760 const_iterator
761 find(const key_type& __x) const
762 { return _M_t.find(__x); }
764 #if __cplusplus > 201103L
765 template<typename _Kt>
766 auto
767 find(const _Kt& __x)
768 -> decltype(iterator{_M_t._M_find_tr(__x)})
769 { return iterator{_M_t._M_find_tr(__x)}; }
771 template<typename _Kt>
772 auto
773 find(const _Kt& __x) const
774 -> decltype(const_iterator{_M_t._M_find_tr(__x)})
775 { return const_iterator{_M_t._M_find_tr(__x)}; }
776 #endif
777 //@}
779 //@{
781 * @brief Finds the beginning of a subsequence matching given key.
782 * @param __x Key to be located.
783 * @return Iterator pointing to first element equal to or greater
784 * than key, or end().
786 * This function returns the first element of a subsequence of elements
787 * that matches the given key. If unsuccessful it returns an iterator
788 * pointing to the first element that has a greater value than given key
789 * or end() if no such element exists.
791 iterator
792 lower_bound(const key_type& __x)
793 { return _M_t.lower_bound(__x); }
795 const_iterator
796 lower_bound(const key_type& __x) const
797 { return _M_t.lower_bound(__x); }
799 #if __cplusplus > 201103L
800 template<typename _Kt>
801 auto
802 lower_bound(const _Kt& __x)
803 -> decltype(_M_t._M_lower_bound_tr(__x))
804 { return _M_t._M_lower_bound_tr(__x); }
806 template<typename _Kt>
807 auto
808 lower_bound(const _Kt& __x) const
809 -> decltype(_M_t._M_lower_bound_tr(__x))
810 { return _M_t._M_lower_bound_tr(__x); }
811 #endif
812 //@}
814 //@{
816 * @brief Finds the end of a subsequence matching given key.
817 * @param __x Key to be located.
818 * @return Iterator pointing to the first element
819 * greater than key, or end().
821 iterator
822 upper_bound(const key_type& __x)
823 { return _M_t.upper_bound(__x); }
825 const_iterator
826 upper_bound(const key_type& __x) const
827 { return _M_t.upper_bound(__x); }
829 #if __cplusplus > 201103L
830 template<typename _Kt>
831 auto
832 upper_bound(const _Kt& __x)
833 -> decltype(_M_t._M_upper_bound_tr(__x))
834 { return _M_t._M_upper_bound_tr(__x); }
836 template<typename _Kt>
837 auto
838 upper_bound(const _Kt& __x) const
839 -> decltype(_M_t._M_upper_bound_tr(__x))
840 { return _M_t._M_upper_bound_tr(__x); }
841 #endif
842 //@}
844 //@{
846 * @brief Finds a subsequence matching given key.
847 * @param __x Key to be located.
848 * @return Pair of iterators that possibly points to the subsequence
849 * matching given key.
851 * This function is equivalent to
852 * @code
853 * std::make_pair(c.lower_bound(val),
854 * c.upper_bound(val))
855 * @endcode
856 * (but is faster than making the calls separately).
858 * This function probably only makes sense for multisets.
860 std::pair<iterator, iterator>
861 equal_range(const key_type& __x)
862 { return _M_t.equal_range(__x); }
864 std::pair<const_iterator, const_iterator>
865 equal_range(const key_type& __x) const
866 { return _M_t.equal_range(__x); }
868 #if __cplusplus > 201103L
869 template<typename _Kt>
870 auto
871 equal_range(const _Kt& __x)
872 -> decltype(_M_t._M_equal_range_tr(__x))
873 { return _M_t._M_equal_range_tr(__x); }
875 template<typename _Kt>
876 auto
877 equal_range(const _Kt& __x) const
878 -> decltype(_M_t._M_equal_range_tr(__x))
879 { return _M_t._M_equal_range_tr(__x); }
880 #endif
881 //@}
883 template<typename _K1, typename _C1, typename _A1>
884 friend bool
885 operator==(const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&);
887 template<typename _K1, typename _C1, typename _A1>
888 friend bool
889 operator<(const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&);
894 * @brief Set equality comparison.
895 * @param __x A %set.
896 * @param __y A %set of the same type as @a x.
897 * @return True iff the size and elements of the sets are equal.
899 * This is an equivalence relation. It is linear in the size of the sets.
900 * Sets are considered equivalent if their sizes are equal, and if
901 * corresponding elements compare equal.
903 template<typename _Key, typename _Compare, typename _Alloc>
904 inline bool
905 operator==(const set<_Key, _Compare, _Alloc>& __x,
906 const set<_Key, _Compare, _Alloc>& __y)
907 { return __x._M_t == __y._M_t; }
910 * @brief Set ordering relation.
911 * @param __x A %set.
912 * @param __y A %set of the same type as @a x.
913 * @return True iff @a __x is lexicographically less than @a __y.
915 * This is a total ordering relation. It is linear in the size of the
916 * sets. The elements must be comparable with @c <.
918 * See std::lexicographical_compare() for how the determination is made.
920 template<typename _Key, typename _Compare, typename _Alloc>
921 inline bool
922 operator<(const set<_Key, _Compare, _Alloc>& __x,
923 const set<_Key, _Compare, _Alloc>& __y)
924 { return __x._M_t < __y._M_t; }
926 /// Returns !(x == y).
927 template<typename _Key, typename _Compare, typename _Alloc>
928 inline bool
929 operator!=(const set<_Key, _Compare, _Alloc>& __x,
930 const set<_Key, _Compare, _Alloc>& __y)
931 { return !(__x == __y); }
933 /// Returns y < x.
934 template<typename _Key, typename _Compare, typename _Alloc>
935 inline bool
936 operator>(const set<_Key, _Compare, _Alloc>& __x,
937 const set<_Key, _Compare, _Alloc>& __y)
938 { return __y < __x; }
940 /// Returns !(y < x)
941 template<typename _Key, typename _Compare, typename _Alloc>
942 inline bool
943 operator<=(const set<_Key, _Compare, _Alloc>& __x,
944 const set<_Key, _Compare, _Alloc>& __y)
945 { return !(__y < __x); }
947 /// Returns !(x < y)
948 template<typename _Key, typename _Compare, typename _Alloc>
949 inline bool
950 operator>=(const set<_Key, _Compare, _Alloc>& __x,
951 const set<_Key, _Compare, _Alloc>& __y)
952 { return !(__x < __y); }
954 /// See std::set::swap().
955 template<typename _Key, typename _Compare, typename _Alloc>
956 inline void
957 swap(set<_Key, _Compare, _Alloc>& __x, set<_Key, _Compare, _Alloc>& __y)
958 _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
959 { __x.swap(__y); }
961 _GLIBCXX_END_NAMESPACE_CONTAINER
963 #if __cplusplus > 201402L
964 _GLIBCXX_BEGIN_NAMESPACE_VERSION
965 // Allow std::set access to internals of compatible sets.
966 template<typename _Val, typename _Cmp1, typename _Alloc, typename _Cmp2>
967 struct
968 _Rb_tree_merge_helper<_GLIBCXX_STD_C::set<_Val, _Cmp1, _Alloc>, _Cmp2>
970 private:
971 friend class _GLIBCXX_STD_C::set<_Val, _Cmp1, _Alloc>;
973 static auto&
974 _S_get_tree(_GLIBCXX_STD_C::set<_Val, _Cmp2, _Alloc>& __set)
975 { return __set._M_t; }
977 static auto&
978 _S_get_tree(_GLIBCXX_STD_C::multiset<_Val, _Cmp2, _Alloc>& __set)
979 { return __set._M_t; }
981 _GLIBCXX_END_NAMESPACE_VERSION
982 #endif // C++17
984 } //namespace std
985 #endif /* _STL_SET_H */