1 // Debugging deque implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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)
121 operator=(initializer_list<value_type> __l)
123 *static_cast<_Base*>(this) = __l;
124 this->_M_invalidate_all();
129 template<class _InputIterator>
131 assign(_InputIterator __first, _InputIterator __last)
133 __glibcxx_check_valid_range(__first, __last);
134 _Base::assign(__first, __last);
135 this->_M_invalidate_all();
139 assign(size_type __n, const _Tp& __t)
141 _Base::assign(__n, __t);
142 this->_M_invalidate_all();
145 #ifdef __GXX_EXPERIMENTAL_CXX0X__
147 assign(initializer_list<value_type> __l)
150 this->_M_invalidate_all();
154 using _Base::get_allocator;
159 { return iterator(_Base::begin(), this); }
163 { return const_iterator(_Base::begin(), this); }
167 { return iterator(_Base::end(), this); }
171 { return const_iterator(_Base::end(), this); }
175 { return reverse_iterator(end()); }
177 const_reverse_iterator
179 { return const_reverse_iterator(end()); }
183 { return reverse_iterator(begin()); }
185 const_reverse_iterator
187 { return const_reverse_iterator(begin()); }
189 #ifdef __GXX_EXPERIMENTAL_CXX0X__
192 { return const_iterator(_Base::begin(), this); }
196 { return const_iterator(_Base::end(), this); }
198 const_reverse_iterator
200 { return const_reverse_iterator(end()); }
202 const_reverse_iterator
204 { return const_reverse_iterator(begin()); }
207 // 23.2.1.2 capacity:
209 using _Base::max_size;
212 resize(size_type __sz, _Tp __c = _Tp())
214 typedef typename _Base::const_iterator _Base_const_iterator;
215 typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth;
217 bool __invalidate_all = __sz > this->size();
218 if (__sz < this->size())
219 this->_M_invalidate_if(_After_nth(__sz, _M_base().begin()));
221 _Base::resize(__sz, __c);
223 if (__invalidate_all)
224 this->_M_invalidate_all();
227 #ifdef __GXX_EXPERIMENTAL_CXX0X__
228 using _Base::shrink_to_fit;
235 operator[](size_type __n)
237 __glibcxx_check_subscript(__n);
238 return _M_base()[__n];
242 operator[](size_type __n) const
244 __glibcxx_check_subscript(__n);
245 return _M_base()[__n];
253 __glibcxx_check_nonempty();
254 return _Base::front();
260 __glibcxx_check_nonempty();
261 return _Base::front();
267 __glibcxx_check_nonempty();
268 return _Base::back();
274 __glibcxx_check_nonempty();
275 return _Base::back();
278 // 23.2.1.3 modifiers:
280 push_front(const _Tp& __x)
282 _Base::push_front(__x);
283 this->_M_invalidate_all();
287 push_back(const _Tp& __x)
289 _Base::push_back(__x);
290 this->_M_invalidate_all();
293 #ifdef __GXX_EXPERIMENTAL_CXX0X__
295 push_front(_Tp&& __x)
296 { emplace_front(std::move(__x)); }
300 { emplace_back(std::move(__x)); }
302 template<typename... _Args>
304 emplace_front(_Args&&... __args)
306 _Base::emplace_front(std::forward<_Args>(__args)...);
307 this->_M_invalidate_all();
310 template<typename... _Args>
312 emplace_back(_Args&&... __args)
314 _Base::emplace_back(std::forward<_Args>(__args)...);
315 this->_M_invalidate_all();
318 template<typename... _Args>
320 emplace(iterator __position, _Args&&... __args)
322 __glibcxx_check_insert(__position);
323 typename _Base::iterator __res = _Base::emplace(__position.base(),
324 std::forward<_Args>(__args)...);
325 this->_M_invalidate_all();
326 return iterator(__res, this);
331 insert(iterator __position, const _Tp& __x)
333 __glibcxx_check_insert(__position);
334 typename _Base::iterator __res = _Base::insert(__position.base(), __x);
335 this->_M_invalidate_all();
336 return iterator(__res, this);
339 #ifdef __GXX_EXPERIMENTAL_CXX0X__
341 insert(iterator __position, _Tp&& __x)
342 { return emplace(__position, std::move(__x)); }
345 insert(iterator __p, initializer_list<value_type> __l)
347 _Base::insert(__p, __l);
348 this->_M_invalidate_all();
353 insert(iterator __position, size_type __n, const _Tp& __x)
355 __glibcxx_check_insert(__position);
356 _Base::insert(__position.base(), __n, __x);
357 this->_M_invalidate_all();
360 template<class _InputIterator>
362 insert(iterator __position,
363 _InputIterator __first, _InputIterator __last)
365 __glibcxx_check_insert_range(__position, __first, __last);
366 _Base::insert(__position.base(), __first, __last);
367 this->_M_invalidate_all();
373 __glibcxx_check_nonempty();
374 iterator __victim = begin();
375 __victim._M_invalidate();
382 __glibcxx_check_nonempty();
383 iterator __victim = end();
385 __victim._M_invalidate();
390 erase(iterator __position)
392 __glibcxx_check_erase(__position);
393 if (__position == begin() || __position == end()-1)
395 __position._M_invalidate();
396 return iterator(_Base::erase(__position.base()), this);
400 typename _Base::iterator __res = _Base::erase(__position.base());
401 this->_M_invalidate_all();
402 return iterator(__res, this);
407 erase(iterator __first, iterator __last)
409 // _GLIBCXX_RESOLVE_LIB_DEFECTS
410 // 151. can't currently clear() empty container
411 __glibcxx_check_erase_range(__first, __last);
412 if (__first == begin() || __last == end())
414 this->_M_detach_singular();
415 for (iterator __position = __first; __position != __last; )
417 iterator __victim = __position++;
418 __victim._M_invalidate();
422 return iterator(_Base::erase(__first.base(), __last.base()),
427 this->_M_revalidate_singular();
428 __throw_exception_again;
433 typename _Base::iterator __res = _Base::erase(__first.base(),
435 this->_M_invalidate_all();
436 return iterator(__res, this);
451 this->_M_invalidate_all();
455 _M_base() { return *this; }
458 _M_base() const { return *this; }
461 template<typename _Tp, typename _Alloc>
463 operator==(const deque<_Tp, _Alloc>& __lhs,
464 const deque<_Tp, _Alloc>& __rhs)
465 { return __lhs._M_base() == __rhs._M_base(); }
467 template<typename _Tp, typename _Alloc>
469 operator!=(const deque<_Tp, _Alloc>& __lhs,
470 const deque<_Tp, _Alloc>& __rhs)
471 { return __lhs._M_base() != __rhs._M_base(); }
473 template<typename _Tp, typename _Alloc>
475 operator<(const deque<_Tp, _Alloc>& __lhs,
476 const deque<_Tp, _Alloc>& __rhs)
477 { return __lhs._M_base() < __rhs._M_base(); }
479 template<typename _Tp, typename _Alloc>
481 operator<=(const deque<_Tp, _Alloc>& __lhs,
482 const deque<_Tp, _Alloc>& __rhs)
483 { return __lhs._M_base() <= __rhs._M_base(); }
485 template<typename _Tp, typename _Alloc>
487 operator>=(const deque<_Tp, _Alloc>& __lhs,
488 const deque<_Tp, _Alloc>& __rhs)
489 { return __lhs._M_base() >= __rhs._M_base(); }
491 template<typename _Tp, typename _Alloc>
493 operator>(const deque<_Tp, _Alloc>& __lhs,
494 const deque<_Tp, _Alloc>& __rhs)
495 { return __lhs._M_base() > __rhs._M_base(); }
497 template<typename _Tp, typename _Alloc>
499 swap(deque<_Tp, _Alloc>& __lhs, deque<_Tp, _Alloc>& __rhs)
500 { __lhs.swap(__rhs); }
502 } // namespace __debug