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