# set.h

Go to the documentation of this file.

    00001 // Debugging set implementation -*- C++ -*-
    00002
    00003 // Copyright (C) 2003, 2004, 2005
    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 2, 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 // You should have received a copy of the GNU General Public License along
    00018 // with this library; see the file COPYING.  If not, write to the Free
    00019 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
    00020 // USA.
    00021
    00022 // As a special exception, you may use this file as part of a free software
    00023 // library without restriction.  Specifically, if other files instantiate
    00024 // templates or use macros or inline functions from this file, or you compile
    00025 // this file and link it with other files to produce an executable, this
    00026 // file does not by itself cause the resulting executable to be covered by
    00027 // the GNU General Public License.  This exception does not however
    00028 // invalidate any other reasons why the executable file might be covered by
    00029 // the GNU General Public License.
    00030
    00031 /** @file debug/set.h
    00032  *  This file is a GNU debug extension to the Standard C++ Library.
    00033  */
    00034
    00035 #ifndef _GLIBCXX_DEBUG_SET_H
    00036 #define _GLIBCXX_DEBUG_SET_H 1
    00037
    00038 #include <debug/safe_sequence.h>
    00039 #include <debug/safe_iterator.h>
    00040 #include <utility>
    00041
    00042 namespace std
    00043 {
    00044 namespace __debug
    00045 {
    00046   template<typename _Key, typename _Compare = std::less<_Key>,
    00047        typename _Allocator = std::allocator<_Key> >
    00048     class set
    00049     : public _GLIBCXX_STD::set<_Key,_Compare,_Allocator>,
    00050       public __gnu_debug::_Safe_sequence<set<_Key, _Compare, _Allocator> >
    00051     {
    00052       typedef _GLIBCXX_STD::set<_Key,_Compare,_Allocator> _Base;
    00053       typedef __gnu_debug::_Safe_sequence<set> _Safe_base;
    00054
    00055     public:
    00056       // types:
    00057       typedef _Key                  key_type;
    00058       typedef _Key                  value_type;
    00059       typedef _Compare                  key_compare;
    00060       typedef _Compare                  value_compare;
    00061       typedef _Allocator                allocator_type;
    00062       typedef typename _Base::reference             reference;
    00063       typedef typename _Base::const_reference       const_reference;
    00064
    00065       typedef __gnu_debug::_Safe_iterator<typename _Base::iterator, set>
    00066                                                     iterator;
    00067       typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator, set>
    00068                                                     const_iterator;
    00069
    00070       typedef typename _Base::size_type             size_type;
    00071       typedef typename _Base::difference_type       difference_type;
    00072       typedef typename _Base::pointer               pointer;
    00073       typedef typename _Base::const_pointer         const_pointer;
    00074       typedef std::reverse_iterator<iterator>       reverse_iterator;
    00075       typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
    00076
    00077       // 23.3.3.1 construct/copy/destroy:
    00078       explicit set(const _Compare& __comp = _Compare(),
    00079            const _Allocator& __a = _Allocator())
    00080       : _Base(__comp, __a) { }
    00081
    00082       template<typename _InputIterator>
    00083         set(_InputIterator __first, _InputIterator __last,
    00084         const _Compare& __comp = _Compare(),
    00085         const _Allocator& __a = _Allocator())
    00086     : _Base(__gnu_debug::__check_valid_range(__first, __last), __last,
    00087         __comp, __a) { }
    00088
    00089       set(const set<_Key,_Compare,_Allocator>& __x)
    00090       : _Base(__x), _Safe_base() { }
    00091
    00092       set(const _Base& __x) : _Base(__x), _Safe_base() { }
    00093
    00094       ~set() { }
    00095
    00096       set<_Key,_Compare,_Allocator>&
    00097       operator=(const set<_Key,_Compare,_Allocator>& __x)
    00098       {
    00099     *static_cast<_Base*>(this) = __x;
    00100     this->_M_invalidate_all();
    00101     return *this;
    00102       }
    00103
    00104       using _Base::get_allocator;
    00105
    00106       // iterators:
    00107       iterator
    00108       begin()
    00109       { return iterator(_Base::begin(), this); }
    00110
    00111       const_iterator
    00112       begin() const
    00113       { return const_iterator(_Base::begin(), this); }
    00114
    00115       iterator
    00116       end()
    00117       { return iterator(_Base::end(), this); }
    00118
    00119       const_iterator
    00120       end() const
    00121       { return const_iterator(_Base::end(), this); }
    00122
    00123       reverse_iterator
    00124       rbegin()
    00125       { return reverse_iterator(end()); }
    00126
    00127       const_reverse_iterator
    00128       rbegin() const
    00129       { return const_reverse_iterator(end()); }
    00130
    00131       reverse_iterator
    00132       rend()
    00133       { return reverse_iterator(begin()); }
    00134
    00135       const_reverse_iterator
    00136       rend() const
    00137       { return const_reverse_iterator(begin()); }
    00138
    00139       // capacity:
    00140       using _Base::empty;
    00141       using _Base::size;
    00142       using _Base::max_size;
    00143
    00144       // modifiers:
    00145       std::pair<iterator, bool>
    00146       insert(const value_type& __x)
    00147       {
    00148     typedef typename _Base::iterator _Base_iterator;
    00149     std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
    00150     return std::pair<iterator, bool>(iterator(__res.first, this),
    00151                      __res.second);
    00152       }
    00153
    00154       iterator
    00155       insert(iterator __position, const value_type& __x)
    00156       {
    00157     __glibcxx_check_insert(__position);
    00158     return iterator(_Base::insert(__position.base(), __x), this);
    00159       }
    00160
    00161       template <typename _InputIterator>
    00162         void
    00163         insert(_InputIterator __first, _InputIterator __last)
    00164         {
    00165       __glibcxx_check_valid_range(__first, __last);
    00166       _Base::insert(__first, __last);
    00167     }
    00168
    00169       void
    00170       erase(iterator __position)
    00171       {
    00172     __glibcxx_check_erase(__position);
    00173     __position._M_invalidate();
    00174     _Base::erase(__position.base());
    00175       }
    00176
    00177       size_type
    00178       erase(const key_type& __x)
    00179       {
    00180     iterator __victim = find(__x);
    00181     if (__victim == end())
    00182           return 0;
    00183     else
    00184         {
    00185       __victim._M_invalidate();
    00186       _Base::erase(__victim.base());
    00187       return 1;
    00188         }
    00189       }
    00190
    00191       void
    00192       erase(iterator __first, iterator __last)
    00193       {
    00194     // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00195     // 151. can't currently clear() empty container
    00196     __glibcxx_check_erase_range(__first, __last);
    00197
    00198     while (__first != __last)
    00199         this->erase(__first++);
    00200       }
    00201
    00202       void
    00203       swap(set<_Key,_Compare,_Allocator>& __x)
    00204       {
    00205     _Base::swap(__x);
    00206     this->_M_swap(__x);
    00207       }
    00208
    00209       void
    00210       clear()
    00211       { this->erase(begin(), end()); }
    00212
    00213       // observers:
    00214       using _Base::key_comp;
    00215       using _Base::value_comp;
    00216
    00217       // set operations:
    00218       iterator
    00219       find(const key_type& __x)
    00220       { return iterator(_Base::find(__x), this); }
    00221
    00222       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00223       // 214. set::find() missing const overload
    00224       const_iterator
    00225       find(const key_type& __x) const
    00226       { return const_iterator(_Base::find(__x), this); }
    00227
    00228       using _Base::count;
    00229
    00230       iterator
    00231       lower_bound(const key_type& __x)
    00232       { return iterator(_Base::lower_bound(__x), this); }
    00233
    00234       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00235       // 214. set::find() missing const overload
    00236       const_iterator
    00237       lower_bound(const key_type& __x) const
    00238       { return const_iterator(_Base::lower_bound(__x), this); }
    00239
    00240       iterator
    00241       upper_bound(const key_type& __x)
    00242       { return iterator(_Base::upper_bound(__x), this); }
    00243
    00244       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00245       // 214. set::find() missing const overload
    00246       const_iterator
    00247       upper_bound(const key_type& __x) const
    00248       { return const_iterator(_Base::upper_bound(__x), this); }
    00249
    00250       std::pair<iterator,iterator>
    00251       equal_range(const key_type& __x)
    00252       {
    00253     typedef typename _Base::iterator _Base_iterator;
    00254     std::pair<_Base_iterator, _Base_iterator> __res =
    00255         _Base::equal_range(__x);
    00256     return std::make_pair(iterator(__res.first, this),
    00257                   iterator(__res.second, this));
    00258       }
    00259
    00260       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00261       // 214. set::find() missing const overload
    00262       std::pair<const_iterator,const_iterator>
    00263       equal_range(const key_type& __x) const
    00264       {
    00265     typedef typename _Base::const_iterator _Base_iterator;
    00266     std::pair<_Base_iterator, _Base_iterator> __res =
    00267         _Base::equal_range(__x);
    00268     return std::make_pair(const_iterator(__res.first, this),
    00269                   const_iterator(__res.second, this));
    00270       }
    00271
    00272       _Base&
    00273       _M_base() { return *this; }
    00274
    00275       const _Base&
    00276       _M_base() const { return *this; }
    00277
    00278     private:
    00279       void
    00280       _M_invalidate_all()
    00281       {
    00282     typedef typename _Base::const_iterator _Base_const_iterator;
    00283     typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
    00284     this->_M_invalidate_if(_Not_equal(_M_base().end()));
    00285       }
    00286     };
    00287
    00288   template<typename _Key, typename _Compare, typename _Allocator>
    00289     inline bool
    00290     operator==(const set<_Key,_Compare,_Allocator>& __lhs,
    00291            const set<_Key,_Compare,_Allocator>& __rhs)
    00292     { return __lhs._M_base() == __rhs._M_base(); }
    00293
    00294   template<typename _Key, typename _Compare, typename _Allocator>
    00295     inline bool
    00296     operator!=(const set<_Key,_Compare,_Allocator>& __lhs,
    00297            const set<_Key,_Compare,_Allocator>& __rhs)
    00298     { return __lhs._M_base() != __rhs._M_base(); }
    00299
    00300   template<typename _Key, typename _Compare, typename _Allocator>
    00301     inline bool
    00302     operator<(const set<_Key,_Compare,_Allocator>& __lhs,
    00303           const set<_Key,_Compare,_Allocator>& __rhs)
    00304     { return __lhs._M_base() < __rhs._M_base(); }
    00305
    00306   template<typename _Key, typename _Compare, typename _Allocator>
    00307     inline bool
    00308     operator<=(const set<_Key,_Compare,_Allocator>& __lhs,
    00309            const set<_Key,_Compare,_Allocator>& __rhs)
    00310     { return __lhs._M_base() <= __rhs._M_base(); }
    00311
    00312   template<typename _Key, typename _Compare, typename _Allocator>
    00313     inline bool
    00314     operator>=(const set<_Key,_Compare,_Allocator>& __lhs,
    00315            const set<_Key,_Compare,_Allocator>& __rhs)
    00316     { return __lhs._M_base() >= __rhs._M_base(); }
    00317
    00318   template<typename _Key, typename _Compare, typename _Allocator>
    00319     inline bool
    00320     operator>(const set<_Key,_Compare,_Allocator>& __lhs,
    00321           const set<_Key,_Compare,_Allocator>& __rhs)
    00322     { return __lhs._M_base() > __rhs._M_base(); }
    00323
    00324   template<typename _Key, typename _Compare, typename _Allocator>
    00325     void
    00326     swap(set<_Key,_Compare,_Allocator>& __x,
    00327      set<_Key,_Compare,_Allocator>& __y)
    00328     { return __x.swap(__y); }
    00329 } // namespace __debug
    00330 } // namespace std
    00331
    00332 #endif