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
38 /// Class std::list wrapper with performance instrumentation.
39 template<typename _Tp, typename _Allocator = std::allocator<_Tp> >
41 : public _GLIBCXX_STD_D::list<_Tp, _Allocator>
43 typedef _GLIBCXX_STD_D::list<_Tp, _Allocator> _Base;
46 typedef typename _Base::reference reference;
47 typedef typename _Base::const_reference const_reference;
49 typedef typename _Base::iterator iterator;
50 typedef typename _Base::const_iterator const_iterator;
52 typedef typename _Base::size_type size_type;
53 typedef typename _Base::difference_type difference_type;
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;
62 // 23.2.2.1 construct/copy/destroy:
63 explicit list(const _Allocator& __a = _Allocator())
66 explicit list(size_type __n, const _Tp& __value = _Tp(),
67 const _Allocator& __a = _Allocator())
68 : _Base(__n, __value, __a) { }
70 template<class _InputIterator>
71 list(_InputIterator __first, _InputIterator __last,
72 const _Allocator& __a = _Allocator())
73 : _Base(__first, __last, __a)
79 list(const _Base& __x)
82 #ifdef __GXX_EXPERIMENTAL_CXX0X__
84 : _Base(std::forward<list>(__x))
87 list(initializer_list<value_type> __l,
88 const allocator_type& __a = allocator_type())
95 operator=(const list& __x)
97 static_cast<_Base&>(*this) = __x;
101 #ifdef __GXX_EXPERIMENTAL_CXX0X__
103 operator=(list&& __x)
113 operator=(initializer_list<value_type> __l)
115 static_cast<_Base&>(*this) = __l;
120 assign(initializer_list<value_type> __l)
121 { _Base::assign(__l); }
124 template<class _InputIterator>
126 assign(_InputIterator __first, _InputIterator __last)
127 { _Base::assign(__first, __last); }
130 assign(size_type __n, const _Tp& __t)
131 { _Base::assign(__n, __t); }
133 using _Base::get_allocator;
138 { return iterator(_Base::begin()); }
142 { return const_iterator(_Base::begin()); }
146 { return iterator(_Base::end()); }
150 { return const_iterator(_Base::end()); }
154 { return reverse_iterator(end()); }
156 const_reverse_iterator
158 { return const_reverse_iterator(end()); }
162 { return reverse_iterator(begin()); }
164 const_reverse_iterator
166 { return const_reverse_iterator(begin()); }
168 #ifdef __GXX_EXPERIMENTAL_CXX0X__
171 { return const_iterator(_Base::begin()); }
175 { return const_iterator(_Base::end()); }
177 const_reverse_iterator
179 { return const_reverse_iterator(end()); }
181 const_reverse_iterator
183 { return const_reverse_iterator(begin()); }
186 // 23.2.2.2 capacity:
189 using _Base::max_size;
192 resize(size_type __sz, _Tp __c = _Tp())
193 { _Base::resize(__sz, __c); }
198 { return _Base::front(); }
202 { return _Base::front(); }
206 { return _Base::back(); }
210 { return _Base::back(); }
212 // 23.2.2.3 modifiers:
213 using _Base::push_front;
215 #ifdef __GXX_EXPERIMENTAL_CXX0X__
216 using _Base::emplace_front;
222 iterator __victim = begin();
226 using _Base::push_back;
228 #ifdef __GXX_EXPERIMENTAL_CXX0X__
229 using _Base::emplace_back;
235 iterator __victim = end();
240 #ifdef __GXX_EXPERIMENTAL_CXX0X__
241 template<typename... _Args>
243 emplace(iterator __position, _Args&&... __args)
245 return iterator(_Base::emplace(__position,
246 std::forward<_Args>(__args)...));
251 insert(iterator __position, const _Tp& __x)
252 { return iterator(_Base::insert(__position, __x)); }
254 #ifdef __GXX_EXPERIMENTAL_CXX0X__
256 insert(iterator __position, _Tp&& __x)
257 { return emplace(__position, std::move(__x)); }
260 insert(iterator __p, initializer_list<value_type> __l)
261 { _Base::insert(__p, __l); }
265 insert(iterator __position, size_type __n, const _Tp& __x)
266 { _Base::insert(__position, __n, __x); }
268 template<class _InputIterator>
270 insert(iterator __position, _InputIterator __first,
271 _InputIterator __last)
272 { _Base::insert(__position, __first, __last); }
275 erase(iterator __position)
276 { return iterator(_Base::erase(__position)); }
279 erase(iterator __position, iterator __last)
281 // _GLIBCXX_RESOLVE_LIB_DEFECTS
282 // 151. can't currently clear() empty container
283 return iterator(_Base::erase(__position, __last));
288 { _Base::swap(__x); }
294 // 23.2.2.4 list operations:
296 #ifdef __GXX_EXPERIMENTAL_CXX0X__
297 splice(iterator __position, list&& __x)
299 splice(iterator __position, list& __x)
301 { this->splice(__position, _GLIBCXX_MOVE(__x), __x.begin(), __x.end()); }
303 #ifdef __GXX_EXPERIMENTAL_CXX0X__
305 splice(iterator __position, list& __x)
306 { this->splice(__position, std::move(__x)); }
310 #ifdef __GXX_EXPERIMENTAL_CXX0X__
311 splice(iterator __position, list&& __x, iterator __i)
313 splice(iterator __position, list& __x, iterator __i)
316 // We used to perform the splice_alloc check: not anymore, redundant
317 // after implementing the relevant bits of N1599.
319 // _GLIBCXX_RESOLVE_LIB_DEFECTS
320 _Base::splice(__position, _GLIBCXX_MOVE(__x._M_base()),
324 #ifdef __GXX_EXPERIMENTAL_CXX0X__
326 splice(iterator __position, list& __x, iterator __i)
327 { this->splice(__position, std::move(__x), __i); }
331 #ifdef __GXX_EXPERIMENTAL_CXX0X__
332 splice(iterator __position, list&& __x, iterator __first,
335 splice(iterator __position, list& __x, iterator __first,
339 // We used to perform the splice_alloc check: not anymore, redundant
340 // after implementing the relevant bits of N1599.
342 _Base::splice(__position, _GLIBCXX_MOVE(__x._M_base()),
346 #ifdef __GXX_EXPERIMENTAL_CXX0X__
348 splice(iterator __position, list& __x, iterator __first, iterator __last)
349 { this->splice(__position, std::move(__x), __first, __last); }
353 remove(const _Tp& __value)
355 for (iterator __x = begin(); __x != _Base::end(); )
364 template<class _Predicate>
366 remove_if(_Predicate __pred)
368 for (iterator __x = begin(); __x != _Base::end(); )
380 iterator __first = begin();
381 iterator __last = end();
382 if (__first == __last)
384 iterator __next = __first;
385 while (++__next != __last)
387 if (*__first == *__next)
395 template<class _BinaryPredicate>
397 unique(_BinaryPredicate __binary_pred)
399 iterator __first = begin();
400 iterator __last = end();
401 if (__first == __last)
403 iterator __next = __first;
404 while (++__next != __last)
406 if (__binary_pred(*__first, *__next))
415 #ifdef __GXX_EXPERIMENTAL_CXX0X__
421 // _GLIBCXX_RESOLVE_LIB_DEFECTS
422 // 300. list::merge() specification incomplete
424 _Base::merge(_GLIBCXX_MOVE(__x._M_base()));
427 #ifdef __GXX_EXPERIMENTAL_CXX0X__
430 { this->merge(std::move(__x)); }
433 template<class _Compare>
435 #ifdef __GXX_EXPERIMENTAL_CXX0X__
436 merge(list&& __x, _Compare __comp)
438 merge(list& __x, _Compare __comp)
441 // _GLIBCXX_RESOLVE_LIB_DEFECTS
442 // 300. list::merge() specification incomplete
444 _Base::merge(_GLIBCXX_MOVE(__x._M_base()), __comp);
447 #ifdef __GXX_EXPERIMENTAL_CXX0X__
448 template<typename _Compare>
450 merge(list& __x, _Compare __comp)
451 { this->merge(std::move(__x), __comp); }
455 sort() { _Base::sort(); }
457 template<typename _StrictWeakOrdering>
459 sort(_StrictWeakOrdering __pred) { _Base::sort(__pred); }
461 using _Base::reverse;
464 _M_base() { return *this; }
467 _M_base() const { return *this; }
471 template<typename _Tp, typename _Alloc>
473 operator==(const list<_Tp, _Alloc>& __lhs,
474 const list<_Tp, _Alloc>& __rhs)
475 { return __lhs._M_base() == __rhs._M_base(); }
477 template<typename _Tp, typename _Alloc>
479 operator!=(const list<_Tp, _Alloc>& __lhs,
480 const list<_Tp, _Alloc>& __rhs)
481 { return __lhs._M_base() != __rhs._M_base(); }
483 template<typename _Tp, typename _Alloc>
485 operator<(const list<_Tp, _Alloc>& __lhs,
486 const list<_Tp, _Alloc>& __rhs)
487 { return __lhs._M_base() < __rhs._M_base(); }
489 template<typename _Tp, typename _Alloc>
491 operator<=(const list<_Tp, _Alloc>& __lhs,
492 const list<_Tp, _Alloc>& __rhs)
493 { return __lhs._M_base() <= __rhs._M_base(); }
495 template<typename _Tp, typename _Alloc>
497 operator>=(const list<_Tp, _Alloc>& __lhs,
498 const list<_Tp, _Alloc>& __rhs)
499 { return __lhs._M_base() >= __rhs._M_base(); }
501 template<typename _Tp, typename _Alloc>
503 operator>(const list<_Tp, _Alloc>& __lhs,
504 const list<_Tp, _Alloc>& __rhs)
505 { return __lhs._M_base() > __rhs._M_base(); }
507 template<typename _Tp, typename _Alloc>
509 swap(list<_Tp, _Alloc>& __lhs, list<_Tp, _Alloc>& __rhs)
510 { __lhs.swap(__rhs); }
512 } // namespace __profile