1 // Debugging list implementation -*- C++ -*-
3 // Copyright (C) 2003-2015 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/>.
26 * This file is a GNU debug extension to the Standard C++ Library.
29 #ifndef _GLIBCXX_DEBUG_LIST
30 #define _GLIBCXX_DEBUG_LIST 1
33 #include <debug/safe_sequence.h>
34 #include <debug/safe_container.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 __gnu_debug::_Safe_container<
45 list<_Tp, _Allocator>, _Allocator,
46 __gnu_debug::_Safe_node_sequence, false>,
47 public _GLIBCXX_STD_C::list<_Tp, _Allocator>
49 typedef _GLIBCXX_STD_C::list<_Tp, _Allocator> _Base;
50 typedef __gnu_debug::_Safe_container<
51 list, _Allocator, __gnu_debug::_Safe_node_sequence, false> _Safe;
53 typedef typename _Base::iterator _Base_iterator;
54 typedef typename _Base::const_iterator _Base_const_iterator;
55 typedef __gnu_debug::_Equal_to<_Base_const_iterator> _Equal;
56 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
59 typedef typename _Base::reference reference;
60 typedef typename _Base::const_reference const_reference;
62 typedef __gnu_debug::_Safe_iterator<_Base_iterator, list>
64 typedef __gnu_debug::_Safe_iterator<_Base_const_iterator, list>
67 typedef typename _Base::size_type size_type;
68 typedef typename _Base::difference_type difference_type;
70 typedef _Tp value_type;
71 typedef _Allocator allocator_type;
72 typedef typename _Base::pointer pointer;
73 typedef typename _Base::const_pointer const_pointer;
74 typedef std::reverse_iterator<iterator> reverse_iterator;
75 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
77 // 23.2.2.1 construct/copy/destroy:
79 #if __cplusplus < 201103L
89 list(const list&) = default;
90 list(list&&) = default;
92 list(initializer_list<value_type> __l,
93 const allocator_type& __a = allocator_type())
100 list(const _Allocator& __a) _GLIBCXX_NOEXCEPT
103 #if __cplusplus >= 201103L
108 list(size_type __n, const _Tp& __value,
109 const _Allocator& __a = _Allocator())
110 : _Base(__n, __value, __a) { }
113 list(size_type __n, const _Tp& __value = _Tp(),
114 const _Allocator& __a = _Allocator())
115 : _Base(__n, __value, __a) { }
118 #if __cplusplus >= 201103L
119 template<class _InputIterator,
120 typename = std::_RequireInputIter<_InputIterator>>
122 template<class _InputIterator>
124 list(_InputIterator __first, _InputIterator __last,
125 const _Allocator& __a = _Allocator())
126 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
128 __gnu_debug::__base(__last), __a)
131 list(const _Base& __x)
134 #if __cplusplus < 201103L
136 operator=(const list& __x)
138 this->_M_safe() = __x;
144 operator=(const list&) = default;
147 operator=(list&&) = default;
150 operator=(initializer_list<value_type> __l)
152 this->_M_invalidate_all();
158 assign(initializer_list<value_type> __l)
161 this->_M_invalidate_all();
165 #if __cplusplus >= 201103L
166 template<class _InputIterator,
167 typename = std::_RequireInputIter<_InputIterator>>
169 template<class _InputIterator>
172 assign(_InputIterator __first, _InputIterator __last)
174 __glibcxx_check_valid_range(__first, __last);
175 _Base::assign(__gnu_debug::__base(__first),
176 __gnu_debug::__base(__last));
177 this->_M_invalidate_all();
181 assign(size_type __n, const _Tp& __t)
183 _Base::assign(__n, __t);
184 this->_M_invalidate_all();
187 using _Base::get_allocator;
191 begin() _GLIBCXX_NOEXCEPT
192 { return iterator(_Base::begin(), this); }
195 begin() const _GLIBCXX_NOEXCEPT
196 { return const_iterator(_Base::begin(), this); }
199 end() _GLIBCXX_NOEXCEPT
200 { return iterator(_Base::end(), this); }
203 end() const _GLIBCXX_NOEXCEPT
204 { return const_iterator(_Base::end(), this); }
207 rbegin() _GLIBCXX_NOEXCEPT
208 { return reverse_iterator(end()); }
210 const_reverse_iterator
211 rbegin() const _GLIBCXX_NOEXCEPT
212 { return const_reverse_iterator(end()); }
215 rend() _GLIBCXX_NOEXCEPT
216 { return reverse_iterator(begin()); }
218 const_reverse_iterator
219 rend() const _GLIBCXX_NOEXCEPT
220 { return const_reverse_iterator(begin()); }
222 #if __cplusplus >= 201103L
224 cbegin() const noexcept
225 { return const_iterator(_Base::begin(), this); }
228 cend() const noexcept
229 { return const_iterator(_Base::end(), this); }
231 const_reverse_iterator
232 crbegin() const noexcept
233 { return const_reverse_iterator(end()); }
235 const_reverse_iterator
236 crend() const noexcept
237 { return const_reverse_iterator(begin()); }
240 // 23.2.2.2 capacity:
243 using _Base::max_size;
245 #if __cplusplus >= 201103L
247 resize(size_type __sz)
249 this->_M_detach_singular();
251 // if __sz < size(), invalidate all iterators in [begin + __sz, end())
252 _Base_iterator __victim = _Base::begin();
253 _Base_iterator __end = _Base::end();
254 for (size_type __i = __sz; __victim != __end && __i > 0; --__i)
257 for (; __victim != __end; ++__victim)
258 this->_M_invalidate_if(_Equal(__victim));
266 this->_M_revalidate_singular();
267 __throw_exception_again;
272 resize(size_type __sz, const _Tp& __c)
274 this->_M_detach_singular();
276 // if __sz < size(), invalidate all iterators in [begin + __sz, end())
277 _Base_iterator __victim = _Base::begin();
278 _Base_iterator __end = _Base::end();
279 for (size_type __i = __sz; __victim != __end && __i > 0; --__i)
282 for (; __victim != __end; ++__victim)
283 this->_M_invalidate_if(_Equal(__victim));
287 _Base::resize(__sz, __c);
291 this->_M_revalidate_singular();
292 __throw_exception_again;
297 resize(size_type __sz, _Tp __c = _Tp())
299 this->_M_detach_singular();
301 // if __sz < size(), invalidate all iterators in [begin + __sz, end())
302 _Base_iterator __victim = _Base::begin();
303 _Base_iterator __end = _Base::end();
304 for (size_type __i = __sz; __victim != __end && __i > 0; --__i)
307 for (; __victim != __end; ++__victim)
308 this->_M_invalidate_if(_Equal(__victim));
312 _Base::resize(__sz, __c);
316 this->_M_revalidate_singular();
317 __throw_exception_again;
324 front() _GLIBCXX_NOEXCEPT
326 __glibcxx_check_nonempty();
327 return _Base::front();
331 front() const _GLIBCXX_NOEXCEPT
333 __glibcxx_check_nonempty();
334 return _Base::front();
338 back() _GLIBCXX_NOEXCEPT
340 __glibcxx_check_nonempty();
341 return _Base::back();
345 back() const _GLIBCXX_NOEXCEPT
347 __glibcxx_check_nonempty();
348 return _Base::back();
351 // 23.2.2.3 modifiers:
352 using _Base::push_front;
354 #if __cplusplus >= 201103L
355 using _Base::emplace_front;
359 pop_front() _GLIBCXX_NOEXCEPT
361 __glibcxx_check_nonempty();
362 this->_M_invalidate_if(_Equal(_Base::begin()));
366 using _Base::push_back;
368 #if __cplusplus >= 201103L
369 using _Base::emplace_back;
373 pop_back() _GLIBCXX_NOEXCEPT
375 __glibcxx_check_nonempty();
376 this->_M_invalidate_if(_Equal(--_Base::end()));
380 #if __cplusplus >= 201103L
381 template<typename... _Args>
383 emplace(const_iterator __position, _Args&&... __args)
385 __glibcxx_check_insert(__position);
386 return iterator(_Base::emplace(__position.base(),
387 std::forward<_Args>(__args)...), this);
392 #if __cplusplus >= 201103L
393 insert(const_iterator __position, const _Tp& __x)
395 insert(iterator __position, const _Tp& __x)
398 __glibcxx_check_insert(__position);
399 return iterator(_Base::insert(__position.base(), __x), this);
402 #if __cplusplus >= 201103L
404 insert(const_iterator __position, _Tp&& __x)
405 { return emplace(__position, std::move(__x)); }
408 insert(const_iterator __p, initializer_list<value_type> __l)
410 __glibcxx_check_insert(__p);
411 return iterator(_Base::insert(__p.base(), __l), this);
415 #if __cplusplus >= 201103L
417 insert(const_iterator __position, size_type __n, const _Tp& __x)
419 __glibcxx_check_insert(__position);
420 return iterator(_Base::insert(__position.base(), __n, __x), this);
424 insert(iterator __position, size_type __n, const _Tp& __x)
426 __glibcxx_check_insert(__position);
427 _Base::insert(__position.base(), __n, __x);
431 #if __cplusplus >= 201103L
432 template<class _InputIterator,
433 typename = std::_RequireInputIter<_InputIterator>>
435 insert(const_iterator __position, _InputIterator __first,
436 _InputIterator __last)
438 __glibcxx_check_insert_range(__position, __first, __last);
439 return iterator(_Base::insert(__position.base(),
440 __gnu_debug::__base(__first),
441 __gnu_debug::__base(__last)),
445 template<class _InputIterator>
447 insert(iterator __position, _InputIterator __first,
448 _InputIterator __last)
450 __glibcxx_check_insert_range(__position, __first, __last);
451 _Base::insert(__position.base(), __gnu_debug::__base(__first),
452 __gnu_debug::__base(__last));
458 #if __cplusplus >= 201103L
459 _M_erase(_Base_const_iterator __position) noexcept
461 _M_erase(_Base_iterator __position)
464 this->_M_invalidate_if(_Equal(__position));
465 return _Base::erase(__position);
470 #if __cplusplus >= 201103L
471 erase(const_iterator __position) noexcept
473 erase(iterator __position)
476 __glibcxx_check_erase(__position);
477 return iterator(_M_erase(__position.base()), this);
481 #if __cplusplus >= 201103L
482 erase(const_iterator __first, const_iterator __last) noexcept
484 erase(iterator __first, iterator __last)
487 // _GLIBCXX_RESOLVE_LIB_DEFECTS
488 // 151. can't currently clear() empty container
489 __glibcxx_check_erase_range(__first, __last);
490 for (_Base_const_iterator __victim = __first.base();
491 __victim != __last.base(); ++__victim)
493 _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
494 _M_message(__gnu_debug::__msg_valid_range)
495 ._M_iterator(__first, "position")
496 ._M_iterator(__last, "last"));
497 this->_M_invalidate_if(_Equal(__victim));
499 return iterator(_Base::erase(__first.base(), __last.base()), this);
504 #if __cplusplus >= 201103L
505 noexcept( noexcept(declval<_Base>().swap(__x)) )
513 clear() _GLIBCXX_NOEXCEPT
516 this->_M_invalidate_all();
519 // 23.2.2.4 list operations:
521 #if __cplusplus >= 201103L
522 splice(const_iterator __position, list&& __x) noexcept
524 splice(iterator __position, list& __x)
527 _GLIBCXX_DEBUG_VERIFY(&__x != this,
528 _M_message(__gnu_debug::__msg_self_splice)
529 ._M_sequence(*this, "this"));
530 this->_M_transfer_from_if(__x, _Not_equal(__x._M_base().end()));
531 _Base::splice(__position.base(), _GLIBCXX_MOVE(__x._M_base()));
534 #if __cplusplus >= 201103L
536 splice(const_iterator __position, list& __x) noexcept
537 { splice(__position, std::move(__x)); }
541 #if __cplusplus >= 201103L
542 splice(const_iterator __position, list&& __x, const_iterator __i) noexcept
544 splice(iterator __position, list& __x, iterator __i)
547 __glibcxx_check_insert(__position);
549 // We used to perform the splice_alloc check: not anymore, redundant
550 // after implementing the relevant bits of N1599.
552 _GLIBCXX_DEBUG_VERIFY(__i._M_dereferenceable(),
553 _M_message(__gnu_debug::__msg_splice_bad)
554 ._M_iterator(__i, "__i"));
555 _GLIBCXX_DEBUG_VERIFY(__i._M_attached_to(&__x),
556 _M_message(__gnu_debug::__msg_splice_other)
557 ._M_iterator(__i, "__i")._M_sequence(__x, "__x"));
559 // _GLIBCXX_RESOLVE_LIB_DEFECTS
560 // 250. splicing invalidates iterators
561 this->_M_transfer_from_if(__x, _Equal(__i.base()));
562 _Base::splice(__position.base(), _GLIBCXX_MOVE(__x._M_base()),
566 #if __cplusplus >= 201103L
568 splice(const_iterator __position, list& __x, const_iterator __i) noexcept
569 { splice(__position, std::move(__x), __i); }
573 #if __cplusplus >= 201103L
574 splice(const_iterator __position, list&& __x, const_iterator __first,
575 const_iterator __last) noexcept
577 splice(iterator __position, list& __x, iterator __first,
581 __glibcxx_check_insert(__position);
582 __glibcxx_check_valid_range(__first, __last);
583 _GLIBCXX_DEBUG_VERIFY(__first._M_attached_to(&__x),
584 _M_message(__gnu_debug::__msg_splice_other)
585 ._M_sequence(__x, "x")
586 ._M_iterator(__first, "first"));
588 // We used to perform the splice_alloc check: not anymore, redundant
589 // after implementing the relevant bits of N1599.
591 for (_Base_const_iterator __tmp = __first.base();
592 __tmp != __last.base(); ++__tmp)
594 _GLIBCXX_DEBUG_VERIFY(__tmp != _Base::end(),
595 _M_message(__gnu_debug::__msg_valid_range)
596 ._M_iterator(__first, "first")
597 ._M_iterator(__last, "last"));
598 _GLIBCXX_DEBUG_VERIFY(&__x != this || __tmp != __position.base(),
599 _M_message(__gnu_debug::__msg_splice_overlap)
600 ._M_iterator(__tmp, "position")
601 ._M_iterator(__first, "first")
602 ._M_iterator(__last, "last"));
603 // _GLIBCXX_RESOLVE_LIB_DEFECTS
604 // 250. splicing invalidates iterators
605 this->_M_transfer_from_if(__x, _Equal(__tmp));
608 _Base::splice(__position.base(), _GLIBCXX_MOVE(__x._M_base()),
609 __first.base(), __last.base());
612 #if __cplusplus >= 201103L
614 splice(const_iterator __position, list& __x,
615 const_iterator __first, const_iterator __last) noexcept
616 { splice(__position, std::move(__x), __first, __last); }
620 remove(const _Tp& __value)
622 for (_Base_iterator __x = _Base::begin(); __x != _Base::end(); )
631 template<class _Predicate>
633 remove_if(_Predicate __pred)
635 for (_Base_iterator __x = _Base::begin(); __x != _Base::end(); )
647 _Base_iterator __first = _Base::begin();
648 _Base_iterator __last = _Base::end();
649 if (__first == __last)
651 _Base_iterator __next = __first; ++__next;
652 while (__next != __last)
654 if (*__first == *__next)
655 __next = _M_erase(__next);
661 template<class _BinaryPredicate>
663 unique(_BinaryPredicate __binary_pred)
665 _Base_iterator __first = _Base::begin();
666 _Base_iterator __last = _Base::end();
667 if (__first == __last)
669 _Base_iterator __next = __first; ++__next;
670 while (__next != __last)
672 if (__binary_pred(*__first, *__next))
673 __next = _M_erase(__next);
680 #if __cplusplus >= 201103L
686 // _GLIBCXX_RESOLVE_LIB_DEFECTS
687 // 300. list::merge() specification incomplete
690 __glibcxx_check_sorted(_Base::begin(), _Base::end());
691 __glibcxx_check_sorted(__x.begin().base(), __x.end().base());
692 this->_M_transfer_from_if(__x, _Not_equal(__x._M_base().end()));
693 _Base::merge(_GLIBCXX_MOVE(__x._M_base()));
697 #if __cplusplus >= 201103L
700 { merge(std::move(__x)); }
703 template<class _Compare>
705 #if __cplusplus >= 201103L
706 merge(list&& __x, _Compare __comp)
708 merge(list& __x, _Compare __comp)
711 // _GLIBCXX_RESOLVE_LIB_DEFECTS
712 // 300. list::merge() specification incomplete
715 __glibcxx_check_sorted_pred(_Base::begin(), _Base::end(),
717 __glibcxx_check_sorted_pred(__x.begin().base(), __x.end().base(),
719 this->_M_transfer_from_if(__x, _Not_equal(__x._M_base().end()));
720 _Base::merge(_GLIBCXX_MOVE(__x._M_base()), __comp);
724 #if __cplusplus >= 201103L
725 template<typename _Compare>
727 merge(list& __x, _Compare __comp)
728 { merge(std::move(__x), __comp); }
732 sort() { _Base::sort(); }
734 template<typename _StrictWeakOrdering>
736 sort(_StrictWeakOrdering __pred) { _Base::sort(__pred); }
738 using _Base::reverse;
741 _M_base() _GLIBCXX_NOEXCEPT { return *this; }
744 _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
747 template<typename _Tp, typename _Alloc>
749 operator==(const list<_Tp, _Alloc>& __lhs,
750 const list<_Tp, _Alloc>& __rhs)
751 { return __lhs._M_base() == __rhs._M_base(); }
753 template<typename _Tp, typename _Alloc>
755 operator!=(const list<_Tp, _Alloc>& __lhs,
756 const list<_Tp, _Alloc>& __rhs)
757 { return __lhs._M_base() != __rhs._M_base(); }
759 template<typename _Tp, typename _Alloc>
761 operator<(const list<_Tp, _Alloc>& __lhs,
762 const list<_Tp, _Alloc>& __rhs)
763 { return __lhs._M_base() < __rhs._M_base(); }
765 template<typename _Tp, typename _Alloc>
767 operator<=(const list<_Tp, _Alloc>& __lhs,
768 const list<_Tp, _Alloc>& __rhs)
769 { return __lhs._M_base() <= __rhs._M_base(); }
771 template<typename _Tp, typename _Alloc>
773 operator>=(const list<_Tp, _Alloc>& __lhs,
774 const list<_Tp, _Alloc>& __rhs)
775 { return __lhs._M_base() >= __rhs._M_base(); }
777 template<typename _Tp, typename _Alloc>
779 operator>(const list<_Tp, _Alloc>& __lhs,
780 const list<_Tp, _Alloc>& __rhs)
781 { return __lhs._M_base() > __rhs._M_base(); }
783 template<typename _Tp, typename _Alloc>
785 swap(list<_Tp, _Alloc>& __lhs, list<_Tp, _Alloc>& __rhs)
786 { __lhs.swap(__rhs); }
788 } // namespace __debug
791 #ifndef _GLIBCXX_DEBUG_PEDANTIC
792 namespace __gnu_debug
794 template<class _Tp, class _Alloc>
795 struct _Insert_range_from_self_is_safe<std::__debug::list<_Tp, _Alloc> >
796 { enum { __value = 1 }; };