1 // Debugging list implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
27 * This file is a GNU debug extension to the Standard C++ Library.
30 #ifndef _GLIBCXX_DEBUG_LIST
31 #define _GLIBCXX_DEBUG_LIST 1
34 #include <bits/stl_algo.h>
35 #include <debug/safe_sequence.h>
36 #include <debug/safe_iterator.h>
42 template<typename _Tp, typename _Allocator = std::allocator<_Tp> >
44 : public _GLIBCXX_STD_D::list<_Tp, _Allocator>,
45 public __gnu_debug::_Safe_sequence<list<_Tp, _Allocator> >
47 typedef _GLIBCXX_STD_D::list<_Tp, _Allocator> _Base;
48 typedef __gnu_debug::_Safe_sequence<list> _Safe_base;
51 typedef typename _Base::reference reference;
52 typedef typename _Base::const_reference const_reference;
54 typedef __gnu_debug::_Safe_iterator<typename _Base::iterator, list>
56 typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator, list>
59 typedef typename _Base::size_type size_type;
60 typedef typename _Base::difference_type difference_type;
62 typedef _Tp value_type;
63 typedef _Allocator allocator_type;
64 typedef typename _Base::pointer pointer;
65 typedef typename _Base::const_pointer const_pointer;
66 typedef std::reverse_iterator<iterator> reverse_iterator;
67 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
69 // 23.2.2.1 construct/copy/destroy:
70 explicit list(const _Allocator& __a = _Allocator())
73 explicit list(size_type __n, const _Tp& __value = _Tp(),
74 const _Allocator& __a = _Allocator())
75 : _Base(__n, __value, __a) { }
77 template<class _InputIterator>
78 list(_InputIterator __first, _InputIterator __last,
79 const _Allocator& __a = _Allocator())
80 : _Base(__gnu_debug::__check_valid_range(__first, __last), __last, __a)
85 : _Base(__x), _Safe_base() { }
87 list(const _Base& __x)
88 : _Base(__x), _Safe_base() { }
90 #ifdef __GXX_EXPERIMENTAL_CXX0X__
92 : _Base(std::forward<list>(__x)), _Safe_base()
93 { this->_M_swap(__x); }
95 list(initializer_list<value_type> __l,
96 const allocator_type& __a = allocator_type())
97 : _Base(__l, __a), _Safe_base() { }
103 operator=(const list& __x)
105 static_cast<_Base&>(*this) = __x;
106 this->_M_invalidate_all();
110 #ifdef __GXX_EXPERIMENTAL_CXX0X__
112 operator=(list&& __x)
121 operator=(initializer_list<value_type> __l)
123 static_cast<_Base&>(*this) = __l;
124 this->_M_invalidate_all();
129 assign(initializer_list<value_type> __l)
132 this->_M_invalidate_all();
136 template<class _InputIterator>
138 assign(_InputIterator __first, _InputIterator __last)
140 __glibcxx_check_valid_range(__first, __last);
141 _Base::assign(__first, __last);
142 this->_M_invalidate_all();
146 assign(size_type __n, const _Tp& __t)
148 _Base::assign(__n, __t);
149 this->_M_invalidate_all();
152 using _Base::get_allocator;
157 { return iterator(_Base::begin(), this); }
161 { return const_iterator(_Base::begin(), this); }
165 { return iterator(_Base::end(), this); }
169 { return const_iterator(_Base::end(), this); }
173 { return reverse_iterator(end()); }
175 const_reverse_iterator
177 { return const_reverse_iterator(end()); }
181 { return reverse_iterator(begin()); }
183 const_reverse_iterator
185 { return const_reverse_iterator(begin()); }
187 #ifdef __GXX_EXPERIMENTAL_CXX0X__
190 { return const_iterator(_Base::begin(), this); }
194 { return const_iterator(_Base::end(), this); }
196 const_reverse_iterator
198 { return const_reverse_iterator(end()); }
200 const_reverse_iterator
202 { return const_reverse_iterator(begin()); }
205 // 23.2.2.2 capacity:
208 using _Base::max_size;
211 resize(size_type __sz, _Tp __c = _Tp())
213 this->_M_detach_singular();
215 // if __sz < size(), invalidate all iterators in [begin+__sz, end())
216 iterator __victim = begin();
217 iterator __end = end();
218 for (size_type __i = __sz; __victim != __end && __i > 0; --__i)
221 while (__victim != __end)
223 iterator __real_victim = __victim++;
224 __real_victim._M_invalidate();
229 _Base::resize(__sz, __c);
233 this->_M_revalidate_singular();
234 __throw_exception_again;
242 __glibcxx_check_nonempty();
243 return _Base::front();
249 __glibcxx_check_nonempty();
250 return _Base::front();
256 __glibcxx_check_nonempty();
257 return _Base::back();
263 __glibcxx_check_nonempty();
264 return _Base::back();
267 // 23.2.2.3 modifiers:
268 using _Base::push_front;
270 #ifdef __GXX_EXPERIMENTAL_CXX0X__
271 using _Base::emplace_front;
277 __glibcxx_check_nonempty();
278 iterator __victim = begin();
279 __victim._M_invalidate();
283 using _Base::push_back;
285 #ifdef __GXX_EXPERIMENTAL_CXX0X__
286 using _Base::emplace_back;
292 __glibcxx_check_nonempty();
293 iterator __victim = end();
295 __victim._M_invalidate();
299 #ifdef __GXX_EXPERIMENTAL_CXX0X__
300 template<typename... _Args>
302 emplace(iterator __position, _Args&&... __args)
304 __glibcxx_check_insert(__position);
305 return iterator(_Base::emplace(__position.base(),
306 std::forward<_Args>(__args)...), this);
311 insert(iterator __position, const _Tp& __x)
313 __glibcxx_check_insert(__position);
314 return iterator(_Base::insert(__position.base(), __x), this);
317 #ifdef __GXX_EXPERIMENTAL_CXX0X__
319 insert(iterator __position, _Tp&& __x)
320 { return emplace(__position, std::move(__x)); }
323 insert(iterator __p, initializer_list<value_type> __l)
325 __glibcxx_check_insert(__p);
326 _Base::insert(__p, __l);
331 insert(iterator __position, size_type __n, const _Tp& __x)
333 __glibcxx_check_insert(__position);
334 _Base::insert(__position.base(), __n, __x);
337 template<class _InputIterator>
339 insert(iterator __position, _InputIterator __first,
340 _InputIterator __last)
342 __glibcxx_check_insert_range(__position, __first, __last);
343 _Base::insert(__position.base(), __first, __last);
347 erase(iterator __position)
349 __glibcxx_check_erase(__position);
350 __position._M_invalidate();
351 return iterator(_Base::erase(__position.base()), this);
355 erase(iterator __position, iterator __last)
357 // _GLIBCXX_RESOLVE_LIB_DEFECTS
358 // 151. can't currently clear() empty container
359 __glibcxx_check_erase_range(__position, __last);
360 for (iterator __victim = __position; __victim != __last; )
362 iterator __old = __victim;
364 __old._M_invalidate();
366 return iterator(_Base::erase(__position.base(), __last.base()), this);
370 #ifdef __GXX_EXPERIMENTAL_CXX0X__
384 this->_M_invalidate_all();
387 // 23.2.2.4 list operations:
389 #ifdef __GXX_EXPERIMENTAL_CXX0X__
390 splice(iterator __position, list&& __x)
392 splice(iterator __position, list& __x)
395 _GLIBCXX_DEBUG_VERIFY(&__x != this,
396 _M_message(__gnu_debug::__msg_self_splice)
397 ._M_sequence(*this, "this"));
398 this->splice(__position, _GLIBCXX_MOVE(__x), __x.begin(), __x.end());
402 #ifdef __GXX_EXPERIMENTAL_CXX0X__
403 splice(iterator __position, list&& __x, iterator __i)
405 splice(iterator __position, list& __x, iterator __i)
408 __glibcxx_check_insert(__position);
410 // We used to perform the splice_alloc check: not anymore, redundant
411 // after implementing the relevant bits of N1599.
413 _GLIBCXX_DEBUG_VERIFY(__i._M_dereferenceable(),
414 _M_message(__gnu_debug::__msg_splice_bad)
415 ._M_iterator(__i, "__i"));
416 _GLIBCXX_DEBUG_VERIFY(__i._M_attached_to(&__x),
417 _M_message(__gnu_debug::__msg_splice_other)
418 ._M_iterator(__i, "__i")._M_sequence(__x, "__x"));
420 // _GLIBCXX_RESOLVE_LIB_DEFECTS
421 // 250. splicing invalidates iterators
422 this->_M_transfer_iter(__i);
423 _Base::splice(__position.base(), _GLIBCXX_MOVE(__x._M_base()),
428 #ifdef __GXX_EXPERIMENTAL_CXX0X__
429 splice(iterator __position, list&& __x, iterator __first,
432 splice(iterator __position, list& __x, iterator __first,
436 __glibcxx_check_insert(__position);
437 __glibcxx_check_valid_range(__first, __last);
438 _GLIBCXX_DEBUG_VERIFY(__first._M_attached_to(&__x),
439 _M_message(__gnu_debug::__msg_splice_other)
440 ._M_sequence(__x, "x")
441 ._M_iterator(__first, "first"));
443 // We used to perform the splice_alloc check: not anymore, redundant
444 // after implementing the relevant bits of N1599.
446 for (iterator __tmp = __first; __tmp != __last; )
448 _GLIBCXX_DEBUG_VERIFY(&__x != this || __tmp != __position,
449 _M_message(__gnu_debug::__msg_splice_overlap)
450 ._M_iterator(__tmp, "position")
451 ._M_iterator(__first, "first")
452 ._M_iterator(__last, "last"));
453 iterator __victim = __tmp++;
454 // _GLIBCXX_RESOLVE_LIB_DEFECTS
455 // 250. splicing invalidates iterators
456 this->_M_transfer_iter(__victim);
459 _Base::splice(__position.base(), _GLIBCXX_MOVE(__x._M_base()),
460 __first.base(), __last.base());
464 remove(const _Tp& __value)
466 for (iterator __x = begin(); __x.base() != _Base::end(); )
475 template<class _Predicate>
477 remove_if(_Predicate __pred)
479 for (iterator __x = begin(); __x.base() != _Base::end(); )
491 iterator __first = begin();
492 iterator __last = end();
493 if (__first == __last)
495 iterator __next = __first;
496 while (++__next != __last)
498 if (*__first == *__next)
506 template<class _BinaryPredicate>
508 unique(_BinaryPredicate __binary_pred)
510 iterator __first = begin();
511 iterator __last = end();
512 if (__first == __last)
514 iterator __next = __first;
515 while (++__next != __last)
517 if (__binary_pred(*__first, *__next))
526 #ifdef __GXX_EXPERIMENTAL_CXX0X__
532 // _GLIBCXX_RESOLVE_LIB_DEFECTS
533 // 300. list::merge() specification incomplete
536 __glibcxx_check_sorted(_Base::begin(), _Base::end());
537 __glibcxx_check_sorted(__x.begin().base(), __x.end().base());
538 for (iterator __tmp = __x.begin(); __tmp != __x.end();)
540 iterator __victim = __tmp++;
541 this->_M_transfer_iter(__victim);
543 _Base::merge(_GLIBCXX_MOVE(__x._M_base()));
547 template<class _Compare>
549 #ifdef __GXX_EXPERIMENTAL_CXX0X__
550 merge(list&& __x, _Compare __comp)
552 merge(list& __x, _Compare __comp)
555 // _GLIBCXX_RESOLVE_LIB_DEFECTS
556 // 300. list::merge() specification incomplete
559 __glibcxx_check_sorted_pred(_Base::begin(), _Base::end(),
561 __glibcxx_check_sorted_pred(__x.begin().base(), __x.end().base(),
563 for (iterator __tmp = __x.begin(); __tmp != __x.end();)
565 iterator __victim = __tmp++;
566 this->_M_transfer_iter(__victim);
568 _Base::merge(_GLIBCXX_MOVE(__x._M_base()), __comp);
573 sort() { _Base::sort(); }
575 template<typename _StrictWeakOrdering>
577 sort(_StrictWeakOrdering __pred) { _Base::sort(__pred); }
579 using _Base::reverse;
582 _M_base() { return *this; }
585 _M_base() const { return *this; }
591 typedef typename _Base::const_iterator _Base_const_iterator;
592 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
593 this->_M_invalidate_if(_Not_equal(_M_base().end()));
597 template<typename _Tp, typename _Alloc>
599 operator==(const list<_Tp, _Alloc>& __lhs,
600 const list<_Tp, _Alloc>& __rhs)
601 { return __lhs._M_base() == __rhs._M_base(); }
603 template<typename _Tp, typename _Alloc>
605 operator!=(const list<_Tp, _Alloc>& __lhs,
606 const list<_Tp, _Alloc>& __rhs)
607 { return __lhs._M_base() != __rhs._M_base(); }
609 template<typename _Tp, typename _Alloc>
611 operator<(const list<_Tp, _Alloc>& __lhs,
612 const list<_Tp, _Alloc>& __rhs)
613 { return __lhs._M_base() < __rhs._M_base(); }
615 template<typename _Tp, typename _Alloc>
617 operator<=(const list<_Tp, _Alloc>& __lhs,
618 const list<_Tp, _Alloc>& __rhs)
619 { return __lhs._M_base() <= __rhs._M_base(); }
621 template<typename _Tp, typename _Alloc>
623 operator>=(const list<_Tp, _Alloc>& __lhs,
624 const list<_Tp, _Alloc>& __rhs)
625 { return __lhs._M_base() >= __rhs._M_base(); }
627 template<typename _Tp, typename _Alloc>
629 operator>(const list<_Tp, _Alloc>& __lhs,
630 const list<_Tp, _Alloc>& __rhs)
631 { return __lhs._M_base() > __rhs._M_base(); }
633 template<typename _Tp, typename _Alloc>
635 swap(list<_Tp, _Alloc>& __lhs, list<_Tp, _Alloc>& __rhs)
636 { __lhs.swap(__rhs); }
638 #ifdef __GXX_EXPERIMENTAL_CXX0X__
639 template<typename _Tp, typename _Alloc>
641 swap(list<_Tp, _Alloc>&& __lhs, list<_Tp, _Alloc>& __rhs)
642 { __lhs.swap(__rhs); }
644 template<typename _Tp, typename _Alloc>
646 swap(list<_Tp, _Alloc>& __lhs, list<_Tp, _Alloc>&& __rhs)
647 { __lhs.swap(__rhs); }
650 } // namespace __debug