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 #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 _Base::reference             reference;
00057       typedef typename _Base::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 _Base::pointer               pointer;
00067       typedef typename _Base::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       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00146       // DR 464. Suggestion for new member functions in standard containers.
00147       using _Base::at;
00148 
00149       // modifiers:
00150       std::pair<iterator, bool>
00151       insert(const value_type& __x)
00152       {
00153     typedef typename _Base::iterator _Base_iterator;
00154     std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
00155     return std::pair<iterator, bool>(iterator(__res.first, this),
00156                      __res.second);
00157       }
00158 
00159       iterator
00160       insert(iterator __position, const value_type& __x)
00161       {
00162     __glibcxx_check_insert(__position);
00163     return iterator(_Base::insert(__position.base(), __x), this);
00164       }
00165 
00166       template<typename _InputIterator>
00167         void
00168         insert(_InputIterator __first, _InputIterator __last)
00169         {
00170       __glibcxx_check_valid_range(__first, __last);
00171       _Base::insert(__first, __last);
00172     }
00173 
00174       void
00175       erase(iterator __position)
00176       {
00177     __glibcxx_check_erase(__position);
00178     __position._M_invalidate();
00179     _Base::erase(__position.base());
00180       }
00181 
00182       size_type
00183       erase(const key_type& __x)
00184       {
00185     iterator __victim = find(__x);
00186     if (__victim == end())
00187       return 0;
00188     else
00189     {
00190       __victim._M_invalidate();
00191       _Base::erase(__victim.base());
00192       return 1;
00193     }
00194       }
00195 
00196       void
00197       erase(iterator __first, iterator __last)
00198       {
00199     // _GLIBCXX_RESOLVE_LIB_DEFECTS
00200     // 151. can't currently clear() empty container
00201     __glibcxx_check_erase_range(__first, __last);
00202     while (__first != __last)
00203       this->erase(__first++);
00204       }
00205 
00206       void
00207       swap(map<_Key,_Tp,_Compare,_Allocator>& __x)
00208       {
00209     _Base::swap(__x);
00210     this->_M_swap(__x);
00211       }
00212 
00213       void
00214       clear()
00215       { this->erase(begin(), end()); }
00216 
00217       // observers:
00218       using _Base::key_comp;
00219       using _Base::value_comp;
00220 
00221       // 23.3.1.3 map operations:
00222       iterator
00223       find(const key_type& __x)
00224       { return iterator(_Base::find(__x), this); }
00225 
00226       const_iterator
00227       find(const key_type& __x) const
00228       { return const_iterator(_Base::find(__x), this); }
00229 
00230       using _Base::count;
00231 
00232       iterator
00233       lower_bound(const key_type& __x)
00234       { return iterator(_Base::lower_bound(__x), this); }
00235 
00236       const_iterator
00237       lower_bound(const key_type& __x) const
00238       { return const_iterator(_Base::lower_bound(__x), this); }
00239 
00240       iterator
00241       upper_bound(const key_type& __x)
00242       { return iterator(_Base::upper_bound(__x), this); }
00243 
00244       const_iterator
00245       upper_bound(const key_type& __x) const
00246       { return const_iterator(_Base::upper_bound(__x), this); }
00247 
00248       std::pair<iterator,iterator>
00249       equal_range(const key_type& __x)
00250       {
00251     typedef typename _Base::iterator _Base_iterator;
00252     std::pair<_Base_iterator, _Base_iterator> __res =
00253     _Base::equal_range(__x);
00254     return std::make_pair(iterator(__res.first, this),
00255                   iterator(__res.second, this));
00256       }
00257 
00258       std::pair<const_iterator,const_iterator>
00259       equal_range(const key_type& __x) const
00260       {
00261     typedef typename _Base::const_iterator _Base_const_iterator;
00262     std::pair<_Base_const_iterator, _Base_const_iterator> __res =
00263     _Base::equal_range(__x);
00264     return std::make_pair(const_iterator(__res.first, this),
00265                   const_iterator(__res.second, this));
00266       }
00267 
00268       _Base& 
00269       _M_base() { return *this; }
00270 
00271       const _Base&
00272       _M_base() const { return *this; }
00273 
00274     private:
00275       void
00276       _M_invalidate_all()
00277       {
00278     typedef typename _Base::const_iterator _Base_const_iterator;
00279     typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
00280     this->_M_invalidate_if(_Not_equal(_M_base().end()));
00281       }
00282     };
00283 
00284   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
00285     inline bool
00286     operator==(const map<_Key,_Tp,_Compare,_Allocator>& __lhs,
00287            const map<_Key,_Tp,_Compare,_Allocator>& __rhs)
00288     { return __lhs._M_base() == __rhs._M_base(); }
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 void
00322     swap(map<_Key,_Tp,_Compare,_Allocator>& __lhs,
00323      map<_Key,_Tp,_Compare,_Allocator>& __rhs)
00324     { __lhs.swap(__rhs); }
00325 } // namespace __gnu_debug_def
00326 
00327 #endif

Generated on Tue Feb 2 16:56:15 2010 for GNU C++ STL by  doxygen 1.4.7