stl_multiset.h

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

Generated on Tue Dec 2 03:59:29 2008 for libstdc++ by  doxygen 1.5.7.1