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