debug/set.h

Go to the documentation of this file.
00001 // Debugging set 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/set.h
00027  *  This file is a GNU debug extension to the Standard C++ Library.
00028  */
00029 
00030 #ifndef _GLIBCXX_DEBUG_SET_H
00031 #define _GLIBCXX_DEBUG_SET_H 1
00032 
00033 #include <debug/safe_sequence.h>
00034 #include <debug/safe_iterator.h>
00035 #include <utility>
00036 
00037 namespace std 
00038 {
00039 namespace __debug
00040 {
00041   /// Class std::set with safety/checking/debug instrumentation.
00042   template<typename _Key, typename _Compare = std::less<_Key>,
00043        typename _Allocator = std::allocator<_Key> >
00044     class set
00045     : public _GLIBCXX_STD_D::set<_Key,_Compare,_Allocator>,
00046       public __gnu_debug::_Safe_sequence<set<_Key, _Compare, _Allocator> >
00047     {
00048       typedef _GLIBCXX_STD_D::set<_Key, _Compare, _Allocator> _Base;
00049       typedef __gnu_debug::_Safe_sequence<set> _Safe_base;
00050 
00051     public:
00052       // types:
00053       typedef _Key                  key_type;
00054       typedef _Key                  value_type;
00055       typedef _Compare                  key_compare;
00056       typedef _Compare                  value_compare;
00057       typedef _Allocator                allocator_type;
00058       typedef typename _Base::reference             reference;
00059       typedef typename _Base::const_reference       const_reference;
00060 
00061       typedef __gnu_debug::_Safe_iterator<typename _Base::iterator, set>
00062                                                     iterator;
00063       typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator, set>
00064                                                     const_iterator;
00065 
00066       typedef typename _Base::size_type             size_type;
00067       typedef typename _Base::difference_type       difference_type;
00068       typedef typename _Base::pointer               pointer;
00069       typedef typename _Base::const_pointer         const_pointer;
00070       typedef std::reverse_iterator<iterator>       reverse_iterator;
00071       typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00072 
00073       // 23.3.3.1 construct/copy/destroy:
00074       explicit set(const _Compare& __comp = _Compare(),
00075            const _Allocator& __a = _Allocator())
00076       : _Base(__comp, __a) { }
00077 
00078       template<typename _InputIterator>
00079         set(_InputIterator __first, _InputIterator __last,
00080         const _Compare& __comp = _Compare(),
00081         const _Allocator& __a = _Allocator())
00082     : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
00083                                      __last)),
00084         __gnu_debug::__base(__last),
00085         __comp, __a) { }
00086 
00087       set(const set& __x)
00088       : _Base(__x), _Safe_base() { }
00089 
00090       set(const _Base& __x)
00091       : _Base(__x), _Safe_base() { }
00092 
00093 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00094       set(set&& __x)
00095       : _Base(std::move(__x)), _Safe_base()
00096       { this->_M_swap(__x); }
00097 
00098       set(initializer_list<value_type> __l,
00099       const _Compare& __comp = _Compare(),
00100       const allocator_type& __a = allocator_type())
00101       : _Base(__l, __comp, __a), _Safe_base() { }
00102 #endif
00103 
00104       ~set() { }
00105 
00106       set&
00107       operator=(const set& __x)
00108       {
00109     *static_cast<_Base*>(this) = __x;
00110     this->_M_invalidate_all();
00111     return *this;
00112       }
00113 
00114 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00115       set&
00116       operator=(set&& __x)
00117       {
00118     // NB: DR 1204.
00119     // NB: DR 675.
00120     clear();
00121     swap(__x);
00122     return *this;
00123       }
00124 
00125       set&
00126       operator=(initializer_list<value_type> __l)
00127       {
00128     this->clear();
00129     this->insert(__l);
00130     return *this;
00131       }
00132 #endif
00133 
00134       using _Base::get_allocator;
00135 
00136       // iterators:
00137       iterator
00138       begin()
00139       { return iterator(_Base::begin(), this); }
00140 
00141       const_iterator
00142       begin() const
00143       { return const_iterator(_Base::begin(), this); }
00144 
00145       iterator
00146       end()
00147       { return iterator(_Base::end(), this); }
00148 
00149       const_iterator
00150       end() const
00151       { return const_iterator(_Base::end(), this); }
00152 
00153       reverse_iterator
00154       rbegin()
00155       { return reverse_iterator(end()); }
00156 
00157       const_reverse_iterator
00158       rbegin() const
00159       { return const_reverse_iterator(end()); }
00160 
00161       reverse_iterator
00162       rend()
00163       { return reverse_iterator(begin()); }
00164 
00165       const_reverse_iterator
00166       rend() const
00167       { return const_reverse_iterator(begin()); }
00168 
00169 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00170       const_iterator
00171       cbegin() const
00172       { return const_iterator(_Base::begin(), this); }
00173 
00174       const_iterator
00175       cend() const
00176       { return const_iterator(_Base::end(), this); }
00177 
00178       const_reverse_iterator
00179       crbegin() const
00180       { return const_reverse_iterator(end()); }
00181 
00182       const_reverse_iterator
00183       crend() const
00184       { return const_reverse_iterator(begin()); }
00185 #endif
00186 
00187       // capacity:
00188       using _Base::empty;
00189       using _Base::size;
00190       using _Base::max_size;
00191 
00192       // modifiers:
00193       std::pair<iterator, bool>
00194       insert(const value_type& __x)
00195       {
00196     typedef typename _Base::iterator _Base_iterator;
00197     std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
00198     return std::pair<iterator, bool>(iterator(__res.first, this),
00199                      __res.second);
00200       }
00201 
00202       iterator
00203       insert(iterator __position, const value_type& __x)
00204       {
00205     __glibcxx_check_insert(__position);
00206     return iterator(_Base::insert(__position.base(), __x), this);
00207       }
00208 
00209       template <typename _InputIterator>
00210         void
00211         insert(_InputIterator __first, _InputIterator __last)
00212         {
00213       __glibcxx_check_valid_range(__first, __last);
00214       _Base::insert(__gnu_debug::__base(__first),
00215             __gnu_debug::__base(__last));
00216     }
00217 
00218 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00219       void
00220       insert(initializer_list<value_type> __l)
00221       { _Base::insert(__l); }
00222 #endif
00223 
00224 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00225       iterator
00226       erase(iterator __position)
00227       {
00228     __glibcxx_check_erase(__position);
00229     __position._M_invalidate();
00230     return iterator(_Base::erase(__position.base()), this);
00231       }
00232 #else
00233       void
00234       erase(iterator __position)
00235       {
00236     __glibcxx_check_erase(__position);
00237     __position._M_invalidate();
00238     _Base::erase(__position.base());
00239       }
00240 #endif
00241 
00242       size_type
00243       erase(const key_type& __x)
00244       {
00245     iterator __victim = find(__x);
00246     if (__victim == end())
00247           return 0;
00248     else
00249         {
00250       __victim._M_invalidate();
00251       _Base::erase(__victim.base());
00252       return 1;
00253         }
00254       }
00255 
00256 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00257       iterator
00258       erase(iterator __first, iterator __last)
00259       {
00260     // _GLIBCXX_RESOLVE_LIB_DEFECTS
00261     // 151. can't currently clear() empty container
00262     __glibcxx_check_erase_range(__first, __last);
00263     while (__first != __last)
00264       this->erase(__first++);
00265     return __last;
00266       }
00267 #else
00268       void
00269       erase(iterator __first, iterator __last)
00270       {
00271     // _GLIBCXX_RESOLVE_LIB_DEFECTS
00272     // 151. can't currently clear() empty container
00273     __glibcxx_check_erase_range(__first, __last);
00274     while (__first != __last)
00275       this->erase(__first++);
00276       }
00277 #endif
00278 
00279       void
00280       swap(set& __x)
00281       {
00282     _Base::swap(__x);
00283     this->_M_swap(__x);
00284       }
00285 
00286       void
00287       clear()
00288       { this->erase(begin(), end()); }
00289 
00290       // observers:
00291       using _Base::key_comp;
00292       using _Base::value_comp;
00293 
00294       // set operations:
00295       iterator
00296       find(const key_type& __x)
00297       { return iterator(_Base::find(__x), this); }
00298 
00299       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00300       // 214. set::find() missing const overload
00301       const_iterator
00302       find(const key_type& __x) const
00303       { return const_iterator(_Base::find(__x), this); }
00304 
00305       using _Base::count;
00306 
00307       iterator
00308       lower_bound(const key_type& __x)
00309       { return iterator(_Base::lower_bound(__x), this); }
00310 
00311       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00312       // 214. set::find() missing const overload
00313       const_iterator
00314       lower_bound(const key_type& __x) const
00315       { return const_iterator(_Base::lower_bound(__x), this); }
00316 
00317       iterator
00318       upper_bound(const key_type& __x)
00319       { return iterator(_Base::upper_bound(__x), this); }
00320 
00321       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00322       // 214. set::find() missing const overload
00323       const_iterator
00324       upper_bound(const key_type& __x) const
00325       { return const_iterator(_Base::upper_bound(__x), this); }
00326 
00327       std::pair<iterator,iterator>
00328       equal_range(const key_type& __x)
00329       {
00330     typedef typename _Base::iterator _Base_iterator;
00331     std::pair<_Base_iterator, _Base_iterator> __res =
00332         _Base::equal_range(__x);
00333     return std::make_pair(iterator(__res.first, this),
00334                   iterator(__res.second, this));
00335       }
00336 
00337       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00338       // 214. set::find() missing const overload
00339       std::pair<const_iterator,const_iterator>
00340       equal_range(const key_type& __x) const
00341       {
00342     typedef typename _Base::const_iterator _Base_iterator;
00343     std::pair<_Base_iterator, _Base_iterator> __res =
00344         _Base::equal_range(__x);
00345     return std::make_pair(const_iterator(__res.first, this),
00346                   const_iterator(__res.second, this));
00347       }
00348 
00349       _Base&
00350       _M_base() { return *this; }
00351 
00352       const _Base&
00353       _M_base() const { return *this; }
00354 
00355     private:
00356       void
00357       _M_invalidate_all()
00358       {
00359     typedef typename _Base::const_iterator _Base_const_iterator;
00360     typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
00361     this->_M_invalidate_if(_Not_equal(_M_base().end()));
00362       }
00363     };
00364 
00365   template<typename _Key, typename _Compare, typename _Allocator>
00366     inline bool
00367     operator==(const set<_Key, _Compare, _Allocator>& __lhs,
00368            const set<_Key, _Compare, _Allocator>& __rhs)
00369     { return __lhs._M_base() == __rhs._M_base(); }
00370 
00371   template<typename _Key, typename _Compare, typename _Allocator>
00372     inline bool
00373     operator!=(const set<_Key, _Compare, _Allocator>& __lhs,
00374            const set<_Key, _Compare, _Allocator>& __rhs)
00375     { return __lhs._M_base() != __rhs._M_base(); }
00376 
00377   template<typename _Key, typename _Compare, typename _Allocator>
00378     inline bool
00379     operator<(const set<_Key, _Compare, _Allocator>& __lhs,
00380           const set<_Key, _Compare, _Allocator>& __rhs)
00381     { return __lhs._M_base() < __rhs._M_base(); }
00382 
00383   template<typename _Key, typename _Compare, typename _Allocator>
00384     inline bool
00385     operator<=(const set<_Key, _Compare, _Allocator>& __lhs,
00386            const set<_Key, _Compare, _Allocator>& __rhs)
00387     { return __lhs._M_base() <= __rhs._M_base(); }
00388 
00389   template<typename _Key, typename _Compare, typename _Allocator>
00390     inline bool
00391     operator>=(const set<_Key, _Compare, _Allocator>& __lhs,
00392            const set<_Key, _Compare, _Allocator>& __rhs)
00393     { return __lhs._M_base() >= __rhs._M_base(); }
00394 
00395   template<typename _Key, typename _Compare, typename _Allocator>
00396     inline bool
00397     operator>(const set<_Key, _Compare, _Allocator>& __lhs,
00398           const set<_Key, _Compare, _Allocator>& __rhs)
00399     { return __lhs._M_base() > __rhs._M_base(); }
00400 
00401   template<typename _Key, typename _Compare, typename _Allocator>
00402     void
00403     swap(set<_Key, _Compare, _Allocator>& __x,
00404      set<_Key, _Compare, _Allocator>& __y)
00405     { return __x.swap(__y); }
00406 
00407 } // namespace __debug
00408 } // namespace std
00409 
00410 #endif