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
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     clear();
00125     swap(__x);
00126     return *this;
00127       }
00128 
00129       multimap&
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       template<typename _Pair, typename = typename
00203            std::enable_if<std::is_convertible<_Pair,
00204                           value_type>::value>::type>
00205         iterator
00206         insert(_Pair&& __x)
00207         { return iterator(_Base::insert(std::forward<_Pair>(__x)), this); }
00208 #endif
00209 
00210 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00211       void
00212       insert(std::initializer_list<value_type> __list)
00213       { _Base::insert(__list); }
00214 #endif
00215 
00216       iterator
00217 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00218       insert(const_iterator __position, const value_type& __x)
00219 #else
00220       insert(iterator __position, const value_type& __x)
00221 #endif
00222       {
00223     __glibcxx_check_insert(__position);
00224     return iterator(_Base::insert(__position.base(), __x), this);
00225       }
00226 
00227 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00228       template<typename _Pair, typename = typename
00229            std::enable_if<std::is_convertible<_Pair,
00230                           value_type>::value>::type>
00231         iterator
00232         insert(const_iterator __position, _Pair&& __x)
00233         {
00234       __glibcxx_check_insert(__position);
00235       return iterator(_Base::insert(__position.base(),
00236                     std::forward<_Pair>(__x)), this);
00237     }
00238 #endif
00239 
00240       template<typename _InputIterator>
00241         void
00242         insert(_InputIterator __first, _InputIterator __last)
00243         {
00244       __glibcxx_check_valid_range(__first, __last);
00245       _Base::insert(__gnu_debug::__base(__first),
00246             __gnu_debug::__base(__last));
00247     }
00248 
00249 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00250       iterator
00251       erase(const_iterator __position)
00252       {
00253     __glibcxx_check_erase(__position);
00254     this->_M_invalidate_if(_Equal(__position.base()));
00255     return iterator(_Base::erase(__position.base()), this);
00256       }
00257 
00258       iterator
00259       erase(iterator __position)
00260       { return erase(const_iterator(__position)); }
00261 #else
00262       void
00263       erase(iterator __position)
00264       {
00265     __glibcxx_check_erase(__position);
00266     this->_M_invalidate_if(_Equal(__position.base()));
00267     _Base::erase(__position.base());
00268       }
00269 #endif
00270 
00271       size_type
00272       erase(const key_type& __x)
00273       {
00274     std::pair<_Base_iterator, _Base_iterator> __victims =
00275       _Base::equal_range(__x);
00276     size_type __count = 0;
00277     _Base_iterator __victim = __victims.first;
00278     while (__victim !=  __victims.second)
00279       {
00280         this->_M_invalidate_if(_Equal(__victim));
00281         _Base::erase(__victim++);
00282         ++__count;
00283       }
00284     return __count;
00285       }
00286 
00287 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00288       iterator
00289       erase(const_iterator __first, const_iterator __last)
00290       {
00291     // _GLIBCXX_RESOLVE_LIB_DEFECTS
00292     // 151. can't currently clear() empty container
00293     __glibcxx_check_erase_range(__first, __last);
00294     for (_Base_const_iterator __victim = __first.base();
00295          __victim != __last.base(); ++__victim)
00296       {
00297         _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
00298                   _M_message(__gnu_debug::__msg_valid_range)
00299                   ._M_iterator(__first, "first")
00300                   ._M_iterator(__last, "last"));
00301         this->_M_invalidate_if(_Equal(__victim));
00302       }
00303     return iterator(_Base::erase(__first.base(), __last.base()), this);
00304       }
00305 #else
00306       void
00307       erase(iterator __first, iterator __last)
00308       {
00309     // _GLIBCXX_RESOLVE_LIB_DEFECTS
00310     // 151. can't currently clear() empty container
00311     __glibcxx_check_erase_range(__first, __last);
00312     for (_Base_iterator __victim = __first.base();
00313          __victim != __last.base(); ++__victim)
00314       {
00315         _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
00316                   _M_message(__gnu_debug::__msg_valid_range)
00317                   ._M_iterator(__first, "first")
00318                   ._M_iterator(__last, "last"));
00319         this->_M_invalidate_if(_Equal(__victim));
00320       }
00321     _Base::erase(__first.base(), __last.base());
00322       }
00323 #endif
00324 
00325       void
00326       swap(multimap& __x)
00327       {
00328     _Base::swap(__x);
00329     this->_M_swap(__x);
00330       }
00331 
00332       void
00333       clear() _GLIBCXX_NOEXCEPT
00334       {
00335     this->_M_invalidate_all();
00336     _Base::clear();
00337       }
00338 
00339       // observers:
00340       using _Base::key_comp;
00341       using _Base::value_comp;
00342 
00343       // 23.3.1.3 multimap operations:
00344       iterator
00345       find(const key_type& __x)
00346       { return iterator(_Base::find(__x), this); }
00347 
00348       const_iterator
00349       find(const key_type& __x) const
00350       { return const_iterator(_Base::find(__x), this); }
00351 
00352       using _Base::count;
00353 
00354       iterator
00355       lower_bound(const key_type& __x)
00356       { return iterator(_Base::lower_bound(__x), this); }
00357 
00358       const_iterator
00359       lower_bound(const key_type& __x) const
00360       { return const_iterator(_Base::lower_bound(__x), this); }
00361 
00362       iterator
00363       upper_bound(const key_type& __x)
00364       { return iterator(_Base::upper_bound(__x), this); }
00365 
00366       const_iterator
00367       upper_bound(const key_type& __x) const
00368       { return const_iterator(_Base::upper_bound(__x), this); }
00369 
00370       std::pair<iterator,iterator>
00371       equal_range(const key_type& __x)
00372       {
00373     std::pair<_Base_iterator, _Base_iterator> __res =
00374     _Base::equal_range(__x);
00375     return std::make_pair(iterator(__res.first, this),
00376                   iterator(__res.second, this));
00377       }
00378 
00379       std::pair<const_iterator,const_iterator>
00380       equal_range(const key_type& __x) const
00381       {
00382     std::pair<_Base_const_iterator, _Base_const_iterator> __res =
00383       _Base::equal_range(__x);
00384     return std::make_pair(const_iterator(__res.first, this),
00385                   const_iterator(__res.second, this));
00386       }
00387 
00388       _Base&
00389       _M_base() _GLIBCXX_NOEXCEPT       { return *this; }
00390 
00391       const _Base&
00392       _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
00393 
00394     private:
00395       void
00396       _M_invalidate_all()
00397       {
00398     typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
00399     this->_M_invalidate_if(_Not_equal(_Base::end()));
00400       }
00401     };
00402 
00403   template<typename _Key, typename _Tp,
00404        typename _Compare, typename _Allocator>
00405     inline bool
00406     operator==(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
00407            const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
00408     { return __lhs._M_base() == __rhs._M_base(); }
00409 
00410   template<typename _Key, typename _Tp,
00411        typename _Compare, typename _Allocator>
00412     inline bool
00413     operator!=(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
00414            const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
00415     { return __lhs._M_base() != __rhs._M_base(); }
00416 
00417   template<typename _Key, typename _Tp,
00418        typename _Compare, typename _Allocator>
00419     inline bool
00420     operator<(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
00421           const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
00422     { return __lhs._M_base() < __rhs._M_base(); }
00423 
00424   template<typename _Key, typename _Tp,
00425        typename _Compare, typename _Allocator>
00426     inline bool
00427     operator<=(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
00428            const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
00429     { return __lhs._M_base() <= __rhs._M_base(); }
00430 
00431   template<typename _Key, typename _Tp,
00432        typename _Compare, typename _Allocator>
00433     inline bool
00434     operator>=(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
00435            const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
00436     { return __lhs._M_base() >= __rhs._M_base(); }
00437 
00438   template<typename _Key, typename _Tp,
00439        typename _Compare, typename _Allocator>
00440     inline bool
00441     operator>(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
00442           const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
00443     { return __lhs._M_base() > __rhs._M_base(); }
00444 
00445   template<typename _Key, typename _Tp,
00446        typename _Compare, typename _Allocator>
00447     inline void
00448     swap(multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
00449      multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
00450     { __lhs.swap(__rhs); }
00451 
00452 } // namespace __debug
00453 } // namespace std
00454 
00455 #endif