1 // Debugging list implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007
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 2, 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 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
31 // Free Software Foundation, Inc.
33 // This file is part of the GNU ISO C++ Library. This library is free
34 // software; you can redistribute it and/or modify it under the
35 // terms of the GNU General Public License as published by the
36 // Free Software Foundation; either version 2, or (at your option)
39 // This library is distributed in the hope that it will be useful,
40 // but WITHOUT ANY WARRANTY; without even the implied warranty of
41 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 // GNU General Public License for more details.
44 // You should have received a copy of the GNU General Public License along
45 // with this library; see the file COPYING. If not, write to the Free
46 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
49 // As a special exception, you may use this file as part of a free software
50 // library without restriction. Specifically, if other files instantiate
51 // templates or use macros or inline functions from this file, or you compile
52 // this file and link it with other files to produce an executable, this
53 // file does not by itself cause the resulting executable to be covered by
54 // the GNU General Public License. This exception does not however
55 // invalidate any other reasons why the executable file might be covered by
56 // the GNU General Public License.
59 * This file is a GNU debug extension to the Standard C++ Library.
62 #ifndef _GLIBCXX_DEBUG_LIST
63 #define _GLIBCXX_DEBUG_LIST 1
66 #include <bits/stl_algo.h>
67 #include <debug/safe_sequence.h>
68 #include <debug/safe_iterator.h>
74 template<typename _Tp, typename _Allocator = std::allocator<_Tp> >
76 : public _GLIBCXX_STD_D::list<_Tp, _Allocator>,
77 public __gnu_debug::_Safe_sequence<list<_Tp, _Allocator> >
79 typedef _GLIBCXX_STD_D::list<_Tp, _Allocator> _Base;
80 typedef __gnu_debug::_Safe_sequence<list> _Safe_base;
83 typedef typename _Base::reference reference;
84 typedef typename _Base::const_reference const_reference;
86 typedef __gnu_debug::_Safe_iterator<typename _Base::iterator, list>
88 typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator, list>
91 typedef typename _Base::size_type size_type;
92 typedef typename _Base::difference_type difference_type;
94 typedef _Tp value_type;
95 typedef _Allocator allocator_type;
96 typedef typename _Base::pointer pointer;
97 typedef typename _Base::const_pointer const_pointer;
98 typedef std::reverse_iterator<iterator> reverse_iterator;
99 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
101 // 23.2.2.1 construct/copy/destroy:
102 explicit list(const _Allocator& __a = _Allocator())
105 explicit list(size_type __n, const _Tp& __value = _Tp(),
106 const _Allocator& __a = _Allocator())
107 : _Base(__n, __value, __a) { }
109 template<class _InputIterator>
110 list(_InputIterator __first, _InputIterator __last,
111 const _Allocator& __a = _Allocator())
112 : _Base(__gnu_debug::__check_valid_range(__first, __last), __last, __a)
116 list(const list& __x)
117 : _Base(__x), _Safe_base() { }
119 list(const _Base& __x)
120 : _Base(__x), _Safe_base() { }
122 #ifdef __GXX_EXPERIMENTAL_CXX0X__
124 : _Base(std::forward<list>(__x)), _Safe_base()
125 { this->_M_swap(__x); }
131 operator=(const list& __x)
133 static_cast<_Base&>(*this) = __x;
134 this->_M_invalidate_all();
138 #ifdef __GXX_EXPERIMENTAL_CXX0X__
140 operator=(list&& __x)
149 template<class _InputIterator>
151 assign(_InputIterator __first, _InputIterator __last)
153 __glibcxx_check_valid_range(__first, __last);
154 _Base::assign(__first, __last);
155 this->_M_invalidate_all();
159 assign(size_type __n, const _Tp& __t)
161 _Base::assign(__n, __t);
162 this->_M_invalidate_all();
165 using _Base::get_allocator;
170 { return iterator(_Base::begin(), this); }
174 { return const_iterator(_Base::begin(), this); }
178 { return iterator(_Base::end(), this); }
182 { return const_iterator(_Base::end(), this); }
186 { return reverse_iterator(end()); }
188 const_reverse_iterator
190 { return const_reverse_iterator(end()); }
194 { return reverse_iterator(begin()); }
196 const_reverse_iterator
198 { return const_reverse_iterator(begin()); }
200 #ifdef __GXX_EXPERIMENTAL_CXX0X__
203 { return const_iterator(_Base::begin(), this); }
207 { return const_iterator(_Base::end(), this); }
209 const_reverse_iterator
211 { return const_reverse_iterator(end()); }
213 const_reverse_iterator
215 { return const_reverse_iterator(begin()); }
218 // 23.2.2.2 capacity:
221 using _Base::max_size;
224 resize(size_type __sz, _Tp __c = _Tp())
226 this->_M_detach_singular();
228 // if __sz < size(), invalidate all iterators in [begin+__sz, end())
229 iterator __victim = begin();
230 iterator __end = end();
231 for (size_type __i = __sz; __victim != __end && __i > 0; --__i)
234 while (__victim != __end)
236 iterator __real_victim = __victim++;
237 __real_victim._M_invalidate();
242 _Base::resize(__sz, __c);
246 this->_M_revalidate_singular();
247 __throw_exception_again;
255 __glibcxx_check_nonempty();
256 return _Base::front();
262 __glibcxx_check_nonempty();
263 return _Base::front();
269 __glibcxx_check_nonempty();
270 return _Base::back();
276 __glibcxx_check_nonempty();
277 return _Base::back();
280 // 23.2.2.3 modifiers:
281 using _Base::push_front;
286 __glibcxx_check_nonempty();
287 iterator __victim = begin();
288 __victim._M_invalidate();
292 using _Base::push_back;
297 __glibcxx_check_nonempty();
298 iterator __victim = end();
300 __victim._M_invalidate();
304 #ifdef __GXX_EXPERIMENTAL_CXX0X__
305 template<typename... _Args>
307 emplace(iterator __position, _Args&&... __args)
309 __glibcxx_check_insert(__position);
310 return iterator(_Base::emplace(__position.base(),
311 std::forward<_Args>(__args)...), this);
316 insert(iterator __position, const _Tp& __x)
318 __glibcxx_check_insert(__position);
319 return iterator(_Base::insert(__position.base(), __x), this);
322 #ifdef __GXX_EXPERIMENTAL_CXX0X__
324 insert(iterator __position, _Tp&& __x)
325 { return emplace(__position, std::move(__x)); }
329 insert(iterator __position, size_type __n, const _Tp& __x)
331 __glibcxx_check_insert(__position);
332 _Base::insert(__position.base(), __n, __x);
335 template<class _InputIterator>
337 insert(iterator __position, _InputIterator __first,
338 _InputIterator __last)
340 __glibcxx_check_insert_range(__position, __first, __last);
341 _Base::insert(__position.base(), __first, __last);
345 erase(iterator __position)
347 __glibcxx_check_erase(__position);
348 __position._M_invalidate();
349 return iterator(_Base::erase(__position.base()), this);
353 erase(iterator __position, iterator __last)
355 // _GLIBCXX_RESOLVE_LIB_DEFECTS
356 // 151. can't currently clear() empty container
357 __glibcxx_check_erase_range(__position, __last);
358 for (iterator __victim = __position; __victim != __last; )
360 iterator __old = __victim;
362 __old._M_invalidate();
364 return iterator(_Base::erase(__position.base(), __last.base()), this);
368 #ifdef __GXX_EXPERIMENTAL_CXX0X__
382 this->_M_invalidate_all();
385 // 23.2.2.4 list operations:
387 #ifdef __GXX_EXPERIMENTAL_CXX0X__
388 splice(iterator __position, list&& __x)
390 splice(iterator __position, list& __x)
393 _GLIBCXX_DEBUG_VERIFY(&__x != this,
394 _M_message(__gnu_debug::__msg_self_splice)
395 ._M_sequence(*this, "this"));
396 this->splice(__position, _GLIBCXX_MOVE(__x), __x.begin(), __x.end());
400 #ifdef __GXX_EXPERIMENTAL_CXX0X__
401 splice(iterator __position, list&& __x, iterator __i)
403 splice(iterator __position, list& __x, iterator __i)
406 __glibcxx_check_insert(__position);
408 // We used to perform the splice_alloc check: not anymore, redundant
409 // after implementing the relevant bits of N1599.
411 _GLIBCXX_DEBUG_VERIFY(__i._M_dereferenceable(),
412 _M_message(__gnu_debug::__msg_splice_bad)
413 ._M_iterator(__i, "__i"));
414 _GLIBCXX_DEBUG_VERIFY(__i._M_attached_to(&__x),
415 _M_message(__gnu_debug::__msg_splice_other)
416 ._M_iterator(__i, "__i")._M_sequence(__x, "__x"));
418 // _GLIBCXX_RESOLVE_LIB_DEFECTS
419 // 250. splicing invalidates iterators
420 this->_M_transfer_iter(__i);
421 _Base::splice(__position.base(), _GLIBCXX_MOVE(__x._M_base()),
426 #ifdef __GXX_EXPERIMENTAL_CXX0X__
427 splice(iterator __position, list&& __x, iterator __first,
430 splice(iterator __position, list& __x, iterator __first,
434 __glibcxx_check_insert(__position);
435 __glibcxx_check_valid_range(__first, __last);
436 _GLIBCXX_DEBUG_VERIFY(__first._M_attached_to(&__x),
437 _M_message(__gnu_debug::__msg_splice_other)
438 ._M_sequence(__x, "x")
439 ._M_iterator(__first, "first"));
441 // We used to perform the splice_alloc check: not anymore, redundant
442 // after implementing the relevant bits of N1599.
444 for (iterator __tmp = __first; __tmp != __last; )
446 _GLIBCXX_DEBUG_VERIFY(&__x != this || __tmp != __position,
447 _M_message(__gnu_debug::__msg_splice_overlap)
448 ._M_iterator(__tmp, "position")
449 ._M_iterator(__first, "first")
450 ._M_iterator(__last, "last"));
451 iterator __victim = __tmp++;
452 // _GLIBCXX_RESOLVE_LIB_DEFECTS
453 // 250. splicing invalidates iterators
454 this->_M_transfer_iter(__victim);
457 _Base::splice(__position.base(), _GLIBCXX_MOVE(__x._M_base()),
458 __first.base(), __last.base());
462 remove(const _Tp& __value)
464 for (iterator __x = begin(); __x.base() != _Base::end(); )
473 template<class _Predicate>
475 remove_if(_Predicate __pred)
477 for (iterator __x = begin(); __x.base() != _Base::end(); )
489 iterator __first = begin();
490 iterator __last = end();
491 if (__first == __last)
493 iterator __next = __first;
494 while (++__next != __last)
496 if (*__first == *__next)
504 template<class _BinaryPredicate>
506 unique(_BinaryPredicate __binary_pred)
508 iterator __first = begin();
509 iterator __last = end();
510 if (__first == __last)
512 iterator __next = __first;
513 while (++__next != __last)
515 if (__binary_pred(*__first, *__next))
524 #ifdef __GXX_EXPERIMENTAL_CXX0X__
530 __glibcxx_check_sorted(_Base::begin(), _Base::end());
531 __glibcxx_check_sorted(__x.begin().base(), __x.end().base());
532 for (iterator __tmp = __x.begin(); __tmp != __x.end(); )
534 iterator __victim = __tmp++;
535 __victim._M_attach(&__x);
537 _Base::merge(_GLIBCXX_MOVE(__x._M_base()));
540 template<class _Compare>
542 #ifdef __GXX_EXPERIMENTAL_CXX0X__
543 merge(list&& __x, _Compare __comp)
545 merge(list& __x, _Compare __comp)
548 __glibcxx_check_sorted_pred(_Base::begin(), _Base::end(), __comp);
549 __glibcxx_check_sorted_pred(__x.begin().base(), __x.end().base(),
551 for (iterator __tmp = __x.begin(); __tmp != __x.end(); )
553 iterator __victim = __tmp++;
554 __victim._M_attach(&__x);
556 _Base::merge(_GLIBCXX_MOVE(__x._M_base()), __comp);
560 sort() { _Base::sort(); }
562 template<typename _StrictWeakOrdering>
564 sort(_StrictWeakOrdering __pred) { _Base::sort(__pred); }
566 using _Base::reverse;
569 _M_base() { return *this; }
572 _M_base() const { return *this; }
578 typedef typename _Base::const_iterator _Base_const_iterator;
579 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
580 this->_M_invalidate_if(_Not_equal(_M_base().end()));
584 template<typename _Tp, typename _Alloc>
586 operator==(const list<_Tp, _Alloc>& __lhs,
587 const list<_Tp, _Alloc>& __rhs)
588 { return __lhs._M_base() == __rhs._M_base(); }
590 template<typename _Tp, typename _Alloc>
592 operator!=(const list<_Tp, _Alloc>& __lhs,
593 const list<_Tp, _Alloc>& __rhs)
594 { return __lhs._M_base() != __rhs._M_base(); }
596 template<typename _Tp, typename _Alloc>
598 operator<(const list<_Tp, _Alloc>& __lhs,
599 const list<_Tp, _Alloc>& __rhs)
600 { return __lhs._M_base() < __rhs._M_base(); }
602 template<typename _Tp, typename _Alloc>
604 operator<=(const list<_Tp, _Alloc>& __lhs,
605 const list<_Tp, _Alloc>& __rhs)
606 { return __lhs._M_base() <= __rhs._M_base(); }
608 template<typename _Tp, typename _Alloc>
610 operator>=(const list<_Tp, _Alloc>& __lhs,
611 const list<_Tp, _Alloc>& __rhs)
612 { return __lhs._M_base() >= __rhs._M_base(); }
614 template<typename _Tp, typename _Alloc>
616 operator>(const list<_Tp, _Alloc>& __lhs,
617 const list<_Tp, _Alloc>& __rhs)
618 { return __lhs._M_base() > __rhs._M_base(); }
620 template<typename _Tp, typename _Alloc>
622 swap(list<_Tp, _Alloc>& __lhs, list<_Tp, _Alloc>& __rhs)
623 { __lhs.swap(__rhs); }
625 #ifdef __GXX_EXPERIMENTAL_CXX0X__
626 template<typename _Tp, typename _Alloc>
628 swap(list<_Tp, _Alloc>&& __lhs, list<_Tp, _Alloc>& __rhs)
629 { __lhs.swap(__rhs); }
631 template<typename _Tp, typename _Alloc>
633 swap(list<_Tp, _Alloc>& __lhs, list<_Tp, _Alloc>&& __rhs)
634 { __lhs.swap(__rhs); }
637 } // namespace __debug