1 // Debugging deque implementation -*- C++ -*-
3 // Copyright (C) 2003-2019 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
32 #pragma GCC system_header
34 #include <bits/c++config.h>
35 namespace std _GLIBCXX_VISIBILITY(default) { namespace __debug {
36 template<typename _Tp, typename _Allocator> class deque;
37 } } // namespace std::__debug
40 #include <debug/safe_sequence.h>
41 #include <debug/safe_container.h>
42 #include <debug/safe_iterator.h>
44 namespace std _GLIBCXX_VISIBILITY(default)
48 /// Class std::deque with safety/checking/debug instrumentation.
49 template<typename _Tp, typename _Allocator = std::allocator<_Tp> >
51 : public __gnu_debug::_Safe_container<
52 deque<_Tp, _Allocator>, _Allocator,
53 __gnu_debug::_Safe_sequence>,
54 public _GLIBCXX_STD_C::deque<_Tp, _Allocator>
56 typedef _GLIBCXX_STD_C::deque<_Tp, _Allocator> _Base;
57 typedef __gnu_debug::_Safe_container<
58 deque, _Allocator, __gnu_debug::_Safe_sequence> _Safe;
60 typedef typename _Base::const_iterator _Base_const_iterator;
61 typedef typename _Base::iterator _Base_iterator;
62 typedef __gnu_debug::_Equal_to<_Base_const_iterator> _Equal;
64 template<typename _ItT, typename _SeqT, typename _CatT>
65 friend class ::__gnu_debug::_Safe_iterator;
68 typedef typename _Base::reference reference;
69 typedef typename _Base::const_reference const_reference;
71 typedef __gnu_debug::_Safe_iterator<_Base_iterator, deque>
73 typedef __gnu_debug::_Safe_iterator<_Base_const_iterator, deque>
76 typedef typename _Base::size_type size_type;
77 typedef typename _Base::difference_type difference_type;
79 typedef _Tp value_type;
80 typedef _Allocator allocator_type;
81 typedef typename _Base::pointer pointer;
82 typedef typename _Base::const_pointer const_pointer;
83 typedef std::reverse_iterator<iterator> reverse_iterator;
84 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
86 // 23.2.1.1 construct/copy/destroy:
88 #if __cplusplus < 201103L
92 deque(const deque& __x)
98 deque(const deque&) = default;
99 deque(deque&&) = default;
101 deque(const deque& __d, const _Allocator& __a)
102 : _Base(__d, __a) { }
104 deque(deque&& __d, const _Allocator& __a)
105 : _Safe(std::move(__d)), _Base(std::move(__d), __a) { }
107 deque(initializer_list<value_type> __l,
108 const allocator_type& __a = allocator_type())
109 : _Base(__l, __a) { }
115 deque(const _Allocator& __a)
118 #if __cplusplus >= 201103L
120 deque(size_type __n, const _Allocator& __a = _Allocator())
121 : _Base(__n, __a) { }
123 deque(size_type __n, const _Tp& __value,
124 const _Allocator& __a = _Allocator())
125 : _Base(__n, __value, __a) { }
128 deque(size_type __n, const _Tp& __value = _Tp(),
129 const _Allocator& __a = _Allocator())
130 : _Base(__n, __value, __a) { }
133 #if __cplusplus >= 201103L
134 template<class _InputIterator,
135 typename = std::_RequireInputIter<_InputIterator>>
137 template<class _InputIterator>
139 deque(_InputIterator __first, _InputIterator __last,
140 const _Allocator& __a = _Allocator())
141 : _Base(__gnu_debug::__base(
142 __glibcxx_check_valid_constructor_range(__first, __last)),
143 __gnu_debug::__base(__last), __a)
146 deque(const _Base& __x)
149 #if __cplusplus < 201103L
151 operator=(const deque& __x)
153 this->_M_safe() = __x;
159 operator=(const deque&) = default;
162 operator=(deque&&) = default;
165 operator=(initializer_list<value_type> __l)
168 this->_M_invalidate_all();
173 #if __cplusplus >= 201103L
174 template<class _InputIterator,
175 typename = std::_RequireInputIter<_InputIterator>>
177 template<class _InputIterator>
180 assign(_InputIterator __first, _InputIterator __last)
182 typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
183 __glibcxx_check_valid_range2(__first, __last, __dist);
184 if (__dist.second >= __gnu_debug::__dp_sign)
185 _Base::assign(__gnu_debug::__unsafe(__first),
186 __gnu_debug::__unsafe(__last));
188 _Base::assign(__first, __last);
190 this->_M_invalidate_all();
194 assign(size_type __n, const _Tp& __t)
196 _Base::assign(__n, __t);
197 this->_M_invalidate_all();
200 #if __cplusplus >= 201103L
202 assign(initializer_list<value_type> __l)
205 this->_M_invalidate_all();
209 using _Base::get_allocator;
213 begin() _GLIBCXX_NOEXCEPT
214 { return iterator(_Base::begin(), this); }
217 begin() const _GLIBCXX_NOEXCEPT
218 { return const_iterator(_Base::begin(), this); }
221 end() _GLIBCXX_NOEXCEPT
222 { return iterator(_Base::end(), this); }
225 end() const _GLIBCXX_NOEXCEPT
226 { return const_iterator(_Base::end(), this); }
229 rbegin() _GLIBCXX_NOEXCEPT
230 { return reverse_iterator(end()); }
232 const_reverse_iterator
233 rbegin() const _GLIBCXX_NOEXCEPT
234 { return const_reverse_iterator(end()); }
237 rend() _GLIBCXX_NOEXCEPT
238 { return reverse_iterator(begin()); }
240 const_reverse_iterator
241 rend() const _GLIBCXX_NOEXCEPT
242 { return const_reverse_iterator(begin()); }
244 #if __cplusplus >= 201103L
246 cbegin() const noexcept
247 { return const_iterator(_Base::begin(), this); }
250 cend() const noexcept
251 { return const_iterator(_Base::end(), this); }
253 const_reverse_iterator
254 crbegin() const noexcept
255 { return const_reverse_iterator(end()); }
257 const_reverse_iterator
258 crend() const noexcept
259 { return const_reverse_iterator(begin()); }
264 _M_invalidate_after_nth(difference_type __n)
266 typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth;
267 this->_M_invalidate_if(_After_nth(__n, _Base::begin()));
271 // 23.2.1.2 capacity:
273 using _Base::max_size;
275 #if __cplusplus >= 201103L
277 resize(size_type __sz)
279 bool __invalidate_all = __sz > this->size();
280 if (__sz < this->size())
281 this->_M_invalidate_after_nth(__sz);
285 if (__invalidate_all)
286 this->_M_invalidate_all();
290 resize(size_type __sz, const _Tp& __c)
292 bool __invalidate_all = __sz > this->size();
293 if (__sz < this->size())
294 this->_M_invalidate_after_nth(__sz);
296 _Base::resize(__sz, __c);
298 if (__invalidate_all)
299 this->_M_invalidate_all();
303 resize(size_type __sz, _Tp __c = _Tp())
305 bool __invalidate_all = __sz > this->size();
306 if (__sz < this->size())
307 this->_M_invalidate_after_nth(__sz);
309 _Base::resize(__sz, __c);
311 if (__invalidate_all)
312 this->_M_invalidate_all();
316 #if __cplusplus >= 201103L
318 shrink_to_fit() noexcept
320 if (_Base::_M_shrink_to_fit())
321 this->_M_invalidate_all();
329 operator[](size_type __n) _GLIBCXX_NOEXCEPT
331 __glibcxx_check_subscript(__n);
332 return _M_base()[__n];
336 operator[](size_type __n) const _GLIBCXX_NOEXCEPT
338 __glibcxx_check_subscript(__n);
339 return _M_base()[__n];
345 front() _GLIBCXX_NOEXCEPT
347 __glibcxx_check_nonempty();
348 return _Base::front();
352 front() const _GLIBCXX_NOEXCEPT
354 __glibcxx_check_nonempty();
355 return _Base::front();
359 back() _GLIBCXX_NOEXCEPT
361 __glibcxx_check_nonempty();
362 return _Base::back();
366 back() const _GLIBCXX_NOEXCEPT
368 __glibcxx_check_nonempty();
369 return _Base::back();
372 // 23.2.1.3 modifiers:
374 push_front(const _Tp& __x)
376 _Base::push_front(__x);
377 this->_M_invalidate_all();
381 push_back(const _Tp& __x)
383 _Base::push_back(__x);
384 this->_M_invalidate_all();
387 #if __cplusplus >= 201103L
389 push_front(_Tp&& __x)
390 { emplace_front(std::move(__x)); }
394 { emplace_back(std::move(__x)); }
396 template<typename... _Args>
397 #if __cplusplus > 201402L
402 emplace_front(_Args&&... __args)
404 _Base::emplace_front(std::forward<_Args>(__args)...);
405 this->_M_invalidate_all();
406 #if __cplusplus > 201402L
411 template<typename... _Args>
412 #if __cplusplus > 201402L
417 emplace_back(_Args&&... __args)
419 _Base::emplace_back(std::forward<_Args>(__args)...);
420 this->_M_invalidate_all();
421 #if __cplusplus > 201402L
426 template<typename... _Args>
428 emplace(const_iterator __position, _Args&&... __args)
430 __glibcxx_check_insert(__position);
431 _Base_iterator __res = _Base::emplace(__position.base(),
432 std::forward<_Args>(__args)...);
433 this->_M_invalidate_all();
434 return iterator(__res, this);
439 #if __cplusplus >= 201103L
440 insert(const_iterator __position, const _Tp& __x)
442 insert(iterator __position, const _Tp& __x)
445 __glibcxx_check_insert(__position);
446 _Base_iterator __res = _Base::insert(__position.base(), __x);
447 this->_M_invalidate_all();
448 return iterator(__res, this);
451 #if __cplusplus >= 201103L
453 insert(const_iterator __position, _Tp&& __x)
454 { return emplace(__position, std::move(__x)); }
457 insert(const_iterator __position, initializer_list<value_type> __l)
459 __glibcxx_check_insert(__position);
460 _Base_iterator __res = _Base::insert(__position.base(), __l);
461 this->_M_invalidate_all();
462 return iterator(__res, this);
466 #if __cplusplus >= 201103L
468 insert(const_iterator __position, size_type __n, const _Tp& __x)
470 __glibcxx_check_insert(__position);
471 _Base_iterator __res = _Base::insert(__position.base(), __n, __x);
472 this->_M_invalidate_all();
473 return iterator(__res, this);
477 insert(iterator __position, size_type __n, const _Tp& __x)
479 __glibcxx_check_insert(__position);
480 _Base::insert(__position.base(), __n, __x);
481 this->_M_invalidate_all();
485 #if __cplusplus >= 201103L
486 template<class _InputIterator,
487 typename = std::_RequireInputIter<_InputIterator>>
489 insert(const_iterator __position,
490 _InputIterator __first, _InputIterator __last)
492 typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
493 __glibcxx_check_insert_range(__position, __first, __last, __dist);
494 _Base_iterator __res;
495 if (__dist.second >= __gnu_debug::__dp_sign)
496 __res = _Base::insert(__position.base(),
497 __gnu_debug::__unsafe(__first),
498 __gnu_debug::__unsafe(__last));
500 __res = _Base::insert(__position.base(), __first, __last);
502 this->_M_invalidate_all();
503 return iterator(__res, this);
506 template<class _InputIterator>
508 insert(iterator __position,
509 _InputIterator __first, _InputIterator __last)
511 typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
512 __glibcxx_check_insert_range(__position, __first, __last, __dist);
514 if (__dist.second >= __gnu_debug::__dp_sign)
515 _Base::insert(__position.base(),
516 __gnu_debug::__unsafe(__first),
517 __gnu_debug::__unsafe(__last));
519 _Base::insert(__position.base(), __first, __last);
521 this->_M_invalidate_all();
526 pop_front() _GLIBCXX_NOEXCEPT
528 __glibcxx_check_nonempty();
529 this->_M_invalidate_if(_Equal(_Base::begin()));
534 pop_back() _GLIBCXX_NOEXCEPT
536 __glibcxx_check_nonempty();
537 this->_M_invalidate_if(_Equal(--_Base::end()));
542 #if __cplusplus >= 201103L
543 erase(const_iterator __position)
545 erase(iterator __position)
548 __glibcxx_check_erase(__position);
549 #if __cplusplus >= 201103L
550 _Base_const_iterator __victim = __position.base();
552 _Base_iterator __victim = __position.base();
554 if (__victim == _Base::begin() || __victim == _Base::end() - 1)
556 this->_M_invalidate_if(_Equal(__victim));
557 return iterator(_Base::erase(__victim), this);
561 _Base_iterator __res = _Base::erase(__victim);
562 this->_M_invalidate_all();
563 return iterator(__res, this);
568 #if __cplusplus >= 201103L
569 erase(const_iterator __first, const_iterator __last)
571 erase(iterator __first, iterator __last)
574 // _GLIBCXX_RESOLVE_LIB_DEFECTS
575 // 151. can't currently clear() empty container
576 __glibcxx_check_erase_range(__first, __last);
578 if (__first.base() == __last.base())
579 #if __cplusplus >= 201103L
580 return iterator(__first.base()._M_const_cast(), this);
584 else if (__first.base() == _Base::begin()
585 || __last.base() == _Base::end())
587 this->_M_detach_singular();
588 for (_Base_const_iterator __position = __first.base();
589 __position != __last.base(); ++__position)
591 this->_M_invalidate_if(_Equal(__position));
595 return iterator(_Base::erase(__first.base(), __last.base()),
600 this->_M_revalidate_singular();
601 __throw_exception_again;
606 _Base_iterator __res = _Base::erase(__first.base(),
608 this->_M_invalidate_all();
609 return iterator(__res, this);
615 _GLIBCXX_NOEXCEPT_IF( noexcept(declval<_Base&>().swap(__x)) )
622 clear() _GLIBCXX_NOEXCEPT
625 this->_M_invalidate_all();
629 _M_base() _GLIBCXX_NOEXCEPT { return *this; }
632 _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
635 #if __cpp_deduction_guides >= 201606
636 template<typename _InputIterator, typename _ValT
637 = typename iterator_traits<_InputIterator>::value_type,
638 typename _Allocator = allocator<_ValT>,
639 typename = _RequireInputIter<_InputIterator>,
640 typename = _RequireAllocator<_Allocator>>
641 deque(_InputIterator, _InputIterator, _Allocator = _Allocator())
642 -> deque<_ValT, _Allocator>;
645 template<typename _Tp, typename _Alloc>
647 operator==(const deque<_Tp, _Alloc>& __lhs,
648 const deque<_Tp, _Alloc>& __rhs)
649 { return __lhs._M_base() == __rhs._M_base(); }
651 template<typename _Tp, typename _Alloc>
653 operator!=(const deque<_Tp, _Alloc>& __lhs,
654 const deque<_Tp, _Alloc>& __rhs)
655 { return __lhs._M_base() != __rhs._M_base(); }
657 template<typename _Tp, typename _Alloc>
659 operator<(const deque<_Tp, _Alloc>& __lhs,
660 const deque<_Tp, _Alloc>& __rhs)
661 { return __lhs._M_base() < __rhs._M_base(); }
663 template<typename _Tp, typename _Alloc>
665 operator<=(const deque<_Tp, _Alloc>& __lhs,
666 const deque<_Tp, _Alloc>& __rhs)
667 { return __lhs._M_base() <= __rhs._M_base(); }
669 template<typename _Tp, typename _Alloc>
671 operator>=(const deque<_Tp, _Alloc>& __lhs,
672 const deque<_Tp, _Alloc>& __rhs)
673 { return __lhs._M_base() >= __rhs._M_base(); }
675 template<typename _Tp, typename _Alloc>
677 operator>(const deque<_Tp, _Alloc>& __lhs,
678 const deque<_Tp, _Alloc>& __rhs)
679 { return __lhs._M_base() > __rhs._M_base(); }
681 template<typename _Tp, typename _Alloc>
683 swap(deque<_Tp, _Alloc>& __lhs, deque<_Tp, _Alloc>& __rhs)
684 _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs.swap(__rhs)))
685 { __lhs.swap(__rhs); }
687 } // namespace __debug