Create startup files from the GCC sources and drop our versions.
[dragonfly.git] / contrib / gcc-4.0 / libstdc++-v3 / include / bits / stl_set.h
1 // Set implementation -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2004, 2005 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 2, or (at your option)
9 // any later version.
10
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.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING.  If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 /*
31  *
32  * Copyright (c) 1994
33  * Hewlett-Packard Company
34  *
35  * Permission to use, copy, modify, distribute and sell this software
36  * and its documentation for any purpose is hereby granted without fee,
37  * provided that the above copyright notice appear in all copies and
38  * that both that copyright notice and this permission notice appear
39  * in supporting documentation.  Hewlett-Packard Company makes no
40  * representations about the suitability of this software for any
41  * purpose.  It is provided "as is" without express or implied warranty.
42  *
43  *
44  * Copyright (c) 1996,1997
45  * Silicon Graphics Computer Systems, Inc.
46  *
47  * Permission to use, copy, modify, distribute and sell this software
48  * and its documentation for any purpose is hereby granted without fee,
49  * provided that the above copyright notice appear in all copies and
50  * that both that copyright notice and this permission notice appear
51  * in supporting documentation.  Silicon Graphics makes no
52  * representations about the suitability of this software for any
53  * purpose.  It is provided "as is" without express or implied warranty.
54  */
55
56 /** @file stl_set.h
57  *  This is an internal header file, included by other library headers.
58  *  You should not attempt to use it directly.
59  */
60
61 #ifndef _SET_H
62 #define _SET_H 1
63
64 #include <bits/concept_check.h>
65
66 namespace _GLIBCXX_STD
67 {
68   // Forward declarations of operators < and ==, needed for friend declaration.
69   template<class _Key, class _Compare = std::less<_Key>,
70            class _Alloc = std::allocator<_Key> >
71     class set;
72
73   template<class _Key, class _Compare, class _Alloc>
74     inline bool
75     operator==(const set<_Key,_Compare,_Alloc>& __x,
76                const set<_Key,_Compare,_Alloc>& __y);
77
78   template<class _Key, class _Compare, class _Alloc>
79     inline bool
80     operator<(const set<_Key,_Compare,_Alloc>& __x,
81               const set<_Key,_Compare,_Alloc>& __y);
82
83   /**
84    *  @brief A standard container made up of unique keys, which can be
85    *  retrieved in logarithmic time.
86    *
87    *  @ingroup Containers
88    *  @ingroup Assoc_containers
89    *
90    *  Meets the requirements of a <a href="tables.html#65">container</a>, a
91    *  <a href="tables.html#66">reversible container</a>, and an
92    *  <a href="tables.html#69">associative container</a> (using unique keys).
93    *
94    *  Sets support bidirectional iterators.
95    *
96    *  @param  Key  Type of key objects.
97    *  @param  Compare  Comparison function object type, defaults to less<Key>.
98    *  @param  Alloc  Allocator type, defaults to allocator<Key>.
99    *
100    *  @if maint
101    *  The private tree data is declared exactly the same way for set and
102    *  multiset; the distinction is made entirely in how the tree functions are
103    *  called (*_unique versus *_equal, same as the standard).
104    *  @endif
105   */
106   template<class _Key, class _Compare, class _Alloc>
107     class set
108     {
109       // concept requirements
110       __glibcxx_class_requires(_Key, _SGIAssignableConcept)
111       __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
112                                 _BinaryFunctionConcept)
113
114     public:
115       // typedefs:
116       //@{
117       /// Public typedefs.
118       typedef _Key     key_type;
119       typedef _Key     value_type;
120       typedef _Compare key_compare;
121       typedef _Compare value_compare;
122       //@}
123
124     private:
125       typedef _Rb_tree<key_type, value_type,
126                        _Identity<value_type>, key_compare, _Alloc> _Rep_type;
127       _Rep_type _M_t;  // red-black tree representing set
128     public:
129       //@{
130       ///  Iterator-related typedefs.
131       typedef typename _Alloc::pointer pointer;
132       typedef typename _Alloc::const_pointer const_pointer;
133       typedef typename _Alloc::reference reference;
134       typedef typename _Alloc::const_reference const_reference;
135       // _GLIBCXX_RESOLVE_LIB_DEFECTS
136       // DR 103. set::iterator is required to be modifiable,
137       // but this allows modification of keys.
138       typedef typename _Rep_type::const_iterator iterator;
139       typedef typename _Rep_type::const_iterator const_iterator;
140       typedef typename _Rep_type::const_reverse_iterator reverse_iterator;
141       typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
142       typedef typename _Rep_type::size_type size_type;
143       typedef typename _Rep_type::difference_type difference_type;
144       typedef typename _Rep_type::allocator_type allocator_type;
145       //@}
146
147       // allocation/deallocation
148       ///  Default constructor creates no elements.
149       set()
150       : _M_t(_Compare(), allocator_type()) {}
151
152       /**
153        *  @brief  Default constructor creates no elements.
154        *
155        *  @param  comp  Comparator to use.
156        *  @param  a  Allocator to use.
157        */
158       explicit set(const _Compare& __comp,
159                    const allocator_type& __a = allocator_type())
160       : _M_t(__comp, __a) {}
161
162       /**
163        *  @brief  Builds a %set from a range.
164        *  @param  first  An input iterator.
165        *  @param  last  An input iterator.
166        *
167        *  Create a %set consisting of copies of the elements from [first,last).
168        *  This is linear in N if the range is already sorted, and NlogN
169        *  otherwise (where N is distance(first,last)).
170        */
171       template<class _InputIterator>
172         set(_InputIterator __first, _InputIterator __last)
173         : _M_t(_Compare(), allocator_type())
174         { _M_t.insert_unique(__first, __last); }
175
176       /**
177        *  @brief  Builds a %set from a range.
178        *  @param  first  An input iterator.
179        *  @param  last  An input iterator.
180        *  @param  comp  A comparison functor.
181        *  @param  a  An allocator object.
182        *
183        *  Create a %set consisting of copies of the elements from [first,last).
184        *  This is linear in N if the range is already sorted, and NlogN
185        *  otherwise (where N is distance(first,last)).
186        */
187       template<class _InputIterator>
188         set(_InputIterator __first, _InputIterator __last,
189             const _Compare& __comp,
190             const allocator_type& __a = allocator_type())
191         : _M_t(__comp, __a)
192         { _M_t.insert_unique(__first, __last); }
193
194       /**
195        *  @brief  Set copy constructor.
196        *  @param  x  A %set of identical element and allocator types.
197        *
198        *  The newly-created %set uses a copy of the allocation object used
199        *  by @a x.
200        */
201       set(const set<_Key,_Compare,_Alloc>& __x)
202       : _M_t(__x._M_t) { }
203
204       /**
205        *  @brief  Set assignment operator.
206        *  @param  x  A %set of identical element and allocator types.
207        *
208        *  All the elements of @a x are copied, but unlike the copy constructor,
209        *  the allocator object is not copied.
210        */
211       set<_Key,_Compare,_Alloc>&
212       operator=(const set<_Key, _Compare, _Alloc>& __x)
213       {
214         _M_t = __x._M_t;
215         return *this;
216       }
217
218       // accessors:
219
220       ///  Returns the comparison object with which the %set was constructed.
221       key_compare
222       key_comp() const
223       { return _M_t.key_comp(); }
224       ///  Returns the comparison object with which the %set was constructed.
225       value_compare
226       value_comp() const
227       { return _M_t.key_comp(); }
228       ///  Returns the allocator object with which the %set was constructed.
229       allocator_type
230       get_allocator() const
231       { return _M_t.get_allocator(); }
232
233       /**
234        *  Returns a read/write iterator that points to the first element in the
235        *  %set.  Iteration is done in ascending order according to the keys.
236        */
237       iterator
238       begin() const
239       { return _M_t.begin(); }
240
241       /**
242        *  Returns a read/write iterator that points one past the last element in
243        *  the %set.  Iteration is done in ascending order according to the keys.
244        */
245       iterator
246       end() const
247       { return _M_t.end(); }
248
249       /**
250        *  Returns a read/write reverse iterator that points to the last element
251        *  in the %set.  Iteration is done in descending order according to the
252        *  keys.
253        */
254       reverse_iterator
255       rbegin() const
256       { return _M_t.rbegin(); }
257
258       /**
259        *  Returns a read-only (constant) reverse iterator that points to the
260        *  last pair in the %map.  Iteration is done in descending order
261        *  according to the keys.
262        */
263       reverse_iterator
264       rend() const
265       { return _M_t.rend(); }
266
267       ///  Returns true if the %set is empty.
268       bool
269       empty() const
270       { return _M_t.empty(); }
271
272       ///  Returns the size of the %set.
273       size_type
274       size() const
275       { return _M_t.size(); }
276
277       ///  Returns the maximum size of the %set.
278       size_type
279       max_size() const
280       { return _M_t.max_size(); }
281
282       /**
283        *  @brief  Swaps data with another %set.
284        *  @param  x  A %set of the same element and allocator types.
285        *
286        *  This exchanges the elements between two sets in constant time.
287        *  (It is only swapping a pointer, an integer, and an instance of
288        *  the @c Compare type (which itself is often stateless and empty), so it
289        *  should be quite fast.)
290        *  Note that the global std::swap() function is specialized such that
291        *  std::swap(s1,s2) will feed to this function.
292        */
293       void
294       swap(set<_Key,_Compare,_Alloc>& __x)
295       { _M_t.swap(__x._M_t); }
296
297       // insert/erase
298       /**
299        *  @brief Attempts to insert an element into the %set.
300        *  @param  x  Element to be inserted.
301        *  @return  A pair, of which the first element is an iterator that points
302        *           to the possibly inserted element, and the second is a bool
303        *           that is true if the element was actually inserted.
304        *
305        *  This function attempts to insert an element into the %set.  A %set
306        *  relies on unique keys and thus an element is only inserted if it is
307        *  not already present in the %set.
308        *
309        *  Insertion requires logarithmic time.
310        */
311       std::pair<iterator,bool>
312       insert(const value_type& __x)
313       {
314         std::pair<typename _Rep_type::iterator, bool> __p =
315           _M_t.insert_unique(__x);
316         return std::pair<iterator, bool>(__p.first, __p.second);
317       }
318
319       /**
320        *  @brief Attempts to insert an element into the %set.
321        *  @param  position  An iterator that serves as a hint as to where the
322        *                    element should be inserted.
323        *  @param  x  Element to be inserted.
324        *  @return  An iterator that points to the element with key of @a x (may
325        *           or may not be the element passed in).
326        *
327        *  This function is not concerned about whether the insertion took place,
328        *  and thus does not return a boolean like the single-argument insert()
329        *  does.  Note that the first parameter is only a hint and can
330        *  potentially improve the performance of the insertion process.  A bad
331        *  hint would cause no gains in efficiency.
332        *
333        *  See http://gcc.gnu.org/onlinedocs/libstdc++/23_containers/howto.html#4
334        *  for more on "hinting".
335        *
336        *  Insertion requires logarithmic time (if the hint is not taken).
337        */
338       iterator
339       insert(iterator __position, const value_type& __x)
340       {
341         typedef typename _Rep_type::iterator _Rep_iterator;
342         return _M_t.insert_unique((_Rep_iterator&)__position, __x);
343       }
344
345       /**
346        *  @brief A template function that attemps to insert a range of elements.
347        *  @param  first  Iterator pointing to the start of the range to be
348        *                 inserted.
349        *  @param  last  Iterator pointing to the end of the range.
350        *
351        *  Complexity similar to that of the range constructor.
352        */
353       template<class _InputIterator>
354       void
355       insert(_InputIterator __first, _InputIterator __last)
356       { _M_t.insert_unique(__first, __last); }
357
358       /**
359        *  @brief Erases an element from a %set.
360        *  @param  position  An iterator pointing to the element to be erased.
361        *
362        *  This function erases an element, pointed to by the given iterator,
363        *  from a %set.  Note that this function only erases the element, and
364        *  that if the element is itself a pointer, the pointed-to memory is not
365        *  touched in any way.  Managing the pointer is the user's responsibilty.
366        */
367       void
368       erase(iterator __position)
369       {
370         typedef typename _Rep_type::iterator _Rep_iterator;
371         _M_t.erase((_Rep_iterator&)__position);
372       }
373
374       /**
375        *  @brief Erases elements according to the provided key.
376        *  @param  x  Key of element to be erased.
377        *  @return  The number of elements erased.
378        *
379        *  This function erases all the elements located by the given key from
380        *  a %set.
381        *  Note that this function only erases the element, and that if
382        *  the element is itself a pointer, the pointed-to memory is not touched
383        *  in any way.  Managing the pointer is the user's responsibilty.
384        */
385       size_type
386       erase(const key_type& __x) { return _M_t.erase(__x); }
387
388       /**
389        *  @brief Erases a [first,last) range of elements from a %set.
390        *  @param  first  Iterator pointing to the start of the range to be
391        *                 erased.
392        *  @param  last  Iterator pointing to the end of the range to be erased.
393        *
394        *  This function erases a sequence of elements from a %set.
395        *  Note that this function only erases the element, and that if
396        *  the element is itself a pointer, the pointed-to memory is not touched
397        *  in any way.  Managing the pointer is the user's responsibilty.
398        */
399       void
400       erase(iterator __first, iterator __last)
401       {
402         typedef typename _Rep_type::iterator _Rep_iterator;
403         _M_t.erase((_Rep_iterator&)__first, (_Rep_iterator&)__last);
404       }
405
406       /**
407        *  Erases all elements in a %set.  Note that this function only erases
408        *  the elements, and that if the elements themselves are pointers, the
409        *  pointed-to memory is not touched in any way.  Managing the pointer is
410        *  the user's responsibilty.
411        */
412       void
413       clear()
414       { _M_t.clear(); }
415
416       // set operations:
417
418       /**
419        *  @brief  Finds the number of elements.
420        *  @param  x  Element to located.
421        *  @return  Number of elements with specified key.
422        *
423        *  This function only makes sense for multisets; for set the result will
424        *  either be 0 (not present) or 1 (present).
425        */
426       size_type
427       count(const key_type& __x) const
428       { return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
429
430       // _GLIBCXX_RESOLVE_LIB_DEFECTS
431       // 214.  set::find() missing const overload
432       //@{
433       /**
434        *  @brief Tries to locate an element in a %set.
435        *  @param  x  Element to be located.
436        *  @return  Iterator pointing to sought-after element, or end() if not
437        *           found.
438        *
439        *  This function takes a key and tries to locate the element with which
440        *  the key matches.  If successful the function returns an iterator
441        *  pointing to the sought after element.  If unsuccessful it returns the
442        *  past-the-end ( @c end() ) iterator.
443        */
444       iterator
445       find(const key_type& __x)
446       { return _M_t.find(__x); }
447
448       const_iterator
449       find(const key_type& __x) const
450       { return _M_t.find(__x); }
451       //@}
452
453       //@{
454       /**
455        *  @brief Finds the beginning of a subsequence matching given key.
456        *  @param  x  Key to be located.
457        *  @return  Iterator pointing to first element equal to or greater
458        *           than key, or end().
459        *
460        *  This function returns the first element of a subsequence of elements
461        *  that matches the given key.  If unsuccessful it returns an iterator
462        *  pointing to the first element that has a greater value than given key
463        *  or end() if no such element exists.
464        */
465       iterator
466       lower_bound(const key_type& __x)
467       { return _M_t.lower_bound(__x); }
468
469       const_iterator
470       lower_bound(const key_type& __x) const
471       { return _M_t.lower_bound(__x); }
472       //@}
473
474       //@{
475       /**
476        *  @brief Finds the end of a subsequence matching given key.
477        *  @param  x  Key to be located.
478        *  @return Iterator pointing to the first element
479        *          greater than key, or end().
480        */
481       iterator
482       upper_bound(const key_type& __x)
483       { return _M_t.upper_bound(__x); }
484
485       const_iterator
486       upper_bound(const key_type& __x) const
487       { return _M_t.upper_bound(__x); }
488       //@}
489
490       //@{
491       /**
492        *  @brief Finds a subsequence matching given key.
493        *  @param  x  Key to be located.
494        *  @return  Pair of iterators that possibly points to the subsequence
495        *           matching given key.
496        *
497        *  This function is equivalent to
498        *  @code
499        *    std::make_pair(c.lower_bound(val),
500        *                   c.upper_bound(val))
501        *  @endcode
502        *  (but is faster than making the calls separately).
503        *
504        *  This function probably only makes sense for multisets.
505        */
506       std::pair<iterator,iterator>
507       equal_range(const key_type& __x)
508       { return _M_t.equal_range(__x); }
509
510       std::pair<const_iterator,const_iterator>
511       equal_range(const key_type& __x) const
512       { return _M_t.equal_range(__x); }
513       //@}
514
515       template<class _K1, class _C1, class _A1>
516         friend bool
517         operator== (const set<_K1,_C1,_A1>&, const set<_K1,_C1,_A1>&);
518
519       template<class _K1, class _C1, class _A1>
520         friend bool
521         operator< (const set<_K1,_C1,_A1>&, const set<_K1,_C1,_A1>&);
522     };
523
524
525   /**
526    *  @brief  Set equality comparison.
527    *  @param  x  A %set.
528    *  @param  y  A %set of the same type as @a x.
529    *  @return  True iff the size and elements of the sets are equal.
530    *
531    *  This is an equivalence relation.  It is linear in the size of the sets.
532    *  Sets are considered equivalent if their sizes are equal, and if
533    *  corresponding elements compare equal.
534   */
535   template<class _Key, class _Compare, class _Alloc>
536     inline bool
537     operator==(const set<_Key,_Compare,_Alloc>& __x,
538                const set<_Key,_Compare,_Alloc>& __y)
539     { return __x._M_t == __y._M_t; }
540
541   /**
542    *  @brief  Set ordering relation.
543    *  @param  x  A %set.
544    *  @param  y  A %set of the same type as @a x.
545    *  @return  True iff @a x is lexicographically less than @a y.
546    *
547    *  This is a total ordering relation.  It is linear in the size of the
548    *  maps.  The elements must be comparable with @c <.
549    *
550    *  See std::lexicographical_compare() for how the determination is made.
551   */
552   template<class _Key, class _Compare, class _Alloc>
553     inline bool
554     operator<(const set<_Key,_Compare,_Alloc>& __x,
555               const set<_Key,_Compare,_Alloc>& __y)
556     { return __x._M_t < __y._M_t; }
557
558   ///  Returns !(x == y).
559   template<class _Key, class _Compare, class _Alloc>
560     inline bool
561     operator!=(const set<_Key,_Compare,_Alloc>& __x,
562                const set<_Key,_Compare,_Alloc>& __y)
563     { return !(__x == __y); }
564
565   ///  Returns y < x.
566   template<class _Key, class _Compare, class _Alloc>
567     inline bool
568     operator>(const set<_Key,_Compare,_Alloc>& __x,
569               const set<_Key,_Compare,_Alloc>& __y)
570     { return __y < __x; }
571
572   ///  Returns !(y < x)
573   template<class _Key, class _Compare, class _Alloc>
574     inline bool
575     operator<=(const set<_Key,_Compare,_Alloc>& __x,
576                const set<_Key,_Compare,_Alloc>& __y)
577     { return !(__y < __x); }
578
579   ///  Returns !(x < y)
580   template<class _Key, class _Compare, class _Alloc>
581     inline bool
582     operator>=(const set<_Key,_Compare,_Alloc>& __x,
583                const set<_Key,_Compare,_Alloc>& __y)
584     { return !(__x < __y); }
585
586   /// See std::set::swap().
587   template<class _Key, class _Compare, class _Alloc>
588     inline void
589     swap(set<_Key,_Compare,_Alloc>& __x, set<_Key,_Compare,_Alloc>& __y)
590     { __x.swap(__y); }
591
592 } // namespace std
593
594 #endif /* _SET_H */