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 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); }
127 list(initializer_list<value_type> __l,
128 const allocator_type& __a = allocator_type())
129 : _Base(__l, __a), _Safe_base() { }
135 operator=(const list& __x)
137 static_cast<_Base&>(*this) = __x;
138 this->_M_invalidate_all();
142 #ifdef __GXX_EXPERIMENTAL_CXX0X__
144 operator=(list&& __x)
153 operator=(initializer_list<value_type> __l)
155 static_cast<_Base&>(*this) = __l;
156 this->_M_invalidate_all();
161 assign(initializer_list<value_type> __l)
164 this->_M_invalidate_all();
168 template<class _InputIterator>
170 assign(_InputIterator __first, _InputIterator __last)
172 __glibcxx_check_valid_range(__first, __last);
173 _Base::assign(__first, __last);
174 this->_M_invalidate_all();
178 assign(size_type __n, const _Tp& __t)
180 _Base::assign(__n, __t);
181 this->_M_invalidate_all();
184 using _Base::get_allocator;
189 { return iterator(_Base::begin(), this); }
193 { return const_iterator(_Base::begin(), this); }
197 { return iterator(_Base::end(), this); }
201 { return const_iterator(_Base::end(), this); }
205 { return reverse_iterator(end()); }
207 const_reverse_iterator
209 { return const_reverse_iterator(end()); }
213 { return reverse_iterator(begin()); }
215 const_reverse_iterator
217 { return const_reverse_iterator(begin()); }
219 #ifdef __GXX_EXPERIMENTAL_CXX0X__
222 { return const_iterator(_Base::begin(), this); }
226 { return const_iterator(_Base::end(), this); }
228 const_reverse_iterator
230 { return const_reverse_iterator(end()); }
232 const_reverse_iterator
234 { return const_reverse_iterator(begin()); }
237 // 23.2.2.2 capacity:
240 using _Base::max_size;
243 resize(size_type __sz, _Tp __c = _Tp())
245 this->_M_detach_singular();
247 // if __sz < size(), invalidate all iterators in [begin+__sz, end())
248 iterator __victim = begin();
249 iterator __end = end();
250 for (size_type __i = __sz; __victim != __end && __i > 0; --__i)
253 while (__victim != __end)
255 iterator __real_victim = __victim++;
256 __real_victim._M_invalidate();
261 _Base::resize(__sz, __c);
265 this->_M_revalidate_singular();
266 __throw_exception_again;
274 __glibcxx_check_nonempty();
275 return _Base::front();
281 __glibcxx_check_nonempty();
282 return _Base::front();
288 __glibcxx_check_nonempty();
289 return _Base::back();
295 __glibcxx_check_nonempty();
296 return _Base::back();
299 // 23.2.2.3 modifiers:
300 using _Base::push_front;
302 #ifdef __GXX_EXPERIMENTAL_CXX0X__
303 using _Base::emplace_front;
309 __glibcxx_check_nonempty();
310 iterator __victim = begin();
311 __victim._M_invalidate();
315 using _Base::push_back;
317 #ifdef __GXX_EXPERIMENTAL_CXX0X__
318 using _Base::emplace_back;
324 __glibcxx_check_nonempty();
325 iterator __victim = end();
327 __victim._M_invalidate();
331 #ifdef __GXX_EXPERIMENTAL_CXX0X__
332 template<typename... _Args>
334 emplace(iterator __position, _Args&&... __args)
336 __glibcxx_check_insert(__position);
337 return iterator(_Base::emplace(__position.base(),
338 std::forward<_Args>(__args)...), this);
343 insert(iterator __position, const _Tp& __x)
345 __glibcxx_check_insert(__position);
346 return iterator(_Base::insert(__position.base(), __x), this);
349 #ifdef __GXX_EXPERIMENTAL_CXX0X__
351 insert(iterator __position, _Tp&& __x)
352 { return emplace(__position, std::move(__x)); }
355 insert(iterator __p, initializer_list<value_type> __l)
357 __glibcxx_check_insert(__p);
358 _Base::insert(__p, __l);
363 insert(iterator __position, size_type __n, const _Tp& __x)
365 __glibcxx_check_insert(__position);
366 _Base::insert(__position.base(), __n, __x);
369 template<class _InputIterator>
371 insert(iterator __position, _InputIterator __first,
372 _InputIterator __last)
374 __glibcxx_check_insert_range(__position, __first, __last);
375 _Base::insert(__position.base(), __first, __last);
379 erase(iterator __position)
381 __glibcxx_check_erase(__position);
382 __position._M_invalidate();
383 return iterator(_Base::erase(__position.base()), this);
387 erase(iterator __position, iterator __last)
389 // _GLIBCXX_RESOLVE_LIB_DEFECTS
390 // 151. can't currently clear() empty container
391 __glibcxx_check_erase_range(__position, __last);
392 for (iterator __victim = __position; __victim != __last; )
394 iterator __old = __victim;
396 __old._M_invalidate();
398 return iterator(_Base::erase(__position.base(), __last.base()), this);
402 #ifdef __GXX_EXPERIMENTAL_CXX0X__
416 this->_M_invalidate_all();
419 // 23.2.2.4 list operations:
421 #ifdef __GXX_EXPERIMENTAL_CXX0X__
422 splice(iterator __position, list&& __x)
424 splice(iterator __position, list& __x)
427 _GLIBCXX_DEBUG_VERIFY(&__x != this,
428 _M_message(__gnu_debug::__msg_self_splice)
429 ._M_sequence(*this, "this"));
430 this->splice(__position, _GLIBCXX_MOVE(__x), __x.begin(), __x.end());
434 #ifdef __GXX_EXPERIMENTAL_CXX0X__
435 splice(iterator __position, list&& __x, iterator __i)
437 splice(iterator __position, list& __x, iterator __i)
440 __glibcxx_check_insert(__position);
442 // We used to perform the splice_alloc check: not anymore, redundant
443 // after implementing the relevant bits of N1599.
445 _GLIBCXX_DEBUG_VERIFY(__i._M_dereferenceable(),
446 _M_message(__gnu_debug::__msg_splice_bad)
447 ._M_iterator(__i, "__i"));
448 _GLIBCXX_DEBUG_VERIFY(__i._M_attached_to(&__x),
449 _M_message(__gnu_debug::__msg_splice_other)
450 ._M_iterator(__i, "__i")._M_sequence(__x, "__x"));
452 // _GLIBCXX_RESOLVE_LIB_DEFECTS
453 // 250. splicing invalidates iterators
454 this->_M_transfer_iter(__i);
455 _Base::splice(__position.base(), _GLIBCXX_MOVE(__x._M_base()),
460 #ifdef __GXX_EXPERIMENTAL_CXX0X__
461 splice(iterator __position, list&& __x, iterator __first,
464 splice(iterator __position, list& __x, iterator __first,
468 __glibcxx_check_insert(__position);
469 __glibcxx_check_valid_range(__first, __last);
470 _GLIBCXX_DEBUG_VERIFY(__first._M_attached_to(&__x),
471 _M_message(__gnu_debug::__msg_splice_other)
472 ._M_sequence(__x, "x")
473 ._M_iterator(__first, "first"));
475 // We used to perform the splice_alloc check: not anymore, redundant
476 // after implementing the relevant bits of N1599.
478 for (iterator __tmp = __first; __tmp != __last; )
480 _GLIBCXX_DEBUG_VERIFY(&__x != this || __tmp != __position,
481 _M_message(__gnu_debug::__msg_splice_overlap)
482 ._M_iterator(__tmp, "position")
483 ._M_iterator(__first, "first")
484 ._M_iterator(__last, "last"));
485 iterator __victim = __tmp++;
486 // _GLIBCXX_RESOLVE_LIB_DEFECTS
487 // 250. splicing invalidates iterators
488 this->_M_transfer_iter(__victim);
491 _Base::splice(__position.base(), _GLIBCXX_MOVE(__x._M_base()),
492 __first.base(), __last.base());
496 remove(const _Tp& __value)
498 for (iterator __x = begin(); __x.base() != _Base::end(); )
507 template<class _Predicate>
509 remove_if(_Predicate __pred)
511 for (iterator __x = begin(); __x.base() != _Base::end(); )
523 iterator __first = begin();
524 iterator __last = end();
525 if (__first == __last)
527 iterator __next = __first;
528 while (++__next != __last)
530 if (*__first == *__next)
538 template<class _BinaryPredicate>
540 unique(_BinaryPredicate __binary_pred)
542 iterator __first = begin();
543 iterator __last = end();
544 if (__first == __last)
546 iterator __next = __first;
547 while (++__next != __last)
549 if (__binary_pred(*__first, *__next))
558 #ifdef __GXX_EXPERIMENTAL_CXX0X__
564 // _GLIBCXX_RESOLVE_LIB_DEFECTS
565 // 300. list::merge() specification incomplete
568 __glibcxx_check_sorted(_Base::begin(), _Base::end());
569 __glibcxx_check_sorted(__x.begin().base(), __x.end().base());
570 for (iterator __tmp = __x.begin(); __tmp != __x.end();)
572 iterator __victim = __tmp++;
573 this->_M_transfer_iter(__victim);
575 _Base::merge(_GLIBCXX_MOVE(__x._M_base()));
579 template<class _Compare>
581 #ifdef __GXX_EXPERIMENTAL_CXX0X__
582 merge(list&& __x, _Compare __comp)
584 merge(list& __x, _Compare __comp)
587 // _GLIBCXX_RESOLVE_LIB_DEFECTS
588 // 300. list::merge() specification incomplete
591 __glibcxx_check_sorted_pred(_Base::begin(), _Base::end(),
593 __glibcxx_check_sorted_pred(__x.begin().base(), __x.end().base(),
595 for (iterator __tmp = __x.begin(); __tmp != __x.end();)
597 iterator __victim = __tmp++;
598 this->_M_transfer_iter(__victim);
600 _Base::merge(_GLIBCXX_MOVE(__x._M_base()), __comp);
605 sort() { _Base::sort(); }
607 template<typename _StrictWeakOrdering>
609 sort(_StrictWeakOrdering __pred) { _Base::sort(__pred); }
611 using _Base::reverse;
614 _M_base() { return *this; }
617 _M_base() const { return *this; }
623 typedef typename _Base::const_iterator _Base_const_iterator;
624 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
625 this->_M_invalidate_if(_Not_equal(_M_base().end()));
629 template<typename _Tp, typename _Alloc>
631 operator==(const list<_Tp, _Alloc>& __lhs,
632 const list<_Tp, _Alloc>& __rhs)
633 { return __lhs._M_base() == __rhs._M_base(); }
635 template<typename _Tp, typename _Alloc>
637 operator!=(const list<_Tp, _Alloc>& __lhs,
638 const list<_Tp, _Alloc>& __rhs)
639 { return __lhs._M_base() != __rhs._M_base(); }
641 template<typename _Tp, typename _Alloc>
643 operator<(const list<_Tp, _Alloc>& __lhs,
644 const list<_Tp, _Alloc>& __rhs)
645 { return __lhs._M_base() < __rhs._M_base(); }
647 template<typename _Tp, typename _Alloc>
649 operator<=(const list<_Tp, _Alloc>& __lhs,
650 const list<_Tp, _Alloc>& __rhs)
651 { return __lhs._M_base() <= __rhs._M_base(); }
653 template<typename _Tp, typename _Alloc>
655 operator>=(const list<_Tp, _Alloc>& __lhs,
656 const list<_Tp, _Alloc>& __rhs)
657 { return __lhs._M_base() >= __rhs._M_base(); }
659 template<typename _Tp, typename _Alloc>
661 operator>(const list<_Tp, _Alloc>& __lhs,
662 const list<_Tp, _Alloc>& __rhs)
663 { return __lhs._M_base() > __rhs._M_base(); }
665 template<typename _Tp, typename _Alloc>
667 swap(list<_Tp, _Alloc>& __lhs, list<_Tp, _Alloc>& __rhs)
668 { __lhs.swap(__rhs); }
670 #ifdef __GXX_EXPERIMENTAL_CXX0X__
671 template<typename _Tp, typename _Alloc>
673 swap(list<_Tp, _Alloc>&& __lhs, list<_Tp, _Alloc>& __rhs)
674 { __lhs.swap(__rhs); }
676 template<typename _Tp, typename _Alloc>
678 swap(list<_Tp, _Alloc>& __lhs, list<_Tp, _Alloc>&& __rhs)
679 { __lhs.swap(__rhs); }
682 } // namespace __debug