map.h

Go to the documentation of this file.
00001 // Debugging map implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2003, 2004, 2005
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 2, 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 // You should have received a copy of the GNU General Public License along
00018 // with this library; see the file COPYING.  If not, write to the Free
00019 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00020 // USA.
00021 
00022 // As a special exception, you may use this file as part of a free software
00023 // library without restriction.  Specifically, if other files instantiate
00024 // templates or use macros or inline functions from this file, or you compile
00025 // this file and link it with other files to produce an executable, this
00026 // file does not by itself cause the resulting executable to be covered by
00027 // the GNU General Public License.  This exception does not however
00028 // invalidate any other reasons why the executable file might be covered by
00029 // the GNU General Public License.
00030 
00031 /** @file debug/map.h
00032  *  This file is a GNU debug extension to the Standard C++ Library.
00033  */
00034 
00035 #ifndef _GLIBCXX_DEBUG_MAP_H
00036 #define _GLIBCXX_DEBUG_MAP_H 1
00037 
00038 #include <debug/safe_sequence.h>
00039 #include <debug/safe_iterator.h>
00040 #include <utility>
00041 
00042 namespace std
00043 {
00044 namespace __debug
00045 {
00046   template<typename _Key, typename _Tp, typename _Compare = std::less<_Key>,
00047        typename _Allocator = std::allocator<std::pair<const _Key, _Tp> > >
00048     class map
00049     : public _GLIBCXX_STD::map<_Key, _Tp, _Compare, _Allocator>,
00050       public __gnu_debug::_Safe_sequence<map<_Key, _Tp, _Compare, _Allocator> >
00051     {
00052       typedef _GLIBCXX_STD::map<_Key, _Tp, _Compare, _Allocator> _Base;
00053       typedef __gnu_debug::_Safe_sequence<map> _Safe_base;
00054 
00055     public:
00056       // types:
00057       typedef _Key                                  key_type;
00058       typedef _Tp                                   mapped_type;
00059       typedef std::pair<const _Key, _Tp>            value_type;
00060       typedef _Compare                              key_compare;
00061       typedef _Allocator                            allocator_type;
00062       typedef typename _Base::reference             reference;
00063       typedef typename _Base::const_reference       const_reference;
00064 
00065       typedef __gnu_debug::_Safe_iterator<typename _Base::iterator, map>
00066                                                     iterator;
00067       typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator, map>
00068                                                     const_iterator;
00069 
00070       typedef typename _Base::size_type             size_type;
00071       typedef typename _Base::difference_type       difference_type;
00072       typedef typename _Base::pointer               pointer;
00073       typedef typename _Base::const_pointer         const_pointer;
00074       typedef std::reverse_iterator<iterator>       reverse_iterator;
00075       typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00076 
00077       using _Base::value_compare;
00078 
00079       // 23.3.1.1 construct/copy/destroy:
00080       explicit map(const _Compare& __comp = _Compare(),
00081            const _Allocator& __a = _Allocator())
00082       : _Base(__comp, __a) { }
00083 
00084       template<typename _InputIterator>
00085         map(_InputIterator __first, _InputIterator __last,
00086         const _Compare& __comp = _Compare(),
00087         const _Allocator& __a = _Allocator())
00088     : _Base(__gnu_debug::__check_valid_range(__first, __last), __last,
00089         __comp, __a), _Safe_base() { }
00090 
00091       map(const map<_Key,_Tp,_Compare,_Allocator>& __x)
00092       : _Base(__x), _Safe_base() { }
00093 
00094       map(const _Base& __x) : _Base(__x), _Safe_base() { }
00095 
00096       ~map() { }
00097 
00098       map<_Key,_Tp,_Compare,_Allocator>&
00099       operator=(const map<_Key,_Tp,_Compare,_Allocator>& __x)
00100       {
00101     *static_cast<_Base*>(this) = __x;
00102     this->_M_invalidate_all();
00103     return *this;
00104       }
00105 
00106       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00107       // 133. map missing get_allocator()
00108       using _Base::get_allocator;
00109 
00110       // iterators:
00111       iterator 
00112       begin()
00113       { return iterator(_Base::begin(), this); }
00114 
00115       const_iterator
00116       begin() const
00117       { return const_iterator(_Base::begin(), this); }
00118 
00119       iterator
00120       end()
00121       { return iterator(_Base::end(), this); }
00122 
00123       const_iterator
00124       end() const
00125       { return const_iterator(_Base::end(), this); }
00126 
00127       reverse_iterator
00128       rbegin()
00129       { return reverse_iterator(end()); }
00130 
00131       const_reverse_iterator
00132       rbegin() const
00133       { return const_reverse_iterator(end()); }
00134 
00135       reverse_iterator
00136       rend()
00137       { return reverse_iterator(begin()); }
00138 
00139       const_reverse_iterator
00140       rend() const
00141       { return const_reverse_iterator(begin()); }
00142 
00143       // capacity:
00144       using _Base::empty;
00145       using _Base::size;
00146       using _Base::max_size;
00147 
00148       // 23.3.1.2 element access:
00149       using _Base::operator[];
00150 
00151       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00152       // DR 464. Suggestion for new member functions in standard containers.
00153       using _Base::at;
00154 
00155       // modifiers:
00156       std::pair<iterator, bool>
00157       insert(const value_type& __x)
00158       {
00159     typedef typename _Base::iterator _Base_iterator;
00160     std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
00161     return std::pair<iterator, bool>(iterator(__res.first, this),
00162                      __res.second);
00163       }
00164 
00165       iterator
00166       insert(iterator __position, const value_type& __x)
00167       {
00168     __glibcxx_check_insert(__position);
00169     return iterator(_Base::insert(__position.base(), __x), this);
00170       }
00171 
00172       template<typename _InputIterator>
00173         void
00174         insert(_InputIterator __first, _InputIterator __last)
00175         {
00176       __glibcxx_check_valid_range(__first, __last);
00177       _Base::insert(__first, __last);
00178     }
00179 
00180       void
00181       erase(iterator __position)
00182       {
00183     __glibcxx_check_erase(__position);
00184     __position._M_invalidate();
00185     _Base::erase(__position.base());
00186       }
00187 
00188       size_type
00189       erase(const key_type& __x)
00190       {
00191     iterator __victim = find(__x);
00192     if (__victim == end())
00193       return 0;
00194     else
00195     {
00196       __victim._M_invalidate();
00197       _Base::erase(__victim.base());
00198       return 1;
00199     }
00200       }
00201 
00202       void
00203       erase(iterator __first, iterator __last)
00204       {
00205     // _GLIBCXX_RESOLVE_LIB_DEFECTS
00206     // 151. can't currently clear() empty container
00207     __glibcxx_check_erase_range(__first, __last);
00208     while (__first != __last)
00209       this->erase(__first++);
00210       }
00211 
00212       void
00213       swap(map<_Key,_Tp,_Compare,_Allocator>& __x)
00214       {
00215     _Base::swap(__x);
00216     this->_M_swap(__x);
00217       }
00218 
00219       void
00220       clear()
00221       { this->erase(begin(), end()); }
00222 
00223       // observers:
00224       using _Base::key_comp;
00225       using _Base::value_comp;
00226 
00227       // 23.3.1.3 map operations:
00228       iterator
00229       find(const key_type& __x)
00230       { return iterator(_Base::find(__x), this); }
00231 
00232       const_iterator
00233       find(const key_type& __x) const
00234       { return const_iterator(_Base::find(__x), this); }
00235 
00236       using _Base::count;
00237 
00238       iterator
00239       lower_bound(const key_type& __x)
00240       { return iterator(_Base::lower_bound(__x), this); }
00241 
00242       const_iterator
00243       lower_bound(const key_type& __x) const
00244       { return const_iterator(_Base::lower_bound(__x), this); }
00245 
00246       iterator
00247       upper_bound(const key_type& __x)
00248       { return iterator(_Base::upper_bound(__x), this); }
00249 
00250       const_iterator
00251       upper_bound(const key_type& __x) const
00252       { return const_iterator(_Base::upper_bound(__x), this); }
00253 
00254       std::pair<iterator,iterator>
00255       equal_range(const key_type& __x)
00256       {
00257     typedef typename _Base::iterator _Base_iterator;
00258     std::pair<_Base_iterator, _Base_iterator> __res =
00259     _Base::equal_range(__x);
00260     return std::make_pair(iterator(__res.first, this),
00261                   iterator(__res.second, this));
00262       }
00263 
00264       std::pair<const_iterator,const_iterator>
00265       equal_range(const key_type& __x) const
00266       {
00267     typedef typename _Base::const_iterator _Base_const_iterator;
00268     std::pair<_Base_const_iterator, _Base_const_iterator> __res =
00269     _Base::equal_range(__x);
00270     return std::make_pair(const_iterator(__res.first, this),
00271                   const_iterator(__res.second, this));
00272       }
00273 
00274       _Base& 
00275       _M_base() { return *this; }
00276 
00277       const _Base&
00278       _M_base() const { return *this; }
00279 
00280     private:
00281       void
00282       _M_invalidate_all()
00283       {
00284     typedef typename _Base::const_iterator _Base_const_iterator;
00285     typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
00286     this->_M_invalidate_if(_Not_equal(_M_base().end()));
00287       }
00288     };
00289 
00290   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
00291     inline bool
00292     operator==(const map<_Key,_Tp,_Compare,_Allocator>& __lhs,
00293            const map<_Key,_Tp,_Compare,_Allocator>& __rhs)
00294     { return __lhs._M_base() == __rhs._M_base(); }
00295 
00296   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
00297     inline bool
00298     operator!=(const map<_Key,_Tp,_Compare,_Allocator>& __lhs,
00299            const map<_Key,_Tp,_Compare,_Allocator>& __rhs)
00300     { return __lhs._M_base() != __rhs._M_base(); }
00301 
00302   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
00303     inline bool
00304     operator<(const map<_Key,_Tp,_Compare,_Allocator>& __lhs,
00305           const map<_Key,_Tp,_Compare,_Allocator>& __rhs)
00306     { return __lhs._M_base() < __rhs._M_base(); }
00307 
00308   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
00309     inline bool
00310     operator<=(const map<_Key,_Tp,_Compare,_Allocator>& __lhs,
00311            const map<_Key,_Tp,_Compare,_Allocator>& __rhs)
00312     { return __lhs._M_base() <= __rhs._M_base(); }
00313 
00314   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
00315     inline bool
00316     operator>=(const map<_Key,_Tp,_Compare,_Allocator>& __lhs,
00317            const map<_Key,_Tp,_Compare,_Allocator>& __rhs)
00318     { return __lhs._M_base() >= __rhs._M_base(); }
00319 
00320   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
00321     inline bool
00322     operator>(const map<_Key,_Tp,_Compare,_Allocator>& __lhs,
00323           const map<_Key,_Tp,_Compare,_Allocator>& __rhs)
00324     { return __lhs._M_base() > __rhs._M_base(); }
00325 
00326   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
00327     inline void
00328     swap(map<_Key,_Tp,_Compare,_Allocator>& __lhs,
00329      map<_Key,_Tp,_Compare,_Allocator>& __rhs)
00330     { __lhs.swap(__rhs); }
00331 } // namespace __debug
00332 } // namespace std
00333 
00334 #endif

Generated on Thu Nov 1 13:12:09 2007 for libstdc++ by  doxygen 1.5.1