debug/unordered_set

Go to the documentation of this file.
00001 // Debugging unordered_set/unordered_multiset 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_set
00027  *  This file is a GNU debug extension to the Standard C++ Library.
00028  */
00029 
00030 #ifndef _GLIBCXX_DEBUG_UNORDERED_SET
00031 #define _GLIBCXX_DEBUG_UNORDERED_SET 1
00032 
00033 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00034 # include <unordered_set>
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_set with safety/checking/debug instrumentation.
00048   template<typename _Value,
00049        typename _Hash = std::hash<_Value>,
00050        typename _Pred = std::equal_to<_Value>,
00051        typename _Alloc = std::allocator<_Value> >
00052     class unordered_set
00053     : public _GLIBCXX_STD_D::unordered_set<_Value, _Hash, _Pred, _Alloc>,
00054       public __gnu_debug::_Safe_sequence<unordered_set<_Value, _Hash,
00055                                _Pred, _Alloc> >
00056     {
00057       typedef _GLIBCXX_STD_D::unordered_set<_Value, _Hash,
00058                         _Pred, _Alloc> _Base;
00059       typedef __gnu_debug::_Safe_sequence<unordered_set> _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_set> iterator;
00072       typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
00073                       unordered_set> const_iterator;
00074 
00075       explicit
00076       unordered_set(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_set(_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_set(const unordered_set& __x) 
00092       : _Base(__x), _Safe_base() { }
00093 
00094       unordered_set(const _Base& __x) 
00095       : _Base(__x), _Safe_base() { }
00096 
00097       unordered_set(unordered_set&& __x) 
00098       : _Base(std::forward<unordered_set>(__x)), _Safe_base() { }
00099 
00100       unordered_set(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_set&
00108       operator=(const unordered_set& __x)
00109       {
00110     *static_cast<_Base*>(this) = __x;
00111     this->_M_invalidate_all();
00112     return *this;
00113       }
00114 
00115       unordered_set&
00116       operator=(unordered_set&& __x)
00117       {
00118     // NB: DR 1204.
00119     // NB: DR 675.
00120     clear();
00121     swap(__x);
00122     return *this;
00123       }
00124 
00125       unordered_set&
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_set& __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 _Value, typename _Hash, typename _Pred, typename _Alloc>
00313     inline void
00314     swap(unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
00315      unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
00316     { __x.swap(__y); }
00317 
00318 
00319   /// Class std::unordered_multiset with safety/checking/debug instrumentation.
00320   template<typename _Value,
00321        typename _Hash = std::hash<_Value>,
00322        typename _Pred = std::equal_to<_Value>,
00323        typename _Alloc = std::allocator<_Value> >
00324     class unordered_multiset
00325     : public _GLIBCXX_STD_D::unordered_multiset<_Value, _Hash, _Pred, _Alloc>,
00326       public __gnu_debug::_Safe_sequence<unordered_multiset<_Value, _Hash,
00327                                 _Pred, _Alloc> >
00328     {
00329       typedef _GLIBCXX_STD_D::unordered_multiset<_Value, _Hash,
00330                          _Pred, _Alloc> _Base;
00331       typedef __gnu_debug::_Safe_sequence<unordered_multiset> _Safe_base;
00332 
00333     public:
00334       typedef typename _Base::size_type       size_type;
00335       typedef typename _Base::hasher          hasher;
00336       typedef typename _Base::key_equal       key_equal;
00337       typedef typename _Base::allocator_type  allocator_type;
00338 
00339       typedef typename _Base::key_type        key_type;
00340       typedef typename _Base::value_type      value_type;
00341 
00342       typedef __gnu_debug::_Safe_iterator<typename _Base::iterator,
00343                       unordered_multiset> iterator;
00344       typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
00345                       unordered_multiset> const_iterator;
00346 
00347       explicit
00348       unordered_multiset(size_type __n = 10,
00349              const hasher& __hf = hasher(),
00350              const key_equal& __eql = key_equal(),
00351              const allocator_type& __a = allocator_type())
00352       : _Base(__n, __hf, __eql, __a) { }
00353 
00354       template<typename _InputIterator>
00355         unordered_multiset(_InputIterator __f, _InputIterator __l, 
00356                size_type __n = 10,
00357                const hasher& __hf = hasher(), 
00358                const key_equal& __eql = key_equal(), 
00359                const allocator_type& __a = allocator_type())
00360     : _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n,
00361         __hf, __eql, __a), _Safe_base() { }
00362 
00363       unordered_multiset(const unordered_multiset& __x) 
00364       : _Base(__x), _Safe_base() { }
00365 
00366       unordered_multiset(const _Base& __x) 
00367       : _Base(__x), _Safe_base() { }
00368 
00369       unordered_multiset(unordered_multiset&& __x) 
00370       : _Base(std::forward<unordered_multiset>(__x)), _Safe_base() { }
00371 
00372       unordered_multiset(initializer_list<value_type> __l,
00373              size_type __n = 10,
00374              const hasher& __hf = hasher(),
00375              const key_equal& __eql = key_equal(),
00376              const allocator_type& __a = allocator_type())
00377       : _Base(__l, __n, __hf, __eql, __a), _Safe_base() { }
00378 
00379       unordered_multiset&
00380       operator=(const unordered_multiset& __x)
00381       {
00382     *static_cast<_Base*>(this) = __x;
00383     this->_M_invalidate_all();
00384     return *this;
00385       }
00386 
00387       unordered_multiset&
00388       operator=(unordered_multiset&& __x)
00389       {
00390     // NB: DR 1204.
00391         // NB: DR 675.
00392     clear();
00393     swap(__x);
00394     return *this;
00395       }
00396 
00397       unordered_multiset&
00398       operator=(initializer_list<value_type> __l)
00399       {
00400     this->clear();
00401     this->insert(__l);
00402     return *this;
00403       }
00404 
00405       void
00406       swap(unordered_multiset& __x)
00407       {
00408     _Base::swap(__x);
00409     _Safe_base::_M_swap(__x);
00410       }
00411 
00412       void
00413       clear()
00414       {
00415     _Base::clear();
00416     this->_M_invalidate_all();
00417       }
00418 
00419       iterator
00420       begin()
00421       { return iterator(_Base::begin(), this); }
00422 
00423       const_iterator
00424       begin() const
00425       { return const_iterator(_Base::begin(), this); }
00426 
00427       iterator
00428       end()
00429       { return iterator(_Base::end(), this); }
00430 
00431       const_iterator
00432       end() const
00433       { return const_iterator(_Base::end(), this); }
00434 
00435       const_iterator
00436       cbegin() const
00437       { return const_iterator(_Base::begin(), this); }
00438 
00439       const_iterator
00440       cend() const
00441       { return const_iterator(_Base::end(), this); }
00442 
00443       // local versions
00444       using _Base::begin;
00445       using _Base::end;
00446       using _Base::cbegin;
00447       using _Base::cend;
00448 
00449       iterator
00450       insert(const value_type& __obj)
00451       { return iterator(_Base::insert(__obj), this); }
00452 
00453       iterator
00454       insert(iterator, const value_type& __obj)
00455       { return iterator(_Base::insert(__obj), this); }
00456 
00457       const_iterator
00458       insert(const_iterator, const value_type& __obj)
00459       { return const_iterator(_Base::insert(__obj), this); }
00460 
00461       void
00462       insert(std::initializer_list<value_type> __l)
00463       { _Base::insert(__l); }
00464 
00465       template<typename _InputIterator>
00466         void
00467         insert(_InputIterator __first, _InputIterator __last)
00468         {
00469       __glibcxx_check_valid_range(__first, __last);
00470       _Base::insert(__first, __last);
00471     }
00472 
00473       iterator
00474       find(const key_type& __key)
00475       { return iterator(_Base::find(__key), this); }
00476 
00477       const_iterator
00478       find(const key_type& __key) const
00479       { return const_iterator(_Base::find(__key), this); }
00480 
00481       std::pair<iterator, iterator>
00482       equal_range(const key_type& __key)
00483       {
00484     typedef typename _Base::iterator _Base_iterator;
00485     typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
00486     __pair_type __res = _Base::equal_range(__key);
00487     return std::make_pair(iterator(__res.first, this),
00488                   iterator(__res.second, this));
00489       }
00490 
00491       std::pair<const_iterator, const_iterator>
00492       equal_range(const key_type& __key) const
00493       {
00494     typedef typename _Base::const_iterator _Base_iterator;
00495     typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
00496     __pair_type __res = _Base::equal_range(__key);
00497     return std::make_pair(const_iterator(__res.first, this),
00498                   const_iterator(__res.second, this));
00499       }
00500 
00501       size_type
00502       erase(const key_type& __key)
00503       {
00504     size_type __ret(0);
00505     iterator __victim(_Base::find(__key), this);
00506     if (__victim != end())
00507       {
00508         this->erase(__victim);
00509         __ret = 1;
00510       }
00511     return __ret;
00512       }
00513 
00514       iterator
00515       erase(iterator __it)
00516       {
00517     __glibcxx_check_erase(__it);
00518     __it._M_invalidate();
00519     return iterator(_Base::erase(__it.base()), this);
00520       }
00521 
00522       const_iterator
00523       erase(const_iterator __it)
00524       {
00525     __glibcxx_check_erase(__it);
00526     __it._M_invalidate();
00527     return const_iterator(_Base::erase(__it.base()), this);
00528       }
00529 
00530       iterator
00531       erase(iterator __first, iterator __last)
00532       {
00533     __glibcxx_check_erase_range(__first, __last);
00534     for (iterator __tmp = __first; __tmp != __last;)
00535     {
00536       iterator __victim = __tmp++;
00537       __victim._M_invalidate();
00538     }
00539     return iterator(_Base::erase(__first.base(),
00540                      __last.base()), this);
00541       }
00542 
00543       const_iterator
00544       erase(const_iterator __first, const_iterator __last)
00545       {
00546     __glibcxx_check_erase_range(__first, __last);
00547     for (const_iterator __tmp = __first; __tmp != __last;)
00548     {
00549       const_iterator __victim = __tmp++;
00550       __victim._M_invalidate();
00551     }
00552     return const_iterator(_Base::erase(__first.base(),
00553                        __last.base()), this);
00554       }
00555 
00556       _Base&
00557       _M_base() { return *this; }
00558 
00559       const _Base&
00560       _M_base() const { return *this; }
00561 
00562     private:
00563       void
00564       _M_invalidate_all()
00565       {
00566     typedef typename _Base::const_iterator _Base_const_iterator;
00567     typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
00568     this->_M_invalidate_if(_Not_equal(_M_base().end()));
00569       }
00570     };
00571 
00572   template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
00573     inline void
00574     swap(unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
00575      unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
00576     { __x.swap(__y); }
00577 
00578 } // namespace __debug
00579 } // namespace std
00580 
00581 #endif

Generated on 9 Feb 2010 for libstdc++ by  doxygen 1.6.1