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       noexcept(is_nothrow_copy_constructible<_Compare>::value)
00086       : _Base(std::move(__x))
00087       { }
00088 
00089       multimap(initializer_list<value_type> __l,
00090            const _Compare& __c = _Compare(),
00091            const allocator_type& __a = allocator_type())
00092       : _Base(__l, __c, __a) { }
00093 #endif
00094 
00095       ~multimap() _GLIBCXX_NOEXCEPT { }
00096 
00097       multimap&
00098       operator=(const multimap& __x)
00099       {
00100     *static_cast<_Base*>(this) = __x;
00101     return *this;
00102       }
00103 
00104 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00105       multimap&
00106       operator=(multimap&& __x)
00107       {
00108     // NB: DR 1204.
00109     // NB: DR 675.
00110     this->clear();
00111     this->swap(__x);
00112     return *this;
00113       }
00114 
00115       multimap&
00116       operator=(initializer_list<value_type> __l)
00117       {
00118     this->clear();
00119     this->insert(__l);
00120     return *this;
00121       }
00122 #endif
00123 
00124       using _Base::get_allocator;
00125 
00126       // iterators:
00127       iterator
00128       begin() _GLIBCXX_NOEXCEPT
00129       { return iterator(_Base::begin()); }
00130 
00131       const_iterator
00132       begin() const _GLIBCXX_NOEXCEPT
00133       { return const_iterator(_Base::begin()); }
00134 
00135       iterator
00136       end() _GLIBCXX_NOEXCEPT
00137       { return iterator(_Base::end()); }
00138 
00139       const_iterator
00140       end() const _GLIBCXX_NOEXCEPT
00141       { return const_iterator(_Base::end()); }
00142 
00143       reverse_iterator
00144       rbegin() _GLIBCXX_NOEXCEPT
00145       { return reverse_iterator(end()); }
00146 
00147       const_reverse_iterator
00148       rbegin() const _GLIBCXX_NOEXCEPT
00149       { return const_reverse_iterator(end()); }
00150 
00151       reverse_iterator
00152       rend() _GLIBCXX_NOEXCEPT
00153       { return reverse_iterator(begin()); }
00154 
00155       const_reverse_iterator
00156       rend() const _GLIBCXX_NOEXCEPT
00157       { return const_reverse_iterator(begin()); }
00158 
00159 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00160       const_iterator
00161       cbegin() const noexcept
00162       { return const_iterator(_Base::begin()); }
00163 
00164       const_iterator
00165       cend() const noexcept
00166       { return const_iterator(_Base::end()); }
00167 
00168       const_reverse_iterator
00169       crbegin() const noexcept
00170       { return const_reverse_iterator(end()); }
00171 
00172       const_reverse_iterator
00173       crend() const noexcept
00174       { return const_reverse_iterator(begin()); }
00175 #endif
00176 
00177       // capacity:
00178       using _Base::empty;
00179       using _Base::size;
00180       using _Base::max_size;
00181 
00182       // modifiers:
00183       iterator
00184       insert(const value_type& __x)
00185       { return iterator(_Base::insert(__x)); }
00186 
00187 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00188       template<typename _Pair, typename = typename
00189            std::enable_if<std::is_convertible<_Pair,
00190                           value_type>::value>::type>
00191         iterator
00192         insert(_Pair&& __x)
00193         { return iterator(_Base::insert(std::forward<_Pair>(__x))); }
00194 #endif
00195 
00196 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00197       void
00198       insert(std::initializer_list<value_type> __list)
00199       { _Base::insert(__list); }
00200 #endif
00201 
00202       iterator
00203 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00204       insert(const_iterator __position, const value_type& __x)
00205 #else
00206       insert(iterator __position, const value_type& __x)
00207 #endif
00208       { return iterator(_Base::insert(__position, __x)); }
00209 
00210 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00211       template<typename _Pair, typename = typename
00212            std::enable_if<std::is_convertible<_Pair,
00213                           value_type>::value>::type>
00214         iterator
00215         insert(const_iterator __position, _Pair&& __x)
00216         { return iterator(_Base::insert(__position,
00217                     std::forward<_Pair>(__x))); }
00218 #endif
00219 
00220       template<typename _InputIterator>
00221         void
00222         insert(_InputIterator __first, _InputIterator __last)
00223         { _Base::insert(__first, __last); }
00224 
00225 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00226       iterator
00227       erase(const_iterator __position)
00228       { return iterator(_Base::erase(__position)); }
00229 
00230       iterator
00231       erase(iterator __position)
00232       { return iterator(_Base::erase(__position)); }
00233 #else
00234       void
00235       erase(iterator __position)
00236       { _Base::erase(__position); }
00237 #endif
00238 
00239       size_type
00240       erase(const key_type& __x)
00241       {
00242     std::pair<iterator, iterator> __victims = this->equal_range(__x);
00243     size_type __count = 0;
00244     while (__victims.first != __victims.second)
00245     {
00246       iterator __victim = __victims.first++;
00247       _Base::erase(__victim);
00248       ++__count;
00249     }
00250     return __count;
00251       }
00252 
00253 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00254       iterator
00255       erase(const_iterator __first, const_iterator __last)
00256       { return iterator(_Base::erase(__first, __last)); }
00257 #else
00258       void
00259       erase(iterator __first, iterator __last)
00260       { _Base::erase(__first, __last); }
00261 #endif
00262 
00263       void
00264       swap(multimap& __x)
00265       { _Base::swap(__x); }
00266 
00267       void
00268       clear() _GLIBCXX_NOEXCEPT
00269       { this->erase(begin(), end()); }
00270 
00271       // observers:
00272       using _Base::key_comp;
00273       using _Base::value_comp;
00274 
00275       // 23.3.1.3 multimap operations:
00276       iterator
00277       find(const key_type& __x)
00278       { return iterator(_Base::find(__x)); }
00279 
00280       const_iterator
00281       find(const key_type& __x) const
00282       { return const_iterator(_Base::find(__x)); }
00283 
00284       using _Base::count;
00285 
00286       iterator
00287       lower_bound(const key_type& __x)
00288       { return iterator(_Base::lower_bound(__x)); }
00289 
00290       const_iterator
00291       lower_bound(const key_type& __x) const
00292       { return const_iterator(_Base::lower_bound(__x)); }
00293 
00294       iterator
00295       upper_bound(const key_type& __x)
00296       { return iterator(_Base::upper_bound(__x)); }
00297 
00298       const_iterator
00299       upper_bound(const key_type& __x) const
00300       { return const_iterator(_Base::upper_bound(__x)); }
00301 
00302       std::pair<iterator,iterator>
00303       equal_range(const key_type& __x)
00304       {
00305     typedef typename _Base::iterator _Base_iterator;
00306     std::pair<_Base_iterator, _Base_iterator> __res =
00307     _Base::equal_range(__x);
00308     return std::make_pair(iterator(__res.first),
00309                   iterator(__res.second));
00310       }
00311 
00312       std::pair<const_iterator,const_iterator>
00313       equal_range(const key_type& __x) const
00314       {
00315     typedef typename _Base::const_iterator _Base_const_iterator;
00316     std::pair<_Base_const_iterator, _Base_const_iterator> __res =
00317     _Base::equal_range(__x);
00318     return std::make_pair(const_iterator(__res.first),
00319                   const_iterator(__res.second));
00320       }
00321 
00322       _Base&
00323       _M_base() _GLIBCXX_NOEXCEPT       { return *this; }
00324 
00325       const _Base&
00326       _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
00327     };
00328 
00329   template<typename _Key, typename _Tp,
00330        typename _Compare, typename _Allocator>
00331     inline bool
00332     operator==(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
00333            const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
00334     { return __lhs._M_base() == __rhs._M_base(); }
00335 
00336   template<typename _Key, typename _Tp,
00337        typename _Compare, typename _Allocator>
00338     inline bool
00339     operator!=(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
00340            const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
00341     { return __lhs._M_base() != __rhs._M_base(); }
00342 
00343   template<typename _Key, typename _Tp,
00344        typename _Compare, typename _Allocator>
00345     inline bool
00346     operator<(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
00347           const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
00348     { return __lhs._M_base() < __rhs._M_base(); }
00349 
00350   template<typename _Key, typename _Tp,
00351        typename _Compare, typename _Allocator>
00352     inline bool
00353     operator<=(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
00354            const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
00355     { return __lhs._M_base() <= __rhs._M_base(); }
00356 
00357   template<typename _Key, typename _Tp,
00358        typename _Compare, typename _Allocator>
00359     inline bool
00360     operator>=(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
00361            const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
00362     { return __lhs._M_base() >= __rhs._M_base(); }
00363 
00364   template<typename _Key, typename _Tp,
00365        typename _Compare, typename _Allocator>
00366     inline bool
00367     operator>(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
00368           const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
00369     { return __lhs._M_base() > __rhs._M_base(); }
00370 
00371   template<typename _Key, typename _Tp,
00372        typename _Compare, typename _Allocator>
00373     inline void
00374     swap(multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
00375      multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
00376     { __lhs.swap(__rhs); }
00377 
00378 } // namespace __profile
00379 } // namespace std
00380 
00381 #endif