OSDN Git Service

2010-01-08 Paolo Carlini <paolo.carlini@oracle.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / profile / list
1 // Profiling list 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/list
26  *  This file is a GNU profile extension to the Standard C++ Library.
27  */
28
29 #ifndef _GLIBCXX_PROFILE_LIST
30 #define _GLIBCXX_PROFILE_LIST 1
31
32 #include <list>
33
34 namespace std
35 {
36 namespace __profile
37 {
38   /// Class std::list wrapper with performance instrumentation.
39   template<typename _Tp, typename _Allocator = std::allocator<_Tp> >
40     class list
41     : public _GLIBCXX_STD_D::list<_Tp, _Allocator>
42     {
43       typedef _GLIBCXX_STD_D::list<_Tp, _Allocator> _Base;
44
45     public:
46       typedef typename _Base::reference             reference;
47       typedef typename _Base::const_reference       const_reference;
48
49       typedef typename _Base::iterator              iterator;
50       typedef typename _Base::const_iterator        const_iterator;
51
52       typedef typename _Base::size_type             size_type;
53       typedef typename _Base::difference_type       difference_type;
54
55       typedef _Tp                                   value_type;
56       typedef _Allocator                            allocator_type;
57       typedef typename _Base::pointer               pointer;
58       typedef typename _Base::const_pointer         const_pointer;
59       typedef std::reverse_iterator<iterator>       reverse_iterator;
60       typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
61
62       // 23.2.2.1 construct/copy/destroy:
63       explicit list(const _Allocator& __a = _Allocator())
64       : _Base(__a) { }
65
66       explicit list(size_type __n, const _Tp& __value = _Tp(),
67                     const _Allocator& __a = _Allocator())
68       : _Base(__n, __value, __a) { }
69
70       template<class _InputIterator>
71       list(_InputIterator __first, _InputIterator __last,
72            const _Allocator& __a = _Allocator())
73       : _Base(__first, __last, __a)
74       { }
75
76       list(const list& __x)
77       : _Base(__x) { }
78
79       list(const _Base& __x)
80       : _Base(__x) { }
81
82 #ifdef __GXX_EXPERIMENTAL_CXX0X__
83       list(list&& __x)
84       : _Base(std::forward<list>(__x))
85       { }
86
87       list(initializer_list<value_type> __l,
88            const allocator_type& __a = allocator_type())
89         : _Base(__l, __a) { }
90 #endif
91
92       ~list() { }
93
94       list&
95       operator=(const list& __x)
96       {
97         static_cast<_Base&>(*this) = __x;
98         return *this;
99       }
100
101 #ifdef __GXX_EXPERIMENTAL_CXX0X__
102       list&
103       operator=(list&& __x)
104       {
105         if (this != &__x)
106           {
107             // NB: DR 675.
108             this->clear();
109             this->swap(__x);
110           }
111         return *this;
112       }
113
114       list&
115       operator=(initializer_list<value_type> __l)
116       {
117         static_cast<_Base&>(*this) = __l;
118         return *this;
119       }
120
121       void
122       assign(initializer_list<value_type> __l)
123       { _Base::assign(__l); }
124 #endif
125
126       template<class _InputIterator>
127         void
128         assign(_InputIterator __first, _InputIterator __last)
129         { _Base::assign(__first, __last); }
130
131       void
132       assign(size_type __n, const _Tp& __t)
133       { _Base::assign(__n, __t); }
134
135       using _Base::get_allocator;
136
137       // iterators:
138       iterator
139       begin()
140       { return iterator(_Base::begin()); }
141
142       const_iterator
143       begin() const
144       { return const_iterator(_Base::begin()); }
145
146       iterator
147       end()
148       { return iterator(_Base::end()); }
149
150       const_iterator
151       end() const
152       { return const_iterator(_Base::end()); }
153
154       reverse_iterator
155       rbegin()
156       { return reverse_iterator(end()); }
157
158       const_reverse_iterator
159       rbegin() const
160       { return const_reverse_iterator(end()); }
161
162       reverse_iterator
163       rend()
164       { return reverse_iterator(begin()); }
165
166       const_reverse_iterator
167       rend() const
168       { return const_reverse_iterator(begin()); }
169
170 #ifdef __GXX_EXPERIMENTAL_CXX0X__
171       const_iterator
172       cbegin() const
173       { return const_iterator(_Base::begin()); }
174
175       const_iterator
176       cend() const
177       { return const_iterator(_Base::end()); }
178
179       const_reverse_iterator
180       crbegin() const
181       { return const_reverse_iterator(end()); }
182
183       const_reverse_iterator
184       crend() const
185       { return const_reverse_iterator(begin()); }
186 #endif
187
188       // 23.2.2.2 capacity:
189       using _Base::empty;
190       using _Base::size;
191       using _Base::max_size;
192
193       void
194       resize(size_type __sz, _Tp __c = _Tp())
195       { _Base::resize(__sz, __c); }
196
197       // element access:
198       reference
199       front()
200       { return _Base::front(); }
201
202       const_reference
203       front() const
204       { return _Base::front(); }
205
206       reference
207       back()
208       { return _Base::back(); }
209
210       const_reference
211       back() const
212       { return _Base::back(); }
213
214       // 23.2.2.3 modifiers:
215       using _Base::push_front;
216
217 #ifdef __GXX_EXPERIMENTAL_CXX0X__
218       using _Base::emplace_front;
219 #endif
220
221       void
222       pop_front()
223       {
224         iterator __victim = begin();
225         _Base::pop_front();
226       }
227
228       using _Base::push_back;
229
230 #ifdef __GXX_EXPERIMENTAL_CXX0X__
231       using _Base::emplace_back;
232 #endif
233
234       void
235       pop_back()
236       {
237         iterator __victim = end();
238         --__victim;
239         _Base::pop_back();
240       }
241
242 #ifdef __GXX_EXPERIMENTAL_CXX0X__
243       template<typename... _Args>
244         iterator
245         emplace(iterator __position, _Args&&... __args)
246         {
247           return iterator(_Base::emplace(__position,
248                                         std::forward<_Args>(__args)...));
249         }
250 #endif
251
252       iterator
253       insert(iterator __position, const _Tp& __x)
254       { return iterator(_Base::insert(__position, __x)); }
255
256 #ifdef __GXX_EXPERIMENTAL_CXX0X__
257       iterator
258       insert(iterator __position, _Tp&& __x)
259       { return emplace(__position, std::move(__x)); }
260
261       void
262       insert(iterator __p, initializer_list<value_type> __l)
263       { _Base::insert(__p, __l); }
264 #endif
265
266       void
267       insert(iterator __position, size_type __n, const _Tp& __x)
268       { _Base::insert(__position, __n, __x); }
269
270       template<class _InputIterator>
271         void
272         insert(iterator __position, _InputIterator __first,
273                _InputIterator __last)
274         { _Base::insert(__position, __first, __last); }
275
276       iterator
277       erase(iterator __position)
278       { return iterator(_Base::erase(__position)); }
279
280       iterator
281       erase(iterator __position, iterator __last)
282       {
283         // _GLIBCXX_RESOLVE_LIB_DEFECTS
284         // 151. can't currently clear() empty container
285         return iterator(_Base::erase(__position, __last));
286       }
287
288       void
289       swap(list& __x)
290       { _Base::swap(__x); }
291
292       void
293       clear()
294       { _Base::clear(); }
295
296       // 23.2.2.4 list operations:
297       void
298 #ifdef __GXX_EXPERIMENTAL_CXX0X__
299       splice(iterator __position, list&& __x)
300 #else
301       splice(iterator __position, list& __x)
302 #endif
303       { this->splice(__position, _GLIBCXX_MOVE(__x), __x.begin(), __x.end()); }
304
305 #ifdef __GXX_EXPERIMENTAL_CXX0X__
306       void
307       splice(iterator __position, list& __x)
308       { this->splice(__position, std::move(__x)); }
309 #endif
310
311       void
312 #ifdef __GXX_EXPERIMENTAL_CXX0X__
313       splice(iterator __position, list&& __x, iterator __i)
314 #else
315       splice(iterator __position, list& __x, iterator __i)
316 #endif
317       {
318         // We used to perform the splice_alloc check:  not anymore, redundant
319         // after implementing the relevant bits of N1599.
320
321         // _GLIBCXX_RESOLVE_LIB_DEFECTS
322         _Base::splice(__position, _GLIBCXX_MOVE(__x._M_base()),
323                       __i);
324       }
325
326 #ifdef __GXX_EXPERIMENTAL_CXX0X__
327       void
328       splice(iterator __position, list& __x, iterator __i)
329       { this->splice(__position, std::move(__x), __i); }
330 #endif
331
332       void
333 #ifdef __GXX_EXPERIMENTAL_CXX0X__
334       splice(iterator __position, list&& __x, iterator __first,
335              iterator __last)
336 #else
337       splice(iterator __position, list& __x, iterator __first,
338              iterator __last)
339 #endif
340       {
341         // We used to perform the splice_alloc check:  not anymore, redundant
342         // after implementing the relevant bits of N1599.
343
344         _Base::splice(__position, _GLIBCXX_MOVE(__x._M_base()),
345                       __first, __last);
346       }
347
348 #ifdef __GXX_EXPERIMENTAL_CXX0X__
349       void
350       splice(iterator __position, list& __x, iterator __first, iterator __last)
351       { this->splice(__position, std::move(__x), __first, __last); }
352 #endif
353
354       void
355       remove(const _Tp& __value)
356       {
357         for (iterator __x = begin(); __x != _Base::end(); )
358           {
359             if (*__x == __value)
360               __x = erase(__x);
361             else
362               ++__x;
363           }
364       }
365
366       template<class _Predicate>
367         void
368         remove_if(_Predicate __pred)
369         {
370           for (iterator __x = begin(); __x != _Base::end(); )
371             {
372               if (__pred(*__x))
373                 __x = erase(__x);
374               else
375                 ++__x;
376             }
377         }
378
379       void
380       unique()
381       {
382         iterator __first = begin();
383         iterator __last = end();
384         if (__first == __last)
385           return;
386         iterator __next = __first;
387         while (++__next != __last)
388           {
389             if (*__first == *__next)
390               erase(__next);
391             else
392               __first = __next;
393             __next = __first;
394           }
395       }
396
397       template<class _BinaryPredicate>
398         void
399         unique(_BinaryPredicate __binary_pred)
400         {
401           iterator __first = begin();
402           iterator __last = end();
403           if (__first == __last)
404             return;
405           iterator __next = __first;
406           while (++__next != __last)
407             {
408               if (__binary_pred(*__first, *__next))
409                 erase(__next);
410               else
411                 __first = __next;
412               __next = __first;
413             }
414         }
415
416       void
417 #ifdef __GXX_EXPERIMENTAL_CXX0X__
418       merge(list&& __x)
419 #else
420       merge(list& __x)
421 #endif
422       {
423         // _GLIBCXX_RESOLVE_LIB_DEFECTS
424         // 300. list::merge() specification incomplete
425         if (this != &__x)
426           _Base::merge(_GLIBCXX_MOVE(__x._M_base()));
427       }
428
429 #ifdef __GXX_EXPERIMENTAL_CXX0X__
430       void
431       merge(list& __x)
432       { this->merge(std::move(__x)); }
433 #endif
434  
435       template<class _Compare>
436         void
437 #ifdef __GXX_EXPERIMENTAL_CXX0X__
438         merge(list&& __x, _Compare __comp)
439 #else
440         merge(list& __x, _Compare __comp)
441 #endif
442         {
443           // _GLIBCXX_RESOLVE_LIB_DEFECTS
444           // 300. list::merge() specification incomplete
445           if (this != &__x)
446             _Base::merge(_GLIBCXX_MOVE(__x._M_base()), __comp);
447         }
448
449 #ifdef __GXX_EXPERIMENTAL_CXX0X__
450       template<typename _Compare>
451         void
452         merge(list& __x, _Compare __comp)
453         { this->merge(std::move(__x), __comp); }
454 #endif
455
456       void
457       sort() { _Base::sort(); }
458
459       template<typename _StrictWeakOrdering>
460         void
461         sort(_StrictWeakOrdering __pred) { _Base::sort(__pred); }
462
463       using _Base::reverse;
464
465       _Base&
466       _M_base()       { return *this; }
467
468       const _Base&
469       _M_base() const { return *this; }
470
471     };
472
473   template<typename _Tp, typename _Alloc>
474     inline bool
475     operator==(const list<_Tp, _Alloc>& __lhs,
476                const list<_Tp, _Alloc>& __rhs)
477     { return __lhs._M_base() == __rhs._M_base(); }
478
479   template<typename _Tp, typename _Alloc>
480     inline bool
481     operator!=(const list<_Tp, _Alloc>& __lhs,
482                const list<_Tp, _Alloc>& __rhs)
483     { return __lhs._M_base() != __rhs._M_base(); }
484
485   template<typename _Tp, typename _Alloc>
486     inline bool
487     operator<(const list<_Tp, _Alloc>& __lhs,
488               const list<_Tp, _Alloc>& __rhs)
489     { return __lhs._M_base() < __rhs._M_base(); }
490
491   template<typename _Tp, typename _Alloc>
492     inline bool
493     operator<=(const list<_Tp, _Alloc>& __lhs,
494                const list<_Tp, _Alloc>& __rhs)
495     { return __lhs._M_base() <= __rhs._M_base(); }
496
497   template<typename _Tp, typename _Alloc>
498     inline bool
499     operator>=(const list<_Tp, _Alloc>& __lhs,
500                const list<_Tp, _Alloc>& __rhs)
501     { return __lhs._M_base() >= __rhs._M_base(); }
502
503   template<typename _Tp, typename _Alloc>
504     inline bool
505     operator>(const list<_Tp, _Alloc>& __lhs,
506               const list<_Tp, _Alloc>& __rhs)
507     { return __lhs._M_base() > __rhs._M_base(); }
508
509   template<typename _Tp, typename _Alloc>
510     inline void
511     swap(list<_Tp, _Alloc>& __lhs, list<_Tp, _Alloc>& __rhs)
512     { __lhs.swap(__rhs); }
513
514 } // namespace __profile
515 } // namespace std
516
517 #endif