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>
38 namespace __gnu_debug_def
40 template<typename _Tp, typename _Allocator = std::allocator<_Tp> >
42 : public _GLIBCXX_STD::deque<_Tp, _Allocator>,
43 public __gnu_debug::_Safe_sequence<deque<_Tp, _Allocator> >
45 typedef _GLIBCXX_STD::deque<_Tp, _Allocator> _Base;
46 typedef __gnu_debug::_Safe_sequence<deque> _Safe_base;
49 typedef typename _Base::reference reference;
50 typedef typename _Base::const_reference const_reference;
52 typedef __gnu_debug::_Safe_iterator<typename _Base::iterator,deque>
54 typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,deque>
57 typedef typename _Base::size_type size_type;
58 typedef typename _Base::difference_type difference_type;
60 typedef _Tp value_type;
61 typedef _Allocator allocator_type;
62 typedef typename _Base::pointer pointer;
63 typedef typename _Base::const_pointer const_pointer;
64 typedef std::reverse_iterator<iterator> reverse_iterator;
65 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
67 // 23.2.1.1 construct/copy/destroy:
68 explicit deque(const _Allocator& __a = _Allocator())
71 explicit deque(size_type __n, const _Tp& __value = _Tp(),
72 const _Allocator& __a = _Allocator())
73 : _Base(__n, __value, __a) { }
75 template<class _InputIterator>
76 deque(_InputIterator __first, _InputIterator __last,
77 const _Allocator& __a = _Allocator())
78 : _Base(__gnu_debug::__check_valid_range(__first, __last), __last, __a)
81 deque(const deque<_Tp,_Allocator>& __x) : _Base(__x), _Safe_base() { }
83 deque(const _Base& __x) : _Base(__x), _Safe_base() { }
87 deque<_Tp,_Allocator>&
88 operator=(const deque<_Tp,_Allocator>& __x)
90 *static_cast<_Base*>(this) = __x;
91 this->_M_invalidate_all();
95 template<class _InputIterator>
97 assign(_InputIterator __first, _InputIterator __last)
99 __glibcxx_check_valid_range(__first, __last);
100 _Base::assign(__first, __last);
101 this->_M_invalidate_all();
105 assign(size_type __n, const _Tp& __t)
107 _Base::assign(__n, __t);
108 this->_M_invalidate_all();
111 using _Base::get_allocator;
116 { return iterator(_Base::begin(), this); }
120 { return const_iterator(_Base::begin(), this); }
124 { return iterator(_Base::end(), this); }
128 { return const_iterator(_Base::end(), this); }
132 { return reverse_iterator(end()); }
134 const_reverse_iterator
136 { return const_reverse_iterator(end()); }
140 { return reverse_iterator(begin()); }
142 const_reverse_iterator
144 { return const_reverse_iterator(begin()); }
146 // 23.2.1.2 capacity:
148 using _Base::max_size;
151 resize(size_type __sz, _Tp __c = _Tp())
153 typedef typename _Base::const_iterator _Base_const_iterator;
154 typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth;
156 bool __invalidate_all = __sz > this->size();
157 if (__sz < this->size())
158 this->_M_invalidate_if(_After_nth(__sz, _M_base().begin()));
160 _Base::resize(__sz, __c);
162 if (__invalidate_all)
163 this->_M_invalidate_all();
170 operator[](size_type __n)
172 __glibcxx_check_subscript(__n);
173 return _M_base()[__n];
177 operator[](size_type __n) const
179 __glibcxx_check_subscript(__n);
180 return _M_base()[__n];
188 __glibcxx_check_nonempty();
189 return _Base::front();
195 __glibcxx_check_nonempty();
196 return _Base::front();
202 __glibcxx_check_nonempty();
203 return _Base::back();
209 __glibcxx_check_nonempty();
210 return _Base::back();
213 // 23.2.1.3 modifiers:
215 push_front(const _Tp& __x)
217 _Base::push_front(__x);
218 this->_M_invalidate_all();
222 push_back(const _Tp& __x)
224 _Base::push_back(__x);
225 this->_M_invalidate_all();
229 insert(iterator __position, const _Tp& __x)
231 __glibcxx_check_insert(__position);
232 typename _Base::iterator __res = _Base::insert(__position.base(), __x);
233 this->_M_invalidate_all();
234 return iterator(__res, this);
238 insert(iterator __position, size_type __n, const _Tp& __x)
240 __glibcxx_check_insert(__position);
241 _Base::insert(__position.base(), __n, __x);
242 this->_M_invalidate_all();
245 template<class _InputIterator>
247 insert(iterator __position,
248 _InputIterator __first, _InputIterator __last)
250 __glibcxx_check_insert_range(__position, __first, __last);
251 _Base::insert(__position.base(), __first, __last);
252 this->_M_invalidate_all();
258 __glibcxx_check_nonempty();
259 iterator __victim = begin();
260 __victim._M_invalidate();
267 __glibcxx_check_nonempty();
268 iterator __victim = end();
270 __victim._M_invalidate();
275 erase(iterator __position)
277 __glibcxx_check_erase(__position);
278 if (__position == begin() || __position == end()-1)
280 __position._M_invalidate();
281 return iterator(_Base::erase(__position.base()), this);
285 typename _Base::iterator __res = _Base::erase(__position.base());
286 this->_M_invalidate_all();
287 return iterator(__res, this);
292 erase(iterator __first, iterator __last)
294 // _GLIBCXX_RESOLVE_LIB_DEFECTS
295 // 151. can't currently clear() empty container
296 __glibcxx_check_erase_range(__first, __last);
297 if (__first == begin() || __last == end())
299 this->_M_detach_singular();
300 for (iterator __position = __first; __position != __last; )
302 iterator __victim = __position++;
303 __victim._M_invalidate();
307 return iterator(_Base::erase(__first.base(), __last.base()),
312 this->_M_revalidate_singular();
313 __throw_exception_again;
318 typename _Base::iterator __res = _Base::erase(__first.base(),
320 this->_M_invalidate_all();
321 return iterator(__res, this);
326 swap(deque<_Tp,_Allocator>& __x)
336 this->_M_invalidate_all();
340 _M_base() { return *this; }
343 _M_base() const { return *this; }
346 template<typename _Tp, typename _Alloc>
348 operator==(const deque<_Tp, _Alloc>& __lhs,
349 const deque<_Tp, _Alloc>& __rhs)
350 { return __lhs._M_base() == __rhs._M_base(); }
352 template<typename _Tp, typename _Alloc>
354 operator!=(const deque<_Tp, _Alloc>& __lhs,
355 const deque<_Tp, _Alloc>& __rhs)
356 { return __lhs._M_base() != __rhs._M_base(); }
358 template<typename _Tp, typename _Alloc>
360 operator<(const deque<_Tp, _Alloc>& __lhs, const deque<_Tp, _Alloc>& __rhs)
361 { return __lhs._M_base() < __rhs._M_base(); }
363 template<typename _Tp, typename _Alloc>
365 operator<=(const deque<_Tp, _Alloc>& __lhs,
366 const deque<_Tp, _Alloc>& __rhs)
367 { return __lhs._M_base() <= __rhs._M_base(); }
369 template<typename _Tp, typename _Alloc>
371 operator>=(const deque<_Tp, _Alloc>& __lhs,
372 const deque<_Tp, _Alloc>& __rhs)
373 { return __lhs._M_base() >= __rhs._M_base(); }
375 template<typename _Tp, typename _Alloc>
377 operator>(const deque<_Tp, _Alloc>& __lhs, const deque<_Tp, _Alloc>& __rhs)
378 { return __lhs._M_base() > __rhs._M_base(); }
380 template<typename _Tp, typename _Alloc>
382 swap(deque<_Tp, _Alloc>& __lhs, deque<_Tp, _Alloc>& __rhs)
383 { __lhs.swap(__rhs); }
384 } // namespace __gnu_debug_def