1 // Debugging deque implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007
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.
32 * This file is a GNU debug extension to the Standard C++ Library.
35 #ifndef _GLIBCXX_DEBUG_DEQUE
36 #define _GLIBCXX_DEBUG_DEQUE 1
39 #include <debug/safe_sequence.h>
40 #include <debug/safe_iterator.h>
46 template<typename _Tp, typename _Allocator = std::allocator<_Tp> >
48 : public _GLIBCXX_STD_D::deque<_Tp, _Allocator>,
49 public __gnu_debug::_Safe_sequence<deque<_Tp, _Allocator> >
51 typedef _GLIBCXX_STD_D::deque<_Tp, _Allocator> _Base;
52 typedef __gnu_debug::_Safe_sequence<deque> _Safe_base;
55 typedef typename _Base::reference reference;
56 typedef typename _Base::const_reference const_reference;
58 typedef __gnu_debug::_Safe_iterator<typename _Base::iterator,deque>
60 typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,deque>
63 typedef typename _Base::size_type size_type;
64 typedef typename _Base::difference_type difference_type;
66 typedef _Tp value_type;
67 typedef _Allocator allocator_type;
68 typedef typename _Base::pointer pointer;
69 typedef typename _Base::const_pointer const_pointer;
70 typedef std::reverse_iterator<iterator> reverse_iterator;
71 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
73 // 23.2.1.1 construct/copy/destroy:
74 explicit deque(const _Allocator& __a = _Allocator())
77 explicit deque(size_type __n, const _Tp& __value = _Tp(),
78 const _Allocator& __a = _Allocator())
79 : _Base(__n, __value, __a) { }
81 template<class _InputIterator>
82 deque(_InputIterator __first, _InputIterator __last,
83 const _Allocator& __a = _Allocator())
84 : _Base(__gnu_debug::__check_valid_range(__first, __last), __last, __a)
87 deque(const deque& __x)
88 : _Base(__x), _Safe_base() { }
90 deque(const _Base& __x)
91 : _Base(__x), _Safe_base() { }
93 #ifdef __GXX_EXPERIMENTAL_CXX0X__
95 : _Base(std::forward<deque>(__x)), _Safe_base()
96 { this->_M_swap(__x); }
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 template<class _InputIterator>
122 assign(_InputIterator __first, _InputIterator __last)
124 __glibcxx_check_valid_range(__first, __last);
125 _Base::assign(__first, __last);
126 this->_M_invalidate_all();
130 assign(size_type __n, const _Tp& __t)
132 _Base::assign(__n, __t);
133 this->_M_invalidate_all();
136 using _Base::get_allocator;
141 { return iterator(_Base::begin(), this); }
145 { return const_iterator(_Base::begin(), this); }
149 { return iterator(_Base::end(), this); }
153 { return const_iterator(_Base::end(), this); }
157 { return reverse_iterator(end()); }
159 const_reverse_iterator
161 { return const_reverse_iterator(end()); }
165 { return reverse_iterator(begin()); }
167 const_reverse_iterator
169 { return const_reverse_iterator(begin()); }
171 #ifdef __GXX_EXPERIMENTAL_CXX0X__
174 { return const_iterator(_Base::begin(), this); }
178 { return const_iterator(_Base::end(), this); }
180 const_reverse_iterator
182 { return const_reverse_iterator(end()); }
184 const_reverse_iterator
186 { return const_reverse_iterator(begin()); }
189 // 23.2.1.2 capacity:
191 using _Base::max_size;
194 resize(size_type __sz, _Tp __c = _Tp())
196 typedef typename _Base::const_iterator _Base_const_iterator;
197 typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth;
199 bool __invalidate_all = __sz > this->size();
200 if (__sz < this->size())
201 this->_M_invalidate_if(_After_nth(__sz, _M_base().begin()));
203 _Base::resize(__sz, __c);
205 if (__invalidate_all)
206 this->_M_invalidate_all();
213 operator[](size_type __n)
215 __glibcxx_check_subscript(__n);
216 return _M_base()[__n];
220 operator[](size_type __n) const
222 __glibcxx_check_subscript(__n);
223 return _M_base()[__n];
231 __glibcxx_check_nonempty();
232 return _Base::front();
238 __glibcxx_check_nonempty();
239 return _Base::front();
245 __glibcxx_check_nonempty();
246 return _Base::back();
252 __glibcxx_check_nonempty();
253 return _Base::back();
256 // 23.2.1.3 modifiers:
257 #ifndef __GXX_EXPERIMENTAL_CXX0X__
259 push_front(const _Tp& __x)
261 _Base::push_front(__x);
262 this->_M_invalidate_all();
266 push_back(const _Tp& __x)
268 _Base::push_back(__x);
269 this->_M_invalidate_all();
272 template<typename... _Args>
274 push_front(_Args&&... __args)
276 _Base::push_front(std::forward<_Args>(__args)...);
277 this->_M_invalidate_all();
280 template<typename... _Args>
282 push_back(_Args&&... __args)
284 _Base::push_back(std::forward<_Args>(__args)...);
285 this->_M_invalidate_all();
288 template<typename... _Args>
290 emplace(iterator __position, _Args&&... __args)
292 __glibcxx_check_insert(__position);
293 typename _Base::iterator __res = _Base::emplace(__position.base(),
294 std::forward<_Args>(__args)...);
295 this->_M_invalidate_all();
296 return iterator(__res, this);
301 insert(iterator __position, const _Tp& __x)
303 __glibcxx_check_insert(__position);
304 typename _Base::iterator __res = _Base::insert(__position.base(), __x);
305 this->_M_invalidate_all();
306 return iterator(__res, this);
309 #ifdef __GXX_EXPERIMENTAL_CXX0X__
311 insert(iterator __position, _Tp&& __x)
313 __glibcxx_check_insert(__position);
314 typename _Base::iterator __res = _Base::insert(__position.base(),
316 this->_M_invalidate_all();
317 return iterator(__res, this);
322 insert(iterator __position, size_type __n, const _Tp& __x)
324 __glibcxx_check_insert(__position);
325 _Base::insert(__position.base(), __n, __x);
326 this->_M_invalidate_all();
329 template<class _InputIterator>
331 insert(iterator __position,
332 _InputIterator __first, _InputIterator __last)
334 __glibcxx_check_insert_range(__position, __first, __last);
335 _Base::insert(__position.base(), __first, __last);
336 this->_M_invalidate_all();
342 __glibcxx_check_nonempty();
343 iterator __victim = begin();
344 __victim._M_invalidate();
351 __glibcxx_check_nonempty();
352 iterator __victim = end();
354 __victim._M_invalidate();
359 erase(iterator __position)
361 __glibcxx_check_erase(__position);
362 if (__position == begin() || __position == end()-1)
364 __position._M_invalidate();
365 return iterator(_Base::erase(__position.base()), this);
369 typename _Base::iterator __res = _Base::erase(__position.base());
370 this->_M_invalidate_all();
371 return iterator(__res, this);
376 erase(iterator __first, iterator __last)
378 // _GLIBCXX_RESOLVE_LIB_DEFECTS
379 // 151. can't currently clear() empty container
380 __glibcxx_check_erase_range(__first, __last);
381 if (__first == begin() || __last == end())
383 this->_M_detach_singular();
384 for (iterator __position = __first; __position != __last; )
386 iterator __victim = __position++;
387 __victim._M_invalidate();
391 return iterator(_Base::erase(__first.base(), __last.base()),
396 this->_M_revalidate_singular();
397 __throw_exception_again;
402 typename _Base::iterator __res = _Base::erase(__first.base(),
404 this->_M_invalidate_all();
405 return iterator(__res, this);
410 #ifdef __GXX_EXPERIMENTAL_CXX0X__
424 this->_M_invalidate_all();
428 _M_base() { return *this; }
431 _M_base() const { return *this; }
434 template<typename _Tp, typename _Alloc>
436 operator==(const deque<_Tp, _Alloc>& __lhs,
437 const deque<_Tp, _Alloc>& __rhs)
438 { return __lhs._M_base() == __rhs._M_base(); }
440 template<typename _Tp, typename _Alloc>
442 operator!=(const deque<_Tp, _Alloc>& __lhs,
443 const deque<_Tp, _Alloc>& __rhs)
444 { return __lhs._M_base() != __rhs._M_base(); }
446 template<typename _Tp, typename _Alloc>
448 operator<(const deque<_Tp, _Alloc>& __lhs,
449 const deque<_Tp, _Alloc>& __rhs)
450 { return __lhs._M_base() < __rhs._M_base(); }
452 template<typename _Tp, typename _Alloc>
454 operator<=(const deque<_Tp, _Alloc>& __lhs,
455 const deque<_Tp, _Alloc>& __rhs)
456 { return __lhs._M_base() <= __rhs._M_base(); }
458 template<typename _Tp, typename _Alloc>
460 operator>=(const deque<_Tp, _Alloc>& __lhs,
461 const deque<_Tp, _Alloc>& __rhs)
462 { return __lhs._M_base() >= __rhs._M_base(); }
464 template<typename _Tp, typename _Alloc>
466 operator>(const deque<_Tp, _Alloc>& __lhs,
467 const deque<_Tp, _Alloc>& __rhs)
468 { return __lhs._M_base() > __rhs._M_base(); }
470 template<typename _Tp, typename _Alloc>
472 swap(deque<_Tp, _Alloc>& __lhs, deque<_Tp, _Alloc>& __rhs)
473 { __lhs.swap(__rhs); }
475 #ifdef __GXX_EXPERIMENTAL_CXX0X__
476 template<typename _Tp, typename _Alloc>
478 swap(deque<_Tp, _Alloc>&& __lhs, deque<_Tp, _Alloc>& __rhs)
479 { __lhs.swap(__rhs); }
481 template<typename _Tp, typename _Alloc>
483 swap(deque<_Tp, _Alloc>& __lhs, deque<_Tp, _Alloc>&& __rhs)
484 { __lhs.swap(__rhs); }
487 } // namespace __debug