1 // Profiling list implementation -*- C++ -*-
3 // Copyright (C) 2009, 2010 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/list
26 * This file is a GNU profile extension to the Standard C++ Library.
29 #ifndef _GLIBCXX_PROFILE_LIST
30 #define _GLIBCXX_PROFILE_LIST 1
33 #include <profile/base.h>
34 #include <profile/iterator_tracker.h>
40 /** @brief List wrapper with performance instrumentation. */
41 template<typename _Tp, typename _Allocator = std::allocator<_Tp> >
43 : public _GLIBCXX_STD_D::list<_Tp, _Allocator>
45 typedef _GLIBCXX_STD_D::list<_Tp, _Allocator> _Base;
48 typedef typename _Base::reference reference;
49 typedef typename _Base::const_reference const_reference;
51 typedef __iterator_tracker<typename _Base::iterator, list>
53 typedef __iterator_tracker<typename _Base::const_iterator, list>
56 typedef typename _Base::size_type size_type;
57 typedef typename _Base::difference_type difference_type;
59 typedef _Tp value_type;
60 typedef _Allocator allocator_type;
61 typedef typename _Base::pointer pointer;
62 typedef typename _Base::const_pointer const_pointer;
63 typedef std::reverse_iterator<iterator> reverse_iterator;
64 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
66 // 23.2.2.1 construct/copy/destroy:
67 explicit list(const _Allocator& __a = _Allocator())
70 __profcxx_list_construct(this); // list2slist
71 __profcxx_list_construct2(this); // list2vector
74 explicit list(size_type __n, const _Tp& __value = _Tp(),
75 const _Allocator& __a = _Allocator())
76 : _Base(__n, __value, __a)
78 __profcxx_list_construct(this);
79 __profcxx_list_construct2(this);
82 template<class _InputIterator>
83 list(_InputIterator __first, _InputIterator __last,
84 const _Allocator& __a = _Allocator())
85 : _Base(__first, __last, __a)
87 __profcxx_list_construct(this);
88 __profcxx_list_construct2(this);
94 __profcxx_list_construct(this);
95 __profcxx_list_construct2(this);
98 list(const _Base& __x)
101 __profcxx_list_construct(this);
102 __profcxx_list_construct2(this);
105 #ifdef __GXX_EXPERIMENTAL_CXX0X__
107 : _Base(std::forward<list>(__x))
109 __profcxx_list_construct(this);
110 __profcxx_list_construct2(this);
113 list(initializer_list<value_type> __l,
114 const allocator_type& __a = allocator_type())
115 : _Base(__l, __a) { }
119 __profcxx_list_destruct(this);
120 __profcxx_list_destruct2(this);
124 operator=(const list& __x)
126 static_cast<_Base&>(*this) = __x;
130 #ifdef __GXX_EXPERIMENTAL_CXX0X__
132 operator=(list&& __x)
142 operator=(initializer_list<value_type> __l)
144 static_cast<_Base&>(*this) = __l;
149 assign(initializer_list<value_type> __l)
150 { _Base::assign(__l); }
153 template<class _InputIterator>
155 assign(_InputIterator __first, _InputIterator __last)
156 { _Base::assign(__first, __last); }
159 assign(size_type __n, const _Tp& __t)
160 { _Base::assign(__n, __t); }
162 using _Base::get_allocator;
167 { return iterator(_Base::begin(), this); }
171 { return const_iterator(_Base::begin(), this); }
176 __profcxx_list_rewind(this);
177 return iterator(_Base::end(), this);
183 __profcxx_list_rewind(this);
184 return const_iterator(_Base::end(), this);
190 __profcxx_list_rewind(this);
191 return reverse_iterator(end());
194 const_reverse_iterator
197 __profcxx_list_rewind(this);
198 return const_reverse_iterator(end());
203 { return reverse_iterator(begin()); }
205 const_reverse_iterator
207 { return const_reverse_iterator(begin()); }
209 #ifdef __GXX_EXPERIMENTAL_CXX0X__
212 { return const_iterator(_Base::begin(), this); }
216 { return const_iterator(_Base::end(), this); }
218 const_reverse_iterator
220 { return const_reverse_iterator(end()); }
222 const_reverse_iterator
224 { return const_reverse_iterator(begin()); }
227 // 23.2.2.2 capacity:
230 using _Base::max_size;
233 resize(size_type __sz, _Tp __c = _Tp())
234 { _Base::resize(__sz, __c); }
239 { return _Base::front(); }
243 { return _Base::front(); }
248 __profcxx_list_rewind(this);
249 return _Base::back();
255 __profcxx_list_rewind(this);
256 return _Base::back();
259 // 23.2.2.3 modifiers:
261 push_front(const value_type& __x)
263 __profcxx_list_invalid_operator(this);
264 __profcxx_list_operation(this);
265 _Base::push_front(__x);
268 #ifdef __GXX_EXPERIMENTAL_CXX0X__
269 using _Base::emplace_front;
275 __profcxx_list_operation(this);
279 using _Base::push_back;
281 #ifdef __GXX_EXPERIMENTAL_CXX0X__
282 using _Base::emplace_back;
288 iterator __victim = end();
291 __profcxx_list_rewind(this);
294 #ifdef __GXX_EXPERIMENTAL_CXX0X__
295 template<typename... _Args>
297 emplace(iterator __position, _Args&&... __args)
299 return iterator(_Base::emplace(__position.base(),
300 std::forward<_Args>(__args)...));
305 insert(iterator __position, const _Tp& __x)
307 _M_profile_insert(this, __position, size());
308 return iterator(_Base::insert(__position.base(), __x), this);
311 #ifdef __GXX_EXPERIMENTAL_CXX0X__
313 insert(iterator __position, _Tp&& __x)
315 _M_profile_insert(this, __position, size());
316 return iterator(_Base::emplace(__position.base(), std::move(__x)),
321 insert(iterator __position, initializer_list<value_type> __l)
323 _M_profile_insert(this, __position, size());
324 _Base::insert(__position.base(), __l);
329 insert(iterator __position, size_type __n, const _Tp& __x)
331 _M_profile_insert(this, __position, size());
332 _Base::insert(__position.base(), __n, __x);
335 template<class _InputIterator>
337 insert(iterator __position, _InputIterator __first,
338 _InputIterator __last)
340 _M_profile_insert(this, __position, size());
341 _Base::insert(__position.base(), __first, __last);
345 erase(iterator __position)
346 { return iterator(_Base::erase(__position.base()), this); }
349 erase(iterator __position, iterator __last)
351 // _GLIBCXX_RESOLVE_LIB_DEFECTS
352 // 151. can't currently clear() empty container
353 return iterator(_Base::erase(__position.base(), __last.base()), this);
358 { _Base::swap(__x); }
364 // 23.2.2.4 list operations:
366 #ifdef __GXX_EXPERIMENTAL_CXX0X__
367 splice(iterator __position, list&& __x)
369 splice(iterator __position, list& __x)
371 { this->splice(__position, _GLIBCXX_MOVE(__x), __x.begin(), __x.end()); }
373 #ifdef __GXX_EXPERIMENTAL_CXX0X__
375 splice(iterator __position, list& __x)
376 { this->splice(__position, std::move(__x)); }
379 #ifdef __GXX_EXPERIMENTAL_CXX0X__
381 splice(iterator __position, list& __x, iterator __i)
382 { this->splice(__position, std::move(__x), __i); }
386 #ifdef __GXX_EXPERIMENTAL_CXX0X__
387 splice(iterator __position, list&& __x, iterator __i)
389 splice(iterator __position, list& __x, iterator __i)
392 // We used to perform the splice_alloc check: not anymore, redundant
393 // after implementing the relevant bits of N1599.
395 // _GLIBCXX_RESOLVE_LIB_DEFECTS
396 _Base::splice(__position.base(), _GLIBCXX_MOVE(__x._M_base()),
401 #ifdef __GXX_EXPERIMENTAL_CXX0X__
402 splice(iterator __position, list&& __x, iterator __first,
405 splice(iterator __position, list& __x, iterator __first,
409 // We used to perform the splice_alloc check: not anymore, redundant
410 // after implementing the relevant bits of N1599.
412 _Base::splice(__position.base(), _GLIBCXX_MOVE(__x._M_base()),
413 __first.base(), __last.base());
416 #ifdef __GXX_EXPERIMENTAL_CXX0X__
418 splice(iterator __position, list& __x, iterator __first, iterator __last)
419 { this->splice(__position, std::move(__x), __first, __last); }
423 remove(const _Tp& __value)
425 for (iterator __x = begin(); __x != end(); )
434 template<class _Predicate>
436 remove_if(_Predicate __pred)
438 for (iterator __x = begin(); __x != end(); )
440 __profcxx_list_operation(this);
451 iterator __first = begin();
452 iterator __last = end();
453 if (__first == __last)
455 iterator __next = __first;
456 while (++__next != __last)
458 __profcxx_list_operation(this);
459 if (*__first == *__next)
467 template<class _BinaryPredicate>
469 unique(_BinaryPredicate __binary_pred)
471 iterator __first = begin();
472 iterator __last = end();
473 if (__first == __last)
475 iterator __next = __first;
476 while (++__next != __last)
478 __profcxx_list_operation(this);
479 if (__binary_pred(*__first, *__next))
488 #ifdef __GXX_EXPERIMENTAL_CXX0X__
494 // _GLIBCXX_RESOLVE_LIB_DEFECTS
495 // 300. list::merge() specification incomplete
497 { _Base::merge(_GLIBCXX_MOVE(__x._M_base())); }
500 #ifdef __GXX_EXPERIMENTAL_CXX0X__
503 { this->merge(std::move(__x)); }
506 template<class _Compare>
508 #ifdef __GXX_EXPERIMENTAL_CXX0X__
509 merge(list&& __x, _Compare __comp)
511 merge(list& __x, _Compare __comp)
514 // _GLIBCXX_RESOLVE_LIB_DEFECTS
515 // 300. list::merge() specification incomplete
517 { _Base::merge(_GLIBCXX_MOVE(__x._M_base()), __comp); }
520 #ifdef __GXX_EXPERIMENTAL_CXX0X__
521 template<typename _Compare>
523 merge(list& __x, _Compare __comp)
524 { this->merge(std::move(__x), __comp); }
528 sort() { _Base::sort(); }
530 template<typename _StrictWeakOrdering>
532 sort(_StrictWeakOrdering __pred) { _Base::sort(__pred); }
534 using _Base::reverse;
537 _M_base() { return *this; }
540 _M_base() const { return *this; }
542 inline void _M_profile_find() const
545 inline void _M_profile_iterate(int __rewind = 0) const
547 __profcxx_list_operation(this);
548 __profcxx_list_iterate(this);
550 __profcxx_list_rewind(this);
554 size_type _M_profile_insert(void* obj, iterator __pos, size_type __size)
556 size_type __shift = 0;
557 typename _Base::iterator __it = __pos.base();
558 for ( ; __it!=_Base::end(); __it++)
560 __profcxx_list_rewind(this);
561 __profcxx_list_operation(this);
562 __profcxx_list_insert(this, __shift, __size);
566 template<typename _Tp, typename _Alloc>
568 operator==(const list<_Tp, _Alloc>& __lhs,
569 const list<_Tp, _Alloc>& __rhs)
570 { return __lhs._M_base() == __rhs._M_base(); }
572 template<typename _Tp, typename _Alloc>
574 operator!=(const list<_Tp, _Alloc>& __lhs,
575 const list<_Tp, _Alloc>& __rhs)
576 { return __lhs._M_base() != __rhs._M_base(); }
578 template<typename _Tp, typename _Alloc>
580 operator<(const list<_Tp, _Alloc>& __lhs,
581 const list<_Tp, _Alloc>& __rhs)
582 { return __lhs._M_base() < __rhs._M_base(); }
584 template<typename _Tp, typename _Alloc>
586 operator<=(const list<_Tp, _Alloc>& __lhs,
587 const list<_Tp, _Alloc>& __rhs)
588 { return __lhs._M_base() <= __rhs._M_base(); }
590 template<typename _Tp, typename _Alloc>
592 operator>=(const list<_Tp, _Alloc>& __lhs,
593 const list<_Tp, _Alloc>& __rhs)
594 { return __lhs._M_base() >= __rhs._M_base(); }
596 template<typename _Tp, typename _Alloc>
598 operator>(const list<_Tp, _Alloc>& __lhs,
599 const list<_Tp, _Alloc>& __rhs)
600 { return __lhs._M_base() > __rhs._M_base(); }
602 template<typename _Tp, typename _Alloc>
604 swap(list<_Tp, _Alloc>& __lhs, list<_Tp, _Alloc>& __rhs)
605 { __lhs.swap(__rhs); }
607 } // namespace __profile