debug/unordered_map

Go to the documentation of this file.
00001 // Debugging unordered_map/unordered_multimap implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 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/unordered_map
00027  *  This file is a GNU debug extension to the Standard C++ Library.
00028  */
00029 
00030 #ifndef _GLIBCXX_DEBUG_UNORDERED_MAP
00031 #define _GLIBCXX_DEBUG_UNORDERED_MAP 1
00032 
00033 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00034 # include <unordered_map>
00035 #else
00036 # include <bits/c++0x_warning.h>
00037 #endif
00038 
00039 #include <debug/safe_sequence.h>
00040 #include <debug/safe_iterator.h>
00041 #include <initializer_list>
00042 
00043 namespace std
00044 {
00045 namespace __debug
00046 {
00047   /// Class std::unordered_map with safety/checking/debug instrumentation.
00048   template<typename _Key, typename _Tp,
00049        typename _Hash = std::hash<_Key>,
00050        typename _Pred = std::equal_to<_Key>,
00051        typename _Alloc = std::allocator<_Key> >
00052     class unordered_map
00053     : public _GLIBCXX_STD_D::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>,
00054       public __gnu_debug::_Safe_sequence<unordered_map<_Key, _Tp, _Hash,
00055                                _Pred, _Alloc> >
00056     {
00057       typedef _GLIBCXX_STD_D::unordered_map<_Key, _Tp, _Hash,
00058                         _Pred, _Alloc> _Base;
00059       typedef __gnu_debug::_Safe_sequence<unordered_map> _Safe_base;
00060 
00061     public:
00062       typedef typename _Base::size_type       size_type;
00063       typedef typename _Base::hasher          hasher;
00064       typedef typename _Base::key_equal       key_equal;
00065       typedef typename _Base::allocator_type  allocator_type;
00066 
00067       typedef typename _Base::key_type        key_type;
00068       typedef typename _Base::value_type      value_type;
00069 
00070       typedef __gnu_debug::_Safe_iterator<typename _Base::iterator,
00071                       unordered_map> iterator;
00072       typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
00073                       unordered_map> const_iterator;
00074 
00075       explicit
00076       unordered_map(size_type __n = 10,
00077             const hasher& __hf = hasher(),
00078             const key_equal& __eql = key_equal(),
00079             const allocator_type& __a = allocator_type())
00080       : _Base(__n, __hf, __eql, __a) { }
00081 
00082       template<typename _InputIterator>
00083         unordered_map(_InputIterator __f, _InputIterator __l, 
00084               size_type __n = 10,
00085               const hasher& __hf = hasher(), 
00086               const key_equal& __eql = key_equal(), 
00087               const allocator_type& __a = allocator_type())
00088     : _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n,
00089         __hf, __eql, __a), _Safe_base() { }
00090 
00091       unordered_map(const unordered_map& __x) 
00092       : _Base(__x), _Safe_base() { }
00093 
00094       unordered_map(const _Base& __x)
00095       : _Base(__x), _Safe_base() { }
00096 
00097       unordered_map(unordered_map&& __x)
00098       : _Base(std::forward<unordered_map>(__x)), _Safe_base() { }
00099 
00100       unordered_map(initializer_list<value_type> __l,
00101             size_type __n = 10,
00102             const hasher& __hf = hasher(),
00103             const key_equal& __eql = key_equal(),
00104             const allocator_type& __a = allocator_type())
00105       : _Base(__l, __n, __hf, __eql, __a), _Safe_base() { }
00106 
00107       unordered_map&
00108       operator=(const unordered_map& __x)
00109       {
00110     *static_cast<_Base*>(this) = __x;
00111     this->_M_invalidate_all();
00112     return *this;
00113       }
00114 
00115       unordered_map&
00116       operator=(unordered_map&& __x)
00117       {
00118     // NB: DR 1204.
00119     // NB: DR 675.
00120     clear();
00121     swap(__x);
00122     return *this;
00123       }
00124 
00125       unordered_map&
00126       operator=(initializer_list<value_type> __l)
00127       {
00128     this->clear();
00129     this->insert(__l);
00130     return *this;
00131       }
00132 
00133       void
00134       swap(unordered_map& __x)
00135       {
00136     _Base::swap(__x);
00137     _Safe_base::_M_swap(__x);
00138       }
00139 
00140       void
00141       clear()
00142       {
00143     _Base::clear();
00144     this->_M_invalidate_all();
00145       }
00146 
00147       iterator 
00148       begin()
00149       { return iterator(_Base::begin(), this); }
00150 
00151       const_iterator
00152       begin() const
00153       { return const_iterator(_Base::begin(), this); }
00154 
00155       iterator
00156       end()
00157       { return iterator(_Base::end(), this); }
00158 
00159       const_iterator
00160       end() const
00161       { return const_iterator(_Base::end(), this); }
00162 
00163       const_iterator
00164       cbegin() const
00165       { return const_iterator(_Base::begin(), this); }
00166 
00167       const_iterator
00168       cend() const
00169       { return const_iterator(_Base::end(), this); }
00170 
00171       // local versions
00172       using _Base::begin;
00173       using _Base::end;
00174       using _Base::cbegin;
00175       using _Base::cend;
00176 
00177       std::pair<iterator, bool>
00178       insert(const value_type& __obj)
00179       {
00180     typedef std::pair<typename _Base::iterator, bool> __pair_type;
00181     __pair_type __res = _Base::insert(__obj);
00182     return std::make_pair(iterator(__res.first, this), __res.second);
00183       }
00184 
00185       iterator
00186       insert(const_iterator, const value_type& __obj)
00187       {
00188     typedef std::pair<typename _Base::iterator, bool> __pair_type;
00189     __pair_type __res = _Base::insert(__obj);
00190     return iterator(__res.first, this);
00191       }
00192 
00193       void
00194       insert(std::initializer_list<value_type> __l)
00195       { _Base::insert(__l); }
00196 
00197       template<typename _InputIterator>
00198         void
00199         insert(_InputIterator __first, _InputIterator __last)
00200         {
00201       __glibcxx_check_valid_range(__first, __last);
00202       _Base::insert(__first, __last);
00203     }
00204 
00205       iterator
00206       find(const key_type& __key)
00207       { return iterator(_Base::find(__key), this); }
00208 
00209       const_iterator
00210       find(const key_type& __key) const
00211       { return const_iterator(_Base::find(__key), this); }
00212 
00213       std::pair<iterator, iterator>
00214       equal_range(const key_type& __key)
00215       {
00216     typedef typename _Base::iterator _Base_iterator;
00217     typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
00218     __pair_type __res = _Base::equal_range(__key);
00219     return std::make_pair(iterator(__res.first, this),
00220                   iterator(__res.second, this));
00221       }
00222 
00223       std::pair<const_iterator, const_iterator>
00224       equal_range(const key_type& __key) const
00225       {
00226     typedef typename _Base::const_iterator _Base_iterator;
00227     typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
00228     __pair_type __res = _Base::equal_range(__key);
00229     return std::make_pair(const_iterator(__res.first, this),
00230                   const_iterator(__res.second, this));
00231       }
00232 
00233       size_type
00234       erase(const key_type& __key)
00235       {
00236     size_type __ret(0);
00237     iterator __victim(_Base::find(__key), this);
00238     if (__victim != end())
00239       {
00240         this->erase(__victim);
00241         __ret = 1;
00242       }
00243     return __ret;
00244       }
00245 
00246       iterator
00247       erase(const_iterator __it)
00248       {
00249     __glibcxx_check_erase(__it);
00250     __it._M_invalidate();
00251     return iterator(_Base::erase(__it.base()), this);
00252       }
00253 
00254       iterator
00255       erase(const_iterator __first, const_iterator __last)
00256       {
00257     __glibcxx_check_erase_range(__first, __last);
00258     for (const_iterator __tmp = __first; __tmp != __last;)
00259     {
00260       const_iterator __victim = __tmp++;
00261       __victim._M_invalidate();
00262     }
00263     return iterator(_Base::erase(__first.base(),
00264                      __last.base()), this);
00265       }
00266 
00267       _Base&
00268       _M_base() { return *this; }
00269 
00270       const _Base&
00271       _M_base() const { return *this; }
00272 
00273     private:
00274       void
00275       _M_invalidate_all()
00276       {
00277     typedef typename _Base::const_iterator _Base_const_iterator;
00278     typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
00279     this->_M_invalidate_if(_Not_equal(_M_base().end()));
00280       }
00281     };
00282 
00283   template<typename _Key, typename _Tp, typename _Hash,
00284        typename _Pred, typename _Alloc>
00285     inline void
00286     swap(unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
00287      unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
00288     { __x.swap(__y); }
00289 
00290 
00291   /// Class std::unordered_multimap with safety/checking/debug instrumentation.
00292   template<typename _Key, typename _Tp,
00293        typename _Hash = std::hash<_Key>,
00294        typename _Pred = std::equal_to<_Key>,
00295        typename _Alloc = std::allocator<_Key> >
00296     class unordered_multimap
00297     : public _GLIBCXX_STD_D::unordered_multimap<_Key, _Tp, _Hash,
00298                         _Pred, _Alloc>,
00299       public __gnu_debug::_Safe_sequence<unordered_multimap<_Key, _Tp, _Hash,
00300                                 _Pred, _Alloc> >
00301     {
00302       typedef _GLIBCXX_STD_D::unordered_multimap<_Key, _Tp, _Hash,
00303                          _Pred, _Alloc> _Base;
00304       typedef __gnu_debug::_Safe_sequence<unordered_multimap> _Safe_base;
00305 
00306     public:
00307       typedef typename _Base::size_type       size_type;
00308       typedef typename _Base::hasher          hasher;
00309       typedef typename _Base::key_equal       key_equal;
00310       typedef typename _Base::allocator_type  allocator_type;
00311 
00312       typedef typename _Base::key_type        key_type;
00313       typedef typename _Base::value_type      value_type;
00314 
00315       typedef __gnu_debug::_Safe_iterator<typename _Base::iterator,
00316                       unordered_multimap> iterator;
00317       typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
00318                       unordered_multimap> const_iterator;
00319 
00320       explicit
00321       unordered_multimap(size_type __n = 10,
00322              const hasher& __hf = hasher(),
00323              const key_equal& __eql = key_equal(),
00324              const allocator_type& __a = allocator_type())
00325       : _Base(__n, __hf, __eql, __a) { }
00326 
00327       template<typename _InputIterator>
00328         unordered_multimap(_InputIterator __f, _InputIterator __l, 
00329                size_type __n = 10,
00330                const hasher& __hf = hasher(), 
00331                const key_equal& __eql = key_equal(), 
00332                const allocator_type& __a = allocator_type())
00333     : _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n,
00334         __hf, __eql, __a), _Safe_base() { }
00335 
00336       unordered_multimap(const unordered_multimap& __x) 
00337       : _Base(__x), _Safe_base() { }
00338 
00339       unordered_multimap(const _Base& __x) 
00340       : _Base(__x), _Safe_base() { }
00341 
00342       unordered_multimap(unordered_multimap&& __x) 
00343       : _Base(std::forward<unordered_multimap>(__x)), _Safe_base() { }
00344 
00345       unordered_multimap(initializer_list<value_type> __l,
00346              size_type __n = 10,
00347              const hasher& __hf = hasher(),
00348              const key_equal& __eql = key_equal(),
00349              const allocator_type& __a = allocator_type())
00350       : _Base(__l, __n, __hf, __eql, __a), _Safe_base() { }
00351 
00352       unordered_multimap&
00353       operator=(const unordered_multimap& __x)
00354       {
00355     *static_cast<_Base*>(this) = __x;
00356     this->_M_invalidate_all();
00357     return *this;
00358       }
00359 
00360       unordered_multimap&
00361       operator=(unordered_multimap&& __x)
00362       {
00363     // NB: DR 1204.
00364     // NB: DR 675.
00365     clear();
00366     swap(__x);
00367     return *this;
00368       }
00369 
00370       unordered_multimap&
00371       operator=(initializer_list<value_type> __l)
00372       {
00373     this->clear();
00374     this->insert(__l);
00375     return *this;
00376       }
00377 
00378       void
00379       swap(unordered_multimap& __x)
00380       {
00381     _Base::swap(__x);
00382     _Safe_base::_M_swap(__x);
00383       }
00384 
00385       void
00386       clear()
00387       {
00388     _Base::clear();
00389     this->_M_invalidate_all();
00390       }
00391 
00392       iterator 
00393       begin()
00394       { return iterator(_Base::begin(), this); }
00395 
00396       const_iterator
00397       begin() const
00398       { return const_iterator(_Base::begin(), this); }
00399 
00400       iterator
00401       end()
00402       { return iterator(_Base::end(), this); }
00403 
00404       const_iterator
00405       end() const
00406       { return const_iterator(_Base::end(), this); }
00407 
00408       const_iterator
00409       cbegin() const
00410       { return const_iterator(_Base::begin(), this); }
00411 
00412       const_iterator
00413       cend() const
00414       { return const_iterator(_Base::end(), this); }
00415 
00416       // local versions
00417       using _Base::begin;
00418       using _Base::end;
00419       using _Base::cbegin;
00420       using _Base::cend;
00421 
00422       iterator
00423       insert(const value_type& __obj)
00424       { return iterator(_Base::insert(__obj), this); }
00425 
00426       iterator
00427       insert(const_iterator, const value_type& __obj)
00428       { return iterator(_Base::insert(__obj), this); }
00429 
00430       void
00431       insert(std::initializer_list<value_type> __l)
00432       { _Base::insert(__l); }
00433 
00434       template<typename _InputIterator>
00435         void
00436         insert(_InputIterator __first, _InputIterator __last)
00437         {
00438       __glibcxx_check_valid_range(__first, __last);
00439       _Base::insert(__first, __last);
00440     }
00441 
00442       iterator
00443       find(const key_type& __key)
00444       { return iterator(_Base::find(__key), this); }
00445 
00446       const_iterator
00447       find(const key_type& __key) const
00448       { return const_iterator(_Base::find(__key), this); }
00449 
00450       std::pair<iterator, iterator>
00451       equal_range(const key_type& __key)
00452       {
00453     typedef typename _Base::iterator _Base_iterator;
00454     typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
00455     __pair_type __res = _Base::equal_range(__key);
00456     return std::make_pair(iterator(__res.first, this),
00457                   iterator(__res.second, this));
00458       }
00459 
00460       std::pair<const_iterator, const_iterator>
00461       equal_range(const key_type& __key) const
00462       {
00463     typedef typename _Base::const_iterator _Base_iterator;
00464     typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
00465     __pair_type __res = _Base::equal_range(__key);
00466     return std::make_pair(const_iterator(__res.first, this),
00467                   const_iterator(__res.second, this));
00468       }
00469 
00470       size_type
00471       erase(const key_type& __key)
00472       {
00473     size_type __ret(0);
00474     iterator __victim(_Base::find(__key), this);
00475     if (__victim != end())
00476       {
00477         this->erase(__victim);
00478         __ret = 1;
00479       }
00480     return __ret;
00481       }
00482 
00483       iterator
00484       erase(const_iterator __it)
00485       {
00486     __glibcxx_check_erase(__it);
00487     __it._M_invalidate();
00488     return iterator(_Base::erase(__it.base()), this);
00489       }
00490 
00491       iterator
00492       erase(const_iterator __first, const_iterator __last)
00493       {
00494     __glibcxx_check_erase_range(__first, __last);
00495     for (const_iterator __tmp = __first; __tmp != __last;)
00496     {
00497       const_iterator __victim = __tmp++;
00498       __victim._M_invalidate();
00499     }
00500     return iterator(_Base::erase(__first.base(),
00501                      __last.base()), this);
00502       }
00503 
00504       _Base&
00505       _M_base() { return *this; }
00506 
00507       const _Base&
00508       _M_base() const { return *this; }
00509 
00510     private:
00511       void
00512       _M_invalidate_all()
00513       {
00514     typedef typename _Base::const_iterator _Base_const_iterator;
00515     typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
00516     this->_M_invalidate_if(_Not_equal(_M_base().end()));
00517       }
00518     };
00519 
00520   template<typename _Key, typename _Tp, typename _Hash,
00521        typename _Pred, typename _Alloc>
00522     inline void
00523     swap(unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
00524      unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
00525     { __x.swap(__y); }
00526 
00527 } // namespace __debug
00528 } // namespace std
00529 
00530 #endif

Generated on 12 Mar 2010 for libstdc++ by  doxygen 1.6.1