1 // Set implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 // 2011, 2012 Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
29 * Hewlett-Packard Company
31 * Permission to use, copy, modify, distribute and sell this software
32 * and its documentation for any purpose is hereby granted without fee,
33 * provided that the above copyright notice appear in all copies and
34 * that both that copyright notice and this permission notice appear
35 * in supporting documentation. Hewlett-Packard Company makes no
36 * representations about the suitability of this software for any
37 * purpose. It is provided "as is" without express or implied warranty.
40 * Copyright (c) 1996,1997
41 * Silicon Graphics Computer Systems, Inc.
43 * Permission to use, copy, modify, distribute and sell this software
44 * and its documentation for any purpose is hereby granted without fee,
45 * provided that the above copyright notice appear in all copies and
46 * that both that copyright notice and this permission notice appear
47 * in supporting documentation. Silicon Graphics makes no
48 * representations about the suitability of this software for any
49 * purpose. It is provided "as is" without express or implied warranty.
52 /** @file bits/stl_set.h
53 * This is an internal header file, included by other library headers.
54 * Do not attempt to use it directly. @headername{set}
60 #include <bits/concept_check.h>
61 #if __cplusplus >= 201103L
62 #include <initializer_list>
65 namespace std
_GLIBCXX_VISIBILITY(default)
67 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
70 * @brief A standard container made up of unique keys, which can be
71 * retrieved in logarithmic time.
73 * @ingroup associative_containers
75 * @tparam _Key Type of key objects.
76 * @tparam _Compare Comparison function object type, defaults to less<_Key>.
77 * @tparam _Alloc Allocator type, defaults to allocator<_Key>.
79 * Meets the requirements of a <a href="tables.html#65">container</a>, a
80 * <a href="tables.html#66">reversible container</a>, and an
81 * <a href="tables.html#69">associative container</a> (using unique keys).
83 * Sets support bidirectional iterators.
85 * The private tree data is declared exactly the same way for set and
86 * multiset; the distinction is made entirely in how the tree functions are
87 * called (*_unique versus *_equal, same as the standard).
89 template<typename _Key
, typename _Compare
= std::less
<_Key
>,
90 typename _Alloc
= std::allocator
<_Key
> >
93 // concept requirements
94 typedef typename
_Alloc::value_type _Alloc_value_type
;
95 __glibcxx_class_requires(_Key
, _SGIAssignableConcept
)
96 __glibcxx_class_requires4(_Compare
, bool, _Key
, _Key
,
97 _BinaryFunctionConcept
)
98 __glibcxx_class_requires2(_Key
, _Alloc_value_type
, _SameTypeConcept
)
104 typedef _Key key_type
;
105 typedef _Key value_type
;
106 typedef _Compare key_compare
;
107 typedef _Compare value_compare
;
108 typedef _Alloc allocator_type
;
112 typedef typename
_Alloc::template rebind
<_Key
>::other _Key_alloc_type
;
114 typedef _Rb_tree
<key_type
, value_type
, _Identity
<value_type
>,
115 key_compare
, _Key_alloc_type
> _Rep_type
;
116 _Rep_type _M_t
; // Red-black tree representing set.
120 /// Iterator-related typedefs.
121 typedef typename
_Key_alloc_type::pointer pointer
;
122 typedef typename
_Key_alloc_type::const_pointer const_pointer
;
123 typedef typename
_Key_alloc_type::reference reference
;
124 typedef typename
_Key_alloc_type::const_reference const_reference
;
125 // _GLIBCXX_RESOLVE_LIB_DEFECTS
126 // DR 103. set::iterator is required to be modifiable,
127 // but this allows modification of keys.
128 typedef typename
_Rep_type::const_iterator iterator
;
129 typedef typename
_Rep_type::const_iterator const_iterator
;
130 typedef typename
_Rep_type::const_reverse_iterator reverse_iterator
;
131 typedef typename
_Rep_type::const_reverse_iterator const_reverse_iterator
;
132 typedef typename
_Rep_type::size_type size_type
;
133 typedef typename
_Rep_type::difference_type difference_type
;
136 // allocation/deallocation
138 * @brief Default constructor creates no elements.
144 * @brief Creates a %set with no elements.
145 * @param __comp Comparator to use.
146 * @param __a An allocator object.
149 set(const _Compare
& __comp
,
150 const allocator_type
& __a
= allocator_type())
151 : _M_t(__comp
, _Key_alloc_type(__a
)) { }
154 * @brief Builds a %set from a range.
155 * @param __first An input iterator.
156 * @param __last An input iterator.
158 * Create a %set consisting of copies of the elements from
159 * [__first,__last). This is linear in N if the range is
160 * already sorted, and NlogN otherwise (where N is
161 * distance(__first,__last)).
163 template<typename _InputIterator
>
164 set(_InputIterator __first
, _InputIterator __last
)
166 { _M_t
._M_insert_unique(__first
, __last
); }
169 * @brief Builds a %set from a range.
170 * @param __first An input iterator.
171 * @param __last An input iterator.
172 * @param __comp A comparison functor.
173 * @param __a An allocator object.
175 * Create a %set consisting of copies of the elements from
176 * [__first,__last). This is linear in N if the range is
177 * already sorted, and NlogN otherwise (where N is
178 * distance(__first,__last)).
180 template<typename _InputIterator
>
181 set(_InputIterator __first
, _InputIterator __last
,
182 const _Compare
& __comp
,
183 const allocator_type
& __a
= allocator_type())
184 : _M_t(__comp
, _Key_alloc_type(__a
))
185 { _M_t
._M_insert_unique(__first
, __last
); }
188 * @brief %Set copy constructor.
189 * @param __x A %set of identical element and allocator types.
191 * The newly-created %set uses a copy of the allocation object used
197 #if __cplusplus >= 201103L
199 * @brief %Set move constructor
200 * @param __x A %set of identical element and allocator types.
202 * The newly-created %set contains the exact contents of @a x.
203 * The contents of @a x are a valid, but unspecified %set.
206 noexcept(is_nothrow_copy_constructible
<_Compare
>::value
)
207 : _M_t(std::move(__x
._M_t
)) { }
210 * @brief Builds a %set from an initializer_list.
211 * @param __l An initializer_list.
212 * @param __comp A comparison functor.
213 * @param __a An allocator object.
215 * Create a %set consisting of copies of the elements in the list.
216 * This is linear in N if the list is already sorted, and NlogN
217 * otherwise (where N is @a __l.size()).
219 set(initializer_list
<value_type
> __l
,
220 const _Compare
& __comp
= _Compare(),
221 const allocator_type
& __a
= allocator_type())
222 : _M_t(__comp
, _Key_alloc_type(__a
))
223 { _M_t
._M_insert_unique(__l
.begin(), __l
.end()); }
227 * @brief %Set assignment operator.
228 * @param __x A %set of identical element and allocator types.
230 * All the elements of @a __x are copied, but unlike the copy
231 * constructor, the allocator object is not copied.
234 operator=(const set
& __x
)
240 #if __cplusplus >= 201103L
242 * @brief %Set move assignment operator.
243 * @param __x A %set of identical element and allocator types.
245 * The contents of @a __x are moved into this %set (without copying).
246 * @a __x is a valid, but unspecified %set.
259 * @brief %Set list assignment operator.
260 * @param __l An initializer_list.
262 * This function fills a %set with copies of the elements in the
263 * initializer list @a __l.
265 * Note that the assignment completely changes the %set and
266 * that the resulting %set's size is the same as the number
267 * of elements assigned. Old data may be lost.
270 operator=(initializer_list
<value_type
> __l
)
273 this->insert(__l
.begin(), __l
.end());
280 /// Returns the comparison object with which the %set was constructed.
283 { return _M_t
.key_comp(); }
284 /// Returns the comparison object with which the %set was constructed.
287 { return _M_t
.key_comp(); }
288 /// Returns the allocator object with which the %set was constructed.
290 get_allocator() const _GLIBCXX_NOEXCEPT
291 { return allocator_type(_M_t
.get_allocator()); }
294 * Returns a read-only (constant) iterator that points to the first
295 * element in the %set. Iteration is done in ascending order according
299 begin() const _GLIBCXX_NOEXCEPT
300 { return _M_t
.begin(); }
303 * Returns a read-only (constant) iterator that points one past the last
304 * element in the %set. Iteration is done in ascending order according
308 end() const _GLIBCXX_NOEXCEPT
309 { return _M_t
.end(); }
312 * Returns a read-only (constant) iterator that points to the last
313 * element in the %set. Iteration is done in descending order according
317 rbegin() const _GLIBCXX_NOEXCEPT
318 { return _M_t
.rbegin(); }
321 * Returns a read-only (constant) reverse iterator that points to the
322 * last pair in the %set. Iteration is done in descending order
323 * according to the keys.
326 rend() const _GLIBCXX_NOEXCEPT
327 { return _M_t
.rend(); }
329 #if __cplusplus >= 201103L
331 * Returns a read-only (constant) iterator that points to the first
332 * element in the %set. Iteration is done in ascending order according
336 cbegin() const noexcept
337 { return _M_t
.begin(); }
340 * Returns a read-only (constant) iterator that points one past the last
341 * element in the %set. Iteration is done in ascending order according
345 cend() const noexcept
346 { return _M_t
.end(); }
349 * Returns a read-only (constant) iterator that points to the last
350 * element in the %set. Iteration is done in descending order according
354 crbegin() const noexcept
355 { return _M_t
.rbegin(); }
358 * Returns a read-only (constant) reverse iterator that points to the
359 * last pair in the %set. Iteration is done in descending order
360 * according to the keys.
363 crend() const noexcept
364 { return _M_t
.rend(); }
367 /// Returns true if the %set is empty.
369 empty() const _GLIBCXX_NOEXCEPT
370 { return _M_t
.empty(); }
372 /// Returns the size of the %set.
374 size() const _GLIBCXX_NOEXCEPT
375 { return _M_t
.size(); }
377 /// Returns the maximum size of the %set.
379 max_size() const _GLIBCXX_NOEXCEPT
380 { return _M_t
.max_size(); }
383 * @brief Swaps data with another %set.
384 * @param __x A %set of the same element and allocator types.
386 * This exchanges the elements between two sets in constant
387 * time. (It is only swapping a pointer, an integer, and an
388 * instance of the @c Compare type (which itself is often
389 * stateless and empty), so it should be quite fast.) Note
390 * that the global std::swap() function is specialized such
391 * that std::swap(s1,s2) will feed to this function.
395 { _M_t
.swap(__x
._M_t
); }
398 #if __cplusplus >= 201103L
400 * @brief Attempts to build and insert an element into the %set.
401 * @param __args Arguments used to generate an element.
402 * @return A pair, of which the first element is an iterator that points
403 * to the possibly inserted element, and the second is a bool
404 * that is true if the element was actually inserted.
406 * This function attempts to build and insert an element into the %set.
407 * A %set relies on unique keys and thus an element is only inserted if
408 * it is not already present in the %set.
410 * Insertion requires logarithmic time.
412 template<typename
... _Args
>
413 std::pair
<iterator
, bool>
414 emplace(_Args
&&... __args
)
415 { return _M_t
._M_emplace_unique(std::forward
<_Args
>(__args
)...); }
418 * @brief Attempts to insert an element into the %set.
419 * @param __pos An iterator that serves as a hint as to where the
420 * element should be inserted.
421 * @param __args Arguments used to generate the element to be
423 * @return An iterator that points to the element with key equivalent to
424 * the one generated from @a __args (may or may not be the
427 * This function is not concerned about whether the insertion took place,
428 * and thus does not return a boolean like the single-argument emplace()
429 * does. Note that the first parameter is only a hint and can
430 * potentially improve the performance of the insertion process. A bad
431 * hint would cause no gains in efficiency.
433 * For more on @a hinting, see:
434 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt07ch17.html
436 * Insertion requires logarithmic time (if the hint is not taken).
438 template<typename
... _Args
>
440 emplace_hint(const_iterator __pos
, _Args
&&... __args
)
442 return _M_t
._M_emplace_hint_unique(__pos
,
443 std::forward
<_Args
>(__args
)...);
448 * @brief Attempts to insert an element into the %set.
449 * @param __x Element to be inserted.
450 * @return A pair, of which the first element is an iterator that points
451 * to the possibly inserted element, and the second is a bool
452 * that is true if the element was actually inserted.
454 * This function attempts to insert an element into the %set. A %set
455 * relies on unique keys and thus an element is only inserted if it is
456 * not already present in the %set.
458 * Insertion requires logarithmic time.
460 std::pair
<iterator
, bool>
461 insert(const value_type
& __x
)
463 std::pair
<typename
_Rep_type::iterator
, bool> __p
=
464 _M_t
._M_insert_unique(__x
);
465 return std::pair
<iterator
, bool>(__p
.first
, __p
.second
);
468 #if __cplusplus >= 201103L
469 std::pair
<iterator
, bool>
470 insert(value_type
&& __x
)
472 std::pair
<typename
_Rep_type::iterator
, bool> __p
=
473 _M_t
._M_insert_unique(std::move(__x
));
474 return std::pair
<iterator
, bool>(__p
.first
, __p
.second
);
479 * @brief Attempts to insert an element into the %set.
480 * @param __position An iterator that serves as a hint as to where the
481 * element should be inserted.
482 * @param __x Element to be inserted.
483 * @return An iterator that points to the element with key of
484 * @a __x (may or may not be the element passed in).
486 * This function is not concerned about whether the insertion took place,
487 * and thus does not return a boolean like the single-argument insert()
488 * does. Note that the first parameter is only a hint and can
489 * potentially improve the performance of the insertion process. A bad
490 * hint would cause no gains in efficiency.
492 * For more on @a hinting, see:
493 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt07ch17.html
495 * Insertion requires logarithmic time (if the hint is not taken).
498 insert(const_iterator __position
, const value_type
& __x
)
499 { return _M_t
._M_insert_unique_(__position
, __x
); }
501 #if __cplusplus >= 201103L
503 insert(const_iterator __position
, value_type
&& __x
)
504 { return _M_t
._M_insert_unique_(__position
, std::move(__x
)); }
508 * @brief A template function that attempts to insert a range
510 * @param __first Iterator pointing to the start of the range to be
512 * @param __last Iterator pointing to the end of the range.
514 * Complexity similar to that of the range constructor.
516 template<typename _InputIterator
>
518 insert(_InputIterator __first
, _InputIterator __last
)
519 { _M_t
._M_insert_unique(__first
, __last
); }
521 #if __cplusplus >= 201103L
523 * @brief Attempts to insert a list of elements into the %set.
524 * @param __l A std::initializer_list<value_type> of elements
527 * Complexity similar to that of the range constructor.
530 insert(initializer_list
<value_type
> __l
)
531 { this->insert(__l
.begin(), __l
.end()); }
534 #if __cplusplus >= 201103L
535 // _GLIBCXX_RESOLVE_LIB_DEFECTS
536 // DR 130. Associative erase should return an iterator.
538 * @brief Erases an element from a %set.
539 * @param __position An iterator pointing to the element to be erased.
540 * @return An iterator pointing to the element immediately following
541 * @a __position prior to the element being erased. If no such
542 * element exists, end() is returned.
544 * This function erases an element, pointed to by the given iterator,
545 * from a %set. Note that this function only erases the element, and
546 * that if the element is itself a pointer, the pointed-to memory is not
547 * touched in any way. Managing the pointer is the user's
551 erase(const_iterator __position
)
552 { return _M_t
.erase(__position
); }
555 * @brief Erases an element from a %set.
556 * @param position An iterator pointing to the element to be erased.
558 * This function erases an element, pointed to by the given iterator,
559 * from a %set. Note that this function only erases the element, and
560 * that if the element is itself a pointer, the pointed-to memory is not
561 * touched in any way. Managing the pointer is the user's
565 erase(iterator __position
)
566 { _M_t
.erase(__position
); }
570 * @brief Erases elements according to the provided key.
571 * @param __x Key of element to be erased.
572 * @return The number of elements erased.
574 * This function erases all the elements located by the given key from
576 * Note that this function only erases the element, and that if
577 * the element is itself a pointer, the pointed-to memory is not touched
578 * in any way. Managing the pointer is the user's responsibility.
581 erase(const key_type
& __x
)
582 { return _M_t
.erase(__x
); }
584 #if __cplusplus >= 201103L
585 // _GLIBCXX_RESOLVE_LIB_DEFECTS
586 // DR 130. Associative erase should return an iterator.
588 * @brief Erases a [__first,__last) range of elements from a %set.
589 * @param __first Iterator pointing to the start of the range to be
592 * @param __last Iterator pointing to the end of the range to
594 * @return The iterator @a __last.
596 * This function erases a sequence of elements from a %set.
597 * Note that this function only erases the element, and that if
598 * the element is itself a pointer, the pointed-to memory is not touched
599 * in any way. Managing the pointer is the user's responsibility.
602 erase(const_iterator __first
, const_iterator __last
)
603 { return _M_t
.erase(__first
, __last
); }
606 * @brief Erases a [first,last) range of elements from a %set.
607 * @param __first Iterator pointing to the start of the range to be
609 * @param __last Iterator pointing to the end of the range to
612 * This function erases a sequence of elements from a %set.
613 * Note that this function only erases the element, and that if
614 * the element is itself a pointer, the pointed-to memory is not touched
615 * in any way. Managing the pointer is the user's responsibility.
618 erase(iterator __first
, iterator __last
)
619 { _M_t
.erase(__first
, __last
); }
623 * Erases all elements in a %set. Note that this function only erases
624 * the elements, and that if the elements themselves are pointers, the
625 * pointed-to memory is not touched in any way. Managing the pointer is
626 * the user's responsibility.
629 clear() _GLIBCXX_NOEXCEPT
635 * @brief Finds the number of elements.
636 * @param __x Element to located.
637 * @return Number of elements with specified key.
639 * This function only makes sense for multisets; for set the result will
640 * either be 0 (not present) or 1 (present).
643 count(const key_type
& __x
) const
644 { return _M_t
.find(__x
) == _M_t
.end() ? 0 : 1; }
646 // _GLIBCXX_RESOLVE_LIB_DEFECTS
647 // 214. set::find() missing const overload
650 * @brief Tries to locate an element in a %set.
651 * @param __x Element to be located.
652 * @return Iterator pointing to sought-after element, or end() if not
655 * This function takes a key and tries to locate the element with which
656 * the key matches. If successful the function returns an iterator
657 * pointing to the sought after element. If unsuccessful it returns the
658 * past-the-end ( @c end() ) iterator.
661 find(const key_type
& __x
)
662 { return _M_t
.find(__x
); }
665 find(const key_type
& __x
) const
666 { return _M_t
.find(__x
); }
671 * @brief Finds the beginning of a subsequence matching given key.
672 * @param __x Key to be located.
673 * @return Iterator pointing to first element equal to or greater
674 * than key, or end().
676 * This function returns the first element of a subsequence of elements
677 * that matches the given key. If unsuccessful it returns an iterator
678 * pointing to the first element that has a greater value than given key
679 * or end() if no such element exists.
682 lower_bound(const key_type
& __x
)
683 { return _M_t
.lower_bound(__x
); }
686 lower_bound(const key_type
& __x
) const
687 { return _M_t
.lower_bound(__x
); }
692 * @brief Finds the end of a subsequence matching given key.
693 * @param __x Key to be located.
694 * @return Iterator pointing to the first element
695 * greater than key, or end().
698 upper_bound(const key_type
& __x
)
699 { return _M_t
.upper_bound(__x
); }
702 upper_bound(const key_type
& __x
) const
703 { return _M_t
.upper_bound(__x
); }
708 * @brief Finds a subsequence matching given key.
709 * @param __x Key to be located.
710 * @return Pair of iterators that possibly points to the subsequence
711 * matching given key.
713 * This function is equivalent to
715 * std::make_pair(c.lower_bound(val),
716 * c.upper_bound(val))
718 * (but is faster than making the calls separately).
720 * This function probably only makes sense for multisets.
722 std::pair
<iterator
, iterator
>
723 equal_range(const key_type
& __x
)
724 { return _M_t
.equal_range(__x
); }
726 std::pair
<const_iterator
, const_iterator
>
727 equal_range(const key_type
& __x
) const
728 { return _M_t
.equal_range(__x
); }
731 template<typename _K1
, typename _C1
, typename _A1
>
733 operator==(const set
<_K1
, _C1
, _A1
>&, const set
<_K1
, _C1
, _A1
>&);
735 template<typename _K1
, typename _C1
, typename _A1
>
737 operator<(const set
<_K1
, _C1
, _A1
>&, const set
<_K1
, _C1
, _A1
>&);
742 * @brief Set equality comparison.
744 * @param __y A %set of the same type as @a x.
745 * @return True iff the size and elements of the sets are equal.
747 * This is an equivalence relation. It is linear in the size of the sets.
748 * Sets are considered equivalent if their sizes are equal, and if
749 * corresponding elements compare equal.
751 template<typename _Key
, typename _Compare
, typename _Alloc
>
753 operator==(const set
<_Key
, _Compare
, _Alloc
>& __x
,
754 const set
<_Key
, _Compare
, _Alloc
>& __y
)
755 { return __x
._M_t
== __y
._M_t
; }
758 * @brief Set ordering relation.
760 * @param __y A %set of the same type as @a x.
761 * @return True iff @a __x is lexicographically less than @a __y.
763 * This is a total ordering relation. It is linear in the size of the
764 * maps. The elements must be comparable with @c <.
766 * See std::lexicographical_compare() for how the determination is made.
768 template<typename _Key
, typename _Compare
, typename _Alloc
>
770 operator<(const set
<_Key
, _Compare
, _Alloc
>& __x
,
771 const set
<_Key
, _Compare
, _Alloc
>& __y
)
772 { return __x
._M_t
< __y
._M_t
; }
774 /// Returns !(x == y).
775 template<typename _Key
, typename _Compare
, typename _Alloc
>
777 operator!=(const set
<_Key
, _Compare
, _Alloc
>& __x
,
778 const set
<_Key
, _Compare
, _Alloc
>& __y
)
779 { return !(__x
== __y
); }
782 template<typename _Key
, typename _Compare
, typename _Alloc
>
784 operator>(const set
<_Key
, _Compare
, _Alloc
>& __x
,
785 const set
<_Key
, _Compare
, _Alloc
>& __y
)
786 { return __y
< __x
; }
789 template<typename _Key
, typename _Compare
, typename _Alloc
>
791 operator<=(const set
<_Key
, _Compare
, _Alloc
>& __x
,
792 const set
<_Key
, _Compare
, _Alloc
>& __y
)
793 { return !(__y
< __x
); }
796 template<typename _Key
, typename _Compare
, typename _Alloc
>
798 operator>=(const set
<_Key
, _Compare
, _Alloc
>& __x
,
799 const set
<_Key
, _Compare
, _Alloc
>& __y
)
800 { return !(__x
< __y
); }
802 /// See std::set::swap().
803 template<typename _Key
, typename _Compare
, typename _Alloc
>
805 swap(set
<_Key
, _Compare
, _Alloc
>& __x
, set
<_Key
, _Compare
, _Alloc
>& __y
)
808 _GLIBCXX_END_NAMESPACE_CONTAINER
810 #endif /* _STL_SET_H */