stl_multimap.h

Go to the documentation of this file.
00001 // Multimap 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,1997
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 
00061 #ifndef _MULTIMAP_H
00062 #define _MULTIMAP_H 1
00063 
00064 #include <bits/concept_check.h>
00065 
00066 namespace _GLIBCXX_STD
00067 {
00068   // Forward declaration of operators < and ==, needed for friend declaration.
00069 
00070   template <typename _Key, typename _Tp,
00071             typename _Compare = std::less<_Key>,
00072             typename _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
00073     class multimap;
00074 
00075   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
00076     inline bool
00077     operator==(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
00078            const multimap<_Key, _Tp, _Compare, _Alloc>& __y);
00079 
00080   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
00081     inline bool
00082     operator<(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
00083           const multimap<_Key, _Tp, _Compare, _Alloc>& __y);
00084 
00106   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
00107     class multimap
00108     {
00109     public:
00110       typedef _Key                                          key_type;
00111       typedef _Tp                                           mapped_type;
00112       typedef std::pair<const _Key, _Tp>                    value_type;
00113       typedef _Compare                                      key_compare;
00114       typedef _Alloc                                        allocator_type;
00115 
00116     private:
00117       // concept requirements
00118       typedef typename _Alloc::value_type                   _Alloc_value_type;
00119       __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
00120       __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
00121                 _BinaryFunctionConcept)
00122       __glibcxx_class_requires2(value_type, _Alloc_value_type, _SameTypeConcept)    
00123 
00124     public:
00125       class value_compare
00126       : public std::binary_function<value_type, value_type, bool>
00127       {
00128     friend class multimap<_Key, _Tp, _Compare, _Alloc>;
00129       protected:
00130     _Compare comp;
00131 
00132     value_compare(_Compare __c)
00133     : comp(__c) { }
00134 
00135       public:
00136     bool operator()(const value_type& __x, const value_type& __y) const
00137     { return comp(__x.first, __y.first); }
00138       };
00139 
00140     private:
00142       typedef typename _Alloc::template rebind<value_type>::other 
00143         _Pair_alloc_type;
00144 
00145       typedef _Rb_tree<key_type, value_type, _Select1st<value_type>,
00146                key_compare, _Pair_alloc_type> _Rep_type;
00148       _Rep_type _M_t;
00149 
00150     public:
00151       // many of these are specified differently in ISO, but the following are
00152       // "functionally equivalent"
00153       typedef typename _Pair_alloc_type::pointer         pointer;
00154       typedef typename _Pair_alloc_type::const_pointer   const_pointer;
00155       typedef typename _Pair_alloc_type::reference       reference;
00156       typedef typename _Pair_alloc_type::const_reference const_reference;
00157       typedef typename _Rep_type::iterator               iterator;
00158       typedef typename _Rep_type::const_iterator         const_iterator;
00159       typedef typename _Rep_type::size_type              size_type;
00160       typedef typename _Rep_type::difference_type        difference_type;
00161       typedef typename _Rep_type::reverse_iterator       reverse_iterator;
00162       typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
00163 
00164       // [23.3.2] construct/copy/destroy
00165       // (get_allocator() is also listed in this section)
00169       multimap()
00170       : _M_t(_Compare(), allocator_type()) { }
00171 
00172       // for some reason this was made a separate function
00176       explicit
00177       multimap(const _Compare& __comp,
00178            const allocator_type& __a = allocator_type())
00179       : _M_t(__comp, __a) { }
00180 
00188       multimap(const multimap& __x)
00189       : _M_t(__x._M_t) { }
00190 
00200       template <typename _InputIterator>
00201         multimap(_InputIterator __first, _InputIterator __last)
00202     : _M_t(_Compare(), allocator_type())
00203         { _M_t.insert_equal(__first, __last); }
00204 
00216       template <typename _InputIterator>
00217         multimap(_InputIterator __first, _InputIterator __last,
00218          const _Compare& __comp,
00219          const allocator_type& __a = allocator_type())
00220         : _M_t(__comp, __a)
00221         { _M_t.insert_equal(__first, __last); }
00222 
00223       // FIXME There is no dtor declared, but we should have something generated
00224       // by Doxygen.  I don't know what tags to add to this paragraph to make
00225       // that happen:
00239       multimap&
00240       operator=(const multimap& __x)
00241       {
00242     _M_t = __x._M_t;
00243     return *this;
00244       }
00245 
00247       allocator_type
00248       get_allocator() const
00249       { return _M_t.get_allocator(); }
00250 
00251       // iterators
00257       iterator
00258       begin()
00259       { return _M_t.begin(); }
00260 
00266       const_iterator
00267       begin() const
00268       { return _M_t.begin(); }
00269 
00275       iterator
00276       end()
00277       { return _M_t.end(); }
00278 
00284       const_iterator
00285       end() const
00286       { return _M_t.end(); }
00287 
00293       reverse_iterator
00294       rbegin()
00295       { return _M_t.rbegin(); }
00296 
00302       const_reverse_iterator
00303       rbegin() const
00304       { return _M_t.rbegin(); }
00305 
00311       reverse_iterator
00312       rend()
00313       { return _M_t.rend(); }
00314 
00320       const_reverse_iterator
00321       rend() const
00322       { return _M_t.rend(); }
00323 
00324       // capacity
00326       bool
00327       empty() const
00328       { return _M_t.empty(); }
00329 
00331       size_type
00332       size() const
00333       { return _M_t.size(); }
00334 
00336       size_type
00337       max_size() const
00338       { return _M_t.max_size(); }
00339 
00340       // modifiers
00353       iterator
00354       insert(const value_type& __x)
00355       { return _M_t.insert_equal(__x); }
00356 
00377       iterator
00378       insert(iterator __position, const value_type& __x)
00379       { return _M_t.insert_equal(__position, __x); }
00380 
00389       template <typename _InputIterator>
00390         void
00391         insert(_InputIterator __first, _InputIterator __last)
00392         { _M_t.insert_equal(__first, __last); }
00393 
00404       void
00405       erase(iterator __position)
00406       { _M_t.erase(__position); }
00407 
00419       size_type
00420       erase(const key_type& __x)
00421       { return _M_t.erase(__x); }
00422 
00434       void
00435       erase(iterator __first, iterator __last)
00436       { _M_t.erase(__first, __last); }
00437 
00449       void
00450       swap(multimap& __x)
00451       { _M_t.swap(__x._M_t); }
00452 
00459       void
00460       clear()
00461       { _M_t.clear(); }
00462 
00463       // observers
00468       key_compare
00469       key_comp() const
00470       { return _M_t.key_comp(); }
00471 
00476       value_compare
00477       value_comp() const
00478       { return value_compare(_M_t.key_comp()); }
00479 
00480       // multimap operations
00492       iterator
00493       find(const key_type& __x)
00494       { return _M_t.find(__x); }
00495 
00507       const_iterator
00508       find(const key_type& __x) const
00509       { return _M_t.find(__x); }
00510 
00516       size_type
00517       count(const key_type& __x) const
00518       { return _M_t.count(__x); }
00519 
00531       iterator
00532       lower_bound(const key_type& __x)
00533       { return _M_t.lower_bound(__x); }
00534 
00546       const_iterator
00547       lower_bound(const key_type& __x) const
00548       { return _M_t.lower_bound(__x); }
00549 
00556       iterator
00557       upper_bound(const key_type& __x)
00558       { return _M_t.upper_bound(__x); }
00559 
00566       const_iterator
00567       upper_bound(const key_type& __x) const
00568       { return _M_t.upper_bound(__x); }
00569 
00583       std::pair<iterator, iterator>
00584       equal_range(const key_type& __x)
00585       { return _M_t.equal_range(__x); }
00586 
00600       std::pair<const_iterator, const_iterator>
00601       equal_range(const key_type& __x) const
00602       { return _M_t.equal_range(__x); }
00603 
00604       template <typename _K1, typename _T1, typename _C1, typename _A1>
00605         friend bool
00606         operator== (const multimap<_K1, _T1, _C1, _A1>&,
00607             const multimap<_K1, _T1, _C1, _A1>&);
00608 
00609       template <typename _K1, typename _T1, typename _C1, typename _A1>
00610         friend bool
00611         operator< (const multimap<_K1, _T1, _C1, _A1>&,
00612            const multimap<_K1, _T1, _C1, _A1>&);
00613   };
00614 
00625   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
00626     inline bool
00627     operator==(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
00628                const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
00629     { return __x._M_t == __y._M_t; }
00630 
00642   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
00643     inline bool
00644     operator<(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
00645               const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
00646     { return __x._M_t < __y._M_t; }
00647 
00649   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
00650     inline bool
00651     operator!=(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
00652                const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
00653     { return !(__x == __y); }
00654 
00656   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
00657     inline bool
00658     operator>(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
00659               const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
00660     { return __y < __x; }
00661 
00663   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
00664     inline bool
00665     operator<=(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
00666                const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
00667     { return !(__y < __x); }
00668 
00670   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
00671     inline bool
00672     operator>=(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
00673                const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
00674     { return !(__x < __y); }
00675 
00677   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
00678     inline void
00679     swap(multimap<_Key, _Tp, _Compare, _Alloc>& __x,
00680          multimap<_Key, _Tp, _Compare, _Alloc>& __y)
00681     { __x.swap(__y); }
00682 } // namespace std
00683 
00684 #endif /* _MULTIMAP_H */

Generated on Tue Feb 2 16:56:39 2010 for GNU C++ STL by  doxygen 1.4.7