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