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 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___CXX03_DEQUE
11 #define _LIBCPP___CXX03_DEQUE
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 <__cxx03/__algorithm/copy.h>
181 #include <__cxx03/__algorithm/copy_backward.h>
182 #include <__cxx03/__algorithm/copy_n.h>
183 #include <__cxx03/__algorithm/equal.h>
184 #include <__cxx03/__algorithm/fill_n.h>
185 #include <__cxx03/__algorithm/lexicographical_compare.h>
186 #include <__cxx03/__algorithm/lexicographical_compare_three_way.h>
187 #include <__cxx03/__algorithm/min.h>
188 #include <__cxx03/__algorithm/remove.h>
189 #include <__cxx03/__algorithm/remove_if.h>
190 #include <__cxx03/__algorithm/unwrap_iter.h>
191 #include <__cxx03/__assert>
192 #include <__cxx03/__config>
193 #include <__cxx03/__debug_utils/sanitizers.h>
194 #include <__cxx03/__format/enable_insertable.h>
195 #include <__cxx03/__fwd/deque.h>
196 #include <__cxx03/__iterator/distance.h>
197 #include <__cxx03/__iterator/iterator_traits.h>
198 #include <__cxx03/__iterator/next.h>
199 #include <__cxx03/__iterator/prev.h>
200 #include <__cxx03/__iterator/reverse_iterator.h>
201 #include <__cxx03/__iterator/segmented_iterator.h>
202 #include <__cxx03/__memory/addressof.h>
203 #include <__cxx03/__memory/allocator_destructor.h>
204 #include <__cxx03/__memory/pointer_traits.h>
205 #include <__cxx03/__memory/temp_value.h>
206 #include <__cxx03/__memory/unique_ptr.h>
207 #include <__cxx03/__memory_resource/polymorphic_allocator.h>
208 #include <__cxx03/__ranges/access.h>
209 #include <__cxx03/__ranges/concepts.h>
210 #include <__cxx03/__ranges/container_compatible_range.h>
211 #include <__cxx03/__ranges/from_range.h>
212 #include <__cxx03/__ranges/size.h>
213 #include <__cxx03/__split_buffer>
214 #include <__cxx03/__type_traits/is_allocator.h>
215 #include <__cxx03/__type_traits/is_convertible.h>
216 #include <__cxx03/__type_traits/is_same.h>
217 #include <__cxx03/__type_traits/is_swappable.h>
218 #include <__cxx03/__type_traits/type_identity.h>
219 #include <__cxx03/__utility/forward.h>
220 #include <__cxx03/__utility/move.h>
221 #include <__cxx03/__utility/pair.h>
222 #include <__cxx03/__utility/swap.h>
223 #include <__cxx03/limits>
224 #include <__cxx03/stdexcept>
225 #include <__cxx03/version>
227 // standard-mandated includes
230 #include <__cxx03/__iterator/access.h>
231 #include <__cxx03/__iterator/data.h>
232 #include <__cxx03/__iterator/empty.h>
233 #include <__cxx03/__iterator/reverse_access.h>
234 #include <__cxx03/__iterator/size.h>
237 #include <__cxx03/compare>
238 #include <__cxx03/initializer_list>
240 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
241 # pragma GCC system_header
245 #include <__cxx03/__undef_macros>
247 _LIBCPP_BEGIN_NAMESPACE_STD
249 template <class _ValueType, class _DiffType>
250 struct __deque_block_size {
251 static const _DiffType value = sizeof(_ValueType) < 256 ? 4096 / sizeof(_ValueType) : 16;
254 template <class _ValueType,
260 #ifdef _LIBCPP_ABI_INCOMPLETE_TYPES_IN_DEQUE
261 // Keep template parameter to avoid changing all template declarations thoughout
265 __deque_block_size<_ValueType, _DiffType>::value
268 class _LIBCPP_TEMPLATE_VIS __deque_iterator {
269 typedef _MapPointer __map_iterator;
272 typedef _Pointer pointer;
273 typedef _DiffType difference_type;
276 __map_iterator __m_iter_;
279 static const difference_type __block_size;
282 typedef _ValueType value_type;
283 typedef random_access_iterator_tag iterator_category;
284 typedef _Reference reference;
286 _LIBCPP_HIDE_FROM_ABI __deque_iterator() _NOEXCEPT
287 #if _LIBCPP_STD_VER >= 14
288 : __m_iter_(nullptr),
294 template <class _Pp, class _Rp, class _MP, __enable_if_t<is_convertible<_Pp, pointer>::value, int> = 0>
295 _LIBCPP_HIDE_FROM_ABI
296 __deque_iterator(const __deque_iterator<value_type, _Pp, _Rp, _MP, difference_type, _BS>& __it) _NOEXCEPT
297 : __m_iter_(__it.__m_iter_),
298 __ptr_(__it.__ptr_) {}
300 _LIBCPP_HIDE_FROM_ABI reference operator*() const { return *__ptr_; }
301 _LIBCPP_HIDE_FROM_ABI pointer operator->() const { return __ptr_; }
303 _LIBCPP_HIDE_FROM_ABI __deque_iterator& operator++() {
304 if (++__ptr_ - *__m_iter_ == __block_size) {
311 _LIBCPP_HIDE_FROM_ABI __deque_iterator operator++(int) {
312 __deque_iterator __tmp = *this;
317 _LIBCPP_HIDE_FROM_ABI __deque_iterator& operator--() {
318 if (__ptr_ == *__m_iter_) {
320 __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+=(difference_type __n) {
334 __n += __ptr_ - *__m_iter_;
336 __m_iter_ += __n / __block_size;
337 __ptr_ = *__m_iter_ + __n % __block_size;
340 difference_type __z = __block_size - 1 - __n;
341 __m_iter_ -= __z / __block_size;
342 __ptr_ = *__m_iter_ + (__block_size - 1 - __z % __block_size);
348 _LIBCPP_HIDE_FROM_ABI __deque_iterator& operator-=(difference_type __n) { return *this += -__n; }
350 _LIBCPP_HIDE_FROM_ABI __deque_iterator operator+(difference_type __n) const {
351 __deque_iterator __t(*this);
356 _LIBCPP_HIDE_FROM_ABI __deque_iterator operator-(difference_type __n) const {
357 __deque_iterator __t(*this);
362 _LIBCPP_HIDE_FROM_ABI friend __deque_iterator operator+(difference_type __n, const __deque_iterator& __it) {
366 _LIBCPP_HIDE_FROM_ABI friend difference_type operator-(const __deque_iterator& __x, const __deque_iterator& __y) {
368 return (__x.__m_iter_ - __y.__m_iter_) * __block_size + (__x.__ptr_ - *__x.__m_iter_) -
369 (__y.__ptr_ - *__y.__m_iter_);
373 _LIBCPP_HIDE_FROM_ABI reference operator[](difference_type __n) const { return *(*this + __n); }
375 _LIBCPP_HIDE_FROM_ABI friend bool operator==(const __deque_iterator& __x, const __deque_iterator& __y) {
376 return __x.__ptr_ == __y.__ptr_;
379 #if _LIBCPP_STD_VER <= 17
380 _LIBCPP_HIDE_FROM_ABI friend bool operator!=(const __deque_iterator& __x, const __deque_iterator& __y) {
381 return !(__x == __y);
385 // TODO(mordante) disable these overloads in the LLVM 20 release.
386 _LIBCPP_HIDE_FROM_ABI friend bool operator<(const __deque_iterator& __x, const __deque_iterator& __y) {
387 return __x.__m_iter_ < __y.__m_iter_ || (__x.__m_iter_ == __y.__m_iter_ && __x.__ptr_ < __y.__ptr_);
390 _LIBCPP_HIDE_FROM_ABI friend bool operator>(const __deque_iterator& __x, const __deque_iterator& __y) {
394 _LIBCPP_HIDE_FROM_ABI friend bool operator<=(const __deque_iterator& __x, const __deque_iterator& __y) {
398 _LIBCPP_HIDE_FROM_ABI friend bool operator>=(const __deque_iterator& __x, const __deque_iterator& __y) {
402 #if _LIBCPP_STD_VER >= 20
403 _LIBCPP_HIDE_FROM_ABI friend strong_ordering operator<=>(const __deque_iterator& __x, const __deque_iterator& __y) {
404 if (__x.__m_iter_ < __y.__m_iter_)
405 return strong_ordering::less;
407 if (__x.__m_iter_ == __y.__m_iter_) {
408 if constexpr (three_way_comparable<pointer, strong_ordering>) {
409 return __x.__ptr_ <=> __y.__ptr_;
411 if (__x.__ptr_ < __y.__ptr_)
412 return strong_ordering::less;
414 if (__x.__ptr_ == __y.__ptr_)
415 return strong_ordering::equal;
417 return strong_ordering::greater;
421 return strong_ordering::greater;
423 #endif // _LIBCPP_STD_VER >= 20
426 _LIBCPP_HIDE_FROM_ABI explicit __deque_iterator(__map_iterator __m, pointer __p) _NOEXCEPT
430 template <class _Tp, class _Ap>
431 friend class _LIBCPP_TEMPLATE_VIS deque;
432 template <class _Vp, class _Pp, class _Rp, class _MP, class _Dp, _Dp>
433 friend class _LIBCPP_TEMPLATE_VIS __deque_iterator;
436 friend struct __segmented_iterator_traits;
439 template <class _ValueType, class _Pointer, class _Reference, class _MapPointer, class _DiffType, _DiffType _BlockSize>
440 struct __segmented_iterator_traits<
441 __deque_iterator<_ValueType, _Pointer, _Reference, _MapPointer, _DiffType, _BlockSize> > {
443 using _Iterator = __deque_iterator<_ValueType, _Pointer, _Reference, _MapPointer, _DiffType, _BlockSize>;
446 using __is_segmented_iterator = true_type;
447 using __segment_iterator = _MapPointer;
448 using __local_iterator = _Pointer;
450 static _LIBCPP_HIDE_FROM_ABI __segment_iterator __segment(_Iterator __iter) { return __iter.__m_iter_; }
451 static _LIBCPP_HIDE_FROM_ABI __local_iterator __local(_Iterator __iter) { return __iter.__ptr_; }
452 static _LIBCPP_HIDE_FROM_ABI __local_iterator __begin(__segment_iterator __iter) { return *__iter; }
454 static _LIBCPP_HIDE_FROM_ABI __local_iterator __end(__segment_iterator __iter) {
455 return *__iter + _Iterator::__block_size;
458 static _LIBCPP_HIDE_FROM_ABI _Iterator __compose(__segment_iterator __segment, __local_iterator __local) {
459 if (__segment && __local == __end(__segment)) {
461 return _Iterator(__segment, *__segment);
463 return _Iterator(__segment, __local);
467 template <class _ValueType, class _Pointer, class _Reference, class _MapPointer, class _DiffType, _DiffType _BlockSize>
468 const _DiffType __deque_iterator<_ValueType, _Pointer, _Reference, _MapPointer, _DiffType, _BlockSize>::__block_size =
469 __deque_block_size<_ValueType, _DiffType>::value;
471 template <class _Tp, class _Allocator /*= allocator<_Tp>*/>
472 class _LIBCPP_TEMPLATE_VIS deque {
476 using value_type = _Tp;
478 using allocator_type = _Allocator;
479 using __alloc_traits = allocator_traits<allocator_type>;
480 static_assert(__check_valid_allocator<allocator_type>::value, "");
481 static_assert(is_same<typename allocator_type::value_type, value_type>::value,
482 "Allocator::value_type must be same type as value_type");
484 using size_type = typename __alloc_traits::size_type;
485 using difference_type = typename __alloc_traits::difference_type;
487 using pointer = typename __alloc_traits::pointer;
488 using const_pointer = typename __alloc_traits::const_pointer;
490 using __pointer_allocator = __rebind_alloc<__alloc_traits, pointer>;
491 using __const_pointer_allocator = __rebind_alloc<__alloc_traits, const_pointer>;
492 using __map = __split_buffer<pointer, __pointer_allocator>;
493 using __map_alloc_traits = allocator_traits<__pointer_allocator>;
494 using __map_pointer = typename __map_alloc_traits::pointer;
495 using __map_const_pointer = typename allocator_traits<__const_pointer_allocator>::const_pointer;
496 using __map_const_iterator = typename __map::const_iterator;
498 using reference = value_type&;
499 using const_reference = const value_type&;
501 using iterator = __deque_iterator<value_type, pointer, reference, __map_pointer, difference_type>;
502 using const_iterator =
503 __deque_iterator<value_type, const_pointer, const_reference, __map_const_pointer, difference_type>;
504 using reverse_iterator = std::reverse_iterator<iterator>;
505 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
507 // A deque contains the following members which may be trivially relocatable:
508 // - __map: is a `__split_buffer`, see `__split_buffer` for more information on when it is trivially relocatable
509 // - size_type: is always trivially relocatable, since it is required to be an integral type
510 // - allocator_type: may not be trivially relocatable, so it's checked
511 // None of these are referencing the `deque` itself, so if all of them are trivially relocatable, `deque` is too.
512 using __trivially_relocatable = __conditional_t<
513 __libcpp_is_trivially_relocatable<__map>::value && __libcpp_is_trivially_relocatable<allocator_type>::value,
517 static_assert(is_nothrow_default_constructible<allocator_type>::value ==
518 is_nothrow_default_constructible<__pointer_allocator>::value,
519 "rebinding an allocator should not change exception guarantees");
520 static_assert(is_nothrow_move_constructible<allocator_type>::value ==
521 is_nothrow_move_constructible<typename __map::allocator_type>::value,
522 "rebinding an allocator should not change exception guarantees");
525 struct __deque_block_range {
526 explicit _LIBCPP_HIDE_FROM_ABI __deque_block_range(pointer __b, pointer __e) _NOEXCEPT
529 const pointer __begin_;
530 const pointer __end_;
533 struct __deque_range {
535 const iterator __end_;
537 _LIBCPP_HIDE_FROM_ABI __deque_range(iterator __pos, iterator __e) _NOEXCEPT : __pos_(__pos), __end_(__e) {}
539 explicit _LIBCPP_HIDE_FROM_ABI operator bool() const _NOEXCEPT { return __pos_ != __end_; }
541 _LIBCPP_HIDE_FROM_ABI __deque_range begin() const { return *this; }
543 _LIBCPP_HIDE_FROM_ABI __deque_range end() const { return __deque_range(__end_, __end_); }
544 _LIBCPP_HIDE_FROM_ABI __deque_block_range operator*() const _NOEXCEPT {
545 if (__pos_.__m_iter_ == __end_.__m_iter_) {
546 return __deque_block_range(__pos_.__ptr_, __end_.__ptr_);
548 return __deque_block_range(__pos_.__ptr_, *__pos_.__m_iter_ + __block_size);
551 _LIBCPP_HIDE_FROM_ABI __deque_range& operator++() _NOEXCEPT {
552 if (__pos_.__m_iter_ == __end_.__m_iter_) {
556 __pos_.__ptr_ = *__pos_.__m_iter_;
561 _LIBCPP_HIDE_FROM_ABI friend bool operator==(__deque_range const& __lhs, __deque_range const& __rhs) {
562 return __lhs.__pos_ == __rhs.__pos_;
564 _LIBCPP_HIDE_FROM_ABI friend bool operator!=(__deque_range const& __lhs, __deque_range const& __rhs) {
565 return !(__lhs == __rhs);
569 struct _ConstructTransaction {
570 _LIBCPP_HIDE_FROM_ABI _ConstructTransaction(deque* __db, __deque_block_range& __r)
571 : __pos_(__r.__begin_), __end_(__r.__end_), __begin_(__r.__begin_), __base_(__db) {}
573 _LIBCPP_HIDE_FROM_ABI ~_ConstructTransaction() { __base_->__size() += (__pos_ - __begin_); }
576 const pointer __end_;
579 const pointer __begin_;
580 deque* const __base_;
583 static const difference_type __block_size;
587 __compressed_pair<size_type, allocator_type> __size_;
590 // construct/copy/destroy:
591 _LIBCPP_HIDE_FROM_ABI deque() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
592 : __start_(0), __size_(0, __default_init_tag()) {
596 _LIBCPP_HIDE_FROM_ABI ~deque() {
599 typename __map::iterator __i = __map_.begin();
600 typename __map::iterator __e = __map_.end();
601 for (; __i != __e; ++__i)
602 __alloc_traits::deallocate(__alloc(), *__i, __block_size);
605 _LIBCPP_HIDE_FROM_ABI explicit deque(const allocator_type& __a)
606 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a) {
610 explicit _LIBCPP_HIDE_FROM_ABI deque(size_type __n);
611 #if _LIBCPP_STD_VER >= 14
612 explicit _LIBCPP_HIDE_FROM_ABI deque(size_type __n, const _Allocator& __a);
614 _LIBCPP_HIDE_FROM_ABI deque(size_type __n, const value_type& __v);
616 template <__enable_if_t<__is_allocator<_Allocator>::value, int> = 0>
617 _LIBCPP_HIDE_FROM_ABI deque(size_type __n, const value_type& __v, const allocator_type& __a)
618 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a) {
624 template <class _InputIter, __enable_if_t<__has_input_iterator_category<_InputIter>::value, int> = 0>
625 _LIBCPP_HIDE_FROM_ABI deque(_InputIter __f, _InputIter __l);
626 template <class _InputIter, __enable_if_t<__has_input_iterator_category<_InputIter>::value, int> = 0>
627 _LIBCPP_HIDE_FROM_ABI deque(_InputIter __f, _InputIter __l, const allocator_type& __a);
629 #if _LIBCPP_STD_VER >= 23
630 template <_ContainerCompatibleRange<_Tp> _Range>
631 _LIBCPP_HIDE_FROM_ABI deque(from_range_t, _Range&& __range, const allocator_type& __a = allocator_type())
632 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a) {
633 if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
634 __append_with_size(ranges::begin(__range), ranges::distance(__range));
637 for (auto&& __e : __range) {
638 emplace_back(std::forward<decltype(__e)>(__e));
644 _LIBCPP_HIDE_FROM_ABI deque(const deque& __c);
645 _LIBCPP_HIDE_FROM_ABI deque(const deque& __c, const __type_identity_t<allocator_type>& __a);
647 _LIBCPP_HIDE_FROM_ABI deque& operator=(const deque& __c);
649 #ifndef _LIBCPP_CXX03_LANG
650 _LIBCPP_HIDE_FROM_ABI deque(initializer_list<value_type> __il);
651 _LIBCPP_HIDE_FROM_ABI deque(initializer_list<value_type> __il, const allocator_type& __a);
653 _LIBCPP_HIDE_FROM_ABI deque& operator=(initializer_list<value_type> __il) {
658 _LIBCPP_HIDE_FROM_ABI deque(deque&& __c) noexcept(is_nothrow_move_constructible<allocator_type>::value);
659 _LIBCPP_HIDE_FROM_ABI deque(deque&& __c, const __type_identity_t<allocator_type>& __a);
660 _LIBCPP_HIDE_FROM_ABI deque&
661 operator=(deque&& __c) noexcept(__alloc_traits::propagate_on_container_move_assignment::value &&
662 is_nothrow_move_assignable<allocator_type>::value);
664 _LIBCPP_HIDE_FROM_ABI void assign(initializer_list<value_type> __il) { assign(__il.begin(), __il.end()); }
665 #endif // _LIBCPP_CXX03_LANG
667 template <class _InputIter,
668 __enable_if_t<__has_input_iterator_category<_InputIter>::value &&
669 !__has_random_access_iterator_category<_InputIter>::value,
671 _LIBCPP_HIDE_FROM_ABI void assign(_InputIter __f, _InputIter __l);
672 template <class _RAIter, __enable_if_t<__has_random_access_iterator_category<_RAIter>::value, int> = 0>
673 _LIBCPP_HIDE_FROM_ABI void assign(_RAIter __f, _RAIter __l);
675 #if _LIBCPP_STD_VER >= 23
676 template <_ContainerCompatibleRange<_Tp> _Range>
677 _LIBCPP_HIDE_FROM_ABI void assign_range(_Range&& __range) {
678 if constexpr (ranges::random_access_range<_Range>) {
679 auto __n = static_cast<size_type>(ranges::distance(__range));
680 __assign_with_size_random_access(ranges::begin(__range), __n);
682 } else if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
683 auto __n = static_cast<size_type>(ranges::distance(__range));
684 __assign_with_size(ranges::begin(__range), __n);
687 __assign_with_sentinel(ranges::begin(__range), ranges::end(__range));
692 _LIBCPP_HIDE_FROM_ABI void assign(size_type __n, const value_type& __v);
694 _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const _NOEXCEPT;
695 _LIBCPP_HIDE_FROM_ABI allocator_type& __alloc() _NOEXCEPT { return __size_.second(); }
696 _LIBCPP_HIDE_FROM_ABI const allocator_type& __alloc() const _NOEXCEPT { return __size_.second(); }
700 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT {
701 __map_pointer __mp = __map_.begin() + __start_ / __block_size;
702 return iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
705 _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT {
706 __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __start_ / __block_size);
707 return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
710 _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT {
711 size_type __p = size() + __start_;
712 __map_pointer __mp = __map_.begin() + __p / __block_size;
713 return iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
716 _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT {
717 size_type __p = size() + __start_;
718 __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __p / __block_size);
719 return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
722 _LIBCPP_HIDE_FROM_ABI reverse_iterator rbegin() _NOEXCEPT { return reverse_iterator(end()); }
723 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rbegin() const _NOEXCEPT { return const_reverse_iterator(end()); }
724 _LIBCPP_HIDE_FROM_ABI reverse_iterator rend() _NOEXCEPT { return reverse_iterator(begin()); }
725 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rend() const _NOEXCEPT { return const_reverse_iterator(begin()); }
727 _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT { return begin(); }
728 _LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT { return end(); }
729 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crbegin() const _NOEXCEPT { return const_reverse_iterator(end()); }
730 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crend() const _NOEXCEPT { return const_reverse_iterator(begin()); }
733 _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return __size(); }
735 _LIBCPP_HIDE_FROM_ABI size_type& __size() _NOEXCEPT { return __size_.first(); }
736 _LIBCPP_HIDE_FROM_ABI const size_type& __size() const _NOEXCEPT { return __size_.first(); }
738 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
739 return std::min<size_type>(__alloc_traits::max_size(__alloc()), numeric_limits<difference_type>::max());
741 _LIBCPP_HIDE_FROM_ABI void resize(size_type __n);
742 _LIBCPP_HIDE_FROM_ABI void resize(size_type __n, const value_type& __v);
743 _LIBCPP_HIDE_FROM_ABI void shrink_to_fit() _NOEXCEPT;
744 _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT { return size() == 0; }
747 _LIBCPP_HIDE_FROM_ABI reference operator[](size_type __i) _NOEXCEPT;
748 _LIBCPP_HIDE_FROM_ABI const_reference operator[](size_type __i) const _NOEXCEPT;
749 _LIBCPP_HIDE_FROM_ABI reference at(size_type __i);
750 _LIBCPP_HIDE_FROM_ABI const_reference at(size_type __i) const;
751 _LIBCPP_HIDE_FROM_ABI reference front() _NOEXCEPT;
752 _LIBCPP_HIDE_FROM_ABI const_reference front() const _NOEXCEPT;
753 _LIBCPP_HIDE_FROM_ABI reference back() _NOEXCEPT;
754 _LIBCPP_HIDE_FROM_ABI const_reference back() const _NOEXCEPT;
756 // 23.2.2.3 modifiers:
757 _LIBCPP_HIDE_FROM_ABI void push_front(const value_type& __v);
758 _LIBCPP_HIDE_FROM_ABI void push_back(const value_type& __v);
759 #ifndef _LIBCPP_CXX03_LANG
760 # if _LIBCPP_STD_VER >= 17
761 template <class... _Args>
762 _LIBCPP_HIDE_FROM_ABI reference emplace_front(_Args&&... __args);
763 template <class... _Args>
764 _LIBCPP_HIDE_FROM_ABI reference emplace_back(_Args&&... __args);
766 template <class... _Args>
767 _LIBCPP_HIDE_FROM_ABI void emplace_front(_Args&&... __args);
768 template <class... _Args>
769 _LIBCPP_HIDE_FROM_ABI void emplace_back(_Args&&... __args);
771 template <class... _Args>
772 _LIBCPP_HIDE_FROM_ABI iterator emplace(const_iterator __p, _Args&&... __args);
774 _LIBCPP_HIDE_FROM_ABI void push_front(value_type&& __v);
775 _LIBCPP_HIDE_FROM_ABI void push_back(value_type&& __v);
777 # if _LIBCPP_STD_VER >= 23
778 template <_ContainerCompatibleRange<_Tp> _Range>
779 _LIBCPP_HIDE_FROM_ABI void prepend_range(_Range&& __range) {
780 insert_range(begin(), std::forward<_Range>(__range));
783 template <_ContainerCompatibleRange<_Tp> _Range>
784 _LIBCPP_HIDE_FROM_ABI void append_range(_Range&& __range) {
785 insert_range(end(), std::forward<_Range>(__range));
789 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, value_type&& __v);
791 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, initializer_list<value_type> __il) {
792 return insert(__p, __il.begin(), __il.end());
794 #endif // _LIBCPP_CXX03_LANG
795 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, const value_type& __v);
796 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, size_type __n, const value_type& __v);
797 template <class _InputIter, __enable_if_t<__has_exactly_input_iterator_category<_InputIter>::value, int> = 0>
798 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, _InputIter __f, _InputIter __l);
799 template <class _ForwardIterator,
800 __enable_if_t<__has_exactly_forward_iterator_category<_ForwardIterator>::value, int> = 0>
801 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l);
802 template <class _BiIter, __enable_if_t<__has_bidirectional_iterator_category<_BiIter>::value, int> = 0>
803 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, _BiIter __f, _BiIter __l);
805 #if _LIBCPP_STD_VER >= 23
806 template <_ContainerCompatibleRange<_Tp> _Range>
807 _LIBCPP_HIDE_FROM_ABI iterator insert_range(const_iterator __position, _Range&& __range) {
808 if constexpr (ranges::bidirectional_range<_Range>) {
809 auto __n = static_cast<size_type>(ranges::distance(__range));
810 return __insert_bidirectional(__position, ranges::begin(__range), ranges::end(__range), __n);
812 } else if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
813 auto __n = static_cast<size_type>(ranges::distance(__range));
814 return __insert_with_size(__position, ranges::begin(__range), __n);
817 return __insert_with_sentinel(__position, ranges::begin(__range), ranges::end(__range));
822 _LIBCPP_HIDE_FROM_ABI void pop_front();
823 _LIBCPP_HIDE_FROM_ABI void pop_back();
824 _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __p);
825 _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __f, const_iterator __l);
827 _LIBCPP_HIDE_FROM_ABI void swap(deque& __c)
828 #if _LIBCPP_STD_VER >= 14
831 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<allocator_type>);
833 _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT;
835 _LIBCPP_HIDE_FROM_ABI bool __invariants() const {
836 if (!__map_.__invariants())
838 if (__map_.size() >= size_type(-1) / __block_size)
840 for (__map_const_iterator __i = __map_.begin(), __e = __map_.end(); __i != __e; ++__i)
843 if (__map_.size() != 0) {
844 if (size() >= __map_.size() * __block_size)
846 if (__start_ >= __map_.size() * __block_size - size())
857 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(deque& __c)
858 _NOEXCEPT_(!__alloc_traits::propagate_on_container_move_assignment::value ||
859 is_nothrow_move_assignable<allocator_type>::value) {
860 __move_assign_alloc(__c, integral_constant<bool, __alloc_traits::propagate_on_container_move_assignment::value>());
863 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(deque& __c, true_type)
864 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) {
865 __alloc() = std::move(__c.__alloc());
868 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(deque&, false_type) _NOEXCEPT {}
870 _LIBCPP_HIDE_FROM_ABI void __move_assign(deque& __c)
871 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value&&
872 is_nothrow_move_assignable<allocator_type>::value) {
873 __map_ = std::move(__c.__map_);
874 __start_ = __c.__start_;
875 __size() = __c.size();
876 __move_assign_alloc(__c);
877 __c.__start_ = __c.__size() = 0;
880 _LIBCPP_HIDE_FROM_ABI static size_type __recommend_blocks(size_type __n) {
881 return __n / __block_size + (__n % __block_size != 0);
883 _LIBCPP_HIDE_FROM_ABI size_type __capacity() const {
884 return __map_.size() == 0 ? 0 : __map_.size() * __block_size - 1;
886 _LIBCPP_HIDE_FROM_ABI size_type __block_count() const { return __map_.size(); }
888 _LIBCPP_HIDE_FROM_ABI size_type __front_spare() const { return __start_; }
889 _LIBCPP_HIDE_FROM_ABI size_type __front_spare_blocks() const { return __front_spare() / __block_size; }
890 _LIBCPP_HIDE_FROM_ABI size_type __back_spare() const { return __capacity() - (__start_ + size()); }
891 _LIBCPP_HIDE_FROM_ABI size_type __back_spare_blocks() const { return __back_spare() / __block_size; }
894 enum __asan_annotation_type { __asan_unposion, __asan_poison };
896 enum __asan_annotation_place {
901 _LIBCPP_HIDE_FROM_ABI void __annotate_from_to(
904 __asan_annotation_type __annotation_type,
905 __asan_annotation_place __place) const _NOEXCEPT {
908 (void)__annotation_type;
910 #ifndef _LIBCPP_HAS_NO_ASAN
911 // __beg - index of the first item to annotate
912 // __end - index behind the last item to annotate (so last item + 1)
913 // __annotation_type - __asan_unposion or __asan_poison
914 // __place - __asan_front_moved or __asan_back_moved
915 // Note: All indexes in __map_
918 // __annotations_beg_map - first chunk which annotations we want to modify
919 // __annotations_end_map - last chunk which annotations we want to modify
920 // NOTE: if __end % __block_size == 0, __annotations_end_map points at the next block, which may not exist
921 __map_const_iterator __annotations_beg_map = __map_.begin() + __beg / __block_size;
922 __map_const_iterator __annotations_end_map = __map_.begin() + __end / __block_size;
924 bool const __poisoning = __annotation_type == __asan_poison;
925 // __old_c_beg_index - index of the first element in old container
926 // __old_c_end_index - index of the end of old container (last + 1)
927 // Note: may be outside the area we are annotating
928 size_t __old_c_beg_index = (__poisoning && __place == __asan_front_moved) ? __beg : __start_;
929 size_t __old_c_end_index = (__poisoning && __place == __asan_back_moved) ? __end : __start_ + size();
930 bool const __front = __place == __asan_front_moved;
932 if (__poisoning && empty()) {
933 // Special case: we shouldn't trust __start_
934 __old_c_beg_index = __beg;
935 __old_c_end_index = __end;
937 // __old_c_beg_map - memory block (chunk) with first element
938 // __old_c_end_map - memory block (chunk) with end of old container
939 // Note: if __old_c_end_index % __block_size == 0, __old_c_end_map points at the next block,
940 // which may not exist
941 __map_const_iterator __old_c_beg_map = __map_.begin() + __old_c_beg_index / __block_size;
942 __map_const_iterator __old_c_end_map = __map_.begin() + __old_c_end_index / __block_size;
944 // One edge (front/end) of the container was moved and one was not modified.
945 // __new_edge_index - index of new edge
946 // __new_edge_map - memory block (chunk) with new edge, it always equals to
947 // __annotations_beg_map or __annotations_end_map
948 // __old_edge_map - memory block (chunk) with old edge, it always equals to
949 // __old_c_beg_map or __old_c_end_map
950 size_t __new_edge_index = (__poisoning ^ __front) ? __beg : __end;
951 __map_const_iterator __new_edge_map = __map_.begin() + __new_edge_index / __block_size;
952 __map_const_iterator __old_edge_map = __front ? __old_c_end_map : __old_c_beg_map;
954 // We iterate over map pointers (chunks) and fully poison all memory blocks between the first and the last.
955 // First and last chunk may be partially poisoned.
956 // __annotate_end_map may point at not existing chunk, therefore we have to have a check for it.
957 for (__map_const_iterator __map_it = __annotations_beg_map; __map_it <= __annotations_end_map; ++__map_it) {
958 if (__map_it == __annotations_end_map && __end % __block_size == 0)
959 // Chunk may not exist, but nothing to do here anyway
962 // The beginning and the end of the current memory block
963 const void* __mem_beg = std::__to_address(*__map_it);
964 const void* __mem_end = std::__to_address(*__map_it + __block_size);
966 // The beginning of memory-in-use in the memory block before container modification
967 const void* __old_beg =
968 (__map_it == __old_c_beg_map) ? std::__to_address(*__map_it + (__old_c_beg_index % __block_size)) : __mem_beg;
970 // The end of memory-in-use in the memory block before container modification
971 const void* __old_end;
972 if (__map_it < __old_c_beg_map || __map_it > __old_c_end_map || (!__poisoning && empty()))
973 __old_end = __old_beg;
975 __old_end = (__map_it == __old_c_end_map)
976 ? std::__to_address(*__map_it + (__old_c_end_index % __block_size))
979 // New edge of the container in current memory block
980 // If the edge is in a different chunk it points on corresponding end of the memory block
981 const void* __new_edge;
982 if (__map_it == __new_edge_map)
983 __new_edge = std::__to_address(*__map_it + (__new_edge_index % __block_size));
985 __new_edge = (__poisoning ^ __front) ? __mem_beg : __mem_end;
987 // Not modified edge of the container
988 // If the edge is in a different chunk it points on corresponding end of the memory block
989 const void* __old_edge;
990 if (__map_it == __old_edge_map)
991 __old_edge = __front ? __old_end : __old_beg;
993 __old_edge = __front ? __mem_end : __mem_beg;
995 // __new_beg - the beginning of memory-in-use in the memory block after container modification
996 // __new_end - the end of memory-in-use in the memory block after container modification
997 const void* __new_beg = __front ? __new_edge : __old_edge;
998 const void* __new_end = __front ? __old_edge : __new_edge;
1000 std::__annotate_double_ended_contiguous_container<_Allocator>(
1001 __mem_beg, __mem_end, __old_beg, __old_end, __new_beg, __new_end);
1003 #endif // !_LIBCPP_HAS_NO_ASAN
1006 _LIBCPP_HIDE_FROM_ABI void __annotate_new(size_type __current_size) const _NOEXCEPT {
1007 (void)__current_size;
1008 #ifndef _LIBCPP_HAS_NO_ASAN
1009 if (__current_size == 0)
1010 __annotate_from_to(0, __map_.size() * __block_size, __asan_poison, __asan_back_moved);
1012 __annotate_from_to(0, __start_, __asan_poison, __asan_front_moved);
1013 __annotate_from_to(__start_ + __current_size, __map_.size() * __block_size, __asan_poison, __asan_back_moved);
1018 _LIBCPP_HIDE_FROM_ABI void __annotate_delete() const _NOEXCEPT {
1019 #ifndef _LIBCPP_HAS_NO_ASAN
1021 for (size_t __i = 0; __i < __map_.size(); ++__i) {
1022 __annotate_whole_block(__i, __asan_unposion);
1025 __annotate_from_to(0, __start_, __asan_unposion, __asan_front_moved);
1026 __annotate_from_to(__start_ + size(), __map_.size() * __block_size, __asan_unposion, __asan_back_moved);
1031 _LIBCPP_HIDE_FROM_ABI void __annotate_increase_front(size_type __n) const _NOEXCEPT {
1033 #ifndef _LIBCPP_HAS_NO_ASAN
1034 __annotate_from_to(__start_ - __n, __start_, __asan_unposion, __asan_front_moved);
1038 _LIBCPP_HIDE_FROM_ABI void __annotate_increase_back(size_type __n) const _NOEXCEPT {
1040 #ifndef _LIBCPP_HAS_NO_ASAN
1041 __annotate_from_to(__start_ + size(), __start_ + size() + __n, __asan_unposion, __asan_back_moved);
1045 _LIBCPP_HIDE_FROM_ABI void __annotate_shrink_front(size_type __old_size, size_type __old_start) const _NOEXCEPT {
1048 #ifndef _LIBCPP_HAS_NO_ASAN
1049 __annotate_from_to(__old_start, __old_start + (__old_size - size()), __asan_poison, __asan_front_moved);
1053 _LIBCPP_HIDE_FROM_ABI void __annotate_shrink_back(size_type __old_size, size_type __old_start) const _NOEXCEPT {
1056 #ifndef _LIBCPP_HAS_NO_ASAN
1057 __annotate_from_to(__old_start + size(), __old_start + __old_size, __asan_poison, __asan_back_moved);
1061 _LIBCPP_HIDE_FROM_ABI void __annotate_poison_block(const void* __beginning, const void* __end) const _NOEXCEPT {
1062 std::__annotate_double_ended_contiguous_container<_Allocator>(__beginning, __end, __beginning, __end, __end, __end);
1065 _LIBCPP_HIDE_FROM_ABI void
1066 __annotate_whole_block(size_t __block_index, __asan_annotation_type __annotation_type) const _NOEXCEPT {
1067 (void)__block_index;
1068 (void)__annotation_type;
1069 #ifndef _LIBCPP_HAS_NO_ASAN
1070 __map_const_iterator __block_it = __map_.begin() + __block_index;
1071 const void* __block_start = std::__to_address(*__block_it);
1072 const void* __block_end = std::__to_address(*__block_it + __block_size);
1074 if (__annotation_type == __asan_poison)
1075 __annotate_poison_block(__block_start, __block_end);
1077 std::__annotate_double_ended_contiguous_container<_Allocator>(
1078 __block_start, __block_end, __block_start, __block_start, __block_start, __block_end);
1082 #if !defined(_LIBCPP_HAS_NO_ASAN)
1085 _LIBCPP_HIDE_FROM_ABI bool __verify_asan_annotations() const _NOEXCEPT {
1086 // This function tests deque object annotations.
1088 for (__map_const_iterator __it = __map_.begin(); __it != __map_.end(); ++__it) {
1089 if (!__sanitizer_verify_double_ended_contiguous_container(
1090 std::__to_address(*__it),
1091 std::__to_address(*__it),
1092 std::__to_address(*__it),
1093 std::__to_address(*__it + __block_size)))
1100 size_type __end = __start_ + size();
1101 __map_const_iterator __first_mp = __map_.begin() + __start_ / __block_size;
1102 __map_const_iterator __last_mp = __map_.begin() + (__end - 1) / __block_size;
1104 // Pointers to first and after last elements
1105 // Those can be in different deque blocks
1106 const void* __p_beg = std::__to_address(*__first_mp + (__start_ % __block_size));
1107 const void* __p_end =
1108 std::__to_address(*__last_mp + ((__end % __block_size == 0) ? __block_size : __end % __block_size));
1110 for (__map_const_iterator __it = __map_.begin(); __it != __map_.end(); ++__it) {
1111 // Go over all blocks, find the place we are in and verify its annotations
1112 // Note that __p_end points *behind* the last item.
1114 // - blocks before the first block with container elements
1115 // - first block with items
1116 // - last block with items
1117 // - blocks after last block with ciontainer elements
1119 // Is the block before or after deque blocks that contain elements?
1120 if (__it < __first_mp || __it > __last_mp) {
1121 if (!__sanitizer_verify_double_ended_contiguous_container(
1122 std::__to_address(*__it),
1123 std::__to_address(*__it),
1124 std::__to_address(*__it),
1125 std::__to_address(*__it + __block_size)))
1128 const void* __containers_buffer_beg = (__it == __first_mp) ? __p_beg : (const void*)std::__to_address(*__it);
1129 const void* __containers_buffer_end =
1130 (__it == __last_mp) ? __p_end : (const void*)std::__to_address(*__it + __block_size);
1131 if (!__sanitizer_verify_double_ended_contiguous_container(
1132 std::__to_address(*__it),
1133 __containers_buffer_beg,
1134 __containers_buffer_end,
1135 std::__to_address(*__it + __block_size))) {
1144 #endif // _LIBCPP_VERIFY_ASAN_DEQUE_ANNOTATIONS
1145 _LIBCPP_HIDE_FROM_ABI bool __maybe_remove_front_spare(bool __keep_one = true) {
1146 if (__front_spare_blocks() >= 2 || (!__keep_one && __front_spare_blocks())) {
1147 __annotate_whole_block(0, __asan_unposion);
1148 __alloc_traits::deallocate(__alloc(), __map_.front(), __block_size);
1150 __start_ -= __block_size;
1156 _LIBCPP_HIDE_FROM_ABI bool __maybe_remove_back_spare(bool __keep_one = true) {
1157 if (__back_spare_blocks() >= 2 || (!__keep_one && __back_spare_blocks())) {
1158 __annotate_whole_block(__map_.size() - 1, __asan_unposion);
1159 __alloc_traits::deallocate(__alloc(), __map_.back(), __block_size);
1166 template <class _Iterator, class _Sentinel>
1167 _LIBCPP_HIDE_FROM_ABI void __assign_with_sentinel(_Iterator __f, _Sentinel __l);
1169 template <class _RandomAccessIterator>
1170 _LIBCPP_HIDE_FROM_ABI void __assign_with_size_random_access(_RandomAccessIterator __f, difference_type __n);
1171 template <class _Iterator>
1172 _LIBCPP_HIDE_FROM_ABI void __assign_with_size(_Iterator __f, difference_type __n);
1174 template <class _Iterator, class _Sentinel>
1175 _LIBCPP_HIDE_FROM_ABI iterator __insert_with_sentinel(const_iterator __p, _Iterator __f, _Sentinel __l);
1177 template <class _Iterator>
1178 _LIBCPP_HIDE_FROM_ABI iterator __insert_with_size(const_iterator __p, _Iterator __f, size_type __n);
1180 template <class _BiIter, class _Sentinel>
1181 _LIBCPP_HIDE_FROM_ABI iterator
1182 __insert_bidirectional(const_iterator __p, _BiIter __f, _Sentinel __sent, size_type __n);
1183 template <class _BiIter>
1184 _LIBCPP_HIDE_FROM_ABI iterator __insert_bidirectional(const_iterator __p, _BiIter __f, _BiIter __l, size_type __n);
1186 template <class _InpIter, __enable_if_t<__has_exactly_input_iterator_category<_InpIter>::value, int> = 0>
1187 _LIBCPP_HIDE_FROM_ABI void __append(_InpIter __f, _InpIter __l);
1188 template <class _ForIter, __enable_if_t<__has_forward_iterator_category<_ForIter>::value, int> = 0>
1189 _LIBCPP_HIDE_FROM_ABI void __append(_ForIter __f, _ForIter __l);
1191 template <class _InputIterator>
1192 _LIBCPP_HIDE_FROM_ABI void __append_with_size(_InputIterator __from, size_type __n);
1193 template <class _InputIterator, class _Sentinel>
1194 _LIBCPP_HIDE_FROM_ABI void __append_with_sentinel(_InputIterator __f, _Sentinel __l);
1196 _LIBCPP_HIDE_FROM_ABI void __append(size_type __n);
1197 _LIBCPP_HIDE_FROM_ABI void __append(size_type __n, const value_type& __v);
1198 _LIBCPP_HIDE_FROM_ABI void __erase_to_end(const_iterator __f);
1199 _LIBCPP_HIDE_FROM_ABI void __add_front_capacity();
1200 _LIBCPP_HIDE_FROM_ABI void __add_front_capacity(size_type __n);
1201 _LIBCPP_HIDE_FROM_ABI void __add_back_capacity();
1202 _LIBCPP_HIDE_FROM_ABI void __add_back_capacity(size_type __n);
1203 _LIBCPP_HIDE_FROM_ABI iterator __move_and_check(iterator __f, iterator __l, iterator __r, const_pointer& __vt);
1204 _LIBCPP_HIDE_FROM_ABI iterator
1205 __move_backward_and_check(iterator __f, iterator __l, iterator __r, const_pointer& __vt);
1206 _LIBCPP_HIDE_FROM_ABI void __move_construct_and_check(iterator __f, iterator __l, iterator __r, const_pointer& __vt);
1207 _LIBCPP_HIDE_FROM_ABI void
1208 __move_construct_backward_and_check(iterator __f, iterator __l, iterator __r, const_pointer& __vt);
1210 _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const deque& __c) {
1211 __copy_assign_alloc(__c, integral_constant<bool, __alloc_traits::propagate_on_container_copy_assignment::value>());
1214 _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const deque& __c, true_type) {
1215 if (__alloc() != __c.__alloc()) {
1219 __alloc() = __c.__alloc();
1220 __map_.__alloc() = __c.__map_.__alloc();
1223 _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const deque&, false_type) {}
1225 _LIBCPP_HIDE_FROM_ABI void __move_assign(deque& __c, true_type)
1226 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
1227 _LIBCPP_HIDE_FROM_ABI void __move_assign(deque& __c, false_type);
1230 template <class _Tp, class _Alloc>
1231 _LIBCPP_CONSTEXPR const typename allocator_traits<_Alloc>::difference_type deque<_Tp, _Alloc>::__block_size =
1232 __deque_block_size<value_type, difference_type>::value;
1234 #if _LIBCPP_STD_VER >= 17
1235 template <class _InputIterator,
1236 class _Alloc = allocator<__iter_value_type<_InputIterator>>,
1237 class = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
1238 class = enable_if_t<__is_allocator<_Alloc>::value> >
1239 deque(_InputIterator, _InputIterator) -> deque<__iter_value_type<_InputIterator>, _Alloc>;
1241 template <class _InputIterator,
1243 class = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
1244 class = enable_if_t<__is_allocator<_Alloc>::value> >
1245 deque(_InputIterator, _InputIterator, _Alloc) -> deque<__iter_value_type<_InputIterator>, _Alloc>;
1248 #if _LIBCPP_STD_VER >= 23
1249 template <ranges::input_range _Range,
1250 class _Alloc = allocator<ranges::range_value_t<_Range>>,
1251 class = enable_if_t<__is_allocator<_Alloc>::value> >
1252 deque(from_range_t, _Range&&, _Alloc = _Alloc()) -> deque<ranges::range_value_t<_Range>, _Alloc>;
1255 template <class _Tp, class _Allocator>
1256 deque<_Tp, _Allocator>::deque(size_type __n) : __start_(0), __size_(0, __default_init_tag()) {
1262 #if _LIBCPP_STD_VER >= 14
1263 template <class _Tp, class _Allocator>
1264 deque<_Tp, _Allocator>::deque(size_type __n, const _Allocator& __a)
1265 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a) {
1272 template <class _Tp, class _Allocator>
1273 deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v) : __start_(0), __size_(0, __default_init_tag()) {
1279 template <class _Tp, class _Allocator>
1280 template <class _InputIter, __enable_if_t<__has_input_iterator_category<_InputIter>::value, int> >
1281 deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l) : __start_(0), __size_(0, __default_init_tag()) {
1286 template <class _Tp, class _Allocator>
1287 template <class _InputIter, __enable_if_t<__has_input_iterator_category<_InputIter>::value, int> >
1288 deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l, const allocator_type& __a)
1289 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a) {
1294 template <class _Tp, class _Allocator>
1295 deque<_Tp, _Allocator>::deque(const deque& __c)
1296 : __map_(__pointer_allocator(__alloc_traits::select_on_container_copy_construction(__c.__alloc()))),
1298 __size_(0, __map_.__alloc()) {
1300 __append(__c.begin(), __c.end());
1303 template <class _Tp, class _Allocator>
1304 deque<_Tp, _Allocator>::deque(const deque& __c, const __type_identity_t<allocator_type>& __a)
1305 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a) {
1307 __append(__c.begin(), __c.end());
1310 template <class _Tp, class _Allocator>
1311 deque<_Tp, _Allocator>& deque<_Tp, _Allocator>::operator=(const deque& __c) {
1312 if (this != std::addressof(__c)) {
1313 __copy_assign_alloc(__c);
1314 assign(__c.begin(), __c.end());
1319 #ifndef _LIBCPP_CXX03_LANG
1321 template <class _Tp, class _Allocator>
1322 deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il) : __start_(0), __size_(0, __default_init_tag()) {
1324 __append(__il.begin(), __il.end());
1327 template <class _Tp, class _Allocator>
1328 deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il, const allocator_type& __a)
1329 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a) {
1331 __append(__il.begin(), __il.end());
1334 template <class _Tp, class _Allocator>
1335 inline deque<_Tp, _Allocator>::deque(deque&& __c) noexcept(is_nothrow_move_constructible<allocator_type>::value)
1336 : __map_(std::move(__c.__map_)), __start_(std::move(__c.__start_)), __size_(std::move(__c.__size_)) {
1341 template <class _Tp, class _Allocator>
1342 inline deque<_Tp, _Allocator>::deque(deque&& __c, const __type_identity_t<allocator_type>& __a)
1343 : __map_(std::move(__c.__map_), __pointer_allocator(__a)),
1344 __start_(std::move(__c.__start_)),
1345 __size_(std::move(__c.__size()), __a) {
1346 if (__a == __c.__alloc()) {
1353 typedef move_iterator<iterator> _Ip;
1354 assign(_Ip(__c.begin()), _Ip(__c.end()));
1358 template <class _Tp, class _Allocator>
1359 inline deque<_Tp, _Allocator>& deque<_Tp, _Allocator>::operator=(deque&& __c) noexcept(
1360 __alloc_traits::propagate_on_container_move_assignment::value &&
1361 is_nothrow_move_assignable<allocator_type>::value) {
1362 __move_assign(__c, integral_constant<bool, __alloc_traits::propagate_on_container_move_assignment::value>());
1366 template <class _Tp, class _Allocator>
1367 void deque<_Tp, _Allocator>::__move_assign(deque& __c, false_type) {
1368 if (__alloc() != __c.__alloc()) {
1369 typedef move_iterator<iterator> _Ip;
1370 assign(_Ip(__c.begin()), _Ip(__c.end()));
1372 __move_assign(__c, true_type());
1375 template <class _Tp, class _Allocator>
1376 void deque<_Tp, _Allocator>::__move_assign(deque& __c,
1377 true_type) noexcept(is_nothrow_move_assignable<allocator_type>::value) {
1383 #endif // _LIBCPP_CXX03_LANG
1385 template <class _Tp, class _Allocator>
1386 template <class _InputIter,
1387 __enable_if_t<__has_input_iterator_category<_InputIter>::value &&
1388 !__has_random_access_iterator_category<_InputIter>::value,
1390 void deque<_Tp, _Allocator>::assign(_InputIter __f, _InputIter __l) {
1391 __assign_with_sentinel(__f, __l);
1394 template <class _Tp, class _Allocator>
1395 template <class _Iterator, class _Sentinel>
1396 _LIBCPP_HIDE_FROM_ABI void deque<_Tp, _Allocator>::__assign_with_sentinel(_Iterator __f, _Sentinel __l) {
1397 iterator __i = begin();
1398 iterator __e = end();
1399 for (; __f != __l && __i != __e; ++__f, (void)++__i)
1402 __append_with_sentinel(std::move(__f), std::move(__l));
1404 __erase_to_end(__i);
1407 template <class _Tp, class _Allocator>
1408 template <class _RAIter, __enable_if_t<__has_random_access_iterator_category<_RAIter>::value, int> >
1409 void deque<_Tp, _Allocator>::assign(_RAIter __f, _RAIter __l) {
1410 __assign_with_size_random_access(__f, __l - __f);
1413 template <class _Tp, class _Allocator>
1414 template <class _RandomAccessIterator>
1415 _LIBCPP_HIDE_FROM_ABI void
1416 deque<_Tp, _Allocator>::__assign_with_size_random_access(_RandomAccessIterator __f, difference_type __n) {
1417 if (static_cast<size_type>(__n) > size()) {
1418 auto __l = __f + size();
1419 std::copy(__f, __l, begin());
1420 __append_with_size(__l, __n - size());
1422 __erase_to_end(std::copy_n(__f, __n, begin()));
1425 template <class _Tp, class _Allocator>
1426 template <class _Iterator>
1427 _LIBCPP_HIDE_FROM_ABI void deque<_Tp, _Allocator>::__assign_with_size(_Iterator __f, difference_type __n) {
1428 if (static_cast<size_type>(__n) > size()) {
1429 auto __added_size = __n - size();
1432 for (auto __count = size(); __count != 0; --__count) {
1436 __append_with_size(__f, __added_size);
1439 __erase_to_end(std::copy_n(__f, __n, begin()));
1443 template <class _Tp, class _Allocator>
1444 void deque<_Tp, _Allocator>::assign(size_type __n, const value_type& __v) {
1446 std::fill_n(begin(), size(), __v);
1450 __erase_to_end(std::fill_n(begin(), __n, __v));
1453 template <class _Tp, class _Allocator>
1454 inline _Allocator deque<_Tp, _Allocator>::get_allocator() const _NOEXCEPT {
1458 template <class _Tp, class _Allocator>
1459 void deque<_Tp, _Allocator>::resize(size_type __n) {
1461 __append(__n - size());
1462 else if (__n < size())
1463 __erase_to_end(begin() + __n);
1466 template <class _Tp, class _Allocator>
1467 void deque<_Tp, _Allocator>::resize(size_type __n, const value_type& __v) {
1469 __append(__n - size(), __v);
1470 else if (__n < size())
1471 __erase_to_end(begin() + __n);
1474 template <class _Tp, class _Allocator>
1475 void deque<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT {
1476 allocator_type& __a = __alloc();
1478 __annotate_delete();
1479 while (__map_.size() > 0) {
1480 __alloc_traits::deallocate(__a, __map_.back(), __block_size);
1485 __maybe_remove_front_spare(/*__keep_one=*/false);
1486 __maybe_remove_back_spare(/*__keep_one=*/false);
1488 __map_.shrink_to_fit();
1491 template <class _Tp, class _Allocator>
1492 inline typename deque<_Tp, _Allocator>::reference deque<_Tp, _Allocator>::operator[](size_type __i) _NOEXCEPT {
1493 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__i < size(), "deque::operator[] index out of bounds");
1494 size_type __p = __start_ + __i;
1495 return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
1498 template <class _Tp, class _Allocator>
1499 inline typename deque<_Tp, _Allocator>::const_reference
1500 deque<_Tp, _Allocator>::operator[](size_type __i) const _NOEXCEPT {
1501 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__i < size(), "deque::operator[] index out of bounds");
1502 size_type __p = __start_ + __i;
1503 return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
1506 template <class _Tp, class _Allocator>
1507 inline typename deque<_Tp, _Allocator>::reference deque<_Tp, _Allocator>::at(size_type __i) {
1509 std::__throw_out_of_range("deque");
1510 size_type __p = __start_ + __i;
1511 return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
1514 template <class _Tp, class _Allocator>
1515 inline typename deque<_Tp, _Allocator>::const_reference deque<_Tp, _Allocator>::at(size_type __i) const {
1517 std::__throw_out_of_range("deque");
1518 size_type __p = __start_ + __i;
1519 return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
1522 template <class _Tp, class _Allocator>
1523 inline typename deque<_Tp, _Allocator>::reference deque<_Tp, _Allocator>::front() _NOEXCEPT {
1524 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "deque::front called on an empty deque");
1525 return *(*(__map_.begin() + __start_ / __block_size) + __start_ % __block_size);
1528 template <class _Tp, class _Allocator>
1529 inline typename deque<_Tp, _Allocator>::const_reference deque<_Tp, _Allocator>::front() const _NOEXCEPT {
1530 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "deque::front called on an empty deque");
1531 return *(*(__map_.begin() + __start_ / __block_size) + __start_ % __block_size);
1534 template <class _Tp, class _Allocator>
1535 inline typename deque<_Tp, _Allocator>::reference deque<_Tp, _Allocator>::back() _NOEXCEPT {
1536 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "deque::back called on an empty deque");
1537 size_type __p = size() + __start_ - 1;
1538 return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
1541 template <class _Tp, class _Allocator>
1542 inline typename deque<_Tp, _Allocator>::const_reference deque<_Tp, _Allocator>::back() const _NOEXCEPT {
1543 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "deque::back called on an empty deque");
1544 size_type __p = size() + __start_ - 1;
1545 return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
1548 template <class _Tp, class _Allocator>
1549 void deque<_Tp, _Allocator>::push_back(const value_type& __v) {
1550 allocator_type& __a = __alloc();
1551 if (__back_spare() == 0)
1552 __add_back_capacity();
1553 // __back_spare() >= 1
1554 __annotate_increase_back(1);
1555 __alloc_traits::construct(__a, std::addressof(*end()), __v);
1559 template <class _Tp, class _Allocator>
1560 void deque<_Tp, _Allocator>::push_front(const value_type& __v) {
1561 allocator_type& __a = __alloc();
1562 if (__front_spare() == 0)
1563 __add_front_capacity();
1564 // __front_spare() >= 1
1565 __annotate_increase_front(1);
1566 __alloc_traits::construct(__a, std::addressof(*--begin()), __v);
1571 #ifndef _LIBCPP_CXX03_LANG
1572 template <class _Tp, class _Allocator>
1573 void deque<_Tp, _Allocator>::push_back(value_type&& __v) {
1574 allocator_type& __a = __alloc();
1575 if (__back_spare() == 0)
1576 __add_back_capacity();
1577 // __back_spare() >= 1
1578 __annotate_increase_back(1);
1579 __alloc_traits::construct(__a, std::addressof(*end()), std::move(__v));
1583 template <class _Tp, class _Allocator>
1584 template <class... _Args>
1585 # if _LIBCPP_STD_VER >= 17
1586 typename deque<_Tp, _Allocator>::reference
1590 deque<_Tp, _Allocator>::emplace_back(_Args&&... __args) {
1591 allocator_type& __a = __alloc();
1592 if (__back_spare() == 0)
1593 __add_back_capacity();
1594 // __back_spare() >= 1
1595 __annotate_increase_back(1);
1596 __alloc_traits::construct(__a, std::addressof(*end()), std::forward<_Args>(__args)...);
1598 # if _LIBCPP_STD_VER >= 17
1603 template <class _Tp, class _Allocator>
1604 void deque<_Tp, _Allocator>::push_front(value_type&& __v) {
1605 allocator_type& __a = __alloc();
1606 if (__front_spare() == 0)
1607 __add_front_capacity();
1608 // __front_spare() >= 1
1609 __annotate_increase_front(1);
1610 __alloc_traits::construct(__a, std::addressof(*--begin()), std::move(__v));
1615 template <class _Tp, class _Allocator>
1616 template <class... _Args>
1617 # if _LIBCPP_STD_VER >= 17
1618 typename deque<_Tp, _Allocator>::reference
1622 deque<_Tp, _Allocator>::emplace_front(_Args&&... __args) {
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::forward<_Args>(__args)...);
1631 # if _LIBCPP_STD_VER >= 17
1636 template <class _Tp, class _Allocator>
1637 typename deque<_Tp, _Allocator>::iterator deque<_Tp, _Allocator>::insert(const_iterator __p, value_type&& __v) {
1638 size_type __pos = __p - begin();
1639 size_type __to_end = size() - __pos;
1640 allocator_type& __a = __alloc();
1641 if (__pos < __to_end) { // insert by shifting things backward
1642 if (__front_spare() == 0)
1643 __add_front_capacity();
1644 // __front_spare() >= 1
1645 __annotate_increase_front(1);
1647 __alloc_traits::construct(__a, std::addressof(*--begin()), std::move(__v));
1651 iterator __b = begin();
1652 iterator __bm1 = std::prev(__b);
1653 __alloc_traits::construct(__a, std::addressof(*__bm1), std::move(*__b));
1657 __b = std::move(std::next(__b), __b + __pos, __b);
1658 *__b = std::move(__v);
1660 } else { // insert by shifting things forward
1661 if (__back_spare() == 0)
1662 __add_back_capacity();
1663 // __back_capacity >= 1
1664 __annotate_increase_back(1);
1665 size_type __de = size() - __pos;
1667 __alloc_traits::construct(__a, std::addressof(*end()), std::move(__v));
1670 iterator __e = end();
1671 iterator __em1 = std::prev(__e);
1672 __alloc_traits::construct(__a, std::addressof(*__e), std::move(*__em1));
1675 __e = std::move_backward(__e - __de, __em1, __e);
1676 *--__e = std::move(__v);
1679 return begin() + __pos;
1682 template <class _Tp, class _Allocator>
1683 template <class... _Args>
1684 typename deque<_Tp, _Allocator>::iterator deque<_Tp, _Allocator>::emplace(const_iterator __p, _Args&&... __args) {
1685 size_type __pos = __p - begin();
1686 size_type __to_end = size() - __pos;
1687 allocator_type& __a = __alloc();
1688 if (__pos < __to_end) { // insert by shifting things backward
1689 if (__front_spare() == 0)
1690 __add_front_capacity();
1691 // __front_spare() >= 1
1692 __annotate_increase_front(1);
1694 __alloc_traits::construct(__a, std::addressof(*--begin()), std::forward<_Args>(__args)...);
1698 __temp_value<value_type, _Allocator> __tmp(__alloc(), std::forward<_Args>(__args)...);
1699 iterator __b = begin();
1700 iterator __bm1 = std::prev(__b);
1701 __alloc_traits::construct(__a, std::addressof(*__bm1), std::move(*__b));
1705 __b = std::move(std::next(__b), __b + __pos, __b);
1706 *__b = std::move(__tmp.get());
1708 } else { // insert by shifting things forward
1709 if (__back_spare() == 0)
1710 __add_back_capacity();
1711 // __back_capacity >= 1
1712 __annotate_increase_back(1);
1713 size_type __de = size() - __pos;
1715 __alloc_traits::construct(__a, std::addressof(*end()), std::forward<_Args>(__args)...);
1718 __temp_value<value_type, _Allocator> __tmp(__alloc(), std::forward<_Args>(__args)...);
1719 iterator __e = end();
1720 iterator __em1 = std::prev(__e);
1721 __alloc_traits::construct(__a, std::addressof(*__e), std::move(*__em1));
1724 __e = std::move_backward(__e - __de, __em1, __e);
1725 *--__e = std::move(__tmp.get());
1728 return begin() + __pos;
1731 #endif // _LIBCPP_CXX03_LANG
1733 template <class _Tp, class _Allocator>
1734 typename deque<_Tp, _Allocator>::iterator deque<_Tp, _Allocator>::insert(const_iterator __p, const value_type& __v) {
1735 size_type __pos = __p - begin();
1736 size_type __to_end = size() - __pos;
1737 allocator_type& __a = __alloc();
1738 if (__pos < __to_end) { // insert by shifting things backward
1739 if (__front_spare() == 0)
1740 __add_front_capacity();
1741 // __front_spare() >= 1
1742 __annotate_increase_front(1);
1744 __alloc_traits::construct(__a, std::addressof(*--begin()), __v);
1748 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1749 iterator __b = begin();
1750 iterator __bm1 = std::prev(__b);
1751 if (__vt == pointer_traits<const_pointer>::pointer_to(*__b))
1752 __vt = pointer_traits<const_pointer>::pointer_to(*__bm1);
1753 __alloc_traits::construct(__a, std::addressof(*__bm1), std::move(*__b));
1757 __b = __move_and_check(std::next(__b), __b + __pos, __b, __vt);
1760 } else { // insert by shifting things forward
1761 if (__back_spare() == 0)
1762 __add_back_capacity();
1763 // __back_capacity >= 1
1764 __annotate_increase_back(1);
1765 size_type __de = size() - __pos;
1767 __alloc_traits::construct(__a, std::addressof(*end()), __v);
1770 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1771 iterator __e = end();
1772 iterator __em1 = std::prev(__e);
1773 if (__vt == pointer_traits<const_pointer>::pointer_to(*__em1))
1774 __vt = pointer_traits<const_pointer>::pointer_to(*__e);
1775 __alloc_traits::construct(__a, std::addressof(*__e), std::move(*__em1));
1778 __e = __move_backward_and_check(__e - __de, __em1, __e, __vt);
1782 return begin() + __pos;
1785 template <class _Tp, class _Allocator>
1786 typename deque<_Tp, _Allocator>::iterator
1787 deque<_Tp, _Allocator>::insert(const_iterator __p, size_type __n, const value_type& __v) {
1788 size_type __pos = __p - begin();
1789 size_type __to_end = __size() - __pos;
1790 allocator_type& __a = __alloc();
1791 if (__pos < __to_end) { // insert by shifting things backward
1792 if (__n > __front_spare())
1793 __add_front_capacity(__n - __front_spare());
1794 // __n <= __front_spare()
1795 __annotate_increase_front(__n);
1796 iterator __old_begin = begin();
1797 iterator __i = __old_begin;
1799 for (size_type __m = __n - __pos; __m; --__m, --__start_, ++__size())
1800 __alloc_traits::construct(__a, std::addressof(*--__i), __v);
1804 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1805 iterator __obn = __old_begin + __n;
1806 __move_construct_backward_and_check(__old_begin, __obn, __i, __vt);
1808 __old_begin = __move_and_check(__obn, __old_begin + __pos, __old_begin, __vt);
1809 std::fill_n(__old_begin, __n, *__vt);
1811 } else { // insert by shifting things forward
1812 size_type __back_capacity = __back_spare();
1813 if (__n > __back_capacity)
1814 __add_back_capacity(__n - __back_capacity);
1815 // __n <= __back_capacity
1816 __annotate_increase_back(__n);
1817 iterator __old_end = end();
1818 iterator __i = __old_end;
1819 size_type __de = size() - __pos;
1821 for (size_type __m = __n - __de; __m; --__m, (void)++__i, ++__size())
1822 __alloc_traits::construct(__a, std::addressof(*__i), __v);
1826 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1827 iterator __oen = __old_end - __n;
1828 __move_construct_and_check(__oen, __old_end, __i, __vt);
1830 __old_end = __move_backward_and_check(__old_end - __de, __oen, __old_end, __vt);
1831 std::fill_n(__old_end - __n, __n, *__vt);
1834 return begin() + __pos;
1837 template <class _Tp, class _Allocator>
1838 template <class _InputIter, __enable_if_t<__has_exactly_input_iterator_category<_InputIter>::value, int> >
1839 typename deque<_Tp, _Allocator>::iterator
1840 deque<_Tp, _Allocator>::insert(const_iterator __p, _InputIter __f, _InputIter __l) {
1841 return __insert_with_sentinel(__p, __f, __l);
1844 template <class _Tp, class _Allocator>
1845 template <class _Iterator, class _Sentinel>
1846 _LIBCPP_HIDE_FROM_ABI typename deque<_Tp, _Allocator>::iterator
1847 deque<_Tp, _Allocator>::__insert_with_sentinel(const_iterator __p, _Iterator __f, _Sentinel __l) {
1848 __split_buffer<value_type, allocator_type&> __buf(__alloc());
1849 __buf.__construct_at_end_with_sentinel(std::move(__f), std::move(__l));
1850 typedef typename __split_buffer<value_type, allocator_type&>::iterator __bi;
1851 return insert(__p, move_iterator<__bi>(__buf.begin()), move_iterator<__bi>(__buf.end()));
1854 template <class _Tp, class _Allocator>
1855 template <class _ForwardIterator, __enable_if_t<__has_exactly_forward_iterator_category<_ForwardIterator>::value, int> >
1856 typename deque<_Tp, _Allocator>::iterator
1857 deque<_Tp, _Allocator>::insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l) {
1858 return __insert_with_size(__p, __f, std::distance(__f, __l));
1861 template <class _Tp, class _Allocator>
1862 template <class _Iterator>
1863 _LIBCPP_HIDE_FROM_ABI typename deque<_Tp, _Allocator>::iterator
1864 deque<_Tp, _Allocator>::__insert_with_size(const_iterator __p, _Iterator __f, size_type __n) {
1865 __split_buffer<value_type, allocator_type&> __buf(__n, 0, __alloc());
1866 __buf.__construct_at_end_with_size(__f, __n);
1867 typedef typename __split_buffer<value_type, allocator_type&>::iterator __fwd;
1868 return insert(__p, move_iterator<__fwd>(__buf.begin()), move_iterator<__fwd>(__buf.end()));
1871 template <class _Tp, class _Allocator>
1872 template <class _BiIter, __enable_if_t<__has_bidirectional_iterator_category<_BiIter>::value, int> >
1873 typename deque<_Tp, _Allocator>::iterator deque<_Tp, _Allocator>::insert(const_iterator __p, _BiIter __f, _BiIter __l) {
1874 return __insert_bidirectional(__p, __f, __l, std::distance(__f, __l));
1877 template <class _Tp, class _Allocator>
1878 template <class _BiIter, class _Sentinel>
1879 _LIBCPP_HIDE_FROM_ABI typename deque<_Tp, _Allocator>::iterator
1880 deque<_Tp, _Allocator>::__insert_bidirectional(const_iterator __p, _BiIter __f, _Sentinel, size_type __n) {
1881 return __insert_bidirectional(__p, __f, std::next(__f, __n), __n);
1884 template <class _Tp, class _Allocator>
1885 template <class _BiIter>
1886 _LIBCPP_HIDE_FROM_ABI typename deque<_Tp, _Allocator>::iterator
1887 deque<_Tp, _Allocator>::__insert_bidirectional(const_iterator __p, _BiIter __f, _BiIter __l, size_type __n) {
1888 size_type __pos = __p - begin();
1889 size_type __to_end = size() - __pos;
1890 allocator_type& __a = __alloc();
1891 if (__pos < __to_end) { // insert by shifting things backward
1892 if (__n > __front_spare())
1893 __add_front_capacity(__n - __front_spare());
1894 // __n <= __front_spare()
1895 __annotate_increase_front(__n);
1896 iterator __old_begin = begin();
1897 iterator __i = __old_begin;
1900 __m = __pos < __n / 2 ? std::prev(__l, __pos) : std::next(__f, __n - __pos);
1901 for (_BiIter __j = __m; __j != __f; --__start_, ++__size())
1902 __alloc_traits::construct(__a, std::addressof(*--__i), *--__j);
1906 iterator __obn = __old_begin + __n;
1907 for (iterator __j = __obn; __j != __old_begin;) {
1908 __alloc_traits::construct(__a, std::addressof(*--__i), std::move(*--__j));
1913 __old_begin = std::move(__obn, __old_begin + __pos, __old_begin);
1914 std::copy(__m, __l, __old_begin);
1916 } else { // insert by shifting things forward
1917 size_type __back_capacity = __back_spare();
1918 if (__n > __back_capacity)
1919 __add_back_capacity(__n - __back_capacity);
1920 // __n <= __back_capacity
1921 __annotate_increase_back(__n);
1922 iterator __old_end = end();
1923 iterator __i = __old_end;
1925 size_type __de = size() - __pos;
1927 __m = __de < __n / 2 ? std::next(__f, __de) : std::prev(__l, __n - __de);
1928 for (_BiIter __j = __m; __j != __l; ++__i, (void)++__j, ++__size())
1929 __alloc_traits::construct(__a, std::addressof(*__i), *__j);
1933 iterator __oen = __old_end - __n;
1934 for (iterator __j = __oen; __j != __old_end; ++__i, (void)++__j, ++__size())
1935 __alloc_traits::construct(__a, std::addressof(*__i), std::move(*__j));
1937 __old_end = std::move_backward(__old_end - __de, __oen, __old_end);
1938 std::copy_backward(__f, __m, __old_end);
1941 return begin() + __pos;
1944 template <class _Tp, class _Allocator>
1945 template <class _InpIter, __enable_if_t<__has_exactly_input_iterator_category<_InpIter>::value, int> >
1946 void deque<_Tp, _Allocator>::__append(_InpIter __f, _InpIter __l) {
1947 __append_with_sentinel(__f, __l);
1950 template <class _Tp, class _Allocator>
1951 template <class _InputIterator, class _Sentinel>
1952 _LIBCPP_HIDE_FROM_ABI void deque<_Tp, _Allocator>::__append_with_sentinel(_InputIterator __f, _Sentinel __l) {
1953 for (; __f != __l; ++__f)
1954 #ifdef _LIBCPP_CXX03_LANG
1961 template <class _Tp, class _Allocator>
1962 template <class _ForIter, __enable_if_t<__has_forward_iterator_category<_ForIter>::value, int> >
1963 void deque<_Tp, _Allocator>::__append(_ForIter __f, _ForIter __l) {
1964 __append_with_size(__f, std::distance(__f, __l));
1967 template <class _Tp, class _Allocator>
1968 template <class _InputIterator>
1969 _LIBCPP_HIDE_FROM_ABI void deque<_Tp, _Allocator>::__append_with_size(_InputIterator __f, size_type __n) {
1970 allocator_type& __a = __alloc();
1971 size_type __back_capacity = __back_spare();
1972 if (__n > __back_capacity)
1973 __add_back_capacity(__n - __back_capacity);
1975 // __n <= __back_capacity
1976 __annotate_increase_back(__n);
1977 for (__deque_block_range __br : __deque_range(end(), end() + __n)) {
1978 _ConstructTransaction __tx(this, __br);
1979 for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, (void)++__f) {
1980 __alloc_traits::construct(__a, std::__to_address(__tx.__pos_), *__f);
1985 template <class _Tp, class _Allocator>
1986 void deque<_Tp, _Allocator>::__append(size_type __n) {
1987 allocator_type& __a = __alloc();
1988 size_type __back_capacity = __back_spare();
1989 if (__n > __back_capacity)
1990 __add_back_capacity(__n - __back_capacity);
1991 // __n <= __back_capacity
1992 __annotate_increase_back(__n);
1993 for (__deque_block_range __br : __deque_range(end(), end() + __n)) {
1994 _ConstructTransaction __tx(this, __br);
1995 for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
1996 __alloc_traits::construct(__a, std::__to_address(__tx.__pos_));
2001 template <class _Tp, class _Allocator>
2002 void deque<_Tp, _Allocator>::__append(size_type __n, const value_type& __v) {
2003 allocator_type& __a = __alloc();
2004 size_type __back_capacity = __back_spare();
2005 if (__n > __back_capacity)
2006 __add_back_capacity(__n - __back_capacity);
2007 // __n <= __back_capacity
2008 __annotate_increase_back(__n);
2009 for (__deque_block_range __br : __deque_range(end(), end() + __n)) {
2010 _ConstructTransaction __tx(this, __br);
2011 for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
2012 __alloc_traits::construct(__a, std::__to_address(__tx.__pos_), __v);
2017 // Create front capacity for one block of elements.
2018 // Strong guarantee. Either do it or don't touch anything.
2019 template <class _Tp, class _Allocator>
2020 void deque<_Tp, _Allocator>::__add_front_capacity() {
2021 allocator_type& __a = __alloc();
2022 if (__back_spare() >= __block_size) {
2023 __start_ += __block_size;
2024 pointer __pt = __map_.back();
2026 __map_.push_front(__pt);
2028 // Else if __map_.size() < __map_.capacity() then we need to allocate 1 buffer
2029 else if (__map_.size() < __map_.capacity()) { // we can put the new buffer into the map, but don't shift things around
2030 // until all buffers are allocated. If we throw, we don't need to fix
2031 // anything up (any added buffers are undetectible)
2032 if (__map_.__front_spare() > 0)
2033 __map_.push_front(__alloc_traits::allocate(__a, __block_size));
2035 __map_.push_back(__alloc_traits::allocate(__a, __block_size));
2036 // Done allocating, reorder capacity
2037 pointer __pt = __map_.back();
2039 __map_.push_front(__pt);
2041 __start_ = __map_.size() == 1 ? __block_size / 2 : __start_ + __block_size;
2043 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2045 __split_buffer<pointer, __pointer_allocator&> __buf(
2046 std::max<size_type>(2 * __map_.capacity(), 1), 0, __map_.__alloc());
2048 typedef __allocator_destructor<_Allocator> _Dp;
2049 unique_ptr<pointer, _Dp> __hold(__alloc_traits::allocate(__a, __block_size), _Dp(__a, __block_size));
2050 __buf.push_back(__hold.get());
2053 for (__map_pointer __i = __map_.begin(); __i != __map_.end(); ++__i)
2054 __buf.push_back(*__i);
2055 std::swap(__map_.__first_, __buf.__first_);
2056 std::swap(__map_.__begin_, __buf.__begin_);
2057 std::swap(__map_.__end_, __buf.__end_);
2058 std::swap(__map_.__end_cap(), __buf.__end_cap());
2059 __start_ = __map_.size() == 1 ? __block_size / 2 : __start_ + __block_size;
2061 __annotate_whole_block(0, __asan_poison);
2064 // Create front capacity for __n elements.
2065 // Strong guarantee. Either do it or don't touch anything.
2066 template <class _Tp, class _Allocator>
2067 void deque<_Tp, _Allocator>::__add_front_capacity(size_type __n) {
2068 allocator_type& __a = __alloc();
2069 size_type __nb = __recommend_blocks(__n + __map_.empty());
2070 // Number of unused blocks at back:
2071 size_type __back_capacity = __back_spare() / __block_size;
2072 __back_capacity = std::min(__back_capacity, __nb); // don't take more than you need
2073 __nb -= __back_capacity; // number of blocks need to allocate
2074 // If __nb == 0, then we have sufficient capacity.
2076 __start_ += __block_size * __back_capacity;
2077 for (; __back_capacity > 0; --__back_capacity) {
2078 pointer __pt = __map_.back();
2080 __map_.push_front(__pt);
2083 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2084 else if (__nb <= __map_.capacity() -
2085 __map_.size()) { // we can put the new buffers into the map, but don't shift things around
2086 // until all buffers are allocated. If we throw, we don't need to fix
2087 // anything up (any added buffers are undetectible)
2088 for (; __nb > 0; --__nb, __start_ += __block_size - (__map_.size() == 1)) {
2089 if (__map_.__front_spare() == 0)
2091 __map_.push_front(__alloc_traits::allocate(__a, __block_size));
2092 __annotate_whole_block(0, __asan_poison);
2094 for (; __nb > 0; --__nb, ++__back_capacity)
2095 __map_.push_back(__alloc_traits::allocate(__a, __block_size));
2096 // Done allocating, reorder capacity
2097 __start_ += __back_capacity * __block_size;
2098 for (; __back_capacity > 0; --__back_capacity) {
2099 pointer __pt = __map_.back();
2101 __map_.push_front(__pt);
2102 __annotate_whole_block(0, __asan_poison);
2105 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2107 size_type __ds = (__nb + __back_capacity) * __block_size - __map_.empty();
2108 __split_buffer<pointer, __pointer_allocator&> __buf(
2109 std::max<size_type>(2 * __map_.capacity(), __nb + __map_.size()), 0, __map_.__alloc());
2110 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2112 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
2113 for (; __nb > 0; --__nb) {
2114 __buf.push_back(__alloc_traits::allocate(__a, __block_size));
2115 // ASan: this is empty container, we have to poison whole block
2116 __annotate_poison_block(std::__to_address(__buf.back()), std::__to_address(__buf.back() + __block_size));
2118 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2120 __annotate_delete();
2121 for (__map_pointer __i = __buf.begin(); __i != __buf.end(); ++__i)
2122 __alloc_traits::deallocate(__a, *__i, __block_size);
2125 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
2126 for (; __back_capacity > 0; --__back_capacity) {
2127 __buf.push_back(__map_.back());
2130 for (__map_pointer __i = __map_.begin(); __i != __map_.end(); ++__i)
2131 __buf.push_back(*__i);
2132 std::swap(__map_.__first_, __buf.__first_);
2133 std::swap(__map_.__begin_, __buf.__begin_);
2134 std::swap(__map_.__end_, __buf.__end_);
2135 std::swap(__map_.__end_cap(), __buf.__end_cap());
2140 // Create back capacity for one block of elements.
2141 // Strong guarantee. Either do it or don't touch anything.
2142 template <class _Tp, class _Allocator>
2143 void deque<_Tp, _Allocator>::__add_back_capacity() {
2144 allocator_type& __a = __alloc();
2145 if (__front_spare() >= __block_size) {
2146 __start_ -= __block_size;
2147 pointer __pt = __map_.front();
2149 __map_.push_back(__pt);
2151 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2152 else if (__map_.size() < __map_.capacity()) { // we can put the new buffer into the map, but don't shift things around
2153 // until it is allocated. If we throw, we don't need to fix
2154 // anything up (any added buffers are undetectible)
2155 if (__map_.__back_spare() != 0)
2156 __map_.push_back(__alloc_traits::allocate(__a, __block_size));
2158 __map_.push_front(__alloc_traits::allocate(__a, __block_size));
2159 // Done allocating, reorder capacity
2160 pointer __pt = __map_.front();
2162 __map_.push_back(__pt);
2164 __annotate_whole_block(__map_.size() - 1, __asan_poison);
2166 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2168 __split_buffer<pointer, __pointer_allocator&> __buf(
2169 std::max<size_type>(2 * __map_.capacity(), 1), __map_.size(), __map_.__alloc());
2171 typedef __allocator_destructor<_Allocator> _Dp;
2172 unique_ptr<pointer, _Dp> __hold(__alloc_traits::allocate(__a, __block_size), _Dp(__a, __block_size));
2173 __buf.push_back(__hold.get());
2176 for (__map_pointer __i = __map_.end(); __i != __map_.begin();)
2177 __buf.push_front(*--__i);
2178 std::swap(__map_.__first_, __buf.__first_);
2179 std::swap(__map_.__begin_, __buf.__begin_);
2180 std::swap(__map_.__end_, __buf.__end_);
2181 std::swap(__map_.__end_cap(), __buf.__end_cap());
2182 __annotate_whole_block(__map_.size() - 1, __asan_poison);
2186 // Create back capacity for __n elements.
2187 // Strong guarantee. Either do it or don't touch anything.
2188 template <class _Tp, class _Allocator>
2189 void deque<_Tp, _Allocator>::__add_back_capacity(size_type __n) {
2190 allocator_type& __a = __alloc();
2191 size_type __nb = __recommend_blocks(__n + __map_.empty());
2192 // Number of unused blocks at front:
2193 size_type __front_capacity = __front_spare() / __block_size;
2194 __front_capacity = std::min(__front_capacity, __nb); // don't take more than you need
2195 __nb -= __front_capacity; // number of blocks need to allocate
2196 // If __nb == 0, then we have sufficient capacity.
2198 __start_ -= __block_size * __front_capacity;
2199 for (; __front_capacity > 0; --__front_capacity) {
2200 pointer __pt = __map_.front();
2202 __map_.push_back(__pt);
2205 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2206 else if (__nb <= __map_.capacity() -
2207 __map_.size()) { // we can put the new buffers into the map, but don't shift things around
2208 // until all buffers are allocated. If we throw, we don't need to fix
2209 // anything up (any added buffers are undetectible)
2210 for (; __nb > 0; --__nb) {
2211 if (__map_.__back_spare() == 0)
2213 __map_.push_back(__alloc_traits::allocate(__a, __block_size));
2214 __annotate_whole_block(__map_.size() - 1, __asan_poison);
2216 for (; __nb > 0; --__nb, ++__front_capacity, __start_ += __block_size - (__map_.size() == 1)) {
2217 __map_.push_front(__alloc_traits::allocate(__a, __block_size));
2218 __annotate_whole_block(0, __asan_poison);
2220 // Done allocating, reorder capacity
2221 __start_ -= __block_size * __front_capacity;
2222 for (; __front_capacity > 0; --__front_capacity) {
2223 pointer __pt = __map_.front();
2225 __map_.push_back(__pt);
2228 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2230 size_type __ds = __front_capacity * __block_size;
2231 __split_buffer<pointer, __pointer_allocator&> __buf(
2232 std::max<size_type>(2 * __map_.capacity(), __nb + __map_.size()),
2233 __map_.size() - __front_capacity,
2235 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2237 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
2238 for (; __nb > 0; --__nb) {
2239 __buf.push_back(__alloc_traits::allocate(__a, __block_size));
2240 // ASan: this is an empty container, we have to poison the whole block
2241 __annotate_poison_block(std::__to_address(__buf.back()), std::__to_address(__buf.back() + __block_size));
2243 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2245 __annotate_delete();
2246 for (__map_pointer __i = __buf.begin(); __i != __buf.end(); ++__i)
2247 __alloc_traits::deallocate(__a, *__i, __block_size);
2250 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
2251 for (; __front_capacity > 0; --__front_capacity) {
2252 __buf.push_back(__map_.front());
2255 for (__map_pointer __i = __map_.end(); __i != __map_.begin();)
2256 __buf.push_front(*--__i);
2257 std::swap(__map_.__first_, __buf.__first_);
2258 std::swap(__map_.__begin_, __buf.__begin_);
2259 std::swap(__map_.__end_, __buf.__end_);
2260 std::swap(__map_.__end_cap(), __buf.__end_cap());
2265 template <class _Tp, class _Allocator>
2266 void deque<_Tp, _Allocator>::pop_front() {
2267 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "deque::pop_front called on an empty deque");
2268 size_type __old_sz = size();
2269 size_type __old_start = __start_;
2270 allocator_type& __a = __alloc();
2271 __alloc_traits::destroy(
2272 __a, std::__to_address(*(__map_.begin() + __start_ / __block_size) + __start_ % __block_size));
2275 __annotate_shrink_front(__old_sz, __old_start);
2276 __maybe_remove_front_spare();
2279 template <class _Tp, class _Allocator>
2280 void deque<_Tp, _Allocator>::pop_back() {
2281 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "deque::pop_back called on an empty deque");
2282 size_type __old_sz = size();
2283 size_type __old_start = __start_;
2284 allocator_type& __a = __alloc();
2285 size_type __p = size() + __start_ - 1;
2286 __alloc_traits::destroy(__a, std::__to_address(*(__map_.begin() + __p / __block_size) + __p % __block_size));
2288 __annotate_shrink_back(__old_sz, __old_start);
2289 __maybe_remove_back_spare();
2292 // move assign [__f, __l) to [__r, __r + (__l-__f)).
2293 // If __vt points into [__f, __l), then subtract (__f - __r) from __vt.
2294 template <class _Tp, class _Allocator>
2295 typename deque<_Tp, _Allocator>::iterator
2296 deque<_Tp, _Allocator>::__move_and_check(iterator __f, iterator __l, iterator __r, const_pointer& __vt) {
2298 // for (; __f != __l; ++__f, ++__r)
2299 // *__r = std::move(*__f);
2300 difference_type __n = __l - __f;
2302 pointer __fb = __f.__ptr_;
2303 pointer __fe = *__f.__m_iter_ + __block_size;
2304 difference_type __bs = __fe - __fb;
2309 if (__fb <= __vt && __vt < __fe)
2310 __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) -= __f - __r).__ptr_;
2311 __r = std::move(__fb, __fe, __r);
2318 // move assign [__f, __l) to [__r - (__l-__f), __r) backwards.
2319 // If __vt points into [__f, __l), then add (__r - __l) to __vt.
2320 template <class _Tp, class _Allocator>
2321 typename deque<_Tp, _Allocator>::iterator
2322 deque<_Tp, _Allocator>::__move_backward_and_check(iterator __f, iterator __l, iterator __r, const_pointer& __vt) {
2324 // while (__f != __l)
2325 // *--__r = std::move(*--__l);
2326 difference_type __n = __l - __f;
2329 pointer __lb = *__l.__m_iter_;
2330 pointer __le = __l.__ptr_ + 1;
2331 difference_type __bs = __le - __lb;
2336 if (__lb <= __vt && __vt < __le)
2337 __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) += __r - __l - 1).__ptr_;
2338 __r = std::move_backward(__lb, __le, __r);
2345 // move construct [__f, __l) to [__r, __r + (__l-__f)).
2346 // If __vt points into [__f, __l), then add (__r - __f) to __vt.
2347 template <class _Tp, class _Allocator>
2348 void deque<_Tp, _Allocator>::__move_construct_and_check(iterator __f, iterator __l, iterator __r, const_pointer& __vt) {
2349 allocator_type& __a = __alloc();
2351 // for (; __f != __l; ++__r, ++__f, ++__size())
2352 // __alloc_traits::construct(__a, std::addressof(*__r), std::move(*__f));
2353 difference_type __n = __l - __f;
2355 pointer __fb = __f.__ptr_;
2356 pointer __fe = *__f.__m_iter_ + __block_size;
2357 difference_type __bs = __fe - __fb;
2362 if (__fb <= __vt && __vt < __fe)
2363 __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) += __r - __f).__ptr_;
2364 for (; __fb != __fe; ++__fb, ++__r, ++__size())
2365 __alloc_traits::construct(__a, std::addressof(*__r), std::move(*__fb));
2371 // move construct [__f, __l) to [__r - (__l-__f), __r) backwards.
2372 // If __vt points into [__f, __l), then subtract (__l - __r) from __vt.
2373 template <class _Tp, class _Allocator>
2374 void deque<_Tp, _Allocator>::__move_construct_backward_and_check(
2375 iterator __f, iterator __l, iterator __r, const_pointer& __vt) {
2376 allocator_type& __a = __alloc();
2378 // for (iterator __j = __l; __j != __f;)
2380 // __alloc_traitsconstruct(__a, std::addressof(*--__r), std::move(*--__j));
2384 difference_type __n = __l - __f;
2387 pointer __lb = *__l.__m_iter_;
2388 pointer __le = __l.__ptr_ + 1;
2389 difference_type __bs = __le - __lb;
2394 if (__lb <= __vt && __vt < __le)
2395 __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) -= __l - __r + 1).__ptr_;
2396 while (__le != __lb) {
2397 __alloc_traits::construct(__a, std::addressof(*--__r), std::move(*--__le));
2406 template <class _Tp, class _Allocator>
2407 typename deque<_Tp, _Allocator>::iterator deque<_Tp, _Allocator>::erase(const_iterator __f) {
2408 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
2409 __f != end(), "deque::erase(iterator) called with a non-dereferenceable iterator");
2410 size_type __old_sz = size();
2411 size_type __old_start = __start_;
2412 iterator __b = begin();
2413 difference_type __pos = __f - __b;
2414 iterator __p = __b + __pos;
2415 allocator_type& __a = __alloc();
2416 if (static_cast<size_t>(__pos) <= (size() - 1) / 2) { // erase from front
2417 std::move_backward(__b, __p, std::next(__p));
2418 __alloc_traits::destroy(__a, std::addressof(*__b));
2421 __annotate_shrink_front(__old_sz, __old_start);
2422 __maybe_remove_front_spare();
2423 } else { // erase from back
2424 iterator __i = std::move(std::next(__p), end(), __p);
2425 __alloc_traits::destroy(__a, std::addressof(*__i));
2427 __annotate_shrink_back(__old_sz, __old_start);
2428 __maybe_remove_back_spare();
2430 return begin() + __pos;
2433 template <class _Tp, class _Allocator>
2434 typename deque<_Tp, _Allocator>::iterator deque<_Tp, _Allocator>::erase(const_iterator __f, const_iterator __l) {
2435 _LIBCPP_ASSERT_VALID_INPUT_RANGE(__f <= __l, "deque::erase(first, last) called with an invalid range");
2436 size_type __old_sz = size();
2437 size_type __old_start = __start_;
2438 difference_type __n = __l - __f;
2439 iterator __b = begin();
2440 difference_type __pos = __f - __b;
2441 iterator __p = __b + __pos;
2443 allocator_type& __a = __alloc();
2444 if (static_cast<size_t>(__pos) <= (size() - __n) / 2) { // erase from front
2445 iterator __i = std::move_backward(__b, __p, __p + __n);
2446 for (; __b != __i; ++__b)
2447 __alloc_traits::destroy(__a, std::addressof(*__b));
2450 __annotate_shrink_front(__old_sz, __old_start);
2451 while (__maybe_remove_front_spare()) {
2453 } else { // erase from back
2454 iterator __i = std::move(__p + __n, end(), __p);
2455 for (iterator __e = end(); __i != __e; ++__i)
2456 __alloc_traits::destroy(__a, std::addressof(*__i));
2458 __annotate_shrink_back(__old_sz, __old_start);
2459 while (__maybe_remove_back_spare()) {
2463 return begin() + __pos;
2466 template <class _Tp, class _Allocator>
2467 void deque<_Tp, _Allocator>::__erase_to_end(const_iterator __f) {
2468 size_type __old_sz = size();
2469 size_type __old_start = __start_;
2470 iterator __e = end();
2471 difference_type __n = __e - __f;
2473 allocator_type& __a = __alloc();
2474 iterator __b = begin();
2475 difference_type __pos = __f - __b;
2476 for (iterator __p = __b + __pos; __p != __e; ++__p)
2477 __alloc_traits::destroy(__a, std::addressof(*__p));
2479 __annotate_shrink_back(__old_sz, __old_start);
2480 while (__maybe_remove_back_spare()) {
2485 template <class _Tp, class _Allocator>
2486 inline void deque<_Tp, _Allocator>::swap(deque& __c)
2487 #if _LIBCPP_STD_VER >= 14
2490 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<allocator_type>)
2493 __map_.swap(__c.__map_);
2494 std::swap(__start_, __c.__start_);
2495 std::swap(__size(), __c.__size());
2496 std::__swap_allocator(__alloc(), __c.__alloc());
2499 template <class _Tp, class _Allocator>
2500 inline void deque<_Tp, _Allocator>::clear() _NOEXCEPT {
2501 __annotate_delete();
2502 allocator_type& __a = __alloc();
2503 for (iterator __i = begin(), __e = end(); __i != __e; ++__i)
2504 __alloc_traits::destroy(__a, std::addressof(*__i));
2506 while (__map_.size() > 2) {
2507 __alloc_traits::deallocate(__a, __map_.front(), __block_size);
2510 switch (__map_.size()) {
2512 __start_ = __block_size / 2;
2515 __start_ = __block_size;
2521 template <class _Tp, class _Allocator>
2522 inline _LIBCPP_HIDE_FROM_ABI bool operator==(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) {
2523 const typename deque<_Tp, _Allocator>::size_type __sz = __x.size();
2524 return __sz == __y.size() && std::equal(__x.begin(), __x.end(), __y.begin());
2527 #if _LIBCPP_STD_VER <= 17
2529 template <class _Tp, class _Allocator>
2530 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) {
2531 return !(__x == __y);
2534 template <class _Tp, class _Allocator>
2535 inline _LIBCPP_HIDE_FROM_ABI bool operator<(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) {
2536 return std::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
2539 template <class _Tp, class _Allocator>
2540 inline _LIBCPP_HIDE_FROM_ABI bool operator>(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) {
2544 template <class _Tp, class _Allocator>
2545 inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) {
2546 return !(__x < __y);
2549 template <class _Tp, class _Allocator>
2550 inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) {
2551 return !(__y < __x);
2554 #else // _LIBCPP_STD_VER <= 17
2556 template <class _Tp, class _Allocator>
2557 _LIBCPP_HIDE_FROM_ABI __synth_three_way_result<_Tp>
2558 operator<=>(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) {
2559 return std::lexicographical_compare_three_way(__x.begin(), __x.end(), __y.begin(), __y.end(), std::__synth_three_way);
2562 #endif // _LIBCPP_STD_VER <= 17
2564 template <class _Tp, class _Allocator>
2565 inline _LIBCPP_HIDE_FROM_ABI void swap(deque<_Tp, _Allocator>& __x, deque<_Tp, _Allocator>& __y)
2566 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) {
2570 #if _LIBCPP_STD_VER >= 20
2571 template <class _Tp, class _Allocator, class _Up>
2572 inline _LIBCPP_HIDE_FROM_ABI typename deque<_Tp, _Allocator>::size_type
2573 erase(deque<_Tp, _Allocator>& __c, const _Up& __v) {
2574 auto __old_size = __c.size();
2575 __c.erase(std::remove(__c.begin(), __c.end(), __v), __c.end());
2576 return __old_size - __c.size();
2579 template <class _Tp, class _Allocator, class _Predicate>
2580 inline _LIBCPP_HIDE_FROM_ABI typename deque<_Tp, _Allocator>::size_type
2581 erase_if(deque<_Tp, _Allocator>& __c, _Predicate __pred) {
2582 auto __old_size = __c.size();
2583 __c.erase(std::remove_if(__c.begin(), __c.end(), __pred), __c.end());
2584 return __old_size - __c.size();
2588 inline constexpr bool __format::__enable_insertable<std::deque<char>> = true;
2589 # ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
2591 inline constexpr bool __format::__enable_insertable<std::deque<wchar_t>> = true;
2594 #endif // _LIBCPP_STD_VER >= 20
2596 _LIBCPP_END_NAMESPACE_STD
2598 #if _LIBCPP_STD_VER >= 17
2599 _LIBCPP_BEGIN_NAMESPACE_STD
2601 template <class _ValueT>
2602 using deque _LIBCPP_AVAILABILITY_PMR = std::deque<_ValueT, polymorphic_allocator<_ValueT>>;
2604 _LIBCPP_END_NAMESPACE_STD
2609 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
2610 # include <__cxx03/algorithm>
2611 # include <__cxx03/atomic>
2612 # include <__cxx03/concepts>
2613 # include <__cxx03/cstdlib>
2614 # include <__cxx03/functional>
2615 # include <__cxx03/iosfwd>
2616 # include <__cxx03/iterator>
2617 # include <__cxx03/type_traits>
2618 # include <__cxx03/typeinfo>
2621 #endif // _LIBCPP___CXX03_DEQUE