1 // Debugging list implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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 <debug/safe_sequence.h>
35 #include <debug/safe_iterator.h>
37 namespace std _GLIBCXX_VISIBILITY(default)
41 /// Class std::list with safety/checking/debug instrumentation.
42 template<typename _Tp, typename _Allocator = std::allocator<_Tp> >
44 : public _GLIBCXX_STD_C::list<_Tp, _Allocator>,
45 public __gnu_debug::_Safe_sequence<list<_Tp, _Allocator> >
47 typedef _GLIBCXX_STD_C::list<_Tp, _Allocator> _Base;
48 typedef __gnu_debug::_Safe_sequence<list> _Safe_base;
50 typedef typename _Base::iterator _Base_iterator;
51 typedef typename _Base::const_iterator _Base_const_iterator;
52 typedef __gnu_debug::_Equal_to<_Base_const_iterator> _Equal;
53 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
55 typedef typename _Base::reference reference;
56 typedef typename _Base::const_reference const_reference;
58 typedef __gnu_debug::_Safe_iterator<_Base_iterator, list>
60 typedef __gnu_debug::_Safe_iterator<_Base_const_iterator, list>
63 typedef typename _Base::size_type size_type;
64 typedef typename _Base::difference_type difference_type;
66 typedef _Tp value_type;
67 typedef _Allocator allocator_type;
68 typedef typename _Base::pointer pointer;
69 typedef typename _Base::const_pointer const_pointer;
70 typedef std::reverse_iterator<iterator> reverse_iterator;
71 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
73 // 23.2.2.1 construct/copy/destroy:
75 list(const _Allocator& __a = _Allocator())
78 #ifdef __GXX_EXPERIMENTAL_CXX0X__
83 list(size_type __n, const _Tp& __value,
84 const _Allocator& __a = _Allocator())
85 : _Base(__n, __value, __a) { }
88 list(size_type __n, const _Tp& __value = _Tp(),
89 const _Allocator& __a = _Allocator())
90 : _Base(__n, __value, __a) { }
93 template<class _InputIterator>
94 list(_InputIterator __first, _InputIterator __last,
95 const _Allocator& __a = _Allocator())
96 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
98 __gnu_debug::__base(__last), __a)
102 list(const list& __x)
103 : _Base(__x), _Safe_base() { }
105 list(const _Base& __x)
106 : _Base(__x), _Safe_base() { }
108 #ifdef __GXX_EXPERIMENTAL_CXX0X__
110 : _Base(std::move(__x)), _Safe_base()
111 { this->_M_swap(__x); }
113 list(initializer_list<value_type> __l,
114 const allocator_type& __a = allocator_type())
115 : _Base(__l, __a), _Safe_base() { }
121 operator=(const list& __x)
123 static_cast<_Base&>(*this) = __x;
124 this->_M_invalidate_all();
128 #ifdef __GXX_EXPERIMENTAL_CXX0X__
130 operator=(list&& __x)
140 operator=(initializer_list<value_type> __l)
142 static_cast<_Base&>(*this) = __l;
143 this->_M_invalidate_all();
148 assign(initializer_list<value_type> __l)
151 this->_M_invalidate_all();
155 template<class _InputIterator>
157 assign(_InputIterator __first, _InputIterator __last)
159 __glibcxx_check_valid_range(__first, __last);
160 _Base::assign(__gnu_debug::__base(__first),
161 __gnu_debug::__base(__last));
162 this->_M_invalidate_all();
166 assign(size_type __n, const _Tp& __t)
168 _Base::assign(__n, __t);
169 this->_M_invalidate_all();
172 using _Base::get_allocator;
177 { return iterator(_Base::begin(), this); }
181 { return const_iterator(_Base::begin(), this); }
185 { return iterator(_Base::end(), this); }
189 { return const_iterator(_Base::end(), this); }
193 { return reverse_iterator(end()); }
195 const_reverse_iterator
197 { return const_reverse_iterator(end()); }
201 { return reverse_iterator(begin()); }
203 const_reverse_iterator
205 { return const_reverse_iterator(begin()); }
207 #ifdef __GXX_EXPERIMENTAL_CXX0X__
210 { return const_iterator(_Base::begin(), this); }
214 { return const_iterator(_Base::end(), this); }
216 const_reverse_iterator
218 { return const_reverse_iterator(end()); }
220 const_reverse_iterator
222 { return const_reverse_iterator(begin()); }
225 // 23.2.2.2 capacity:
228 using _Base::max_size;
230 #ifdef __GXX_EXPERIMENTAL_CXX0X__
232 resize(size_type __sz)
234 this->_M_detach_singular();
236 // if __sz < size(), invalidate all iterators in [begin+__sz, end())
237 _Base_iterator __victim = _Base::begin();
238 _Base_iterator __end = _Base::end();
239 for (size_type __i = __sz; __victim != __end && __i > 0; --__i)
242 for (; __victim != __end; ++__victim)
244 this->_M_invalidate_if(_Equal(__victim));
253 this->_M_revalidate_singular();
254 __throw_exception_again;
259 resize(size_type __sz, const _Tp& __c)
261 this->_M_detach_singular();
263 // if __sz < size(), invalidate all iterators in [begin+__sz, end())
264 _Base_iterator __victim = _Base::begin();
265 _Base_iterator __end = _Base::end();
266 for (size_type __i = __sz; __victim != __end && __i > 0; --__i)
269 for (; __victim != __end; ++__victim)
271 this->_M_invalidate_if(_Equal(__victim));
276 _Base::resize(__sz, __c);
280 this->_M_revalidate_singular();
281 __throw_exception_again;
286 resize(size_type __sz, _Tp __c = _Tp())
288 this->_M_detach_singular();
290 // if __sz < size(), invalidate all iterators in [begin+__sz, end())
291 _Base_iterator __victim = _Base::begin();
292 _Base_iterator __end = _Base::end();
293 for (size_type __i = __sz; __victim != __end && __i > 0; --__i)
296 for (; __victim != __end; ++__victim)
298 this->_M_invalidate_if(_Equal(__victim));
303 _Base::resize(__sz, __c);
307 this->_M_revalidate_singular();
308 __throw_exception_again;
317 __glibcxx_check_nonempty();
318 return _Base::front();
324 __glibcxx_check_nonempty();
325 return _Base::front();
331 __glibcxx_check_nonempty();
332 return _Base::back();
338 __glibcxx_check_nonempty();
339 return _Base::back();
342 // 23.2.2.3 modifiers:
343 using _Base::push_front;
345 #ifdef __GXX_EXPERIMENTAL_CXX0X__
346 using _Base::emplace_front;
352 __glibcxx_check_nonempty();
353 this->_M_invalidate_if(_Equal(_Base::begin()));
357 using _Base::push_back;
359 #ifdef __GXX_EXPERIMENTAL_CXX0X__
360 using _Base::emplace_back;
366 __glibcxx_check_nonempty();
367 this->_M_invalidate_if(_Equal(--_Base::end()));
371 #ifdef __GXX_EXPERIMENTAL_CXX0X__
372 template<typename... _Args>
374 emplace(iterator __position, _Args&&... __args)
376 __glibcxx_check_insert(__position);
377 return iterator(_Base::emplace(__position.base(),
378 std::forward<_Args>(__args)...), this);
383 insert(iterator __position, const _Tp& __x)
385 __glibcxx_check_insert(__position);
386 return iterator(_Base::insert(__position.base(), __x), this);
389 #ifdef __GXX_EXPERIMENTAL_CXX0X__
391 insert(iterator __position, _Tp&& __x)
392 { return emplace(__position, std::move(__x)); }
395 insert(iterator __p, initializer_list<value_type> __l)
397 __glibcxx_check_insert(__p);
398 _Base::insert(__p, __l);
403 insert(iterator __position, size_type __n, const _Tp& __x)
405 __glibcxx_check_insert(__position);
406 _Base::insert(__position.base(), __n, __x);
409 template<class _InputIterator>
411 insert(iterator __position, _InputIterator __first,
412 _InputIterator __last)
414 __glibcxx_check_insert_range(__position, __first, __last);
415 _Base::insert(__position.base(), __gnu_debug::__base(__first),
416 __gnu_debug::__base(__last));
421 _M_erase(_Base_iterator __position)
423 this->_M_invalidate_if(_Equal(__position));
424 return _Base::erase(__position);
428 erase(iterator __position)
430 __glibcxx_check_erase(__position);
431 return iterator(_M_erase(__position.base()), this);
435 erase(iterator __position, iterator __last)
437 // _GLIBCXX_RESOLVE_LIB_DEFECTS
438 // 151. can't currently clear() empty container
439 __glibcxx_check_erase_range(__position, __last);
440 for (_Base_iterator __victim = __position.base();
441 __victim != __last.base(); ++__victim)
443 _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
444 _M_message(__gnu_debug::__msg_valid_range)
445 ._M_iterator(__position, "position")
446 ._M_iterator(__last, "last"));
447 this->_M_invalidate_if(_Equal(__victim));
449 return iterator(_Base::erase(__position.base(), __last.base()), this);
463 this->_M_invalidate_all();
466 // 23.2.2.4 list operations:
468 #ifdef __GXX_EXPERIMENTAL_CXX0X__
469 splice(iterator __position, list&& __x)
471 splice(iterator __position, list& __x)
474 _GLIBCXX_DEBUG_VERIFY(&__x != this,
475 _M_message(__gnu_debug::__msg_self_splice)
476 ._M_sequence(*this, "this"));
477 this->_M_transfer_from_if(__x, _Not_equal(__x._M_base().end()));
478 _Base::splice(__position.base(), _GLIBCXX_MOVE(__x._M_base()));
481 #ifdef __GXX_EXPERIMENTAL_CXX0X__
483 splice(iterator __position, list& __x)
484 { splice(__position, std::move(__x)); }
488 #ifdef __GXX_EXPERIMENTAL_CXX0X__
489 splice(iterator __position, list&& __x, iterator __i)
491 splice(iterator __position, list& __x, iterator __i)
494 __glibcxx_check_insert(__position);
496 // We used to perform the splice_alloc check: not anymore, redundant
497 // after implementing the relevant bits of N1599.
499 _GLIBCXX_DEBUG_VERIFY(__i._M_dereferenceable(),
500 _M_message(__gnu_debug::__msg_splice_bad)
501 ._M_iterator(__i, "__i"));
502 _GLIBCXX_DEBUG_VERIFY(__i._M_attached_to(&__x),
503 _M_message(__gnu_debug::__msg_splice_other)
504 ._M_iterator(__i, "__i")._M_sequence(__x, "__x"));
506 // _GLIBCXX_RESOLVE_LIB_DEFECTS
507 // 250. splicing invalidates iterators
508 this->_M_transfer_from_if(__x, _Equal(__i.base()));
509 _Base::splice(__position.base(), _GLIBCXX_MOVE(__x._M_base()),
513 #ifdef __GXX_EXPERIMENTAL_CXX0X__
515 splice(iterator __position, list& __x, iterator __i)
516 { splice(__position, std::move(__x), __i); }
520 #ifdef __GXX_EXPERIMENTAL_CXX0X__
521 splice(iterator __position, list&& __x, iterator __first,
524 splice(iterator __position, list& __x, iterator __first,
528 __glibcxx_check_insert(__position);
529 __glibcxx_check_valid_range(__first, __last);
530 _GLIBCXX_DEBUG_VERIFY(__first._M_attached_to(&__x),
531 _M_message(__gnu_debug::__msg_splice_other)
532 ._M_sequence(__x, "x")
533 ._M_iterator(__first, "first"));
535 // We used to perform the splice_alloc check: not anymore, redundant
536 // after implementing the relevant bits of N1599.
538 for (_Base_iterator __tmp = __first.base();
539 __tmp != __last.base(); ++__tmp)
541 _GLIBCXX_DEBUG_VERIFY(__tmp != _Base::end(),
542 _M_message(__gnu_debug::__msg_valid_range)
543 ._M_iterator(__first, "first")
544 ._M_iterator(__last, "last"));
545 _GLIBCXX_DEBUG_VERIFY(&__x != this || __tmp != __position,
546 _M_message(__gnu_debug::__msg_splice_overlap)
547 ._M_iterator(__tmp, "position")
548 ._M_iterator(__first, "first")
549 ._M_iterator(__last, "last"));
550 // _GLIBCXX_RESOLVE_LIB_DEFECTS
551 // 250. splicing invalidates iterators
552 this->_M_transfer_from_if(__x, _Equal(__tmp));
555 _Base::splice(__position.base(), _GLIBCXX_MOVE(__x._M_base()),
556 __first.base(), __last.base());
559 #ifdef __GXX_EXPERIMENTAL_CXX0X__
561 splice(iterator __position, list& __x, iterator __first, iterator __last)
562 { splice(__position, std::move(__x), __first, __last); }
566 remove(const _Tp& __value)
568 for (_Base_iterator __x = _Base::begin(); __x != _Base::end(); )
577 template<class _Predicate>
579 remove_if(_Predicate __pred)
581 for (_Base_iterator __x = _Base::begin(); __x != _Base::end(); )
593 _Base_iterator __first = _Base::begin();
594 _Base_iterator __last = _Base::end();
595 if (__first == __last)
597 _Base_iterator __next = __first; ++__next;
598 while (__next != __last)
600 if (*__first == *__next)
601 __next = _M_erase(__next);
607 template<class _BinaryPredicate>
609 unique(_BinaryPredicate __binary_pred)
611 _Base_iterator __first = _Base::begin();
612 _Base_iterator __last = _Base::end();
613 if (__first == __last)
615 _Base_iterator __next = __first; ++__next;
616 while (__next != __last)
618 if (__binary_pred(*__first, *__next))
619 __next = _M_erase(__next);
626 #ifdef __GXX_EXPERIMENTAL_CXX0X__
632 // _GLIBCXX_RESOLVE_LIB_DEFECTS
633 // 300. list::merge() specification incomplete
636 __glibcxx_check_sorted(_Base::begin(), _Base::end());
637 __glibcxx_check_sorted(__x.begin().base(), __x.end().base());
638 this->_M_transfer_from_if(__x, _Not_equal(__x._M_base().end()));
639 _Base::merge(_GLIBCXX_MOVE(__x._M_base()));
643 #ifdef __GXX_EXPERIMENTAL_CXX0X__
646 { merge(std::move(__x)); }
649 template<class _Compare>
651 #ifdef __GXX_EXPERIMENTAL_CXX0X__
652 merge(list&& __x, _Compare __comp)
654 merge(list& __x, _Compare __comp)
657 // _GLIBCXX_RESOLVE_LIB_DEFECTS
658 // 300. list::merge() specification incomplete
661 __glibcxx_check_sorted_pred(_Base::begin(), _Base::end(),
663 __glibcxx_check_sorted_pred(__x.begin().base(), __x.end().base(),
665 this->_M_transfer_from_if(__x, _Not_equal(__x._M_base().end()));
666 _Base::merge(_GLIBCXX_MOVE(__x._M_base()), __comp);
670 #ifdef __GXX_EXPERIMENTAL_CXX0X__
671 template<typename _Compare>
673 merge(list& __x, _Compare __comp)
674 { merge(std::move(__x), __comp); }
678 sort() { _Base::sort(); }
680 template<typename _StrictWeakOrdering>
682 sort(_StrictWeakOrdering __pred) { _Base::sort(__pred); }
684 using _Base::reverse;
687 _M_base() { return *this; }
690 _M_base() const { return *this; }
696 this->_M_invalidate_if(_Not_equal(_Base::end()));
700 template<typename _Tp, typename _Alloc>
702 operator==(const list<_Tp, _Alloc>& __lhs,
703 const list<_Tp, _Alloc>& __rhs)
704 { return __lhs._M_base() == __rhs._M_base(); }
706 template<typename _Tp, typename _Alloc>
708 operator!=(const list<_Tp, _Alloc>& __lhs,
709 const list<_Tp, _Alloc>& __rhs)
710 { return __lhs._M_base() != __rhs._M_base(); }
712 template<typename _Tp, typename _Alloc>
714 operator<(const list<_Tp, _Alloc>& __lhs,
715 const list<_Tp, _Alloc>& __rhs)
716 { return __lhs._M_base() < __rhs._M_base(); }
718 template<typename _Tp, typename _Alloc>
720 operator<=(const list<_Tp, _Alloc>& __lhs,
721 const list<_Tp, _Alloc>& __rhs)
722 { return __lhs._M_base() <= __rhs._M_base(); }
724 template<typename _Tp, typename _Alloc>
726 operator>=(const list<_Tp, _Alloc>& __lhs,
727 const list<_Tp, _Alloc>& __rhs)
728 { return __lhs._M_base() >= __rhs._M_base(); }
730 template<typename _Tp, typename _Alloc>
732 operator>(const list<_Tp, _Alloc>& __lhs,
733 const list<_Tp, _Alloc>& __rhs)
734 { return __lhs._M_base() > __rhs._M_base(); }
736 template<typename _Tp, typename _Alloc>
738 swap(list<_Tp, _Alloc>& __lhs, list<_Tp, _Alloc>& __rhs)
739 { __lhs.swap(__rhs); }
741 } // namespace __debug