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