libstdc++
debug/map.h
Go to the documentation of this file.
00001 // Debugging map 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/map.h
00027  *  This file is a GNU debug extension to the Standard C++ Library.
00028  */
00029 
00030 #ifndef _GLIBCXX_DEBUG_MAP_H
00031 #define _GLIBCXX_DEBUG_MAP_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::map 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 map
00045     : public _GLIBCXX_STD_C::map<_Key, _Tp, _Compare, _Allocator>,
00046       public __gnu_debug::_Safe_sequence<map<_Key, _Tp, _Compare, _Allocator> >
00047     {
00048       typedef _GLIBCXX_STD_C::map<_Key, _Tp, _Compare, _Allocator> _Base;
00049 
00050       typedef typename _Base::const_iterator _Base_const_iterator;
00051       typedef typename _Base::iterator _Base_iterator;
00052       typedef __gnu_debug::_Equal_to<_Base_const_iterator> _Equal;
00053     public:
00054       // types:
00055       typedef _Key                                  key_type;
00056       typedef _Tp                                   mapped_type;
00057       typedef std::pair<const _Key, _Tp>            value_type;
00058       typedef _Compare                              key_compare;
00059       typedef _Allocator                            allocator_type;
00060       typedef typename _Base::reference             reference;
00061       typedef typename _Base::const_reference       const_reference;
00062 
00063       typedef __gnu_debug::_Safe_iterator<_Base_iterator, map>
00064                                                     iterator;
00065       typedef __gnu_debug::_Safe_iterator<_Base_const_iterator, map>
00066                                                     const_iterator;
00067 
00068       typedef typename _Base::size_type             size_type;
00069       typedef typename _Base::difference_type       difference_type;
00070       typedef typename _Base::pointer               pointer;
00071       typedef typename _Base::const_pointer         const_pointer;
00072       typedef std::reverse_iterator<iterator>       reverse_iterator;
00073       typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00074 
00075       // 23.3.1.1 construct/copy/destroy:
00076       explicit map(const _Compare& __comp = _Compare(),
00077            const _Allocator& __a = _Allocator())
00078       : _Base(__comp, __a) { }
00079 
00080       template<typename _InputIterator>
00081         map(_InputIterator __first, _InputIterator __last,
00082         const _Compare& __comp = _Compare(),
00083         const _Allocator& __a = _Allocator())
00084     : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
00085                                      __last)),
00086         __gnu_debug::__base(__last),
00087         __comp, __a) { }
00088 
00089       map(const map& __x)
00090       : _Base(__x) { }
00091 
00092       map(const _Base& __x)
00093       : _Base(__x) { }
00094 
00095 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00096       map(map&& __x)
00097       noexcept(is_nothrow_copy_constructible<_Compare>::value)
00098       : _Base(std::move(__x))
00099       { this->_M_swap(__x); }
00100 
00101       map(initializer_list<value_type> __l,
00102       const _Compare& __c = _Compare(),
00103       const allocator_type& __a = allocator_type())
00104       : _Base(__l, __c, __a) { }
00105 #endif
00106 
00107       ~map() _GLIBCXX_NOEXCEPT { }
00108 
00109       map&
00110       operator=(const map& __x)
00111       {
00112     *static_cast<_Base*>(this) = __x;
00113     this->_M_invalidate_all();
00114     return *this;
00115       }
00116 
00117 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00118       map&
00119       operator=(map&& __x)
00120       {
00121     // NB: DR 1204.
00122     // NB: DR 675.
00123     clear();
00124     swap(__x);
00125     return *this;
00126       }
00127 
00128       map&
00129       operator=(initializer_list<value_type> __l)
00130       {
00131     this->clear();
00132     this->insert(__l);
00133     return *this;
00134       }
00135 #endif
00136 
00137       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00138       // 133. map missing get_allocator()
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       // 23.3.1.2 element access:
00198       using _Base::operator[];
00199 
00200       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00201       // DR 464. Suggestion for new member functions in standard containers.
00202       using _Base::at;
00203 
00204       // modifiers:
00205       std::pair<iterator, bool>
00206       insert(const value_type& __x)
00207       {
00208     std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
00209     return std::pair<iterator, bool>(iterator(__res.first, this),
00210                      __res.second);
00211       }
00212 
00213 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00214       template<typename _Pair, typename = typename
00215            std::enable_if<std::is_convertible<_Pair,
00216                           value_type>::value>::type>
00217         std::pair<iterator, bool>
00218         insert(_Pair&& __x)
00219         {
00220       std::pair<_Base_iterator, bool> __res
00221         = _Base::insert(std::forward<_Pair>(__x));
00222       return std::pair<iterator, bool>(iterator(__res.first, this),
00223                        __res.second);
00224     }
00225 #endif
00226 
00227 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00228       void
00229       insert(std::initializer_list<value_type> __list)
00230       { _Base::insert(__list); }
00231 #endif
00232 
00233       iterator
00234 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00235       insert(const_iterator __position, const value_type& __x)
00236 #else
00237       insert(iterator __position, const value_type& __x)
00238 #endif
00239       {
00240     __glibcxx_check_insert(__position);
00241     return iterator(_Base::insert(__position.base(), __x), this);
00242       }
00243 
00244 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00245       template<typename _Pair, typename = typename
00246            std::enable_if<std::is_convertible<_Pair,
00247                           value_type>::value>::type>
00248         iterator
00249         insert(const_iterator __position, _Pair&& __x)
00250         {
00251       __glibcxx_check_insert(__position);
00252       return iterator(_Base::insert(__position.base(),
00253                     std::forward<_Pair>(__x)), this);
00254     }
00255 #endif
00256 
00257       template<typename _InputIterator>
00258         void
00259         insert(_InputIterator __first, _InputIterator __last)
00260         {
00261       __glibcxx_check_valid_range(__first, __last);
00262       _Base::insert(__gnu_debug::__base(__first),
00263             __gnu_debug::__base(__last));
00264     }
00265 
00266 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00267       iterator
00268       erase(const_iterator __position)
00269       {
00270     __glibcxx_check_erase(__position);
00271     this->_M_invalidate_if(_Equal(__position.base()));
00272     return iterator(_Base::erase(__position.base()), this);
00273       }
00274 
00275       iterator
00276       erase(iterator __position)
00277       { return erase(const_iterator(__position)); }
00278 #else
00279       void
00280       erase(iterator __position)
00281       {
00282     __glibcxx_check_erase(__position);
00283     this->_M_invalidate_if(_Equal(__position.base()));
00284     _Base::erase(__position.base());
00285       }
00286 #endif
00287 
00288       size_type
00289       erase(const key_type& __x)
00290       {
00291     _Base_iterator __victim = _Base::find(__x);
00292     if (__victim == _Base::end())
00293       return 0;
00294     else
00295       {
00296         this->_M_invalidate_if(_Equal(__victim));
00297         _Base::erase(__victim);
00298         return 1;
00299       }
00300       }
00301 
00302 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00303       iterator
00304       erase(const_iterator __first, const_iterator __last)
00305       {
00306     // _GLIBCXX_RESOLVE_LIB_DEFECTS
00307     // 151. can't currently clear() empty container
00308     __glibcxx_check_erase_range(__first, __last);
00309     for (_Base_const_iterator __victim = __first.base();
00310          __victim != __last.base(); ++__victim)
00311       {
00312         _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
00313                   _M_message(__gnu_debug::__msg_valid_range)
00314                   ._M_iterator(__first, "first")
00315                   ._M_iterator(__last, "last"));
00316         this->_M_invalidate_if(_Equal(__victim));
00317       }
00318     return iterator(_Base::erase(__first.base(), __last.base()), this);
00319       }
00320 #else
00321       void
00322       erase(iterator __first, iterator __last)
00323       {
00324     // _GLIBCXX_RESOLVE_LIB_DEFECTS
00325     // 151. can't currently clear() empty container
00326     __glibcxx_check_erase_range(__first, __last);
00327     for (_Base_iterator __victim = __first.base();
00328          __victim != __last.base(); ++__victim)
00329       {
00330         _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
00331                   _M_message(__gnu_debug::__msg_valid_range)
00332                   ._M_iterator(__first, "first")
00333                   ._M_iterator(__last, "last"));
00334         this->_M_invalidate_if(_Equal(__victim));
00335       }
00336     _Base::erase(__first.base(), __last.base());
00337       }
00338 #endif
00339 
00340       void
00341       swap(map& __x)
00342       {
00343     _Base::swap(__x);
00344     this->_M_swap(__x);
00345       }
00346 
00347       void
00348       clear() _GLIBCXX_NOEXCEPT
00349       {
00350     this->_M_invalidate_all();
00351     _Base::clear();
00352       }
00353 
00354       // observers:
00355       using _Base::key_comp;
00356       using _Base::value_comp;
00357 
00358       // 23.3.1.3 map operations:
00359       iterator
00360       find(const key_type& __x)
00361       { return iterator(_Base::find(__x), this); }
00362 
00363       const_iterator
00364       find(const key_type& __x) const
00365       { return const_iterator(_Base::find(__x), this); }
00366 
00367       using _Base::count;
00368 
00369       iterator
00370       lower_bound(const key_type& __x)
00371       { return iterator(_Base::lower_bound(__x), this); }
00372 
00373       const_iterator
00374       lower_bound(const key_type& __x) const
00375       { return const_iterator(_Base::lower_bound(__x), this); }
00376 
00377       iterator
00378       upper_bound(const key_type& __x)
00379       { return iterator(_Base::upper_bound(__x), this); }
00380 
00381       const_iterator
00382       upper_bound(const key_type& __x) const
00383       { return const_iterator(_Base::upper_bound(__x), this); }
00384 
00385       std::pair<iterator,iterator>
00386       equal_range(const key_type& __x)
00387       {
00388     std::pair<_Base_iterator, _Base_iterator> __res =
00389     _Base::equal_range(__x);
00390     return std::make_pair(iterator(__res.first, this),
00391                   iterator(__res.second, this));
00392       }
00393 
00394       std::pair<const_iterator,const_iterator>
00395       equal_range(const key_type& __x) const
00396       {
00397     std::pair<_Base_const_iterator, _Base_const_iterator> __res =
00398     _Base::equal_range(__x);
00399     return std::make_pair(const_iterator(__res.first, this),
00400                   const_iterator(__res.second, this));
00401       }
00402 
00403       _Base&
00404       _M_base() _GLIBCXX_NOEXCEPT       { return *this; }
00405 
00406       const _Base&
00407       _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
00408 
00409     private:
00410       void
00411       _M_invalidate_all()
00412       {
00413     typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
00414     this->_M_invalidate_if(_Not_equal(_M_base().end()));
00415       }
00416     };
00417 
00418   template<typename _Key, typename _Tp,
00419        typename _Compare, typename _Allocator>
00420     inline bool
00421     operator==(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00422            const map<_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 map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00429            const map<_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 map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00436           const map<_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 map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00443            const map<_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 bool
00449     operator>=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00450            const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
00451     { return __lhs._M_base() >= __rhs._M_base(); }
00452 
00453   template<typename _Key, typename _Tp,
00454        typename _Compare, typename _Allocator>
00455     inline bool
00456     operator>(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00457           const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
00458     { return __lhs._M_base() > __rhs._M_base(); }
00459 
00460   template<typename _Key, typename _Tp,
00461        typename _Compare, typename _Allocator>
00462     inline void
00463     swap(map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00464      map<_Key, _Tp, _Compare, _Allocator>& __rhs)
00465     { __lhs.swap(__rhs); }
00466 
00467 } // namespace __debug
00468 } // namespace std
00469 
00470 #endif