OSDN Git Service

2005-08-17 Kelley Cook <kcook@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / stl_multiset.h
1 // Multiset 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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
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_multiset.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 _MULTISET_H
62 #define _MULTISET_H 1
63
64 #include <bits/concept_check.h>
65
66 namespace _GLIBCXX_STD
67 {
68
69   // Forward declaration of operators < and ==, needed for friend declaration.
70   template <class _Key, class _Compare = std::less<_Key>,
71             class _Alloc = std::allocator<_Key> >
72     class multiset;
73
74   template <class _Key, class _Compare, class _Alloc>
75     inline bool
76     operator==(const multiset<_Key, _Compare, _Alloc>& __x,
77                const multiset<_Key, _Compare, _Alloc>& __y);
78
79   template <class _Key, class _Compare, class _Alloc>
80     inline bool
81     operator<(const multiset<_Key, _Compare, _Alloc>& __x,
82               const multiset<_Key, _Compare, _Alloc>& __y);
83
84   /**
85    *  @brief A standard container made up of elements, which can be retrieved
86    *  in logarithmic time.
87    *
88    *  @ingroup Containers
89    *  @ingroup Assoc_containers
90    *
91    *  Meets the requirements of a <a href="tables.html#65">container</a>, a
92    *  <a href="tables.html#66">reversible container</a>, and an
93    *  <a href="tables.html#69">associative container</a> (using equivalent
94    *  keys).  For a @c multiset<Key> the key_type and value_type are Key.
95    *
96    *  Multisets support bidirectional iterators.
97    *
98    *  @if maint
99    *  The private tree data is declared exactly the same way for set and
100    *  multiset; the distinction is made entirely in how the tree functions are
101    *  called (*_unique versus *_equal, same as the standard).
102    *  @endif
103   */
104   template <class _Key, class _Compare, class _Alloc>
105     class multiset
106     {
107       // concept requirements
108       typedef typename _Alloc::value_type                   _Alloc_value_type;
109       __glibcxx_class_requires(_Key, _SGIAssignableConcept)
110       __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
111                                 _BinaryFunctionConcept)
112       __glibcxx_class_requires2(_Key, _Alloc_value_type, _SameTypeConcept)      
113
114     public:
115       // typedefs:
116       typedef _Key     key_type;
117       typedef _Key     value_type;
118       typedef _Compare key_compare;
119       typedef _Compare value_compare;
120       typedef _Alloc   allocator_type;
121
122     private:
123       /// @if maint  This turns a red-black tree into a [multi]set.  @endif
124       typedef typename _Alloc::template rebind<_Key>::other _Key_alloc_type;
125
126       typedef _Rb_tree<key_type, value_type, _Identity<value_type>,
127                        key_compare, _Key_alloc_type> _Rep_type;
128       /// @if maint  The actual tree structure.  @endif
129       _Rep_type _M_t;
130
131     public:
132       typedef typename _Key_alloc_type::pointer             pointer;
133       typedef typename _Key_alloc_type::const_pointer       const_pointer;
134       typedef typename _Key_alloc_type::reference           reference;
135       typedef typename _Key_alloc_type::const_reference     const_reference;
136       // _GLIBCXX_RESOLVE_LIB_DEFECTS
137       // DR 103. set::iterator is required to be modifiable,
138       // but this allows modification of keys.
139       typedef typename _Rep_type::const_iterator            iterator;
140       typedef typename _Rep_type::const_iterator            const_iterator;
141       typedef typename _Rep_type::const_reverse_iterator    reverse_iterator;
142       typedef typename _Rep_type::const_reverse_iterator    const_reverse_iterator;
143       typedef typename _Rep_type::size_type                 size_type;
144       typedef typename _Rep_type::difference_type           difference_type;
145
146       // allocation/deallocation
147
148       /**
149        *  @brief  Default constructor creates no elements.
150        */
151       multiset()
152       : _M_t(_Compare(), allocator_type()) { }
153
154       explicit
155       multiset(const _Compare& __comp,
156                const allocator_type& __a = allocator_type())
157       : _M_t(__comp, __a) { }
158
159       /**
160        *  @brief  Builds a %multiset from a range.
161        *  @param  first  An input iterator.
162        *  @param  last  An input iterator.
163        *
164        *  Create a %multiset consisting of copies of the elements from
165        *  [first,last).  This is linear in N if the range is already sorted,
166        *  and NlogN otherwise (where N is distance(first,last)).
167        */
168       template <class _InputIterator>
169         multiset(_InputIterator __first, _InputIterator __last)
170         : _M_t(_Compare(), allocator_type())
171         { _M_t.insert_equal(__first, __last); }
172
173       /**
174        *  @brief  Builds a %multiset from a range.
175        *  @param  first  An input iterator.
176        *  @param  last  An input iterator.
177        *  @param  comp  A comparison functor.
178        *  @param  a  An allocator object.
179        *
180        *  Create a %multiset consisting of copies of the elements from
181        *  [first,last).  This is linear in N if the range is already sorted,
182        *  and NlogN otherwise (where N is distance(first,last)).
183        */
184       template <class _InputIterator>
185         multiset(_InputIterator __first, _InputIterator __last,
186                  const _Compare& __comp,
187                  const allocator_type& __a = allocator_type())
188         : _M_t(__comp, __a)
189         { _M_t.insert_equal(__first, __last); }
190
191       /**
192        *  @brief  %Multiset copy constructor.
193        *  @param  x  A %multiset of identical element and allocator types.
194        *
195        *  The newly-created %multiset uses a copy of the allocation object used
196        *  by @a x.
197        */
198       multiset(const multiset<_Key,_Compare,_Alloc>& __x)
199       : _M_t(__x._M_t) { }
200
201       /**
202        *  @brief  %Multiset assignment operator.
203        *  @param  x  A %multiset of identical element and allocator types.
204        *
205        *  All the elements of @a x are copied, but unlike the copy constructor,
206        *  the allocator object is not copied.
207        */
208       multiset<_Key,_Compare,_Alloc>&
209       operator=(const multiset<_Key,_Compare,_Alloc>& __x)
210       {
211         _M_t = __x._M_t;
212         return *this;
213       }
214
215       // accessors:
216
217       ///  Returns the comparison object.
218       key_compare
219       key_comp() const
220       { return _M_t.key_comp(); }
221       ///  Returns the comparison object.
222       value_compare
223       value_comp() const
224       { return _M_t.key_comp(); }
225       ///  Returns the memory allocation object.
226       allocator_type
227       get_allocator() const
228       { return _M_t.get_allocator(); }
229
230       /**
231        *  Returns a read/write iterator that points to the first element in the
232        *  %multiset.  Iteration is done in ascending order according to the
233        *  keys.
234        */
235       iterator
236       begin() const
237       { return _M_t.begin(); }
238
239       /**
240        *  Returns a read/write iterator that points one past the last element in
241        *  the %multiset.  Iteration is done in ascending order according to the
242        *  keys.
243        */
244       iterator
245       end() const
246       { return _M_t.end(); }
247
248       /**
249        *  Returns a read/write reverse iterator that points to the last element
250        *  in the %multiset.  Iteration is done in descending order according to
251        *  the keys.
252        */
253       reverse_iterator
254       rbegin() const
255       { return _M_t.rbegin(); }
256
257       /**
258        *  Returns a read/write reverse iterator that points to the last element
259        *  in the %multiset.  Iteration is done in descending order according to
260        *  the keys.
261        */
262       reverse_iterator
263       rend() const
264       { return _M_t.rend(); }
265
266       ///  Returns true if the %set is empty.
267       bool
268       empty() const
269       { return _M_t.empty(); }
270
271       ///  Returns the size of the %set.
272       size_type
273       size() const
274       { return _M_t.size(); }
275
276       ///  Returns the maximum size of the %set.
277       size_type
278       max_size() const
279       { return _M_t.max_size(); }
280
281       /**
282        *  @brief  Swaps data with another %multiset.
283        *  @param  x  A %multiset of the same element and allocator types.
284        *
285        *  This exchanges the elements between two multisets in constant time.
286        *  (It is only swapping a pointer, an integer, and an instance of the @c
287        *  Compare type (which itself is often stateless and empty), so it should
288        *  be quite fast.)
289        *  Note that the global std::swap() function is specialized such that
290        *  std::swap(s1,s2) will feed to this function.
291        */
292       void
293       swap(multiset<_Key, _Compare, _Alloc>& __x)
294       { _M_t.swap(__x._M_t); }
295
296       // insert/erase
297       /**
298        *  @brief Inserts an element into the %multiset.
299        *  @param  x  Element to be inserted.
300        *  @return An iterator that points to the inserted element.
301        *
302        *  This function inserts an element into the %multiset.  Contrary
303        *  to a std::set the %multiset does not rely on unique keys and thus
304        *  multiple copies of the same element can be inserted.
305        *
306        *  Insertion requires logarithmic time.
307        */
308       iterator
309       insert(const value_type& __x)
310       { return _M_t.insert_equal(__x); }
311
312       /**
313        *  @brief Inserts an element into the %multiset.
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 inserted element.
318        *
319        *  This function inserts an element into the %multiset.  Contrary
320        *  to a std::set the %multiset does not rely on unique keys and thus
321        *  multiple copies of the same element can be inserted.
322        *
323        *  Note that the first parameter is only a hint and can potentially
324        *  improve the performance of the insertion process.  A bad hint would
325        *  cause no gains in efficiency.
326        *
327        *  See http://gcc.gnu.org/onlinedocs/libstdc++/23_containers/howto.html#4
328        *  for more on "hinting".
329        *
330        *  Insertion requires logarithmic time (if the hint is not taken).
331        */
332       iterator
333       insert(iterator __position, const value_type& __x)
334       {
335         typedef typename _Rep_type::iterator _Rep_iterator;
336         return _M_t.insert_equal((_Rep_iterator&)__position, __x);
337       }
338
339       /**
340        *  @brief A template function that attemps to insert a range of elements.
341        *  @param  first  Iterator pointing to the start of the range to be
342        *                 inserted.
343        *  @param  last  Iterator pointing to the end of the range.
344        *
345        *  Complexity similar to that of the range constructor.
346        */
347       template <class _InputIterator>
348         void
349         insert(_InputIterator __first, _InputIterator __last)
350         { _M_t.insert_equal(__first, __last); }
351
352       /**
353        *  @brief Erases an element from a %multiset.
354        *  @param  position  An iterator pointing to the element to be erased.
355        *
356        *  This function erases an element, pointed to by the given iterator,
357        *  from a %multiset.  Note that this function only erases the element,
358        *  and that if the element is itself a pointer, the pointed-to memory is
359        *  not touched in any way.  Managing the pointer is the user's
360        *  responsibilty.
361        */
362       void
363       erase(iterator __position)
364       {
365         typedef typename _Rep_type::iterator _Rep_iterator;
366         _M_t.erase((_Rep_iterator&)__position);
367       }
368
369       /**
370        *  @brief Erases elements according to the provided key.
371        *  @param  x  Key of element to be erased.
372        *  @return  The number of elements erased.
373        *
374        *  This function erases all elements located by the given key from a
375        *  %multiset.
376        *  Note that this function only erases the element, and that if
377        *  the element is itself a pointer, the pointed-to memory is not touched
378        *  in any way.  Managing the pointer is the user's responsibilty.
379        */
380       size_type
381       erase(const key_type& __x)
382       { return _M_t.erase(__x); }
383
384       /**
385        *  @brief Erases a [first,last) range of elements from a %multiset.
386        *  @param  first  Iterator pointing to the start of the range to be
387        *                 erased.
388        *  @param  last  Iterator pointing to the end of the range to be erased.
389        *
390        *  This function erases a sequence of elements from a %multiset.
391        *  Note that this function only erases the elements, and that if
392        *  the elements themselves are pointers, the pointed-to memory is not
393        *  touched in any way.  Managing the pointer is the user's responsibilty.
394        */
395       void
396       erase(iterator __first, iterator __last)
397       {
398         typedef typename _Rep_type::iterator _Rep_iterator;
399         _M_t.erase((_Rep_iterator&)__first, (_Rep_iterator&)__last);
400       }
401
402       /**
403        *  Erases all elements in a %multiset.  Note that this function only
404        *  erases the elements, and that if the elements themselves are pointers,
405        *  the pointed-to memory is not touched in any way.  Managing the pointer
406        *  is the user's responsibilty.
407        */
408       void
409       clear()
410       { _M_t.clear(); }
411
412       // multiset operations:
413
414       /**
415        *  @brief Finds the number of elements with given key.
416        *  @param  x  Key of elements to be located.
417        *  @return Number of elements with specified key.
418        */
419       size_type
420       count(const key_type& __x) const
421       { return _M_t.count(__x); }
422
423       // _GLIBCXX_RESOLVE_LIB_DEFECTS
424       // 214.  set::find() missing const overload
425       //@{
426       /**
427        *  @brief Tries to locate an element in a %set.
428        *  @param  x  Element to be located.
429        *  @return  Iterator pointing to sought-after element, or end() if not
430        *           found.
431        *
432        *  This function takes a key and tries to locate the element with which
433        *  the key matches.  If successful the function returns an iterator
434        *  pointing to the sought after element.  If unsuccessful it returns the
435        *  past-the-end ( @c end() ) iterator.
436        */
437       iterator
438       find(const key_type& __x)
439       { return _M_t.find(__x); }
440
441       const_iterator
442       find(const key_type& __x) const
443       { return _M_t.find(__x); }
444       //@}
445
446       //@{
447       /**
448        *  @brief Finds the beginning of a subsequence matching given key.
449        *  @param  x  Key to be located.
450        *  @return  Iterator pointing to first element equal to or greater
451        *           than key, or end().
452        *
453        *  This function returns the first element of a subsequence of elements
454        *  that matches the given key.  If unsuccessful it returns an iterator
455        *  pointing to the first element that has a greater value than given key
456        *  or end() if no such element exists.
457        */
458       iterator
459       lower_bound(const key_type& __x)
460       { return _M_t.lower_bound(__x); }
461
462       const_iterator
463       lower_bound(const key_type& __x) const
464       { return _M_t.lower_bound(__x); }
465       //@}
466
467       //@{
468       /**
469        *  @brief Finds the end of a subsequence matching given key.
470        *  @param  x  Key to be located.
471        *  @return Iterator pointing to the first element
472        *          greater than key, or end().
473        */
474       iterator
475       upper_bound(const key_type& __x)
476       { return _M_t.upper_bound(__x); }
477
478       const_iterator
479       upper_bound(const key_type& __x) const
480       { return _M_t.upper_bound(__x); }
481       //@}
482
483       //@{
484       /**
485        *  @brief Finds a subsequence matching given key.
486        *  @param  x  Key to be located.
487        *  @return  Pair of iterators that possibly points to the subsequence
488        *           matching given key.
489        *
490        *  This function is equivalent to
491        *  @code
492        *    std::make_pair(c.lower_bound(val),
493        *                   c.upper_bound(val))
494        *  @endcode
495        *  (but is faster than making the calls separately).
496        *
497        *  This function probably only makes sense for multisets.
498        */
499       std::pair<iterator, iterator>
500       equal_range(const key_type& __x)
501       { return _M_t.equal_range(__x); }
502
503       std::pair<const_iterator, const_iterator>
504       equal_range(const key_type& __x) const
505       { return _M_t.equal_range(__x); }
506
507       template <class _K1, class _C1, class _A1>
508         friend bool
509         operator== (const multiset<_K1, _C1, _A1>&,
510                     const multiset<_K1, _C1, _A1>&);
511
512       template <class _K1, class _C1, class _A1>
513         friend bool
514         operator< (const multiset<_K1, _C1, _A1>&,
515                    const multiset<_K1, _C1, _A1>&);
516     };
517
518   /**
519    *  @brief  Multiset equality comparison.
520    *  @param  x  A %multiset.
521    *  @param  y  A %multiset of the same type as @a x.
522    *  @return  True iff the size and elements of the multisets are equal.
523    *
524    *  This is an equivalence relation.  It is linear in the size of the
525    *  multisets.
526    *  Multisets are considered equivalent if their sizes are equal, and if
527    *  corresponding elements compare equal.
528   */
529   template <class _Key, class _Compare, class _Alloc>
530     inline bool
531     operator==(const multiset<_Key, _Compare, _Alloc>& __x,
532                const multiset<_Key, _Compare, _Alloc>& __y)
533     { return __x._M_t == __y._M_t; }
534
535   /**
536    *  @brief  Multiset ordering relation.
537    *  @param  x  A %multiset.
538    *  @param  y  A %multiset of the same type as @a x.
539    *  @return  True iff @a x is lexicographically less than @a y.
540    *
541    *  This is a total ordering relation.  It is linear in the size of the
542    *  maps.  The elements must be comparable with @c <.
543    *
544    *  See std::lexicographical_compare() for how the determination is made.
545   */
546   template <class _Key, class _Compare, class _Alloc>
547     inline bool
548     operator<(const multiset<_Key, _Compare, _Alloc>& __x,
549               const multiset<_Key, _Compare, _Alloc>& __y)
550     { return __x._M_t < __y._M_t; }
551
552   ///  Returns !(x == y).
553   template <class _Key, class _Compare, class _Alloc>
554     inline bool
555     operator!=(const multiset<_Key, _Compare, _Alloc>& __x,
556                const multiset<_Key, _Compare, _Alloc>& __y)
557     { return !(__x == __y); }
558
559   ///  Returns y < x.
560   template <class _Key, class _Compare, class _Alloc>
561     inline bool
562     operator>(const multiset<_Key,_Compare,_Alloc>& __x,
563               const multiset<_Key,_Compare,_Alloc>& __y)
564     { return __y < __x; }
565
566   ///  Returns !(y < x)
567   template <class _Key, class _Compare, class _Alloc>
568     inline bool
569     operator<=(const multiset<_Key, _Compare, _Alloc>& __x,
570                const multiset<_Key, _Compare, _Alloc>& __y)
571     { return !(__y < __x); }
572
573   ///  Returns !(x < y)
574   template <class _Key, class _Compare, class _Alloc>
575     inline bool
576     operator>=(const multiset<_Key, _Compare, _Alloc>& __x,
577                const multiset<_Key, _Compare, _Alloc>& __y)
578     { return !(__x < __y); }
579
580   /// See std::multiset::swap().
581   template <class _Key, class _Compare, class _Alloc>
582     inline void
583     swap(multiset<_Key, _Compare, _Alloc>& __x,
584          multiset<_Key, _Compare, _Alloc>& __y)
585     { __x.swap(__y); }
586
587 } // namespace std
588
589 #endif /* _MULTISET_H */