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
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       typedef __gnu_debug::_Safe_sequence<map> _Safe_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<typename _Base::iterator, map>
00065                                                     iterator;
00066       typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator, map>
00067                                                     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       using _Base::value_compare;
00077 
00078       // 23.3.1.1 construct/copy/destroy:
00079       explicit map(const _Compare& __comp = _Compare(),
00080            const _Allocator& __a = _Allocator())
00081       : _Base(__comp, __a) { }
00082 
00083       template<typename _InputIterator>
00084         map(_InputIterator __first, _InputIterator __last,
00085         const _Compare& __comp = _Compare(),
00086         const _Allocator& __a = _Allocator())
00087     : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
00088                                      __last)),
00089         __gnu_debug::__base(__last),
00090         __comp, __a), _Safe_base() { }
00091 
00092       map(const map& __x)
00093       : _Base(__x), _Safe_base() { }
00094 
00095       map(const _Base& __x)
00096       : _Base(__x), _Safe_base() { }
00097 
00098 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00099       map(map&& __x)
00100       : _Base(std::move(__x)), _Safe_base()
00101       { this->_M_swap(__x); }
00102 
00103       map(initializer_list<value_type> __l,
00104       const _Compare& __c = _Compare(),
00105       const allocator_type& __a = allocator_type())
00106       : _Base(__l, __c, __a), _Safe_base() { }
00107 #endif
00108 
00109       ~map() { }
00110 
00111       map&
00112       operator=(const map& __x)
00113       {
00114     *static_cast<_Base*>(this) = __x;
00115     this->_M_invalidate_all();
00116     return *this;
00117       }
00118 
00119 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00120       map&
00121       operator=(map&& __x)
00122       {
00123     // NB: DR 1204.
00124     // NB: DR 675.
00125     clear();
00126     swap(__x);
00127     return *this;
00128       }
00129 
00130       map&
00131       operator=(initializer_list<value_type> __l)
00132       {
00133     this->clear();
00134     this->insert(__l);
00135     return *this;
00136       }
00137 #endif
00138 
00139       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00140       // 133. map missing get_allocator()
00141       using _Base::get_allocator;
00142 
00143       // iterators:
00144       iterator 
00145       begin()
00146       { return iterator(_Base::begin(), this); }
00147 
00148       const_iterator
00149       begin() const
00150       { return const_iterator(_Base::begin(), this); }
00151 
00152       iterator
00153       end()
00154       { return iterator(_Base::end(), this); }
00155 
00156       const_iterator
00157       end() const
00158       { return const_iterator(_Base::end(), this); }
00159 
00160       reverse_iterator
00161       rbegin()
00162       { return reverse_iterator(end()); }
00163 
00164       const_reverse_iterator
00165       rbegin() const
00166       { return const_reverse_iterator(end()); }
00167 
00168       reverse_iterator
00169       rend()
00170       { return reverse_iterator(begin()); }
00171 
00172       const_reverse_iterator
00173       rend() const
00174       { return const_reverse_iterator(begin()); }
00175 
00176 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00177       const_iterator
00178       cbegin() const
00179       { return const_iterator(_Base::begin(), this); }
00180 
00181       const_iterator
00182       cend() const
00183       { return const_iterator(_Base::end(), this); }
00184 
00185       const_reverse_iterator
00186       crbegin() const
00187       { return const_reverse_iterator(end()); }
00188 
00189       const_reverse_iterator
00190       crend() const
00191       { return const_reverse_iterator(begin()); }
00192 #endif
00193 
00194       // capacity:
00195       using _Base::empty;
00196       using _Base::size;
00197       using _Base::max_size;
00198 
00199       // 23.3.1.2 element access:
00200       using _Base::operator[];
00201 
00202       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00203       // DR 464. Suggestion for new member functions in standard containers.
00204       using _Base::at;
00205 
00206       // modifiers:
00207       std::pair<iterator, bool>
00208       insert(const value_type& __x)
00209       {
00210     typedef typename _Base::iterator _Base_iterator;
00211     std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
00212     return std::pair<iterator, bool>(iterator(__res.first, this),
00213                      __res.second);
00214       }
00215 
00216 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00217       template<typename _Pair, typename = typename
00218            std::enable_if<std::is_convertible<_Pair,
00219                           value_type>::value>::type>
00220         std::pair<iterator, bool>
00221         insert(_Pair&& __x)
00222         {
00223       typedef typename _Base::iterator _Base_iterator;
00224       std::pair<_Base_iterator, bool> __res
00225         = _Base::insert(std::forward<_Pair>(__x));
00226       return std::pair<iterator, bool>(iterator(__res.first, this),
00227                        __res.second);
00228     }
00229 #endif
00230 
00231 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00232       void
00233       insert(std::initializer_list<value_type> __list)
00234       { _Base::insert(__list); }
00235 #endif
00236 
00237       iterator
00238 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00239       insert(const_iterator __position, const value_type& __x)
00240 #else
00241       insert(iterator __position, const value_type& __x)
00242 #endif
00243       {
00244     __glibcxx_check_insert(__position);
00245     return iterator(_Base::insert(__position.base(), __x), this);
00246       }
00247 
00248 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00249       template<typename _Pair, typename = typename
00250            std::enable_if<std::is_convertible<_Pair,
00251                           value_type>::value>::type>
00252         iterator
00253         insert(const_iterator __position, _Pair&& __x)
00254         {
00255       __glibcxx_check_insert(__position);
00256       return iterator(_Base::insert(__position.base(),
00257                     std::forward<_Pair>(__x)), this);
00258     }
00259 #endif
00260 
00261       template<typename _InputIterator>
00262         void
00263         insert(_InputIterator __first, _InputIterator __last)
00264         {
00265       __glibcxx_check_valid_range(__first, __last);
00266       _Base::insert(__gnu_debug::__base(__first),
00267             __gnu_debug::__base(__last));
00268     }
00269 
00270 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00271       iterator
00272       erase(const_iterator __position)
00273       {
00274     __glibcxx_check_erase(__position);
00275     this->_M_invalidate_if(_Equal(__position.base()));
00276     return iterator(_Base::erase(__position.base()), this);
00277       }
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()
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     typedef typename _Base::iterator _Base_iterator;
00389     std::pair<_Base_iterator, _Base_iterator> __res =
00390     _Base::equal_range(__x);
00391     return std::make_pair(iterator(__res.first, this),
00392                   iterator(__res.second, this));
00393       }
00394 
00395       std::pair<const_iterator,const_iterator>
00396       equal_range(const key_type& __x) const
00397       {
00398     typedef typename _Base::const_iterator _Base_const_iterator;
00399     std::pair<_Base_const_iterator, _Base_const_iterator> __res =
00400     _Base::equal_range(__x);
00401     return std::make_pair(const_iterator(__res.first, this),
00402                   const_iterator(__res.second, this));
00403       }
00404 
00405       _Base& 
00406       _M_base() { return *this; }
00407 
00408       const _Base&
00409       _M_base() const { return *this; }
00410 
00411     private:
00412       void
00413       _M_invalidate_all()
00414       {
00415     typedef typename _Base::const_iterator _Base_const_iterator;
00416     typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
00417     this->_M_invalidate_if(_Not_equal(_M_base().end()));
00418       }
00419     };
00420 
00421   template<typename _Key, typename _Tp,
00422        typename _Compare, typename _Allocator>
00423     inline bool
00424     operator==(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00425            const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
00426     { return __lhs._M_base() == __rhs._M_base(); }
00427 
00428   template<typename _Key, typename _Tp,
00429        typename _Compare, typename _Allocator>
00430     inline bool
00431     operator!=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00432            const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
00433     { return __lhs._M_base() != __rhs._M_base(); }
00434 
00435   template<typename _Key, typename _Tp,
00436        typename _Compare, typename _Allocator>
00437     inline bool
00438     operator<(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00439           const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
00440     { return __lhs._M_base() < __rhs._M_base(); }
00441 
00442   template<typename _Key, typename _Tp,
00443        typename _Compare, typename _Allocator>
00444     inline bool
00445     operator<=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00446            const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
00447     { return __lhs._M_base() <= __rhs._M_base(); }
00448 
00449   template<typename _Key, typename _Tp,
00450        typename _Compare, typename _Allocator>
00451     inline bool
00452     operator>=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00453            const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
00454     { return __lhs._M_base() >= __rhs._M_base(); }
00455 
00456   template<typename _Key, typename _Tp,
00457        typename _Compare, typename _Allocator>
00458     inline bool
00459     operator>(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00460           const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
00461     { return __lhs._M_base() > __rhs._M_base(); }
00462 
00463   template<typename _Key, typename _Tp,
00464        typename _Compare, typename _Allocator>
00465     inline void
00466     swap(map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00467      map<_Key, _Tp, _Compare, _Allocator>& __rhs)
00468     { __lhs.swap(__rhs); }
00469 
00470 } // namespace __debug
00471 } // namespace std
00472 
00473 #endif