1 // Debugging list implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
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;
49 typedef typename _Base::iterator _Base_iterator;
50 typedef typename _Base::const_iterator _Base_const_iterator;
51 typedef __gnu_debug::_Equal_to<_Base_const_iterator> _Equal;
52 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
54 typedef typename _Base::reference reference;
55 typedef typename _Base::const_reference const_reference;
57 typedef __gnu_debug::_Safe_iterator<_Base_iterator, list>
59 typedef __gnu_debug::_Safe_iterator<_Base_const_iterator, list>
62 typedef typename _Base::size_type size_type;
63 typedef typename _Base::difference_type difference_type;
65 typedef _Tp value_type;
66 typedef _Allocator allocator_type;
67 typedef typename _Base::pointer pointer;
68 typedef typename _Base::const_pointer const_pointer;
69 typedef std::reverse_iterator<iterator> reverse_iterator;
70 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
72 // 23.2.2.1 construct/copy/destroy:
74 list(const _Allocator& __a = _Allocator())
77 #ifdef __GXX_EXPERIMENTAL_CXX0X__
82 list(size_type __n, const _Tp& __value,
83 const _Allocator& __a = _Allocator())
84 : _Base(__n, __value, __a) { }
87 list(size_type __n, const _Tp& __value = _Tp(),
88 const _Allocator& __a = _Allocator())
89 : _Base(__n, __value, __a) { }
92 template<class _InputIterator>
93 list(_InputIterator __first, _InputIterator __last,
94 const _Allocator& __a = _Allocator())
95 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
97 __gnu_debug::__base(__last), __a)
101 list(const list& __x)
104 list(const _Base& __x)
107 #ifdef __GXX_EXPERIMENTAL_CXX0X__
108 list(list&& __x) noexcept
109 : _Base(std::move(__x))
110 { this->_M_swap(__x); }
112 list(initializer_list<value_type> __l,
113 const allocator_type& __a = allocator_type())
114 : _Base(__l, __a) { }
117 ~list() _GLIBCXX_NOEXCEPT { }
120 operator=(const list& __x)
122 static_cast<_Base&>(*this) = __x;
123 this->_M_invalidate_all();
127 #ifdef __GXX_EXPERIMENTAL_CXX0X__
129 operator=(list&& __x)
139 operator=(initializer_list<value_type> __l)
141 static_cast<_Base&>(*this) = __l;
142 this->_M_invalidate_all();
147 assign(initializer_list<value_type> __l)
150 this->_M_invalidate_all();
154 template<class _InputIterator>
156 assign(_InputIterator __first, _InputIterator __last)
158 __glibcxx_check_valid_range(__first, __last);
159 _Base::assign(__gnu_debug::__base(__first),
160 __gnu_debug::__base(__last));
161 this->_M_invalidate_all();
165 assign(size_type __n, const _Tp& __t)
167 _Base::assign(__n, __t);
168 this->_M_invalidate_all();
171 using _Base::get_allocator;
175 begin() _GLIBCXX_NOEXCEPT
176 { return iterator(_Base::begin(), this); }
179 begin() const _GLIBCXX_NOEXCEPT
180 { return const_iterator(_Base::begin(), this); }
183 end() _GLIBCXX_NOEXCEPT
184 { return iterator(_Base::end(), this); }
187 end() const _GLIBCXX_NOEXCEPT
188 { return const_iterator(_Base::end(), this); }
191 rbegin() _GLIBCXX_NOEXCEPT
192 { return reverse_iterator(end()); }
194 const_reverse_iterator
195 rbegin() const _GLIBCXX_NOEXCEPT
196 { return const_reverse_iterator(end()); }
199 rend() _GLIBCXX_NOEXCEPT
200 { return reverse_iterator(begin()); }
202 const_reverse_iterator
203 rend() const _GLIBCXX_NOEXCEPT
204 { return const_reverse_iterator(begin()); }
206 #ifdef __GXX_EXPERIMENTAL_CXX0X__
208 cbegin() const noexcept
209 { return const_iterator(_Base::begin(), this); }
212 cend() const noexcept
213 { return const_iterator(_Base::end(), this); }
215 const_reverse_iterator
216 crbegin() const noexcept
217 { return const_reverse_iterator(end()); }
219 const_reverse_iterator
220 crend() const noexcept
221 { return const_reverse_iterator(begin()); }
224 // 23.2.2.2 capacity:
227 using _Base::max_size;
229 #ifdef __GXX_EXPERIMENTAL_CXX0X__
231 resize(size_type __sz)
233 this->_M_detach_singular();
235 // if __sz < size(), invalidate all iterators in [begin+__sz, end())
236 _Base_iterator __victim = _Base::begin();
237 _Base_iterator __end = _Base::end();
238 for (size_type __i = __sz; __victim != __end && __i > 0; --__i)
241 for (; __victim != __end; ++__victim)
243 this->_M_invalidate_if(_Equal(__victim));
252 this->_M_revalidate_singular();
253 __throw_exception_again;
258 resize(size_type __sz, const _Tp& __c)
260 this->_M_detach_singular();
262 // if __sz < size(), invalidate all iterators in [begin+__sz, end())
263 _Base_iterator __victim = _Base::begin();
264 _Base_iterator __end = _Base::end();
265 for (size_type __i = __sz; __victim != __end && __i > 0; --__i)
268 for (; __victim != __end; ++__victim)
270 this->_M_invalidate_if(_Equal(__victim));
275 _Base::resize(__sz, __c);
279 this->_M_revalidate_singular();
280 __throw_exception_again;
285 resize(size_type __sz, _Tp __c = _Tp())
287 this->_M_detach_singular();
289 // if __sz < size(), invalidate all iterators in [begin+__sz, end())
290 _Base_iterator __victim = _Base::begin();
291 _Base_iterator __end = _Base::end();
292 for (size_type __i = __sz; __victim != __end && __i > 0; --__i)
295 for (; __victim != __end; ++__victim)
297 this->_M_invalidate_if(_Equal(__victim));
302 _Base::resize(__sz, __c);
306 this->_M_revalidate_singular();
307 __throw_exception_again;
316 __glibcxx_check_nonempty();
317 return _Base::front();
323 __glibcxx_check_nonempty();
324 return _Base::front();
330 __glibcxx_check_nonempty();
331 return _Base::back();
337 __glibcxx_check_nonempty();
338 return _Base::back();
341 // 23.2.2.3 modifiers:
342 using _Base::push_front;
344 #ifdef __GXX_EXPERIMENTAL_CXX0X__
345 using _Base::emplace_front;
351 __glibcxx_check_nonempty();
352 this->_M_invalidate_if(_Equal(_Base::begin()));
356 using _Base::push_back;
358 #ifdef __GXX_EXPERIMENTAL_CXX0X__
359 using _Base::emplace_back;
365 __glibcxx_check_nonempty();
366 this->_M_invalidate_if(_Equal(--_Base::end()));
370 #ifdef __GXX_EXPERIMENTAL_CXX0X__
371 template<typename... _Args>
373 emplace(iterator __position, _Args&&... __args)
375 __glibcxx_check_insert(__position);
376 return iterator(_Base::emplace(__position.base(),
377 std::forward<_Args>(__args)...), this);
382 insert(iterator __position, const _Tp& __x)
384 __glibcxx_check_insert(__position);
385 return iterator(_Base::insert(__position.base(), __x), this);
388 #ifdef __GXX_EXPERIMENTAL_CXX0X__
390 insert(iterator __position, _Tp&& __x)
391 { return emplace(__position, std::move(__x)); }
394 insert(iterator __p, initializer_list<value_type> __l)
396 __glibcxx_check_insert(__p);
397 _Base::insert(__p.base(), __l);
402 insert(iterator __position, size_type __n, const _Tp& __x)
404 __glibcxx_check_insert(__position);
405 _Base::insert(__position.base(), __n, __x);
408 template<class _InputIterator>
410 insert(iterator __position, _InputIterator __first,
411 _InputIterator __last)
413 __glibcxx_check_insert_range(__position, __first, __last);
414 _Base::insert(__position.base(), __gnu_debug::__base(__first),
415 __gnu_debug::__base(__last));
420 _M_erase(_Base_iterator __position)
422 this->_M_invalidate_if(_Equal(__position));
423 return _Base::erase(__position);
427 erase(iterator __position)
429 __glibcxx_check_erase(__position);
430 return iterator(_M_erase(__position.base()), this);
434 erase(iterator __position, iterator __last)
436 // _GLIBCXX_RESOLVE_LIB_DEFECTS
437 // 151. can't currently clear() empty container
438 __glibcxx_check_erase_range(__position, __last);
439 for (_Base_iterator __victim = __position.base();
440 __victim != __last.base(); ++__victim)
442 _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
443 _M_message(__gnu_debug::__msg_valid_range)
444 ._M_iterator(__position, "position")
445 ._M_iterator(__last, "last"));
446 this->_M_invalidate_if(_Equal(__victim));
448 return iterator(_Base::erase(__position.base(), __last.base()), this);
459 clear() _GLIBCXX_NOEXCEPT
462 this->_M_invalidate_all();
465 // 23.2.2.4 list operations:
467 #ifdef __GXX_EXPERIMENTAL_CXX0X__
468 splice(iterator __position, list&& __x)
470 splice(iterator __position, list& __x)
473 _GLIBCXX_DEBUG_VERIFY(&__x != this,
474 _M_message(__gnu_debug::__msg_self_splice)
475 ._M_sequence(*this, "this"));
476 this->_M_transfer_from_if(__x, _Not_equal(__x._M_base().end()));
477 _Base::splice(__position.base(), _GLIBCXX_MOVE(__x._M_base()));
480 #ifdef __GXX_EXPERIMENTAL_CXX0X__
482 splice(iterator __position, list& __x)
483 { splice(__position, std::move(__x)); }
487 #ifdef __GXX_EXPERIMENTAL_CXX0X__
488 splice(iterator __position, list&& __x, iterator __i)
490 splice(iterator __position, list& __x, iterator __i)
493 __glibcxx_check_insert(__position);
495 // We used to perform the splice_alloc check: not anymore, redundant
496 // after implementing the relevant bits of N1599.
498 _GLIBCXX_DEBUG_VERIFY(__i._M_dereferenceable(),
499 _M_message(__gnu_debug::__msg_splice_bad)
500 ._M_iterator(__i, "__i"));
501 _GLIBCXX_DEBUG_VERIFY(__i._M_attached_to(&__x),
502 _M_message(__gnu_debug::__msg_splice_other)
503 ._M_iterator(__i, "__i")._M_sequence(__x, "__x"));
505 // _GLIBCXX_RESOLVE_LIB_DEFECTS
506 // 250. splicing invalidates iterators
507 this->_M_transfer_from_if(__x, _Equal(__i.base()));
508 _Base::splice(__position.base(), _GLIBCXX_MOVE(__x._M_base()),
512 #ifdef __GXX_EXPERIMENTAL_CXX0X__
514 splice(iterator __position, list& __x, iterator __i)
515 { splice(__position, std::move(__x), __i); }
519 #ifdef __GXX_EXPERIMENTAL_CXX0X__
520 splice(iterator __position, list&& __x, iterator __first,
523 splice(iterator __position, list& __x, iterator __first,
527 __glibcxx_check_insert(__position);
528 __glibcxx_check_valid_range(__first, __last);
529 _GLIBCXX_DEBUG_VERIFY(__first._M_attached_to(&__x),
530 _M_message(__gnu_debug::__msg_splice_other)
531 ._M_sequence(__x, "x")
532 ._M_iterator(__first, "first"));
534 // We used to perform the splice_alloc check: not anymore, redundant
535 // after implementing the relevant bits of N1599.
537 for (_Base_iterator __tmp = __first.base();
538 __tmp != __last.base(); ++__tmp)
540 _GLIBCXX_DEBUG_VERIFY(__tmp != _Base::end(),
541 _M_message(__gnu_debug::__msg_valid_range)
542 ._M_iterator(__first, "first")
543 ._M_iterator(__last, "last"));
544 _GLIBCXX_DEBUG_VERIFY(&__x != this || __tmp != __position,
545 _M_message(__gnu_debug::__msg_splice_overlap)
546 ._M_iterator(__tmp, "position")
547 ._M_iterator(__first, "first")
548 ._M_iterator(__last, "last"));
549 // _GLIBCXX_RESOLVE_LIB_DEFECTS
550 // 250. splicing invalidates iterators
551 this->_M_transfer_from_if(__x, _Equal(__tmp));
554 _Base::splice(__position.base(), _GLIBCXX_MOVE(__x._M_base()),
555 __first.base(), __last.base());
558 #ifdef __GXX_EXPERIMENTAL_CXX0X__
560 splice(iterator __position, list& __x, iterator __first, iterator __last)
561 { splice(__position, std::move(__x), __first, __last); }
565 remove(const _Tp& __value)
567 for (_Base_iterator __x = _Base::begin(); __x != _Base::end(); )
576 template<class _Predicate>
578 remove_if(_Predicate __pred)
580 for (_Base_iterator __x = _Base::begin(); __x != _Base::end(); )
592 _Base_iterator __first = _Base::begin();
593 _Base_iterator __last = _Base::end();
594 if (__first == __last)
596 _Base_iterator __next = __first; ++__next;
597 while (__next != __last)
599 if (*__first == *__next)
600 __next = _M_erase(__next);
606 template<class _BinaryPredicate>
608 unique(_BinaryPredicate __binary_pred)
610 _Base_iterator __first = _Base::begin();
611 _Base_iterator __last = _Base::end();
612 if (__first == __last)
614 _Base_iterator __next = __first; ++__next;
615 while (__next != __last)
617 if (__binary_pred(*__first, *__next))
618 __next = _M_erase(__next);
625 #ifdef __GXX_EXPERIMENTAL_CXX0X__
631 // _GLIBCXX_RESOLVE_LIB_DEFECTS
632 // 300. list::merge() specification incomplete
635 __glibcxx_check_sorted(_Base::begin(), _Base::end());
636 __glibcxx_check_sorted(__x.begin().base(), __x.end().base());
637 this->_M_transfer_from_if(__x, _Not_equal(__x._M_base().end()));
638 _Base::merge(_GLIBCXX_MOVE(__x._M_base()));
642 #ifdef __GXX_EXPERIMENTAL_CXX0X__
645 { merge(std::move(__x)); }
648 template<class _Compare>
650 #ifdef __GXX_EXPERIMENTAL_CXX0X__
651 merge(list&& __x, _Compare __comp)
653 merge(list& __x, _Compare __comp)
656 // _GLIBCXX_RESOLVE_LIB_DEFECTS
657 // 300. list::merge() specification incomplete
660 __glibcxx_check_sorted_pred(_Base::begin(), _Base::end(),
662 __glibcxx_check_sorted_pred(__x.begin().base(), __x.end().base(),
664 this->_M_transfer_from_if(__x, _Not_equal(__x._M_base().end()));
665 _Base::merge(_GLIBCXX_MOVE(__x._M_base()), __comp);
669 #ifdef __GXX_EXPERIMENTAL_CXX0X__
670 template<typename _Compare>
672 merge(list& __x, _Compare __comp)
673 { merge(std::move(__x), __comp); }
677 sort() { _Base::sort(); }
679 template<typename _StrictWeakOrdering>
681 sort(_StrictWeakOrdering __pred) { _Base::sort(__pred); }
683 using _Base::reverse;
686 _M_base() _GLIBCXX_NOEXCEPT { return *this; }
689 _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
695 this->_M_invalidate_if(_Not_equal(_Base::end()));
699 template<typename _Tp, typename _Alloc>
701 operator==(const list<_Tp, _Alloc>& __lhs,
702 const list<_Tp, _Alloc>& __rhs)
703 { return __lhs._M_base() == __rhs._M_base(); }
705 template<typename _Tp, typename _Alloc>
707 operator!=(const list<_Tp, _Alloc>& __lhs,
708 const list<_Tp, _Alloc>& __rhs)
709 { return __lhs._M_base() != __rhs._M_base(); }
711 template<typename _Tp, typename _Alloc>
713 operator<(const list<_Tp, _Alloc>& __lhs,
714 const list<_Tp, _Alloc>& __rhs)
715 { return __lhs._M_base() < __rhs._M_base(); }
717 template<typename _Tp, typename _Alloc>
719 operator<=(const list<_Tp, _Alloc>& __lhs,
720 const list<_Tp, _Alloc>& __rhs)
721 { return __lhs._M_base() <= __rhs._M_base(); }
723 template<typename _Tp, typename _Alloc>
725 operator>=(const list<_Tp, _Alloc>& __lhs,
726 const list<_Tp, _Alloc>& __rhs)
727 { return __lhs._M_base() >= __rhs._M_base(); }
729 template<typename _Tp, typename _Alloc>
731 operator>(const list<_Tp, _Alloc>& __lhs,
732 const list<_Tp, _Alloc>& __rhs)
733 { return __lhs._M_base() > __rhs._M_base(); }
735 template<typename _Tp, typename _Alloc>
737 swap(list<_Tp, _Alloc>& __lhs, list<_Tp, _Alloc>& __rhs)
738 { __lhs.swap(__rhs); }
740 } // namespace __debug