OSDN Git Service

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