1 // Debugging deque implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005
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 #ifndef _GLIBCXX_DEBUG_DEQUE
32 #define _GLIBCXX_DEBUG_DEQUE 1
35 #include <debug/safe_sequence.h>
36 #include <debug/safe_iterator.h>
40 namespace __gnu_debug_def
42 template<typename _Tp, typename _Allocator = std::allocator<_Tp> >
44 : public _GLIBCXX_STD::deque<_Tp, _Allocator>,
45 public __gnu_debug::_Safe_sequence<deque<_Tp, _Allocator> >
47 typedef _GLIBCXX_STD::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<_Tp,_Allocator>& __x) : _Base(__x), _Safe_base() { }
85 deque(const _Base& __x) : _Base(__x), _Safe_base() { }
89 deque<_Tp,_Allocator>&
90 operator=(const deque<_Tp,_Allocator>& __x)
92 *static_cast<_Base*>(this) = __x;
93 this->_M_invalidate_all();
97 template<class _InputIterator>
99 assign(_InputIterator __first, _InputIterator __last)
101 __glibcxx_check_valid_range(__first, __last);
102 _Base::assign(__first, __last);
103 this->_M_invalidate_all();
107 assign(size_type __n, const _Tp& __t)
109 _Base::assign(__n, __t);
110 this->_M_invalidate_all();
113 using _Base::get_allocator;
118 { return iterator(_Base::begin(), this); }
122 { return const_iterator(_Base::begin(), this); }
126 { return iterator(_Base::end(), this); }
130 { return const_iterator(_Base::end(), this); }
134 { return reverse_iterator(end()); }
136 const_reverse_iterator
138 { return const_reverse_iterator(end()); }
142 { return reverse_iterator(begin()); }
144 const_reverse_iterator
146 { return const_reverse_iterator(begin()); }
148 // 23.2.1.2 capacity:
150 using _Base::max_size;
153 resize(size_type __sz, _Tp __c = _Tp())
155 typedef typename _Base::const_iterator _Base_const_iterator;
156 typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth;
158 bool __invalidate_all = __sz > this->size();
159 if (__sz < this->size())
160 this->_M_invalidate_if(_After_nth(__sz, _M_base().begin()));
162 _Base::resize(__sz, __c);
164 if (__invalidate_all)
165 this->_M_invalidate_all();
172 operator[](size_type __n)
174 __glibcxx_check_subscript(__n);
175 return _M_base()[__n];
179 operator[](size_type __n) const
181 __glibcxx_check_subscript(__n);
182 return _M_base()[__n];
190 __glibcxx_check_nonempty();
191 return _Base::front();
197 __glibcxx_check_nonempty();
198 return _Base::front();
204 __glibcxx_check_nonempty();
205 return _Base::back();
211 __glibcxx_check_nonempty();
212 return _Base::back();
215 // 23.2.1.3 modifiers:
217 push_front(const _Tp& __x)
219 _Base::push_front(__x);
220 this->_M_invalidate_all();
224 push_back(const _Tp& __x)
226 _Base::push_back(__x);
227 this->_M_invalidate_all();
231 insert(iterator __position, const _Tp& __x)
233 __glibcxx_check_insert(__position);
234 typename _Base::iterator __res = _Base::insert(__position.base(), __x);
235 this->_M_invalidate_all();
236 return iterator(__res, this);
240 insert(iterator __position, size_type __n, const _Tp& __x)
242 __glibcxx_check_insert(__position);
243 _Base::insert(__position.base(), __n, __x);
244 this->_M_invalidate_all();
247 template<class _InputIterator>
249 insert(iterator __position,
250 _InputIterator __first, _InputIterator __last)
252 __glibcxx_check_insert_range(__position, __first, __last);
253 _Base::insert(__position.base(), __first, __last);
254 this->_M_invalidate_all();
260 __glibcxx_check_nonempty();
261 iterator __victim = begin();
262 __victim._M_invalidate();
269 __glibcxx_check_nonempty();
270 iterator __victim = end();
272 __victim._M_invalidate();
277 erase(iterator __position)
279 __glibcxx_check_erase(__position);
280 if (__position == begin() || __position == end()-1)
282 __position._M_invalidate();
283 return iterator(_Base::erase(__position.base()), this);
287 typename _Base::iterator __res = _Base::erase(__position.base());
288 this->_M_invalidate_all();
289 return iterator(__res, this);
294 erase(iterator __first, iterator __last)
296 // _GLIBCXX_RESOLVE_LIB_DEFECTS
297 // 151. can't currently clear() empty container
298 __glibcxx_check_erase_range(__first, __last);
299 if (__first == begin() || __last == end())
301 this->_M_detach_singular();
302 for (iterator __position = __first; __position != __last; )
304 iterator __victim = __position++;
305 __victim._M_invalidate();
309 return iterator(_Base::erase(__first.base(), __last.base()),
314 this->_M_revalidate_singular();
315 __throw_exception_again;
320 typename _Base::iterator __res = _Base::erase(__first.base(),
322 this->_M_invalidate_all();
323 return iterator(__res, this);
328 swap(deque<_Tp,_Allocator>& __x)
338 this->_M_invalidate_all();
342 _M_base() { return *this; }
345 _M_base() const { return *this; }
348 template<typename _Tp, typename _Alloc>
350 operator==(const deque<_Tp, _Alloc>& __lhs,
351 const deque<_Tp, _Alloc>& __rhs)
352 { return __lhs._M_base() == __rhs._M_base(); }
354 template<typename _Tp, typename _Alloc>
356 operator!=(const deque<_Tp, _Alloc>& __lhs,
357 const deque<_Tp, _Alloc>& __rhs)
358 { return __lhs._M_base() != __rhs._M_base(); }
360 template<typename _Tp, typename _Alloc>
362 operator<(const deque<_Tp, _Alloc>& __lhs, const deque<_Tp, _Alloc>& __rhs)
363 { return __lhs._M_base() < __rhs._M_base(); }
365 template<typename _Tp, typename _Alloc>
367 operator<=(const deque<_Tp, _Alloc>& __lhs,
368 const deque<_Tp, _Alloc>& __rhs)
369 { return __lhs._M_base() <= __rhs._M_base(); }
371 template<typename _Tp, typename _Alloc>
373 operator>=(const deque<_Tp, _Alloc>& __lhs,
374 const deque<_Tp, _Alloc>& __rhs)
375 { return __lhs._M_base() >= __rhs._M_base(); }
377 template<typename _Tp, typename _Alloc>
379 operator>(const deque<_Tp, _Alloc>& __lhs, const deque<_Tp, _Alloc>& __rhs)
380 { return __lhs._M_base() > __rhs._M_base(); }
382 template<typename _Tp, typename _Alloc>
384 swap(deque<_Tp, _Alloc>& __lhs, deque<_Tp, _Alloc>& __rhs)
385 { __lhs.swap(__rhs); }
386 } // namespace __gnu_debug_def