deque revision 253159
1227825Stheraven// -*- C++ -*-
2227825Stheraven//===---------------------------- deque -----------------------------------===//
3227825Stheraven//
4227825Stheraven//                     The LLVM Compiler Infrastructure
5227825Stheraven//
6227825Stheraven// This file is dual licensed under the MIT and the University of Illinois Open
7227825Stheraven// Source Licenses. See LICENSE.TXT for details.
8227825Stheraven//
9227825Stheraven//===----------------------------------------------------------------------===//
10227825Stheraven
11227825Stheraven#ifndef _LIBCPP_DEQUE
12227825Stheraven#define _LIBCPP_DEQUE
13227825Stheraven
14227825Stheraven/*
15227825Stheraven    deque synopsis
16227825Stheraven
17227825Stheravennamespace std
18227825Stheraven{
19227825Stheraven
20227825Stheraventemplate <class T, class Allocator = allocator<T> >
21227825Stheravenclass deque
22227825Stheraven{
23227825Stheravenpublic:
24227825Stheraven    // types:
25227825Stheraven    typedef T value_type;
26227825Stheraven    typedef Allocator allocator_type;
27227825Stheraven
28227825Stheraven    typedef typename allocator_type::reference       reference;
29227825Stheraven    typedef typename allocator_type::const_reference const_reference;
30227825Stheraven    typedef implementation-defined                   iterator;
31227825Stheraven    typedef implementation-defined                   const_iterator;
32227825Stheraven    typedef typename allocator_type::size_type       size_type;
33227825Stheraven    typedef typename allocator_type::difference_type difference_type;
34227825Stheraven
35227825Stheraven    typedef typename allocator_type::pointer         pointer;
36227825Stheraven    typedef typename allocator_type::const_pointer   const_pointer;
37227825Stheraven    typedef std::reverse_iterator<iterator>          reverse_iterator;
38227825Stheraven    typedef std::reverse_iterator<const_iterator>    const_reverse_iterator;
39227825Stheraven
40227825Stheraven    // construct/copy/destroy:
41227825Stheraven    deque() noexcept(is_nothrow_default_constructible<allocator_type>::value);
42227825Stheraven    explicit deque(const allocator_type& a);
43227825Stheraven    explicit deque(size_type n);
44227825Stheraven    deque(size_type n, const value_type& v);
45227825Stheraven    deque(size_type n, const value_type& v, const allocator_type& a);
46227825Stheraven    template <class InputIterator>
47227825Stheraven        deque(InputIterator f, InputIterator l);
48227825Stheraven    template <class InputIterator>
49227825Stheraven        deque(InputIterator f, InputIterator l, const allocator_type& a);
50227825Stheraven    deque(const deque& c);
51227825Stheraven    deque(deque&& c)
52227825Stheraven        noexcept(is_nothrow_move_constructible<allocator_type>::value);
53227825Stheraven    deque(initializer_list<value_type> il, const Allocator& a = allocator_type());
54227825Stheraven    deque(const deque& c, const allocator_type& a);
55227825Stheraven    deque(deque&& c, const allocator_type& a);
56227825Stheraven    ~deque();
57227825Stheraven
58227825Stheraven    deque& operator=(const deque& c);
59227825Stheraven    deque& operator=(deque&& c)
60227825Stheraven        noexcept(
61227825Stheraven             allocator_type::propagate_on_container_move_assignment::value &&
62227825Stheraven             is_nothrow_move_assignable<allocator_type>::value);
63227825Stheraven    deque& operator=(initializer_list<value_type> il);
64227825Stheraven
65227825Stheraven    template <class InputIterator>
66227825Stheraven        void assign(InputIterator f, InputIterator l);
67227825Stheraven    void assign(size_type n, const value_type& v);
68227825Stheraven    void assign(initializer_list<value_type> il);
69227825Stheraven
70227825Stheraven    allocator_type get_allocator() const noexcept;
71227825Stheraven
72227825Stheraven    // iterators:
73227825Stheraven
74227825Stheraven    iterator       begin() noexcept;
75227825Stheraven    const_iterator begin() const noexcept;
76227825Stheraven    iterator       end() noexcept;
77227825Stheraven    const_iterator end() const noexcept;
78227825Stheraven
79227825Stheraven    reverse_iterator       rbegin() noexcept;
80227825Stheraven    const_reverse_iterator rbegin() const noexcept;
81227825Stheraven    reverse_iterator       rend() noexcept;
82227825Stheraven    const_reverse_iterator rend() const noexcept;
83227825Stheraven
84227825Stheraven    const_iterator         cbegin() const noexcept;
85227825Stheraven    const_iterator         cend() const noexcept;
86227825Stheraven    const_reverse_iterator crbegin() const noexcept;
87227825Stheraven    const_reverse_iterator crend() const noexcept;
88227825Stheraven
89227825Stheraven    // capacity:
90227825Stheraven    size_type size() const noexcept;
91227825Stheraven    size_type max_size() const noexcept;
92227825Stheraven    void resize(size_type n);
93227825Stheraven    void resize(size_type n, const value_type& v);
94227825Stheraven    void shrink_to_fit();
95227825Stheraven    bool empty() const noexcept;
96227825Stheraven
97227825Stheraven    // element access:
98227825Stheraven    reference operator[](size_type i);
99227825Stheraven    const_reference operator[](size_type i) const;
100227825Stheraven    reference at(size_type i);
101227825Stheraven    const_reference at(size_type i) const;
102227825Stheraven    reference front();
103227825Stheraven    const_reference front() const;
104227825Stheraven    reference back();
105227825Stheraven    const_reference back() const;
106227825Stheraven
107227825Stheraven    // modifiers:
108227825Stheraven    void push_front(const value_type& v);
109227825Stheraven    void push_front(value_type&& v);
110227825Stheraven    void push_back(const value_type& v);
111227825Stheraven    void push_back(value_type&& v);
112227825Stheraven    template <class... Args> void emplace_front(Args&&... args);
113227825Stheraven    template <class... Args> void emplace_back(Args&&... args);
114227825Stheraven    template <class... Args> iterator emplace(const_iterator p, Args&&... args);
115227825Stheraven    iterator insert(const_iterator p, const value_type& v);
116227825Stheraven    iterator insert(const_iterator p, value_type&& v);
117227825Stheraven    iterator insert(const_iterator p, size_type n, const value_type& v);
118227825Stheraven    template <class InputIterator>
119227825Stheraven        iterator insert (const_iterator p, InputIterator f, InputIterator l);
120227825Stheraven    iterator insert(const_iterator p, initializer_list<value_type> il);
121227825Stheraven    void pop_front();
122227825Stheraven    void pop_back();
123227825Stheraven    iterator erase(const_iterator p);
124227825Stheraven    iterator erase(const_iterator f, const_iterator l);
125227825Stheraven    void swap(deque& c)
126227825Stheraven        noexcept(!allocator_type::propagate_on_container_swap::value ||
127227825Stheraven                 __is_nothrow_swappable<allocator_type>::value);
128227825Stheraven    void clear() noexcept;
129227825Stheraven};
130227825Stheraven
131227825Stheraventemplate <class T, class Allocator>
132227825Stheraven    bool operator==(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
133227825Stheraventemplate <class T, class Allocator>
134227825Stheraven    bool operator< (const deque<T,Allocator>& x, const deque<T,Allocator>& y);
135227825Stheraventemplate <class T, class Allocator>
136227825Stheraven    bool operator!=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
137227825Stheraventemplate <class T, class Allocator>
138227825Stheraven    bool operator> (const deque<T,Allocator>& x, const deque<T,Allocator>& y);
139227825Stheraventemplate <class T, class Allocator>
140227825Stheraven    bool operator>=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
141227825Stheraventemplate <class T, class Allocator>
142227825Stheraven    bool operator<=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
143227825Stheraven
144227825Stheraven// specialized algorithms:
145227825Stheraventemplate <class T, class Allocator>
146227825Stheraven    void swap(deque<T,Allocator>& x, deque<T,Allocator>& y)
147227825Stheraven         noexcept(noexcept(x.swap(y)));
148227825Stheraven
149227825Stheraven}  // std
150227825Stheraven
151227825Stheraven*/
152227825Stheraven
153227825Stheraven#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
154227825Stheraven#pragma GCC system_header
155227825Stheraven#endif
156227825Stheraven
157227825Stheraven#include <__config>
158227825Stheraven#include <__split_buffer>
159227825Stheraven#include <type_traits>
160227825Stheraven#include <initializer_list>
161227825Stheraven#include <iterator>
162227825Stheraven#include <algorithm>
163227825Stheraven#include <stdexcept>
164227825Stheraven
165232950Stheraven#include <__undef_min_max>
166232950Stheraven
167227825Stheraven_LIBCPP_BEGIN_NAMESPACE_STD
168227825Stheraven
169227825Stheraventemplate <class _Tp, class _Allocator> class __deque_base;
170227825Stheraven
171227825Stheraventemplate <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
172227825Stheraven          class _DiffType, _DiffType _BlockSize>
173249998Sdimclass _LIBCPP_TYPE_VIS __deque_iterator;
174227825Stheraven
175227825Stheraventemplate <class _RAIter,
176227825Stheraven          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
177227825Stheraven__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
178227825Stheravencopy(_RAIter __f,
179227825Stheraven     _RAIter __l,
180227825Stheraven     __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
181227825Stheraven     typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
182227825Stheraven
183227825Stheraventemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
184227825Stheraven          class _OutputIterator>
185227825Stheraven_OutputIterator
186227825Stheravencopy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
187227825Stheraven     __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
188227825Stheraven     _OutputIterator __r);
189227825Stheraven
190227825Stheraventemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
191227825Stheraven          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
192227825Stheraven__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
193227825Stheravencopy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
194227825Stheraven     __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
195227825Stheraven     __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
196227825Stheraven
197227825Stheraventemplate <class _RAIter,
198227825Stheraven          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
199227825Stheraven__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
200227825Stheravencopy_backward(_RAIter __f,
201227825Stheraven              _RAIter __l,
202227825Stheraven              __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
203227825Stheraven              typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
204227825Stheraven
205227825Stheraventemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
206227825Stheraven          class _OutputIterator>
207227825Stheraven_OutputIterator
208227825Stheravencopy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
209227825Stheraven              __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
210227825Stheraven              _OutputIterator __r);
211227825Stheraven
212227825Stheraventemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
213227825Stheraven          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
214227825Stheraven__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
215227825Stheravencopy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
216227825Stheraven              __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
217227825Stheraven              __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
218227825Stheraven
219227825Stheraventemplate <class _RAIter,
220227825Stheraven          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
221227825Stheraven__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
222227825Stheravenmove(_RAIter __f,
223227825Stheraven     _RAIter __l,
224227825Stheraven     __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
225227825Stheraven     typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
226227825Stheraven
227227825Stheraventemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
228227825Stheraven          class _OutputIterator>
229227825Stheraven_OutputIterator
230227825Stheravenmove(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
231227825Stheraven     __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
232227825Stheraven     _OutputIterator __r);
233227825Stheraven
234227825Stheraventemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
235227825Stheraven          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
236227825Stheraven__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
237227825Stheravenmove(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
238227825Stheraven     __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
239227825Stheraven     __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
240227825Stheraven
241227825Stheraventemplate <class _RAIter,
242227825Stheraven          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
243227825Stheraven__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
244227825Stheravenmove_backward(_RAIter __f,
245227825Stheraven              _RAIter __l,
246227825Stheraven              __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
247227825Stheraven              typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
248227825Stheraven
249227825Stheraventemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
250227825Stheraven          class _OutputIterator>
251227825Stheraven_OutputIterator
252227825Stheravenmove_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
253227825Stheraven              __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
254227825Stheraven              _OutputIterator __r);
255227825Stheraven
256227825Stheraventemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
257227825Stheraven          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
258227825Stheraven__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
259227825Stheravenmove_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
260227825Stheraven              __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
261227825Stheraven              __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
262227825Stheraven
263227825Stheraventemplate <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
264227825Stheraven          class _DiffType, _DiffType _BlockSize>
265249998Sdimclass _LIBCPP_TYPE_VIS __deque_iterator
266227825Stheraven{
267227825Stheraven    typedef _MapPointer __map_iterator;
268227825Stheravenpublic:
269227825Stheraven    typedef _Pointer  pointer;
270227825Stheraven    typedef _DiffType difference_type;
271227825Stheravenprivate:
272227825Stheraven    __map_iterator __m_iter_;
273227825Stheraven    pointer        __ptr_;
274227825Stheraven
275227825Stheraven    static const difference_type __block_size = _BlockSize;
276227825Stheravenpublic:
277227825Stheraven    typedef _ValueType                  value_type;
278227825Stheraven    typedef random_access_iterator_tag  iterator_category;
279227825Stheraven    typedef _Reference                  reference;
280227825Stheraven
281227825Stheraven    _LIBCPP_INLINE_VISIBILITY __deque_iterator() _NOEXCEPT {}
282227825Stheraven
283232950Stheraven    template <class _Pp, class _Rp, class _MP>
284227825Stheraven    _LIBCPP_INLINE_VISIBILITY
285232950Stheraven    __deque_iterator(const __deque_iterator<value_type, _Pp, _Rp, _MP, difference_type, __block_size>& __it,
286232950Stheraven                typename enable_if<is_convertible<_Pp, pointer>::value>::type* = 0) _NOEXCEPT
287227825Stheraven        : __m_iter_(__it.__m_iter_), __ptr_(__it.__ptr_) {}
288227825Stheraven
289227825Stheraven    _LIBCPP_INLINE_VISIBILITY reference operator*() const {return *__ptr_;}
290227825Stheraven    _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return __ptr_;}
291227825Stheraven
292227825Stheraven    _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator++()
293227825Stheraven    {
294227825Stheraven        if (++__ptr_ - *__m_iter_ == __block_size)
295227825Stheraven        {
296227825Stheraven            ++__m_iter_;
297227825Stheraven            __ptr_ = *__m_iter_;
298227825Stheraven        }
299227825Stheraven        return *this;
300227825Stheraven    }
301227825Stheraven
302227825Stheraven    _LIBCPP_INLINE_VISIBILITY __deque_iterator operator++(int)
303227825Stheraven    {
304227825Stheraven        __deque_iterator __tmp = *this;
305227825Stheraven        ++(*this);
306227825Stheraven        return __tmp;
307227825Stheraven    }
308227825Stheraven
309227825Stheraven    _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator--()
310227825Stheraven    {
311227825Stheraven        if (__ptr_ == *__m_iter_)
312227825Stheraven        {
313227825Stheraven            --__m_iter_;
314227825Stheraven            __ptr_ = *__m_iter_ + __block_size;
315227825Stheraven        }
316227825Stheraven        --__ptr_;
317227825Stheraven        return *this;
318227825Stheraven    }
319227825Stheraven
320227825Stheraven    _LIBCPP_INLINE_VISIBILITY __deque_iterator operator--(int)
321227825Stheraven    {
322227825Stheraven        __deque_iterator __tmp = *this;
323227825Stheraven        --(*this);
324227825Stheraven        return __tmp;
325227825Stheraven    }
326227825Stheraven
327227825Stheraven    _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator+=(difference_type __n)
328227825Stheraven    {
329227825Stheraven        if (__n != 0)
330227825Stheraven        {
331227825Stheraven            __n += __ptr_ - *__m_iter_;
332227825Stheraven            if (__n > 0)
333227825Stheraven            {
334227825Stheraven                __m_iter_ += __n / __block_size;
335227825Stheraven                __ptr_ = *__m_iter_ + __n % __block_size;
336227825Stheraven            }
337227825Stheraven            else // (__n < 0)
338227825Stheraven            {
339227825Stheraven                difference_type __z = __block_size - 1 - __n;
340227825Stheraven                __m_iter_ -= __z / __block_size;
341227825Stheraven                __ptr_ = *__m_iter_ + (__block_size - 1 - __z % __block_size);
342227825Stheraven            }
343227825Stheraven        }
344227825Stheraven        return *this;
345227825Stheraven    }
346227825Stheraven
347227825Stheraven    _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator-=(difference_type __n)
348227825Stheraven    {
349227825Stheraven        return *this += -__n;
350227825Stheraven    }
351227825Stheraven
352227825Stheraven    _LIBCPP_INLINE_VISIBILITY __deque_iterator operator+(difference_type __n) const
353227825Stheraven    {
354227825Stheraven        __deque_iterator __t(*this);
355227825Stheraven        __t += __n;
356227825Stheraven        return __t;
357227825Stheraven    }
358227825Stheraven
359227825Stheraven    _LIBCPP_INLINE_VISIBILITY __deque_iterator operator-(difference_type __n) const
360227825Stheraven    {
361227825Stheraven        __deque_iterator __t(*this);
362227825Stheraven        __t -= __n;
363227825Stheraven        return __t;
364227825Stheraven    }
365227825Stheraven
366227825Stheraven    _LIBCPP_INLINE_VISIBILITY
367227825Stheraven    friend __deque_iterator operator+(difference_type __n, const __deque_iterator& __it)
368227825Stheraven        {return __it + __n;}
369227825Stheraven
370227825Stheraven    _LIBCPP_INLINE_VISIBILITY
371227825Stheraven    friend difference_type operator-(const __deque_iterator& __x, const __deque_iterator& __y)
372227825Stheraven    {
373227825Stheraven        if (__x != __y)
374227825Stheraven            return (__x.__m_iter_ - __y.__m_iter_) * __block_size
375227825Stheraven                 + (__x.__ptr_ - *__x.__m_iter_)
376227825Stheraven                 - (__y.__ptr_ - *__y.__m_iter_);
377227825Stheraven        return 0;
378227825Stheraven    }
379227825Stheraven
380227825Stheraven    _LIBCPP_INLINE_VISIBILITY reference operator[](difference_type __n) const
381227825Stheraven        {return *(*this + __n);}
382227825Stheraven
383227825Stheraven    _LIBCPP_INLINE_VISIBILITY friend
384227825Stheraven        bool operator==(const __deque_iterator& __x, const __deque_iterator& __y)
385227825Stheraven        {return __x.__ptr_ == __y.__ptr_;}
386227825Stheraven
387227825Stheraven    _LIBCPP_INLINE_VISIBILITY friend
388227825Stheraven        bool operator!=(const __deque_iterator& __x, const __deque_iterator& __y)
389227825Stheraven        {return !(__x == __y);}
390227825Stheraven
391227825Stheraven    _LIBCPP_INLINE_VISIBILITY friend
392227825Stheraven        bool operator<(const __deque_iterator& __x, const __deque_iterator& __y)
393227825Stheraven        {return __x.__m_iter_ < __y.__m_iter_ ||
394227825Stheraven               (__x.__m_iter_ == __y.__m_iter_ && __x.__ptr_ < __y.__ptr_);}
395227825Stheraven
396227825Stheraven    _LIBCPP_INLINE_VISIBILITY friend
397227825Stheraven        bool operator>(const __deque_iterator& __x, const __deque_iterator& __y)
398227825Stheraven        {return __y < __x;}
399227825Stheraven
400227825Stheraven    _LIBCPP_INLINE_VISIBILITY friend
401227825Stheraven        bool operator<=(const __deque_iterator& __x, const __deque_iterator& __y)
402227825Stheraven        {return !(__y < __x);}
403227825Stheraven
404227825Stheraven    _LIBCPP_INLINE_VISIBILITY friend
405227825Stheraven        bool operator>=(const __deque_iterator& __x, const __deque_iterator& __y)
406227825Stheraven        {return !(__x < __y);}
407227825Stheraven
408227825Stheravenprivate:
409227825Stheraven    _LIBCPP_INLINE_VISIBILITY __deque_iterator(__map_iterator __m, pointer __p) _NOEXCEPT
410227825Stheraven        : __m_iter_(__m), __ptr_(__p) {}
411227825Stheraven
412232950Stheraven    template <class _Tp, class _Ap> friend class __deque_base;
413249998Sdim    template <class _Tp, class _Ap> friend class _LIBCPP_TYPE_VIS deque;
414232950Stheraven    template <class _Vp, class _Pp, class _Rp, class _MP, class _Dp, _Dp>
415249998Sdim        friend class _LIBCPP_TYPE_VIS __deque_iterator;
416227825Stheraven
417227825Stheraven    template <class _RAIter,
418227825Stheraven              class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
419227825Stheraven    friend
420227825Stheraven    __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
421227825Stheraven    copy(_RAIter __f,
422227825Stheraven         _RAIter __l,
423227825Stheraven         __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
424227825Stheraven         typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
425227825Stheraven
426227825Stheraven    template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
427227825Stheraven              class _OutputIterator>
428227825Stheraven    friend
429227825Stheraven    _OutputIterator
430227825Stheraven    copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
431227825Stheraven         __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
432227825Stheraven         _OutputIterator __r);
433227825Stheraven
434227825Stheraven    template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
435227825Stheraven              class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
436227825Stheraven    friend
437227825Stheraven    __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
438227825Stheraven    copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
439227825Stheraven         __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
440227825Stheraven         __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
441227825Stheraven
442227825Stheraven    template <class _RAIter,
443227825Stheraven              class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
444227825Stheraven    friend
445227825Stheraven    __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
446227825Stheraven    copy_backward(_RAIter __f,
447227825Stheraven                  _RAIter __l,
448227825Stheraven                  __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
449227825Stheraven                  typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
450227825Stheraven
451227825Stheraven    template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
452227825Stheraven              class _OutputIterator>
453227825Stheraven    friend
454227825Stheraven    _OutputIterator
455227825Stheraven    copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
456227825Stheraven                  __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
457227825Stheraven                  _OutputIterator __r);
458227825Stheraven
459227825Stheraven    template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
460227825Stheraven              class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
461227825Stheraven    friend
462227825Stheraven    __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
463227825Stheraven    copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
464227825Stheraven                  __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
465227825Stheraven                  __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
466227825Stheraven
467227825Stheraven    template <class _RAIter,
468227825Stheraven              class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
469227825Stheraven    friend
470227825Stheraven    __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
471227825Stheraven    move(_RAIter __f,
472227825Stheraven         _RAIter __l,
473227825Stheraven         __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
474227825Stheraven         typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
475227825Stheraven
476227825Stheraven    template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
477227825Stheraven              class _OutputIterator>
478227825Stheraven    friend
479227825Stheraven    _OutputIterator
480227825Stheraven    move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
481227825Stheraven         __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
482227825Stheraven         _OutputIterator __r);
483227825Stheraven
484227825Stheraven    template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
485227825Stheraven              class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
486227825Stheraven    friend
487227825Stheraven    __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
488227825Stheraven    move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
489227825Stheraven         __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
490227825Stheraven         __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
491227825Stheraven
492227825Stheraven    template <class _RAIter,
493227825Stheraven              class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
494227825Stheraven    friend
495227825Stheraven    __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
496227825Stheraven    move_backward(_RAIter __f,
497227825Stheraven                  _RAIter __l,
498227825Stheraven                  __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
499227825Stheraven                  typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
500227825Stheraven
501227825Stheraven    template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
502227825Stheraven              class _OutputIterator>
503227825Stheraven    friend
504227825Stheraven    _OutputIterator
505227825Stheraven    move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
506227825Stheraven                  __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
507227825Stheraven                  _OutputIterator __r);
508227825Stheraven
509227825Stheraven    template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
510227825Stheraven              class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
511227825Stheraven    friend
512227825Stheraven    __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
513227825Stheraven    move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
514227825Stheraven                  __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
515227825Stheraven                  __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
516227825Stheraven};
517227825Stheraven
518227825Stheraven// copy
519227825Stheraven
520227825Stheraventemplate <class _RAIter,
521227825Stheraven          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
522227825Stheraven__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
523227825Stheravencopy(_RAIter __f,
524227825Stheraven     _RAIter __l,
525227825Stheraven     __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
526227825Stheraven     typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
527227825Stheraven{
528227825Stheraven    typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
529227825Stheraven    typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
530227825Stheraven    while (__f != __l)
531227825Stheraven    {
532227825Stheraven        pointer __rb = __r.__ptr_;
533227825Stheraven        pointer __re = *__r.__m_iter_ + _B2;
534227825Stheraven        difference_type __bs = __re - __rb;
535227825Stheraven        difference_type __n = __l - __f;
536227825Stheraven        _RAIter __m = __l;
537227825Stheraven        if (__n > __bs)
538227825Stheraven        {
539227825Stheraven            __n = __bs;
540227825Stheraven            __m = __f + __n;
541227825Stheraven        }
542227825Stheraven        _VSTD::copy(__f, __m, __rb);
543227825Stheraven        __f = __m;
544227825Stheraven        __r += __n;
545227825Stheraven    }
546227825Stheraven    return __r;
547227825Stheraven}
548227825Stheraven
549227825Stheraventemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
550227825Stheraven          class _OutputIterator>
551227825Stheraven_OutputIterator
552227825Stheravencopy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
553227825Stheraven     __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
554227825Stheraven     _OutputIterator __r)
555227825Stheraven{
556227825Stheraven    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
557227825Stheraven    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
558227825Stheraven    difference_type __n = __l - __f;
559227825Stheraven    while (__n > 0)
560227825Stheraven    {
561227825Stheraven        pointer __fb = __f.__ptr_;
562227825Stheraven        pointer __fe = *__f.__m_iter_ + _B1;
563227825Stheraven        difference_type __bs = __fe - __fb;
564227825Stheraven        if (__bs > __n)
565227825Stheraven        {
566227825Stheraven            __bs = __n;
567227825Stheraven            __fe = __fb + __bs;
568227825Stheraven        }
569227825Stheraven        __r = _VSTD::copy(__fb, __fe, __r);
570227825Stheraven        __n -= __bs;
571227825Stheraven        __f += __bs;
572227825Stheraven    }
573227825Stheraven    return __r;
574227825Stheraven}
575227825Stheraven
576227825Stheraventemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
577227825Stheraven          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
578227825Stheraven__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
579227825Stheravencopy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
580227825Stheraven     __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
581227825Stheraven     __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
582227825Stheraven{
583227825Stheraven    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
584227825Stheraven    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
585227825Stheraven    difference_type __n = __l - __f;
586227825Stheraven    while (__n > 0)
587227825Stheraven    {
588227825Stheraven        pointer __fb = __f.__ptr_;
589227825Stheraven        pointer __fe = *__f.__m_iter_ + _B1;
590227825Stheraven        difference_type __bs = __fe - __fb;
591227825Stheraven        if (__bs > __n)
592227825Stheraven        {
593227825Stheraven            __bs = __n;
594227825Stheraven            __fe = __fb + __bs;
595227825Stheraven        }
596227825Stheraven        __r = _VSTD::copy(__fb, __fe, __r);
597227825Stheraven        __n -= __bs;
598227825Stheraven        __f += __bs;
599227825Stheraven    }
600227825Stheraven    return __r;
601227825Stheraven}
602227825Stheraven
603227825Stheraven// copy_backward
604227825Stheraven
605227825Stheraventemplate <class _RAIter,
606227825Stheraven          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
607227825Stheraven__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
608227825Stheravencopy_backward(_RAIter __f,
609227825Stheraven              _RAIter __l,
610227825Stheraven              __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
611227825Stheraven              typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
612227825Stheraven{
613227825Stheraven    typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
614227825Stheraven    typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
615227825Stheraven    while (__f != __l)
616227825Stheraven    {
617227825Stheraven        __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _VSTD::prev(__r);
618227825Stheraven        pointer __rb = *__rp.__m_iter_;
619227825Stheraven        pointer __re = __rp.__ptr_ + 1;
620227825Stheraven        difference_type __bs = __re - __rb;
621227825Stheraven        difference_type __n = __l - __f;
622227825Stheraven        _RAIter __m = __f;
623227825Stheraven        if (__n > __bs)
624227825Stheraven        {
625227825Stheraven            __n = __bs;
626227825Stheraven            __m = __l - __n;
627227825Stheraven        }
628227825Stheraven        _VSTD::copy_backward(__m, __l, __re);
629227825Stheraven        __l = __m;
630227825Stheraven        __r -= __n;
631227825Stheraven    }
632227825Stheraven    return __r;
633227825Stheraven}
634227825Stheraven
635227825Stheraventemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
636227825Stheraven          class _OutputIterator>
637227825Stheraven_OutputIterator
638227825Stheravencopy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
639227825Stheraven              __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
640227825Stheraven              _OutputIterator __r)
641227825Stheraven{
642227825Stheraven    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
643227825Stheraven    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
644227825Stheraven    difference_type __n = __l - __f;
645227825Stheraven    while (__n > 0)
646227825Stheraven    {
647227825Stheraven        --__l;
648227825Stheraven        pointer __lb = *__l.__m_iter_;
649227825Stheraven        pointer __le = __l.__ptr_ + 1;
650227825Stheraven        difference_type __bs = __le - __lb;
651227825Stheraven        if (__bs > __n)
652227825Stheraven        {
653227825Stheraven            __bs = __n;
654227825Stheraven            __lb = __le - __bs;
655227825Stheraven        }
656227825Stheraven        __r = _VSTD::copy_backward(__lb, __le, __r);
657227825Stheraven        __n -= __bs;
658227825Stheraven        __l -= __bs - 1;
659227825Stheraven    }
660227825Stheraven    return __r;
661227825Stheraven}
662227825Stheraven
663227825Stheraventemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
664227825Stheraven          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
665227825Stheraven__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
666227825Stheravencopy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
667227825Stheraven              __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
668227825Stheraven              __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
669227825Stheraven{
670227825Stheraven    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
671227825Stheraven    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
672227825Stheraven    difference_type __n = __l - __f;
673227825Stheraven    while (__n > 0)
674227825Stheraven    {
675227825Stheraven        --__l;
676227825Stheraven        pointer __lb = *__l.__m_iter_;
677227825Stheraven        pointer __le = __l.__ptr_ + 1;
678227825Stheraven        difference_type __bs = __le - __lb;
679227825Stheraven        if (__bs > __n)
680227825Stheraven        {
681227825Stheraven            __bs = __n;
682227825Stheraven            __lb = __le - __bs;
683227825Stheraven        }
684227825Stheraven        __r = _VSTD::copy_backward(__lb, __le, __r);
685227825Stheraven        __n -= __bs;
686227825Stheraven        __l -= __bs - 1;
687227825Stheraven    }
688227825Stheraven    return __r;
689227825Stheraven}
690227825Stheraven
691227825Stheraven// move
692227825Stheraven
693227825Stheraventemplate <class _RAIter,
694227825Stheraven          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
695227825Stheraven__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
696227825Stheravenmove(_RAIter __f,
697227825Stheraven     _RAIter __l,
698227825Stheraven     __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
699227825Stheraven     typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
700227825Stheraven{
701227825Stheraven    typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
702227825Stheraven    typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
703227825Stheraven    while (__f != __l)
704227825Stheraven    {
705227825Stheraven        pointer __rb = __r.__ptr_;
706227825Stheraven        pointer __re = *__r.__m_iter_ + _B2;
707227825Stheraven        difference_type __bs = __re - __rb;
708227825Stheraven        difference_type __n = __l - __f;
709227825Stheraven        _RAIter __m = __l;
710227825Stheraven        if (__n > __bs)
711227825Stheraven        {
712227825Stheraven            __n = __bs;
713227825Stheraven            __m = __f + __n;
714227825Stheraven        }
715227825Stheraven        _VSTD::move(__f, __m, __rb);
716227825Stheraven        __f = __m;
717227825Stheraven        __r += __n;
718227825Stheraven    }
719227825Stheraven    return __r;
720227825Stheraven}
721227825Stheraven
722227825Stheraventemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
723227825Stheraven          class _OutputIterator>
724227825Stheraven_OutputIterator
725227825Stheravenmove(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
726227825Stheraven     __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
727227825Stheraven     _OutputIterator __r)
728227825Stheraven{
729227825Stheraven    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
730227825Stheraven    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
731227825Stheraven    difference_type __n = __l - __f;
732227825Stheraven    while (__n > 0)
733227825Stheraven    {
734227825Stheraven        pointer __fb = __f.__ptr_;
735227825Stheraven        pointer __fe = *__f.__m_iter_ + _B1;
736227825Stheraven        difference_type __bs = __fe - __fb;
737227825Stheraven        if (__bs > __n)
738227825Stheraven        {
739227825Stheraven            __bs = __n;
740227825Stheraven            __fe = __fb + __bs;
741227825Stheraven        }
742227825Stheraven        __r = _VSTD::move(__fb, __fe, __r);
743227825Stheraven        __n -= __bs;
744227825Stheraven        __f += __bs;
745227825Stheraven    }
746227825Stheraven    return __r;
747227825Stheraven}
748227825Stheraven
749227825Stheraventemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
750227825Stheraven          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
751227825Stheraven__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
752227825Stheravenmove(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
753227825Stheraven     __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
754227825Stheraven     __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
755227825Stheraven{
756227825Stheraven    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
757227825Stheraven    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
758227825Stheraven    difference_type __n = __l - __f;
759227825Stheraven    while (__n > 0)
760227825Stheraven    {
761227825Stheraven        pointer __fb = __f.__ptr_;
762227825Stheraven        pointer __fe = *__f.__m_iter_ + _B1;
763227825Stheraven        difference_type __bs = __fe - __fb;
764227825Stheraven        if (__bs > __n)
765227825Stheraven        {
766227825Stheraven            __bs = __n;
767227825Stheraven            __fe = __fb + __bs;
768227825Stheraven        }
769227825Stheraven        __r = _VSTD::move(__fb, __fe, __r);
770227825Stheraven        __n -= __bs;
771227825Stheraven        __f += __bs;
772227825Stheraven    }
773227825Stheraven    return __r;
774227825Stheraven}
775227825Stheraven
776227825Stheraven// move_backward
777227825Stheraven
778227825Stheraventemplate <class _RAIter,
779227825Stheraven          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
780227825Stheraven__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
781227825Stheravenmove_backward(_RAIter __f,
782227825Stheraven              _RAIter __l,
783227825Stheraven              __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
784227825Stheraven              typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
785227825Stheraven{
786227825Stheraven    typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
787227825Stheraven    typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
788227825Stheraven    while (__f != __l)
789227825Stheraven    {
790227825Stheraven        __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _VSTD::prev(__r);
791227825Stheraven        pointer __rb = *__rp.__m_iter_;
792227825Stheraven        pointer __re = __rp.__ptr_ + 1;
793227825Stheraven        difference_type __bs = __re - __rb;
794227825Stheraven        difference_type __n = __l - __f;
795227825Stheraven        _RAIter __m = __f;
796227825Stheraven        if (__n > __bs)
797227825Stheraven        {
798227825Stheraven            __n = __bs;
799227825Stheraven            __m = __l - __n;
800227825Stheraven        }
801227825Stheraven        _VSTD::move_backward(__m, __l, __re);
802227825Stheraven        __l = __m;
803227825Stheraven        __r -= __n;
804227825Stheraven    }
805227825Stheraven    return __r;
806227825Stheraven}
807227825Stheraven
808227825Stheraventemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
809227825Stheraven          class _OutputIterator>
810227825Stheraven_OutputIterator
811227825Stheravenmove_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
812227825Stheraven              __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
813227825Stheraven              _OutputIterator __r)
814227825Stheraven{
815227825Stheraven    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
816227825Stheraven    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
817227825Stheraven    difference_type __n = __l - __f;
818227825Stheraven    while (__n > 0)
819227825Stheraven    {
820227825Stheraven        --__l;
821227825Stheraven        pointer __lb = *__l.__m_iter_;
822227825Stheraven        pointer __le = __l.__ptr_ + 1;
823227825Stheraven        difference_type __bs = __le - __lb;
824227825Stheraven        if (__bs > __n)
825227825Stheraven        {
826227825Stheraven            __bs = __n;
827227825Stheraven            __lb = __le - __bs;
828227825Stheraven        }
829227825Stheraven        __r = _VSTD::move_backward(__lb, __le, __r);
830227825Stheraven        __n -= __bs;
831227825Stheraven        __l -= __bs - 1;
832227825Stheraven    }
833227825Stheraven    return __r;
834227825Stheraven}
835227825Stheraven
836227825Stheraventemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
837227825Stheraven          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
838227825Stheraven__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
839227825Stheravenmove_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
840227825Stheraven              __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
841227825Stheraven              __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
842227825Stheraven{
843227825Stheraven    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
844227825Stheraven    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
845227825Stheraven    difference_type __n = __l - __f;
846227825Stheraven    while (__n > 0)
847227825Stheraven    {
848227825Stheraven        --__l;
849227825Stheraven        pointer __lb = *__l.__m_iter_;
850227825Stheraven        pointer __le = __l.__ptr_ + 1;
851227825Stheraven        difference_type __bs = __le - __lb;
852227825Stheraven        if (__bs > __n)
853227825Stheraven        {
854227825Stheraven            __bs = __n;
855227825Stheraven            __lb = __le - __bs;
856227825Stheraven        }
857227825Stheraven        __r = _VSTD::move_backward(__lb, __le, __r);
858227825Stheraven        __n -= __bs;
859227825Stheraven        __l -= __bs - 1;
860227825Stheraven    }
861227825Stheraven    return __r;
862227825Stheraven}
863227825Stheraven
864227825Stheraventemplate <bool>
865227825Stheravenclass __deque_base_common
866227825Stheraven{
867227825Stheravenprotected:
868227825Stheraven    void __throw_length_error() const;
869227825Stheraven    void __throw_out_of_range() const;
870227825Stheraven};
871227825Stheraven
872227825Stheraventemplate <bool __b>
873227825Stheravenvoid
874227825Stheraven__deque_base_common<__b>::__throw_length_error() const
875227825Stheraven{
876227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
877227825Stheraven    throw length_error("deque");
878227825Stheraven#endif
879227825Stheraven}
880227825Stheraven
881227825Stheraventemplate <bool __b>
882227825Stheravenvoid
883227825Stheraven__deque_base_common<__b>::__throw_out_of_range() const
884227825Stheraven{
885227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
886227825Stheraven    throw out_of_range("deque");
887227825Stheraven#endif
888227825Stheraven}
889227825Stheraven
890227825Stheraventemplate <class _Tp, class _Allocator>
891227825Stheravenclass __deque_base
892227825Stheraven    : protected __deque_base_common<true>
893227825Stheraven{
894227825Stheraven    __deque_base(const __deque_base& __c);
895227825Stheraven    __deque_base& operator=(const __deque_base& __c);
896227825Stheravenprotected:
897227825Stheraven    typedef _Tp                                      value_type;
898227825Stheraven    typedef _Allocator                               allocator_type;
899227825Stheraven    typedef allocator_traits<allocator_type>         __alloc_traits;
900227825Stheraven    typedef value_type&                              reference;
901227825Stheraven    typedef const value_type&                        const_reference;
902227825Stheraven    typedef typename __alloc_traits::size_type       size_type;
903227825Stheraven    typedef typename __alloc_traits::difference_type difference_type;
904227825Stheraven    typedef typename __alloc_traits::pointer         pointer;
905227825Stheraven    typedef typename __alloc_traits::const_pointer   const_pointer;
906227825Stheraven
907227825Stheraven    static const difference_type __block_size = sizeof(value_type) < 256 ? 4096 / sizeof(value_type) : 16;
908227825Stheraven
909227825Stheraven    typedef typename __alloc_traits::template
910227825Stheraven#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
911227825Stheraven                rebind_alloc<pointer>
912227825Stheraven#else
913227825Stheraven                rebind_alloc<pointer>::other
914227825Stheraven#endif
915227825Stheraven                                                         __pointer_allocator;
916227825Stheraven    typedef allocator_traits<__pointer_allocator>        __map_traits;
917227825Stheraven    typedef typename __map_traits::pointer               __map_pointer;
918253159Stheraven    typedef typename __alloc_traits::template
919253159Stheraven#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
920253159Stheraven                rebind_alloc<const_pointer>
921253159Stheraven#else
922253159Stheraven                rebind_alloc<const_pointer>::other
923253159Stheraven#endif
924253159Stheraven                                                         __const_pointer_allocator;
925253159Stheraven    typedef typename allocator_traits<__const_pointer_allocator>::const_pointer __map_const_pointer;
926227825Stheraven    typedef __split_buffer<pointer, __pointer_allocator> __map;
927227825Stheraven
928227825Stheraven    typedef __deque_iterator<value_type, pointer, reference, __map_pointer,
929227825Stheraven                             difference_type, __block_size>    iterator;
930227825Stheraven    typedef __deque_iterator<value_type, const_pointer, const_reference, __map_const_pointer,
931227825Stheraven                             difference_type, __block_size>    const_iterator;
932227825Stheraven
933227825Stheraven    __map __map_;
934227825Stheraven    size_type __start_;
935227825Stheraven    __compressed_pair<size_type, allocator_type> __size_;
936227825Stheraven
937227825Stheraven    iterator       begin() _NOEXCEPT;
938227825Stheraven    const_iterator begin() const _NOEXCEPT;
939227825Stheraven    iterator       end() _NOEXCEPT;
940227825Stheraven    const_iterator end() const _NOEXCEPT;
941227825Stheraven
942227825Stheraven    _LIBCPP_INLINE_VISIBILITY size_type&            size()          {return __size_.first();}
943227825Stheraven    _LIBCPP_INLINE_VISIBILITY
944227825Stheraven    const size_type& size() const _NOEXCEPT {return __size_.first();}
945227825Stheraven    _LIBCPP_INLINE_VISIBILITY allocator_type&       __alloc()       {return __size_.second();}
946227825Stheraven    _LIBCPP_INLINE_VISIBILITY
947227825Stheraven    const allocator_type& __alloc() const _NOEXCEPT {return __size_.second();}
948227825Stheraven
949227825Stheraven    __deque_base()
950227825Stheraven        _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
951227825Stheraven    explicit __deque_base(const allocator_type& __a);
952227825Stheravenpublic:
953227825Stheraven    ~__deque_base();
954227825Stheraven
955227825Stheraven#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
956227825Stheraven
957227825Stheraven    __deque_base(__deque_base&& __c)
958227825Stheraven        _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
959227825Stheraven    __deque_base(__deque_base&& __c, const allocator_type& __a);
960227825Stheraven
961227825Stheraven#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
962227825Stheraven    void swap(__deque_base& __c)
963227825Stheraven        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
964227825Stheraven                   __is_nothrow_swappable<allocator_type>::value);
965227825Stheravenprotected:
966227825Stheraven    void clear() _NOEXCEPT;
967227825Stheraven
968227825Stheraven    bool __invariants() const;
969227825Stheraven
970227825Stheraven    _LIBCPP_INLINE_VISIBILITY
971227825Stheraven    void __move_assign(__deque_base& __c)
972227825Stheraven        _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
973227825Stheraven                   is_nothrow_move_assignable<allocator_type>::value)
974227825Stheraven    {
975227825Stheraven        __map_ = _VSTD::move(__c.__map_);
976227825Stheraven        __start_ = __c.__start_;
977227825Stheraven        size() = __c.size();
978227825Stheraven        __move_assign_alloc(__c);
979227825Stheraven        __c.__start_ = __c.size() = 0;
980227825Stheraven    }
981227825Stheraven
982227825Stheraven    _LIBCPP_INLINE_VISIBILITY
983227825Stheraven    void __move_assign_alloc(__deque_base& __c)
984227825Stheraven        _NOEXCEPT_(!__alloc_traits::propagate_on_container_move_assignment::value ||
985227825Stheraven                   is_nothrow_move_assignable<allocator_type>::value)
986227825Stheraven        {__move_assign_alloc(__c, integral_constant<bool,
987227825Stheraven                      __alloc_traits::propagate_on_container_move_assignment::value>());}
988227825Stheraven
989227825Stheravenprivate:
990227825Stheraven    _LIBCPP_INLINE_VISIBILITY
991227825Stheraven    void __move_assign_alloc(__deque_base& __c, true_type)
992227825Stheraven        _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
993227825Stheraven        {
994227825Stheraven            __alloc() = _VSTD::move(__c.__alloc());
995227825Stheraven        }
996227825Stheraven
997227825Stheraven    _LIBCPP_INLINE_VISIBILITY
998232950Stheraven    void __move_assign_alloc(__deque_base&, false_type) _NOEXCEPT
999227825Stheraven        {}
1000227825Stheraven
1001227825Stheraven    _LIBCPP_INLINE_VISIBILITY
1002227825Stheraven    static void __swap_alloc(allocator_type& __x, allocator_type& __y)
1003227825Stheraven        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1004227825Stheraven                   __is_nothrow_swappable<allocator_type>::value)
1005227825Stheraven        {__swap_alloc(__x, __y, integral_constant<bool,
1006227825Stheraven                      __alloc_traits::propagate_on_container_swap::value>());}
1007227825Stheraven
1008227825Stheraven    _LIBCPP_INLINE_VISIBILITY
1009227825Stheraven    static void __swap_alloc(allocator_type& __x, allocator_type& __y, true_type)
1010227825Stheraven        _NOEXCEPT_(__is_nothrow_swappable<allocator_type>::value)
1011227825Stheraven        {
1012227825Stheraven            using _VSTD::swap;
1013227825Stheraven            swap(__x, __y);
1014227825Stheraven        }
1015227825Stheraven
1016227825Stheraven    _LIBCPP_INLINE_VISIBILITY
1017232950Stheraven    static void __swap_alloc(allocator_type&, allocator_type&, false_type)
1018227825Stheraven        _NOEXCEPT
1019227825Stheraven        {}
1020227825Stheraven};
1021227825Stheraven
1022227825Stheraventemplate <class _Tp, class _Allocator>
1023227825Stheravenbool
1024227825Stheraven__deque_base<_Tp, _Allocator>::__invariants() const
1025227825Stheraven{
1026227825Stheraven    if (!__map_.__invariants())
1027227825Stheraven        return false;
1028227825Stheraven    if (__map_.size() >= size_type(-1) / __block_size)
1029227825Stheraven        return false;
1030227825Stheraven    for (typename __map::const_iterator __i = __map_.begin(), __e = __map_.end();
1031227825Stheraven         __i != __e; ++__i)
1032227825Stheraven        if (*__i == nullptr)
1033227825Stheraven            return false;
1034227825Stheraven    if (__map_.size() != 0)
1035227825Stheraven    {
1036227825Stheraven        if (size() >= __map_.size() * __block_size)
1037227825Stheraven            return false;
1038227825Stheraven        if (__start_ >= __map_.size() * __block_size - size())
1039227825Stheraven            return false;
1040227825Stheraven    }
1041227825Stheraven    else
1042227825Stheraven    {
1043227825Stheraven        if (size() != 0)
1044227825Stheraven            return false;
1045227825Stheraven        if (__start_ != 0)
1046227825Stheraven            return false;
1047227825Stheraven    }
1048227825Stheraven    return true;
1049227825Stheraven}
1050227825Stheraven
1051227825Stheraventemplate <class _Tp, class _Allocator>
1052227825Stheraventypename __deque_base<_Tp, _Allocator>::iterator
1053227825Stheraven__deque_base<_Tp, _Allocator>::begin() _NOEXCEPT
1054227825Stheraven{
1055227825Stheraven    __map_pointer __mp = __map_.begin() + __start_ / __block_size;
1056227825Stheraven    return iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
1057227825Stheraven}
1058227825Stheraven
1059227825Stheraventemplate <class _Tp, class _Allocator>
1060227825Stheraventypename __deque_base<_Tp, _Allocator>::const_iterator
1061227825Stheraven__deque_base<_Tp, _Allocator>::begin() const _NOEXCEPT
1062227825Stheraven{
1063253159Stheraven    __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __start_ / __block_size);
1064227825Stheraven    return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
1065227825Stheraven}
1066227825Stheraven
1067227825Stheraventemplate <class _Tp, class _Allocator>
1068227825Stheraventypename __deque_base<_Tp, _Allocator>::iterator
1069227825Stheraven__deque_base<_Tp, _Allocator>::end() _NOEXCEPT
1070227825Stheraven{
1071227825Stheraven    size_type __p = size() + __start_;
1072227825Stheraven    __map_pointer __mp = __map_.begin() + __p / __block_size;
1073227825Stheraven    return iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
1074227825Stheraven}
1075227825Stheraven
1076227825Stheraventemplate <class _Tp, class _Allocator>
1077227825Stheraventypename __deque_base<_Tp, _Allocator>::const_iterator
1078227825Stheraven__deque_base<_Tp, _Allocator>::end() const _NOEXCEPT
1079227825Stheraven{
1080227825Stheraven    size_type __p = size() + __start_;
1081253159Stheraven    __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __p / __block_size);
1082227825Stheraven    return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
1083227825Stheraven}
1084227825Stheraven
1085227825Stheraventemplate <class _Tp, class _Allocator>
1086227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1087227825Stheraven__deque_base<_Tp, _Allocator>::__deque_base()
1088227825Stheraven    _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
1089227825Stheraven    : __start_(0), __size_(0) {}
1090227825Stheraven
1091227825Stheraventemplate <class _Tp, class _Allocator>
1092227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1093227825Stheraven__deque_base<_Tp, _Allocator>::__deque_base(const allocator_type& __a)
1094227825Stheraven    : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a) {}
1095227825Stheraven
1096227825Stheraventemplate <class _Tp, class _Allocator>
1097227825Stheraven__deque_base<_Tp, _Allocator>::~__deque_base()
1098227825Stheraven{
1099227825Stheraven    clear();
1100227825Stheraven    typename __map::iterator __i = __map_.begin();
1101227825Stheraven    typename __map::iterator __e = __map_.end();
1102227825Stheraven    for (; __i != __e; ++__i)
1103227825Stheraven        __alloc_traits::deallocate(__alloc(), *__i, __block_size);
1104227825Stheraven}
1105227825Stheraven
1106227825Stheraven#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1107227825Stheraven
1108227825Stheraventemplate <class _Tp, class _Allocator>
1109227825Stheraven__deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c)
1110227825Stheraven    _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
1111227825Stheraven    : __map_(_VSTD::move(__c.__map_)),
1112227825Stheraven      __start_(_VSTD::move(__c.__start_)),
1113227825Stheraven      __size_(_VSTD::move(__c.__size_))
1114227825Stheraven{
1115227825Stheraven    __c.__start_ = 0;
1116227825Stheraven    __c.size() = 0;
1117227825Stheraven}
1118227825Stheraven
1119227825Stheraventemplate <class _Tp, class _Allocator>
1120227825Stheraven__deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c, const allocator_type& __a)
1121227825Stheraven    : __map_(_VSTD::move(__c.__map_), __pointer_allocator(__a)),
1122227825Stheraven      __start_(_VSTD::move(__c.__start_)),
1123227825Stheraven      __size_(_VSTD::move(__c.size()), __a)
1124227825Stheraven{
1125227825Stheraven    if (__a == __c.__alloc())
1126227825Stheraven    {
1127227825Stheraven        __c.__start_ = 0;
1128227825Stheraven        __c.size() = 0;
1129227825Stheraven    }
1130227825Stheraven    else
1131227825Stheraven    {
1132227825Stheraven        __map_.clear();
1133227825Stheraven        __start_ = 0;
1134227825Stheraven        size() = 0;
1135227825Stheraven    }
1136227825Stheraven}
1137227825Stheraven
1138227825Stheraven#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1139227825Stheraven
1140227825Stheraventemplate <class _Tp, class _Allocator>
1141227825Stheravenvoid
1142227825Stheraven__deque_base<_Tp, _Allocator>::swap(__deque_base& __c)
1143227825Stheraven        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value||
1144227825Stheraven                   __is_nothrow_swappable<allocator_type>::value)
1145227825Stheraven{
1146227825Stheraven    __map_.swap(__c.__map_);
1147227825Stheraven    _VSTD::swap(__start_, __c.__start_);
1148227825Stheraven    _VSTD::swap(size(), __c.size());
1149227825Stheraven    __swap_alloc(__alloc(), __c.__alloc());
1150227825Stheraven}
1151227825Stheraven
1152227825Stheraventemplate <class _Tp, class _Allocator>
1153227825Stheravenvoid
1154227825Stheraven__deque_base<_Tp, _Allocator>::clear() _NOEXCEPT
1155227825Stheraven{
1156227825Stheraven    allocator_type& __a = __alloc();
1157227825Stheraven    for (iterator __i = begin(), __e = end(); __i != __e; ++__i)
1158227825Stheraven        __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
1159227825Stheraven    size() = 0;
1160227825Stheraven    while (__map_.size() > 2)
1161227825Stheraven    {
1162227825Stheraven        __alloc_traits::deallocate(__a, __map_.front(), __block_size);
1163227825Stheraven        __map_.pop_front();
1164227825Stheraven    }
1165227825Stheraven    switch (__map_.size())
1166227825Stheraven    {
1167227825Stheraven    case 1:
1168227825Stheraven        __start_ = __block_size / 2;
1169227825Stheraven        break;
1170227825Stheraven    case 2:
1171227825Stheraven        __start_ = __block_size;
1172227825Stheraven        break;
1173227825Stheraven    }
1174227825Stheraven}
1175227825Stheraven
1176227825Stheraventemplate <class _Tp, class _Allocator = allocator<_Tp> >
1177249998Sdimclass _LIBCPP_TYPE_VIS deque
1178227825Stheraven    : private __deque_base<_Tp, _Allocator>
1179227825Stheraven{
1180227825Stheravenpublic:
1181227825Stheraven    // types:
1182227825Stheraven
1183227825Stheraven    typedef _Tp value_type;
1184227825Stheraven    typedef _Allocator allocator_type;
1185227825Stheraven
1186227825Stheraven    typedef __deque_base<value_type, allocator_type> __base;
1187227825Stheraven
1188227825Stheraven    typedef typename __base::__alloc_traits        __alloc_traits;
1189227825Stheraven    typedef typename __base::reference             reference;
1190227825Stheraven    typedef typename __base::const_reference       const_reference;
1191227825Stheraven    typedef typename __base::iterator              iterator;
1192227825Stheraven    typedef typename __base::const_iterator        const_iterator;
1193227825Stheraven    typedef typename __base::size_type             size_type;
1194227825Stheraven    typedef typename __base::difference_type       difference_type;
1195227825Stheraven
1196227825Stheraven    typedef typename __base::pointer               pointer;
1197227825Stheraven    typedef typename __base::const_pointer         const_pointer;
1198227825Stheraven    typedef _VSTD::reverse_iterator<iterator>       reverse_iterator;
1199227825Stheraven    typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
1200227825Stheraven
1201227825Stheraven    // construct/copy/destroy:
1202227825Stheraven    _LIBCPP_INLINE_VISIBILITY
1203227825Stheraven    deque()
1204227825Stheraven        _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
1205227825Stheraven        {}
1206227825Stheraven    _LIBCPP_INLINE_VISIBILITY deque(const allocator_type& __a) : __base(__a) {}
1207227825Stheraven    explicit deque(size_type __n);
1208227825Stheraven    deque(size_type __n, const value_type& __v);
1209227825Stheraven    deque(size_type __n, const value_type& __v, const allocator_type& __a);
1210227825Stheraven    template <class _InputIter>
1211227825Stheraven        deque(_InputIter __f, _InputIter __l,
1212227825Stheraven              typename enable_if<__is_input_iterator<_InputIter>::value>::type* = 0);
1213227825Stheraven    template <class _InputIter>
1214227825Stheraven        deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
1215227825Stheraven              typename enable_if<__is_input_iterator<_InputIter>::value>::type* = 0);
1216227825Stheraven    deque(const deque& __c);
1217227825Stheraven    deque(const deque& __c, const allocator_type& __a);
1218227825Stheraven#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1219227825Stheraven    deque(initializer_list<value_type> __il);
1220227825Stheraven    deque(initializer_list<value_type> __il, const allocator_type& __a);
1221227825Stheraven#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1222227825Stheraven
1223227825Stheraven    deque& operator=(const deque& __c);
1224227825Stheraven#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1225227825Stheraven    _LIBCPP_INLINE_VISIBILITY
1226227825Stheraven    deque& operator=(initializer_list<value_type> __il) {assign(__il); return *this;}
1227227825Stheraven#endif   // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1228227825Stheraven
1229227825Stheraven#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1230227825Stheraven    deque(deque&& __c) _NOEXCEPT_(is_nothrow_move_constructible<__base>::value);
1231227825Stheraven    deque(deque&& __c, const allocator_type& __a);
1232227825Stheraven    deque& operator=(deque&& __c)
1233227825Stheraven        _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1234227825Stheraven                   is_nothrow_move_assignable<allocator_type>::value);
1235227825Stheraven#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1236227825Stheraven
1237227825Stheraven    template <class _InputIter>
1238227825Stheraven        void assign(_InputIter __f, _InputIter __l,
1239227825Stheraven                    typename enable_if<__is_input_iterator<_InputIter>::value &&
1240227825Stheraven                                      !__is_random_access_iterator<_InputIter>::value>::type* = 0);
1241227825Stheraven    template <class _RAIter>
1242227825Stheraven        void assign(_RAIter __f, _RAIter __l,
1243227825Stheraven                    typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
1244227825Stheraven    void assign(size_type __n, const value_type& __v);
1245227825Stheraven#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1246227825Stheraven    _LIBCPP_INLINE_VISIBILITY
1247227825Stheraven    void assign(initializer_list<value_type> __il) {assign(__il.begin(), __il.end());}
1248227825Stheraven#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1249227825Stheraven
1250227825Stheraven    allocator_type get_allocator() const _NOEXCEPT;
1251227825Stheraven
1252227825Stheraven    // iterators:
1253227825Stheraven
1254227825Stheraven    _LIBCPP_INLINE_VISIBILITY
1255227825Stheraven    iterator       begin() _NOEXCEPT       {return __base::begin();}
1256227825Stheraven    _LIBCPP_INLINE_VISIBILITY
1257227825Stheraven    const_iterator begin() const _NOEXCEPT {return __base::begin();}
1258227825Stheraven    _LIBCPP_INLINE_VISIBILITY
1259227825Stheraven    iterator       end() _NOEXCEPT         {return __base::end();}
1260227825Stheraven    _LIBCPP_INLINE_VISIBILITY
1261227825Stheraven    const_iterator end()   const _NOEXCEPT {return __base::end();}
1262227825Stheraven
1263227825Stheraven    _LIBCPP_INLINE_VISIBILITY
1264227825Stheraven    reverse_iterator       rbegin() _NOEXCEPT
1265227825Stheraven        {return       reverse_iterator(__base::end());}
1266227825Stheraven    _LIBCPP_INLINE_VISIBILITY
1267227825Stheraven    const_reverse_iterator rbegin() const _NOEXCEPT
1268227825Stheraven        {return const_reverse_iterator(__base::end());}
1269227825Stheraven    _LIBCPP_INLINE_VISIBILITY
1270227825Stheraven    reverse_iterator       rend() _NOEXCEPT
1271227825Stheraven        {return       reverse_iterator(__base::begin());}
1272227825Stheraven    _LIBCPP_INLINE_VISIBILITY
1273227825Stheraven    const_reverse_iterator rend()   const _NOEXCEPT
1274227825Stheraven        {return const_reverse_iterator(__base::begin());}
1275227825Stheraven
1276227825Stheraven    _LIBCPP_INLINE_VISIBILITY
1277227825Stheraven    const_iterator         cbegin()  const _NOEXCEPT
1278227825Stheraven        {return __base::begin();}
1279227825Stheraven    _LIBCPP_INLINE_VISIBILITY
1280227825Stheraven    const_iterator         cend()    const _NOEXCEPT
1281227825Stheraven        {return __base::end();}
1282227825Stheraven    _LIBCPP_INLINE_VISIBILITY
1283227825Stheraven    const_reverse_iterator crbegin() const _NOEXCEPT
1284227825Stheraven        {return const_reverse_iterator(__base::end());}
1285227825Stheraven    _LIBCPP_INLINE_VISIBILITY
1286227825Stheraven    const_reverse_iterator crend()   const _NOEXCEPT
1287227825Stheraven        {return const_reverse_iterator(__base::begin());}
1288227825Stheraven
1289227825Stheraven    // capacity:
1290227825Stheraven    _LIBCPP_INLINE_VISIBILITY
1291227825Stheraven    size_type size() const _NOEXCEPT {return __base::size();}
1292227825Stheraven    _LIBCPP_INLINE_VISIBILITY
1293227825Stheraven    size_type max_size() const _NOEXCEPT
1294227825Stheraven        {return __alloc_traits::max_size(__base::__alloc());}
1295227825Stheraven    void resize(size_type __n);
1296227825Stheraven    void resize(size_type __n, const value_type& __v);
1297227825Stheraven    void shrink_to_fit() _NOEXCEPT;
1298227825Stheraven    _LIBCPP_INLINE_VISIBILITY
1299227825Stheraven    bool empty() const _NOEXCEPT {return __base::size() == 0;}
1300227825Stheraven
1301227825Stheraven    // element access:
1302227825Stheraven    reference operator[](size_type __i);
1303227825Stheraven    const_reference operator[](size_type __i) const;
1304227825Stheraven    reference at(size_type __i);
1305227825Stheraven    const_reference at(size_type __i) const;
1306227825Stheraven    reference front();
1307227825Stheraven    const_reference front() const;
1308227825Stheraven    reference back();
1309227825Stheraven    const_reference back() const;
1310227825Stheraven
1311227825Stheraven    // 23.2.2.3 modifiers:
1312227825Stheraven    void push_front(const value_type& __v);
1313227825Stheraven    void push_back(const value_type& __v);
1314227825Stheraven#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1315227825Stheraven#ifndef _LIBCPP_HAS_NO_VARIADICS
1316227825Stheraven    template <class... _Args> void emplace_front(_Args&&... __args);
1317227825Stheraven    template <class... _Args> void emplace_back(_Args&&... __args);
1318227825Stheraven    template <class... _Args> iterator emplace(const_iterator __p, _Args&&... __args);
1319227825Stheraven#endif  // _LIBCPP_HAS_NO_VARIADICS
1320227825Stheraven    void push_front(value_type&& __v);
1321227825Stheraven    void push_back(value_type&& __v);
1322227825Stheraven    iterator insert(const_iterator __p, value_type&& __v);
1323227825Stheraven#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1324227825Stheraven    iterator insert(const_iterator __p, const value_type& __v);
1325227825Stheraven    iterator insert(const_iterator __p, size_type __n, const value_type& __v);
1326227825Stheraven    template <class _InputIter>
1327227825Stheraven        iterator insert (const_iterator __p, _InputIter __f, _InputIter __l,
1328227825Stheraven                         typename enable_if<__is_input_iterator<_InputIter>::value
1329227825Stheraven                                         &&!__is_bidirectional_iterator<_InputIter>::value>::type* = 0);
1330227825Stheraven    template <class _BiIter>
1331227825Stheraven        iterator insert (const_iterator __p, _BiIter __f, _BiIter __l,
1332227825Stheraven                         typename enable_if<__is_bidirectional_iterator<_BiIter>::value>::type* = 0);
1333227825Stheraven#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1334227825Stheraven    _LIBCPP_INLINE_VISIBILITY
1335227825Stheraven    iterator insert(const_iterator __p, initializer_list<value_type> __il)
1336227825Stheraven        {return insert(__p, __il.begin(), __il.end());}
1337227825Stheraven#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1338227825Stheraven    void pop_front();
1339227825Stheraven    void pop_back();
1340227825Stheraven    iterator erase(const_iterator __p);
1341227825Stheraven    iterator erase(const_iterator __f, const_iterator __l);
1342227825Stheraven
1343227825Stheraven    void swap(deque& __c)
1344227825Stheraven        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1345227825Stheraven                   __is_nothrow_swappable<allocator_type>::value);
1346227825Stheraven    void clear() _NOEXCEPT;
1347227825Stheraven
1348227825Stheraven    _LIBCPP_INLINE_VISIBILITY
1349227825Stheraven    bool __invariants() const {return __base::__invariants();}
1350227825Stheravenprivate:
1351253159Stheraven    typedef typename __base::__map_const_pointer __map_const_pointer;
1352253159Stheraven
1353227825Stheraven    _LIBCPP_INLINE_VISIBILITY
1354227825Stheraven    static size_type __recommend_blocks(size_type __n)
1355227825Stheraven    {
1356227825Stheraven        return __n / __base::__block_size + (__n % __base::__block_size != 0);
1357227825Stheraven    }
1358227825Stheraven    _LIBCPP_INLINE_VISIBILITY
1359227825Stheraven    size_type __capacity() const
1360227825Stheraven    {
1361227825Stheraven        return __base::__map_.size() == 0 ? 0 : __base::__map_.size() * __base::__block_size - 1;
1362227825Stheraven    }
1363227825Stheraven    _LIBCPP_INLINE_VISIBILITY
1364227825Stheraven    size_type __front_spare() const
1365227825Stheraven    {
1366227825Stheraven        return __base::__start_;
1367227825Stheraven    }
1368227825Stheraven    _LIBCPP_INLINE_VISIBILITY
1369227825Stheraven    size_type __back_spare() const
1370227825Stheraven    {
1371227825Stheraven        return __capacity() - (__base::__start_ + __base::size());
1372227825Stheraven    }
1373227825Stheraven
1374227825Stheraven    template <class _InpIter>
1375227825Stheraven        void __append(_InpIter __f, _InpIter __l,
1376227825Stheraven                 typename enable_if<__is_input_iterator<_InpIter>::value &&
1377227825Stheraven                                   !__is_forward_iterator<_InpIter>::value>::type* = 0);
1378227825Stheraven    template <class _ForIter>
1379227825Stheraven        void __append(_ForIter __f, _ForIter __l,
1380227825Stheraven                      typename enable_if<__is_forward_iterator<_ForIter>::value>::type* = 0);
1381227825Stheraven    void __append(size_type __n);
1382227825Stheraven    void __append(size_type __n, const value_type& __v);
1383227825Stheraven    void __erase_to_end(const_iterator __f);
1384227825Stheraven    void __add_front_capacity();
1385227825Stheraven    void __add_front_capacity(size_type __n);
1386227825Stheraven    void __add_back_capacity();
1387227825Stheraven    void __add_back_capacity(size_type __n);
1388227825Stheraven    iterator __move_and_check(iterator __f, iterator __l, iterator __r,
1389227825Stheraven                              const_pointer& __vt);
1390227825Stheraven    iterator __move_backward_and_check(iterator __f, iterator __l, iterator __r,
1391227825Stheraven                                       const_pointer& __vt);
1392227825Stheraven    void __move_construct_and_check(iterator __f, iterator __l,
1393227825Stheraven                                    iterator __r, const_pointer& __vt);
1394227825Stheraven    void __move_construct_backward_and_check(iterator __f, iterator __l,
1395227825Stheraven                                             iterator __r, const_pointer& __vt);
1396227825Stheraven
1397227825Stheraven    _LIBCPP_INLINE_VISIBILITY
1398227825Stheraven    void __copy_assign_alloc(const deque& __c)
1399227825Stheraven        {__copy_assign_alloc(__c, integral_constant<bool,
1400227825Stheraven                      __alloc_traits::propagate_on_container_copy_assignment::value>());}
1401227825Stheraven
1402227825Stheraven    _LIBCPP_INLINE_VISIBILITY
1403227825Stheraven    void __copy_assign_alloc(const deque& __c, true_type)
1404227825Stheraven        {
1405227825Stheraven            if (__base::__alloc() != __c.__alloc())
1406227825Stheraven            {
1407227825Stheraven                clear();
1408227825Stheraven                shrink_to_fit();
1409227825Stheraven            }
1410227825Stheraven            __base::__alloc() = __c.__alloc();
1411227825Stheraven            __base::__map_.__alloc() = __c.__map_.__alloc();
1412227825Stheraven        }
1413227825Stheraven
1414227825Stheraven    _LIBCPP_INLINE_VISIBILITY
1415232950Stheraven    void __copy_assign_alloc(const deque&, false_type)
1416227825Stheraven        {}
1417227825Stheraven
1418227825Stheraven    void __move_assign(deque& __c, true_type)
1419227825Stheraven        _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
1420227825Stheraven    void __move_assign(deque& __c, false_type);
1421227825Stheraven};
1422227825Stheraven
1423227825Stheraventemplate <class _Tp, class _Allocator>
1424227825Stheravendeque<_Tp, _Allocator>::deque(size_type __n)
1425227825Stheraven{
1426227825Stheraven    if (__n > 0)
1427227825Stheraven        __append(__n);
1428227825Stheraven}
1429227825Stheraven
1430227825Stheraventemplate <class _Tp, class _Allocator>
1431227825Stheravendeque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v)
1432227825Stheraven{
1433227825Stheraven    if (__n > 0)
1434227825Stheraven        __append(__n, __v);
1435227825Stheraven}
1436227825Stheraven
1437227825Stheraventemplate <class _Tp, class _Allocator>
1438227825Stheravendeque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v, const allocator_type& __a)
1439227825Stheraven    : __base(__a)
1440227825Stheraven{
1441227825Stheraven    if (__n > 0)
1442227825Stheraven        __append(__n, __v);
1443227825Stheraven}
1444227825Stheraven
1445227825Stheraventemplate <class _Tp, class _Allocator>
1446227825Stheraventemplate <class _InputIter>
1447227825Stheravendeque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l,
1448227825Stheraven              typename enable_if<__is_input_iterator<_InputIter>::value>::type*)
1449227825Stheraven{
1450227825Stheraven    __append(__f, __l);
1451227825Stheraven}
1452227825Stheraven
1453227825Stheraventemplate <class _Tp, class _Allocator>
1454227825Stheraventemplate <class _InputIter>
1455227825Stheravendeque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
1456227825Stheraven              typename enable_if<__is_input_iterator<_InputIter>::value>::type*)
1457227825Stheraven    : __base(__a)
1458227825Stheraven{
1459227825Stheraven    __append(__f, __l);
1460227825Stheraven}
1461227825Stheraven
1462227825Stheraventemplate <class _Tp, class _Allocator>
1463227825Stheravendeque<_Tp, _Allocator>::deque(const deque& __c)
1464227825Stheraven    : __base(__alloc_traits::select_on_container_copy_construction(__c.__alloc()))
1465227825Stheraven{
1466227825Stheraven    __append(__c.begin(), __c.end());
1467227825Stheraven}
1468227825Stheraven
1469227825Stheraventemplate <class _Tp, class _Allocator>
1470227825Stheravendeque<_Tp, _Allocator>::deque(const deque& __c, const allocator_type& __a)
1471227825Stheraven    : __base(__a)
1472227825Stheraven{
1473227825Stheraven    __append(__c.begin(), __c.end());
1474227825Stheraven}
1475227825Stheraven
1476227825Stheraven#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1477227825Stheraven
1478227825Stheraventemplate <class _Tp, class _Allocator>
1479227825Stheravendeque<_Tp, _Allocator>::deque(initializer_list<value_type> __il)
1480227825Stheraven{
1481227825Stheraven    __append(__il.begin(), __il.end());
1482227825Stheraven}
1483227825Stheraven
1484227825Stheraventemplate <class _Tp, class _Allocator>
1485227825Stheravendeque<_Tp, _Allocator>::deque(initializer_list<value_type> __il, const allocator_type& __a)
1486227825Stheraven    : __base(__a)
1487227825Stheraven{
1488227825Stheraven    __append(__il.begin(), __il.end());
1489227825Stheraven}
1490227825Stheraven
1491227825Stheraven#endif  // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1492227825Stheraven
1493227825Stheraventemplate <class _Tp, class _Allocator>
1494227825Stheravendeque<_Tp, _Allocator>&
1495227825Stheravendeque<_Tp, _Allocator>::operator=(const deque& __c)
1496227825Stheraven{
1497227825Stheraven    if (this != &__c)
1498227825Stheraven    {
1499227825Stheraven        __copy_assign_alloc(__c);
1500227825Stheraven        assign(__c.begin(), __c.end());
1501227825Stheraven    }
1502227825Stheraven    return *this;
1503227825Stheraven}
1504227825Stheraven
1505227825Stheraven#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1506227825Stheraven
1507227825Stheraventemplate <class _Tp, class _Allocator>
1508227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1509227825Stheravendeque<_Tp, _Allocator>::deque(deque&& __c)
1510227825Stheraven    _NOEXCEPT_(is_nothrow_move_constructible<__base>::value)
1511227825Stheraven    : __base(_VSTD::move(__c))
1512227825Stheraven{
1513227825Stheraven}
1514227825Stheraven
1515227825Stheraventemplate <class _Tp, class _Allocator>
1516227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1517227825Stheravendeque<_Tp, _Allocator>::deque(deque&& __c, const allocator_type& __a)
1518227825Stheraven    : __base(_VSTD::move(__c), __a)
1519227825Stheraven{
1520227825Stheraven    if (__a != __c.__alloc())
1521227825Stheraven    {
1522232950Stheraven        typedef move_iterator<iterator> _Ip;
1523232950Stheraven        assign(_Ip(__c.begin()), _Ip(__c.end()));
1524227825Stheraven    }
1525227825Stheraven}
1526227825Stheraven
1527227825Stheraventemplate <class _Tp, class _Allocator>
1528227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1529227825Stheravendeque<_Tp, _Allocator>&
1530227825Stheravendeque<_Tp, _Allocator>::operator=(deque&& __c)
1531227825Stheraven        _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1532227825Stheraven                   is_nothrow_move_assignable<allocator_type>::value)
1533227825Stheraven{
1534227825Stheraven    __move_assign(__c, integral_constant<bool,
1535227825Stheraven          __alloc_traits::propagate_on_container_move_assignment::value>());
1536227825Stheraven    return *this;
1537227825Stheraven}
1538227825Stheraven
1539227825Stheraventemplate <class _Tp, class _Allocator>
1540227825Stheravenvoid
1541227825Stheravendeque<_Tp, _Allocator>::__move_assign(deque& __c, false_type)
1542227825Stheraven{
1543227825Stheraven    if (__base::__alloc() != __c.__alloc())
1544227825Stheraven    {
1545232950Stheraven        typedef move_iterator<iterator> _Ip;
1546232950Stheraven        assign(_Ip(__c.begin()), _Ip(__c.end()));
1547227825Stheraven    }
1548227825Stheraven    else
1549227825Stheraven        __move_assign(__c, true_type());
1550227825Stheraven}
1551227825Stheraven
1552227825Stheraventemplate <class _Tp, class _Allocator>
1553227825Stheravenvoid
1554227825Stheravendeque<_Tp, _Allocator>::__move_assign(deque& __c, true_type)
1555227825Stheraven    _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1556227825Stheraven{
1557227825Stheraven    clear();
1558227825Stheraven    shrink_to_fit();
1559227825Stheraven    __base::__move_assign(__c);
1560227825Stheraven}
1561227825Stheraven
1562227825Stheraven#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1563227825Stheraven
1564227825Stheraventemplate <class _Tp, class _Allocator>
1565227825Stheraventemplate <class _InputIter>
1566227825Stheravenvoid
1567227825Stheravendeque<_Tp, _Allocator>::assign(_InputIter __f, _InputIter __l,
1568227825Stheraven                               typename enable_if<__is_input_iterator<_InputIter>::value &&
1569227825Stheraven                                                 !__is_random_access_iterator<_InputIter>::value>::type*)
1570227825Stheraven{
1571227825Stheraven    iterator __i = __base::begin();
1572227825Stheraven    iterator __e = __base::end();
1573227825Stheraven    for (; __f != __l && __i != __e; ++__f, ++__i)
1574227825Stheraven        *__i = *__f;
1575227825Stheraven    if (__f != __l)
1576227825Stheraven        __append(__f, __l);
1577227825Stheraven    else
1578227825Stheraven        __erase_to_end(__i);
1579227825Stheraven}
1580227825Stheraven
1581227825Stheraventemplate <class _Tp, class _Allocator>
1582227825Stheraventemplate <class _RAIter>
1583227825Stheravenvoid
1584227825Stheravendeque<_Tp, _Allocator>::assign(_RAIter __f, _RAIter __l,
1585227825Stheraven                               typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
1586227825Stheraven{
1587227825Stheraven    if (static_cast<size_type>(__l - __f) > __base::size())
1588227825Stheraven    {
1589227825Stheraven        _RAIter __m = __f + __base::size();
1590227825Stheraven        _VSTD::copy(__f, __m, __base::begin());
1591227825Stheraven        __append(__m, __l);
1592227825Stheraven    }
1593227825Stheraven    else
1594227825Stheraven        __erase_to_end(_VSTD::copy(__f, __l, __base::begin()));
1595227825Stheraven}
1596227825Stheraven
1597227825Stheraventemplate <class _Tp, class _Allocator>
1598227825Stheravenvoid
1599227825Stheravendeque<_Tp, _Allocator>::assign(size_type __n, const value_type& __v)
1600227825Stheraven{
1601227825Stheraven    if (__n > __base::size())
1602227825Stheraven    {
1603227825Stheraven        _VSTD::fill_n(__base::begin(), __base::size(), __v);
1604227825Stheraven        __n -= __base::size();
1605227825Stheraven        __append(__n, __v);
1606227825Stheraven    }
1607227825Stheraven    else
1608227825Stheraven        __erase_to_end(_VSTD::fill_n(__base::begin(), __n, __v));
1609227825Stheraven}
1610227825Stheraven
1611227825Stheraventemplate <class _Tp, class _Allocator>
1612227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1613227825Stheraven_Allocator
1614227825Stheravendeque<_Tp, _Allocator>::get_allocator() const _NOEXCEPT
1615227825Stheraven{
1616227825Stheraven    return __base::__alloc();
1617227825Stheraven}
1618227825Stheraven
1619227825Stheraventemplate <class _Tp, class _Allocator>
1620227825Stheravenvoid
1621227825Stheravendeque<_Tp, _Allocator>::resize(size_type __n)
1622227825Stheraven{
1623227825Stheraven    if (__n > __base::size())
1624227825Stheraven        __append(__n - __base::size());
1625227825Stheraven    else if (__n < __base::size())
1626227825Stheraven        __erase_to_end(__base::begin() + __n);
1627227825Stheraven}
1628227825Stheraven
1629227825Stheraventemplate <class _Tp, class _Allocator>
1630227825Stheravenvoid
1631227825Stheravendeque<_Tp, _Allocator>::resize(size_type __n, const value_type& __v)
1632227825Stheraven{
1633227825Stheraven    if (__n > __base::size())
1634227825Stheraven        __append(__n - __base::size(), __v);
1635227825Stheraven    else if (__n < __base::size())
1636227825Stheraven        __erase_to_end(__base::begin() + __n);
1637227825Stheraven}
1638227825Stheraven
1639227825Stheraventemplate <class _Tp, class _Allocator>
1640227825Stheravenvoid
1641227825Stheravendeque<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
1642227825Stheraven{
1643227825Stheraven    allocator_type& __a = __base::__alloc();
1644227825Stheraven    if (empty())
1645227825Stheraven    {
1646227825Stheraven        while (__base::__map_.size() > 0)
1647227825Stheraven        {
1648227825Stheraven            __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
1649227825Stheraven            __base::__map_.pop_back();
1650227825Stheraven        }
1651227825Stheraven        __base::__start_ = 0;
1652227825Stheraven    }
1653227825Stheraven    else
1654227825Stheraven    {
1655227825Stheraven        if (__front_spare() >= __base::__block_size)
1656227825Stheraven        {
1657227825Stheraven            __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
1658227825Stheraven            __base::__map_.pop_front();
1659227825Stheraven            __base::__start_ -= __base::__block_size;
1660227825Stheraven        }
1661227825Stheraven        if (__back_spare() >= __base::__block_size)
1662227825Stheraven        {
1663227825Stheraven            __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
1664227825Stheraven            __base::__map_.pop_back();
1665227825Stheraven        }
1666227825Stheraven    }
1667227825Stheraven    __base::__map_.shrink_to_fit();
1668227825Stheraven}
1669227825Stheraven
1670227825Stheraventemplate <class _Tp, class _Allocator>
1671227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1672227825Stheraventypename deque<_Tp, _Allocator>::reference
1673227825Stheravendeque<_Tp, _Allocator>::operator[](size_type __i)
1674227825Stheraven{
1675227825Stheraven    size_type __p = __base::__start_ + __i;
1676227825Stheraven    return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1677227825Stheraven}
1678227825Stheraven
1679227825Stheraventemplate <class _Tp, class _Allocator>
1680227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1681227825Stheraventypename deque<_Tp, _Allocator>::const_reference
1682227825Stheravendeque<_Tp, _Allocator>::operator[](size_type __i) const
1683227825Stheraven{
1684227825Stheraven    size_type __p = __base::__start_ + __i;
1685227825Stheraven    return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1686227825Stheraven}
1687227825Stheraven
1688227825Stheraventemplate <class _Tp, class _Allocator>
1689227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1690227825Stheraventypename deque<_Tp, _Allocator>::reference
1691227825Stheravendeque<_Tp, _Allocator>::at(size_type __i)
1692227825Stheraven{
1693227825Stheraven    if (__i >= __base::size())
1694227825Stheraven        __base::__throw_out_of_range();
1695227825Stheraven    size_type __p = __base::__start_ + __i;
1696227825Stheraven    return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1697227825Stheraven}
1698227825Stheraven
1699227825Stheraventemplate <class _Tp, class _Allocator>
1700227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1701227825Stheraventypename deque<_Tp, _Allocator>::const_reference
1702227825Stheravendeque<_Tp, _Allocator>::at(size_type __i) const
1703227825Stheraven{
1704227825Stheraven    if (__i >= __base::size())
1705227825Stheraven        __base::__throw_out_of_range();
1706227825Stheraven    size_type __p = __base::__start_ + __i;
1707227825Stheraven    return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1708227825Stheraven}
1709227825Stheraven
1710227825Stheraventemplate <class _Tp, class _Allocator>
1711227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1712227825Stheraventypename deque<_Tp, _Allocator>::reference
1713227825Stheravendeque<_Tp, _Allocator>::front()
1714227825Stheraven{
1715227825Stheraven    return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1716227825Stheraven                                      + __base::__start_ % __base::__block_size);
1717227825Stheraven}
1718227825Stheraven
1719227825Stheraventemplate <class _Tp, class _Allocator>
1720227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1721227825Stheraventypename deque<_Tp, _Allocator>::const_reference
1722227825Stheravendeque<_Tp, _Allocator>::front() const
1723227825Stheraven{
1724227825Stheraven    return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1725227825Stheraven                                      + __base::__start_ % __base::__block_size);
1726227825Stheraven}
1727227825Stheraven
1728227825Stheraventemplate <class _Tp, class _Allocator>
1729227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1730227825Stheraventypename deque<_Tp, _Allocator>::reference
1731227825Stheravendeque<_Tp, _Allocator>::back()
1732227825Stheraven{
1733227825Stheraven    size_type __p = __base::size() + __base::__start_ - 1;
1734227825Stheraven    return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1735227825Stheraven}
1736227825Stheraven
1737227825Stheraventemplate <class _Tp, class _Allocator>
1738227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1739227825Stheraventypename deque<_Tp, _Allocator>::const_reference
1740227825Stheravendeque<_Tp, _Allocator>::back() const
1741227825Stheraven{
1742227825Stheraven    size_type __p = __base::size() + __base::__start_ - 1;
1743227825Stheraven    return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1744227825Stheraven}
1745227825Stheraven
1746227825Stheraventemplate <class _Tp, class _Allocator>
1747227825Stheravenvoid
1748227825Stheravendeque<_Tp, _Allocator>::push_back(const value_type& __v)
1749227825Stheraven{
1750227825Stheraven    allocator_type& __a = __base::__alloc();
1751227825Stheraven    if (__back_spare() == 0)
1752227825Stheraven        __add_back_capacity();
1753227825Stheraven    // __back_spare() >= 1
1754227825Stheraven    __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v);
1755227825Stheraven    ++__base::size();
1756227825Stheraven}
1757227825Stheraven
1758227825Stheraven#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1759227825Stheraven
1760227825Stheraventemplate <class _Tp, class _Allocator>
1761227825Stheravenvoid
1762227825Stheravendeque<_Tp, _Allocator>::push_back(value_type&& __v)
1763227825Stheraven{
1764227825Stheraven    allocator_type& __a = __base::__alloc();
1765227825Stheraven    if (__back_spare() == 0)
1766227825Stheraven        __add_back_capacity();
1767227825Stheraven    // __back_spare() >= 1
1768227825Stheraven    __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v));
1769227825Stheraven    ++__base::size();
1770227825Stheraven}
1771227825Stheraven
1772227825Stheraven#ifndef _LIBCPP_HAS_NO_VARIADICS
1773227825Stheraven
1774227825Stheraventemplate <class _Tp, class _Allocator>
1775227825Stheraventemplate <class... _Args>
1776227825Stheravenvoid
1777227825Stheravendeque<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1778227825Stheraven{
1779227825Stheraven    allocator_type& __a = __base::__alloc();
1780227825Stheraven    if (__back_spare() == 0)
1781227825Stheraven        __add_back_capacity();
1782227825Stheraven    // __back_spare() >= 1
1783227825Stheraven    __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::forward<_Args>(__args)...);
1784227825Stheraven    ++__base::size();
1785227825Stheraven}
1786227825Stheraven
1787227825Stheraven#endif  // _LIBCPP_HAS_NO_VARIADICS
1788227825Stheraven#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1789227825Stheraven
1790227825Stheraventemplate <class _Tp, class _Allocator>
1791227825Stheravenvoid
1792227825Stheravendeque<_Tp, _Allocator>::push_front(const value_type& __v)
1793227825Stheraven{
1794227825Stheraven    allocator_type& __a = __base::__alloc();
1795227825Stheraven    if (__front_spare() == 0)
1796227825Stheraven        __add_front_capacity();
1797227825Stheraven    // __front_spare() >= 1
1798227825Stheraven    __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v);
1799227825Stheraven    --__base::__start_;
1800227825Stheraven    ++__base::size();
1801227825Stheraven}
1802227825Stheraven
1803227825Stheraven#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1804227825Stheraven
1805227825Stheraventemplate <class _Tp, class _Allocator>
1806227825Stheravenvoid
1807227825Stheravendeque<_Tp, _Allocator>::push_front(value_type&& __v)
1808227825Stheraven{
1809227825Stheraven    allocator_type& __a = __base::__alloc();
1810227825Stheraven    if (__front_spare() == 0)
1811227825Stheraven        __add_front_capacity();
1812227825Stheraven    // __front_spare() >= 1
1813227825Stheraven    __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v));
1814227825Stheraven    --__base::__start_;
1815227825Stheraven    ++__base::size();
1816227825Stheraven}
1817227825Stheraven
1818227825Stheraven#ifndef _LIBCPP_HAS_NO_VARIADICS
1819227825Stheraven
1820227825Stheraventemplate <class _Tp, class _Allocator>
1821227825Stheraventemplate <class... _Args>
1822227825Stheravenvoid
1823227825Stheravendeque<_Tp, _Allocator>::emplace_front(_Args&&... __args)
1824227825Stheraven{
1825227825Stheraven    allocator_type& __a = __base::__alloc();
1826227825Stheraven    if (__front_spare() == 0)
1827227825Stheraven        __add_front_capacity();
1828227825Stheraven    // __front_spare() >= 1
1829227825Stheraven    __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...);
1830227825Stheraven    --__base::__start_;
1831227825Stheraven    ++__base::size();
1832227825Stheraven}
1833227825Stheraven
1834227825Stheraven#endif  // _LIBCPP_HAS_NO_VARIADICS
1835227825Stheraven#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1836227825Stheraven
1837227825Stheraventemplate <class _Tp, class _Allocator>
1838227825Stheraventypename deque<_Tp, _Allocator>::iterator
1839227825Stheravendeque<_Tp, _Allocator>::insert(const_iterator __p, const value_type& __v)
1840227825Stheraven{
1841227825Stheraven    size_type __pos = __p - __base::begin();
1842227825Stheraven    size_type __to_end = __base::size() - __pos;
1843227825Stheraven    allocator_type& __a = __base::__alloc();
1844227825Stheraven    if (__pos < __to_end)
1845227825Stheraven    {   // insert by shifting things backward
1846227825Stheraven        if (__front_spare() == 0)
1847227825Stheraven            __add_front_capacity();
1848227825Stheraven        // __front_spare() >= 1
1849227825Stheraven        if (__pos == 0)
1850227825Stheraven        {
1851227825Stheraven            __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v);
1852227825Stheraven            --__base::__start_;
1853227825Stheraven            ++__base::size();
1854227825Stheraven        }
1855227825Stheraven        else
1856227825Stheraven        {
1857227825Stheraven            const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1858227825Stheraven            iterator __b = __base::begin();
1859227825Stheraven            iterator __bm1 = _VSTD::prev(__b);
1860227825Stheraven            if (__vt == pointer_traits<const_pointer>::pointer_to(*__b))
1861227825Stheraven                __vt = pointer_traits<const_pointer>::pointer_to(*__bm1);
1862227825Stheraven            __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
1863227825Stheraven            --__base::__start_;
1864227825Stheraven            ++__base::size();
1865227825Stheraven            if (__pos > 1)
1866227825Stheraven                __b = __move_and_check(_VSTD::next(__b), __b + __pos, __b, __vt);
1867227825Stheraven            *__b = *__vt;
1868227825Stheraven        }
1869227825Stheraven    }
1870227825Stheraven    else
1871227825Stheraven    {   // insert by shifting things forward
1872227825Stheraven        if (__back_spare() == 0)
1873227825Stheraven            __add_back_capacity();
1874227825Stheraven        // __back_capacity >= 1
1875227825Stheraven        size_type __de = __base::size() - __pos;
1876227825Stheraven        if (__de == 0)
1877227825Stheraven        {
1878227825Stheraven            __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v);
1879227825Stheraven            ++__base::size();
1880227825Stheraven        }
1881227825Stheraven        else
1882227825Stheraven        {
1883227825Stheraven            const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1884227825Stheraven            iterator __e = __base::end();
1885227825Stheraven            iterator __em1 = _VSTD::prev(__e);
1886227825Stheraven            if (__vt == pointer_traits<const_pointer>::pointer_to(*__em1))
1887227825Stheraven                __vt = pointer_traits<const_pointer>::pointer_to(*__e);
1888227825Stheraven            __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
1889227825Stheraven            ++__base::size();
1890227825Stheraven            if (__de > 1)
1891227825Stheraven                __e = __move_backward_and_check(__e - __de, __em1, __e, __vt);
1892227825Stheraven            *--__e = *__vt;
1893227825Stheraven        }
1894227825Stheraven    }
1895227825Stheraven    return __base::begin() + __pos;
1896227825Stheraven}
1897227825Stheraven
1898227825Stheraven#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1899227825Stheraven
1900227825Stheraventemplate <class _Tp, class _Allocator>
1901227825Stheraventypename deque<_Tp, _Allocator>::iterator
1902227825Stheravendeque<_Tp, _Allocator>::insert(const_iterator __p, value_type&& __v)
1903227825Stheraven{
1904227825Stheraven    size_type __pos = __p - __base::begin();
1905227825Stheraven    size_type __to_end = __base::size() - __pos;
1906227825Stheraven    allocator_type& __a = __base::__alloc();
1907227825Stheraven    if (__pos < __to_end)
1908227825Stheraven    {   // insert by shifting things backward
1909227825Stheraven        if (__front_spare() == 0)
1910227825Stheraven            __add_front_capacity();
1911227825Stheraven        // __front_spare() >= 1
1912227825Stheraven        if (__pos == 0)
1913227825Stheraven        {
1914227825Stheraven            __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v));
1915227825Stheraven            --__base::__start_;
1916227825Stheraven            ++__base::size();
1917227825Stheraven        }
1918227825Stheraven        else
1919227825Stheraven        {
1920227825Stheraven            iterator __b = __base::begin();
1921227825Stheraven            iterator __bm1 = _VSTD::prev(__b);
1922227825Stheraven            __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
1923227825Stheraven            --__base::__start_;
1924227825Stheraven            ++__base::size();
1925227825Stheraven            if (__pos > 1)
1926227825Stheraven                __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
1927227825Stheraven            *__b = _VSTD::move(__v);
1928227825Stheraven        }
1929227825Stheraven    }
1930227825Stheraven    else
1931227825Stheraven    {   // insert by shifting things forward
1932227825Stheraven        if (__back_spare() == 0)
1933227825Stheraven            __add_back_capacity();
1934227825Stheraven        // __back_capacity >= 1
1935227825Stheraven        size_type __de = __base::size() - __pos;
1936227825Stheraven        if (__de == 0)
1937227825Stheraven        {
1938227825Stheraven            __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v));
1939227825Stheraven            ++__base::size();
1940227825Stheraven        }
1941227825Stheraven        else
1942227825Stheraven        {
1943227825Stheraven            iterator __e = __base::end();
1944227825Stheraven            iterator __em1 = _VSTD::prev(__e);
1945227825Stheraven            __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
1946227825Stheraven            ++__base::size();
1947227825Stheraven            if (__de > 1)
1948227825Stheraven                __e = _VSTD::move_backward(__e - __de, __em1, __e);
1949227825Stheraven            *--__e = _VSTD::move(__v);
1950227825Stheraven        }
1951227825Stheraven    }
1952227825Stheraven    return __base::begin() + __pos;
1953227825Stheraven}
1954227825Stheraven
1955227825Stheraven#ifndef _LIBCPP_HAS_NO_VARIADICS
1956227825Stheraven
1957227825Stheraventemplate <class _Tp, class _Allocator>
1958227825Stheraventemplate <class... _Args>
1959227825Stheraventypename deque<_Tp, _Allocator>::iterator
1960227825Stheravendeque<_Tp, _Allocator>::emplace(const_iterator __p, _Args&&... __args)
1961227825Stheraven{
1962227825Stheraven    size_type __pos = __p - __base::begin();
1963227825Stheraven    size_type __to_end = __base::size() - __pos;
1964227825Stheraven    allocator_type& __a = __base::__alloc();
1965227825Stheraven    if (__pos < __to_end)
1966227825Stheraven    {   // insert by shifting things backward
1967227825Stheraven        if (__front_spare() == 0)
1968227825Stheraven            __add_front_capacity();
1969227825Stheraven        // __front_spare() >= 1
1970227825Stheraven        if (__pos == 0)
1971227825Stheraven        {
1972227825Stheraven            __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...);
1973227825Stheraven            --__base::__start_;
1974227825Stheraven            ++__base::size();
1975227825Stheraven        }
1976227825Stheraven        else
1977227825Stheraven        {
1978241903Sdim            value_type __tmp(_VSTD::forward<_Args>(__args)...);
1979227825Stheraven            iterator __b = __base::begin();
1980227825Stheraven            iterator __bm1 = _VSTD::prev(__b);
1981227825Stheraven            __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
1982227825Stheraven            --__base::__start_;
1983227825Stheraven            ++__base::size();
1984227825Stheraven            if (__pos > 1)
1985227825Stheraven                __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
1986241903Sdim            *__b = _VSTD::move(__tmp);
1987227825Stheraven        }
1988227825Stheraven    }
1989227825Stheraven    else
1990227825Stheraven    {   // insert by shifting things forward
1991227825Stheraven        if (__back_spare() == 0)
1992227825Stheraven            __add_back_capacity();
1993227825Stheraven        // __back_capacity >= 1
1994227825Stheraven        size_type __de = __base::size() - __pos;
1995227825Stheraven        if (__de == 0)
1996227825Stheraven        {
1997227825Stheraven            __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::forward<_Args>(__args)...);
1998227825Stheraven            ++__base::size();
1999227825Stheraven        }
2000227825Stheraven        else
2001227825Stheraven        {
2002241903Sdim            value_type __tmp(_VSTD::forward<_Args>(__args)...);
2003227825Stheraven            iterator __e = __base::end();
2004227825Stheraven            iterator __em1 = _VSTD::prev(__e);
2005227825Stheraven            __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
2006227825Stheraven            ++__base::size();
2007227825Stheraven            if (__de > 1)
2008227825Stheraven                __e = _VSTD::move_backward(__e - __de, __em1, __e);
2009241903Sdim            *--__e = _VSTD::move(__tmp);
2010227825Stheraven        }
2011227825Stheraven    }
2012227825Stheraven    return __base::begin() + __pos;
2013227825Stheraven}
2014227825Stheraven
2015227825Stheraven#endif  // _LIBCPP_HAS_NO_VARIADICS
2016227825Stheraven#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2017227825Stheraven
2018227825Stheraventemplate <class _Tp, class _Allocator>
2019227825Stheraventypename deque<_Tp, _Allocator>::iterator
2020227825Stheravendeque<_Tp, _Allocator>::insert(const_iterator __p, size_type __n, const value_type& __v)
2021227825Stheraven{
2022227825Stheraven    size_type __pos = __p - __base::begin();
2023227825Stheraven    size_type __to_end = __base::size() - __pos;
2024227825Stheraven    allocator_type& __a = __base::__alloc();
2025227825Stheraven    if (__pos < __to_end)
2026227825Stheraven    {   // insert by shifting things backward
2027227825Stheraven        if (__n > __front_spare())
2028227825Stheraven            __add_front_capacity(__n - __front_spare());
2029227825Stheraven        // __n <= __front_spare()
2030227825Stheraven        size_type __old_n = __n;
2031227825Stheraven        iterator __old_begin = __base::begin();
2032227825Stheraven        iterator __i = __old_begin;
2033227825Stheraven        if (__n > __pos)
2034227825Stheraven        {
2035227825Stheraven            for (size_type __m = __n - __pos; __m; --__m, --__base::__start_, ++__base::size())
2036227825Stheraven                __alloc_traits::construct(__a, _VSTD::addressof(*--__i), __v);
2037227825Stheraven            __n = __pos;
2038227825Stheraven        }
2039227825Stheraven        if (__n > 0)
2040227825Stheraven        {
2041227825Stheraven            const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2042227825Stheraven            iterator __obn = __old_begin + __n;
2043227825Stheraven            __move_construct_backward_and_check(__old_begin, __obn, __i, __vt);
2044227825Stheraven            if (__n < __pos)
2045227825Stheraven                __old_begin = __move_and_check(__obn, __old_begin + __pos, __old_begin, __vt);
2046227825Stheraven            _VSTD::fill_n(__old_begin, __n, *__vt);
2047227825Stheraven        }
2048227825Stheraven    }
2049227825Stheraven    else
2050227825Stheraven    {   // insert by shifting things forward
2051227825Stheraven        size_type __back_capacity = __back_spare();
2052227825Stheraven        if (__n > __back_capacity)
2053227825Stheraven            __add_back_capacity(__n - __back_capacity);
2054227825Stheraven        // __n <= __back_capacity
2055227825Stheraven        size_type __old_n = __n;
2056227825Stheraven        iterator __old_end = __base::end();
2057227825Stheraven        iterator __i = __old_end;
2058227825Stheraven        size_type __de = __base::size() - __pos;
2059227825Stheraven        if (__n > __de)
2060227825Stheraven        {
2061227825Stheraven            for (size_type __m = __n - __de; __m; --__m, ++__i, ++__base::size())
2062227825Stheraven                __alloc_traits::construct(__a, _VSTD::addressof(*__i), __v);
2063227825Stheraven            __n = __de;
2064227825Stheraven        }
2065227825Stheraven        if (__n > 0)
2066227825Stheraven        {
2067227825Stheraven            const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2068227825Stheraven            iterator __oen = __old_end - __n;
2069227825Stheraven            __move_construct_and_check(__oen, __old_end, __i, __vt);
2070227825Stheraven            if (__n < __de)
2071227825Stheraven                __old_end = __move_backward_and_check(__old_end - __de, __oen, __old_end, __vt);
2072227825Stheraven            _VSTD::fill_n(__old_end - __n, __n, *__vt);
2073227825Stheraven        }
2074227825Stheraven    }
2075227825Stheraven    return __base::begin() + __pos;
2076227825Stheraven}
2077227825Stheraven
2078227825Stheraventemplate <class _Tp, class _Allocator>
2079227825Stheraventemplate <class _InputIter>
2080227825Stheraventypename deque<_Tp, _Allocator>::iterator
2081227825Stheravendeque<_Tp, _Allocator>::insert(const_iterator __p, _InputIter __f, _InputIter __l,
2082227825Stheraven                               typename enable_if<__is_input_iterator<_InputIter>::value
2083227825Stheraven                                               &&!__is_bidirectional_iterator<_InputIter>::value>::type*)
2084227825Stheraven{
2085227825Stheraven    __split_buffer<value_type, allocator_type&> __buf(__base::__alloc());
2086227825Stheraven    __buf.__construct_at_end(__f, __l);
2087227825Stheraven    typedef typename __split_buffer<value_type, allocator_type&>::iterator __bi;
2088227825Stheraven    return insert(__p, move_iterator<__bi>(__buf.begin()), move_iterator<__bi>(__buf.end()));
2089227825Stheraven}
2090227825Stheraven
2091227825Stheraventemplate <class _Tp, class _Allocator>
2092227825Stheraventemplate <class _BiIter>
2093227825Stheraventypename deque<_Tp, _Allocator>::iterator
2094227825Stheravendeque<_Tp, _Allocator>::insert(const_iterator __p, _BiIter __f, _BiIter __l,
2095227825Stheraven                               typename enable_if<__is_bidirectional_iterator<_BiIter>::value>::type*)
2096227825Stheraven{
2097227825Stheraven    size_type __n = _VSTD::distance(__f, __l);
2098227825Stheraven    size_type __pos = __p - __base::begin();
2099227825Stheraven    size_type __to_end = __base::size() - __pos;
2100227825Stheraven    allocator_type& __a = __base::__alloc();
2101227825Stheraven    if (__pos < __to_end)
2102227825Stheraven    {   // insert by shifting things backward
2103227825Stheraven        if (__n > __front_spare())
2104227825Stheraven            __add_front_capacity(__n - __front_spare());
2105227825Stheraven        // __n <= __front_spare()
2106227825Stheraven        size_type __old_n = __n;
2107227825Stheraven        iterator __old_begin = __base::begin();
2108227825Stheraven        iterator __i = __old_begin;
2109227825Stheraven        _BiIter __m = __f;
2110227825Stheraven        if (__n > __pos)
2111227825Stheraven        {
2112227825Stheraven            __m = __pos < __n / 2 ? _VSTD::prev(__l, __pos) : _VSTD::next(__f, __n - __pos);
2113227825Stheraven            for (_BiIter __j = __m; __j != __f; --__base::__start_, ++__base::size())
2114227825Stheraven                __alloc_traits::construct(__a, _VSTD::addressof(*--__i), *--__j);
2115227825Stheraven            __n = __pos;
2116227825Stheraven        }
2117227825Stheraven        if (__n > 0)
2118227825Stheraven        {
2119227825Stheraven            iterator __obn = __old_begin + __n;
2120227825Stheraven            for (iterator __j = __obn; __j != __old_begin;)
2121227825Stheraven            {
2122227825Stheraven                __alloc_traits::construct(__a, _VSTD::addressof(*--__i), _VSTD::move(*--__j));
2123227825Stheraven                --__base::__start_;
2124227825Stheraven                ++__base::size();
2125227825Stheraven            }
2126227825Stheraven            if (__n < __pos)
2127227825Stheraven                __old_begin = _VSTD::move(__obn, __old_begin + __pos, __old_begin);
2128227825Stheraven            _VSTD::copy(__m, __l, __old_begin);
2129227825Stheraven        }
2130227825Stheraven    }
2131227825Stheraven    else
2132227825Stheraven    {   // insert by shifting things forward
2133227825Stheraven        size_type __back_capacity = __back_spare();
2134227825Stheraven        if (__n > __back_capacity)
2135227825Stheraven            __add_back_capacity(__n - __back_capacity);
2136227825Stheraven        // __n <= __back_capacity
2137227825Stheraven        size_type __old_n = __n;
2138227825Stheraven        iterator __old_end = __base::end();
2139227825Stheraven        iterator __i = __old_end;
2140227825Stheraven        _BiIter __m = __l;
2141227825Stheraven        size_type __de = __base::size() - __pos;
2142227825Stheraven        if (__n > __de)
2143227825Stheraven        {
2144227825Stheraven            __m = __de < __n / 2 ? _VSTD::next(__f, __de) : _VSTD::prev(__l, __n - __de);
2145227825Stheraven            for (_BiIter __j = __m; __j != __l; ++__i, ++__j, ++__base::size())
2146227825Stheraven                __alloc_traits::construct(__a, _VSTD::addressof(*__i), *__j);
2147227825Stheraven            __n = __de;
2148227825Stheraven        }
2149227825Stheraven        if (__n > 0)
2150227825Stheraven        {
2151227825Stheraven            iterator __oen = __old_end - __n;
2152227825Stheraven            for (iterator __j = __oen; __j != __old_end; ++__i, ++__j, ++__base::size())
2153227825Stheraven                __alloc_traits::construct(__a, _VSTD::addressof(*__i), _VSTD::move(*__j));
2154227825Stheraven            if (__n < __de)
2155227825Stheraven                __old_end = _VSTD::move_backward(__old_end - __de, __oen, __old_end);
2156227825Stheraven            _VSTD::copy_backward(__f, __m, __old_end);
2157227825Stheraven        }
2158227825Stheraven    }
2159227825Stheraven    return __base::begin() + __pos;
2160227825Stheraven}
2161227825Stheraven
2162227825Stheraventemplate <class _Tp, class _Allocator>
2163227825Stheraventemplate <class _InpIter>
2164227825Stheravenvoid
2165227825Stheravendeque<_Tp, _Allocator>::__append(_InpIter __f, _InpIter __l,
2166227825Stheraven                                 typename enable_if<__is_input_iterator<_InpIter>::value &&
2167227825Stheraven                                                   !__is_forward_iterator<_InpIter>::value>::type*)
2168227825Stheraven{
2169227825Stheraven    for (; __f != __l; ++__f)
2170227825Stheraven        push_back(*__f);
2171227825Stheraven}
2172227825Stheraven
2173227825Stheraventemplate <class _Tp, class _Allocator>
2174227825Stheraventemplate <class _ForIter>
2175227825Stheravenvoid
2176227825Stheravendeque<_Tp, _Allocator>::__append(_ForIter __f, _ForIter __l,
2177227825Stheraven                                 typename enable_if<__is_forward_iterator<_ForIter>::value>::type*)
2178227825Stheraven{
2179227825Stheraven    size_type __n = _VSTD::distance(__f, __l);
2180227825Stheraven    allocator_type& __a = __base::__alloc();
2181227825Stheraven    size_type __back_capacity = __back_spare();
2182227825Stheraven    if (__n > __back_capacity)
2183227825Stheraven        __add_back_capacity(__n - __back_capacity);
2184227825Stheraven    // __n <= __back_capacity
2185227825Stheraven    for (iterator __i = __base::end(); __f != __l; ++__i, ++__f, ++__base::size())
2186227825Stheraven        __alloc_traits::construct(__a, _VSTD::addressof(*__i), *__f);
2187227825Stheraven}
2188227825Stheraven
2189227825Stheraventemplate <class _Tp, class _Allocator>
2190227825Stheravenvoid
2191227825Stheravendeque<_Tp, _Allocator>::__append(size_type __n)
2192227825Stheraven{
2193227825Stheraven    allocator_type& __a = __base::__alloc();
2194227825Stheraven    size_type __back_capacity = __back_spare();
2195227825Stheraven    if (__n > __back_capacity)
2196227825Stheraven        __add_back_capacity(__n - __back_capacity);
2197227825Stheraven    // __n <= __back_capacity
2198227825Stheraven    for (iterator __i = __base::end(); __n; --__n, ++__i, ++__base::size())
2199227825Stheraven        __alloc_traits::construct(__a, _VSTD::addressof(*__i));
2200227825Stheraven}
2201227825Stheraven
2202227825Stheraventemplate <class _Tp, class _Allocator>
2203227825Stheravenvoid
2204227825Stheravendeque<_Tp, _Allocator>::__append(size_type __n, const value_type& __v)
2205227825Stheraven{
2206227825Stheraven    allocator_type& __a = __base::__alloc();
2207227825Stheraven    size_type __back_capacity = __back_spare();
2208227825Stheraven    if (__n > __back_capacity)
2209227825Stheraven        __add_back_capacity(__n - __back_capacity);
2210227825Stheraven    // __n <= __back_capacity
2211227825Stheraven    for (iterator __i = __base::end(); __n; --__n, ++__i, ++__base::size())
2212227825Stheraven        __alloc_traits::construct(__a, _VSTD::addressof(*__i), __v);
2213227825Stheraven}
2214227825Stheraven
2215227825Stheraven// Create front capacity for one block of elements.
2216227825Stheraven// Strong guarantee.  Either do it or don't touch anything.
2217227825Stheraventemplate <class _Tp, class _Allocator>
2218227825Stheravenvoid
2219227825Stheravendeque<_Tp, _Allocator>::__add_front_capacity()
2220227825Stheraven{
2221227825Stheraven    allocator_type& __a = __base::__alloc();
2222227825Stheraven    if (__back_spare() >= __base::__block_size)
2223227825Stheraven    {
2224227825Stheraven        __base::__start_ += __base::__block_size;
2225227825Stheraven        pointer __pt = __base::__map_.back();
2226227825Stheraven        __base::__map_.pop_back();
2227227825Stheraven        __base::__map_.push_front(__pt);
2228227825Stheraven    }
2229227825Stheraven    // Else if __base::__map_.size() < __base::__map_.capacity() then we need to allocate 1 buffer
2230227825Stheraven    else if (__base::__map_.size() < __base::__map_.capacity())
2231227825Stheraven    {   // we can put the new buffer into the map, but don't shift things around
2232227825Stheraven        // until all buffers are allocated.  If we throw, we don't need to fix
2233227825Stheraven        // anything up (any added buffers are undetectible)
2234227825Stheraven        if (__base::__map_.__front_spare() > 0)
2235227825Stheraven            __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2236227825Stheraven        else
2237227825Stheraven        {
2238227825Stheraven            __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2239227825Stheraven            // Done allocating, reorder capacity
2240227825Stheraven            pointer __pt = __base::__map_.back();
2241227825Stheraven            __base::__map_.pop_back();
2242227825Stheraven            __base::__map_.push_front(__pt);
2243227825Stheraven        }
2244227825Stheraven        __base::__start_ = __base::__map_.size() == 1 ?
2245227825Stheraven                               __base::__block_size / 2 :
2246227825Stheraven                               __base::__start_ + __base::__block_size;
2247227825Stheraven    }
2248227825Stheraven    // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2249227825Stheraven    else
2250227825Stheraven    {
2251227825Stheraven        __split_buffer<pointer, typename __base::__pointer_allocator&>
2252227825Stheraven            __buf(max<size_type>(2 * __base::__map_.capacity(), 1),
2253227825Stheraven                  0, __base::__map_.__alloc());
2254227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
2255227825Stheraven        try
2256227825Stheraven        {
2257227825Stheraven#endif  // _LIBCPP_NO_EXCEPTIONS
2258227825Stheraven            __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2259227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
2260227825Stheraven        }
2261227825Stheraven        catch (...)
2262227825Stheraven        {
2263227825Stheraven            __alloc_traits::deallocate(__a, __buf.front(), __base::__block_size);
2264227825Stheraven            throw;
2265227825Stheraven        }
2266227825Stheraven#endif  // _LIBCPP_NO_EXCEPTIONS
2267227825Stheraven        for (typename __base::__map_pointer __i = __base::__map_.begin();
2268227825Stheraven                __i != __base::__map_.end(); ++__i)
2269227825Stheraven            __buf.push_back(*__i);
2270227825Stheraven        _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2271227825Stheraven        _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2272227825Stheraven        _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2273227825Stheraven        _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
2274227825Stheraven        __base::__start_ = __base::__map_.size() == 1 ?
2275227825Stheraven                               __base::__block_size / 2 :
2276227825Stheraven                               __base::__start_ + __base::__block_size;
2277227825Stheraven    }
2278227825Stheraven}
2279227825Stheraven
2280227825Stheraven// Create front capacity for __n elements.
2281227825Stheraven// Strong guarantee.  Either do it or don't touch anything.
2282227825Stheraventemplate <class _Tp, class _Allocator>
2283227825Stheravenvoid
2284227825Stheravendeque<_Tp, _Allocator>::__add_front_capacity(size_type __n)
2285227825Stheraven{
2286227825Stheraven    allocator_type& __a = __base::__alloc();
2287227825Stheraven    size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2288227825Stheraven    // Number of unused blocks at back:
2289227825Stheraven    size_type __back_capacity = __back_spare() / __base::__block_size;
2290227825Stheraven    __back_capacity = _VSTD::min(__back_capacity, __nb);  // don't take more than you need
2291227825Stheraven    __nb -= __back_capacity;  // number of blocks need to allocate
2292227825Stheraven    // If __nb == 0, then we have sufficient capacity.
2293227825Stheraven    if (__nb == 0)
2294227825Stheraven    {
2295227825Stheraven        __base::__start_ += __base::__block_size * __back_capacity;
2296227825Stheraven        for (; __back_capacity > 0; --__back_capacity)
2297227825Stheraven        {
2298227825Stheraven            pointer __pt = __base::__map_.back();
2299227825Stheraven            __base::__map_.pop_back();
2300227825Stheraven            __base::__map_.push_front(__pt);
2301227825Stheraven        }
2302227825Stheraven    }
2303227825Stheraven    // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2304227825Stheraven    else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2305227825Stheraven    {   // we can put the new buffers into the map, but don't shift things around
2306227825Stheraven        // until all buffers are allocated.  If we throw, we don't need to fix
2307227825Stheraven        // anything up (any added buffers are undetectible)
2308227825Stheraven        for (; __nb > 0; --__nb, __base::__start_ += __base::__block_size - (__base::__map_.size() == 1))
2309227825Stheraven        {
2310227825Stheraven            if (__base::__map_.__front_spare() == 0)
2311227825Stheraven                break;
2312227825Stheraven            __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2313227825Stheraven        }
2314227825Stheraven        for (; __nb > 0; --__nb, ++__back_capacity)
2315227825Stheraven            __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2316227825Stheraven        // Done allocating, reorder capacity
2317227825Stheraven        __base::__start_ += __back_capacity * __base::__block_size;
2318227825Stheraven        for (; __back_capacity > 0; --__back_capacity)
2319227825Stheraven        {
2320227825Stheraven            pointer __pt = __base::__map_.back();
2321227825Stheraven            __base::__map_.pop_back();
2322227825Stheraven            __base::__map_.push_front(__pt);
2323227825Stheraven        }
2324227825Stheraven    }
2325227825Stheraven    // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2326227825Stheraven    else
2327227825Stheraven    {
2328227825Stheraven        size_type __ds = (__nb + __back_capacity) * __base::__block_size - __base::__map_.empty();
2329227825Stheraven        __split_buffer<pointer, typename __base::__pointer_allocator&>
2330227825Stheraven            __buf(max<size_type>(2* __base::__map_.capacity(),
2331227825Stheraven                                 __nb + __base::__map_.size()),
2332227825Stheraven                  0, __base::__map_.__alloc());
2333227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
2334227825Stheraven        try
2335227825Stheraven        {
2336227825Stheraven#endif  // _LIBCPP_NO_EXCEPTIONS
2337227825Stheraven            for (; __nb > 0; --__nb)
2338227825Stheraven                __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2339227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
2340227825Stheraven        }
2341227825Stheraven        catch (...)
2342227825Stheraven        {
2343227825Stheraven            for (typename __base::__map_pointer __i = __buf.begin();
2344227825Stheraven                    __i != __buf.end(); ++__i)
2345227825Stheraven                __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2346227825Stheraven            throw;
2347227825Stheraven        }
2348227825Stheraven#endif  // _LIBCPP_NO_EXCEPTIONS
2349227825Stheraven        for (; __back_capacity > 0; --__back_capacity)
2350227825Stheraven        {
2351227825Stheraven            __buf.push_back(__base::__map_.back());
2352227825Stheraven            __base::__map_.pop_back();
2353227825Stheraven        }
2354227825Stheraven        for (typename __base::__map_pointer __i = __base::__map_.begin();
2355227825Stheraven                __i != __base::__map_.end(); ++__i)
2356227825Stheraven            __buf.push_back(*__i);
2357227825Stheraven        _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2358227825Stheraven        _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2359227825Stheraven        _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2360227825Stheraven        _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
2361227825Stheraven        __base::__start_ += __ds;
2362227825Stheraven    }
2363227825Stheraven}
2364227825Stheraven
2365227825Stheraven// Create back capacity for one block of elements.
2366227825Stheraven// Strong guarantee.  Either do it or don't touch anything.
2367227825Stheraventemplate <class _Tp, class _Allocator>
2368227825Stheravenvoid
2369227825Stheravendeque<_Tp, _Allocator>::__add_back_capacity()
2370227825Stheraven{
2371227825Stheraven    allocator_type& __a = __base::__alloc();
2372227825Stheraven    if (__front_spare() >= __base::__block_size)
2373227825Stheraven    {
2374227825Stheraven        __base::__start_ -= __base::__block_size;
2375227825Stheraven        pointer __pt = __base::__map_.front();
2376227825Stheraven        __base::__map_.pop_front();
2377227825Stheraven        __base::__map_.push_back(__pt);
2378227825Stheraven    }
2379227825Stheraven    // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2380227825Stheraven    else if (__base::__map_.size() < __base::__map_.capacity())
2381227825Stheraven    {   // we can put the new buffer into the map, but don't shift things around
2382227825Stheraven        // until it is allocated.  If we throw, we don't need to fix
2383227825Stheraven        // anything up (any added buffers are undetectible)
2384227825Stheraven        if (__base::__map_.__back_spare() != 0)
2385227825Stheraven            __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2386227825Stheraven        else
2387227825Stheraven        {
2388227825Stheraven            __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2389227825Stheraven            // Done allocating, reorder capacity
2390227825Stheraven            pointer __pt = __base::__map_.front();
2391227825Stheraven            __base::__map_.pop_front();
2392227825Stheraven            __base::__map_.push_back(__pt);
2393227825Stheraven        }
2394227825Stheraven    }
2395227825Stheraven    // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2396227825Stheraven    else
2397227825Stheraven    {
2398227825Stheraven        __split_buffer<pointer, typename __base::__pointer_allocator&>
2399227825Stheraven            __buf(max<size_type>(2* __base::__map_.capacity(), 1),
2400227825Stheraven                  __base::__map_.size(),
2401227825Stheraven                  __base::__map_.__alloc());
2402227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
2403227825Stheraven        try
2404227825Stheraven        {
2405227825Stheraven#endif  // _LIBCPP_NO_EXCEPTIONS
2406227825Stheraven            __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2407227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
2408227825Stheraven        }
2409227825Stheraven        catch (...)
2410227825Stheraven        {
2411227825Stheraven            __alloc_traits::deallocate(__a, __buf.back(), __base::__block_size);
2412227825Stheraven            throw;
2413227825Stheraven        }
2414227825Stheraven#endif  // _LIBCPP_NO_EXCEPTIONS
2415227825Stheraven        for (typename __base::__map_pointer __i = __base::__map_.end();
2416227825Stheraven                __i != __base::__map_.begin();)
2417227825Stheraven            __buf.push_front(*--__i);
2418227825Stheraven        _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2419227825Stheraven        _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2420227825Stheraven        _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2421227825Stheraven        _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
2422227825Stheraven    }
2423227825Stheraven}
2424227825Stheraven
2425227825Stheraven// Create back capacity for __n elements.
2426227825Stheraven// Strong guarantee.  Either do it or don't touch anything.
2427227825Stheraventemplate <class _Tp, class _Allocator>
2428227825Stheravenvoid
2429227825Stheravendeque<_Tp, _Allocator>::__add_back_capacity(size_type __n)
2430227825Stheraven{
2431227825Stheraven    allocator_type& __a = __base::__alloc();
2432227825Stheraven    size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2433227825Stheraven    // Number of unused blocks at front:
2434227825Stheraven    size_type __front_capacity = __front_spare() / __base::__block_size;
2435227825Stheraven    __front_capacity = _VSTD::min(__front_capacity, __nb);  // don't take more than you need
2436227825Stheraven    __nb -= __front_capacity;  // number of blocks need to allocate
2437227825Stheraven    // If __nb == 0, then we have sufficient capacity.
2438227825Stheraven    if (__nb == 0)
2439227825Stheraven    {
2440227825Stheraven        __base::__start_ -= __base::__block_size * __front_capacity;
2441227825Stheraven        for (; __front_capacity > 0; --__front_capacity)
2442227825Stheraven        {
2443227825Stheraven            pointer __pt = __base::__map_.front();
2444227825Stheraven            __base::__map_.pop_front();
2445227825Stheraven            __base::__map_.push_back(__pt);
2446227825Stheraven        }
2447227825Stheraven    }
2448227825Stheraven    // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2449227825Stheraven    else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2450227825Stheraven    {   // we can put the new buffers into the map, but don't shift things around
2451227825Stheraven        // until all buffers are allocated.  If we throw, we don't need to fix
2452227825Stheraven        // anything up (any added buffers are undetectible)
2453227825Stheraven        for (; __nb > 0; --__nb)
2454227825Stheraven        {
2455227825Stheraven            if (__base::__map_.__back_spare() == 0)
2456227825Stheraven                break;
2457227825Stheraven            __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2458227825Stheraven        }
2459227825Stheraven        for (; __nb > 0; --__nb, ++__front_capacity, __base::__start_ +=
2460227825Stheraven                                 __base::__block_size - (__base::__map_.size() == 1))
2461227825Stheraven            __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2462227825Stheraven        // Done allocating, reorder capacity
2463227825Stheraven        __base::__start_ -= __base::__block_size * __front_capacity;
2464227825Stheraven        for (; __front_capacity > 0; --__front_capacity)
2465227825Stheraven        {
2466227825Stheraven            pointer __pt = __base::__map_.front();
2467227825Stheraven            __base::__map_.pop_front();
2468227825Stheraven            __base::__map_.push_back(__pt);
2469227825Stheraven        }
2470227825Stheraven    }
2471227825Stheraven    // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2472227825Stheraven    else
2473227825Stheraven    {
2474227825Stheraven        size_type __ds = __front_capacity * __base::__block_size;
2475227825Stheraven        __split_buffer<pointer, typename __base::__pointer_allocator&>
2476227825Stheraven            __buf(max<size_type>(2* __base::__map_.capacity(),
2477227825Stheraven                                 __nb + __base::__map_.size()),
2478227825Stheraven                  __base::__map_.size() - __front_capacity,
2479227825Stheraven                  __base::__map_.__alloc());
2480227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
2481227825Stheraven        try
2482227825Stheraven        {
2483227825Stheraven#endif  // _LIBCPP_NO_EXCEPTIONS
2484227825Stheraven            for (; __nb > 0; --__nb)
2485227825Stheraven                __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2486227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
2487227825Stheraven        }
2488227825Stheraven        catch (...)
2489227825Stheraven        {
2490227825Stheraven            for (typename __base::__map_pointer __i = __buf.begin();
2491227825Stheraven                    __i != __buf.end(); ++__i)
2492227825Stheraven                __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2493227825Stheraven            throw;
2494227825Stheraven        }
2495227825Stheraven#endif  // _LIBCPP_NO_EXCEPTIONS
2496227825Stheraven        for (; __front_capacity > 0; --__front_capacity)
2497227825Stheraven        {
2498227825Stheraven            __buf.push_back(__base::__map_.front());
2499227825Stheraven            __base::__map_.pop_front();
2500227825Stheraven        }
2501227825Stheraven        for (typename __base::__map_pointer __i = __base::__map_.end();
2502227825Stheraven                __i != __base::__map_.begin();)
2503227825Stheraven            __buf.push_front(*--__i);
2504227825Stheraven        _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2505227825Stheraven        _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2506227825Stheraven        _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2507227825Stheraven        _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
2508227825Stheraven        __base::__start_ -= __ds;
2509227825Stheraven    }
2510227825Stheraven}
2511227825Stheraven
2512227825Stheraventemplate <class _Tp, class _Allocator>
2513227825Stheravenvoid
2514227825Stheravendeque<_Tp, _Allocator>::pop_front()
2515227825Stheraven{
2516227825Stheraven    allocator_type& __a = __base::__alloc();
2517253159Stheraven    __alloc_traits::destroy(__a, __to_raw_pointer(*(__base::__map_.begin() +
2518253159Stheraven                                                    __base::__start_ / __base::__block_size) +
2519253159Stheraven                                                    __base::__start_ % __base::__block_size));
2520227825Stheraven    --__base::size();
2521227825Stheraven    if (++__base::__start_ >= 2 * __base::__block_size)
2522227825Stheraven    {
2523227825Stheraven        __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2524227825Stheraven        __base::__map_.pop_front();
2525227825Stheraven        __base::__start_ -= __base::__block_size;
2526227825Stheraven    }
2527227825Stheraven}
2528227825Stheraven
2529227825Stheraventemplate <class _Tp, class _Allocator>
2530227825Stheravenvoid
2531227825Stheravendeque<_Tp, _Allocator>::pop_back()
2532227825Stheraven{
2533227825Stheraven    allocator_type& __a = __base::__alloc();
2534227825Stheraven    size_type __p = __base::size() + __base::__start_ - 1;
2535253159Stheraven    __alloc_traits::destroy(__a, __to_raw_pointer(*(__base::__map_.begin() +
2536253159Stheraven                                                    __p / __base::__block_size) +
2537253159Stheraven                                                    __p % __base::__block_size));
2538227825Stheraven    --__base::size();
2539227825Stheraven    if (__back_spare() >= 2 * __base::__block_size)
2540227825Stheraven    {
2541227825Stheraven        __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2542227825Stheraven        __base::__map_.pop_back();
2543227825Stheraven    }
2544227825Stheraven}
2545227825Stheraven
2546227825Stheraven// move assign [__f, __l) to [__r, __r + (__l-__f)).
2547227825Stheraven// If __vt points into [__f, __l), then subtract (__f - __r) from __vt.
2548227825Stheraventemplate <class _Tp, class _Allocator>
2549227825Stheraventypename deque<_Tp, _Allocator>::iterator
2550227825Stheravendeque<_Tp, _Allocator>::__move_and_check(iterator __f, iterator __l, iterator __r,
2551227825Stheraven                                         const_pointer& __vt)
2552227825Stheraven{
2553227825Stheraven    // as if
2554227825Stheraven    //   for (; __f != __l; ++__f, ++__r)
2555227825Stheraven    //       *__r = _VSTD::move(*__f);
2556227825Stheraven    difference_type __n = __l - __f;
2557227825Stheraven    while (__n > 0)
2558227825Stheraven    {
2559227825Stheraven        pointer __fb = __f.__ptr_;
2560227825Stheraven        pointer __fe = *__f.__m_iter_ + __base::__block_size;
2561227825Stheraven        difference_type __bs = __fe - __fb;
2562227825Stheraven        if (__bs > __n)
2563227825Stheraven        {
2564227825Stheraven            __bs = __n;
2565227825Stheraven            __fe = __fb + __bs;
2566227825Stheraven        }
2567227825Stheraven        if (__fb <= __vt && __vt < __fe)
2568253159Stheraven            __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) -= __f - __r).__ptr_;
2569227825Stheraven        __r = _VSTD::move(__fb, __fe, __r);
2570227825Stheraven        __n -= __bs;
2571227825Stheraven        __f += __bs;
2572227825Stheraven    }
2573227825Stheraven    return __r;
2574227825Stheraven}
2575227825Stheraven
2576227825Stheraven// move assign [__f, __l) to [__r - (__l-__f), __r) backwards.
2577227825Stheraven// If __vt points into [__f, __l), then add (__r - __l) to __vt.
2578227825Stheraventemplate <class _Tp, class _Allocator>
2579227825Stheraventypename deque<_Tp, _Allocator>::iterator
2580227825Stheravendeque<_Tp, _Allocator>::__move_backward_and_check(iterator __f, iterator __l, iterator __r,
2581227825Stheraven                                                  const_pointer& __vt)
2582227825Stheraven{
2583227825Stheraven    // as if
2584227825Stheraven    //   while (__f != __l)
2585227825Stheraven    //       *--__r = _VSTD::move(*--__l);
2586227825Stheraven    difference_type __n = __l - __f;
2587227825Stheraven    while (__n > 0)
2588227825Stheraven    {
2589227825Stheraven        --__l;
2590227825Stheraven        pointer __lb = *__l.__m_iter_;
2591227825Stheraven        pointer __le = __l.__ptr_ + 1;
2592227825Stheraven        difference_type __bs = __le - __lb;
2593227825Stheraven        if (__bs > __n)
2594227825Stheraven        {
2595227825Stheraven            __bs = __n;
2596227825Stheraven            __lb = __le - __bs;
2597227825Stheraven        }
2598227825Stheraven        if (__lb <= __vt && __vt < __le)
2599253159Stheraven            __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) += __r - __l - 1).__ptr_;
2600227825Stheraven        __r = _VSTD::move_backward(__lb, __le, __r);
2601227825Stheraven        __n -= __bs;
2602227825Stheraven        __l -= __bs - 1;
2603227825Stheraven    }
2604227825Stheraven    return __r;
2605227825Stheraven}
2606227825Stheraven
2607227825Stheraven// move construct [__f, __l) to [__r, __r + (__l-__f)).
2608227825Stheraven// If __vt points into [__f, __l), then add (__r - __f) to __vt.
2609227825Stheraventemplate <class _Tp, class _Allocator>
2610227825Stheravenvoid
2611227825Stheravendeque<_Tp, _Allocator>::__move_construct_and_check(iterator __f, iterator __l,
2612227825Stheraven                                                   iterator __r, const_pointer& __vt)
2613227825Stheraven{
2614227825Stheraven    allocator_type& __a = __base::__alloc();
2615227825Stheraven    // as if
2616227825Stheraven    //   for (; __f != __l; ++__r, ++__f, ++__base::size())
2617227825Stheraven    //       __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__f));
2618227825Stheraven    difference_type __n = __l - __f;
2619227825Stheraven    while (__n > 0)
2620227825Stheraven    {
2621227825Stheraven        pointer __fb = __f.__ptr_;
2622227825Stheraven        pointer __fe = *__f.__m_iter_ + __base::__block_size;
2623227825Stheraven        difference_type __bs = __fe - __fb;
2624227825Stheraven        if (__bs > __n)
2625227825Stheraven        {
2626227825Stheraven            __bs = __n;
2627227825Stheraven            __fe = __fb + __bs;
2628227825Stheraven        }
2629227825Stheraven        if (__fb <= __vt && __vt < __fe)
2630253159Stheraven            __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) += __r - __f).__ptr_;
2631227825Stheraven        for (; __fb != __fe; ++__fb, ++__r, ++__base::size())
2632227825Stheraven            __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__fb));
2633227825Stheraven        __n -= __bs;
2634227825Stheraven        __f += __bs;
2635227825Stheraven    }
2636227825Stheraven}
2637227825Stheraven
2638227825Stheraven// move construct [__f, __l) to [__r - (__l-__f), __r) backwards.
2639227825Stheraven// If __vt points into [__f, __l), then subtract (__l - __r) from __vt.
2640227825Stheraventemplate <class _Tp, class _Allocator>
2641227825Stheravenvoid
2642227825Stheravendeque<_Tp, _Allocator>::__move_construct_backward_and_check(iterator __f, iterator __l,
2643227825Stheraven                                                            iterator __r, const_pointer& __vt)
2644227825Stheraven{
2645227825Stheraven    allocator_type& __a = __base::__alloc();
2646227825Stheraven    // as if
2647227825Stheraven    //   for (iterator __j = __l; __j != __f;)
2648227825Stheraven    //   {
2649227825Stheraven    //       __alloc_traitsconstruct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__j));
2650227825Stheraven    //       --__base::__start_;
2651227825Stheraven    //       ++__base::size();
2652227825Stheraven    //   }
2653227825Stheraven    difference_type __n = __l - __f;
2654227825Stheraven    while (__n > 0)
2655227825Stheraven    {
2656227825Stheraven        --__l;
2657227825Stheraven        pointer __lb = *__l.__m_iter_;
2658227825Stheraven        pointer __le = __l.__ptr_ + 1;
2659227825Stheraven        difference_type __bs = __le - __lb;
2660227825Stheraven        if (__bs > __n)
2661227825Stheraven        {
2662227825Stheraven            __bs = __n;
2663227825Stheraven            __lb = __le - __bs;
2664227825Stheraven        }
2665227825Stheraven        if (__lb <= __vt && __vt < __le)
2666253159Stheraven            __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) -= __l - __r + 1).__ptr_;
2667227825Stheraven        while (__le != __lb)
2668227825Stheraven        {
2669227825Stheraven            __alloc_traits::construct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__le));
2670227825Stheraven            --__base::__start_;
2671227825Stheraven            ++__base::size();
2672227825Stheraven        }
2673227825Stheraven        __n -= __bs;
2674227825Stheraven        __l -= __bs - 1;
2675227825Stheraven    }
2676227825Stheraven}
2677227825Stheraven
2678227825Stheraventemplate <class _Tp, class _Allocator>
2679227825Stheraventypename deque<_Tp, _Allocator>::iterator
2680227825Stheravendeque<_Tp, _Allocator>::erase(const_iterator __f)
2681227825Stheraven{
2682227825Stheraven    difference_type __n = 1;
2683227825Stheraven    iterator __b = __base::begin();
2684227825Stheraven    difference_type __pos = __f - __b;
2685227825Stheraven    iterator __p = __b + __pos;
2686227825Stheraven    allocator_type& __a = __base::__alloc();
2687227825Stheraven    if (__pos < (__base::size() - 1) / 2)
2688227825Stheraven    {   // erase from front
2689227825Stheraven        _VSTD::move_backward(__b, __p, _VSTD::next(__p));
2690227825Stheraven        __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
2691227825Stheraven        --__base::size();
2692227825Stheraven        ++__base::__start_;
2693227825Stheraven        if (__front_spare() >= 2 * __base::__block_size)
2694227825Stheraven        {
2695227825Stheraven            __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2696227825Stheraven            __base::__map_.pop_front();
2697227825Stheraven            __base::__start_ -= __base::__block_size;
2698227825Stheraven        }
2699227825Stheraven    }
2700227825Stheraven    else
2701227825Stheraven    {   // erase from back
2702227825Stheraven        iterator __i = _VSTD::move(_VSTD::next(__p), __base::end(), __p);
2703227825Stheraven        __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
2704227825Stheraven        --__base::size();
2705227825Stheraven        if (__back_spare() >= 2 * __base::__block_size)
2706227825Stheraven        {
2707227825Stheraven            __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2708227825Stheraven            __base::__map_.pop_back();
2709227825Stheraven        }
2710227825Stheraven    }
2711227825Stheraven    return __base::begin() + __pos;
2712227825Stheraven}
2713227825Stheraven
2714227825Stheraventemplate <class _Tp, class _Allocator>
2715227825Stheraventypename deque<_Tp, _Allocator>::iterator
2716227825Stheravendeque<_Tp, _Allocator>::erase(const_iterator __f, const_iterator __l)
2717227825Stheraven{
2718227825Stheraven    difference_type __n = __l - __f;
2719227825Stheraven    iterator __b = __base::begin();
2720227825Stheraven    difference_type __pos = __f - __b;
2721227825Stheraven    iterator __p = __b + __pos;
2722227825Stheraven    if (__n > 0)
2723227825Stheraven    {
2724227825Stheraven        allocator_type& __a = __base::__alloc();
2725227825Stheraven        if (__pos < (__base::size() - __n) / 2)
2726227825Stheraven        {   // erase from front
2727227825Stheraven            iterator __i = _VSTD::move_backward(__b, __p, __p + __n);
2728227825Stheraven            for (; __b != __i; ++__b)
2729227825Stheraven                __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
2730227825Stheraven            __base::size() -= __n;
2731227825Stheraven            __base::__start_ += __n;
2732227825Stheraven            while (__front_spare() >= 2 * __base::__block_size)
2733227825Stheraven            {
2734227825Stheraven                __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2735227825Stheraven                __base::__map_.pop_front();
2736227825Stheraven                __base::__start_ -= __base::__block_size;
2737227825Stheraven            }
2738227825Stheraven        }
2739227825Stheraven        else
2740227825Stheraven        {   // erase from back
2741227825Stheraven            iterator __i = _VSTD::move(__p + __n, __base::end(), __p);
2742227825Stheraven            for (iterator __e = __base::end(); __i != __e; ++__i)
2743227825Stheraven                __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
2744227825Stheraven            __base::size() -= __n;
2745227825Stheraven            while (__back_spare() >= 2 * __base::__block_size)
2746227825Stheraven            {
2747227825Stheraven                __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2748227825Stheraven                __base::__map_.pop_back();
2749227825Stheraven            }
2750227825Stheraven        }
2751227825Stheraven    }
2752227825Stheraven    return __base::begin() + __pos;
2753227825Stheraven}
2754227825Stheraven
2755227825Stheraventemplate <class _Tp, class _Allocator>
2756227825Stheravenvoid
2757227825Stheravendeque<_Tp, _Allocator>::__erase_to_end(const_iterator __f)
2758227825Stheraven{
2759227825Stheraven    iterator __e = __base::end();
2760227825Stheraven    difference_type __n = __e - __f;
2761227825Stheraven    if (__n > 0)
2762227825Stheraven    {
2763227825Stheraven        allocator_type& __a = __base::__alloc();
2764227825Stheraven        iterator __b = __base::begin();
2765227825Stheraven        difference_type __pos = __f - __b;
2766227825Stheraven        for (iterator __p = __b + __pos; __p != __e; ++__p)
2767227825Stheraven            __alloc_traits::destroy(__a, _VSTD::addressof(*__p));
2768227825Stheraven        __base::size() -= __n;
2769227825Stheraven        while (__back_spare() >= 2 * __base::__block_size)
2770227825Stheraven        {
2771227825Stheraven            __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2772227825Stheraven            __base::__map_.pop_back();
2773227825Stheraven        }
2774227825Stheraven    }
2775227825Stheraven}
2776227825Stheraven
2777227825Stheraventemplate <class _Tp, class _Allocator>
2778227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2779227825Stheravenvoid
2780227825Stheravendeque<_Tp, _Allocator>::swap(deque& __c)
2781227825Stheraven        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2782227825Stheraven                   __is_nothrow_swappable<allocator_type>::value)
2783227825Stheraven{
2784227825Stheraven    __base::swap(__c);
2785227825Stheraven}
2786227825Stheraven
2787227825Stheraventemplate <class _Tp, class _Allocator>
2788227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2789227825Stheravenvoid
2790227825Stheravendeque<_Tp, _Allocator>::clear() _NOEXCEPT
2791227825Stheraven{
2792227825Stheraven    __base::clear();
2793227825Stheraven}
2794227825Stheraven
2795227825Stheraventemplate <class _Tp, class _Allocator>
2796227825Stheraven_LIBCPP_INLINE_VISIBILITY inline
2797227825Stheravenbool
2798227825Stheravenoperator==(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2799227825Stheraven{
2800227825Stheraven    const typename deque<_Tp, _Allocator>::size_type __sz = __x.size();
2801227825Stheraven    return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
2802227825Stheraven}
2803227825Stheraven
2804227825Stheraventemplate <class _Tp, class _Allocator>
2805227825Stheraven_LIBCPP_INLINE_VISIBILITY inline
2806227825Stheravenbool
2807227825Stheravenoperator!=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2808227825Stheraven{
2809227825Stheraven    return !(__x == __y);
2810227825Stheraven}
2811227825Stheraven
2812227825Stheraventemplate <class _Tp, class _Allocator>
2813227825Stheraven_LIBCPP_INLINE_VISIBILITY inline
2814227825Stheravenbool
2815227825Stheravenoperator< (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2816227825Stheraven{
2817227825Stheraven    return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
2818227825Stheraven}
2819227825Stheraven
2820227825Stheraventemplate <class _Tp, class _Allocator>
2821227825Stheraven_LIBCPP_INLINE_VISIBILITY inline
2822227825Stheravenbool
2823227825Stheravenoperator> (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2824227825Stheraven{
2825227825Stheraven    return __y < __x;
2826227825Stheraven}
2827227825Stheraven
2828227825Stheraventemplate <class _Tp, class _Allocator>
2829227825Stheraven_LIBCPP_INLINE_VISIBILITY inline
2830227825Stheravenbool
2831227825Stheravenoperator>=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2832227825Stheraven{
2833227825Stheraven    return !(__x < __y);
2834227825Stheraven}
2835227825Stheraven
2836227825Stheraventemplate <class _Tp, class _Allocator>
2837227825Stheraven_LIBCPP_INLINE_VISIBILITY inline
2838227825Stheravenbool
2839227825Stheravenoperator<=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2840227825Stheraven{
2841227825Stheraven    return !(__y < __x);
2842227825Stheraven}
2843227825Stheraven
2844227825Stheraventemplate <class _Tp, class _Allocator>
2845227825Stheraven_LIBCPP_INLINE_VISIBILITY inline
2846227825Stheravenvoid
2847227825Stheravenswap(deque<_Tp, _Allocator>& __x, deque<_Tp, _Allocator>& __y)
2848227825Stheraven    _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2849227825Stheraven{
2850227825Stheraven    __x.swap(__y);
2851227825Stheraven}
2852227825Stheraven
2853227825Stheraven_LIBCPP_END_NAMESPACE_STD
2854227825Stheraven
2855227825Stheraven#endif  // _LIBCPP_DEQUE
2856