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