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 <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_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(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 _Value, typename _Hash, typename _Pred, typename _Alloc>
00284     inline void
00285     swap(unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
00286      unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
00287     { __x.swap(__y); }
00288 
00289 
00290   /// Class std::unordered_multiset with safety/checking/debug instrumentation.
00291   template<typename _Value,
00292        typename _Hash = std::hash<_Value>,
00293        typename _Pred = std::equal_to<_Value>,
00294        typename _Alloc = std::allocator<_Value> >
00295     class unordered_multiset
00296     : public _GLIBCXX_STD_D::unordered_multiset<_Value, _Hash, _Pred, _Alloc>,
00297       public __gnu_debug::_Safe_sequence<unordered_multiset<_Value, _Hash,
00298                                 _Pred, _Alloc> >
00299     {
00300       typedef _GLIBCXX_STD_D::unordered_multiset<_Value, _Hash,
00301                          _Pred, _Alloc> _Base;
00302       typedef __gnu_debug::_Safe_sequence<unordered_multiset> _Safe_base;
00303 
00304     public:
00305       typedef typename _Base::size_type       size_type;
00306       typedef typename _Base::hasher          hasher;
00307       typedef typename _Base::key_equal       key_equal;
00308       typedef typename _Base::allocator_type  allocator_type;
00309 
00310       typedef typename _Base::key_type        key_type;
00311       typedef typename _Base::value_type      value_type;
00312 
00313       typedef __gnu_debug::_Safe_iterator<typename _Base::iterator,
00314                       unordered_multiset> iterator;
00315       typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
00316                       unordered_multiset> const_iterator;
00317 
00318       explicit
00319       unordered_multiset(size_type __n = 10,
00320              const hasher& __hf = hasher(),
00321              const key_equal& __eql = key_equal(),
00322              const allocator_type& __a = allocator_type())
00323       : _Base(__n, __hf, __eql, __a) { }
00324 
00325       template<typename _InputIterator>
00326         unordered_multiset(_InputIterator __f, _InputIterator __l, 
00327                size_type __n = 10,
00328                const hasher& __hf = hasher(), 
00329                const key_equal& __eql = key_equal(), 
00330                const allocator_type& __a = allocator_type())
00331     : _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n,
00332         __hf, __eql, __a), _Safe_base() { }
00333 
00334       unordered_multiset(const unordered_multiset& __x) 
00335       : _Base(__x), _Safe_base() { }
00336 
00337       unordered_multiset(const _Base& __x) 
00338       : _Base(__x), _Safe_base() { }
00339 
00340       unordered_multiset(unordered_multiset&& __x) 
00341       : _Base(std::forward<unordered_multiset>(__x)), _Safe_base() { }
00342 
00343       unordered_multiset(initializer_list<value_type> __l,
00344              size_type __n = 10,
00345              const hasher& __hf = hasher(),
00346              const key_equal& __eql = key_equal(),
00347              const allocator_type& __a = allocator_type())
00348       : _Base(__l, __n, __hf, __eql, __a), _Safe_base() { }
00349 
00350       unordered_multiset&
00351       operator=(const unordered_multiset& __x)
00352       {
00353     *static_cast<_Base*>(this) = __x;
00354     this->_M_invalidate_all();
00355     return *this;
00356       }
00357 
00358       unordered_multiset&
00359       operator=(unordered_multiset&& __x)
00360       {
00361     // NB: DR 1204.
00362         // NB: DR 675.
00363     clear();
00364     swap(__x);
00365     return *this;
00366       }
00367 
00368       unordered_multiset&
00369       operator=(initializer_list<value_type> __l)
00370       {
00371     this->clear();
00372     this->insert(__l);
00373     return *this;
00374       }
00375 
00376       void
00377       swap(unordered_multiset& __x)
00378       {
00379     _Base::swap(__x);
00380     _Safe_base::_M_swap(__x);
00381       }
00382 
00383       void
00384       clear()
00385       {
00386     _Base::clear();
00387     this->_M_invalidate_all();
00388       }
00389 
00390       iterator
00391       begin()
00392       { return iterator(_Base::begin(), this); }
00393 
00394       const_iterator
00395       begin() const
00396       { return const_iterator(_Base::begin(), this); }
00397 
00398       iterator
00399       end()
00400       { return iterator(_Base::end(), this); }
00401 
00402       const_iterator
00403       end() const
00404       { return const_iterator(_Base::end(), this); }
00405 
00406       const_iterator
00407       cbegin() const
00408       { return const_iterator(_Base::begin(), this); }
00409 
00410       const_iterator
00411       cend() const
00412       { return const_iterator(_Base::end(), this); }
00413 
00414       // local versions
00415       using _Base::begin;
00416       using _Base::end;
00417       using _Base::cbegin;
00418       using _Base::cend;
00419 
00420       iterator
00421       insert(const value_type& __obj)
00422       { return iterator(_Base::insert(__obj), this); }
00423 
00424       iterator
00425       insert(const_iterator, const value_type& __obj)
00426       { return iterator(_Base::insert(__obj), this); }
00427 
00428       void
00429       insert(std::initializer_list<value_type> __l)
00430       { _Base::insert(__l); }
00431 
00432       template<typename _InputIterator>
00433         void
00434         insert(_InputIterator __first, _InputIterator __last)
00435         {
00436       __glibcxx_check_valid_range(__first, __last);
00437       _Base::insert(__first, __last);
00438     }
00439 
00440       iterator
00441       find(const key_type& __key)
00442       { return iterator(_Base::find(__key), this); }
00443 
00444       const_iterator
00445       find(const key_type& __key) const
00446       { return const_iterator(_Base::find(__key), this); }
00447 
00448       std::pair<iterator, iterator>
00449       equal_range(const key_type& __key)
00450       {
00451     typedef typename _Base::iterator _Base_iterator;
00452     typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
00453     __pair_type __res = _Base::equal_range(__key);
00454     return std::make_pair(iterator(__res.first, this),
00455                   iterator(__res.second, this));
00456       }
00457 
00458       std::pair<const_iterator, const_iterator>
00459       equal_range(const key_type& __key) const
00460       {
00461     typedef typename _Base::const_iterator _Base_iterator;
00462     typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
00463     __pair_type __res = _Base::equal_range(__key);
00464     return std::make_pair(const_iterator(__res.first, this),
00465                   const_iterator(__res.second, this));
00466       }
00467 
00468       size_type
00469       erase(const key_type& __key)
00470       {
00471     size_type __ret(0);
00472     iterator __victim(_Base::find(__key), this);
00473     if (__victim != end())
00474       {
00475         this->erase(__victim);
00476         __ret = 1;
00477       }
00478     return __ret;
00479       }
00480 
00481       iterator
00482       erase(const_iterator __it)
00483       {
00484     __glibcxx_check_erase(__it);
00485     __it._M_invalidate();
00486     return iterator(_Base::erase(__it.base()), this);
00487       }
00488 
00489       iterator
00490       erase(const_iterator __first, const_iterator __last)
00491       {
00492     __glibcxx_check_erase_range(__first, __last);
00493     for (const_iterator __tmp = __first; __tmp != __last;)
00494     {
00495       const_iterator __victim = __tmp++;
00496       __victim._M_invalidate();
00497     }
00498     return iterator(_Base::erase(__first.base(),
00499                      __last.base()), this);
00500       }
00501 
00502       _Base&
00503       _M_base() { return *this; }
00504 
00505       const _Base&
00506       _M_base() const { return *this; }
00507 
00508     private:
00509       void
00510       _M_invalidate_all()
00511       {
00512     typedef typename _Base::const_iterator _Base_const_iterator;
00513     typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
00514     this->_M_invalidate_if(_Not_equal(_M_base().end()));
00515       }
00516     };
00517 
00518   template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
00519     inline void
00520     swap(unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
00521      unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
00522     { __x.swap(__y); }
00523 
00524 } // namespace __debug
00525 } // namespace std
00526 
00527 #endif

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