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