1 // Profiling set implementation -*- C++ -*-
3 // Copyright (C) 2009-2014 Free Software Foundation, Inc.
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)
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.
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.
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/>.
25 /** @file profile/set.h
26 * This file is a GNU profile extension to the Standard C++ Library.
29 #ifndef _GLIBCXX_PROFILE_SET_H
30 #define _GLIBCXX_PROFILE_SET_H 1
34 namespace std
_GLIBCXX_VISIBILITY(default)
38 /// Class std::set wrapper with performance instrumentation.
39 template<typename _Key
, typename _Compare
= std::less
<_Key
>,
40 typename _Allocator
= std::allocator
<_Key
> >
42 : public _GLIBCXX_STD_C::set
<_Key
,_Compare
,_Allocator
>
44 typedef _GLIBCXX_STD_C::set
<_Key
, _Compare
, _Allocator
> _Base
;
46 #if __cplusplus >= 201103L
47 typedef __gnu_cxx::__alloc_traits
<_Allocator
> _Alloc_traits
;
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
;
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
;
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
;
70 // 23.3.3.1 construct/copy/destroy:
71 explicit set(const _Compare
& __comp
= _Compare(),
72 const _Allocator
& __a
= _Allocator())
73 : _Base(__comp
, __a
) { }
75 #if __cplusplus >= 201103L
76 template<typename _InputIterator
,
77 typename
= std::_RequireInputIter
<_InputIterator
>>
79 template<typename _InputIterator
>
81 set(_InputIterator __first
, _InputIterator __last
,
82 const _Compare
& __comp
= _Compare(),
83 const _Allocator
& __a
= _Allocator())
84 : _Base(__first
, __last
, __comp
, __a
) { }
86 #if __cplusplus < 201103L
90 set(const set
&) = default;
93 set(initializer_list
<value_type
> __l
,
94 const _Compare
& __comp
= _Compare(),
95 const allocator_type
& __a
= allocator_type())
96 : _Base(__l
, __comp
, __a
) { }
99 set(const allocator_type
& __a
)
102 set(const set
& __x
, const allocator_type
& __a
)
103 : _Base(__x
, __a
) { }
105 set(set
&& __x
, const allocator_type
& __a
)
106 noexcept(is_nothrow_copy_constructible
<_Compare
>::value
107 && _Alloc_traits::_S_always_equal())
108 : _Base(std::move(__x
), __a
) { }
110 set(initializer_list
<value_type
> __l
, const allocator_type
& __a
)
111 : _Base(__l
, __a
) { }
113 template<typename _InputIterator
>
114 set(_InputIterator __first
, _InputIterator __last
,
115 const allocator_type
& __a
)
116 : _Base(__first
, __last
, __a
) { }
119 set(const _Base
& __x
)
122 ~set() _GLIBCXX_NOEXCEPT
{ }
124 #if __cplusplus < 201103L
126 operator=(const set
& __x
)
133 operator=(const set
&) = default;
136 operator=(set
&&) = default;
139 operator=(initializer_list
<value_type
> __l
)
146 using _Base::get_allocator
;
150 begin() _GLIBCXX_NOEXCEPT
151 { return iterator(_Base::begin()); }
154 begin() const _GLIBCXX_NOEXCEPT
155 { return const_iterator(_Base::begin()); }
158 end() _GLIBCXX_NOEXCEPT
159 { return iterator(_Base::end()); }
162 end() const _GLIBCXX_NOEXCEPT
163 { return const_iterator(_Base::end()); }
166 rbegin() _GLIBCXX_NOEXCEPT
167 { return reverse_iterator(end()); }
169 const_reverse_iterator
170 rbegin() const _GLIBCXX_NOEXCEPT
171 { return const_reverse_iterator(end()); }
174 rend() _GLIBCXX_NOEXCEPT
175 { return reverse_iterator(begin()); }
177 const_reverse_iterator
178 rend() const _GLIBCXX_NOEXCEPT
179 { return const_reverse_iterator(begin()); }
181 #if __cplusplus >= 201103L
183 cbegin() const noexcept
184 { return const_iterator(_Base::begin()); }
187 cend() const noexcept
188 { return const_iterator(_Base::end()); }
190 const_reverse_iterator
191 crbegin() const noexcept
192 { return const_reverse_iterator(end()); }
194 const_reverse_iterator
195 crend() const noexcept
196 { return const_reverse_iterator(begin()); }
202 using _Base::max_size
;
205 #if __cplusplus >= 201103L
206 template<typename
... _Args
>
207 std::pair
<iterator
, bool>
208 emplace(_Args
&&... __args
)
210 auto __res
= _Base::emplace(std::forward
<_Args
>(__args
)...);
211 return std::pair
<iterator
, bool>(iterator(__res
.first
),
215 template<typename
... _Args
>
217 emplace_hint(const_iterator __pos
, _Args
&&... __args
)
219 return iterator(_Base::emplace_hint(__pos
,
220 std::forward
<_Args
>(__args
)...));
224 std::pair
<iterator
, bool>
225 insert(const value_type
& __x
)
227 typedef typename
_Base::iterator _Base_iterator
;
228 std::pair
<_Base_iterator
, bool> __res
= _Base::insert(__x
);
229 return std::pair
<iterator
, bool>(iterator(__res
.first
),
233 #if __cplusplus >= 201103L
234 std::pair
<iterator
, bool>
235 insert(value_type
&& __x
)
237 typedef typename
_Base::iterator _Base_iterator
;
238 std::pair
<_Base_iterator
, bool> __res
239 = _Base::insert(std::move(__x
));
240 return std::pair
<iterator
, bool>(iterator(__res
.first
),
246 insert(const_iterator __position
, const value_type
& __x
)
247 { return iterator(_Base::insert(__position
, __x
)); }
249 #if __cplusplus >= 201103L
251 insert(const_iterator __position
, value_type
&& __x
)
252 { return iterator(_Base::insert(__position
, std::move(__x
))); }
255 #if __cplusplus >= 201103L
256 template<typename _InputIterator
,
257 typename
= std::_RequireInputIter
<_InputIterator
>>
259 template<typename _InputIterator
>
262 insert(_InputIterator __first
, _InputIterator __last
)
263 { _Base::insert(__first
, __last
); }
265 #if __cplusplus >= 201103L
267 insert(initializer_list
<value_type
> __l
)
268 { _Base::insert(__l
); }
271 #if __cplusplus >= 201103L
273 erase(const_iterator __position
)
274 { return iterator(_Base::erase(__position
)); }
277 erase(iterator __position
)
278 { _Base::erase(__position
); }
282 erase(const key_type
& __x
)
284 iterator __victim
= find(__x
);
285 if (__victim
== end())
289 _Base::erase(__victim
);
294 #if __cplusplus >= 201103L
296 erase(const_iterator __first
, const_iterator __last
)
297 { return iterator(_Base::erase(__first
, __last
)); }
300 erase(iterator __first
, iterator __last
)
301 { _Base::erase(__first
, __last
); }
306 #if __cplusplus >= 201103L
307 noexcept(_Alloc_traits::_S_nothrow_swap())
309 { _Base::swap(__x
); }
312 clear() _GLIBCXX_NOEXCEPT
313 { this->erase(begin(), end()); }
316 using _Base::key_comp
;
317 using _Base::value_comp
;
321 find(const key_type
& __x
)
322 { return iterator(_Base::find(__x
)); }
324 // _GLIBCXX_RESOLVE_LIB_DEFECTS
325 // 214. set::find() missing const overload
327 find(const key_type
& __x
) const
328 { return const_iterator(_Base::find(__x
)); }
333 lower_bound(const key_type
& __x
)
334 { return iterator(_Base::lower_bound(__x
)); }
336 // _GLIBCXX_RESOLVE_LIB_DEFECTS
337 // 214. set::find() missing const overload
339 lower_bound(const key_type
& __x
) const
340 { return const_iterator(_Base::lower_bound(__x
)); }
343 upper_bound(const key_type
& __x
)
344 { return iterator(_Base::upper_bound(__x
)); }
346 // _GLIBCXX_RESOLVE_LIB_DEFECTS
347 // 214. set::find() missing const overload
349 upper_bound(const key_type
& __x
) const
350 { return const_iterator(_Base::upper_bound(__x
)); }
352 std::pair
<iterator
,iterator
>
353 equal_range(const key_type
& __x
)
355 typedef typename
_Base::iterator _Base_iterator
;
356 std::pair
<_Base_iterator
, _Base_iterator
> __res
=
357 _Base::equal_range(__x
);
358 return std::make_pair(iterator(__res
.first
),
359 iterator(__res
.second
));
362 // _GLIBCXX_RESOLVE_LIB_DEFECTS
363 // 214. set::find() missing const overload
364 std::pair
<const_iterator
,const_iterator
>
365 equal_range(const key_type
& __x
) const
367 typedef typename
_Base::const_iterator _Base_iterator
;
368 std::pair
<_Base_iterator
, _Base_iterator
> __res
=
369 _Base::equal_range(__x
);
370 return std::make_pair(const_iterator(__res
.first
),
371 const_iterator(__res
.second
));
375 _M_base() _GLIBCXX_NOEXCEPT
{ return *this; }
378 _M_base() const _GLIBCXX_NOEXCEPT
{ return *this; }
382 template<typename _Key
, typename _Compare
, typename _Allocator
>
384 operator==(const set
<_Key
, _Compare
, _Allocator
>& __lhs
,
385 const set
<_Key
, _Compare
, _Allocator
>& __rhs
)
386 { return __lhs
._M_base() == __rhs
._M_base(); }
388 template<typename _Key
, typename _Compare
, typename _Allocator
>
390 operator!=(const set
<_Key
, _Compare
, _Allocator
>& __lhs
,
391 const set
<_Key
, _Compare
, _Allocator
>& __rhs
)
392 { return __lhs
._M_base() != __rhs
._M_base(); }
394 template<typename _Key
, typename _Compare
, typename _Allocator
>
396 operator<(const set
<_Key
, _Compare
, _Allocator
>& __lhs
,
397 const set
<_Key
, _Compare
, _Allocator
>& __rhs
)
398 { return __lhs
._M_base() < __rhs
._M_base(); }
400 template<typename _Key
, typename _Compare
, typename _Allocator
>
402 operator<=(const set
<_Key
, _Compare
, _Allocator
>& __lhs
,
403 const set
<_Key
, _Compare
, _Allocator
>& __rhs
)
404 { return __lhs
._M_base() <= __rhs
._M_base(); }
406 template<typename _Key
, typename _Compare
, typename _Allocator
>
408 operator>=(const set
<_Key
, _Compare
, _Allocator
>& __lhs
,
409 const set
<_Key
, _Compare
, _Allocator
>& __rhs
)
410 { return __lhs
._M_base() >= __rhs
._M_base(); }
412 template<typename _Key
, typename _Compare
, typename _Allocator
>
414 operator>(const set
<_Key
, _Compare
, _Allocator
>& __lhs
,
415 const set
<_Key
, _Compare
, _Allocator
>& __rhs
)
416 { return __lhs
._M_base() > __rhs
._M_base(); }
418 template<typename _Key
, typename _Compare
, typename _Allocator
>
420 swap(set
<_Key
, _Compare
, _Allocator
>& __x
,
421 set
<_Key
, _Compare
, _Allocator
>& __y
)
422 { return __x
.swap(__y
); }
424 } // namespace __profile