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