1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
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
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_DEQUE
11#define _LIBCPP_DEQUE
12
13/*
14 deque synopsis
15
16namespace std
17{
18
19template <class T, class Allocator = allocator<T> >
20class deque
21{
22public:
23 // types:
24 typedef T value_type;
25 typedef Allocator allocator_type;
26
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;
33
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;
38
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);
53 deque(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);
58 ~deque();
59
60 deque& operator=(const deque& c);
61 deque& operator=(deque&& c)
62 noexcept((allocator_traits<allocator_type>::propagate_on_container_move_assignment::value &&
63 is_nothrow_move_assignable<allocator_type>::value) ||
64 allocator_traits<allocator_type>::is_always_equal::value);
65 deque& operator=(initializer_list<value_type> il);
66
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);
73
74 allocator_type get_allocator() const noexcept;
75
76 // iterators:
77
78 iterator begin() noexcept;
79 const_iterator begin() const noexcept;
80 iterator end() noexcept;
81 const_iterator end() const noexcept;
82
83 reverse_iterator rbegin() noexcept;
84 const_reverse_iterator rbegin() const noexcept;
85 reverse_iterator rend() noexcept;
86 const_reverse_iterator rend() const noexcept;
87
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;
92
93 // capacity:
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);
98 void shrink_to_fit();
99 bool empty() const noexcept;
100
101 // element access:
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;
106 reference front();
107 const_reference front() const;
108 reference back();
109 const_reference back() const;
110
111 // modifiers:
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);
131 void pop_front();
132 void pop_back();
133 iterator erase(const_iterator p);
134 iterator erase(const_iterator f, const_iterator l);
135 void swap(deque& c)
136 noexcept(allocator_traits<allocator_type>::is_always_equal::value); // C++17
137 void clear() noexcept;
138};
139
140template <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
143
144template<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
147
148template <class T, class Allocator>
149 bool operator==(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
150template <class T, class Allocator>
151 bool operator< (const deque<T,Allocator>& x, const deque<T,Allocator>& y); // removed in C++20
152template <class T, class Allocator>
153 bool operator!=(const deque<T,Allocator>& x, const deque<T,Allocator>& y); // removed in C++20
154template <class T, class Allocator>
155 bool operator> (const deque<T,Allocator>& x, const deque<T,Allocator>& y); // removed in C++20
156template <class T, class Allocator>
157 bool operator>=(const deque<T,Allocator>& x, const deque<T,Allocator>& y); // removed in C++20
158template <class T, class Allocator>
159 bool operator<=(const deque<T,Allocator>& x, const deque<T,Allocator>& y); // removed in C++20
160template<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
163
164// specialized algorithms:
165template <class T, class Allocator>
166 void swap(deque<T,Allocator>& x, deque<T,Allocator>& y)
167 noexcept(noexcept(x.swap(y)));
168
169template <class T, class Allocator, class U>
170 typename deque<T, Allocator>::size_type
171 erase(deque<T, Allocator>& c, const U& value); // C++20
172template <class T, class Allocator, class Predicate>
173 typename deque<T, Allocator>::size_type
174 erase_if(deque<T, Allocator>& c, Predicate pred); // C++20
175
176} // std
177
178*/
179
180#if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
181# include <__cxx03/deque>
182#else
183# include <__algorithm/copy.h>
184# include <__algorithm/copy_backward.h>
185# include <__algorithm/copy_n.h>
186# include <__algorithm/equal.h>
187# include <__algorithm/fill_n.h>
188# include <__algorithm/lexicographical_compare.h>
189# include <__algorithm/lexicographical_compare_three_way.h>
190# include <__algorithm/max.h>
191# include <__algorithm/min.h>
192# include <__algorithm/move.h>
193# include <__algorithm/move_backward.h>
194# include <__algorithm/ranges_copy_n.h>
195# include <__algorithm/remove.h>
196# include <__algorithm/remove_if.h>
197# include <__assert>
198# include <__config>
199# include <__debug_utils/sanitizers.h>
200# include <__format/enable_insertable.h>
201# include <__fwd/deque.h>
202# include <__iterator/distance.h>
203# include <__iterator/iterator_traits.h>
204# include <__iterator/move_iterator.h>
205# include <__iterator/next.h>
206# include <__iterator/prev.h>
207# include <__iterator/reverse_iterator.h>
208# include <__iterator/segmented_iterator.h>
209# include <__memory/addressof.h>
210# include <__memory/allocator.h>
211# include <__memory/allocator_destructor.h>
212# include <__memory/allocator_traits.h>
213# include <__memory/compressed_pair.h>
214# include <__memory/pointer_traits.h>
215# include <__memory/swap_allocator.h>
216# include <__memory/temp_value.h>
217# include <__memory/unique_ptr.h>
218# include <__memory_resource/polymorphic_allocator.h>
219# include <__ranges/access.h>
220# include <__ranges/concepts.h>
221# include <__ranges/container_compatible_range.h>
222# include <__ranges/from_range.h>
223# include <__split_buffer>
224# include <__type_traits/conditional.h>
225# include <__type_traits/container_traits.h>
226# include <__type_traits/enable_if.h>
227# include <__type_traits/is_allocator.h>
228# include <__type_traits/is_convertible.h>
229# include <__type_traits/is_nothrow_assignable.h>
230# include <__type_traits/is_nothrow_constructible.h>
231# include <__type_traits/is_same.h>
232# include <__type_traits/is_swappable.h>
233# include <__type_traits/is_trivially_relocatable.h>
234# include <__type_traits/type_identity.h>
235# include <__utility/exception_guard.h>
236# include <__utility/forward.h>
237# include <__utility/move.h>
238# include <__utility/pair.h>
239# include <__utility/swap.h>
240# include <limits>
241# include <stdexcept>
242# include <version>
243
244// standard-mandated includes
245
246// [iterator.range]
247# include <__iterator/access.h>
248# include <__iterator/data.h>
249# include <__iterator/empty.h>
250# include <__iterator/reverse_access.h>
251# include <__iterator/size.h>
252
253// [deque.syn]
254# include <compare>
255# include <initializer_list>
256
257# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
258# pragma GCC system_header
259# endif
260
261_LIBCPP_PUSH_MACROS
262# include <__undef_macros>
263
264_LIBCPP_BEGIN_NAMESPACE_STD
265
266template <class _ValueType, class _DiffType>
267struct __deque_block_size {
268 static const _DiffType value = sizeof(_ValueType) < 256 ? 4096 / sizeof(_ValueType) : 16;
269};
270
271template <class _ValueType,
272 class _Pointer,
273 class _Reference,
274 class _MapPointer,
275 class _DiffType,
276 _DiffType _BS =
277# ifdef _LIBCPP_ABI_INCOMPLETE_TYPES_IN_DEQUE
278 // Keep template parameter to avoid changing all template declarations thoughout
279 // this file.
280 0
281# else
282 __deque_block_size<_ValueType, _DiffType>::value
283# endif
284 >
285class __deque_iterator {
286 using __map_iterator _LIBCPP_NODEBUG = __rebind_pointer_t<_Pointer, const __rebind_pointer_t<_Pointer, _ValueType> >;
287
288// TODO(LLVM 25): Remove this check
289# ifndef _LIBCPP_ABI_DEQUE_ITERATORS
290 static_assert(
291 sizeof(__map_iterator) == sizeof(_MapPointer) && _LIBCPP_ALIGNOF(__map_iterator) == _LIBCPP_ALIGNOF(_MapPointer),
292 "It looks like you are using fancy pointers such that FancyPtr<const FancyPtr<const value_type>> is not "
293 "layout-compatible with FancyPtr<FancyPtr<value_type>> or FancyPtr<const FancyPtr<value_type>>. "
294 "This means that your ABI is being broken between LLVM 22 and LLVM 23 if deque::iterator or "
295 "deque::const_iterator is used. If you don't care about your ABI being broken, define the "
296 "_LIBCPP_ABI_DEQUE_ITERATORS macro to silence this diagnostic.");
297# endif
298
299public:
300 typedef _Pointer pointer;
301 typedef _DiffType difference_type;
302
303private:
304 __map_iterator __m_iter_;
305 pointer __ptr_;
306
307 static const difference_type __block_size;
308
309public:
310 typedef _ValueType value_type;
311 typedef random_access_iterator_tag iterator_category;
312 typedef _Reference reference;
313
314 _LIBCPP_HIDE_FROM_ABI __deque_iterator() _NOEXCEPT
315# if _LIBCPP_STD_VER >= 14
316 : __m_iter_(nullptr),
317 __ptr_(nullptr)
318# endif
319 {
320 }
321
322 template <class _Pp, class _Rp, class _MP, __enable_if_t<is_convertible<_Pp, pointer>::value, int> = 0>
323 _LIBCPP_HIDE_FROM_ABI
324 __deque_iterator(const __deque_iterator<value_type, _Pp, _Rp, _MP, difference_type, _BS>& __it) _NOEXCEPT
325 : __m_iter_(__it.__m_iter_),
326 __ptr_(__it.__ptr_) {}
327
328 _LIBCPP_HIDE_FROM_ABI reference operator*() const { return *__ptr_; }
329 _LIBCPP_HIDE_FROM_ABI pointer operator->() const { return __ptr_; }
330
331 _LIBCPP_HIDE_FROM_ABI __deque_iterator& operator++() {
332 if (++__ptr_ - *__m_iter_ == __block_size) {
333 ++__m_iter_;
334 __ptr_ = *__m_iter_;
335 }
336 return *this;
337 }
338
339 _LIBCPP_HIDE_FROM_ABI __deque_iterator operator++(int) {
340 __deque_iterator __tmp = *this;
341 ++(*this);
342 return __tmp;
343 }
344
345 _LIBCPP_HIDE_FROM_ABI __deque_iterator& operator--() {
346 if (__ptr_ == *__m_iter_) {
347 --__m_iter_;
348 __ptr_ = *__m_iter_ + __block_size;
349 }
350 --__ptr_;
351 return *this;
352 }
353
354 _LIBCPP_HIDE_FROM_ABI __deque_iterator operator--(int) {
355 __deque_iterator __tmp = *this;
356 --(*this);
357 return __tmp;
358 }
359
360 _LIBCPP_HIDE_FROM_ABI __deque_iterator& operator+=(difference_type __n) {
361 if (__n != 0) {
362 __n += __ptr_ - *__m_iter_;
363 if (__n > 0) {
364 __m_iter_ += __n / __block_size;
365 __ptr_ = *__m_iter_ + __n % __block_size;
366 } else // (__n < 0)
367 {
368 difference_type __z = __block_size - 1 - __n;
369 __m_iter_ -= __z / __block_size;
370 __ptr_ = *__m_iter_ + (__block_size - 1 - __z % __block_size);
371 }
372 }
373 return *this;
374 }
375
376 _LIBCPP_HIDE_FROM_ABI __deque_iterator& operator-=(difference_type __n) { return *this += -__n; }
377
378 _LIBCPP_HIDE_FROM_ABI __deque_iterator operator+(difference_type __n) const {
379 __deque_iterator __t(*this);
380 __t += __n;
381 return __t;
382 }
383
384 _LIBCPP_HIDE_FROM_ABI __deque_iterator operator-(difference_type __n) const {
385 __deque_iterator __t(*this);
386 __t -= __n;
387 return __t;
388 }
389
390 _LIBCPP_HIDE_FROM_ABI friend __deque_iterator operator+(difference_type __n, const __deque_iterator& __it) {
391 return __it + __n;
392 }
393
394 _LIBCPP_HIDE_FROM_ABI friend difference_type operator-(const __deque_iterator& __x, const __deque_iterator& __y) {
395 if (__x != __y)
396 return (__x.__m_iter_ - __y.__m_iter_) * __block_size + (__x.__ptr_ - *__x.__m_iter_) -
397 (__y.__ptr_ - *__y.__m_iter_);
398 return 0;
399 }
400
401 _LIBCPP_HIDE_FROM_ABI reference operator[](difference_type __n) const { return *(*this + __n); }
402
403 _LIBCPP_HIDE_FROM_ABI friend bool operator==(const __deque_iterator& __x, const __deque_iterator& __y) {
404 return __x.__ptr_ == __y.__ptr_;
405 }
406
407# if _LIBCPP_STD_VER <= 17
408 _LIBCPP_HIDE_FROM_ABI friend bool operator!=(const __deque_iterator& __x, const __deque_iterator& __y) {
409 return !(__x == __y);
410 }
411 _LIBCPP_HIDE_FROM_ABI friend bool operator<(const __deque_iterator& __x, const __deque_iterator& __y) {
412 return __x.__m_iter_ < __y.__m_iter_ || (__x.__m_iter_ == __y.__m_iter_ && __x.__ptr_ < __y.__ptr_);
413 }
414
415 _LIBCPP_HIDE_FROM_ABI friend bool operator>(const __deque_iterator& __x, const __deque_iterator& __y) {
416 return __y < __x;
417 }
418
419 _LIBCPP_HIDE_FROM_ABI friend bool operator<=(const __deque_iterator& __x, const __deque_iterator& __y) {
420 return !(__y < __x);
421 }
422
423 _LIBCPP_HIDE_FROM_ABI friend bool operator>=(const __deque_iterator& __x, const __deque_iterator& __y) {
424 return !(__x < __y);
425 }
426
427# else
428
429 _LIBCPP_HIDE_FROM_ABI friend strong_ordering operator<=>(const __deque_iterator& __x, const __deque_iterator& __y) {
430 if (__x.__m_iter_ < __y.__m_iter_)
431 return strong_ordering::less;
432
433 if (__x.__m_iter_ == __y.__m_iter_) {
434 if constexpr (three_way_comparable<pointer, strong_ordering>) {
435 return __x.__ptr_ <=> __y.__ptr_;
436 } else {
437 if (__x.__ptr_ < __y.__ptr_)
438 return strong_ordering::less;
439
440 if (__x.__ptr_ == __y.__ptr_)
441 return strong_ordering::equal;
442
443 return strong_ordering::greater;
444 }
445 }
446
447 return strong_ordering::greater;
448 }
449# endif // _LIBCPP_STD_VER >= 20
450
451private:
452 _LIBCPP_HIDE_FROM_ABI explicit __deque_iterator(__map_iterator __m, pointer __p) _NOEXCEPT
453 : __m_iter_(__m),
454 __ptr_(__p) {}
455
456 template <class _Tp, class _Ap>
457 friend class deque;
458 template <class _Vp, class _Pp, class _Rp, class _MP, class _Dp, _Dp>
459 friend class __deque_iterator;
460
461 template <class>
462 friend struct __segmented_iterator_traits;
463};
464
465template <class _ValueType, class _Pointer, class _Reference, class _MapPointer, class _DiffType, _DiffType _BlockSize>
466struct __segmented_iterator_traits<
467 __deque_iterator<_ValueType, _Pointer, _Reference, _MapPointer, _DiffType, _BlockSize> > {
468private:
469 using _Iterator _LIBCPP_NODEBUG =
470 __deque_iterator<_ValueType, _Pointer, _Reference, _MapPointer, _DiffType, _BlockSize>;
471
472public:
473 using __segment_iterator _LIBCPP_NODEBUG = typename _Iterator::__map_iterator;
474 using __local_iterator _LIBCPP_NODEBUG = _Pointer;
475
476 static _LIBCPP_HIDE_FROM_ABI __segment_iterator __segment(_Iterator __iter) { return __iter.__m_iter_; }
477 static _LIBCPP_HIDE_FROM_ABI __local_iterator __local(_Iterator __iter) { return __iter.__ptr_; }
478 static _LIBCPP_HIDE_FROM_ABI __local_iterator __begin(__segment_iterator __iter) { return *__iter; }
479
480 static _LIBCPP_HIDE_FROM_ABI __local_iterator __end(__segment_iterator __iter) {
481 return *__iter + _Iterator::__block_size;
482 }
483
484 static _LIBCPP_HIDE_FROM_ABI _Iterator __compose(__segment_iterator __segment, __local_iterator __local) {
485 if (__segment && __local == __end(iter: __segment)) {
486 ++__segment;
487 return _Iterator(__segment, *__segment);
488 }
489 return _Iterator(__segment, __local);
490 }
491};
492
493template <class _ValueType, class _Pointer, class _Reference, class _MapPointer, class _DiffType, _DiffType _BlockSize>
494const _DiffType __deque_iterator<_ValueType, _Pointer, _Reference, _MapPointer, _DiffType, _BlockSize>::__block_size =
495 __deque_block_size<_ValueType, _DiffType>::value;
496
497template <class _Tp, class _Allocator /*= allocator<_Tp>*/>
498class deque {
499 template <class _Up, class _Alloc>
500 using __split_buffer _LIBCPP_NODEBUG = std::__split_buffer<_Up, _Alloc, __split_buffer_pointer_layout>;
501
502public:
503 // types:
504
505 using value_type = _Tp;
506
507 using allocator_type = _Allocator;
508 using __alloc_traits _LIBCPP_NODEBUG = allocator_traits<allocator_type>;
509 static_assert(__check_valid_allocator<allocator_type>::value, "");
510 static_assert(is_same<typename allocator_type::value_type, value_type>::value,
511 "Allocator::value_type must be same type as value_type");
512
513 using size_type = typename __alloc_traits::size_type;
514 using difference_type = typename __alloc_traits::difference_type;
515
516 using pointer = typename __alloc_traits::pointer;
517 using const_pointer = typename __alloc_traits::const_pointer;
518
519 using __pointer_allocator _LIBCPP_NODEBUG = __rebind_alloc<__alloc_traits, pointer>;
520 using __const_pointer_allocator _LIBCPP_NODEBUG = __rebind_alloc<__alloc_traits, const_pointer>;
521 using __map _LIBCPP_NODEBUG = __split_buffer<pointer, __pointer_allocator>;
522 using __map_alloc_traits _LIBCPP_NODEBUG = allocator_traits<__pointer_allocator>;
523 using __map_pointer _LIBCPP_NODEBUG = typename __map_alloc_traits::pointer;
524 using __map_const_pointer _LIBCPP_NODEBUG = typename allocator_traits<__const_pointer_allocator>::const_pointer;
525 using __map_const_iterator _LIBCPP_NODEBUG = typename __map::const_iterator;
526
527 using reference = value_type&;
528 using const_reference = const value_type&;
529
530 using iterator = __deque_iterator<value_type, pointer, reference, __map_pointer, difference_type>;
531 using const_iterator =
532 __deque_iterator<value_type, const_pointer, const_reference, __map_const_pointer, difference_type>;
533 using reverse_iterator = std::reverse_iterator<iterator>;
534 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
535
536 // A deque contains the following members which may be trivially relocatable:
537 // - __map: is a `__split_buffer`, see `__split_buffer` for more information on when it is trivially relocatable
538 // - size_type: is always trivially relocatable, since it is required to be an integral type
539 // - allocator_type: may not be trivially relocatable, so it's checked
540 // None of these are referencing the `deque` itself, so if all of them are trivially relocatable, `deque` is too.
541 using __trivially_relocatable _LIBCPP_NODEBUG = __conditional_t<
542 __libcpp_is_trivially_relocatable<__map>::value && __libcpp_is_trivially_relocatable<allocator_type>::value,
543 deque,
544 void>;
545
546 static_assert(is_nothrow_default_constructible<allocator_type>::value ==
547 is_nothrow_default_constructible<__pointer_allocator>::value,
548 "rebinding an allocator should not change exception guarantees");
549 static_assert(is_nothrow_move_constructible<allocator_type>::value ==
550 is_nothrow_move_constructible<typename __map::allocator_type>::value,
551 "rebinding an allocator should not change exception guarantees");
552
553private:
554 struct __deque_block_range {
555 explicit _LIBCPP_HIDE_FROM_ABI __deque_block_range(pointer __b, pointer __e) _NOEXCEPT
556 : __begin_(__b),
557 __end_(__e) {}
558 const pointer __begin_;
559 const pointer __end_;
560 };
561
562 struct __deque_range {
563 iterator __pos_;
564 const iterator __end_;
565
566 _LIBCPP_HIDE_FROM_ABI __deque_range(iterator __pos, iterator __e) _NOEXCEPT : __pos_(__pos), __end_(__e) {}
567
568 explicit _LIBCPP_HIDE_FROM_ABI operator bool() const _NOEXCEPT { return __pos_ != __end_; }
569
570 _LIBCPP_HIDE_FROM_ABI __deque_range begin() const { return *this; }
571
572 _LIBCPP_HIDE_FROM_ABI __deque_range end() const { return __deque_range(__end_, __end_); }
573 _LIBCPP_HIDE_FROM_ABI __deque_block_range operator*() const _NOEXCEPT {
574 if (__pos_.__m_iter_ == __end_.__m_iter_) {
575 return __deque_block_range(__pos_.__ptr_, __end_.__ptr_);
576 }
577 return __deque_block_range(__pos_.__ptr_, *__pos_.__m_iter_ + __block_size);
578 }
579
580 _LIBCPP_HIDE_FROM_ABI __deque_range& operator++() _NOEXCEPT {
581 if (__pos_.__m_iter_ == __end_.__m_iter_) {
582 __pos_ = __end_;
583 } else {
584 ++__pos_.__m_iter_;
585 __pos_.__ptr_ = *__pos_.__m_iter_;
586 }
587 return *this;
588 }
589
590 _LIBCPP_HIDE_FROM_ABI friend bool operator==(__deque_range const& __lhs, __deque_range const& __rhs) {
591 return __lhs.__pos_ == __rhs.__pos_;
592 }
593 _LIBCPP_HIDE_FROM_ABI friend bool operator!=(__deque_range const& __lhs, __deque_range const& __rhs) {
594 return !(__lhs == __rhs);
595 }
596 };
597
598 struct _ConstructTransaction {
599 _LIBCPP_HIDE_FROM_ABI _ConstructTransaction(deque* __db, __deque_block_range& __r)
600 : __pos_(__r.__begin_), __end_(__r.__end_), __begin_(__r.__begin_), __base_(__db) {}
601
602 _LIBCPP_HIDE_FROM_ABI ~_ConstructTransaction() { __base_->__size() += (__pos_ - __begin_); }
603
604 pointer __pos_;
605 const pointer __end_;
606
607 private:
608 const pointer __begin_;
609 deque* const __base_;
610 };
611
612 static const difference_type __block_size;
613
614 __map __map_;
615 size_type __start_;
616 _LIBCPP_COMPRESSED_PAIR(size_type, __size_, allocator_type, __alloc_);
617
618public:
619 // construct/copy/destroy:
620 _LIBCPP_HIDE_FROM_ABI deque() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
621 : __start_(0), __size_(0) {
622 __annotate_new(current_size: 0);
623 }
624
625 _LIBCPP_HIDE_FROM_ABI ~deque() {
626 clear();
627 __annotate_delete();
628 typename __map::iterator __i = __map_.begin();
629 typename __map::iterator __e = __map_.end();
630 for (; __i != __e; ++__i)
631 __alloc_traits::deallocate(__alloc(), *__i, __block_size);
632 }
633
634 _LIBCPP_HIDE_FROM_ABI explicit deque(const allocator_type& __a)
635 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0), __alloc_(__a) {
636 __annotate_new(current_size: 0);
637 }
638
639 explicit _LIBCPP_HIDE_FROM_ABI deque(size_type __n);
640# if _LIBCPP_STD_VER >= 14
641 explicit _LIBCPP_HIDE_FROM_ABI deque(size_type __n, const _Allocator& __a);
642# endif
643 _LIBCPP_HIDE_FROM_ABI deque(size_type __n, const value_type& __v);
644
645 template <__enable_if_t<__is_allocator_v<_Allocator>, int> = 0>
646 _LIBCPP_HIDE_FROM_ABI deque(size_type __n, const value_type& __v, const allocator_type& __a)
647 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0), __alloc_(__a) {
648 __annotate_new(current_size: 0);
649 if (__n > 0)
650 __append(__n, __v);
651 }
652
653 template <class _InputIter, __enable_if_t<__has_input_iterator_category<_InputIter>::value, int> = 0>
654 _LIBCPP_HIDE_FROM_ABI deque(_InputIter __f, _InputIter __l);
655 template <class _InputIter, __enable_if_t<__has_input_iterator_category<_InputIter>::value, int> = 0>
656 _LIBCPP_HIDE_FROM_ABI deque(_InputIter __f, _InputIter __l, const allocator_type& __a);
657
658# if _LIBCPP_STD_VER >= 23
659 template <_ContainerCompatibleRange<_Tp> _Range>
660 _LIBCPP_HIDE_FROM_ABI deque(from_range_t, _Range&& __range, const allocator_type& __a = allocator_type())
661 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0), __alloc_(__a) {
662 if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
663 __append_with_size(ranges::begin(__range), ranges::distance(__range));
664
665 } else {
666 for (auto&& __e : __range) {
667 emplace_back(std::forward<decltype(__e)>(__e));
668 }
669 }
670 }
671# endif
672
673 _LIBCPP_HIDE_FROM_ABI deque(const deque& __c);
674 _LIBCPP_HIDE_FROM_ABI deque(const deque& __c, const __type_identity_t<allocator_type>& __a);
675
676 _LIBCPP_HIDE_FROM_ABI deque& operator=(const deque& __c);
677
678# ifndef _LIBCPP_CXX03_LANG
679 _LIBCPP_HIDE_FROM_ABI deque(initializer_list<value_type> __il);
680 _LIBCPP_HIDE_FROM_ABI deque(initializer_list<value_type> __il, const allocator_type& __a);
681
682 _LIBCPP_HIDE_FROM_ABI deque& operator=(initializer_list<value_type> __il) {
683 assign(__il);
684 return *this;
685 }
686
687 _LIBCPP_HIDE_FROM_ABI deque(deque&& __c) noexcept(is_nothrow_move_constructible<allocator_type>::value);
688 _LIBCPP_HIDE_FROM_ABI deque(deque&& __c, const __type_identity_t<allocator_type>& __a);
689 _LIBCPP_HIDE_FROM_ABI deque& operator=(deque&& __c) noexcept(
690 (__alloc_traits::propagate_on_container_move_assignment::value &&
691 is_nothrow_move_assignable<allocator_type>::value) ||
692 __alloc_traits::is_always_equal::value);
693
694 _LIBCPP_HIDE_FROM_ABI void assign(initializer_list<value_type> __il) { assign(__il.begin(), __il.end()); }
695# endif // _LIBCPP_CXX03_LANG
696
697 template <class _InputIter,
698 __enable_if_t<__has_input_iterator_category<_InputIter>::value &&
699 !__has_random_access_iterator_category<_InputIter>::value,
700 int> = 0>
701 _LIBCPP_HIDE_FROM_ABI void assign(_InputIter __f, _InputIter __l);
702 template <class _RAIter, __enable_if_t<__has_random_access_iterator_category<_RAIter>::value, int> = 0>
703 _LIBCPP_HIDE_FROM_ABI void assign(_RAIter __f, _RAIter __l);
704
705# if _LIBCPP_STD_VER >= 23
706 template <_ContainerCompatibleRange<_Tp> _Range>
707 _LIBCPP_HIDE_FROM_ABI void assign_range(_Range&& __range) {
708 if constexpr (ranges::random_access_range<_Range>) {
709 auto __n = static_cast<size_type>(ranges::distance(__range));
710 __assign_with_size_random_access(ranges::begin(__range), __n);
711
712 } else if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
713 auto __n = ranges::distance(__range);
714 auto __first = ranges::begin(__range);
715
716 auto __result = std::ranges::copy_n(std::move(__first), std::min<size_t>(__n, size()), begin());
717
718 if (static_cast<size_type>(__n) > size()) {
719 __append_with_size(std::move(__result.in), __n - size());
720 } else {
721 __erase_to_end(f: __result.out);
722 }
723
724 } else {
725 __assign_with_sentinel(ranges::begin(__range), ranges::end(__range));
726 }
727 }
728# endif
729
730 _LIBCPP_HIDE_FROM_ABI void assign(size_type __n, const value_type& __v);
731
732 _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const _NOEXCEPT;
733 _LIBCPP_HIDE_FROM_ABI allocator_type& __alloc() _NOEXCEPT { return __alloc_; }
734 _LIBCPP_HIDE_FROM_ABI const allocator_type& __alloc() const _NOEXCEPT { return __alloc_; }
735
736 // iterators:
737
738 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT {
739 auto __mp = __map_.begin() + __start_ / __block_size;
740 return iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
741 }
742
743 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT {
744 auto __mp = __map_.begin() + __start_ / __block_size;
745 return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
746 }
747
748 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT {
749 size_type __p = size() + __start_;
750 auto __mp = __map_.begin() + __p / __block_size;
751 return iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
752 }
753
754 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT {
755 size_type __p = size() + __start_;
756 auto __mp = __map_.begin() + __p / __block_size;
757 return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
758 }
759
760 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI reverse_iterator rbegin() _NOEXCEPT { return reverse_iterator(end()); }
761 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rbegin() const _NOEXCEPT {
762 return const_reverse_iterator(end());
763 }
764 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI reverse_iterator rend() _NOEXCEPT { return reverse_iterator(begin()); }
765 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rend() const _NOEXCEPT {
766 return const_reverse_iterator(begin());
767 }
768
769 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT { return begin(); }
770 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT { return end(); }
771 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crbegin() const _NOEXCEPT {
772 return const_reverse_iterator(end());
773 }
774 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crend() const _NOEXCEPT {
775 return const_reverse_iterator(begin());
776 }
777
778 // capacity:
779 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return __size(); }
780
781 _LIBCPP_HIDE_FROM_ABI size_type& __size() _NOEXCEPT { return __size_; }
782 _LIBCPP_HIDE_FROM_ABI const size_type& __size() const _NOEXCEPT { return __size_; }
783
784 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
785 return std::min<size_type>(__alloc_traits::max_size(__alloc()), numeric_limits<difference_type>::max());
786 }
787 _LIBCPP_HIDE_FROM_ABI void resize(size_type __n);
788 _LIBCPP_HIDE_FROM_ABI void resize(size_type __n, const value_type& __v);
789 _LIBCPP_HIDE_FROM_ABI void shrink_to_fit() _NOEXCEPT;
790 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT { return size() == 0; }
791
792 // element access:
793 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI reference operator[](size_type __i) _NOEXCEPT;
794 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_reference operator[](size_type __i) const _NOEXCEPT;
795 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI reference at(size_type __i);
796 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_reference at(size_type __i) const;
797 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI reference front() _NOEXCEPT;
798 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_reference front() const _NOEXCEPT;
799 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI reference back() _NOEXCEPT;
800 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_reference back() const _NOEXCEPT;
801
802 // 23.2.2.3 modifiers:
803 _LIBCPP_HIDE_FROM_ABI void push_front(const value_type& __v);
804 _LIBCPP_HIDE_FROM_ABI void push_back(const value_type& __v);
805
806 template <class... _Args>
807 _LIBCPP_HIDE_FROM_ABI iterator __emplace(const_iterator __p, _Args&&... __args);
808
809# ifndef _LIBCPP_CXX03_LANG
810# if _LIBCPP_STD_VER >= 17
811 template <class... _Args>
812 _LIBCPP_HIDE_FROM_ABI reference emplace_front(_Args&&... __args);
813 template <class... _Args>
814 _LIBCPP_HIDE_FROM_ABI reference emplace_back(_Args&&... __args);
815# else
816 template <class... _Args>
817 _LIBCPP_HIDE_FROM_ABI void emplace_front(_Args&&... __args);
818 template <class... _Args>
819 _LIBCPP_HIDE_FROM_ABI void emplace_back(_Args&&... __args);
820# endif
821
822 template <class... _Args>
823 _LIBCPP_HIDE_FROM_ABI iterator emplace(const_iterator __p, _Args&&... __args) {
824 return __emplace(__p, std::forward<_Args>(__args)...);
825 }
826
827 _LIBCPP_HIDE_FROM_ABI void push_front(value_type&& __v);
828 _LIBCPP_HIDE_FROM_ABI void push_back(value_type&& __v);
829
830# if _LIBCPP_STD_VER >= 23
831 template <_ContainerCompatibleRange<_Tp> _Range>
832 _LIBCPP_HIDE_FROM_ABI void prepend_range(_Range&& __range) {
833 insert_range(begin(), std::forward<_Range>(__range));
834 }
835
836 template <_ContainerCompatibleRange<_Tp> _Range>
837 _LIBCPP_HIDE_FROM_ABI void append_range(_Range&& __range) {
838 if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
839 __append_with_size(ranges::begin(__range), ranges::distance(__range));
840 } else {
841 __append_with_sentinel(ranges::begin(__range), ranges::end(__range));
842 }
843 }
844# endif
845
846 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, value_type&& __v) { return __emplace(__p, std::move(__v)); }
847
848 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, initializer_list<value_type> __il) {
849 return insert(__p, __il.begin(), __il.end());
850 }
851# endif // _LIBCPP_CXX03_LANG
852 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, const value_type& __v) { return __emplace(__p, __v); }
853 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, size_type __n, const value_type& __v);
854 template <class _InputIter, __enable_if_t<__has_exactly_input_iterator_category<_InputIter>::value, int> = 0>
855 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, _InputIter __f, _InputIter __l);
856 template <class _ForwardIterator,
857 __enable_if_t<__has_exactly_forward_iterator_category<_ForwardIterator>::value, int> = 0>
858 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l);
859 template <class _BiIter, __enable_if_t<__has_bidirectional_iterator_category<_BiIter>::value, int> = 0>
860 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, _BiIter __f, _BiIter __l);
861
862# if _LIBCPP_STD_VER >= 23
863 template <_ContainerCompatibleRange<_Tp> _Range>
864 _LIBCPP_HIDE_FROM_ABI iterator insert_range(const_iterator __position, _Range&& __range) {
865 if constexpr (ranges::bidirectional_range<_Range>) {
866 auto __n = static_cast<size_type>(ranges::distance(__range));
867 return __insert_bidirectional(__position, ranges::begin(__range), ranges::end(__range), __n);
868
869 } else if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
870 auto __n = static_cast<size_type>(ranges::distance(__range));
871 return __insert_with_size(__position, ranges::begin(__range), __n);
872
873 } else {
874 return __insert_with_sentinel(__position, ranges::begin(__range), ranges::end(__range));
875 }
876 }
877# endif
878
879 _LIBCPP_HIDE_FROM_ABI void pop_front();
880 _LIBCPP_HIDE_FROM_ABI void pop_back();
881 _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __p);
882 _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __f, const_iterator __l);
883
884 _LIBCPP_HIDE_FROM_ABI void swap(deque& __c)
885# if _LIBCPP_STD_VER >= 14
886 _NOEXCEPT;
887# else
888 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<allocator_type>);
889# endif
890 _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT;
891
892 _LIBCPP_HIDE_FROM_ABI bool __invariants() const {
893 if (!__map_.__invariants())
894 return false;
895 if (__map_.size() >= size_type(-1) / __block_size)
896 return false;
897 for (__map_const_iterator __i = __map_.begin(), __e = __map_.end(); __i != __e; ++__i)
898 if (*__i == nullptr)
899 return false;
900 if (__map_.size() != 0) {
901 if (size() >= __map_.size() * __block_size)
902 return false;
903 if (__start_ >= __map_.size() * __block_size - size())
904 return false;
905 } else {
906 if (size() != 0)
907 return false;
908 if (__start_ != 0)
909 return false;
910 }
911 return true;
912 }
913
914 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(deque& __c)
915 _NOEXCEPT_(!__alloc_traits::propagate_on_container_move_assignment::value ||
916 is_nothrow_move_assignable<allocator_type>::value) {
917 __move_assign_alloc(__c, integral_constant<bool, __alloc_traits::propagate_on_container_move_assignment::value>());
918 }
919
920 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(deque& __c, true_type)
921 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) {
922 __alloc() = std::move(__c.__alloc());
923 }
924
925 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(deque&, false_type) _NOEXCEPT {}
926
927 _LIBCPP_HIDE_FROM_ABI void __move_assign(deque& __c)
928 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value&&
929 is_nothrow_move_assignable<allocator_type>::value) {
930 __map_ = std::move(__c.__map_);
931 __start_ = __c.__start_;
932 __size() = __c.size();
933 __move_assign_alloc(__c);
934 __c.__start_ = __c.__size() = 0;
935 }
936
937 _LIBCPP_HIDE_FROM_ABI static size_type __recommend_blocks(size_type __n) {
938 return __n / __block_size + (__n % __block_size != 0);
939 }
940 _LIBCPP_HIDE_FROM_ABI size_type __capacity() const {
941 return __map_.size() == 0 ? 0 : __map_.size() * __block_size - 1;
942 }
943 _LIBCPP_HIDE_FROM_ABI size_type __block_count() const { return __map_.size(); }
944
945 _LIBCPP_HIDE_FROM_ABI size_type __front_spare() const { return __start_; }
946 _LIBCPP_HIDE_FROM_ABI size_type __front_spare_blocks() const { return __front_spare() / __block_size; }
947 _LIBCPP_HIDE_FROM_ABI size_type __back_spare() const { return __capacity() - (__start_ + size()); }
948 _LIBCPP_HIDE_FROM_ABI size_type __back_spare_blocks() const { return __back_spare() / __block_size; }
949
950private:
951 enum __asan_annotation_type { __asan_unposion, __asan_poison };
952
953 enum __asan_annotation_place {
954 __asan_front_moved,
955 __asan_back_moved,
956 };
957
958 _LIBCPP_HIDE_FROM_ABI void __annotate_from_to(
959 size_type __beg,
960 size_type __end,
961 __asan_annotation_type __annotation_type,
962 __asan_annotation_place __place) const _NOEXCEPT {
963 (void)__beg;
964 (void)__end;
965 (void)__annotation_type;
966 (void)__place;
967# if _LIBCPP_ENABLE_ASAN_CONTAINER_CHECKS
968 // __beg - index of the first item to annotate
969 // __end - index behind the last item to annotate (so last item + 1)
970 // __annotation_type - __asan_unposion or __asan_poison
971 // __place - __asan_front_moved or __asan_back_moved
972 // Note: All indexes in __map_
973 if (__beg == __end)
974 return;
975 // __annotations_beg_map - first chunk which annotations we want to modify
976 // __annotations_end_map - last chunk which annotations we want to modify
977 // NOTE: if __end % __block_size == 0, __annotations_end_map points at the next block, which may not exist
978 __map_const_iterator __annotations_beg_map = __map_.begin() + __beg / __block_size;
979 __map_const_iterator __annotations_end_map = __map_.begin() + __end / __block_size;
980
981 bool const __poisoning = __annotation_type == __asan_poison;
982 // __old_c_beg_index - index of the first element in old container
983 // __old_c_end_index - index of the end of old container (last + 1)
984 // Note: may be outside the area we are annotating
985 size_t __old_c_beg_index = (__poisoning && __place == __asan_front_moved) ? __beg : __start_;
986 size_t __old_c_end_index = (__poisoning && __place == __asan_back_moved) ? __end : __start_ + size();
987 bool const __front = __place == __asan_front_moved;
988
989 if (__poisoning && empty()) {
990 // Special case: we shouldn't trust __start_
991 __old_c_beg_index = __beg;
992 __old_c_end_index = __end;
993 }
994 // __old_c_beg_map - memory block (chunk) with first element
995 // __old_c_end_map - memory block (chunk) with end of old container
996 // Note: if __old_c_end_index % __block_size == 0, __old_c_end_map points at the next block,
997 // which may not exist
998 __map_const_iterator __old_c_beg_map = __map_.begin() + __old_c_beg_index / __block_size;
999 __map_const_iterator __old_c_end_map = __map_.begin() + __old_c_end_index / __block_size;
1000
1001 // One edge (front/end) of the container was moved and one was not modified.
1002 // __new_edge_index - index of new edge
1003 // __new_edge_map - memory block (chunk) with new edge, it always equals to
1004 // __annotations_beg_map or __annotations_end_map
1005 // __old_edge_map - memory block (chunk) with old edge, it always equals to
1006 // __old_c_beg_map or __old_c_end_map
1007 size_t __new_edge_index = (__poisoning ^ __front) ? __beg : __end;
1008 __map_const_iterator __new_edge_map = __map_.begin() + __new_edge_index / __block_size;
1009 __map_const_iterator __old_edge_map = __front ? __old_c_end_map : __old_c_beg_map;
1010
1011 // We iterate over map pointers (chunks) and fully poison all memory blocks between the first and the last.
1012 // First and last chunk may be partially poisoned.
1013 // __annotate_end_map may point at not existing chunk, therefore we have to have a check for it.
1014 for (__map_const_iterator __map_it = __annotations_beg_map; __map_it <= __annotations_end_map; ++__map_it) {
1015 if (__map_it == __annotations_end_map && __end % __block_size == 0)
1016 // Chunk may not exist, but nothing to do here anyway
1017 break;
1018
1019 // The beginning and the end of the current memory block
1020 const void* __mem_beg = std::__to_address(*__map_it);
1021 const void* __mem_end = std::__to_address(*__map_it + __block_size);
1022
1023 // The beginning of memory-in-use in the memory block before container modification
1024 const void* __old_beg =
1025 (__map_it == __old_c_beg_map) ? std::__to_address(*__map_it + (__old_c_beg_index % __block_size)) : __mem_beg;
1026
1027 // The end of memory-in-use in the memory block before container modification
1028 const void* __old_end;
1029 if (__map_it < __old_c_beg_map || __map_it > __old_c_end_map || (!__poisoning && empty()))
1030 __old_end = __old_beg;
1031 else
1032 __old_end = (__map_it == __old_c_end_map)
1033 ? std::__to_address(*__map_it + (__old_c_end_index % __block_size))
1034 : __mem_end;
1035
1036 // New edge of the container in current memory block
1037 // If the edge is in a different chunk it points on corresponding end of the memory block
1038 const void* __new_edge;
1039 if (__map_it == __new_edge_map)
1040 __new_edge = std::__to_address(*__map_it + (__new_edge_index % __block_size));
1041 else
1042 __new_edge = (__poisoning ^ __front) ? __mem_beg : __mem_end;
1043
1044 // Not modified edge of the container
1045 // If the edge is in a different chunk it points on corresponding end of the memory block
1046 const void* __old_edge;
1047 if (__map_it == __old_edge_map)
1048 __old_edge = __front ? __old_end : __old_beg;
1049 else
1050 __old_edge = __front ? __mem_end : __mem_beg;
1051
1052 // __new_beg - the beginning of memory-in-use in the memory block after container modification
1053 // __new_end - the end of memory-in-use in the memory block after container modification
1054 const void* __new_beg = __front ? __new_edge : __old_edge;
1055 const void* __new_end = __front ? __old_edge : __new_edge;
1056
1057 std::__annotate_double_ended_contiguous_container<_Allocator>(
1058 __mem_beg, __mem_end, __old_beg, __old_end, __new_beg, __new_end);
1059 }
1060# endif // _LIBCPP_ENABLE_ASAN_CONTAINER_CHECKS
1061 }
1062
1063 _LIBCPP_HIDE_FROM_ABI void __annotate_new(size_type __current_size) const _NOEXCEPT {
1064 (void)__current_size;
1065# if _LIBCPP_ENABLE_ASAN_CONTAINER_CHECKS
1066 if (__current_size == 0)
1067 __annotate_from_to(0, __map_.size() * __block_size, __asan_poison, __asan_back_moved);
1068 else {
1069 __annotate_from_to(0, __start_, __asan_poison, __asan_front_moved);
1070 __annotate_from_to(__start_ + __current_size, __map_.size() * __block_size, __asan_poison, __asan_back_moved);
1071 }
1072# endif // _LIBCPP_ENABLE_ASAN_CONTAINER_CHECKS
1073 }
1074
1075 _LIBCPP_HIDE_FROM_ABI void __annotate_delete() const _NOEXCEPT {
1076# if _LIBCPP_ENABLE_ASAN_CONTAINER_CHECKS
1077 if (empty()) {
1078 for (size_t __i = 0; __i < __map_.size(); ++__i) {
1079 __annotate_whole_block(__i, __asan_unposion);
1080 }
1081 } else {
1082 __annotate_from_to(0, __start_, __asan_unposion, __asan_front_moved);
1083 __annotate_from_to(__start_ + size(), __map_.size() * __block_size, __asan_unposion, __asan_back_moved);
1084 }
1085# endif // _LIBCPP_ENABLE_ASAN_CONTAINER_CHECKS
1086 }
1087
1088 _LIBCPP_HIDE_FROM_ABI void __annotate_increase_front(size_type __n) const _NOEXCEPT {
1089 (void)__n;
1090# if _LIBCPP_ENABLE_ASAN_CONTAINER_CHECKS
1091 __annotate_from_to(__start_ - __n, __start_, __asan_unposion, __asan_front_moved);
1092# endif
1093 }
1094
1095 _LIBCPP_HIDE_FROM_ABI void __annotate_increase_back(size_type __n) const _NOEXCEPT {
1096 (void)__n;
1097# if _LIBCPP_ENABLE_ASAN_CONTAINER_CHECKS
1098 __annotate_from_to(__start_ + size(), __start_ + size() + __n, __asan_unposion, __asan_back_moved);
1099# endif
1100 }
1101
1102 _LIBCPP_HIDE_FROM_ABI void __annotate_shrink_front(size_type __old_size, size_type __old_start) const _NOEXCEPT {
1103 (void)__old_size;
1104 (void)__old_start;
1105# if _LIBCPP_ENABLE_ASAN_CONTAINER_CHECKS
1106 __annotate_from_to(__old_start, __old_start + (__old_size - size()), __asan_poison, __asan_front_moved);
1107# endif
1108 }
1109
1110 _LIBCPP_HIDE_FROM_ABI void __annotate_shrink_back(size_type __old_size, size_type __old_start) const _NOEXCEPT {
1111 (void)__old_size;
1112 (void)__old_start;
1113# if _LIBCPP_ENABLE_ASAN_CONTAINER_CHECKS
1114 __annotate_from_to(__old_start + size(), __old_start + __old_size, __asan_poison, __asan_back_moved);
1115# endif
1116 }
1117
1118 _LIBCPP_HIDE_FROM_ABI void __annotate_poison_block(const void* __beginning, const void* __end) const _NOEXCEPT {
1119 std::__annotate_double_ended_contiguous_container<_Allocator>(__beginning, __end, __beginning, __end, __end, __end);
1120 }
1121
1122 _LIBCPP_HIDE_FROM_ABI void
1123 __annotate_whole_block(size_t __block_index, __asan_annotation_type __annotation_type) const _NOEXCEPT {
1124 (void)__block_index;
1125 (void)__annotation_type;
1126# if _LIBCPP_ENABLE_ASAN_CONTAINER_CHECKS
1127 __map_const_iterator __block_it = __map_.begin() + __block_index;
1128 const void* __block_start = std::__to_address(*__block_it);
1129 const void* __block_end = std::__to_address(*__block_it + __block_size);
1130
1131 if (__annotation_type == __asan_poison)
1132 __annotate_poison_block(__block_start, __block_end);
1133 else {
1134 std::__annotate_double_ended_contiguous_container<_Allocator>(
1135 __block_start, __block_end, __block_start, __block_start, __block_start, __block_end);
1136 }
1137# endif
1138 }
1139# if _LIBCPP_ENABLE_ASAN_CONTAINER_CHECKS
1140
1141public:
1142 _LIBCPP_HIDE_FROM_ABI bool __verify_asan_annotations() const _NOEXCEPT {
1143 // This function tests deque object annotations.
1144 if (empty()) {
1145 for (__map_const_iterator __it = __map_.begin(); __it != __map_.end(); ++__it) {
1146 if (!::__sanitizer_verify_double_ended_contiguous_container(
1147 std::__to_address(*__it),
1148 std::__to_address(*__it),
1149 std::__to_address(*__it),
1150 std::__to_address(*__it + __block_size)))
1151 return false;
1152 }
1153
1154 return true;
1155 }
1156
1157 size_type __end = __start_ + size();
1158 __map_const_iterator __first_mp = __map_.begin() + __start_ / __block_size;
1159 __map_const_iterator __last_mp = __map_.begin() + (__end - 1) / __block_size;
1160
1161 // Pointers to first and after last elements
1162 // Those can be in different deque blocks
1163 const void* __p_beg = std::__to_address(*__first_mp + (__start_ % __block_size));
1164 const void* __p_end =
1165 std::__to_address(*__last_mp + ((__end % __block_size == 0) ? __block_size : __end % __block_size));
1166
1167 for (__map_const_iterator __it = __map_.begin(); __it != __map_.end(); ++__it) {
1168 // Go over all blocks, find the place we are in and verify its annotations
1169 // Note that __p_end points *behind* the last item.
1170
1171 // - blocks before the first block with container elements
1172 // - first block with items
1173 // - last block with items
1174 // - blocks after last block with ciontainer elements
1175
1176 // Is the block before or after deque blocks that contain elements?
1177 if (__it < __first_mp || __it > __last_mp) {
1178 if (!::__sanitizer_verify_double_ended_contiguous_container(
1179 std::__to_address(*__it),
1180 std::__to_address(*__it),
1181 std::__to_address(*__it),
1182 std::__to_address(*__it + __block_size)))
1183 return false;
1184 } else {
1185 const void* __containers_buffer_beg = (__it == __first_mp) ? __p_beg : (const void*)std::__to_address(*__it);
1186 const void* __containers_buffer_end =
1187 (__it == __last_mp) ? __p_end : (const void*)std::__to_address(*__it + __block_size);
1188 if (!::__sanitizer_verify_double_ended_contiguous_container(
1189 std::__to_address(*__it),
1190 __containers_buffer_beg,
1191 __containers_buffer_end,
1192 std::__to_address(*__it + __block_size))) {
1193 return false;
1194 }
1195 }
1196 }
1197 return true;
1198 }
1199
1200private:
1201# endif // _LIBCPP_ENABLE_ASAN_CONTAINER_CHECKS
1202 _LIBCPP_HIDE_FROM_ABI bool __maybe_remove_front_spare(bool __keep_one = true) {
1203 if (__front_spare_blocks() >= 2 || (!__keep_one && __front_spare_blocks())) {
1204 __annotate_whole_block(block_index: 0, annotation_type: __asan_unposion);
1205 __alloc_traits::deallocate(__alloc(), __map_.front(), __block_size);
1206 __map_.pop_front();
1207 __start_ -= __block_size;
1208 return true;
1209 }
1210 return false;
1211 }
1212
1213 _LIBCPP_HIDE_FROM_ABI bool __maybe_remove_back_spare(bool __keep_one = true) {
1214 if (__back_spare_blocks() >= 2 || (!__keep_one && __back_spare_blocks())) {
1215 __annotate_whole_block(block_index: __map_.size() - 1, annotation_type: __asan_unposion);
1216 __alloc_traits::deallocate(__alloc(), __map_.back(), __block_size);
1217 __map_.pop_back();
1218 return true;
1219 }
1220 return false;
1221 }
1222
1223 template <class _Iterator, class _Sentinel>
1224 _LIBCPP_HIDE_FROM_ABI void __assign_with_sentinel(_Iterator __f, _Sentinel __l);
1225
1226 template <class _RandomAccessIterator>
1227 _LIBCPP_HIDE_FROM_ABI void __assign_with_size_random_access(_RandomAccessIterator __f, difference_type __n);
1228 template <class _AlgPolicy, class _Iterator>
1229 _LIBCPP_HIDE_FROM_ABI void __assign_with_size(_Iterator __f, difference_type __n);
1230
1231 template <class _Iterator, class _Sentinel>
1232 _LIBCPP_HIDE_FROM_ABI iterator __insert_with_sentinel(const_iterator __p, _Iterator __f, _Sentinel __l);
1233
1234 template <class _Iterator>
1235 _LIBCPP_HIDE_FROM_ABI iterator __insert_with_size(const_iterator __p, _Iterator __f, size_type __n);
1236
1237 template <class _BiIter, class _Sentinel>
1238 _LIBCPP_HIDE_FROM_ABI iterator
1239 __insert_bidirectional(const_iterator __p, _BiIter __f, _Sentinel __sent, size_type __n);
1240 template <class _BiIter>
1241 _LIBCPP_HIDE_FROM_ABI iterator __insert_bidirectional(const_iterator __p, _BiIter __f, _BiIter __l, size_type __n);
1242
1243 template <class _InpIter, __enable_if_t<__has_exactly_input_iterator_category<_InpIter>::value, int> = 0>
1244 _LIBCPP_HIDE_FROM_ABI void __append(_InpIter __f, _InpIter __l);
1245 template <class _ForIter, __enable_if_t<__has_forward_iterator_category<_ForIter>::value, int> = 0>
1246 _LIBCPP_HIDE_FROM_ABI void __append(_ForIter __f, _ForIter __l);
1247
1248 template <class _InputIterator>
1249 _LIBCPP_HIDE_FROM_ABI void __append_with_size(_InputIterator __from, size_type __n);
1250 template <class _InputIterator, class _Sentinel>
1251 _LIBCPP_HIDE_FROM_ABI void __append_with_sentinel(_InputIterator __f, _Sentinel __l);
1252
1253 _LIBCPP_HIDE_FROM_ABI void __append(size_type __n);
1254 _LIBCPP_HIDE_FROM_ABI void __append(size_type __n, const value_type& __v);
1255 _LIBCPP_HIDE_FROM_ABI void __erase_to_end(const_iterator __f);
1256 _LIBCPP_HIDE_FROM_ABI void __add_front_capacity();
1257 _LIBCPP_HIDE_FROM_ABI void __add_front_capacity(size_type __n);
1258 _LIBCPP_HIDE_FROM_ABI void __add_back_capacity();
1259 _LIBCPP_HIDE_FROM_ABI void __add_back_capacity(size_type __n);
1260 _LIBCPP_HIDE_FROM_ABI iterator __move_and_check(iterator __f, iterator __l, iterator __r, const_pointer& __vt);
1261 _LIBCPP_HIDE_FROM_ABI iterator
1262 __move_backward_and_check(iterator __f, iterator __l, iterator __r, const_pointer& __vt);
1263 _LIBCPP_HIDE_FROM_ABI void __move_construct_and_check(iterator __f, iterator __l, iterator __r, const_pointer& __vt);
1264 _LIBCPP_HIDE_FROM_ABI void
1265 __move_construct_backward_and_check(iterator __f, iterator __l, iterator __r, const_pointer& __vt);
1266
1267 _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const deque& __c) {
1268 __copy_assign_alloc(__c, integral_constant<bool, __alloc_traits::propagate_on_container_copy_assignment::value>());
1269 }
1270
1271 _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const deque& __c, true_type) {
1272 if (__alloc() != __c.__alloc()) {
1273 clear();
1274 shrink_to_fit();
1275 }
1276 __alloc() = __c.__alloc();
1277 __map_.__get_allocator() = __c.__map_.__get_allocator();
1278 }
1279
1280 _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const deque&, false_type) {}
1281
1282 _LIBCPP_HIDE_FROM_ABI void __move_assign(deque& __c, true_type)
1283 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
1284 _LIBCPP_HIDE_FROM_ABI void __move_assign(deque& __c, false_type);
1285};
1286
1287template <class _Tp, class _Alloc>
1288_LIBCPP_CONSTEXPR const typename allocator_traits<_Alloc>::difference_type deque<_Tp, _Alloc>::__block_size =
1289 __deque_block_size<value_type, difference_type>::value;
1290
1291# if _LIBCPP_STD_VER >= 17
1292template <class _InputIterator,
1293 class _Alloc = allocator<__iterator_value_type<_InputIterator>>,
1294 class = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
1295 class = enable_if_t<__is_allocator_v<_Alloc>>>
1296deque(_InputIterator, _InputIterator) -> deque<__iterator_value_type<_InputIterator>, _Alloc>;
1297
1298template <class _InputIterator,
1299 class _Alloc,
1300 class = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
1301 class = enable_if_t<__is_allocator_v<_Alloc>>>
1302deque(_InputIterator, _InputIterator, _Alloc) -> deque<__iterator_value_type<_InputIterator>, _Alloc>;
1303# endif
1304
1305# if _LIBCPP_STD_VER >= 23
1306template <ranges::input_range _Range,
1307 class _Alloc = allocator<ranges::range_value_t<_Range>>,
1308 class = enable_if_t<__is_allocator_v<_Alloc>>>
1309deque(from_range_t, _Range&&, _Alloc = _Alloc()) -> deque<ranges::range_value_t<_Range>, _Alloc>;
1310# endif
1311
1312template <class _Tp, class _Allocator>
1313deque<_Tp, _Allocator>::deque(size_type __n) : __start_(0), __size_(0) {
1314 __annotate_new(current_size: 0);
1315 if (__n > 0)
1316 __append(__n);
1317}
1318
1319# if _LIBCPP_STD_VER >= 14
1320template <class _Tp, class _Allocator>
1321deque<_Tp, _Allocator>::deque(size_type __n, const _Allocator& __a)
1322 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0), __alloc_(__a) {
1323 __annotate_new(current_size: 0);
1324 if (__n > 0)
1325 __append(__n);
1326}
1327# endif
1328
1329template <class _Tp, class _Allocator>
1330deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v) : __start_(0), __size_(0) {
1331 __annotate_new(current_size: 0);
1332 if (__n > 0)
1333 __append(__n, __v);
1334}
1335
1336template <class _Tp, class _Allocator>
1337template <class _InputIter, __enable_if_t<__has_input_iterator_category<_InputIter>::value, int> >
1338deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l) : __start_(0), __size_(0) {
1339 __annotate_new(current_size: 0);
1340 __append(__f, __l);
1341}
1342
1343template <class _Tp, class _Allocator>
1344template <class _InputIter, __enable_if_t<__has_input_iterator_category<_InputIter>::value, int> >
1345deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l, const allocator_type& __a)
1346 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0), __alloc_(__a) {
1347 __annotate_new(current_size: 0);
1348 __append(__f, __l);
1349}
1350
1351template <class _Tp, class _Allocator>
1352deque<_Tp, _Allocator>::deque(const deque& __c)
1353 : __map_(__pointer_allocator(__alloc_traits::select_on_container_copy_construction(__c.__alloc()))),
1354 __start_(0),
1355 __size_(0),
1356 __alloc_(__map_.__get_allocator()) {
1357 __annotate_new(current_size: 0);
1358 __append(__c.begin(), __c.end());
1359}
1360
1361template <class _Tp, class _Allocator>
1362deque<_Tp, _Allocator>::deque(const deque& __c, const __type_identity_t<allocator_type>& __a)
1363 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0), __alloc_(__a) {
1364 __annotate_new(current_size: 0);
1365 __append(__c.begin(), __c.end());
1366}
1367
1368template <class _Tp, class _Allocator>
1369deque<_Tp, _Allocator>& deque<_Tp, _Allocator>::operator=(const deque& __c) {
1370 if (this != std::addressof(__c)) {
1371 __copy_assign_alloc(__c);
1372 assign(__c.begin(), __c.end());
1373 }
1374 return *this;
1375}
1376
1377# ifndef _LIBCPP_CXX03_LANG
1378
1379template <class _Tp, class _Allocator>
1380deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il) : __start_(0), __size_(0) {
1381 __annotate_new(current_size: 0);
1382 __append(__il.begin(), __il.end());
1383}
1384
1385template <class _Tp, class _Allocator>
1386deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il, const allocator_type& __a)
1387 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0), __alloc_(__a) {
1388 __annotate_new(current_size: 0);
1389 __append(__il.begin(), __il.end());
1390}
1391
1392template <class _Tp, class _Allocator>
1393inline deque<_Tp, _Allocator>::deque(deque&& __c) noexcept(is_nothrow_move_constructible<allocator_type>::value)
1394 : __map_(std::move(__c.__map_)),
1395 __start_(std::move(__c.__start_)),
1396 __size_(std::move(__c.__size_)),
1397 __alloc_(std::move(__c.__alloc_)) {
1398 __c.__start_ = 0;
1399 __c.__size() = 0;
1400}
1401
1402template <class _Tp, class _Allocator>
1403inline deque<_Tp, _Allocator>::deque(deque&& __c, const __type_identity_t<allocator_type>& __a)
1404 : __map_(std::move(__c.__map_), __pointer_allocator(__a)),
1405 __start_(std::move(__c.__start_)),
1406 __size_(std::move(__c.__size_)),
1407 __alloc_(__a) {
1408 if (__a == __c.__alloc()) {
1409 __c.__start_ = 0;
1410 __c.__size() = 0;
1411 } else {
1412 __map_.clear();
1413 __start_ = 0;
1414 __size() = 0;
1415 typedef move_iterator<iterator> _Ip;
1416 assign(_Ip(__c.begin()), _Ip(__c.end()));
1417 }
1418}
1419
1420template <class _Tp, class _Allocator>
1421inline deque<_Tp, _Allocator>& deque<_Tp, _Allocator>::operator=(deque&& __c) noexcept(
1422 (__alloc_traits::propagate_on_container_move_assignment::value &&
1423 is_nothrow_move_assignable<allocator_type>::value) ||
1424 __alloc_traits::is_always_equal::value) {
1425 __move_assign(__c, integral_constant<bool, __alloc_traits::propagate_on_container_move_assignment::value>());
1426 return *this;
1427}
1428
1429template <class _Tp, class _Allocator>
1430void deque<_Tp, _Allocator>::__move_assign(deque& __c, false_type) {
1431 if (__alloc() != __c.__alloc()) {
1432 typedef move_iterator<iterator> _Ip;
1433 assign(_Ip(__c.begin()), _Ip(__c.end()));
1434 } else
1435 __move_assign(__c, true_type());
1436}
1437
1438template <class _Tp, class _Allocator>
1439void deque<_Tp, _Allocator>::__move_assign(deque& __c,
1440 true_type) noexcept(is_nothrow_move_assignable<allocator_type>::value) {
1441 clear();
1442 shrink_to_fit();
1443 __move_assign(__c);
1444}
1445
1446# endif // _LIBCPP_CXX03_LANG
1447
1448template <class _Tp, class _Allocator>
1449template <class _InputIter,
1450 __enable_if_t<__has_input_iterator_category<_InputIter>::value &&
1451 !__has_random_access_iterator_category<_InputIter>::value,
1452 int> >
1453void deque<_Tp, _Allocator>::assign(_InputIter __f, _InputIter __l) {
1454 __assign_with_sentinel(__f, __l);
1455}
1456
1457template <class _Tp, class _Allocator>
1458template <class _Iterator, class _Sentinel>
1459_LIBCPP_HIDE_FROM_ABI void deque<_Tp, _Allocator>::__assign_with_sentinel(_Iterator __f, _Sentinel __l) {
1460 iterator __i = begin();
1461 iterator __e = end();
1462 for (; __f != __l && __i != __e; ++__f, (void)++__i)
1463 *__i = *__f;
1464 if (__f != __l)
1465 __append_with_sentinel(std::move(__f), std::move(__l));
1466 else
1467 __erase_to_end(f: __i);
1468}
1469
1470template <class _Tp, class _Allocator>
1471template <class _RAIter, __enable_if_t<__has_random_access_iterator_category<_RAIter>::value, int> >
1472void deque<_Tp, _Allocator>::assign(_RAIter __f, _RAIter __l) {
1473 __assign_with_size_random_access(__f, __l - __f);
1474}
1475
1476template <class _Tp, class _Allocator>
1477template <class _RandomAccessIterator>
1478_LIBCPP_HIDE_FROM_ABI void
1479deque<_Tp, _Allocator>::__assign_with_size_random_access(_RandomAccessIterator __f, difference_type __n) {
1480 if (static_cast<size_type>(__n) > size()) {
1481 auto __l = __f + size();
1482 std::copy(__f, __l, begin());
1483 __append_with_size(__l, __n - size());
1484 } else
1485 __erase_to_end(f: std::copy_n(__f, __n, begin()));
1486}
1487
1488template <class _Tp, class _Allocator>
1489void deque<_Tp, _Allocator>::assign(size_type __n, const value_type& __v) {
1490 if (__n > size()) {
1491 std::fill_n(begin(), size(), __v);
1492 __n -= size();
1493 __append(__n, __v);
1494 } else
1495 __erase_to_end(f: std::fill_n(begin(), __n, __v));
1496}
1497
1498template <class _Tp, class _Allocator>
1499inline _Allocator deque<_Tp, _Allocator>::get_allocator() const _NOEXCEPT {
1500 return __alloc();
1501}
1502
1503template <class _Tp, class _Allocator>
1504void deque<_Tp, _Allocator>::resize(size_type __n) {
1505 if (__n > size())
1506 __append(__n - size());
1507 else if (__n < size())
1508 __erase_to_end(f: begin() + __n);
1509}
1510
1511template <class _Tp, class _Allocator>
1512void deque<_Tp, _Allocator>::resize(size_type __n, const value_type& __v) {
1513 if (__n > size())
1514 __append(__n - size(), __v);
1515 else if (__n < size())
1516 __erase_to_end(f: begin() + __n);
1517}
1518
1519template <class _Tp, class _Allocator>
1520void deque<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT {
1521 allocator_type& __a = __alloc();
1522 if (empty()) {
1523 __annotate_delete();
1524 while (__map_.size() > 0) {
1525 __alloc_traits::deallocate(__a, __map_.back(), __block_size);
1526 __map_.pop_back();
1527 }
1528 __start_ = 0;
1529 } else {
1530 __maybe_remove_front_spare(/*__keep_one=*/keep_one: false);
1531 __maybe_remove_back_spare(/*__keep_one=*/keep_one: false);
1532 }
1533 __map_.shrink_to_fit();
1534}
1535
1536template <class _Tp, class _Allocator>
1537inline typename deque<_Tp, _Allocator>::reference deque<_Tp, _Allocator>::operator[](size_type __i) _NOEXCEPT {
1538 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__i < size(), "deque::operator[] index out of bounds");
1539 size_type __p = __start_ + __i;
1540 return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
1541}
1542
1543template <class _Tp, class _Allocator>
1544inline typename deque<_Tp, _Allocator>::const_reference
1545deque<_Tp, _Allocator>::operator[](size_type __i) const _NOEXCEPT {
1546 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__i < size(), "deque::operator[] index out of bounds");
1547 size_type __p = __start_ + __i;
1548 return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
1549}
1550
1551template <class _Tp, class _Allocator>
1552inline typename deque<_Tp, _Allocator>::reference deque<_Tp, _Allocator>::at(size_type __i) {
1553 if (__i >= size())
1554 std::__throw_out_of_range(msg: "deque");
1555 size_type __p = __start_ + __i;
1556 return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
1557}
1558
1559template <class _Tp, class _Allocator>
1560inline typename deque<_Tp, _Allocator>::const_reference deque<_Tp, _Allocator>::at(size_type __i) const {
1561 if (__i >= size())
1562 std::__throw_out_of_range(msg: "deque");
1563 size_type __p = __start_ + __i;
1564 return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
1565}
1566
1567template <class _Tp, class _Allocator>
1568inline typename deque<_Tp, _Allocator>::reference deque<_Tp, _Allocator>::front() _NOEXCEPT {
1569 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "deque::front called on an empty deque");
1570 return *(*(__map_.begin() + __start_ / __block_size) + __start_ % __block_size);
1571}
1572
1573template <class _Tp, class _Allocator>
1574inline typename deque<_Tp, _Allocator>::const_reference deque<_Tp, _Allocator>::front() const _NOEXCEPT {
1575 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "deque::front called on an empty deque");
1576 return *(*(__map_.begin() + __start_ / __block_size) + __start_ % __block_size);
1577}
1578
1579template <class _Tp, class _Allocator>
1580inline typename deque<_Tp, _Allocator>::reference deque<_Tp, _Allocator>::back() _NOEXCEPT {
1581 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "deque::back called on an empty deque");
1582 size_type __p = size() + __start_ - 1;
1583 return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
1584}
1585
1586template <class _Tp, class _Allocator>
1587inline typename deque<_Tp, _Allocator>::const_reference deque<_Tp, _Allocator>::back() const _NOEXCEPT {
1588 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "deque::back called on an empty deque");
1589 size_type __p = size() + __start_ - 1;
1590 return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
1591}
1592
1593template <class _Tp, class _Allocator>
1594void deque<_Tp, _Allocator>::push_back(const value_type& __v) {
1595 allocator_type& __a = __alloc();
1596 if (__back_spare() == 0)
1597 __add_back_capacity();
1598 // __back_spare() >= 1
1599 __annotate_increase_back(n: 1);
1600 __alloc_traits::construct(__a, std::addressof(*end()), __v);
1601 ++__size();
1602}
1603
1604template <class _Tp, class _Allocator>
1605void deque<_Tp, _Allocator>::push_front(const value_type& __v) {
1606 allocator_type& __a = __alloc();
1607 if (__front_spare() == 0)
1608 __add_front_capacity();
1609 // __front_spare() >= 1
1610 __annotate_increase_front(n: 1);
1611 __alloc_traits::construct(__a, std::addressof(*--begin()), __v);
1612 --__start_;
1613 ++__size();
1614}
1615
1616# ifndef _LIBCPP_CXX03_LANG
1617template <class _Tp, class _Allocator>
1618void deque<_Tp, _Allocator>::push_back(value_type&& __v) {
1619 allocator_type& __a = __alloc();
1620 if (__back_spare() == 0)
1621 __add_back_capacity();
1622 // __back_spare() >= 1
1623 __annotate_increase_back(n: 1);
1624 __alloc_traits::construct(__a, std::addressof(*end()), std::move(__v));
1625 ++__size();
1626}
1627
1628template <class _Tp, class _Allocator>
1629template <class... _Args>
1630# if _LIBCPP_STD_VER >= 17
1631typename deque<_Tp, _Allocator>::reference
1632# else
1633void
1634# endif
1635deque<_Tp, _Allocator>::emplace_back(_Args&&... __args) {
1636 allocator_type& __a = __alloc();
1637 if (__back_spare() == 0)
1638 __add_back_capacity();
1639 // __back_spare() >= 1
1640 __annotate_increase_back(n: 1);
1641 __alloc_traits::construct(__a, std::addressof(*end()), std::forward<_Args>(__args)...);
1642 ++__size();
1643# if _LIBCPP_STD_VER >= 17
1644 return *--end();
1645# endif
1646}
1647
1648template <class _Tp, class _Allocator>
1649void deque<_Tp, _Allocator>::push_front(value_type&& __v) {
1650 allocator_type& __a = __alloc();
1651 if (__front_spare() == 0)
1652 __add_front_capacity();
1653 // __front_spare() >= 1
1654 __annotate_increase_front(n: 1);
1655 __alloc_traits::construct(__a, std::addressof(*--begin()), std::move(__v));
1656 --__start_;
1657 ++__size();
1658}
1659
1660template <class _Tp, class _Allocator>
1661template <class... _Args>
1662# if _LIBCPP_STD_VER >= 17
1663typename deque<_Tp, _Allocator>::reference
1664# else
1665void
1666# endif
1667deque<_Tp, _Allocator>::emplace_front(_Args&&... __args) {
1668 allocator_type& __a = __alloc();
1669 if (__front_spare() == 0)
1670 __add_front_capacity();
1671 // __front_spare() >= 1
1672 __annotate_increase_front(n: 1);
1673 __alloc_traits::construct(__a, std::addressof(*--begin()), std::forward<_Args>(__args)...);
1674 --__start_;
1675 ++__size();
1676# if _LIBCPP_STD_VER >= 17
1677 return *begin();
1678# endif
1679}
1680# endif // _LIBCPP_CXX03_LANG
1681
1682template <class _Tp, class _Allocator>
1683template <class... _Args>
1684typename 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(n: 1);
1693 if (__pos == 0) {
1694 __alloc_traits::construct(__a, std::addressof(*--begin()), std::forward<_Args>(__args)...);
1695 --__start_;
1696 ++__size();
1697 } else {
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));
1702 --__start_;
1703 ++__size();
1704 if (__pos > 1)
1705 __b = std::move(std::next(__b), __b + __pos, __b);
1706 *__b = std::move(__tmp.get());
1707 }
1708 } else { // insert by shifting things forward
1709 if (__back_spare() == 0)
1710 __add_back_capacity();
1711 // __back_capacity >= 1
1712 __annotate_increase_back(n: 1);
1713 size_type __de = size() - __pos;
1714 if (__de == 0) {
1715 __alloc_traits::construct(__a, std::addressof(*end()), std::forward<_Args>(__args)...);
1716 ++__size();
1717 } else {
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));
1722 ++__size();
1723 if (__de > 1)
1724 __e = std::move_backward(__e - __de, __em1, __e);
1725 *--__e = std::move(__tmp.get());
1726 }
1727 }
1728 return begin() + __pos;
1729}
1730
1731template <class _Tp, class _Allocator>
1732typename deque<_Tp, _Allocator>::iterator
1733deque<_Tp, _Allocator>::insert(const_iterator __p, size_type __n, const value_type& __v) {
1734 size_type __pos = __p - begin();
1735 size_type __to_end = __size() - __pos;
1736 allocator_type& __a = __alloc();
1737 if (__pos < __to_end) { // insert by shifting things backward
1738 if (__n > __front_spare())
1739 __add_front_capacity(__n - __front_spare());
1740 // __n <= __front_spare()
1741 __annotate_increase_front(__n);
1742 iterator __old_begin = begin();
1743 iterator __i = __old_begin;
1744 if (__n > __pos) {
1745 for (size_type __m = __n - __pos; __m; --__m, --__start_, ++__size())
1746 __alloc_traits::construct(__a, std::addressof(*--__i), __v);
1747 __n = __pos;
1748 }
1749 if (__n > 0) {
1750 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1751 iterator __obn = __old_begin + __n;
1752 __move_construct_backward_and_check(f: __old_begin, l: __obn, r: __i, __vt);
1753 if (__n < __pos)
1754 __old_begin = __move_and_check(f: __obn, l: __old_begin + __pos, r: __old_begin, __vt);
1755 std::fill_n(__old_begin, __n, *__vt);
1756 }
1757 } else { // insert by shifting things forward
1758 size_type __back_capacity = __back_spare();
1759 if (__n > __back_capacity)
1760 __add_back_capacity(__n - __back_capacity);
1761 // __n <= __back_capacity
1762 __annotate_increase_back(__n);
1763 iterator __old_end = end();
1764 iterator __i = __old_end;
1765 size_type __de = size() - __pos;
1766 if (__n > __de) {
1767 for (size_type __m = __n - __de; __m; --__m, (void)++__i, ++__size())
1768 __alloc_traits::construct(__a, std::addressof(*__i), __v);
1769 __n = __de;
1770 }
1771 if (__n > 0) {
1772 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1773 iterator __oen = __old_end - __n;
1774 __move_construct_and_check(f: __oen, l: __old_end, r: __i, __vt);
1775 if (__n < __de)
1776 __old_end = __move_backward_and_check(f: __old_end - __de, l: __oen, r: __old_end, __vt);
1777 std::fill_n(__old_end - __n, __n, *__vt);
1778 }
1779 }
1780 return begin() + __pos;
1781}
1782
1783template <class _Tp, class _Allocator>
1784template <class _InputIter, __enable_if_t<__has_exactly_input_iterator_category<_InputIter>::value, int> >
1785typename deque<_Tp, _Allocator>::iterator
1786deque<_Tp, _Allocator>::insert(const_iterator __p, _InputIter __f, _InputIter __l) {
1787 return __insert_with_sentinel(__p, __f, __l);
1788}
1789
1790template <class _Tp, class _Allocator>
1791template <class _Iterator, class _Sentinel>
1792_LIBCPP_HIDE_FROM_ABI typename deque<_Tp, _Allocator>::iterator
1793deque<_Tp, _Allocator>::__insert_with_sentinel(const_iterator __p, _Iterator __f, _Sentinel __l) {
1794 __split_buffer<value_type, allocator_type> __buf(__alloc());
1795 __buf.__construct_at_end_with_sentinel(std::move(__f), std::move(__l));
1796 typedef typename __split_buffer<value_type, allocator_type>::iterator __bi;
1797 return insert(__p, move_iterator<__bi>(__buf.begin()), move_iterator<__bi>(__buf.end()));
1798}
1799
1800template <class _Tp, class _Allocator>
1801template <class _ForwardIterator, __enable_if_t<__has_exactly_forward_iterator_category<_ForwardIterator>::value, int> >
1802typename deque<_Tp, _Allocator>::iterator
1803deque<_Tp, _Allocator>::insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l) {
1804 return __insert_with_size(__p, __f, std::distance(__f, __l));
1805}
1806
1807template <class _Tp, class _Allocator>
1808template <class _Iterator>
1809_LIBCPP_HIDE_FROM_ABI typename deque<_Tp, _Allocator>::iterator
1810deque<_Tp, _Allocator>::__insert_with_size(const_iterator __p, _Iterator __f, size_type __n) {
1811 __split_buffer<value_type, allocator_type> __buf(__n, 0, __alloc());
1812 __buf.__construct_at_end_with_size(std::move(__f), __n);
1813 typedef typename __split_buffer<value_type, allocator_type>::iterator __fwd;
1814 return insert(__p, move_iterator<__fwd>(__buf.begin()), move_iterator<__fwd>(__buf.end()));
1815}
1816
1817template <class _Tp, class _Allocator>
1818template <class _BiIter, __enable_if_t<__has_bidirectional_iterator_category<_BiIter>::value, int> >
1819typename deque<_Tp, _Allocator>::iterator deque<_Tp, _Allocator>::insert(const_iterator __p, _BiIter __f, _BiIter __l) {
1820 return __insert_bidirectional(__p, __f, __l, std::distance(__f, __l));
1821}
1822
1823template <class _Tp, class _Allocator>
1824template <class _BiIter, class _Sentinel>
1825_LIBCPP_HIDE_FROM_ABI typename deque<_Tp, _Allocator>::iterator
1826deque<_Tp, _Allocator>::__insert_bidirectional(const_iterator __p, _BiIter __f, _Sentinel, size_type __n) {
1827 return __insert_bidirectional(__p, __f, std::next(__f, __n), __n);
1828}
1829
1830template <class _Tp, class _Allocator>
1831template <class _BiIter>
1832_LIBCPP_HIDE_FROM_ABI typename deque<_Tp, _Allocator>::iterator
1833deque<_Tp, _Allocator>::__insert_bidirectional(const_iterator __p, _BiIter __f, _BiIter __l, size_type __n) {
1834 size_type __pos = __p - begin();
1835 size_type __to_end = size() - __pos;
1836 allocator_type& __a = __alloc();
1837 if (__pos < __to_end) { // insert by shifting things backward
1838 if (__n > __front_spare())
1839 __add_front_capacity(__n - __front_spare());
1840 // __n <= __front_spare()
1841 __annotate_increase_front(__n);
1842 iterator __old_begin = begin();
1843 iterator __i = __old_begin;
1844 _BiIter __m = __f;
1845 if (__n > __pos) {
1846 __m = __pos < __n / 2 ? std::prev(__l, __pos) : std::next(__f, __n - __pos);
1847 for (_BiIter __j = __m; __j != __f; --__start_, ++__size())
1848 __alloc_traits::construct(__a, std::addressof(*--__i), *--__j);
1849 __n = __pos;
1850 }
1851 if (__n > 0) {
1852 iterator __obn = __old_begin + __n;
1853 for (iterator __j = __obn; __j != __old_begin;) {
1854 __alloc_traits::construct(__a, std::addressof(*--__i), std::move(*--__j));
1855 --__start_;
1856 ++__size();
1857 }
1858 if (__n < __pos)
1859 __old_begin = std::move(__obn, __old_begin + __pos, __old_begin);
1860 std::copy(__m, __l, __old_begin);
1861 }
1862 } else { // insert by shifting things forward
1863 size_type __back_capacity = __back_spare();
1864 if (__n > __back_capacity)
1865 __add_back_capacity(__n - __back_capacity);
1866 // __n <= __back_capacity
1867 __annotate_increase_back(__n);
1868 iterator __old_end = end();
1869 iterator __i = __old_end;
1870 _BiIter __m = __l;
1871 size_type __de = size() - __pos;
1872 if (__n > __de) {
1873 __m = __de < __n / 2 ? std::next(__f, __de) : std::prev(__l, __n - __de);
1874 for (_BiIter __j = __m; __j != __l; ++__i, (void)++__j, ++__size())
1875 __alloc_traits::construct(__a, std::addressof(*__i), *__j);
1876 __n = __de;
1877 }
1878 if (__n > 0) {
1879 iterator __oen = __old_end - __n;
1880 for (iterator __j = __oen; __j != __old_end; ++__i, (void)++__j, ++__size())
1881 __alloc_traits::construct(__a, std::addressof(*__i), std::move(*__j));
1882 if (__n < __de)
1883 __old_end = std::move_backward(__old_end - __de, __oen, __old_end);
1884 std::copy_backward(__f, __m, __old_end);
1885 }
1886 }
1887 return begin() + __pos;
1888}
1889
1890template <class _Tp, class _Allocator>
1891template <class _InpIter, __enable_if_t<__has_exactly_input_iterator_category<_InpIter>::value, int> >
1892void deque<_Tp, _Allocator>::__append(_InpIter __f, _InpIter __l) {
1893 __append_with_sentinel(__f, __l);
1894}
1895
1896template <class _Tp, class _Allocator>
1897template <class _InputIterator, class _Sentinel>
1898_LIBCPP_HIDE_FROM_ABI void deque<_Tp, _Allocator>::__append_with_sentinel(_InputIterator __f, _Sentinel __l) {
1899 for (; __f != __l; ++__f)
1900# ifdef _LIBCPP_CXX03_LANG
1901 push_back(*__f);
1902# else
1903 emplace_back(*__f);
1904# endif
1905}
1906
1907template <class _Tp, class _Allocator>
1908template <class _ForIter, __enable_if_t<__has_forward_iterator_category<_ForIter>::value, int> >
1909void deque<_Tp, _Allocator>::__append(_ForIter __f, _ForIter __l) {
1910 __append_with_size(__f, std::distance(__f, __l));
1911}
1912
1913template <class _Tp, class _Allocator>
1914template <class _InputIterator>
1915_LIBCPP_HIDE_FROM_ABI void deque<_Tp, _Allocator>::__append_with_size(_InputIterator __f, size_type __n) {
1916 allocator_type& __a = __alloc();
1917 size_type __back_capacity = __back_spare();
1918 if (__n > __back_capacity)
1919 __add_back_capacity(__n - __back_capacity);
1920
1921 // __n <= __back_capacity
1922 __annotate_increase_back(__n);
1923 for (__deque_block_range __br : __deque_range(end(), end() + __n)) {
1924 _ConstructTransaction __tx(this, __br);
1925 for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, (void)++__f) {
1926 __alloc_traits::construct(__a, std::__to_address(__tx.__pos_), *__f);
1927 }
1928 }
1929}
1930
1931template <class _Tp, class _Allocator>
1932void deque<_Tp, _Allocator>::__append(size_type __n) {
1933 allocator_type& __a = __alloc();
1934 size_type __back_capacity = __back_spare();
1935 if (__n > __back_capacity)
1936 __add_back_capacity(__n - __back_capacity);
1937 // __n <= __back_capacity
1938 __annotate_increase_back(__n);
1939 for (__deque_block_range __br : __deque_range(end(), end() + __n)) {
1940 _ConstructTransaction __tx(this, __br);
1941 for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
1942 __alloc_traits::construct(__a, std::__to_address(__tx.__pos_));
1943 }
1944 }
1945}
1946
1947template <class _Tp, class _Allocator>
1948void deque<_Tp, _Allocator>::__append(size_type __n, const value_type& __v) {
1949 allocator_type& __a = __alloc();
1950 size_type __back_capacity = __back_spare();
1951 if (__n > __back_capacity)
1952 __add_back_capacity(__n - __back_capacity);
1953 // __n <= __back_capacity
1954 __annotate_increase_back(__n);
1955 for (__deque_block_range __br : __deque_range(end(), end() + __n)) {
1956 _ConstructTransaction __tx(this, __br);
1957 for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
1958 __alloc_traits::construct(__a, std::__to_address(__tx.__pos_), __v);
1959 }
1960 }
1961}
1962
1963// Create front capacity for one block of elements.
1964// Strong guarantee. Either do it or don't touch anything.
1965template <class _Tp, class _Allocator>
1966void deque<_Tp, _Allocator>::__add_front_capacity() {
1967 allocator_type& __a = __alloc();
1968 if (__back_spare() >= __block_size) {
1969 __start_ += __block_size;
1970 pointer __pt = __map_.back();
1971 __map_.pop_back();
1972 __map_.emplace_front(__pt);
1973 }
1974 // Else if __map_.size() < __map_.capacity() then we need to allocate 1 buffer
1975 else if (__map_.size() < __map_.capacity()) { // we can put the new buffer into the map, but don't shift things around
1976 // until all buffers are allocated. If we throw, we don't need to fix
1977 // anything up (any added buffers are undetectible)
1978 if (__map_.__front_spare() > 0)
1979 __map_.emplace_front(__alloc_traits::allocate(__a, __block_size));
1980 else {
1981 __map_.emplace_back(__alloc_traits::allocate(__a, __block_size));
1982 // Done allocating, reorder capacity
1983 pointer __pt = __map_.back();
1984 __map_.pop_back();
1985 __map_.emplace_front(__pt);
1986 }
1987 __start_ = __map_.size() == 1 ? __block_size / 2 : __start_ + __block_size;
1988 }
1989 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
1990 else {
1991 __split_buffer<pointer, __pointer_allocator> __buf(
1992 std::max<size_type>(2 * __map_.capacity(), 1), 0, __map_.__get_allocator());
1993
1994 typedef __allocator_destructor<_Allocator> _Dp;
1995 unique_ptr<pointer, _Dp> __hold(__alloc_traits::allocate(__a, __block_size), _Dp(__a, __block_size));
1996 __buf.emplace_back(__hold.get());
1997 __hold.release();
1998
1999 for (__map_pointer __i = __map_.begin(); __i != __map_.end(); ++__i)
2000 __buf.emplace_back(*__i);
2001 __map_.__swap_without_allocator(__buf);
2002 __start_ = __map_.size() == 1 ? __block_size / 2 : __start_ + __block_size;
2003 }
2004 __annotate_whole_block(block_index: 0, annotation_type: __asan_poison);
2005}
2006
2007// Create front capacity for __n elements.
2008// Strong guarantee. Either do it or don't touch anything.
2009template <class _Tp, class _Allocator>
2010void deque<_Tp, _Allocator>::__add_front_capacity(size_type __n) {
2011 allocator_type& __a = __alloc();
2012 size_type __nb = __recommend_blocks(n: __n + __map_.empty());
2013 // Number of unused blocks at back:
2014 size_type __back_capacity = __back_spare() / __block_size;
2015 __back_capacity = std::min(__back_capacity, __nb); // don't take more than you need
2016 __nb -= __back_capacity; // number of blocks need to allocate
2017 // If __nb == 0, then we have sufficient capacity.
2018 if (__nb == 0) {
2019 __start_ += __block_size * __back_capacity;
2020 for (; __back_capacity > 0; --__back_capacity) {
2021 pointer __pt = __map_.back();
2022 __map_.pop_back();
2023 __map_.emplace_front(__pt);
2024 }
2025 }
2026 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2027 else if (__nb <= __map_.capacity() -
2028 __map_.size()) { // we can put the new buffers into the map, but don't shift things around
2029 // until all buffers are allocated. If we throw, we don't need to fix
2030 // anything up (any added buffers are undetectible)
2031 for (; __nb > 0; --__nb, __start_ += __block_size - (__map_.size() == 1)) {
2032 if (__map_.__front_spare() == 0)
2033 break;
2034 __map_.emplace_front(__alloc_traits::allocate(__a, __block_size));
2035 __annotate_whole_block(block_index: 0, annotation_type: __asan_poison);
2036 }
2037 for (; __nb > 0; --__nb, ++__back_capacity)
2038 __map_.emplace_back(__alloc_traits::allocate(__a, __block_size));
2039 // Done allocating, reorder capacity
2040 __start_ += __back_capacity * __block_size;
2041 for (; __back_capacity > 0; --__back_capacity) {
2042 pointer __pt = __map_.back();
2043 __map_.pop_back();
2044 __map_.emplace_front(__pt);
2045 __annotate_whole_block(block_index: 0, annotation_type: __asan_poison);
2046 }
2047 }
2048 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2049 else {
2050 size_type __ds = (__nb + __back_capacity) * __block_size - __map_.empty();
2051 __split_buffer<pointer, __pointer_allocator> __buf(
2052 std::max<size_type>(2 * __map_.capacity(), __nb + __map_.size()), 0, __map_.__get_allocator());
2053 auto __guard = std::__make_exception_guard([&] {
2054 __annotate_delete();
2055 for (__map_pointer __i = __buf.begin(); __i != __buf.end(); ++__i)
2056 __alloc_traits::deallocate(__a, *__i, __block_size);
2057 });
2058 for (; __nb > 0; --__nb) {
2059 __buf.emplace_back(__alloc_traits::allocate(__a, __block_size));
2060 // ASan: this is empty container, we have to poison whole block
2061 __annotate_poison_block(beginning: std::__to_address(__buf.back()), end: std::__to_address(__buf.back() + __block_size));
2062 }
2063 __guard.__complete();
2064 for (; __back_capacity > 0; --__back_capacity) {
2065 __buf.emplace_back(__map_.back());
2066 __map_.pop_back();
2067 }
2068 for (__map_pointer __i = __map_.begin(); __i != __map_.end(); ++__i)
2069 __buf.emplace_back(*__i);
2070 __map_.__swap_without_allocator(__buf);
2071 __start_ += __ds;
2072 }
2073}
2074
2075// Create back capacity for one block of elements.
2076// Strong guarantee. Either do it or don't touch anything.
2077template <class _Tp, class _Allocator>
2078void deque<_Tp, _Allocator>::__add_back_capacity() {
2079 allocator_type& __a = __alloc();
2080 if (__front_spare() >= __block_size) {
2081 __start_ -= __block_size;
2082 pointer __pt = __map_.front();
2083 __map_.pop_front();
2084 __map_.emplace_back(__pt);
2085 }
2086 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2087 else if (__map_.size() < __map_.capacity()) { // we can put the new buffer into the map, but don't shift things around
2088 // until it is allocated. If we throw, we don't need to fix
2089 // anything up (any added buffers are undetectible)
2090 if (__map_.__back_spare() != 0)
2091 __map_.emplace_back(__alloc_traits::allocate(__a, __block_size));
2092 else {
2093 __map_.emplace_front(__alloc_traits::allocate(__a, __block_size));
2094 // Done allocating, reorder capacity
2095 pointer __pt = __map_.front();
2096 __map_.pop_front();
2097 __map_.emplace_back(__pt);
2098 }
2099 __annotate_whole_block(block_index: __map_.size() - 1, annotation_type: __asan_poison);
2100 }
2101 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2102 else {
2103 __split_buffer<pointer, __pointer_allocator> __buf(
2104 std::max<size_type>(2 * __map_.capacity(), 1), __map_.size(), __map_.__get_allocator());
2105
2106 typedef __allocator_destructor<_Allocator> _Dp;
2107 unique_ptr<pointer, _Dp> __hold(__alloc_traits::allocate(__a, __block_size), _Dp(__a, __block_size));
2108 __buf.emplace_back(__hold.get());
2109 __hold.release();
2110
2111 for (__map_pointer __i = __map_.end(); __i != __map_.begin();)
2112 __buf.emplace_front(*--__i);
2113 __map_.__swap_without_allocator(__buf);
2114 __annotate_whole_block(block_index: __map_.size() - 1, annotation_type: __asan_poison);
2115 }
2116}
2117
2118// Create back capacity for __n elements.
2119// Strong guarantee. Either do it or don't touch anything.
2120template <class _Tp, class _Allocator>
2121void deque<_Tp, _Allocator>::__add_back_capacity(size_type __n) {
2122 allocator_type& __a = __alloc();
2123 size_type __nb = __recommend_blocks(n: __n + __map_.empty());
2124 // Number of unused blocks at front:
2125 size_type __front_capacity = __front_spare() / __block_size;
2126 __front_capacity = std::min(__front_capacity, __nb); // don't take more than you need
2127 __nb -= __front_capacity; // number of blocks need to allocate
2128 // If __nb == 0, then we have sufficient capacity.
2129 if (__nb == 0) {
2130 __start_ -= __block_size * __front_capacity;
2131 for (; __front_capacity > 0; --__front_capacity) {
2132 pointer __pt = __map_.front();
2133 __map_.pop_front();
2134 __map_.emplace_back(__pt);
2135 }
2136 }
2137 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2138 else if (__nb <= __map_.capacity() -
2139 __map_.size()) { // we can put the new buffers into the map, but don't shift things around
2140 // until all buffers are allocated. If we throw, we don't need to fix
2141 // anything up (any added buffers are undetectible)
2142 for (; __nb > 0; --__nb) {
2143 if (__map_.__back_spare() == 0)
2144 break;
2145 __map_.emplace_back(__alloc_traits::allocate(__a, __block_size));
2146 __annotate_whole_block(block_index: __map_.size() - 1, annotation_type: __asan_poison);
2147 }
2148 for (; __nb > 0; --__nb, ++__front_capacity, __start_ += __block_size - (__map_.size() == 1)) {
2149 __map_.emplace_front(__alloc_traits::allocate(__a, __block_size));
2150 __annotate_whole_block(block_index: 0, annotation_type: __asan_poison);
2151 }
2152 // Done allocating, reorder capacity
2153 __start_ -= __block_size * __front_capacity;
2154 for (; __front_capacity > 0; --__front_capacity) {
2155 pointer __pt = __map_.front();
2156 __map_.pop_front();
2157 __map_.emplace_back(__pt);
2158 }
2159 }
2160 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2161 else {
2162 size_type __ds = __front_capacity * __block_size;
2163 __split_buffer<pointer, __pointer_allocator> __buf(
2164 std::max<size_type>(2 * __map_.capacity(), __nb + __map_.size()),
2165 __map_.size() - __front_capacity,
2166 __map_.__get_allocator());
2167 auto __guard = std::__make_exception_guard([&] {
2168 __annotate_delete();
2169 for (__map_pointer __i = __buf.begin(); __i != __buf.end(); ++__i)
2170 __alloc_traits::deallocate(__a, *__i, __block_size);
2171 });
2172 for (; __nb > 0; --__nb) {
2173 __buf.emplace_back(__alloc_traits::allocate(__a, __block_size));
2174 // ASan: this is an empty container, we have to poison the whole block
2175 __annotate_poison_block(beginning: std::__to_address(__buf.back()), end: std::__to_address(__buf.back() + __block_size));
2176 }
2177 __guard.__complete();
2178 for (; __front_capacity > 0; --__front_capacity) {
2179 __buf.emplace_back(__map_.front());
2180 __map_.pop_front();
2181 }
2182 for (__map_pointer __i = __map_.end(); __i != __map_.begin();)
2183 __buf.emplace_front(*--__i);
2184 __map_.__swap_without_allocator(__buf);
2185 __start_ -= __ds;
2186 }
2187}
2188
2189template <class _Tp, class _Allocator>
2190void deque<_Tp, _Allocator>::pop_front() {
2191 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "deque::pop_front called on an empty deque");
2192 size_type __old_sz = size();
2193 size_type __old_start = __start_;
2194 allocator_type& __a = __alloc();
2195 __alloc_traits::destroy(
2196 __a, std::__to_address(*(__map_.begin() + __start_ / __block_size) + __start_ % __block_size));
2197 --__size();
2198 ++__start_;
2199 __annotate_shrink_front(old_size: __old_sz, __old_start);
2200 __maybe_remove_front_spare();
2201}
2202
2203template <class _Tp, class _Allocator>
2204void deque<_Tp, _Allocator>::pop_back() {
2205 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "deque::pop_back called on an empty deque");
2206 size_type __old_sz = size();
2207 size_type __old_start = __start_;
2208 allocator_type& __a = __alloc();
2209 size_type __p = size() + __start_ - 1;
2210 __alloc_traits::destroy(__a, std::__to_address(*(__map_.begin() + __p / __block_size) + __p % __block_size));
2211 --__size();
2212 __annotate_shrink_back(old_size: __old_sz, __old_start);
2213 __maybe_remove_back_spare();
2214}
2215
2216// move assign [__f, __l) to [__r, __r + (__l-__f)).
2217// If __vt points into [__f, __l), then subtract (__f - __r) from __vt.
2218template <class _Tp, class _Allocator>
2219typename deque<_Tp, _Allocator>::iterator
2220deque<_Tp, _Allocator>::__move_and_check(iterator __f, iterator __l, iterator __r, const_pointer& __vt) {
2221 // as if
2222 // for (; __f != __l; ++__f, ++__r)
2223 // *__r = std::move(*__f);
2224 difference_type __n = __l - __f;
2225 while (__n > 0) {
2226 pointer __fb = __f.__ptr_;
2227 pointer __fe = *__f.__m_iter_ + __block_size;
2228 difference_type __bs = __fe - __fb;
2229 if (__bs > __n) {
2230 __bs = __n;
2231 __fe = __fb + __bs;
2232 }
2233 if (__fb <= __vt && __vt < __fe)
2234 __vt = (const_iterator(__f.__m_iter_, __vt) -= __f - __r).__ptr_;
2235 __r = std::move(__fb, __fe, __r);
2236 __n -= __bs;
2237 __f += __bs;
2238 }
2239 return __r;
2240}
2241
2242// move assign [__f, __l) to [__r - (__l-__f), __r) backwards.
2243// If __vt points into [__f, __l), then add (__r - __l) to __vt.
2244template <class _Tp, class _Allocator>
2245typename deque<_Tp, _Allocator>::iterator
2246deque<_Tp, _Allocator>::__move_backward_and_check(iterator __f, iterator __l, iterator __r, const_pointer& __vt) {
2247 // as if
2248 // while (__f != __l)
2249 // *--__r = std::move(*--__l);
2250 difference_type __n = __l - __f;
2251 while (__n > 0) {
2252 --__l;
2253 pointer __lb = *__l.__m_iter_;
2254 pointer __le = __l.__ptr_ + 1;
2255 difference_type __bs = __le - __lb;
2256 if (__bs > __n) {
2257 __bs = __n;
2258 __lb = __le - __bs;
2259 }
2260 if (__lb <= __vt && __vt < __le)
2261 __vt = (const_iterator(__l.__m_iter_, __vt) += __r - __l - 1).__ptr_;
2262 __r = std::move_backward(__lb, __le, __r);
2263 __n -= __bs;
2264 __l -= __bs - 1;
2265 }
2266 return __r;
2267}
2268
2269// move construct [__f, __l) to [__r, __r + (__l-__f)).
2270// If __vt points into [__f, __l), then add (__r - __f) to __vt.
2271template <class _Tp, class _Allocator>
2272void deque<_Tp, _Allocator>::__move_construct_and_check(iterator __f, iterator __l, iterator __r, const_pointer& __vt) {
2273 allocator_type& __a = __alloc();
2274 // as if
2275 // for (; __f != __l; ++__r, ++__f, ++__size())
2276 // __alloc_traits::construct(__a, std::addressof(*__r), std::move(*__f));
2277 difference_type __n = __l - __f;
2278 while (__n > 0) {
2279 pointer __fb = __f.__ptr_;
2280 pointer __fe = *__f.__m_iter_ + __block_size;
2281 difference_type __bs = __fe - __fb;
2282 if (__bs > __n) {
2283 __bs = __n;
2284 __fe = __fb + __bs;
2285 }
2286 if (__fb <= __vt && __vt < __fe)
2287 __vt = (const_iterator(__f.__m_iter_, __vt) += __r - __f).__ptr_;
2288 for (; __fb != __fe; ++__fb, ++__r, ++__size())
2289 __alloc_traits::construct(__a, std::addressof(*__r), std::move(*__fb));
2290 __n -= __bs;
2291 __f += __bs;
2292 }
2293}
2294
2295// move construct [__f, __l) to [__r - (__l-__f), __r) backwards.
2296// If __vt points into [__f, __l), then subtract (__l - __r) from __vt.
2297template <class _Tp, class _Allocator>
2298void deque<_Tp, _Allocator>::__move_construct_backward_and_check(
2299 iterator __f, iterator __l, iterator __r, const_pointer& __vt) {
2300 allocator_type& __a = __alloc();
2301 // as if
2302 // for (iterator __j = __l; __j != __f;)
2303 // {
2304 // __alloc_traitsconstruct(__a, std::addressof(*--__r), std::move(*--__j));
2305 // --__start_;
2306 // ++__size();
2307 // }
2308 difference_type __n = __l - __f;
2309 while (__n > 0) {
2310 --__l;
2311 pointer __lb = *__l.__m_iter_;
2312 pointer __le = __l.__ptr_ + 1;
2313 difference_type __bs = __le - __lb;
2314 if (__bs > __n) {
2315 __bs = __n;
2316 __lb = __le - __bs;
2317 }
2318 if (__lb <= __vt && __vt < __le)
2319 __vt = (const_iterator(__l.__m_iter_, __vt) -= __l - __r + 1).__ptr_;
2320 while (__le != __lb) {
2321 __alloc_traits::construct(__a, std::addressof(*--__r), std::move(*--__le));
2322 --__start_;
2323 ++__size();
2324 }
2325 __n -= __bs;
2326 __l -= __bs - 1;
2327 }
2328}
2329
2330template <class _Tp, class _Allocator>
2331typename deque<_Tp, _Allocator>::iterator deque<_Tp, _Allocator>::erase(const_iterator __f) {
2332 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
2333 __f != end(), "deque::erase(iterator) called with a non-dereferenceable iterator");
2334 size_type __old_sz = size();
2335 size_type __old_start = __start_;
2336 iterator __b = begin();
2337 difference_type __pos = __f - __b;
2338 iterator __p = __b + __pos;
2339 allocator_type& __a = __alloc();
2340 if (static_cast<size_type>(__pos) <= (size() - 1) / 2) { // erase from front
2341 std::move_backward(__b, __p, std::next(__p));
2342 __alloc_traits::destroy(__a, std::addressof(*__b));
2343 --__size();
2344 ++__start_;
2345 __annotate_shrink_front(old_size: __old_sz, __old_start);
2346 __maybe_remove_front_spare();
2347 } else { // erase from back
2348 iterator __i = std::move(std::next(__p), end(), __p);
2349 __alloc_traits::destroy(__a, std::addressof(*__i));
2350 --__size();
2351 __annotate_shrink_back(old_size: __old_sz, __old_start);
2352 __maybe_remove_back_spare();
2353 }
2354 return begin() + __pos;
2355}
2356
2357template <class _Tp, class _Allocator>
2358typename deque<_Tp, _Allocator>::iterator deque<_Tp, _Allocator>::erase(const_iterator __f, const_iterator __l) {
2359 _LIBCPP_ASSERT_VALID_INPUT_RANGE(__f <= __l, "deque::erase(first, last) called with an invalid range");
2360 size_type __old_sz = size();
2361 size_type __old_start = __start_;
2362 difference_type __n = __l - __f;
2363 iterator __b = begin();
2364 difference_type __pos = __f - __b;
2365 iterator __p = __b + __pos;
2366 if (__n > 0) {
2367 allocator_type& __a = __alloc();
2368 if (static_cast<size_type>(__pos) <= (size() - __n) / 2) { // erase from front
2369 iterator __i = std::move_backward(__b, __p, __p + __n);
2370 for (; __b != __i; ++__b)
2371 __alloc_traits::destroy(__a, std::addressof(*__b));
2372 __size() -= __n;
2373 __start_ += __n;
2374 __annotate_shrink_front(old_size: __old_sz, __old_start);
2375 while (__maybe_remove_front_spare()) {
2376 }
2377 } else { // erase from back
2378 iterator __i = std::move(__p + __n, end(), __p);
2379 for (iterator __e = end(); __i != __e; ++__i)
2380 __alloc_traits::destroy(__a, std::addressof(*__i));
2381 __size() -= __n;
2382 __annotate_shrink_back(old_size: __old_sz, __old_start);
2383 while (__maybe_remove_back_spare()) {
2384 }
2385 }
2386 }
2387 return begin() + __pos;
2388}
2389
2390template <class _Tp, class _Allocator>
2391void deque<_Tp, _Allocator>::__erase_to_end(const_iterator __f) {
2392 size_type __old_sz = size();
2393 size_type __old_start = __start_;
2394 iterator __e = end();
2395 difference_type __n = __e - __f;
2396 if (__n > 0) {
2397 allocator_type& __a = __alloc();
2398 iterator __b = begin();
2399 difference_type __pos = __f - __b;
2400 for (iterator __p = __b + __pos; __p != __e; ++__p)
2401 __alloc_traits::destroy(__a, std::addressof(*__p));
2402 __size() -= __n;
2403 __annotate_shrink_back(old_size: __old_sz, __old_start);
2404 while (__maybe_remove_back_spare()) {
2405 }
2406 }
2407}
2408
2409template <class _Tp, class _Allocator>
2410inline void deque<_Tp, _Allocator>::swap(deque& __c)
2411# if _LIBCPP_STD_VER >= 14
2412 _NOEXCEPT
2413# else
2414 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<allocator_type>)
2415# endif
2416{
2417 __map_.swap(__c.__map_);
2418 std::swap(__start_, __c.__start_);
2419 std::swap(__size(), __c.__size());
2420 std::__swap_allocator(__alloc(), __c.__alloc());
2421}
2422
2423template <class _Tp, class _Allocator>
2424inline void deque<_Tp, _Allocator>::clear() _NOEXCEPT {
2425 __annotate_delete();
2426 allocator_type& __a = __alloc();
2427 for (iterator __i = begin(), __e = end(); __i != __e; ++__i)
2428 __alloc_traits::destroy(__a, std::addressof(*__i));
2429 __size() = 0;
2430 while (__map_.size() > 2) {
2431 __alloc_traits::deallocate(__a, __map_.front(), __block_size);
2432 __map_.pop_front();
2433 }
2434 switch (__map_.size()) {
2435 case 1:
2436 __start_ = __block_size / 2;
2437 break;
2438 case 2:
2439 __start_ = __block_size;
2440 break;
2441 }
2442 __annotate_new(current_size: 0);
2443}
2444
2445template <class _Tp, class _Allocator>
2446inline _LIBCPP_HIDE_FROM_ABI bool operator==(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) {
2447 const typename deque<_Tp, _Allocator>::size_type __sz = __x.size();
2448 return __sz == __y.size() && std::equal(__x.begin(), __x.end(), __y.begin());
2449}
2450
2451# if _LIBCPP_STD_VER <= 17
2452
2453template <class _Tp, class _Allocator>
2454inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) {
2455 return !(__x == __y);
2456}
2457
2458template <class _Tp, class _Allocator>
2459inline _LIBCPP_HIDE_FROM_ABI bool operator<(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) {
2460 return std::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
2461}
2462
2463template <class _Tp, class _Allocator>
2464inline _LIBCPP_HIDE_FROM_ABI bool operator>(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) {
2465 return __y < __x;
2466}
2467
2468template <class _Tp, class _Allocator>
2469inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) {
2470 return !(__x < __y);
2471}
2472
2473template <class _Tp, class _Allocator>
2474inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) {
2475 return !(__y < __x);
2476}
2477
2478# else // _LIBCPP_STD_VER <= 17
2479
2480template <class _Tp, class _Allocator>
2481_LIBCPP_HIDE_FROM_ABI __synth_three_way_result<_Tp>
2482operator<=>(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) {
2483 return std::lexicographical_compare_three_way(__x.begin(), __x.end(), __y.begin(), __y.end(), std::__synth_three_way);
2484}
2485
2486# endif // _LIBCPP_STD_VER <= 17
2487
2488template <class _Tp, class _Allocator>
2489inline _LIBCPP_HIDE_FROM_ABI void swap(deque<_Tp, _Allocator>& __x, deque<_Tp, _Allocator>& __y)
2490 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) {
2491 __x.swap(__y);
2492}
2493
2494# if _LIBCPP_STD_VER >= 20
2495template <class _Tp, class _Allocator, class _Up>
2496inline _LIBCPP_HIDE_FROM_ABI typename deque<_Tp, _Allocator>::size_type
2497erase(deque<_Tp, _Allocator>& __c, const _Up& __v) {
2498 auto __old_size = __c.size();
2499 __c.erase(std::remove(__c.begin(), __c.end(), __v), __c.end());
2500 return __old_size - __c.size();
2501}
2502
2503template <class _Tp, class _Allocator, class _Predicate>
2504inline _LIBCPP_HIDE_FROM_ABI typename deque<_Tp, _Allocator>::size_type
2505erase_if(deque<_Tp, _Allocator>& __c, _Predicate __pred) {
2506 auto __old_size = __c.size();
2507 __c.erase(std::remove_if(__c.begin(), __c.end(), __pred), __c.end());
2508 return __old_size - __c.size();
2509}
2510
2511template <>
2512inline constexpr bool __format::__enable_insertable<std::deque<char>> = true;
2513# if _LIBCPP_HAS_WIDE_CHARACTERS
2514template <>
2515inline constexpr bool __format::__enable_insertable<std::deque<wchar_t>> = true;
2516# endif
2517
2518# endif // _LIBCPP_STD_VER >= 20
2519
2520template <class _Tp, class _Allocator>
2521struct __container_traits<deque<_Tp, _Allocator> > {
2522 // http://eel.is/c++draft/deque.modifiers#3
2523 // If an exception is thrown other than by the copy constructor, move constructor, assignment operator, or move
2524 // assignment operator of T, there are no effects. If an exception is thrown while inserting a single element at
2525 // either end, there are no effects. Otherwise, if an exception is thrown by the move constructor of a
2526 // non-Cpp17CopyInsertable T, the effects are unspecified.
2527 static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee =
2528 is_nothrow_move_constructible<_Tp>::value || __is_cpp17_copy_insertable_v<_Allocator>;
2529
2530 static _LIBCPP_CONSTEXPR const bool __reservable = false;
2531};
2532
2533_LIBCPP_END_NAMESPACE_STD
2534
2535# if _LIBCPP_STD_VER >= 17
2536_LIBCPP_BEGIN_NAMESPACE_STD
2537namespace pmr {
2538template <class _ValueT>
2539using deque _LIBCPP_AVAILABILITY_PMR = std::deque<_ValueT, polymorphic_allocator<_ValueT>>;
2540} // namespace pmr
2541_LIBCPP_END_NAMESPACE_STD
2542# endif
2543
2544_LIBCPP_POP_MACROS
2545
2546# if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
2547# include <algorithm>
2548# include <atomic>
2549# include <concepts>
2550# include <cstdlib>
2551# include <functional>
2552# include <iosfwd>
2553# include <iterator>
2554# include <type_traits>
2555# include <typeinfo>
2556# endif
2557#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
2558
2559#endif // _LIBCPP_DEQUE
2560