libstdc++
multiset.h
Go to the documentation of this file.
00001 // Profiling multiset implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2009-2015 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 <profile/base.h>
00033 #include <profile/ordered_base.h>
00034 
00035 namespace std _GLIBCXX_VISIBILITY(default)
00036 {
00037 namespace __profile
00038 {
00039   /// Class std::multiset wrapper with performance instrumentation.
00040   template<typename _Key, typename _Compare = std::less<_Key>,
00041            typename _Allocator = std::allocator<_Key> >
00042     class multiset
00043     : public _GLIBCXX_STD_C::multiset<_Key, _Compare, _Allocator>,
00044       public _Ordered_profile<multiset<_Key, _Compare, _Allocator> >
00045     {
00046       typedef _GLIBCXX_STD_C::multiset<_Key, _Compare, _Allocator> _Base;
00047 
00048       typedef typename _Base::iterator                  _Base_iterator;
00049       typedef typename _Base::const_iterator            _Base_const_iterator;
00050 
00051     public:
00052       // types:
00053       typedef _Key                                      key_type;
00054       typedef _Key                                      value_type;
00055       typedef _Compare                                  key_compare;
00056       typedef _Compare                                  value_compare;
00057       typedef _Allocator                                allocator_type;
00058       typedef typename _Base::reference                 reference;
00059       typedef typename _Base::const_reference           const_reference;
00060 
00061       typedef __iterator_tracker<_Base_iterator,
00062                                  multiset>              iterator;
00063       typedef __iterator_tracker<_Base_const_iterator,
00064                                  multiset>              const_iterator;
00065       typedef std::reverse_iterator<iterator>           reverse_iterator;
00066       typedef std::reverse_iterator<const_iterator>     const_reverse_iterator;
00067 
00068       typedef typename _Base::size_type                 size_type;
00069       typedef typename _Base::difference_type           difference_type;
00070 
00071       // 23.3.3.1 construct/copy/destroy:
00072 
00073 #if __cplusplus < 201103L
00074       multiset()
00075       : _Base() { }
00076       multiset(const multiset& __x)
00077       : _Base(__x) { }
00078       ~multiset() { }
00079 #else
00080       multiset() = default;
00081       multiset(const multiset&) = default;
00082       multiset(multiset&&) = default;
00083       ~multiset() = default;
00084 #endif
00085 
00086       explicit multiset(const _Compare& __comp,
00087                         const _Allocator& __a = _Allocator())
00088       : _Base(__comp, __a) { }
00089 
00090 #if __cplusplus >= 201103L
00091       template<typename _InputIterator,
00092                typename = std::_RequireInputIter<_InputIterator>>
00093 #else
00094       template<typename _InputIterator>
00095 #endif
00096         multiset(_InputIterator __first, _InputIterator __last,
00097                  const _Compare& __comp = _Compare(),
00098                  const _Allocator& __a = _Allocator())
00099         : _Base(__first, __last, __comp, __a) { }
00100 
00101 #if __cplusplus >= 201103L
00102       multiset(initializer_list<value_type> __l,
00103                const _Compare& __comp = _Compare(),
00104                const allocator_type& __a = allocator_type())
00105       : _Base(__l, __comp, __a) { }
00106 
00107       explicit
00108       multiset(const allocator_type& __a)
00109       : _Base(__a) { }
00110 
00111       multiset(const multiset& __x, const allocator_type& __a)
00112       : _Base(__x, __a) { }
00113 
00114       multiset(multiset&& __x, const allocator_type& __a)
00115       noexcept( noexcept(_Base(std::move(__x), __a)) )
00116       : _Base(std::move(__x), __a) { }
00117 
00118       multiset(initializer_list<value_type> __l, const allocator_type& __a)
00119       : _Base(__l, __a) { }
00120 
00121       template<typename _InputIterator>
00122         multiset(_InputIterator __first, _InputIterator __last,
00123                  const allocator_type& __a)
00124         : _Base(__first, __last, __a) { }
00125 #endif
00126 
00127       multiset(const _Base& __x)
00128       : _Base(__x) { }
00129 
00130 #if __cplusplus < 201103L
00131       multiset&
00132       operator=(const multiset& __x)
00133       {
00134         this->_M_profile_destruct();
00135         _M_base() = __x;
00136         this->_M_profile_construct();
00137         return *this;
00138       }
00139 #else
00140       multiset&
00141       operator=(const multiset&) = default;
00142 
00143       multiset&
00144       operator=(multiset&&) = default;
00145 
00146       multiset&
00147       operator=(initializer_list<value_type> __l)
00148       {
00149         this->_M_profile_destruct();
00150         _M_base() = __l;
00151         this->_M_profile_construct();
00152         return *this;
00153       }
00154 #endif
00155 
00156       // iterators
00157       iterator
00158       begin() _GLIBCXX_NOEXCEPT
00159       { return iterator(_Base::begin(), this); }
00160 
00161       const_iterator
00162       begin() const _GLIBCXX_NOEXCEPT
00163       { return const_iterator(_Base::begin(), this); }
00164 
00165       iterator
00166       end() _GLIBCXX_NOEXCEPT
00167       { return iterator(_Base::end(), this); }
00168 
00169       const_iterator
00170       end() const _GLIBCXX_NOEXCEPT
00171       { return const_iterator(_Base::end(), this); }
00172 
00173 #if __cplusplus >= 201103L
00174       const_iterator
00175       cbegin() const noexcept
00176       { return const_iterator(_Base::cbegin(), this); }
00177 
00178       const_iterator
00179       cend() const noexcept
00180       { return const_iterator(_Base::cend(), this); }
00181 #endif
00182 
00183       reverse_iterator
00184       rbegin() _GLIBCXX_NOEXCEPT
00185       {
00186         __profcxx_map2umap_invalidate(this->_M_map2umap_info);
00187         return reverse_iterator(end());
00188       }
00189 
00190       const_reverse_iterator
00191       rbegin() const _GLIBCXX_NOEXCEPT
00192       {
00193         __profcxx_map2umap_invalidate(this->_M_map2umap_info);
00194         return const_reverse_iterator(end());
00195       }
00196 
00197       reverse_iterator
00198       rend() _GLIBCXX_NOEXCEPT
00199       {
00200         __profcxx_map2umap_invalidate(this->_M_map2umap_info);
00201         return reverse_iterator(begin());
00202       }
00203 
00204       const_reverse_iterator
00205       rend() const _GLIBCXX_NOEXCEPT
00206       {
00207         __profcxx_map2umap_invalidate(this->_M_map2umap_info);
00208         return const_reverse_iterator(begin());
00209       }
00210 
00211 #if __cplusplus >= 201103L
00212       const_reverse_iterator
00213       crbegin() const noexcept
00214       {
00215         __profcxx_map2umap_invalidate(this->_M_map2umap_info);
00216         return const_reverse_iterator(cend());
00217       }
00218 
00219       const_reverse_iterator
00220       crend() const noexcept
00221       {
00222         __profcxx_map2umap_invalidate(this->_M_map2umap_info);
00223         return const_reverse_iterator(cbegin());
00224       }
00225 #endif
00226 
00227       void
00228       swap(multiset& __x)
00229 #if __cplusplus >= 201103L
00230         noexcept( noexcept(declval<_Base>().swap(__x)) )
00231 #endif
00232       {
00233         _Base::swap(__x);
00234         this->_M_swap(__x);
00235       }
00236 
00237       // modifiers:
00238 #if __cplusplus >= 201103L
00239       template<typename... _Args>
00240         iterator
00241         emplace(_Args&&... __args)
00242         {
00243           // The cost is the same whether or not the element is inserted so we
00244           // always report insertion of 1 element.
00245           __profcxx_map2umap_insert(this->_M_map2umap_info, this->size(), 1);
00246           return iterator(_Base::emplace(std::forward<_Args>(__args)...), this);
00247         }
00248 
00249       template<typename... _Args>
00250         iterator
00251         emplace_hint(const_iterator __pos, _Args&&... __args)
00252         {
00253           auto size_before = this->size();
00254           auto __res
00255             = _Base::emplace_hint(__pos.base(), std::forward<_Args>(__args)...);
00256           __profcxx_map2umap_insert(this->_M_map2umap_info,
00257                 size_before, _M_hint_used(__pos.base(), __res) ? 0 : 1);
00258           return iterator(__res, this);
00259         }
00260 #endif
00261 
00262       iterator
00263       insert(const value_type& __x)
00264       {
00265         __profcxx_map2umap_insert(this->_M_map2umap_info, this->size(), 1);
00266         return iterator(_Base::insert(__x), this);
00267       }
00268 
00269 #if __cplusplus >= 201103L
00270       iterator
00271       insert(value_type&& __x)
00272       {
00273         __profcxx_map2umap_insert(this->_M_map2umap_info, this->size(), 1);
00274         return iterator(_Base::insert(std::move(__x)), this);
00275       }
00276 #endif
00277 
00278       iterator
00279       insert(const_iterator __pos, const value_type& __x)
00280       {
00281         size_type size_before = this->size();
00282         _Base_iterator __res = _Base::insert(__pos.base(), __x);
00283         
00284         __profcxx_map2umap_insert(this->_M_map2umap_info,
00285                 size_before, _M_hint_used(__pos.base(), __res) ? 0 : 1);
00286         return iterator(__res, this);
00287       }
00288 
00289 #if __cplusplus >= 201103L
00290       iterator
00291       insert(const_iterator __pos, value_type&& __x)
00292       {
00293         auto size_before = this->size();
00294         auto __res = _Base::insert(__pos.base(), std::move(__x));
00295         __profcxx_map2umap_insert(this->_M_map2umap_info,
00296                 size_before, _M_hint_used(__pos.base(), __res) ? 0 : 1);
00297         return iterator(__res, this);
00298       }
00299 #endif
00300 
00301 #if __cplusplus >= 201103L
00302       template<typename _InputIterator,
00303                typename = std::_RequireInputIter<_InputIterator>>
00304 #else
00305       template<typename _InputIterator>
00306 #endif
00307         void
00308         insert(_InputIterator __first, _InputIterator __last)
00309         {
00310           for (; __first != __last; ++__first)
00311             insert(*__first);
00312         }
00313 
00314 #if __cplusplus >= 201103L
00315       void
00316       insert(initializer_list<value_type> __l)
00317       { insert(__l.begin(), __l.end()); }
00318 #endif
00319 
00320 #if __cplusplus >= 201103L
00321       iterator
00322       erase(const_iterator __pos)
00323       {
00324         __profcxx_map2umap_erase(this->_M_map2umap_info, this->size(), 1);
00325         return iterator(_Base::erase(__pos.base()), this);
00326       }
00327 #else
00328       void
00329       erase(iterator __pos)
00330       {
00331         __profcxx_map2umap_erase(this->_M_map2umap_info, this->size(), 1);
00332         _Base::erase(__pos.base());
00333       }
00334 #endif
00335 
00336       size_type
00337       erase(const key_type& __x)
00338       {
00339         __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
00340         __profcxx_map2umap_erase(this->_M_map2umap_info, this->size(), 1);
00341         return _Base::erase(__x);
00342       }
00343 
00344 #if __cplusplus >= 201103L
00345       iterator
00346       erase(const_iterator __first, const_iterator __last)
00347       {
00348         if (__first != __last)
00349           {
00350             iterator __ret;
00351             for (; __first != __last;)
00352               __ret = erase(__first++);
00353             return __ret;
00354           }
00355         else
00356           return iterator(_Base::erase(__first.base(), __last.base()), this);
00357       }
00358 #else
00359       void
00360       erase(iterator __first, iterator __last)
00361       {
00362         for (; __first != __last;)
00363           erase(__first++);
00364       }
00365 #endif
00366 
00367       void
00368       clear() _GLIBCXX_NOEXCEPT
00369       {
00370         this->_M_profile_destruct();
00371         _Base::clear();
00372         this->_M_profile_construct();
00373       }
00374  
00375       size_type
00376       count(const key_type& __x) const
00377       {
00378         __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
00379         return _Base::count(__x);
00380       }
00381 
00382       // multiset operations:
00383       iterator
00384       find(const key_type& __x)
00385       {
00386         __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
00387         return iterator(_Base::find(__x), this);
00388       }
00389 
00390       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00391       // 214. set::find() missing const overload
00392       const_iterator
00393       find(const key_type& __x) const
00394       {
00395         __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
00396         return const_iterator(_Base::find(__x), this);
00397       }
00398 
00399       iterator
00400       lower_bound(const key_type& __x)
00401       {
00402         __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
00403         return iterator(_Base::lower_bound(__x), this);
00404       }
00405 
00406       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00407       // 214. set::find() missing const overload
00408       const_iterator
00409       lower_bound(const key_type& __x) const
00410       {
00411         __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
00412         __profcxx_map2umap_invalidate(this->_M_map2umap_info);
00413         return const_iterator(_Base::lower_bound(__x), this);
00414       }
00415 
00416       iterator
00417       upper_bound(const key_type& __x)
00418       {
00419         __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
00420         __profcxx_map2umap_invalidate(this->_M_map2umap_info);
00421         return iterator(_Base::upper_bound(__x), this);
00422       }
00423 
00424       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00425       // 214. set::find() missing const overload
00426       const_iterator
00427       upper_bound(const key_type& __x) const
00428       {
00429         __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
00430         __profcxx_map2umap_invalidate(this->_M_map2umap_info);
00431         return const_iterator(_Base::upper_bound(__x), this);
00432       }
00433 
00434       std::pair<iterator,iterator>
00435       equal_range(const key_type& __x)
00436       {
00437         __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
00438         std::pair<_Base_iterator, _Base_iterator> __base_ret
00439           = _Base::equal_range(__x);
00440         return std::make_pair(iterator(__base_ret.first, this),
00441                               iterator(__base_ret.second, this));
00442       }
00443 
00444       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00445       // 214. set::find() missing const overload
00446       std::pair<const_iterator,const_iterator>
00447       equal_range(const key_type& __x) const
00448       {
00449         __profcxx_map2umap_find(this->_M_map2umap_info, this->size());
00450         std::pair<_Base_const_iterator, _Base_const_iterator> __base_ret
00451           = _Base::equal_range(__x);
00452         return std::make_pair(const_iterator(__base_ret.first, this),
00453                               const_iterator(__base_ret.second, this));
00454       }
00455 
00456       _Base&
00457       _M_base() _GLIBCXX_NOEXCEPT       { return *this; }
00458 
00459       const _Base&
00460       _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
00461 
00462     private:
00463       /** If hint is used we consider that the map and unordered_map
00464        * operations have equivalent insertion cost so we do not update metrics
00465        * about it.
00466        * Note that to find out if hint has been used is libstdc++
00467        * implementation dependent.
00468        */
00469       bool
00470       _M_hint_used(_Base_const_iterator __hint, _Base_iterator __res)
00471       {
00472         return (__hint == __res
00473                 || (__hint == _M_base().end() && ++__res == _M_base().end())
00474                 || (__hint != _M_base().end() && (++__hint == __res
00475                                                   || ++__res == --__hint)));
00476       }
00477 
00478       template<typename _K1, typename _C1, typename _A1>
00479         friend bool
00480         operator==(const multiset<_K1, _C1, _A1>&,
00481                    const multiset<_K1, _C1, _A1>&);
00482 
00483       template<typename _K1, typename _C1, typename _A1>
00484         friend bool
00485         operator< (const multiset<_K1, _C1, _A1>&,
00486                    const multiset<_K1, _C1, _A1>&);
00487     };
00488 
00489   template<typename _Key, typename _Compare, typename _Allocator>
00490     inline bool
00491     operator==(const multiset<_Key, _Compare, _Allocator>& __lhs,
00492                const multiset<_Key, _Compare, _Allocator>& __rhs)
00493     {
00494       __profcxx_map2umap_invalidate(__lhs._M_map2umap_info);
00495       __profcxx_map2umap_invalidate(__rhs._M_map2umap_info);
00496       return __lhs._M_base() == __rhs._M_base();
00497     }
00498 
00499   template<typename _Key, typename _Compare, typename _Allocator>
00500     inline bool
00501     operator<(const multiset<_Key, _Compare, _Allocator>& __lhs,
00502               const multiset<_Key, _Compare, _Allocator>& __rhs)
00503     {
00504       __profcxx_map2umap_invalidate(__lhs._M_map2umap_info);
00505       __profcxx_map2umap_invalidate(__rhs._M_map2umap_info);
00506       return __lhs._M_base() < __rhs._M_base();
00507     }
00508 
00509   template<typename _Key, typename _Compare, typename _Allocator>
00510     inline bool
00511     operator!=(const multiset<_Key, _Compare, _Allocator>& __lhs,
00512                const multiset<_Key, _Compare, _Allocator>& __rhs)
00513     { return !(__lhs == __rhs); }
00514 
00515   template<typename _Key, typename _Compare, typename _Allocator>
00516     inline bool
00517     operator<=(const multiset<_Key, _Compare, _Allocator>& __lhs,
00518                const multiset<_Key, _Compare, _Allocator>& __rhs)
00519     { return !(__rhs < __lhs); }
00520 
00521   template<typename _Key, typename _Compare, typename _Allocator>
00522     inline bool
00523     operator>=(const multiset<_Key, _Compare, _Allocator>& __lhs,
00524                const multiset<_Key, _Compare, _Allocator>& __rhs)
00525     { return !(__lhs < __rhs); }
00526 
00527   template<typename _Key, typename _Compare, typename _Allocator>
00528     inline bool
00529     operator>(const multiset<_Key, _Compare, _Allocator>& __lhs,
00530               const multiset<_Key, _Compare, _Allocator>& __rhs)
00531     { return __rhs < __lhs; }
00532 
00533   template<typename _Key, typename _Compare, typename _Allocator>
00534     void
00535     swap(multiset<_Key, _Compare, _Allocator>& __x,
00536          multiset<_Key, _Compare, _Allocator>& __y)
00537     { return __x.swap(__y); }
00538 
00539 } // namespace __profile
00540 } // namespace std
00541 
00542 #endif