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
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_D::map<_Key, _Tp, _Compare, _Allocator>,
00046       public __gnu_debug::_Safe_sequence<map<_Key, _Tp, _Compare, _Allocator> >
00047     {
00048       typedef _GLIBCXX_STD_D::map<_Key, _Tp, _Compare, _Allocator> _Base;
00049       typedef __gnu_debug::_Safe_sequence<map> _Safe_base;
00050 
00051     public:
00052       // types:
00053       typedef _Key                                  key_type;
00054       typedef _Tp                                   mapped_type;
00055       typedef std::pair<const _Key, _Tp>            value_type;
00056       typedef _Compare                              key_compare;
00057       typedef _Allocator                            allocator_type;
00058       typedef typename _Base::reference             reference;
00059       typedef typename _Base::const_reference       const_reference;
00060 
00061       typedef __gnu_debug::_Safe_iterator<typename _Base::iterator, map>
00062                                                     iterator;
00063       typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator, map>
00064                                                     const_iterator;
00065 
00066       typedef typename _Base::size_type             size_type;
00067       typedef typename _Base::difference_type       difference_type;
00068       typedef typename _Base::pointer               pointer;
00069       typedef typename _Base::const_pointer         const_pointer;
00070       typedef std::reverse_iterator<iterator>       reverse_iterator;
00071       typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00072 
00073       using _Base::value_compare;
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::__check_valid_range(__first, __last), __last,
00085         __comp, __a), _Safe_base() { }
00086 
00087       map(const map& __x)
00088       : _Base(__x), _Safe_base() { }
00089 
00090       map(const _Base& __x)
00091       : _Base(__x), _Safe_base() { }
00092 
00093 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00094       map(map&& __x)
00095       : _Base(std::forward<map>(__x)), _Safe_base()
00096       { this->_M_swap(__x); }
00097 
00098       map(initializer_list<value_type> __l,
00099       const _Compare& __c = _Compare(),
00100       const allocator_type& __a = allocator_type())
00101       : _Base(__l, __c, __a), _Safe_base() { }
00102 #endif
00103 
00104       ~map() { }
00105 
00106       map&
00107       operator=(const map& __x)
00108       {
00109     *static_cast<_Base*>(this) = __x;
00110     this->_M_invalidate_all();
00111     return *this;
00112       }
00113 
00114 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00115       map&
00116       operator=(map&& __x)
00117       {
00118     // NB: DR 1204.
00119     // NB: DR 675.
00120     clear();
00121     swap(__x);
00122     return *this;
00123       }
00124 
00125       map&
00126       operator=(initializer_list<value_type> __l)
00127       {
00128     this->clear();
00129     this->insert(__l);
00130     return *this;
00131       }
00132 #endif
00133 
00134       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00135       // 133. map missing get_allocator()
00136       using _Base::get_allocator;
00137 
00138       // iterators:
00139       iterator 
00140       begin()
00141       { return iterator(_Base::begin(), this); }
00142 
00143       const_iterator
00144       begin() const
00145       { return const_iterator(_Base::begin(), this); }
00146 
00147       iterator
00148       end()
00149       { return iterator(_Base::end(), this); }
00150 
00151       const_iterator
00152       end() const
00153       { return const_iterator(_Base::end(), this); }
00154 
00155       reverse_iterator
00156       rbegin()
00157       { return reverse_iterator(end()); }
00158 
00159       const_reverse_iterator
00160       rbegin() const
00161       { return const_reverse_iterator(end()); }
00162 
00163       reverse_iterator
00164       rend()
00165       { return reverse_iterator(begin()); }
00166 
00167       const_reverse_iterator
00168       rend() const
00169       { return const_reverse_iterator(begin()); }
00170 
00171 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00172       const_iterator
00173       cbegin() const
00174       { return const_iterator(_Base::begin(), this); }
00175 
00176       const_iterator
00177       cend() const
00178       { return const_iterator(_Base::end(), this); }
00179 
00180       const_reverse_iterator
00181       crbegin() const
00182       { return const_reverse_iterator(end()); }
00183 
00184       const_reverse_iterator
00185       crend() const
00186       { return const_reverse_iterator(begin()); }
00187 #endif
00188 
00189       // capacity:
00190       using _Base::empty;
00191       using _Base::size;
00192       using _Base::max_size;
00193 
00194       // 23.3.1.2 element access:
00195       using _Base::operator[];
00196 
00197       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00198       // DR 464. Suggestion for new member functions in standard containers.
00199       using _Base::at;
00200 
00201       // modifiers:
00202       std::pair<iterator, bool>
00203       insert(const value_type& __x)
00204       {
00205     typedef typename _Base::iterator _Base_iterator;
00206     std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
00207     return std::pair<iterator, bool>(iterator(__res.first, this),
00208                      __res.second);
00209       }
00210 
00211 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00212       void
00213       insert(std::initializer_list<value_type> __list)
00214       { _Base::insert(__list); }
00215 #endif
00216 
00217       iterator
00218       insert(iterator __position, const value_type& __x)
00219       {
00220     __glibcxx_check_insert(__position);
00221     return iterator(_Base::insert(__position.base(), __x), this);
00222       }
00223 
00224       template<typename _InputIterator>
00225         void
00226         insert(_InputIterator __first, _InputIterator __last)
00227         {
00228       __glibcxx_check_valid_range(__first, __last);
00229       _Base::insert(__first, __last);
00230     }
00231 
00232 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00233       iterator
00234       erase(iterator __position)
00235       {
00236     __glibcxx_check_erase(__position);
00237     __position._M_invalidate();
00238     return iterator(_Base::erase(__position.base()), this);
00239       }
00240 #else
00241       void
00242       erase(iterator __position)
00243       {
00244     __glibcxx_check_erase(__position);
00245     __position._M_invalidate();
00246     _Base::erase(__position.base());
00247       }
00248 #endif
00249 
00250       size_type
00251       erase(const key_type& __x)
00252       {
00253     iterator __victim = find(__x);
00254     if (__victim == end())
00255       return 0;
00256     else
00257     {
00258       __victim._M_invalidate();
00259       _Base::erase(__victim.base());
00260       return 1;
00261     }
00262       }
00263 
00264 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00265       iterator
00266       erase(iterator __first, iterator __last)
00267       {
00268     // _GLIBCXX_RESOLVE_LIB_DEFECTS
00269     // 151. can't currently clear() empty container
00270     __glibcxx_check_erase_range(__first, __last);
00271     while (__first != __last)
00272       this->erase(__first++);
00273     return __last;
00274       }
00275 #else
00276       void
00277       erase(iterator __first, iterator __last)
00278       {
00279     // _GLIBCXX_RESOLVE_LIB_DEFECTS
00280     // 151. can't currently clear() empty container
00281     __glibcxx_check_erase_range(__first, __last);
00282     while (__first != __last)
00283       this->erase(__first++);
00284       }
00285 #endif
00286 
00287       void
00288       swap(map& __x)
00289       {
00290     _Base::swap(__x);
00291     this->_M_swap(__x);
00292       }
00293 
00294       void
00295       clear()
00296       { this->erase(begin(), end()); }
00297 
00298       // observers:
00299       using _Base::key_comp;
00300       using _Base::value_comp;
00301 
00302       // 23.3.1.3 map operations:
00303       iterator
00304       find(const key_type& __x)
00305       { return iterator(_Base::find(__x), this); }
00306 
00307       const_iterator
00308       find(const key_type& __x) const
00309       { return const_iterator(_Base::find(__x), this); }
00310 
00311       using _Base::count;
00312 
00313       iterator
00314       lower_bound(const key_type& __x)
00315       { return iterator(_Base::lower_bound(__x), this); }
00316 
00317       const_iterator
00318       lower_bound(const key_type& __x) const
00319       { return const_iterator(_Base::lower_bound(__x), this); }
00320 
00321       iterator
00322       upper_bound(const key_type& __x)
00323       { return iterator(_Base::upper_bound(__x), this); }
00324 
00325       const_iterator
00326       upper_bound(const key_type& __x) const
00327       { return const_iterator(_Base::upper_bound(__x), this); }
00328 
00329       std::pair<iterator,iterator>
00330       equal_range(const key_type& __x)
00331       {
00332     typedef typename _Base::iterator _Base_iterator;
00333     std::pair<_Base_iterator, _Base_iterator> __res =
00334     _Base::equal_range(__x);
00335     return std::make_pair(iterator(__res.first, this),
00336                   iterator(__res.second, this));
00337       }
00338 
00339       std::pair<const_iterator,const_iterator>
00340       equal_range(const key_type& __x) const
00341       {
00342     typedef typename _Base::const_iterator _Base_const_iterator;
00343     std::pair<_Base_const_iterator, _Base_const_iterator> __res =
00344     _Base::equal_range(__x);
00345     return std::make_pair(const_iterator(__res.first, this),
00346                   const_iterator(__res.second, this));
00347       }
00348 
00349       _Base& 
00350       _M_base() { return *this; }
00351 
00352       const _Base&
00353       _M_base() const { return *this; }
00354 
00355     private:
00356       void
00357       _M_invalidate_all()
00358       {
00359     typedef typename _Base::const_iterator _Base_const_iterator;
00360     typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
00361     this->_M_invalidate_if(_Not_equal(_M_base().end()));
00362       }
00363     };
00364 
00365   template<typename _Key, typename _Tp,
00366        typename _Compare, typename _Allocator>
00367     inline bool
00368     operator==(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00369            const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
00370     { return __lhs._M_base() == __rhs._M_base(); }
00371 
00372   template<typename _Key, typename _Tp,
00373        typename _Compare, typename _Allocator>
00374     inline bool
00375     operator!=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00376            const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
00377     { return __lhs._M_base() != __rhs._M_base(); }
00378 
00379   template<typename _Key, typename _Tp,
00380        typename _Compare, typename _Allocator>
00381     inline bool
00382     operator<(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00383           const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
00384     { return __lhs._M_base() < __rhs._M_base(); }
00385 
00386   template<typename _Key, typename _Tp,
00387        typename _Compare, typename _Allocator>
00388     inline bool
00389     operator<=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00390            const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
00391     { return __lhs._M_base() <= __rhs._M_base(); }
00392 
00393   template<typename _Key, typename _Tp,
00394        typename _Compare, typename _Allocator>
00395     inline bool
00396     operator>=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00397            const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
00398     { return __lhs._M_base() >= __rhs._M_base(); }
00399 
00400   template<typename _Key, typename _Tp,
00401        typename _Compare, typename _Allocator>
00402     inline bool
00403     operator>(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00404           const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
00405     { return __lhs._M_base() > __rhs._M_base(); }
00406 
00407   template<typename _Key, typename _Tp,
00408        typename _Compare, typename _Allocator>
00409     inline void
00410     swap(map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00411      map<_Key, _Tp, _Compare, _Allocator>& __rhs)
00412     { __lhs.swap(__rhs); }
00413 
00414 } // namespace __debug
00415 } // namespace std
00416 
00417 #endif