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