1 // Debugging deque implementation -*- C++ -*-
3 // Copyright (C) 2003-2013 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
26 * This file is a GNU debug extension to the Standard C++ Library.
29 #ifndef _GLIBCXX_DEBUG_DEQUE
30 #define _GLIBCXX_DEBUG_DEQUE 1
33 #include <debug/safe_sequence.h>
34 #include <debug/safe_iterator.h>
36 namespace std _GLIBCXX_VISIBILITY(default)
40 /// Class std::deque with safety/checking/debug instrumentation.
41 template<typename _Tp, typename _Allocator = std::allocator<_Tp> >
43 : public _GLIBCXX_STD_C::deque<_Tp, _Allocator>,
44 public __gnu_debug::_Safe_sequence<deque<_Tp, _Allocator> >
46 typedef _GLIBCXX_STD_C::deque<_Tp, _Allocator> _Base;
48 typedef typename _Base::const_iterator _Base_const_iterator;
49 typedef typename _Base::iterator _Base_iterator;
50 typedef __gnu_debug::_Equal_to<_Base_const_iterator> _Equal;
52 typedef typename _Base::reference reference;
53 typedef typename _Base::const_reference const_reference;
55 typedef __gnu_debug::_Safe_iterator<_Base_iterator,deque>
57 typedef __gnu_debug::_Safe_iterator<_Base_const_iterator,deque>
60 typedef typename _Base::size_type size_type;
61 typedef typename _Base::difference_type difference_type;
63 typedef _Tp value_type;
64 typedef _Allocator allocator_type;
65 typedef typename _Base::pointer pointer;
66 typedef typename _Base::const_pointer const_pointer;
67 typedef std::reverse_iterator<iterator> reverse_iterator;
68 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
70 // 23.2.1.1 construct/copy/destroy:
72 deque(const _Allocator& __a = _Allocator())
75 #if __cplusplus >= 201103L
80 deque(size_type __n, const _Tp& __value,
81 const _Allocator& __a = _Allocator())
82 : _Base(__n, __value, __a) { }
85 deque(size_type __n, const _Tp& __value = _Tp(),
86 const _Allocator& __a = _Allocator())
87 : _Base(__n, __value, __a) { }
90 #if __cplusplus >= 201103L
91 template<class _InputIterator,
92 typename = std::_RequireInputIter<_InputIterator>>
94 template<class _InputIterator>
96 deque(_InputIterator __first, _InputIterator __last,
97 const _Allocator& __a = _Allocator())
98 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
100 __gnu_debug::__base(__last), __a)
103 deque(const deque& __x)
106 deque(const _Base& __x)
109 #if __cplusplus >= 201103L
111 : _Base(std::move(__x))
112 { this->_M_swap(__x); }
114 deque(initializer_list<value_type> __l,
115 const allocator_type& __a = allocator_type())
116 : _Base(__l, __a) { }
119 ~deque() _GLIBCXX_NOEXCEPT { }
122 operator=(const deque& __x)
124 *static_cast<_Base*>(this) = __x;
125 this->_M_invalidate_all();
129 #if __cplusplus >= 201103L
131 operator=(deque&& __x)
135 __glibcxx_check_self_move_assign(__x);
142 operator=(initializer_list<value_type> __l)
144 *static_cast<_Base*>(this) = __l;
145 this->_M_invalidate_all();
150 #if __cplusplus >= 201103L
151 template<class _InputIterator,
152 typename = std::_RequireInputIter<_InputIterator>>
154 template<class _InputIterator>
157 assign(_InputIterator __first, _InputIterator __last)
159 __glibcxx_check_valid_range(__first, __last);
160 _Base::assign(__gnu_debug::__base(__first),
161 __gnu_debug::__base(__last));
162 this->_M_invalidate_all();
166 assign(size_type __n, const _Tp& __t)
168 _Base::assign(__n, __t);
169 this->_M_invalidate_all();
172 #if __cplusplus >= 201103L
174 assign(initializer_list<value_type> __l)
177 this->_M_invalidate_all();
181 using _Base::get_allocator;
185 begin() _GLIBCXX_NOEXCEPT
186 { return iterator(_Base::begin(), this); }
189 begin() const _GLIBCXX_NOEXCEPT
190 { return const_iterator(_Base::begin(), this); }
193 end() _GLIBCXX_NOEXCEPT
194 { return iterator(_Base::end(), this); }
197 end() const _GLIBCXX_NOEXCEPT
198 { return const_iterator(_Base::end(), this); }
201 rbegin() _GLIBCXX_NOEXCEPT
202 { return reverse_iterator(end()); }
204 const_reverse_iterator
205 rbegin() const _GLIBCXX_NOEXCEPT
206 { return const_reverse_iterator(end()); }
209 rend() _GLIBCXX_NOEXCEPT
210 { return reverse_iterator(begin()); }
212 const_reverse_iterator
213 rend() const _GLIBCXX_NOEXCEPT
214 { return const_reverse_iterator(begin()); }
216 #if __cplusplus >= 201103L
218 cbegin() const noexcept
219 { return const_iterator(_Base::begin(), this); }
222 cend() const noexcept
223 { return const_iterator(_Base::end(), this); }
225 const_reverse_iterator
226 crbegin() const noexcept
227 { return const_reverse_iterator(end()); }
229 const_reverse_iterator
230 crend() const noexcept
231 { return const_reverse_iterator(begin()); }
236 _M_invalidate_after_nth(difference_type __n)
238 typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth;
239 this->_M_invalidate_if(_After_nth(__n, _Base::begin()));
243 // 23.2.1.2 capacity:
245 using _Base::max_size;
247 #if __cplusplus >= 201103L
249 resize(size_type __sz)
251 bool __invalidate_all = __sz > this->size();
252 if (__sz < this->size())
253 this->_M_invalidate_after_nth(__sz);
257 if (__invalidate_all)
258 this->_M_invalidate_all();
262 resize(size_type __sz, const _Tp& __c)
264 bool __invalidate_all = __sz > this->size();
265 if (__sz < this->size())
266 this->_M_invalidate_after_nth(__sz);
268 _Base::resize(__sz, __c);
270 if (__invalidate_all)
271 this->_M_invalidate_all();
275 resize(size_type __sz, _Tp __c = _Tp())
277 bool __invalidate_all = __sz > this->size();
278 if (__sz < this->size())
279 this->_M_invalidate_after_nth(__sz);
281 _Base::resize(__sz, __c);
283 if (__invalidate_all)
284 this->_M_invalidate_all();
288 #if __cplusplus >= 201103L
292 if (_Base::_M_shrink_to_fit())
293 this->_M_invalidate_all();
301 operator[](size_type __n)
303 __glibcxx_check_subscript(__n);
304 return _M_base()[__n];
308 operator[](size_type __n) const
310 __glibcxx_check_subscript(__n);
311 return _M_base()[__n];
319 __glibcxx_check_nonempty();
320 return _Base::front();
326 __glibcxx_check_nonempty();
327 return _Base::front();
333 __glibcxx_check_nonempty();
334 return _Base::back();
340 __glibcxx_check_nonempty();
341 return _Base::back();
344 // 23.2.1.3 modifiers:
346 push_front(const _Tp& __x)
348 _Base::push_front(__x);
349 this->_M_invalidate_all();
353 push_back(const _Tp& __x)
355 _Base::push_back(__x);
356 this->_M_invalidate_all();
359 #if __cplusplus >= 201103L
361 push_front(_Tp&& __x)
362 { emplace_front(std::move(__x)); }
366 { emplace_back(std::move(__x)); }
368 template<typename... _Args>
370 emplace_front(_Args&&... __args)
372 _Base::emplace_front(std::forward<_Args>(__args)...);
373 this->_M_invalidate_all();
376 template<typename... _Args>
378 emplace_back(_Args&&... __args)
380 _Base::emplace_back(std::forward<_Args>(__args)...);
381 this->_M_invalidate_all();
384 template<typename... _Args>
386 emplace(iterator __position, _Args&&... __args)
388 __glibcxx_check_insert(__position);
389 _Base_iterator __res = _Base::emplace(__position.base(),
390 std::forward<_Args>(__args)...);
391 this->_M_invalidate_all();
392 return iterator(__res, this);
397 insert(iterator __position, const _Tp& __x)
399 __glibcxx_check_insert(__position);
400 _Base_iterator __res = _Base::insert(__position.base(), __x);
401 this->_M_invalidate_all();
402 return iterator(__res, this);
405 #if __cplusplus >= 201103L
407 insert(iterator __position, _Tp&& __x)
408 { return emplace(__position, std::move(__x)); }
411 insert(iterator __p, initializer_list<value_type> __l)
413 _Base::insert(__p, __l);
414 this->_M_invalidate_all();
419 insert(iterator __position, size_type __n, const _Tp& __x)
421 __glibcxx_check_insert(__position);
422 _Base::insert(__position.base(), __n, __x);
423 this->_M_invalidate_all();
426 #if __cplusplus >= 201103L
427 template<class _InputIterator,
428 typename = std::_RequireInputIter<_InputIterator>>
430 template<class _InputIterator>
433 insert(iterator __position,
434 _InputIterator __first, _InputIterator __last)
436 __glibcxx_check_insert_range(__position, __first, __last);
437 _Base::insert(__position.base(), __gnu_debug::__base(__first),
438 __gnu_debug::__base(__last));
439 this->_M_invalidate_all();
445 __glibcxx_check_nonempty();
446 this->_M_invalidate_if(_Equal(_Base::begin()));
453 __glibcxx_check_nonempty();
454 this->_M_invalidate_if(_Equal(--_Base::end()));
459 erase(iterator __position)
461 __glibcxx_check_erase(__position);
462 _Base_iterator __victim = __position.base();
463 if (__victim == _Base::begin() || __victim == _Base::end()-1)
465 this->_M_invalidate_if(_Equal(__victim));
466 return iterator(_Base::erase(__victim), this);
470 _Base_iterator __res = _Base::erase(__victim);
471 this->_M_invalidate_all();
472 return iterator(__res, this);
477 erase(iterator __first, iterator __last)
479 // _GLIBCXX_RESOLVE_LIB_DEFECTS
480 // 151. can't currently clear() empty container
481 __glibcxx_check_erase_range(__first, __last);
483 if (__first.base() == __last.base())
485 else if (__first.base() == _Base::begin()
486 || __last.base() == _Base::end())
488 this->_M_detach_singular();
489 for (_Base_iterator __position = __first.base();
490 __position != __last.base(); ++__position)
492 this->_M_invalidate_if(_Equal(__position));
496 return iterator(_Base::erase(__first.base(), __last.base()),
501 this->_M_revalidate_singular();
502 __throw_exception_again;
507 _Base_iterator __res = _Base::erase(__first.base(),
509 this->_M_invalidate_all();
510 return iterator(__res, this);
522 clear() _GLIBCXX_NOEXCEPT
525 this->_M_invalidate_all();
529 _M_base() _GLIBCXX_NOEXCEPT { return *this; }
532 _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
535 template<typename _Tp, typename _Alloc>
537 operator==(const deque<_Tp, _Alloc>& __lhs,
538 const deque<_Tp, _Alloc>& __rhs)
539 { return __lhs._M_base() == __rhs._M_base(); }
541 template<typename _Tp, typename _Alloc>
543 operator!=(const deque<_Tp, _Alloc>& __lhs,
544 const deque<_Tp, _Alloc>& __rhs)
545 { return __lhs._M_base() != __rhs._M_base(); }
547 template<typename _Tp, typename _Alloc>
549 operator<(const deque<_Tp, _Alloc>& __lhs,
550 const deque<_Tp, _Alloc>& __rhs)
551 { return __lhs._M_base() < __rhs._M_base(); }
553 template<typename _Tp, typename _Alloc>
555 operator<=(const deque<_Tp, _Alloc>& __lhs,
556 const deque<_Tp, _Alloc>& __rhs)
557 { return __lhs._M_base() <= __rhs._M_base(); }
559 template<typename _Tp, typename _Alloc>
561 operator>=(const deque<_Tp, _Alloc>& __lhs,
562 const deque<_Tp, _Alloc>& __rhs)
563 { return __lhs._M_base() >= __rhs._M_base(); }
565 template<typename _Tp, typename _Alloc>
567 operator>(const deque<_Tp, _Alloc>& __lhs,
568 const deque<_Tp, _Alloc>& __rhs)
569 { return __lhs._M_base() > __rhs._M_base(); }
571 template<typename _Tp, typename _Alloc>
573 swap(deque<_Tp, _Alloc>& __lhs, deque<_Tp, _Alloc>& __rhs)
574 { __lhs.swap(__rhs); }
576 } // namespace __debug