libstdc++
multimap.h
Go to the documentation of this file.
00001 // Debugging multimap implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2003-2017 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           typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
00290           __glibcxx_check_valid_range2(__first, __last, __dist);
00291 
00292           if (__dist.second >= __gnu_debug::__dp_sign)
00293             _Base::insert(__gnu_debug::__unsafe(__first),
00294                           __gnu_debug::__unsafe(__last));
00295           else
00296             _Base::insert(__first, __last);
00297         }
00298 
00299 #if __cplusplus > 201402L
00300       using node_type = typename _Base::node_type;
00301 
00302       node_type
00303       extract(const_iterator __position)
00304       {
00305         __glibcxx_check_erase(__position);
00306         this->_M_invalidate_if(_Equal(__position.base()));
00307         return _Base::extract(__position.base());
00308       }
00309 
00310       node_type
00311       extract(const key_type& __key)
00312       {
00313         const auto __position = find(__key);
00314         if (__position != end())
00315           return extract(__position);
00316         return {};
00317       }
00318 
00319       iterator
00320       insert(node_type&& __nh)
00321       { return iterator(_Base::insert(std::move(__nh)), this); }
00322 
00323       iterator
00324       insert(const_iterator __hint, node_type&& __nh)
00325       {
00326         __glibcxx_check_insert(__hint);
00327         return iterator(_Base::insert(__hint.base(), std::move(__nh)), this);
00328       }
00329 
00330       using _Base::merge;
00331 #endif // C++17
00332 
00333 #if __cplusplus >= 201103L
00334       iterator
00335       erase(const_iterator __position)
00336       {
00337         __glibcxx_check_erase(__position);
00338         this->_M_invalidate_if(_Equal(__position.base()));
00339         return iterator(_Base::erase(__position.base()), this);
00340       }
00341 
00342       iterator
00343       erase(iterator __position)
00344       { return erase(const_iterator(__position)); }
00345 #else
00346       void
00347       erase(iterator __position)
00348       {
00349         __glibcxx_check_erase(__position);
00350         this->_M_invalidate_if(_Equal(__position.base()));
00351         _Base::erase(__position.base());
00352       }
00353 #endif
00354 
00355       size_type
00356       erase(const key_type& __x)
00357       {
00358         std::pair<_Base_iterator, _Base_iterator> __victims =
00359           _Base::equal_range(__x);
00360         size_type __count = 0;
00361         _Base_iterator __victim = __victims.first;
00362         while (__victim !=  __victims.second)
00363           {
00364             this->_M_invalidate_if(_Equal(__victim));
00365             _Base::erase(__victim++);
00366             ++__count;
00367           }
00368         return __count;
00369       }
00370 
00371 #if __cplusplus >= 201103L
00372       iterator
00373       erase(const_iterator __first, const_iterator __last)
00374       {
00375         // _GLIBCXX_RESOLVE_LIB_DEFECTS
00376         // 151. can't currently clear() empty container
00377         __glibcxx_check_erase_range(__first, __last);
00378         for (_Base_const_iterator __victim = __first.base();
00379              __victim != __last.base(); ++__victim)
00380           {
00381             _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
00382                                   _M_message(__gnu_debug::__msg_valid_range)
00383                                   ._M_iterator(__first, "first")
00384                                   ._M_iterator(__last, "last"));
00385             this->_M_invalidate_if(_Equal(__victim));
00386           }
00387         return iterator(_Base::erase(__first.base(), __last.base()), this);
00388       }
00389 #else
00390       void
00391       erase(iterator __first, iterator __last)
00392       {
00393         // _GLIBCXX_RESOLVE_LIB_DEFECTS
00394         // 151. can't currently clear() empty container
00395         __glibcxx_check_erase_range(__first, __last);
00396         for (_Base_iterator __victim = __first.base();
00397              __victim != __last.base(); ++__victim)
00398           {
00399             _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
00400                                   _M_message(__gnu_debug::__msg_valid_range)
00401                                   ._M_iterator(__first, "first")
00402                                   ._M_iterator(__last, "last"));
00403             this->_M_invalidate_if(_Equal(__victim));
00404           }
00405         _Base::erase(__first.base(), __last.base());
00406       }
00407 #endif
00408 
00409       void
00410       swap(multimap& __x)
00411       _GLIBCXX_NOEXCEPT_IF( noexcept(declval<_Base&>().swap(__x)) )
00412       {
00413         _Safe::_M_swap(__x);
00414         _Base::swap(__x);
00415       }
00416 
00417       void
00418       clear() _GLIBCXX_NOEXCEPT
00419       {
00420         this->_M_invalidate_all();
00421         _Base::clear();
00422       }
00423 
00424       // observers:
00425       using _Base::key_comp;
00426       using _Base::value_comp;
00427 
00428       // 23.3.1.3 multimap operations:
00429       iterator
00430       find(const key_type& __x)
00431       { return iterator(_Base::find(__x), this); }
00432 
00433 #if __cplusplus > 201103L
00434       template<typename _Kt,
00435                typename _Req =
00436                  typename __has_is_transparent<_Compare, _Kt>::type>
00437         iterator
00438         find(const _Kt& __x)
00439         { return { _Base::find(__x), this }; }
00440 #endif
00441 
00442       const_iterator
00443       find(const key_type& __x) const
00444       { return const_iterator(_Base::find(__x), this); }
00445 
00446 #if __cplusplus > 201103L
00447       template<typename _Kt,
00448                typename _Req =
00449                  typename __has_is_transparent<_Compare, _Kt>::type>
00450         const_iterator
00451         find(const _Kt& __x) const
00452         { return { _Base::find(__x), this }; }
00453 #endif
00454 
00455       using _Base::count;
00456 
00457       iterator
00458       lower_bound(const key_type& __x)
00459       { return iterator(_Base::lower_bound(__x), this); }
00460 
00461 #if __cplusplus > 201103L
00462       template<typename _Kt,
00463                typename _Req =
00464                  typename __has_is_transparent<_Compare, _Kt>::type>
00465         iterator
00466         lower_bound(const _Kt& __x)
00467         { return { _Base::lower_bound(__x), this }; }
00468 #endif
00469 
00470       const_iterator
00471       lower_bound(const key_type& __x) const
00472       { return const_iterator(_Base::lower_bound(__x), this); }
00473 
00474 #if __cplusplus > 201103L
00475       template<typename _Kt,
00476                typename _Req =
00477                  typename __has_is_transparent<_Compare, _Kt>::type>
00478         const_iterator
00479         lower_bound(const _Kt& __x) const
00480         { return { _Base::lower_bound(__x), this }; }
00481 #endif
00482 
00483       iterator
00484       upper_bound(const key_type& __x)
00485       { return iterator(_Base::upper_bound(__x), this); }
00486 
00487 #if __cplusplus > 201103L
00488       template<typename _Kt,
00489                typename _Req =
00490                  typename __has_is_transparent<_Compare, _Kt>::type>
00491         iterator
00492         upper_bound(const _Kt& __x)
00493         { return { _Base::upper_bound(__x), this }; }
00494 #endif
00495 
00496       const_iterator
00497       upper_bound(const key_type& __x) const
00498       { return const_iterator(_Base::upper_bound(__x), this); }
00499 
00500 #if __cplusplus > 201103L
00501       template<typename _Kt,
00502                typename _Req =
00503                  typename __has_is_transparent<_Compare, _Kt>::type>
00504         const_iterator
00505         upper_bound(const _Kt& __x) const
00506         { return { _Base::upper_bound(__x), this }; }
00507 #endif
00508 
00509       std::pair<iterator,iterator>
00510       equal_range(const key_type& __x)
00511       {
00512         std::pair<_Base_iterator, _Base_iterator> __res =
00513         _Base::equal_range(__x);
00514         return std::make_pair(iterator(__res.first, this),
00515                               iterator(__res.second, this));
00516       }
00517 
00518 #if __cplusplus > 201103L
00519       template<typename _Kt,
00520                typename _Req =
00521                  typename __has_is_transparent<_Compare, _Kt>::type>
00522         std::pair<iterator, iterator>
00523         equal_range(const _Kt& __x)
00524         {
00525           auto __res = _Base::equal_range(__x);
00526           return { { __res.first, this }, { __res.second, this } };
00527         }
00528 #endif
00529 
00530       std::pair<const_iterator,const_iterator>
00531       equal_range(const key_type& __x) const
00532       {
00533         std::pair<_Base_const_iterator, _Base_const_iterator> __res =
00534           _Base::equal_range(__x);
00535         return std::make_pair(const_iterator(__res.first, this),
00536                               const_iterator(__res.second, this));
00537       }
00538 
00539 #if __cplusplus > 201103L
00540       template<typename _Kt,
00541                typename _Req =
00542                  typename __has_is_transparent<_Compare, _Kt>::type>
00543         std::pair<const_iterator, const_iterator>
00544         equal_range(const _Kt& __x) const
00545         {
00546           auto __res = _Base::equal_range(__x);
00547           return { { __res.first, this }, { __res.second, this } };
00548         }
00549 #endif
00550 
00551       _Base&
00552       _M_base() _GLIBCXX_NOEXCEPT { return *this; }
00553 
00554       const _Base&
00555       _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
00556     };
00557 
00558   template<typename _Key, typename _Tp,
00559            typename _Compare, typename _Allocator>
00560     inline bool
00561     operator==(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
00562                const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
00563     { return __lhs._M_base() == __rhs._M_base(); }
00564 
00565   template<typename _Key, typename _Tp,
00566            typename _Compare, typename _Allocator>
00567     inline bool
00568     operator!=(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
00569                const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
00570     { return __lhs._M_base() != __rhs._M_base(); }
00571 
00572   template<typename _Key, typename _Tp,
00573            typename _Compare, typename _Allocator>
00574     inline bool
00575     operator<(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
00576               const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
00577     { return __lhs._M_base() < __rhs._M_base(); }
00578 
00579   template<typename _Key, typename _Tp,
00580            typename _Compare, typename _Allocator>
00581     inline bool
00582     operator<=(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
00583                const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
00584     { return __lhs._M_base() <= __rhs._M_base(); }
00585 
00586   template<typename _Key, typename _Tp,
00587            typename _Compare, typename _Allocator>
00588     inline bool
00589     operator>=(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
00590                const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
00591     { return __lhs._M_base() >= __rhs._M_base(); }
00592 
00593   template<typename _Key, typename _Tp,
00594            typename _Compare, typename _Allocator>
00595     inline bool
00596     operator>(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
00597               const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
00598     { return __lhs._M_base() > __rhs._M_base(); }
00599 
00600   template<typename _Key, typename _Tp,
00601            typename _Compare, typename _Allocator>
00602     inline void
00603     swap(multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
00604          multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
00605     _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs.swap(__rhs)))
00606     { __lhs.swap(__rhs); }
00607 
00608 } // namespace __debug
00609 } // namespace std
00610 
00611 #endif