1 // Set implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
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>.
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).
92 template<class _Key
, class _Compare
= std::less
<_Key
>,
93 class _Alloc
= std::allocator
<_Key
> >
96 // concept requirements
97 typedef typename
_Alloc::value_type _Alloc_value_type
;
98 __glibcxx_class_requires(_Key
, _SGIAssignableConcept
)
99 __glibcxx_class_requires4(_Compare
, bool, _Key
, _Key
,
100 _BinaryFunctionConcept
)
101 __glibcxx_class_requires2(_Key
, _Alloc_value_type
, _SameTypeConcept
)
107 typedef _Key key_type
;
108 typedef _Key value_type
;
109 typedef _Compare key_compare
;
110 typedef _Compare value_compare
;
111 typedef _Alloc allocator_type
;
115 typedef typename
_Alloc::template 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
123 /// Iterator-related typedefs.
124 typedef typename
_Key_alloc_type::pointer pointer
;
125 typedef typename
_Key_alloc_type::const_pointer const_pointer
;
126 typedef typename
_Key_alloc_type::reference reference
;
127 typedef typename
_Key_alloc_type::const_reference const_reference
;
128 // _GLIBCXX_RESOLVE_LIB_DEFECTS
129 // DR 103. set::iterator is required to be modifiable,
130 // but this allows modification of keys.
131 typedef typename
_Rep_type::const_iterator iterator
;
132 typedef typename
_Rep_type::const_iterator const_iterator
;
133 typedef typename
_Rep_type::const_reverse_iterator reverse_iterator
;
134 typedef typename
_Rep_type::const_reverse_iterator const_reverse_iterator
;
135 typedef typename
_Rep_type::size_type size_type
;
136 typedef typename
_Rep_type::difference_type difference_type
;
139 // allocation/deallocation
140 /// Default constructor creates no elements.
142 : _M_t(_Compare(), allocator_type()) {}
145 * @brief Default constructor creates no elements.
147 * @param comp Comparator to use.
148 * @param a Allocator to use.
151 set(const _Compare
& __comp
,
152 const allocator_type
& __a
= allocator_type())
153 : _M_t(__comp
, __a
) {}
156 * @brief Builds a %set from a range.
157 * @param first An input iterator.
158 * @param last An input iterator.
160 * Create a %set consisting of copies of the elements from [first,last).
161 * This is linear in N if the range is already sorted, and NlogN
162 * otherwise (where N is distance(first,last)).
164 template<class _InputIterator
>
165 set(_InputIterator __first
, _InputIterator __last
)
166 : _M_t(_Compare(), allocator_type())
167 { _M_t
._M_insert_unique(__first
, __last
); }
170 * @brief Builds a %set from a range.
171 * @param first An input iterator.
172 * @param last An input iterator.
173 * @param comp A comparison functor.
174 * @param a An allocator object.
176 * Create a %set consisting of copies of the elements from [first,last).
177 * This is linear in N if the range is already sorted, and NlogN
178 * otherwise (where N is distance(first,last)).
180 template<class _InputIterator
>
181 set(_InputIterator __first
, _InputIterator __last
,
182 const _Compare
& __comp
,
183 const allocator_type
& __a
= allocator_type())
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
194 set(const set
<_Key
,_Compare
,_Alloc
>& __x
)
198 * @brief Set assignment operator.
199 * @param x A %set of identical element and allocator types.
201 * All the elements of @a x are copied, but unlike the copy constructor,
202 * the allocator object is not copied.
204 set
<_Key
,_Compare
,_Alloc
>&
205 operator=(const set
<_Key
, _Compare
, _Alloc
>& __x
)
213 /// Returns the comparison object with which the %set was constructed.
216 { return _M_t
.key_comp(); }
217 /// Returns the comparison object with which the %set was constructed.
220 { return _M_t
.key_comp(); }
221 /// Returns the allocator object with which the %set was constructed.
223 get_allocator() const
224 { return _M_t
.get_allocator(); }
227 * Returns a read/write iterator that points to the first element in the
228 * %set. Iteration is done in ascending order according to the keys.
232 { return _M_t
.begin(); }
235 * Returns a read/write iterator that points one past the last element in
236 * the %set. Iteration is done in ascending order according to the keys.
240 { return _M_t
.end(); }
243 * Returns a read/write reverse iterator that points to the last element
244 * in the %set. Iteration is done in descending order according to the
249 { return _M_t
.rbegin(); }
252 * Returns a read-only (constant) reverse iterator that points to the
253 * last pair in the %map. Iteration is done in descending order
254 * according to the keys.
258 { return _M_t
.rend(); }
260 /// Returns true if the %set is empty.
263 { return _M_t
.empty(); }
265 /// Returns the size of the %set.
268 { return _M_t
.size(); }
270 /// Returns the maximum size of the %set.
273 { return _M_t
.max_size(); }
276 * @brief Swaps data with another %set.
277 * @param x A %set of the same element and allocator types.
279 * This exchanges the elements between two sets in constant time.
280 * (It is only swapping a pointer, an integer, and an instance of
281 * the @c Compare type (which itself is often stateless and empty), so it
282 * should be quite fast.)
283 * Note that the global std::swap() function is specialized such that
284 * std::swap(s1,s2) will feed to this function.
287 swap(set
<_Key
,_Compare
,_Alloc
>& __x
)
288 { _M_t
.swap(__x
._M_t
); }
292 * @brief Attempts to insert an element into the %set.
293 * @param x Element to be inserted.
294 * @return A pair, of which the first element is an iterator that points
295 * to the possibly inserted element, and the second is a bool
296 * that is true if the element was actually inserted.
298 * This function attempts to insert an element into the %set. A %set
299 * relies on unique keys and thus an element is only inserted if it is
300 * not already present in the %set.
302 * Insertion requires logarithmic time.
304 std::pair
<iterator
,bool>
305 insert(const value_type
& __x
)
307 std::pair
<typename
_Rep_type::iterator
, bool> __p
=
308 _M_t
._M_insert_unique(__x
);
309 return std::pair
<iterator
, bool>(__p
.first
, __p
.second
);
313 * @brief Attempts to insert an element into the %set.
314 * @param position An iterator that serves as a hint as to where the
315 * element should be inserted.
316 * @param x Element to be inserted.
317 * @return An iterator that points to the element with key of @a x (may
318 * or may not be the element passed in).
320 * This function is not concerned about whether the insertion took place,
321 * and thus does not return a boolean like the single-argument insert()
322 * does. Note that the first parameter is only a hint and can
323 * potentially improve the performance of the insertion process. A bad
324 * hint would cause no gains in efficiency.
326 * See http://gcc.gnu.org/onlinedocs/libstdc++/23_containers/howto.html#4
327 * for more on "hinting".
329 * Insertion requires logarithmic time (if the hint is not taken).
332 insert(iterator __position
, const value_type
& __x
)
333 { return _M_t
._M_insert_unique_(__position
, __x
); }
336 * @brief A template function that attemps to insert a range of elements.
337 * @param first Iterator pointing to the start of the range to be
339 * @param last Iterator pointing to the end of the range.
341 * Complexity similar to that of the range constructor.
343 template<class _InputIterator
>
345 insert(_InputIterator __first
, _InputIterator __last
)
346 { _M_t
._M_insert_unique(__first
, __last
); }
349 * @brief Erases an element from a %set.
350 * @param position An iterator pointing to the element to be erased.
352 * This function erases an element, pointed to by the given iterator,
353 * from a %set. Note that this function only erases the element, and
354 * that if the element is itself a pointer, the pointed-to memory is not
355 * touched in any way. Managing the pointer is the user's responsibilty.
358 erase(iterator __position
)
359 { _M_t
.erase(__position
); }
362 * @brief Erases elements according to the provided key.
363 * @param x Key of element to be erased.
364 * @return The number of elements erased.
366 * This function erases all the elements located by the given key from
368 * Note that this function only erases the element, and that if
369 * the element is itself a pointer, the pointed-to memory is not touched
370 * in any way. Managing the pointer is the user's responsibilty.
373 erase(const key_type
& __x
)
374 { return _M_t
.erase(__x
); }
377 * @brief Erases a [first,last) range of elements from a %set.
378 * @param first Iterator pointing to the start of the range to be
380 * @param last Iterator pointing to the end of the range to be erased.
382 * This function erases a sequence of elements from a %set.
383 * Note that this function only erases the element, and that if
384 * the element is itself a pointer, the pointed-to memory is not touched
385 * in any way. Managing the pointer is the user's responsibilty.
388 erase(iterator __first
, iterator __last
)
389 { _M_t
.erase(__first
, __last
); }
392 * Erases all elements in a %set. Note that this function only erases
393 * the elements, and that if the elements themselves are pointers, the
394 * pointed-to memory is not touched in any way. Managing the pointer is
395 * the user's responsibilty.
404 * @brief Finds the number of elements.
405 * @param x Element to located.
406 * @return Number of elements with specified key.
408 * This function only makes sense for multisets; for set the result will
409 * either be 0 (not present) or 1 (present).
412 count(const key_type
& __x
) const
413 { return _M_t
.find(__x
) == _M_t
.end() ? 0 : 1; }
415 // _GLIBCXX_RESOLVE_LIB_DEFECTS
416 // 214. set::find() missing const overload
419 * @brief Tries to locate an element in a %set.
420 * @param x Element to be located.
421 * @return Iterator pointing to sought-after element, or end() if not
424 * This function takes a key and tries to locate the element with which
425 * the key matches. If successful the function returns an iterator
426 * pointing to the sought after element. If unsuccessful it returns the
427 * past-the-end ( @c end() ) iterator.
430 find(const key_type
& __x
)
431 { return _M_t
.find(__x
); }
434 find(const key_type
& __x
) const
435 { return _M_t
.find(__x
); }
440 * @brief Finds the beginning of a subsequence matching given key.
441 * @param x Key to be located.
442 * @return Iterator pointing to first element equal to or greater
443 * than key, or end().
445 * This function returns the first element of a subsequence of elements
446 * that matches the given key. If unsuccessful it returns an iterator
447 * pointing to the first element that has a greater value than given key
448 * or end() if no such element exists.
451 lower_bound(const key_type
& __x
)
452 { return _M_t
.lower_bound(__x
); }
455 lower_bound(const key_type
& __x
) const
456 { return _M_t
.lower_bound(__x
); }
461 * @brief Finds the end of a subsequence matching given key.
462 * @param x Key to be located.
463 * @return Iterator pointing to the first element
464 * greater than key, or end().
467 upper_bound(const key_type
& __x
)
468 { return _M_t
.upper_bound(__x
); }
471 upper_bound(const key_type
& __x
) const
472 { return _M_t
.upper_bound(__x
); }
477 * @brief Finds a subsequence matching given key.
478 * @param x Key to be located.
479 * @return Pair of iterators that possibly points to the subsequence
480 * matching given key.
482 * This function is equivalent to
484 * std::make_pair(c.lower_bound(val),
485 * c.upper_bound(val))
487 * (but is faster than making the calls separately).
489 * This function probably only makes sense for multisets.
491 std::pair
<iterator
, iterator
>
492 equal_range(const key_type
& __x
)
493 { return _M_t
.equal_range(__x
); }
495 std::pair
<const_iterator
, const_iterator
>
496 equal_range(const key_type
& __x
) const
497 { return _M_t
.equal_range(__x
); }
500 template<class _K1
, class _C1
, class _A1
>
502 operator== (const set
<_K1
, _C1
, _A1
>&, const set
<_K1
, _C1
, _A1
>&);
504 template<class _K1
, class _C1
, class _A1
>
506 operator< (const set
<_K1
, _C1
, _A1
>&, const set
<_K1
, _C1
, _A1
>&);
511 * @brief Set equality comparison.
513 * @param y A %set of the same type as @a x.
514 * @return True iff the size and elements of the sets are equal.
516 * This is an equivalence relation. It is linear in the size of the sets.
517 * Sets are considered equivalent if their sizes are equal, and if
518 * corresponding elements compare equal.
520 template<class _Key
, class _Compare
, class _Alloc
>
522 operator==(const set
<_Key
, _Compare
, _Alloc
>& __x
,
523 const set
<_Key
, _Compare
, _Alloc
>& __y
)
524 { return __x
._M_t
== __y
._M_t
; }
527 * @brief Set ordering relation.
529 * @param y A %set of the same type as @a x.
530 * @return True iff @a x is lexicographically less than @a y.
532 * This is a total ordering relation. It is linear in the size of the
533 * maps. The elements must be comparable with @c <.
535 * See std::lexicographical_compare() for how the determination is made.
537 template<class _Key
, class _Compare
, class _Alloc
>
539 operator<(const set
<_Key
, _Compare
, _Alloc
>& __x
,
540 const set
<_Key
, _Compare
, _Alloc
>& __y
)
541 { return __x
._M_t
< __y
._M_t
; }
543 /// Returns !(x == y).
544 template<class _Key
, class _Compare
, class _Alloc
>
546 operator!=(const set
<_Key
, _Compare
, _Alloc
>& __x
,
547 const set
<_Key
, _Compare
, _Alloc
>& __y
)
548 { return !(__x
== __y
); }
551 template<class _Key
, class _Compare
, class _Alloc
>
553 operator>(const set
<_Key
, _Compare
, _Alloc
>& __x
,
554 const set
<_Key
, _Compare
, _Alloc
>& __y
)
555 { return __y
< __x
; }
558 template<class _Key
, class _Compare
, class _Alloc
>
560 operator<=(const set
<_Key
, _Compare
, _Alloc
>& __x
,
561 const set
<_Key
, _Compare
, _Alloc
>& __y
)
562 { return !(__y
< __x
); }
565 template<class _Key
, class _Compare
, class _Alloc
>
567 operator>=(const set
<_Key
, _Compare
, _Alloc
>& __x
,
568 const set
<_Key
, _Compare
, _Alloc
>& __y
)
569 { return !(__x
< __y
); }
571 /// See std::set::swap().
572 template<class _Key
, class _Compare
, class _Alloc
>
574 swap(set
<_Key
, _Compare
, _Alloc
>& __x
, set
<_Key
, _Compare
, _Alloc
>& __y
)
577 _GLIBCXX_END_NESTED_NAMESPACE
579 #endif /* _STL_SET_H */