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