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
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
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_D::multiset<_Key, _Compare, _Allocator>,
00046       public __gnu_debug::_Safe_sequence<multiset<_Key, _Compare, _Allocator> >
00047     {
00048       typedef _GLIBCXX_STD_D::multiset<_Key, _Compare, _Allocator> _Base;
00049       typedef __gnu_debug::_Safe_sequence<multiset> _Safe_base;
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 __gnu_debug::_Safe_iterator<typename _Base::iterator, multiset>
00062       iterator;
00063       typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
00064                       multiset> const_iterator;
00065 
00066       typedef typename _Base::size_type              size_type;
00067       typedef typename _Base::difference_type        difference_type;
00068       typedef typename _Base::pointer                pointer;
00069       typedef typename _Base::const_pointer          const_pointer;
00070       typedef std::reverse_iterator<iterator>        reverse_iterator;
00071       typedef std::reverse_iterator<const_iterator>  const_reverse_iterator;
00072 
00073       // 23.3.3.1 construct/copy/destroy:
00074       explicit multiset(const _Compare& __comp = _Compare(),
00075             const _Allocator& __a = _Allocator())
00076       : _Base(__comp, __a) { }
00077 
00078       template<typename _InputIterator>
00079         multiset(_InputIterator __first, _InputIterator __last,
00080          const _Compare& __comp = _Compare(),
00081          const _Allocator& __a = _Allocator())
00082     : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
00083                                      __last)),
00084         __gnu_debug::__base(__last),
00085         __comp, __a) { }
00086 
00087       multiset(const multiset& __x)
00088       : _Base(__x), _Safe_base() { }
00089 
00090       multiset(const _Base& __x)
00091       : _Base(__x), _Safe_base() { }
00092 
00093 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00094       multiset(multiset&& __x)
00095       : _Base(std::move(__x)), _Safe_base()
00096       { this->_M_swap(__x); }
00097 
00098       multiset(initializer_list<value_type> __l,
00099            const _Compare& __comp = _Compare(),
00100            const allocator_type& __a = allocator_type())
00101       : _Base(__l, __comp, __a), _Safe_base() { }
00102 #endif
00103 
00104       ~multiset() { }
00105 
00106       multiset&
00107       operator=(const multiset& __x)
00108       {
00109     *static_cast<_Base*>(this) = __x;
00110     this->_M_invalidate_all();
00111     return *this;
00112       }
00113 
00114 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00115       multiset&
00116       operator=(multiset&& __x)
00117       {
00118     // NB: DR 1204.
00119     // NB: DR 675.
00120     clear();
00121     swap(__x);
00122     return *this;
00123       }
00124 
00125       multiset&
00126       operator=(initializer_list<value_type> __l)
00127       {
00128     this->clear();
00129     this->insert(__l);
00130     return *this;
00131       }
00132 #endif
00133 
00134       using _Base::get_allocator;
00135 
00136       // iterators:
00137       iterator
00138       begin()
00139       { return iterator(_Base::begin(), this); }
00140 
00141       const_iterator
00142       begin() const
00143       { return const_iterator(_Base::begin(), this); }
00144 
00145       iterator
00146       end()
00147       { return iterator(_Base::end(), this); }
00148 
00149       const_iterator
00150       end() const
00151       { return const_iterator(_Base::end(), this); }
00152 
00153       reverse_iterator
00154       rbegin()
00155       { return reverse_iterator(end()); }
00156 
00157       const_reverse_iterator
00158       rbegin() const
00159       { return const_reverse_iterator(end()); }
00160 
00161       reverse_iterator
00162       rend()
00163       { return reverse_iterator(begin()); }
00164 
00165       const_reverse_iterator
00166       rend() const
00167       { return const_reverse_iterator(begin()); }
00168 
00169 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00170       const_iterator
00171       cbegin() const
00172       { return const_iterator(_Base::begin(), this); }
00173 
00174       const_iterator
00175       cend() const
00176       { return const_iterator(_Base::end(), this); }
00177 
00178       const_reverse_iterator
00179       crbegin() const
00180       { return const_reverse_iterator(end()); }
00181 
00182       const_reverse_iterator
00183       crend() const
00184       { return const_reverse_iterator(begin()); }
00185 #endif
00186 
00187       // capacity:
00188       using _Base::empty;
00189       using _Base::size;
00190       using _Base::max_size;
00191 
00192       // modifiers:
00193       iterator
00194       insert(const value_type& __x)
00195       { return iterator(_Base::insert(__x), this); }
00196 
00197       iterator
00198       insert(iterator __position, const value_type& __x)
00199       {
00200     __glibcxx_check_insert(__position);
00201     return iterator(_Base::insert(__position.base(), __x), this);
00202       }
00203 
00204       template<typename _InputIterator>
00205     void
00206     insert(_InputIterator __first, _InputIterator __last)
00207     {
00208       __glibcxx_check_valid_range(__first, __last);
00209       _Base::insert(__gnu_debug::__base(__first),
00210             __gnu_debug::__base(__last));
00211     }
00212 
00213 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00214       void
00215       insert(initializer_list<value_type> __l)
00216       { _Base::insert(__l); }
00217 #endif
00218 
00219 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00220       iterator
00221       erase(iterator __position)
00222       {
00223     __glibcxx_check_erase(__position);
00224     __position._M_invalidate();
00225     return iterator(_Base::erase(__position.base()), this);
00226       }
00227 #else
00228       void
00229       erase(iterator __position)
00230       {
00231     __glibcxx_check_erase(__position);
00232     __position._M_invalidate();
00233     _Base::erase(__position.base());
00234       }
00235 #endif
00236 
00237       size_type
00238       erase(const key_type& __x)
00239       {
00240     std::pair<iterator, iterator> __victims = this->equal_range(__x);
00241     size_type __count = 0;
00242     while (__victims.first != __victims.second)
00243     {
00244       iterator __victim = __victims.first++;
00245       __victim._M_invalidate();
00246       _Base::erase(__victim.base());
00247       ++__count;
00248     }
00249     return __count;
00250       }
00251 
00252 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00253       iterator
00254       erase(iterator __first, iterator __last)
00255       {
00256     // _GLIBCXX_RESOLVE_LIB_DEFECTS
00257     // 151. can't currently clear() empty container
00258     __glibcxx_check_erase_range(__first, __last);
00259     while (__first != __last)
00260       this->erase(__first++);
00261     return __last;
00262       }
00263 #else
00264       void
00265       erase(iterator __first, iterator __last)
00266       {
00267     // _GLIBCXX_RESOLVE_LIB_DEFECTS
00268     // 151. can't currently clear() empty container
00269     __glibcxx_check_erase_range(__first, __last);
00270     while (__first != __last)
00271       this->erase(__first++);
00272       }
00273 #endif
00274 
00275       void
00276       swap(multiset& __x)
00277       {
00278     _Base::swap(__x);
00279     this->_M_swap(__x);
00280       }
00281 
00282       void
00283       clear()
00284       { this->erase(begin(), end()); }
00285 
00286       // observers:
00287       using _Base::key_comp;
00288       using _Base::value_comp;
00289 
00290       // multiset operations:
00291       iterator
00292       find(const key_type& __x)
00293       { return iterator(_Base::find(__x), this); }
00294 
00295       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00296       // 214. set::find() missing const overload
00297       const_iterator
00298       find(const key_type& __x) const
00299       { return const_iterator(_Base::find(__x), this); }
00300 
00301       using _Base::count;
00302 
00303       iterator
00304       lower_bound(const key_type& __x)
00305       { return iterator(_Base::lower_bound(__x), this); }
00306 
00307       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00308       // 214. set::find() missing const overload
00309       const_iterator
00310       lower_bound(const key_type& __x) const
00311       { return const_iterator(_Base::lower_bound(__x), this); }
00312 
00313       iterator
00314       upper_bound(const key_type& __x)
00315       { return iterator(_Base::upper_bound(__x), this); }
00316 
00317       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00318       // 214. set::find() missing const overload
00319       const_iterator
00320       upper_bound(const key_type& __x) const
00321       { return const_iterator(_Base::upper_bound(__x), this); }
00322 
00323       std::pair<iterator,iterator>
00324       equal_range(const key_type& __x)
00325       {
00326     typedef typename _Base::iterator _Base_iterator;
00327     std::pair<_Base_iterator, _Base_iterator> __res =
00328         _Base::equal_range(__x);
00329     return std::make_pair(iterator(__res.first, this),
00330                   iterator(__res.second, this));
00331       }
00332 
00333       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00334       // 214. set::find() missing const overload
00335       std::pair<const_iterator,const_iterator>
00336       equal_range(const key_type& __x) const
00337       {
00338     typedef typename _Base::const_iterator _Base_iterator;
00339     std::pair<_Base_iterator, _Base_iterator> __res =
00340         _Base::equal_range(__x);
00341     return std::make_pair(const_iterator(__res.first, this),
00342                   const_iterator(__res.second, this));
00343       }
00344 
00345       _Base&
00346       _M_base() { return *this; }
00347 
00348       const _Base&
00349       _M_base() const { return *this; }
00350 
00351     private:
00352       void
00353       _M_invalidate_all()
00354       {
00355     typedef typename _Base::const_iterator _Base_const_iterator;
00356     typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
00357     this->_M_invalidate_if(_Not_equal(_M_base().end()));
00358       }
00359     };
00360 
00361   template<typename _Key, typename _Compare, typename _Allocator>
00362     inline bool
00363     operator==(const multiset<_Key, _Compare, _Allocator>& __lhs,
00364            const multiset<_Key, _Compare, _Allocator>& __rhs)
00365     { return __lhs._M_base() == __rhs._M_base(); }
00366 
00367   template<typename _Key, typename _Compare, typename _Allocator>
00368     inline bool
00369     operator!=(const multiset<_Key, _Compare, _Allocator>& __lhs,
00370            const multiset<_Key, _Compare, _Allocator>& __rhs)
00371     { return __lhs._M_base() != __rhs._M_base(); }
00372 
00373   template<typename _Key, typename _Compare, typename _Allocator>
00374     inline bool
00375     operator<(const multiset<_Key, _Compare, _Allocator>& __lhs,
00376           const multiset<_Key, _Compare, _Allocator>& __rhs)
00377     { return __lhs._M_base() < __rhs._M_base(); }
00378 
00379   template<typename _Key, typename _Compare, typename _Allocator>
00380     inline bool
00381     operator<=(const multiset<_Key, _Compare, _Allocator>& __lhs,
00382            const multiset<_Key, _Compare, _Allocator>& __rhs)
00383     { return __lhs._M_base() <= __rhs._M_base(); }
00384 
00385   template<typename _Key, typename _Compare, typename _Allocator>
00386     inline bool
00387     operator>=(const multiset<_Key, _Compare, _Allocator>& __lhs,
00388            const multiset<_Key, _Compare, _Allocator>& __rhs)
00389     { return __lhs._M_base() >= __rhs._M_base(); }
00390 
00391   template<typename _Key, typename _Compare, typename _Allocator>
00392     inline bool
00393     operator>(const multiset<_Key, _Compare, _Allocator>& __lhs,
00394           const multiset<_Key, _Compare, _Allocator>& __rhs)
00395     { return __lhs._M_base() > __rhs._M_base(); }
00396 
00397   template<typename _Key, typename _Compare, typename _Allocator>
00398     void
00399     swap(multiset<_Key, _Compare, _Allocator>& __x,
00400      multiset<_Key, _Compare, _Allocator>& __y)
00401     { return __x.swap(__y); }
00402 
00403 } // namespace __debug
00404 } // namespace std
00405 
00406 #endif