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 <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(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       const_iterator
00194       insert(const_iterator, const value_type& __obj)
00195       {
00196     typedef std::pair<typename _Base::iterator, bool> __pair_type;
00197     __pair_type __res = _Base::insert(__obj);
00198     return const_iterator(__res.first, this);
00199       }
00200 
00201       void
00202       insert(std::initializer_list<value_type> __l)
00203       { _Base::insert(__l); }
00204 
00205       template<typename _InputIterator>
00206         void
00207         insert(_InputIterator __first, _InputIterator __last)
00208         {
00209       __glibcxx_check_valid_range(__first, __last);
00210       _Base::insert(__first, __last);
00211     }
00212 
00213       iterator
00214       find(const key_type& __key)
00215       { return iterator(_Base::find(__key), this); }
00216 
00217       const_iterator
00218       find(const key_type& __key) const
00219       { return const_iterator(_Base::find(__key), this); }
00220 
00221       std::pair<iterator, iterator>
00222       equal_range(const key_type& __key)
00223       {
00224     typedef typename _Base::iterator _Base_iterator;
00225     typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
00226     __pair_type __res = _Base::equal_range(__key);
00227     return std::make_pair(iterator(__res.first, this),
00228                   iterator(__res.second, this));
00229       }
00230 
00231       std::pair<const_iterator, const_iterator>
00232       equal_range(const key_type& __key) const
00233       {
00234     typedef typename _Base::const_iterator _Base_iterator;
00235     typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
00236     __pair_type __res = _Base::equal_range(__key);
00237     return std::make_pair(const_iterator(__res.first, this),
00238                   const_iterator(__res.second, this));
00239       }
00240 
00241       size_type
00242       erase(const key_type& __key)
00243       {
00244     size_type __ret(0);
00245     iterator __victim(_Base::find(__key), this);
00246     if (__victim != end())
00247       {
00248         this->erase(__victim);
00249         __ret = 1;
00250       }
00251     return __ret;
00252       }
00253 
00254       iterator
00255       erase(iterator __it)
00256       {
00257     __glibcxx_check_erase(__it);
00258     __it._M_invalidate();
00259     return iterator(_Base::erase(__it.base()), this);
00260       }
00261 
00262       const_iterator
00263       erase(const_iterator __it)
00264       {
00265     __glibcxx_check_erase(__it);
00266     __it._M_invalidate();
00267     return const_iterator(_Base::erase(__it.base()), this);
00268       }
00269 
00270       iterator
00271       erase(iterator __first, iterator __last)
00272       {
00273     __glibcxx_check_erase_range(__first, __last);
00274     for (iterator __tmp = __first; __tmp != __last;)
00275     {
00276       iterator __victim = __tmp++;
00277       __victim._M_invalidate();
00278     }
00279     return iterator(_Base::erase(__first.base(),
00280                      __last.base()), this);
00281       }
00282 
00283       const_iterator
00284       erase(const_iterator __first, const_iterator __last)
00285       {
00286     __glibcxx_check_erase_range(__first, __last);
00287     for (const_iterator __tmp = __first; __tmp != __last;)
00288     {
00289       const_iterator __victim = __tmp++;
00290       __victim._M_invalidate();
00291     }
00292     return const_iterator(_Base::erase(__first.base(),
00293                        __last.base()), this);
00294       }
00295 
00296       _Base&
00297       _M_base() { return *this; }
00298 
00299       const _Base&
00300       _M_base() const { return *this; }
00301 
00302     private:
00303       void
00304       _M_invalidate_all()
00305       {
00306     typedef typename _Base::const_iterator _Base_const_iterator;
00307     typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
00308     this->_M_invalidate_if(_Not_equal(_M_base().end()));
00309       }
00310     };
00311 
00312   template<typename _Key, typename _Tp, typename _Hash,
00313        typename _Pred, typename _Alloc>
00314     inline void
00315     swap(unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
00316      unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
00317     { __x.swap(__y); }
00318 
00319 
00320   /// Class std::unordered_multimap with safety/checking/debug instrumentation.
00321   template<typename _Key, typename _Tp,
00322        typename _Hash = std::hash<_Key>,
00323        typename _Pred = std::equal_to<_Key>,
00324        typename _Alloc = std::allocator<_Key> >
00325     class unordered_multimap
00326     : public _GLIBCXX_STD_D::unordered_multimap<_Key, _Tp, _Hash,
00327                         _Pred, _Alloc>,
00328       public __gnu_debug::_Safe_sequence<unordered_multimap<_Key, _Tp, _Hash,
00329                                 _Pred, _Alloc> >
00330     {
00331       typedef _GLIBCXX_STD_D::unordered_multimap<_Key, _Tp, _Hash,
00332                          _Pred, _Alloc> _Base;
00333       typedef __gnu_debug::_Safe_sequence<unordered_multimap> _Safe_base;
00334 
00335     public:
00336       typedef typename _Base::size_type       size_type;
00337       typedef typename _Base::hasher          hasher;
00338       typedef typename _Base::key_equal       key_equal;
00339       typedef typename _Base::allocator_type  allocator_type;
00340 
00341       typedef typename _Base::key_type        key_type;
00342       typedef typename _Base::value_type      value_type;
00343 
00344       typedef __gnu_debug::_Safe_iterator<typename _Base::iterator,
00345                       unordered_multimap> iterator;
00346       typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
00347                       unordered_multimap> const_iterator;
00348 
00349       explicit
00350       unordered_multimap(size_type __n = 10,
00351              const hasher& __hf = hasher(),
00352              const key_equal& __eql = key_equal(),
00353              const allocator_type& __a = allocator_type())
00354       : _Base(__n, __hf, __eql, __a) { }
00355 
00356       template<typename _InputIterator>
00357         unordered_multimap(_InputIterator __f, _InputIterator __l, 
00358                size_type __n = 10,
00359                const hasher& __hf = hasher(), 
00360                const key_equal& __eql = key_equal(), 
00361                const allocator_type& __a = allocator_type())
00362     : _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n,
00363         __hf, __eql, __a), _Safe_base() { }
00364 
00365       unordered_multimap(const unordered_multimap& __x) 
00366       : _Base(__x), _Safe_base() { }
00367 
00368       unordered_multimap(const _Base& __x) 
00369       : _Base(__x), _Safe_base() { }
00370 
00371       unordered_multimap(unordered_multimap&& __x) 
00372       : _Base(std::forward<unordered_multimap>(__x)), _Safe_base() { }
00373 
00374       unordered_multimap(initializer_list<value_type> __l,
00375              size_type __n = 10,
00376              const hasher& __hf = hasher(),
00377              const key_equal& __eql = key_equal(),
00378              const allocator_type& __a = allocator_type())
00379       : _Base(__l, __n, __hf, __eql, __a), _Safe_base() { }
00380 
00381       unordered_multimap&
00382       operator=(const unordered_multimap& __x)
00383       {
00384     *static_cast<_Base*>(this) = __x;
00385     this->_M_invalidate_all();
00386     return *this;
00387       }
00388 
00389       unordered_multimap&
00390       operator=(unordered_multimap&& __x)
00391       {
00392     // NB: DR 1204.
00393     // NB: DR 675.
00394     clear();
00395     swap(__x);
00396     return *this;
00397       }
00398 
00399       unordered_multimap&
00400       operator=(initializer_list<value_type> __l)
00401       {
00402     this->clear();
00403     this->insert(__l);
00404     return *this;
00405       }
00406 
00407       void
00408       swap(unordered_multimap& __x)
00409       {
00410     _Base::swap(__x);
00411     _Safe_base::_M_swap(__x);
00412       }
00413 
00414       void
00415       clear()
00416       {
00417     _Base::clear();
00418     this->_M_invalidate_all();
00419       }
00420 
00421       iterator 
00422       begin()
00423       { return iterator(_Base::begin(), this); }
00424 
00425       const_iterator
00426       begin() const
00427       { return const_iterator(_Base::begin(), this); }
00428 
00429       iterator
00430       end()
00431       { return iterator(_Base::end(), this); }
00432 
00433       const_iterator
00434       end() const
00435       { return const_iterator(_Base::end(), this); }
00436 
00437       const_iterator
00438       cbegin() const
00439       { return const_iterator(_Base::begin(), this); }
00440 
00441       const_iterator
00442       cend() const
00443       { return const_iterator(_Base::end(), this); }
00444 
00445       // local versions
00446       using _Base::begin;
00447       using _Base::end;
00448       using _Base::cbegin;
00449       using _Base::cend;
00450 
00451       iterator
00452       insert(const value_type& __obj)
00453       { return iterator(_Base::insert(__obj), this); }
00454 
00455       iterator
00456       insert(iterator, const value_type& __obj)
00457       { return iterator(_Base::insert(__obj), this); }
00458 
00459       const_iterator
00460       insert(const_iterator, const value_type& __obj)
00461       { return const_iterator(_Base::insert(__obj), this); }
00462 
00463       void
00464       insert(std::initializer_list<value_type> __l)
00465       { _Base::insert(__l); }
00466 
00467       template<typename _InputIterator>
00468         void
00469         insert(_InputIterator __first, _InputIterator __last)
00470         {
00471       __glibcxx_check_valid_range(__first, __last);
00472       _Base::insert(__first, __last);
00473     }
00474 
00475       iterator
00476       find(const key_type& __key)
00477       { return iterator(_Base::find(__key), this); }
00478 
00479       const_iterator
00480       find(const key_type& __key) const
00481       { return const_iterator(_Base::find(__key), this); }
00482 
00483       std::pair<iterator, iterator>
00484       equal_range(const key_type& __key)
00485       {
00486     typedef typename _Base::iterator _Base_iterator;
00487     typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
00488     __pair_type __res = _Base::equal_range(__key);
00489     return std::make_pair(iterator(__res.first, this),
00490                   iterator(__res.second, this));
00491       }
00492 
00493       std::pair<const_iterator, const_iterator>
00494       equal_range(const key_type& __key) const
00495       {
00496     typedef typename _Base::const_iterator _Base_iterator;
00497     typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
00498     __pair_type __res = _Base::equal_range(__key);
00499     return std::make_pair(const_iterator(__res.first, this),
00500                   const_iterator(__res.second, this));
00501       }
00502 
00503       size_type
00504       erase(const key_type& __key)
00505       {
00506     size_type __ret(0);
00507     iterator __victim(_Base::find(__key), this);
00508     if (__victim != end())
00509       {
00510         this->erase(__victim);
00511         __ret = 1;
00512       }
00513     return __ret;
00514       }
00515 
00516       iterator
00517       erase(iterator __it)
00518       {
00519     __glibcxx_check_erase(__it);
00520     __it._M_invalidate();
00521     return iterator(_Base::erase(__it.base()), this);
00522       }
00523 
00524       const_iterator
00525       erase(const_iterator __it)
00526       {
00527     __glibcxx_check_erase(__it);
00528     __it._M_invalidate();
00529     return const_iterator(_Base::erase(__it.base()), this);
00530       }
00531 
00532       iterator
00533       erase(iterator __first, iterator __last)
00534       {
00535     __glibcxx_check_erase_range(__first, __last);
00536     for (iterator __tmp = __first; __tmp != __last;)
00537     {
00538       iterator __victim = __tmp++;
00539       __victim._M_invalidate();
00540     }
00541     return iterator(_Base::erase(__first.base(),
00542                      __last.base()), this);
00543       }
00544 
00545       const_iterator
00546       erase(const_iterator __first, const_iterator __last)
00547       {
00548     __glibcxx_check_erase_range(__first, __last);
00549     for (const_iterator __tmp = __first; __tmp != __last;)
00550     {
00551       const_iterator __victim = __tmp++;
00552       __victim._M_invalidate();
00553     }
00554     return const_iterator(_Base::erase(__first.base(),
00555                        __last.base()), this);
00556       }
00557 
00558       _Base&
00559       _M_base() { return *this; }
00560 
00561       const _Base&
00562       _M_base() const { return *this; }
00563 
00564     private:
00565       void
00566       _M_invalidate_all()
00567       {
00568     typedef typename _Base::const_iterator _Base_const_iterator;
00569     typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
00570     this->_M_invalidate_if(_Not_equal(_M_base().end()));
00571       }
00572     };
00573 
00574   template<typename _Key, typename _Tp, typename _Hash,
00575        typename _Pred, typename _Alloc>
00576     inline void
00577     swap(unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
00578      unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
00579     { __x.swap(__y); }
00580 
00581 } // namespace __debug
00582 } // namespace std
00583 
00584 #endif

Generated on 11 Jan 2010 for libstdc++ by  doxygen 1.6.1