# stl_multiset.h

Go to the documentation of this file.

    00001 // Multiset implementation -*- C++ -*-
    00002
    00003 // Copyright (C) 2001, 2002, 2004, 2005, 2006 Free Software Foundation, Inc.
    00004 //
    00005 // This file is part of the GNU ISO C++ Library.  This library is free
    00006 // software; you can redistribute it and/or modify it under the
    00007 // terms of the GNU General Public License as published by the
    00008 // Free Software Foundation; either version 2, or (at your option)
    00009 // any later version.
    00010
    00011 // This library is distributed in the hope that it will be useful,
    00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00014 // GNU General Public License for more details.
    00015
    00016 // You should have received a copy of the GNU General Public License along
    00017 // with this library; see the file COPYING.  If not, write to the Free
    00018 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
    00019 // USA.
    00020
    00021 // As a special exception, you may use this file as part of a free software
    00022 // library without restriction.  Specifically, if other files instantiate
    00023 // templates or use macros or inline functions from this file, or you compile
    00024 // this file and link it with other files to produce an executable, this
    00025 // file does not by itself cause the resulting executable to be covered by
    00026 // the GNU General Public License.  This exception does not however
    00027 // invalidate any other reasons why the executable file might be covered by
    00028 // the GNU General Public License.
    00029
    00030 /*
    00031  *
    00032  * Copyright (c) 1994
    00033  * Hewlett-Packard Company
    00034  *
    00035  * Permission to use, copy, modify, distribute and sell this software
    00036  * and its documentation for any purpose is hereby granted without fee,
    00037  * provided that the above copyright notice appear in all copies and
    00038  * that both that copyright notice and this permission notice appear
    00039  * in supporting documentation.  Hewlett-Packard Company makes no
    00040  * representations about the suitability of this software for any
    00041  * purpose.  It is provided "as is" without express or implied warranty.
    00042  *
    00043  *
    00044  * Copyright (c) 1996
    00045  * Silicon Graphics Computer Systems, Inc.
    00046  *
    00047  * Permission to use, copy, modify, distribute and sell this software
    00048  * and its documentation for any purpose is hereby granted without fee,
    00049  * provided that the above copyright notice appear in all copies and
    00050  * that both that copyright notice and this permission notice appear
    00051  * in supporting documentation.  Silicon Graphics makes no
    00052  * representations about the suitability of this software for any
    00053  * purpose.  It is provided "as is" without express or implied warranty.
    00054  */
    00055
    00056 /** @file stl_multiset.h
    00057  *  This is an internal header file, included by other library headers.
    00058  *  You should not attempt to use it directly.
    00059  */
    00060
    00061 #ifndef _MULTISET_H
    00062 #define _MULTISET_H 1
    00063
    00064 #include <bits/concept_check.h>
    00065
    00066 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD)
    00067
    00068   /**
    00069    *  @brief A standard container made up of elements, which can be retrieved
    00070    *  in logarithmic time.
    00071    *
    00072    *  @ingroup Containers
    00073    *  @ingroup Assoc_containers
    00074    *
    00075    *  Meets the requirements of a <a href="tables.html#65">container</a>, a
    00076    *  <a href="tables.html#66">reversible container</a>, and an
    00077    *  <a href="tables.html#69">associative container</a> (using equivalent
    00078    *  keys).  For a @c multiset<Key> the key_type and value_type are Key.
    00079    *
    00080    *  Multisets support bidirectional iterators.
    00081    *
    00082    *  @if maint
    00083    *  The private tree data is declared exactly the same way for set and
    00084    *  multiset; the distinction is made entirely in how the tree functions are
    00085    *  called (*_unique versus *_equal, same as the standard).
    00086    *  @endif
    00087   */
    00088   template <class _Key, class _Compare = std::less<_Key>,
    00089         class _Alloc = std::allocator<_Key> >
    00090     class multiset
    00091     {
    00092       // concept requirements
    00093       typedef typename _Alloc::value_type                   _Alloc_value_type;
    00094       __glibcxx_class_requires(_Key, _SGIAssignableConcept)
    00095       __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
    00096                 _BinaryFunctionConcept)
    00097       __glibcxx_class_requires2(_Key, _Alloc_value_type, _SameTypeConcept)
    00098
    00099     public:
    00100       // typedefs:
    00101       typedef _Key     key_type;
    00102       typedef _Key     value_type;
    00103       typedef _Compare key_compare;
    00104       typedef _Compare value_compare;
    00105       typedef _Alloc   allocator_type;
    00106
    00107     private:
    00108       /// @if maint  This turns a red-black tree into a [multi]set.  @endif
    00109       typedef typename _Alloc::template rebind<_Key>::other _Key_alloc_type;
    00110
    00111       typedef _Rb_tree<key_type, value_type, _Identity<value_type>,
    00112                key_compare, _Key_alloc_type> _Rep_type;
    00113       /// @if maint  The actual tree structure.  @endif
    00114       _Rep_type _M_t;
    00115
    00116     public:
    00117       typedef typename _Key_alloc_type::pointer             pointer;
    00118       typedef typename _Key_alloc_type::const_pointer       const_pointer;
    00119       typedef typename _Key_alloc_type::reference           reference;
    00120       typedef typename _Key_alloc_type::const_reference     const_reference;
    00121       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00122       // DR 103. set::iterator is required to be modifiable,
    00123       // but this allows modification of keys.
    00124       typedef typename _Rep_type::const_iterator            iterator;
    00125       typedef typename _Rep_type::const_iterator            const_iterator;
    00126       typedef typename _Rep_type::const_reverse_iterator    reverse_iterator;
    00127       typedef typename _Rep_type::const_reverse_iterator    const_reverse_iterator;
    00128       typedef typename _Rep_type::size_type                 size_type;
    00129       typedef typename _Rep_type::difference_type           difference_type;
    00130
    00131       // allocation/deallocation
    00132
    00133       /**
    00134        *  @brief  Default constructor creates no elements.
    00135        */
    00136       multiset()
    00137       : _M_t(_Compare(), allocator_type()) { }
    00138
    00139       explicit
    00140       multiset(const _Compare& __comp,
    00141            const allocator_type& __a = allocator_type())
    00142       : _M_t(__comp, __a) { }
    00143
    00144       /**
    00145        *  @brief  Builds a %multiset from a range.
    00146        *  @param  first  An input iterator.
    00147        *  @param  last  An input iterator.
    00148        *
    00149        *  Create a %multiset consisting of copies of the elements from
    00150        *  [first,last).  This is linear in N if the range is already sorted,
    00151        *  and NlogN otherwise (where N is distance(first,last)).
    00152        */
    00153       template <class _InputIterator>
    00154         multiset(_InputIterator __first, _InputIterator __last)
    00155     : _M_t(_Compare(), allocator_type())
    00156         { _M_t._M_insert_equal(__first, __last); }
    00157
    00158       /**
    00159        *  @brief  Builds a %multiset from a range.
    00160        *  @param  first  An input iterator.
    00161        *  @param  last  An input iterator.
    00162        *  @param  comp  A comparison functor.
    00163        *  @param  a  An allocator object.
    00164        *
    00165        *  Create a %multiset consisting of copies of the elements from
    00166        *  [first,last).  This is linear in N if the range is already sorted,
    00167        *  and NlogN otherwise (where N is distance(first,last)).
    00168        */
    00169       template <class _InputIterator>
    00170         multiset(_InputIterator __first, _InputIterator __last,
    00171          const _Compare& __comp,
    00172          const allocator_type& __a = allocator_type())
    00173     : _M_t(__comp, __a)
    00174         { _M_t._M_insert_equal(__first, __last); }
    00175
    00176       /**
    00177        *  @brief  %Multiset copy constructor.
    00178        *  @param  x  A %multiset of identical element and allocator types.
    00179        *
    00180        *  The newly-created %multiset uses a copy of the allocation object used
    00181        *  by @a x.
    00182        */
    00183       multiset(const multiset<_Key,_Compare,_Alloc>& __x)
    00184       : _M_t(__x._M_t) { }
    00185
    00186       /**
    00187        *  @brief  %Multiset assignment operator.
    00188        *  @param  x  A %multiset of identical element and allocator types.
    00189        *
    00190        *  All the elements of @a x are copied, but unlike the copy constructor,
    00191        *  the allocator object is not copied.
    00192        */
    00193       multiset<_Key,_Compare,_Alloc>&
    00194       operator=(const multiset<_Key,_Compare,_Alloc>& __x)
    00195       {
    00196     _M_t = __x._M_t;
    00197     return *this;
    00198       }
    00199
    00200       // accessors:
    00201
    00202       ///  Returns the comparison object.
    00203       key_compare
    00204       key_comp() const
    00205       { return _M_t.key_comp(); }
    00206       ///  Returns the comparison object.
    00207       value_compare
    00208       value_comp() const
    00209       { return _M_t.key_comp(); }
    00210       ///  Returns the memory allocation object.
    00211       allocator_type
    00212       get_allocator() const
    00213       { return _M_t.get_allocator(); }
    00214
    00215       /**
    00216        *  Returns a read/write iterator that points to the first element in the
    00217        *  %multiset.  Iteration is done in ascending order according to the
    00218        *  keys.
    00219        */
    00220       iterator
    00221       begin() const
    00222       { return _M_t.begin(); }
    00223
    00224       /**
    00225        *  Returns a read/write iterator that points one past the last element in
    00226        *  the %multiset.  Iteration is done in ascending order according to the
    00227        *  keys.
    00228        */
    00229       iterator
    00230       end() const
    00231       { return _M_t.end(); }
    00232
    00233       /**
    00234        *  Returns a read/write reverse iterator that points to the last element
    00235        *  in the %multiset.  Iteration is done in descending order according to
    00236        *  the keys.
    00237        */
    00238       reverse_iterator
    00239       rbegin() const
    00240       { return _M_t.rbegin(); }
    00241
    00242       /**
    00243        *  Returns a read/write reverse iterator that points to the last element
    00244        *  in the %multiset.  Iteration is done in descending order according to
    00245        *  the keys.
    00246        */
    00247       reverse_iterator
    00248       rend() const
    00249       { return _M_t.rend(); }
    00250
    00251       ///  Returns true if the %set is empty.
    00252       bool
    00253       empty() const
    00254       { return _M_t.empty(); }
    00255
    00256       ///  Returns the size of the %set.
    00257       size_type
    00258       size() const
    00259       { return _M_t.size(); }
    00260
    00261       ///  Returns the maximum size of the %set.
    00262       size_type
    00263       max_size() const
    00264       { return _M_t.max_size(); }
    00265
    00266       /**
    00267        *  @brief  Swaps data with another %multiset.
    00268        *  @param  x  A %multiset of the same element and allocator types.
    00269        *
    00270        *  This exchanges the elements between two multisets in constant time.
    00271        *  (It is only swapping a pointer, an integer, and an instance of the @c
    00272        *  Compare type (which itself is often stateless and empty), so it should
    00273        *  be quite fast.)
    00274        *  Note that the global std::swap() function is specialized such that
    00275        *  std::swap(s1,s2) will feed to this function.
    00276        */
    00277       void
    00278       swap(multiset<_Key, _Compare, _Alloc>& __x)
    00279       { _M_t.swap(__x._M_t); }
    00280
    00281       // insert/erase
    00282       /**
    00283        *  @brief Inserts an element into the %multiset.
    00284        *  @param  x  Element to be inserted.
    00285        *  @return An iterator that points to the inserted element.
    00286        *
    00287        *  This function inserts an element into the %multiset.  Contrary
    00288        *  to a std::set the %multiset does not rely on unique keys and thus
    00289        *  multiple copies of the same element can be inserted.
    00290        *
    00291        *  Insertion requires logarithmic time.
    00292        */
    00293       iterator
    00294       insert(const value_type& __x)
    00295       { return _M_t._M_insert_equal(__x); }
    00296
    00297       /**
    00298        *  @brief Inserts an element into the %multiset.
    00299        *  @param  position  An iterator that serves as a hint as to where the
    00300        *                    element should be inserted.
    00301        *  @param  x  Element to be inserted.
    00302        *  @return An iterator that points to the inserted element.
    00303        *
    00304        *  This function inserts an element into the %multiset.  Contrary
    00305        *  to a std::set the %multiset does not rely on unique keys and thus
    00306        *  multiple copies of the same element can be inserted.
    00307        *
    00308        *  Note that the first parameter is only a hint and can potentially
    00309        *  improve the performance of the insertion process.  A bad hint would
    00310        *  cause no gains in efficiency.
    00311        *
    00312        *  See http://gcc.gnu.org/onlinedocs/libstdc++/23_containers/howto.html#4
    00313        *  for more on "hinting".
    00314        *
    00315        *  Insertion requires logarithmic time (if the hint is not taken).
    00316        */
    00317       iterator
    00318       insert(iterator __position, const value_type& __x)
    00319       { return _M_t._M_insert_equal(__position, __x); }
    00320
    00321       /**
    00322        *  @brief A template function that attemps to insert a range of elements.
    00323        *  @param  first  Iterator pointing to the start of the range to be
    00324        *                 inserted.
    00325        *  @param  last  Iterator pointing to the end of the range.
    00326        *
    00327        *  Complexity similar to that of the range constructor.
    00328        */
    00329       template <class _InputIterator>
    00330         void
    00331         insert(_InputIterator __first, _InputIterator __last)
    00332         { _M_t._M_insert_equal(__first, __last); }
    00333
    00334       /**
    00335        *  @brief Erases an element from a %multiset.
    00336        *  @param  position  An iterator pointing to the element to be erased.
    00337        *
    00338        *  This function erases an element, pointed to by the given iterator,
    00339        *  from a %multiset.  Note that this function only erases the element,
    00340        *  and that if the element is itself a pointer, the pointed-to memory is
    00341        *  not touched in any way.  Managing the pointer is the user's
    00342        *  responsibilty.
    00343        */
    00344       void
    00345       erase(iterator __position)
    00346       { _M_t.erase(__position); }
    00347
    00348       /**
    00349        *  @brief Erases elements according to the provided key.
    00350        *  @param  x  Key of element to be erased.
    00351        *  @return  The number of elements erased.
    00352        *
    00353        *  This function erases all elements located by the given key from a
    00354        *  %multiset.
    00355        *  Note that this function only erases the element, and that if
    00356        *  the element is itself a pointer, the pointed-to memory is not touched
    00357        *  in any way.  Managing the pointer is the user's responsibilty.
    00358        */
    00359       size_type
    00360       erase(const key_type& __x)
    00361       { return _M_t.erase(__x); }
    00362
    00363       /**
    00364        *  @brief Erases a [first,last) range of elements from a %multiset.
    00365        *  @param  first  Iterator pointing to the start of the range to be
    00366        *                 erased.
    00367        *  @param  last  Iterator pointing to the end of the range to be erased.
    00368        *
    00369        *  This function erases a sequence of elements from a %multiset.
    00370        *  Note that this function only erases the elements, and that if
    00371        *  the elements themselves are pointers, the pointed-to memory is not
    00372        *  touched in any way.  Managing the pointer is the user's responsibilty.
    00373        */
    00374       void
    00375       erase(iterator __first, iterator __last)
    00376       { _M_t.erase(__first, __last); }
    00377
    00378       /**
    00379        *  Erases all elements in a %multiset.  Note that this function only
    00380        *  erases the elements, and that if the elements themselves are pointers,
    00381        *  the pointed-to memory is not touched in any way.  Managing the pointer
    00382        *  is the user's responsibilty.
    00383        */
    00384       void
    00385       clear()
    00386       { _M_t.clear(); }
    00387
    00388       // multiset operations:
    00389
    00390       /**
    00391        *  @brief Finds the number of elements with given key.
    00392        *  @param  x  Key of elements to be located.
    00393        *  @return Number of elements with specified key.
    00394        */
    00395       size_type
    00396       count(const key_type& __x) const
    00397       { return _M_t.count(__x); }
    00398
    00399       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00400       // 214.  set::find() missing const overload
    00401       //@{
    00402       /**
    00403        *  @brief Tries to locate an element in a %set.
    00404        *  @param  x  Element to be located.
    00405        *  @return  Iterator pointing to sought-after element, or end() if not
    00406        *           found.
    00407        *
    00408        *  This function takes a key and tries to locate the element with which
    00409        *  the key matches.  If successful the function returns an iterator
    00410        *  pointing to the sought after element.  If unsuccessful it returns the
    00411        *  past-the-end ( @c end() ) iterator.
    00412        */
    00413       iterator
    00414       find(const key_type& __x)
    00415       { return _M_t.find(__x); }
    00416
    00417       const_iterator
    00418       find(const key_type& __x) const
    00419       { return _M_t.find(__x); }
    00420       //@}
    00421
    00422       //@{
    00423       /**
    00424        *  @brief Finds the beginning of a subsequence matching given key.
    00425        *  @param  x  Key to be located.
    00426        *  @return  Iterator pointing to first element equal to or greater
    00427        *           than key, or end().
    00428        *
    00429        *  This function returns the first element of a subsequence of elements
    00430        *  that matches the given key.  If unsuccessful it returns an iterator
    00431        *  pointing to the first element that has a greater value than given key
    00432        *  or end() if no such element exists.
    00433        */
    00434       iterator
    00435       lower_bound(const key_type& __x)
    00436       { return _M_t.lower_bound(__x); }
    00437
    00438       const_iterator
    00439       lower_bound(const key_type& __x) const
    00440       { return _M_t.lower_bound(__x); }
    00441       //@}
    00442
    00443       //@{
    00444       /**
    00445        *  @brief Finds the end of a subsequence matching given key.
    00446        *  @param  x  Key to be located.
    00447        *  @return Iterator pointing to the first element
    00448        *          greater than key, or end().
    00449        */
    00450       iterator
    00451       upper_bound(const key_type& __x)
    00452       { return _M_t.upper_bound(__x); }
    00453
    00454       const_iterator
    00455       upper_bound(const key_type& __x) const
    00456       { return _M_t.upper_bound(__x); }
    00457       //@}
    00458
    00459       //@{
    00460       /**
    00461        *  @brief Finds a subsequence matching given key.
    00462        *  @param  x  Key to be located.
    00463        *  @return  Pair of iterators that possibly points to the subsequence
    00464        *           matching given key.
    00465        *
    00466        *  This function is equivalent to
    00467        *  @code
    00468        *    std::make_pair(c.lower_bound(val),
    00469        *                   c.upper_bound(val))
    00470        *  @endcode
    00471        *  (but is faster than making the calls separately).
    00472        *
    00473        *  This function probably only makes sense for multisets.
    00474        */
    00475       std::pair<iterator, iterator>
    00476       equal_range(const key_type& __x)
    00477       { return _M_t.equal_range(__x); }
    00478
    00479       std::pair<const_iterator, const_iterator>
    00480       equal_range(const key_type& __x) const
    00481       { return _M_t.equal_range(__x); }
    00482
    00483       template <class _K1, class _C1, class _A1>
    00484         friend bool
    00485         operator== (const multiset<_K1, _C1, _A1>&,
    00486             const multiset<_K1, _C1, _A1>&);
    00487
    00488       template <class _K1, class _C1, class _A1>
    00489         friend bool
    00490         operator< (const multiset<_K1, _C1, _A1>&,
    00491            const multiset<_K1, _C1, _A1>&);
    00492     };
    00493
    00494   /**
    00495    *  @brief  Multiset equality comparison.
    00496    *  @param  x  A %multiset.
    00497    *  @param  y  A %multiset of the same type as @a x.
    00498    *  @return  True iff the size and elements of the multisets are equal.
    00499    *
    00500    *  This is an equivalence relation.  It is linear in the size of the
    00501    *  multisets.
    00502    *  Multisets are considered equivalent if their sizes are equal, and if
    00503    *  corresponding elements compare equal.
    00504   */
    00505   template <class _Key, class _Compare, class _Alloc>
    00506     inline bool
    00507     operator==(const multiset<_Key, _Compare, _Alloc>& __x,
    00508            const multiset<_Key, _Compare, _Alloc>& __y)
    00509     { return __x._M_t == __y._M_t; }
    00510
    00511   /**
    00512    *  @brief  Multiset ordering relation.
    00513    *  @param  x  A %multiset.
    00514    *  @param  y  A %multiset of the same type as @a x.
    00515    *  @return  True iff @a x is lexicographically less than @a y.
    00516    *
    00517    *  This is a total ordering relation.  It is linear in the size of the
    00518    *  maps.  The elements must be comparable with @c <.
    00519    *
    00520    *  See std::lexicographical_compare() for how the determination is made.
    00521   */
    00522   template <class _Key, class _Compare, class _Alloc>
    00523     inline bool
    00524     operator<(const multiset<_Key, _Compare, _Alloc>& __x,
    00525           const multiset<_Key, _Compare, _Alloc>& __y)
    00526     { return __x._M_t < __y._M_t; }
    00527
    00528   ///  Returns !(x == y).
    00529   template <class _Key, class _Compare, class _Alloc>
    00530     inline bool
    00531     operator!=(const multiset<_Key, _Compare, _Alloc>& __x,
    00532            const multiset<_Key, _Compare, _Alloc>& __y)
    00533     { return !(__x == __y); }
    00534
    00535   ///  Returns y < x.
    00536   template <class _Key, class _Compare, class _Alloc>
    00537     inline bool
    00538     operator>(const multiset<_Key,_Compare,_Alloc>& __x,
    00539           const multiset<_Key,_Compare,_Alloc>& __y)
    00540     { return __y < __x; }
    00541
    00542   ///  Returns !(y < x)
    00543   template <class _Key, class _Compare, class _Alloc>
    00544     inline bool
    00545     operator<=(const multiset<_Key, _Compare, _Alloc>& __x,
    00546            const multiset<_Key, _Compare, _Alloc>& __y)
    00547     { return !(__y < __x); }
    00548
    00549   ///  Returns !(x < y)
    00550   template <class _Key, class _Compare, class _Alloc>
    00551     inline bool
    00552     operator>=(const multiset<_Key, _Compare, _Alloc>& __x,
    00553            const multiset<_Key, _Compare, _Alloc>& __y)
    00554     { return !(__x < __y); }
    00555
    00556   /// See std::multiset::swap().
    00557   template <class _Key, class _Compare, class _Alloc>
    00558     inline void
    00559     swap(multiset<_Key, _Compare, _Alloc>& __x,
    00560      multiset<_Key, _Compare, _Alloc>& __y)
    00561     { __x.swap(__y); }
    00562
    00563 _GLIBCXX_END_NESTED_NAMESPACE
    00564
    00565 #endif /* _MULTISET_H */