OSDN Git Service

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