Install gcc-4.3.3-tdm-1-g++.tar.gz
[msysgit.git] / mingw / lib / gcc / mingw32 / 4.3.3 / include / c++ / bits / stl_set.h
blobfbd70d1eba4bbdf0e871031e1fab234a349eaf96
1 // Set implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 // Free Software Foundation, Inc.
5 //
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)
10 // any later version.
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,
20 // USA.
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.
33 * Copyright (c) 1994
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.
57 /** @file stl_set.h
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
62 #ifndef _STL_SET_H
63 #define _STL_SET_H 1
65 #include <bits/concept_check.h>
67 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
69 /**
70 * @brief A standard container made up of unique keys, which can be
71 * retrieved in logarithmic time.
73 * @ingroup Containers
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> >
92 class set
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)
101 public:
102 // typedefs:
103 //@{
104 /// Public typedefs.
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;
110 //@}
112 private:
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.
119 public:
120 //@{
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;
135 //@}
137 // allocation/deallocation
139 * @brief Default constructor creates no elements.
141 set()
142 : _M_t() { }
145 * @brief Creates a %set with no elements.
146 * @param comp Comparator to use.
147 * @param a An allocator object.
149 explicit
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)
165 : _M_t()
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())
183 : _M_t(__comp, __a)
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
191 * by @a x.
193 set(const set& __x)
194 : _M_t(__x._M_t) { }
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.
204 set(set&& __x)
205 : _M_t(std::forward<_Rep_type>(__x._M_t)) { }
206 #endif
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.
215 set&
216 operator=(const set& __x)
218 _M_t = __x._M_t;
219 return *this;
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.
230 set&
231 operator=(set&& __x)
233 // NB: DR 675.
234 this->clear();
235 this->swap(__x);
236 return *this;
238 #endif
240 // accessors:
242 /// Returns the comparison object with which the %set was constructed.
243 key_compare
244 key_comp() const
245 { return _M_t.key_comp(); }
246 /// Returns the comparison object with which the %set was constructed.
247 value_compare
248 value_comp() const
249 { return _M_t.key_comp(); }
250 /// Returns the allocator object with which the %set was constructed.
251 allocator_type
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
258 * to the keys.
260 iterator
261 begin() const
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
267 * to the keys.
269 iterator
270 end() const
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
276 * to the keys.
278 reverse_iterator
279 rbegin() const
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.
287 reverse_iterator
288 rend() const
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
295 * to the keys.
297 iterator
298 cbegin() const
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
304 * to the keys.
306 iterator
307 cend() const
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
313 * to the keys.
315 reverse_iterator
316 crbegin() const
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.
324 reverse_iterator
325 crend() const
326 { return _M_t.rend(); }
327 #endif
329 /// Returns true if the %set is empty.
330 bool
331 empty() const
332 { return _M_t.empty(); }
334 /// Returns the size of the %set.
335 size_type
336 size() const
337 { return _M_t.size(); }
339 /// Returns the maximum size of the %set.
340 size_type
341 max_size() const
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.
355 void
356 #ifdef __GXX_EXPERIMENTAL_CXX0X__
357 swap(set&& __x)
358 #else
359 swap(set& __x)
360 #endif
361 { _M_t.swap(__x._M_t); }
363 // insert/erase
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 * For more on "hinting", see:
400 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt07ch17.html
402 * Insertion requires logarithmic time (if the hint is not taken).
404 iterator
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
410 * of elements.
411 * @param first Iterator pointing to the start of the range to be
412 * inserted.
413 * @param last Iterator pointing to the end of the range.
415 * Complexity similar to that of the range constructor.
417 template<typename _InputIterator>
418 void
419 insert(_InputIterator __first, _InputIterator __last)
420 { _M_t._M_insert_unique(__first, __last); }
423 * @brief Erases an element from a %set.
424 * @param position An iterator pointing to the element to be erased.
426 * This function erases an element, pointed to by the given iterator,
427 * from a %set. Note that this function only erases the element, and
428 * that if the element is itself a pointer, the pointed-to memory is not
429 * touched in any way. Managing the pointer is the user's responsibility.
431 void
432 erase(iterator __position)
433 { _M_t.erase(__position); }
436 * @brief Erases elements according to the provided key.
437 * @param x Key of element to be erased.
438 * @return The number of elements erased.
440 * This function erases all the elements located by the given key from
441 * a %set.
442 * Note that this function only erases the element, and that if
443 * the element is itself a pointer, the pointed-to memory is not touched
444 * in any way. Managing the pointer is the user's responsibility.
446 size_type
447 erase(const key_type& __x)
448 { return _M_t.erase(__x); }
451 * @brief Erases a [first,last) range of elements from a %set.
452 * @param first Iterator pointing to the start of the range to be
453 * erased.
454 * @param last Iterator pointing to the end of the range to be erased.
456 * This function erases a sequence of elements from a %set.
457 * Note that this function only erases the element, and that if
458 * the element is itself a pointer, the pointed-to memory is not touched
459 * in any way. Managing the pointer is the user's responsibility.
461 void
462 erase(iterator __first, iterator __last)
463 { _M_t.erase(__first, __last); }
466 * Erases all elements in a %set. Note that this function only erases
467 * the elements, and that if the elements themselves are pointers, the
468 * pointed-to memory is not touched in any way. Managing the pointer is
469 * the user's responsibility.
471 void
472 clear()
473 { _M_t.clear(); }
475 // set operations:
478 * @brief Finds the number of elements.
479 * @param x Element to located.
480 * @return Number of elements with specified key.
482 * This function only makes sense for multisets; for set the result will
483 * either be 0 (not present) or 1 (present).
485 size_type
486 count(const key_type& __x) const
487 { return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
489 // _GLIBCXX_RESOLVE_LIB_DEFECTS
490 // 214. set::find() missing const overload
491 //@{
493 * @brief Tries to locate an element in a %set.
494 * @param x Element to be located.
495 * @return Iterator pointing to sought-after element, or end() if not
496 * found.
498 * This function takes a key and tries to locate the element with which
499 * the key matches. If successful the function returns an iterator
500 * pointing to the sought after element. If unsuccessful it returns the
501 * past-the-end ( @c end() ) iterator.
503 iterator
504 find(const key_type& __x)
505 { return _M_t.find(__x); }
507 const_iterator
508 find(const key_type& __x) const
509 { return _M_t.find(__x); }
510 //@}
512 //@{
514 * @brief Finds the beginning of a subsequence matching given key.
515 * @param x Key to be located.
516 * @return Iterator pointing to first element equal to or greater
517 * than key, or end().
519 * This function returns the first element of a subsequence of elements
520 * that matches the given key. If unsuccessful it returns an iterator
521 * pointing to the first element that has a greater value than given key
522 * or end() if no such element exists.
524 iterator
525 lower_bound(const key_type& __x)
526 { return _M_t.lower_bound(__x); }
528 const_iterator
529 lower_bound(const key_type& __x) const
530 { return _M_t.lower_bound(__x); }
531 //@}
533 //@{
535 * @brief Finds the end of a subsequence matching given key.
536 * @param x Key to be located.
537 * @return Iterator pointing to the first element
538 * greater than key, or end().
540 iterator
541 upper_bound(const key_type& __x)
542 { return _M_t.upper_bound(__x); }
544 const_iterator
545 upper_bound(const key_type& __x) const
546 { return _M_t.upper_bound(__x); }
547 //@}
549 //@{
551 * @brief Finds a subsequence matching given key.
552 * @param x Key to be located.
553 * @return Pair of iterators that possibly points to the subsequence
554 * matching given key.
556 * This function is equivalent to
557 * @code
558 * std::make_pair(c.lower_bound(val),
559 * c.upper_bound(val))
560 * @endcode
561 * (but is faster than making the calls separately).
563 * This function probably only makes sense for multisets.
565 std::pair<iterator, iterator>
566 equal_range(const key_type& __x)
567 { return _M_t.equal_range(__x); }
569 std::pair<const_iterator, const_iterator>
570 equal_range(const key_type& __x) const
571 { return _M_t.equal_range(__x); }
572 //@}
574 template<typename _K1, typename _C1, typename _A1>
575 friend bool
576 operator==(const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&);
578 template<typename _K1, typename _C1, typename _A1>
579 friend bool
580 operator<(const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&);
585 * @brief Set equality comparison.
586 * @param x A %set.
587 * @param y A %set of the same type as @a x.
588 * @return True iff the size and elements of the sets are equal.
590 * This is an equivalence relation. It is linear in the size of the sets.
591 * Sets are considered equivalent if their sizes are equal, and if
592 * corresponding elements compare equal.
594 template<typename _Key, typename _Compare, typename _Alloc>
595 inline bool
596 operator==(const set<_Key, _Compare, _Alloc>& __x,
597 const set<_Key, _Compare, _Alloc>& __y)
598 { return __x._M_t == __y._M_t; }
601 * @brief Set ordering relation.
602 * @param x A %set.
603 * @param y A %set of the same type as @a x.
604 * @return True iff @a x is lexicographically less than @a y.
606 * This is a total ordering relation. It is linear in the size of the
607 * maps. The elements must be comparable with @c <.
609 * See std::lexicographical_compare() for how the determination is made.
611 template<typename _Key, typename _Compare, typename _Alloc>
612 inline bool
613 operator<(const set<_Key, _Compare, _Alloc>& __x,
614 const set<_Key, _Compare, _Alloc>& __y)
615 { return __x._M_t < __y._M_t; }
617 /// Returns !(x == y).
618 template<typename _Key, typename _Compare, typename _Alloc>
619 inline bool
620 operator!=(const set<_Key, _Compare, _Alloc>& __x,
621 const set<_Key, _Compare, _Alloc>& __y)
622 { return !(__x == __y); }
624 /// Returns y < x.
625 template<typename _Key, typename _Compare, typename _Alloc>
626 inline bool
627 operator>(const set<_Key, _Compare, _Alloc>& __x,
628 const set<_Key, _Compare, _Alloc>& __y)
629 { return __y < __x; }
631 /// Returns !(y < x)
632 template<typename _Key, typename _Compare, typename _Alloc>
633 inline bool
634 operator<=(const set<_Key, _Compare, _Alloc>& __x,
635 const set<_Key, _Compare, _Alloc>& __y)
636 { return !(__y < __x); }
638 /// Returns !(x < y)
639 template<typename _Key, typename _Compare, typename _Alloc>
640 inline bool
641 operator>=(const set<_Key, _Compare, _Alloc>& __x,
642 const set<_Key, _Compare, _Alloc>& __y)
643 { return !(__x < __y); }
645 /// See std::set::swap().
646 template<typename _Key, typename _Compare, typename _Alloc>
647 inline void
648 swap(set<_Key, _Compare, _Alloc>& __x, set<_Key, _Compare, _Alloc>& __y)
649 { __x.swap(__y); }
651 #ifdef __GXX_EXPERIMENTAL_CXX0X__
652 template<typename _Key, typename _Compare, typename _Alloc>
653 inline void
654 swap(set<_Key, _Compare, _Alloc>&& __x, set<_Key, _Compare, _Alloc>& __y)
655 { __x.swap(__y); }
657 template<typename _Key, typename _Compare, typename _Alloc>
658 inline void
659 swap(set<_Key, _Compare, _Alloc>& __x, set<_Key, _Compare, _Alloc>&& __y)
660 { __x.swap(__y); }
661 #endif
663 _GLIBCXX_END_NESTED_NAMESPACE
665 #endif /* _STL_SET_H */