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