1 // Debugging deque 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 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_DEQUE
31 #define _GLIBCXX_DEBUG_DEQUE 1
34 #include <debug/safe_sequence.h>
35 #include <debug/safe_iterator.h>
41 /// Class std::deque with safety/checking/debug instrumentation.
42 template<typename _Tp, typename _Allocator = std::allocator<_Tp> >
44 : public _GLIBCXX_STD_D::deque<_Tp, _Allocator>,
45 public __gnu_debug::_Safe_sequence<deque<_Tp, _Allocator> >
47 typedef _GLIBCXX_STD_D::deque<_Tp, _Allocator> _Base;
48 typedef __gnu_debug::_Safe_sequence<deque> _Safe_base;
51 typedef typename _Base::reference reference;
52 typedef typename _Base::const_reference const_reference;
54 typedef __gnu_debug::_Safe_iterator<typename _Base::iterator,deque>
56 typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,deque>
59 typedef typename _Base::size_type size_type;
60 typedef typename _Base::difference_type difference_type;
62 typedef _Tp value_type;
63 typedef _Allocator allocator_type;
64 typedef typename _Base::pointer pointer;
65 typedef typename _Base::const_pointer const_pointer;
66 typedef std::reverse_iterator<iterator> reverse_iterator;
67 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
69 // 23.2.1.1 construct/copy/destroy:
70 explicit deque(const _Allocator& __a = _Allocator())
73 explicit deque(size_type __n, const _Tp& __value = _Tp(),
74 const _Allocator& __a = _Allocator())
75 : _Base(__n, __value, __a) { }
77 template<class _InputIterator>
78 deque(_InputIterator __first, _InputIterator __last,
79 const _Allocator& __a = _Allocator())
80 : _Base(__gnu_debug::__check_valid_range(__first, __last), __last, __a)
83 deque(const deque& __x)
84 : _Base(__x), _Safe_base() { }
86 deque(const _Base& __x)
87 : _Base(__x), _Safe_base() { }
89 #ifdef __GXX_EXPERIMENTAL_CXX0X__
91 : _Base(std::forward<deque>(__x)), _Safe_base()
92 { this->_M_swap(__x); }
94 deque(initializer_list<value_type> __l,
95 const allocator_type& __a = allocator_type())
96 : _Base(__l, __a), _Safe_base() { }
102 operator=(const deque& __x)
104 *static_cast<_Base*>(this) = __x;
105 this->_M_invalidate_all();
109 #ifdef __GXX_EXPERIMENTAL_CXX0X__
111 operator=(deque&& __x)
120 operator=(initializer_list<value_type> __l)
122 *static_cast<_Base*>(this) = __l;
123 this->_M_invalidate_all();
128 template<class _InputIterator>
130 assign(_InputIterator __first, _InputIterator __last)
132 __glibcxx_check_valid_range(__first, __last);
133 _Base::assign(__first, __last);
134 this->_M_invalidate_all();
138 assign(size_type __n, const _Tp& __t)
140 _Base::assign(__n, __t);
141 this->_M_invalidate_all();
144 #ifdef __GXX_EXPERIMENTAL_CXX0X__
146 assign(initializer_list<value_type> __l)
149 this->_M_invalidate_all();
153 using _Base::get_allocator;
158 { return iterator(_Base::begin(), this); }
162 { return const_iterator(_Base::begin(), this); }
166 { return iterator(_Base::end(), this); }
170 { return const_iterator(_Base::end(), this); }
174 { return reverse_iterator(end()); }
176 const_reverse_iterator
178 { return const_reverse_iterator(end()); }
182 { return reverse_iterator(begin()); }
184 const_reverse_iterator
186 { return const_reverse_iterator(begin()); }
188 #ifdef __GXX_EXPERIMENTAL_CXX0X__
191 { return const_iterator(_Base::begin(), this); }
195 { return const_iterator(_Base::end(), this); }
197 const_reverse_iterator
199 { return const_reverse_iterator(end()); }
201 const_reverse_iterator
203 { return const_reverse_iterator(begin()); }
206 // 23.2.1.2 capacity:
208 using _Base::max_size;
211 resize(size_type __sz, _Tp __c = _Tp())
213 typedef typename _Base::const_iterator _Base_const_iterator;
214 typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth;
216 bool __invalidate_all = __sz > this->size();
217 if (__sz < this->size())
218 this->_M_invalidate_if(_After_nth(__sz, _M_base().begin()));
220 _Base::resize(__sz, __c);
222 if (__invalidate_all)
223 this->_M_invalidate_all();
230 operator[](size_type __n)
232 __glibcxx_check_subscript(__n);
233 return _M_base()[__n];
237 operator[](size_type __n) const
239 __glibcxx_check_subscript(__n);
240 return _M_base()[__n];
248 __glibcxx_check_nonempty();
249 return _Base::front();
255 __glibcxx_check_nonempty();
256 return _Base::front();
262 __glibcxx_check_nonempty();
263 return _Base::back();
269 __glibcxx_check_nonempty();
270 return _Base::back();
273 // 23.2.1.3 modifiers:
275 push_front(const _Tp& __x)
277 _Base::push_front(__x);
278 this->_M_invalidate_all();
282 push_back(const _Tp& __x)
284 _Base::push_back(__x);
285 this->_M_invalidate_all();
288 #ifdef __GXX_EXPERIMENTAL_CXX0X__
290 push_front(_Tp&& __x)
291 { emplace_front(std::move(__x)); }
295 { emplace_back(std::move(__x)); }
297 template<typename... _Args>
299 emplace_front(_Args&&... __args)
301 _Base::emplace_front(std::forward<_Args>(__args)...);
302 this->_M_invalidate_all();
305 template<typename... _Args>
307 emplace_back(_Args&&... __args)
309 _Base::emplace_back(std::forward<_Args>(__args)...);
310 this->_M_invalidate_all();
313 template<typename... _Args>
315 emplace(iterator __position, _Args&&... __args)
317 __glibcxx_check_insert(__position);
318 typename _Base::iterator __res = _Base::emplace(__position.base(),
319 std::forward<_Args>(__args)...);
320 this->_M_invalidate_all();
321 return iterator(__res, this);
326 insert(iterator __position, const _Tp& __x)
328 __glibcxx_check_insert(__position);
329 typename _Base::iterator __res = _Base::insert(__position.base(), __x);
330 this->_M_invalidate_all();
331 return iterator(__res, this);
334 #ifdef __GXX_EXPERIMENTAL_CXX0X__
336 insert(iterator __position, _Tp&& __x)
337 { return emplace(__position, std::move(__x)); }
340 insert(iterator __p, initializer_list<value_type> __l)
342 _Base::insert(__p, __l);
343 this->_M_invalidate_all();
348 insert(iterator __position, size_type __n, const _Tp& __x)
350 __glibcxx_check_insert(__position);
351 _Base::insert(__position.base(), __n, __x);
352 this->_M_invalidate_all();
355 template<class _InputIterator>
357 insert(iterator __position,
358 _InputIterator __first, _InputIterator __last)
360 __glibcxx_check_insert_range(__position, __first, __last);
361 _Base::insert(__position.base(), __first, __last);
362 this->_M_invalidate_all();
368 __glibcxx_check_nonempty();
369 iterator __victim = begin();
370 __victim._M_invalidate();
377 __glibcxx_check_nonempty();
378 iterator __victim = end();
380 __victim._M_invalidate();
385 erase(iterator __position)
387 __glibcxx_check_erase(__position);
388 if (__position == begin() || __position == end()-1)
390 __position._M_invalidate();
391 return iterator(_Base::erase(__position.base()), this);
395 typename _Base::iterator __res = _Base::erase(__position.base());
396 this->_M_invalidate_all();
397 return iterator(__res, this);
402 erase(iterator __first, iterator __last)
404 // _GLIBCXX_RESOLVE_LIB_DEFECTS
405 // 151. can't currently clear() empty container
406 __glibcxx_check_erase_range(__first, __last);
407 if (__first == begin() || __last == end())
409 this->_M_detach_singular();
410 for (iterator __position = __first; __position != __last; )
412 iterator __victim = __position++;
413 __victim._M_invalidate();
417 return iterator(_Base::erase(__first.base(), __last.base()),
422 this->_M_revalidate_singular();
423 __throw_exception_again;
428 typename _Base::iterator __res = _Base::erase(__first.base(),
430 this->_M_invalidate_all();
431 return iterator(__res, this);
446 this->_M_invalidate_all();
450 _M_base() { return *this; }
453 _M_base() const { return *this; }
456 template<typename _Tp, typename _Alloc>
458 operator==(const deque<_Tp, _Alloc>& __lhs,
459 const deque<_Tp, _Alloc>& __rhs)
460 { return __lhs._M_base() == __rhs._M_base(); }
462 template<typename _Tp, typename _Alloc>
464 operator!=(const deque<_Tp, _Alloc>& __lhs,
465 const deque<_Tp, _Alloc>& __rhs)
466 { return __lhs._M_base() != __rhs._M_base(); }
468 template<typename _Tp, typename _Alloc>
470 operator<(const deque<_Tp, _Alloc>& __lhs,
471 const deque<_Tp, _Alloc>& __rhs)
472 { return __lhs._M_base() < __rhs._M_base(); }
474 template<typename _Tp, typename _Alloc>
476 operator<=(const deque<_Tp, _Alloc>& __lhs,
477 const deque<_Tp, _Alloc>& __rhs)
478 { return __lhs._M_base() <= __rhs._M_base(); }
480 template<typename _Tp, typename _Alloc>
482 operator>=(const deque<_Tp, _Alloc>& __lhs,
483 const deque<_Tp, _Alloc>& __rhs)
484 { return __lhs._M_base() >= __rhs._M_base(); }
486 template<typename _Tp, typename _Alloc>
488 operator>(const deque<_Tp, _Alloc>& __lhs,
489 const deque<_Tp, _Alloc>& __rhs)
490 { return __lhs._M_base() > __rhs._M_base(); }
492 template<typename _Tp, typename _Alloc>
494 swap(deque<_Tp, _Alloc>& __lhs, deque<_Tp, _Alloc>& __rhs)
495 { __lhs.swap(__rhs); }
497 } // namespace __debug