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 #ifndef __GXX_EXPERIMENTAL_CXX0X__
00034 # include <bits/c++0x_warning.h>
00035 #else
00036 # include <unordered_set>
00037 
00038 #include <debug/safe_sequence.h>
00039 #include <debug/safe_iterator.h>
00040 
00041 namespace std
00042 {
00043 namespace __debug
00044 {
00045   /// Class std::unordered_set with safety/checking/debug instrumentation.
00046   template<typename _Value,
00047        typename _Hash = std::hash<_Value>,
00048        typename _Pred = std::equal_to<_Value>,
00049        typename _Alloc = std::allocator<_Value> >
00050     class unordered_set
00051     : public _GLIBCXX_STD_D::unordered_set<_Value, _Hash, _Pred, _Alloc>,
00052       public __gnu_debug::_Safe_sequence<unordered_set<_Value, _Hash,
00053                                _Pred, _Alloc> >
00054     {
00055       typedef _GLIBCXX_STD_D::unordered_set<_Value, _Hash,
00056                         _Pred, _Alloc> _Base;
00057       typedef __gnu_debug::_Safe_sequence<unordered_set> _Safe_base;
00058 
00059     public:
00060       typedef typename _Base::size_type       size_type;
00061       typedef typename _Base::hasher          hasher;
00062       typedef typename _Base::key_equal       key_equal;
00063       typedef typename _Base::allocator_type  allocator_type;
00064 
00065       typedef typename _Base::key_type        key_type;
00066       typedef typename _Base::value_type      value_type;
00067 
00068       typedef __gnu_debug::_Safe_iterator<typename _Base::iterator,
00069                       unordered_set> iterator;
00070       typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
00071                       unordered_set> const_iterator;
00072 
00073       explicit
00074       unordered_set(size_type __n = 10,
00075             const hasher& __hf = hasher(),
00076             const key_equal& __eql = key_equal(),
00077             const allocator_type& __a = allocator_type())
00078       : _Base(__n, __hf, __eql, __a) { }
00079 
00080       template<typename _InputIterator>
00081         unordered_set(_InputIterator __first, _InputIterator __last, 
00082               size_type __n = 0,
00083               const hasher& __hf = hasher(), 
00084               const key_equal& __eql = key_equal(), 
00085               const allocator_type& __a = allocator_type())
00086     : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
00087                                      __last)),
00088         __gnu_debug::__base(__last), __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::move(__x)), _Safe_base() { }
00099 
00100       unordered_set(initializer_list<value_type> __l,
00101             size_type __n = 0,
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(__gnu_debug::__base(__first),
00203             __gnu_debug::__base(__last));
00204     }
00205 
00206       iterator
00207       find(const key_type& __key)
00208       { return iterator(_Base::find(__key), this); }
00209 
00210       const_iterator
00211       find(const key_type& __key) const
00212       { return const_iterator(_Base::find(__key), this); }
00213 
00214       std::pair<iterator, iterator>
00215       equal_range(const key_type& __key)
00216       {
00217     typedef typename _Base::iterator _Base_iterator;
00218     typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
00219     __pair_type __res = _Base::equal_range(__key);
00220     return std::make_pair(iterator(__res.first, this),
00221                   iterator(__res.second, this));
00222       }
00223 
00224       std::pair<const_iterator, const_iterator>
00225       equal_range(const key_type& __key) const
00226       {
00227     typedef typename _Base::const_iterator _Base_iterator;
00228     typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
00229     __pair_type __res = _Base::equal_range(__key);
00230     return std::make_pair(const_iterator(__res.first, this),
00231                   const_iterator(__res.second, this));
00232       }
00233 
00234       size_type
00235       erase(const key_type& __key)
00236       {
00237     size_type __ret(0);
00238     iterator __victim(_Base::find(__key), this);
00239     if (__victim != end())
00240       {
00241         this->erase(__victim);
00242         __ret = 1;
00243       }
00244     return __ret;
00245       }
00246 
00247       iterator
00248       erase(const_iterator __it)
00249       {
00250     __glibcxx_check_erase(__it);
00251     __it._M_invalidate();
00252     return iterator(_Base::erase(__it.base()), this);
00253       }
00254 
00255       iterator
00256       erase(const_iterator __first, const_iterator __last)
00257       {
00258     __glibcxx_check_erase_range(__first, __last);
00259     for (const_iterator __tmp = __first; __tmp != __last;)
00260     {
00261       const_iterator __victim = __tmp++;
00262       __victim._M_invalidate();
00263     }
00264     return iterator(_Base::erase(__first.base(),
00265                      __last.base()), 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 _Value, typename _Hash, typename _Pred, typename _Alloc>
00285     inline void
00286     swap(unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
00287      unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
00288     { __x.swap(__y); }
00289 
00290   template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
00291     inline bool
00292     operator==(const unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
00293            const unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
00294     { return __x._M_equal(__y); }
00295 
00296   template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
00297     inline bool
00298     operator!=(const unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
00299            const unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
00300     { return !(__x == __y); }
00301 
00302 
00303   /// Class std::unordered_multiset with safety/checking/debug instrumentation.
00304   template<typename _Value,
00305        typename _Hash = std::hash<_Value>,
00306        typename _Pred = std::equal_to<_Value>,
00307        typename _Alloc = std::allocator<_Value> >
00308     class unordered_multiset
00309     : public _GLIBCXX_STD_D::unordered_multiset<_Value, _Hash, _Pred, _Alloc>,
00310       public __gnu_debug::_Safe_sequence<unordered_multiset<_Value, _Hash,
00311                                 _Pred, _Alloc> >
00312     {
00313       typedef _GLIBCXX_STD_D::unordered_multiset<_Value, _Hash,
00314                          _Pred, _Alloc> _Base;
00315       typedef __gnu_debug::_Safe_sequence<unordered_multiset> _Safe_base;
00316 
00317     public:
00318       typedef typename _Base::size_type       size_type;
00319       typedef typename _Base::hasher          hasher;
00320       typedef typename _Base::key_equal       key_equal;
00321       typedef typename _Base::allocator_type  allocator_type;
00322 
00323       typedef typename _Base::key_type        key_type;
00324       typedef typename _Base::value_type      value_type;
00325 
00326       typedef __gnu_debug::_Safe_iterator<typename _Base::iterator,
00327                       unordered_multiset> iterator;
00328       typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
00329                       unordered_multiset> const_iterator;
00330 
00331       explicit
00332       unordered_multiset(size_type __n = 10,
00333              const hasher& __hf = hasher(),
00334              const key_equal& __eql = key_equal(),
00335              const allocator_type& __a = allocator_type())
00336       : _Base(__n, __hf, __eql, __a) { }
00337 
00338       template<typename _InputIterator>
00339         unordered_multiset(_InputIterator __first, _InputIterator __last, 
00340                size_type __n = 0,
00341                const hasher& __hf = hasher(), 
00342                const key_equal& __eql = key_equal(), 
00343                const allocator_type& __a = allocator_type())
00344     : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
00345                                      __last)),
00346         __gnu_debug::__base(__last), __n,
00347         __hf, __eql, __a), _Safe_base() { }
00348 
00349       unordered_multiset(const unordered_multiset& __x) 
00350       : _Base(__x), _Safe_base() { }
00351 
00352       unordered_multiset(const _Base& __x) 
00353       : _Base(__x), _Safe_base() { }
00354 
00355       unordered_multiset(unordered_multiset&& __x) 
00356       : _Base(std::move(__x)), _Safe_base() { }
00357 
00358       unordered_multiset(initializer_list<value_type> __l,
00359              size_type __n = 0,
00360              const hasher& __hf = hasher(),
00361              const key_equal& __eql = key_equal(),
00362              const allocator_type& __a = allocator_type())
00363       : _Base(__l, __n, __hf, __eql, __a), _Safe_base() { }
00364 
00365       unordered_multiset&
00366       operator=(const unordered_multiset& __x)
00367       {
00368     *static_cast<_Base*>(this) = __x;
00369     this->_M_invalidate_all();
00370     return *this;
00371       }
00372 
00373       unordered_multiset&
00374       operator=(unordered_multiset&& __x)
00375       {
00376     // NB: DR 1204.
00377         // NB: DR 675.
00378     clear();
00379     swap(__x);
00380     return *this;
00381       }
00382 
00383       unordered_multiset&
00384       operator=(initializer_list<value_type> __l)
00385       {
00386     this->clear();
00387     this->insert(__l);
00388     return *this;
00389       }
00390 
00391       void
00392       swap(unordered_multiset& __x)
00393       {
00394     _Base::swap(__x);
00395     _Safe_base::_M_swap(__x);
00396       }
00397 
00398       void
00399       clear()
00400       {
00401     _Base::clear();
00402     this->_M_invalidate_all();
00403       }
00404 
00405       iterator
00406       begin()
00407       { return iterator(_Base::begin(), this); }
00408 
00409       const_iterator
00410       begin() const
00411       { return const_iterator(_Base::begin(), this); }
00412 
00413       iterator
00414       end()
00415       { return iterator(_Base::end(), this); }
00416 
00417       const_iterator
00418       end() const
00419       { return const_iterator(_Base::end(), this); }
00420 
00421       const_iterator
00422       cbegin() const
00423       { return const_iterator(_Base::begin(), this); }
00424 
00425       const_iterator
00426       cend() const
00427       { return const_iterator(_Base::end(), this); }
00428 
00429       // local versions
00430       using _Base::begin;
00431       using _Base::end;
00432       using _Base::cbegin;
00433       using _Base::cend;
00434 
00435       iterator
00436       insert(const value_type& __obj)
00437       { return iterator(_Base::insert(__obj), this); }
00438 
00439       iterator
00440       insert(const_iterator, const value_type& __obj)
00441       { return iterator(_Base::insert(__obj), this); }
00442 
00443       void
00444       insert(std::initializer_list<value_type> __l)
00445       { _Base::insert(__l); }
00446 
00447       template<typename _InputIterator>
00448         void
00449         insert(_InputIterator __first, _InputIterator __last)
00450         {
00451       __glibcxx_check_valid_range(__first, __last);
00452       _Base::insert(__gnu_debug::__base(__first),
00453             __gnu_debug::__base(__last));
00454     }
00455 
00456       iterator
00457       find(const key_type& __key)
00458       { return iterator(_Base::find(__key), this); }
00459 
00460       const_iterator
00461       find(const key_type& __key) const
00462       { return const_iterator(_Base::find(__key), this); }
00463 
00464       std::pair<iterator, iterator>
00465       equal_range(const key_type& __key)
00466       {
00467     typedef typename _Base::iterator _Base_iterator;
00468     typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
00469     __pair_type __res = _Base::equal_range(__key);
00470     return std::make_pair(iterator(__res.first, this),
00471                   iterator(__res.second, this));
00472       }
00473 
00474       std::pair<const_iterator, const_iterator>
00475       equal_range(const key_type& __key) const
00476       {
00477     typedef typename _Base::const_iterator _Base_iterator;
00478     typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
00479     __pair_type __res = _Base::equal_range(__key);
00480     return std::make_pair(const_iterator(__res.first, this),
00481                   const_iterator(__res.second, this));
00482       }
00483 
00484       size_type
00485       erase(const key_type& __key)
00486       {
00487     size_type __ret(0);
00488     iterator __victim(_Base::find(__key), this);
00489     if (__victim != end())
00490       {
00491         this->erase(__victim);
00492         __ret = 1;
00493       }
00494     return __ret;
00495       }
00496 
00497       iterator
00498       erase(const_iterator __it)
00499       {
00500     __glibcxx_check_erase(__it);
00501     __it._M_invalidate();
00502     return iterator(_Base::erase(__it.base()), this);
00503       }
00504 
00505       iterator
00506       erase(const_iterator __first, const_iterator __last)
00507       {
00508     __glibcxx_check_erase_range(__first, __last);
00509     for (const_iterator __tmp = __first; __tmp != __last;)
00510     {
00511       const_iterator __victim = __tmp++;
00512       __victim._M_invalidate();
00513     }
00514     return iterator(_Base::erase(__first.base(),
00515                      __last.base()), this);
00516       }
00517 
00518       _Base&
00519       _M_base() { return *this; }
00520 
00521       const _Base&
00522       _M_base() const { return *this; }
00523 
00524     private:
00525       void
00526       _M_invalidate_all()
00527       {
00528     typedef typename _Base::const_iterator _Base_const_iterator;
00529     typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
00530     this->_M_invalidate_if(_Not_equal(_M_base().end()));
00531       }
00532     };
00533 
00534   template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
00535     inline void
00536     swap(unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
00537      unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
00538     { __x.swap(__y); }
00539 
00540   template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
00541     inline bool
00542     operator==(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
00543            const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
00544     { return __x._M_equal(__y); }
00545 
00546   template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
00547     inline bool
00548     operator!=(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
00549            const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
00550     { return !(__x == __y); }
00551 
00552 } // namespace __debug
00553 } // namespace std
00554 
00555 #endif // __GXX_EXPERIMENTAL_CXX0X__
00556 
00557 #endif