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