libstdc++
multiset.h
Go to the documentation of this file.
00001 // Debugging multiset implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2003-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 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_iterator.h>
00034 #include <utility>
00035 
00036 namespace std _GLIBCXX_VISIBILITY(default)
00037 {
00038 namespace __debug
00039 {
00040   /// Class std::multiset with safety/checking/debug instrumentation.
00041   template<typename _Key, typename _Compare = std::less<_Key>,
00042        typename _Allocator = std::allocator<_Key> >
00043     class multiset
00044     : public _GLIBCXX_STD_C::multiset<_Key, _Compare, _Allocator>,
00045       public __gnu_debug::_Safe_sequence<multiset<_Key, _Compare, _Allocator> >
00046     {
00047       typedef _GLIBCXX_STD_C::multiset<_Key, _Compare, _Allocator> _Base;
00048 
00049       typedef typename _Base::const_iterator _Base_const_iterator;
00050       typedef typename _Base::iterator _Base_iterator;
00051       typedef __gnu_debug::_Equal_to<_Base_const_iterator> _Equal;
00052     public:
00053       // types:
00054       typedef _Key                   key_type;
00055       typedef _Key                   value_type;
00056       typedef _Compare                   key_compare;
00057       typedef _Compare                   value_compare;
00058       typedef _Allocator                 allocator_type;
00059       typedef typename _Base::reference              reference;
00060       typedef typename _Base::const_reference        const_reference;
00061 
00062       typedef __gnu_debug::_Safe_iterator<_Base_iterator, multiset>
00063       iterator;
00064       typedef __gnu_debug::_Safe_iterator<_Base_const_iterator,
00065                       multiset> const_iterator;
00066 
00067       typedef typename _Base::size_type              size_type;
00068       typedef typename _Base::difference_type        difference_type;
00069       typedef typename _Base::pointer                pointer;
00070       typedef typename _Base::const_pointer          const_pointer;
00071       typedef std::reverse_iterator<iterator>        reverse_iterator;
00072       typedef std::reverse_iterator<const_iterator>  const_reverse_iterator;
00073 
00074       // 23.3.3.1 construct/copy/destroy:
00075       explicit multiset(const _Compare& __comp = _Compare(),
00076             const _Allocator& __a = _Allocator())
00077       : _Base(__comp, __a) { }
00078 
00079       template<typename _InputIterator>
00080         multiset(_InputIterator __first, _InputIterator __last,
00081          const _Compare& __comp = _Compare(),
00082          const _Allocator& __a = _Allocator())
00083     : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
00084                                      __last)),
00085         __gnu_debug::__base(__last),
00086         __comp, __a) { }
00087 
00088       multiset(const multiset& __x)
00089       : _Base(__x) { }
00090 
00091       multiset(const _Base& __x)
00092       : _Base(__x) { }
00093 
00094 #if __cplusplus >= 201103L
00095       multiset(multiset&& __x)
00096       noexcept(is_nothrow_copy_constructible<_Compare>::value)
00097       : _Base(std::move(__x))
00098       { this->_M_swap(__x); }
00099 
00100       multiset(initializer_list<value_type> __l,
00101            const _Compare& __comp = _Compare(),
00102            const allocator_type& __a = allocator_type())
00103       : _Base(__l, __comp, __a) { }
00104 #endif
00105 
00106       ~multiset() _GLIBCXX_NOEXCEPT { }
00107 
00108       multiset&
00109       operator=(const multiset& __x)
00110       {
00111     *static_cast<_Base*>(this) = __x;
00112     this->_M_invalidate_all();
00113     return *this;
00114       }
00115 
00116 #if __cplusplus >= 201103L
00117       multiset&
00118       operator=(multiset&& __x)
00119       {
00120     // NB: DR 1204.
00121     // NB: DR 675.
00122     __glibcxx_check_self_move_assign(__x);
00123     clear();
00124     swap(__x);
00125     return *this;
00126       }
00127 
00128       multiset&
00129       operator=(initializer_list<value_type> __l)
00130       {
00131     this->clear();
00132     this->insert(__l);
00133     return *this;
00134       }
00135 #endif
00136 
00137       using _Base::get_allocator;
00138 
00139       // iterators:
00140       iterator
00141       begin() _GLIBCXX_NOEXCEPT
00142       { return iterator(_Base::begin(), this); }
00143 
00144       const_iterator
00145       begin() const _GLIBCXX_NOEXCEPT
00146       { return const_iterator(_Base::begin(), this); }
00147 
00148       iterator
00149       end() _GLIBCXX_NOEXCEPT
00150       { return iterator(_Base::end(), this); }
00151 
00152       const_iterator
00153       end() const _GLIBCXX_NOEXCEPT
00154       { return const_iterator(_Base::end(), this); }
00155 
00156       reverse_iterator
00157       rbegin() _GLIBCXX_NOEXCEPT
00158       { return reverse_iterator(end()); }
00159 
00160       const_reverse_iterator
00161       rbegin() const _GLIBCXX_NOEXCEPT
00162       { return const_reverse_iterator(end()); }
00163 
00164       reverse_iterator
00165       rend() _GLIBCXX_NOEXCEPT
00166       { return reverse_iterator(begin()); }
00167 
00168       const_reverse_iterator
00169       rend() const _GLIBCXX_NOEXCEPT
00170       { return const_reverse_iterator(begin()); }
00171 
00172 #if __cplusplus >= 201103L
00173       const_iterator
00174       cbegin() const noexcept
00175       { return const_iterator(_Base::begin(), this); }
00176 
00177       const_iterator
00178       cend() const noexcept
00179       { return const_iterator(_Base::end(), this); }
00180 
00181       const_reverse_iterator
00182       crbegin() const noexcept
00183       { return const_reverse_iterator(end()); }
00184 
00185       const_reverse_iterator
00186       crend() const noexcept
00187       { return const_reverse_iterator(begin()); }
00188 #endif
00189 
00190       // capacity:
00191       using _Base::empty;
00192       using _Base::size;
00193       using _Base::max_size;
00194 
00195       // modifiers:
00196 #if __cplusplus >= 201103L
00197       template<typename... _Args>
00198     iterator
00199     emplace(_Args&&... __args)
00200     {
00201       return iterator(_Base::emplace(std::forward<_Args>(__args)...), this);
00202     }
00203 
00204       template<typename... _Args>
00205     iterator
00206     emplace_hint(const_iterator __pos, _Args&&... __args)
00207     {
00208       __glibcxx_check_insert(__pos);
00209       return iterator(_Base::emplace_hint(__pos.base(),
00210                           std::forward<_Args>(__args)...),
00211               this);
00212     }
00213 #endif
00214 
00215       iterator
00216       insert(const value_type& __x)
00217       { return iterator(_Base::insert(__x), this); }
00218 
00219 #if __cplusplus >= 201103L
00220       iterator
00221       insert(value_type&& __x)
00222       { return iterator(_Base::insert(std::move(__x)), this); }
00223 #endif
00224 
00225       iterator
00226       insert(const_iterator __position, const value_type& __x)
00227       {
00228     __glibcxx_check_insert(__position);
00229     return iterator(_Base::insert(__position.base(), __x), this);
00230       }
00231 
00232 #if __cplusplus >= 201103L
00233       iterator
00234       insert(const_iterator __position, value_type&& __x)
00235       {
00236     __glibcxx_check_insert(__position);
00237     return iterator(_Base::insert(__position.base(), std::move(__x)),
00238             this);
00239       }
00240 #endif
00241 
00242       template<typename _InputIterator>
00243     void
00244     insert(_InputIterator __first, _InputIterator __last)
00245     {
00246       __glibcxx_check_valid_range(__first, __last);
00247       _Base::insert(__gnu_debug::__base(__first),
00248             __gnu_debug::__base(__last));
00249     }
00250 
00251 #if __cplusplus >= 201103L
00252       void
00253       insert(initializer_list<value_type> __l)
00254       { _Base::insert(__l); }
00255 #endif
00256 
00257 #if __cplusplus >= 201103L
00258       iterator
00259       erase(const_iterator __position)
00260       {
00261     __glibcxx_check_erase(__position);
00262     this->_M_invalidate_if(_Equal(__position.base()));
00263     return iterator(_Base::erase(__position.base()), this);
00264       }
00265 #else
00266       void
00267       erase(iterator __position)
00268       {
00269     __glibcxx_check_erase(__position);
00270     this->_M_invalidate_if(_Equal(__position.base()));
00271     _Base::erase(__position.base());
00272       }
00273 #endif
00274 
00275       size_type
00276       erase(const key_type& __x)
00277       {
00278     std::pair<_Base_iterator, _Base_iterator> __victims =
00279       _Base::equal_range(__x);
00280     size_type __count = 0;
00281     _Base_iterator __victim = __victims.first;
00282     while (__victim != __victims.second)
00283       {
00284         this->_M_invalidate_if(_Equal(__victim));
00285         _Base::erase(__victim++);
00286         ++__count;
00287       }
00288     return __count;
00289       }
00290 
00291 #if __cplusplus >= 201103L
00292       iterator
00293       erase(const_iterator __first, const_iterator __last)
00294       {
00295     // _GLIBCXX_RESOLVE_LIB_DEFECTS
00296     // 151. can't currently clear() empty container
00297     __glibcxx_check_erase_range(__first, __last);
00298     for (_Base_const_iterator __victim = __first.base();
00299          __victim != __last.base(); ++__victim)
00300       {
00301         _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
00302                   _M_message(__gnu_debug::__msg_valid_range)
00303                   ._M_iterator(__first, "first")
00304                   ._M_iterator(__last, "last"));
00305         this->_M_invalidate_if(_Equal(__victim));
00306       }
00307     return iterator(_Base::erase(__first.base(), __last.base()), this);
00308       }
00309 #else
00310       void
00311       erase(iterator __first, iterator __last)
00312       {
00313     // _GLIBCXX_RESOLVE_LIB_DEFECTS
00314     // 151. can't currently clear() empty container
00315     __glibcxx_check_erase_range(__first, __last);
00316     for (_Base_iterator __victim = __first.base();
00317          __victim != __last.base(); ++__victim)
00318       {
00319         _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
00320                   _M_message(__gnu_debug::__msg_valid_range)
00321                   ._M_iterator(__first, "first")
00322                   ._M_iterator(__last, "last"));
00323         this->_M_invalidate_if(_Equal(__victim));
00324       }
00325     _Base::erase(__first.base(), __last.base());
00326       }
00327 #endif
00328 
00329       void
00330       swap(multiset& __x)
00331       {
00332     _Base::swap(__x);
00333     this->_M_swap(__x);
00334       }
00335 
00336       void
00337       clear() _GLIBCXX_NOEXCEPT
00338       {
00339     this->_M_invalidate_all();
00340     _Base::clear();
00341       }
00342 
00343       // observers:
00344       using _Base::key_comp;
00345       using _Base::value_comp;
00346 
00347       // multiset operations:
00348       iterator
00349       find(const key_type& __x)
00350       { return iterator(_Base::find(__x), this); }
00351 
00352       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00353       // 214. set::find() missing const overload
00354       const_iterator
00355       find(const key_type& __x) const
00356       { return const_iterator(_Base::find(__x), this); }
00357 
00358       using _Base::count;
00359 
00360       iterator
00361       lower_bound(const key_type& __x)
00362       { return iterator(_Base::lower_bound(__x), this); }
00363 
00364       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00365       // 214. set::find() missing const overload
00366       const_iterator
00367       lower_bound(const key_type& __x) const
00368       { return const_iterator(_Base::lower_bound(__x), this); }
00369 
00370       iterator
00371       upper_bound(const key_type& __x)
00372       { return iterator(_Base::upper_bound(__x), this); }
00373 
00374       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00375       // 214. set::find() missing const overload
00376       const_iterator
00377       upper_bound(const key_type& __x) const
00378       { return const_iterator(_Base::upper_bound(__x), this); }
00379 
00380       std::pair<iterator,iterator>
00381       equal_range(const key_type& __x)
00382       {
00383     std::pair<_Base_iterator, _Base_iterator> __res =
00384       _Base::equal_range(__x);
00385     return std::make_pair(iterator(__res.first, this),
00386                   iterator(__res.second, this));
00387       }
00388 
00389       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00390       // 214. set::find() missing const overload
00391       std::pair<const_iterator,const_iterator>
00392       equal_range(const key_type& __x) const
00393       {
00394     std::pair<_Base_const_iterator, _Base_const_iterator> __res =
00395       _Base::equal_range(__x);
00396     return std::make_pair(const_iterator(__res.first, this),
00397                   const_iterator(__res.second, this));
00398       }
00399 
00400       _Base&
00401       _M_base() _GLIBCXX_NOEXCEPT       { return *this; }
00402 
00403       const _Base&
00404       _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
00405 
00406     private:
00407       void
00408       _M_invalidate_all()
00409       {
00410     typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
00411     this->_M_invalidate_if(_Not_equal(_Base::end()));
00412       }
00413     };
00414 
00415   template<typename _Key, typename _Compare, typename _Allocator>
00416     inline bool
00417     operator==(const multiset<_Key, _Compare, _Allocator>& __lhs,
00418            const multiset<_Key, _Compare, _Allocator>& __rhs)
00419     { return __lhs._M_base() == __rhs._M_base(); }
00420 
00421   template<typename _Key, typename _Compare, typename _Allocator>
00422     inline bool
00423     operator!=(const multiset<_Key, _Compare, _Allocator>& __lhs,
00424            const multiset<_Key, _Compare, _Allocator>& __rhs)
00425     { return __lhs._M_base() != __rhs._M_base(); }
00426 
00427   template<typename _Key, typename _Compare, typename _Allocator>
00428     inline bool
00429     operator<(const multiset<_Key, _Compare, _Allocator>& __lhs,
00430           const multiset<_Key, _Compare, _Allocator>& __rhs)
00431     { return __lhs._M_base() < __rhs._M_base(); }
00432 
00433   template<typename _Key, typename _Compare, typename _Allocator>
00434     inline bool
00435     operator<=(const multiset<_Key, _Compare, _Allocator>& __lhs,
00436            const multiset<_Key, _Compare, _Allocator>& __rhs)
00437     { return __lhs._M_base() <= __rhs._M_base(); }
00438 
00439   template<typename _Key, typename _Compare, typename _Allocator>
00440     inline bool
00441     operator>=(const multiset<_Key, _Compare, _Allocator>& __lhs,
00442            const multiset<_Key, _Compare, _Allocator>& __rhs)
00443     { return __lhs._M_base() >= __rhs._M_base(); }
00444 
00445   template<typename _Key, typename _Compare, typename _Allocator>
00446     inline bool
00447     operator>(const multiset<_Key, _Compare, _Allocator>& __lhs,
00448           const multiset<_Key, _Compare, _Allocator>& __rhs)
00449     { return __lhs._M_base() > __rhs._M_base(); }
00450 
00451   template<typename _Key, typename _Compare, typename _Allocator>
00452     void
00453     swap(multiset<_Key, _Compare, _Allocator>& __x,
00454      multiset<_Key, _Compare, _Allocator>& __y)
00455     { return __x.swap(__y); }
00456 
00457 } // namespace __debug
00458 } // namespace std
00459 
00460 #endif