libstdc++
multiset.h
Go to the documentation of this file.
00001 // Debugging multiset implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2003-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 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           __glibcxx_check_valid_range(__first, __last);
00275           _Base::insert(__gnu_debug::__base(__first),
00276                         __gnu_debug::__base(__last));
00277         }
00278 
00279 #if __cplusplus >= 201103L
00280       void
00281       insert(initializer_list<value_type> __l)
00282       { _Base::insert(__l); }
00283 #endif
00284 
00285 #if __cplusplus >= 201103L
00286       iterator
00287       erase(const_iterator __position)
00288       {
00289         __glibcxx_check_erase(__position);
00290         this->_M_invalidate_if(_Equal(__position.base()));
00291         return iterator(_Base::erase(__position.base()), this);
00292       }
00293 #else
00294       void
00295       erase(iterator __position)
00296       {
00297         __glibcxx_check_erase(__position);
00298         this->_M_invalidate_if(_Equal(__position.base()));
00299         _Base::erase(__position.base());
00300       }
00301 #endif
00302 
00303       size_type
00304       erase(const key_type& __x)
00305       {
00306         std::pair<_Base_iterator, _Base_iterator> __victims =
00307           _Base::equal_range(__x);
00308         size_type __count = 0;
00309         _Base_iterator __victim = __victims.first;
00310         while (__victim != __victims.second)
00311           {
00312             this->_M_invalidate_if(_Equal(__victim));
00313             _Base::erase(__victim++);
00314             ++__count;
00315           }
00316         return __count;
00317       }
00318 
00319 #if __cplusplus >= 201103L
00320       iterator
00321       erase(const_iterator __first, const_iterator __last)
00322       {
00323         // _GLIBCXX_RESOLVE_LIB_DEFECTS
00324         // 151. can't currently clear() empty container
00325         __glibcxx_check_erase_range(__first, __last);
00326         for (_Base_const_iterator __victim = __first.base();
00327              __victim != __last.base(); ++__victim)
00328           {
00329             _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
00330                                   _M_message(__gnu_debug::__msg_valid_range)
00331                                   ._M_iterator(__first, "first")
00332                                   ._M_iterator(__last, "last"));
00333             this->_M_invalidate_if(_Equal(__victim));
00334           }
00335         return iterator(_Base::erase(__first.base(), __last.base()), this);
00336       }
00337 #else
00338       void
00339       erase(iterator __first, iterator __last)
00340       {
00341         // _GLIBCXX_RESOLVE_LIB_DEFECTS
00342         // 151. can't currently clear() empty container
00343         __glibcxx_check_erase_range(__first, __last);
00344         for (_Base_iterator __victim = __first.base();
00345              __victim != __last.base(); ++__victim)
00346           {
00347             _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
00348                                   _M_message(__gnu_debug::__msg_valid_range)
00349                                   ._M_iterator(__first, "first")
00350                                   ._M_iterator(__last, "last"));
00351             this->_M_invalidate_if(_Equal(__victim));
00352           }
00353         _Base::erase(__first.base(), __last.base());
00354       }
00355 #endif
00356 
00357       void
00358       swap(multiset& __x)
00359 #if __cplusplus >= 201103L
00360         noexcept( noexcept(declval<_Base>().swap(__x)) )
00361 #endif
00362       {
00363         _Safe::_M_swap(__x);
00364         _Base::swap(__x);
00365       }
00366 
00367       void
00368       clear() _GLIBCXX_NOEXCEPT
00369       {
00370         this->_M_invalidate_all();
00371         _Base::clear();
00372       }
00373 
00374       // observers:
00375       using _Base::key_comp;
00376       using _Base::value_comp;
00377 
00378       // multiset operations:
00379       iterator
00380       find(const key_type& __x)
00381       { return iterator(_Base::find(__x), this); }
00382 
00383       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00384       // 214. set::find() missing const overload
00385       const_iterator
00386       find(const key_type& __x) const
00387       { return const_iterator(_Base::find(__x), this); }
00388 
00389       using _Base::count;
00390 
00391       iterator
00392       lower_bound(const key_type& __x)
00393       { return iterator(_Base::lower_bound(__x), this); }
00394 
00395       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00396       // 214. set::find() missing const overload
00397       const_iterator
00398       lower_bound(const key_type& __x) const
00399       { return const_iterator(_Base::lower_bound(__x), this); }
00400 
00401       iterator
00402       upper_bound(const key_type& __x)
00403       { return iterator(_Base::upper_bound(__x), this); }
00404 
00405       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00406       // 214. set::find() missing const overload
00407       const_iterator
00408       upper_bound(const key_type& __x) const
00409       { return const_iterator(_Base::upper_bound(__x), this); }
00410 
00411       std::pair<iterator,iterator>
00412       equal_range(const key_type& __x)
00413       {
00414         std::pair<_Base_iterator, _Base_iterator> __res =
00415           _Base::equal_range(__x);
00416         return std::make_pair(iterator(__res.first, this),
00417                               iterator(__res.second, this));
00418       }
00419 
00420       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00421       // 214. set::find() missing const overload
00422       std::pair<const_iterator,const_iterator>
00423       equal_range(const key_type& __x) const
00424       {
00425         std::pair<_Base_const_iterator, _Base_const_iterator> __res =
00426           _Base::equal_range(__x);
00427         return std::make_pair(const_iterator(__res.first, this),
00428                               const_iterator(__res.second, this));
00429       }
00430 
00431       _Base&
00432       _M_base() _GLIBCXX_NOEXCEPT { return *this; }
00433 
00434       const _Base&
00435       _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
00436     };
00437 
00438   template<typename _Key, typename _Compare, typename _Allocator>
00439     inline bool
00440     operator==(const multiset<_Key, _Compare, _Allocator>& __lhs,
00441                const multiset<_Key, _Compare, _Allocator>& __rhs)
00442     { return __lhs._M_base() == __rhs._M_base(); }
00443 
00444   template<typename _Key, typename _Compare, typename _Allocator>
00445     inline bool
00446     operator!=(const multiset<_Key, _Compare, _Allocator>& __lhs,
00447                const multiset<_Key, _Compare, _Allocator>& __rhs)
00448     { return __lhs._M_base() != __rhs._M_base(); }
00449 
00450   template<typename _Key, typename _Compare, typename _Allocator>
00451     inline bool
00452     operator<(const multiset<_Key, _Compare, _Allocator>& __lhs,
00453               const multiset<_Key, _Compare, _Allocator>& __rhs)
00454     { return __lhs._M_base() < __rhs._M_base(); }
00455 
00456   template<typename _Key, typename _Compare, typename _Allocator>
00457     inline bool
00458     operator<=(const multiset<_Key, _Compare, _Allocator>& __lhs,
00459                const multiset<_Key, _Compare, _Allocator>& __rhs)
00460     { return __lhs._M_base() <= __rhs._M_base(); }
00461 
00462   template<typename _Key, typename _Compare, typename _Allocator>
00463     inline bool
00464     operator>=(const multiset<_Key, _Compare, _Allocator>& __lhs,
00465                const multiset<_Key, _Compare, _Allocator>& __rhs)
00466     { return __lhs._M_base() >= __rhs._M_base(); }
00467 
00468   template<typename _Key, typename _Compare, typename _Allocator>
00469     inline bool
00470     operator>(const multiset<_Key, _Compare, _Allocator>& __lhs,
00471               const multiset<_Key, _Compare, _Allocator>& __rhs)
00472     { return __lhs._M_base() > __rhs._M_base(); }
00473 
00474   template<typename _Key, typename _Compare, typename _Allocator>
00475     void
00476     swap(multiset<_Key, _Compare, _Allocator>& __x,
00477          multiset<_Key, _Compare, _Allocator>& __y)
00478     { return __x.swap(__y); }
00479 
00480 } // namespace __debug
00481 } // namespace std
00482 
00483 #endif