libstdc++
debug/multiset.h
Go to the documentation of this file.
00001 // Debugging multiset implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009, 2010, 2011, 2012
00004 // Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 3, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // Under Section 7 of GPL version 3, you are granted additional
00018 // permissions described in the GCC Runtime Library Exception, version
00019 // 3.1, as published by the Free Software Foundation.
00020 
00021 // You should have received a copy of the GNU General Public License and
00022 // a copy of the GCC Runtime Library Exception along with this program;
00023 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00024 // <http://www.gnu.org/licenses/>.
00025 
00026 /** @file debug/multiset.h
00027  *  This file is a GNU debug extension to the Standard C++ Library.
00028  */
00029 
00030 #ifndef _GLIBCXX_DEBUG_MULTISET_H
00031 #define _GLIBCXX_DEBUG_MULTISET_H 1
00032 
00033 #include <debug/safe_sequence.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 _GLIBCXX_STD_C::multiset<_Key, _Compare, _Allocator>,
00046       public __gnu_debug::_Safe_sequence<multiset<_Key, _Compare, _Allocator> >
00047     {
00048       typedef _GLIBCXX_STD_C::multiset<_Key, _Compare, _Allocator> _Base;
00049 
00050       typedef typename _Base::const_iterator _Base_const_iterator;
00051       typedef typename _Base::iterator _Base_iterator;
00052       typedef __gnu_debug::_Equal_to<_Base_const_iterator> _Equal;
00053     public:
00054       // types:
00055       typedef _Key                   key_type;
00056       typedef _Key                   value_type;
00057       typedef _Compare                   key_compare;
00058       typedef _Compare                   value_compare;
00059       typedef _Allocator                 allocator_type;
00060       typedef typename _Base::reference              reference;
00061       typedef typename _Base::const_reference        const_reference;
00062 
00063       typedef __gnu_debug::_Safe_iterator<_Base_iterator, multiset>
00064       iterator;
00065       typedef __gnu_debug::_Safe_iterator<_Base_const_iterator,
00066                       multiset> const_iterator;
00067 
00068       typedef typename _Base::size_type              size_type;
00069       typedef typename _Base::difference_type        difference_type;
00070       typedef typename _Base::pointer                pointer;
00071       typedef typename _Base::const_pointer          const_pointer;
00072       typedef std::reverse_iterator<iterator>        reverse_iterator;
00073       typedef std::reverse_iterator<const_iterator>  const_reverse_iterator;
00074 
00075       // 23.3.3.1 construct/copy/destroy:
00076       explicit multiset(const _Compare& __comp = _Compare(),
00077             const _Allocator& __a = _Allocator())
00078       : _Base(__comp, __a) { }
00079 
00080       template<typename _InputIterator>
00081         multiset(_InputIterator __first, _InputIterator __last,
00082          const _Compare& __comp = _Compare(),
00083          const _Allocator& __a = _Allocator())
00084     : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
00085                                      __last)),
00086         __gnu_debug::__base(__last),
00087         __comp, __a) { }
00088 
00089       multiset(const multiset& __x)
00090       : _Base(__x) { }
00091 
00092       multiset(const _Base& __x)
00093       : _Base(__x) { }
00094 
00095 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00096       multiset(multiset&& __x)
00097       noexcept(is_nothrow_copy_constructible<_Compare>::value)
00098       : _Base(std::move(__x))
00099       { this->_M_swap(__x); }
00100 
00101       multiset(initializer_list<value_type> __l,
00102            const _Compare& __comp = _Compare(),
00103            const allocator_type& __a = allocator_type())
00104       : _Base(__l, __comp, __a) { }
00105 #endif
00106 
00107       ~multiset() _GLIBCXX_NOEXCEPT { }
00108 
00109       multiset&
00110       operator=(const multiset& __x)
00111       {
00112     *static_cast<_Base*>(this) = __x;
00113     this->_M_invalidate_all();
00114     return *this;
00115       }
00116 
00117 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00118       multiset&
00119       operator=(multiset&& __x)
00120       {
00121     // NB: DR 1204.
00122     // NB: DR 675.
00123     __glibcxx_check_self_move_assign(__x);
00124     clear();
00125     swap(__x);
00126     return *this;
00127       }
00128 
00129       multiset&
00130       operator=(initializer_list<value_type> __l)
00131       {
00132     this->clear();
00133     this->insert(__l);
00134     return *this;
00135       }
00136 #endif
00137 
00138       using _Base::get_allocator;
00139 
00140       // iterators:
00141       iterator
00142       begin() _GLIBCXX_NOEXCEPT
00143       { return iterator(_Base::begin(), this); }
00144 
00145       const_iterator
00146       begin() const _GLIBCXX_NOEXCEPT
00147       { return const_iterator(_Base::begin(), this); }
00148 
00149       iterator
00150       end() _GLIBCXX_NOEXCEPT
00151       { return iterator(_Base::end(), this); }
00152 
00153       const_iterator
00154       end() const _GLIBCXX_NOEXCEPT
00155       { return const_iterator(_Base::end(), this); }
00156 
00157       reverse_iterator
00158       rbegin() _GLIBCXX_NOEXCEPT
00159       { return reverse_iterator(end()); }
00160 
00161       const_reverse_iterator
00162       rbegin() const _GLIBCXX_NOEXCEPT
00163       { return const_reverse_iterator(end()); }
00164 
00165       reverse_iterator
00166       rend() _GLIBCXX_NOEXCEPT
00167       { return reverse_iterator(begin()); }
00168 
00169       const_reverse_iterator
00170       rend() const _GLIBCXX_NOEXCEPT
00171       { return const_reverse_iterator(begin()); }
00172 
00173 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00174       const_iterator
00175       cbegin() const noexcept
00176       { return const_iterator(_Base::begin(), this); }
00177 
00178       const_iterator
00179       cend() const noexcept
00180       { return const_iterator(_Base::end(), this); }
00181 
00182       const_reverse_iterator
00183       crbegin() const noexcept
00184       { return const_reverse_iterator(end()); }
00185 
00186       const_reverse_iterator
00187       crend() const noexcept
00188       { return const_reverse_iterator(begin()); }
00189 #endif
00190 
00191       // capacity:
00192       using _Base::empty;
00193       using _Base::size;
00194       using _Base::max_size;
00195 
00196       // modifiers:
00197       iterator
00198       insert(const value_type& __x)
00199       { return iterator(_Base::insert(__x), this); }
00200 
00201 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00202       iterator
00203       insert(value_type&& __x)
00204       { return iterator(_Base::insert(std::move(__x)), this); }
00205 #endif
00206 
00207       iterator
00208       insert(const_iterator __position, const value_type& __x)
00209       {
00210     __glibcxx_check_insert(__position);
00211     return iterator(_Base::insert(__position.base(), __x), this);
00212       }
00213 
00214 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00215       iterator
00216       insert(const_iterator __position, value_type&& __x)
00217       {
00218     __glibcxx_check_insert(__position);
00219     return iterator(_Base::insert(__position.base(), std::move(__x)),
00220             this);
00221       }
00222 #endif
00223 
00224       template<typename _InputIterator>
00225     void
00226     insert(_InputIterator __first, _InputIterator __last)
00227     {
00228       __glibcxx_check_valid_range(__first, __last);
00229       _Base::insert(__gnu_debug::__base(__first),
00230             __gnu_debug::__base(__last));
00231     }
00232 
00233 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00234       void
00235       insert(initializer_list<value_type> __l)
00236       { _Base::insert(__l); }
00237 #endif
00238 
00239 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00240       iterator
00241       erase(const_iterator __position)
00242       {
00243     __glibcxx_check_erase(__position);
00244     this->_M_invalidate_if(_Equal(__position.base()));
00245     return iterator(_Base::erase(__position.base()), this);
00246       }
00247 #else
00248       void
00249       erase(iterator __position)
00250       {
00251     __glibcxx_check_erase(__position);
00252     this->_M_invalidate_if(_Equal(__position.base()));
00253     _Base::erase(__position.base());
00254       }
00255 #endif
00256 
00257       size_type
00258       erase(const key_type& __x)
00259       {
00260     std::pair<_Base_iterator, _Base_iterator> __victims =
00261       _Base::equal_range(__x);
00262     size_type __count = 0;
00263     _Base_iterator __victim = __victims.first;
00264     while (__victim != __victims.second)
00265       {
00266         this->_M_invalidate_if(_Equal(__victim));
00267         _Base::erase(__victim++);
00268         ++__count;
00269       }
00270     return __count;
00271       }
00272 
00273 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00274       iterator
00275       erase(const_iterator __first, const_iterator __last)
00276       {
00277     // _GLIBCXX_RESOLVE_LIB_DEFECTS
00278     // 151. can't currently clear() empty container
00279     __glibcxx_check_erase_range(__first, __last);
00280     for (_Base_const_iterator __victim = __first.base();
00281          __victim != __last.base(); ++__victim)
00282       {
00283         _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
00284                   _M_message(__gnu_debug::__msg_valid_range)
00285                   ._M_iterator(__first, "first")
00286                   ._M_iterator(__last, "last"));
00287         this->_M_invalidate_if(_Equal(__victim));
00288       }
00289     return iterator(_Base::erase(__first.base(), __last.base()), this);
00290       }
00291 #else
00292       void
00293       erase(iterator __first, 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_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     _Base::erase(__first.base(), __last.base());
00308       }
00309 #endif
00310 
00311       void
00312       swap(multiset& __x)
00313       {
00314     _Base::swap(__x);
00315     this->_M_swap(__x);
00316       }
00317 
00318       void
00319       clear() _GLIBCXX_NOEXCEPT
00320       {
00321     this->_M_invalidate_all();
00322     _Base::clear();
00323       }
00324 
00325       // observers:
00326       using _Base::key_comp;
00327       using _Base::value_comp;
00328 
00329       // multiset operations:
00330       iterator
00331       find(const key_type& __x)
00332       { return iterator(_Base::find(__x), this); }
00333 
00334       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00335       // 214. set::find() missing const overload
00336       const_iterator
00337       find(const key_type& __x) const
00338       { return const_iterator(_Base::find(__x), this); }
00339 
00340       using _Base::count;
00341 
00342       iterator
00343       lower_bound(const key_type& __x)
00344       { return iterator(_Base::lower_bound(__x), this); }
00345 
00346       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00347       // 214. set::find() missing const overload
00348       const_iterator
00349       lower_bound(const key_type& __x) const
00350       { return const_iterator(_Base::lower_bound(__x), this); }
00351 
00352       iterator
00353       upper_bound(const key_type& __x)
00354       { return iterator(_Base::upper_bound(__x), this); }
00355 
00356       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00357       // 214. set::find() missing const overload
00358       const_iterator
00359       upper_bound(const key_type& __x) const
00360       { return const_iterator(_Base::upper_bound(__x), this); }
00361 
00362       std::pair<iterator,iterator>
00363       equal_range(const key_type& __x)
00364       {
00365     std::pair<_Base_iterator, _Base_iterator> __res =
00366       _Base::equal_range(__x);
00367     return std::make_pair(iterator(__res.first, this),
00368                   iterator(__res.second, this));
00369       }
00370 
00371       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00372       // 214. set::find() missing const overload
00373       std::pair<const_iterator,const_iterator>
00374       equal_range(const key_type& __x) const
00375       {
00376     std::pair<_Base_const_iterator, _Base_const_iterator> __res =
00377       _Base::equal_range(__x);
00378     return std::make_pair(const_iterator(__res.first, this),
00379                   const_iterator(__res.second, this));
00380       }
00381 
00382       _Base&
00383       _M_base() _GLIBCXX_NOEXCEPT       { return *this; }
00384 
00385       const _Base&
00386       _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
00387 
00388     private:
00389       void
00390       _M_invalidate_all()
00391       {
00392     typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
00393     this->_M_invalidate_if(_Not_equal(_Base::end()));
00394       }
00395     };
00396 
00397   template<typename _Key, typename _Compare, typename _Allocator>
00398     inline bool
00399     operator==(const multiset<_Key, _Compare, _Allocator>& __lhs,
00400            const multiset<_Key, _Compare, _Allocator>& __rhs)
00401     { return __lhs._M_base() == __rhs._M_base(); }
00402 
00403   template<typename _Key, typename _Compare, typename _Allocator>
00404     inline bool
00405     operator!=(const multiset<_Key, _Compare, _Allocator>& __lhs,
00406            const multiset<_Key, _Compare, _Allocator>& __rhs)
00407     { return __lhs._M_base() != __rhs._M_base(); }
00408 
00409   template<typename _Key, typename _Compare, typename _Allocator>
00410     inline bool
00411     operator<(const multiset<_Key, _Compare, _Allocator>& __lhs,
00412           const multiset<_Key, _Compare, _Allocator>& __rhs)
00413     { return __lhs._M_base() < __rhs._M_base(); }
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     void
00435     swap(multiset<_Key, _Compare, _Allocator>& __x,
00436      multiset<_Key, _Compare, _Allocator>& __y)
00437     { return __x.swap(__y); }
00438 
00439 } // namespace __debug
00440 } // namespace std
00441 
00442 #endif