libstdc++
profile/multiset.h
Go to the documentation of this file.
00001 // Profiling multiset 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/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       template<typename _InputIterator>
00072         multiset(_InputIterator __first, _InputIterator __last,
00073          const _Compare& __comp = _Compare(),
00074          const _Allocator& __a = _Allocator())
00075     : _Base(__first, __last, __comp, __a) { }
00076 
00077       multiset(const multiset& __x)
00078       : _Base(__x) { }
00079 
00080       multiset(const _Base& __x)
00081       : _Base(__x) { }
00082 
00083 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00084       multiset(multiset&& __x)
00085       noexcept(is_nothrow_copy_constructible<_Compare>::value)
00086       : _Base(std::move(__x))
00087       { }
00088 
00089       multiset(initializer_list<value_type> __l,
00090            const _Compare& __comp = _Compare(),
00091            const allocator_type& __a = allocator_type())
00092       : _Base(__l, __comp, __a) { }
00093 #endif
00094 
00095       ~multiset() _GLIBCXX_NOEXCEPT { }
00096 
00097       multiset&
00098       operator=(const multiset& __x)
00099       {
00100     *static_cast<_Base*>(this) = __x;
00101     return *this;
00102       }
00103 
00104 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00105       multiset&
00106       operator=(multiset&& __x)
00107       {
00108     // NB: DR 1204.
00109     // NB: DR 675.
00110     this->clear();
00111     this->swap(__x);
00112     return *this;
00113       }
00114 
00115       multiset&
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       iterator
00189       insert(value_type&& __x)
00190       { return iterator(_Base::insert(std::move(__x))); }
00191 #endif
00192 
00193       iterator
00194       insert(const_iterator __position, const value_type& __x)
00195       { return iterator(_Base::insert(__position, __x)); }
00196 
00197 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00198       iterator
00199       insert(const_iterator __position, value_type&& __x)
00200       { return iterator(_Base::insert(__position, std::move(__x))); }
00201 #endif
00202 
00203       template<typename _InputIterator>
00204         void
00205         insert(_InputIterator __first, _InputIterator __last)
00206         { _Base::insert(__first, __last); }
00207 
00208 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00209       void
00210       insert(initializer_list<value_type> __l)
00211       { _Base::insert(__l); }
00212 #endif
00213 
00214 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00215       iterator
00216       erase(const_iterator __position)
00217       { return iterator(_Base::erase(__position)); }
00218 #else
00219       void
00220       erase(iterator __position)
00221       { _Base::erase(__position); }
00222 #endif
00223 
00224       size_type
00225       erase(const key_type& __x)
00226       {
00227     std::pair<iterator, iterator> __victims = this->equal_range(__x);
00228     size_type __count = 0;
00229     while (__victims.first != __victims.second)
00230     {
00231       iterator __victim = __victims.first++;
00232       _Base::erase(__victim);
00233       ++__count;
00234     }
00235     return __count;
00236       }
00237 
00238 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00239       iterator
00240       erase(const_iterator __first, const_iterator __last)
00241       { return iterator(_Base::erase(__first, __last)); }
00242 #else
00243       void
00244       erase(iterator __first, iterator __last)
00245       { _Base::erase(__first, __last); }
00246 #endif
00247 
00248       void
00249       swap(multiset& __x)
00250       { _Base::swap(__x); }
00251 
00252       void
00253       clear() _GLIBCXX_NOEXCEPT
00254       { this->erase(begin(), end()); }
00255 
00256       // observers:
00257       using _Base::key_comp;
00258       using _Base::value_comp;
00259 
00260       // multiset operations:
00261       iterator
00262       find(const key_type& __x)
00263       { return iterator(_Base::find(__x)); }
00264 
00265       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00266       // 214. set::find() missing const overload
00267       const_iterator
00268       find(const key_type& __x) const
00269       { return const_iterator(_Base::find(__x)); }
00270 
00271       using _Base::count;
00272 
00273       iterator
00274       lower_bound(const key_type& __x)
00275       { return iterator(_Base::lower_bound(__x)); }
00276 
00277       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00278       // 214. set::find() missing const overload
00279       const_iterator
00280       lower_bound(const key_type& __x) const
00281       { return const_iterator(_Base::lower_bound(__x)); }
00282 
00283       iterator
00284       upper_bound(const key_type& __x)
00285       { return iterator(_Base::upper_bound(__x)); }
00286 
00287       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00288       // 214. set::find() missing const overload
00289       const_iterator
00290       upper_bound(const key_type& __x) const
00291       { return const_iterator(_Base::upper_bound(__x)); }
00292 
00293       std::pair<iterator,iterator>
00294       equal_range(const key_type& __x)
00295       {
00296     typedef typename _Base::iterator _Base_iterator;
00297     std::pair<_Base_iterator, _Base_iterator> __res =
00298         _Base::equal_range(__x);
00299     return std::make_pair(iterator(__res.first),
00300                   iterator(__res.second));
00301       }
00302 
00303       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00304       // 214. set::find() missing const overload
00305       std::pair<const_iterator,const_iterator>
00306       equal_range(const key_type& __x) const
00307       {
00308     typedef typename _Base::const_iterator _Base_iterator;
00309     std::pair<_Base_iterator, _Base_iterator> __res =
00310         _Base::equal_range(__x);
00311     return std::make_pair(const_iterator(__res.first),
00312                   const_iterator(__res.second));
00313       }
00314 
00315       _Base&
00316       _M_base() _GLIBCXX_NOEXCEPT       { return *this; }
00317 
00318       const _Base&
00319       _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
00320 
00321     };
00322 
00323   template<typename _Key, typename _Compare, typename _Allocator>
00324     inline bool
00325     operator==(const multiset<_Key, _Compare, _Allocator>& __lhs,
00326            const multiset<_Key, _Compare, _Allocator>& __rhs)
00327     { return __lhs._M_base() == __rhs._M_base(); }
00328 
00329   template<typename _Key, typename _Compare, typename _Allocator>
00330     inline bool
00331     operator!=(const multiset<_Key, _Compare, _Allocator>& __lhs,
00332            const multiset<_Key, _Compare, _Allocator>& __rhs)
00333     { return __lhs._M_base() != __rhs._M_base(); }
00334 
00335   template<typename _Key, typename _Compare, typename _Allocator>
00336     inline bool
00337     operator<(const multiset<_Key, _Compare, _Allocator>& __lhs,
00338           const multiset<_Key, _Compare, _Allocator>& __rhs)
00339     { return __lhs._M_base() < __rhs._M_base(); }
00340 
00341   template<typename _Key, typename _Compare, typename _Allocator>
00342     inline bool
00343     operator<=(const multiset<_Key, _Compare, _Allocator>& __lhs,
00344            const multiset<_Key, _Compare, _Allocator>& __rhs)
00345     { return __lhs._M_base() <= __rhs._M_base(); }
00346 
00347   template<typename _Key, typename _Compare, typename _Allocator>
00348     inline bool
00349     operator>=(const multiset<_Key, _Compare, _Allocator>& __lhs,
00350            const multiset<_Key, _Compare, _Allocator>& __rhs)
00351     { return __lhs._M_base() >= __rhs._M_base(); }
00352 
00353   template<typename _Key, typename _Compare, typename _Allocator>
00354     inline bool
00355     operator>(const multiset<_Key, _Compare, _Allocator>& __lhs,
00356           const multiset<_Key, _Compare, _Allocator>& __rhs)
00357     { return __lhs._M_base() > __rhs._M_base(); }
00358 
00359   template<typename _Key, typename _Compare, typename _Allocator>
00360     void
00361     swap(multiset<_Key, _Compare, _Allocator>& __x,
00362      multiset<_Key, _Compare, _Allocator>& __y)
00363     { return __x.swap(__y); }
00364 
00365 } // namespace __profile
00366 } // namespace std
00367 
00368 #endif