libstdc++
profile/set.h
Go to the documentation of this file.
00001 // Profiling set implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file profile/set.h
00026  *  This file is a GNU profile extension to the Standard C++ Library.
00027  */
00028 
00029 #ifndef _GLIBCXX_PROFILE_SET_H
00030 #define _GLIBCXX_PROFILE_SET_H 1
00031 
00032 #include <utility>
00033 
00034 namespace std _GLIBCXX_VISIBILITY(default) 
00035 {
00036 namespace __profile
00037 {
00038   /// Class std::set wrapper with performance instrumentation.
00039   template<typename _Key, typename _Compare = std::less<_Key>,
00040        typename _Allocator = std::allocator<_Key> >
00041     class set
00042     : public _GLIBCXX_STD_C::set<_Key,_Compare,_Allocator>
00043     {
00044       typedef _GLIBCXX_STD_C::set<_Key, _Compare, _Allocator> _Base;
00045 
00046     public:
00047       // types:
00048       typedef _Key                  key_type;
00049       typedef _Key                  value_type;
00050       typedef _Compare                  key_compare;
00051       typedef _Compare                  value_compare;
00052       typedef _Allocator                allocator_type;
00053       typedef typename _Base::reference             reference;
00054       typedef typename _Base::const_reference       const_reference;
00055 
00056       typedef typename _Base::iterator               iterator;
00057       typedef typename _Base::const_iterator         const_iterator;
00058       typedef typename _Base::reverse_iterator       reverse_iterator;
00059       typedef typename _Base::const_reverse_iterator const_reverse_iterator;
00060 
00061       typedef typename _Base::size_type             size_type;
00062       typedef typename _Base::difference_type       difference_type;
00063       typedef typename _Base::pointer               pointer;
00064       typedef typename _Base::const_pointer         const_pointer;
00065 
00066       // 23.3.3.1 construct/copy/destroy:
00067       explicit set(const _Compare& __comp = _Compare(),
00068            const _Allocator& __a = _Allocator())
00069       : _Base(__comp, __a) { }
00070 
00071       template<typename _InputIterator>
00072         set(_InputIterator __first, _InputIterator __last,
00073         const _Compare& __comp = _Compare(),
00074         const _Allocator& __a = _Allocator())
00075     : _Base(__first, __last, __comp, __a) { }
00076 
00077       set(const set& __x)
00078       : _Base(__x) { }
00079 
00080       set(const _Base& __x)
00081       : _Base(__x) { }
00082 
00083 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00084       set(set&& __x)
00085       noexcept(is_nothrow_copy_constructible<_Compare>::value)
00086       : _Base(std::move(__x))
00087       { }
00088 
00089       set(initializer_list<value_type> __l,
00090       const _Compare& __comp = _Compare(),
00091       const allocator_type& __a = allocator_type())
00092       : _Base(__l, __comp, __a) { }
00093 #endif
00094 
00095       ~set() _GLIBCXX_NOEXCEPT { }
00096 
00097       set&
00098       operator=(const set& __x)
00099       {
00100     *static_cast<_Base*>(this) = __x;
00101     return *this;
00102       }
00103 
00104 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00105       set&
00106       operator=(set&& __x)
00107       {
00108     // NB: DR 1204.
00109     // NB: DR 675.
00110     this->clear();
00111     this->swap(__x);
00112     return *this;
00113       }
00114 
00115       set&
00116       operator=(initializer_list<value_type> __l)
00117       {
00118     this->clear();
00119     this->insert(__l);
00120     return *this;
00121       }
00122 #endif
00123 
00124       using _Base::get_allocator;
00125 
00126       // iterators:
00127       iterator
00128       begin() _GLIBCXX_NOEXCEPT
00129       { return iterator(_Base::begin()); }
00130 
00131       const_iterator
00132       begin() const _GLIBCXX_NOEXCEPT
00133       { return const_iterator(_Base::begin()); }
00134 
00135       iterator
00136       end() _GLIBCXX_NOEXCEPT
00137       { return iterator(_Base::end()); }
00138 
00139       const_iterator
00140       end() const _GLIBCXX_NOEXCEPT
00141       { return const_iterator(_Base::end()); }
00142 
00143       reverse_iterator
00144       rbegin() _GLIBCXX_NOEXCEPT
00145       { return reverse_iterator(end()); }
00146 
00147       const_reverse_iterator
00148       rbegin() const _GLIBCXX_NOEXCEPT
00149       { return const_reverse_iterator(end()); }
00150 
00151       reverse_iterator
00152       rend() _GLIBCXX_NOEXCEPT
00153       { return reverse_iterator(begin()); }
00154 
00155       const_reverse_iterator
00156       rend() const _GLIBCXX_NOEXCEPT
00157       { return const_reverse_iterator(begin()); }
00158 
00159 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00160       const_iterator
00161       cbegin() const noexcept
00162       { return const_iterator(_Base::begin()); }
00163 
00164       const_iterator
00165       cend() const noexcept
00166       { return const_iterator(_Base::end()); }
00167 
00168       const_reverse_iterator
00169       crbegin() const noexcept
00170       { return const_reverse_iterator(end()); }
00171 
00172       const_reverse_iterator
00173       crend() const noexcept
00174       { return const_reverse_iterator(begin()); }
00175 #endif
00176 
00177       // capacity:
00178       using _Base::empty;
00179       using _Base::size;
00180       using _Base::max_size;
00181 
00182       // modifiers:
00183       std::pair<iterator, bool>
00184       insert(const value_type& __x)
00185       {
00186     typedef typename _Base::iterator _Base_iterator;
00187     std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
00188     return std::pair<iterator, bool>(iterator(__res.first),
00189                      __res.second);
00190       }
00191 
00192 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00193       std::pair<iterator, bool>
00194       insert(value_type&& __x)
00195       {
00196     typedef typename _Base::iterator _Base_iterator;
00197     std::pair<_Base_iterator, bool> __res
00198       = _Base::insert(std::move(__x));
00199     return std::pair<iterator, bool>(iterator(__res.first),
00200                      __res.second);
00201       }
00202 #endif
00203 
00204       iterator
00205       insert(const_iterator __position, const value_type& __x)
00206       { return iterator(_Base::insert(__position, __x)); }
00207 
00208 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00209       iterator
00210       insert(const_iterator __position, value_type&& __x)
00211       { return iterator(_Base::insert(__position, std::move(__x))); }
00212 #endif
00213 
00214       template <typename _InputIterator>
00215         void
00216         insert(_InputIterator __first, _InputIterator __last)
00217         { _Base::insert(__first, __last); }
00218 
00219 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00220       void
00221       insert(initializer_list<value_type> __l)
00222       { _Base::insert(__l); }
00223 #endif
00224 
00225 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00226       iterator
00227       erase(const_iterator __position)
00228       { return iterator(_Base::erase(__position)); }
00229 #else
00230       void
00231       erase(iterator __position)
00232       { _Base::erase(__position); }
00233 #endif
00234 
00235       size_type
00236       erase(const key_type& __x)
00237       {
00238     iterator __victim = find(__x);
00239     if (__victim == end())
00240           return 0;
00241     else
00242         {
00243       _Base::erase(__victim);
00244       return 1;
00245         }
00246       }
00247 
00248 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00249       iterator
00250       erase(const_iterator __first, const_iterator __last)
00251       { return iterator(_Base::erase(__first, __last)); }
00252 #else
00253       void
00254       erase(iterator __first, iterator __last)
00255       { _Base::erase(__first, __last); }
00256 #endif
00257 
00258       void
00259       swap(set& __x)
00260       { _Base::swap(__x); }
00261 
00262       void
00263       clear() _GLIBCXX_NOEXCEPT
00264       { this->erase(begin(), end()); }
00265 
00266       // observers:
00267       using _Base::key_comp;
00268       using _Base::value_comp;
00269 
00270       // set operations:
00271       iterator
00272       find(const key_type& __x)
00273       { return iterator(_Base::find(__x)); }
00274 
00275       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00276       // 214. set::find() missing const overload
00277       const_iterator
00278       find(const key_type& __x) const
00279       { return const_iterator(_Base::find(__x)); }
00280 
00281       using _Base::count;
00282 
00283       iterator
00284       lower_bound(const key_type& __x)
00285       { return iterator(_Base::lower_bound(__x)); }
00286 
00287       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00288       // 214. set::find() missing const overload
00289       const_iterator
00290       lower_bound(const key_type& __x) const
00291       { return const_iterator(_Base::lower_bound(__x)); }
00292 
00293       iterator
00294       upper_bound(const key_type& __x)
00295       { return iterator(_Base::upper_bound(__x)); }
00296 
00297       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00298       // 214. set::find() missing const overload
00299       const_iterator
00300       upper_bound(const key_type& __x) const
00301       { return const_iterator(_Base::upper_bound(__x)); }
00302 
00303       std::pair<iterator,iterator>
00304       equal_range(const key_type& __x)
00305       {
00306     typedef typename _Base::iterator _Base_iterator;
00307     std::pair<_Base_iterator, _Base_iterator> __res =
00308         _Base::equal_range(__x);
00309     return std::make_pair(iterator(__res.first),
00310                   iterator(__res.second));
00311       }
00312 
00313       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00314       // 214. set::find() missing const overload
00315       std::pair<const_iterator,const_iterator>
00316       equal_range(const key_type& __x) const
00317       {
00318     typedef typename _Base::const_iterator _Base_iterator;
00319     std::pair<_Base_iterator, _Base_iterator> __res =
00320         _Base::equal_range(__x);
00321     return std::make_pair(const_iterator(__res.first),
00322                   const_iterator(__res.second));
00323       }
00324 
00325       _Base&
00326       _M_base() _GLIBCXX_NOEXCEPT       { return *this; }
00327 
00328       const _Base&
00329       _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
00330 
00331     };
00332 
00333   template<typename _Key, typename _Compare, typename _Allocator>
00334     inline bool
00335     operator==(const set<_Key, _Compare, _Allocator>& __lhs,
00336            const set<_Key, _Compare, _Allocator>& __rhs)
00337     { return __lhs._M_base() == __rhs._M_base(); }
00338 
00339   template<typename _Key, typename _Compare, typename _Allocator>
00340     inline bool
00341     operator!=(const set<_Key, _Compare, _Allocator>& __lhs,
00342            const set<_Key, _Compare, _Allocator>& __rhs)
00343     { return __lhs._M_base() != __rhs._M_base(); }
00344 
00345   template<typename _Key, typename _Compare, typename _Allocator>
00346     inline bool
00347     operator<(const set<_Key, _Compare, _Allocator>& __lhs,
00348           const set<_Key, _Compare, _Allocator>& __rhs)
00349     { return __lhs._M_base() < __rhs._M_base(); }
00350 
00351   template<typename _Key, typename _Compare, typename _Allocator>
00352     inline bool
00353     operator<=(const set<_Key, _Compare, _Allocator>& __lhs,
00354            const set<_Key, _Compare, _Allocator>& __rhs)
00355     { return __lhs._M_base() <= __rhs._M_base(); }
00356 
00357   template<typename _Key, typename _Compare, typename _Allocator>
00358     inline bool
00359     operator>=(const set<_Key, _Compare, _Allocator>& __lhs,
00360            const set<_Key, _Compare, _Allocator>& __rhs)
00361     { return __lhs._M_base() >= __rhs._M_base(); }
00362 
00363   template<typename _Key, typename _Compare, typename _Allocator>
00364     inline bool
00365     operator>(const set<_Key, _Compare, _Allocator>& __lhs,
00366           const set<_Key, _Compare, _Allocator>& __rhs)
00367     { return __lhs._M_base() > __rhs._M_base(); }
00368 
00369   template<typename _Key, typename _Compare, typename _Allocator>
00370     void
00371     swap(set<_Key, _Compare, _Allocator>& __x,
00372      set<_Key, _Compare, _Allocator>& __y)
00373     { return __x.swap(__y); }
00374 
00375 } // namespace __profile
00376 } // namespace std
00377 
00378 #endif