Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/c++/v1/__cxx03/deque is written in an unsupported language. File is not indexed.

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___CXX03_DEQUE
0011 #define _LIBCPP___CXX03_DEQUE
0012 
0013 /*
0014     deque synopsis
0015 
0016 namespace std
0017 {
0018 
0019 template <class T, class Allocator = allocator<T> >
0020 class deque
0021 {
0022 public:
0023     // types:
0024     typedef T value_type;
0025     typedef Allocator allocator_type;
0026 
0027     typedef typename allocator_type::reference       reference;
0028     typedef typename allocator_type::const_reference const_reference;
0029     typedef implementation-defined                   iterator;
0030     typedef implementation-defined                   const_iterator;
0031     typedef typename allocator_type::size_type       size_type;
0032     typedef typename allocator_type::difference_type difference_type;
0033 
0034     typedef typename allocator_type::pointer         pointer;
0035     typedef typename allocator_type::const_pointer   const_pointer;
0036     typedef std::reverse_iterator<iterator>          reverse_iterator;
0037     typedef std::reverse_iterator<const_iterator>    const_reverse_iterator;
0038 
0039     // construct/copy/destroy:
0040     deque() noexcept(is_nothrow_default_constructible<allocator_type>::value);
0041     explicit deque(const allocator_type& a);
0042     explicit deque(size_type n);
0043     explicit deque(size_type n, const allocator_type& a); // C++14
0044     deque(size_type n, const value_type& v);
0045     deque(size_type n, const value_type& v, const allocator_type& a);
0046     template <class InputIterator>
0047         deque(InputIterator f, InputIterator l);
0048     template <class InputIterator>
0049         deque(InputIterator f, InputIterator l, const allocator_type& a);
0050     template<container-compatible-range<T> R>
0051         deque(from_range_t, R&& rg, const Allocator& = Allocator()); // C++23
0052     deque(const deque& c);
0053     deque(deque&& c)
0054         noexcept(is_nothrow_move_constructible<allocator_type>::value);
0055     deque(initializer_list<value_type> il, const Allocator& a = allocator_type());
0056     deque(const deque& c, const allocator_type& a);
0057     deque(deque&& c, const allocator_type& a);
0058     ~deque();
0059 
0060     deque& operator=(const deque& c);
0061     deque& operator=(deque&& c)
0062         noexcept(
0063              allocator_type::propagate_on_container_move_assignment::value &&
0064              is_nothrow_move_assignable<allocator_type>::value);
0065     deque& operator=(initializer_list<value_type> il);
0066 
0067     template <class InputIterator>
0068         void assign(InputIterator f, InputIterator l);
0069     template<container-compatible-range<T> R>
0070       void assign_range(R&& rg); // C++23
0071     void assign(size_type n, const value_type& v);
0072     void assign(initializer_list<value_type> il);
0073 
0074     allocator_type get_allocator() const noexcept;
0075 
0076     // iterators:
0077 
0078     iterator       begin() noexcept;
0079     const_iterator begin() const noexcept;
0080     iterator       end() noexcept;
0081     const_iterator end() const noexcept;
0082 
0083     reverse_iterator       rbegin() noexcept;
0084     const_reverse_iterator rbegin() const noexcept;
0085     reverse_iterator       rend() noexcept;
0086     const_reverse_iterator rend() const noexcept;
0087 
0088     const_iterator         cbegin() const noexcept;
0089     const_iterator         cend() const noexcept;
0090     const_reverse_iterator crbegin() const noexcept;
0091     const_reverse_iterator crend() const noexcept;
0092 
0093     // capacity:
0094     size_type size() const noexcept;
0095     size_type max_size() const noexcept;
0096     void resize(size_type n);
0097     void resize(size_type n, const value_type& v);
0098     void shrink_to_fit();
0099     bool empty() const noexcept;
0100 
0101     // element access:
0102     reference operator[](size_type i);
0103     const_reference operator[](size_type i) const;
0104     reference at(size_type i);
0105     const_reference at(size_type i) const;
0106     reference front();
0107     const_reference front() const;
0108     reference back();
0109     const_reference back() const;
0110 
0111     // modifiers:
0112     void push_front(const value_type& v);
0113     void push_front(value_type&& v);
0114     template<container-compatible-range<T> R>
0115       void prepend_range(R&& rg); // C++23
0116     void push_back(const value_type& v);
0117     void push_back(value_type&& v);
0118     template<container-compatible-range<T> R>
0119       void append_range(R&& rg); // C++23
0120     template <class... Args> reference emplace_front(Args&&... args);  // reference in C++17
0121     template <class... Args> reference emplace_back(Args&&... args);   // reference in C++17
0122     template <class... Args> iterator emplace(const_iterator p, Args&&... args);
0123     iterator insert(const_iterator p, const value_type& v);
0124     iterator insert(const_iterator p, value_type&& v);
0125     iterator insert(const_iterator p, size_type n, const value_type& v);
0126     template <class InputIterator>
0127         iterator insert(const_iterator p, InputIterator f, InputIterator l);
0128     template<container-compatible-range<T> R>
0129       iterator insert_range(const_iterator position, R&& rg); // C++23
0130     iterator insert(const_iterator p, initializer_list<value_type> il);
0131     void pop_front();
0132     void pop_back();
0133     iterator erase(const_iterator p);
0134     iterator erase(const_iterator f, const_iterator l);
0135     void swap(deque& c)
0136         noexcept(allocator_traits<allocator_type>::is_always_equal::value);  // C++17
0137     void clear() noexcept;
0138 };
0139 
0140 template <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
0141    deque(InputIterator, InputIterator, Allocator = Allocator())
0142    -> deque<typename iterator_traits<InputIterator>::value_type, Allocator>; // C++17
0143 
0144 template<ranges::input_range R, class Allocator = allocator<ranges::range_value_t<R>>>
0145   deque(from_range_t, R&&, Allocator = Allocator())
0146     -> deque<ranges::range_value_t<R>, Allocator>; // C++23
0147 
0148 template <class T, class Allocator>
0149     bool operator==(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
0150 template <class T, class Allocator>
0151     bool operator< (const deque<T,Allocator>& x, const deque<T,Allocator>& y); // removed in C++20
0152 template <class T, class Allocator>
0153     bool operator!=(const deque<T,Allocator>& x, const deque<T,Allocator>& y); // removed in C++20
0154 template <class T, class Allocator>
0155     bool operator> (const deque<T,Allocator>& x, const deque<T,Allocator>& y); // removed in C++20
0156 template <class T, class Allocator>
0157     bool operator>=(const deque<T,Allocator>& x, const deque<T,Allocator>& y); // removed in C++20
0158 template <class T, class Allocator>
0159     bool operator<=(const deque<T,Allocator>& x, const deque<T,Allocator>& y); // removed in C++20
0160 template<class T, class Allocator>
0161     synth-three-way-result<T> operator<=>(const deque<T, Allocator>& x,
0162                                           const deque<T, Allocator>& y);       // since C++20
0163 
0164 // specialized algorithms:
0165 template <class T, class Allocator>
0166     void swap(deque<T,Allocator>& x, deque<T,Allocator>& y)
0167          noexcept(noexcept(x.swap(y)));
0168 
0169 template <class T, class Allocator, class U>
0170     typename deque<T, Allocator>::size_type
0171     erase(deque<T, Allocator>& c, const U& value);       // C++20
0172 template <class T, class Allocator, class Predicate>
0173     typename deque<T, Allocator>::size_type
0174     erase_if(deque<T, Allocator>& c, Predicate pred);    // C++20
0175 
0176 }  // std
0177 
0178 */
0179 
0180 #include <__cxx03/__algorithm/copy.h>
0181 #include <__cxx03/__algorithm/copy_backward.h>
0182 #include <__cxx03/__algorithm/copy_n.h>
0183 #include <__cxx03/__algorithm/equal.h>
0184 #include <__cxx03/__algorithm/fill_n.h>
0185 #include <__cxx03/__algorithm/lexicographical_compare.h>
0186 #include <__cxx03/__algorithm/lexicographical_compare_three_way.h>
0187 #include <__cxx03/__algorithm/min.h>
0188 #include <__cxx03/__algorithm/remove.h>
0189 #include <__cxx03/__algorithm/remove_if.h>
0190 #include <__cxx03/__algorithm/unwrap_iter.h>
0191 #include <__cxx03/__assert>
0192 #include <__cxx03/__config>
0193 #include <__cxx03/__debug_utils/sanitizers.h>
0194 #include <__cxx03/__format/enable_insertable.h>
0195 #include <__cxx03/__fwd/deque.h>
0196 #include <__cxx03/__iterator/distance.h>
0197 #include <__cxx03/__iterator/iterator_traits.h>
0198 #include <__cxx03/__iterator/next.h>
0199 #include <__cxx03/__iterator/prev.h>
0200 #include <__cxx03/__iterator/reverse_iterator.h>
0201 #include <__cxx03/__iterator/segmented_iterator.h>
0202 #include <__cxx03/__memory/addressof.h>
0203 #include <__cxx03/__memory/allocator_destructor.h>
0204 #include <__cxx03/__memory/pointer_traits.h>
0205 #include <__cxx03/__memory/temp_value.h>
0206 #include <__cxx03/__memory/unique_ptr.h>
0207 #include <__cxx03/__memory_resource/polymorphic_allocator.h>
0208 #include <__cxx03/__ranges/access.h>
0209 #include <__cxx03/__ranges/concepts.h>
0210 #include <__cxx03/__ranges/container_compatible_range.h>
0211 #include <__cxx03/__ranges/from_range.h>
0212 #include <__cxx03/__ranges/size.h>
0213 #include <__cxx03/__split_buffer>
0214 #include <__cxx03/__type_traits/is_allocator.h>
0215 #include <__cxx03/__type_traits/is_convertible.h>
0216 #include <__cxx03/__type_traits/is_same.h>
0217 #include <__cxx03/__type_traits/is_swappable.h>
0218 #include <__cxx03/__type_traits/type_identity.h>
0219 #include <__cxx03/__utility/forward.h>
0220 #include <__cxx03/__utility/move.h>
0221 #include <__cxx03/__utility/pair.h>
0222 #include <__cxx03/__utility/swap.h>
0223 #include <__cxx03/limits>
0224 #include <__cxx03/stdexcept>
0225 #include <__cxx03/version>
0226 
0227 // standard-mandated includes
0228 
0229 // [iterator.range]
0230 #include <__cxx03/__iterator/access.h>
0231 #include <__cxx03/__iterator/data.h>
0232 #include <__cxx03/__iterator/empty.h>
0233 #include <__cxx03/__iterator/reverse_access.h>
0234 #include <__cxx03/__iterator/size.h>
0235 
0236 // [deque.syn]
0237 #include <__cxx03/compare>
0238 #include <__cxx03/initializer_list>
0239 
0240 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0241 #  pragma GCC system_header
0242 #endif
0243 
0244 _LIBCPP_PUSH_MACROS
0245 #include <__cxx03/__undef_macros>
0246 
0247 _LIBCPP_BEGIN_NAMESPACE_STD
0248 
0249 template <class _ValueType, class _DiffType>
0250 struct __deque_block_size {
0251   static const _DiffType value = sizeof(_ValueType) < 256 ? 4096 / sizeof(_ValueType) : 16;
0252 };
0253 
0254 template <class _ValueType,
0255           class _Pointer,
0256           class _Reference,
0257           class _MapPointer,
0258           class _DiffType,
0259           _DiffType _BS =
0260 #ifdef _LIBCPP_ABI_INCOMPLETE_TYPES_IN_DEQUE
0261               // Keep template parameter to avoid changing all template declarations thoughout
0262               // this file.
0263           0
0264 #else
0265               __deque_block_size<_ValueType, _DiffType>::value
0266 #endif
0267           >
0268 class _LIBCPP_TEMPLATE_VIS __deque_iterator {
0269   typedef _MapPointer __map_iterator;
0270 
0271 public:
0272   typedef _Pointer pointer;
0273   typedef _DiffType difference_type;
0274 
0275 private:
0276   __map_iterator __m_iter_;
0277   pointer __ptr_;
0278 
0279   static const difference_type __block_size;
0280 
0281 public:
0282   typedef _ValueType value_type;
0283   typedef random_access_iterator_tag iterator_category;
0284   typedef _Reference reference;
0285 
0286   _LIBCPP_HIDE_FROM_ABI __deque_iterator() _NOEXCEPT
0287 #if _LIBCPP_STD_VER >= 14
0288       : __m_iter_(nullptr),
0289         __ptr_(nullptr)
0290 #endif
0291   {
0292   }
0293 
0294   template <class _Pp, class _Rp, class _MP, __enable_if_t<is_convertible<_Pp, pointer>::value, int> = 0>
0295   _LIBCPP_HIDE_FROM_ABI
0296   __deque_iterator(const __deque_iterator<value_type, _Pp, _Rp, _MP, difference_type, _BS>& __it) _NOEXCEPT
0297       : __m_iter_(__it.__m_iter_),
0298         __ptr_(__it.__ptr_) {}
0299 
0300   _LIBCPP_HIDE_FROM_ABI reference operator*() const { return *__ptr_; }
0301   _LIBCPP_HIDE_FROM_ABI pointer operator->() const { return __ptr_; }
0302 
0303   _LIBCPP_HIDE_FROM_ABI __deque_iterator& operator++() {
0304     if (++__ptr_ - *__m_iter_ == __block_size) {
0305       ++__m_iter_;
0306       __ptr_ = *__m_iter_;
0307     }
0308     return *this;
0309   }
0310 
0311   _LIBCPP_HIDE_FROM_ABI __deque_iterator operator++(int) {
0312     __deque_iterator __tmp = *this;
0313     ++(*this);
0314     return __tmp;
0315   }
0316 
0317   _LIBCPP_HIDE_FROM_ABI __deque_iterator& operator--() {
0318     if (__ptr_ == *__m_iter_) {
0319       --__m_iter_;
0320       __ptr_ = *__m_iter_ + __block_size;
0321     }
0322     --__ptr_;
0323     return *this;
0324   }
0325 
0326   _LIBCPP_HIDE_FROM_ABI __deque_iterator operator--(int) {
0327     __deque_iterator __tmp = *this;
0328     --(*this);
0329     return __tmp;
0330   }
0331 
0332   _LIBCPP_HIDE_FROM_ABI __deque_iterator& operator+=(difference_type __n) {
0333     if (__n != 0) {
0334       __n += __ptr_ - *__m_iter_;
0335       if (__n > 0) {
0336         __m_iter_ += __n / __block_size;
0337         __ptr_ = *__m_iter_ + __n % __block_size;
0338       } else // (__n < 0)
0339       {
0340         difference_type __z = __block_size - 1 - __n;
0341         __m_iter_ -= __z / __block_size;
0342         __ptr_ = *__m_iter_ + (__block_size - 1 - __z % __block_size);
0343       }
0344     }
0345     return *this;
0346   }
0347 
0348   _LIBCPP_HIDE_FROM_ABI __deque_iterator& operator-=(difference_type __n) { return *this += -__n; }
0349 
0350   _LIBCPP_HIDE_FROM_ABI __deque_iterator operator+(difference_type __n) const {
0351     __deque_iterator __t(*this);
0352     __t += __n;
0353     return __t;
0354   }
0355 
0356   _LIBCPP_HIDE_FROM_ABI __deque_iterator operator-(difference_type __n) const {
0357     __deque_iterator __t(*this);
0358     __t -= __n;
0359     return __t;
0360   }
0361 
0362   _LIBCPP_HIDE_FROM_ABI friend __deque_iterator operator+(difference_type __n, const __deque_iterator& __it) {
0363     return __it + __n;
0364   }
0365 
0366   _LIBCPP_HIDE_FROM_ABI friend difference_type operator-(const __deque_iterator& __x, const __deque_iterator& __y) {
0367     if (__x != __y)
0368       return (__x.__m_iter_ - __y.__m_iter_) * __block_size + (__x.__ptr_ - *__x.__m_iter_) -
0369              (__y.__ptr_ - *__y.__m_iter_);
0370     return 0;
0371   }
0372 
0373   _LIBCPP_HIDE_FROM_ABI reference operator[](difference_type __n) const { return *(*this + __n); }
0374 
0375   _LIBCPP_HIDE_FROM_ABI friend bool operator==(const __deque_iterator& __x, const __deque_iterator& __y) {
0376     return __x.__ptr_ == __y.__ptr_;
0377   }
0378 
0379 #if _LIBCPP_STD_VER <= 17
0380   _LIBCPP_HIDE_FROM_ABI friend bool operator!=(const __deque_iterator& __x, const __deque_iterator& __y) {
0381     return !(__x == __y);
0382   }
0383 #endif
0384 
0385   // TODO(mordante) disable these overloads in the LLVM 20 release.
0386   _LIBCPP_HIDE_FROM_ABI friend bool operator<(const __deque_iterator& __x, const __deque_iterator& __y) {
0387     return __x.__m_iter_ < __y.__m_iter_ || (__x.__m_iter_ == __y.__m_iter_ && __x.__ptr_ < __y.__ptr_);
0388   }
0389 
0390   _LIBCPP_HIDE_FROM_ABI friend bool operator>(const __deque_iterator& __x, const __deque_iterator& __y) {
0391     return __y < __x;
0392   }
0393 
0394   _LIBCPP_HIDE_FROM_ABI friend bool operator<=(const __deque_iterator& __x, const __deque_iterator& __y) {
0395     return !(__y < __x);
0396   }
0397 
0398   _LIBCPP_HIDE_FROM_ABI friend bool operator>=(const __deque_iterator& __x, const __deque_iterator& __y) {
0399     return !(__x < __y);
0400   }
0401 
0402 #if _LIBCPP_STD_VER >= 20
0403   _LIBCPP_HIDE_FROM_ABI friend strong_ordering operator<=>(const __deque_iterator& __x, const __deque_iterator& __y) {
0404     if (__x.__m_iter_ < __y.__m_iter_)
0405       return strong_ordering::less;
0406 
0407     if (__x.__m_iter_ == __y.__m_iter_) {
0408       if constexpr (three_way_comparable<pointer, strong_ordering>) {
0409         return __x.__ptr_ <=> __y.__ptr_;
0410       } else {
0411         if (__x.__ptr_ < __y.__ptr_)
0412           return strong_ordering::less;
0413 
0414         if (__x.__ptr_ == __y.__ptr_)
0415           return strong_ordering::equal;
0416 
0417         return strong_ordering::greater;
0418       }
0419     }
0420 
0421     return strong_ordering::greater;
0422   }
0423 #endif // _LIBCPP_STD_VER >= 20
0424 
0425 private:
0426   _LIBCPP_HIDE_FROM_ABI explicit __deque_iterator(__map_iterator __m, pointer __p) _NOEXCEPT
0427       : __m_iter_(__m),
0428         __ptr_(__p) {}
0429 
0430   template <class _Tp, class _Ap>
0431   friend class _LIBCPP_TEMPLATE_VIS deque;
0432   template <class _Vp, class _Pp, class _Rp, class _MP, class _Dp, _Dp>
0433   friend class _LIBCPP_TEMPLATE_VIS __deque_iterator;
0434 
0435   template <class>
0436   friend struct __segmented_iterator_traits;
0437 };
0438 
0439 template <class _ValueType, class _Pointer, class _Reference, class _MapPointer, class _DiffType, _DiffType _BlockSize>
0440 struct __segmented_iterator_traits<
0441     __deque_iterator<_ValueType, _Pointer, _Reference, _MapPointer, _DiffType, _BlockSize> > {
0442 private:
0443   using _Iterator = __deque_iterator<_ValueType, _Pointer, _Reference, _MapPointer, _DiffType, _BlockSize>;
0444 
0445 public:
0446   using __is_segmented_iterator = true_type;
0447   using __segment_iterator      = _MapPointer;
0448   using __local_iterator        = _Pointer;
0449 
0450   static _LIBCPP_HIDE_FROM_ABI __segment_iterator __segment(_Iterator __iter) { return __iter.__m_iter_; }
0451   static _LIBCPP_HIDE_FROM_ABI __local_iterator __local(_Iterator __iter) { return __iter.__ptr_; }
0452   static _LIBCPP_HIDE_FROM_ABI __local_iterator __begin(__segment_iterator __iter) { return *__iter; }
0453 
0454   static _LIBCPP_HIDE_FROM_ABI __local_iterator __end(__segment_iterator __iter) {
0455     return *__iter + _Iterator::__block_size;
0456   }
0457 
0458   static _LIBCPP_HIDE_FROM_ABI _Iterator __compose(__segment_iterator __segment, __local_iterator __local) {
0459     if (__segment && __local == __end(__segment)) {
0460       ++__segment;
0461       return _Iterator(__segment, *__segment);
0462     }
0463     return _Iterator(__segment, __local);
0464   }
0465 };
0466 
0467 template <class _ValueType, class _Pointer, class _Reference, class _MapPointer, class _DiffType, _DiffType _BlockSize>
0468 const _DiffType __deque_iterator<_ValueType, _Pointer, _Reference, _MapPointer, _DiffType, _BlockSize>::__block_size =
0469     __deque_block_size<_ValueType, _DiffType>::value;
0470 
0471 template <class _Tp, class _Allocator /*= allocator<_Tp>*/>
0472 class _LIBCPP_TEMPLATE_VIS deque {
0473 public:
0474   // types:
0475 
0476   using value_type = _Tp;
0477 
0478   using allocator_type = _Allocator;
0479   using __alloc_traits = allocator_traits<allocator_type>;
0480   static_assert(__check_valid_allocator<allocator_type>::value, "");
0481   static_assert(is_same<typename allocator_type::value_type, value_type>::value,
0482                 "Allocator::value_type must be same type as value_type");
0483 
0484   using size_type       = typename __alloc_traits::size_type;
0485   using difference_type = typename __alloc_traits::difference_type;
0486 
0487   using pointer       = typename __alloc_traits::pointer;
0488   using const_pointer = typename __alloc_traits::const_pointer;
0489 
0490   using __pointer_allocator       = __rebind_alloc<__alloc_traits, pointer>;
0491   using __const_pointer_allocator = __rebind_alloc<__alloc_traits, const_pointer>;
0492   using __map                     = __split_buffer<pointer, __pointer_allocator>;
0493   using __map_alloc_traits        = allocator_traits<__pointer_allocator>;
0494   using __map_pointer             = typename __map_alloc_traits::pointer;
0495   using __map_const_pointer       = typename allocator_traits<__const_pointer_allocator>::const_pointer;
0496   using __map_const_iterator      = typename __map::const_iterator;
0497 
0498   using reference       = value_type&;
0499   using const_reference = const value_type&;
0500 
0501   using iterator = __deque_iterator<value_type, pointer, reference, __map_pointer, difference_type>;
0502   using const_iterator =
0503       __deque_iterator<value_type, const_pointer, const_reference, __map_const_pointer, difference_type>;
0504   using reverse_iterator       = std::reverse_iterator<iterator>;
0505   using const_reverse_iterator = std::reverse_iterator<const_iterator>;
0506 
0507   // A deque contains the following members which may be trivially relocatable:
0508   // - __map: is a `__split_buffer`, see `__split_buffer` for more information on when it is trivially relocatable
0509   // - size_type: is always trivially relocatable, since it is required to be an integral type
0510   // - allocator_type: may not be trivially relocatable, so it's checked
0511   // None of these are referencing the `deque` itself, so if all of them are trivially relocatable, `deque` is too.
0512   using __trivially_relocatable = __conditional_t<
0513       __libcpp_is_trivially_relocatable<__map>::value && __libcpp_is_trivially_relocatable<allocator_type>::value,
0514       deque,
0515       void>;
0516 
0517   static_assert(is_nothrow_default_constructible<allocator_type>::value ==
0518                     is_nothrow_default_constructible<__pointer_allocator>::value,
0519                 "rebinding an allocator should not change exception guarantees");
0520   static_assert(is_nothrow_move_constructible<allocator_type>::value ==
0521                     is_nothrow_move_constructible<typename __map::allocator_type>::value,
0522                 "rebinding an allocator should not change exception guarantees");
0523 
0524 private:
0525   struct __deque_block_range {
0526     explicit _LIBCPP_HIDE_FROM_ABI __deque_block_range(pointer __b, pointer __e) _NOEXCEPT
0527         : __begin_(__b),
0528           __end_(__e) {}
0529     const pointer __begin_;
0530     const pointer __end_;
0531   };
0532 
0533   struct __deque_range {
0534     iterator __pos_;
0535     const iterator __end_;
0536 
0537     _LIBCPP_HIDE_FROM_ABI __deque_range(iterator __pos, iterator __e) _NOEXCEPT : __pos_(__pos), __end_(__e) {}
0538 
0539     explicit _LIBCPP_HIDE_FROM_ABI operator bool() const _NOEXCEPT { return __pos_ != __end_; }
0540 
0541     _LIBCPP_HIDE_FROM_ABI __deque_range begin() const { return *this; }
0542 
0543     _LIBCPP_HIDE_FROM_ABI __deque_range end() const { return __deque_range(__end_, __end_); }
0544     _LIBCPP_HIDE_FROM_ABI __deque_block_range operator*() const _NOEXCEPT {
0545       if (__pos_.__m_iter_ == __end_.__m_iter_) {
0546         return __deque_block_range(__pos_.__ptr_, __end_.__ptr_);
0547       }
0548       return __deque_block_range(__pos_.__ptr_, *__pos_.__m_iter_ + __block_size);
0549     }
0550 
0551     _LIBCPP_HIDE_FROM_ABI __deque_range& operator++() _NOEXCEPT {
0552       if (__pos_.__m_iter_ == __end_.__m_iter_) {
0553         __pos_ = __end_;
0554       } else {
0555         ++__pos_.__m_iter_;
0556         __pos_.__ptr_ = *__pos_.__m_iter_;
0557       }
0558       return *this;
0559     }
0560 
0561     _LIBCPP_HIDE_FROM_ABI friend bool operator==(__deque_range const& __lhs, __deque_range const& __rhs) {
0562       return __lhs.__pos_ == __rhs.__pos_;
0563     }
0564     _LIBCPP_HIDE_FROM_ABI friend bool operator!=(__deque_range const& __lhs, __deque_range const& __rhs) {
0565       return !(__lhs == __rhs);
0566     }
0567   };
0568 
0569   struct _ConstructTransaction {
0570     _LIBCPP_HIDE_FROM_ABI _ConstructTransaction(deque* __db, __deque_block_range& __r)
0571         : __pos_(__r.__begin_), __end_(__r.__end_), __begin_(__r.__begin_), __base_(__db) {}
0572 
0573     _LIBCPP_HIDE_FROM_ABI ~_ConstructTransaction() { __base_->__size() += (__pos_ - __begin_); }
0574 
0575     pointer __pos_;
0576     const pointer __end_;
0577 
0578   private:
0579     const pointer __begin_;
0580     deque* const __base_;
0581   };
0582 
0583   static const difference_type __block_size;
0584 
0585   __map __map_;
0586   size_type __start_;
0587   __compressed_pair<size_type, allocator_type> __size_;
0588 
0589 public:
0590   // construct/copy/destroy:
0591   _LIBCPP_HIDE_FROM_ABI deque() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
0592       : __start_(0), __size_(0, __default_init_tag()) {
0593     __annotate_new(0);
0594   }
0595 
0596   _LIBCPP_HIDE_FROM_ABI ~deque() {
0597     clear();
0598     __annotate_delete();
0599     typename __map::iterator __i = __map_.begin();
0600     typename __map::iterator __e = __map_.end();
0601     for (; __i != __e; ++__i)
0602       __alloc_traits::deallocate(__alloc(), *__i, __block_size);
0603   }
0604 
0605   _LIBCPP_HIDE_FROM_ABI explicit deque(const allocator_type& __a)
0606       : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a) {
0607     __annotate_new(0);
0608   }
0609 
0610   explicit _LIBCPP_HIDE_FROM_ABI deque(size_type __n);
0611 #if _LIBCPP_STD_VER >= 14
0612   explicit _LIBCPP_HIDE_FROM_ABI deque(size_type __n, const _Allocator& __a);
0613 #endif
0614   _LIBCPP_HIDE_FROM_ABI deque(size_type __n, const value_type& __v);
0615 
0616   template <__enable_if_t<__is_allocator<_Allocator>::value, int> = 0>
0617   _LIBCPP_HIDE_FROM_ABI deque(size_type __n, const value_type& __v, const allocator_type& __a)
0618       : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a) {
0619     __annotate_new(0);
0620     if (__n > 0)
0621       __append(__n, __v);
0622   }
0623 
0624   template <class _InputIter, __enable_if_t<__has_input_iterator_category<_InputIter>::value, int> = 0>
0625   _LIBCPP_HIDE_FROM_ABI deque(_InputIter __f, _InputIter __l);
0626   template <class _InputIter, __enable_if_t<__has_input_iterator_category<_InputIter>::value, int> = 0>
0627   _LIBCPP_HIDE_FROM_ABI deque(_InputIter __f, _InputIter __l, const allocator_type& __a);
0628 
0629 #if _LIBCPP_STD_VER >= 23
0630   template <_ContainerCompatibleRange<_Tp> _Range>
0631   _LIBCPP_HIDE_FROM_ABI deque(from_range_t, _Range&& __range, const allocator_type& __a = allocator_type())
0632       : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a) {
0633     if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
0634       __append_with_size(ranges::begin(__range), ranges::distance(__range));
0635 
0636     } else {
0637       for (auto&& __e : __range) {
0638         emplace_back(std::forward<decltype(__e)>(__e));
0639       }
0640     }
0641   }
0642 #endif
0643 
0644   _LIBCPP_HIDE_FROM_ABI deque(const deque& __c);
0645   _LIBCPP_HIDE_FROM_ABI deque(const deque& __c, const __type_identity_t<allocator_type>& __a);
0646 
0647   _LIBCPP_HIDE_FROM_ABI deque& operator=(const deque& __c);
0648 
0649 #ifndef _LIBCPP_CXX03_LANG
0650   _LIBCPP_HIDE_FROM_ABI deque(initializer_list<value_type> __il);
0651   _LIBCPP_HIDE_FROM_ABI deque(initializer_list<value_type> __il, const allocator_type& __a);
0652 
0653   _LIBCPP_HIDE_FROM_ABI deque& operator=(initializer_list<value_type> __il) {
0654     assign(__il);
0655     return *this;
0656   }
0657 
0658   _LIBCPP_HIDE_FROM_ABI deque(deque&& __c) noexcept(is_nothrow_move_constructible<allocator_type>::value);
0659   _LIBCPP_HIDE_FROM_ABI deque(deque&& __c, const __type_identity_t<allocator_type>& __a);
0660   _LIBCPP_HIDE_FROM_ABI deque&
0661   operator=(deque&& __c) noexcept(__alloc_traits::propagate_on_container_move_assignment::value &&
0662                                   is_nothrow_move_assignable<allocator_type>::value);
0663 
0664   _LIBCPP_HIDE_FROM_ABI void assign(initializer_list<value_type> __il) { assign(__il.begin(), __il.end()); }
0665 #endif // _LIBCPP_CXX03_LANG
0666 
0667   template <class _InputIter,
0668             __enable_if_t<__has_input_iterator_category<_InputIter>::value &&
0669                               !__has_random_access_iterator_category<_InputIter>::value,
0670                           int> = 0>
0671   _LIBCPP_HIDE_FROM_ABI void assign(_InputIter __f, _InputIter __l);
0672   template <class _RAIter, __enable_if_t<__has_random_access_iterator_category<_RAIter>::value, int> = 0>
0673   _LIBCPP_HIDE_FROM_ABI void assign(_RAIter __f, _RAIter __l);
0674 
0675 #if _LIBCPP_STD_VER >= 23
0676   template <_ContainerCompatibleRange<_Tp> _Range>
0677   _LIBCPP_HIDE_FROM_ABI void assign_range(_Range&& __range) {
0678     if constexpr (ranges::random_access_range<_Range>) {
0679       auto __n = static_cast<size_type>(ranges::distance(__range));
0680       __assign_with_size_random_access(ranges::begin(__range), __n);
0681 
0682     } else if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
0683       auto __n = static_cast<size_type>(ranges::distance(__range));
0684       __assign_with_size(ranges::begin(__range), __n);
0685 
0686     } else {
0687       __assign_with_sentinel(ranges::begin(__range), ranges::end(__range));
0688     }
0689   }
0690 #endif
0691 
0692   _LIBCPP_HIDE_FROM_ABI void assign(size_type __n, const value_type& __v);
0693 
0694   _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const _NOEXCEPT;
0695   _LIBCPP_HIDE_FROM_ABI allocator_type& __alloc() _NOEXCEPT { return __size_.second(); }
0696   _LIBCPP_HIDE_FROM_ABI const allocator_type& __alloc() const _NOEXCEPT { return __size_.second(); }
0697 
0698   // iterators:
0699 
0700   _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT {
0701     __map_pointer __mp = __map_.begin() + __start_ / __block_size;
0702     return iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
0703   }
0704 
0705   _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT {
0706     __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __start_ / __block_size);
0707     return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
0708   }
0709 
0710   _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT {
0711     size_type __p      = size() + __start_;
0712     __map_pointer __mp = __map_.begin() + __p / __block_size;
0713     return iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
0714   }
0715 
0716   _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT {
0717     size_type __p            = size() + __start_;
0718     __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __p / __block_size);
0719     return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
0720   }
0721 
0722   _LIBCPP_HIDE_FROM_ABI reverse_iterator rbegin() _NOEXCEPT { return reverse_iterator(end()); }
0723   _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rbegin() const _NOEXCEPT { return const_reverse_iterator(end()); }
0724   _LIBCPP_HIDE_FROM_ABI reverse_iterator rend() _NOEXCEPT { return reverse_iterator(begin()); }
0725   _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rend() const _NOEXCEPT { return const_reverse_iterator(begin()); }
0726 
0727   _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT { return begin(); }
0728   _LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT { return end(); }
0729   _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crbegin() const _NOEXCEPT { return const_reverse_iterator(end()); }
0730   _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crend() const _NOEXCEPT { return const_reverse_iterator(begin()); }
0731 
0732   // capacity:
0733   _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return __size(); }
0734 
0735   _LIBCPP_HIDE_FROM_ABI size_type& __size() _NOEXCEPT { return __size_.first(); }
0736   _LIBCPP_HIDE_FROM_ABI const size_type& __size() const _NOEXCEPT { return __size_.first(); }
0737 
0738   _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
0739     return std::min<size_type>(__alloc_traits::max_size(__alloc()), numeric_limits<difference_type>::max());
0740   }
0741   _LIBCPP_HIDE_FROM_ABI void resize(size_type __n);
0742   _LIBCPP_HIDE_FROM_ABI void resize(size_type __n, const value_type& __v);
0743   _LIBCPP_HIDE_FROM_ABI void shrink_to_fit() _NOEXCEPT;
0744   _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT { return size() == 0; }
0745 
0746   // element access:
0747   _LIBCPP_HIDE_FROM_ABI reference operator[](size_type __i) _NOEXCEPT;
0748   _LIBCPP_HIDE_FROM_ABI const_reference operator[](size_type __i) const _NOEXCEPT;
0749   _LIBCPP_HIDE_FROM_ABI reference at(size_type __i);
0750   _LIBCPP_HIDE_FROM_ABI const_reference at(size_type __i) const;
0751   _LIBCPP_HIDE_FROM_ABI reference front() _NOEXCEPT;
0752   _LIBCPP_HIDE_FROM_ABI const_reference front() const _NOEXCEPT;
0753   _LIBCPP_HIDE_FROM_ABI reference back() _NOEXCEPT;
0754   _LIBCPP_HIDE_FROM_ABI const_reference back() const _NOEXCEPT;
0755 
0756   // 23.2.2.3 modifiers:
0757   _LIBCPP_HIDE_FROM_ABI void push_front(const value_type& __v);
0758   _LIBCPP_HIDE_FROM_ABI void push_back(const value_type& __v);
0759 #ifndef _LIBCPP_CXX03_LANG
0760 #  if _LIBCPP_STD_VER >= 17
0761   template <class... _Args>
0762   _LIBCPP_HIDE_FROM_ABI reference emplace_front(_Args&&... __args);
0763   template <class... _Args>
0764   _LIBCPP_HIDE_FROM_ABI reference emplace_back(_Args&&... __args);
0765 #  else
0766   template <class... _Args>
0767   _LIBCPP_HIDE_FROM_ABI void emplace_front(_Args&&... __args);
0768   template <class... _Args>
0769   _LIBCPP_HIDE_FROM_ABI void emplace_back(_Args&&... __args);
0770 #  endif
0771   template <class... _Args>
0772   _LIBCPP_HIDE_FROM_ABI iterator emplace(const_iterator __p, _Args&&... __args);
0773 
0774   _LIBCPP_HIDE_FROM_ABI void push_front(value_type&& __v);
0775   _LIBCPP_HIDE_FROM_ABI void push_back(value_type&& __v);
0776 
0777 #  if _LIBCPP_STD_VER >= 23
0778   template <_ContainerCompatibleRange<_Tp> _Range>
0779   _LIBCPP_HIDE_FROM_ABI void prepend_range(_Range&& __range) {
0780     insert_range(begin(), std::forward<_Range>(__range));
0781   }
0782 
0783   template <_ContainerCompatibleRange<_Tp> _Range>
0784   _LIBCPP_HIDE_FROM_ABI void append_range(_Range&& __range) {
0785     insert_range(end(), std::forward<_Range>(__range));
0786   }
0787 #  endif
0788 
0789   _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, value_type&& __v);
0790 
0791   _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, initializer_list<value_type> __il) {
0792     return insert(__p, __il.begin(), __il.end());
0793   }
0794 #endif // _LIBCPP_CXX03_LANG
0795   _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, const value_type& __v);
0796   _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, size_type __n, const value_type& __v);
0797   template <class _InputIter, __enable_if_t<__has_exactly_input_iterator_category<_InputIter>::value, int> = 0>
0798   _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, _InputIter __f, _InputIter __l);
0799   template <class _ForwardIterator,
0800             __enable_if_t<__has_exactly_forward_iterator_category<_ForwardIterator>::value, int> = 0>
0801   _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l);
0802   template <class _BiIter, __enable_if_t<__has_bidirectional_iterator_category<_BiIter>::value, int> = 0>
0803   _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, _BiIter __f, _BiIter __l);
0804 
0805 #if _LIBCPP_STD_VER >= 23
0806   template <_ContainerCompatibleRange<_Tp> _Range>
0807   _LIBCPP_HIDE_FROM_ABI iterator insert_range(const_iterator __position, _Range&& __range) {
0808     if constexpr (ranges::bidirectional_range<_Range>) {
0809       auto __n = static_cast<size_type>(ranges::distance(__range));
0810       return __insert_bidirectional(__position, ranges::begin(__range), ranges::end(__range), __n);
0811 
0812     } else if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
0813       auto __n = static_cast<size_type>(ranges::distance(__range));
0814       return __insert_with_size(__position, ranges::begin(__range), __n);
0815 
0816     } else {
0817       return __insert_with_sentinel(__position, ranges::begin(__range), ranges::end(__range));
0818     }
0819   }
0820 #endif
0821 
0822   _LIBCPP_HIDE_FROM_ABI void pop_front();
0823   _LIBCPP_HIDE_FROM_ABI void pop_back();
0824   _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __p);
0825   _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __f, const_iterator __l);
0826 
0827   _LIBCPP_HIDE_FROM_ABI void swap(deque& __c)
0828 #if _LIBCPP_STD_VER >= 14
0829       _NOEXCEPT;
0830 #else
0831       _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<allocator_type>);
0832 #endif
0833   _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT;
0834 
0835   _LIBCPP_HIDE_FROM_ABI bool __invariants() const {
0836     if (!__map_.__invariants())
0837       return false;
0838     if (__map_.size() >= size_type(-1) / __block_size)
0839       return false;
0840     for (__map_const_iterator __i = __map_.begin(), __e = __map_.end(); __i != __e; ++__i)
0841       if (*__i == nullptr)
0842         return false;
0843     if (__map_.size() != 0) {
0844       if (size() >= __map_.size() * __block_size)
0845         return false;
0846       if (__start_ >= __map_.size() * __block_size - size())
0847         return false;
0848     } else {
0849       if (size() != 0)
0850         return false;
0851       if (__start_ != 0)
0852         return false;
0853     }
0854     return true;
0855   }
0856 
0857   _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(deque& __c)
0858       _NOEXCEPT_(!__alloc_traits::propagate_on_container_move_assignment::value ||
0859                  is_nothrow_move_assignable<allocator_type>::value) {
0860     __move_assign_alloc(__c, integral_constant<bool, __alloc_traits::propagate_on_container_move_assignment::value>());
0861   }
0862 
0863   _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(deque& __c, true_type)
0864       _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) {
0865     __alloc() = std::move(__c.__alloc());
0866   }
0867 
0868   _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(deque&, false_type) _NOEXCEPT {}
0869 
0870   _LIBCPP_HIDE_FROM_ABI void __move_assign(deque& __c)
0871       _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value&&
0872                      is_nothrow_move_assignable<allocator_type>::value) {
0873     __map_   = std::move(__c.__map_);
0874     __start_ = __c.__start_;
0875     __size() = __c.size();
0876     __move_assign_alloc(__c);
0877     __c.__start_ = __c.__size() = 0;
0878   }
0879 
0880   _LIBCPP_HIDE_FROM_ABI static size_type __recommend_blocks(size_type __n) {
0881     return __n / __block_size + (__n % __block_size != 0);
0882   }
0883   _LIBCPP_HIDE_FROM_ABI size_type __capacity() const {
0884     return __map_.size() == 0 ? 0 : __map_.size() * __block_size - 1;
0885   }
0886   _LIBCPP_HIDE_FROM_ABI size_type __block_count() const { return __map_.size(); }
0887 
0888   _LIBCPP_HIDE_FROM_ABI size_type __front_spare() const { return __start_; }
0889   _LIBCPP_HIDE_FROM_ABI size_type __front_spare_blocks() const { return __front_spare() / __block_size; }
0890   _LIBCPP_HIDE_FROM_ABI size_type __back_spare() const { return __capacity() - (__start_ + size()); }
0891   _LIBCPP_HIDE_FROM_ABI size_type __back_spare_blocks() const { return __back_spare() / __block_size; }
0892 
0893 private:
0894   enum __asan_annotation_type { __asan_unposion, __asan_poison };
0895 
0896   enum __asan_annotation_place {
0897     __asan_front_moved,
0898     __asan_back_moved,
0899   };
0900 
0901   _LIBCPP_HIDE_FROM_ABI void __annotate_from_to(
0902       size_type __beg,
0903       size_type __end,
0904       __asan_annotation_type __annotation_type,
0905       __asan_annotation_place __place) const _NOEXCEPT {
0906     (void)__beg;
0907     (void)__end;
0908     (void)__annotation_type;
0909     (void)__place;
0910 #ifndef _LIBCPP_HAS_NO_ASAN
0911     // __beg - index of the first item to annotate
0912     // __end - index behind the last item to annotate (so last item + 1)
0913     // __annotation_type - __asan_unposion or __asan_poison
0914     // __place - __asan_front_moved or __asan_back_moved
0915     // Note: All indexes in __map_
0916     if (__beg == __end)
0917       return;
0918     // __annotations_beg_map - first chunk which annotations we want to modify
0919     // __annotations_end_map - last chunk which annotations we want to modify
0920     // NOTE: if __end % __block_size == 0, __annotations_end_map points at the next block, which may not exist
0921     __map_const_iterator __annotations_beg_map = __map_.begin() + __beg / __block_size;
0922     __map_const_iterator __annotations_end_map = __map_.begin() + __end / __block_size;
0923 
0924     bool const __poisoning = __annotation_type == __asan_poison;
0925     // __old_c_beg_index - index of the first element in old container
0926     // __old_c_end_index - index of the end of old container (last + 1)
0927     // Note: may be outside the area we are annotating
0928     size_t __old_c_beg_index = (__poisoning && __place == __asan_front_moved) ? __beg : __start_;
0929     size_t __old_c_end_index = (__poisoning && __place == __asan_back_moved) ? __end : __start_ + size();
0930     bool const __front       = __place == __asan_front_moved;
0931 
0932     if (__poisoning && empty()) {
0933       // Special case: we shouldn't trust __start_
0934       __old_c_beg_index = __beg;
0935       __old_c_end_index = __end;
0936     }
0937     // __old_c_beg_map - memory block (chunk) with first element
0938     // __old_c_end_map - memory block (chunk) with end of old container
0939     // Note: if __old_c_end_index % __block_size == 0, __old_c_end_map points at the next block,
0940     // which may not exist
0941     __map_const_iterator __old_c_beg_map = __map_.begin() + __old_c_beg_index / __block_size;
0942     __map_const_iterator __old_c_end_map = __map_.begin() + __old_c_end_index / __block_size;
0943 
0944     // One edge (front/end) of the container was moved and one was not modified.
0945     // __new_edge_index - index of new edge
0946     // __new_edge_map    - memory block (chunk) with new edge, it always equals to
0947     //                    __annotations_beg_map or __annotations_end_map
0948     // __old_edge_map    - memory block (chunk) with old edge, it always equals to
0949     //                    __old_c_beg_map or __old_c_end_map
0950     size_t __new_edge_index             = (__poisoning ^ __front) ? __beg : __end;
0951     __map_const_iterator __new_edge_map = __map_.begin() + __new_edge_index / __block_size;
0952     __map_const_iterator __old_edge_map = __front ? __old_c_end_map : __old_c_beg_map;
0953 
0954     // We iterate over map pointers (chunks) and fully poison all memory blocks between the first and the last.
0955     // First and last chunk may be partially poisoned.
0956     // __annotate_end_map may point at not existing chunk, therefore we have to have a check for it.
0957     for (__map_const_iterator __map_it = __annotations_beg_map; __map_it <= __annotations_end_map; ++__map_it) {
0958       if (__map_it == __annotations_end_map && __end % __block_size == 0)
0959         // Chunk may not exist, but nothing to do here anyway
0960         break;
0961 
0962       // The beginning and the end of the current memory block
0963       const void* __mem_beg = std::__to_address(*__map_it);
0964       const void* __mem_end = std::__to_address(*__map_it + __block_size);
0965 
0966       // The beginning of memory-in-use in the memory block before container modification
0967       const void* __old_beg =
0968           (__map_it == __old_c_beg_map) ? std::__to_address(*__map_it + (__old_c_beg_index % __block_size)) : __mem_beg;
0969 
0970       // The end of memory-in-use in the memory block before container modification
0971       const void* __old_end;
0972       if (__map_it < __old_c_beg_map || __map_it > __old_c_end_map || (!__poisoning && empty()))
0973         __old_end = __old_beg;
0974       else
0975         __old_end = (__map_it == __old_c_end_map)
0976                       ? std::__to_address(*__map_it + (__old_c_end_index % __block_size))
0977                       : __mem_end;
0978 
0979       // New edge of the container in current memory block
0980       // If the edge is in a different chunk it points on corresponding end of the memory block
0981       const void* __new_edge;
0982       if (__map_it == __new_edge_map)
0983         __new_edge = std::__to_address(*__map_it + (__new_edge_index % __block_size));
0984       else
0985         __new_edge = (__poisoning ^ __front) ? __mem_beg : __mem_end;
0986 
0987       // Not modified edge of the container
0988       // If the edge is in a different chunk it points on corresponding end of the memory block
0989       const void* __old_edge;
0990       if (__map_it == __old_edge_map)
0991         __old_edge = __front ? __old_end : __old_beg;
0992       else
0993         __old_edge = __front ? __mem_end : __mem_beg;
0994 
0995       // __new_beg - the beginning of memory-in-use in the memory block after container modification
0996       // __new_end - the end of memory-in-use in the memory block after container modification
0997       const void* __new_beg = __front ? __new_edge : __old_edge;
0998       const void* __new_end = __front ? __old_edge : __new_edge;
0999 
1000       std::__annotate_double_ended_contiguous_container<_Allocator>(
1001           __mem_beg, __mem_end, __old_beg, __old_end, __new_beg, __new_end);
1002     }
1003 #endif // !_LIBCPP_HAS_NO_ASAN
1004   }
1005 
1006   _LIBCPP_HIDE_FROM_ABI void __annotate_new(size_type __current_size) const _NOEXCEPT {
1007     (void)__current_size;
1008 #ifndef _LIBCPP_HAS_NO_ASAN
1009     if (__current_size == 0)
1010       __annotate_from_to(0, __map_.size() * __block_size, __asan_poison, __asan_back_moved);
1011     else {
1012       __annotate_from_to(0, __start_, __asan_poison, __asan_front_moved);
1013       __annotate_from_to(__start_ + __current_size, __map_.size() * __block_size, __asan_poison, __asan_back_moved);
1014     }
1015 #endif
1016   }
1017 
1018   _LIBCPP_HIDE_FROM_ABI void __annotate_delete() const _NOEXCEPT {
1019 #ifndef _LIBCPP_HAS_NO_ASAN
1020     if (empty()) {
1021       for (size_t __i = 0; __i < __map_.size(); ++__i) {
1022         __annotate_whole_block(__i, __asan_unposion);
1023       }
1024     } else {
1025       __annotate_from_to(0, __start_, __asan_unposion, __asan_front_moved);
1026       __annotate_from_to(__start_ + size(), __map_.size() * __block_size, __asan_unposion, __asan_back_moved);
1027     }
1028 #endif
1029   }
1030 
1031   _LIBCPP_HIDE_FROM_ABI void __annotate_increase_front(size_type __n) const _NOEXCEPT {
1032     (void)__n;
1033 #ifndef _LIBCPP_HAS_NO_ASAN
1034     __annotate_from_to(__start_ - __n, __start_, __asan_unposion, __asan_front_moved);
1035 #endif
1036   }
1037 
1038   _LIBCPP_HIDE_FROM_ABI void __annotate_increase_back(size_type __n) const _NOEXCEPT {
1039     (void)__n;
1040 #ifndef _LIBCPP_HAS_NO_ASAN
1041     __annotate_from_to(__start_ + size(), __start_ + size() + __n, __asan_unposion, __asan_back_moved);
1042 #endif
1043   }
1044 
1045   _LIBCPP_HIDE_FROM_ABI void __annotate_shrink_front(size_type __old_size, size_type __old_start) const _NOEXCEPT {
1046     (void)__old_size;
1047     (void)__old_start;
1048 #ifndef _LIBCPP_HAS_NO_ASAN
1049     __annotate_from_to(__old_start, __old_start + (__old_size - size()), __asan_poison, __asan_front_moved);
1050 #endif
1051   }
1052 
1053   _LIBCPP_HIDE_FROM_ABI void __annotate_shrink_back(size_type __old_size, size_type __old_start) const _NOEXCEPT {
1054     (void)__old_size;
1055     (void)__old_start;
1056 #ifndef _LIBCPP_HAS_NO_ASAN
1057     __annotate_from_to(__old_start + size(), __old_start + __old_size, __asan_poison, __asan_back_moved);
1058 #endif
1059   }
1060 
1061   _LIBCPP_HIDE_FROM_ABI void __annotate_poison_block(const void* __beginning, const void* __end) const _NOEXCEPT {
1062     std::__annotate_double_ended_contiguous_container<_Allocator>(__beginning, __end, __beginning, __end, __end, __end);
1063   }
1064 
1065   _LIBCPP_HIDE_FROM_ABI void
1066   __annotate_whole_block(size_t __block_index, __asan_annotation_type __annotation_type) const _NOEXCEPT {
1067     (void)__block_index;
1068     (void)__annotation_type;
1069 #ifndef _LIBCPP_HAS_NO_ASAN
1070     __map_const_iterator __block_it = __map_.begin() + __block_index;
1071     const void* __block_start       = std::__to_address(*__block_it);
1072     const void* __block_end         = std::__to_address(*__block_it + __block_size);
1073 
1074     if (__annotation_type == __asan_poison)
1075       __annotate_poison_block(__block_start, __block_end);
1076     else {
1077       std::__annotate_double_ended_contiguous_container<_Allocator>(
1078           __block_start, __block_end, __block_start, __block_start, __block_start, __block_end);
1079     }
1080 #endif
1081   }
1082 #if !defined(_LIBCPP_HAS_NO_ASAN)
1083 
1084 public:
1085   _LIBCPP_HIDE_FROM_ABI bool __verify_asan_annotations() const _NOEXCEPT {
1086     // This function tests deque object annotations.
1087     if (empty()) {
1088       for (__map_const_iterator __it = __map_.begin(); __it != __map_.end(); ++__it) {
1089         if (!__sanitizer_verify_double_ended_contiguous_container(
1090                 std::__to_address(*__it),
1091                 std::__to_address(*__it),
1092                 std::__to_address(*__it),
1093                 std::__to_address(*__it + __block_size)))
1094           return false;
1095       }
1096 
1097       return true;
1098     }
1099 
1100     size_type __end                 = __start_ + size();
1101     __map_const_iterator __first_mp = __map_.begin() + __start_ / __block_size;
1102     __map_const_iterator __last_mp  = __map_.begin() + (__end - 1) / __block_size;
1103 
1104     // Pointers to first and after last elements
1105     // Those can be in different deque blocks
1106     const void* __p_beg = std::__to_address(*__first_mp + (__start_ % __block_size));
1107     const void* __p_end =
1108         std::__to_address(*__last_mp + ((__end % __block_size == 0) ? __block_size : __end % __block_size));
1109 
1110     for (__map_const_iterator __it = __map_.begin(); __it != __map_.end(); ++__it) {
1111       // Go over all blocks, find the place we are in and verify its annotations
1112       // Note that __p_end points *behind* the last item.
1113 
1114       // - blocks before the first block with container elements
1115       // - first block with items
1116       // - last block with items
1117       // - blocks after last block with ciontainer elements
1118 
1119       // Is the block before or after deque blocks that contain elements?
1120       if (__it < __first_mp || __it > __last_mp) {
1121         if (!__sanitizer_verify_double_ended_contiguous_container(
1122                 std::__to_address(*__it),
1123                 std::__to_address(*__it),
1124                 std::__to_address(*__it),
1125                 std::__to_address(*__it + __block_size)))
1126           return false;
1127       } else {
1128         const void* __containers_buffer_beg = (__it == __first_mp) ? __p_beg : (const void*)std::__to_address(*__it);
1129         const void* __containers_buffer_end =
1130             (__it == __last_mp) ? __p_end : (const void*)std::__to_address(*__it + __block_size);
1131         if (!__sanitizer_verify_double_ended_contiguous_container(
1132                 std::__to_address(*__it),
1133                 __containers_buffer_beg,
1134                 __containers_buffer_end,
1135                 std::__to_address(*__it + __block_size))) {
1136           return false;
1137         }
1138       }
1139     }
1140     return true;
1141   }
1142 
1143 private:
1144 #endif // _LIBCPP_VERIFY_ASAN_DEQUE_ANNOTATIONS
1145   _LIBCPP_HIDE_FROM_ABI bool __maybe_remove_front_spare(bool __keep_one = true) {
1146     if (__front_spare_blocks() >= 2 || (!__keep_one && __front_spare_blocks())) {
1147       __annotate_whole_block(0, __asan_unposion);
1148       __alloc_traits::deallocate(__alloc(), __map_.front(), __block_size);
1149       __map_.pop_front();
1150       __start_ -= __block_size;
1151       return true;
1152     }
1153     return false;
1154   }
1155 
1156   _LIBCPP_HIDE_FROM_ABI bool __maybe_remove_back_spare(bool __keep_one = true) {
1157     if (__back_spare_blocks() >= 2 || (!__keep_one && __back_spare_blocks())) {
1158       __annotate_whole_block(__map_.size() - 1, __asan_unposion);
1159       __alloc_traits::deallocate(__alloc(), __map_.back(), __block_size);
1160       __map_.pop_back();
1161       return true;
1162     }
1163     return false;
1164   }
1165 
1166   template <class _Iterator, class _Sentinel>
1167   _LIBCPP_HIDE_FROM_ABI void __assign_with_sentinel(_Iterator __f, _Sentinel __l);
1168 
1169   template <class _RandomAccessIterator>
1170   _LIBCPP_HIDE_FROM_ABI void __assign_with_size_random_access(_RandomAccessIterator __f, difference_type __n);
1171   template <class _Iterator>
1172   _LIBCPP_HIDE_FROM_ABI void __assign_with_size(_Iterator __f, difference_type __n);
1173 
1174   template <class _Iterator, class _Sentinel>
1175   _LIBCPP_HIDE_FROM_ABI iterator __insert_with_sentinel(const_iterator __p, _Iterator __f, _Sentinel __l);
1176 
1177   template <class _Iterator>
1178   _LIBCPP_HIDE_FROM_ABI iterator __insert_with_size(const_iterator __p, _Iterator __f, size_type __n);
1179 
1180   template <class _BiIter, class _Sentinel>
1181   _LIBCPP_HIDE_FROM_ABI iterator
1182   __insert_bidirectional(const_iterator __p, _BiIter __f, _Sentinel __sent, size_type __n);
1183   template <class _BiIter>
1184   _LIBCPP_HIDE_FROM_ABI iterator __insert_bidirectional(const_iterator __p, _BiIter __f, _BiIter __l, size_type __n);
1185 
1186   template <class _InpIter, __enable_if_t<__has_exactly_input_iterator_category<_InpIter>::value, int> = 0>
1187   _LIBCPP_HIDE_FROM_ABI void __append(_InpIter __f, _InpIter __l);
1188   template <class _ForIter, __enable_if_t<__has_forward_iterator_category<_ForIter>::value, int> = 0>
1189   _LIBCPP_HIDE_FROM_ABI void __append(_ForIter __f, _ForIter __l);
1190 
1191   template <class _InputIterator>
1192   _LIBCPP_HIDE_FROM_ABI void __append_with_size(_InputIterator __from, size_type __n);
1193   template <class _InputIterator, class _Sentinel>
1194   _LIBCPP_HIDE_FROM_ABI void __append_with_sentinel(_InputIterator __f, _Sentinel __l);
1195 
1196   _LIBCPP_HIDE_FROM_ABI void __append(size_type __n);
1197   _LIBCPP_HIDE_FROM_ABI void __append(size_type __n, const value_type& __v);
1198   _LIBCPP_HIDE_FROM_ABI void __erase_to_end(const_iterator __f);
1199   _LIBCPP_HIDE_FROM_ABI void __add_front_capacity();
1200   _LIBCPP_HIDE_FROM_ABI void __add_front_capacity(size_type __n);
1201   _LIBCPP_HIDE_FROM_ABI void __add_back_capacity();
1202   _LIBCPP_HIDE_FROM_ABI void __add_back_capacity(size_type __n);
1203   _LIBCPP_HIDE_FROM_ABI iterator __move_and_check(iterator __f, iterator __l, iterator __r, const_pointer& __vt);
1204   _LIBCPP_HIDE_FROM_ABI iterator
1205   __move_backward_and_check(iterator __f, iterator __l, iterator __r, const_pointer& __vt);
1206   _LIBCPP_HIDE_FROM_ABI void __move_construct_and_check(iterator __f, iterator __l, iterator __r, const_pointer& __vt);
1207   _LIBCPP_HIDE_FROM_ABI void
1208   __move_construct_backward_and_check(iterator __f, iterator __l, iterator __r, const_pointer& __vt);
1209 
1210   _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const deque& __c) {
1211     __copy_assign_alloc(__c, integral_constant<bool, __alloc_traits::propagate_on_container_copy_assignment::value>());
1212   }
1213 
1214   _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const deque& __c, true_type) {
1215     if (__alloc() != __c.__alloc()) {
1216       clear();
1217       shrink_to_fit();
1218     }
1219     __alloc()        = __c.__alloc();
1220     __map_.__alloc() = __c.__map_.__alloc();
1221   }
1222 
1223   _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const deque&, false_type) {}
1224 
1225   _LIBCPP_HIDE_FROM_ABI void __move_assign(deque& __c, true_type)
1226       _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
1227   _LIBCPP_HIDE_FROM_ABI void __move_assign(deque& __c, false_type);
1228 };
1229 
1230 template <class _Tp, class _Alloc>
1231 _LIBCPP_CONSTEXPR const typename allocator_traits<_Alloc>::difference_type deque<_Tp, _Alloc>::__block_size =
1232     __deque_block_size<value_type, difference_type>::value;
1233 
1234 #if _LIBCPP_STD_VER >= 17
1235 template <class _InputIterator,
1236           class _Alloc = allocator<__iter_value_type<_InputIterator>>,
1237           class        = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
1238           class        = enable_if_t<__is_allocator<_Alloc>::value> >
1239 deque(_InputIterator, _InputIterator) -> deque<__iter_value_type<_InputIterator>, _Alloc>;
1240 
1241 template <class _InputIterator,
1242           class _Alloc,
1243           class = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
1244           class = enable_if_t<__is_allocator<_Alloc>::value> >
1245 deque(_InputIterator, _InputIterator, _Alloc) -> deque<__iter_value_type<_InputIterator>, _Alloc>;
1246 #endif
1247 
1248 #if _LIBCPP_STD_VER >= 23
1249 template <ranges::input_range _Range,
1250           class _Alloc = allocator<ranges::range_value_t<_Range>>,
1251           class        = enable_if_t<__is_allocator<_Alloc>::value> >
1252 deque(from_range_t, _Range&&, _Alloc = _Alloc()) -> deque<ranges::range_value_t<_Range>, _Alloc>;
1253 #endif
1254 
1255 template <class _Tp, class _Allocator>
1256 deque<_Tp, _Allocator>::deque(size_type __n) : __start_(0), __size_(0, __default_init_tag()) {
1257   __annotate_new(0);
1258   if (__n > 0)
1259     __append(__n);
1260 }
1261 
1262 #if _LIBCPP_STD_VER >= 14
1263 template <class _Tp, class _Allocator>
1264 deque<_Tp, _Allocator>::deque(size_type __n, const _Allocator& __a)
1265     : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a) {
1266   __annotate_new(0);
1267   if (__n > 0)
1268     __append(__n);
1269 }
1270 #endif
1271 
1272 template <class _Tp, class _Allocator>
1273 deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v) : __start_(0), __size_(0, __default_init_tag()) {
1274   __annotate_new(0);
1275   if (__n > 0)
1276     __append(__n, __v);
1277 }
1278 
1279 template <class _Tp, class _Allocator>
1280 template <class _InputIter, __enable_if_t<__has_input_iterator_category<_InputIter>::value, int> >
1281 deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l) : __start_(0), __size_(0, __default_init_tag()) {
1282   __annotate_new(0);
1283   __append(__f, __l);
1284 }
1285 
1286 template <class _Tp, class _Allocator>
1287 template <class _InputIter, __enable_if_t<__has_input_iterator_category<_InputIter>::value, int> >
1288 deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l, const allocator_type& __a)
1289     : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a) {
1290   __annotate_new(0);
1291   __append(__f, __l);
1292 }
1293 
1294 template <class _Tp, class _Allocator>
1295 deque<_Tp, _Allocator>::deque(const deque& __c)
1296     : __map_(__pointer_allocator(__alloc_traits::select_on_container_copy_construction(__c.__alloc()))),
1297       __start_(0),
1298       __size_(0, __map_.__alloc()) {
1299   __annotate_new(0);
1300   __append(__c.begin(), __c.end());
1301 }
1302 
1303 template <class _Tp, class _Allocator>
1304 deque<_Tp, _Allocator>::deque(const deque& __c, const __type_identity_t<allocator_type>& __a)
1305     : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a) {
1306   __annotate_new(0);
1307   __append(__c.begin(), __c.end());
1308 }
1309 
1310 template <class _Tp, class _Allocator>
1311 deque<_Tp, _Allocator>& deque<_Tp, _Allocator>::operator=(const deque& __c) {
1312   if (this != std::addressof(__c)) {
1313     __copy_assign_alloc(__c);
1314     assign(__c.begin(), __c.end());
1315   }
1316   return *this;
1317 }
1318 
1319 #ifndef _LIBCPP_CXX03_LANG
1320 
1321 template <class _Tp, class _Allocator>
1322 deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il) : __start_(0), __size_(0, __default_init_tag()) {
1323   __annotate_new(0);
1324   __append(__il.begin(), __il.end());
1325 }
1326 
1327 template <class _Tp, class _Allocator>
1328 deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il, const allocator_type& __a)
1329     : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a) {
1330   __annotate_new(0);
1331   __append(__il.begin(), __il.end());
1332 }
1333 
1334 template <class _Tp, class _Allocator>
1335 inline deque<_Tp, _Allocator>::deque(deque&& __c) noexcept(is_nothrow_move_constructible<allocator_type>::value)
1336     : __map_(std::move(__c.__map_)), __start_(std::move(__c.__start_)), __size_(std::move(__c.__size_)) {
1337   __c.__start_ = 0;
1338   __c.__size() = 0;
1339 }
1340 
1341 template <class _Tp, class _Allocator>
1342 inline deque<_Tp, _Allocator>::deque(deque&& __c, const __type_identity_t<allocator_type>& __a)
1343     : __map_(std::move(__c.__map_), __pointer_allocator(__a)),
1344       __start_(std::move(__c.__start_)),
1345       __size_(std::move(__c.__size()), __a) {
1346   if (__a == __c.__alloc()) {
1347     __c.__start_ = 0;
1348     __c.__size() = 0;
1349   } else {
1350     __map_.clear();
1351     __start_ = 0;
1352     __size() = 0;
1353     typedef move_iterator<iterator> _Ip;
1354     assign(_Ip(__c.begin()), _Ip(__c.end()));
1355   }
1356 }
1357 
1358 template <class _Tp, class _Allocator>
1359 inline deque<_Tp, _Allocator>& deque<_Tp, _Allocator>::operator=(deque&& __c) noexcept(
1360     __alloc_traits::propagate_on_container_move_assignment::value &&
1361     is_nothrow_move_assignable<allocator_type>::value) {
1362   __move_assign(__c, integral_constant<bool, __alloc_traits::propagate_on_container_move_assignment::value>());
1363   return *this;
1364 }
1365 
1366 template <class _Tp, class _Allocator>
1367 void deque<_Tp, _Allocator>::__move_assign(deque& __c, false_type) {
1368   if (__alloc() != __c.__alloc()) {
1369     typedef move_iterator<iterator> _Ip;
1370     assign(_Ip(__c.begin()), _Ip(__c.end()));
1371   } else
1372     __move_assign(__c, true_type());
1373 }
1374 
1375 template <class _Tp, class _Allocator>
1376 void deque<_Tp, _Allocator>::__move_assign(deque& __c,
1377                                            true_type) noexcept(is_nothrow_move_assignable<allocator_type>::value) {
1378   clear();
1379   shrink_to_fit();
1380   __move_assign(__c);
1381 }
1382 
1383 #endif // _LIBCPP_CXX03_LANG
1384 
1385 template <class _Tp, class _Allocator>
1386 template <class _InputIter,
1387           __enable_if_t<__has_input_iterator_category<_InputIter>::value &&
1388                             !__has_random_access_iterator_category<_InputIter>::value,
1389                         int> >
1390 void deque<_Tp, _Allocator>::assign(_InputIter __f, _InputIter __l) {
1391   __assign_with_sentinel(__f, __l);
1392 }
1393 
1394 template <class _Tp, class _Allocator>
1395 template <class _Iterator, class _Sentinel>
1396 _LIBCPP_HIDE_FROM_ABI void deque<_Tp, _Allocator>::__assign_with_sentinel(_Iterator __f, _Sentinel __l) {
1397   iterator __i = begin();
1398   iterator __e = end();
1399   for (; __f != __l && __i != __e; ++__f, (void)++__i)
1400     *__i = *__f;
1401   if (__f != __l)
1402     __append_with_sentinel(std::move(__f), std::move(__l));
1403   else
1404     __erase_to_end(__i);
1405 }
1406 
1407 template <class _Tp, class _Allocator>
1408 template <class _RAIter, __enable_if_t<__has_random_access_iterator_category<_RAIter>::value, int> >
1409 void deque<_Tp, _Allocator>::assign(_RAIter __f, _RAIter __l) {
1410   __assign_with_size_random_access(__f, __l - __f);
1411 }
1412 
1413 template <class _Tp, class _Allocator>
1414 template <class _RandomAccessIterator>
1415 _LIBCPP_HIDE_FROM_ABI void
1416 deque<_Tp, _Allocator>::__assign_with_size_random_access(_RandomAccessIterator __f, difference_type __n) {
1417   if (static_cast<size_type>(__n) > size()) {
1418     auto __l = __f + size();
1419     std::copy(__f, __l, begin());
1420     __append_with_size(__l, __n - size());
1421   } else
1422     __erase_to_end(std::copy_n(__f, __n, begin()));
1423 }
1424 
1425 template <class _Tp, class _Allocator>
1426 template <class _Iterator>
1427 _LIBCPP_HIDE_FROM_ABI void deque<_Tp, _Allocator>::__assign_with_size(_Iterator __f, difference_type __n) {
1428   if (static_cast<size_type>(__n) > size()) {
1429     auto __added_size = __n - size();
1430 
1431     auto __i = begin();
1432     for (auto __count = size(); __count != 0; --__count) {
1433       *__i++ = *__f++;
1434     }
1435 
1436     __append_with_size(__f, __added_size);
1437 
1438   } else {
1439     __erase_to_end(std::copy_n(__f, __n, begin()));
1440   }
1441 }
1442 
1443 template <class _Tp, class _Allocator>
1444 void deque<_Tp, _Allocator>::assign(size_type __n, const value_type& __v) {
1445   if (__n > size()) {
1446     std::fill_n(begin(), size(), __v);
1447     __n -= size();
1448     __append(__n, __v);
1449   } else
1450     __erase_to_end(std::fill_n(begin(), __n, __v));
1451 }
1452 
1453 template <class _Tp, class _Allocator>
1454 inline _Allocator deque<_Tp, _Allocator>::get_allocator() const _NOEXCEPT {
1455   return __alloc();
1456 }
1457 
1458 template <class _Tp, class _Allocator>
1459 void deque<_Tp, _Allocator>::resize(size_type __n) {
1460   if (__n > size())
1461     __append(__n - size());
1462   else if (__n < size())
1463     __erase_to_end(begin() + __n);
1464 }
1465 
1466 template <class _Tp, class _Allocator>
1467 void deque<_Tp, _Allocator>::resize(size_type __n, const value_type& __v) {
1468   if (__n > size())
1469     __append(__n - size(), __v);
1470   else if (__n < size())
1471     __erase_to_end(begin() + __n);
1472 }
1473 
1474 template <class _Tp, class _Allocator>
1475 void deque<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT {
1476   allocator_type& __a = __alloc();
1477   if (empty()) {
1478     __annotate_delete();
1479     while (__map_.size() > 0) {
1480       __alloc_traits::deallocate(__a, __map_.back(), __block_size);
1481       __map_.pop_back();
1482     }
1483     __start_ = 0;
1484   } else {
1485     __maybe_remove_front_spare(/*__keep_one=*/false);
1486     __maybe_remove_back_spare(/*__keep_one=*/false);
1487   }
1488   __map_.shrink_to_fit();
1489 }
1490 
1491 template <class _Tp, class _Allocator>
1492 inline typename deque<_Tp, _Allocator>::reference deque<_Tp, _Allocator>::operator[](size_type __i) _NOEXCEPT {
1493   _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__i < size(), "deque::operator[] index out of bounds");
1494   size_type __p = __start_ + __i;
1495   return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
1496 }
1497 
1498 template <class _Tp, class _Allocator>
1499 inline typename deque<_Tp, _Allocator>::const_reference
1500 deque<_Tp, _Allocator>::operator[](size_type __i) const _NOEXCEPT {
1501   _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__i < size(), "deque::operator[] index out of bounds");
1502   size_type __p = __start_ + __i;
1503   return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
1504 }
1505 
1506 template <class _Tp, class _Allocator>
1507 inline typename deque<_Tp, _Allocator>::reference deque<_Tp, _Allocator>::at(size_type __i) {
1508   if (__i >= size())
1509     std::__throw_out_of_range("deque");
1510   size_type __p = __start_ + __i;
1511   return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
1512 }
1513 
1514 template <class _Tp, class _Allocator>
1515 inline typename deque<_Tp, _Allocator>::const_reference deque<_Tp, _Allocator>::at(size_type __i) const {
1516   if (__i >= size())
1517     std::__throw_out_of_range("deque");
1518   size_type __p = __start_ + __i;
1519   return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
1520 }
1521 
1522 template <class _Tp, class _Allocator>
1523 inline typename deque<_Tp, _Allocator>::reference deque<_Tp, _Allocator>::front() _NOEXCEPT {
1524   _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "deque::front called on an empty deque");
1525   return *(*(__map_.begin() + __start_ / __block_size) + __start_ % __block_size);
1526 }
1527 
1528 template <class _Tp, class _Allocator>
1529 inline typename deque<_Tp, _Allocator>::const_reference deque<_Tp, _Allocator>::front() const _NOEXCEPT {
1530   _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "deque::front called on an empty deque");
1531   return *(*(__map_.begin() + __start_ / __block_size) + __start_ % __block_size);
1532 }
1533 
1534 template <class _Tp, class _Allocator>
1535 inline typename deque<_Tp, _Allocator>::reference deque<_Tp, _Allocator>::back() _NOEXCEPT {
1536   _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "deque::back called on an empty deque");
1537   size_type __p = size() + __start_ - 1;
1538   return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
1539 }
1540 
1541 template <class _Tp, class _Allocator>
1542 inline typename deque<_Tp, _Allocator>::const_reference deque<_Tp, _Allocator>::back() const _NOEXCEPT {
1543   _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "deque::back called on an empty deque");
1544   size_type __p = size() + __start_ - 1;
1545   return *(*(__map_.begin() + __p / __block_size) + __p % __block_size);
1546 }
1547 
1548 template <class _Tp, class _Allocator>
1549 void deque<_Tp, _Allocator>::push_back(const value_type& __v) {
1550   allocator_type& __a = __alloc();
1551   if (__back_spare() == 0)
1552     __add_back_capacity();
1553   // __back_spare() >= 1
1554   __annotate_increase_back(1);
1555   __alloc_traits::construct(__a, std::addressof(*end()), __v);
1556   ++__size();
1557 }
1558 
1559 template <class _Tp, class _Allocator>
1560 void deque<_Tp, _Allocator>::push_front(const value_type& __v) {
1561   allocator_type& __a = __alloc();
1562   if (__front_spare() == 0)
1563     __add_front_capacity();
1564   // __front_spare() >= 1
1565   __annotate_increase_front(1);
1566   __alloc_traits::construct(__a, std::addressof(*--begin()), __v);
1567   --__start_;
1568   ++__size();
1569 }
1570 
1571 #ifndef _LIBCPP_CXX03_LANG
1572 template <class _Tp, class _Allocator>
1573 void deque<_Tp, _Allocator>::push_back(value_type&& __v) {
1574   allocator_type& __a = __alloc();
1575   if (__back_spare() == 0)
1576     __add_back_capacity();
1577   // __back_spare() >= 1
1578   __annotate_increase_back(1);
1579   __alloc_traits::construct(__a, std::addressof(*end()), std::move(__v));
1580   ++__size();
1581 }
1582 
1583 template <class _Tp, class _Allocator>
1584 template <class... _Args>
1585 #  if _LIBCPP_STD_VER >= 17
1586 typename deque<_Tp, _Allocator>::reference
1587 #  else
1588 void
1589 #  endif
1590 deque<_Tp, _Allocator>::emplace_back(_Args&&... __args) {
1591   allocator_type& __a = __alloc();
1592   if (__back_spare() == 0)
1593     __add_back_capacity();
1594   // __back_spare() >= 1
1595   __annotate_increase_back(1);
1596   __alloc_traits::construct(__a, std::addressof(*end()), std::forward<_Args>(__args)...);
1597   ++__size();
1598 #  if _LIBCPP_STD_VER >= 17
1599   return *--end();
1600 #  endif
1601 }
1602 
1603 template <class _Tp, class _Allocator>
1604 void deque<_Tp, _Allocator>::push_front(value_type&& __v) {
1605   allocator_type& __a = __alloc();
1606   if (__front_spare() == 0)
1607     __add_front_capacity();
1608   // __front_spare() >= 1
1609   __annotate_increase_front(1);
1610   __alloc_traits::construct(__a, std::addressof(*--begin()), std::move(__v));
1611   --__start_;
1612   ++__size();
1613 }
1614 
1615 template <class _Tp, class _Allocator>
1616 template <class... _Args>
1617 #  if _LIBCPP_STD_VER >= 17
1618 typename deque<_Tp, _Allocator>::reference
1619 #  else
1620 void
1621 #  endif
1622 deque<_Tp, _Allocator>::emplace_front(_Args&&... __args) {
1623   allocator_type& __a = __alloc();
1624   if (__front_spare() == 0)
1625     __add_front_capacity();
1626   // __front_spare() >= 1
1627   __annotate_increase_front(1);
1628   __alloc_traits::construct(__a, std::addressof(*--begin()), std::forward<_Args>(__args)...);
1629   --__start_;
1630   ++__size();
1631 #  if _LIBCPP_STD_VER >= 17
1632   return *begin();
1633 #  endif
1634 }
1635 
1636 template <class _Tp, class _Allocator>
1637 typename deque<_Tp, _Allocator>::iterator deque<_Tp, _Allocator>::insert(const_iterator __p, value_type&& __v) {
1638   size_type __pos     = __p - begin();
1639   size_type __to_end  = size() - __pos;
1640   allocator_type& __a = __alloc();
1641   if (__pos < __to_end) { // insert by shifting things backward
1642     if (__front_spare() == 0)
1643       __add_front_capacity();
1644     // __front_spare() >= 1
1645     __annotate_increase_front(1);
1646     if (__pos == 0) {
1647       __alloc_traits::construct(__a, std::addressof(*--begin()), std::move(__v));
1648       --__start_;
1649       ++__size();
1650     } else {
1651       iterator __b   = begin();
1652       iterator __bm1 = std::prev(__b);
1653       __alloc_traits::construct(__a, std::addressof(*__bm1), std::move(*__b));
1654       --__start_;
1655       ++__size();
1656       if (__pos > 1)
1657         __b = std::move(std::next(__b), __b + __pos, __b);
1658       *__b = std::move(__v);
1659     }
1660   } else { // insert by shifting things forward
1661     if (__back_spare() == 0)
1662       __add_back_capacity();
1663     // __back_capacity >= 1
1664     __annotate_increase_back(1);
1665     size_type __de = size() - __pos;
1666     if (__de == 0) {
1667       __alloc_traits::construct(__a, std::addressof(*end()), std::move(__v));
1668       ++__size();
1669     } else {
1670       iterator __e   = end();
1671       iterator __em1 = std::prev(__e);
1672       __alloc_traits::construct(__a, std::addressof(*__e), std::move(*__em1));
1673       ++__size();
1674       if (__de > 1)
1675         __e = std::move_backward(__e - __de, __em1, __e);
1676       *--__e = std::move(__v);
1677     }
1678   }
1679   return begin() + __pos;
1680 }
1681 
1682 template <class _Tp, class _Allocator>
1683 template <class... _Args>
1684 typename deque<_Tp, _Allocator>::iterator deque<_Tp, _Allocator>::emplace(const_iterator __p, _Args&&... __args) {
1685   size_type __pos     = __p - begin();
1686   size_type __to_end  = size() - __pos;
1687   allocator_type& __a = __alloc();
1688   if (__pos < __to_end) { // insert by shifting things backward
1689     if (__front_spare() == 0)
1690       __add_front_capacity();
1691     // __front_spare() >= 1
1692     __annotate_increase_front(1);
1693     if (__pos == 0) {
1694       __alloc_traits::construct(__a, std::addressof(*--begin()), std::forward<_Args>(__args)...);
1695       --__start_;
1696       ++__size();
1697     } else {
1698       __temp_value<value_type, _Allocator> __tmp(__alloc(), std::forward<_Args>(__args)...);
1699       iterator __b   = begin();
1700       iterator __bm1 = std::prev(__b);
1701       __alloc_traits::construct(__a, std::addressof(*__bm1), std::move(*__b));
1702       --__start_;
1703       ++__size();
1704       if (__pos > 1)
1705         __b = std::move(std::next(__b), __b + __pos, __b);
1706       *__b = std::move(__tmp.get());
1707     }
1708   } else { // insert by shifting things forward
1709     if (__back_spare() == 0)
1710       __add_back_capacity();
1711     // __back_capacity >= 1
1712     __annotate_increase_back(1);
1713     size_type __de = size() - __pos;
1714     if (__de == 0) {
1715       __alloc_traits::construct(__a, std::addressof(*end()), std::forward<_Args>(__args)...);
1716       ++__size();
1717     } else {
1718       __temp_value<value_type, _Allocator> __tmp(__alloc(), std::forward<_Args>(__args)...);
1719       iterator __e   = end();
1720       iterator __em1 = std::prev(__e);
1721       __alloc_traits::construct(__a, std::addressof(*__e), std::move(*__em1));
1722       ++__size();
1723       if (__de > 1)
1724         __e = std::move_backward(__e - __de, __em1, __e);
1725       *--__e = std::move(__tmp.get());
1726     }
1727   }
1728   return begin() + __pos;
1729 }
1730 
1731 #endif // _LIBCPP_CXX03_LANG
1732 
1733 template <class _Tp, class _Allocator>
1734 typename deque<_Tp, _Allocator>::iterator deque<_Tp, _Allocator>::insert(const_iterator __p, const value_type& __v) {
1735   size_type __pos     = __p - begin();
1736   size_type __to_end  = size() - __pos;
1737   allocator_type& __a = __alloc();
1738   if (__pos < __to_end) { // insert by shifting things backward
1739     if (__front_spare() == 0)
1740       __add_front_capacity();
1741     // __front_spare() >= 1
1742     __annotate_increase_front(1);
1743     if (__pos == 0) {
1744       __alloc_traits::construct(__a, std::addressof(*--begin()), __v);
1745       --__start_;
1746       ++__size();
1747     } else {
1748       const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1749       iterator __b       = begin();
1750       iterator __bm1     = std::prev(__b);
1751       if (__vt == pointer_traits<const_pointer>::pointer_to(*__b))
1752         __vt = pointer_traits<const_pointer>::pointer_to(*__bm1);
1753       __alloc_traits::construct(__a, std::addressof(*__bm1), std::move(*__b));
1754       --__start_;
1755       ++__size();
1756       if (__pos > 1)
1757         __b = __move_and_check(std::next(__b), __b + __pos, __b, __vt);
1758       *__b = *__vt;
1759     }
1760   } else { // insert by shifting things forward
1761     if (__back_spare() == 0)
1762       __add_back_capacity();
1763     // __back_capacity >= 1
1764     __annotate_increase_back(1);
1765     size_type __de = size() - __pos;
1766     if (__de == 0) {
1767       __alloc_traits::construct(__a, std::addressof(*end()), __v);
1768       ++__size();
1769     } else {
1770       const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1771       iterator __e       = end();
1772       iterator __em1     = std::prev(__e);
1773       if (__vt == pointer_traits<const_pointer>::pointer_to(*__em1))
1774         __vt = pointer_traits<const_pointer>::pointer_to(*__e);
1775       __alloc_traits::construct(__a, std::addressof(*__e), std::move(*__em1));
1776       ++__size();
1777       if (__de > 1)
1778         __e = __move_backward_and_check(__e - __de, __em1, __e, __vt);
1779       *--__e = *__vt;
1780     }
1781   }
1782   return begin() + __pos;
1783 }
1784 
1785 template <class _Tp, class _Allocator>
1786 typename deque<_Tp, _Allocator>::iterator
1787 deque<_Tp, _Allocator>::insert(const_iterator __p, size_type __n, const value_type& __v) {
1788   size_type __pos     = __p - begin();
1789   size_type __to_end  = __size() - __pos;
1790   allocator_type& __a = __alloc();
1791   if (__pos < __to_end) { // insert by shifting things backward
1792     if (__n > __front_spare())
1793       __add_front_capacity(__n - __front_spare());
1794     // __n <= __front_spare()
1795     __annotate_increase_front(__n);
1796     iterator __old_begin = begin();
1797     iterator __i         = __old_begin;
1798     if (__n > __pos) {
1799       for (size_type __m = __n - __pos; __m; --__m, --__start_, ++__size())
1800         __alloc_traits::construct(__a, std::addressof(*--__i), __v);
1801       __n = __pos;
1802     }
1803     if (__n > 0) {
1804       const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1805       iterator __obn     = __old_begin + __n;
1806       __move_construct_backward_and_check(__old_begin, __obn, __i, __vt);
1807       if (__n < __pos)
1808         __old_begin = __move_and_check(__obn, __old_begin + __pos, __old_begin, __vt);
1809       std::fill_n(__old_begin, __n, *__vt);
1810     }
1811   } else { // insert by shifting things forward
1812     size_type __back_capacity = __back_spare();
1813     if (__n > __back_capacity)
1814       __add_back_capacity(__n - __back_capacity);
1815     // __n <= __back_capacity
1816     __annotate_increase_back(__n);
1817     iterator __old_end = end();
1818     iterator __i       = __old_end;
1819     size_type __de     = size() - __pos;
1820     if (__n > __de) {
1821       for (size_type __m = __n - __de; __m; --__m, (void)++__i, ++__size())
1822         __alloc_traits::construct(__a, std::addressof(*__i), __v);
1823       __n = __de;
1824     }
1825     if (__n > 0) {
1826       const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1827       iterator __oen     = __old_end - __n;
1828       __move_construct_and_check(__oen, __old_end, __i, __vt);
1829       if (__n < __de)
1830         __old_end = __move_backward_and_check(__old_end - __de, __oen, __old_end, __vt);
1831       std::fill_n(__old_end - __n, __n, *__vt);
1832     }
1833   }
1834   return begin() + __pos;
1835 }
1836 
1837 template <class _Tp, class _Allocator>
1838 template <class _InputIter, __enable_if_t<__has_exactly_input_iterator_category<_InputIter>::value, int> >
1839 typename deque<_Tp, _Allocator>::iterator
1840 deque<_Tp, _Allocator>::insert(const_iterator __p, _InputIter __f, _InputIter __l) {
1841   return __insert_with_sentinel(__p, __f, __l);
1842 }
1843 
1844 template <class _Tp, class _Allocator>
1845 template <class _Iterator, class _Sentinel>
1846 _LIBCPP_HIDE_FROM_ABI typename deque<_Tp, _Allocator>::iterator
1847 deque<_Tp, _Allocator>::__insert_with_sentinel(const_iterator __p, _Iterator __f, _Sentinel __l) {
1848   __split_buffer<value_type, allocator_type&> __buf(__alloc());
1849   __buf.__construct_at_end_with_sentinel(std::move(__f), std::move(__l));
1850   typedef typename __split_buffer<value_type, allocator_type&>::iterator __bi;
1851   return insert(__p, move_iterator<__bi>(__buf.begin()), move_iterator<__bi>(__buf.end()));
1852 }
1853 
1854 template <class _Tp, class _Allocator>
1855 template <class _ForwardIterator, __enable_if_t<__has_exactly_forward_iterator_category<_ForwardIterator>::value, int> >
1856 typename deque<_Tp, _Allocator>::iterator
1857 deque<_Tp, _Allocator>::insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l) {
1858   return __insert_with_size(__p, __f, std::distance(__f, __l));
1859 }
1860 
1861 template <class _Tp, class _Allocator>
1862 template <class _Iterator>
1863 _LIBCPP_HIDE_FROM_ABI typename deque<_Tp, _Allocator>::iterator
1864 deque<_Tp, _Allocator>::__insert_with_size(const_iterator __p, _Iterator __f, size_type __n) {
1865   __split_buffer<value_type, allocator_type&> __buf(__n, 0, __alloc());
1866   __buf.__construct_at_end_with_size(__f, __n);
1867   typedef typename __split_buffer<value_type, allocator_type&>::iterator __fwd;
1868   return insert(__p, move_iterator<__fwd>(__buf.begin()), move_iterator<__fwd>(__buf.end()));
1869 }
1870 
1871 template <class _Tp, class _Allocator>
1872 template <class _BiIter, __enable_if_t<__has_bidirectional_iterator_category<_BiIter>::value, int> >
1873 typename deque<_Tp, _Allocator>::iterator deque<_Tp, _Allocator>::insert(const_iterator __p, _BiIter __f, _BiIter __l) {
1874   return __insert_bidirectional(__p, __f, __l, std::distance(__f, __l));
1875 }
1876 
1877 template <class _Tp, class _Allocator>
1878 template <class _BiIter, class _Sentinel>
1879 _LIBCPP_HIDE_FROM_ABI typename deque<_Tp, _Allocator>::iterator
1880 deque<_Tp, _Allocator>::__insert_bidirectional(const_iterator __p, _BiIter __f, _Sentinel, size_type __n) {
1881   return __insert_bidirectional(__p, __f, std::next(__f, __n), __n);
1882 }
1883 
1884 template <class _Tp, class _Allocator>
1885 template <class _BiIter>
1886 _LIBCPP_HIDE_FROM_ABI typename deque<_Tp, _Allocator>::iterator
1887 deque<_Tp, _Allocator>::__insert_bidirectional(const_iterator __p, _BiIter __f, _BiIter __l, size_type __n) {
1888   size_type __pos     = __p - begin();
1889   size_type __to_end  = size() - __pos;
1890   allocator_type& __a = __alloc();
1891   if (__pos < __to_end) { // insert by shifting things backward
1892     if (__n > __front_spare())
1893       __add_front_capacity(__n - __front_spare());
1894     // __n <= __front_spare()
1895     __annotate_increase_front(__n);
1896     iterator __old_begin = begin();
1897     iterator __i         = __old_begin;
1898     _BiIter __m          = __f;
1899     if (__n > __pos) {
1900       __m = __pos < __n / 2 ? std::prev(__l, __pos) : std::next(__f, __n - __pos);
1901       for (_BiIter __j = __m; __j != __f; --__start_, ++__size())
1902         __alloc_traits::construct(__a, std::addressof(*--__i), *--__j);
1903       __n = __pos;
1904     }
1905     if (__n > 0) {
1906       iterator __obn = __old_begin + __n;
1907       for (iterator __j = __obn; __j != __old_begin;) {
1908         __alloc_traits::construct(__a, std::addressof(*--__i), std::move(*--__j));
1909         --__start_;
1910         ++__size();
1911       }
1912       if (__n < __pos)
1913         __old_begin = std::move(__obn, __old_begin + __pos, __old_begin);
1914       std::copy(__m, __l, __old_begin);
1915     }
1916   } else { // insert by shifting things forward
1917     size_type __back_capacity = __back_spare();
1918     if (__n > __back_capacity)
1919       __add_back_capacity(__n - __back_capacity);
1920     // __n <= __back_capacity
1921     __annotate_increase_back(__n);
1922     iterator __old_end = end();
1923     iterator __i       = __old_end;
1924     _BiIter __m        = __l;
1925     size_type __de     = size() - __pos;
1926     if (__n > __de) {
1927       __m = __de < __n / 2 ? std::next(__f, __de) : std::prev(__l, __n - __de);
1928       for (_BiIter __j = __m; __j != __l; ++__i, (void)++__j, ++__size())
1929         __alloc_traits::construct(__a, std::addressof(*__i), *__j);
1930       __n = __de;
1931     }
1932     if (__n > 0) {
1933       iterator __oen = __old_end - __n;
1934       for (iterator __j = __oen; __j != __old_end; ++__i, (void)++__j, ++__size())
1935         __alloc_traits::construct(__a, std::addressof(*__i), std::move(*__j));
1936       if (__n < __de)
1937         __old_end = std::move_backward(__old_end - __de, __oen, __old_end);
1938       std::copy_backward(__f, __m, __old_end);
1939     }
1940   }
1941   return begin() + __pos;
1942 }
1943 
1944 template <class _Tp, class _Allocator>
1945 template <class _InpIter, __enable_if_t<__has_exactly_input_iterator_category<_InpIter>::value, int> >
1946 void deque<_Tp, _Allocator>::__append(_InpIter __f, _InpIter __l) {
1947   __append_with_sentinel(__f, __l);
1948 }
1949 
1950 template <class _Tp, class _Allocator>
1951 template <class _InputIterator, class _Sentinel>
1952 _LIBCPP_HIDE_FROM_ABI void deque<_Tp, _Allocator>::__append_with_sentinel(_InputIterator __f, _Sentinel __l) {
1953   for (; __f != __l; ++__f)
1954 #ifdef _LIBCPP_CXX03_LANG
1955     push_back(*__f);
1956 #else
1957     emplace_back(*__f);
1958 #endif
1959 }
1960 
1961 template <class _Tp, class _Allocator>
1962 template <class _ForIter, __enable_if_t<__has_forward_iterator_category<_ForIter>::value, int> >
1963 void deque<_Tp, _Allocator>::__append(_ForIter __f, _ForIter __l) {
1964   __append_with_size(__f, std::distance(__f, __l));
1965 }
1966 
1967 template <class _Tp, class _Allocator>
1968 template <class _InputIterator>
1969 _LIBCPP_HIDE_FROM_ABI void deque<_Tp, _Allocator>::__append_with_size(_InputIterator __f, size_type __n) {
1970   allocator_type& __a       = __alloc();
1971   size_type __back_capacity = __back_spare();
1972   if (__n > __back_capacity)
1973     __add_back_capacity(__n - __back_capacity);
1974 
1975   // __n <= __back_capacity
1976   __annotate_increase_back(__n);
1977   for (__deque_block_range __br : __deque_range(end(), end() + __n)) {
1978     _ConstructTransaction __tx(this, __br);
1979     for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, (void)++__f) {
1980       __alloc_traits::construct(__a, std::__to_address(__tx.__pos_), *__f);
1981     }
1982   }
1983 }
1984 
1985 template <class _Tp, class _Allocator>
1986 void deque<_Tp, _Allocator>::__append(size_type __n) {
1987   allocator_type& __a       = __alloc();
1988   size_type __back_capacity = __back_spare();
1989   if (__n > __back_capacity)
1990     __add_back_capacity(__n - __back_capacity);
1991   // __n <= __back_capacity
1992   __annotate_increase_back(__n);
1993   for (__deque_block_range __br : __deque_range(end(), end() + __n)) {
1994     _ConstructTransaction __tx(this, __br);
1995     for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
1996       __alloc_traits::construct(__a, std::__to_address(__tx.__pos_));
1997     }
1998   }
1999 }
2000 
2001 template <class _Tp, class _Allocator>
2002 void deque<_Tp, _Allocator>::__append(size_type __n, const value_type& __v) {
2003   allocator_type& __a       = __alloc();
2004   size_type __back_capacity = __back_spare();
2005   if (__n > __back_capacity)
2006     __add_back_capacity(__n - __back_capacity);
2007   // __n <= __back_capacity
2008   __annotate_increase_back(__n);
2009   for (__deque_block_range __br : __deque_range(end(), end() + __n)) {
2010     _ConstructTransaction __tx(this, __br);
2011     for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
2012       __alloc_traits::construct(__a, std::__to_address(__tx.__pos_), __v);
2013     }
2014   }
2015 }
2016 
2017 // Create front capacity for one block of elements.
2018 // Strong guarantee.  Either do it or don't touch anything.
2019 template <class _Tp, class _Allocator>
2020 void deque<_Tp, _Allocator>::__add_front_capacity() {
2021   allocator_type& __a = __alloc();
2022   if (__back_spare() >= __block_size) {
2023     __start_ += __block_size;
2024     pointer __pt = __map_.back();
2025     __map_.pop_back();
2026     __map_.push_front(__pt);
2027   }
2028   // Else if __map_.size() < __map_.capacity() then we need to allocate 1 buffer
2029   else if (__map_.size() < __map_.capacity()) { // we can put the new buffer into the map, but don't shift things around
2030     // until all buffers are allocated.  If we throw, we don't need to fix
2031     // anything up (any added buffers are undetectible)
2032     if (__map_.__front_spare() > 0)
2033       __map_.push_front(__alloc_traits::allocate(__a, __block_size));
2034     else {
2035       __map_.push_back(__alloc_traits::allocate(__a, __block_size));
2036       // Done allocating, reorder capacity
2037       pointer __pt = __map_.back();
2038       __map_.pop_back();
2039       __map_.push_front(__pt);
2040     }
2041     __start_ = __map_.size() == 1 ? __block_size / 2 : __start_ + __block_size;
2042   }
2043   // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2044   else {
2045     __split_buffer<pointer, __pointer_allocator&> __buf(
2046         std::max<size_type>(2 * __map_.capacity(), 1), 0, __map_.__alloc());
2047 
2048     typedef __allocator_destructor<_Allocator> _Dp;
2049     unique_ptr<pointer, _Dp> __hold(__alloc_traits::allocate(__a, __block_size), _Dp(__a, __block_size));
2050     __buf.push_back(__hold.get());
2051     __hold.release();
2052 
2053     for (__map_pointer __i = __map_.begin(); __i != __map_.end(); ++__i)
2054       __buf.push_back(*__i);
2055     std::swap(__map_.__first_, __buf.__first_);
2056     std::swap(__map_.__begin_, __buf.__begin_);
2057     std::swap(__map_.__end_, __buf.__end_);
2058     std::swap(__map_.__end_cap(), __buf.__end_cap());
2059     __start_ = __map_.size() == 1 ? __block_size / 2 : __start_ + __block_size;
2060   }
2061   __annotate_whole_block(0, __asan_poison);
2062 }
2063 
2064 // Create front capacity for __n elements.
2065 // Strong guarantee.  Either do it or don't touch anything.
2066 template <class _Tp, class _Allocator>
2067 void deque<_Tp, _Allocator>::__add_front_capacity(size_type __n) {
2068   allocator_type& __a = __alloc();
2069   size_type __nb      = __recommend_blocks(__n + __map_.empty());
2070   // Number of unused blocks at back:
2071   size_type __back_capacity = __back_spare() / __block_size;
2072   __back_capacity           = std::min(__back_capacity, __nb); // don't take more than you need
2073   __nb -= __back_capacity;                                     // number of blocks need to allocate
2074   // If __nb == 0, then we have sufficient capacity.
2075   if (__nb == 0) {
2076     __start_ += __block_size * __back_capacity;
2077     for (; __back_capacity > 0; --__back_capacity) {
2078       pointer __pt = __map_.back();
2079       __map_.pop_back();
2080       __map_.push_front(__pt);
2081     }
2082   }
2083   // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2084   else if (__nb <= __map_.capacity() -
2085                        __map_.size()) { // we can put the new buffers into the map, but don't shift things around
2086     // until all buffers are allocated.  If we throw, we don't need to fix
2087     // anything up (any added buffers are undetectible)
2088     for (; __nb > 0; --__nb, __start_ += __block_size - (__map_.size() == 1)) {
2089       if (__map_.__front_spare() == 0)
2090         break;
2091       __map_.push_front(__alloc_traits::allocate(__a, __block_size));
2092       __annotate_whole_block(0, __asan_poison);
2093     }
2094     for (; __nb > 0; --__nb, ++__back_capacity)
2095       __map_.push_back(__alloc_traits::allocate(__a, __block_size));
2096     // Done allocating, reorder capacity
2097     __start_ += __back_capacity * __block_size;
2098     for (; __back_capacity > 0; --__back_capacity) {
2099       pointer __pt = __map_.back();
2100       __map_.pop_back();
2101       __map_.push_front(__pt);
2102       __annotate_whole_block(0, __asan_poison);
2103     }
2104   }
2105   // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2106   else {
2107     size_type __ds = (__nb + __back_capacity) * __block_size - __map_.empty();
2108     __split_buffer<pointer, __pointer_allocator&> __buf(
2109         std::max<size_type>(2 * __map_.capacity(), __nb + __map_.size()), 0, __map_.__alloc());
2110 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2111     try {
2112 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
2113       for (; __nb > 0; --__nb) {
2114         __buf.push_back(__alloc_traits::allocate(__a, __block_size));
2115         // ASan: this is empty container, we have to poison whole block
2116         __annotate_poison_block(std::__to_address(__buf.back()), std::__to_address(__buf.back() + __block_size));
2117       }
2118 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2119     } catch (...) {
2120       __annotate_delete();
2121       for (__map_pointer __i = __buf.begin(); __i != __buf.end(); ++__i)
2122         __alloc_traits::deallocate(__a, *__i, __block_size);
2123       throw;
2124     }
2125 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
2126     for (; __back_capacity > 0; --__back_capacity) {
2127       __buf.push_back(__map_.back());
2128       __map_.pop_back();
2129     }
2130     for (__map_pointer __i = __map_.begin(); __i != __map_.end(); ++__i)
2131       __buf.push_back(*__i);
2132     std::swap(__map_.__first_, __buf.__first_);
2133     std::swap(__map_.__begin_, __buf.__begin_);
2134     std::swap(__map_.__end_, __buf.__end_);
2135     std::swap(__map_.__end_cap(), __buf.__end_cap());
2136     __start_ += __ds;
2137   }
2138 }
2139 
2140 // Create back capacity for one block of elements.
2141 // Strong guarantee.  Either do it or don't touch anything.
2142 template <class _Tp, class _Allocator>
2143 void deque<_Tp, _Allocator>::__add_back_capacity() {
2144   allocator_type& __a = __alloc();
2145   if (__front_spare() >= __block_size) {
2146     __start_ -= __block_size;
2147     pointer __pt = __map_.front();
2148     __map_.pop_front();
2149     __map_.push_back(__pt);
2150   }
2151   // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2152   else if (__map_.size() < __map_.capacity()) { // we can put the new buffer into the map, but don't shift things around
2153     // until it is allocated.  If we throw, we don't need to fix
2154     // anything up (any added buffers are undetectible)
2155     if (__map_.__back_spare() != 0)
2156       __map_.push_back(__alloc_traits::allocate(__a, __block_size));
2157     else {
2158       __map_.push_front(__alloc_traits::allocate(__a, __block_size));
2159       // Done allocating, reorder capacity
2160       pointer __pt = __map_.front();
2161       __map_.pop_front();
2162       __map_.push_back(__pt);
2163     }
2164     __annotate_whole_block(__map_.size() - 1, __asan_poison);
2165   }
2166   // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2167   else {
2168     __split_buffer<pointer, __pointer_allocator&> __buf(
2169         std::max<size_type>(2 * __map_.capacity(), 1), __map_.size(), __map_.__alloc());
2170 
2171     typedef __allocator_destructor<_Allocator> _Dp;
2172     unique_ptr<pointer, _Dp> __hold(__alloc_traits::allocate(__a, __block_size), _Dp(__a, __block_size));
2173     __buf.push_back(__hold.get());
2174     __hold.release();
2175 
2176     for (__map_pointer __i = __map_.end(); __i != __map_.begin();)
2177       __buf.push_front(*--__i);
2178     std::swap(__map_.__first_, __buf.__first_);
2179     std::swap(__map_.__begin_, __buf.__begin_);
2180     std::swap(__map_.__end_, __buf.__end_);
2181     std::swap(__map_.__end_cap(), __buf.__end_cap());
2182     __annotate_whole_block(__map_.size() - 1, __asan_poison);
2183   }
2184 }
2185 
2186 // Create back capacity for __n elements.
2187 // Strong guarantee.  Either do it or don't touch anything.
2188 template <class _Tp, class _Allocator>
2189 void deque<_Tp, _Allocator>::__add_back_capacity(size_type __n) {
2190   allocator_type& __a = __alloc();
2191   size_type __nb      = __recommend_blocks(__n + __map_.empty());
2192   // Number of unused blocks at front:
2193   size_type __front_capacity = __front_spare() / __block_size;
2194   __front_capacity           = std::min(__front_capacity, __nb); // don't take more than you need
2195   __nb -= __front_capacity;                                      // number of blocks need to allocate
2196   // If __nb == 0, then we have sufficient capacity.
2197   if (__nb == 0) {
2198     __start_ -= __block_size * __front_capacity;
2199     for (; __front_capacity > 0; --__front_capacity) {
2200       pointer __pt = __map_.front();
2201       __map_.pop_front();
2202       __map_.push_back(__pt);
2203     }
2204   }
2205   // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2206   else if (__nb <= __map_.capacity() -
2207                        __map_.size()) { // we can put the new buffers into the map, but don't shift things around
2208     // until all buffers are allocated.  If we throw, we don't need to fix
2209     // anything up (any added buffers are undetectible)
2210     for (; __nb > 0; --__nb) {
2211       if (__map_.__back_spare() == 0)
2212         break;
2213       __map_.push_back(__alloc_traits::allocate(__a, __block_size));
2214       __annotate_whole_block(__map_.size() - 1, __asan_poison);
2215     }
2216     for (; __nb > 0; --__nb, ++__front_capacity, __start_ += __block_size - (__map_.size() == 1)) {
2217       __map_.push_front(__alloc_traits::allocate(__a, __block_size));
2218       __annotate_whole_block(0, __asan_poison);
2219     }
2220     // Done allocating, reorder capacity
2221     __start_ -= __block_size * __front_capacity;
2222     for (; __front_capacity > 0; --__front_capacity) {
2223       pointer __pt = __map_.front();
2224       __map_.pop_front();
2225       __map_.push_back(__pt);
2226     }
2227   }
2228   // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2229   else {
2230     size_type __ds = __front_capacity * __block_size;
2231     __split_buffer<pointer, __pointer_allocator&> __buf(
2232         std::max<size_type>(2 * __map_.capacity(), __nb + __map_.size()),
2233         __map_.size() - __front_capacity,
2234         __map_.__alloc());
2235 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2236     try {
2237 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
2238       for (; __nb > 0; --__nb) {
2239         __buf.push_back(__alloc_traits::allocate(__a, __block_size));
2240         // ASan: this is an empty container, we have to poison the whole block
2241         __annotate_poison_block(std::__to_address(__buf.back()), std::__to_address(__buf.back() + __block_size));
2242       }
2243 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
2244     } catch (...) {
2245       __annotate_delete();
2246       for (__map_pointer __i = __buf.begin(); __i != __buf.end(); ++__i)
2247         __alloc_traits::deallocate(__a, *__i, __block_size);
2248       throw;
2249     }
2250 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
2251     for (; __front_capacity > 0; --__front_capacity) {
2252       __buf.push_back(__map_.front());
2253       __map_.pop_front();
2254     }
2255     for (__map_pointer __i = __map_.end(); __i != __map_.begin();)
2256       __buf.push_front(*--__i);
2257     std::swap(__map_.__first_, __buf.__first_);
2258     std::swap(__map_.__begin_, __buf.__begin_);
2259     std::swap(__map_.__end_, __buf.__end_);
2260     std::swap(__map_.__end_cap(), __buf.__end_cap());
2261     __start_ -= __ds;
2262   }
2263 }
2264 
2265 template <class _Tp, class _Allocator>
2266 void deque<_Tp, _Allocator>::pop_front() {
2267   _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "deque::pop_front called on an empty deque");
2268   size_type __old_sz    = size();
2269   size_type __old_start = __start_;
2270   allocator_type& __a   = __alloc();
2271   __alloc_traits::destroy(
2272       __a, std::__to_address(*(__map_.begin() + __start_ / __block_size) + __start_ % __block_size));
2273   --__size();
2274   ++__start_;
2275   __annotate_shrink_front(__old_sz, __old_start);
2276   __maybe_remove_front_spare();
2277 }
2278 
2279 template <class _Tp, class _Allocator>
2280 void deque<_Tp, _Allocator>::pop_back() {
2281   _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "deque::pop_back called on an empty deque");
2282   size_type __old_sz    = size();
2283   size_type __old_start = __start_;
2284   allocator_type& __a   = __alloc();
2285   size_type __p         = size() + __start_ - 1;
2286   __alloc_traits::destroy(__a, std::__to_address(*(__map_.begin() + __p / __block_size) + __p % __block_size));
2287   --__size();
2288   __annotate_shrink_back(__old_sz, __old_start);
2289   __maybe_remove_back_spare();
2290 }
2291 
2292 // move assign [__f, __l) to [__r, __r + (__l-__f)).
2293 // If __vt points into [__f, __l), then subtract (__f - __r) from __vt.
2294 template <class _Tp, class _Allocator>
2295 typename deque<_Tp, _Allocator>::iterator
2296 deque<_Tp, _Allocator>::__move_and_check(iterator __f, iterator __l, iterator __r, const_pointer& __vt) {
2297   // as if
2298   //   for (; __f != __l; ++__f, ++__r)
2299   //       *__r = std::move(*__f);
2300   difference_type __n = __l - __f;
2301   while (__n > 0) {
2302     pointer __fb         = __f.__ptr_;
2303     pointer __fe         = *__f.__m_iter_ + __block_size;
2304     difference_type __bs = __fe - __fb;
2305     if (__bs > __n) {
2306       __bs = __n;
2307       __fe = __fb + __bs;
2308     }
2309     if (__fb <= __vt && __vt < __fe)
2310       __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) -= __f - __r).__ptr_;
2311     __r = std::move(__fb, __fe, __r);
2312     __n -= __bs;
2313     __f += __bs;
2314   }
2315   return __r;
2316 }
2317 
2318 // move assign [__f, __l) to [__r - (__l-__f), __r) backwards.
2319 // If __vt points into [__f, __l), then add (__r - __l) to __vt.
2320 template <class _Tp, class _Allocator>
2321 typename deque<_Tp, _Allocator>::iterator
2322 deque<_Tp, _Allocator>::__move_backward_and_check(iterator __f, iterator __l, iterator __r, const_pointer& __vt) {
2323   // as if
2324   //   while (__f != __l)
2325   //       *--__r = std::move(*--__l);
2326   difference_type __n = __l - __f;
2327   while (__n > 0) {
2328     --__l;
2329     pointer __lb         = *__l.__m_iter_;
2330     pointer __le         = __l.__ptr_ + 1;
2331     difference_type __bs = __le - __lb;
2332     if (__bs > __n) {
2333       __bs = __n;
2334       __lb = __le - __bs;
2335     }
2336     if (__lb <= __vt && __vt < __le)
2337       __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) += __r - __l - 1).__ptr_;
2338     __r = std::move_backward(__lb, __le, __r);
2339     __n -= __bs;
2340     __l -= __bs - 1;
2341   }
2342   return __r;
2343 }
2344 
2345 // move construct [__f, __l) to [__r, __r + (__l-__f)).
2346 // If __vt points into [__f, __l), then add (__r - __f) to __vt.
2347 template <class _Tp, class _Allocator>
2348 void deque<_Tp, _Allocator>::__move_construct_and_check(iterator __f, iterator __l, iterator __r, const_pointer& __vt) {
2349   allocator_type& __a = __alloc();
2350   // as if
2351   //   for (; __f != __l; ++__r, ++__f, ++__size())
2352   //       __alloc_traits::construct(__a, std::addressof(*__r), std::move(*__f));
2353   difference_type __n = __l - __f;
2354   while (__n > 0) {
2355     pointer __fb         = __f.__ptr_;
2356     pointer __fe         = *__f.__m_iter_ + __block_size;
2357     difference_type __bs = __fe - __fb;
2358     if (__bs > __n) {
2359       __bs = __n;
2360       __fe = __fb + __bs;
2361     }
2362     if (__fb <= __vt && __vt < __fe)
2363       __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) += __r - __f).__ptr_;
2364     for (; __fb != __fe; ++__fb, ++__r, ++__size())
2365       __alloc_traits::construct(__a, std::addressof(*__r), std::move(*__fb));
2366     __n -= __bs;
2367     __f += __bs;
2368   }
2369 }
2370 
2371 // move construct [__f, __l) to [__r - (__l-__f), __r) backwards.
2372 // If __vt points into [__f, __l), then subtract (__l - __r) from __vt.
2373 template <class _Tp, class _Allocator>
2374 void deque<_Tp, _Allocator>::__move_construct_backward_and_check(
2375     iterator __f, iterator __l, iterator __r, const_pointer& __vt) {
2376   allocator_type& __a = __alloc();
2377   // as if
2378   //   for (iterator __j = __l; __j != __f;)
2379   //   {
2380   //       __alloc_traitsconstruct(__a, std::addressof(*--__r), std::move(*--__j));
2381   //       --__start_;
2382   //       ++__size();
2383   //   }
2384   difference_type __n = __l - __f;
2385   while (__n > 0) {
2386     --__l;
2387     pointer __lb         = *__l.__m_iter_;
2388     pointer __le         = __l.__ptr_ + 1;
2389     difference_type __bs = __le - __lb;
2390     if (__bs > __n) {
2391       __bs = __n;
2392       __lb = __le - __bs;
2393     }
2394     if (__lb <= __vt && __vt < __le)
2395       __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) -= __l - __r + 1).__ptr_;
2396     while (__le != __lb) {
2397       __alloc_traits::construct(__a, std::addressof(*--__r), std::move(*--__le));
2398       --__start_;
2399       ++__size();
2400     }
2401     __n -= __bs;
2402     __l -= __bs - 1;
2403   }
2404 }
2405 
2406 template <class _Tp, class _Allocator>
2407 typename deque<_Tp, _Allocator>::iterator deque<_Tp, _Allocator>::erase(const_iterator __f) {
2408   _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
2409       __f != end(), "deque::erase(iterator) called with a non-dereferenceable iterator");
2410   size_type __old_sz    = size();
2411   size_type __old_start = __start_;
2412   iterator __b          = begin();
2413   difference_type __pos = __f - __b;
2414   iterator __p          = __b + __pos;
2415   allocator_type& __a   = __alloc();
2416   if (static_cast<size_t>(__pos) <= (size() - 1) / 2) { // erase from front
2417     std::move_backward(__b, __p, std::next(__p));
2418     __alloc_traits::destroy(__a, std::addressof(*__b));
2419     --__size();
2420     ++__start_;
2421     __annotate_shrink_front(__old_sz, __old_start);
2422     __maybe_remove_front_spare();
2423   } else { // erase from back
2424     iterator __i = std::move(std::next(__p), end(), __p);
2425     __alloc_traits::destroy(__a, std::addressof(*__i));
2426     --__size();
2427     __annotate_shrink_back(__old_sz, __old_start);
2428     __maybe_remove_back_spare();
2429   }
2430   return begin() + __pos;
2431 }
2432 
2433 template <class _Tp, class _Allocator>
2434 typename deque<_Tp, _Allocator>::iterator deque<_Tp, _Allocator>::erase(const_iterator __f, const_iterator __l) {
2435   _LIBCPP_ASSERT_VALID_INPUT_RANGE(__f <= __l, "deque::erase(first, last) called with an invalid range");
2436   size_type __old_sz    = size();
2437   size_type __old_start = __start_;
2438   difference_type __n   = __l - __f;
2439   iterator __b          = begin();
2440   difference_type __pos = __f - __b;
2441   iterator __p          = __b + __pos;
2442   if (__n > 0) {
2443     allocator_type& __a = __alloc();
2444     if (static_cast<size_t>(__pos) <= (size() - __n) / 2) { // erase from front
2445       iterator __i = std::move_backward(__b, __p, __p + __n);
2446       for (; __b != __i; ++__b)
2447         __alloc_traits::destroy(__a, std::addressof(*__b));
2448       __size() -= __n;
2449       __start_ += __n;
2450       __annotate_shrink_front(__old_sz, __old_start);
2451       while (__maybe_remove_front_spare()) {
2452       }
2453     } else { // erase from back
2454       iterator __i = std::move(__p + __n, end(), __p);
2455       for (iterator __e = end(); __i != __e; ++__i)
2456         __alloc_traits::destroy(__a, std::addressof(*__i));
2457       __size() -= __n;
2458       __annotate_shrink_back(__old_sz, __old_start);
2459       while (__maybe_remove_back_spare()) {
2460       }
2461     }
2462   }
2463   return begin() + __pos;
2464 }
2465 
2466 template <class _Tp, class _Allocator>
2467 void deque<_Tp, _Allocator>::__erase_to_end(const_iterator __f) {
2468   size_type __old_sz    = size();
2469   size_type __old_start = __start_;
2470   iterator __e          = end();
2471   difference_type __n   = __e - __f;
2472   if (__n > 0) {
2473     allocator_type& __a   = __alloc();
2474     iterator __b          = begin();
2475     difference_type __pos = __f - __b;
2476     for (iterator __p = __b + __pos; __p != __e; ++__p)
2477       __alloc_traits::destroy(__a, std::addressof(*__p));
2478     __size() -= __n;
2479     __annotate_shrink_back(__old_sz, __old_start);
2480     while (__maybe_remove_back_spare()) {
2481     }
2482   }
2483 }
2484 
2485 template <class _Tp, class _Allocator>
2486 inline void deque<_Tp, _Allocator>::swap(deque& __c)
2487 #if _LIBCPP_STD_VER >= 14
2488     _NOEXCEPT
2489 #else
2490     _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<allocator_type>)
2491 #endif
2492 {
2493   __map_.swap(__c.__map_);
2494   std::swap(__start_, __c.__start_);
2495   std::swap(__size(), __c.__size());
2496   std::__swap_allocator(__alloc(), __c.__alloc());
2497 }
2498 
2499 template <class _Tp, class _Allocator>
2500 inline void deque<_Tp, _Allocator>::clear() _NOEXCEPT {
2501   __annotate_delete();
2502   allocator_type& __a = __alloc();
2503   for (iterator __i = begin(), __e = end(); __i != __e; ++__i)
2504     __alloc_traits::destroy(__a, std::addressof(*__i));
2505   __size() = 0;
2506   while (__map_.size() > 2) {
2507     __alloc_traits::deallocate(__a, __map_.front(), __block_size);
2508     __map_.pop_front();
2509   }
2510   switch (__map_.size()) {
2511   case 1:
2512     __start_ = __block_size / 2;
2513     break;
2514   case 2:
2515     __start_ = __block_size;
2516     break;
2517   }
2518   __annotate_new(0);
2519 }
2520 
2521 template <class _Tp, class _Allocator>
2522 inline _LIBCPP_HIDE_FROM_ABI bool operator==(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) {
2523   const typename deque<_Tp, _Allocator>::size_type __sz = __x.size();
2524   return __sz == __y.size() && std::equal(__x.begin(), __x.end(), __y.begin());
2525 }
2526 
2527 #if _LIBCPP_STD_VER <= 17
2528 
2529 template <class _Tp, class _Allocator>
2530 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) {
2531   return !(__x == __y);
2532 }
2533 
2534 template <class _Tp, class _Allocator>
2535 inline _LIBCPP_HIDE_FROM_ABI bool operator<(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) {
2536   return std::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
2537 }
2538 
2539 template <class _Tp, class _Allocator>
2540 inline _LIBCPP_HIDE_FROM_ABI bool operator>(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) {
2541   return __y < __x;
2542 }
2543 
2544 template <class _Tp, class _Allocator>
2545 inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) {
2546   return !(__x < __y);
2547 }
2548 
2549 template <class _Tp, class _Allocator>
2550 inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) {
2551   return !(__y < __x);
2552 }
2553 
2554 #else // _LIBCPP_STD_VER <= 17
2555 
2556 template <class _Tp, class _Allocator>
2557 _LIBCPP_HIDE_FROM_ABI __synth_three_way_result<_Tp>
2558 operator<=>(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y) {
2559   return std::lexicographical_compare_three_way(__x.begin(), __x.end(), __y.begin(), __y.end(), std::__synth_three_way);
2560 }
2561 
2562 #endif // _LIBCPP_STD_VER <= 17
2563 
2564 template <class _Tp, class _Allocator>
2565 inline _LIBCPP_HIDE_FROM_ABI void swap(deque<_Tp, _Allocator>& __x, deque<_Tp, _Allocator>& __y)
2566     _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) {
2567   __x.swap(__y);
2568 }
2569 
2570 #if _LIBCPP_STD_VER >= 20
2571 template <class _Tp, class _Allocator, class _Up>
2572 inline _LIBCPP_HIDE_FROM_ABI typename deque<_Tp, _Allocator>::size_type
2573 erase(deque<_Tp, _Allocator>& __c, const _Up& __v) {
2574   auto __old_size = __c.size();
2575   __c.erase(std::remove(__c.begin(), __c.end(), __v), __c.end());
2576   return __old_size - __c.size();
2577 }
2578 
2579 template <class _Tp, class _Allocator, class _Predicate>
2580 inline _LIBCPP_HIDE_FROM_ABI typename deque<_Tp, _Allocator>::size_type
2581 erase_if(deque<_Tp, _Allocator>& __c, _Predicate __pred) {
2582   auto __old_size = __c.size();
2583   __c.erase(std::remove_if(__c.begin(), __c.end(), __pred), __c.end());
2584   return __old_size - __c.size();
2585 }
2586 
2587 template <>
2588 inline constexpr bool __format::__enable_insertable<std::deque<char>> = true;
2589 #  ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
2590 template <>
2591 inline constexpr bool __format::__enable_insertable<std::deque<wchar_t>> = true;
2592 #  endif
2593 
2594 #endif // _LIBCPP_STD_VER >= 20
2595 
2596 _LIBCPP_END_NAMESPACE_STD
2597 
2598 #if _LIBCPP_STD_VER >= 17
2599 _LIBCPP_BEGIN_NAMESPACE_STD
2600 namespace pmr {
2601 template <class _ValueT>
2602 using deque _LIBCPP_AVAILABILITY_PMR = std::deque<_ValueT, polymorphic_allocator<_ValueT>>;
2603 } // namespace pmr
2604 _LIBCPP_END_NAMESPACE_STD
2605 #endif
2606 
2607 _LIBCPP_POP_MACROS
2608 
2609 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
2610 #  include <__cxx03/algorithm>
2611 #  include <__cxx03/atomic>
2612 #  include <__cxx03/concepts>
2613 #  include <__cxx03/cstdlib>
2614 #  include <__cxx03/functional>
2615 #  include <__cxx03/iosfwd>
2616 #  include <__cxx03/iterator>
2617 #  include <__cxx03/type_traits>
2618 #  include <__cxx03/typeinfo>
2619 #endif
2620 
2621 #endif // _LIBCPP___CXX03_DEQUE