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