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