libstdc++
multiset.h
Go to the documentation of this file.
00001 // Profiling multiset implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2009-2013 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/multiset.h
00026  *  This file is a GNU profile extension to the Standard C++ Library.
00027  */
00028 
00029 #ifndef _GLIBCXX_PROFILE_MULTISET_H
00030 #define _GLIBCXX_PROFILE_MULTISET_H 1
00031 
00032 #include <utility>
00033 
00034 namespace std _GLIBCXX_VISIBILITY(default)
00035 {
00036 namespace __profile
00037 {
00038   /// Class std::multiset wrapper with performance instrumentation.
00039   template<typename _Key, typename _Compare = std::less<_Key>,
00040        typename _Allocator = std::allocator<_Key> >
00041     class multiset
00042     : public _GLIBCXX_STD_C::multiset<_Key, _Compare, _Allocator>
00043     {
00044       typedef _GLIBCXX_STD_C::multiset<_Key, _Compare, _Allocator> _Base;
00045 
00046     public:
00047       // types:
00048       typedef _Key                   key_type;
00049       typedef _Key                   value_type;
00050       typedef _Compare                   key_compare;
00051       typedef _Compare                   value_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.3.1 construct/copy/destroy:
00067       explicit multiset(const _Compare& __comp = _Compare(),
00068             const _Allocator& __a = _Allocator())
00069       : _Base(__comp, __a) { }
00070 
00071 #if __cplusplus >= 201103L
00072       template<typename _InputIterator,
00073            typename = std::_RequireInputIter<_InputIterator>>
00074 #else
00075       template<typename _InputIterator>
00076 #endif
00077         multiset(_InputIterator __first, _InputIterator __last,
00078          const _Compare& __comp = _Compare(),
00079          const _Allocator& __a = _Allocator())
00080     : _Base(__first, __last, __comp, __a) { }
00081 
00082       multiset(const multiset& __x)
00083       : _Base(__x) { }
00084 
00085       multiset(const _Base& __x)
00086       : _Base(__x) { }
00087 
00088 #if __cplusplus >= 201103L
00089       multiset(multiset&& __x)
00090       noexcept(is_nothrow_copy_constructible<_Compare>::value)
00091       : _Base(std::move(__x))
00092       { }
00093 
00094       multiset(initializer_list<value_type> __l,
00095            const _Compare& __comp = _Compare(),
00096            const allocator_type& __a = allocator_type())
00097       : _Base(__l, __comp, __a) { }
00098 #endif
00099 
00100       ~multiset() _GLIBCXX_NOEXCEPT { }
00101 
00102       multiset&
00103       operator=(const multiset& __x)
00104       {
00105     *static_cast<_Base*>(this) = __x;
00106     return *this;
00107       }
00108 
00109 #if __cplusplus >= 201103L
00110       multiset&
00111       operator=(multiset&& __x)
00112       {
00113     // NB: DR 1204.
00114     // NB: DR 675.
00115     this->clear();
00116     this->swap(__x);
00117     return *this;
00118       }
00119 
00120       multiset&
00121       operator=(initializer_list<value_type> __l)
00122       {
00123     this->clear();
00124     this->insert(__l);
00125     return *this;
00126       }
00127 #endif
00128 
00129       using _Base::get_allocator;
00130 
00131       // iterators:
00132       iterator
00133       begin() _GLIBCXX_NOEXCEPT
00134       { return iterator(_Base::begin()); }
00135 
00136       const_iterator
00137       begin() const _GLIBCXX_NOEXCEPT
00138       { return const_iterator(_Base::begin()); }
00139 
00140       iterator
00141       end() _GLIBCXX_NOEXCEPT
00142       { return iterator(_Base::end()); }
00143 
00144       const_iterator
00145       end() const _GLIBCXX_NOEXCEPT
00146       { return const_iterator(_Base::end()); }
00147 
00148       reverse_iterator
00149       rbegin() _GLIBCXX_NOEXCEPT
00150       { return reverse_iterator(end()); }
00151 
00152       const_reverse_iterator
00153       rbegin() const _GLIBCXX_NOEXCEPT
00154       { return const_reverse_iterator(end()); }
00155 
00156       reverse_iterator
00157       rend() _GLIBCXX_NOEXCEPT
00158       { return reverse_iterator(begin()); }
00159 
00160       const_reverse_iterator
00161       rend() const _GLIBCXX_NOEXCEPT
00162       { return const_reverse_iterator(begin()); }
00163 
00164 #if __cplusplus >= 201103L
00165       const_iterator
00166       cbegin() const noexcept
00167       { return const_iterator(_Base::begin()); }
00168 
00169       const_iterator
00170       cend() const noexcept
00171       { return const_iterator(_Base::end()); }
00172 
00173       const_reverse_iterator
00174       crbegin() const noexcept
00175       { return const_reverse_iterator(end()); }
00176 
00177       const_reverse_iterator
00178       crend() const noexcept
00179       { return const_reverse_iterator(begin()); }
00180 #endif
00181 
00182       // capacity:
00183       using _Base::empty;
00184       using _Base::size;
00185       using _Base::max_size;
00186 
00187       // modifiers:
00188 #if __cplusplus >= 201103L
00189       template<typename... _Args>
00190     iterator
00191     emplace(_Args&&... __args)
00192     { return iterator(_Base::emplace(std::forward<_Args>(__args)...)); }
00193 
00194       template<typename... _Args>
00195     iterator
00196     emplace_hint(const_iterator __pos, _Args&&... __args)
00197     {
00198       return iterator(_Base::emplace_hint(__pos,
00199                           std::forward<_Args>(__args)...));
00200     }
00201 #endif
00202 
00203       iterator
00204       insert(const value_type& __x)
00205       { return iterator(_Base::insert(__x)); }
00206 
00207 #if __cplusplus >= 201103L
00208       iterator
00209       insert(value_type&& __x)
00210       { return iterator(_Base::insert(std::move(__x))); }
00211 #endif
00212 
00213       iterator
00214       insert(const_iterator __position, const value_type& __x)
00215       { return iterator(_Base::insert(__position, __x)); }
00216 
00217 #if __cplusplus >= 201103L
00218       iterator
00219       insert(const_iterator __position, value_type&& __x)
00220       { return iterator(_Base::insert(__position, std::move(__x))); }
00221 #endif
00222 
00223 #if __cplusplus >= 201103L
00224       template<typename _InputIterator,
00225            typename = std::_RequireInputIter<_InputIterator>>
00226 #else
00227       template<typename _InputIterator>
00228 #endif
00229         void
00230         insert(_InputIterator __first, _InputIterator __last)
00231         { _Base::insert(__first, __last); }
00232 
00233 #if __cplusplus >= 201103L
00234       void
00235       insert(initializer_list<value_type> __l)
00236       { _Base::insert(__l); }
00237 #endif
00238 
00239 #if __cplusplus >= 201103L
00240       iterator
00241       erase(const_iterator __position)
00242       { return iterator(_Base::erase(__position)); }
00243 #else
00244       void
00245       erase(iterator __position)
00246       { _Base::erase(__position); }
00247 #endif
00248 
00249       size_type
00250       erase(const key_type& __x)
00251       {
00252     std::pair<iterator, iterator> __victims = this->equal_range(__x);
00253     size_type __count = 0;
00254     while (__victims.first != __victims.second)
00255     {
00256       iterator __victim = __victims.first++;
00257       _Base::erase(__victim);
00258       ++__count;
00259     }
00260     return __count;
00261       }
00262 
00263 #if __cplusplus >= 201103L
00264       iterator
00265       erase(const_iterator __first, const_iterator __last)
00266       { return iterator(_Base::erase(__first, __last)); }
00267 #else
00268       void
00269       erase(iterator __first, iterator __last)
00270       { _Base::erase(__first, __last); }
00271 #endif
00272 
00273       void
00274       swap(multiset& __x)
00275       { _Base::swap(__x); }
00276 
00277       void
00278       clear() _GLIBCXX_NOEXCEPT
00279       { this->erase(begin(), end()); }
00280 
00281       // observers:
00282       using _Base::key_comp;
00283       using _Base::value_comp;
00284 
00285       // multiset operations:
00286       iterator
00287       find(const key_type& __x)
00288       { return iterator(_Base::find(__x)); }
00289 
00290       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00291       // 214. set::find() missing const overload
00292       const_iterator
00293       find(const key_type& __x) const
00294       { return const_iterator(_Base::find(__x)); }
00295 
00296       using _Base::count;
00297 
00298       iterator
00299       lower_bound(const key_type& __x)
00300       { return iterator(_Base::lower_bound(__x)); }
00301 
00302       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00303       // 214. set::find() missing const overload
00304       const_iterator
00305       lower_bound(const key_type& __x) const
00306       { return const_iterator(_Base::lower_bound(__x)); }
00307 
00308       iterator
00309       upper_bound(const key_type& __x)
00310       { return iterator(_Base::upper_bound(__x)); }
00311 
00312       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00313       // 214. set::find() missing const overload
00314       const_iterator
00315       upper_bound(const key_type& __x) const
00316       { return const_iterator(_Base::upper_bound(__x)); }
00317 
00318       std::pair<iterator,iterator>
00319       equal_range(const key_type& __x)
00320       {
00321     typedef typename _Base::iterator _Base_iterator;
00322     std::pair<_Base_iterator, _Base_iterator> __res =
00323         _Base::equal_range(__x);
00324     return std::make_pair(iterator(__res.first),
00325                   iterator(__res.second));
00326       }
00327 
00328       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00329       // 214. set::find() missing const overload
00330       std::pair<const_iterator,const_iterator>
00331       equal_range(const key_type& __x) const
00332       {
00333     typedef typename _Base::const_iterator _Base_iterator;
00334     std::pair<_Base_iterator, _Base_iterator> __res =
00335         _Base::equal_range(__x);
00336     return std::make_pair(const_iterator(__res.first),
00337                   const_iterator(__res.second));
00338       }
00339 
00340       _Base&
00341       _M_base() _GLIBCXX_NOEXCEPT       { return *this; }
00342 
00343       const _Base&
00344       _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
00345 
00346     };
00347 
00348   template<typename _Key, typename _Compare, typename _Allocator>
00349     inline bool
00350     operator==(const multiset<_Key, _Compare, _Allocator>& __lhs,
00351            const multiset<_Key, _Compare, _Allocator>& __rhs)
00352     { return __lhs._M_base() == __rhs._M_base(); }
00353 
00354   template<typename _Key, typename _Compare, typename _Allocator>
00355     inline bool
00356     operator!=(const multiset<_Key, _Compare, _Allocator>& __lhs,
00357            const multiset<_Key, _Compare, _Allocator>& __rhs)
00358     { return __lhs._M_base() != __rhs._M_base(); }
00359 
00360   template<typename _Key, typename _Compare, typename _Allocator>
00361     inline bool
00362     operator<(const multiset<_Key, _Compare, _Allocator>& __lhs,
00363           const multiset<_Key, _Compare, _Allocator>& __rhs)
00364     { return __lhs._M_base() < __rhs._M_base(); }
00365 
00366   template<typename _Key, typename _Compare, typename _Allocator>
00367     inline bool
00368     operator<=(const multiset<_Key, _Compare, _Allocator>& __lhs,
00369            const multiset<_Key, _Compare, _Allocator>& __rhs)
00370     { return __lhs._M_base() <= __rhs._M_base(); }
00371 
00372   template<typename _Key, typename _Compare, typename _Allocator>
00373     inline bool
00374     operator>=(const multiset<_Key, _Compare, _Allocator>& __lhs,
00375            const multiset<_Key, _Compare, _Allocator>& __rhs)
00376     { return __lhs._M_base() >= __rhs._M_base(); }
00377 
00378   template<typename _Key, typename _Compare, typename _Allocator>
00379     inline bool
00380     operator>(const multiset<_Key, _Compare, _Allocator>& __lhs,
00381           const multiset<_Key, _Compare, _Allocator>& __rhs)
00382     { return __lhs._M_base() > __rhs._M_base(); }
00383 
00384   template<typename _Key, typename _Compare, typename _Allocator>
00385     void
00386     swap(multiset<_Key, _Compare, _Allocator>& __x,
00387      multiset<_Key, _Compare, _Allocator>& __y)
00388     { return __x.swap(__y); }
00389 
00390 } // namespace __profile
00391 } // namespace std
00392 
00393 #endif