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