1 // Debugging deque implementation -*- C++ -*-
3 // Copyright (C) 2003-2014 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_container.h>
35 #include <debug/safe_iterator.h>
37 namespace std _GLIBCXX_VISIBILITY(default)
41 /// Class std::deque with safety/checking/debug instrumentation.
42 template<typename _Tp, typename _Allocator = std::allocator<_Tp> >
44 : public __gnu_debug::_Safe_container<
45 deque<_Tp, _Allocator>, _Allocator,
46 __gnu_debug::_Safe_sequence, false>,
47 public _GLIBCXX_STD_C::deque<_Tp, _Allocator>
49 typedef _GLIBCXX_STD_C::deque<_Tp, _Allocator> _Base;
50 typedef __gnu_debug::_Safe_container<
51 deque, _Allocator, __gnu_debug::_Safe_sequence, false> _Safe;
53 typedef typename _Base::const_iterator _Base_const_iterator;
54 typedef typename _Base::iterator _Base_iterator;
55 typedef __gnu_debug::_Equal_to<_Base_const_iterator> _Equal;
58 typedef typename _Base::reference reference;
59 typedef typename _Base::const_reference const_reference;
61 typedef __gnu_debug::_Safe_iterator<_Base_iterator, deque>
63 typedef __gnu_debug::_Safe_iterator<_Base_const_iterator, deque>
66 typedef typename _Base::size_type size_type;
67 typedef typename _Base::difference_type difference_type;
69 typedef _Tp value_type;
70 typedef _Allocator allocator_type;
71 typedef typename _Base::pointer pointer;
72 typedef typename _Base::const_pointer const_pointer;
73 typedef std::reverse_iterator<iterator> reverse_iterator;
74 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
76 // 23.2.1.1 construct/copy/destroy:
78 #if __cplusplus < 201103L
82 deque(const deque& __x)
88 deque(const deque&) = default;
89 deque(deque&&) = default;
91 deque(initializer_list<value_type> __l,
92 const allocator_type& __a = allocator_type())
99 deque(const _Allocator& __a)
102 #if __cplusplus >= 201103L
107 deque(size_type __n, const _Tp& __value,
108 const _Allocator& __a = _Allocator())
109 : _Base(__n, __value, __a) { }
112 deque(size_type __n, const _Tp& __value = _Tp(),
113 const _Allocator& __a = _Allocator())
114 : _Base(__n, __value, __a) { }
117 #if __cplusplus >= 201103L
118 template<class _InputIterator,
119 typename = std::_RequireInputIter<_InputIterator>>
121 template<class _InputIterator>
123 deque(_InputIterator __first, _InputIterator __last,
124 const _Allocator& __a = _Allocator())
125 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
127 __gnu_debug::__base(__last), __a)
130 deque(const _Base& __x)
133 #if __cplusplus < 201103L
135 operator=(const deque& __x)
137 this->_M_safe() = __x;
143 operator=(const deque&) = default;
146 operator=(deque&&) = default;
149 operator=(initializer_list<value_type> __l)
152 this->_M_invalidate_all();
157 #if __cplusplus >= 201103L
158 template<class _InputIterator,
159 typename = std::_RequireInputIter<_InputIterator>>
161 template<class _InputIterator>
164 assign(_InputIterator __first, _InputIterator __last)
166 __glibcxx_check_valid_range(__first, __last);
167 _Base::assign(__gnu_debug::__base(__first),
168 __gnu_debug::__base(__last));
169 this->_M_invalidate_all();
173 assign(size_type __n, const _Tp& __t)
175 _Base::assign(__n, __t);
176 this->_M_invalidate_all();
179 #if __cplusplus >= 201103L
181 assign(initializer_list<value_type> __l)
184 this->_M_invalidate_all();
188 using _Base::get_allocator;
192 begin() _GLIBCXX_NOEXCEPT
193 { return iterator(_Base::begin(), this); }
196 begin() const _GLIBCXX_NOEXCEPT
197 { return const_iterator(_Base::begin(), this); }
200 end() _GLIBCXX_NOEXCEPT
201 { return iterator(_Base::end(), this); }
204 end() const _GLIBCXX_NOEXCEPT
205 { return const_iterator(_Base::end(), this); }
208 rbegin() _GLIBCXX_NOEXCEPT
209 { return reverse_iterator(end()); }
211 const_reverse_iterator
212 rbegin() const _GLIBCXX_NOEXCEPT
213 { return const_reverse_iterator(end()); }
216 rend() _GLIBCXX_NOEXCEPT
217 { return reverse_iterator(begin()); }
219 const_reverse_iterator
220 rend() const _GLIBCXX_NOEXCEPT
221 { return const_reverse_iterator(begin()); }
223 #if __cplusplus >= 201103L
225 cbegin() const noexcept
226 { return const_iterator(_Base::begin(), this); }
229 cend() const noexcept
230 { return const_iterator(_Base::end(), this); }
232 const_reverse_iterator
233 crbegin() const noexcept
234 { return const_reverse_iterator(end()); }
236 const_reverse_iterator
237 crend() const noexcept
238 { return const_reverse_iterator(begin()); }
243 _M_invalidate_after_nth(difference_type __n)
245 typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth;
246 this->_M_invalidate_if(_After_nth(__n, _Base::begin()));
250 // 23.2.1.2 capacity:
252 using _Base::max_size;
254 #if __cplusplus >= 201103L
256 resize(size_type __sz)
258 bool __invalidate_all = __sz > this->size();
259 if (__sz < this->size())
260 this->_M_invalidate_after_nth(__sz);
264 if (__invalidate_all)
265 this->_M_invalidate_all();
269 resize(size_type __sz, const _Tp& __c)
271 bool __invalidate_all = __sz > this->size();
272 if (__sz < this->size())
273 this->_M_invalidate_after_nth(__sz);
275 _Base::resize(__sz, __c);
277 if (__invalidate_all)
278 this->_M_invalidate_all();
282 resize(size_type __sz, _Tp __c = _Tp())
284 bool __invalidate_all = __sz > this->size();
285 if (__sz < this->size())
286 this->_M_invalidate_after_nth(__sz);
288 _Base::resize(__sz, __c);
290 if (__invalidate_all)
291 this->_M_invalidate_all();
295 #if __cplusplus >= 201103L
297 shrink_to_fit() noexcept
299 if (_Base::_M_shrink_to_fit())
300 this->_M_invalidate_all();
308 operator[](size_type __n) _GLIBCXX_NOEXCEPT
310 __glibcxx_check_subscript(__n);
311 return _M_base()[__n];
315 operator[](size_type __n) const _GLIBCXX_NOEXCEPT
317 __glibcxx_check_subscript(__n);
318 return _M_base()[__n];
324 front() _GLIBCXX_NOEXCEPT
326 __glibcxx_check_nonempty();
327 return _Base::front();
331 front() const _GLIBCXX_NOEXCEPT
333 __glibcxx_check_nonempty();
334 return _Base::front();
338 back() _GLIBCXX_NOEXCEPT
340 __glibcxx_check_nonempty();
341 return _Base::back();
345 back() const _GLIBCXX_NOEXCEPT
347 __glibcxx_check_nonempty();
348 return _Base::back();
351 // 23.2.1.3 modifiers:
353 push_front(const _Tp& __x)
355 _Base::push_front(__x);
356 this->_M_invalidate_all();
360 push_back(const _Tp& __x)
362 _Base::push_back(__x);
363 this->_M_invalidate_all();
366 #if __cplusplus >= 201103L
368 push_front(_Tp&& __x)
369 { emplace_front(std::move(__x)); }
373 { emplace_back(std::move(__x)); }
375 template<typename... _Args>
377 emplace_front(_Args&&... __args)
379 _Base::emplace_front(std::forward<_Args>(__args)...);
380 this->_M_invalidate_all();
383 template<typename... _Args>
385 emplace_back(_Args&&... __args)
387 _Base::emplace_back(std::forward<_Args>(__args)...);
388 this->_M_invalidate_all();
391 template<typename... _Args>
393 emplace(const_iterator __position, _Args&&... __args)
395 __glibcxx_check_insert(__position);
396 _Base_iterator __res = _Base::emplace(__position.base(),
397 std::forward<_Args>(__args)...);
398 this->_M_invalidate_all();
399 return iterator(__res, this);
404 #if __cplusplus >= 201103L
405 insert(const_iterator __position, const _Tp& __x)
407 insert(iterator __position, const _Tp& __x)
410 __glibcxx_check_insert(__position);
411 _Base_iterator __res = _Base::insert(__position.base(), __x);
412 this->_M_invalidate_all();
413 return iterator(__res, this);
416 #if __cplusplus >= 201103L
418 insert(const_iterator __position, _Tp&& __x)
419 { return emplace(__position, std::move(__x)); }
422 insert(const_iterator __position, initializer_list<value_type> __l)
424 __glibcxx_check_insert(__position);
425 _Base_iterator __res = _Base::insert(__position.base(), __l);
426 this->_M_invalidate_all();
427 return iterator(__res, this);
431 #if __cplusplus >= 201103L
433 insert(const_iterator __position, size_type __n, const _Tp& __x)
435 __glibcxx_check_insert(__position);
436 _Base_iterator __res = _Base::insert(__position.base(), __n, __x);
437 this->_M_invalidate_all();
438 return iterator(__res, this);
442 insert(iterator __position, size_type __n, const _Tp& __x)
444 __glibcxx_check_insert(__position);
445 _Base::insert(__position.base(), __n, __x);
446 this->_M_invalidate_all();
450 #if __cplusplus >= 201103L
451 template<class _InputIterator,
452 typename = std::_RequireInputIter<_InputIterator>>
454 insert(const_iterator __position,
455 _InputIterator __first, _InputIterator __last)
457 __glibcxx_check_insert_range(__position, __first, __last);
458 _Base_iterator __res = _Base::insert(__position.base(),
459 __gnu_debug::__base(__first),
460 __gnu_debug::__base(__last));
461 this->_M_invalidate_all();
462 return iterator(__res, this);
465 template<class _InputIterator>
467 insert(iterator __position,
468 _InputIterator __first, _InputIterator __last)
470 __glibcxx_check_insert_range(__position, __first, __last);
471 _Base::insert(__position.base(), __gnu_debug::__base(__first),
472 __gnu_debug::__base(__last));
473 this->_M_invalidate_all();
478 pop_front() _GLIBCXX_NOEXCEPT
480 __glibcxx_check_nonempty();
481 this->_M_invalidate_if(_Equal(_Base::begin()));
486 pop_back() _GLIBCXX_NOEXCEPT
488 __glibcxx_check_nonempty();
489 this->_M_invalidate_if(_Equal(--_Base::end()));
494 #if __cplusplus >= 201103L
495 erase(const_iterator __position)
497 erase(iterator __position)
500 __glibcxx_check_erase(__position);
501 #if __cplusplus >= 201103L
502 _Base_const_iterator __victim = __position.base();
504 _Base_iterator __victim = __position.base();
506 if (__victim == _Base::begin() || __victim == _Base::end() - 1)
508 this->_M_invalidate_if(_Equal(__victim));
509 return iterator(_Base::erase(__victim), this);
513 _Base_iterator __res = _Base::erase(__victim);
514 this->_M_invalidate_all();
515 return iterator(__res, this);
520 #if __cplusplus >= 201103L
521 erase(const_iterator __first, const_iterator __last)
523 erase(iterator __first, iterator __last)
526 // _GLIBCXX_RESOLVE_LIB_DEFECTS
527 // 151. can't currently clear() empty container
528 __glibcxx_check_erase_range(__first, __last);
530 if (__first.base() == __last.base())
531 #if __cplusplus >= 201103L
532 return iterator(__first.base()._M_const_cast(), this);
536 else if (__first.base() == _Base::begin()
537 || __last.base() == _Base::end())
539 this->_M_detach_singular();
540 for (_Base_const_iterator __position = __first.base();
541 __position != __last.base(); ++__position)
543 this->_M_invalidate_if(_Equal(__position));
547 return iterator(_Base::erase(__first.base(), __last.base()),
552 this->_M_revalidate_singular();
553 __throw_exception_again;
558 _Base_iterator __res = _Base::erase(__first.base(),
560 this->_M_invalidate_all();
561 return iterator(__res, this);
567 #if __cplusplus >= 201103L
568 noexcept( noexcept(declval<_Base>().swap(__x)) )
576 clear() _GLIBCXX_NOEXCEPT
579 this->_M_invalidate_all();
583 _M_base() _GLIBCXX_NOEXCEPT { return *this; }
586 _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
589 template<typename _Tp, typename _Alloc>
591 operator==(const deque<_Tp, _Alloc>& __lhs,
592 const deque<_Tp, _Alloc>& __rhs)
593 { return __lhs._M_base() == __rhs._M_base(); }
595 template<typename _Tp, typename _Alloc>
597 operator!=(const deque<_Tp, _Alloc>& __lhs,
598 const deque<_Tp, _Alloc>& __rhs)
599 { return __lhs._M_base() != __rhs._M_base(); }
601 template<typename _Tp, typename _Alloc>
603 operator<(const deque<_Tp, _Alloc>& __lhs,
604 const deque<_Tp, _Alloc>& __rhs)
605 { return __lhs._M_base() < __rhs._M_base(); }
607 template<typename _Tp, typename _Alloc>
609 operator<=(const deque<_Tp, _Alloc>& __lhs,
610 const deque<_Tp, _Alloc>& __rhs)
611 { return __lhs._M_base() <= __rhs._M_base(); }
613 template<typename _Tp, typename _Alloc>
615 operator>=(const deque<_Tp, _Alloc>& __lhs,
616 const deque<_Tp, _Alloc>& __rhs)
617 { return __lhs._M_base() >= __rhs._M_base(); }
619 template<typename _Tp, typename _Alloc>
621 operator>(const deque<_Tp, _Alloc>& __lhs,
622 const deque<_Tp, _Alloc>& __rhs)
623 { return __lhs._M_base() > __rhs._M_base(); }
625 template<typename _Tp, typename _Alloc>
627 swap(deque<_Tp, _Alloc>& __lhs, deque<_Tp, _Alloc>& __rhs)
628 { __lhs.swap(__rhs); }
630 } // namespace __debug