blob: 9c72d24c2893716f8220184d149855550bbbb251 [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:161// -*- C++ -*-
2//===---------------------------- deque -----------------------------------===//
3//
Howard Hinnant5b08a8a2010-05-11 21:36:014// The LLVM Compiler Infrastructure
Howard Hinnant3e519522010-05-11 19:42:165//
Howard Hinnant412dbeb2010-11-16 22:09:026// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnant3e519522010-05-11 19:42:168//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_DEQUE
12#define _LIBCPP_DEQUE
13
14/*
15 deque synopsis
16
17namespace std
18{
19
20template <class T, class Allocator = allocator<T> >
21class deque
22{
23public:
24 // types:
25 typedef T value_type;
26 typedef Allocator allocator_type;
27
28 typedef typename allocator_type::reference reference;
29 typedef typename allocator_type::const_reference const_reference;
30 typedef implementation-defined iterator;
31 typedef implementation-defined const_iterator;
32 typedef typename allocator_type::size_type size_type;
33 typedef typename allocator_type::difference_type difference_type;
34
35 typedef typename allocator_type::pointer pointer;
36 typedef typename allocator_type::const_pointer const_pointer;
37 typedef std::reverse_iterator<iterator> reverse_iterator;
38 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
39
40 // construct/copy/destroy:
Howard Hinnant80129112011-06-03 15:16:4941 deque() noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant3e519522010-05-11 19:42:1642 explicit deque(const allocator_type& a);
43 explicit deque(size_type n);
Marshall Clowf1b6d1b2013-09-09 18:19:4544 explicit deque(size_type n, const allocator_type& a); // C++14
Howard Hinnant3e519522010-05-11 19:42:1645 deque(size_type n, const value_type& v);
46 deque(size_type n, const value_type& v, const allocator_type& a);
47 template <class InputIterator>
48 deque(InputIterator f, InputIterator l);
49 template <class InputIterator>
50 deque(InputIterator f, InputIterator l, const allocator_type& a);
51 deque(const deque& c);
Howard Hinnantb58f59c2011-06-02 21:38:5752 deque(deque&& c)
53 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnant3e519522010-05-11 19:42:1654 deque(initializer_list<value_type> il, const Allocator& a = allocator_type());
55 deque(const deque& c, const allocator_type& a);
56 deque(deque&& c, const allocator_type& a);
57 ~deque();
58
59 deque& operator=(const deque& c);
Howard Hinnantb58f59c2011-06-02 21:38:5760 deque& operator=(deque&& c)
61 noexcept(
62 allocator_type::propagate_on_container_move_assignment::value &&
63 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant3e519522010-05-11 19:42:1664 deque& operator=(initializer_list<value_type> il);
65
66 template <class InputIterator>
67 void assign(InputIterator f, InputIterator l);
68 void assign(size_type n, const value_type& v);
69 void assign(initializer_list<value_type> il);
70
Howard Hinnanta87e8362011-06-02 16:10:2271 allocator_type get_allocator() const noexcept;
Howard Hinnant3e519522010-05-11 19:42:1672
73 // iterators:
74
Howard Hinnanta87e8362011-06-02 16:10:2275 iterator begin() noexcept;
76 const_iterator begin() const noexcept;
77 iterator end() noexcept;
78 const_iterator end() const noexcept;
Howard Hinnant3e519522010-05-11 19:42:1679
Howard Hinnanta87e8362011-06-02 16:10:2280 reverse_iterator rbegin() noexcept;
81 const_reverse_iterator rbegin() const noexcept;
82 reverse_iterator rend() noexcept;
83 const_reverse_iterator rend() const noexcept;
Howard Hinnant3e519522010-05-11 19:42:1684
Howard Hinnanta87e8362011-06-02 16:10:2285 const_iterator cbegin() const noexcept;
86 const_iterator cend() const noexcept;
87 const_reverse_iterator crbegin() const noexcept;
88 const_reverse_iterator crend() const noexcept;
Howard Hinnant3e519522010-05-11 19:42:1689
90 // capacity:
Howard Hinnanta87e8362011-06-02 16:10:2291 size_type size() const noexcept;
92 size_type max_size() const noexcept;
Howard Hinnant3e519522010-05-11 19:42:1693 void resize(size_type n);
94 void resize(size_type n, const value_type& v);
95 void shrink_to_fit();
Howard Hinnanta87e8362011-06-02 16:10:2296 bool empty() const noexcept;
Howard Hinnant3e519522010-05-11 19:42:1697
98 // element access:
99 reference operator[](size_type i);
100 const_reference operator[](size_type i) const;
101 reference at(size_type i);
102 const_reference at(size_type i) const;
103 reference front();
104 const_reference front() const;
105 reference back();
106 const_reference back() const;
107
108 // modifiers:
109 void push_front(const value_type& v);
110 void push_front(value_type&& v);
111 void push_back(const value_type& v);
112 void push_back(value_type&& v);
Eric Fiselier0e411642016-07-21 03:20:17113 template <class... Args> reference emplace_front(Args&&... args);
114 template <class... Args> reference emplace_back(Args&&... args);
Howard Hinnant3e519522010-05-11 19:42:16115 template <class... Args> iterator emplace(const_iterator p, Args&&... args);
116 iterator insert(const_iterator p, const value_type& v);
117 iterator insert(const_iterator p, value_type&& v);
118 iterator insert(const_iterator p, size_type n, const value_type& v);
119 template <class InputIterator>
Marshall Clowf15d7a52015-01-22 18:33:29120 iterator insert(const_iterator p, InputIterator f, InputIterator l);
Howard Hinnant3e519522010-05-11 19:42:16121 iterator insert(const_iterator p, initializer_list<value_type> il);
122 void pop_front();
123 void pop_back();
124 iterator erase(const_iterator p);
125 iterator erase(const_iterator f, const_iterator l);
Howard Hinnantb58f59c2011-06-02 21:38:57126 void swap(deque& c)
Marshall Clowe3fbe142015-07-13 20:04:56127 noexcept(allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnanta87e8362011-06-02 16:10:22128 void clear() noexcept;
Howard Hinnant3e519522010-05-11 19:42:16129};
130
131template <class T, class Allocator>
132 bool operator==(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
133template <class T, class Allocator>
134 bool operator< (const deque<T,Allocator>& x, const deque<T,Allocator>& y);
135template <class T, class Allocator>
136 bool operator!=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
137template <class T, class Allocator>
138 bool operator> (const deque<T,Allocator>& x, const deque<T,Allocator>& y);
139template <class T, class Allocator>
140 bool operator>=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
141template <class T, class Allocator>
142 bool operator<=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
143
144// specialized algorithms:
145template <class T, class Allocator>
Howard Hinnant45900102011-06-03 17:30:28146 void swap(deque<T,Allocator>& x, deque<T,Allocator>& y)
147 noexcept(noexcept(x.swap(y)));
Howard Hinnant3e519522010-05-11 19:42:16148
149} // std
150
151*/
152
Howard Hinnant073458b2011-10-17 20:05:10153#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnant3e519522010-05-11 19:42:16154#pragma GCC system_header
Howard Hinnant073458b2011-10-17 20:05:10155#endif
Howard Hinnant3e519522010-05-11 19:42:16156
157#include <__config>
158#include <__split_buffer>
159#include <type_traits>
160#include <initializer_list>
161#include <iterator>
162#include <algorithm>
163#include <stdexcept>
164
Howard Hinnantab4f4382011-11-29 16:45:27165#include <__undef_min_max>
166
Howard Hinnant3e519522010-05-11 19:42:16167_LIBCPP_BEGIN_NAMESPACE_STD
168
169template <class _Tp, class _Allocator> class __deque_base;
Marshall Clowb5d34aa2015-02-18 17:24:08170template <class _Tp, class _Allocator = allocator<_Tp> > class _LIBCPP_TYPE_VIS_ONLY deque;
Howard Hinnant3e519522010-05-11 19:42:16171
172template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
173 class _DiffType, _DiffType _BlockSize>
Howard Hinnantf0544c22013-08-12 18:38:34174class _LIBCPP_TYPE_VIS_ONLY __deque_iterator;
Howard Hinnant3e519522010-05-11 19:42:16175
176template <class _RAIter,
177 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
178__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
179copy(_RAIter __f,
180 _RAIter __l,
181 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
182 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
183
184template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
185 class _OutputIterator>
186_OutputIterator
187copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
188 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
189 _OutputIterator __r);
190
191template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
192 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
193__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
194copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
195 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
196 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
197
198template <class _RAIter,
199 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
200__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
201copy_backward(_RAIter __f,
202 _RAIter __l,
203 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
204 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
205
206template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
207 class _OutputIterator>
208_OutputIterator
209copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
210 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
211 _OutputIterator __r);
212
213template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
214 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
215__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
216copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
217 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
218 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
219
220template <class _RAIter,
221 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
222__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
223move(_RAIter __f,
224 _RAIter __l,
225 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
226 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
227
228template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
229 class _OutputIterator>
230_OutputIterator
231move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
232 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
233 _OutputIterator __r);
234
235template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
236 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
237__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
238move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
239 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
240 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
241
242template <class _RAIter,
243 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
244__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
245move_backward(_RAIter __f,
246 _RAIter __l,
247 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
248 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
249
250template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
251 class _OutputIterator>
252_OutputIterator
253move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
254 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
255 _OutputIterator __r);
256
257template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
258 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
259__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
260move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
261 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
262 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
263
Evgeniy Stepanov65da7bc2015-11-06 22:02:29264template <class _ValueType, class _DiffType>
265struct __deque_block_size {
266 static const _DiffType value = sizeof(_ValueType) < 256 ? 4096 / sizeof(_ValueType) : 16;
267};
268
Howard Hinnant3e519522010-05-11 19:42:16269template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
Evgeniy Stepanov65da7bc2015-11-06 22:02:29270 class _DiffType, _DiffType _BS =
271#ifdef _LIBCPP_ABI_INCOMPLETE_TYPES_IN_DEQUE
272// Keep template parameter to avoid changing all template declarations thoughout
273// this file.
274 0
275#else
276 __deque_block_size<_ValueType, _DiffType>::value
277#endif
278 >
Howard Hinnantf0544c22013-08-12 18:38:34279class _LIBCPP_TYPE_VIS_ONLY __deque_iterator
Howard Hinnant3e519522010-05-11 19:42:16280{
281 typedef _MapPointer __map_iterator;
282public:
283 typedef _Pointer pointer;
284 typedef _DiffType difference_type;
285private:
286 __map_iterator __m_iter_;
287 pointer __ptr_;
288
Evgeniy Stepanov65da7bc2015-11-06 22:02:29289 static const difference_type __block_size;
Howard Hinnant3e519522010-05-11 19:42:16290public:
291 typedef _ValueType value_type;
292 typedef random_access_iterator_tag iterator_category;
293 typedef _Reference reference;
294
Marshall Clow8fe0a372013-08-06 16:14:36295 _LIBCPP_INLINE_VISIBILITY __deque_iterator() _NOEXCEPT
296#if _LIBCPP_STD_VER > 11
297 : __m_iter_(nullptr), __ptr_(nullptr)
298#endif
299 {}
Howard Hinnant3e519522010-05-11 19:42:16300
Howard Hinnantc003db12011-11-29 18:15:50301 template <class _Pp, class _Rp, class _MP>
Howard Hinnant3e519522010-05-11 19:42:16302 _LIBCPP_INLINE_VISIBILITY
Evgeniy Stepanov65da7bc2015-11-06 22:02:29303 __deque_iterator(const __deque_iterator<value_type, _Pp, _Rp, _MP, difference_type, _BS>& __it,
Howard Hinnantc003db12011-11-29 18:15:50304 typename enable_if<is_convertible<_Pp, pointer>::value>::type* = 0) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16305 : __m_iter_(__it.__m_iter_), __ptr_(__it.__ptr_) {}
306
307 _LIBCPP_INLINE_VISIBILITY reference operator*() const {return *__ptr_;}
308 _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return __ptr_;}
309
310 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator++()
311 {
312 if (++__ptr_ - *__m_iter_ == __block_size)
313 {
314 ++__m_iter_;
315 __ptr_ = *__m_iter_;
316 }
317 return *this;
318 }
319
320 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator++(int)
321 {
322 __deque_iterator __tmp = *this;
323 ++(*this);
324 return __tmp;
325 }
326
327 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator--()
328 {
329 if (__ptr_ == *__m_iter_)
330 {
331 --__m_iter_;
332 __ptr_ = *__m_iter_ + __block_size;
333 }
334 --__ptr_;
335 return *this;
336 }
337
338 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator--(int)
339 {
340 __deque_iterator __tmp = *this;
341 --(*this);
342 return __tmp;
343 }
344
345 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator+=(difference_type __n)
346 {
347 if (__n != 0)
348 {
349 __n += __ptr_ - *__m_iter_;
350 if (__n > 0)
351 {
352 __m_iter_ += __n / __block_size;
353 __ptr_ = *__m_iter_ + __n % __block_size;
354 }
355 else // (__n < 0)
356 {
357 difference_type __z = __block_size - 1 - __n;
358 __m_iter_ -= __z / __block_size;
359 __ptr_ = *__m_iter_ + (__block_size - 1 - __z % __block_size);
360 }
361 }
362 return *this;
363 }
Howard Hinnantb3371f62010-08-22 00:02:43364
Howard Hinnant3e519522010-05-11 19:42:16365 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator-=(difference_type __n)
366 {
367 return *this += -__n;
368 }
Howard Hinnantb3371f62010-08-22 00:02:43369
Howard Hinnant3e519522010-05-11 19:42:16370 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator+(difference_type __n) const
371 {
372 __deque_iterator __t(*this);
373 __t += __n;
374 return __t;
375 }
Howard Hinnantb3371f62010-08-22 00:02:43376
Howard Hinnant3e519522010-05-11 19:42:16377 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator-(difference_type __n) const
378 {
379 __deque_iterator __t(*this);
380 __t -= __n;
381 return __t;
382 }
383
384 _LIBCPP_INLINE_VISIBILITY
385 friend __deque_iterator operator+(difference_type __n, const __deque_iterator& __it)
386 {return __it + __n;}
387
388 _LIBCPP_INLINE_VISIBILITY
389 friend difference_type operator-(const __deque_iterator& __x, const __deque_iterator& __y)
390 {
391 if (__x != __y)
392 return (__x.__m_iter_ - __y.__m_iter_) * __block_size
393 + (__x.__ptr_ - *__x.__m_iter_)
394 - (__y.__ptr_ - *__y.__m_iter_);
395 return 0;
396 }
397
398 _LIBCPP_INLINE_VISIBILITY reference operator[](difference_type __n) const
399 {return *(*this + __n);}
400
401 _LIBCPP_INLINE_VISIBILITY friend
402 bool operator==(const __deque_iterator& __x, const __deque_iterator& __y)
403 {return __x.__ptr_ == __y.__ptr_;}
404
405 _LIBCPP_INLINE_VISIBILITY friend
406 bool operator!=(const __deque_iterator& __x, const __deque_iterator& __y)
407 {return !(__x == __y);}
408
409 _LIBCPP_INLINE_VISIBILITY friend
410 bool operator<(const __deque_iterator& __x, const __deque_iterator& __y)
411 {return __x.__m_iter_ < __y.__m_iter_ ||
412 (__x.__m_iter_ == __y.__m_iter_ && __x.__ptr_ < __y.__ptr_);}
413
414 _LIBCPP_INLINE_VISIBILITY friend
415 bool operator>(const __deque_iterator& __x, const __deque_iterator& __y)
416 {return __y < __x;}
417
418 _LIBCPP_INLINE_VISIBILITY friend
419 bool operator<=(const __deque_iterator& __x, const __deque_iterator& __y)
420 {return !(__y < __x);}
421
422 _LIBCPP_INLINE_VISIBILITY friend
423 bool operator>=(const __deque_iterator& __x, const __deque_iterator& __y)
424 {return !(__x < __y);}
425
426private:
Howard Hinnanta87e8362011-06-02 16:10:22427 _LIBCPP_INLINE_VISIBILITY __deque_iterator(__map_iterator __m, pointer __p) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16428 : __m_iter_(__m), __ptr_(__p) {}
429
Howard Hinnantc003db12011-11-29 18:15:50430 template <class _Tp, class _Ap> friend class __deque_base;
Howard Hinnantf0544c22013-08-12 18:38:34431 template <class _Tp, class _Ap> friend class _LIBCPP_TYPE_VIS_ONLY deque;
Howard Hinnantc003db12011-11-29 18:15:50432 template <class _Vp, class _Pp, class _Rp, class _MP, class _Dp, _Dp>
Howard Hinnantf0544c22013-08-12 18:38:34433 friend class _LIBCPP_TYPE_VIS_ONLY __deque_iterator;
Howard Hinnant3e519522010-05-11 19:42:16434
435 template <class _RAIter,
436 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
437 friend
438 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
439 copy(_RAIter __f,
440 _RAIter __l,
441 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
442 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
443
444 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
445 class _OutputIterator>
446 friend
447 _OutputIterator
448 copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
449 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
450 _OutputIterator __r);
451
452 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
453 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
454 friend
455 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
456 copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
457 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
458 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
459
460 template <class _RAIter,
461 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
462 friend
463 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
464 copy_backward(_RAIter __f,
465 _RAIter __l,
466 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
467 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
468
469 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
470 class _OutputIterator>
471 friend
472 _OutputIterator
473 copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
474 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
475 _OutputIterator __r);
476
477 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
478 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
479 friend
480 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
481 copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
482 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
483 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
484
485 template <class _RAIter,
486 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
487 friend
488 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
489 move(_RAIter __f,
490 _RAIter __l,
491 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
492 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
493
494 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
495 class _OutputIterator>
496 friend
497 _OutputIterator
498 move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
499 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
500 _OutputIterator __r);
501
502 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
503 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
504 friend
505 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
506 move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
507 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
508 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
509
510 template <class _RAIter,
511 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
512 friend
513 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
514 move_backward(_RAIter __f,
515 _RAIter __l,
516 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
517 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
518
519 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
520 class _OutputIterator>
521 friend
522 _OutputIterator
523 move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
524 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
525 _OutputIterator __r);
526
527 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
528 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
529 friend
530 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
531 move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
532 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
533 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
534};
535
Evgeniy Stepanov65da7bc2015-11-06 22:02:29536template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
537 class _DiffType, _DiffType _BlockSize>
538const _DiffType __deque_iterator<_ValueType, _Pointer, _Reference, _MapPointer,
539 _DiffType, _BlockSize>::__block_size =
540 __deque_block_size<_ValueType, _DiffType>::value;
541
Howard Hinnant3e519522010-05-11 19:42:16542// copy
543
544template <class _RAIter,
545 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
546__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
547copy(_RAIter __f,
548 _RAIter __l,
549 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
550 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
551{
552 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
553 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
Evgeniy Stepanov65da7bc2015-11-06 22:02:29554 const difference_type __block_size = __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::__block_size;
Howard Hinnant3e519522010-05-11 19:42:16555 while (__f != __l)
556 {
557 pointer __rb = __r.__ptr_;
Evgeniy Stepanov65da7bc2015-11-06 22:02:29558 pointer __re = *__r.__m_iter_ + __block_size;
Howard Hinnant3e519522010-05-11 19:42:16559 difference_type __bs = __re - __rb;
560 difference_type __n = __l - __f;
561 _RAIter __m = __l;
562 if (__n > __bs)
563 {
564 __n = __bs;
565 __m = __f + __n;
566 }
Howard Hinnantce48a112011-06-30 21:18:19567 _VSTD::copy(__f, __m, __rb);
Howard Hinnant3e519522010-05-11 19:42:16568 __f = __m;
569 __r += __n;
570 }
571 return __r;
572}
573
574template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
575 class _OutputIterator>
576_OutputIterator
577copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
578 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
579 _OutputIterator __r)
580{
581 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
582 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
Evgeniy Stepanov65da7bc2015-11-06 22:02:29583 const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
Howard Hinnant3e519522010-05-11 19:42:16584 difference_type __n = __l - __f;
585 while (__n > 0)
586 {
587 pointer __fb = __f.__ptr_;
Evgeniy Stepanov65da7bc2015-11-06 22:02:29588 pointer __fe = *__f.__m_iter_ + __block_size;
Howard Hinnant3e519522010-05-11 19:42:16589 difference_type __bs = __fe - __fb;
590 if (__bs > __n)
591 {
592 __bs = __n;
593 __fe = __fb + __bs;
594 }
Howard Hinnantce48a112011-06-30 21:18:19595 __r = _VSTD::copy(__fb, __fe, __r);
Howard Hinnant3e519522010-05-11 19:42:16596 __n -= __bs;
597 __f += __bs;
598 }
599 return __r;
600}
601
602template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
603 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
604__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
605copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
606 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
607 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
608{
609 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
610 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
Evgeniy Stepanov65da7bc2015-11-06 22:02:29611 const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
Howard Hinnant3e519522010-05-11 19:42:16612 difference_type __n = __l - __f;
613 while (__n > 0)
614 {
615 pointer __fb = __f.__ptr_;
Evgeniy Stepanov65da7bc2015-11-06 22:02:29616 pointer __fe = *__f.__m_iter_ + __block_size;
Howard Hinnant3e519522010-05-11 19:42:16617 difference_type __bs = __fe - __fb;
618 if (__bs > __n)
619 {
620 __bs = __n;
621 __fe = __fb + __bs;
622 }
Howard Hinnantce48a112011-06-30 21:18:19623 __r = _VSTD::copy(__fb, __fe, __r);
Howard Hinnant3e519522010-05-11 19:42:16624 __n -= __bs;
625 __f += __bs;
626 }
627 return __r;
628}
629
630// copy_backward
631
632template <class _RAIter,
633 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
634__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
635copy_backward(_RAIter __f,
636 _RAIter __l,
637 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
638 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
639{
640 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
641 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
642 while (__f != __l)
643 {
Howard Hinnantce48a112011-06-30 21:18:19644 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _VSTD::prev(__r);
Howard Hinnant3e519522010-05-11 19:42:16645 pointer __rb = *__rp.__m_iter_;
646 pointer __re = __rp.__ptr_ + 1;
647 difference_type __bs = __re - __rb;
648 difference_type __n = __l - __f;
649 _RAIter __m = __f;
650 if (__n > __bs)
651 {
652 __n = __bs;
653 __m = __l - __n;
654 }
Howard Hinnantce48a112011-06-30 21:18:19655 _VSTD::copy_backward(__m, __l, __re);
Howard Hinnant3e519522010-05-11 19:42:16656 __l = __m;
657 __r -= __n;
658 }
659 return __r;
660}
661
662template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
663 class _OutputIterator>
664_OutputIterator
665copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
666 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
667 _OutputIterator __r)
668{
669 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
670 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
671 difference_type __n = __l - __f;
672 while (__n > 0)
673 {
674 --__l;
675 pointer __lb = *__l.__m_iter_;
676 pointer __le = __l.__ptr_ + 1;
677 difference_type __bs = __le - __lb;
678 if (__bs > __n)
679 {
680 __bs = __n;
681 __lb = __le - __bs;
682 }
Howard Hinnantce48a112011-06-30 21:18:19683 __r = _VSTD::copy_backward(__lb, __le, __r);
Howard Hinnant3e519522010-05-11 19:42:16684 __n -= __bs;
685 __l -= __bs - 1;
686 }
687 return __r;
688}
689
690template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
691 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
692__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
693copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
694 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
695 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
696{
697 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
698 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
699 difference_type __n = __l - __f;
700 while (__n > 0)
701 {
702 --__l;
703 pointer __lb = *__l.__m_iter_;
704 pointer __le = __l.__ptr_ + 1;
705 difference_type __bs = __le - __lb;
706 if (__bs > __n)
707 {
708 __bs = __n;
709 __lb = __le - __bs;
710 }
Howard Hinnantce48a112011-06-30 21:18:19711 __r = _VSTD::copy_backward(__lb, __le, __r);
Howard Hinnant3e519522010-05-11 19:42:16712 __n -= __bs;
713 __l -= __bs - 1;
714 }
715 return __r;
716}
717
718// move
719
720template <class _RAIter,
721 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
722__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
723move(_RAIter __f,
724 _RAIter __l,
725 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
726 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
727{
728 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
729 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
Evgeniy Stepanov65da7bc2015-11-06 22:02:29730 const difference_type __block_size = __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::__block_size;
Howard Hinnant3e519522010-05-11 19:42:16731 while (__f != __l)
732 {
733 pointer __rb = __r.__ptr_;
Evgeniy Stepanov65da7bc2015-11-06 22:02:29734 pointer __re = *__r.__m_iter_ + __block_size;
Howard Hinnant3e519522010-05-11 19:42:16735 difference_type __bs = __re - __rb;
736 difference_type __n = __l - __f;
737 _RAIter __m = __l;
738 if (__n > __bs)
739 {
740 __n = __bs;
741 __m = __f + __n;
742 }
Howard Hinnantce48a112011-06-30 21:18:19743 _VSTD::move(__f, __m, __rb);
Howard Hinnant3e519522010-05-11 19:42:16744 __f = __m;
745 __r += __n;
746 }
747 return __r;
748}
749
750template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
751 class _OutputIterator>
752_OutputIterator
753move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
754 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
755 _OutputIterator __r)
756{
757 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
758 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
Evgeniy Stepanov65da7bc2015-11-06 22:02:29759 const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
Howard Hinnant3e519522010-05-11 19:42:16760 difference_type __n = __l - __f;
761 while (__n > 0)
762 {
763 pointer __fb = __f.__ptr_;
Evgeniy Stepanov65da7bc2015-11-06 22:02:29764 pointer __fe = *__f.__m_iter_ + __block_size;
Howard Hinnant3e519522010-05-11 19:42:16765 difference_type __bs = __fe - __fb;
766 if (__bs > __n)
767 {
768 __bs = __n;
769 __fe = __fb + __bs;
770 }
Howard Hinnantce48a112011-06-30 21:18:19771 __r = _VSTD::move(__fb, __fe, __r);
Howard Hinnant3e519522010-05-11 19:42:16772 __n -= __bs;
773 __f += __bs;
774 }
775 return __r;
776}
777
778template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
779 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
780__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
781move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
782 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
783 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
784{
785 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
786 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
Evgeniy Stepanov65da7bc2015-11-06 22:02:29787 const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
Howard Hinnant3e519522010-05-11 19:42:16788 difference_type __n = __l - __f;
789 while (__n > 0)
790 {
791 pointer __fb = __f.__ptr_;
Evgeniy Stepanov65da7bc2015-11-06 22:02:29792 pointer __fe = *__f.__m_iter_ + __block_size;
Howard Hinnant3e519522010-05-11 19:42:16793 difference_type __bs = __fe - __fb;
794 if (__bs > __n)
795 {
796 __bs = __n;
797 __fe = __fb + __bs;
798 }
Howard Hinnantce48a112011-06-30 21:18:19799 __r = _VSTD::move(__fb, __fe, __r);
Howard Hinnant3e519522010-05-11 19:42:16800 __n -= __bs;
801 __f += __bs;
802 }
803 return __r;
804}
805
806// move_backward
807
808template <class _RAIter,
809 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
810__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
811move_backward(_RAIter __f,
812 _RAIter __l,
813 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
814 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
815{
816 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
817 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
818 while (__f != __l)
819 {
Howard Hinnantce48a112011-06-30 21:18:19820 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _VSTD::prev(__r);
Howard Hinnant3e519522010-05-11 19:42:16821 pointer __rb = *__rp.__m_iter_;
822 pointer __re = __rp.__ptr_ + 1;
823 difference_type __bs = __re - __rb;
824 difference_type __n = __l - __f;
825 _RAIter __m = __f;
826 if (__n > __bs)
827 {
828 __n = __bs;
829 __m = __l - __n;
830 }
Howard Hinnantce48a112011-06-30 21:18:19831 _VSTD::move_backward(__m, __l, __re);
Howard Hinnant3e519522010-05-11 19:42:16832 __l = __m;
833 __r -= __n;
834 }
835 return __r;
836}
837
838template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
839 class _OutputIterator>
840_OutputIterator
841move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
842 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
843 _OutputIterator __r)
844{
845 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
846 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
847 difference_type __n = __l - __f;
848 while (__n > 0)
849 {
850 --__l;
851 pointer __lb = *__l.__m_iter_;
852 pointer __le = __l.__ptr_ + 1;
853 difference_type __bs = __le - __lb;
854 if (__bs > __n)
855 {
856 __bs = __n;
857 __lb = __le - __bs;
858 }
Howard Hinnantce48a112011-06-30 21:18:19859 __r = _VSTD::move_backward(__lb, __le, __r);
Howard Hinnant3e519522010-05-11 19:42:16860 __n -= __bs;
861 __l -= __bs - 1;
862 }
863 return __r;
864}
865
866template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
867 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
868__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
869move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
870 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
871 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
872{
873 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
874 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
875 difference_type __n = __l - __f;
876 while (__n > 0)
877 {
878 --__l;
879 pointer __lb = *__l.__m_iter_;
880 pointer __le = __l.__ptr_ + 1;
881 difference_type __bs = __le - __lb;
882 if (__bs > __n)
883 {
884 __bs = __n;
885 __lb = __le - __bs;
886 }
Howard Hinnantce48a112011-06-30 21:18:19887 __r = _VSTD::move_backward(__lb, __le, __r);
Howard Hinnant3e519522010-05-11 19:42:16888 __n -= __bs;
889 __l -= __bs - 1;
890 }
891 return __r;
892}
893
894template <bool>
895class __deque_base_common
896{
897protected:
Marshall Clowd437fa52016-08-25 15:09:01898 _LIBCPP_NORETURN void __throw_length_error() const;
899 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnant3e519522010-05-11 19:42:16900};
901
902template <bool __b>
903void
904__deque_base_common<__b>::__throw_length_error() const
905{
Marshall Clowd437fa52016-08-25 15:09:01906 _VSTD::__throw_length_error("deque");
Howard Hinnant3e519522010-05-11 19:42:16907}
908
909template <bool __b>
910void
911__deque_base_common<__b>::__throw_out_of_range() const
912{
Marshall Clowd437fa52016-08-25 15:09:01913 _VSTD::__throw_out_of_range("deque");
Howard Hinnant3e519522010-05-11 19:42:16914}
915
916template <class _Tp, class _Allocator>
917class __deque_base
918 : protected __deque_base_common<true>
919{
920 __deque_base(const __deque_base& __c);
921 __deque_base& operator=(const __deque_base& __c);
922protected:
923 typedef _Tp value_type;
924 typedef _Allocator allocator_type;
925 typedef allocator_traits<allocator_type> __alloc_traits;
926 typedef value_type& reference;
927 typedef const value_type& const_reference;
928 typedef typename __alloc_traits::size_type size_type;
929 typedef typename __alloc_traits::difference_type difference_type;
930 typedef typename __alloc_traits::pointer pointer;
931 typedef typename __alloc_traits::const_pointer const_pointer;
932
Evgeniy Stepanov65da7bc2015-11-06 22:02:29933 static const difference_type __block_size;
Howard Hinnant3e519522010-05-11 19:42:16934
Marshall Clow1f508012015-04-07 05:21:38935 typedef typename __rebind_alloc_helper<__alloc_traits, pointer>::type __pointer_allocator;
Howard Hinnant3e519522010-05-11 19:42:16936 typedef allocator_traits<__pointer_allocator> __map_traits;
937 typedef typename __map_traits::pointer __map_pointer;
Marshall Clow1f508012015-04-07 05:21:38938 typedef typename __rebind_alloc_helper<__alloc_traits, const_pointer>::type __const_pointer_allocator;
Howard Hinnant14e200d2013-06-23 21:17:24939 typedef typename allocator_traits<__const_pointer_allocator>::const_pointer __map_const_pointer;
Howard Hinnant3e519522010-05-11 19:42:16940 typedef __split_buffer<pointer, __pointer_allocator> __map;
941
942 typedef __deque_iterator<value_type, pointer, reference, __map_pointer,
Evgeniy Stepanov65da7bc2015-11-06 22:02:29943 difference_type> iterator;
Howard Hinnant3e519522010-05-11 19:42:16944 typedef __deque_iterator<value_type, const_pointer, const_reference, __map_const_pointer,
Evgeniy Stepanov65da7bc2015-11-06 22:02:29945 difference_type> const_iterator;
Howard Hinnant3e519522010-05-11 19:42:16946
947 __map __map_;
948 size_type __start_;
949 __compressed_pair<size_type, allocator_type> __size_;
950
Howard Hinnanta87e8362011-06-02 16:10:22951 iterator begin() _NOEXCEPT;
952 const_iterator begin() const _NOEXCEPT;
953 iterator end() _NOEXCEPT;
954 const_iterator end() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16955
956 _LIBCPP_INLINE_VISIBILITY size_type& size() {return __size_.first();}
Howard Hinnanta87e8362011-06-02 16:10:22957 _LIBCPP_INLINE_VISIBILITY
958 const size_type& size() const _NOEXCEPT {return __size_.first();}
Howard Hinnant3e519522010-05-11 19:42:16959 _LIBCPP_INLINE_VISIBILITY allocator_type& __alloc() {return __size_.second();}
Howard Hinnanta87e8362011-06-02 16:10:22960 _LIBCPP_INLINE_VISIBILITY
961 const allocator_type& __alloc() const _NOEXCEPT {return __size_.second();}
Howard Hinnant3e519522010-05-11 19:42:16962
Evgeniy Stepanov906c8722015-11-07 01:22:13963 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant80129112011-06-03 15:16:49964 __deque_base()
965 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Evgeniy Stepanov906c8722015-11-07 01:22:13966 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16967 explicit __deque_base(const allocator_type& __a);
Howard Hinnant9eebe112011-06-02 20:00:14968public:
Howard Hinnant3e519522010-05-11 19:42:16969 ~__deque_base();
970
Howard Hinnant7609c9b2010-09-04 23:28:19971#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16972
Howard Hinnant9eebe112011-06-02 20:00:14973 __deque_base(__deque_base&& __c)
974 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnant3e519522010-05-11 19:42:16975 __deque_base(__deque_base&& __c, const allocator_type& __a);
976
Howard Hinnant7609c9b2010-09-04 23:28:19977#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant9eebe112011-06-02 20:00:14978 void swap(__deque_base& __c)
Marshall Clowe3fbe142015-07-13 20:04:56979#if _LIBCPP_STD_VER >= 14
980 _NOEXCEPT;
981#else
982 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
983 __is_nothrow_swappable<allocator_type>::value);
984#endif
Howard Hinnant9eebe112011-06-02 20:00:14985protected:
Howard Hinnanta87e8362011-06-02 16:10:22986 void clear() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16987
988 bool __invariants() const;
989
Howard Hinnantfb100022010-09-21 21:28:23990 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16991 void __move_assign(__deque_base& __c)
Howard Hinnantb58f59c2011-06-02 21:38:57992 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
993 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnant3e519522010-05-11 19:42:16994 {
Howard Hinnantce48a112011-06-30 21:18:19995 __map_ = _VSTD::move(__c.__map_);
Howard Hinnant3e519522010-05-11 19:42:16996 __start_ = __c.__start_;
997 size() = __c.size();
998 __move_assign_alloc(__c);
999 __c.__start_ = __c.size() = 0;
1000 }
1001
Howard Hinnantfb100022010-09-21 21:28:231002 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161003 void __move_assign_alloc(__deque_base& __c)
Howard Hinnant9eebe112011-06-02 20:00:141004 _NOEXCEPT_(!__alloc_traits::propagate_on_container_move_assignment::value ||
1005 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnant3e519522010-05-11 19:42:161006 {__move_assign_alloc(__c, integral_constant<bool,
1007 __alloc_traits::propagate_on_container_move_assignment::value>());}
1008
1009private:
Howard Hinnantfb100022010-09-21 21:28:231010 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86681392011-09-02 20:42:311011 void __move_assign_alloc(__deque_base& __c, true_type)
Howard Hinnant9eebe112011-06-02 20:00:141012 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnant3e519522010-05-11 19:42:161013 {
Howard Hinnantce48a112011-06-30 21:18:191014 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnant3e519522010-05-11 19:42:161015 }
1016
Howard Hinnantfb100022010-09-21 21:28:231017 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2063662011-12-01 20:21:041018 void __move_assign_alloc(__deque_base&, false_type) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161019 {}
Howard Hinnant3e519522010-05-11 19:42:161020};
1021
1022template <class _Tp, class _Allocator>
Evgeniy Stepanov65da7bc2015-11-06 22:02:291023const typename __deque_base<_Tp, _Allocator>::difference_type
1024 __deque_base<_Tp, _Allocator>::__block_size =
1025 __deque_block_size<value_type, difference_type>::value;
1026
1027template <class _Tp, class _Allocator>
Howard Hinnant3e519522010-05-11 19:42:161028bool
1029__deque_base<_Tp, _Allocator>::__invariants() const
1030{
1031 if (!__map_.__invariants())
1032 return false;
1033 if (__map_.size() >= size_type(-1) / __block_size)
1034 return false;
1035 for (typename __map::const_iterator __i = __map_.begin(), __e = __map_.end();
1036 __i != __e; ++__i)
1037 if (*__i == nullptr)
1038 return false;
1039 if (__map_.size() != 0)
1040 {
1041 if (size() >= __map_.size() * __block_size)
1042 return false;
1043 if (__start_ >= __map_.size() * __block_size - size())
1044 return false;
1045 }
1046 else
1047 {
1048 if (size() != 0)
1049 return false;
1050 if (__start_ != 0)
1051 return false;
1052 }
1053 return true;
1054}
1055
1056template <class _Tp, class _Allocator>
1057typename __deque_base<_Tp, _Allocator>::iterator
Howard Hinnanta87e8362011-06-02 16:10:221058__deque_base<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161059{
1060 __map_pointer __mp = __map_.begin() + __start_ / __block_size;
1061 return iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
1062}
1063
1064template <class _Tp, class _Allocator>
1065typename __deque_base<_Tp, _Allocator>::const_iterator
Howard Hinnanta87e8362011-06-02 16:10:221066__deque_base<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161067{
Howard Hinnant14e200d2013-06-23 21:17:241068 __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __start_ / __block_size);
Howard Hinnant3e519522010-05-11 19:42:161069 return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
1070}
1071
1072template <class _Tp, class _Allocator>
1073typename __deque_base<_Tp, _Allocator>::iterator
Howard Hinnanta87e8362011-06-02 16:10:221074__deque_base<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161075{
1076 size_type __p = size() + __start_;
1077 __map_pointer __mp = __map_.begin() + __p / __block_size;
1078 return iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
1079}
1080
1081template <class _Tp, class _Allocator>
1082typename __deque_base<_Tp, _Allocator>::const_iterator
Howard Hinnanta87e8362011-06-02 16:10:221083__deque_base<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161084{
1085 size_type __p = size() + __start_;
Howard Hinnant14e200d2013-06-23 21:17:241086 __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __p / __block_size);
Howard Hinnant3e519522010-05-11 19:42:161087 return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
1088}
1089
1090template <class _Tp, class _Allocator>
Evgeniy Stepanov906c8722015-11-07 01:22:131091inline
Howard Hinnant3e519522010-05-11 19:42:161092__deque_base<_Tp, _Allocator>::__deque_base()
Howard Hinnant80129112011-06-03 15:16:491093 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant3e519522010-05-11 19:42:161094 : __start_(0), __size_(0) {}
1095
1096template <class _Tp, class _Allocator>
Evgeniy Stepanov906c8722015-11-07 01:22:131097inline
Howard Hinnant3e519522010-05-11 19:42:161098__deque_base<_Tp, _Allocator>::__deque_base(const allocator_type& __a)
1099 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a) {}
1100
1101template <class _Tp, class _Allocator>
1102__deque_base<_Tp, _Allocator>::~__deque_base()
1103{
1104 clear();
1105 typename __map::iterator __i = __map_.begin();
1106 typename __map::iterator __e = __map_.end();
1107 for (; __i != __e; ++__i)
1108 __alloc_traits::deallocate(__alloc(), *__i, __block_size);
1109}
1110
Howard Hinnant7609c9b2010-09-04 23:28:191111#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:161112
1113template <class _Tp, class _Allocator>
1114__deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c)
Howard Hinnant9eebe112011-06-02 20:00:141115 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantce48a112011-06-30 21:18:191116 : __map_(_VSTD::move(__c.__map_)),
1117 __start_(_VSTD::move(__c.__start_)),
1118 __size_(_VSTD::move(__c.__size_))
Howard Hinnant3e519522010-05-11 19:42:161119{
1120 __c.__start_ = 0;
1121 __c.size() = 0;
1122}
1123
1124template <class _Tp, class _Allocator>
1125__deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c, const allocator_type& __a)
Howard Hinnantce48a112011-06-30 21:18:191126 : __map_(_VSTD::move(__c.__map_), __pointer_allocator(__a)),
1127 __start_(_VSTD::move(__c.__start_)),
1128 __size_(_VSTD::move(__c.size()), __a)
Howard Hinnant3e519522010-05-11 19:42:161129{
1130 if (__a == __c.__alloc())
1131 {
1132 __c.__start_ = 0;
1133 __c.size() = 0;
1134 }
1135 else
1136 {
1137 __map_.clear();
1138 __start_ = 0;
1139 size() = 0;
1140 }
1141}
1142
Howard Hinnant7609c9b2010-09-04 23:28:191143#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:161144
1145template <class _Tp, class _Allocator>
1146void
1147__deque_base<_Tp, _Allocator>::swap(__deque_base& __c)
Marshall Clowe3fbe142015-07-13 20:04:561148#if _LIBCPP_STD_VER >= 14
1149 _NOEXCEPT
1150#else
1151 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1152 __is_nothrow_swappable<allocator_type>::value)
1153#endif
Howard Hinnant3e519522010-05-11 19:42:161154{
1155 __map_.swap(__c.__map_);
Howard Hinnantce48a112011-06-30 21:18:191156 _VSTD::swap(__start_, __c.__start_);
1157 _VSTD::swap(size(), __c.size());
Marshall Clowe3fbe142015-07-13 20:04:561158 __swap_allocator(__alloc(), __c.__alloc());
Howard Hinnant3e519522010-05-11 19:42:161159}
1160
1161template <class _Tp, class _Allocator>
1162void
Howard Hinnanta87e8362011-06-02 16:10:221163__deque_base<_Tp, _Allocator>::clear() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161164{
1165 allocator_type& __a = __alloc();
1166 for (iterator __i = begin(), __e = end(); __i != __e; ++__i)
Howard Hinnantce48a112011-06-30 21:18:191167 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnant3e519522010-05-11 19:42:161168 size() = 0;
1169 while (__map_.size() > 2)
1170 {
1171 __alloc_traits::deallocate(__a, __map_.front(), __block_size);
1172 __map_.pop_front();
1173 }
1174 switch (__map_.size())
1175 {
1176 case 1:
1177 __start_ = __block_size / 2;
1178 break;
1179 case 2:
1180 __start_ = __block_size;
1181 break;
1182 }
1183}
1184
Marshall Clowb5d34aa2015-02-18 17:24:081185template <class _Tp, class _Allocator /*= allocator<_Tp>*/>
Howard Hinnantf0544c22013-08-12 18:38:341186class _LIBCPP_TYPE_VIS_ONLY deque
Howard Hinnant3e519522010-05-11 19:42:161187 : private __deque_base<_Tp, _Allocator>
1188{
1189public:
1190 // types:
Howard Hinnantb3371f62010-08-22 00:02:431191
Howard Hinnant3e519522010-05-11 19:42:161192 typedef _Tp value_type;
1193 typedef _Allocator allocator_type;
1194
Marshall Clow94f89ae2015-11-26 01:24:041195 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
1196 "Allocator::value_type must be same type as value_type");
1197
Howard Hinnant3e519522010-05-11 19:42:161198 typedef __deque_base<value_type, allocator_type> __base;
1199
1200 typedef typename __base::__alloc_traits __alloc_traits;
1201 typedef typename __base::reference reference;
1202 typedef typename __base::const_reference const_reference;
1203 typedef typename __base::iterator iterator;
1204 typedef typename __base::const_iterator const_iterator;
1205 typedef typename __base::size_type size_type;
1206 typedef typename __base::difference_type difference_type;
1207
1208 typedef typename __base::pointer pointer;
1209 typedef typename __base::const_pointer const_pointer;
Howard Hinnantce48a112011-06-30 21:18:191210 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
1211 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnant3e519522010-05-11 19:42:161212
1213 // construct/copy/destroy:
Howard Hinnant80129112011-06-03 15:16:491214 _LIBCPP_INLINE_VISIBILITY
1215 deque()
1216 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
1217 {}
Marshall Clow78a87e82014-03-05 19:06:201218 _LIBCPP_INLINE_VISIBILITY explicit deque(const allocator_type& __a) : __base(__a) {}
Howard Hinnant3e519522010-05-11 19:42:161219 explicit deque(size_type __n);
Marshall Clow630c5e52013-09-07 16:16:191220#if _LIBCPP_STD_VER > 11
1221 explicit deque(size_type __n, const _Allocator& __a);
1222#endif
Howard Hinnant3e519522010-05-11 19:42:161223 deque(size_type __n, const value_type& __v);
1224 deque(size_type __n, const value_type& __v, const allocator_type& __a);
1225 template <class _InputIter>
1226 deque(_InputIter __f, _InputIter __l,
1227 typename enable_if<__is_input_iterator<_InputIter>::value>::type* = 0);
1228 template <class _InputIter>
1229 deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
1230 typename enable_if<__is_input_iterator<_InputIter>::value>::type* = 0);
1231 deque(const deque& __c);
1232 deque(const deque& __c, const allocator_type& __a);
Howard Hinnant54976f22011-08-12 21:56:021233#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:161234 deque(initializer_list<value_type> __il);
1235 deque(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnant54976f22011-08-12 21:56:021236#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:161237
1238 deque& operator=(const deque& __c);
Howard Hinnant54976f22011-08-12 21:56:021239#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantfb100022010-09-21 21:28:231240 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161241 deque& operator=(initializer_list<value_type> __il) {assign(__il); return *this;}
Howard Hinnant54976f22011-08-12 21:56:021242#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:161243
Howard Hinnant7609c9b2010-09-04 23:28:191244#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov906c8722015-11-07 01:22:131245 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9eebe112011-06-02 20:00:141246 deque(deque&& __c) _NOEXCEPT_(is_nothrow_move_constructible<__base>::value);
Evgeniy Stepanov906c8722015-11-07 01:22:131247 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161248 deque(deque&& __c, const allocator_type& __a);
Evgeniy Stepanov906c8722015-11-07 01:22:131249 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9eebe112011-06-02 20:00:141250 deque& operator=(deque&& __c)
Howard Hinnantb58f59c2011-06-02 21:38:571251 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1252 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant7609c9b2010-09-04 23:28:191253#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:161254
1255 template <class _InputIter>
1256 void assign(_InputIter __f, _InputIter __l,
1257 typename enable_if<__is_input_iterator<_InputIter>::value &&
1258 !__is_random_access_iterator<_InputIter>::value>::type* = 0);
1259 template <class _RAIter>
1260 void assign(_RAIter __f, _RAIter __l,
1261 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
1262 void assign(size_type __n, const value_type& __v);
Howard Hinnant54976f22011-08-12 21:56:021263#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantfb100022010-09-21 21:28:231264 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161265 void assign(initializer_list<value_type> __il) {assign(__il.begin(), __il.end());}
Howard Hinnant54976f22011-08-12 21:56:021266#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:161267
Evgeniy Stepanov906c8722015-11-07 01:22:131268 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta87e8362011-06-02 16:10:221269 allocator_type get_allocator() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:161270
1271 // iterators:
1272
Howard Hinnantfb100022010-09-21 21:28:231273 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta87e8362011-06-02 16:10:221274 iterator begin() _NOEXCEPT {return __base::begin();}
Howard Hinnantfb100022010-09-21 21:28:231275 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta87e8362011-06-02 16:10:221276 const_iterator begin() const _NOEXCEPT {return __base::begin();}
Howard Hinnantfb100022010-09-21 21:28:231277 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta87e8362011-06-02 16:10:221278 iterator end() _NOEXCEPT {return __base::end();}
Howard Hinnantfb100022010-09-21 21:28:231279 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta87e8362011-06-02 16:10:221280 const_iterator end() const _NOEXCEPT {return __base::end();}
Howard Hinnant3e519522010-05-11 19:42:161281
Howard Hinnantfb100022010-09-21 21:28:231282 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta87e8362011-06-02 16:10:221283 reverse_iterator rbegin() _NOEXCEPT
1284 {return reverse_iterator(__base::end());}
Howard Hinnantfb100022010-09-21 21:28:231285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta87e8362011-06-02 16:10:221286 const_reverse_iterator rbegin() const _NOEXCEPT
1287 {return const_reverse_iterator(__base::end());}
Howard Hinnantfb100022010-09-21 21:28:231288 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta87e8362011-06-02 16:10:221289 reverse_iterator rend() _NOEXCEPT
1290 {return reverse_iterator(__base::begin());}
Howard Hinnantfb100022010-09-21 21:28:231291 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta87e8362011-06-02 16:10:221292 const_reverse_iterator rend() const _NOEXCEPT
1293 {return const_reverse_iterator(__base::begin());}
Howard Hinnant3e519522010-05-11 19:42:161294
Howard Hinnantfb100022010-09-21 21:28:231295 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta87e8362011-06-02 16:10:221296 const_iterator cbegin() const _NOEXCEPT
1297 {return __base::begin();}
Howard Hinnantfb100022010-09-21 21:28:231298 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta87e8362011-06-02 16:10:221299 const_iterator cend() const _NOEXCEPT
1300 {return __base::end();}
Howard Hinnantfb100022010-09-21 21:28:231301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta87e8362011-06-02 16:10:221302 const_reverse_iterator crbegin() const _NOEXCEPT
1303 {return const_reverse_iterator(__base::end());}
Howard Hinnantfb100022010-09-21 21:28:231304 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta87e8362011-06-02 16:10:221305 const_reverse_iterator crend() const _NOEXCEPT
1306 {return const_reverse_iterator(__base::begin());}
Howard Hinnant3e519522010-05-11 19:42:161307
1308 // capacity:
Howard Hinnantfb100022010-09-21 21:28:231309 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta87e8362011-06-02 16:10:221310 size_type size() const _NOEXCEPT {return __base::size();}
1311 _LIBCPP_INLINE_VISIBILITY
1312 size_type max_size() const _NOEXCEPT
1313 {return __alloc_traits::max_size(__base::__alloc());}
Howard Hinnant3e519522010-05-11 19:42:161314 void resize(size_type __n);
1315 void resize(size_type __n, const value_type& __v);
Howard Hinnantb58f59c2011-06-02 21:38:571316 void shrink_to_fit() _NOEXCEPT;
Howard Hinnanta87e8362011-06-02 16:10:221317 _LIBCPP_INLINE_VISIBILITY
1318 bool empty() const _NOEXCEPT {return __base::size() == 0;}
Howard Hinnant3e519522010-05-11 19:42:161319
1320 // element access:
Evgeniy Stepanov906c8722015-11-07 01:22:131321 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161322 reference operator[](size_type __i);
Evgeniy Stepanov906c8722015-11-07 01:22:131323 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161324 const_reference operator[](size_type __i) const;
Evgeniy Stepanov906c8722015-11-07 01:22:131325 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161326 reference at(size_type __i);
Evgeniy Stepanov906c8722015-11-07 01:22:131327 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161328 const_reference at(size_type __i) const;
Evgeniy Stepanov906c8722015-11-07 01:22:131329 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161330 reference front();
Evgeniy Stepanov906c8722015-11-07 01:22:131331 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161332 const_reference front() const;
Evgeniy Stepanov906c8722015-11-07 01:22:131333 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161334 reference back();
Evgeniy Stepanov906c8722015-11-07 01:22:131335 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161336 const_reference back() const;
1337
1338 // 23.2.2.3 modifiers:
1339 void push_front(const value_type& __v);
1340 void push_back(const value_type& __v);
Howard Hinnant7609c9b2010-09-04 23:28:191341#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1342#ifndef _LIBCPP_HAS_NO_VARIADICS
Eric Fiselier0e411642016-07-21 03:20:171343 template <class... _Args> reference emplace_front(_Args&&... __args);
1344 template <class... _Args> reference emplace_back(_Args&&... __args);
Howard Hinnant3e519522010-05-11 19:42:161345 template <class... _Args> iterator emplace(const_iterator __p, _Args&&... __args);
Howard Hinnant7609c9b2010-09-04 23:28:191346#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant3e519522010-05-11 19:42:161347 void push_front(value_type&& __v);
1348 void push_back(value_type&& __v);
1349 iterator insert(const_iterator __p, value_type&& __v);
Howard Hinnant7609c9b2010-09-04 23:28:191350#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:161351 iterator insert(const_iterator __p, const value_type& __v);
1352 iterator insert(const_iterator __p, size_type __n, const value_type& __v);
1353 template <class _InputIter>
Marshall Clowf15d7a52015-01-22 18:33:291354 iterator insert(const_iterator __p, _InputIter __f, _InputIter __l,
Howard Hinnant3e519522010-05-11 19:42:161355 typename enable_if<__is_input_iterator<_InputIter>::value
Marshall Clowf15d7a52015-01-22 18:33:291356 &&!__is_forward_iterator<_InputIter>::value>::type* = 0);
1357 template <class _ForwardIterator>
1358 iterator insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l,
1359 typename enable_if<__is_forward_iterator<_ForwardIterator>::value
1360 &&!__is_bidirectional_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnant3e519522010-05-11 19:42:161361 template <class _BiIter>
Marshall Clowf15d7a52015-01-22 18:33:291362 iterator insert(const_iterator __p, _BiIter __f, _BiIter __l,
Howard Hinnant3e519522010-05-11 19:42:161363 typename enable_if<__is_bidirectional_iterator<_BiIter>::value>::type* = 0);
Howard Hinnant54976f22011-08-12 21:56:021364#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantfb100022010-09-21 21:28:231365 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161366 iterator insert(const_iterator __p, initializer_list<value_type> __il)
1367 {return insert(__p, __il.begin(), __il.end());}
Howard Hinnant54976f22011-08-12 21:56:021368#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant3e519522010-05-11 19:42:161369 void pop_front();
1370 void pop_back();
1371 iterator erase(const_iterator __p);
1372 iterator erase(const_iterator __f, const_iterator __l);
1373
Evgeniy Stepanov906c8722015-11-07 01:22:131374 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9eebe112011-06-02 20:00:141375 void swap(deque& __c)
Marshall Clowe3fbe142015-07-13 20:04:561376#if _LIBCPP_STD_VER >= 14
1377 _NOEXCEPT;
1378#else
Howard Hinnant9eebe112011-06-02 20:00:141379 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1380 __is_nothrow_swappable<allocator_type>::value);
Marshall Clowe3fbe142015-07-13 20:04:561381#endif
Evgeniy Stepanov906c8722015-11-07 01:22:131382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta87e8362011-06-02 16:10:221383 void clear() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:161384
Howard Hinnantfb100022010-09-21 21:28:231385 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161386 bool __invariants() const {return __base::__invariants();}
1387private:
Howard Hinnant14e200d2013-06-23 21:17:241388 typedef typename __base::__map_const_pointer __map_const_pointer;
1389
Howard Hinnantfb100022010-09-21 21:28:231390 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161391 static size_type __recommend_blocks(size_type __n)
1392 {
1393 return __n / __base::__block_size + (__n % __base::__block_size != 0);
1394 }
Howard Hinnantfb100022010-09-21 21:28:231395 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161396 size_type __capacity() const
1397 {
1398 return __base::__map_.size() == 0 ? 0 : __base::__map_.size() * __base::__block_size - 1;
1399 }
Howard Hinnantfb100022010-09-21 21:28:231400 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161401 size_type __front_spare() const
1402 {
1403 return __base::__start_;
1404 }
Howard Hinnantfb100022010-09-21 21:28:231405 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161406 size_type __back_spare() const
1407 {
1408 return __capacity() - (__base::__start_ + __base::size());
1409 }
1410
1411 template <class _InpIter>
1412 void __append(_InpIter __f, _InpIter __l,
1413 typename enable_if<__is_input_iterator<_InpIter>::value &&
1414 !__is_forward_iterator<_InpIter>::value>::type* = 0);
1415 template <class _ForIter>
1416 void __append(_ForIter __f, _ForIter __l,
1417 typename enable_if<__is_forward_iterator<_ForIter>::value>::type* = 0);
1418 void __append(size_type __n);
1419 void __append(size_type __n, const value_type& __v);
1420 void __erase_to_end(const_iterator __f);
1421 void __add_front_capacity();
1422 void __add_front_capacity(size_type __n);
1423 void __add_back_capacity();
1424 void __add_back_capacity(size_type __n);
1425 iterator __move_and_check(iterator __f, iterator __l, iterator __r,
1426 const_pointer& __vt);
1427 iterator __move_backward_and_check(iterator __f, iterator __l, iterator __r,
1428 const_pointer& __vt);
1429 void __move_construct_and_check(iterator __f, iterator __l,
1430 iterator __r, const_pointer& __vt);
1431 void __move_construct_backward_and_check(iterator __f, iterator __l,
1432 iterator __r, const_pointer& __vt);
1433
Howard Hinnantfb100022010-09-21 21:28:231434 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161435 void __copy_assign_alloc(const deque& __c)
1436 {__copy_assign_alloc(__c, integral_constant<bool,
1437 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1438
Howard Hinnantfb100022010-09-21 21:28:231439 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161440 void __copy_assign_alloc(const deque& __c, true_type)
1441 {
1442 if (__base::__alloc() != __c.__alloc())
1443 {
1444 clear();
1445 shrink_to_fit();
1446 }
1447 __base::__alloc() = __c.__alloc();
1448 __base::__map_.__alloc() = __c.__map_.__alloc();
1449 }
1450
Howard Hinnantfb100022010-09-21 21:28:231451 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2063662011-12-01 20:21:041452 void __copy_assign_alloc(const deque&, false_type)
Howard Hinnant3e519522010-05-11 19:42:161453 {}
1454
Howard Hinnantb58f59c2011-06-02 21:38:571455 void __move_assign(deque& __c, true_type)
1456 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant3e519522010-05-11 19:42:161457 void __move_assign(deque& __c, false_type);
1458};
1459
1460template <class _Tp, class _Allocator>
1461deque<_Tp, _Allocator>::deque(size_type __n)
1462{
1463 if (__n > 0)
1464 __append(__n);
1465}
1466
Marshall Clow630c5e52013-09-07 16:16:191467#if _LIBCPP_STD_VER > 11
1468template <class _Tp, class _Allocator>
1469deque<_Tp, _Allocator>::deque(size_type __n, const _Allocator& __a)
1470 : __base(__a)
1471{
1472 if (__n > 0)
1473 __append(__n);
1474}
1475#endif
1476
Howard Hinnant3e519522010-05-11 19:42:161477template <class _Tp, class _Allocator>
1478deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v)
1479{
1480 if (__n > 0)
1481 __append(__n, __v);
1482}
1483
1484template <class _Tp, class _Allocator>
1485deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v, const allocator_type& __a)
1486 : __base(__a)
1487{
1488 if (__n > 0)
1489 __append(__n, __v);
1490}
1491
1492template <class _Tp, class _Allocator>
1493template <class _InputIter>
1494deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l,
1495 typename enable_if<__is_input_iterator<_InputIter>::value>::type*)
1496{
1497 __append(__f, __l);
1498}
1499
1500template <class _Tp, class _Allocator>
1501template <class _InputIter>
1502deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
1503 typename enable_if<__is_input_iterator<_InputIter>::value>::type*)
1504 : __base(__a)
1505{
1506 __append(__f, __l);
1507}
1508
1509template <class _Tp, class _Allocator>
1510deque<_Tp, _Allocator>::deque(const deque& __c)
1511 : __base(__alloc_traits::select_on_container_copy_construction(__c.__alloc()))
1512{
1513 __append(__c.begin(), __c.end());
1514}
1515
1516template <class _Tp, class _Allocator>
1517deque<_Tp, _Allocator>::deque(const deque& __c, const allocator_type& __a)
1518 : __base(__a)
1519{
1520 __append(__c.begin(), __c.end());
1521}
1522
Howard Hinnant54976f22011-08-12 21:56:021523#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1524
Howard Hinnant3e519522010-05-11 19:42:161525template <class _Tp, class _Allocator>
1526deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il)
1527{
1528 __append(__il.begin(), __il.end());
1529}
1530
1531template <class _Tp, class _Allocator>
1532deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il, const allocator_type& __a)
1533 : __base(__a)
1534{
1535 __append(__il.begin(), __il.end());
1536}
1537
Howard Hinnant54976f22011-08-12 21:56:021538#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1539
Howard Hinnant3e519522010-05-11 19:42:161540template <class _Tp, class _Allocator>
1541deque<_Tp, _Allocator>&
1542deque<_Tp, _Allocator>::operator=(const deque& __c)
1543{
1544 if (this != &__c)
1545 {
1546 __copy_assign_alloc(__c);
1547 assign(__c.begin(), __c.end());
1548 }
1549 return *this;
1550}
1551
Howard Hinnant7609c9b2010-09-04 23:28:191552#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:161553
1554template <class _Tp, class _Allocator>
Evgeniy Stepanov906c8722015-11-07 01:22:131555inline
Howard Hinnant3e519522010-05-11 19:42:161556deque<_Tp, _Allocator>::deque(deque&& __c)
Howard Hinnant9eebe112011-06-02 20:00:141557 _NOEXCEPT_(is_nothrow_move_constructible<__base>::value)
Howard Hinnantce48a112011-06-30 21:18:191558 : __base(_VSTD::move(__c))
Howard Hinnant3e519522010-05-11 19:42:161559{
1560}
1561
1562template <class _Tp, class _Allocator>
Evgeniy Stepanov906c8722015-11-07 01:22:131563inline
Howard Hinnant3e519522010-05-11 19:42:161564deque<_Tp, _Allocator>::deque(deque&& __c, const allocator_type& __a)
Howard Hinnantce48a112011-06-30 21:18:191565 : __base(_VSTD::move(__c), __a)
Howard Hinnant3e519522010-05-11 19:42:161566{
1567 if (__a != __c.__alloc())
1568 {
Howard Hinnantc003db12011-11-29 18:15:501569 typedef move_iterator<iterator> _Ip;
1570 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnant3e519522010-05-11 19:42:161571 }
1572}
1573
1574template <class _Tp, class _Allocator>
Evgeniy Stepanov906c8722015-11-07 01:22:131575inline
Howard Hinnant3e519522010-05-11 19:42:161576deque<_Tp, _Allocator>&
1577deque<_Tp, _Allocator>::operator=(deque&& __c)
Howard Hinnantb58f59c2011-06-02 21:38:571578 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1579 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnant3e519522010-05-11 19:42:161580{
1581 __move_assign(__c, integral_constant<bool,
1582 __alloc_traits::propagate_on_container_move_assignment::value>());
1583 return *this;
1584}
1585
1586template <class _Tp, class _Allocator>
1587void
1588deque<_Tp, _Allocator>::__move_assign(deque& __c, false_type)
1589{
1590 if (__base::__alloc() != __c.__alloc())
1591 {
Howard Hinnantc003db12011-11-29 18:15:501592 typedef move_iterator<iterator> _Ip;
1593 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnant3e519522010-05-11 19:42:161594 }
1595 else
1596 __move_assign(__c, true_type());
1597}
1598
1599template <class _Tp, class _Allocator>
1600void
1601deque<_Tp, _Allocator>::__move_assign(deque& __c, true_type)
Howard Hinnantb58f59c2011-06-02 21:38:571602 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnant3e519522010-05-11 19:42:161603{
1604 clear();
1605 shrink_to_fit();
1606 __base::__move_assign(__c);
1607}
1608
Howard Hinnant7609c9b2010-09-04 23:28:191609#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:161610
1611template <class _Tp, class _Allocator>
1612template <class _InputIter>
1613void
1614deque<_Tp, _Allocator>::assign(_InputIter __f, _InputIter __l,
1615 typename enable_if<__is_input_iterator<_InputIter>::value &&
1616 !__is_random_access_iterator<_InputIter>::value>::type*)
1617{
1618 iterator __i = __base::begin();
1619 iterator __e = __base::end();
Eric Fiselier910285b2014-10-27 19:28:201620 for (; __f != __l && __i != __e; ++__f, (void) ++__i)
Howard Hinnant3e519522010-05-11 19:42:161621 *__i = *__f;
1622 if (__f != __l)
1623 __append(__f, __l);
1624 else
1625 __erase_to_end(__i);
1626}
1627
1628template <class _Tp, class _Allocator>
1629template <class _RAIter>
1630void
1631deque<_Tp, _Allocator>::assign(_RAIter __f, _RAIter __l,
1632 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
1633{
1634 if (static_cast<size_type>(__l - __f) > __base::size())
1635 {
1636 _RAIter __m = __f + __base::size();
Howard Hinnantce48a112011-06-30 21:18:191637 _VSTD::copy(__f, __m, __base::begin());
Howard Hinnant3e519522010-05-11 19:42:161638 __append(__m, __l);
1639 }
1640 else
Howard Hinnantce48a112011-06-30 21:18:191641 __erase_to_end(_VSTD::copy(__f, __l, __base::begin()));
Howard Hinnant3e519522010-05-11 19:42:161642}
1643
1644template <class _Tp, class _Allocator>
1645void
1646deque<_Tp, _Allocator>::assign(size_type __n, const value_type& __v)
1647{
1648 if (__n > __base::size())
1649 {
Howard Hinnantce48a112011-06-30 21:18:191650 _VSTD::fill_n(__base::begin(), __base::size(), __v);
Howard Hinnant3e519522010-05-11 19:42:161651 __n -= __base::size();
1652 __append(__n, __v);
1653 }
1654 else
Howard Hinnantce48a112011-06-30 21:18:191655 __erase_to_end(_VSTD::fill_n(__base::begin(), __n, __v));
Howard Hinnant3e519522010-05-11 19:42:161656}
1657
1658template <class _Tp, class _Allocator>
Evgeniy Stepanov906c8722015-11-07 01:22:131659inline
Howard Hinnant3e519522010-05-11 19:42:161660_Allocator
Howard Hinnanta87e8362011-06-02 16:10:221661deque<_Tp, _Allocator>::get_allocator() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161662{
1663 return __base::__alloc();
1664}
1665
1666template <class _Tp, class _Allocator>
1667void
1668deque<_Tp, _Allocator>::resize(size_type __n)
1669{
1670 if (__n > __base::size())
1671 __append(__n - __base::size());
1672 else if (__n < __base::size())
1673 __erase_to_end(__base::begin() + __n);
1674}
1675
1676template <class _Tp, class _Allocator>
1677void
1678deque<_Tp, _Allocator>::resize(size_type __n, const value_type& __v)
1679{
1680 if (__n > __base::size())
1681 __append(__n - __base::size(), __v);
1682 else if (__n < __base::size())
1683 __erase_to_end(__base::begin() + __n);
1684}
1685
1686template <class _Tp, class _Allocator>
1687void
Howard Hinnantb58f59c2011-06-02 21:38:571688deque<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161689{
1690 allocator_type& __a = __base::__alloc();
1691 if (empty())
1692 {
1693 while (__base::__map_.size() > 0)
1694 {
1695 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
1696 __base::__map_.pop_back();
1697 }
1698 __base::__start_ = 0;
1699 }
1700 else
1701 {
1702 if (__front_spare() >= __base::__block_size)
1703 {
1704 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
1705 __base::__map_.pop_front();
1706 __base::__start_ -= __base::__block_size;
1707 }
1708 if (__back_spare() >= __base::__block_size)
1709 {
1710 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
1711 __base::__map_.pop_back();
1712 }
1713 }
1714 __base::__map_.shrink_to_fit();
1715}
1716
1717template <class _Tp, class _Allocator>
Evgeniy Stepanov906c8722015-11-07 01:22:131718inline
Howard Hinnant3e519522010-05-11 19:42:161719typename deque<_Tp, _Allocator>::reference
1720deque<_Tp, _Allocator>::operator[](size_type __i)
1721{
1722 size_type __p = __base::__start_ + __i;
1723 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1724}
1725
1726template <class _Tp, class _Allocator>
Evgeniy Stepanov906c8722015-11-07 01:22:131727inline
Howard Hinnant3e519522010-05-11 19:42:161728typename deque<_Tp, _Allocator>::const_reference
1729deque<_Tp, _Allocator>::operator[](size_type __i) const
1730{
1731 size_type __p = __base::__start_ + __i;
1732 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1733}
1734
1735template <class _Tp, class _Allocator>
Evgeniy Stepanov906c8722015-11-07 01:22:131736inline
Howard Hinnant3e519522010-05-11 19:42:161737typename deque<_Tp, _Allocator>::reference
1738deque<_Tp, _Allocator>::at(size_type __i)
1739{
1740 if (__i >= __base::size())
1741 __base::__throw_out_of_range();
1742 size_type __p = __base::__start_ + __i;
1743 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1744}
1745
1746template <class _Tp, class _Allocator>
Evgeniy Stepanov906c8722015-11-07 01:22:131747inline
Howard Hinnant3e519522010-05-11 19:42:161748typename deque<_Tp, _Allocator>::const_reference
1749deque<_Tp, _Allocator>::at(size_type __i) const
1750{
1751 if (__i >= __base::size())
1752 __base::__throw_out_of_range();
1753 size_type __p = __base::__start_ + __i;
1754 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1755}
1756
1757template <class _Tp, class _Allocator>
Evgeniy Stepanov906c8722015-11-07 01:22:131758inline
Howard Hinnant3e519522010-05-11 19:42:161759typename deque<_Tp, _Allocator>::reference
1760deque<_Tp, _Allocator>::front()
1761{
1762 return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1763 + __base::__start_ % __base::__block_size);
1764}
1765
1766template <class _Tp, class _Allocator>
Evgeniy Stepanov906c8722015-11-07 01:22:131767inline
Howard Hinnant3e519522010-05-11 19:42:161768typename deque<_Tp, _Allocator>::const_reference
1769deque<_Tp, _Allocator>::front() const
1770{
1771 return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1772 + __base::__start_ % __base::__block_size);
1773}
1774
1775template <class _Tp, class _Allocator>
Evgeniy Stepanov906c8722015-11-07 01:22:131776inline
Howard Hinnant3e519522010-05-11 19:42:161777typename deque<_Tp, _Allocator>::reference
1778deque<_Tp, _Allocator>::back()
1779{
1780 size_type __p = __base::size() + __base::__start_ - 1;
1781 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1782}
1783
1784template <class _Tp, class _Allocator>
Evgeniy Stepanov906c8722015-11-07 01:22:131785inline
Howard Hinnant3e519522010-05-11 19:42:161786typename deque<_Tp, _Allocator>::const_reference
1787deque<_Tp, _Allocator>::back() const
1788{
1789 size_type __p = __base::size() + __base::__start_ - 1;
1790 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1791}
1792
1793template <class _Tp, class _Allocator>
1794void
1795deque<_Tp, _Allocator>::push_back(const value_type& __v)
1796{
1797 allocator_type& __a = __base::__alloc();
1798 if (__back_spare() == 0)
1799 __add_back_capacity();
1800 // __back_spare() >= 1
Howard Hinnantce48a112011-06-30 21:18:191801 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v);
Howard Hinnant3e519522010-05-11 19:42:161802 ++__base::size();
1803}
1804
Howard Hinnant7609c9b2010-09-04 23:28:191805#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:161806
1807template <class _Tp, class _Allocator>
1808void
1809deque<_Tp, _Allocator>::push_back(value_type&& __v)
1810{
1811 allocator_type& __a = __base::__alloc();
1812 if (__back_spare() == 0)
1813 __add_back_capacity();
1814 // __back_spare() >= 1
Howard Hinnantce48a112011-06-30 21:18:191815 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v));
Howard Hinnant3e519522010-05-11 19:42:161816 ++__base::size();
1817}
1818
Howard Hinnant7609c9b2010-09-04 23:28:191819#ifndef _LIBCPP_HAS_NO_VARIADICS
1820
Howard Hinnant3e519522010-05-11 19:42:161821template <class _Tp, class _Allocator>
1822template <class... _Args>
Eric Fiselier0e411642016-07-21 03:20:171823typename deque<_Tp, _Allocator>::reference
Howard Hinnant3e519522010-05-11 19:42:161824deque<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1825{
1826 allocator_type& __a = __base::__alloc();
1827 if (__back_spare() == 0)
1828 __add_back_capacity();
1829 // __back_spare() >= 1
Eric Fiselier0e411642016-07-21 03:20:171830 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()),
1831 _VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:161832 ++__base::size();
Eric Fiselier0e411642016-07-21 03:20:171833 return *--__base::end();
Howard Hinnant3e519522010-05-11 19:42:161834}
1835
Howard Hinnant7609c9b2010-09-04 23:28:191836#endif // _LIBCPP_HAS_NO_VARIADICS
1837#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:161838
1839template <class _Tp, class _Allocator>
1840void
1841deque<_Tp, _Allocator>::push_front(const value_type& __v)
1842{
1843 allocator_type& __a = __base::__alloc();
1844 if (__front_spare() == 0)
1845 __add_front_capacity();
1846 // __front_spare() >= 1
Howard Hinnantce48a112011-06-30 21:18:191847 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v);
Howard Hinnant3e519522010-05-11 19:42:161848 --__base::__start_;
1849 ++__base::size();
1850}
1851
Howard Hinnant7609c9b2010-09-04 23:28:191852#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:161853
1854template <class _Tp, class _Allocator>
1855void
1856deque<_Tp, _Allocator>::push_front(value_type&& __v)
1857{
1858 allocator_type& __a = __base::__alloc();
1859 if (__front_spare() == 0)
1860 __add_front_capacity();
1861 // __front_spare() >= 1
Howard Hinnantce48a112011-06-30 21:18:191862 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v));
Howard Hinnant3e519522010-05-11 19:42:161863 --__base::__start_;
1864 ++__base::size();
1865}
1866
Howard Hinnant7609c9b2010-09-04 23:28:191867#ifndef _LIBCPP_HAS_NO_VARIADICS
1868
Howard Hinnant3e519522010-05-11 19:42:161869template <class _Tp, class _Allocator>
1870template <class... _Args>
Eric Fiselier0e411642016-07-21 03:20:171871typename deque<_Tp, _Allocator>::reference
Howard Hinnant3e519522010-05-11 19:42:161872deque<_Tp, _Allocator>::emplace_front(_Args&&... __args)
1873{
1874 allocator_type& __a = __base::__alloc();
1875 if (__front_spare() == 0)
1876 __add_front_capacity();
1877 // __front_spare() >= 1
Howard Hinnantce48a112011-06-30 21:18:191878 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:161879 --__base::__start_;
1880 ++__base::size();
Eric Fiselier0e411642016-07-21 03:20:171881 return *__base::begin();
Howard Hinnant3e519522010-05-11 19:42:161882}
1883
Howard Hinnant7609c9b2010-09-04 23:28:191884#endif // _LIBCPP_HAS_NO_VARIADICS
1885#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:161886
1887template <class _Tp, class _Allocator>
1888typename deque<_Tp, _Allocator>::iterator
1889deque<_Tp, _Allocator>::insert(const_iterator __p, const value_type& __v)
1890{
1891 size_type __pos = __p - __base::begin();
1892 size_type __to_end = __base::size() - __pos;
1893 allocator_type& __a = __base::__alloc();
1894 if (__pos < __to_end)
1895 { // insert by shifting things backward
1896 if (__front_spare() == 0)
1897 __add_front_capacity();
1898 // __front_spare() >= 1
1899 if (__pos == 0)
1900 {
Howard Hinnantce48a112011-06-30 21:18:191901 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v);
Howard Hinnant3e519522010-05-11 19:42:161902 --__base::__start_;
1903 ++__base::size();
1904 }
1905 else
1906 {
1907 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1908 iterator __b = __base::begin();
Howard Hinnantce48a112011-06-30 21:18:191909 iterator __bm1 = _VSTD::prev(__b);
Howard Hinnant3e519522010-05-11 19:42:161910 if (__vt == pointer_traits<const_pointer>::pointer_to(*__b))
1911 __vt = pointer_traits<const_pointer>::pointer_to(*__bm1);
Howard Hinnantce48a112011-06-30 21:18:191912 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
Howard Hinnant3e519522010-05-11 19:42:161913 --__base::__start_;
1914 ++__base::size();
1915 if (__pos > 1)
Howard Hinnantce48a112011-06-30 21:18:191916 __b = __move_and_check(_VSTD::next(__b), __b + __pos, __b, __vt);
Howard Hinnant3e519522010-05-11 19:42:161917 *__b = *__vt;
1918 }
1919 }
1920 else
1921 { // insert by shifting things forward
1922 if (__back_spare() == 0)
1923 __add_back_capacity();
1924 // __back_capacity >= 1
1925 size_type __de = __base::size() - __pos;
1926 if (__de == 0)
1927 {
Howard Hinnantce48a112011-06-30 21:18:191928 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v);
Howard Hinnant3e519522010-05-11 19:42:161929 ++__base::size();
1930 }
1931 else
1932 {
1933 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1934 iterator __e = __base::end();
Howard Hinnantce48a112011-06-30 21:18:191935 iterator __em1 = _VSTD::prev(__e);
Howard Hinnant3e519522010-05-11 19:42:161936 if (__vt == pointer_traits<const_pointer>::pointer_to(*__em1))
1937 __vt = pointer_traits<const_pointer>::pointer_to(*__e);
Howard Hinnantce48a112011-06-30 21:18:191938 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
Howard Hinnant3e519522010-05-11 19:42:161939 ++__base::size();
1940 if (__de > 1)
1941 __e = __move_backward_and_check(__e - __de, __em1, __e, __vt);
1942 *--__e = *__vt;
1943 }
1944 }
1945 return __base::begin() + __pos;
1946}
1947
Howard Hinnant7609c9b2010-09-04 23:28:191948#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:161949
1950template <class _Tp, class _Allocator>
1951typename deque<_Tp, _Allocator>::iterator
1952deque<_Tp, _Allocator>::insert(const_iterator __p, value_type&& __v)
1953{
1954 size_type __pos = __p - __base::begin();
1955 size_type __to_end = __base::size() - __pos;
1956 allocator_type& __a = __base::__alloc();
1957 if (__pos < __to_end)
1958 { // insert by shifting things backward
1959 if (__front_spare() == 0)
1960 __add_front_capacity();
1961 // __front_spare() >= 1
1962 if (__pos == 0)
1963 {
Howard Hinnantce48a112011-06-30 21:18:191964 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v));
Howard Hinnant3e519522010-05-11 19:42:161965 --__base::__start_;
1966 ++__base::size();
1967 }
1968 else
1969 {
1970 iterator __b = __base::begin();
Howard Hinnantce48a112011-06-30 21:18:191971 iterator __bm1 = _VSTD::prev(__b);
1972 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
Howard Hinnant3e519522010-05-11 19:42:161973 --__base::__start_;
1974 ++__base::size();
1975 if (__pos > 1)
Howard Hinnantce48a112011-06-30 21:18:191976 __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
1977 *__b = _VSTD::move(__v);
Howard Hinnant3e519522010-05-11 19:42:161978 }
1979 }
1980 else
1981 { // insert by shifting things forward
1982 if (__back_spare() == 0)
1983 __add_back_capacity();
1984 // __back_capacity >= 1
1985 size_type __de = __base::size() - __pos;
1986 if (__de == 0)
1987 {
Howard Hinnantce48a112011-06-30 21:18:191988 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v));
Howard Hinnant3e519522010-05-11 19:42:161989 ++__base::size();
1990 }
1991 else
1992 {
1993 iterator __e = __base::end();
Howard Hinnantce48a112011-06-30 21:18:191994 iterator __em1 = _VSTD::prev(__e);
1995 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
Howard Hinnant3e519522010-05-11 19:42:161996 ++__base::size();
1997 if (__de > 1)
Howard Hinnantce48a112011-06-30 21:18:191998 __e = _VSTD::move_backward(__e - __de, __em1, __e);
1999 *--__e = _VSTD::move(__v);
Howard Hinnant3e519522010-05-11 19:42:162000 }
2001 }
2002 return __base::begin() + __pos;
2003}
2004
Howard Hinnant7609c9b2010-09-04 23:28:192005#ifndef _LIBCPP_HAS_NO_VARIADICS
2006
Howard Hinnant3e519522010-05-11 19:42:162007template <class _Tp, class _Allocator>
2008template <class... _Args>
2009typename deque<_Tp, _Allocator>::iterator
2010deque<_Tp, _Allocator>::emplace(const_iterator __p, _Args&&... __args)
2011{
2012 size_type __pos = __p - __base::begin();
2013 size_type __to_end = __base::size() - __pos;
2014 allocator_type& __a = __base::__alloc();
2015 if (__pos < __to_end)
2016 { // insert by shifting things backward
2017 if (__front_spare() == 0)
2018 __add_front_capacity();
2019 // __front_spare() >= 1
2020 if (__pos == 0)
2021 {
Howard Hinnantce48a112011-06-30 21:18:192022 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:162023 --__base::__start_;
2024 ++__base::size();
2025 }
2026 else
2027 {
Marshall Clowdc3eb832016-07-11 21:38:082028 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:162029 iterator __b = __base::begin();
Howard Hinnantce48a112011-06-30 21:18:192030 iterator __bm1 = _VSTD::prev(__b);
2031 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
Howard Hinnant3e519522010-05-11 19:42:162032 --__base::__start_;
2033 ++__base::size();
2034 if (__pos > 1)
Howard Hinnantce48a112011-06-30 21:18:192035 __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
Marshall Clowdc3eb832016-07-11 21:38:082036 *__b = _VSTD::move(__tmp.get());
Howard Hinnant3e519522010-05-11 19:42:162037 }
2038 }
2039 else
2040 { // insert by shifting things forward
2041 if (__back_spare() == 0)
2042 __add_back_capacity();
2043 // __back_capacity >= 1
2044 size_type __de = __base::size() - __pos;
2045 if (__de == 0)
2046 {
Howard Hinnantce48a112011-06-30 21:18:192047 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:162048 ++__base::size();
2049 }
2050 else
2051 {
Marshall Clowdc3eb832016-07-11 21:38:082052 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:162053 iterator __e = __base::end();
Howard Hinnantce48a112011-06-30 21:18:192054 iterator __em1 = _VSTD::prev(__e);
2055 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
Howard Hinnant3e519522010-05-11 19:42:162056 ++__base::size();
2057 if (__de > 1)
Howard Hinnantce48a112011-06-30 21:18:192058 __e = _VSTD::move_backward(__e - __de, __em1, __e);
Marshall Clowdc3eb832016-07-11 21:38:082059 *--__e = _VSTD::move(__tmp.get());
Howard Hinnant3e519522010-05-11 19:42:162060 }
2061 }
2062 return __base::begin() + __pos;
2063}
2064
Howard Hinnant7609c9b2010-09-04 23:28:192065#endif // _LIBCPP_HAS_NO_VARIADICS
2066#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:162067
2068template <class _Tp, class _Allocator>
2069typename deque<_Tp, _Allocator>::iterator
2070deque<_Tp, _Allocator>::insert(const_iterator __p, size_type __n, const value_type& __v)
2071{
2072 size_type __pos = __p - __base::begin();
2073 size_type __to_end = __base::size() - __pos;
2074 allocator_type& __a = __base::__alloc();
2075 if (__pos < __to_end)
2076 { // insert by shifting things backward
2077 if (__n > __front_spare())
2078 __add_front_capacity(__n - __front_spare());
2079 // __n <= __front_spare()
Howard Hinnant3e519522010-05-11 19:42:162080 iterator __old_begin = __base::begin();
2081 iterator __i = __old_begin;
2082 if (__n > __pos)
2083 {
2084 for (size_type __m = __n - __pos; __m; --__m, --__base::__start_, ++__base::size())
Howard Hinnantce48a112011-06-30 21:18:192085 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), __v);
Howard Hinnant3e519522010-05-11 19:42:162086 __n = __pos;
2087 }
2088 if (__n > 0)
2089 {
2090 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2091 iterator __obn = __old_begin + __n;
2092 __move_construct_backward_and_check(__old_begin, __obn, __i, __vt);
2093 if (__n < __pos)
2094 __old_begin = __move_and_check(__obn, __old_begin + __pos, __old_begin, __vt);
Howard Hinnantce48a112011-06-30 21:18:192095 _VSTD::fill_n(__old_begin, __n, *__vt);
Howard Hinnant3e519522010-05-11 19:42:162096 }
2097 }
2098 else
2099 { // insert by shifting things forward
2100 size_type __back_capacity = __back_spare();
2101 if (__n > __back_capacity)
2102 __add_back_capacity(__n - __back_capacity);
2103 // __n <= __back_capacity
Howard Hinnant3e519522010-05-11 19:42:162104 iterator __old_end = __base::end();
2105 iterator __i = __old_end;
2106 size_type __de = __base::size() - __pos;
2107 if (__n > __de)
2108 {
2109 for (size_type __m = __n - __de; __m; --__m, ++__i, ++__base::size())
Howard Hinnantce48a112011-06-30 21:18:192110 __alloc_traits::construct(__a, _VSTD::addressof(*__i), __v);
Howard Hinnant3e519522010-05-11 19:42:162111 __n = __de;
2112 }
2113 if (__n > 0)
2114 {
2115 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2116 iterator __oen = __old_end - __n;
2117 __move_construct_and_check(__oen, __old_end, __i, __vt);
2118 if (__n < __de)
2119 __old_end = __move_backward_and_check(__old_end - __de, __oen, __old_end, __vt);
Howard Hinnantce48a112011-06-30 21:18:192120 _VSTD::fill_n(__old_end - __n, __n, *__vt);
Howard Hinnant3e519522010-05-11 19:42:162121 }
2122 }
2123 return __base::begin() + __pos;
2124}
2125
2126template <class _Tp, class _Allocator>
2127template <class _InputIter>
2128typename deque<_Tp, _Allocator>::iterator
2129deque<_Tp, _Allocator>::insert(const_iterator __p, _InputIter __f, _InputIter __l,
2130 typename enable_if<__is_input_iterator<_InputIter>::value
Marshall Clowf15d7a52015-01-22 18:33:292131 &&!__is_forward_iterator<_InputIter>::value>::type*)
Howard Hinnant3e519522010-05-11 19:42:162132{
2133 __split_buffer<value_type, allocator_type&> __buf(__base::__alloc());
2134 __buf.__construct_at_end(__f, __l);
2135 typedef typename __split_buffer<value_type, allocator_type&>::iterator __bi;
2136 return insert(__p, move_iterator<__bi>(__buf.begin()), move_iterator<__bi>(__buf.end()));
2137}
2138
2139template <class _Tp, class _Allocator>
Marshall Clowf15d7a52015-01-22 18:33:292140template <class _ForwardIterator>
2141typename deque<_Tp, _Allocator>::iterator
2142deque<_Tp, _Allocator>::insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l,
2143 typename enable_if<__is_forward_iterator<_ForwardIterator>::value
2144 &&!__is_bidirectional_iterator<_ForwardIterator>::value>::type*)
2145{
2146 size_type __n = _VSTD::distance(__f, __l);
2147 __split_buffer<value_type, allocator_type&> __buf(__n, 0, __base::__alloc());
2148 __buf.__construct_at_end(__f, __l);
2149 typedef typename __split_buffer<value_type, allocator_type&>::iterator __fwd;
2150 return insert(__p, move_iterator<__fwd>(__buf.begin()), move_iterator<__fwd>(__buf.end()));
2151}
2152
2153template <class _Tp, class _Allocator>
Howard Hinnant3e519522010-05-11 19:42:162154template <class _BiIter>
2155typename deque<_Tp, _Allocator>::iterator
2156deque<_Tp, _Allocator>::insert(const_iterator __p, _BiIter __f, _BiIter __l,
2157 typename enable_if<__is_bidirectional_iterator<_BiIter>::value>::type*)
2158{
Howard Hinnantce48a112011-06-30 21:18:192159 size_type __n = _VSTD::distance(__f, __l);
Howard Hinnant3e519522010-05-11 19:42:162160 size_type __pos = __p - __base::begin();
2161 size_type __to_end = __base::size() - __pos;
2162 allocator_type& __a = __base::__alloc();
2163 if (__pos < __to_end)
2164 { // insert by shifting things backward
2165 if (__n > __front_spare())
2166 __add_front_capacity(__n - __front_spare());
2167 // __n <= __front_spare()
Howard Hinnant3e519522010-05-11 19:42:162168 iterator __old_begin = __base::begin();
2169 iterator __i = __old_begin;
2170 _BiIter __m = __f;
2171 if (__n > __pos)
2172 {
Howard Hinnantce48a112011-06-30 21:18:192173 __m = __pos < __n / 2 ? _VSTD::prev(__l, __pos) : _VSTD::next(__f, __n - __pos);
Howard Hinnant3e519522010-05-11 19:42:162174 for (_BiIter __j = __m; __j != __f; --__base::__start_, ++__base::size())
Howard Hinnantce48a112011-06-30 21:18:192175 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), *--__j);
Howard Hinnant3e519522010-05-11 19:42:162176 __n = __pos;
2177 }
2178 if (__n > 0)
2179 {
2180 iterator __obn = __old_begin + __n;
2181 for (iterator __j = __obn; __j != __old_begin;)
2182 {
Howard Hinnantce48a112011-06-30 21:18:192183 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), _VSTD::move(*--__j));
Howard Hinnant3e519522010-05-11 19:42:162184 --__base::__start_;
2185 ++__base::size();
2186 }
2187 if (__n < __pos)
Howard Hinnantce48a112011-06-30 21:18:192188 __old_begin = _VSTD::move(__obn, __old_begin + __pos, __old_begin);
2189 _VSTD::copy(__m, __l, __old_begin);
Howard Hinnant3e519522010-05-11 19:42:162190 }
2191 }
2192 else
2193 { // insert by shifting things forward
2194 size_type __back_capacity = __back_spare();
2195 if (__n > __back_capacity)
2196 __add_back_capacity(__n - __back_capacity);
2197 // __n <= __back_capacity
Howard Hinnant3e519522010-05-11 19:42:162198 iterator __old_end = __base::end();
2199 iterator __i = __old_end;
2200 _BiIter __m = __l;
2201 size_type __de = __base::size() - __pos;
2202 if (__n > __de)
2203 {
Howard Hinnantce48a112011-06-30 21:18:192204 __m = __de < __n / 2 ? _VSTD::next(__f, __de) : _VSTD::prev(__l, __n - __de);
Eric Fiselier910285b2014-10-27 19:28:202205 for (_BiIter __j = __m; __j != __l; ++__i, (void) ++__j, ++__base::size())
Howard Hinnantce48a112011-06-30 21:18:192206 __alloc_traits::construct(__a, _VSTD::addressof(*__i), *__j);
Howard Hinnant3e519522010-05-11 19:42:162207 __n = __de;
2208 }
2209 if (__n > 0)
2210 {
2211 iterator __oen = __old_end - __n;
2212 for (iterator __j = __oen; __j != __old_end; ++__i, ++__j, ++__base::size())
Howard Hinnantce48a112011-06-30 21:18:192213 __alloc_traits::construct(__a, _VSTD::addressof(*__i), _VSTD::move(*__j));
Howard Hinnant3e519522010-05-11 19:42:162214 if (__n < __de)
Howard Hinnantce48a112011-06-30 21:18:192215 __old_end = _VSTD::move_backward(__old_end - __de, __oen, __old_end);
2216 _VSTD::copy_backward(__f, __m, __old_end);
Howard Hinnant3e519522010-05-11 19:42:162217 }
2218 }
2219 return __base::begin() + __pos;
2220}
2221
2222template <class _Tp, class _Allocator>
2223template <class _InpIter>
2224void
2225deque<_Tp, _Allocator>::__append(_InpIter __f, _InpIter __l,
2226 typename enable_if<__is_input_iterator<_InpIter>::value &&
2227 !__is_forward_iterator<_InpIter>::value>::type*)
2228{
2229 for (; __f != __l; ++__f)
2230 push_back(*__f);
2231}
2232
2233template <class _Tp, class _Allocator>
2234template <class _ForIter>
2235void
2236deque<_Tp, _Allocator>::__append(_ForIter __f, _ForIter __l,
2237 typename enable_if<__is_forward_iterator<_ForIter>::value>::type*)
2238{
Howard Hinnantce48a112011-06-30 21:18:192239 size_type __n = _VSTD::distance(__f, __l);
Howard Hinnant3e519522010-05-11 19:42:162240 allocator_type& __a = __base::__alloc();
2241 size_type __back_capacity = __back_spare();
2242 if (__n > __back_capacity)
2243 __add_back_capacity(__n - __back_capacity);
2244 // __n <= __back_capacity
Eric Fiselier910285b2014-10-27 19:28:202245 for (iterator __i = __base::end(); __f != __l; ++__i, (void) ++__f, ++__base::size())
Howard Hinnantce48a112011-06-30 21:18:192246 __alloc_traits::construct(__a, _VSTD::addressof(*__i), *__f);
Howard Hinnant3e519522010-05-11 19:42:162247}
2248
2249template <class _Tp, class _Allocator>
2250void
2251deque<_Tp, _Allocator>::__append(size_type __n)
2252{
2253 allocator_type& __a = __base::__alloc();
2254 size_type __back_capacity = __back_spare();
2255 if (__n > __back_capacity)
2256 __add_back_capacity(__n - __back_capacity);
2257 // __n <= __back_capacity
2258 for (iterator __i = __base::end(); __n; --__n, ++__i, ++__base::size())
Howard Hinnantce48a112011-06-30 21:18:192259 __alloc_traits::construct(__a, _VSTD::addressof(*__i));
Howard Hinnant3e519522010-05-11 19:42:162260}
2261
2262template <class _Tp, class _Allocator>
2263void
2264deque<_Tp, _Allocator>::__append(size_type __n, const value_type& __v)
2265{
2266 allocator_type& __a = __base::__alloc();
2267 size_type __back_capacity = __back_spare();
2268 if (__n > __back_capacity)
2269 __add_back_capacity(__n - __back_capacity);
2270 // __n <= __back_capacity
2271 for (iterator __i = __base::end(); __n; --__n, ++__i, ++__base::size())
Howard Hinnantce48a112011-06-30 21:18:192272 __alloc_traits::construct(__a, _VSTD::addressof(*__i), __v);
Howard Hinnant3e519522010-05-11 19:42:162273}
2274
2275// Create front capacity for one block of elements.
2276// Strong guarantee. Either do it or don't touch anything.
2277template <class _Tp, class _Allocator>
2278void
2279deque<_Tp, _Allocator>::__add_front_capacity()
2280{
2281 allocator_type& __a = __base::__alloc();
2282 if (__back_spare() >= __base::__block_size)
2283 {
2284 __base::__start_ += __base::__block_size;
2285 pointer __pt = __base::__map_.back();
2286 __base::__map_.pop_back();
2287 __base::__map_.push_front(__pt);
2288 }
2289 // Else if __base::__map_.size() < __base::__map_.capacity() then we need to allocate 1 buffer
2290 else if (__base::__map_.size() < __base::__map_.capacity())
2291 { // we can put the new buffer into the map, but don't shift things around
2292 // until all buffers are allocated. If we throw, we don't need to fix
2293 // anything up (any added buffers are undetectible)
2294 if (__base::__map_.__front_spare() > 0)
2295 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2296 else
2297 {
2298 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2299 // Done allocating, reorder capacity
2300 pointer __pt = __base::__map_.back();
2301 __base::__map_.pop_back();
2302 __base::__map_.push_front(__pt);
2303 }
2304 __base::__start_ = __base::__map_.size() == 1 ?
2305 __base::__block_size / 2 :
2306 __base::__start_ + __base::__block_size;
2307 }
2308 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2309 else
2310 {
2311 __split_buffer<pointer, typename __base::__pointer_allocator&>
2312 __buf(max<size_type>(2 * __base::__map_.capacity(), 1),
2313 0, __base::__map_.__alloc());
Marshall Clowf4903af2015-03-09 17:08:512314
Marshall Clowe3fbe142015-07-13 20:04:562315 typedef __allocator_destructor<_Allocator> _Dp;
2316 unique_ptr<pointer, _Dp> __hold(
2317 __alloc_traits::allocate(__a, __base::__block_size),
2318 _Dp(__a, __base::__block_size));
2319 __buf.push_back(__hold.get());
2320 __hold.release();
2321
Howard Hinnant3e519522010-05-11 19:42:162322 for (typename __base::__map_pointer __i = __base::__map_.begin();
2323 __i != __base::__map_.end(); ++__i)
2324 __buf.push_back(*__i);
Howard Hinnantce48a112011-06-30 21:18:192325 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2326 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2327 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2328 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnant3e519522010-05-11 19:42:162329 __base::__start_ = __base::__map_.size() == 1 ?
2330 __base::__block_size / 2 :
2331 __base::__start_ + __base::__block_size;
2332 }
2333}
2334
2335// Create front capacity for __n elements.
2336// Strong guarantee. Either do it or don't touch anything.
2337template <class _Tp, class _Allocator>
2338void
2339deque<_Tp, _Allocator>::__add_front_capacity(size_type __n)
2340{
2341 allocator_type& __a = __base::__alloc();
2342 size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2343 // Number of unused blocks at back:
2344 size_type __back_capacity = __back_spare() / __base::__block_size;
Howard Hinnantce48a112011-06-30 21:18:192345 __back_capacity = _VSTD::min(__back_capacity, __nb); // don't take more than you need
Howard Hinnant3e519522010-05-11 19:42:162346 __nb -= __back_capacity; // number of blocks need to allocate
2347 // If __nb == 0, then we have sufficient capacity.
2348 if (__nb == 0)
2349 {
2350 __base::__start_ += __base::__block_size * __back_capacity;
2351 for (; __back_capacity > 0; --__back_capacity)
2352 {
2353 pointer __pt = __base::__map_.back();
2354 __base::__map_.pop_back();
2355 __base::__map_.push_front(__pt);
2356 }
2357 }
2358 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2359 else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2360 { // we can put the new buffers into the map, but don't shift things around
2361 // until all buffers are allocated. If we throw, we don't need to fix
2362 // anything up (any added buffers are undetectible)
2363 for (; __nb > 0; --__nb, __base::__start_ += __base::__block_size - (__base::__map_.size() == 1))
2364 {
2365 if (__base::__map_.__front_spare() == 0)
2366 break;
2367 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2368 }
2369 for (; __nb > 0; --__nb, ++__back_capacity)
2370 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2371 // Done allocating, reorder capacity
2372 __base::__start_ += __back_capacity * __base::__block_size;
2373 for (; __back_capacity > 0; --__back_capacity)
2374 {
2375 pointer __pt = __base::__map_.back();
2376 __base::__map_.pop_back();
2377 __base::__map_.push_front(__pt);
2378 }
2379 }
2380 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2381 else
2382 {
2383 size_type __ds = (__nb + __back_capacity) * __base::__block_size - __base::__map_.empty();
2384 __split_buffer<pointer, typename __base::__pointer_allocator&>
2385 __buf(max<size_type>(2* __base::__map_.capacity(),
2386 __nb + __base::__map_.size()),
2387 0, __base::__map_.__alloc());
2388#ifndef _LIBCPP_NO_EXCEPTIONS
2389 try
2390 {
Howard Hinnantb3371f62010-08-22 00:02:432391#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:162392 for (; __nb > 0; --__nb)
2393 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2394#ifndef _LIBCPP_NO_EXCEPTIONS
2395 }
2396 catch (...)
2397 {
2398 for (typename __base::__map_pointer __i = __buf.begin();
2399 __i != __buf.end(); ++__i)
2400 __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2401 throw;
2402 }
Howard Hinnantb3371f62010-08-22 00:02:432403#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:162404 for (; __back_capacity > 0; --__back_capacity)
2405 {
2406 __buf.push_back(__base::__map_.back());
2407 __base::__map_.pop_back();
2408 }
2409 for (typename __base::__map_pointer __i = __base::__map_.begin();
2410 __i != __base::__map_.end(); ++__i)
2411 __buf.push_back(*__i);
Howard Hinnantce48a112011-06-30 21:18:192412 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2413 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2414 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2415 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnant3e519522010-05-11 19:42:162416 __base::__start_ += __ds;
2417 }
2418}
2419
2420// Create back capacity for one block of elements.
2421// Strong guarantee. Either do it or don't touch anything.
2422template <class _Tp, class _Allocator>
2423void
2424deque<_Tp, _Allocator>::__add_back_capacity()
2425{
2426 allocator_type& __a = __base::__alloc();
2427 if (__front_spare() >= __base::__block_size)
2428 {
2429 __base::__start_ -= __base::__block_size;
2430 pointer __pt = __base::__map_.front();
2431 __base::__map_.pop_front();
2432 __base::__map_.push_back(__pt);
2433 }
2434 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2435 else if (__base::__map_.size() < __base::__map_.capacity())
2436 { // we can put the new buffer into the map, but don't shift things around
2437 // until it is allocated. If we throw, we don't need to fix
2438 // anything up (any added buffers are undetectible)
2439 if (__base::__map_.__back_spare() != 0)
2440 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2441 else
2442 {
2443 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2444 // Done allocating, reorder capacity
2445 pointer __pt = __base::__map_.front();
2446 __base::__map_.pop_front();
2447 __base::__map_.push_back(__pt);
2448 }
2449 }
2450 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2451 else
2452 {
2453 __split_buffer<pointer, typename __base::__pointer_allocator&>
2454 __buf(max<size_type>(2* __base::__map_.capacity(), 1),
2455 __base::__map_.size(),
2456 __base::__map_.__alloc());
Marshall Clowf4903af2015-03-09 17:08:512457
Marshall Clowe3fbe142015-07-13 20:04:562458 typedef __allocator_destructor<_Allocator> _Dp;
2459 unique_ptr<pointer, _Dp> __hold(
2460 __alloc_traits::allocate(__a, __base::__block_size),
2461 _Dp(__a, __base::__block_size));
2462 __buf.push_back(__hold.get());
2463 __hold.release();
Marshall Clowf4903af2015-03-09 17:08:512464
Howard Hinnant3e519522010-05-11 19:42:162465 for (typename __base::__map_pointer __i = __base::__map_.end();
2466 __i != __base::__map_.begin();)
2467 __buf.push_front(*--__i);
Howard Hinnantce48a112011-06-30 21:18:192468 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2469 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2470 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2471 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnant3e519522010-05-11 19:42:162472 }
2473}
2474
2475// Create back capacity for __n elements.
2476// Strong guarantee. Either do it or don't touch anything.
2477template <class _Tp, class _Allocator>
2478void
2479deque<_Tp, _Allocator>::__add_back_capacity(size_type __n)
2480{
2481 allocator_type& __a = __base::__alloc();
2482 size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2483 // Number of unused blocks at front:
2484 size_type __front_capacity = __front_spare() / __base::__block_size;
Howard Hinnantce48a112011-06-30 21:18:192485 __front_capacity = _VSTD::min(__front_capacity, __nb); // don't take more than you need
Howard Hinnant3e519522010-05-11 19:42:162486 __nb -= __front_capacity; // number of blocks need to allocate
2487 // If __nb == 0, then we have sufficient capacity.
2488 if (__nb == 0)
2489 {
2490 __base::__start_ -= __base::__block_size * __front_capacity;
2491 for (; __front_capacity > 0; --__front_capacity)
2492 {
2493 pointer __pt = __base::__map_.front();
2494 __base::__map_.pop_front();
2495 __base::__map_.push_back(__pt);
2496 }
2497 }
2498 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2499 else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2500 { // we can put the new buffers into the map, but don't shift things around
2501 // until all buffers are allocated. If we throw, we don't need to fix
2502 // anything up (any added buffers are undetectible)
2503 for (; __nb > 0; --__nb)
2504 {
2505 if (__base::__map_.__back_spare() == 0)
2506 break;
2507 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2508 }
2509 for (; __nb > 0; --__nb, ++__front_capacity, __base::__start_ +=
2510 __base::__block_size - (__base::__map_.size() == 1))
2511 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2512 // Done allocating, reorder capacity
2513 __base::__start_ -= __base::__block_size * __front_capacity;
2514 for (; __front_capacity > 0; --__front_capacity)
2515 {
2516 pointer __pt = __base::__map_.front();
2517 __base::__map_.pop_front();
2518 __base::__map_.push_back(__pt);
2519 }
2520 }
2521 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2522 else
2523 {
2524 size_type __ds = __front_capacity * __base::__block_size;
2525 __split_buffer<pointer, typename __base::__pointer_allocator&>
2526 __buf(max<size_type>(2* __base::__map_.capacity(),
2527 __nb + __base::__map_.size()),
2528 __base::__map_.size() - __front_capacity,
2529 __base::__map_.__alloc());
2530#ifndef _LIBCPP_NO_EXCEPTIONS
2531 try
2532 {
Howard Hinnantb3371f62010-08-22 00:02:432533#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:162534 for (; __nb > 0; --__nb)
2535 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2536#ifndef _LIBCPP_NO_EXCEPTIONS
2537 }
2538 catch (...)
2539 {
2540 for (typename __base::__map_pointer __i = __buf.begin();
2541 __i != __buf.end(); ++__i)
2542 __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2543 throw;
2544 }
Howard Hinnantb3371f62010-08-22 00:02:432545#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:162546 for (; __front_capacity > 0; --__front_capacity)
2547 {
2548 __buf.push_back(__base::__map_.front());
2549 __base::__map_.pop_front();
2550 }
2551 for (typename __base::__map_pointer __i = __base::__map_.end();
2552 __i != __base::__map_.begin();)
2553 __buf.push_front(*--__i);
Howard Hinnantce48a112011-06-30 21:18:192554 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2555 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2556 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2557 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnant3e519522010-05-11 19:42:162558 __base::__start_ -= __ds;
2559 }
2560}
2561
2562template <class _Tp, class _Allocator>
2563void
2564deque<_Tp, _Allocator>::pop_front()
2565{
2566 allocator_type& __a = __base::__alloc();
Howard Hinnant14e200d2013-06-23 21:17:242567 __alloc_traits::destroy(__a, __to_raw_pointer(*(__base::__map_.begin() +
2568 __base::__start_ / __base::__block_size) +
2569 __base::__start_ % __base::__block_size));
Howard Hinnant3e519522010-05-11 19:42:162570 --__base::size();
2571 if (++__base::__start_ >= 2 * __base::__block_size)
2572 {
2573 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2574 __base::__map_.pop_front();
2575 __base::__start_ -= __base::__block_size;
2576 }
2577}
2578
2579template <class _Tp, class _Allocator>
2580void
2581deque<_Tp, _Allocator>::pop_back()
2582{
2583 allocator_type& __a = __base::__alloc();
2584 size_type __p = __base::size() + __base::__start_ - 1;
Howard Hinnant14e200d2013-06-23 21:17:242585 __alloc_traits::destroy(__a, __to_raw_pointer(*(__base::__map_.begin() +
2586 __p / __base::__block_size) +
2587 __p % __base::__block_size));
Howard Hinnant3e519522010-05-11 19:42:162588 --__base::size();
2589 if (__back_spare() >= 2 * __base::__block_size)
2590 {
2591 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2592 __base::__map_.pop_back();
2593 }
2594}
2595
2596// move assign [__f, __l) to [__r, __r + (__l-__f)).
2597// If __vt points into [__f, __l), then subtract (__f - __r) from __vt.
2598template <class _Tp, class _Allocator>
2599typename deque<_Tp, _Allocator>::iterator
2600deque<_Tp, _Allocator>::__move_and_check(iterator __f, iterator __l, iterator __r,
2601 const_pointer& __vt)
2602{
2603 // as if
2604 // for (; __f != __l; ++__f, ++__r)
Howard Hinnantce48a112011-06-30 21:18:192605 // *__r = _VSTD::move(*__f);
Howard Hinnant3e519522010-05-11 19:42:162606 difference_type __n = __l - __f;
2607 while (__n > 0)
2608 {
2609 pointer __fb = __f.__ptr_;
2610 pointer __fe = *__f.__m_iter_ + __base::__block_size;
2611 difference_type __bs = __fe - __fb;
2612 if (__bs > __n)
2613 {
2614 __bs = __n;
2615 __fe = __fb + __bs;
2616 }
2617 if (__fb <= __vt && __vt < __fe)
Howard Hinnant14e200d2013-06-23 21:17:242618 __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) -= __f - __r).__ptr_;
Howard Hinnantce48a112011-06-30 21:18:192619 __r = _VSTD::move(__fb, __fe, __r);
Howard Hinnant3e519522010-05-11 19:42:162620 __n -= __bs;
2621 __f += __bs;
2622 }
2623 return __r;
2624}
2625
2626// move assign [__f, __l) to [__r - (__l-__f), __r) backwards.
2627// If __vt points into [__f, __l), then add (__r - __l) to __vt.
2628template <class _Tp, class _Allocator>
2629typename deque<_Tp, _Allocator>::iterator
2630deque<_Tp, _Allocator>::__move_backward_and_check(iterator __f, iterator __l, iterator __r,
2631 const_pointer& __vt)
2632{
2633 // as if
2634 // while (__f != __l)
Howard Hinnantce48a112011-06-30 21:18:192635 // *--__r = _VSTD::move(*--__l);
Howard Hinnant3e519522010-05-11 19:42:162636 difference_type __n = __l - __f;
2637 while (__n > 0)
2638 {
2639 --__l;
2640 pointer __lb = *__l.__m_iter_;
2641 pointer __le = __l.__ptr_ + 1;
2642 difference_type __bs = __le - __lb;
2643 if (__bs > __n)
2644 {
2645 __bs = __n;
2646 __lb = __le - __bs;
2647 }
2648 if (__lb <= __vt && __vt < __le)
Howard Hinnant14e200d2013-06-23 21:17:242649 __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) += __r - __l - 1).__ptr_;
Howard Hinnantce48a112011-06-30 21:18:192650 __r = _VSTD::move_backward(__lb, __le, __r);
Howard Hinnant3e519522010-05-11 19:42:162651 __n -= __bs;
2652 __l -= __bs - 1;
2653 }
2654 return __r;
2655}
2656
2657// move construct [__f, __l) to [__r, __r + (__l-__f)).
2658// If __vt points into [__f, __l), then add (__r - __f) to __vt.
2659template <class _Tp, class _Allocator>
2660void
2661deque<_Tp, _Allocator>::__move_construct_and_check(iterator __f, iterator __l,
2662 iterator __r, const_pointer& __vt)
2663{
2664 allocator_type& __a = __base::__alloc();
2665 // as if
2666 // for (; __f != __l; ++__r, ++__f, ++__base::size())
Howard Hinnantce48a112011-06-30 21:18:192667 // __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__f));
Howard Hinnant3e519522010-05-11 19:42:162668 difference_type __n = __l - __f;
2669 while (__n > 0)
2670 {
2671 pointer __fb = __f.__ptr_;
2672 pointer __fe = *__f.__m_iter_ + __base::__block_size;
2673 difference_type __bs = __fe - __fb;
2674 if (__bs > __n)
2675 {
2676 __bs = __n;
2677 __fe = __fb + __bs;
2678 }
2679 if (__fb <= __vt && __vt < __fe)
Howard Hinnant14e200d2013-06-23 21:17:242680 __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) += __r - __f).__ptr_;
Howard Hinnant3e519522010-05-11 19:42:162681 for (; __fb != __fe; ++__fb, ++__r, ++__base::size())
Howard Hinnantce48a112011-06-30 21:18:192682 __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__fb));
Howard Hinnant3e519522010-05-11 19:42:162683 __n -= __bs;
2684 __f += __bs;
2685 }
2686}
2687
2688// move construct [__f, __l) to [__r - (__l-__f), __r) backwards.
2689// If __vt points into [__f, __l), then subtract (__l - __r) from __vt.
2690template <class _Tp, class _Allocator>
2691void
2692deque<_Tp, _Allocator>::__move_construct_backward_and_check(iterator __f, iterator __l,
2693 iterator __r, const_pointer& __vt)
2694{
2695 allocator_type& __a = __base::__alloc();
2696 // as if
2697 // for (iterator __j = __l; __j != __f;)
2698 // {
Howard Hinnantce48a112011-06-30 21:18:192699 // __alloc_traitsconstruct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__j));
Howard Hinnant3e519522010-05-11 19:42:162700 // --__base::__start_;
2701 // ++__base::size();
2702 // }
2703 difference_type __n = __l - __f;
2704 while (__n > 0)
2705 {
2706 --__l;
2707 pointer __lb = *__l.__m_iter_;
2708 pointer __le = __l.__ptr_ + 1;
2709 difference_type __bs = __le - __lb;
2710 if (__bs > __n)
2711 {
2712 __bs = __n;
2713 __lb = __le - __bs;
2714 }
2715 if (__lb <= __vt && __vt < __le)
Howard Hinnant14e200d2013-06-23 21:17:242716 __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) -= __l - __r + 1).__ptr_;
Howard Hinnant3e519522010-05-11 19:42:162717 while (__le != __lb)
2718 {
Howard Hinnantce48a112011-06-30 21:18:192719 __alloc_traits::construct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__le));
Howard Hinnant3e519522010-05-11 19:42:162720 --__base::__start_;
2721 ++__base::size();
2722 }
2723 __n -= __bs;
2724 __l -= __bs - 1;
2725 }
2726}
2727
2728template <class _Tp, class _Allocator>
2729typename deque<_Tp, _Allocator>::iterator
2730deque<_Tp, _Allocator>::erase(const_iterator __f)
2731{
Howard Hinnant3e519522010-05-11 19:42:162732 iterator __b = __base::begin();
2733 difference_type __pos = __f - __b;
2734 iterator __p = __b + __pos;
2735 allocator_type& __a = __base::__alloc();
Marshall Clowb41e76b2015-06-05 22:34:192736 if (__pos <= (__base::size() - 1) / 2)
Howard Hinnant3e519522010-05-11 19:42:162737 { // erase from front
Howard Hinnantce48a112011-06-30 21:18:192738 _VSTD::move_backward(__b, __p, _VSTD::next(__p));
2739 __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
Howard Hinnant3e519522010-05-11 19:42:162740 --__base::size();
2741 ++__base::__start_;
2742 if (__front_spare() >= 2 * __base::__block_size)
2743 {
2744 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2745 __base::__map_.pop_front();
2746 __base::__start_ -= __base::__block_size;
2747 }
2748 }
2749 else
2750 { // erase from back
Howard Hinnantce48a112011-06-30 21:18:192751 iterator __i = _VSTD::move(_VSTD::next(__p), __base::end(), __p);
2752 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnant3e519522010-05-11 19:42:162753 --__base::size();
2754 if (__back_spare() >= 2 * __base::__block_size)
2755 {
2756 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2757 __base::__map_.pop_back();
2758 }
2759 }
2760 return __base::begin() + __pos;
2761}
2762
2763template <class _Tp, class _Allocator>
2764typename deque<_Tp, _Allocator>::iterator
2765deque<_Tp, _Allocator>::erase(const_iterator __f, const_iterator __l)
2766{
2767 difference_type __n = __l - __f;
2768 iterator __b = __base::begin();
2769 difference_type __pos = __f - __b;
2770 iterator __p = __b + __pos;
2771 if (__n > 0)
2772 {
2773 allocator_type& __a = __base::__alloc();
Marshall Clowb41e76b2015-06-05 22:34:192774 if (__pos <= (__base::size() - __n) / 2)
Howard Hinnant3e519522010-05-11 19:42:162775 { // erase from front
Howard Hinnantce48a112011-06-30 21:18:192776 iterator __i = _VSTD::move_backward(__b, __p, __p + __n);
Howard Hinnant3e519522010-05-11 19:42:162777 for (; __b != __i; ++__b)
Howard Hinnantce48a112011-06-30 21:18:192778 __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
Howard Hinnant3e519522010-05-11 19:42:162779 __base::size() -= __n;
2780 __base::__start_ += __n;
2781 while (__front_spare() >= 2 * __base::__block_size)
2782 {
2783 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2784 __base::__map_.pop_front();
2785 __base::__start_ -= __base::__block_size;
2786 }
2787 }
2788 else
2789 { // erase from back
Howard Hinnantce48a112011-06-30 21:18:192790 iterator __i = _VSTD::move(__p + __n, __base::end(), __p);
Howard Hinnant3e519522010-05-11 19:42:162791 for (iterator __e = __base::end(); __i != __e; ++__i)
Howard Hinnantce48a112011-06-30 21:18:192792 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnant3e519522010-05-11 19:42:162793 __base::size() -= __n;
2794 while (__back_spare() >= 2 * __base::__block_size)
2795 {
2796 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2797 __base::__map_.pop_back();
2798 }
2799 }
2800 }
2801 return __base::begin() + __pos;
2802}
2803
2804template <class _Tp, class _Allocator>
2805void
2806deque<_Tp, _Allocator>::__erase_to_end(const_iterator __f)
2807{
2808 iterator __e = __base::end();
2809 difference_type __n = __e - __f;
2810 if (__n > 0)
2811 {
2812 allocator_type& __a = __base::__alloc();
2813 iterator __b = __base::begin();
2814 difference_type __pos = __f - __b;
2815 for (iterator __p = __b + __pos; __p != __e; ++__p)
Howard Hinnantce48a112011-06-30 21:18:192816 __alloc_traits::destroy(__a, _VSTD::addressof(*__p));
Howard Hinnant3e519522010-05-11 19:42:162817 __base::size() -= __n;
2818 while (__back_spare() >= 2 * __base::__block_size)
2819 {
2820 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2821 __base::__map_.pop_back();
2822 }
2823 }
2824}
2825
2826template <class _Tp, class _Allocator>
Evgeniy Stepanov906c8722015-11-07 01:22:132827inline
Howard Hinnant3e519522010-05-11 19:42:162828void
2829deque<_Tp, _Allocator>::swap(deque& __c)
Marshall Clowe3fbe142015-07-13 20:04:562830#if _LIBCPP_STD_VER >= 14
2831 _NOEXCEPT
2832#else
2833 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2834 __is_nothrow_swappable<allocator_type>::value)
2835#endif
Howard Hinnant3e519522010-05-11 19:42:162836{
2837 __base::swap(__c);
2838}
2839
2840template <class _Tp, class _Allocator>
Evgeniy Stepanov906c8722015-11-07 01:22:132841inline
Howard Hinnant3e519522010-05-11 19:42:162842void
Howard Hinnanta87e8362011-06-02 16:10:222843deque<_Tp, _Allocator>::clear() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:162844{
2845 __base::clear();
2846}
2847
2848template <class _Tp, class _Allocator>
Howard Hinnant3af48ef2013-10-04 22:09:002849inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:162850bool
2851operator==(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2852{
2853 const typename deque<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnantce48a112011-06-30 21:18:192854 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnant3e519522010-05-11 19:42:162855}
2856
2857template <class _Tp, class _Allocator>
Howard Hinnant3af48ef2013-10-04 22:09:002858inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:162859bool
2860operator!=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2861{
2862 return !(__x == __y);
2863}
2864
2865template <class _Tp, class _Allocator>
Howard Hinnant3af48ef2013-10-04 22:09:002866inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:162867bool
2868operator< (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2869{
Howard Hinnantce48a112011-06-30 21:18:192870 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnant3e519522010-05-11 19:42:162871}
2872
2873template <class _Tp, class _Allocator>
Howard Hinnant3af48ef2013-10-04 22:09:002874inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:162875bool
2876operator> (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2877{
2878 return __y < __x;
2879}
2880
2881template <class _Tp, class _Allocator>
Howard Hinnant3af48ef2013-10-04 22:09:002882inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:162883bool
2884operator>=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2885{
2886 return !(__x < __y);
2887}
2888
2889template <class _Tp, class _Allocator>
Howard Hinnant3af48ef2013-10-04 22:09:002890inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:162891bool
2892operator<=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2893{
2894 return !(__y < __x);
2895}
2896
2897template <class _Tp, class _Allocator>
Howard Hinnant3af48ef2013-10-04 22:09:002898inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:162899void
2900swap(deque<_Tp, _Allocator>& __x, deque<_Tp, _Allocator>& __y)
Howard Hinnant9eebe112011-06-02 20:00:142901 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnant3e519522010-05-11 19:42:162902{
2903 __x.swap(__y);
2904}
2905
2906_LIBCPP_END_NAMESPACE_STD
2907
2908#endif // _LIBCPP_DEQUE