libstdc++
profile/multimap.h
Go to the documentation of this file.
00001 // Profiling multimap implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2009, 2010, 2011 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file profile/multimap.h
00026  *  This file is a GNU profile extension to the Standard C++ Library.
00027  */
00028 
00029 #ifndef _GLIBCXX_PROFILE_MULTIMAP_H
00030 #define _GLIBCXX_PROFILE_MULTIMAP_H 1
00031 
00032 #include <utility>
00033 
00034 namespace std _GLIBCXX_VISIBILITY(default)
00035 {
00036 namespace __profile
00037 {
00038   /// Class std::multimap wrapper with performance instrumentation.
00039   template<typename _Key, typename _Tp, typename _Compare = std::less<_Key>,
00040        typename _Allocator = std::allocator<std::pair<const _Key, _Tp> > >
00041     class multimap
00042     : public _GLIBCXX_STD_C::multimap<_Key, _Tp, _Compare, _Allocator>
00043     {
00044       typedef _GLIBCXX_STD_C::multimap<_Key, _Tp, _Compare, _Allocator> _Base;
00045 
00046     public:
00047       // types:
00048       typedef _Key                   key_type;
00049       typedef _Tp                    mapped_type;
00050       typedef std::pair<const _Key, _Tp>             value_type;
00051       typedef _Compare                               key_compare;
00052       typedef _Allocator                             allocator_type;
00053       typedef typename _Base::reference              reference;
00054       typedef typename _Base::const_reference        const_reference;
00055 
00056       typedef typename _Base::iterator               iterator;
00057       typedef typename _Base::const_iterator         const_iterator;
00058       typedef typename _Base::reverse_iterator       reverse_iterator;
00059       typedef typename _Base::const_reverse_iterator const_reverse_iterator;
00060 
00061       typedef typename _Base::size_type              size_type;
00062       typedef typename _Base::difference_type        difference_type;
00063       typedef typename _Base::pointer                pointer;
00064       typedef typename _Base::const_pointer          const_pointer;
00065 
00066       // 23.3.1.1 construct/copy/destroy:
00067       explicit multimap(const _Compare& __comp = _Compare(),
00068             const _Allocator& __a = _Allocator())
00069       : _Base(__comp, __a) { }
00070 
00071       template<typename _InputIterator>
00072       multimap(_InputIterator __first, _InputIterator __last,
00073            const _Compare& __comp = _Compare(),
00074            const _Allocator& __a = _Allocator())
00075       : _Base(__first, __last, __comp, __a) { }
00076 
00077       multimap(const multimap& __x)
00078       : _Base(__x) { }
00079 
00080       multimap(const _Base& __x)
00081       : _Base(__x) { }
00082 
00083 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00084       multimap(multimap&& __x)
00085       : _Base(std::move(__x))
00086       { }
00087 
00088       multimap(initializer_list<value_type> __l,
00089            const _Compare& __c = _Compare(),
00090            const allocator_type& __a = allocator_type())
00091       : _Base(__l, __c, __a) { }
00092 #endif
00093 
00094       ~multimap() { }
00095 
00096       multimap&
00097       operator=(const multimap& __x)
00098       {
00099     *static_cast<_Base*>(this) = __x;
00100     return *this;
00101       }
00102 
00103 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00104       multimap&
00105       operator=(multimap&& __x)
00106       {
00107     // NB: DR 1204.
00108     // NB: DR 675.
00109     this->clear();
00110     this->swap(__x);
00111     return *this;
00112       }
00113 
00114       multimap&
00115       operator=(initializer_list<value_type> __l)
00116       {
00117     this->clear();
00118     this->insert(__l);
00119     return *this;
00120       }
00121 #endif
00122 
00123       using _Base::get_allocator;
00124 
00125       // iterators:
00126       iterator
00127       begin()
00128       { return iterator(_Base::begin()); }
00129 
00130       const_iterator
00131       begin() const
00132       { return const_iterator(_Base::begin()); }
00133 
00134       iterator
00135       end()
00136       { return iterator(_Base::end()); }
00137 
00138       const_iterator
00139       end() const
00140       { return const_iterator(_Base::end()); }
00141 
00142       reverse_iterator
00143       rbegin()
00144       { return reverse_iterator(end()); }
00145 
00146       const_reverse_iterator
00147       rbegin() const
00148       { return const_reverse_iterator(end()); }
00149 
00150       reverse_iterator
00151       rend()
00152       { return reverse_iterator(begin()); }
00153 
00154       const_reverse_iterator
00155       rend() const
00156       { return const_reverse_iterator(begin()); }
00157 
00158 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00159       const_iterator
00160       cbegin() const
00161       { return const_iterator(_Base::begin()); }
00162 
00163       const_iterator
00164       cend() const
00165       { return const_iterator(_Base::end()); }
00166 
00167       const_reverse_iterator
00168       crbegin() const
00169       { return const_reverse_iterator(end()); }
00170 
00171       const_reverse_iterator
00172       crend() const
00173       { return const_reverse_iterator(begin()); }
00174 #endif
00175 
00176       // capacity:
00177       using _Base::empty;
00178       using _Base::size;
00179       using _Base::max_size;
00180 
00181       // modifiers:
00182       iterator
00183       insert(const value_type& __x)
00184       { return iterator(_Base::insert(__x)); }
00185 
00186 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00187       template<typename _Pair, typename = typename
00188            std::enable_if<std::is_convertible<_Pair,
00189                           value_type>::value>::type>
00190         iterator
00191         insert(_Pair&& __x)
00192         { return iterator(_Base::insert(std::forward<_Pair>(__x))); }
00193 #endif
00194 
00195 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00196       void
00197       insert(std::initializer_list<value_type> __list)
00198       { _Base::insert(__list); }
00199 #endif
00200 
00201       iterator
00202 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00203       insert(const_iterator __position, const value_type& __x)
00204 #else
00205       insert(iterator __position, const value_type& __x)
00206 #endif
00207       { return iterator(_Base::insert(__position, __x)); }
00208 
00209 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00210       template<typename _Pair, typename = typename
00211            std::enable_if<std::is_convertible<_Pair,
00212                           value_type>::value>::type>
00213         iterator
00214         insert(const_iterator __position, _Pair&& __x)
00215         { return iterator(_Base::insert(__position,
00216                     std::forward<_Pair>(__x))); }
00217 #endif
00218 
00219       template<typename _InputIterator>
00220         void
00221         insert(_InputIterator __first, _InputIterator __last)
00222         { _Base::insert(__first, __last); }
00223 
00224 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00225       iterator
00226       erase(const_iterator __position)
00227       { return iterator(_Base::erase(__position)); }
00228 
00229       iterator
00230       erase(iterator __position)
00231       { return iterator(_Base::erase(__position)); }
00232 #else
00233       void
00234       erase(iterator __position)
00235       { _Base::erase(__position); }
00236 #endif
00237 
00238       size_type
00239       erase(const key_type& __x)
00240       {
00241     std::pair<iterator, iterator> __victims = this->equal_range(__x);
00242     size_type __count = 0;
00243     while (__victims.first != __victims.second)
00244     {
00245       iterator __victim = __victims.first++;
00246       _Base::erase(__victim);
00247       ++__count;
00248     }
00249     return __count;
00250       }
00251 
00252 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00253       iterator
00254       erase(const_iterator __first, const_iterator __last)
00255       { return iterator(_Base::erase(__first, __last)); }
00256 #else
00257       void
00258       erase(iterator __first, iterator __last)
00259       { _Base::erase(__first, __last); }
00260 #endif
00261 
00262       void
00263       swap(multimap& __x)
00264       { _Base::swap(__x); }
00265 
00266       void
00267       clear()
00268       { this->erase(begin(), end()); }
00269 
00270       // observers:
00271       using _Base::key_comp;
00272       using _Base::value_comp;
00273 
00274       // 23.3.1.3 multimap operations:
00275       iterator
00276       find(const key_type& __x)
00277       { return iterator(_Base::find(__x)); }
00278 
00279       const_iterator
00280       find(const key_type& __x) const
00281       { return const_iterator(_Base::find(__x)); }
00282 
00283       using _Base::count;
00284 
00285       iterator
00286       lower_bound(const key_type& __x)
00287       { return iterator(_Base::lower_bound(__x)); }
00288 
00289       const_iterator
00290       lower_bound(const key_type& __x) const
00291       { return const_iterator(_Base::lower_bound(__x)); }
00292 
00293       iterator
00294       upper_bound(const key_type& __x)
00295       { return iterator(_Base::upper_bound(__x)); }
00296 
00297       const_iterator
00298       upper_bound(const key_type& __x) const
00299       { return const_iterator(_Base::upper_bound(__x)); }
00300 
00301       std::pair<iterator,iterator>
00302       equal_range(const key_type& __x)
00303       {
00304     typedef typename _Base::iterator _Base_iterator;
00305     std::pair<_Base_iterator, _Base_iterator> __res =
00306     _Base::equal_range(__x);
00307     return std::make_pair(iterator(__res.first),
00308                   iterator(__res.second));
00309       }
00310 
00311       std::pair<const_iterator,const_iterator>
00312       equal_range(const key_type& __x) const
00313       {
00314     typedef typename _Base::const_iterator _Base_const_iterator;
00315     std::pair<_Base_const_iterator, _Base_const_iterator> __res =
00316     _Base::equal_range(__x);
00317     return std::make_pair(const_iterator(__res.first),
00318                   const_iterator(__res.second));
00319       }
00320 
00321       _Base&
00322       _M_base() { return *this; }
00323 
00324       const _Base&
00325       _M_base() const { return *this; }
00326     };
00327 
00328   template<typename _Key, typename _Tp,
00329        typename _Compare, typename _Allocator>
00330     inline bool
00331     operator==(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
00332            const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
00333     { return __lhs._M_base() == __rhs._M_base(); }
00334 
00335   template<typename _Key, typename _Tp,
00336        typename _Compare, typename _Allocator>
00337     inline bool
00338     operator!=(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
00339            const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
00340     { return __lhs._M_base() != __rhs._M_base(); }
00341 
00342   template<typename _Key, typename _Tp,
00343        typename _Compare, typename _Allocator>
00344     inline bool
00345     operator<(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
00346           const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
00347     { return __lhs._M_base() < __rhs._M_base(); }
00348 
00349   template<typename _Key, typename _Tp,
00350        typename _Compare, typename _Allocator>
00351     inline bool
00352     operator<=(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
00353            const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
00354     { return __lhs._M_base() <= __rhs._M_base(); }
00355 
00356   template<typename _Key, typename _Tp,
00357        typename _Compare, typename _Allocator>
00358     inline bool
00359     operator>=(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
00360            const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
00361     { return __lhs._M_base() >= __rhs._M_base(); }
00362 
00363   template<typename _Key, typename _Tp,
00364        typename _Compare, typename _Allocator>
00365     inline bool
00366     operator>(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
00367           const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
00368     { return __lhs._M_base() > __rhs._M_base(); }
00369 
00370   template<typename _Key, typename _Tp,
00371        typename _Compare, typename _Allocator>
00372     inline void
00373     swap(multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
00374      multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
00375     { __lhs.swap(__rhs); }
00376 
00377 } // namespace __profile
00378 } // namespace std
00379 
00380 #endif