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