1 // Debugging deque implementation -*- C++ -*-
3 // Copyright (C) 2003-2015 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>,
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> _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(const deque& __d, const _Allocator& __a)
94 deque(deque&& __d, const _Allocator& __a)
95 : _Safe(std::move(__d)), _Base(std::move(__d), __a) { }
97 deque(initializer_list<value_type> __l,
98 const allocator_type& __a = allocator_type())
105 deque(const _Allocator& __a)
108 #if __cplusplus >= 201103L
110 deque(size_type __n, const _Allocator& __a = _Allocator())
111 : _Base(__n, __a) { }
113 deque(size_type __n, const _Tp& __value,
114 const _Allocator& __a = _Allocator())
115 : _Base(__n, __value, __a) { }
118 deque(size_type __n, const _Tp& __value = _Tp(),
119 const _Allocator& __a = _Allocator())
120 : _Base(__n, __value, __a) { }
123 #if __cplusplus >= 201103L
124 template<class _InputIterator,
125 typename = std::_RequireInputIter<_InputIterator>>
127 template<class _InputIterator>
129 deque(_InputIterator __first, _InputIterator __last,
130 const _Allocator& __a = _Allocator())
131 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
133 __gnu_debug::__base(__last), __a)
136 deque(const _Base& __x)
139 #if __cplusplus < 201103L
141 operator=(const deque& __x)
143 this->_M_safe() = __x;
149 operator=(const deque&) = default;
152 operator=(deque&&) = default;
155 operator=(initializer_list<value_type> __l)
158 this->_M_invalidate_all();
163 #if __cplusplus >= 201103L
164 template<class _InputIterator,
165 typename = std::_RequireInputIter<_InputIterator>>
167 template<class _InputIterator>
170 assign(_InputIterator __first, _InputIterator __last)
172 __glibcxx_check_valid_range(__first, __last);
173 _Base::assign(__gnu_debug::__base(__first),
174 __gnu_debug::__base(__last));
175 this->_M_invalidate_all();
179 assign(size_type __n, const _Tp& __t)
181 _Base::assign(__n, __t);
182 this->_M_invalidate_all();
185 #if __cplusplus >= 201103L
187 assign(initializer_list<value_type> __l)
190 this->_M_invalidate_all();
194 using _Base::get_allocator;
198 begin() _GLIBCXX_NOEXCEPT
199 { return iterator(_Base::begin(), this); }
202 begin() const _GLIBCXX_NOEXCEPT
203 { return const_iterator(_Base::begin(), this); }
206 end() _GLIBCXX_NOEXCEPT
207 { return iterator(_Base::end(), this); }
210 end() const _GLIBCXX_NOEXCEPT
211 { return const_iterator(_Base::end(), this); }
214 rbegin() _GLIBCXX_NOEXCEPT
215 { return reverse_iterator(end()); }
217 const_reverse_iterator
218 rbegin() const _GLIBCXX_NOEXCEPT
219 { return const_reverse_iterator(end()); }
222 rend() _GLIBCXX_NOEXCEPT
223 { return reverse_iterator(begin()); }
225 const_reverse_iterator
226 rend() const _GLIBCXX_NOEXCEPT
227 { return const_reverse_iterator(begin()); }
229 #if __cplusplus >= 201103L
231 cbegin() const noexcept
232 { return const_iterator(_Base::begin(), this); }
235 cend() const noexcept
236 { return const_iterator(_Base::end(), this); }
238 const_reverse_iterator
239 crbegin() const noexcept
240 { return const_reverse_iterator(end()); }
242 const_reverse_iterator
243 crend() const noexcept
244 { return const_reverse_iterator(begin()); }
249 _M_invalidate_after_nth(difference_type __n)
251 typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth;
252 this->_M_invalidate_if(_After_nth(__n, _Base::begin()));
256 // 23.2.1.2 capacity:
258 using _Base::max_size;
260 #if __cplusplus >= 201103L
262 resize(size_type __sz)
264 bool __invalidate_all = __sz > this->size();
265 if (__sz < this->size())
266 this->_M_invalidate_after_nth(__sz);
270 if (__invalidate_all)
271 this->_M_invalidate_all();
275 resize(size_type __sz, const _Tp& __c)
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 resize(size_type __sz, _Tp __c = _Tp())
290 bool __invalidate_all = __sz > this->size();
291 if (__sz < this->size())
292 this->_M_invalidate_after_nth(__sz);
294 _Base::resize(__sz, __c);
296 if (__invalidate_all)
297 this->_M_invalidate_all();
301 #if __cplusplus >= 201103L
303 shrink_to_fit() noexcept
305 if (_Base::_M_shrink_to_fit())
306 this->_M_invalidate_all();
314 operator[](size_type __n) _GLIBCXX_NOEXCEPT
316 __glibcxx_check_subscript(__n);
317 return _M_base()[__n];
321 operator[](size_type __n) const _GLIBCXX_NOEXCEPT
323 __glibcxx_check_subscript(__n);
324 return _M_base()[__n];
330 front() _GLIBCXX_NOEXCEPT
332 __glibcxx_check_nonempty();
333 return _Base::front();
337 front() const _GLIBCXX_NOEXCEPT
339 __glibcxx_check_nonempty();
340 return _Base::front();
344 back() _GLIBCXX_NOEXCEPT
346 __glibcxx_check_nonempty();
347 return _Base::back();
351 back() const _GLIBCXX_NOEXCEPT
353 __glibcxx_check_nonempty();
354 return _Base::back();
357 // 23.2.1.3 modifiers:
359 push_front(const _Tp& __x)
361 _Base::push_front(__x);
362 this->_M_invalidate_all();
366 push_back(const _Tp& __x)
368 _Base::push_back(__x);
369 this->_M_invalidate_all();
372 #if __cplusplus >= 201103L
374 push_front(_Tp&& __x)
375 { emplace_front(std::move(__x)); }
379 { emplace_back(std::move(__x)); }
381 template<typename... _Args>
383 emplace_front(_Args&&... __args)
385 _Base::emplace_front(std::forward<_Args>(__args)...);
386 this->_M_invalidate_all();
389 template<typename... _Args>
391 emplace_back(_Args&&... __args)
393 _Base::emplace_back(std::forward<_Args>(__args)...);
394 this->_M_invalidate_all();
397 template<typename... _Args>
399 emplace(const_iterator __position, _Args&&... __args)
401 __glibcxx_check_insert(__position);
402 _Base_iterator __res = _Base::emplace(__position.base(),
403 std::forward<_Args>(__args)...);
404 this->_M_invalidate_all();
405 return iterator(__res, this);
410 #if __cplusplus >= 201103L
411 insert(const_iterator __position, const _Tp& __x)
413 insert(iterator __position, const _Tp& __x)
416 __glibcxx_check_insert(__position);
417 _Base_iterator __res = _Base::insert(__position.base(), __x);
418 this->_M_invalidate_all();
419 return iterator(__res, this);
422 #if __cplusplus >= 201103L
424 insert(const_iterator __position, _Tp&& __x)
425 { return emplace(__position, std::move(__x)); }
428 insert(const_iterator __position, initializer_list<value_type> __l)
430 __glibcxx_check_insert(__position);
431 _Base_iterator __res = _Base::insert(__position.base(), __l);
432 this->_M_invalidate_all();
433 return iterator(__res, this);
437 #if __cplusplus >= 201103L
439 insert(const_iterator __position, size_type __n, const _Tp& __x)
441 __glibcxx_check_insert(__position);
442 _Base_iterator __res = _Base::insert(__position.base(), __n, __x);
443 this->_M_invalidate_all();
444 return iterator(__res, this);
448 insert(iterator __position, size_type __n, const _Tp& __x)
450 __glibcxx_check_insert(__position);
451 _Base::insert(__position.base(), __n, __x);
452 this->_M_invalidate_all();
456 #if __cplusplus >= 201103L
457 template<class _InputIterator,
458 typename = std::_RequireInputIter<_InputIterator>>
460 insert(const_iterator __position,
461 _InputIterator __first, _InputIterator __last)
463 __glibcxx_check_insert_range(__position, __first, __last);
464 _Base_iterator __res = _Base::insert(__position.base(),
465 __gnu_debug::__base(__first),
466 __gnu_debug::__base(__last));
467 this->_M_invalidate_all();
468 return iterator(__res, this);
471 template<class _InputIterator>
473 insert(iterator __position,
474 _InputIterator __first, _InputIterator __last)
476 __glibcxx_check_insert_range(__position, __first, __last);
477 _Base::insert(__position.base(), __gnu_debug::__base(__first),
478 __gnu_debug::__base(__last));
479 this->_M_invalidate_all();
484 pop_front() _GLIBCXX_NOEXCEPT
486 __glibcxx_check_nonempty();
487 this->_M_invalidate_if(_Equal(_Base::begin()));
492 pop_back() _GLIBCXX_NOEXCEPT
494 __glibcxx_check_nonempty();
495 this->_M_invalidate_if(_Equal(--_Base::end()));
500 #if __cplusplus >= 201103L
501 erase(const_iterator __position)
503 erase(iterator __position)
506 __glibcxx_check_erase(__position);
507 #if __cplusplus >= 201103L
508 _Base_const_iterator __victim = __position.base();
510 _Base_iterator __victim = __position.base();
512 if (__victim == _Base::begin() || __victim == _Base::end() - 1)
514 this->_M_invalidate_if(_Equal(__victim));
515 return iterator(_Base::erase(__victim), this);
519 _Base_iterator __res = _Base::erase(__victim);
520 this->_M_invalidate_all();
521 return iterator(__res, this);
526 #if __cplusplus >= 201103L
527 erase(const_iterator __first, const_iterator __last)
529 erase(iterator __first, iterator __last)
532 // _GLIBCXX_RESOLVE_LIB_DEFECTS
533 // 151. can't currently clear() empty container
534 __glibcxx_check_erase_range(__first, __last);
536 if (__first.base() == __last.base())
537 #if __cplusplus >= 201103L
538 return iterator(__first.base()._M_const_cast(), this);
542 else if (__first.base() == _Base::begin()
543 || __last.base() == _Base::end())
545 this->_M_detach_singular();
546 for (_Base_const_iterator __position = __first.base();
547 __position != __last.base(); ++__position)
549 this->_M_invalidate_if(_Equal(__position));
553 return iterator(_Base::erase(__first.base(), __last.base()),
558 this->_M_revalidate_singular();
559 __throw_exception_again;
564 _Base_iterator __res = _Base::erase(__first.base(),
566 this->_M_invalidate_all();
567 return iterator(__res, this);
573 #if __cplusplus >= 201103L
574 noexcept( noexcept(declval<_Base>().swap(__x)) )
582 clear() _GLIBCXX_NOEXCEPT
585 this->_M_invalidate_all();
589 _M_base() _GLIBCXX_NOEXCEPT { return *this; }
592 _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
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 operator>(const deque<_Tp, _Alloc>& __lhs,
628 const deque<_Tp, _Alloc>& __rhs)
629 { return __lhs._M_base() > __rhs._M_base(); }
631 template<typename _Tp, typename _Alloc>
633 swap(deque<_Tp, _Alloc>& __lhs, deque<_Tp, _Alloc>& __rhs)
634 { __lhs.swap(__rhs); }
636 } // namespace __debug