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