1 // Set implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 // 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 2, 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 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
34 * Hewlett-Packard Company
36 * Permission to use, copy, modify, distribute and sell this software
37 * and its documentation for any purpose is hereby granted without fee,
38 * provided that the above copyright notice appear in all copies and
39 * that both that copyright notice and this permission notice appear
40 * in supporting documentation. Hewlett-Packard Company makes no
41 * representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied warranty.
45 * Copyright (c) 1996,1997
46 * Silicon Graphics Computer Systems, Inc.
48 * Permission to use, copy, modify, distribute and sell this software
49 * and its documentation for any purpose is hereby granted without fee,
50 * provided that the above copyright notice appear in all copies and
51 * that both that copyright notice and this permission notice appear
52 * in supporting documentation. Silicon Graphics makes no
53 * representations about the suitability of this software for any
54 * purpose. It is provided "as is" without express or implied warranty.
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
65 #include <bits/concept_check.h>
67 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD_D
)
70 * @brief A standard container made up of unique keys, which can be
71 * retrieved in logarithmic time.
74 * @ingroup Assoc_containers
76 * Meets the requirements of a <a href="tables.html#65">container</a>, a
77 * <a href="tables.html#66">reversible container</a>, and an
78 * <a href="tables.html#69">associative container</a> (using unique keys).
80 * Sets support bidirectional iterators.
82 * @param Key Type of key objects.
83 * @param Compare Comparison function object type, defaults to less<Key>.
84 * @param Alloc Allocator type, defaults to allocator<Key>.
86 * The private tree data is declared exactly the same way for set and
87 * multiset; the distinction is made entirely in how the tree functions are
88 * called (*_unique versus *_equal, same as the standard).
90 template<typename _Key
, typename _Compare
= std::less
<_Key
>,
91 typename _Alloc
= std::allocator
<_Key
> >
94 // concept requirements
95 typedef typename
_Alloc::value_type _Alloc_value_type
;
96 __glibcxx_class_requires(_Key
, _SGIAssignableConcept
)
97 __glibcxx_class_requires4(_Compare
, bool, _Key
, _Key
,
98 _BinaryFunctionConcept
)
99 __glibcxx_class_requires2(_Key
, _Alloc_value_type
, _SameTypeConcept
)
105 typedef _Key key_type
;
106 typedef _Key value_type
;
107 typedef _Compare key_compare
;
108 typedef _Compare value_compare
;
109 typedef _Alloc allocator_type
;
113 typedef typename
_Alloc::template rebind
<_Key
>::other _Key_alloc_type
;
115 typedef _Rb_tree
<key_type
, value_type
, _Identity
<value_type
>,
116 key_compare
, _Key_alloc_type
> _Rep_type
;
117 _Rep_type _M_t
; // Red-black tree representing set.
121 /// Iterator-related typedefs.
122 typedef typename
_Key_alloc_type::pointer pointer
;
123 typedef typename
_Key_alloc_type::const_pointer const_pointer
;
124 typedef typename
_Key_alloc_type::reference reference
;
125 typedef typename
_Key_alloc_type::const_reference const_reference
;
126 // _GLIBCXX_RESOLVE_LIB_DEFECTS
127 // DR 103. set::iterator is required to be modifiable,
128 // but this allows modification of keys.
129 typedef typename
_Rep_type::const_iterator iterator
;
130 typedef typename
_Rep_type::const_iterator const_iterator
;
131 typedef typename
_Rep_type::const_reverse_iterator reverse_iterator
;
132 typedef typename
_Rep_type::const_reverse_iterator const_reverse_iterator
;
133 typedef typename
_Rep_type::size_type size_type
;
134 typedef typename
_Rep_type::difference_type difference_type
;
137 // allocation/deallocation
139 * @brief Default constructor creates no elements.
145 * @brief Creates a %set with no elements.
146 * @param comp Comparator to use.
147 * @param a An allocator object.
150 set(const _Compare
& __comp
,
151 const allocator_type
& __a
= allocator_type())
152 : _M_t(__comp
, __a
) { }
155 * @brief Builds a %set from a range.
156 * @param first An input iterator.
157 * @param last An input iterator.
159 * Create a %set consisting of copies of the elements from [first,last).
160 * This is linear in N if the range is already sorted, and NlogN
161 * otherwise (where N is 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 [first,last).
176 * This is linear in N if the range is already sorted, and NlogN
177 * otherwise (where N is distance(first,last)).
179 template<typename _InputIterator
>
180 set(_InputIterator __first
, _InputIterator __last
,
181 const _Compare
& __comp
,
182 const allocator_type
& __a
= allocator_type())
184 { _M_t
._M_insert_unique(__first
, __last
); }
187 * @brief %Set copy constructor.
188 * @param x A %set of identical element and allocator types.
190 * The newly-created %set uses a copy of the allocation object used
196 #ifdef __GXX_EXPERIMENTAL_CXX0X__
198 * @brief %Set move constructor
199 * @param x A %set of identical element and allocator types.
201 * The newly-created %set contains the exact contents of @a x.
202 * The contents of @a x are a valid, but unspecified %set.
205 : _M_t(std::forward
<_Rep_type
>(__x
._M_t
)) { }
209 * @brief %Set assignment operator.
210 * @param x A %set of identical element and allocator types.
212 * All the elements of @a x are copied, but unlike the copy constructor,
213 * the allocator object is not copied.
216 operator=(const set
& __x
)
222 #ifdef __GXX_EXPERIMENTAL_CXX0X__
224 * @brief %Set move assignment operator.
225 * @param x A %set of identical element and allocator types.
227 * The contents of @a x are moved into this %set (without copying).
228 * @a x is a valid, but unspecified %set.
242 /// Returns the comparison object with which the %set was constructed.
245 { return _M_t
.key_comp(); }
246 /// Returns the comparison object with which the %set was constructed.
249 { return _M_t
.key_comp(); }
250 /// Returns the allocator object with which the %set was constructed.
252 get_allocator() const
253 { return _M_t
.get_allocator(); }
256 * Returns a read-only (constant) iterator that points to the first
257 * element in the %set. Iteration is done in ascending order according
262 { return _M_t
.begin(); }
265 * Returns a read-only (constant) iterator that points one past the last
266 * element in the %set. Iteration is done in ascending order according
271 { return _M_t
.end(); }
274 * Returns a read-only (constant) iterator that points to the last
275 * element in the %set. Iteration is done in descending order according
280 { return _M_t
.rbegin(); }
283 * Returns a read-only (constant) reverse iterator that points to the
284 * last pair in the %set. Iteration is done in descending order
285 * according to the keys.
289 { return _M_t
.rend(); }
291 #ifdef __GXX_EXPERIMENTAL_CXX0X__
293 * Returns a read-only (constant) iterator that points to the first
294 * element in the %set. Iteration is done in ascending order according
299 { return _M_t
.begin(); }
302 * Returns a read-only (constant) iterator that points one past the last
303 * element in the %set. Iteration is done in ascending order according
308 { return _M_t
.end(); }
311 * Returns a read-only (constant) iterator that points to the last
312 * element in the %set. Iteration is done in descending order according
317 { return _M_t
.rbegin(); }
320 * Returns a read-only (constant) reverse iterator that points to the
321 * last pair in the %set. Iteration is done in descending order
322 * according to the keys.
326 { return _M_t
.rend(); }
329 /// Returns true if the %set is empty.
332 { return _M_t
.empty(); }
334 /// Returns the size of the %set.
337 { return _M_t
.size(); }
339 /// Returns the maximum size of the %set.
342 { return _M_t
.max_size(); }
345 * @brief Swaps data with another %set.
346 * @param x A %set of the same element and allocator types.
348 * This exchanges the elements between two sets in constant time.
349 * (It is only swapping a pointer, an integer, and an instance of
350 * the @c Compare type (which itself is often stateless and empty), so it
351 * should be quite fast.)
352 * Note that the global std::swap() function is specialized such that
353 * std::swap(s1,s2) will feed to this function.
356 #ifdef __GXX_EXPERIMENTAL_CXX0X__
361 { _M_t
.swap(__x
._M_t
); }
365 * @brief Attempts to insert an element into the %set.
366 * @param x Element to be inserted.
367 * @return A pair, of which the first element is an iterator that points
368 * to the possibly inserted element, and the second is a bool
369 * that is true if the element was actually inserted.
371 * This function attempts to insert an element into the %set. A %set
372 * relies on unique keys and thus an element is only inserted if it is
373 * not already present in the %set.
375 * Insertion requires logarithmic time.
377 std::pair
<iterator
, bool>
378 insert(const value_type
& __x
)
380 std::pair
<typename
_Rep_type::iterator
, bool> __p
=
381 _M_t
._M_insert_unique(__x
);
382 return std::pair
<iterator
, bool>(__p
.first
, __p
.second
);
386 * @brief Attempts to insert an element into the %set.
387 * @param position An iterator that serves as a hint as to where the
388 * element should be inserted.
389 * @param x Element to be inserted.
390 * @return An iterator that points to the element with key of @a x (may
391 * or may not be the element passed in).
393 * This function is not concerned about whether the insertion took place,
394 * and thus does not return a boolean like the single-argument insert()
395 * does. Note that the first parameter is only a hint and can
396 * potentially improve the performance of the insertion process. A bad
397 * hint would cause no gains in efficiency.
399 * See http://gcc.gnu.org/onlinedocs/libstdc++/23_containers/howto.html#4
400 * for more on "hinting".
402 * Insertion requires logarithmic time (if the hint is not taken).
405 insert(iterator __position
, const value_type
& __x
)
406 { return _M_t
._M_insert_unique_(__position
, __x
); }
409 * @brief A template function that attempts to insert a range of elements.
410 * @param first Iterator pointing to the start of the range to be
412 * @param last Iterator pointing to the end of the range.
414 * Complexity similar to that of the range constructor.
416 template<typename _InputIterator
>
418 insert(_InputIterator __first
, _InputIterator __last
)
419 { _M_t
._M_insert_unique(__first
, __last
); }
422 * @brief Erases an element from a %set.
423 * @param position An iterator pointing to the element to be erased.
425 * This function erases an element, pointed to by the given iterator,
426 * from a %set. Note that this function only erases the element, and
427 * that if the element is itself a pointer, the pointed-to memory is not
428 * touched in any way. Managing the pointer is the user's responsibility.
431 erase(iterator __position
)
432 { _M_t
.erase(__position
); }
435 * @brief Erases elements according to the provided key.
436 * @param x Key of element to be erased.
437 * @return The number of elements erased.
439 * This function erases all the elements located by the given key from
441 * Note that this function only erases the element, and that if
442 * the element is itself a pointer, the pointed-to memory is not touched
443 * in any way. Managing the pointer is the user's responsibility.
446 erase(const key_type
& __x
)
447 { return _M_t
.erase(__x
); }
450 * @brief Erases a [first,last) range of elements from a %set.
451 * @param first Iterator pointing to the start of the range to be
453 * @param last Iterator pointing to the end of the range to be erased.
455 * This function erases a sequence of elements from a %set.
456 * Note that this function only erases the element, and that if
457 * the element is itself a pointer, the pointed-to memory is not touched
458 * in any way. Managing the pointer is the user's responsibility.
461 erase(iterator __first
, iterator __last
)
462 { _M_t
.erase(__first
, __last
); }
465 * Erases all elements in a %set. Note that this function only erases
466 * the elements, and that if the elements themselves are pointers, the
467 * pointed-to memory is not touched in any way. Managing the pointer is
468 * the user's responsibility.
477 * @brief Finds the number of elements.
478 * @param x Element to located.
479 * @return Number of elements with specified key.
481 * This function only makes sense for multisets; for set the result will
482 * either be 0 (not present) or 1 (present).
485 count(const key_type
& __x
) const
486 { return _M_t
.find(__x
) == _M_t
.end() ? 0 : 1; }
488 // _GLIBCXX_RESOLVE_LIB_DEFECTS
489 // 214. set::find() missing const overload
492 * @brief Tries to locate an element in a %set.
493 * @param x Element to be located.
494 * @return Iterator pointing to sought-after element, or end() if not
497 * This function takes a key and tries to locate the element with which
498 * the key matches. If successful the function returns an iterator
499 * pointing to the sought after element. If unsuccessful it returns the
500 * past-the-end ( @c end() ) iterator.
503 find(const key_type
& __x
)
504 { return _M_t
.find(__x
); }
507 find(const key_type
& __x
) const
508 { return _M_t
.find(__x
); }
513 * @brief Finds the beginning of a subsequence matching given key.
514 * @param x Key to be located.
515 * @return Iterator pointing to first element equal to or greater
516 * than key, or end().
518 * This function returns the first element of a subsequence of elements
519 * that matches the given key. If unsuccessful it returns an iterator
520 * pointing to the first element that has a greater value than given key
521 * or end() if no such element exists.
524 lower_bound(const key_type
& __x
)
525 { return _M_t
.lower_bound(__x
); }
528 lower_bound(const key_type
& __x
) const
529 { return _M_t
.lower_bound(__x
); }
534 * @brief Finds the end of a subsequence matching given key.
535 * @param x Key to be located.
536 * @return Iterator pointing to the first element
537 * greater than key, or end().
540 upper_bound(const key_type
& __x
)
541 { return _M_t
.upper_bound(__x
); }
544 upper_bound(const key_type
& __x
) const
545 { return _M_t
.upper_bound(__x
); }
550 * @brief Finds a subsequence matching given key.
551 * @param x Key to be located.
552 * @return Pair of iterators that possibly points to the subsequence
553 * matching given key.
555 * This function is equivalent to
557 * std::make_pair(c.lower_bound(val),
558 * c.upper_bound(val))
560 * (but is faster than making the calls separately).
562 * This function probably only makes sense for multisets.
564 std::pair
<iterator
, iterator
>
565 equal_range(const key_type
& __x
)
566 { return _M_t
.equal_range(__x
); }
568 std::pair
<const_iterator
, const_iterator
>
569 equal_range(const key_type
& __x
) const
570 { return _M_t
.equal_range(__x
); }
573 template<typename _K1
, typename _C1
, typename _A1
>
575 operator==(const set
<_K1
, _C1
, _A1
>&, const set
<_K1
, _C1
, _A1
>&);
577 template<typename _K1
, typename _C1
, typename _A1
>
579 operator<(const set
<_K1
, _C1
, _A1
>&, const set
<_K1
, _C1
, _A1
>&);
584 * @brief Set equality comparison.
586 * @param y A %set of the same type as @a x.
587 * @return True iff the size and elements of the sets are equal.
589 * This is an equivalence relation. It is linear in the size of the sets.
590 * Sets are considered equivalent if their sizes are equal, and if
591 * corresponding elements compare equal.
593 template<typename _Key
, typename _Compare
, typename _Alloc
>
595 operator==(const set
<_Key
, _Compare
, _Alloc
>& __x
,
596 const set
<_Key
, _Compare
, _Alloc
>& __y
)
597 { return __x
._M_t
== __y
._M_t
; }
600 * @brief Set ordering relation.
602 * @param y A %set of the same type as @a x.
603 * @return True iff @a x is lexicographically less than @a y.
605 * This is a total ordering relation. It is linear in the size of the
606 * maps. The elements must be comparable with @c <.
608 * See std::lexicographical_compare() for how the determination is made.
610 template<typename _Key
, typename _Compare
, typename _Alloc
>
612 operator<(const set
<_Key
, _Compare
, _Alloc
>& __x
,
613 const set
<_Key
, _Compare
, _Alloc
>& __y
)
614 { return __x
._M_t
< __y
._M_t
; }
616 /// Returns !(x == y).
617 template<typename _Key
, typename _Compare
, typename _Alloc
>
619 operator!=(const set
<_Key
, _Compare
, _Alloc
>& __x
,
620 const set
<_Key
, _Compare
, _Alloc
>& __y
)
621 { return !(__x
== __y
); }
624 template<typename _Key
, typename _Compare
, typename _Alloc
>
626 operator>(const set
<_Key
, _Compare
, _Alloc
>& __x
,
627 const set
<_Key
, _Compare
, _Alloc
>& __y
)
628 { return __y
< __x
; }
631 template<typename _Key
, typename _Compare
, typename _Alloc
>
633 operator<=(const set
<_Key
, _Compare
, _Alloc
>& __x
,
634 const set
<_Key
, _Compare
, _Alloc
>& __y
)
635 { return !(__y
< __x
); }
638 template<typename _Key
, typename _Compare
, typename _Alloc
>
640 operator>=(const set
<_Key
, _Compare
, _Alloc
>& __x
,
641 const set
<_Key
, _Compare
, _Alloc
>& __y
)
642 { return !(__x
< __y
); }
644 /// See std::set::swap().
645 template<typename _Key
, typename _Compare
, typename _Alloc
>
647 swap(set
<_Key
, _Compare
, _Alloc
>& __x
, set
<_Key
, _Compare
, _Alloc
>& __y
)
650 #ifdef __GXX_EXPERIMENTAL_CXX0X__
651 template<typename _Key
, typename _Compare
, typename _Alloc
>
653 swap(set
<_Key
, _Compare
, _Alloc
>&& __x
, set
<_Key
, _Compare
, _Alloc
>& __y
)
656 template<typename _Key
, typename _Compare
, typename _Alloc
>
658 swap(set
<_Key
, _Compare
, _Alloc
>& __x
, set
<_Key
, _Compare
, _Alloc
>&& __y
)
662 _GLIBCXX_END_NESTED_NAMESPACE
664 #endif /* _STL_SET_H */