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