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:
71 deque(const _Allocator& __a = _Allocator())
74 #ifdef __GXX_EXPERIMENTAL_CXX0X__
79 deque(size_type __n, const _Tp& __value,
80 const _Allocator& __a = _Allocator())
81 : _Base(__n, __value, __a) { }
84 deque(size_type __n, const _Tp& __value = _Tp(),
85 const _Allocator& __a = _Allocator())
86 : _Base(__n, __value, __a) { }
89 template<class _InputIterator>
90 deque(_InputIterator __first, _InputIterator __last,
91 const _Allocator& __a = _Allocator())
92 : _Base(__gnu_debug::__check_valid_range(__first, __last), __last, __a)
95 deque(const deque& __x)
96 : _Base(__x), _Safe_base() { }
98 deque(const _Base& __x)
99 : _Base(__x), _Safe_base() { }
101 #ifdef __GXX_EXPERIMENTAL_CXX0X__
103 : _Base(std::move(__x)), _Safe_base()
104 { this->_M_swap(__x); }
106 deque(initializer_list<value_type> __l,
107 const allocator_type& __a = allocator_type())
108 : _Base(__l, __a), _Safe_base() { }
114 operator=(const deque& __x)
116 *static_cast<_Base*>(this) = __x;
117 this->_M_invalidate_all();
121 #ifdef __GXX_EXPERIMENTAL_CXX0X__
123 operator=(deque&& __x)
133 operator=(initializer_list<value_type> __l)
135 *static_cast<_Base*>(this) = __l;
136 this->_M_invalidate_all();
141 template<class _InputIterator>
143 assign(_InputIterator __first, _InputIterator __last)
145 __glibcxx_check_valid_range(__first, __last);
146 _Base::assign(__first, __last);
147 this->_M_invalidate_all();
151 assign(size_type __n, const _Tp& __t)
153 _Base::assign(__n, __t);
154 this->_M_invalidate_all();
157 #ifdef __GXX_EXPERIMENTAL_CXX0X__
159 assign(initializer_list<value_type> __l)
162 this->_M_invalidate_all();
166 using _Base::get_allocator;
171 { return iterator(_Base::begin(), this); }
175 { return const_iterator(_Base::begin(), this); }
179 { return iterator(_Base::end(), this); }
183 { return const_iterator(_Base::end(), this); }
187 { return reverse_iterator(end()); }
189 const_reverse_iterator
191 { return const_reverse_iterator(end()); }
195 { return reverse_iterator(begin()); }
197 const_reverse_iterator
199 { return const_reverse_iterator(begin()); }
201 #ifdef __GXX_EXPERIMENTAL_CXX0X__
204 { return const_iterator(_Base::begin(), this); }
208 { return const_iterator(_Base::end(), this); }
210 const_reverse_iterator
212 { return const_reverse_iterator(end()); }
214 const_reverse_iterator
216 { return const_reverse_iterator(begin()); }
219 // 23.2.1.2 capacity:
221 using _Base::max_size;
223 #ifdef __GXX_EXPERIMENTAL_CXX0X__
225 resize(size_type __sz)
227 typedef typename _Base::const_iterator _Base_const_iterator;
228 typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth;
230 bool __invalidate_all = __sz > this->size();
231 if (__sz < this->size())
232 this->_M_invalidate_if(_After_nth(__sz, _M_base().begin()));
236 if (__invalidate_all)
237 this->_M_invalidate_all();
241 resize(size_type __sz, const _Tp& __c)
243 typedef typename _Base::const_iterator _Base_const_iterator;
244 typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth;
246 bool __invalidate_all = __sz > this->size();
247 if (__sz < this->size())
248 this->_M_invalidate_if(_After_nth(__sz, _M_base().begin()));
250 _Base::resize(__sz, __c);
252 if (__invalidate_all)
253 this->_M_invalidate_all();
257 resize(size_type __sz, _Tp __c = _Tp())
259 typedef typename _Base::const_iterator _Base_const_iterator;
260 typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth;
262 bool __invalidate_all = __sz > this->size();
263 if (__sz < this->size())
264 this->_M_invalidate_if(_After_nth(__sz, _M_base().begin()));
266 _Base::resize(__sz, __c);
268 if (__invalidate_all)
269 this->_M_invalidate_all();
273 #ifdef __GXX_EXPERIMENTAL_CXX0X__
274 using _Base::shrink_to_fit;
281 operator[](size_type __n)
283 __glibcxx_check_subscript(__n);
284 return _M_base()[__n];
288 operator[](size_type __n) const
290 __glibcxx_check_subscript(__n);
291 return _M_base()[__n];
299 __glibcxx_check_nonempty();
300 return _Base::front();
306 __glibcxx_check_nonempty();
307 return _Base::front();
313 __glibcxx_check_nonempty();
314 return _Base::back();
320 __glibcxx_check_nonempty();
321 return _Base::back();
324 // 23.2.1.3 modifiers:
326 push_front(const _Tp& __x)
328 _Base::push_front(__x);
329 this->_M_invalidate_all();
333 push_back(const _Tp& __x)
335 _Base::push_back(__x);
336 this->_M_invalidate_all();
339 #ifdef __GXX_EXPERIMENTAL_CXX0X__
341 push_front(_Tp&& __x)
342 { emplace_front(std::move(__x)); }
346 { emplace_back(std::move(__x)); }
348 template<typename... _Args>
350 emplace_front(_Args&&... __args)
352 _Base::emplace_front(std::forward<_Args>(__args)...);
353 this->_M_invalidate_all();
356 template<typename... _Args>
358 emplace_back(_Args&&... __args)
360 _Base::emplace_back(std::forward<_Args>(__args)...);
361 this->_M_invalidate_all();
364 template<typename... _Args>
366 emplace(iterator __position, _Args&&... __args)
368 __glibcxx_check_insert(__position);
369 typename _Base::iterator __res = _Base::emplace(__position.base(),
370 std::forward<_Args>(__args)...);
371 this->_M_invalidate_all();
372 return iterator(__res, this);
377 insert(iterator __position, const _Tp& __x)
379 __glibcxx_check_insert(__position);
380 typename _Base::iterator __res = _Base::insert(__position.base(), __x);
381 this->_M_invalidate_all();
382 return iterator(__res, this);
385 #ifdef __GXX_EXPERIMENTAL_CXX0X__
387 insert(iterator __position, _Tp&& __x)
388 { return emplace(__position, std::move(__x)); }
391 insert(iterator __p, initializer_list<value_type> __l)
393 _Base::insert(__p, __l);
394 this->_M_invalidate_all();
399 insert(iterator __position, size_type __n, const _Tp& __x)
401 __glibcxx_check_insert(__position);
402 _Base::insert(__position.base(), __n, __x);
403 this->_M_invalidate_all();
406 template<class _InputIterator>
408 insert(iterator __position,
409 _InputIterator __first, _InputIterator __last)
411 __glibcxx_check_insert_range(__position, __first, __last);
412 _Base::insert(__position.base(), __first, __last);
413 this->_M_invalidate_all();
419 __glibcxx_check_nonempty();
420 iterator __victim = begin();
421 __victim._M_invalidate();
428 __glibcxx_check_nonempty();
429 iterator __victim = end();
431 __victim._M_invalidate();
436 erase(iterator __position)
438 __glibcxx_check_erase(__position);
439 if (__position == begin() || __position == end()-1)
441 __position._M_invalidate();
442 return iterator(_Base::erase(__position.base()), this);
446 typename _Base::iterator __res = _Base::erase(__position.base());
447 this->_M_invalidate_all();
448 return iterator(__res, this);
453 erase(iterator __first, iterator __last)
455 // _GLIBCXX_RESOLVE_LIB_DEFECTS
456 // 151. can't currently clear() empty container
457 __glibcxx_check_erase_range(__first, __last);
458 if (__first == begin() || __last == end())
460 this->_M_detach_singular();
461 for (iterator __position = __first; __position != __last; )
463 iterator __victim = __position++;
464 __victim._M_invalidate();
468 return iterator(_Base::erase(__first.base(), __last.base()),
473 this->_M_revalidate_singular();
474 __throw_exception_again;
479 typename _Base::iterator __res = _Base::erase(__first.base(),
481 this->_M_invalidate_all();
482 return iterator(__res, this);
497 this->_M_invalidate_all();
501 _M_base() { return *this; }
504 _M_base() const { return *this; }
507 template<typename _Tp, typename _Alloc>
509 operator==(const deque<_Tp, _Alloc>& __lhs,
510 const deque<_Tp, _Alloc>& __rhs)
511 { return __lhs._M_base() == __rhs._M_base(); }
513 template<typename _Tp, typename _Alloc>
515 operator!=(const deque<_Tp, _Alloc>& __lhs,
516 const deque<_Tp, _Alloc>& __rhs)
517 { return __lhs._M_base() != __rhs._M_base(); }
519 template<typename _Tp, typename _Alloc>
521 operator<(const deque<_Tp, _Alloc>& __lhs,
522 const deque<_Tp, _Alloc>& __rhs)
523 { return __lhs._M_base() < __rhs._M_base(); }
525 template<typename _Tp, typename _Alloc>
527 operator<=(const deque<_Tp, _Alloc>& __lhs,
528 const deque<_Tp, _Alloc>& __rhs)
529 { return __lhs._M_base() <= __rhs._M_base(); }
531 template<typename _Tp, typename _Alloc>
533 operator>=(const deque<_Tp, _Alloc>& __lhs,
534 const deque<_Tp, _Alloc>& __rhs)
535 { return __lhs._M_base() >= __rhs._M_base(); }
537 template<typename _Tp, typename _Alloc>
539 operator>(const deque<_Tp, _Alloc>& __lhs,
540 const deque<_Tp, _Alloc>& __rhs)
541 { return __lhs._M_base() > __rhs._M_base(); }
543 template<typename _Tp, typename _Alloc>
545 swap(deque<_Tp, _Alloc>& __lhs, deque<_Tp, _Alloc>& __rhs)
546 { __lhs.swap(__rhs); }
548 } // namespace __debug