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