Home | History | Annotate | Download | only in profile
      1 // Profiling set implementation -*- C++ -*-
      2 
      3 // Copyright (C) 2009, 2010 Free Software Foundation, Inc.
      4 //
      5 // This file is part of the GNU ISO C++ Library.  This library is free
      6 // software; you can redistribute it and/or modify it under the
      7 // terms of the GNU General Public License as published by the
      8 // Free Software Foundation; either version 3, or (at your option)
      9 // any later version.
     10 
     11 // This library is distributed in the hope that it will be useful,
     12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14 // GNU General Public License for more details.
     15 
     16 // Under Section 7 of GPL version 3, you are granted additional
     17 // permissions described in the GCC Runtime Library Exception, version
     18 // 3.1, as published by the Free Software Foundation.
     19 
     20 // You should have received a copy of the GNU General Public License and
     21 // a copy of the GCC Runtime Library Exception along with this program;
     22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
     23 // <http://www.gnu.org/licenses/>.
     24 
     25 /** @file profile/set.h
     26  *  This file is a GNU profile extension to the Standard C++ Library.
     27  */
     28 
     29 #ifndef _GLIBCXX_PROFILE_SET_H
     30 #define _GLIBCXX_PROFILE_SET_H 1
     31 
     32 #include <utility>
     33 
     34 namespace std _GLIBCXX_VISIBILITY(default)
     35 {
     36 namespace __profile
     37 {
     38   /// Class std::set wrapper with performance instrumentation.
     39   template<typename _Key, typename _Compare = std::less<_Key>,
     40 	   typename _Allocator = std::allocator<_Key> >
     41     class set
     42     : public _GLIBCXX_STD_C::set<_Key,_Compare,_Allocator>
     43     {
     44       typedef _GLIBCXX_STD_C::set<_Key, _Compare, _Allocator> _Base;
     45 
     46     public:
     47       // types:
     48       typedef _Key				    key_type;
     49       typedef _Key				    value_type;
     50       typedef _Compare				    key_compare;
     51       typedef _Compare				    value_compare;
     52       typedef _Allocator			    allocator_type;
     53       typedef typename _Base::reference             reference;
     54       typedef typename _Base::const_reference       const_reference;
     55 
     56       typedef typename _Base::iterator               iterator;
     57       typedef typename _Base::const_iterator         const_iterator;
     58       typedef typename _Base::reverse_iterator       reverse_iterator;
     59       typedef typename _Base::const_reverse_iterator const_reverse_iterator;
     60 
     61       typedef typename _Base::size_type             size_type;
     62       typedef typename _Base::difference_type       difference_type;
     63       typedef typename _Base::pointer               pointer;
     64       typedef typename _Base::const_pointer         const_pointer;
     65 
     66       // 23.3.3.1 construct/copy/destroy:
     67       explicit set(const _Compare& __comp = _Compare(),
     68 		   const _Allocator& __a = _Allocator())
     69       : _Base(__comp, __a) { }
     70 
     71       template<typename _InputIterator>
     72         set(_InputIterator __first, _InputIterator __last,
     73 	    const _Compare& __comp = _Compare(),
     74 	    const _Allocator& __a = _Allocator())
     75 	: _Base(__first, __last, __comp, __a) { }
     76 
     77       set(const set& __x)
     78       : _Base(__x) { }
     79 
     80       set(const _Base& __x)
     81       : _Base(__x) { }
     82 
     83 #ifdef __GXX_EXPERIMENTAL_CXX0X__
     84       set(set&& __x)
     85       : _Base(std::move(__x))
     86       { }
     87 
     88       set(initializer_list<value_type> __l,
     89 	  const _Compare& __comp = _Compare(),
     90 	  const allocator_type& __a = allocator_type())
     91       : _Base(__l, __comp, __a) { }
     92 #endif
     93 
     94       ~set() { }
     95 
     96       set&
     97       operator=(const set& __x)
     98       {
     99 	*static_cast<_Base*>(this) = __x;
    100 	return *this;
    101       }
    102 
    103 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    104       set&
    105       operator=(set&& __x)
    106       {
    107 	// NB: DR 1204.
    108 	// NB: DR 675.
    109 	this->clear();
    110 	this->swap(__x);
    111 	return *this;
    112       }
    113 
    114       set&
    115       operator=(initializer_list<value_type> __l)
    116       {
    117 	this->clear();
    118 	this->insert(__l);
    119 	return *this;
    120       }
    121 #endif
    122 
    123       using _Base::get_allocator;
    124 
    125       // iterators:
    126       iterator
    127       begin()
    128       { return iterator(_Base::begin()); }
    129 
    130       const_iterator
    131       begin() const
    132       { return const_iterator(_Base::begin()); }
    133 
    134       iterator
    135       end()
    136       { return iterator(_Base::end()); }
    137 
    138       const_iterator
    139       end() const
    140       { return const_iterator(_Base::end()); }
    141 
    142       reverse_iterator
    143       rbegin()
    144       { return reverse_iterator(end()); }
    145 
    146       const_reverse_iterator
    147       rbegin() const
    148       { return const_reverse_iterator(end()); }
    149 
    150       reverse_iterator
    151       rend()
    152       { return reverse_iterator(begin()); }
    153 
    154       const_reverse_iterator
    155       rend() const
    156       { return const_reverse_iterator(begin()); }
    157 
    158 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    159       const_iterator
    160       cbegin() const
    161       { return const_iterator(_Base::begin()); }
    162 
    163       const_iterator
    164       cend() const
    165       { return const_iterator(_Base::end()); }
    166 
    167       const_reverse_iterator
    168       crbegin() const
    169       { return const_reverse_iterator(end()); }
    170 
    171       const_reverse_iterator
    172       crend() const
    173       { return const_reverse_iterator(begin()); }
    174 #endif
    175 
    176       // capacity:
    177       using _Base::empty;
    178       using _Base::size;
    179       using _Base::max_size;
    180 
    181       // modifiers:
    182       std::pair<iterator, bool>
    183       insert(const value_type& __x)
    184       {
    185 	typedef typename _Base::iterator _Base_iterator;
    186 	std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
    187 	return std::pair<iterator, bool>(iterator(__res.first),
    188 					 __res.second);
    189       }
    190 
    191 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    192       std::pair<iterator, bool>
    193       insert(value_type&& __x)
    194       {
    195 	typedef typename _Base::iterator _Base_iterator;
    196 	std::pair<_Base_iterator, bool> __res
    197 	  = _Base::insert(std::move(__x));
    198 	return std::pair<iterator, bool>(iterator(__res.first),
    199 					 __res.second);
    200       }
    201 #endif
    202 
    203       iterator
    204       insert(const_iterator __position, const value_type& __x)
    205       { return iterator(_Base::insert(__position, __x)); }
    206 
    207 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    208       iterator
    209       insert(const_iterator __position, value_type&& __x)
    210       { return iterator(_Base::insert(__position, std::move(__x))); }
    211 #endif
    212 
    213       template <typename _InputIterator>
    214         void
    215         insert(_InputIterator __first, _InputIterator __last)
    216         { _Base::insert(__first, __last); }
    217 
    218 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    219       void
    220       insert(initializer_list<value_type> __l)
    221       { _Base::insert(__l); }
    222 #endif
    223 
    224 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    225       iterator
    226       erase(const_iterator __position)
    227       { return iterator(_Base::erase(__position)); }
    228 #else
    229       void
    230       erase(iterator __position)
    231       { _Base::erase(__position); }
    232 #endif
    233 
    234       size_type
    235       erase(const key_type& __x)
    236       {
    237 	iterator __victim = find(__x);
    238 	if (__victim == end())
    239           return 0;
    240 	else
    241         {
    242 	  _Base::erase(__victim);
    243 	  return 1;
    244         }
    245       }
    246 
    247 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    248       iterator
    249       erase(const_iterator __first, const_iterator __last)
    250       { return iterator(_Base::erase(__first, __last)); }
    251 #else
    252       void
    253       erase(iterator __first, iterator __last)
    254       { _Base::erase(__first, __last); }
    255 #endif
    256 
    257       void
    258       swap(set& __x)
    259       { _Base::swap(__x); }
    260 
    261       void
    262       clear()
    263       { this->erase(begin(), end()); }
    264 
    265       // observers:
    266       using _Base::key_comp;
    267       using _Base::value_comp;
    268 
    269       // set operations:
    270       iterator
    271       find(const key_type& __x)
    272       { return iterator(_Base::find(__x)); }
    273 
    274       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    275       // 214. set::find() missing const overload
    276       const_iterator
    277       find(const key_type& __x) const
    278       { return const_iterator(_Base::find(__x)); }
    279 
    280       using _Base::count;
    281 
    282       iterator
    283       lower_bound(const key_type& __x)
    284       { return iterator(_Base::lower_bound(__x)); }
    285 
    286       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    287       // 214. set::find() missing const overload
    288       const_iterator
    289       lower_bound(const key_type& __x) const
    290       { return const_iterator(_Base::lower_bound(__x)); }
    291 
    292       iterator
    293       upper_bound(const key_type& __x)
    294       { return iterator(_Base::upper_bound(__x)); }
    295 
    296       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    297       // 214. set::find() missing const overload
    298       const_iterator
    299       upper_bound(const key_type& __x) const
    300       { return const_iterator(_Base::upper_bound(__x)); }
    301 
    302       std::pair<iterator,iterator>
    303       equal_range(const key_type& __x)
    304       {
    305 	typedef typename _Base::iterator _Base_iterator;
    306 	std::pair<_Base_iterator, _Base_iterator> __res =
    307         _Base::equal_range(__x);
    308 	return std::make_pair(iterator(__res.first),
    309 			      iterator(__res.second));
    310       }
    311 
    312       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    313       // 214. set::find() missing const overload
    314       std::pair<const_iterator,const_iterator>
    315       equal_range(const key_type& __x) const
    316       {
    317 	typedef typename _Base::const_iterator _Base_iterator;
    318 	std::pair<_Base_iterator, _Base_iterator> __res =
    319         _Base::equal_range(__x);
    320 	return std::make_pair(const_iterator(__res.first),
    321 			      const_iterator(__res.second));
    322       }
    323 
    324       _Base&
    325       _M_base() { return *this; }
    326 
    327       const _Base&
    328       _M_base() const { return *this; }
    329 
    330     };
    331 
    332   template<typename _Key, typename _Compare, typename _Allocator>
    333     inline bool
    334     operator==(const set<_Key, _Compare, _Allocator>& __lhs,
    335 	       const set<_Key, _Compare, _Allocator>& __rhs)
    336     { return __lhs._M_base() == __rhs._M_base(); }
    337 
    338   template<typename _Key, typename _Compare, typename _Allocator>
    339     inline bool
    340     operator!=(const set<_Key, _Compare, _Allocator>& __lhs,
    341 	       const set<_Key, _Compare, _Allocator>& __rhs)
    342     { return __lhs._M_base() != __rhs._M_base(); }
    343 
    344   template<typename _Key, typename _Compare, typename _Allocator>
    345     inline bool
    346     operator<(const set<_Key, _Compare, _Allocator>& __lhs,
    347 	      const set<_Key, _Compare, _Allocator>& __rhs)
    348     { return __lhs._M_base() < __rhs._M_base(); }
    349 
    350   template<typename _Key, typename _Compare, typename _Allocator>
    351     inline bool
    352     operator<=(const set<_Key, _Compare, _Allocator>& __lhs,
    353 	       const set<_Key, _Compare, _Allocator>& __rhs)
    354     { return __lhs._M_base() <= __rhs._M_base(); }
    355 
    356   template<typename _Key, typename _Compare, typename _Allocator>
    357     inline bool
    358     operator>=(const set<_Key, _Compare, _Allocator>& __lhs,
    359 	       const set<_Key, _Compare, _Allocator>& __rhs)
    360     { return __lhs._M_base() >= __rhs._M_base(); }
    361 
    362   template<typename _Key, typename _Compare, typename _Allocator>
    363     inline bool
    364     operator>(const set<_Key, _Compare, _Allocator>& __lhs,
    365 	      const set<_Key, _Compare, _Allocator>& __rhs)
    366     { return __lhs._M_base() > __rhs._M_base(); }
    367 
    368   template<typename _Key, typename _Compare, typename _Allocator>
    369     void
    370     swap(set<_Key, _Compare, _Allocator>& __x,
    371 	 set<_Key, _Compare, _Allocator>& __y)
    372     { return __x.swap(__y); }
    373 
    374 } // namespace __profile
    375 } // namespace std
    376 
    377 #endif
    378