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/max.h>
188 #include <__algorithm/min.h>
189 #include <__algorithm/move.h>
190 #include <__algorithm/move_backward.h>
191 #include <__algorithm/remove.h>
192 #include <__algorithm/remove_if.h>
193 #include <__algorithm/unwrap_iter.h>
196 #include <__debug_utils/sanitizers.h>
197 #include <__format/enable_insertable.h>
198 #include <__fwd/deque.h>
199 #include <__iterator/distance.h>
200 #include <__iterator/iterator_traits.h>
201 #include <__iterator/move_iterator.h>
202 #include <__iterator/next.h>
203 #include <__iterator/prev.h>
204 #include <__iterator/reverse_iterator.h>
205 #include <__iterator/segmented_iterator.h>
206 #include <__memory/addressof.h>
207 #include <__memory/allocator.h>
208 #include <__memory/allocator_destructor.h>
209 #include <__memory/allocator_traits.h>
210 #include <__memory/compressed_pair.h>
211 #include <__memory/pointer_traits.h>
212 #include <__memory/swap_allocator.h>
213 #include <__memory/temp_value.h>
214 #include <__memory/unique_ptr.h>
215 #include <__memory_resource/polymorphic_allocator.h>
216 #include <__ranges/access.h>
217 #include <__ranges/concepts.h>
218 #include <__ranges/container_compatible_range.h>
219 #include <__ranges/from_range.h>
220 #include <__ranges/size.h>
221 #include <__split_buffer>
222 #include <__type_traits/conditional.h>
223 #include <__type_traits/container_traits.h>
224 #include <__type_traits/disjunction.h>
225 #include <__type_traits/enable_if.h>
226 #include <__type_traits/is_allocator.h>
227 #include <__type_traits/is_convertible.h>
228 #include <__type_traits/is_nothrow_assignable.h>
229 #include <__type_traits/is_nothrow_constructible.h>
230 #include <__type_traits/is_same.h>
231 #include <__type_traits/is_swappable.h>
232 #include <__type_traits/is_trivially_relocatable.h>
233 #include <__type_traits/type_identity.h>
234 #include <__utility/forward.h>
235 #include <__utility/move.h>
236 #include <__utility/pair.h>
237 #include <__utility/swap.h>
242 // standard-mandated includes
245 #include <__iterator/access.h>
246 #include <__iterator/data.h>
247 #include <__iterator/empty.h>
248 #include <__iterator/reverse_access.h>
249 #include <__iterator/size.h>
253 #include <initializer_list>
255 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
256 # pragma GCC system_header
260 #include <__undef_macros>
262 _LIBCPP_BEGIN_NAMESPACE_STD
264 template <class _ValueType, class _DiffType>
265 struct __deque_block_size {
266 static const _DiffType value = sizeof(_ValueType) < 256 ? 4096 / sizeof(_ValueType) : 16;
269 template <class _ValueType,
275 #ifdef _LIBCPP_ABI_INCOMPLETE_TYPES_IN_DEQUE
276 // Keep template parameter to avoid changing all template declarations thoughout
280 __deque_block_size<_ValueType, _DiffType>::value
283 class _LIBCPP_TEMPLATE_VIS __deque_iterator {
284 typedef _MapPointer __map_iterator;
287 typedef _Pointer pointer;
288 typedef _DiffType difference_type;
291 __map_iterator __m_iter_;
294 static const difference_type __block_size;
297 typedef _ValueType value_type;
298 typedef random_access_iterator_tag iterator_category;
299 typedef _Reference reference;
301 _LIBCPP_HIDE_FROM_ABI __deque_iterator() _NOEXCEPT
302 #if _LIBCPP_STD_VER >= 14
303 : __m_iter_(nullptr),
309 template <class _Pp, class _Rp, class _MP, __enable_if_t<is_convertible<_Pp, pointer>::value, int> = 0>
310 _LIBCPP_HIDE_FROM_ABI
311 __deque_iterator(const __deque_iterator<value_type, _Pp, _Rp, _MP, difference_type, _BS>& __it) _NOEXCEPT
312 : __m_iter_(__it.__m_iter_),
313 __ptr_(__it.__ptr_) {}
315 _LIBCPP_HIDE_FROM_ABI reference operator*() const { return *__ptr_; }
316 _LIBCPP_HIDE_FROM_ABI pointer operator->() const { return __ptr_; }
318 _LIBCPP_HIDE_FROM_ABI __deque_iterator& operator++() {
319 if (++__ptr_ - *__m_iter_ == __block_size) {
326 _LIBCPP_HIDE_FROM_ABI __deque_iterator operator++(int) {
327 __deque_iterator __tmp = *this;
332 _LIBCPP_HIDE_FROM_ABI __deque_iterator& operator--() {
333 if (__ptr_ == *__m_iter_) {
335 __ptr_ = *__m_iter_ + __block_size;
341 _LIBCPP_HIDE_FROM_ABI __deque_iterator operator--(int) {
342 __deque_iterator __tmp = *this;
347 _LIBCPP_HIDE_FROM_ABI __deque_iterator& operator+=(difference_type __n) {
349 __n += __ptr_ - *__m_iter_;
351 __m_iter_ += __n / __block_size;
352 __ptr_ = *__m_iter_ + __n % __block_size;
355 difference_type __z = __block_size - 1 - __n;
356 __m_iter_ -= __z / __block_size;
357 __ptr_ = *__m_iter_ + (__block_size - 1 - __z % __block_size);
363 _LIBCPP_HIDE_FROM_ABI __deque_iterator& operator-=(difference_type __n) { return *this += -__n; }
365 _LIBCPP_HIDE_FROM_ABI __deque_iterator operator+(difference_type __n) const {
366 __deque_iterator __t(*this);
371 _LIBCPP_HIDE_FROM_ABI __deque_iterator operator-(difference_type __n) const {
372 __deque_iterator __t(*this);
377 _LIBCPP_HIDE_FROM_ABI friend __deque_iterator operator+(difference_type __n, const __deque_iterator& __it) {
381 _LIBCPP_HIDE_FROM_ABI friend difference_type operator-(const __deque_iterator& __x, const __deque_iterator& __y) {
383 return (__x.__m_iter_ - __y.__m_iter_) * __block_size + (__x.__ptr_ - *__x.__m_iter_) -
384 (__y.__ptr_ - *__y.__m_iter_);
388 _LIBCPP_HIDE_FROM_ABI reference operator[](difference_type __n) const { return *(*this + __n); }
390 _LIBCPP_HIDE_FROM_ABI friend bool operator==(const __deque_iterator& __x, const __deque_iterator& __y) {
391 return __x.__ptr_ == __y.__ptr_;
394 #if _LIBCPP_STD_VER <= 17
395 _LIBCPP_HIDE_FROM_ABI friend bool operator!=(const __deque_iterator& __x, const __deque_iterator& __y) {
396 return !(__x == __y);
398 _LIBCPP_HIDE_FROM_ABI friend bool operator<(const __deque_iterator& __x, const __deque_iterator& __y) {
399 return __x.__m_iter_ < __y.__m_iter_ || (__x.__m_iter_ == __y.__m_iter_ && __x.__ptr_ < __y.__ptr_);
402 _LIBCPP_HIDE_FROM_ABI friend bool operator>(const __deque_iterator& __x, const __deque_iterator& __y) {
406 _LIBCPP_HIDE_FROM_ABI friend bool operator<=(const __deque_iterator& __x, const __deque_iterator& __y) {
410 _LIBCPP_HIDE_FROM_ABI friend bool operator>=(const __deque_iterator& __x, const __deque_iterator& __y) {
416 _LIBCPP_HIDE_FROM_ABI friend strong_ordering operator<=>(const __deque_iterator& __x, const __deque_iterator& __y) {
417 if (__x.__m_iter_ < __y.__m_iter_)
418 return strong_ordering::less;
420 if (__x.__m_iter_ == __y.__m_iter_) {
421 if constexpr (three_way_comparable<pointer, strong_ordering>) {
422 return __x.__ptr_ <=> __y.__ptr_;
424 if (__x.__ptr_ < __y.__ptr_)
425 return strong_ordering::less;
427 if (__x.__ptr_ == __y.__ptr_)
428 return strong_ordering::equal;
430 return strong_ordering::greater;
434 return strong_ordering::greater;
436 #endif // _LIBCPP_STD_VER >= 20
439 _LIBCPP_HIDE_FROM_ABI explicit __deque_iterator(__map_iterator __m, pointer __p) _NOEXCEPT
443 template <class _Tp, class _Ap>
444 friend class _LIBCPP_TEMPLATE_VIS deque;
445 template <class _Vp, class _Pp, class _Rp, class _MP, class _Dp, _Dp>
446 friend class _LIBCPP_TEMPLATE_VIS __deque_iterator;
449 friend struct __segmented_iterator_traits;
452 template <class _ValueType, class _Pointer, class _Reference, class _MapPointer, class _DiffType, _DiffType _BlockSize>
453 struct __segmented_iterator_traits<
454 __deque_iterator<_ValueType, _Pointer, _Reference, _MapPointer, _DiffType, _BlockSize> > {
456 using _Iterator = __deque_iterator<_ValueType, _Pointer, _Reference, _MapPointer, _DiffType, _BlockSize>;
459 using __is_segmented_iterator = true_type;
460 using __segment_iterator = _MapPointer;
461 using __local_iterator = _Pointer;
463 static _LIBCPP_HIDE_FROM_ABI __segment_iterator __segment(_Iterator __iter) { return __iter.__m_iter_; }
464 static _LIBCPP_HIDE_FROM_ABI __local_iterator __local(_Iterator __iter) { return __iter.__ptr_; }
465 static _LIBCPP_HIDE_FROM_ABI __local_iterator __begin(__segment_iterator __iter) { return *__iter; }
467 static _LIBCPP_HIDE_FROM_ABI __local_iterator __end(__segment_iterator __iter) {
468 return *__iter + _Iterator::__block_size;
471 static _LIBCPP_HIDE_FROM_ABI _Iterator __compose(__segment_iterator __segment, __local_iterator __local) {
472 if (__segment && __local == __end(__segment)) {
474 return _Iterator(__segment, *__segment);
476 return _Iterator(__segment, __local);
480 template <class _ValueType, class _Pointer, class _Reference, class _MapPointer, class _DiffType, _DiffType _BlockSize>
481 const _DiffType __deque_iterator<_ValueType, _Pointer, _Reference, _MapPointer, _DiffType, _BlockSize>::__block_size =
482 __deque_block_size<_ValueType, _DiffType>::value;
484 template <class _Tp, class _Allocator /*= allocator<_Tp>*/>
485 class _LIBCPP_TEMPLATE_VIS deque {
489 using value_type = _Tp;
491 using allocator_type = _Allocator;
492 using __alloc_traits = allocator_traits<allocator_type>;
493 static_assert(__check_valid_allocator<allocator_type>::value, "");
494 static_assert(is_same<typename allocator_type::value_type, value_type>::value,
495 "Allocator::value_type must be same type as value_type");
497 using size_type = typename __alloc_traits::size_type;
498 using difference_type = typename __alloc_traits::difference_type;
500 using pointer = typename __alloc_traits::pointer;
501 using const_pointer = typename __alloc_traits::const_pointer;
503 using __pointer_allocator = __rebind_alloc<__alloc_traits, pointer>;
504 using __const_pointer_allocator = __rebind_alloc<__alloc_traits, const_pointer>;
505 using __map = __split_buffer<pointer, __pointer_allocator>;
506 using __map_alloc_traits = allocator_traits<__pointer_allocator>;
507 using __map_pointer = typename __map_alloc_traits::pointer;
508 using __map_const_pointer = typename allocator_traits<__const_pointer_allocator>::const_pointer;
509 using __map_const_iterator = typename __map::const_iterator;
511 using reference = value_type&;
512 using const_reference = const value_type&;
514 using iterator = __deque_iterator<value_type, pointer, reference, __map_pointer, difference_type>;
515 using const_iterator =
516 __deque_iterator<value_type, const_pointer, const_reference, __map_const_pointer, difference_type>;
517 using reverse_iterator = std::reverse_iterator<iterator>;
518 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
520 // A deque contains the following members which may be trivially relocatable:
521 // - __map: is a `__split_buffer`, see `__split_buffer` for more information on when it is trivially relocatable
522 // - size_type: is always trivially relocatable, since it is required to be an integral type
523 // - allocator_type: may not be trivially relocatable, so it's checked
524 // None of these are referencing the `deque` itself, so if all of them are trivially relocatable, `deque` is too.
525 using __trivially_relocatable = __conditional_t<
526 __libcpp_is_trivially_relocatable<__map>::value && __libcpp_is_trivially_relocatable<allocator_type>::value,
530 static_assert(is_nothrow_default_constructible<allocator_type>::value ==
531 is_nothrow_default_constructible<__pointer_allocator>::value,
532 "rebinding an allocator should not change exception guarantees");
533 static_assert(is_nothrow_move_constructible<allocator_type>::value ==
534 is_nothrow_move_constructible<typename __map::allocator_type>::value,
535 "rebinding an allocator should not change exception guarantees");
538 struct __deque_block_range {
539 explicit _LIBCPP_HIDE_FROM_ABI __deque_block_range(pointer __b, pointer __e) _NOEXCEPT
542 const pointer __begin_;
543 const pointer __end_;
546 struct __deque_range {
548 const iterator __end_;
550 _LIBCPP_HIDE_FROM_ABI __deque_range(iterator __pos, iterator __e) _NOEXCEPT : __pos_(__pos), __end_(__e) {}
552 explicit _LIBCPP_HIDE_FROM_ABI operator bool() const _NOEXCEPT { return __pos_ != __end_; }
554 _LIBCPP_HIDE_FROM_ABI __deque_range begin() const { return *this; }
556 _LIBCPP_HIDE_FROM_ABI __deque_range end() const { return __deque_range(__end_, __end_); }
557 _LIBCPP_HIDE_FROM_ABI __deque_block_range operator*() const _NOEXCEPT {
558 if (__pos_.__m_iter_ == __end_.__m_iter_) {
559 return __deque_block_range(__pos_.__ptr_, __end_.__ptr_);
561 return __deque_block_range(__pos_.__ptr_, *__pos_.__m_iter_ + __block_size);
564 _LIBCPP_HIDE_FROM_ABI __deque_range& operator++() _NOEXCEPT {
565 if (__pos_.__m_iter_ == __end_.__m_iter_) {
569 __pos_.__ptr_ = *__pos_.__m_iter_;
574 _LIBCPP_HIDE_FROM_ABI friend bool operator==(__deque_range const& __lhs, __deque_range const& __rhs) {
575 return __lhs.__pos_ == __rhs.__pos_;
577 _LIBCPP_HIDE_FROM_ABI friend bool operator!=(__deque_range const& __lhs, __deque_range const& __rhs) {
578 return !(__lhs == __rhs);
582 struct _ConstructTransaction {
583 _LIBCPP_HIDE_FROM_ABI _ConstructTransaction(deque* __db, __deque_block_range& __r)
584 : __pos_(__r.__begin_), __end_(__r.__end_), __begin_(__r.__begin_), __base_(__db) {}
586 _LIBCPP_HIDE_FROM_ABI ~_ConstructTransaction() { __base_->__size() += (__pos_ - __begin_); }
589 const pointer __end_;
592 const pointer __begin_;
593 deque* const __base_;
596 static const difference_type __block_size;
600 _LIBCPP_COMPRESSED_PAIR(size_type, __size_, allocator_type, __alloc_);
603 // construct/copy/destroy:
604 _LIBCPP_HIDE_FROM_ABI deque() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
605 : __start_(0), __size_(0) {
609 _LIBCPP_HIDE_FROM_ABI ~deque() {
612 typename __map::iterator __i = __map_.begin();
613 typename __map::iterator __e = __map_.end();
614 for (; __i != __e; ++__i)
615 __alloc_traits::deallocate(__alloc(), *__i, __block_size);
618 _LIBCPP_HIDE_FROM_ABI explicit deque(const allocator_type& __a)
619 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0), __alloc_(__a) {
623 explicit _LIBCPP_HIDE_FROM_ABI deque(size_type __n);
624 #if _LIBCPP_STD_VER >= 14
625 explicit _LIBCPP_HIDE_FROM_ABI deque(size_type __n, const _Allocator& __a);
627 _LIBCPP_HIDE_FROM_ABI deque(size_type __n, const value_type& __v);
629 template <__enable_if_t<__is_allocator<_Allocator>::value, int> = 0>
630 _LIBCPP_HIDE_FROM_ABI deque(size_type __n, const value_type& __v, const allocator_type& __a)
631 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0), __alloc_(__a) {
637 template <class _InputIter, __enable_if_t<__has_input_iterator_category<_InputIter>::value, int> = 0>
638 _LIBCPP_HIDE_FROM_ABI deque(_InputIter __f, _InputIter __l);
639 template <class _InputIter, __enable_if_t<__has_input_iterator_category<_InputIter>::value, int> = 0>
640 _LIBCPP_HIDE_FROM_ABI deque(_InputIter __f, _InputIter __l, const allocator_type& __a);
642 #if _LIBCPP_STD_VER >= 23
643 template <_ContainerCompatibleRange<_Tp> _Range>
644 _LIBCPP_HIDE_FROM_ABI deque(from_range_t, _Range&& __range, const allocator_type& __a = allocator_type())
645 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0), __alloc_(__a) {
646 if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
647 __append_with_size(ranges::begin(__range), ranges::distance(__range));
650 for (auto&& __e : __range) {
651 emplace_back(std::forward<decltype(__e)>(__e));
657 _LIBCPP_HIDE_FROM_ABI deque(const deque& __c);
658 _LIBCPP_HIDE_FROM_ABI deque(const deque& __c, const __type_identity_t<allocator_type>& __a);
660 _LIBCPP_HIDE_FROM_ABI deque& operator=(const deque& __c);
662 #ifndef _LIBCPP_CXX03_LANG
663 _LIBCPP_HIDE_FROM_ABI deque(initializer_list<value_type> __il);
664 _LIBCPP_HIDE_FROM_ABI deque(initializer_list<value_type> __il, const allocator_type& __a);
666 _LIBCPP_HIDE_FROM_ABI deque& operator=(initializer_list<value_type> __il) {
671 _LIBCPP_HIDE_FROM_ABI deque(deque&& __c) noexcept(is_nothrow_move_constructible<allocator_type>::value);
672 _LIBCPP_HIDE_FROM_ABI deque(deque&& __c, const __type_identity_t<allocator_type>& __a);
673 _LIBCPP_HIDE_FROM_ABI deque&
674 operator=(deque&& __c) noexcept(__alloc_traits::propagate_on_container_move_assignment::value &&
675 is_nothrow_move_assignable<allocator_type>::value);
677 _LIBCPP_HIDE_FROM_ABI void assign(initializer_list<value_type> __il) { assign(__il.begin(), __il.end()); }
678 #endif // _LIBCPP_CXX03_LANG
680 template <class _InputIter,
681 __enable_if_t<__has_input_iterator_category<_InputIter>::value &&
682 !__has_random_access_iterator_category<_InputIter>::value,
684 _LIBCPP_HIDE_FROM_ABI void assign(_InputIter __f, _InputIter __l);
685 template <class _RAIter, __enable_if_t<__has_random_access_iterator_category<_RAIter>::value, int> = 0>
686 _LIBCPP_HIDE_FROM_ABI void assign(_RAIter __f, _RAIter __l);
688 #if _LIBCPP_STD_VER >= 23
689 template <_ContainerCompatibleRange<_Tp> _Range>
690 _LIBCPP_HIDE_FROM_ABI void assign_range(_Range&& __range) {
691 if constexpr (ranges::random_access_range<_Range>) {
692 auto __n = static_cast<size_type>(ranges::distance(__range));
693 __assign_with_size_random_access(ranges::begin(__range), __n);
695 } else if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
696 auto __n = static_cast<size_type>(ranges::distance(__range));
697 __assign_with_size(ranges::begin(__range), __n);
700 __assign_with_sentinel(ranges::begin(__range), ranges::end(__range));
705 _LIBCPP_HIDE_FROM_ABI void assign(size_type __n, const value_type& __v);
707 _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const _NOEXCEPT;
708 _LIBCPP_HIDE_FROM_ABI allocator_type& __alloc() _NOEXCEPT { return __alloc_; }
709 _LIBCPP_HIDE_FROM_ABI const allocator_type& __alloc() const _NOEXCEPT { return __alloc_; }
713 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT {
714 __map_pointer __mp = __map_.begin() + __start_ / __block_size;
715 return iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
718 _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT {
719 __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __start_ / __block_size);
720 return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
723 _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT {
724 size_type __p = size() + __start_;
725 __map_pointer __mp = __map_.begin() + __p / __block_size;
726 return iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
729 _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT {
730 size_type __p = size() + __start_;
731 __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __p / __block_size);
732 return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
735 _LIBCPP_HIDE_FROM_ABI reverse_iterator rbegin() _NOEXCEPT { return reverse_iterator(end()); }
736 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rbegin() const _NOEXCEPT { return const_reverse_iterator(end()); }
737 _LIBCPP_HIDE_FROM_ABI reverse_iterator rend() _NOEXCEPT { return reverse_iterator(begin()); }
738 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rend() const _NOEXCEPT { return const_reverse_iterator(begin()); }
740 _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT { return begin(); }
741 _LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT { return end(); }
742 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crbegin() const _NOEXCEPT { return const_reverse_iterator(end()); }
743 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crend() const _NOEXCEPT { return const_reverse_iterator(begin()); }
746 _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return __size(); }
748 _LIBCPP_HIDE_FROM_ABI size_type& __size() _NOEXCEPT { return __size_; }
749 _LIBCPP_HIDE_FROM_ABI const size_type& __size() const _NOEXCEPT { return __size_; }
751 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
752 return std::min<size_type>(__alloc_traits::max_size(__alloc()), numeric_limits<difference_type>::max());
754 _LIBCPP_HIDE_FROM_ABI void resize(size_type __n);
755 _LIBCPP_HIDE_FROM_ABI void resize(size_type __n, const value_type& __v);
756 _LIBCPP_HIDE_FROM_ABI void shrink_to_fit() _NOEXCEPT;
757 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT { return size() == 0; }
760 _LIBCPP_HIDE_FROM_ABI reference operator[](size_type __i) _NOEXCEPT;
761 _LIBCPP_HIDE_FROM_ABI const_reference operator[](size_type __i) const _NOEXCEPT;
762 _LIBCPP_HIDE_FROM_ABI reference at(size_type __i);
763 _LIBCPP_HIDE_FROM_ABI const_reference at(size_type __i) const;
764 _LIBCPP_HIDE_FROM_ABI reference front() _NOEXCEPT;
765 _LIBCPP_HIDE_FROM_ABI const_reference front() const _NOEXCEPT;
766 _LIBCPP_HIDE_FROM_ABI reference back() _NOEXCEPT;
767 _LIBCPP_HIDE_FROM_ABI const_reference back() const _NOEXCEPT;
769 // 23.2.2.3 modifiers:
770 _LIBCPP_HIDE_FROM_ABI void push_front(const value_type& __v);
771 _LIBCPP_HIDE_FROM_ABI void push_back(const value_type& __v);
772 #ifndef _LIBCPP_CXX03_LANG
773 # if _LIBCPP_STD_VER >= 17
774 template <class... _Args>
775 _LIBCPP_HIDE_FROM_ABI reference emplace_front(_Args&&... __args);
776 template <class... _Args>
777 _LIBCPP_HIDE_FROM_ABI reference emplace_back(_Args&&... __args);
779 template <class... _Args>
780 _LIBCPP_HIDE_FROM_ABI void emplace_front(_Args&&... __args);
781 template <class... _Args>
782 _LIBCPP_HIDE_FROM_ABI void emplace_back(_Args&&... __args);
784 template <class... _Args>
785 _LIBCPP_HIDE_FROM_ABI iterator emplace(const_iterator __p, _Args&&... __args);
787 _LIBCPP_HIDE_FROM_ABI void push_front(value_type&& __v);
788 _LIBCPP_HIDE_FROM_ABI void push_back(value_type&& __v);
790 # if _LIBCPP_STD_VER >= 23
791 template <_ContainerCompatibleRange<_Tp> _Range>
792 _LIBCPP_HIDE_FROM_ABI void prepend_range(_Range&& __range) {
793 insert_range(begin(), std::forward<_Range>(__range));
796 template <_ContainerCompatibleRange<_Tp> _Range>
797 _LIBCPP_HIDE_FROM_ABI void append_range(_Range&& __range) {
798 insert_range(end(), std::forward<_Range>(__range));
802 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, value_type&& __v);
804 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, initializer_list<value_type> __il) {
805 return insert(__p, __il.begin(), __il.end());
807 #endif // _LIBCPP_CXX03_LANG
808 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, const value_type& __v);
809 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, size_type __n, const value_type& __v);
810 template <class _InputIter, __enable_if_t<__has_exactly_input_iterator_category<_InputIter>::value, int> = 0>
811 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, _InputIter __f, _InputIter __l);
812 template <class _ForwardIterator,
813 __enable_if_t<__has_exactly_forward_iterator_category<_ForwardIterator>::value, int> = 0>
814 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l);
815 template <class _BiIter, __enable_if_t<__has_bidirectional_iterator_category<_BiIter>::value, int> = 0>
816 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, _BiIter __f, _BiIter __l);
818 #if _LIBCPP_STD_VER >= 23
819 template <_ContainerCompatibleRange<_Tp> _Range>
820 _LIBCPP_HIDE_FROM_ABI iterator insert_range(const_iterator __position, _Range&& __range) {
821 if constexpr (ranges::bidirectional_range<_Range>) {
822 auto __n = static_cast<size_type>(ranges::distance(__range));
823 return __insert_bidirectional(__position, ranges::begin(__range), ranges::end(__range), __n);
825 } else if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
826 auto __n = static_cast<size_type>(ranges::distance(__range));
827 return __insert_with_size(__position, ranges::begin(__range), __n);
830 return __insert_with_sentinel(__position, ranges::begin(__range), ranges::end(__range));
835 _LIBCPP_HIDE_FROM_ABI void pop_front();
836 _LIBCPP_HIDE_FROM_ABI void pop_back();
837 _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __p);
838 _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __f, const_iterator __l);
840 _LIBCPP_HIDE_FROM_ABI void swap(deque& __c)
841 #if _LIBCPP_STD_VER >= 14
844 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<allocator_type>);
846 _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT;
848 _LIBCPP_HIDE_FROM_ABI bool __invariants() const {
849 if (!__map_.__invariants())
851 if (__map_.size() >= size_type(-1) / __block_size)
853 for (__map_const_iterator __i = __map_.begin(), __e = __map_.end(); __i != __e; ++__i)
856 if (__map_.size() != 0) {
857 if (size() >= __map_.size() * __block_size)
859 if (__start_ >= __map_.size() * __block_size - size())
870 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(deque& __c)
871 _NOEXCEPT_(!__alloc_traits::propagate_on_container_move_assignment::value ||
872 is_nothrow_move_assignable<allocator_type>::value) {
873 __move_assign_alloc(__c, integral_constant<bool, __alloc_traits::propagate_on_container_move_assignment::value>());
876 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(deque& __c, true_type)
877 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) {
878 __alloc() = std::move(__c.__alloc());
881 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(deque&, false_type) _NOEXCEPT {}
883 _LIBCPP_HIDE_FROM_ABI void __move_assign(deque& __c)
884 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value&&
885 is_nothrow_move_assignable<allocator_type>::value) {
886 __map_ = std::move(__c.__map_);
887 __start_ = __c.__start_;
888 __size() = __c.size();
889 __move_assign_alloc(__c);
890 __c.__start_ = __c.__size() = 0;
893 _LIBCPP_HIDE_FROM_ABI static size_type __recommend_blocks(size_type __n) {
894 return __n / __block_size + (__n % __block_size != 0);
896 _LIBCPP_HIDE_FROM_ABI size_type __capacity() const {
897 return __map_.size() == 0 ? 0 : __map_.size() * __block_size - 1;
899 _LIBCPP_HIDE_FROM_ABI size_type __block_count() const { return __map_.size(); }
901 _LIBCPP_HIDE_FROM_ABI size_type __front_spare() const { return __start_; }
902 _LIBCPP_HIDE_FROM_ABI size_type __front_spare_blocks() const { return __front_spare() / __block_size; }
903 _LIBCPP_HIDE_FROM_ABI size_type __back_spare() const { return __capacity() - (__start_ + size()); }
904 _LIBCPP_HIDE_FROM_ABI size_type __back_spare_blocks() const { return __back_spare() / __block_size; }
907 enum __asan_annotation_type { __asan_unposion, __asan_poison };
909 enum __asan_annotation_place {
914 _LIBCPP_HIDE_FROM_ABI void __annotate_from_to(
917 __asan_annotation_type __annotation_type,
918 __asan_annotation_place __place) const _NOEXCEPT {
921 (void)__annotation_type;
924 // __beg - index of the first item to annotate
925 // __end - index behind the last item to annotate (so last item + 1)
926 // __annotation_type - __asan_unposion or __asan_poison
927 // __place - __asan_front_moved or __asan_back_moved
928 // Note: All indexes in __map_
931 // __annotations_beg_map - first chunk which annotations we want to modify
932 // __annotations_end_map - last chunk which annotations we want to modify
933 // NOTE: if __end % __block_size == 0, __annotations_end_map points at the next block, which may not exist
934 __map_const_iterator __annotations_beg_map = __map_.begin() + __beg / __block_size;
935 __map_const_iterator __annotations_end_map = __map_.begin() + __end / __block_size;
937 bool const __poisoning = __annotation_type == __asan_poison;
938 // __old_c_beg_index - index of the first element in old container
939 // __old_c_end_index - index of the end of old container (last + 1)
940 // Note: may be outside the area we are annotating
941 size_t __old_c_beg_index = (__poisoning && __place == __asan_front_moved) ? __beg : __start_;
942 size_t __old_c_end_index = (__poisoning && __place == __asan_back_moved) ? __end : __start_ + size();
943 bool const __front = __place == __asan_front_moved;
945 if (__poisoning && empty()) {
946 // Special case: we shouldn't trust __start_
947 __old_c_beg_index = __beg;
948 __old_c_end_index = __end;
950 // __old_c_beg_map - memory block (chunk) with first element
951 // __old_c_end_map - memory block (chunk) with end of old container
952 // Note: if __old_c_end_index % __block_size == 0, __old_c_end_map points at the next block,
953 // which may not exist
954 __map_const_iterator __old_c_beg_map = __map_.begin() + __old_c_beg_index / __block_size;
955 __map_const_iterator __old_c_end_map = __map_.begin() + __old_c_end_index / __block_size;
957 // One edge (front/end) of the container was moved and one was not modified.
958 // __new_edge_index - index of new edge
959 // __new_edge_map - memory block (chunk) with new edge, it always equals to
960 // __annotations_beg_map or __annotations_end_map
961 // __old_edge_map - memory block (chunk) with old edge, it always equals to
962 // __old_c_beg_map or __old_c_end_map
963 size_t __new_edge_index = (__poisoning ^ __front) ? __beg : __end;
964 __map_const_iterator __new_edge_map = __map_.begin() + __new_edge_index / __block_size;
965 __map_const_iterator __old_edge_map = __front ? __old_c_end_map : __old_c_beg_map;
967 // We iterate over map pointers (chunks) and fully poison all memory blocks between the first and the last.
968 // First and last chunk may be partially poisoned.
969 // __annotate_end_map may point at not existing chunk, therefore we have to have a check for it.
970 for (__map_const_iterator __map_it = __annotations_beg_map; __map_it <= __annotations_end_map; ++__map_it) {
971 if (__map_it == __annotations_end_map && __end % __block_size == 0)
972 // Chunk may not exist, but nothing to do here anyway
975 // The beginning and the end of the current memory block
976 const void* __mem_beg = std::__to_address(*__map_it);
977 const void* __mem_end = std::__to_address(*__map_it + __block_size);
979 // The beginning of memory-in-use in the memory block before container modification
980 const void* __old_beg =
981 (__map_it == __old_c_beg_map) ? std::__to_address(*__map_it + (__old_c_beg_index % __block_size)) : __mem_beg;
983 // The end of memory-in-use in the memory block before container modification
984 const void* __old_end;
985 if (__map_it < __old_c_beg_map || __map_it > __old_c_end_map || (!__poisoning && empty()))
986 __old_end = __old_beg;
988 __old_end = (__map_it == __old_c_end_map)
989 ? std::__to_address(*__map_it + (__old_c_end_index % __block_size))
992 // New edge of the container in current memory block
993 // If the edge is in a different chunk it points on corresponding end of the memory block
994 const void* __new_edge;
995 if (__map_it == __new_edge_map)
996 __new_edge = std::__to_address(*__map_it + (__new_edge_index % __block_size));
998 __new_edge = (__poisoning ^ __front) ? __mem_beg : __mem_end;
1000 // Not modified edge of the container
1001 // If the edge is in a different chunk it points on corresponding end of the memory block
1002 const void* __old_edge;
1003 if (__map_it == __old_edge_map)
1004 __old_edge = __front ? __old_end : __old_beg;
1006 __old_edge = __front ? __mem_end : __mem_beg;
1008 // __new_beg - the beginning of memory-in-use in the memory block after container modification
1009 // __new_end - the end of memory-in-use in the memory block after container modification
1010 const void* __new_beg = __front ? __new_edge : __old_edge;
1011 const void* __new_end = __front ? __old_edge : __new_edge;
1013 std::__annotate_double_ended_contiguous_container<_Allocator>(
1014 __mem_beg, __mem_end, __old_beg, __old_end, __new_beg, __new_end);
1016 #endif // _LIBCPP_HAS_ASAN
1019 _LIBCPP_HIDE_FROM_ABI void __annotate_new(size_type __current_size) const _NOEXCEPT {
1020 (void)__current_size;
1021 #if _LIBCPP_HAS_ASAN
1022 if (__current_size == 0)
1023 __annotate_from_to(0, __map_.size() * __block_size, __asan_poison, __asan_back_moved);
1025 __annotate_from_to(0, __start_, __asan_poison, __asan_front_moved);
1026 __annotate_from_to(__start_ + __current_size, __map_.size() * __block_size, __asan_poison, __asan_back_moved);
1028 #endif // _LIBCPP_HAS_ASAN
1031 _LIBCPP_HIDE_FROM_ABI void __annotate_delete() const _NOEXCEPT {
1032 #if _LIBCPP_HAS_ASAN
1034 for (size_t __i = 0; __i < __map_.size(); ++__i) {
1035 __annotate_whole_block(__i, __asan_unposion);
1038 __annotate_from_to(0, __start_, __asan_unposion, __asan_front_moved);
1039 __annotate_from_to(__start_ + size(), __map_.size() * __block_size, __asan_unposion, __asan_back_moved);
1041 #endif // _LIBCPP_HAS_ASAN
1044 _LIBCPP_HIDE_FROM_ABI void __annotate_increase_front(size_type __n) const _NOEXCEPT {
1046 #if _LIBCPP_HAS_ASAN
1047 __annotate_from_to(__start_ - __n, __start_, __asan_unposion, __asan_front_moved);
1051 _LIBCPP_HIDE_FROM_ABI void __annotate_increase_back(size_type __n) const _NOEXCEPT {
1053 #if _LIBCPP_HAS_ASAN
1054 __annotate_from_to(__start_ + size(), __start_ + size() + __n, __asan_unposion, __asan_back_moved);
1058 _LIBCPP_HIDE_FROM_ABI void __annotate_shrink_front(size_type __old_size, size_type __old_start) const _NOEXCEPT {
1061 #if _LIBCPP_HAS_ASAN
1062 __annotate_from_to(__old_start, __old_start + (__old_size - size()), __asan_poison, __asan_front_moved);
1066 _LIBCPP_HIDE_FROM_ABI void __annotate_shrink_back(size_type __old_size, size_type __old_start) const _NOEXCEPT {
1069 #if _LIBCPP_HAS_ASAN
1070 __annotate_from_to(__old_start + size(), __old_start + __old_size, __asan_poison, __asan_back_moved);
1074 _LIBCPP_HIDE_FROM_ABI void __annotate_poison_block(const void* __beginning, const void* __end) const _NOEXCEPT {
1075 std::__annotate_double_ended_contiguous_container<_Allocator>(__beginning, __end, __beginning, __end, __end, __end);
1078 _LIBCPP_HIDE_FROM_ABI void
1079 __annotate_whole_block(size_t __block_index, __asan_annotation_type __annotation_type) const _NOEXCEPT {
1080 (void)__block_index;
1081 (void)__annotation_type;
1082 #if _LIBCPP_HAS_ASAN
1083 __map_const_iterator __block_it = __map_.begin() + __block_index;
1084 const void* __block_start = std::__to_address(*__block_it);
1085 const void* __block_end = std::__to_address(*__block_it + __block_size);
1087 if (__annotation_type == __asan_poison)
1088 __annotate_poison_block(__block_start, __block_end);
1090 std::__annotate_double_ended_contiguous_container<_Allocator>(
1091 __block_start, __block_end, __block_start, __block_start, __block_start, __block_end);
1095 #if _LIBCPP_HAS_ASAN
1098 _LIBCPP_HIDE_FROM_ABI bool __verify_asan_annotations() const _NOEXCEPT {
1099 // This function tests deque object annotations.
1101 for (__map_const_iterator __it = __map_.begin(); __it != __map_.end(); ++__it) {
1102 if (!__sanitizer_verify_double_ended_contiguous_container(
1103 std::__to_address(*__it),
1104 std::__to_address(*__it),
1105 std::__to_address(*__it),
1106 std::__to_address(*__it + __block_size)))
1113 size_type __end = __start_ + size();
1114 __map_const_iterator __first_mp = __map_.begin() + __start_ / __block_size;
1115 __map_const_iterator __last_mp = __map_.begin() + (__end - 1) / __block_size;
1117 // Pointers to first and after last elements
1118 // Those can be in different deque blocks
1119 const void* __p_beg = std::__to_address(*__first_mp + (__start_ % __block_size));
1120 const void* __p_end =
1121 std::__to_address(*__last_mp + ((__end % __block_size == 0) ? __block_size : __end % __block_size));
1123 for (__map_const_iterator __it = __map_.begin(); __it != __map_.end(); ++__it) {
1124 // Go over all blocks, find the place we are in and verify its annotations
1125 // Note that __p_end points *behind* the last item.
1127 // - blocks before the first block with container elements
1128 // - first block with items
1129 // - last block with items
1130 // - blocks after last block with ciontainer elements
1132 // Is the block before or after deque blocks that contain elements?
1133 if (__it < __first_mp || __it > __last_mp) {
1134 if (!__sanitizer_verify_double_ended_contiguous_container(
1135 std::__to_address(*__it),
1136 std::__to_address(*__it),
1137 std::__to_address(*__it),
1138 std::__to_address(*__it + __block_size)))
1141 const void* __containers_buffer_beg = (__it == __first_mp) ? __p_beg : (const void*)std::__to_address(*__it);
1142 const void* __containers_buffer_end =
1143 (__it == __last_mp) ? __p_end : (const void*)std::__to_address(*__it + __block_size);
1144 if (!__sanitizer_verify_double_ended_contiguous_container(
1145 std::__to_address(*__it),
1146 __containers_buffer_beg,
1147 __containers_buffer_end,
1148 std::__to_address(*__it + __block_size))) {
1157 #endif // _LIBCPP_HAS_ASAN
1158 _LIBCPP_HIDE_FROM_ABI bool __maybe_remove_front_spare(bool __keep_one = true) {
1159 if (__front_spare_blocks() >= 2 || (!__keep_one && __front_spare_blocks())) {
1160 __annotate_whole_block(0, __asan_unposion);
1161 __alloc_traits::deallocate(__alloc(), __map_.front(), __block_size);
1163 __start_ -= __block_size;
1169 _LIBCPP_HIDE_FROM_ABI bool __maybe_remove_back_spare(bool __keep_one = true) {
1170 if (__back_spare_blocks() >= 2 || (!__keep_one && __back_spare_blocks())) {
1171 __annotate_whole_block(__map_.size() - 1, __asan_unposion);
1172 __alloc_traits::deallocate(__alloc(), __map_.back(), __block_size);
1179 template <class _Iterator, class _Sentinel>
1180 _LIBCPP_HIDE_FROM_ABI void __assign_with_sentinel(_Iterator __f, _Sentinel __l);
1182 template <class _RandomAccessIterator>
1183 _LIBCPP_HIDE_FROM_ABI void __assign_with_size_random_access(_RandomAccessIterator __f, difference_type __n);
1184 template <class _Iterator>
1185 _LIBCPP_HIDE_FROM_ABI void __assign_with_size(_Iterator __f, difference_type __n);
1187 template <class _Iterator, class _Sentinel>
1188 _LIBCPP_HIDE_FROM_ABI iterator __insert_with_sentinel(const_iterator __p, _Iterator __f, _Sentinel __l);
1190 template <class _Iterator>
1191 _LIBCPP_HIDE_FROM_ABI iterator __insert_with_size(const_iterator __p, _Iterator __f, size_type __n);
1193 template <class _BiIter, class _Sentinel>
1194 _LIBCPP_HIDE_FROM_ABI iterator
1195 __insert_bidirectional(const_iterator __p, _BiIter __f, _Sentinel __sent, size_type __n);
1196 template <class _BiIter>
1197 _LIBCPP_HIDE_FROM_ABI iterator __insert_bidirectional(const_iterator __p, _BiIter __f, _BiIter __l, size_type __n);
1199 template <class _InpIter, __enable_if_t<__has_exactly_input_iterator_category<_InpIter>::value, int> = 0>
1200 _LIBCPP_HIDE_FROM_ABI void __append(_InpIter __f, _InpIter __l);
1201 template <class _ForIter, __enable_if_t<__has_forward_iterator_category<_ForIter>::value, int> = 0>
1202 _LIBCPP_HIDE_FROM_ABI void __append(_ForIter __f, _ForIter __l);
1204 template <class _InputIterator>
1205 _LIBCPP_HIDE_FROM_ABI void __append_with_size(_InputIterator __from, size_type __n);
1206 template <class _InputIterator, class _Sentinel>
1207 _LIBCPP_HIDE_FROM_ABI void __append_with_sentinel(_InputIterator __f, _Sentinel __l);
1209 _LIBCPP_HIDE_FROM_ABI void __append(size_type __n);
1210 _LIBCPP_HIDE_FROM_ABI void __append(size_type __n, const value_type& __v);
1211 _LIBCPP_HIDE_FROM_ABI void __erase_to_end(const_iterator __f);
1212 _LIBCPP_HIDE_FROM_ABI void __add_front_capacity();
1213 _LIBCPP_HIDE_FROM_ABI void __add_front_capacity(size_type __n);
1214 _LIBCPP_HIDE_FROM_ABI void __add_back_capacity();
1215 _LIBCPP_HIDE_FROM_ABI void __add_back_capacity(size_type __n);
1216 _LIBCPP_HIDE_FROM_ABI iterator __move_and_check(iterator __f, iterator __l, iterator __r, const_pointer& __vt);
1217 _LIBCPP_HIDE_FROM_ABI iterator
1218 __move_backward_and_check(iterator __f, iterator __l, iterator __r, const_pointer& __vt);
1219 _LIBCPP_HIDE_FROM_ABI void __move_construct_and_check(iterator __f, iterator __l, iterator __r, const_pointer& __vt);
1220 _LIBCPP_HIDE_FROM_ABI void
1221 __move_construct_backward_and_check(iterator __f, iterator __l, iterator __r, const_pointer& __vt);
1223 _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const deque& __c) {
1224 __copy_assign_alloc(__c, integral_constant<bool, __alloc_traits::propagate_on_container_copy_assignment::value>());
1227 _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const deque& __c, true_type) {
1228 if (__alloc() != __c.__alloc()) {
1232 __alloc() = __c.__alloc();
1233 __map_.__alloc_ = __c.__map_.__alloc_;
1236 _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const deque&, false_type) {}
1238 _LIBCPP_HIDE_FROM_ABI void __move_assign(deque& __c, true_type)
1239 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
1240 _LIBCPP_HIDE_FROM_ABI void __move_assign(deque& __c, false_type);
1243 template <class _Tp, class _Alloc>
1244 _LIBCPP_CONSTEXPR const typename allocator_traits<_Alloc>::difference_type deque<_Tp, _Alloc>::__block_size =
1245 __deque_block_size<value_type, difference_type>::value;
1247 #if _LIBCPP_STD_VER >= 17
1248 template <class _InputIterator,
1249 class _Alloc = allocator<__iter_value_type<_InputIterator>>,
1250 class = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
1251 class = enable_if_t<__is_allocator<_Alloc>::value> >
1252 deque(_InputIterator, _InputIterator) -> deque<__iter_value_type<_InputIterator>, _Alloc>;
1254 template <class _InputIterator,
1256 class = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
1257 class = enable_if_t<__is_allocator<_Alloc>::value> >
1258 deque(_InputIterator, _InputIterator, _Alloc) -> deque<__iter_value_type<_InputIterator>, _Alloc>;
1261 #if _LIBCPP_STD_VER >= 23
1262 template <ranges::input_range _Range,
1263 class _Alloc = allocator<ranges::range_value_t<_Range>>,
1264 class = enable_if_t<__is_allocator<_Alloc>::value> >
1265 deque(from_range_t, _Range&&, _Alloc = _Alloc()) -> deque<ranges::range_value_t<_Range>, _Alloc>;
1268 template <class _Tp, class _Allocator>
1269 deque<_Tp, _Allocator>::deque(size_type __n) : __start_(0), __size_(0) {
1275 #if _LIBCPP_STD_VER >= 14
1276 template <class _Tp, class _Allocator>
1277 deque<_Tp, _Allocator>::deque(size_type __n, const _Allocator& __a)
1278 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0), __alloc_(__a) {
1285 template <class _Tp, class _Allocator>
1286 deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v) : __start_(0), __size_(0) {
1292 template <class _Tp, class _Allocator>
1293 template <class _InputIter, __enable_if_t<__has_input_iterator_category<_InputIter>::value, int> >
1294 deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l) : __start_(0), __size_(0) {
1299 template <class _Tp, class _Allocator>
1300 template <class _InputIter, __enable_if_t<__has_input_iterator_category<_InputIter>::value, int> >
1301 deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l, const allocator_type& __a)
1302 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0), __alloc_(__a) {
1307 template <class _Tp, class _Allocator>
1308 deque<_Tp, _Allocator>::deque(const deque& __c)
1309 : __map_(__pointer_allocator(__alloc_traits::select_on_container_copy_construction(__c.__alloc()))),
1312 __alloc_(__map_.__alloc_) {
1314 __append(__c.begin(), __c.end());
1317 template <class _Tp, class _Allocator>
1318 deque<_Tp, _Allocator>::deque(const deque& __c, const __type_identity_t<allocator_type>& __a)
1319 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0), __alloc_(__a) {
1321 __append(__c.begin(), __c.end());
1324 template <class _Tp, class _Allocator>
1325 deque<_Tp, _Allocator>& deque<_Tp, _Allocator>::operator=(const deque& __c) {
1326 if (this != std::addressof(__c)) {
1327 __copy_assign_alloc(__c);
1328 assign(__c.begin(), __c.end());
1333 #ifndef _LIBCPP_CXX03_LANG
1335 template <class _Tp, class _Allocator>
1336 deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il) : __start_(0), __size_(0) {
1338 __append(__il.begin(), __il.end());
1341 template <class _Tp, class _Allocator>
1342 deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il, const allocator_type& __a)
1343 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0), __alloc_(__a) {
1345 __append(__il.begin(), __il.end());
1348 template <class _Tp, class _Allocator>
1349 inline deque<_Tp, _Allocator>::deque(deque&& __c) noexcept(is_nothrow_move_constructible<allocator_type>::value)
1350 : __map_(std::move(__c.__map_)),
1351 __start_(std::move(__c.__start_)),
1352 __size_(std::move(__c.__size_)),
1353 __alloc_(std::move(__c.__alloc_)) {
1358 template <class _Tp, class _Allocator>
1359 inline deque<_Tp, _Allocator>::deque(deque&& __c, const __type_identity_t<allocator_type>& __a)
1360 : __map_(std::move(__c.__map_), __pointer_allocator(__a)),
1361 __start_(std::move(__c.__start_)),
1362 __size_(std::move(__c.__size_)),
1364 if (__a == __c.__alloc()) {
1371 typedef move_iterator<iterator> _Ip;
1372 assign(_Ip(__c.begin()), _Ip(__c.end()));
1376 template <class _Tp, class _Allocator>
1377 inline deque<_Tp, _Allocator>& deque<_Tp, _Allocator>::operator=(deque&& __c) noexcept(
1378 __alloc_traits::propagate_on_container_move_assignment::value &&
1379 is_nothrow_move_assignable<allocator_type>::value) {
1380 __move_assign(__c, integral_constant<bool, __alloc_traits::propagate_on_container_move_assignment::value>());
1384 template <class _Tp, class _Allocator>
1385 void deque<_Tp, _Allocator>::__move_assign(deque& __c, false_type) {
1386 if (__alloc() != __c.__alloc()) {
1387 typedef move_iterator<iterator> _Ip;
1388 assign(_Ip(__c.begin()), _Ip(__c.end()));
1390 __move_assign(__c, true_type());
1393 template <class _Tp, class _Allocator>
1394 void deque<_Tp, _Allocator>::__move_assign(deque& __c,
1395 true_type) noexcept(is_nothrow_move_assignable<allocator_type>::value) {
1401 #endif // _LIBCPP_CXX03_LANG
1403 template <class _Tp, class _Allocator>
1404 template <class _InputIter,
1405 __enable_if_t<__has_input_iterator_category<_InputIter>::value &&
1406 !__has_random_access_iterator_category<_InputIter>::value,
1408 void deque<_Tp, _Allocator>::assign(_InputIter __f, _InputIter __l) {
1409 __assign_with_sentinel(__f, __l);
1412 template <class _Tp, class _Allocator>
1413 template <class _Iterator, class _Sentinel>
1414 _LIBCPP_HIDE_FROM_ABI void deque<_Tp, _Allocator>::__assign_with_sentinel(_Iterator __f, _Sentinel __l) {
1415 iterator __i = begin();
1416 iterator __e = end();
1417 for (; __f != __l && __i != __e; ++__f, (void)++__i)
1420 __append_with_sentinel(std::move(__f), std::move(__l));
1422 __erase_to_end(__i);
1425 template <class _Tp, class _Allocator>
1426 template <class _RAIter, __enable_if_t<__has_random_access_iterator_category<_RAIter>::value, int> >
1427 void deque<_Tp, _Allocator>::assign(_RAIter __f, _RAIter __l) {
1428 __assign_with_size_random_access(__f, __l - __f);
1431 template <class _Tp, class _Allocator>
1432 template <class _RandomAccessIterator>
1433 _LIBCPP_HIDE_FROM_ABI void
1434 deque<_Tp, _Allocator>::__assign_with_size_random_access(_RandomAccessIterator __f, difference_type __n) {
1435 if (static_cast<size_type>(__n) > size()) {
1436 auto __l = __f + size();
1437 std::copy(__f, __l, begin());
1438 __append_with_size(__l, __n - size());
1440 __erase_to_end(std::copy_n(__f, __n, begin()));
1443 template <class _Tp, class _Allocator>
1444 template <class _Iterator>
1445 _LIBCPP_HIDE_FROM_ABI void deque<_Tp, _Allocator>::__assign_with_size(_Iterator __f, difference_type __n) {
1446 if (static_cast<size_type>(__n) > size()) {
1447 auto __added_size = __n - size();
1450 for (auto __count = size(); __count != 0; --__count) {
1454 __append_with_size(__f, __added_size);
1457 __erase_to_end(std::copy_n(__f, __n, begin()));
1461 template <class _Tp, class _Allocator>
1462 void deque<_Tp, _Allocator>::assign(size_type __n, const value_type& __v) {
1464 std::fill_n(begin(), size(), __v);
1468 __erase_to_end(std::fill_n(begin(), __n, __v));
1471 template <class _Tp, class _Allocator>
1472 inline _Allocator deque<_Tp, _Allocator>::get_allocator() const _NOEXCEPT {
1476 template <class _Tp, class _Allocator>
1477 void deque<_Tp, _Allocator>::resize(size_type __n) {
1479 __append(__n - size());
1480 else if (__n < size())
1481 __erase_to_end(begin() + __n);
1484 template <class _Tp, class _Allocator>
1485 void deque<_Tp, _Allocator>::resize(size_type __n, const value_type& __v) {
1487 __append(__n - size(), __v);
1488 else if (__n < size())
1489 __erase_to_end(begin() + __n);
1492 template <class _Tp, class _Allocator>
1493 void deque<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT {
1494 allocator_type& __a = __alloc();
1496 __annotate_delete();
1497 while (__map_.size() > 0) {
1498 __alloc_traits::deallocate(__a, __map_.back(), __block_size);
1503 __maybe_remove_front_spare(/*__keep_one=*/false);
1504 __maybe_remove_back_spare(/*__keep_one=*/false);
1506 __map_.shrink_to_fit();
1509 template <class _Tp, class _Allocator>
1510 inline typename deque<_Tp, _Allocator>::reference deque<_Tp, _Allocator>::operator[](size_type __i) _NOEXCEPT {
1511 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__i < size(), "deque::operator[] index out of bounds");
1512 size_type __p = __start_ + __i;
1513 return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
1516 template <class _Tp, class _Allocator>
1517 inline typename deque<_Tp, _Allocator>::const_reference
1518 deque<_Tp, _Allocator>::operator[](size_type __i) const _NOEXCEPT {
1519 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__i < size(), "deque::operator[] index out of bounds");
1520 size_type __p = __start_ + __i;
1521 return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
1524 template <class _Tp, class _Allocator>
1525 inline typename deque<_Tp, _Allocator>::reference deque<_Tp, _Allocator>::at(size_type __i) {
1527 std::__throw_out_of_range("deque");
1528 size_type __p = __start_ + __i;
1529 return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
1532 template <class _Tp, class _Allocator>
1533 inline typename deque<_Tp, _Allocator>::const_reference deque<_Tp, _Allocator>::at(size_type __i) const {
1535 std::__throw_out_of_range("deque");
1536 size_type __p = __start_ + __i;
1537 return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
1540 template <class _Tp, class _Allocator>
1541 inline typename deque<_Tp, _Allocator>::reference deque<_Tp, _Allocator>::front() _NOEXCEPT {
1542 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "deque::front called on an empty deque");
1543 return *(*(__map_.begin() + __start_ / __block_size) + __start_ % __block_size);
1546 template <class _Tp, class _Allocator>
1547 inline typename deque<_Tp, _Allocator>::const_reference deque<_Tp, _Allocator>::front() const _NOEXCEPT {
1548 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "deque::front called on an empty deque");
1549 return *(*(__map_.begin() + __start_ / __block_size) + __start_ % __block_size);
1552 template <class _Tp, class _Allocator>
1553 inline typename deque<_Tp, _Allocator>::reference deque<_Tp, _Allocator>::back() _NOEXCEPT {
1554 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "deque::back called on an empty deque");
1555 size_type __p = size() + __start_ - 1;
1556 return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
1559 template <class _Tp, class _Allocator>
1560 inline typename deque<_Tp, _Allocator>::const_reference deque<_Tp, _Allocator>::back() const _NOEXCEPT {
1561 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "deque::back called on an empty deque");
1562 size_type __p = size() + __start_ - 1;
1563 return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
1566 template <class _Tp, class _Allocator>
1567 void deque<_Tp, _Allocator>::push_back(const value_type& __v) {
1568 allocator_type& __a = __alloc();
1569 if (__back_spare() == 0)
1570 __add_back_capacity();
1571 // __back_spare() >= 1
1572 __annotate_increase_back(1);
1573 __alloc_traits::construct(__a, std::addressof(*end()), __v);
1577 template <class _Tp, class _Allocator>
1578 void deque<_Tp, _Allocator>::push_front(const value_type& __v) {
1579 allocator_type& __a = __alloc();
1580 if (__front_spare() == 0)
1581 __add_front_capacity();
1582 // __front_spare() >= 1
1583 __annotate_increase_front(1);
1584 __alloc_traits::construct(__a, std::addressof(*--begin()), __v);
1589 #ifndef _LIBCPP_CXX03_LANG
1590 template <class _Tp, class _Allocator>
1591 void deque<_Tp, _Allocator>::push_back(value_type&& __v) {
1592 allocator_type& __a = __alloc();
1593 if (__back_spare() == 0)
1594 __add_back_capacity();
1595 // __back_spare() >= 1
1596 __annotate_increase_back(1);
1597 __alloc_traits::construct(__a, std::addressof(*end()), std::move(__v));
1601 template <class _Tp, class _Allocator>
1602 template <class... _Args>
1603 # if _LIBCPP_STD_VER >= 17
1604 typename deque<_Tp, _Allocator>::reference
1608 deque<_Tp, _Allocator>::emplace_back(_Args&&... __args) {
1609 allocator_type& __a = __alloc();
1610 if (__back_spare() == 0)
1611 __add_back_capacity();
1612 // __back_spare() >= 1
1613 __annotate_increase_back(1);
1614 __alloc_traits::construct(__a, std::addressof(*end()), std::forward<_Args>(__args)...);
1616 # if _LIBCPP_STD_VER >= 17
1621 template <class _Tp, class _Allocator>
1622 void deque<_Tp, _Allocator>::push_front(value_type&& __v) {
1623 allocator_type& __a = __alloc();
1624 if (__front_spare() == 0)
1625 __add_front_capacity();
1626 // __front_spare() >= 1
1627 __annotate_increase_front(1);
1628 __alloc_traits::construct(__a, std::addressof(*--begin()), std::move(__v));
1633 template <class _Tp, class _Allocator>
1634 template <class... _Args>
1635 # if _LIBCPP_STD_VER >= 17
1636 typename deque<_Tp, _Allocator>::reference
1640 deque<_Tp, _Allocator>::emplace_front(_Args&&... __args) {
1641 allocator_type& __a = __alloc();
1642 if (__front_spare() == 0)
1643 __add_front_capacity();
1644 // __front_spare() >= 1
1645 __annotate_increase_front(1);
1646 __alloc_traits::construct(__a, std::addressof(*--begin()), std::forward<_Args>(__args)...);
1649 # if _LIBCPP_STD_VER >= 17
1654 template <class _Tp, class _Allocator>
1655 typename deque<_Tp, _Allocator>::iterator deque<_Tp, _Allocator>::insert(const_iterator __p, value_type&& __v) {
1656 size_type __pos = __p - begin();
1657 size_type __to_end = size() - __pos;
1658 allocator_type& __a = __alloc();
1659 if (__pos < __to_end) { // insert by shifting things backward
1660 if (__front_spare() == 0)
1661 __add_front_capacity();
1662 // __front_spare() >= 1
1663 __annotate_increase_front(1);
1665 __alloc_traits::construct(__a, std::addressof(*--begin()), std::move(__v));
1669 iterator __b = begin();
1670 iterator __bm1 = std::prev(__b);
1671 __alloc_traits::construct(__a, std::addressof(*__bm1), std::move(*__b));
1675 __b = std::move(std::next(__b), __b + __pos, __b);
1676 *__b = std::move(__v);
1678 } else { // insert by shifting things forward
1679 if (__back_spare() == 0)
1680 __add_back_capacity();
1681 // __back_capacity >= 1
1682 __annotate_increase_back(1);
1683 size_type __de = size() - __pos;
1685 __alloc_traits::construct(__a, std::addressof(*end()), std::move(__v));
1688 iterator __e = end();
1689 iterator __em1 = std::prev(__e);
1690 __alloc_traits::construct(__a, std::addressof(*__e), std::move(*__em1));
1693 __e = std::move_backward(__e - __de, __em1, __e);
1694 *--__e = std::move(__v);
1697 return begin() + __pos;
1700 template <class _Tp, class _Allocator>
1701 template <class... _Args>
1702 typename deque<_Tp, _Allocator>::iterator deque<_Tp, _Allocator>::emplace(const_iterator __p, _Args&&... __args) {
1703 size_type __pos = __p - begin();
1704 size_type __to_end = size() - __pos;
1705 allocator_type& __a = __alloc();
1706 if (__pos < __to_end) { // insert by shifting things backward
1707 if (__front_spare() == 0)
1708 __add_front_capacity();
1709 // __front_spare() >= 1
1710 __annotate_increase_front(1);
1712 __alloc_traits::construct(__a, std::addressof(*--begin()), std::forward<_Args>(__args)...);
1716 __temp_value<value_type, _Allocator> __tmp(__alloc(), std::forward<_Args>(__args)...);
1717 iterator __b = begin();
1718 iterator __bm1 = std::prev(__b);
1719 __alloc_traits::construct(__a, std::addressof(*__bm1), std::move(*__b));
1723 __b = std::move(std::next(__b), __b + __pos, __b);
1724 *__b = std::move(__tmp.get());
1726 } else { // insert by shifting things forward
1727 if (__back_spare() == 0)
1728 __add_back_capacity();
1729 // __back_capacity >= 1
1730 __annotate_increase_back(1);
1731 size_type __de = size() - __pos;
1733 __alloc_traits::construct(__a, std::addressof(*end()), std::forward<_Args>(__args)...);
1736 __temp_value<value_type, _Allocator> __tmp(__alloc(), std::forward<_Args>(__args)...);
1737 iterator __e = end();
1738 iterator __em1 = std::prev(__e);
1739 __alloc_traits::construct(__a, std::addressof(*__e), std::move(*__em1));
1742 __e = std::move_backward(__e - __de, __em1, __e);
1743 *--__e = std::move(__tmp.get());
1746 return begin() + __pos;
1749 #endif // _LIBCPP_CXX03_LANG
1751 template <class _Tp, class _Allocator>
1752 typename deque<_Tp, _Allocator>::iterator deque<_Tp, _Allocator>::insert(const_iterator __p, const value_type& __v) {
1753 size_type __pos = __p - begin();
1754 size_type __to_end = size() - __pos;
1755 allocator_type& __a = __alloc();
1756 if (__pos < __to_end) { // insert by shifting things backward
1757 if (__front_spare() == 0)
1758 __add_front_capacity();
1759 // __front_spare() >= 1
1760 __annotate_increase_front(1);
1762 __alloc_traits::construct(__a, std::addressof(*--begin()), __v);
1766 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1767 iterator __b = begin();
1768 iterator __bm1 = std::prev(__b);
1769 if (__vt == pointer_traits<const_pointer>::pointer_to(*__b))
1770 __vt = pointer_traits<const_pointer>::pointer_to(*__bm1);
1771 __alloc_traits::construct(__a, std::addressof(*__bm1), std::move(*__b));
1775 __b = __move_and_check(std::next(__b), __b + __pos, __b, __vt);
1778 } else { // insert by shifting things forward
1779 if (__back_spare() == 0)
1780 __add_back_capacity();
1781 // __back_capacity >= 1
1782 __annotate_increase_back(1);
1783 size_type __de = size() - __pos;
1785 __alloc_traits::construct(__a, std::addressof(*end()), __v);
1788 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1789 iterator __e = end();
1790 iterator __em1 = std::prev(__e);
1791 if (__vt == pointer_traits<const_pointer>::pointer_to(*__em1))
1792 __vt = pointer_traits<const_pointer>::pointer_to(*__e);
1793 __alloc_traits::construct(__a, std::addressof(*__e), std::move(*__em1));
1796 __e = __move_backward_and_check(__e - __de, __em1, __e, __vt);
1800 return begin() + __pos;
1803 template <class _Tp, class _Allocator>
1804 typename deque<_Tp, _Allocator>::iterator
1805 deque<_Tp, _Allocator>::insert(const_iterator __p, size_type __n, const value_type& __v) {
1806 size_type __pos = __p - begin();
1807 size_type __to_end = __size() - __pos;
1808 allocator_type& __a = __alloc();
1809 if (__pos < __to_end) { // insert by shifting things backward
1810 if (__n > __front_spare())
1811 __add_front_capacity(__n - __front_spare());
1812 // __n <= __front_spare()
1813 __annotate_increase_front(__n);
1814 iterator __old_begin = begin();
1815 iterator __i = __old_begin;
1817 for (size_type __m = __n - __pos; __m; --__m, --__start_, ++__size())
1818 __alloc_traits::construct(__a, std::addressof(*--__i), __v);
1822 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1823 iterator __obn = __old_begin + __n;
1824 __move_construct_backward_and_check(__old_begin, __obn, __i, __vt);
1826 __old_begin = __move_and_check(__obn, __old_begin + __pos, __old_begin, __vt);
1827 std::fill_n(__old_begin, __n, *__vt);
1829 } else { // insert by shifting things forward
1830 size_type __back_capacity = __back_spare();
1831 if (__n > __back_capacity)
1832 __add_back_capacity(__n - __back_capacity);
1833 // __n <= __back_capacity
1834 __annotate_increase_back(__n);
1835 iterator __old_end = end();
1836 iterator __i = __old_end;
1837 size_type __de = size() - __pos;
1839 for (size_type __m = __n - __de; __m; --__m, (void)++__i, ++__size())
1840 __alloc_traits::construct(__a, std::addressof(*__i), __v);
1844 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1845 iterator __oen = __old_end - __n;
1846 __move_construct_and_check(__oen, __old_end, __i, __vt);
1848 __old_end = __move_backward_and_check(__old_end - __de, __oen, __old_end, __vt);
1849 std::fill_n(__old_end - __n, __n, *__vt);
1852 return begin() + __pos;
1855 template <class _Tp, class _Allocator>
1856 template <class _InputIter, __enable_if_t<__has_exactly_input_iterator_category<_InputIter>::value, int> >
1857 typename deque<_Tp, _Allocator>::iterator
1858 deque<_Tp, _Allocator>::insert(const_iterator __p, _InputIter __f, _InputIter __l) {
1859 return __insert_with_sentinel(__p, __f, __l);
1862 template <class _Tp, class _Allocator>
1863 template <class _Iterator, class _Sentinel>
1864 _LIBCPP_HIDE_FROM_ABI typename deque<_Tp, _Allocator>::iterator
1865 deque<_Tp, _Allocator>::__insert_with_sentinel(const_iterator __p, _Iterator __f, _Sentinel __l) {
1866 __split_buffer<value_type, allocator_type&> __buf(__alloc());
1867 __buf.__construct_at_end_with_sentinel(std::move(__f), std::move(__l));
1868 typedef typename __split_buffer<value_type, allocator_type&>::iterator __bi;
1869 return insert(__p, move_iterator<__bi>(__buf.begin()), move_iterator<__bi>(__buf.end()));
1872 template <class _Tp, class _Allocator>
1873 template <class _ForwardIterator, __enable_if_t<__has_exactly_forward_iterator_category<_ForwardIterator>::value, int> >
1874 typename deque<_Tp, _Allocator>::iterator
1875 deque<_Tp, _Allocator>::insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l) {
1876 return __insert_with_size(__p, __f, std::distance(__f, __l));
1879 template <class _Tp, class _Allocator>
1880 template <class _Iterator>
1881 _LIBCPP_HIDE_FROM_ABI typename deque<_Tp, _Allocator>::iterator
1882 deque<_Tp, _Allocator>::__insert_with_size(const_iterator __p, _Iterator __f, size_type __n) {
1883 __split_buffer<value_type, allocator_type&> __buf(__n, 0, __alloc());
1884 __buf.__construct_at_end_with_size(__f, __n);
1885 typedef typename __split_buffer<value_type, allocator_type&>::iterator __fwd;
1886 return insert(__p, move_iterator<__fwd>(__buf.begin()), move_iterator<__fwd>(__buf.end()));
1889 template <class _Tp, class _Allocator>
1890 template <class _BiIter, __enable_if_t<__has_bidirectional_iterator_category<_BiIter>::value, int> >
1891 typename deque<_Tp, _Allocator>::iterator deque<_Tp, _Allocator>::insert(const_iterator __p, _BiIter __f, _BiIter __l) {
1892 return __insert_bidirectional(__p, __f, __l, std::distance(__f, __l));
1895 template <class _Tp, class _Allocator>
1896 template <class _BiIter, class _Sentinel>
1897 _LIBCPP_HIDE_FROM_ABI typename deque<_Tp, _Allocator>::iterator
1898 deque<_Tp, _Allocator>::__insert_bidirectional(const_iterator __p, _BiIter __f, _Sentinel, size_type __n) {
1899 return __insert_bidirectional(__p, __f, std::next(__f, __n), __n);
1902 template <class _Tp, class _Allocator>
1903 template <class _BiIter>
1904 _LIBCPP_HIDE_FROM_ABI typename deque<_Tp, _Allocator>::iterator
1905 deque<_Tp, _Allocator>::__insert_bidirectional(const_iterator __p, _BiIter __f, _BiIter __l, size_type __n) {
1906 size_type __pos = __p - begin();
1907 size_type __to_end = size() - __pos;
1908 allocator_type& __a = __alloc();
1909 if (__pos < __to_end) { // insert by shifting things backward
1910 if (__n > __front_spare())
1911 __add_front_capacity(__n - __front_spare());
1912 // __n <= __front_spare()
1913 __annotate_increase_front(__n);
1914 iterator __old_begin = begin();
1915 iterator __i = __old_begin;
1918 __m = __pos < __n / 2 ? std::prev(__l, __pos) : std::next(__f, __n - __pos);
1919 for (_BiIter __j = __m; __j != __f; --__start_, ++__size())
1920 __alloc_traits::construct(__a, std::addressof(*--__i), *--__j);
1924 iterator __obn = __old_begin + __n;
1925 for (iterator __j = __obn; __j != __old_begin;) {
1926 __alloc_traits::construct(__a, std::addressof(*--__i), std::move(*--__j));
1931 __old_begin = std::move(__obn, __old_begin + __pos, __old_begin);
1932 std::copy(__m, __l, __old_begin);
1934 } else { // insert by shifting things forward
1935 size_type __back_capacity = __back_spare();
1936 if (__n > __back_capacity)
1937 __add_back_capacity(__n - __back_capacity);
1938 // __n <= __back_capacity
1939 __annotate_increase_back(__n);
1940 iterator __old_end = end();
1941 iterator __i = __old_end;
1943 size_type __de = size() - __pos;
1945 __m = __de < __n / 2 ? std::next(__f, __de) : std::prev(__l, __n - __de);
1946 for (_BiIter __j = __m; __j != __l; ++__i, (void)++__j, ++__size())
1947 __alloc_traits::construct(__a, std::addressof(*__i), *__j);
1951 iterator __oen = __old_end - __n;
1952 for (iterator __j = __oen; __j != __old_end; ++__i, (void)++__j, ++__size())
1953 __alloc_traits::construct(__a, std::addressof(*__i), std::move(*__j));
1955 __old_end = std::move_backward(__old_end - __de, __oen, __old_end);
1956 std::copy_backward(__f, __m, __old_end);
1959 return begin() + __pos;
1962 template <class _Tp, class _Allocator>
1963 template <class _InpIter, __enable_if_t<__has_exactly_input_iterator_category<_InpIter>::value, int> >
1964 void deque<_Tp, _Allocator>::__append(_InpIter __f, _InpIter __l) {
1965 __append_with_sentinel(__f, __l);
1968 template <class _Tp, class _Allocator>
1969 template <class _InputIterator, class _Sentinel>
1970 _LIBCPP_HIDE_FROM_ABI void deque<_Tp, _Allocator>::__append_with_sentinel(_InputIterator __f, _Sentinel __l) {
1971 for (; __f != __l; ++__f)
1972 #ifdef _LIBCPP_CXX03_LANG
1979 template <class _Tp, class _Allocator>
1980 template <class _ForIter, __enable_if_t<__has_forward_iterator_category<_ForIter>::value, int> >
1981 void deque<_Tp, _Allocator>::__append(_ForIter __f, _ForIter __l) {
1982 __append_with_size(__f, std::distance(__f, __l));
1985 template <class _Tp, class _Allocator>
1986 template <class _InputIterator>
1987 _LIBCPP_HIDE_FROM_ABI void deque<_Tp, _Allocator>::__append_with_size(_InputIterator __f, size_type __n) {
1988 allocator_type& __a = __alloc();
1989 size_type __back_capacity = __back_spare();
1990 if (__n > __back_capacity)
1991 __add_back_capacity(__n - __back_capacity);
1993 // __n <= __back_capacity
1994 __annotate_increase_back(__n);
1995 for (__deque_block_range __br : __deque_range(end(), end() + __n)) {
1996 _ConstructTransaction __tx(this, __br);
1997 for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, (void)++__f) {
1998 __alloc_traits::construct(__a, std::__to_address(__tx.__pos_), *__f);
2003 template <class _Tp, class _Allocator>
2004 void deque<_Tp, _Allocator>::__append(size_type __n) {
2005 allocator_type& __a = __alloc();
2006 size_type __back_capacity = __back_spare();
2007 if (__n > __back_capacity)
2008 __add_back_capacity(__n - __back_capacity);
2009 // __n <= __back_capacity
2010 __annotate_increase_back(__n);
2011 for (__deque_block_range __br : __deque_range(end(), end() + __n)) {
2012 _ConstructTransaction __tx(this, __br);
2013 for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
2014 __alloc_traits::construct(__a, std::__to_address(__tx.__pos_));
2019 template <class _Tp, class _Allocator>
2020 void deque<_Tp, _Allocator>::__append(size_type __n, const value_type& __v) {
2021 allocator_type& __a = __alloc();
2022 size_type __back_capacity = __back_spare();
2023 if (__n > __back_capacity)
2024 __add_back_capacity(__n - __back_capacity);
2025 // __n <= __back_capacity
2026 __annotate_increase_back(__n);
2027 for (__deque_block_range __br : __deque_range(end(), end() + __n)) {
2028 _ConstructTransaction __tx(this, __br);
2029 for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
2030 __alloc_traits::construct(__a, std::__to_address(__tx.__pos_), __v);
2035 // Create front capacity for one block of elements.
2036 // Strong guarantee. Either do it or don't touch anything.
2037 template <class _Tp, class _Allocator>
2038 void deque<_Tp, _Allocator>::__add_front_capacity() {
2039 allocator_type& __a = __alloc();
2040 if (__back_spare() >= __block_size) {
2041 __start_ += __block_size;
2042 pointer __pt = __map_.back();
2044 __map_.emplace_front(__pt);
2046 // Else if __map_.size() < __map_.capacity() then we need to allocate 1 buffer
2047 else if (__map_.size() < __map_.capacity()) { // we can put the new buffer into the map, but don't shift things around
2048 // until all buffers are allocated. If we throw, we don't need to fix
2049 // anything up (any added buffers are undetectible)
2050 if (__map_.__front_spare() > 0)
2051 __map_.emplace_front(__alloc_traits::allocate(__a, __block_size));
2053 __map_.emplace_back(__alloc_traits::allocate(__a, __block_size));
2054 // Done allocating, reorder capacity
2055 pointer __pt = __map_.back();
2057 __map_.emplace_front(__pt);
2059 __start_ = __map_.size() == 1 ? __block_size / 2 : __start_ + __block_size;
2061 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2063 __split_buffer<pointer, __pointer_allocator&> __buf(
2064 std::max<size_type>(2 * __map_.capacity(), 1), 0, __map_.__alloc_);
2066 typedef __allocator_destructor<_Allocator> _Dp;
2067 unique_ptr<pointer, _Dp> __hold(__alloc_traits::allocate(__a, __block_size), _Dp(__a, __block_size));
2068 __buf.emplace_back(__hold.get());
2071 for (__map_pointer __i = __map_.begin(); __i != __map_.end(); ++__i)
2072 __buf.emplace_back(*__i);
2073 std::swap(__map_.__first_, __buf.__first_);
2074 std::swap(__map_.__begin_, __buf.__begin_);
2075 std::swap(__map_.__end_, __buf.__end_);
2076 std::swap(__map_.__cap_, __buf.__cap_);
2077 __start_ = __map_.size() == 1 ? __block_size / 2 : __start_ + __block_size;
2079 __annotate_whole_block(0, __asan_poison);
2082 // Create front capacity for __n elements.
2083 // Strong guarantee. Either do it or don't touch anything.
2084 template <class _Tp, class _Allocator>
2085 void deque<_Tp, _Allocator>::__add_front_capacity(size_type __n) {
2086 allocator_type& __a = __alloc();
2087 size_type __nb = __recommend_blocks(__n + __map_.empty());
2088 // Number of unused blocks at back:
2089 size_type __back_capacity = __back_spare() / __block_size;
2090 __back_capacity = std::min(__back_capacity, __nb); // don't take more than you need
2091 __nb -= __back_capacity; // number of blocks need to allocate
2092 // If __nb == 0, then we have sufficient capacity.
2094 __start_ += __block_size * __back_capacity;
2095 for (; __back_capacity > 0; --__back_capacity) {
2096 pointer __pt = __map_.back();
2098 __map_.emplace_front(__pt);
2101 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2102 else if (__nb <= __map_.capacity() -
2103 __map_.size()) { // we can put the new buffers into the map, but don't shift things around
2104 // until all buffers are allocated. If we throw, we don't need to fix
2105 // anything up (any added buffers are undetectible)
2106 for (; __nb > 0; --__nb, __start_ += __block_size - (__map_.size() == 1)) {
2107 if (__map_.__front_spare() == 0)
2109 __map_.emplace_front(__alloc_traits::allocate(__a, __block_size));
2110 __annotate_whole_block(0, __asan_poison);
2112 for (; __nb > 0; --__nb, ++__back_capacity)
2113 __map_.emplace_back(__alloc_traits::allocate(__a, __block_size));
2114 // Done allocating, reorder capacity
2115 __start_ += __back_capacity * __block_size;
2116 for (; __back_capacity > 0; --__back_capacity) {
2117 pointer __pt = __map_.back();
2119 __map_.emplace_front(__pt);
2120 __annotate_whole_block(0, __asan_poison);
2123 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2125 size_type __ds = (__nb + __back_capacity) * __block_size - __map_.empty();
2126 __split_buffer<pointer, __pointer_allocator&> __buf(
2127 std::max<size_type>(2 * __map_.capacity(), __nb + __map_.size()), 0, __map_.__alloc_);
2128 #if _LIBCPP_HAS_EXCEPTIONS
2130 #endif // _LIBCPP_HAS_EXCEPTIONS
2131 for (; __nb > 0; --__nb) {
2132 __buf.emplace_back(__alloc_traits::allocate(__a, __block_size));
2133 // ASan: this is empty container, we have to poison whole block
2134 __annotate_poison_block(std::__to_address(__buf.back()), std::__to_address(__buf.back() + __block_size));
2136 #if _LIBCPP_HAS_EXCEPTIONS
2138 __annotate_delete();
2139 for (__map_pointer __i = __buf.begin(); __i != __buf.end(); ++__i)
2140 __alloc_traits::deallocate(__a, *__i, __block_size);
2143 #endif // _LIBCPP_HAS_EXCEPTIONS
2144 for (; __back_capacity > 0; --__back_capacity) {
2145 __buf.emplace_back(__map_.back());
2148 for (__map_pointer __i = __map_.begin(); __i != __map_.end(); ++__i)
2149 __buf.emplace_back(*__i);
2150 std::swap(__map_.__first_, __buf.__first_);
2151 std::swap(__map_.__begin_, __buf.__begin_);
2152 std::swap(__map_.__end_, __buf.__end_);
2153 std::swap(__map_.__cap_, __buf.__cap_);
2158 // Create back capacity for one block of elements.
2159 // Strong guarantee. Either do it or don't touch anything.
2160 template <class _Tp, class _Allocator>
2161 void deque<_Tp, _Allocator>::__add_back_capacity() {
2162 allocator_type& __a = __alloc();
2163 if (__front_spare() >= __block_size) {
2164 __start_ -= __block_size;
2165 pointer __pt = __map_.front();
2167 __map_.emplace_back(__pt);
2169 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2170 else if (__map_.size() < __map_.capacity()) { // we can put the new buffer into the map, but don't shift things around
2171 // until it is allocated. If we throw, we don't need to fix
2172 // anything up (any added buffers are undetectible)
2173 if (__map_.__back_spare() != 0)
2174 __map_.emplace_back(__alloc_traits::allocate(__a, __block_size));
2176 __map_.emplace_front(__alloc_traits::allocate(__a, __block_size));
2177 // Done allocating, reorder capacity
2178 pointer __pt = __map_.front();
2180 __map_.emplace_back(__pt);
2182 __annotate_whole_block(__map_.size() - 1, __asan_poison);
2184 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2186 __split_buffer<pointer, __pointer_allocator&> __buf(
2187 std::max<size_type>(2 * __map_.capacity(), 1), __map_.size(), __map_.__alloc_);
2189 typedef __allocator_destructor<_Allocator> _Dp;
2190 unique_ptr<pointer, _Dp> __hold(__alloc_traits::allocate(__a, __block_size), _Dp(__a, __block_size));
2191 __buf.emplace_back(__hold.get());
2194 for (__map_pointer __i = __map_.end(); __i != __map_.begin();)
2195 __buf.emplace_front(*--__i);
2196 std::swap(__map_.__first_, __buf.__first_);
2197 std::swap(__map_.__begin_, __buf.__begin_);
2198 std::swap(__map_.__end_, __buf.__end_);
2199 std::swap(__map_.__cap_, __buf.__cap_);
2200 __annotate_whole_block(__map_.size() - 1, __asan_poison);
2204 // Create back capacity for __n elements.
2205 // Strong guarantee. Either do it or don't touch anything.
2206 template <class _Tp, class _Allocator>
2207 void deque<_Tp, _Allocator>::__add_back_capacity(size_type __n) {
2208 allocator_type& __a = __alloc();
2209 size_type __nb = __recommend_blocks(__n + __map_.empty());
2210 // Number of unused blocks at front:
2211 size_type __front_capacity = __front_spare() / __block_size;
2212 __front_capacity = std::min(__front_capacity, __nb); // don't take more than you need
2213 __nb -= __front_capacity; // number of blocks need to allocate
2214 // If __nb == 0, then we have sufficient capacity.
2216 __start_ -= __block_size * __front_capacity;
2217 for (; __front_capacity > 0; --__front_capacity) {
2218 pointer __pt = __map_.front();
2220 __map_.emplace_back(__pt);
2223 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2224 else if (__nb <= __map_.capacity() -
2225 __map_.size()) { // we can put the new buffers into the map, but don't shift things around
2226 // until all buffers are allocated. If we throw, we don't need to fix
2227 // anything up (any added buffers are undetectible)
2228 for (; __nb > 0; --__nb) {
2229 if (__map_.__back_spare() == 0)
2231 __map_.emplace_back(__alloc_traits::allocate(__a, __block_size));
2232 __annotate_whole_block(__map_.size() - 1, __asan_poison);
2234 for (; __nb > 0; --__nb, ++__front_capacity, __start_ += __block_size - (__map_.size() == 1)) {
2235 __map_.emplace_front(__alloc_traits::allocate(__a, __block_size));
2236 __annotate_whole_block(0, __asan_poison);
2238 // Done allocating, reorder capacity
2239 __start_ -= __block_size * __front_capacity;
2240 for (; __front_capacity > 0; --__front_capacity) {
2241 pointer __pt = __map_.front();
2243 __map_.emplace_back(__pt);
2246 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2248 size_type __ds = __front_capacity * __block_size;
2249 __split_buffer<pointer, __pointer_allocator&> __buf(
2250 std::max<size_type>(2 * __map_.capacity(), __nb + __map_.size()),
2251 __map_.size() - __front_capacity,
2253 #if _LIBCPP_HAS_EXCEPTIONS
2255 #endif // _LIBCPP_HAS_EXCEPTIONS
2256 for (; __nb > 0; --__nb) {
2257 __buf.emplace_back(__alloc_traits::allocate(__a, __block_size));
2258 // ASan: this is an empty container, we have to poison the whole block
2259 __annotate_poison_block(std::__to_address(__buf.back()), std::__to_address(__buf.back() + __block_size));
2261 #if _LIBCPP_HAS_EXCEPTIONS
2263 __annotate_delete();
2264 for (__map_pointer __i = __buf.begin(); __i != __buf.end(); ++__i)
2265 __alloc_traits::deallocate(__a, *__i, __block_size);
2268 #endif // _LIBCPP_HAS_EXCEPTIONS
2269 for (; __front_capacity > 0; --__front_capacity) {
2270 __buf.emplace_back(__map_.front());
2273 for (__map_pointer __i = __map_.end(); __i != __map_.begin();)
2274 __buf.emplace_front(*--__i);
2275 std::swap(__map_.__first_, __buf.__first_);
2276 std::swap(__map_.__begin_, __buf.__begin_);
2277 std::swap(__map_.__end_, __buf.__end_);
2278 std::swap(__map_.__cap_, __buf.__cap_);
2283 template <class _Tp, class _Allocator>
2284 void deque<_Tp, _Allocator>::pop_front() {
2285 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "deque::pop_front called on an empty deque");
2286 size_type __old_sz = size();
2287 size_type __old_start = __start_;
2288 allocator_type& __a = __alloc();
2289 __alloc_traits::destroy(
2290 __a, std::__to_address(*(__map_.begin() + __start_ / __block_size) + __start_ % __block_size));
2293 __annotate_shrink_front(__old_sz, __old_start);
2294 __maybe_remove_front_spare();
2297 template <class _Tp, class _Allocator>
2298 void deque<_Tp, _Allocator>::pop_back() {
2299 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "deque::pop_back called on an empty deque");
2300 size_type __old_sz = size();
2301 size_type __old_start = __start_;
2302 allocator_type& __a = __alloc();
2303 size_type __p = size() + __start_ - 1;
2304 __alloc_traits::destroy(__a, std::__to_address(*(__map_.begin() + __p / __block_size) + __p % __block_size));
2306 __annotate_shrink_back(__old_sz, __old_start);
2307 __maybe_remove_back_spare();
2310 // move assign [__f, __l) to [__r, __r + (__l-__f)).
2311 // If __vt points into [__f, __l), then subtract (__f - __r) from __vt.
2312 template <class _Tp, class _Allocator>
2313 typename deque<_Tp, _Allocator>::iterator
2314 deque<_Tp, _Allocator>::__move_and_check(iterator __f, iterator __l, iterator __r, const_pointer& __vt) {
2316 // for (; __f != __l; ++__f, ++__r)
2317 // *__r = std::move(*__f);
2318 difference_type __n = __l - __f;
2320 pointer __fb = __f.__ptr_;
2321 pointer __fe = *__f.__m_iter_ + __block_size;
2322 difference_type __bs = __fe - __fb;
2327 if (__fb <= __vt && __vt < __fe)
2328 __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) -= __f - __r).__ptr_;
2329 __r = std::move(__fb, __fe, __r);
2336 // move assign [__f, __l) to [__r - (__l-__f), __r) backwards.
2337 // If __vt points into [__f, __l), then add (__r - __l) to __vt.
2338 template <class _Tp, class _Allocator>
2339 typename deque<_Tp, _Allocator>::iterator
2340 deque<_Tp, _Allocator>::__move_backward_and_check(iterator __f, iterator __l, iterator __r, const_pointer& __vt) {
2342 // while (__f != __l)
2343 // *--__r = std::move(*--__l);
2344 difference_type __n = __l - __f;
2347 pointer __lb = *__l.__m_iter_;
2348 pointer __le = __l.__ptr_ + 1;
2349 difference_type __bs = __le - __lb;
2354 if (__lb <= __vt && __vt < __le)
2355 __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) += __r - __l - 1).__ptr_;
2356 __r = std::move_backward(__lb, __le, __r);
2363 // move construct [__f, __l) to [__r, __r + (__l-__f)).
2364 // If __vt points into [__f, __l), then add (__r - __f) to __vt.
2365 template <class _Tp, class _Allocator>
2366 void deque<_Tp, _Allocator>::__move_construct_and_check(iterator __f, iterator __l, iterator __r, const_pointer& __vt) {
2367 allocator_type& __a = __alloc();
2369 // for (; __f != __l; ++__r, ++__f, ++__size())
2370 // __alloc_traits::construct(__a, std::addressof(*__r), std::move(*__f));
2371 difference_type __n = __l - __f;
2373 pointer __fb = __f.__ptr_;
2374 pointer __fe = *__f.__m_iter_ + __block_size;
2375 difference_type __bs = __fe - __fb;
2380 if (__fb <= __vt && __vt < __fe)
2381 __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) += __r - __f).__ptr_;
2382 for (; __fb != __fe; ++__fb, ++__r, ++__size())
2383 __alloc_traits::construct(__a, std::addressof(*__r), std::move(*__fb));
2389 // move construct [__f, __l) to [__r - (__l-__f), __r) backwards.
2390 // If __vt points into [__f, __l), then subtract (__l - __r) from __vt.
2391 template <class _Tp, class _Allocator>
2392 void deque<_Tp, _Allocator>::__move_construct_backward_and_check(
2393 iterator __f, iterator __l, iterator __r, const_pointer& __vt) {
2394 allocator_type& __a = __alloc();
2396 // for (iterator __j = __l; __j != __f;)
2398 // __alloc_traitsconstruct(__a, std::addressof(*--__r), std::move(*--__j));
2402 difference_type __n = __l - __f;
2405 pointer __lb = *__l.__m_iter_;
2406 pointer __le = __l.__ptr_ + 1;
2407 difference_type __bs = __le - __lb;
2412 if (__lb <= __vt && __vt < __le)
2413 __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) -= __l - __r + 1).__ptr_;
2414 while (__le != __lb) {
2415 __alloc_traits::construct(__a, std::addressof(*--__r), std::move(*--__le));
2424 template <class _Tp, class _Allocator>
2425 typename deque<_Tp, _Allocator>::iterator deque<_Tp, _Allocator>::erase(const_iterator __f) {
2426 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
2427 __f != end(), "deque::erase(iterator) called with a non-dereferenceable iterator");
2428 size_type __old_sz = size();
2429 size_type __old_start = __start_;
2430 iterator __b = begin();
2431 difference_type __pos = __f - __b;
2432 iterator __p = __b + __pos;
2433 allocator_type& __a = __alloc();
2434 if (static_cast<size_t>(__pos) <= (size() - 1) / 2) { // erase from front
2435 std::move_backward(__b, __p, std::next(__p));
2436 __alloc_traits::destroy(__a, std::addressof(*__b));
2439 __annotate_shrink_front(__old_sz, __old_start);
2440 __maybe_remove_front_spare();
2441 } else { // erase from back
2442 iterator __i = std::move(std::next(__p), end(), __p);
2443 __alloc_traits::destroy(__a, std::addressof(*__i));
2445 __annotate_shrink_back(__old_sz, __old_start);
2446 __maybe_remove_back_spare();
2448 return begin() + __pos;
2451 template <class _Tp, class _Allocator>
2452 typename deque<_Tp, _Allocator>::iterator deque<_Tp, _Allocator>::erase(const_iterator __f, const_iterator __l) {
2453 _LIBCPP_ASSERT_VALID_INPUT_RANGE(__f <= __l, "deque::erase(first, last) called with an invalid range");
2454 size_type __old_sz = size();
2455 size_type __old_start = __start_;
2456 difference_type __n = __l - __f;
2457 iterator __b = begin();
2458 difference_type __pos = __f - __b;
2459 iterator __p = __b + __pos;
2461 allocator_type& __a = __alloc();
2462 if (static_cast<size_t>(__pos) <= (size() - __n) / 2) { // erase from front
2463 iterator __i = std::move_backward(__b, __p, __p + __n);
2464 for (; __b != __i; ++__b)
2465 __alloc_traits::destroy(__a, std::addressof(*__b));
2468 __annotate_shrink_front(__old_sz, __old_start);
2469 while (__maybe_remove_front_spare()) {
2471 } else { // erase from back
2472 iterator __i = std::move(__p + __n, end(), __p);
2473 for (iterator __e = end(); __i != __e; ++__i)
2474 __alloc_traits::destroy(__a, std::addressof(*__i));
2476 __annotate_shrink_back(__old_sz, __old_start);
2477 while (__maybe_remove_back_spare()) {
2481 return begin() + __pos;
2484 template <class _Tp, class _Allocator>
2485 void deque<_Tp, _Allocator>::__erase_to_end(const_iterator __f) {
2486 size_type __old_sz = size();
2487 size_type __old_start = __start_;
2488 iterator __e = end();
2489 difference_type __n = __e - __f;
2491 allocator_type& __a = __alloc();
2492 iterator __b = begin();
2493 difference_type __pos = __f - __b;
2494 for (iterator __p = __b + __pos; __p != __e; ++__p)
2495 __alloc_traits::destroy(__a, std::addressof(*__p));
2497 __annotate_shrink_back(__old_sz, __old_start);
2498 while (__maybe_remove_back_spare()) {
2503 template <class _Tp, class _Allocator>
2504 inline void deque<_Tp, _Allocator>::swap(deque& __c)
2505 #if _LIBCPP_STD_VER >= 14
2508 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<allocator_type>)
2511 __map_.swap(__c.__map_);
2512 std::swap(__start_, __c.__start_);
2513 std::swap(__size(), __c.__size());
2514 std::__swap_allocator(__alloc(), __c.__alloc());
2517 template <class _Tp, class _Allocator>
2518 inline void deque<_Tp, _Allocator>::clear() _NOEXCEPT {
2519 __annotate_delete();
2520 allocator_type& __a = __alloc();
2521 for (iterator __i = begin(), __e = end(); __i != __e; ++__i)
2522 __alloc_traits::destroy(__a, std::addressof(*__i));
2524 while (__map_.size() > 2) {
2525 __alloc_traits::deallocate(__a, __map_.front(), __block_size);
2528 switch (__map_.size()) {
2530 __start_ = __block_size / 2;
2533 __start_ = __block_size;
2539 template <class _Tp, class _Allocator>
2540 inline _LIBCPP_HIDE_FROM_ABI bool operator==(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) {
2541 const typename deque<_Tp, _Allocator>::size_type __sz = __x.size();
2542 return __sz == __y.size() && std::equal(__x.begin(), __x.end(), __y.begin());
2545 #if _LIBCPP_STD_VER <= 17
2547 template <class _Tp, class _Allocator>
2548 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) {
2549 return !(__x == __y);
2552 template <class _Tp, class _Allocator>
2553 inline _LIBCPP_HIDE_FROM_ABI bool operator<(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) {
2554 return std::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
2557 template <class _Tp, class _Allocator>
2558 inline _LIBCPP_HIDE_FROM_ABI bool operator>(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) {
2562 template <class _Tp, class _Allocator>
2563 inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) {
2564 return !(__x < __y);
2567 template <class _Tp, class _Allocator>
2568 inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) {
2569 return !(__y < __x);
2572 #else // _LIBCPP_STD_VER <= 17
2574 template <class _Tp, class _Allocator>
2575 _LIBCPP_HIDE_FROM_ABI __synth_three_way_result<_Tp>
2576 operator<=>(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) {
2577 return std::lexicographical_compare_three_way(__x.begin(), __x.end(), __y.begin(), __y.end(), std::__synth_three_way);
2580 #endif // _LIBCPP_STD_VER <= 17
2582 template <class _Tp, class _Allocator>
2583 inline _LIBCPP_HIDE_FROM_ABI void swap(deque<_Tp, _Allocator>& __x, deque<_Tp, _Allocator>& __y)
2584 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) {
2588 #if _LIBCPP_STD_VER >= 20
2589 template <class _Tp, class _Allocator, class _Up>
2590 inline _LIBCPP_HIDE_FROM_ABI typename deque<_Tp, _Allocator>::size_type
2591 erase(deque<_Tp, _Allocator>& __c, const _Up& __v) {
2592 auto __old_size = __c.size();
2593 __c.erase(std::remove(__c.begin(), __c.end(), __v), __c.end());
2594 return __old_size - __c.size();
2597 template <class _Tp, class _Allocator, class _Predicate>
2598 inline _LIBCPP_HIDE_FROM_ABI typename deque<_Tp, _Allocator>::size_type
2599 erase_if(deque<_Tp, _Allocator>& __c, _Predicate __pred) {
2600 auto __old_size = __c.size();
2601 __c.erase(std::remove_if(__c.begin(), __c.end(), __pred), __c.end());
2602 return __old_size - __c.size();
2606 inline constexpr bool __format::__enable_insertable<std::deque<char>> = true;
2607 # if _LIBCPP_HAS_WIDE_CHARACTERS
2609 inline constexpr bool __format::__enable_insertable<std::deque<wchar_t>> = true;
2612 #endif // _LIBCPP_STD_VER >= 20
2614 template <class _Tp, class _Allocator>
2615 struct __container_traits<deque<_Tp, _Allocator> > {
2616 // http://eel.is/c++draft/deque.modifiers#3
2617 // If an exception is thrown other than by the copy constructor, move constructor, assignment operator, or move
2618 // assignment operator of T, there are no effects. If an exception is thrown while inserting a single element at
2619 // either end, there are no effects. Otherwise, if an exception is thrown by the move constructor of a
2620 // non-Cpp17CopyInsertable T, the effects are unspecified.
2621 static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee =
2622 _Or<is_nothrow_move_constructible<_Tp>, __is_cpp17_copy_insertable<_Allocator> >::value;
2625 _LIBCPP_END_NAMESPACE_STD
2627 #if _LIBCPP_STD_VER >= 17
2628 _LIBCPP_BEGIN_NAMESPACE_STD
2630 template <class _ValueT>
2631 using deque _LIBCPP_AVAILABILITY_PMR = std::deque<_ValueT, polymorphic_allocator<_ValueT>>;
2633 _LIBCPP_END_NAMESPACE_STD
2638 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
2639 # include <algorithm>
2641 # include <concepts>
2643 # include <functional>
2645 # include <iterator>
2646 # include <type_traits>
2647 # include <typeinfo>
2650 #endif // _LIBCPP_DEQUE