# set.h

Go to the documentation of this file.

    00001 // Debugging set implementation -*- C++ -*-
    00002
    00003 // Copyright (C) 2003, 2004, 2005, 2006, 2007
    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_D::set<_Key,_Compare,_Allocator>,
    00050       public __gnu_debug::_Safe_sequence<set<_Key, _Compare, _Allocator> >
    00051     {
    00052       typedef _GLIBCXX_STD_D::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& __x)
    00090       : _Base(__x), _Safe_base() { }
    00091
    00092       set(const _Base& __x)
    00093       : _Base(__x), _Safe_base() { }
    00094
    00095 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    00096       set(set&& __x)
    00097       : _Base(std::forward<set>(__x)), _Safe_base()
    00098       { this->_M_swap(__x); }
    00099 #endif
    00100
    00101       ~set() { }
    00102
    00103       set&
    00104       operator=(const set& __x)
    00105       {
    00106     *static_cast<_Base*>(this) = __x;
    00107     this->_M_invalidate_all();
    00108     return *this;
    00109       }
    00110
    00111 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    00112       set&
    00113       operator=(set&& __x)
    00114       {
    00115         // NB: DR 675.
    00116     clear();
    00117     swap(__x);
    00118     return *this;
    00119       }
    00120 #endif
    00121
    00122       using _Base::get_allocator;
    00123
    00124       // iterators:
    00125       iterator
    00126       begin()
    00127       { return iterator(_Base::begin(), this); }
    00128
    00129       const_iterator
    00130       begin() const
    00131       { return const_iterator(_Base::begin(), this); }
    00132
    00133       iterator
    00134       end()
    00135       { return iterator(_Base::end(), this); }
    00136
    00137       const_iterator
    00138       end() const
    00139       { return const_iterator(_Base::end(), this); }
    00140
    00141       reverse_iterator
    00142       rbegin()
    00143       { return reverse_iterator(end()); }
    00144
    00145       const_reverse_iterator
    00146       rbegin() const
    00147       { return const_reverse_iterator(end()); }
    00148
    00149       reverse_iterator
    00150       rend()
    00151       { return reverse_iterator(begin()); }
    00152
    00153       const_reverse_iterator
    00154       rend() const
    00155       { return const_reverse_iterator(begin()); }
    00156
    00157 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    00158       const_iterator
    00159       cbegin() const
    00160       { return const_iterator(_Base::begin(), this); }
    00161
    00162       const_iterator
    00163       cend() const
    00164       { return const_iterator(_Base::end(), this); }
    00165
    00166       const_reverse_iterator
    00167       crbegin() const
    00168       { return const_reverse_iterator(end()); }
    00169
    00170       const_reverse_iterator
    00171       crend() const
    00172       { return const_reverse_iterator(begin()); }
    00173 #endif
    00174
    00175       // capacity:
    00176       using _Base::empty;
    00177       using _Base::size;
    00178       using _Base::max_size;
    00179
    00180       // modifiers:
    00181       std::pair<iterator, bool>
    00182       insert(const value_type& __x)
    00183       {
    00184     typedef typename _Base::iterator _Base_iterator;
    00185     std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
    00186     return std::pair<iterator, bool>(iterator(__res.first, this),
    00187                      __res.second);
    00188       }
    00189
    00190       iterator
    00191       insert(iterator __position, const value_type& __x)
    00192       {
    00193     __glibcxx_check_insert(__position);
    00194     return iterator(_Base::insert(__position.base(), __x), this);
    00195       }
    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       void
    00206       erase(iterator __position)
    00207       {
    00208     __glibcxx_check_erase(__position);
    00209     __position._M_invalidate();
    00210     _Base::erase(__position.base());
    00211       }
    00212
    00213       size_type
    00214       erase(const key_type& __x)
    00215       {
    00216     iterator __victim = find(__x);
    00217     if (__victim == end())
    00218           return 0;
    00219     else
    00220         {
    00221       __victim._M_invalidate();
    00222       _Base::erase(__victim.base());
    00223       return 1;
    00224         }
    00225       }
    00226
    00227       void
    00228       erase(iterator __first, iterator __last)
    00229       {
    00230     // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00231     // 151. can't currently clear() empty container
    00232     __glibcxx_check_erase_range(__first, __last);
    00233
    00234     while (__first != __last)
    00235         this->erase(__first++);
    00236       }
    00237
    00238       void
    00239 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    00240       swap(set&& __x)
    00241 #else
    00242       swap(set& __x)
    00243 #endif
    00244       {
    00245     _Base::swap(__x);
    00246     this->_M_swap(__x);
    00247       }
    00248
    00249       void
    00250       clear()
    00251       { this->erase(begin(), end()); }
    00252
    00253       // observers:
    00254       using _Base::key_comp;
    00255       using _Base::value_comp;
    00256
    00257       // set operations:
    00258       iterator
    00259       find(const key_type& __x)
    00260       { return iterator(_Base::find(__x), this); }
    00261
    00262       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00263       // 214. set::find() missing const overload
    00264       const_iterator
    00265       find(const key_type& __x) const
    00266       { return const_iterator(_Base::find(__x), this); }
    00267
    00268       using _Base::count;
    00269
    00270       iterator
    00271       lower_bound(const key_type& __x)
    00272       { return iterator(_Base::lower_bound(__x), this); }
    00273
    00274       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00275       // 214. set::find() missing const overload
    00276       const_iterator
    00277       lower_bound(const key_type& __x) const
    00278       { return const_iterator(_Base::lower_bound(__x), this); }
    00279
    00280       iterator
    00281       upper_bound(const key_type& __x)
    00282       { return iterator(_Base::upper_bound(__x), this); }
    00283
    00284       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00285       // 214. set::find() missing const overload
    00286       const_iterator
    00287       upper_bound(const key_type& __x) const
    00288       { return const_iterator(_Base::upper_bound(__x), this); }
    00289
    00290       std::pair<iterator,iterator>
    00291       equal_range(const key_type& __x)
    00292       {
    00293     typedef typename _Base::iterator _Base_iterator;
    00294     std::pair<_Base_iterator, _Base_iterator> __res =
    00295         _Base::equal_range(__x);
    00296     return std::make_pair(iterator(__res.first, this),
    00297                   iterator(__res.second, this));
    00298       }
    00299
    00300       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00301       // 214. set::find() missing const overload
    00302       std::pair<const_iterator,const_iterator>
    00303       equal_range(const key_type& __x) const
    00304       {
    00305     typedef typename _Base::const_iterator _Base_iterator;
    00306     std::pair<_Base_iterator, _Base_iterator> __res =
    00307         _Base::equal_range(__x);
    00308     return std::make_pair(const_iterator(__res.first, this),
    00309                   const_iterator(__res.second, this));
    00310       }
    00311
    00312       _Base&
    00313       _M_base() { return *this; }
    00314
    00315       const _Base&
    00316       _M_base() const { return *this; }
    00317
    00318     private:
    00319       void
    00320       _M_invalidate_all()
    00321       {
    00322     typedef typename _Base::const_iterator _Base_const_iterator;
    00323     typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
    00324     this->_M_invalidate_if(_Not_equal(_M_base().end()));
    00325       }
    00326     };
    00327
    00328   template<typename _Key, typename _Compare, typename _Allocator>
    00329     inline bool
    00330     operator==(const set<_Key, _Compare, _Allocator>& __lhs,
    00331            const set<_Key, _Compare, _Allocator>& __rhs)
    00332     { return __lhs._M_base() == __rhs._M_base(); }
    00333
    00334   template<typename _Key, typename _Compare, typename _Allocator>
    00335     inline bool
    00336     operator!=(const set<_Key, _Compare, _Allocator>& __lhs,
    00337            const set<_Key, _Compare, _Allocator>& __rhs)
    00338     { return __lhs._M_base() != __rhs._M_base(); }
    00339
    00340   template<typename _Key, typename _Compare, typename _Allocator>
    00341     inline bool
    00342     operator<(const set<_Key, _Compare, _Allocator>& __lhs,
    00343           const set<_Key, _Compare, _Allocator>& __rhs)
    00344     { return __lhs._M_base() < __rhs._M_base(); }
    00345
    00346   template<typename _Key, typename _Compare, typename _Allocator>
    00347     inline bool
    00348     operator<=(const set<_Key, _Compare, _Allocator>& __lhs,
    00349            const set<_Key, _Compare, _Allocator>& __rhs)
    00350     { return __lhs._M_base() <= __rhs._M_base(); }
    00351
    00352   template<typename _Key, typename _Compare, typename _Allocator>
    00353     inline bool
    00354     operator>=(const set<_Key, _Compare, _Allocator>& __lhs,
    00355            const set<_Key, _Compare, _Allocator>& __rhs)
    00356     { return __lhs._M_base() >= __rhs._M_base(); }
    00357
    00358   template<typename _Key, typename _Compare, typename _Allocator>
    00359     inline bool
    00360     operator>(const set<_Key, _Compare, _Allocator>& __lhs,
    00361           const set<_Key, _Compare, _Allocator>& __rhs)
    00362     { return __lhs._M_base() > __rhs._M_base(); }
    00363
    00364   template<typename _Key, typename _Compare, typename _Allocator>
    00365     void
    00366     swap(set<_Key, _Compare, _Allocator>& __x,
    00367      set<_Key, _Compare, _Allocator>& __y)
    00368     { return __x.swap(__y); }
    00369
    00370 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    00371   template<typename _Key, typename _Compare, typename _Allocator>
    00372     void
    00373     swap(set<_Key, _Compare, _Allocator>&& __x,
    00374      set<_Key, _Compare, _Allocator>& __y)
    00375     { return __x.swap(__y); }
    00376
    00377   template<typename _Key, typename _Compare, typename _Allocator>
    00378     void
    00379     swap(set<_Key, _Compare, _Allocator>& __x,
    00380      set<_Key, _Compare, _Allocator>&& __y)
    00381     { return __x.swap(__y); }
    00382 #endif
    00383
    00384 } // namespace __debug
    00385 } // namespace std
    00386
    00387 #endif