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