Home | History | Annotate | Download | only in profile
      1 // Profiling set implementation -*- C++ -*-
      2 
      3 // Copyright (C) 2009-2014 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 #if __cplusplus >= 201103L
     47       typedef __gnu_cxx::__alloc_traits<_Allocator> _Alloc_traits;
     48 #endif
     49 
     50     public:
     51       // types:
     52       typedef _Key				    key_type;
     53       typedef _Key				    value_type;
     54       typedef _Compare				    key_compare;
     55       typedef _Compare				    value_compare;
     56       typedef _Allocator			    allocator_type;
     57       typedef typename _Base::reference             reference;
     58       typedef typename _Base::const_reference       const_reference;
     59 
     60       typedef typename _Base::iterator               iterator;
     61       typedef typename _Base::const_iterator         const_iterator;
     62       typedef typename _Base::reverse_iterator       reverse_iterator;
     63       typedef typename _Base::const_reverse_iterator const_reverse_iterator;
     64 
     65       typedef typename _Base::size_type             size_type;
     66       typedef typename _Base::difference_type       difference_type;
     67       typedef typename _Base::pointer               pointer;
     68       typedef typename _Base::const_pointer         const_pointer;
     69 
     70       // 23.3.3.1 construct/copy/destroy:
     71 
     72       set()
     73       : _Base() { }
     74 
     75       explicit set(const _Compare& __comp,
     76 		   const _Allocator& __a = _Allocator())
     77       : _Base(__comp, __a) { }
     78 
     79 #if __cplusplus >= 201103L
     80       template<typename _InputIterator,
     81 	       typename = std::_RequireInputIter<_InputIterator>>
     82 #else
     83       template<typename _InputIterator>
     84 #endif
     85         set(_InputIterator __first, _InputIterator __last,
     86 	    const _Compare& __comp = _Compare(),
     87 	    const _Allocator& __a = _Allocator())
     88 	: _Base(__first, __last, __comp, __a) { }
     89 
     90 #if __cplusplus < 201103L
     91       set(const set& __x)
     92       : _Base(__x) { }
     93 #else
     94       set(const set&) = default;
     95       set(set&&) = default;
     96 
     97       set(initializer_list<value_type> __l,
     98 	  const _Compare& __comp = _Compare(),
     99 	  const allocator_type& __a = allocator_type())
    100       : _Base(__l, __comp, __a) { }
    101 
    102       explicit
    103       set(const allocator_type& __a)
    104 	: _Base(__a) { }
    105 
    106       set(const set& __x, const allocator_type& __a)
    107       : _Base(__x, __a) { }
    108 
    109       set(set&& __x, const allocator_type& __a)
    110       noexcept(is_nothrow_copy_constructible<_Compare>::value
    111 	       && _Alloc_traits::_S_always_equal())
    112       : _Base(std::move(__x), __a) { }
    113 
    114       set(initializer_list<value_type> __l, const allocator_type& __a)
    115       : _Base(__l, __a) { }
    116 
    117       template<typename _InputIterator>
    118         set(_InputIterator __first, _InputIterator __last,
    119 	    const allocator_type& __a)
    120 	  : _Base(__first, __last, __a) { }
    121 #endif
    122 
    123       set(const _Base& __x)
    124       : _Base(__x) { }
    125 
    126       ~set() _GLIBCXX_NOEXCEPT { }
    127 
    128 #if __cplusplus < 201103L
    129       set&
    130       operator=(const set& __x)
    131       {
    132 	_M_base() = __x;
    133 	return *this;
    134       }
    135 #else
    136       set&
    137       operator=(const set&) = default;
    138 
    139       set&
    140       operator=(set&&) = default;
    141 
    142       set&
    143       operator=(initializer_list<value_type> __l)
    144       {
    145 	_M_base() = __l;
    146 	return *this;
    147       }
    148 #endif
    149 
    150       using _Base::get_allocator;
    151 
    152       // iterators:
    153       iterator
    154       begin() _GLIBCXX_NOEXCEPT
    155       { return iterator(_Base::begin()); }
    156 
    157       const_iterator
    158       begin() const _GLIBCXX_NOEXCEPT
    159       { return const_iterator(_Base::begin()); }
    160 
    161       iterator
    162       end() _GLIBCXX_NOEXCEPT
    163       { return iterator(_Base::end()); }
    164 
    165       const_iterator
    166       end() const _GLIBCXX_NOEXCEPT
    167       { return const_iterator(_Base::end()); }
    168 
    169       reverse_iterator
    170       rbegin() _GLIBCXX_NOEXCEPT
    171       { return reverse_iterator(end()); }
    172 
    173       const_reverse_iterator
    174       rbegin() const _GLIBCXX_NOEXCEPT
    175       { return const_reverse_iterator(end()); }
    176 
    177       reverse_iterator
    178       rend() _GLIBCXX_NOEXCEPT
    179       { return reverse_iterator(begin()); }
    180 
    181       const_reverse_iterator
    182       rend() const _GLIBCXX_NOEXCEPT
    183       { return const_reverse_iterator(begin()); }
    184 
    185 #if __cplusplus >= 201103L
    186       const_iterator
    187       cbegin() const noexcept
    188       { return const_iterator(_Base::begin()); }
    189 
    190       const_iterator
    191       cend() const noexcept
    192       { return const_iterator(_Base::end()); }
    193 
    194       const_reverse_iterator
    195       crbegin() const noexcept
    196       { return const_reverse_iterator(end()); }
    197 
    198       const_reverse_iterator
    199       crend() const noexcept
    200       { return const_reverse_iterator(begin()); }
    201 #endif
    202 
    203       // capacity:
    204       using _Base::empty;
    205       using _Base::size;
    206       using _Base::max_size;
    207 
    208       // modifiers:
    209 #if __cplusplus >= 201103L
    210       template<typename... _Args>
    211 	std::pair<iterator, bool>
    212 	emplace(_Args&&... __args)
    213 	{
    214 	  auto __res = _Base::emplace(std::forward<_Args>(__args)...);
    215 	  return std::pair<iterator, bool>(iterator(__res.first),
    216 					   __res.second);
    217 	}
    218 
    219       template<typename... _Args>
    220 	iterator
    221 	emplace_hint(const_iterator __pos, _Args&&... __args)
    222 	{
    223 	  return iterator(_Base::emplace_hint(__pos,
    224 					      std::forward<_Args>(__args)...));
    225 	}
    226 #endif
    227 
    228       std::pair<iterator, bool>
    229       insert(const value_type& __x)
    230       {
    231 	typedef typename _Base::iterator _Base_iterator;
    232 	std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
    233 	return std::pair<iterator, bool>(iterator(__res.first),
    234 					 __res.second);
    235       }
    236 
    237 #if __cplusplus >= 201103L
    238       std::pair<iterator, bool>
    239       insert(value_type&& __x)
    240       {
    241 	typedef typename _Base::iterator _Base_iterator;
    242 	std::pair<_Base_iterator, bool> __res
    243 	  = _Base::insert(std::move(__x));
    244 	return std::pair<iterator, bool>(iterator(__res.first),
    245 					 __res.second);
    246       }
    247 #endif
    248 
    249       iterator
    250       insert(const_iterator __position, const value_type& __x)
    251       { return iterator(_Base::insert(__position, __x)); }
    252 
    253 #if __cplusplus >= 201103L
    254       iterator
    255       insert(const_iterator __position, value_type&& __x)
    256       { return iterator(_Base::insert(__position, std::move(__x))); }
    257 #endif
    258 
    259 #if __cplusplus >= 201103L
    260       template<typename _InputIterator,
    261 	       typename = std::_RequireInputIter<_InputIterator>>
    262 #else
    263       template<typename _InputIterator>
    264 #endif
    265         void
    266         insert(_InputIterator __first, _InputIterator __last)
    267         { _Base::insert(__first, __last); }
    268 
    269 #if __cplusplus >= 201103L
    270       void
    271       insert(initializer_list<value_type> __l)
    272       { _Base::insert(__l); }
    273 #endif
    274 
    275 #if __cplusplus >= 201103L
    276       iterator
    277       erase(const_iterator __position)
    278       { return iterator(_Base::erase(__position)); }
    279 #else
    280       void
    281       erase(iterator __position)
    282       { _Base::erase(__position); }
    283 #endif
    284 
    285       size_type
    286       erase(const key_type& __x)
    287       {
    288 	iterator __victim = find(__x);
    289 	if (__victim == end())
    290           return 0;
    291 	else
    292         {
    293 	  _Base::erase(__victim);
    294 	  return 1;
    295         }
    296       }
    297 
    298 #if __cplusplus >= 201103L
    299       iterator
    300       erase(const_iterator __first, const_iterator __last)
    301       { return iterator(_Base::erase(__first, __last)); }
    302 #else
    303       void
    304       erase(iterator __first, iterator __last)
    305       { _Base::erase(__first, __last); }
    306 #endif
    307 
    308       void
    309       swap(set& __x)
    310 #if __cplusplus >= 201103L
    311       noexcept(_Alloc_traits::_S_nothrow_swap())
    312 #endif
    313       { _Base::swap(__x); }
    314 
    315       void
    316       clear() _GLIBCXX_NOEXCEPT
    317       { this->erase(begin(), end()); }
    318 
    319       // observers:
    320       using _Base::key_comp;
    321       using _Base::value_comp;
    322 
    323       // set operations:
    324       iterator
    325       find(const key_type& __x)
    326       { return iterator(_Base::find(__x)); }
    327 
    328       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    329       // 214. set::find() missing const overload
    330       const_iterator
    331       find(const key_type& __x) const
    332       { return const_iterator(_Base::find(__x)); }
    333 
    334       using _Base::count;
    335 
    336       iterator
    337       lower_bound(const key_type& __x)
    338       { return iterator(_Base::lower_bound(__x)); }
    339 
    340       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    341       // 214. set::find() missing const overload
    342       const_iterator
    343       lower_bound(const key_type& __x) const
    344       { return const_iterator(_Base::lower_bound(__x)); }
    345 
    346       iterator
    347       upper_bound(const key_type& __x)
    348       { return iterator(_Base::upper_bound(__x)); }
    349 
    350       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    351       // 214. set::find() missing const overload
    352       const_iterator
    353       upper_bound(const key_type& __x) const
    354       { return const_iterator(_Base::upper_bound(__x)); }
    355 
    356       std::pair<iterator,iterator>
    357       equal_range(const key_type& __x)
    358       {
    359 	typedef typename _Base::iterator _Base_iterator;
    360 	std::pair<_Base_iterator, _Base_iterator> __res =
    361         _Base::equal_range(__x);
    362 	return std::make_pair(iterator(__res.first),
    363 			      iterator(__res.second));
    364       }
    365 
    366       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    367       // 214. set::find() missing const overload
    368       std::pair<const_iterator,const_iterator>
    369       equal_range(const key_type& __x) const
    370       {
    371 	typedef typename _Base::const_iterator _Base_iterator;
    372 	std::pair<_Base_iterator, _Base_iterator> __res =
    373         _Base::equal_range(__x);
    374 	return std::make_pair(const_iterator(__res.first),
    375 			      const_iterator(__res.second));
    376       }
    377 
    378       _Base&
    379       _M_base() _GLIBCXX_NOEXCEPT       { return *this; }
    380 
    381       const _Base&
    382       _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
    383 
    384     };
    385 
    386   template<typename _Key, typename _Compare, typename _Allocator>
    387     inline bool
    388     operator==(const set<_Key, _Compare, _Allocator>& __lhs,
    389 	       const set<_Key, _Compare, _Allocator>& __rhs)
    390     { return __lhs._M_base() == __rhs._M_base(); }
    391 
    392   template<typename _Key, typename _Compare, typename _Allocator>
    393     inline bool
    394     operator!=(const set<_Key, _Compare, _Allocator>& __lhs,
    395 	       const set<_Key, _Compare, _Allocator>& __rhs)
    396     { return __lhs._M_base() != __rhs._M_base(); }
    397 
    398   template<typename _Key, typename _Compare, typename _Allocator>
    399     inline bool
    400     operator<(const set<_Key, _Compare, _Allocator>& __lhs,
    401 	      const set<_Key, _Compare, _Allocator>& __rhs)
    402     { return __lhs._M_base() < __rhs._M_base(); }
    403 
    404   template<typename _Key, typename _Compare, typename _Allocator>
    405     inline bool
    406     operator<=(const set<_Key, _Compare, _Allocator>& __lhs,
    407 	       const set<_Key, _Compare, _Allocator>& __rhs)
    408     { return __lhs._M_base() <= __rhs._M_base(); }
    409 
    410   template<typename _Key, typename _Compare, typename _Allocator>
    411     inline bool
    412     operator>=(const set<_Key, _Compare, _Allocator>& __lhs,
    413 	       const set<_Key, _Compare, _Allocator>& __rhs)
    414     { return __lhs._M_base() >= __rhs._M_base(); }
    415 
    416   template<typename _Key, typename _Compare, typename _Allocator>
    417     inline bool
    418     operator>(const set<_Key, _Compare, _Allocator>& __lhs,
    419 	      const set<_Key, _Compare, _Allocator>& __rhs)
    420     { return __lhs._M_base() > __rhs._M_base(); }
    421 
    422   template<typename _Key, typename _Compare, typename _Allocator>
    423     void
    424     swap(set<_Key, _Compare, _Allocator>& __x,
    425 	 set<_Key, _Compare, _Allocator>& __y)
    426     { return __x.swap(__y); }
    427 
    428 } // namespace __profile
    429 } // namespace std
    430 
    431 #endif
    432