map.h

Go to the documentation of this file.
00001 // Debugging map implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2003, 2004
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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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 #ifndef _GLIBCXX_DEBUG_MAP_H
00032 #define _GLIBCXX_DEBUG_MAP_H 1
00033 
00034 #include <debug/safe_sequence.h>
00035 #include <debug/safe_iterator.h>
00036 #include <utility>
00037 
00038 namespace __gnu_debug_def
00039 {
00040   template<typename _Key, typename _Tp, typename _Compare = std::less<_Key>,
00041        typename _Allocator = std::allocator<std::pair<const _Key, _Tp> > >
00042     class map
00043     : public _GLIBCXX_STD::map<_Key, _Tp, _Compare, _Allocator>,
00044       public __gnu_debug::_Safe_sequence<map<_Key, _Tp, _Compare, _Allocator> >
00045     {
00046       typedef _GLIBCXX_STD::map<_Key, _Tp, _Compare, _Allocator> _Base;
00047       typedef __gnu_debug::_Safe_sequence<map> _Safe_base;
00048 
00049     public:
00050       // types:
00051       typedef _Key                                  key_type;
00052       typedef _Tp                                   mapped_type;
00053       typedef std::pair<const _Key, _Tp>            value_type;
00054       typedef _Compare                              key_compare;
00055       typedef _Allocator                            allocator_type;
00056       typedef typename _Allocator::reference        reference;
00057       typedef typename _Allocator::const_reference  const_reference;
00058 
00059       typedef __gnu_debug::_Safe_iterator<typename _Base::iterator, map>
00060                                                     iterator;
00061       typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator, map>
00062                                                     const_iterator;
00063 
00064       typedef typename _Base::size_type             size_type;
00065       typedef typename _Base::difference_type       difference_type;
00066       typedef typename _Allocator::pointer          pointer;
00067       typedef typename _Allocator::const_pointer    const_pointer;
00068       typedef std::reverse_iterator<iterator>       reverse_iterator;
00069       typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00070 
00071       using _Base::value_compare;
00072 
00073       // 23.3.1.1 construct/copy/destroy:
00074       explicit map(const _Compare& __comp = _Compare(),
00075            const _Allocator& __a = _Allocator())
00076       : _Base(__comp, __a) { }
00077 
00078       template<typename _InputIterator>
00079         map(_InputIterator __first, _InputIterator __last,
00080         const _Compare& __comp = _Compare(),
00081         const _Allocator& __a = _Allocator())
00082     : _Base(__gnu_debug::__check_valid_range(__first, __last), __last,
00083         __comp, __a), _Safe_base() { }
00084 
00085       map(const map<_Key,_Tp,_Compare,_Allocator>& __x)
00086       : _Base(__x), _Safe_base() { }
00087 
00088       map(const _Base& __x) : _Base(__x), _Safe_base() { }
00089 
00090       ~map() { }
00091 
00092       map<_Key,_Tp,_Compare,_Allocator>&
00093       operator=(const map<_Key,_Tp,_Compare,_Allocator>& __x)
00094       {
00095     *static_cast<_Base*>(this) = __x;
00096     this->_M_invalidate_all();
00097     return *this;
00098       }
00099 
00100       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00101       // 133. map missing get_allocator()
00102       using _Base::get_allocator;
00103 
00104       // iterators:
00105       iterator 
00106       begin()
00107       { return iterator(_Base::begin(), this); }
00108 
00109       const_iterator
00110       begin() const
00111       { return const_iterator(_Base::begin(), this); }
00112 
00113       iterator
00114       end()
00115       { return iterator(_Base::end(), this); }
00116 
00117       const_iterator
00118       end() const
00119       { return const_iterator(_Base::end(), this); }
00120 
00121       reverse_iterator
00122       rbegin()
00123       { return reverse_iterator(end()); }
00124 
00125       const_reverse_iterator
00126       rbegin() const
00127       { return const_reverse_iterator(end()); }
00128 
00129       reverse_iterator
00130       rend()
00131       { return reverse_iterator(begin()); }
00132 
00133       const_reverse_iterator
00134       rend() const
00135       { return const_reverse_iterator(begin()); }
00136 
00137       // capacity:
00138       using _Base::empty;
00139       using _Base::size;
00140       using _Base::max_size;
00141 
00142       // 23.3.1.2 element access:
00143       using _Base::operator[];
00144 
00145       // modifiers:
00146       std::pair<iterator, bool>
00147       insert(const value_type& __x)
00148       {
00149     typedef typename _Base::iterator _Base_iterator;
00150     std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
00151     return std::pair<iterator, bool>(iterator(__res.first, this),
00152                      __res.second);
00153       }
00154 
00155       iterator
00156       insert(iterator __position, const value_type& __x)
00157       {
00158     __glibcxx_check_insert(__position);
00159     return iterator(_Base::insert(__position.base(), __x), this);
00160       }
00161 
00162       template<typename _InputIterator>
00163         void
00164         insert(_InputIterator __first, _InputIterator __last)
00165         {
00166       __glibcxx_check_valid_range(__first, __last);
00167       _Base::insert(__first, __last);
00168     }
00169 
00170       void
00171       erase(iterator __position)
00172       {
00173     __glibcxx_check_erase(__position);
00174     __position._M_invalidate();
00175     _Base::erase(__position.base());
00176       }
00177 
00178       size_type
00179       erase(const key_type& __x)
00180       {
00181     iterator __victim = find(__x);
00182     if (__victim == end())
00183       return 0;
00184     else
00185     {
00186       __victim._M_invalidate();
00187       _Base::erase(__victim.base());
00188       return 1;
00189     }
00190       }
00191 
00192       void
00193       erase(iterator __first, iterator __last)
00194       {
00195     // _GLIBCXX_RESOLVE_LIB_DEFECTS
00196     // 151. can't currently clear() empty container
00197     __glibcxx_check_erase_range(__first, __last);
00198     while (__first != __last)
00199       this->erase(__first++);
00200       }
00201 
00202       void
00203       swap(map<_Key,_Tp,_Compare,_Allocator>& __x)
00204       {
00205     _Base::swap(__x);
00206     this->_M_swap(__x);
00207       }
00208 
00209       void
00210       clear()
00211       { this->erase(begin(), end()); }
00212 
00213       // observers:
00214       using _Base::key_comp;
00215       using _Base::value_comp;
00216 
00217       // 23.3.1.3 map operations:
00218       iterator
00219       find(const key_type& __x)
00220       { return iterator(_Base::find(__x), this); }
00221 
00222       const_iterator
00223       find(const key_type& __x) const
00224       { return const_iterator(_Base::find(__x), this); }
00225 
00226       using _Base::count;
00227 
00228       iterator
00229       lower_bound(const key_type& __x)
00230       { return iterator(_Base::lower_bound(__x), this); }
00231 
00232       const_iterator
00233       lower_bound(const key_type& __x) const
00234       { return const_iterator(_Base::lower_bound(__x), this); }
00235 
00236       iterator
00237       upper_bound(const key_type& __x)
00238       { return iterator(_Base::upper_bound(__x), this); }
00239 
00240       const_iterator
00241       upper_bound(const key_type& __x) const
00242       { return const_iterator(_Base::upper_bound(__x), this); }
00243 
00244       std::pair<iterator,iterator>
00245       equal_range(const key_type& __x)
00246       {
00247     typedef typename _Base::iterator _Base_iterator;
00248     std::pair<_Base_iterator, _Base_iterator> __res =
00249     _Base::equal_range(__x);
00250     return std::make_pair(iterator(__res.first, this),
00251                   iterator(__res.second, this));
00252       }
00253 
00254       std::pair<const_iterator,const_iterator>
00255       equal_range(const key_type& __x) const
00256       {
00257     typedef typename _Base::const_iterator _Base_const_iterator;
00258     std::pair<_Base_const_iterator, _Base_const_iterator> __res =
00259     _Base::equal_range(__x);
00260     return std::make_pair(const_iterator(__res.first, this),
00261                   const_iterator(__res.second, this));
00262       }
00263 
00264       _Base& 
00265       _M_base() { return *this; }
00266 
00267       const _Base&
00268       _M_base() const { return *this; }
00269 
00270     private:
00271       void
00272       _M_invalidate_all()
00273       {
00274     typedef typename _Base::const_iterator _Base_const_iterator;
00275     typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
00276     this->_M_invalidate_if(_Not_equal(_M_base().end()));
00277       }
00278     };
00279 
00280   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
00281     inline bool
00282     operator==(const map<_Key,_Tp,_Compare,_Allocator>& __lhs,
00283            const map<_Key,_Tp,_Compare,_Allocator>& __rhs)
00284     { return __lhs._M_base() == __rhs._M_base(); }
00285 
00286   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
00287     inline bool
00288     operator!=(const map<_Key,_Tp,_Compare,_Allocator>& __lhs,
00289            const map<_Key,_Tp,_Compare,_Allocator>& __rhs)
00290     { return __lhs._M_base() != __rhs._M_base(); }
00291 
00292   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
00293     inline bool
00294     operator<(const map<_Key,_Tp,_Compare,_Allocator>& __lhs,
00295           const map<_Key,_Tp,_Compare,_Allocator>& __rhs)
00296     { return __lhs._M_base() < __rhs._M_base(); }
00297 
00298   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
00299     inline bool
00300     operator<=(const map<_Key,_Tp,_Compare,_Allocator>& __lhs,
00301            const map<_Key,_Tp,_Compare,_Allocator>& __rhs)
00302     { return __lhs._M_base() <= __rhs._M_base(); }
00303 
00304   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
00305     inline bool
00306     operator>=(const map<_Key,_Tp,_Compare,_Allocator>& __lhs,
00307            const map<_Key,_Tp,_Compare,_Allocator>& __rhs)
00308     { return __lhs._M_base() >= __rhs._M_base(); }
00309 
00310   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
00311     inline bool
00312     operator>(const map<_Key,_Tp,_Compare,_Allocator>& __lhs,
00313           const map<_Key,_Tp,_Compare,_Allocator>& __rhs)
00314     { return __lhs._M_base() > __rhs._M_base(); }
00315 
00316   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
00317     inline void
00318     swap(map<_Key,_Tp,_Compare,_Allocator>& __lhs,
00319      map<_Key,_Tp,_Compare,_Allocator>& __rhs)
00320     { __lhs.swap(__rhs); }
00321 } // namespace __gnu_debug_def
00322 
00323 #endif

Generated on Tue Jan 30 17:31:51 2007 for GNU C++ STL by doxygen 1.3.6