# set.h

    00001 // Debugging set implementation -*- C++ -*-
    00002
    00003 // Copyright (C) 2003, 2004
    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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
    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 #ifndef _GLIBCXX_DEBUG_SET_H
    00032 #define _GLIBCXX_DEBUG_SET_H 1
    00033
    00034 #include <debug/safe_sequence.h>
    00035 #include <debug/safe_iterator.h>
    00036 #include <utility>
    00037
    00038 namespace __gnu_debug_def
    00039 {
    00040   template<typename _Key, typename _Compare = std::less<_Key>,
    00041        typename _Allocator = std::allocator<_Key> >
    00042     class set
    00043     : public _GLIBCXX_STD::set<_Key,_Compare,_Allocator>,
    00044       public __gnu_debug::_Safe_sequence<set<_Key, _Compare, _Allocator> >
    00045     {
    00046       typedef _GLIBCXX_STD::set<_Key,_Compare,_Allocator> _Base;
    00047       typedef __gnu_debug::_Safe_sequence<set> _Safe_base;
    00048
    00049     public:
    00050       // types:
    00051       typedef _Key                  key_type;
    00052       typedef _Key                  value_type;
    00053       typedef _Compare                  key_compare;
    00054       typedef _Compare                  value_compare;
    00055       typedef _Allocator                allocator_type;
    00056       typedef typename _Allocator::reference        reference;
    00057       typedef typename _Allocator::const_reference  const_reference;
    00058
    00059       typedef __gnu_debug::_Safe_iterator<typename _Base::iterator, set>
    00060                                                     iterator;
    00061       typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator, set>
    00062                                                     const_iterator;
    00063
    00064       typedef typename _Base::size_type             size_type;
    00065       typedef typename _Base::difference_type       difference_type;
    00066       typedef typename _Allocator::pointer          pointer;
    00067       typedef typename _Allocator::const_pointer    const_pointer;
    00068       typedef std::reverse_iterator<iterator>       reverse_iterator;
    00069       typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
    00070
    00071       // 23.3.3.1 construct/copy/destroy:
    00072       explicit set(const _Compare& __comp = _Compare(),
    00073            const _Allocator& __a = _Allocator())
    00074       : _Base(__comp, __a) { }
    00075
    00076       template<typename _InputIterator>
    00077         set(_InputIterator __first, _InputIterator __last,
    00078         const _Compare& __comp = _Compare(),
    00079         const _Allocator& __a = _Allocator())
    00080     : _Base(__gnu_debug::__check_valid_range(__first, __last), __last,
    00081         __comp, __a) { }
    00082
    00083       set(const set<_Key,_Compare,_Allocator>& __x)
    00084       : _Base(__x), _Safe_base() { }
    00085
    00086       set(const _Base& __x) : _Base(__x), _Safe_base() { }
    00087
    00088       ~set() { }
    00089
    00090       set<_Key,_Compare,_Allocator>&
    00091       operator=(const set<_Key,_Compare,_Allocator>& __x)
    00092       {
    00093     *static_cast<_Base*>(this) = __x;
    00094     this->_M_invalidate_all();
    00095     return *this;
    00096       }
    00097
    00098       using _Base::get_allocator;
    00099
    00100       // iterators:
    00101       iterator
    00102       begin()
    00103       { return iterator(_Base::begin(), this); }
    00104
    00105       const_iterator
    00106       begin() const
    00107       { return const_iterator(_Base::begin(), this); }
    00108
    00109       iterator
    00110       end()
    00111       { return iterator(_Base::end(), this); }
    00112
    00113       const_iterator
    00114       end() const
    00115       { return const_iterator(_Base::end(), this); }
    00116
    00117       reverse_iterator
    00118       rbegin()
    00119       { return reverse_iterator(end()); }
    00120
    00121       const_reverse_iterator
    00122       rbegin() const
    00123       { return const_reverse_iterator(end()); }
    00124
    00125       reverse_iterator
    00126       rend()
    00127       { return reverse_iterator(begin()); }
    00128
    00129       const_reverse_iterator
    00130       rend() const
    00131       { return const_reverse_iterator(begin()); }
    00132
    00133       // capacity:
    00134       using _Base::empty;
    00135       using _Base::size;
    00136       using _Base::max_size;
    00137
    00138       // modifiers:
    00139       std::pair<iterator, bool>
    00140       insert(const value_type& __x)
    00141       {
    00142     typedef typename _Base::iterator _Base_iterator;
    00143     std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
    00144     return std::pair<iterator, bool>(iterator(__res.first, this),
    00145                      __res.second);
    00146       }
    00147
    00148       iterator
    00149       insert(iterator __position, const value_type& __x)
    00150       {
    00151     __glibcxx_check_insert(__position);
    00152     return iterator(_Base::insert(__position.base(), __x), this);
    00153       }
    00154
    00155       template <typename _InputIterator>
    00156         void
    00157         insert(_InputIterator __first, _InputIterator __last)
    00158         {
    00159       __glibcxx_check_valid_range(__first, __last);
    00160       _Base::insert(__first, __last);
    00161     }
    00162
    00163       void
    00164       erase(iterator __position)
    00165       {
    00166     __glibcxx_check_erase(__position);
    00167     __position._M_invalidate();
    00168     _Base::erase(__position.base());
    00169       }
    00170
    00171       size_type
    00172       erase(const key_type& __x)
    00173       {
    00174     iterator __victim = find(__x);
    00175     if (__victim == end())
    00176           return 0;
    00177     else
    00178         {
    00179       __victim._M_invalidate();
    00180       _Base::erase(__victim.base());
    00181       return 1;
    00182         }
    00183       }
    00184
    00185       void
    00186       erase(iterator __first, iterator __last)
    00187       {
    00188     // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00189     // 151. can't currently clear() empty container
    00190     __glibcxx_check_erase_range(__first, __last);
    00191
    00192     while (__first != __last)
    00193         this->erase(__first++);
    00194       }
    00195
    00196       void
    00197       swap(set<_Key,_Compare,_Allocator>& __x)
    00198       {
    00199     _Base::swap(__x);
    00200     this->_M_swap(__x);
    00201       }
    00202
    00203       void
    00204       clear()
    00205       { this->erase(begin(), end()); }
    00206
    00207       // observers:
    00208       using _Base::key_comp;
    00209       using _Base::value_comp;
    00210
    00211       // set operations:
    00212       iterator
    00213       find(const key_type& __x)
    00214       { return iterator(_Base::find(__x), this); }
    00215
    00216       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00217       // 214. set::find() missing const overload
    00218       const_iterator
    00219       find(const key_type& __x) const
    00220       { return const_iterator(_Base::find(__x), this); }
    00221
    00222       using _Base::count;
    00223
    00224       iterator
    00225       lower_bound(const key_type& __x)
    00226       { return iterator(_Base::lower_bound(__x), this); }
    00227
    00228       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00229       // 214. set::find() missing const overload
    00230       const_iterator
    00231       lower_bound(const key_type& __x) const
    00232       { return const_iterator(_Base::lower_bound(__x), this); }
    00233
    00234       iterator
    00235       upper_bound(const key_type& __x)
    00236       { return iterator(_Base::upper_bound(__x), this); }
    00237
    00238       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00239       // 214. set::find() missing const overload
    00240       const_iterator
    00241       upper_bound(const key_type& __x) const
    00242       { return const_iterator(_Base::upper_bound(__x), this); }
    00243
    00244       std::pair<iterator,iterator>
    00245       equal_range(const key_type& __x)
    00246       {
    00247     typedef typename _Base::iterator _Base_iterator;
    00248     std::pair<_Base_iterator, _Base_iterator> __res =
    00249         _Base::equal_range(__x);
    00250     return std::make_pair(iterator(__res.first, this),
    00251                   iterator(__res.second, this));
    00252       }
    00253
    00254       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00255       // 214. set::find() missing const overload
    00256       std::pair<const_iterator,const_iterator>
    00257       equal_range(const key_type& __x) const
    00258       {
    00259     typedef typename _Base::const_iterator _Base_iterator;
    00260     std::pair<_Base_iterator, _Base_iterator> __res =
    00261         _Base::equal_range(__x);
    00262     return std::make_pair(const_iterator(__res.first, this),
    00263                   const_iterator(__res.second, this));
    00264       }
    00265
    00266       _Base&
    00267       _M_base() { return *this; }
    00268
    00269       const _Base&
    00270       _M_base() const { return *this; }
    00271
    00272     private:
    00273       void
    00274       _M_invalidate_all()
    00275       {
    00276     typedef typename _Base::const_iterator _Base_const_iterator;
    00277     typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
    00278     this->_M_invalidate_if(_Not_equal(_M_base().end()));
    00279       }
    00280     };
    00281
    00282   template<typename _Key, typename _Compare, typename _Allocator>
    00283     inline bool
    00284     operator==(const set<_Key,_Compare,_Allocator>& __lhs,
    00285            const set<_Key,_Compare,_Allocator>& __rhs)
    00286     { return __lhs._M_base() == __rhs._M_base(); }
    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     void
    00320     swap(set<_Key,_Compare,_Allocator>& __x,
    00321      set<_Key,_Compare,_Allocator>& __y)
    00322     { return __x.swap(__y); }
    00323 } // namespace __gnu_debug_def
    00324
    00325 #endif