stl_multiset.h

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

Generated on Fri May 6 01:09:11 2005 for libstdc++-v3 Source by  doxygen 1.4.2