2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
19 template <class T, class Allocator = allocator<T> >
25 typedef Allocator allocator_type;
27 typedef typename allocator_type::reference reference;
28 typedef typename allocator_type::const_reference const_reference;
29 typedef implementation-defined iterator;
30 typedef implementation-defined const_iterator;
31 typedef typename allocator_type::size_type size_type;
32 typedef typename allocator_type::difference_type difference_type;
34 typedef typename allocator_type::pointer pointer;
35 typedef typename allocator_type::const_pointer const_pointer;
36 typedef std::reverse_iterator<iterator> reverse_iterator;
37 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
39 // construct/copy/destroy:
40 deque() noexcept(is_nothrow_default_constructible<allocator_type>::value);
41 explicit deque(const allocator_type& a);
42 explicit deque(size_type n);
43 explicit deque(size_type n, const allocator_type& a); // C++14
44 deque(size_type n, const value_type& v);
45 deque(size_type n, const value_type& v, const allocator_type& a);
46 template <class InputIterator>
47 deque(InputIterator f, InputIterator l);
48 template <class InputIterator>
49 deque(InputIterator f, InputIterator l, const allocator_type& a);
50 template<container-compatible-range<T> R>
51 deque(from_range_t, R&& rg, const Allocator& = Allocator()); // C++23
52 deque(const deque& c);
54 noexcept(is_nothrow_move_constructible<allocator_type>::value);
55 deque(initializer_list<value_type> il, const Allocator& a = allocator_type());
56 deque(const deque& c, const allocator_type& a);
57 deque(deque&& c, const allocator_type& a);
60 deque& operator=(const deque& c);
61 deque& operator=(deque&& c)
63 allocator_type::propagate_on_container_move_assignment::value &&
64 is_nothrow_move_assignable<allocator_type>::value);
65 deque& operator=(initializer_list<value_type> il);
67 template <class InputIterator>
68 void assign(InputIterator f, InputIterator l);
69 template<container-compatible-range<T> R>
70 void assign_range(R&& rg); // C++23
71 void assign(size_type n, const value_type& v);
72 void assign(initializer_list<value_type> il);
74 allocator_type get_allocator() const noexcept;
78 iterator begin() noexcept;
79 const_iterator begin() const noexcept;
80 iterator end() noexcept;
81 const_iterator end() const noexcept;
83 reverse_iterator rbegin() noexcept;
84 const_reverse_iterator rbegin() const noexcept;
85 reverse_iterator rend() noexcept;
86 const_reverse_iterator rend() const noexcept;
88 const_iterator cbegin() const noexcept;
89 const_iterator cend() const noexcept;
90 const_reverse_iterator crbegin() const noexcept;
91 const_reverse_iterator crend() const noexcept;
94 size_type size() const noexcept;
95 size_type max_size() const noexcept;
96 void resize(size_type n);
97 void resize(size_type n, const value_type& v);
99 bool empty() const noexcept;
102 reference operator[](size_type i);
103 const_reference operator[](size_type i) const;
104 reference at(size_type i);
105 const_reference at(size_type i) const;
107 const_reference front() const;
109 const_reference back() const;
112 void push_front(const value_type& v);
113 void push_front(value_type&& v);
114 template<container-compatible-range<T> R>
115 void prepend_range(R&& rg); // C++23
116 void push_back(const value_type& v);
117 void push_back(value_type&& v);
118 template<container-compatible-range<T> R>
119 void append_range(R&& rg); // C++23
120 template <class... Args> reference emplace_front(Args&&... args); // reference in C++17
121 template <class... Args> reference emplace_back(Args&&... args); // reference in C++17
122 template <class... Args> iterator emplace(const_iterator p, Args&&... args);
123 iterator insert(const_iterator p, const value_type& v);
124 iterator insert(const_iterator p, value_type&& v);
125 iterator insert(const_iterator p, size_type n, const value_type& v);
126 template <class InputIterator>
127 iterator insert(const_iterator p, InputIterator f, InputIterator l);
128 template<container-compatible-range<T> R>
129 iterator insert_range(const_iterator position, R&& rg); // C++23
130 iterator insert(const_iterator p, initializer_list<value_type> il);
133 iterator erase(const_iterator p);
134 iterator erase(const_iterator f, const_iterator l);
136 noexcept(allocator_traits<allocator_type>::is_always_equal::value); // C++17
137 void clear() noexcept;
140 template <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
141 deque(InputIterator, InputIterator, Allocator = Allocator())
142 -> deque<typename iterator_traits<InputIterator>::value_type, Allocator>; // C++17
144 template<ranges::input_range R, class Allocator = allocator<ranges::range_value_t<R>>>
145 deque(from_range_t, R&&, Allocator = Allocator())
146 -> deque<ranges::range_value_t<R>, Allocator>; // C++23
148 template <class T, class Allocator>
149 bool operator==(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
150 template <class T, class Allocator>
151 bool operator< (const deque<T,Allocator>& x, const deque<T,Allocator>& y); // removed in C++20
152 template <class T, class Allocator>
153 bool operator!=(const deque<T,Allocator>& x, const deque<T,Allocator>& y); // removed in C++20
154 template <class T, class Allocator>
155 bool operator> (const deque<T,Allocator>& x, const deque<T,Allocator>& y); // removed in C++20
156 template <class T, class Allocator>
157 bool operator>=(const deque<T,Allocator>& x, const deque<T,Allocator>& y); // removed in C++20
158 template <class T, class Allocator>
159 bool operator<=(const deque<T,Allocator>& x, const deque<T,Allocator>& y); // removed in C++20
160 template<class T, class Allocator>
161 synth-three-way-result<T> operator<=>(const deque<T, Allocator>& x,
162 const deque<T, Allocator>& y); // since C++20
164 // specialized algorithms:
165 template <class T, class Allocator>
166 void swap(deque<T,Allocator>& x, deque<T,Allocator>& y)
167 noexcept(noexcept(x.swap(y)));
169 template <class T, class Allocator, class U>
170 typename deque<T, Allocator>::size_type
171 erase(deque<T, Allocator>& c, const U& value); // C++20
172 template <class T, class Allocator, class Predicate>
173 typename deque<T, Allocator>::size_type
174 erase_if(deque<T, Allocator>& c, Predicate pred); // C++20
180 #include <__algorithm/copy.h>
181 #include <__algorithm/copy_backward.h>
182 #include <__algorithm/copy_n.h>
183 #include <__algorithm/equal.h>
184 #include <__algorithm/fill_n.h>
185 #include <__algorithm/lexicographical_compare.h>
186 #include <__algorithm/lexicographical_compare_three_way.h>
187 #include <__algorithm/min.h>
188 #include <__algorithm/remove.h>
189 #include <__algorithm/remove_if.h>
190 #include <__algorithm/unwrap_iter.h>
191 #include <__assert> // all public C++ headers provide the assertion handler
192 #include <__availability>
194 #include <__format/enable_insertable.h>
195 #include <__iterator/distance.h>
196 #include <__iterator/iterator_traits.h>
197 #include <__iterator/next.h>
198 #include <__iterator/prev.h>
199 #include <__iterator/reverse_iterator.h>
200 #include <__iterator/segmented_iterator.h>
201 #include <__memory/addressof.h>
202 #include <__memory/allocator_destructor.h>
203 #include <__memory/pointer_traits.h>
204 #include <__memory/temp_value.h>
205 #include <__memory/unique_ptr.h>
206 #include <__memory_resource/polymorphic_allocator.h>
207 #include <__ranges/access.h>
208 #include <__ranges/concepts.h>
209 #include <__ranges/container_compatible_range.h>
210 #include <__ranges/from_range.h>
211 #include <__ranges/size.h>
212 #include <__split_buffer>
213 #include <__type_traits/is_allocator.h>
214 #include <__type_traits/is_convertible.h>
215 #include <__type_traits/is_same.h>
216 #include <__type_traits/type_identity.h>
217 #include <__utility/forward.h>
218 #include <__utility/move.h>
219 #include <__utility/pair.h>
220 #include <__utility/swap.h>
225 // standard-mandated includes
228 #include <__iterator/access.h>
229 #include <__iterator/data.h>
230 #include <__iterator/empty.h>
231 #include <__iterator/reverse_access.h>
232 #include <__iterator/size.h>
236 #include <initializer_list>
238 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
239 # pragma GCC system_header
243 #include <__undef_macros>
246 _LIBCPP_BEGIN_NAMESPACE_STD
248 template <class _Tp, class _Allocator = allocator<_Tp> > class _LIBCPP_TEMPLATE_VIS deque;
250 template <class _ValueType, class _DiffType>
251 struct __deque_block_size {
252 static const _DiffType value = sizeof(_ValueType) < 256 ? 4096 / sizeof(_ValueType) : 16;
255 template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
256 class _DiffType, _DiffType _BS =
257 #ifdef _LIBCPP_ABI_INCOMPLETE_TYPES_IN_DEQUE
258 // Keep template parameter to avoid changing all template declarations thoughout
262 __deque_block_size<_ValueType, _DiffType>::value
265 class _LIBCPP_TEMPLATE_VIS __deque_iterator
267 typedef _MapPointer __map_iterator;
269 typedef _Pointer pointer;
270 typedef _DiffType difference_type;
272 __map_iterator __m_iter_;
275 static const difference_type __block_size;
277 typedef _ValueType value_type;
278 typedef random_access_iterator_tag iterator_category;
279 typedef _Reference reference;
281 _LIBCPP_HIDE_FROM_ABI __deque_iterator() _NOEXCEPT
282 #if _LIBCPP_STD_VER >= 14
283 : __m_iter_(nullptr), __ptr_(nullptr)
287 template <class _Pp, class _Rp, class _MP>
288 _LIBCPP_HIDE_FROM_ABI
289 __deque_iterator(const __deque_iterator<value_type, _Pp, _Rp, _MP, difference_type, _BS>& __it,
290 typename enable_if<is_convertible<_Pp, pointer>::value>::type* = 0) _NOEXCEPT
291 : __m_iter_(__it.__m_iter_), __ptr_(__it.__ptr_) {}
293 _LIBCPP_HIDE_FROM_ABI reference operator*() const {return *__ptr_;}
294 _LIBCPP_HIDE_FROM_ABI pointer operator->() const {return __ptr_;}
296 _LIBCPP_HIDE_FROM_ABI __deque_iterator& operator++()
298 if (++__ptr_ - *__m_iter_ == __block_size)
306 _LIBCPP_HIDE_FROM_ABI __deque_iterator operator++(int)
308 __deque_iterator __tmp = *this;
313 _LIBCPP_HIDE_FROM_ABI __deque_iterator& operator--()
315 if (__ptr_ == *__m_iter_)
318 __ptr_ = *__m_iter_ + __block_size;
324 _LIBCPP_HIDE_FROM_ABI __deque_iterator operator--(int)
326 __deque_iterator __tmp = *this;
331 _LIBCPP_HIDE_FROM_ABI __deque_iterator& operator+=(difference_type __n)
335 __n += __ptr_ - *__m_iter_;
338 __m_iter_ += __n / __block_size;
339 __ptr_ = *__m_iter_ + __n % __block_size;
343 difference_type __z = __block_size - 1 - __n;
344 __m_iter_ -= __z / __block_size;
345 __ptr_ = *__m_iter_ + (__block_size - 1 - __z % __block_size);
351 _LIBCPP_HIDE_FROM_ABI __deque_iterator& operator-=(difference_type __n)
353 return *this += -__n;
356 _LIBCPP_HIDE_FROM_ABI __deque_iterator operator+(difference_type __n) const
358 __deque_iterator __t(*this);
363 _LIBCPP_HIDE_FROM_ABI __deque_iterator operator-(difference_type __n) const
365 __deque_iterator __t(*this);
370 _LIBCPP_HIDE_FROM_ABI
371 friend __deque_iterator operator+(difference_type __n, const __deque_iterator& __it)
374 _LIBCPP_HIDE_FROM_ABI
375 friend difference_type operator-(const __deque_iterator& __x, const __deque_iterator& __y)
378 return (__x.__m_iter_ - __y.__m_iter_) * __block_size
379 + (__x.__ptr_ - *__x.__m_iter_)
380 - (__y.__ptr_ - *__y.__m_iter_);
384 _LIBCPP_HIDE_FROM_ABI reference operator[](difference_type __n) const
385 {return *(*this + __n);}
387 _LIBCPP_HIDE_FROM_ABI friend
388 bool operator==(const __deque_iterator& __x, const __deque_iterator& __y)
389 {return __x.__ptr_ == __y.__ptr_;}
391 _LIBCPP_HIDE_FROM_ABI friend
392 bool operator!=(const __deque_iterator& __x, const __deque_iterator& __y)
393 {return !(__x == __y);}
395 _LIBCPP_HIDE_FROM_ABI friend
396 bool operator<(const __deque_iterator& __x, const __deque_iterator& __y)
397 {return __x.__m_iter_ < __y.__m_iter_ ||
398 (__x.__m_iter_ == __y.__m_iter_ && __x.__ptr_ < __y.__ptr_);}
400 _LIBCPP_HIDE_FROM_ABI friend
401 bool operator>(const __deque_iterator& __x, const __deque_iterator& __y)
404 _LIBCPP_HIDE_FROM_ABI friend
405 bool operator<=(const __deque_iterator& __x, const __deque_iterator& __y)
406 {return !(__y < __x);}
408 _LIBCPP_HIDE_FROM_ABI friend
409 bool operator>=(const __deque_iterator& __x, const __deque_iterator& __y)
410 {return !(__x < __y);}
413 _LIBCPP_HIDE_FROM_ABI explicit __deque_iterator(__map_iterator __m, pointer __p) _NOEXCEPT
414 : __m_iter_(__m), __ptr_(__p) {}
416 template <class _Tp, class _Ap> friend class _LIBCPP_TEMPLATE_VIS deque;
417 template <class _Vp, class _Pp, class _Rp, class _MP, class _Dp, _Dp>
418 friend class _LIBCPP_TEMPLATE_VIS __deque_iterator;
421 friend struct __segmented_iterator_traits;
424 template <class _ValueType, class _Pointer, class _Reference, class _MapPointer, class _DiffType, _DiffType _BlockSize>
425 struct __segmented_iterator_traits<
426 __deque_iterator<_ValueType, _Pointer, _Reference, _MapPointer, _DiffType, _BlockSize> > {
428 using _Iterator = __deque_iterator<_ValueType, _Pointer, _Reference, _MapPointer, _DiffType, _BlockSize>;
431 using __is_segmented_iterator = true_type;
432 using __segment_iterator = _MapPointer;
433 using __local_iterator = _Pointer;
435 static _LIBCPP_HIDE_FROM_ABI __segment_iterator __segment(_Iterator __iter) { return __iter.__m_iter_; }
436 static _LIBCPP_HIDE_FROM_ABI __local_iterator __local(_Iterator __iter) { return __iter.__ptr_; }
437 static _LIBCPP_HIDE_FROM_ABI __local_iterator __begin(__segment_iterator __iter) { return *__iter; }
439 static _LIBCPP_HIDE_FROM_ABI __local_iterator __end(__segment_iterator __iter) {
440 return *__iter + _Iterator::__block_size;
443 static _LIBCPP_HIDE_FROM_ABI _Iterator __compose(__segment_iterator __segment, __local_iterator __local) {
444 if (__local == __end(__segment)) {
446 return _Iterator(__segment, *__segment);
448 return _Iterator(__segment, __local);
452 template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
453 class _DiffType, _DiffType _BlockSize>
454 const _DiffType __deque_iterator<_ValueType, _Pointer, _Reference, _MapPointer,
455 _DiffType, _BlockSize>::__block_size =
456 __deque_block_size<_ValueType, _DiffType>::value;
458 template <class _Tp, class _Allocator /*= allocator<_Tp>*/>
459 class _LIBCPP_TEMPLATE_VIS deque
464 using value_type = _Tp;
466 static_assert((is_same<typename _Allocator::value_type, value_type>::value),
467 "Allocator::value_type must be same type as value_type");
469 using allocator_type = _Allocator;
470 using __alloc_traits = allocator_traits<allocator_type>;
472 using size_type = typename __alloc_traits::size_type;
473 using difference_type = typename __alloc_traits::difference_type;
475 using pointer = typename __alloc_traits::pointer;
476 using const_pointer = typename __alloc_traits::const_pointer;
478 using __pointer_allocator = __rebind_alloc<__alloc_traits, pointer>;
479 using __const_pointer_allocator = __rebind_alloc<__alloc_traits, const_pointer>;
480 using __map = __split_buffer<pointer, __pointer_allocator>;
481 using __map_alloc_traits = allocator_traits<__pointer_allocator>;
482 using __map_pointer = typename __map_alloc_traits::pointer;
483 using __map_const_pointer = typename allocator_traits<__const_pointer_allocator>::const_pointer;
484 using __map_const_iterator = typename __map::const_iterator;
486 using reference = value_type&;
487 using const_reference = const value_type&;
489 using iterator = __deque_iterator<value_type, pointer, reference, __map_pointer, difference_type>;
490 using const_iterator =
491 __deque_iterator<value_type, const_pointer, const_reference, __map_const_pointer, difference_type>;
492 using reverse_iterator = std::reverse_iterator<iterator>;
493 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
495 static_assert(is_same<allocator_type, __rebind_alloc<__alloc_traits, value_type> >::value,
496 "[allocator.requirements] states that rebinding an allocator to the same type should result in the "
497 "original allocator");
498 static_assert(is_nothrow_default_constructible<allocator_type>::value ==
499 is_nothrow_default_constructible<__pointer_allocator>::value,
500 "rebinding an allocator should not change excpetion guarantees");
501 static_assert(is_nothrow_move_constructible<allocator_type>::value ==
502 is_nothrow_move_constructible<typename __map::allocator_type>::value,
503 "rebinding an allocator should not change excpetion guarantees");
506 struct __deque_block_range {
507 explicit _LIBCPP_HIDE_FROM_ABI
508 __deque_block_range(pointer __b, pointer __e) _NOEXCEPT : __begin_(__b), __end_(__e) {}
509 const pointer __begin_;
510 const pointer __end_;
513 struct __deque_range {
515 const iterator __end_;
517 _LIBCPP_HIDE_FROM_ABI __deque_range(iterator __pos, iterator __e) _NOEXCEPT
518 : __pos_(__pos), __end_(__e) {}
520 explicit _LIBCPP_HIDE_FROM_ABI operator bool() const _NOEXCEPT {
521 return __pos_ != __end_;
524 _LIBCPP_HIDE_FROM_ABI __deque_range begin() const {
528 _LIBCPP_HIDE_FROM_ABI __deque_range end() const {
529 return __deque_range(__end_, __end_);
531 _LIBCPP_HIDE_FROM_ABI __deque_block_range operator*() const _NOEXCEPT {
532 if (__pos_.__m_iter_ == __end_.__m_iter_) {
533 return __deque_block_range(__pos_.__ptr_, __end_.__ptr_);
535 return __deque_block_range(__pos_.__ptr_, *__pos_.__m_iter_ + __block_size);
538 _LIBCPP_HIDE_FROM_ABI __deque_range& operator++() _NOEXCEPT {
539 if (__pos_.__m_iter_ == __end_.__m_iter_) {
543 __pos_.__ptr_ = *__pos_.__m_iter_;
549 _LIBCPP_HIDE_FROM_ABI friend bool operator==(__deque_range const& __lhs, __deque_range const& __rhs) {
550 return __lhs.__pos_ == __rhs.__pos_;
552 _LIBCPP_HIDE_FROM_ABI friend bool operator!=(__deque_range const& __lhs, __deque_range const& __rhs) {
553 return !(__lhs == __rhs);
557 struct _ConstructTransaction {
558 _LIBCPP_HIDE_FROM_ABI _ConstructTransaction(deque* __db, __deque_block_range& __r)
559 : __pos_(__r.__begin_), __end_(__r.__end_), __begin_(__r.__begin_), __base_(__db) {}
562 _LIBCPP_HIDE_FROM_ABI ~_ConstructTransaction() {
563 __base_->__size() += (__pos_ - __begin_);
567 const pointer __end_;
569 const pointer __begin_;
570 deque* const __base_;
573 static const difference_type __block_size;
577 __compressed_pair<size_type, allocator_type> __size_;
581 // construct/copy/destroy:
582 _LIBCPP_HIDE_FROM_ABI
583 deque() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
584 : __start_(0), __size_(0, __default_init_tag()) {
588 _LIBCPP_HIDE_FROM_ABI ~deque() {
591 typename __map::iterator __i = __map_.begin();
592 typename __map::iterator __e = __map_.end();
593 for (; __i != __e; ++__i)
594 __alloc_traits::deallocate(__alloc(), *__i, __block_size);
597 _LIBCPP_HIDE_FROM_ABI explicit deque(const allocator_type& __a)
598 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a) {
602 explicit _LIBCPP_HIDE_FROM_ABI deque(size_type __n);
603 #if _LIBCPP_STD_VER >= 14
604 explicit _LIBCPP_HIDE_FROM_ABI deque(size_type __n, const _Allocator& __a);
606 _LIBCPP_HIDE_FROM_ABI deque(size_type __n, const value_type& __v);
608 template <class = __enable_if_t<__is_allocator<_Allocator>::value> >
609 _LIBCPP_HIDE_FROM_ABI deque(size_type __n, const value_type& __v, const allocator_type& __a)
610 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a)
617 template <class _InputIter>
618 _LIBCPP_HIDE_FROM_ABI deque(_InputIter __f, _InputIter __l,
619 typename enable_if<__has_input_iterator_category<_InputIter>::value>::type* = 0);
620 template <class _InputIter>
621 _LIBCPP_HIDE_FROM_ABI deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
622 typename enable_if<__has_input_iterator_category<_InputIter>::value>::type* = 0);
624 #if _LIBCPP_STD_VER >= 23
625 template <_ContainerCompatibleRange<_Tp> _Range>
626 _LIBCPP_HIDE_FROM_ABI deque(from_range_t, _Range&& __range,
627 const allocator_type& __a = allocator_type())
628 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a) {
629 if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
630 __append_with_size(ranges::begin(__range), ranges::distance(__range));
633 for (auto&& __e : __range) {
634 emplace_back(std::forward<decltype(__e)>(__e));
640 _LIBCPP_HIDE_FROM_ABI deque(const deque& __c);
641 _LIBCPP_HIDE_FROM_ABI deque(const deque& __c, const __type_identity_t<allocator_type>& __a);
643 _LIBCPP_HIDE_FROM_ABI deque& operator=(const deque& __c);
645 #ifndef _LIBCPP_CXX03_LANG
646 _LIBCPP_HIDE_FROM_ABI deque(initializer_list<value_type> __il);
647 _LIBCPP_HIDE_FROM_ABI deque(initializer_list<value_type> __il, const allocator_type& __a);
649 _LIBCPP_HIDE_FROM_ABI
650 deque& operator=(initializer_list<value_type> __il) {assign(__il); return *this;}
652 _LIBCPP_HIDE_FROM_ABI
653 deque(deque&& __c) _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
654 _LIBCPP_HIDE_FROM_ABI
655 deque(deque&& __c, const __type_identity_t<allocator_type>& __a);
656 _LIBCPP_HIDE_FROM_ABI
657 deque& operator=(deque&& __c)
658 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
659 is_nothrow_move_assignable<allocator_type>::value);
661 _LIBCPP_HIDE_FROM_ABI
662 void assign(initializer_list<value_type> __il) {assign(__il.begin(), __il.end());}
663 #endif // _LIBCPP_CXX03_LANG
665 template <class _InputIter>
666 _LIBCPP_HIDE_FROM_ABI void assign(_InputIter __f, _InputIter __l,
667 typename enable_if<__has_input_iterator_category<_InputIter>::value &&
668 !__has_random_access_iterator_category<_InputIter>::value>::type* = 0);
669 template <class _RAIter>
670 _LIBCPP_HIDE_FROM_ABI void assign(_RAIter __f, _RAIter __l,
671 typename enable_if<__has_random_access_iterator_category<_RAIter>::value>::type* = 0);
673 #if _LIBCPP_STD_VER >= 23
674 template <_ContainerCompatibleRange<_Tp> _Range>
675 _LIBCPP_HIDE_FROM_ABI
676 void assign_range(_Range&& __range) {
677 if constexpr (ranges::random_access_range<_Range>) {
678 auto __n = static_cast<size_type>(ranges::distance(__range));
679 __assign_with_size_random_access(ranges::begin(__range), __n);
681 } else if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
682 auto __n = static_cast<size_type>(ranges::distance(__range));
683 __assign_with_size(ranges::begin(__range), __n);
686 __assign_with_sentinel(ranges::begin(__range), ranges::end(__range));
691 _LIBCPP_HIDE_FROM_ABI void assign(size_type __n, const value_type& __v);
693 _LIBCPP_HIDE_FROM_ABI
694 allocator_type get_allocator() const _NOEXCEPT;
695 _LIBCPP_HIDE_FROM_ABI allocator_type& __alloc() _NOEXCEPT { return __size_.second(); }
696 _LIBCPP_HIDE_FROM_ABI const allocator_type& __alloc() const _NOEXCEPT { return __size_.second(); }
700 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT {
701 __map_pointer __mp = __map_.begin() + __start_ / __block_size;
702 return iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
705 _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT {
706 __map_const_pointer __mp =
707 static_cast<__map_const_pointer>(__map_.begin() + __start_ / __block_size);
708 return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
711 _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT {
712 size_type __p = size() + __start_;
713 __map_pointer __mp = __map_.begin() + __p / __block_size;
714 return iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
717 _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT {
718 size_type __p = size() + __start_;
719 __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __p / __block_size);
720 return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
723 _LIBCPP_HIDE_FROM_ABI
724 reverse_iterator rbegin() _NOEXCEPT
725 {return reverse_iterator(end());}
726 _LIBCPP_HIDE_FROM_ABI
727 const_reverse_iterator rbegin() const _NOEXCEPT
728 {return const_reverse_iterator(end());}
729 _LIBCPP_HIDE_FROM_ABI
730 reverse_iterator rend() _NOEXCEPT
731 {return reverse_iterator(begin());}
732 _LIBCPP_HIDE_FROM_ABI
733 const_reverse_iterator rend() const _NOEXCEPT
734 {return const_reverse_iterator(begin());}
736 _LIBCPP_HIDE_FROM_ABI
737 const_iterator cbegin() const _NOEXCEPT
739 _LIBCPP_HIDE_FROM_ABI
740 const_iterator cend() const _NOEXCEPT
742 _LIBCPP_HIDE_FROM_ABI
743 const_reverse_iterator crbegin() const _NOEXCEPT
744 {return const_reverse_iterator(end());}
745 _LIBCPP_HIDE_FROM_ABI
746 const_reverse_iterator crend() const _NOEXCEPT
747 {return const_reverse_iterator(begin());}
750 _LIBCPP_HIDE_FROM_ABI
751 size_type size() const _NOEXCEPT {return __size();}
753 _LIBCPP_HIDE_FROM_ABI size_type& __size() _NOEXCEPT { return __size_.first(); }
754 _LIBCPP_HIDE_FROM_ABI const size_type& __size() const _NOEXCEPT { return __size_.first(); }
756 _LIBCPP_HIDE_FROM_ABI
757 size_type max_size() const _NOEXCEPT
758 {return _VSTD::min<size_type>(
759 __alloc_traits::max_size(__alloc()),
760 numeric_limits<difference_type>::max());}
761 _LIBCPP_HIDE_FROM_ABI void resize(size_type __n);
762 _LIBCPP_HIDE_FROM_ABI void resize(size_type __n, const value_type& __v);
763 _LIBCPP_HIDE_FROM_ABI void shrink_to_fit() _NOEXCEPT;
764 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
765 bool empty() const _NOEXCEPT {return size() == 0;}
768 _LIBCPP_HIDE_FROM_ABI
769 reference operator[](size_type __i) _NOEXCEPT;
770 _LIBCPP_HIDE_FROM_ABI
771 const_reference operator[](size_type __i) const _NOEXCEPT;
772 _LIBCPP_HIDE_FROM_ABI
773 reference at(size_type __i);
774 _LIBCPP_HIDE_FROM_ABI
775 const_reference at(size_type __i) const;
776 _LIBCPP_HIDE_FROM_ABI
777 reference front() _NOEXCEPT;
778 _LIBCPP_HIDE_FROM_ABI
779 const_reference front() const _NOEXCEPT;
780 _LIBCPP_HIDE_FROM_ABI
781 reference back() _NOEXCEPT;
782 _LIBCPP_HIDE_FROM_ABI
783 const_reference back() const _NOEXCEPT;
785 // 23.2.2.3 modifiers:
786 _LIBCPP_HIDE_FROM_ABI void push_front(const value_type& __v);
787 _LIBCPP_HIDE_FROM_ABI void push_back(const value_type& __v);
788 #ifndef _LIBCPP_CXX03_LANG
789 #if _LIBCPP_STD_VER >= 17
790 template <class... _Args> _LIBCPP_HIDE_FROM_ABI reference emplace_front(_Args&&... __args);
791 template <class... _Args> _LIBCPP_HIDE_FROM_ABI reference emplace_back (_Args&&... __args);
793 template <class... _Args> _LIBCPP_HIDE_FROM_ABI void emplace_front(_Args&&... __args);
794 template <class... _Args> _LIBCPP_HIDE_FROM_ABI void emplace_back (_Args&&... __args);
796 template <class... _Args> _LIBCPP_HIDE_FROM_ABI iterator emplace(const_iterator __p, _Args&&... __args);
798 _LIBCPP_HIDE_FROM_ABI void push_front(value_type&& __v);
799 _LIBCPP_HIDE_FROM_ABI void push_back(value_type&& __v);
801 #if _LIBCPP_STD_VER >= 23
802 template <_ContainerCompatibleRange<_Tp> _Range>
803 _LIBCPP_HIDE_FROM_ABI
804 void prepend_range(_Range&& __range) {
805 insert_range(begin(), std::forward<_Range>(__range));
808 template <_ContainerCompatibleRange<_Tp> _Range>
809 _LIBCPP_HIDE_FROM_ABI
810 void append_range(_Range&& __range) {
811 insert_range(end(), std::forward<_Range>(__range));
815 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, value_type&& __v);
817 _LIBCPP_HIDE_FROM_ABI
818 iterator insert(const_iterator __p, initializer_list<value_type> __il)
819 {return insert(__p, __il.begin(), __il.end());}
820 #endif // _LIBCPP_CXX03_LANG
821 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, const value_type& __v);
822 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, size_type __n, const value_type& __v);
823 template <class _InputIter>
824 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, _InputIter __f, _InputIter __l,
825 typename enable_if<__has_exactly_input_iterator_category<_InputIter>::value>::type* = 0);
826 template <class _ForwardIterator>
827 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l,
828 typename enable_if<__has_exactly_forward_iterator_category<_ForwardIterator>::value>::type* = 0);
829 template <class _BiIter>
830 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, _BiIter __f, _BiIter __l,
831 typename enable_if<__has_bidirectional_iterator_category<_BiIter>::value>::type* = 0);
833 #if _LIBCPP_STD_VER >= 23
834 template <_ContainerCompatibleRange<_Tp> _Range>
835 _LIBCPP_HIDE_FROM_ABI
836 iterator insert_range(const_iterator __position, _Range&& __range) {
837 if constexpr (ranges::bidirectional_range<_Range>) {
838 auto __n = static_cast<size_type>(ranges::distance(__range));
839 return __insert_bidirectional(__position, ranges::begin(__range), ranges::end(__range), __n);
841 } else if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
842 auto __n = static_cast<size_type>(ranges::distance(__range));
843 return __insert_with_size(__position, ranges::begin(__range), __n);
846 return __insert_with_sentinel(__position, ranges::begin(__range), ranges::end(__range));
851 _LIBCPP_HIDE_FROM_ABI void pop_front();
852 _LIBCPP_HIDE_FROM_ABI void pop_back();
853 _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __p);
854 _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __f, const_iterator __l);
856 _LIBCPP_HIDE_FROM_ABI
857 void swap(deque& __c)
858 #if _LIBCPP_STD_VER >= 14
861 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
862 __is_nothrow_swappable<allocator_type>::value);
864 _LIBCPP_HIDE_FROM_ABI
865 void clear() _NOEXCEPT;
867 _LIBCPP_HIDE_FROM_ABI
868 bool __invariants() const {
869 if (!__map_.__invariants())
871 if (__map_.size() >= size_type(-1) / __block_size)
873 for (__map_const_iterator __i = __map_.begin(), __e = __map_.end();
877 if (__map_.size() != 0)
879 if (size() >= __map_.size() * __block_size)
881 if (__start_ >= __map_.size() * __block_size - size())
894 _LIBCPP_HIDE_FROM_ABI
895 void __move_assign_alloc(deque& __c)
896 _NOEXCEPT_(!__alloc_traits::propagate_on_container_move_assignment::value ||
897 is_nothrow_move_assignable<allocator_type>::value)
898 {__move_assign_alloc(__c, integral_constant<bool,
899 __alloc_traits::propagate_on_container_move_assignment::value>());}
901 _LIBCPP_HIDE_FROM_ABI
902 void __move_assign_alloc(deque& __c, true_type)
903 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
905 __alloc() = _VSTD::move(__c.__alloc());
908 _LIBCPP_HIDE_FROM_ABI
909 void __move_assign_alloc(deque&, false_type) _NOEXCEPT
912 _LIBCPP_HIDE_FROM_ABI
913 void __move_assign(deque& __c)
914 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
915 is_nothrow_move_assignable<allocator_type>::value)
917 __map_ = _VSTD::move(__c.__map_);
918 __start_ = __c.__start_;
919 __size() = __c.size();
920 __move_assign_alloc(__c);
921 __c.__start_ = __c.__size() = 0;
924 _LIBCPP_HIDE_FROM_ABI
925 static size_type __recommend_blocks(size_type __n)
927 return __n / __block_size + (__n % __block_size != 0);
929 _LIBCPP_HIDE_FROM_ABI
930 size_type __capacity() const
932 return __map_.size() == 0 ? 0 : __map_.size() * __block_size - 1;
934 _LIBCPP_HIDE_FROM_ABI
935 size_type __block_count() const
937 return __map_.size();
940 _LIBCPP_HIDE_FROM_ABI
941 size_type __front_spare() const
945 _LIBCPP_HIDE_FROM_ABI
946 size_type __front_spare_blocks() const {
947 return __front_spare() / __block_size;
949 _LIBCPP_HIDE_FROM_ABI
950 size_type __back_spare() const
952 return __capacity() - (__start_ + size());
954 _LIBCPP_HIDE_FROM_ABI
955 size_type __back_spare_blocks() const {
956 return __back_spare() / __block_size;
960 enum __asan_annotation_type {
965 enum __asan_annotation_place {
970 // The following functions are no-ops outside of AddressSanitizer mode.
971 // We call annotations for every allocator, unless explicitly disabled.
973 // To disable annotations for a particular allocator, change value of
974 // __asan_annotate_container_with_allocator to false.
975 // For more details, see the "Using libc++" documentation page or
976 // the documentation for __sanitizer_annotate_contiguous_container.
977 #if !defined(_LIBCPP_HAS_NO_ASAN) && _LIBCPP_CLANG_VER >= 1600
978 // TODO LLVM18: Remove the special-casing
979 _LIBCPP_HIDE_FROM_ABI void __annotate_double_ended_contiguous_container(
982 const void* __old_con_beg,
983 const void* __old_con_end,
984 const void* __new_con_beg,
985 const void* __new_con_end) const {
986 if (__beg != nullptr && __asan_annotate_container_with_allocator<_Allocator>::value)
987 __sanitizer_annotate_double_ended_contiguous_container(
988 __beg, __end, __old_con_beg, __old_con_end, __new_con_beg, __new_con_end);
991 _LIBCPP_HIDE_FROM_ABI void __annotate_double_ended_contiguous_container(
992 const void*, const void*, const void*, const void*, const void*, const void*) const _NOEXCEPT {}
993 #endif // !defined(_LIBCPP_HAS_NO_ASAN) && _LIBCPP_CLANG_VER >= 1600
995 _LIBCPP_HIDE_FROM_ABI
996 void __annotate_from_to(size_type __beg, size_type __end, __asan_annotation_type __annotation_type, __asan_annotation_place __place) const _NOEXCEPT {
997 // __beg - index of the first item to annotate
998 // __end - index behind the last item to annotate (so last item + 1)
999 // __annotation_type - __asan_unposion or __asan_poison
1000 // __place - __asan_front_moved or __asan_back_moved
1001 // Note: All indexes in __map_
1004 // __annotations_beg_map - first chunk which annotations we want to modify
1005 // __annotations_end_map - last chunk which annotations we want to modify
1006 // NOTE: if __end % __block_size == 0, __annotations_end_map points at the next block, which may not exist
1007 __map_const_iterator __annotations_beg_map = __map_.begin() + __beg / __block_size;
1008 __map_const_iterator __annotations_end_map = __map_.begin() + __end / __block_size;
1010 bool const __poisoning = __annotation_type == __asan_poison;
1011 // __old_c_beg_index - index of the first element in old container
1012 // __old_c_end_index - index of the end of old container (last + 1)
1013 // Note: may be outside the area we are annotating
1014 size_t __old_c_beg_index = (__poisoning && __place == __asan_front_moved) ? __beg : __start_;
1015 size_t __old_c_end_index = (__poisoning && __place == __asan_back_moved) ? __end : __start_ + size();
1016 bool const __front = __place == __asan_front_moved;
1018 if (__poisoning && empty()) {
1019 // Special case: we shouldn't trust __start_
1020 __old_c_beg_index = __beg;
1021 __old_c_end_index = __end;
1023 // __old_c_beg_map - memory block (chunk) with first element
1024 // __old_c_end_map - memory block (chunk) with end of old container
1025 // Note: if __old_c_end_index % __block_size == 0, __old_c_end_map points at the next block,
1026 // which may not exist
1027 __map_const_iterator __old_c_beg_map = __map_.begin() + __old_c_beg_index / __block_size;
1028 __map_const_iterator __old_c_end_map = __map_.begin() + __old_c_end_index / __block_size;
1030 // One edge (front/end) of the container was moved and one was not modified.
1031 // __new_edge_index - index of new edge
1032 // __new_edge_map - memory block (chunk) with new edge, it always equals to
1033 // __annotations_beg_map or __annotations_end_map
1034 // __old_edge_map - memory block (chunk) with old edge, it always equals to
1035 // __old_c_beg_map or __old_c_end_map
1036 size_t __new_edge_index = (__poisoning ^ __front) ? __beg : __end;
1037 __map_const_iterator __new_edge_map = __map_.begin() + __new_edge_index / __block_size;
1038 __map_const_iterator __old_edge_map = __front ? __old_c_end_map : __old_c_beg_map;
1040 // We iterate over map pointers (chunks) and fully poison all memory blocks between the first and the last.
1041 // First and last chunk may be partially poisoned.
1042 // __annotate_end_map may point at not existing chunk, therefore we have to have a check for it.
1043 for (__map_const_iterator __map_it = __annotations_beg_map; __map_it <= __annotations_end_map; ++__map_it) {
1044 if (__map_it == __annotations_end_map && __end % __block_size == 0)
1045 // Chunk may not exist, but nothing to do here anyway
1048 // The beginning and the end of the current memory block
1049 const void* __mem_beg = std::__to_address(*__map_it);
1050 const void* __mem_end = std::__to_address(*__map_it + __block_size);
1052 // The beginning of memory-in-use in the memory block before container modification
1053 const void* __old_beg =
1054 (__map_it == __old_c_beg_map) ? std::__to_address(*__map_it + (__old_c_beg_index % __block_size)) : __mem_beg;
1056 // The end of memory-in-use in the memory block before container modification
1057 const void* __old_end;
1058 if (__map_it < __old_c_beg_map || __map_it > __old_c_end_map || (!__poisoning && empty()))
1059 __old_end = __old_beg;
1061 __old_end = (__map_it == __old_c_end_map) ? std::__to_address(*__map_it + (__old_c_end_index % __block_size))
1064 // New edge of the container in current memory block
1065 // If the edge is in a different chunk it points on corresponding end of the memory block
1066 const void* __new_edge;
1067 if (__map_it == __new_edge_map)
1068 __new_edge = std::__to_address(*__map_it + (__new_edge_index % __block_size));
1070 __new_edge = (__poisoning ^ __front) ? __mem_beg : __mem_end;
1072 // Not modified edge of the container
1073 // If the edge is in a different chunk it points on corresponding end of the memory block
1074 const void* __old_edge;
1075 if (__map_it == __old_edge_map)
1076 __old_edge = __front ? __old_end : __old_beg;
1078 __old_edge = __front ? __mem_end : __mem_beg;
1080 // __new_beg - the beginning of memory-in-use in the memory block after container modification
1081 // __new_end - the end of memory-in-use in the memory block after container modification
1082 const void* __new_beg = __front ? __new_edge : __old_edge;
1083 const void* __new_end = __front ? __old_edge : __new_edge;
1085 __annotate_double_ended_contiguous_container(__mem_beg, __mem_end, __old_beg, __old_end, __new_beg, __new_end);
1089 _LIBCPP_HIDE_FROM_ABI
1090 void __annotate_new(size_type __current_size) const _NOEXCEPT {
1091 if (__current_size == 0)
1092 __annotate_from_to(0, __map_.size() * __block_size, __asan_poison, __asan_back_moved);
1094 __annotate_from_to(0, __start_, __asan_poison, __asan_front_moved);
1095 __annotate_from_to(__start_ + __current_size, __map_.size() * __block_size, __asan_poison, __asan_back_moved);
1099 _LIBCPP_HIDE_FROM_ABI
1100 void __annotate_delete() const _NOEXCEPT {
1102 for(size_t __i = 0; __i < __map_.size(); ++__i) {
1103 __annotate_whole_block(__i, __asan_unposion);
1107 __annotate_from_to(0, __start_, __asan_unposion, __asan_front_moved);
1108 __annotate_from_to(__start_ + size(), __map_.size() * __block_size, __asan_unposion, __asan_back_moved);
1112 _LIBCPP_HIDE_FROM_ABI
1113 void __annotate_increase_front(size_type __n) const _NOEXCEPT {
1114 __annotate_from_to(__start_ - __n, __start_, __asan_unposion, __asan_front_moved);
1117 _LIBCPP_HIDE_FROM_ABI
1118 void __annotate_increase_back(size_type __n) const _NOEXCEPT {
1119 __annotate_from_to(__start_ + size(), __start_ + size() + __n, __asan_unposion, __asan_back_moved);
1122 _LIBCPP_HIDE_FROM_ABI
1123 void __annotate_shrink_front(size_type __old_size, size_type __old_start) const _NOEXCEPT {
1124 __annotate_from_to(__old_start, __old_start + (__old_size - size()), __asan_poison, __asan_front_moved);
1127 _LIBCPP_HIDE_FROM_ABI
1128 void __annotate_shrink_back(size_type __old_size, size_type __old_start) const _NOEXCEPT {
1129 __annotate_from_to(__old_start + size(), __old_start + __old_size, __asan_poison, __asan_back_moved);
1132 _LIBCPP_HIDE_FROM_ABI
1133 void __annotate_poison_block(const void *__beginning, const void *__end) const _NOEXCEPT {
1134 __annotate_double_ended_contiguous_container(__beginning, __end, __beginning, __end, __end, __end);
1137 _LIBCPP_HIDE_FROM_ABI
1138 void __annotate_whole_block(size_t __block_index, __asan_annotation_type __annotation_type) const _NOEXCEPT {
1139 __map_const_iterator __block_it = __map_.begin() + __block_index;
1140 const void* __block_start = std::__to_address(*__block_it);
1141 const void* __block_end = std::__to_address(*__block_it + __block_size);
1143 if(__annotation_type == __asan_poison)
1144 __annotate_poison_block(__block_start, __block_end);
1146 __annotate_double_ended_contiguous_container(
1147 __block_start, __block_end, __block_start, __block_start, __block_start, __block_end);
1150 #if !defined(_LIBCPP_HAS_NO_ASAN)
1153 _LIBCPP_HIDE_FROM_ABI
1154 bool __verify_asan_annotations() const _NOEXCEPT {
1155 // This function tests deque object annotations.
1157 for (__map_const_iterator __it = __map_.begin(); __it != __map_.end(); ++__it) {
1158 if (!__sanitizer_verify_double_ended_contiguous_container(
1159 std::__to_address(*__it),
1160 std::__to_address(*__it),
1161 std::__to_address(*__it),
1162 std::__to_address(*__it + __block_size)))
1169 size_type __end = __start_ + size();
1170 __map_const_iterator __first_mp = __map_.begin() + __start_ / __block_size;
1171 __map_const_iterator __last_mp = __map_.begin() + (__end - 1) / __block_size;
1173 // Pointers to first and after last elements
1174 // Those can be in different deque blocks
1175 const void* __p_beg = std::__to_address(*__first_mp + (__start_ % __block_size));
1176 const void* __p_end =
1177 std::__to_address(*__last_mp + ((__end % __block_size == 0) ? __block_size : __end % __block_size));
1179 for (__map_const_iterator __it = __map_.begin(); __it != __map_.end(); ++__it) {
1180 // Go over all blocks, find the place we are in and verify its annotations
1181 // Note that __p_end points *behind* the last item.
1183 // - blocks before the first block with container elements
1184 // - first block with items
1185 // - last block with items
1186 // - blocks after last block with ciontainer elements
1188 // Is the block before or after deque blocks that contain elements?
1189 if (__it < __first_mp || __it > __last_mp) {
1190 if (!__sanitizer_verify_double_ended_contiguous_container(
1191 std::__to_address(*__it),
1192 std::__to_address(*__it),
1193 std::__to_address(*__it),
1194 std::__to_address(*__it + __block_size)))
1197 const void* __containers_buffer_beg = (__it == __first_mp) ? __p_beg : (const void*)std::__to_address(*__it);
1198 const void* __containers_buffer_end =
1199 (__it == __last_mp) ? __p_end : (const void*)std::__to_address(*__it + __block_size);
1200 if (!__sanitizer_verify_double_ended_contiguous_container(
1201 std::__to_address(*__it),
1202 __containers_buffer_beg,
1203 __containers_buffer_end,
1204 std::__to_address(*__it + __block_size))) {
1213 #endif // _LIBCPP_VERIFY_ASAN_DEQUE_ANNOTATIONS
1214 _LIBCPP_HIDE_FROM_ABI
1215 bool __maybe_remove_front_spare(bool __keep_one = true) {
1216 if (__front_spare_blocks() >= 2 || (!__keep_one && __front_spare_blocks())) {
1217 __annotate_whole_block(0, __asan_unposion);
1218 __alloc_traits::deallocate(__alloc(), __map_.front(),
1221 __start_ -= __block_size;
1227 _LIBCPP_HIDE_FROM_ABI
1228 bool __maybe_remove_back_spare(bool __keep_one = true) {
1229 if (__back_spare_blocks() >= 2 || (!__keep_one && __back_spare_blocks())) {
1230 __annotate_whole_block(__map_.size() - 1, __asan_unposion);
1231 __alloc_traits::deallocate(__alloc(), __map_.back(),
1239 template <class _Iterator, class _Sentinel>
1240 _LIBCPP_HIDE_FROM_ABI
1241 void __assign_with_sentinel(_Iterator __f, _Sentinel __l);
1243 template <class _RandomAccessIterator>
1244 _LIBCPP_HIDE_FROM_ABI
1245 void __assign_with_size_random_access(_RandomAccessIterator __f, difference_type __n);
1246 template <class _Iterator>
1247 _LIBCPP_HIDE_FROM_ABI
1248 void __assign_with_size(_Iterator __f, difference_type __n);
1250 template <class _Iterator, class _Sentinel>
1251 _LIBCPP_HIDE_FROM_ABI
1252 iterator __insert_with_sentinel(const_iterator __p, _Iterator __f, _Sentinel __l);
1254 template <class _Iterator>
1255 _LIBCPP_HIDE_FROM_ABI
1256 iterator __insert_with_size(const_iterator __p, _Iterator __f, size_type __n);
1258 template <class _BiIter, class _Sentinel>
1259 _LIBCPP_HIDE_FROM_ABI
1260 iterator __insert_bidirectional(const_iterator __p, _BiIter __f, _Sentinel __sent, size_type __n);
1261 template <class _BiIter>
1262 _LIBCPP_HIDE_FROM_ABI
1263 iterator __insert_bidirectional(const_iterator __p, _BiIter __f, _BiIter __l, size_type __n);
1265 template <class _InpIter>
1266 _LIBCPP_HIDE_FROM_ABI void __append(_InpIter __f, _InpIter __l,
1267 typename enable_if<__has_exactly_input_iterator_category<_InpIter>::value>::type* = 0);
1268 template <class _ForIter>
1269 _LIBCPP_HIDE_FROM_ABI void __append(_ForIter __f, _ForIter __l,
1270 typename enable_if<__has_forward_iterator_category<_ForIter>::value>::type* = 0);
1272 template <class _InputIterator>
1273 _LIBCPP_HIDE_FROM_ABI void __append_with_size(_InputIterator __from, size_type __n);
1274 template <class _InputIterator, class _Sentinel>
1275 _LIBCPP_HIDE_FROM_ABI void __append_with_sentinel(_InputIterator __f, _Sentinel __l);
1277 _LIBCPP_HIDE_FROM_ABI void __append(size_type __n);
1278 _LIBCPP_HIDE_FROM_ABI void __append(size_type __n, const value_type& __v);
1279 _LIBCPP_HIDE_FROM_ABI void __erase_to_end(const_iterator __f);
1280 _LIBCPP_HIDE_FROM_ABI void __add_front_capacity();
1281 _LIBCPP_HIDE_FROM_ABI void __add_front_capacity(size_type __n);
1282 _LIBCPP_HIDE_FROM_ABI void __add_back_capacity();
1283 _LIBCPP_HIDE_FROM_ABI void __add_back_capacity(size_type __n);
1284 _LIBCPP_HIDE_FROM_ABI iterator __move_and_check(iterator __f, iterator __l, iterator __r,
1285 const_pointer& __vt);
1286 _LIBCPP_HIDE_FROM_ABI iterator __move_backward_and_check(iterator __f, iterator __l, iterator __r,
1287 const_pointer& __vt);
1288 _LIBCPP_HIDE_FROM_ABI void __move_construct_and_check(iterator __f, iterator __l,
1289 iterator __r, const_pointer& __vt);
1290 _LIBCPP_HIDE_FROM_ABI void __move_construct_backward_and_check(iterator __f, iterator __l,
1291 iterator __r, const_pointer& __vt);
1293 _LIBCPP_HIDE_FROM_ABI
1294 void __copy_assign_alloc(const deque& __c)
1295 {__copy_assign_alloc(__c, integral_constant<bool,
1296 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1298 _LIBCPP_HIDE_FROM_ABI
1299 void __copy_assign_alloc(const deque& __c, true_type)
1301 if (__alloc() != __c.__alloc())
1306 __alloc() = __c.__alloc();
1307 __map_.__alloc() = __c.__map_.__alloc();
1310 _LIBCPP_HIDE_FROM_ABI
1311 void __copy_assign_alloc(const deque&, false_type)
1314 _LIBCPP_HIDE_FROM_ABI void __move_assign(deque& __c, true_type)
1315 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
1316 _LIBCPP_HIDE_FROM_ABI void __move_assign(deque& __c, false_type);
1319 template <class _Tp, class _Alloc>
1320 _LIBCPP_CONSTEXPR const typename allocator_traits<_Alloc>::difference_type deque<_Tp, _Alloc>::__block_size =
1321 __deque_block_size<value_type, difference_type>::value;
1323 #if _LIBCPP_STD_VER >= 17
1324 template<class _InputIterator,
1325 class _Alloc = allocator<__iter_value_type<_InputIterator>>,
1326 class = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
1327 class = enable_if_t<__is_allocator<_Alloc>::value>
1329 deque(_InputIterator, _InputIterator)
1330 -> deque<__iter_value_type<_InputIterator>, _Alloc>;
1332 template<class _InputIterator,
1334 class = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
1335 class = enable_if_t<__is_allocator<_Alloc>::value>
1337 deque(_InputIterator, _InputIterator, _Alloc)
1338 -> deque<__iter_value_type<_InputIterator>, _Alloc>;
1341 #if _LIBCPP_STD_VER >= 23
1342 template <ranges::input_range _Range,
1343 class _Alloc = allocator<ranges::range_value_t<_Range>>,
1344 class = enable_if_t<__is_allocator<_Alloc>::value>
1346 deque(from_range_t, _Range&&, _Alloc = _Alloc())
1347 -> deque<ranges::range_value_t<_Range>, _Alloc>;
1350 template <class _Tp, class _Allocator>
1351 deque<_Tp, _Allocator>::deque(size_type __n)
1352 : __start_(0), __size_(0, __default_init_tag())
1359 #if _LIBCPP_STD_VER >= 14
1360 template <class _Tp, class _Allocator>
1361 deque<_Tp, _Allocator>::deque(size_type __n, const _Allocator& __a)
1362 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a)
1370 template <class _Tp, class _Allocator>
1371 deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v)
1372 : __start_(0), __size_(0, __default_init_tag())
1379 template <class _Tp, class _Allocator>
1380 template <class _InputIter>
1381 deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l,
1382 typename enable_if<__has_input_iterator_category<_InputIter>::value>::type*)
1383 : __start_(0), __size_(0, __default_init_tag())
1389 template <class _Tp, class _Allocator>
1390 template <class _InputIter>
1391 deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
1392 typename enable_if<__has_input_iterator_category<_InputIter>::value>::type*)
1393 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a)
1399 template <class _Tp, class _Allocator>
1400 deque<_Tp, _Allocator>::deque(const deque& __c)
1401 : __map_(__pointer_allocator(__alloc_traits::select_on_container_copy_construction(__c.__alloc()))),
1403 __size_(0, __map_.__alloc())
1406 __append(__c.begin(), __c.end());
1409 template <class _Tp, class _Allocator>
1410 deque<_Tp, _Allocator>::deque(const deque& __c, const __type_identity_t<allocator_type>& __a)
1411 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a)
1414 __append(__c.begin(), __c.end());
1417 template <class _Tp, class _Allocator>
1418 deque<_Tp, _Allocator>&
1419 deque<_Tp, _Allocator>::operator=(const deque& __c)
1421 if (this != _VSTD::addressof(__c))
1423 __copy_assign_alloc(__c);
1424 assign(__c.begin(), __c.end());
1429 #ifndef _LIBCPP_CXX03_LANG
1431 template <class _Tp, class _Allocator>
1432 deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il)
1433 : __start_(0), __size_(0, __default_init_tag())
1436 __append(__il.begin(), __il.end());
1439 template <class _Tp, class _Allocator>
1440 deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il, const allocator_type& __a)
1441 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a)
1444 __append(__il.begin(), __il.end());
1447 template <class _Tp, class _Allocator>
1449 deque<_Tp, _Allocator>::deque(deque&& __c)
1450 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
1451 : __map_(std::move(__c.__map_)), __start_(std::move(__c.__start_)), __size_(std::move(__c.__size_))
1457 template <class _Tp, class _Allocator>
1459 deque<_Tp, _Allocator>::deque(deque&& __c, const __type_identity_t<allocator_type>& __a)
1460 : __map_(std::move(__c.__map_), __pointer_allocator(__a)),
1461 __start_(std::move(__c.__start_)),
1462 __size_(std::move(__c.__size()), __a)
1464 if (__a == __c.__alloc())
1474 typedef move_iterator<iterator> _Ip;
1475 assign(_Ip(__c.begin()), _Ip(__c.end()));
1479 template <class _Tp, class _Allocator>
1481 deque<_Tp, _Allocator>&
1482 deque<_Tp, _Allocator>::operator=(deque&& __c)
1483 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1484 is_nothrow_move_assignable<allocator_type>::value)
1486 __move_assign(__c, integral_constant<bool,
1487 __alloc_traits::propagate_on_container_move_assignment::value>());
1491 template <class _Tp, class _Allocator>
1493 deque<_Tp, _Allocator>::__move_assign(deque& __c, false_type)
1495 if (__alloc() != __c.__alloc())
1497 typedef move_iterator<iterator> _Ip;
1498 assign(_Ip(__c.begin()), _Ip(__c.end()));
1501 __move_assign(__c, true_type());
1504 template <class _Tp, class _Allocator>
1506 deque<_Tp, _Allocator>::__move_assign(deque& __c, true_type)
1507 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1514 #endif // _LIBCPP_CXX03_LANG
1516 template <class _Tp, class _Allocator>
1517 template <class _InputIter>
1519 deque<_Tp, _Allocator>::assign(_InputIter __f, _InputIter __l,
1520 typename enable_if<__has_input_iterator_category<_InputIter>::value &&
1521 !__has_random_access_iterator_category<_InputIter>::value>::type*)
1523 __assign_with_sentinel(__f, __l);
1526 template <class _Tp, class _Allocator>
1527 template <class _Iterator, class _Sentinel>
1528 _LIBCPP_HIDE_FROM_ABI
1529 void deque<_Tp, _Allocator>::__assign_with_sentinel(_Iterator __f, _Sentinel __l) {
1530 iterator __i = begin();
1531 iterator __e = end();
1532 for (; __f != __l && __i != __e; ++__f, (void) ++__i)
1535 __append_with_sentinel(std::move(__f), std::move(__l));
1537 __erase_to_end(__i);
1540 template <class _Tp, class _Allocator>
1541 template <class _RAIter>
1543 deque<_Tp, _Allocator>::assign(_RAIter __f, _RAIter __l,
1544 typename enable_if<__has_random_access_iterator_category<_RAIter>::value>::type*)
1546 __assign_with_size_random_access(__f, __l - __f);
1549 template <class _Tp, class _Allocator>
1550 template <class _RandomAccessIterator>
1551 _LIBCPP_HIDE_FROM_ABI
1552 void deque<_Tp, _Allocator>::__assign_with_size_random_access(_RandomAccessIterator __f, difference_type __n) {
1553 if (static_cast<size_type>(__n) > size())
1555 auto __l = __f + size();
1556 std::copy(__f, __l, begin());
1557 __append_with_size(__l, __n - size());
1560 __erase_to_end(std::copy_n(__f, __n, begin()));
1563 template <class _Tp, class _Allocator>
1564 template <class _Iterator>
1565 _LIBCPP_HIDE_FROM_ABI
1566 void deque<_Tp, _Allocator>::__assign_with_size(_Iterator __f, difference_type __n) {
1567 if (static_cast<size_type>(__n) > size()) {
1568 auto __added_size = __n - size();
1571 for (auto __count = size(); __count != 0; --__count) {
1575 __append_with_size(__f, __added_size);
1578 __erase_to_end(std::copy_n(__f, __n, begin()));
1582 template <class _Tp, class _Allocator>
1584 deque<_Tp, _Allocator>::assign(size_type __n, const value_type& __v)
1588 _VSTD::fill_n(begin(), size(), __v);
1593 __erase_to_end(_VSTD::fill_n(begin(), __n, __v));
1596 template <class _Tp, class _Allocator>
1599 deque<_Tp, _Allocator>::get_allocator() const _NOEXCEPT
1604 template <class _Tp, class _Allocator>
1606 deque<_Tp, _Allocator>::resize(size_type __n)
1609 __append(__n - size());
1610 else if (__n < size())
1611 __erase_to_end(begin() + __n);
1614 template <class _Tp, class _Allocator>
1616 deque<_Tp, _Allocator>::resize(size_type __n, const value_type& __v)
1619 __append(__n - size(), __v);
1620 else if (__n < size())
1621 __erase_to_end(begin() + __n);
1624 template <class _Tp, class _Allocator>
1626 deque<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
1628 allocator_type& __a = __alloc();
1631 __annotate_delete();
1632 while (__map_.size() > 0)
1634 __alloc_traits::deallocate(__a, __map_.back(), __block_size);
1641 __maybe_remove_front_spare(/*__keep_one=*/false);
1642 __maybe_remove_back_spare(/*__keep_one=*/false);
1644 __map_.shrink_to_fit();
1647 template <class _Tp, class _Allocator>
1649 typename deque<_Tp, _Allocator>::reference
1650 deque<_Tp, _Allocator>::operator[](size_type __i) _NOEXCEPT
1652 size_type __p = __start_ + __i;
1653 return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
1656 template <class _Tp, class _Allocator>
1658 typename deque<_Tp, _Allocator>::const_reference
1659 deque<_Tp, _Allocator>::operator[](size_type __i) const _NOEXCEPT
1661 size_type __p = __start_ + __i;
1662 return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
1665 template <class _Tp, class _Allocator>
1667 typename deque<_Tp, _Allocator>::reference
1668 deque<_Tp, _Allocator>::at(size_type __i)
1671 _VSTD::__throw_out_of_range("deque");
1672 size_type __p = __start_ + __i;
1673 return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
1676 template <class _Tp, class _Allocator>
1678 typename deque<_Tp, _Allocator>::const_reference
1679 deque<_Tp, _Allocator>::at(size_type __i) const
1682 _VSTD::__throw_out_of_range("deque");
1683 size_type __p = __start_ + __i;
1684 return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
1687 template <class _Tp, class _Allocator>
1689 typename deque<_Tp, _Allocator>::reference
1690 deque<_Tp, _Allocator>::front() _NOEXCEPT
1692 return *(*(__map_.begin() + __start_ / __block_size)
1693 + __start_ % __block_size);
1696 template <class _Tp, class _Allocator>
1698 typename deque<_Tp, _Allocator>::const_reference
1699 deque<_Tp, _Allocator>::front() const _NOEXCEPT
1701 return *(*(__map_.begin() + __start_ / __block_size)
1702 + __start_ % __block_size);
1705 template <class _Tp, class _Allocator>
1707 typename deque<_Tp, _Allocator>::reference
1708 deque<_Tp, _Allocator>::back() _NOEXCEPT
1710 size_type __p = size() + __start_ - 1;
1711 return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
1714 template <class _Tp, class _Allocator>
1716 typename deque<_Tp, _Allocator>::const_reference
1717 deque<_Tp, _Allocator>::back() const _NOEXCEPT
1719 size_type __p = size() + __start_ - 1;
1720 return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
1723 template <class _Tp, class _Allocator>
1725 deque<_Tp, _Allocator>::push_back(const value_type& __v)
1727 allocator_type& __a = __alloc();
1728 if (__back_spare() == 0)
1729 __add_back_capacity();
1730 // __back_spare() >= 1
1731 __annotate_increase_back(1);
1732 __alloc_traits::construct(__a, _VSTD::addressof(*end()), __v);
1736 template <class _Tp, class _Allocator>
1738 deque<_Tp, _Allocator>::push_front(const value_type& __v)
1740 allocator_type& __a = __alloc();
1741 if (__front_spare() == 0)
1742 __add_front_capacity();
1743 // __front_spare() >= 1
1744 __annotate_increase_front(1);
1745 __alloc_traits::construct(__a, _VSTD::addressof(*--begin()), __v);
1750 #ifndef _LIBCPP_CXX03_LANG
1751 template <class _Tp, class _Allocator>
1753 deque<_Tp, _Allocator>::push_back(value_type&& __v)
1755 allocator_type& __a = __alloc();
1756 if (__back_spare() == 0)
1757 __add_back_capacity();
1758 // __back_spare() >= 1
1759 __annotate_increase_back(1);
1760 __alloc_traits::construct(__a, _VSTD::addressof(*end()), _VSTD::move(__v));
1764 template <class _Tp, class _Allocator>
1765 template <class... _Args>
1766 #if _LIBCPP_STD_VER >= 17
1767 typename deque<_Tp, _Allocator>::reference
1771 deque<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1773 allocator_type& __a = __alloc();
1774 if (__back_spare() == 0)
1775 __add_back_capacity();
1776 // __back_spare() >= 1
1777 __annotate_increase_back(1);
1778 __alloc_traits::construct(__a, _VSTD::addressof(*end()),
1779 _VSTD::forward<_Args>(__args)...);
1781 #if _LIBCPP_STD_VER >= 17
1786 template <class _Tp, class _Allocator>
1788 deque<_Tp, _Allocator>::push_front(value_type&& __v)
1790 allocator_type& __a = __alloc();
1791 if (__front_spare() == 0)
1792 __add_front_capacity();
1793 // __front_spare() >= 1
1794 __annotate_increase_front(1);
1795 __alloc_traits::construct(__a, _VSTD::addressof(*--begin()), _VSTD::move(__v));
1801 template <class _Tp, class _Allocator>
1802 template <class... _Args>
1803 #if _LIBCPP_STD_VER >= 17
1804 typename deque<_Tp, _Allocator>::reference
1808 deque<_Tp, _Allocator>::emplace_front(_Args&&... __args)
1810 allocator_type& __a = __alloc();
1811 if (__front_spare() == 0)
1812 __add_front_capacity();
1813 // __front_spare() >= 1
1814 __annotate_increase_front(1);
1815 __alloc_traits::construct(__a, _VSTD::addressof(*--begin()), _VSTD::forward<_Args>(__args)...);
1818 #if _LIBCPP_STD_VER >= 17
1823 template <class _Tp, class _Allocator>
1824 typename deque<_Tp, _Allocator>::iterator
1825 deque<_Tp, _Allocator>::insert(const_iterator __p, value_type&& __v)
1827 size_type __pos = __p - begin();
1828 size_type __to_end = size() - __pos;
1829 allocator_type& __a = __alloc();
1830 if (__pos < __to_end)
1831 { // insert by shifting things backward
1832 if (__front_spare() == 0)
1833 __add_front_capacity();
1834 // __front_spare() >= 1
1835 __annotate_increase_front(1);
1838 __alloc_traits::construct(__a, _VSTD::addressof(*--begin()), _VSTD::move(__v));
1844 iterator __b = begin();
1845 iterator __bm1 = _VSTD::prev(__b);
1846 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
1850 __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
1851 *__b = _VSTD::move(__v);
1855 { // insert by shifting things forward
1856 if (__back_spare() == 0)
1857 __add_back_capacity();
1858 // __back_capacity >= 1
1859 __annotate_increase_back(1);
1860 size_type __de = size() - __pos;
1863 __alloc_traits::construct(__a, _VSTD::addressof(*end()), _VSTD::move(__v));
1868 iterator __e = end();
1869 iterator __em1 = _VSTD::prev(__e);
1870 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
1873 __e = _VSTD::move_backward(__e - __de, __em1, __e);
1874 *--__e = _VSTD::move(__v);
1877 return begin() + __pos;
1880 template <class _Tp, class _Allocator>
1881 template <class... _Args>
1882 typename deque<_Tp, _Allocator>::iterator
1883 deque<_Tp, _Allocator>::emplace(const_iterator __p, _Args&&... __args)
1885 size_type __pos = __p - begin();
1886 size_type __to_end = size() - __pos;
1887 allocator_type& __a = __alloc();
1888 if (__pos < __to_end)
1889 { // insert by shifting things backward
1890 if (__front_spare() == 0)
1891 __add_front_capacity();
1892 // __front_spare() >= 1
1893 __annotate_increase_front(1);
1896 __alloc_traits::construct(__a, _VSTD::addressof(*--begin()), _VSTD::forward<_Args>(__args)...);
1902 __temp_value<value_type, _Allocator> __tmp(__alloc(), _VSTD::forward<_Args>(__args)...);
1903 iterator __b = begin();
1904 iterator __bm1 = _VSTD::prev(__b);
1905 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
1909 __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
1910 *__b = _VSTD::move(__tmp.get());
1914 { // insert by shifting things forward
1915 if (__back_spare() == 0)
1916 __add_back_capacity();
1917 // __back_capacity >= 1
1918 __annotate_increase_back(1);
1919 size_type __de = size() - __pos;
1922 __alloc_traits::construct(__a, _VSTD::addressof(*end()), _VSTD::forward<_Args>(__args)...);
1927 __temp_value<value_type, _Allocator> __tmp(__alloc(), _VSTD::forward<_Args>(__args)...);
1928 iterator __e = end();
1929 iterator __em1 = _VSTD::prev(__e);
1930 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
1933 __e = _VSTD::move_backward(__e - __de, __em1, __e);
1934 *--__e = _VSTD::move(__tmp.get());
1937 return begin() + __pos;
1940 #endif // _LIBCPP_CXX03_LANG
1943 template <class _Tp, class _Allocator>
1944 typename deque<_Tp, _Allocator>::iterator
1945 deque<_Tp, _Allocator>::insert(const_iterator __p, const value_type& __v)
1947 size_type __pos = __p - begin();
1948 size_type __to_end = size() - __pos;
1949 allocator_type& __a = __alloc();
1950 if (__pos < __to_end)
1951 { // insert by shifting things backward
1952 if (__front_spare() == 0)
1953 __add_front_capacity();
1954 // __front_spare() >= 1
1955 __annotate_increase_front(1);
1958 __alloc_traits::construct(__a, _VSTD::addressof(*--begin()), __v);
1964 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1965 iterator __b = begin();
1966 iterator __bm1 = _VSTD::prev(__b);
1967 if (__vt == pointer_traits<const_pointer>::pointer_to(*__b))
1968 __vt = pointer_traits<const_pointer>::pointer_to(*__bm1);
1969 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
1973 __b = __move_and_check(_VSTD::next(__b), __b + __pos, __b, __vt);
1978 { // insert by shifting things forward
1979 if (__back_spare() == 0)
1980 __add_back_capacity();
1981 // __back_capacity >= 1
1982 __annotate_increase_back(1);
1983 size_type __de = size() - __pos;
1986 __alloc_traits::construct(__a, _VSTD::addressof(*end()), __v);
1991 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1992 iterator __e = end();
1993 iterator __em1 = _VSTD::prev(__e);
1994 if (__vt == pointer_traits<const_pointer>::pointer_to(*__em1))
1995 __vt = pointer_traits<const_pointer>::pointer_to(*__e);
1996 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
1999 __e = __move_backward_and_check(__e - __de, __em1, __e, __vt);
2003 return begin() + __pos;
2006 template <class _Tp, class _Allocator>
2007 typename deque<_Tp, _Allocator>::iterator
2008 deque<_Tp, _Allocator>::insert(const_iterator __p, size_type __n, const value_type& __v)
2010 size_type __pos = __p - begin();
2011 size_type __to_end = __size() - __pos;
2012 allocator_type& __a = __alloc();
2013 if (__pos < __to_end)
2014 { // insert by shifting things backward
2015 if (__n > __front_spare())
2016 __add_front_capacity(__n - __front_spare());
2017 // __n <= __front_spare()
2018 __annotate_increase_front(__n);
2019 iterator __old_begin = begin();
2020 iterator __i = __old_begin;
2023 for (size_type __m = __n - __pos; __m; --__m, --__start_, ++__size())
2024 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), __v);
2029 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2030 iterator __obn = __old_begin + __n;
2031 __move_construct_backward_and_check(__old_begin, __obn, __i, __vt);
2033 __old_begin = __move_and_check(__obn, __old_begin + __pos, __old_begin, __vt);
2034 _VSTD::fill_n(__old_begin, __n, *__vt);
2038 { // insert by shifting things forward
2039 size_type __back_capacity = __back_spare();
2040 if (__n > __back_capacity)
2041 __add_back_capacity(__n - __back_capacity);
2042 // __n <= __back_capacity
2043 __annotate_increase_back(__n);
2044 iterator __old_end = end();
2045 iterator __i = __old_end;
2046 size_type __de = size() - __pos;
2049 for (size_type __m = __n - __de; __m; --__m, (void) ++__i, ++__size())
2050 __alloc_traits::construct(__a, _VSTD::addressof(*__i), __v);
2055 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2056 iterator __oen = __old_end - __n;
2057 __move_construct_and_check(__oen, __old_end, __i, __vt);
2059 __old_end = __move_backward_and_check(__old_end - __de, __oen, __old_end, __vt);
2060 _VSTD::fill_n(__old_end - __n, __n, *__vt);
2063 return begin() + __pos;
2066 template <class _Tp, class _Allocator>
2067 template <class _InputIter>
2068 typename deque<_Tp, _Allocator>::iterator
2069 deque<_Tp, _Allocator>::insert(const_iterator __p, _InputIter __f, _InputIter __l,
2070 typename enable_if<__has_exactly_input_iterator_category<_InputIter>::value>::type*)
2072 return __insert_with_sentinel(__p, __f, __l);
2075 template <class _Tp, class _Allocator>
2076 template <class _Iterator, class _Sentinel>
2077 _LIBCPP_HIDE_FROM_ABI
2078 typename deque<_Tp, _Allocator>::iterator
2079 deque<_Tp, _Allocator>::__insert_with_sentinel(const_iterator __p, _Iterator __f, _Sentinel __l) {
2080 __split_buffer<value_type, allocator_type&> __buf(__alloc());
2081 __buf.__construct_at_end_with_sentinel(std::move(__f), std::move(__l));
2082 typedef typename __split_buffer<value_type, allocator_type&>::iterator __bi;
2083 return insert(__p, move_iterator<__bi>(__buf.begin()), move_iterator<__bi>(__buf.end()));
2086 template <class _Tp, class _Allocator>
2087 template <class _ForwardIterator>
2088 typename deque<_Tp, _Allocator>::iterator
2089 deque<_Tp, _Allocator>::insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l,
2090 typename enable_if<__has_exactly_forward_iterator_category<_ForwardIterator>::value>::type*)
2092 return __insert_with_size(__p, __f, std::distance(__f, __l));
2095 template <class _Tp, class _Allocator>
2096 template <class _Iterator>
2097 _LIBCPP_HIDE_FROM_ABI
2098 typename deque<_Tp, _Allocator>::iterator
2099 deque<_Tp, _Allocator>::__insert_with_size(const_iterator __p, _Iterator __f, size_type __n) {
2100 __split_buffer<value_type, allocator_type&> __buf(__n, 0, __alloc());
2101 __buf.__construct_at_end_with_size(__f, __n);
2102 typedef typename __split_buffer<value_type, allocator_type&>::iterator __fwd;
2103 return insert(__p, move_iterator<__fwd>(__buf.begin()), move_iterator<__fwd>(__buf.end()));
2106 template <class _Tp, class _Allocator>
2107 template <class _BiIter>
2108 typename deque<_Tp, _Allocator>::iterator
2109 deque<_Tp, _Allocator>::insert(const_iterator __p, _BiIter __f, _BiIter __l,
2110 typename enable_if<__has_bidirectional_iterator_category<_BiIter>::value>::type*)
2112 return __insert_bidirectional(__p, __f, __l, std::distance(__f, __l));
2115 template <class _Tp, class _Allocator>
2116 template <class _BiIter, class _Sentinel>
2117 _LIBCPP_HIDE_FROM_ABI
2118 typename deque<_Tp, _Allocator>::iterator
2119 deque<_Tp, _Allocator>::__insert_bidirectional(const_iterator __p, _BiIter __f, _Sentinel, size_type __n) {
2120 return __insert_bidirectional(__p, __f, std::next(__f, __n), __n);
2123 template <class _Tp, class _Allocator>
2124 template <class _BiIter>
2125 _LIBCPP_HIDE_FROM_ABI
2126 typename deque<_Tp, _Allocator>::iterator
2127 deque<_Tp, _Allocator>::__insert_bidirectional(const_iterator __p, _BiIter __f, _BiIter __l, size_type __n) {
2128 size_type __pos = __p - begin();
2129 size_type __to_end = size() - __pos;
2130 allocator_type& __a = __alloc();
2131 if (__pos < __to_end)
2132 { // insert by shifting things backward
2133 if (__n > __front_spare())
2134 __add_front_capacity(__n - __front_spare());
2135 // __n <= __front_spare()
2136 __annotate_increase_front(__n);
2137 iterator __old_begin = begin();
2138 iterator __i = __old_begin;
2142 __m = __pos < __n / 2 ? _VSTD::prev(__l, __pos) : _VSTD::next(__f, __n - __pos);
2143 for (_BiIter __j = __m; __j != __f; --__start_, ++__size())
2144 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), *--__j);
2149 iterator __obn = __old_begin + __n;
2150 for (iterator __j = __obn; __j != __old_begin;)
2152 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), _VSTD::move(*--__j));
2157 __old_begin = _VSTD::move(__obn, __old_begin + __pos, __old_begin);
2158 _VSTD::copy(__m, __l, __old_begin);
2162 { // insert by shifting things forward
2163 size_type __back_capacity = __back_spare();
2164 if (__n > __back_capacity)
2165 __add_back_capacity(__n - __back_capacity);
2166 // __n <= __back_capacity
2167 __annotate_increase_back(__n);
2168 iterator __old_end = end();
2169 iterator __i = __old_end;
2171 size_type __de = size() - __pos;
2174 __m = __de < __n / 2 ? _VSTD::next(__f, __de) : _VSTD::prev(__l, __n - __de);
2175 for (_BiIter __j = __m; __j != __l; ++__i, (void) ++__j, ++__size())
2176 __alloc_traits::construct(__a, _VSTD::addressof(*__i), *__j);
2181 iterator __oen = __old_end - __n;
2182 for (iterator __j = __oen; __j != __old_end; ++__i, (void) ++__j, ++__size())
2183 __alloc_traits::construct(__a, _VSTD::addressof(*__i), _VSTD::move(*__j));
2185 __old_end = _VSTD::move_backward(__old_end - __de, __oen, __old_end);
2186 _VSTD::copy_backward(__f, __m, __old_end);
2189 return begin() + __pos;
2192 template <class _Tp, class _Allocator>
2193 template <class _InpIter>
2195 deque<_Tp, _Allocator>::__append(_InpIter __f, _InpIter __l,
2196 typename enable_if<__has_exactly_input_iterator_category<_InpIter>::value>::type*)
2198 __append_with_sentinel(__f, __l);
2201 template <class _Tp, class _Allocator>
2202 template <class _InputIterator, class _Sentinel>
2203 _LIBCPP_HIDE_FROM_ABI
2204 void deque<_Tp, _Allocator>::__append_with_sentinel(_InputIterator __f, _Sentinel __l) {
2205 for (; __f != __l; ++__f)
2206 #ifdef _LIBCPP_CXX03_LANG
2213 template <class _Tp, class _Allocator>
2214 template <class _ForIter>
2216 deque<_Tp, _Allocator>::__append(_ForIter __f, _ForIter __l,
2217 typename enable_if<__has_forward_iterator_category<_ForIter>::value>::type*)
2219 __append_with_size(__f, std::distance(__f, __l));
2222 template <class _Tp, class _Allocator>
2223 template <class _InputIterator>
2224 _LIBCPP_HIDE_FROM_ABI
2225 void deque<_Tp, _Allocator>::__append_with_size(_InputIterator __f, size_type __n) {
2226 allocator_type& __a = __alloc();
2227 size_type __back_capacity = __back_spare();
2228 if (__n > __back_capacity)
2229 __add_back_capacity(__n - __back_capacity);
2231 // __n <= __back_capacity
2232 __annotate_increase_back(__n);
2233 for (__deque_block_range __br : __deque_range(end(), end() + __n)) {
2234 _ConstructTransaction __tx(this, __br);
2235 for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, (void)++__f) {
2236 __alloc_traits::construct(__a, _VSTD::__to_address(__tx.__pos_), *__f);
2241 template <class _Tp, class _Allocator>
2243 deque<_Tp, _Allocator>::__append(size_type __n)
2245 allocator_type& __a = __alloc();
2246 size_type __back_capacity = __back_spare();
2247 if (__n > __back_capacity)
2248 __add_back_capacity(__n - __back_capacity);
2249 // __n <= __back_capacity
2250 __annotate_increase_back(__n);
2251 for (__deque_block_range __br : __deque_range(end(), end() + __n)) {
2252 _ConstructTransaction __tx(this, __br);
2253 for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
2254 __alloc_traits::construct(__a, _VSTD::__to_address(__tx.__pos_));
2259 template <class _Tp, class _Allocator>
2261 deque<_Tp, _Allocator>::__append(size_type __n, const value_type& __v)
2263 allocator_type& __a = __alloc();
2264 size_type __back_capacity = __back_spare();
2265 if (__n > __back_capacity)
2266 __add_back_capacity(__n - __back_capacity);
2267 // __n <= __back_capacity
2268 __annotate_increase_back(__n);
2269 for (__deque_block_range __br : __deque_range(end(), end() + __n)) {
2270 _ConstructTransaction __tx(this, __br);
2271 for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
2272 __alloc_traits::construct(__a, _VSTD::__to_address(__tx.__pos_), __v);
2278 // Create front capacity for one block of elements.
2279 // Strong guarantee. Either do it or don't touch anything.
2280 template <class _Tp, class _Allocator>
2282 deque<_Tp, _Allocator>::__add_front_capacity()
2284 allocator_type& __a = __alloc();
2285 if (__back_spare() >= __block_size)
2287 __start_ += __block_size;
2288 pointer __pt = __map_.back();
2290 __map_.push_front(__pt);
2292 // Else if __map_.size() < __map_.capacity() then we need to allocate 1 buffer
2293 else if (__map_.size() < __map_.capacity())
2294 { // we can put the new buffer into the map, but don't shift things around
2295 // until all buffers are allocated. If we throw, we don't need to fix
2296 // anything up (any added buffers are undetectible)
2297 if (__map_.__front_spare() > 0)
2298 __map_.push_front(__alloc_traits::allocate(__a, __block_size));
2301 __map_.push_back(__alloc_traits::allocate(__a, __block_size));
2302 // Done allocating, reorder capacity
2303 pointer __pt = __map_.back();
2305 __map_.push_front(__pt);
2307 __start_ = __map_.size() == 1 ?
2309 __start_ + __block_size;
2311 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2314 __split_buffer<pointer, __pointer_allocator&>
2315 __buf(std::max<size_type>(2 * __map_.capacity(), 1),
2316 0, __map_.__alloc());
2318 typedef __allocator_destructor<_Allocator> _Dp;
2319 unique_ptr<pointer, _Dp> __hold(
2320 __alloc_traits::allocate(__a, __block_size),
2321 _Dp(__a, __block_size));
2322 __buf.push_back(__hold.get());
2325 for (__map_pointer __i = __map_.begin();
2326 __i != __map_.end(); ++__i)
2327 __buf.push_back(*__i);
2328 _VSTD::swap(__map_.__first_, __buf.__first_);
2329 _VSTD::swap(__map_.__begin_, __buf.__begin_);
2330 _VSTD::swap(__map_.__end_, __buf.__end_);
2331 _VSTD::swap(__map_.__end_cap(), __buf.__end_cap());
2332 __start_ = __map_.size() == 1 ?
2334 __start_ + __block_size;
2336 __annotate_whole_block(0, __asan_poison);
2339 // Create front capacity for __n elements.
2340 // Strong guarantee. Either do it or don't touch anything.
2341 template <class _Tp, class _Allocator>
2343 deque<_Tp, _Allocator>::__add_front_capacity(size_type __n)
2345 allocator_type& __a = __alloc();
2346 size_type __nb = __recommend_blocks(__n + __map_.empty());
2347 // Number of unused blocks at back:
2348 size_type __back_capacity = __back_spare() / __block_size;
2349 __back_capacity = _VSTD::min(__back_capacity, __nb); // don't take more than you need
2350 __nb -= __back_capacity; // number of blocks need to allocate
2351 // If __nb == 0, then we have sufficient capacity.
2354 __start_ += __block_size * __back_capacity;
2355 for (; __back_capacity > 0; --__back_capacity)
2357 pointer __pt = __map_.back();
2359 __map_.push_front(__pt);
2362 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2363 else if (__nb <= __map_.capacity() - __map_.size())
2364 { // we can put the new buffers into the map, but don't shift things around
2365 // until all buffers are allocated. If we throw, we don't need to fix
2366 // anything up (any added buffers are undetectible)
2367 for (; __nb > 0; --__nb, __start_ += __block_size - (__map_.size() == 1))
2369 if (__map_.__front_spare() == 0)
2371 __map_.push_front(__alloc_traits::allocate(__a, __block_size));
2372 __annotate_whole_block(0, __asan_poison);
2374 for (; __nb > 0; --__nb, ++__back_capacity)
2375 __map_.push_back(__alloc_traits::allocate(__a, __block_size));
2376 // Done allocating, reorder capacity
2377 __start_ += __back_capacity * __block_size;
2378 for (; __back_capacity > 0; --__back_capacity)
2380 pointer __pt = __map_.back();
2382 __map_.push_front(__pt);
2383 __annotate_whole_block(0, __asan_poison);
2386 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2389 size_type __ds = (__nb + __back_capacity) * __block_size - __map_.empty();
2390 __split_buffer<pointer, __pointer_allocator&>
2391 __buf(std::max<size_type>(2* __map_.capacity(),
2392 __nb + __map_.size()),
2393 0, __map_.__alloc());
2394 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2397 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
2398 for (; __nb > 0; --__nb) {
2399 __buf.push_back(__alloc_traits::allocate(__a, __block_size));
2400 // ASan: this is empty container, we have to poison whole block
2401 __annotate_poison_block(
2402 std::__to_address(__buf.back()),
2403 std::__to_address(__buf.back() + __block_size));
2405 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2409 __annotate_delete();
2410 for (__map_pointer __i = __buf.begin();
2411 __i != __buf.end(); ++__i)
2412 __alloc_traits::deallocate(__a, *__i, __block_size);
2415 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
2416 for (; __back_capacity > 0; --__back_capacity)
2418 __buf.push_back(__map_.back());
2421 for (__map_pointer __i = __map_.begin();
2422 __i != __map_.end(); ++__i)
2423 __buf.push_back(*__i);
2424 _VSTD::swap(__map_.__first_, __buf.__first_);
2425 _VSTD::swap(__map_.__begin_, __buf.__begin_);
2426 _VSTD::swap(__map_.__end_, __buf.__end_);
2427 _VSTD::swap(__map_.__end_cap(), __buf.__end_cap());
2432 // Create back capacity for one block of elements.
2433 // Strong guarantee. Either do it or don't touch anything.
2434 template <class _Tp, class _Allocator>
2436 deque<_Tp, _Allocator>::__add_back_capacity()
2438 allocator_type& __a = __alloc();
2439 if (__front_spare() >= __block_size)
2441 __start_ -= __block_size;
2442 pointer __pt = __map_.front();
2444 __map_.push_back(__pt);
2446 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2447 else if (__map_.size() < __map_.capacity())
2448 { // we can put the new buffer into the map, but don't shift things around
2449 // until it is allocated. If we throw, we don't need to fix
2450 // anything up (any added buffers are undetectible)
2451 if (__map_.__back_spare() != 0)
2452 __map_.push_back(__alloc_traits::allocate(__a, __block_size));
2455 __map_.push_front(__alloc_traits::allocate(__a, __block_size));
2456 // Done allocating, reorder capacity
2457 pointer __pt = __map_.front();
2459 __map_.push_back(__pt);
2461 __annotate_whole_block(__map_.size() - 1, __asan_poison);
2463 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2466 __split_buffer<pointer, __pointer_allocator&>
2467 __buf(std::max<size_type>(2* __map_.capacity(), 1),
2471 typedef __allocator_destructor<_Allocator> _Dp;
2472 unique_ptr<pointer, _Dp> __hold(
2473 __alloc_traits::allocate(__a, __block_size),
2474 _Dp(__a, __block_size));
2475 __buf.push_back(__hold.get());
2478 for (__map_pointer __i = __map_.end();
2479 __i != __map_.begin();)
2480 __buf.push_front(*--__i);
2481 _VSTD::swap(__map_.__first_, __buf.__first_);
2482 _VSTD::swap(__map_.__begin_, __buf.__begin_);
2483 _VSTD::swap(__map_.__end_, __buf.__end_);
2484 _VSTD::swap(__map_.__end_cap(), __buf.__end_cap());
2485 __annotate_whole_block(__map_.size() - 1, __asan_poison);
2489 // Create back capacity for __n elements.
2490 // Strong guarantee. Either do it or don't touch anything.
2491 template <class _Tp, class _Allocator>
2493 deque<_Tp, _Allocator>::__add_back_capacity(size_type __n)
2495 allocator_type& __a = __alloc();
2496 size_type __nb = __recommend_blocks(__n + __map_.empty());
2497 // Number of unused blocks at front:
2498 size_type __front_capacity = __front_spare() / __block_size;
2499 __front_capacity = _VSTD::min(__front_capacity, __nb); // don't take more than you need
2500 __nb -= __front_capacity; // number of blocks need to allocate
2501 // If __nb == 0, then we have sufficient capacity.
2504 __start_ -= __block_size * __front_capacity;
2505 for (; __front_capacity > 0; --__front_capacity)
2507 pointer __pt = __map_.front();
2509 __map_.push_back(__pt);
2512 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2513 else if (__nb <= __map_.capacity() - __map_.size())
2514 { // we can put the new buffers into the map, but don't shift things around
2515 // until all buffers are allocated. If we throw, we don't need to fix
2516 // anything up (any added buffers are undetectible)
2517 for (; __nb > 0; --__nb)
2519 if (__map_.__back_spare() == 0)
2521 __map_.push_back(__alloc_traits::allocate(__a, __block_size));
2522 __annotate_whole_block(__map_.size() - 1, __asan_poison);
2524 for (; __nb > 0; --__nb, ++__front_capacity, __start_ +=
2525 __block_size - (__map_.size() == 1)) {
2526 __map_.push_front(__alloc_traits::allocate(__a, __block_size));
2527 __annotate_whole_block(0, __asan_poison);
2529 // Done allocating, reorder capacity
2530 __start_ -= __block_size * __front_capacity;
2531 for (; __front_capacity > 0; --__front_capacity)
2533 pointer __pt = __map_.front();
2535 __map_.push_back(__pt);
2538 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2541 size_type __ds = __front_capacity * __block_size;
2542 __split_buffer<pointer, __pointer_allocator&>
2543 __buf(std::max<size_type>(2* __map_.capacity(),
2544 __nb + __map_.size()),
2545 __map_.size() - __front_capacity,
2547 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2550 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
2551 for (; __nb > 0; --__nb) {
2552 __buf.push_back(__alloc_traits::allocate(__a, __block_size));
2553 // ASan: this is an empty container, we have to poison the whole block
2554 __annotate_poison_block(
2555 std::__to_address(__buf.back()),
2556 std::__to_address(__buf.back() + __block_size));
2558 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2562 __annotate_delete();
2563 for (__map_pointer __i = __buf.begin();
2564 __i != __buf.end(); ++__i)
2565 __alloc_traits::deallocate(__a, *__i, __block_size);
2568 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
2569 for (; __front_capacity > 0; --__front_capacity)
2571 __buf.push_back(__map_.front());
2574 for (__map_pointer __i = __map_.end();
2575 __i != __map_.begin();)
2576 __buf.push_front(*--__i);
2577 _VSTD::swap(__map_.__first_, __buf.__first_);
2578 _VSTD::swap(__map_.__begin_, __buf.__begin_);
2579 _VSTD::swap(__map_.__end_, __buf.__end_);
2580 _VSTD::swap(__map_.__end_cap(), __buf.__end_cap());
2585 template <class _Tp, class _Allocator>
2587 deque<_Tp, _Allocator>::pop_front()
2589 size_type __old_sz = size();
2590 size_type __old_start = __start_;
2591 allocator_type& __a = __alloc();
2592 __alloc_traits::destroy(__a, _VSTD::__to_address(*(__map_.begin() +
2593 __start_ / __block_size) +
2594 __start_ % __block_size));
2597 __annotate_shrink_front(__old_sz, __old_start);
2598 __maybe_remove_front_spare();
2601 template <class _Tp, class _Allocator>
2603 deque<_Tp, _Allocator>::pop_back()
2605 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "deque::pop_back called on an empty deque");
2606 size_type __old_sz = size();
2607 size_type __old_start = __start_;
2608 allocator_type& __a = __alloc();
2609 size_type __p = size() + __start_ - 1;
2610 __alloc_traits::destroy(__a, _VSTD::__to_address(*(__map_.begin() +
2611 __p / __block_size) +
2612 __p % __block_size));
2614 __annotate_shrink_back(__old_sz, __old_start);
2615 __maybe_remove_back_spare();
2618 // move assign [__f, __l) to [__r, __r + (__l-__f)).
2619 // If __vt points into [__f, __l), then subtract (__f - __r) from __vt.
2620 template <class _Tp, class _Allocator>
2621 typename deque<_Tp, _Allocator>::iterator
2622 deque<_Tp, _Allocator>::__move_and_check(iterator __f, iterator __l, iterator __r,
2623 const_pointer& __vt)
2626 // for (; __f != __l; ++__f, ++__r)
2627 // *__r = _VSTD::move(*__f);
2628 difference_type __n = __l - __f;
2631 pointer __fb = __f.__ptr_;
2632 pointer __fe = *__f.__m_iter_ + __block_size;
2633 difference_type __bs = __fe - __fb;
2639 if (__fb <= __vt && __vt < __fe)
2640 __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) -= __f - __r).__ptr_;
2641 __r = _VSTD::move(__fb, __fe, __r);
2648 // move assign [__f, __l) to [__r - (__l-__f), __r) backwards.
2649 // If __vt points into [__f, __l), then add (__r - __l) to __vt.
2650 template <class _Tp, class _Allocator>
2651 typename deque<_Tp, _Allocator>::iterator
2652 deque<_Tp, _Allocator>::__move_backward_and_check(iterator __f, iterator __l, iterator __r,
2653 const_pointer& __vt)
2656 // while (__f != __l)
2657 // *--__r = _VSTD::move(*--__l);
2658 difference_type __n = __l - __f;
2662 pointer __lb = *__l.__m_iter_;
2663 pointer __le = __l.__ptr_ + 1;
2664 difference_type __bs = __le - __lb;
2670 if (__lb <= __vt && __vt < __le)
2671 __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) += __r - __l - 1).__ptr_;
2672 __r = _VSTD::move_backward(__lb, __le, __r);
2679 // move construct [__f, __l) to [__r, __r + (__l-__f)).
2680 // If __vt points into [__f, __l), then add (__r - __f) to __vt.
2681 template <class _Tp, class _Allocator>
2683 deque<_Tp, _Allocator>::__move_construct_and_check(iterator __f, iterator __l,
2684 iterator __r, const_pointer& __vt)
2686 allocator_type& __a = __alloc();
2688 // for (; __f != __l; ++__r, ++__f, ++__size())
2689 // __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__f));
2690 difference_type __n = __l - __f;
2693 pointer __fb = __f.__ptr_;
2694 pointer __fe = *__f.__m_iter_ + __block_size;
2695 difference_type __bs = __fe - __fb;
2701 if (__fb <= __vt && __vt < __fe)
2702 __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) += __r - __f).__ptr_;
2703 for (; __fb != __fe; ++__fb, ++__r, ++__size())
2704 __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__fb));
2710 // move construct [__f, __l) to [__r - (__l-__f), __r) backwards.
2711 // If __vt points into [__f, __l), then subtract (__l - __r) from __vt.
2712 template <class _Tp, class _Allocator>
2714 deque<_Tp, _Allocator>::__move_construct_backward_and_check(iterator __f, iterator __l,
2715 iterator __r, const_pointer& __vt)
2717 allocator_type& __a = __alloc();
2719 // for (iterator __j = __l; __j != __f;)
2721 // __alloc_traitsconstruct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__j));
2725 difference_type __n = __l - __f;
2729 pointer __lb = *__l.__m_iter_;
2730 pointer __le = __l.__ptr_ + 1;
2731 difference_type __bs = __le - __lb;
2737 if (__lb <= __vt && __vt < __le)
2738 __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) -= __l - __r + 1).__ptr_;
2739 while (__le != __lb)
2741 __alloc_traits::construct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__le));
2750 template <class _Tp, class _Allocator>
2751 typename deque<_Tp, _Allocator>::iterator
2752 deque<_Tp, _Allocator>::erase(const_iterator __f)
2754 size_type __old_sz = size();
2755 size_type __old_start = __start_;
2756 iterator __b = begin();
2757 difference_type __pos = __f - __b;
2758 iterator __p = __b + __pos;
2759 allocator_type& __a = __alloc();
2760 if (static_cast<size_t>(__pos) <= (size() - 1) / 2)
2761 { // erase from front
2762 _VSTD::move_backward(__b, __p, _VSTD::next(__p));
2763 __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
2766 __annotate_shrink_front(__old_sz, __old_start);
2767 __maybe_remove_front_spare();
2770 { // erase from back
2771 iterator __i = _VSTD::move(_VSTD::next(__p), end(), __p);
2772 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
2774 __annotate_shrink_back(__old_sz, __old_start);
2775 __maybe_remove_back_spare();
2777 return begin() + __pos;
2780 template <class _Tp, class _Allocator>
2781 typename deque<_Tp, _Allocator>::iterator
2782 deque<_Tp, _Allocator>::erase(const_iterator __f, const_iterator __l)
2784 size_type __old_sz = size();
2785 size_type __old_start = __start_;
2786 difference_type __n = __l - __f;
2787 iterator __b = begin();
2788 difference_type __pos = __f - __b;
2789 iterator __p = __b + __pos;
2792 allocator_type& __a = __alloc();
2793 if (static_cast<size_t>(__pos) <= (size() - __n) / 2)
2794 { // erase from front
2795 iterator __i = _VSTD::move_backward(__b, __p, __p + __n);
2796 for (; __b != __i; ++__b)
2797 __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
2800 __annotate_shrink_front(__old_sz, __old_start);
2801 while (__maybe_remove_front_spare()) {
2805 { // erase from back
2806 iterator __i = _VSTD::move(__p + __n, end(), __p);
2807 for (iterator __e = end(); __i != __e; ++__i)
2808 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
2810 __annotate_shrink_back(__old_sz, __old_start);
2811 while (__maybe_remove_back_spare()) {
2815 return begin() + __pos;
2818 template <class _Tp, class _Allocator>
2820 deque<_Tp, _Allocator>::__erase_to_end(const_iterator __f)
2822 size_type __old_sz = size();
2823 size_type __old_start = __start_;
2824 iterator __e = end();
2825 difference_type __n = __e - __f;
2828 allocator_type& __a = __alloc();
2829 iterator __b = begin();
2830 difference_type __pos = __f - __b;
2831 for (iterator __p = __b + __pos; __p != __e; ++__p)
2832 __alloc_traits::destroy(__a, _VSTD::addressof(*__p));
2834 __annotate_shrink_back(__old_sz, __old_start);
2835 while (__maybe_remove_back_spare()) {
2840 template <class _Tp, class _Allocator>
2843 deque<_Tp, _Allocator>::swap(deque& __c)
2844 #if _LIBCPP_STD_VER >= 14
2847 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2848 __is_nothrow_swappable<allocator_type>::value)
2851 __map_.swap(__c.__map_);
2852 _VSTD::swap(__start_, __c.__start_);
2853 _VSTD::swap(__size(), __c.__size());
2854 _VSTD::__swap_allocator(__alloc(), __c.__alloc());
2857 template <class _Tp, class _Allocator>
2860 deque<_Tp, _Allocator>::clear() _NOEXCEPT
2862 __annotate_delete();
2863 allocator_type& __a = __alloc();
2864 for (iterator __i = begin(), __e = end(); __i != __e; ++__i)
2865 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
2867 while (__map_.size() > 2)
2869 __alloc_traits::deallocate(__a, __map_.front(), __block_size);
2872 switch (__map_.size())
2875 __start_ = __block_size / 2;
2878 __start_ = __block_size;
2884 template <class _Tp, class _Allocator>
2885 inline _LIBCPP_HIDE_FROM_ABI
2887 operator==(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2889 const typename deque<_Tp, _Allocator>::size_type __sz = __x.size();
2890 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
2893 #if _LIBCPP_STD_VER <= 17
2895 template <class _Tp, class _Allocator>
2896 inline _LIBCPP_HIDE_FROM_ABI
2898 operator!=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2900 return !(__x == __y);
2903 template <class _Tp, class _Allocator>
2904 inline _LIBCPP_HIDE_FROM_ABI
2906 operator< (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2908 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
2911 template <class _Tp, class _Allocator>
2912 inline _LIBCPP_HIDE_FROM_ABI
2914 operator> (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2919 template <class _Tp, class _Allocator>
2920 inline _LIBCPP_HIDE_FROM_ABI
2922 operator>=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2924 return !(__x < __y);
2927 template <class _Tp, class _Allocator>
2928 inline _LIBCPP_HIDE_FROM_ABI
2930 operator<=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2932 return !(__y < __x);
2935 #else // _LIBCPP_STD_VER <= 17
2937 template <class _Tp, class _Allocator>
2938 _LIBCPP_HIDE_FROM_ABI __synth_three_way_result<_Tp>
2939 operator<=>(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) {
2940 return std::lexicographical_compare_three_way(
2941 __x.begin(), __x.end(), __y.begin(), __y.end(), std::__synth_three_way<_Tp, _Tp>);
2944 #endif // _LIBCPP_STD_VER <= 17
2946 template <class _Tp, class _Allocator>
2947 inline _LIBCPP_HIDE_FROM_ABI
2949 swap(deque<_Tp, _Allocator>& __x, deque<_Tp, _Allocator>& __y)
2950 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2955 #if _LIBCPP_STD_VER >= 20
2956 template <class _Tp, class _Allocator, class _Up>
2957 inline _LIBCPP_HIDE_FROM_ABI typename deque<_Tp, _Allocator>::size_type
2958 erase(deque<_Tp, _Allocator>& __c, const _Up& __v) {
2959 auto __old_size = __c.size();
2960 __c.erase(_VSTD::remove(__c.begin(), __c.end(), __v), __c.end());
2961 return __old_size - __c.size();
2964 template <class _Tp, class _Allocator, class _Predicate>
2965 inline _LIBCPP_HIDE_FROM_ABI typename deque<_Tp, _Allocator>::size_type
2966 erase_if(deque<_Tp, _Allocator>& __c, _Predicate __pred) {
2967 auto __old_size = __c.size();
2968 __c.erase(_VSTD::remove_if(__c.begin(), __c.end(), __pred), __c.end());
2969 return __old_size - __c.size();
2973 inline constexpr bool __format::__enable_insertable<std::deque<char>> = true;
2974 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
2976 inline constexpr bool __format::__enable_insertable<std::deque<wchar_t>> = true;
2979 #endif // _LIBCPP_STD_VER >= 20
2981 _LIBCPP_END_NAMESPACE_STD
2983 #if _LIBCPP_STD_VER >= 17
2984 _LIBCPP_BEGIN_NAMESPACE_STD
2986 template <class _ValueT>
2987 using deque _LIBCPP_AVAILABILITY_PMR = std::deque<_ValueT, polymorphic_allocator<_ValueT>>;
2989 _LIBCPP_END_NAMESPACE_STD
2994 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
2995 # include <algorithm>
2997 # include <concepts>
2999 # include <functional>
3001 # include <iterator>
3002 # include <type_traits>
3003 # include <typeinfo>
3006 #endif // _LIBCPP_DEQUE