1 // Debugging list implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006
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::list<_Tp, _Allocator>,
77 public __gnu_debug::_Safe_sequence<list<_Tp, _Allocator> >
79 typedef _GLIBCXX_STD::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) : _Base(__x), _Safe_base() { }
118 list(const _Base& __x) : _Base(__x), _Safe_base() { }
123 operator=(const list& __x)
125 static_cast<_Base&>(*this) = __x;
126 this->_M_invalidate_all();
130 template<class _InputIterator>
132 assign(_InputIterator __first, _InputIterator __last)
134 __glibcxx_check_valid_range(__first, __last);
135 _Base::assign(__first, __last);
136 this->_M_invalidate_all();
140 assign(size_type __n, const _Tp& __t)
142 _Base::assign(__n, __t);
143 this->_M_invalidate_all();
146 using _Base::get_allocator;
151 { return iterator(_Base::begin(), this); }
155 { return const_iterator(_Base::begin(), this); }
159 { return iterator(_Base::end(), this); }
163 { return const_iterator(_Base::end(), this); }
167 { return reverse_iterator(end()); }
169 const_reverse_iterator
171 { return const_reverse_iterator(end()); }
175 { return reverse_iterator(begin()); }
177 const_reverse_iterator
179 { return const_reverse_iterator(begin()); }
181 // 23.2.2.2 capacity:
184 using _Base::max_size;
187 resize(size_type __sz, _Tp __c = _Tp())
189 this->_M_detach_singular();
191 // if __sz < size(), invalidate all iterators in [begin+__sz, end())
192 iterator __victim = begin();
193 iterator __end = end();
194 for (size_type __i = __sz; __victim != __end && __i > 0; --__i)
197 while (__victim != __end)
199 iterator __real_victim = __victim++;
200 __real_victim._M_invalidate();
205 _Base::resize(__sz, __c);
209 this->_M_revalidate_singular();
210 __throw_exception_again;
218 __glibcxx_check_nonempty();
219 return _Base::front();
225 __glibcxx_check_nonempty();
226 return _Base::front();
232 __glibcxx_check_nonempty();
233 return _Base::back();
239 __glibcxx_check_nonempty();
240 return _Base::back();
243 // 23.2.2.3 modifiers:
244 using _Base::push_front;
249 __glibcxx_check_nonempty();
250 iterator __victim = begin();
251 __victim._M_invalidate();
255 using _Base::push_back;
260 __glibcxx_check_nonempty();
261 iterator __victim = end();
263 __victim._M_invalidate();
268 insert(iterator __position, const _Tp& __x)
270 __glibcxx_check_insert(__position);
271 return iterator(_Base::insert(__position.base(), __x), this);
275 insert(iterator __position, size_type __n, const _Tp& __x)
277 __glibcxx_check_insert(__position);
278 _Base::insert(__position.base(), __n, __x);
281 template<class _InputIterator>
283 insert(iterator __position, _InputIterator __first,
284 _InputIterator __last)
286 __glibcxx_check_insert_range(__position, __first, __last);
287 _Base::insert(__position.base(), __first, __last);
291 erase(iterator __position)
293 __glibcxx_check_erase(__position);
294 __position._M_invalidate();
295 return iterator(_Base::erase(__position.base()), this);
299 erase(iterator __position, iterator __last)
301 // _GLIBCXX_RESOLVE_LIB_DEFECTS
302 // 151. can't currently clear() empty container
303 __glibcxx_check_erase_range(__position, __last);
304 for (iterator __victim = __position; __victim != __last; )
306 iterator __old = __victim;
308 __old._M_invalidate();
310 return iterator(_Base::erase(__position.base(), __last.base()), this);
324 this->_M_invalidate_all();
327 // 23.2.2.4 list operations:
329 splice(iterator __position, list& __x)
331 _GLIBCXX_DEBUG_VERIFY(&__x != this,
332 _M_message(__gnu_debug::__msg_self_splice)
333 ._M_sequence(*this, "this"));
334 this->splice(__position, __x, __x.begin(), __x.end());
338 splice(iterator __position, list& __x, iterator __i)
340 __glibcxx_check_insert(__position);
342 // We used to perform the splice_alloc check: not anymore, redundant
343 // after implementing the relevant bits of N1599.
345 _GLIBCXX_DEBUG_VERIFY(__i._M_dereferenceable(),
346 _M_message(__gnu_debug::__msg_splice_bad)
347 ._M_iterator(__i, "__i"));
348 _GLIBCXX_DEBUG_VERIFY(__i._M_attached_to(&__x),
349 _M_message(__gnu_debug::__msg_splice_other)
350 ._M_iterator(__i, "__i")._M_sequence(__x, "__x"));
352 // _GLIBCXX_RESOLVE_LIB_DEFECTS
353 // 250. splicing invalidates iterators
354 this->_M_transfer_iter(__i);
355 _Base::splice(__position.base(), __x._M_base(), __i.base());
359 splice(iterator __position, list& __x, iterator __first, iterator __last)
361 __glibcxx_check_insert(__position);
362 __glibcxx_check_valid_range(__first, __last);
363 _GLIBCXX_DEBUG_VERIFY(__first._M_attached_to(&__x),
364 _M_message(__gnu_debug::__msg_splice_other)
365 ._M_sequence(__x, "x")
366 ._M_iterator(__first, "first"));
368 // We used to perform the splice_alloc check: not anymore, redundant
369 // after implementing the relevant bits of N1599.
371 for (iterator __tmp = __first; __tmp != __last; )
373 _GLIBCXX_DEBUG_VERIFY(&__x != this || __tmp != __position,
374 _M_message(__gnu_debug::__msg_splice_overlap)
375 ._M_iterator(__tmp, "position")
376 ._M_iterator(__first, "first")
377 ._M_iterator(__last, "last"));
378 iterator __victim = __tmp++;
379 // _GLIBCXX_RESOLVE_LIB_DEFECTS
380 // 250. splicing invalidates iterators
381 this->_M_transfer_iter(__victim);
384 _Base::splice(__position.base(), __x._M_base(), __first.base(),
389 remove(const _Tp& __value)
391 for (iterator __x = begin(); __x.base() != _Base::end(); )
400 template<class _Predicate>
402 remove_if(_Predicate __pred)
404 for (iterator __x = begin(); __x.base() != _Base::end(); )
416 iterator __first = begin();
417 iterator __last = end();
418 if (__first == __last)
420 iterator __next = __first;
421 while (++__next != __last)
423 if (*__first == *__next)
431 template<class _BinaryPredicate>
433 unique(_BinaryPredicate __binary_pred)
435 iterator __first = begin();
436 iterator __last = end();
437 if (__first == __last)
439 iterator __next = __first;
440 while (++__next != __last)
442 if (__binary_pred(*__first, *__next))
453 __glibcxx_check_sorted(_Base::begin(), _Base::end());
454 __glibcxx_check_sorted(__x.begin().base(), __x.end().base());
455 for (iterator __tmp = __x.begin(); __tmp != __x.end(); )
457 iterator __victim = __tmp++;
458 __victim._M_attach(&__x);
460 _Base::merge(__x._M_base());
463 template<class _Compare>
465 merge(list& __x, _Compare __comp)
467 __glibcxx_check_sorted_pred(_Base::begin(), _Base::end(), __comp);
468 __glibcxx_check_sorted_pred(__x.begin().base(), __x.end().base(),
470 for (iterator __tmp = __x.begin(); __tmp != __x.end(); )
472 iterator __victim = __tmp++;
473 __victim._M_attach(&__x);
475 _Base::merge(__x._M_base(), __comp);
479 sort() { _Base::sort(); }
481 template<typename _StrictWeakOrdering>
483 sort(_StrictWeakOrdering __pred) { _Base::sort(__pred); }
485 using _Base::reverse;
488 _M_base() { return *this; }
491 _M_base() const { return *this; }
497 typedef typename _Base::const_iterator _Base_const_iterator;
498 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
499 this->_M_invalidate_if(_Not_equal(_M_base().end()));
503 template<typename _Tp, typename _Alloc>
505 operator==(const list<_Tp, _Alloc>& __lhs, const list<_Tp, _Alloc>& __rhs)
506 { return __lhs._M_base() == __rhs._M_base(); }
508 template<typename _Tp, typename _Alloc>
510 operator!=(const list<_Tp, _Alloc>& __lhs, const list<_Tp, _Alloc>& __rhs)
511 { return __lhs._M_base() != __rhs._M_base(); }
513 template<typename _Tp, typename _Alloc>
515 operator<(const list<_Tp, _Alloc>& __lhs, const list<_Tp, _Alloc>& __rhs)
516 { return __lhs._M_base() < __rhs._M_base(); }
518 template<typename _Tp, typename _Alloc>
520 operator<=(const list<_Tp, _Alloc>& __lhs, const list<_Tp, _Alloc>& __rhs)
521 { return __lhs._M_base() <= __rhs._M_base(); }
523 template<typename _Tp, typename _Alloc>
525 operator>=(const list<_Tp, _Alloc>& __lhs, const list<_Tp, _Alloc>& __rhs)
526 { return __lhs._M_base() >= __rhs._M_base(); }
528 template<typename _Tp, typename _Alloc>
530 operator>(const list<_Tp, _Alloc>& __lhs, const list<_Tp, _Alloc>& __rhs)
531 { return __lhs._M_base() > __rhs._M_base(); }
533 template<typename _Tp, typename _Alloc>
535 swap(list<_Tp, _Alloc>& __lhs, list<_Tp, _Alloc>& __rhs)
536 { __lhs.swap(__rhs); }
537 } // namespace __debug