blob: 92801540f2e7c563ae988ff360f4b6a2c5706cbc [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------------- deque -----------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +00008//
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 Hinnantad979ba2011-06-03 15:16:49 +000041 deque() noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +000042 explicit deque(const allocator_type& a);
43 explicit deque(size_type n);
Marshall Clow68098692013-09-09 18:19:45 +000044 explicit deque(size_type n, const allocator_type& a); // C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000045 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 Hinnant4931e092011-06-02 21:38:57 +000052 deque(deque&& c)
53 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +000054 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 Hinnant4931e092011-06-02 21:38:57 +000060 deque& operator=(deque&& c)
61 noexcept(
62 allocator_type::propagate_on_container_move_assignment::value &&
63 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +000064 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 Hinnantda2df912011-06-02 16:10:22 +000071 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000072
73 // iterators:
74
Howard Hinnantda2df912011-06-02 16:10:22 +000075 iterator begin() noexcept;
76 const_iterator begin() const noexcept;
77 iterator end() noexcept;
78 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000079
Howard Hinnantda2df912011-06-02 16:10:22 +000080 reverse_iterator rbegin() noexcept;
81 const_reverse_iterator rbegin() const noexcept;
82 reverse_iterator rend() noexcept;
83 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000084
Howard Hinnantda2df912011-06-02 16:10:22 +000085 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 Hinnantc51e1022010-05-11 19:42:16 +000089
90 // capacity:
Howard Hinnantda2df912011-06-02 16:10:22 +000091 size_type size() const noexcept;
92 size_type max_size() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000093 void resize(size_type n);
94 void resize(size_type n, const value_type& v);
95 void shrink_to_fit();
Howard Hinnantda2df912011-06-02 16:10:22 +000096 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000097
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);
Marshall Clowea52cc42017-01-24 23:09:12 +0000113 template <class... Args> reference emplace_front(Args&&... args); // reference in C++17
114 template <class... Args> reference emplace_back(Args&&... args); // reference in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000115 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 Clow6b612cf2015-01-22 18:33:29 +0000120 iterator insert(const_iterator p, InputIterator f, InputIterator l);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000121 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 Hinnant4931e092011-06-02 21:38:57 +0000126 void swap(deque& c)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000127 noexcept(allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantda2df912011-06-02 16:10:22 +0000128 void clear() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000129};
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 Hinnant287548a2011-06-03 17:30:28 +0000146 void swap(deque<T,Allocator>& x, deque<T,Allocator>& y)
147 noexcept(noexcept(x.swap(y)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000148
149} // std
150
151*/
152
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000153#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000154#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000155#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000156
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 Hinnantc5a5fbd2011-11-29 16:45:27 +0000165#include <__undef_min_max>
166
Howard Hinnantc51e1022010-05-11 19:42:16 +0000167_LIBCPP_BEGIN_NAMESPACE_STD
168
169template <class _Tp, class _Allocator> class __deque_base;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000170template <class _Tp, class _Allocator = allocator<_Tp> > class _LIBCPP_TEMPLATE_VIS deque;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000171
172template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
173 class _DiffType, _DiffType _BlockSize>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000174class _LIBCPP_TEMPLATE_VIS __deque_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000175
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 Stepanovc299de22015-11-06 22:02:29 +0000264template <class _ValueType, class _DiffType>
265struct __deque_block_size {
266 static const _DiffType value = sizeof(_ValueType) < 256 ? 4096 / sizeof(_ValueType) : 16;
267};
268
Howard Hinnantc51e1022010-05-11 19:42:16 +0000269template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000270 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 >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000279class _LIBCPP_TEMPLATE_VIS __deque_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000280{
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 Stepanovc299de22015-11-06 22:02:29 +0000289 static const difference_type __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000290public:
291 typedef _ValueType value_type;
292 typedef random_access_iterator_tag iterator_category;
293 typedef _Reference reference;
294
Marshall Clow7a3a61d2013-08-06 16:14:36 +0000295 _LIBCPP_INLINE_VISIBILITY __deque_iterator() _NOEXCEPT
296#if _LIBCPP_STD_VER > 11
297 : __m_iter_(nullptr), __ptr_(nullptr)
298#endif
299 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000300
Howard Hinnantc834c512011-11-29 18:15:50 +0000301 template <class _Pp, class _Rp, class _MP>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000302 _LIBCPP_INLINE_VISIBILITY
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000303 __deque_iterator(const __deque_iterator<value_type, _Pp, _Rp, _MP, difference_type, _BS>& __it,
Howard Hinnantc834c512011-11-29 18:15:50 +0000304 typename enable_if<is_convertible<_Pp, pointer>::value>::type* = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000305 : __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 Hinnant3b6579a2010-08-22 00:02:43 +0000364
Howard Hinnantc51e1022010-05-11 19:42:16 +0000365 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator-=(difference_type __n)
366 {
367 return *this += -__n;
368 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000369
Howard Hinnantc51e1022010-05-11 19:42:16 +0000370 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator+(difference_type __n) const
371 {
372 __deque_iterator __t(*this);
373 __t += __n;
374 return __t;
375 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000376
Howard Hinnantc51e1022010-05-11 19:42:16 +0000377 _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 Hinnantda2df912011-06-02 16:10:22 +0000427 _LIBCPP_INLINE_VISIBILITY __deque_iterator(__map_iterator __m, pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000428 : __m_iter_(__m), __ptr_(__p) {}
429
Howard Hinnantc834c512011-11-29 18:15:50 +0000430 template <class _Tp, class _Ap> friend class __deque_base;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000431 template <class _Tp, class _Ap> friend class _LIBCPP_TEMPLATE_VIS deque;
Howard Hinnantc834c512011-11-29 18:15:50 +0000432 template <class _Vp, class _Pp, class _Rp, class _MP, class _Dp, _Dp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000433 friend class _LIBCPP_TEMPLATE_VIS __deque_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000434
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 Stepanovc299de22015-11-06 22:02:29 +0000536template <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 Hinnantc51e1022010-05-11 19:42:16 +0000542// 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 Stepanovc299de22015-11-06 22:02:29 +0000554 const difference_type __block_size = __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::__block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000555 while (__f != __l)
556 {
557 pointer __rb = __r.__ptr_;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000558 pointer __re = *__r.__m_iter_ + __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000559 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 Hinnantb1ad5a82011-06-30 21:18:19 +0000567 _VSTD::copy(__f, __m, __rb);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000568 __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 Stepanovc299de22015-11-06 22:02:29 +0000583 const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000584 difference_type __n = __l - __f;
585 while (__n > 0)
586 {
587 pointer __fb = __f.__ptr_;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000588 pointer __fe = *__f.__m_iter_ + __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000589 difference_type __bs = __fe - __fb;
590 if (__bs > __n)
591 {
592 __bs = __n;
593 __fe = __fb + __bs;
594 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000595 __r = _VSTD::copy(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000596 __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 Stepanovc299de22015-11-06 22:02:29 +0000611 const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000612 difference_type __n = __l - __f;
613 while (__n > 0)
614 {
615 pointer __fb = __f.__ptr_;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000616 pointer __fe = *__f.__m_iter_ + __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000617 difference_type __bs = __fe - __fb;
618 if (__bs > __n)
619 {
620 __bs = __n;
621 __fe = __fb + __bs;
622 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000623 __r = _VSTD::copy(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000624 __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 Hinnantb1ad5a82011-06-30 21:18:19 +0000644 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _VSTD::prev(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000645 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 Hinnantb1ad5a82011-06-30 21:18:19 +0000655 _VSTD::copy_backward(__m, __l, __re);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000656 __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 Hinnantb1ad5a82011-06-30 21:18:19 +0000683 __r = _VSTD::copy_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000684 __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 Hinnantb1ad5a82011-06-30 21:18:19 +0000711 __r = _VSTD::copy_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000712 __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 Stepanovc299de22015-11-06 22:02:29 +0000730 const difference_type __block_size = __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::__block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000731 while (__f != __l)
732 {
733 pointer __rb = __r.__ptr_;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000734 pointer __re = *__r.__m_iter_ + __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000735 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 Hinnantb1ad5a82011-06-30 21:18:19 +0000743 _VSTD::move(__f, __m, __rb);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000744 __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 Stepanovc299de22015-11-06 22:02:29 +0000759 const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000760 difference_type __n = __l - __f;
761 while (__n > 0)
762 {
763 pointer __fb = __f.__ptr_;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000764 pointer __fe = *__f.__m_iter_ + __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000765 difference_type __bs = __fe - __fb;
766 if (__bs > __n)
767 {
768 __bs = __n;
769 __fe = __fb + __bs;
770 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000771 __r = _VSTD::move(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000772 __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 Stepanovc299de22015-11-06 22:02:29 +0000787 const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000788 difference_type __n = __l - __f;
789 while (__n > 0)
790 {
791 pointer __fb = __f.__ptr_;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000792 pointer __fe = *__f.__m_iter_ + __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000793 difference_type __bs = __fe - __fb;
794 if (__bs > __n)
795 {
796 __bs = __n;
797 __fe = __fb + __bs;
798 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000799 __r = _VSTD::move(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000800 __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 Hinnantb1ad5a82011-06-30 21:18:19 +0000820 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _VSTD::prev(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000821 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 Hinnantb1ad5a82011-06-30 21:18:19 +0000831 _VSTD::move_backward(__m, __l, __re);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000832 __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 Hinnantb1ad5a82011-06-30 21:18:19 +0000859 __r = _VSTD::move_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000860 __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 Hinnantb1ad5a82011-06-30 21:18:19 +0000887 __r = _VSTD::move_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000888 __n -= __bs;
889 __l -= __bs - 1;
890 }
891 return __r;
892}
893
894template <bool>
895class __deque_base_common
896{
897protected:
Marshall Clow8fea1612016-08-25 15:09:01 +0000898 _LIBCPP_NORETURN void __throw_length_error() const;
899 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000900};
901
902template <bool __b>
903void
904__deque_base_common<__b>::__throw_length_error() const
905{
Marshall Clow8fea1612016-08-25 15:09:01 +0000906 _VSTD::__throw_length_error("deque");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000907}
908
909template <bool __b>
910void
911__deque_base_common<__b>::__throw_out_of_range() const
912{
Marshall Clow8fea1612016-08-25 15:09:01 +0000913 _VSTD::__throw_out_of_range("deque");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000914}
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 Stepanovc299de22015-11-06 22:02:29 +0000933 static const difference_type __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000934
Marshall Clow940e01c2015-04-07 05:21:38 +0000935 typedef typename __rebind_alloc_helper<__alloc_traits, pointer>::type __pointer_allocator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000936 typedef allocator_traits<__pointer_allocator> __map_traits;
937 typedef typename __map_traits::pointer __map_pointer;
Marshall Clow940e01c2015-04-07 05:21:38 +0000938 typedef typename __rebind_alloc_helper<__alloc_traits, const_pointer>::type __const_pointer_allocator;
Howard Hinnantdcb73a32013-06-23 21:17:24 +0000939 typedef typename allocator_traits<__const_pointer_allocator>::const_pointer __map_const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000940 typedef __split_buffer<pointer, __pointer_allocator> __map;
941
942 typedef __deque_iterator<value_type, pointer, reference, __map_pointer,
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000943 difference_type> iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000944 typedef __deque_iterator<value_type, const_pointer, const_reference, __map_const_pointer,
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000945 difference_type> const_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000946
947 __map __map_;
948 size_type __start_;
949 __compressed_pair<size_type, allocator_type> __size_;
950
Howard Hinnantda2df912011-06-02 16:10:22 +0000951 iterator begin() _NOEXCEPT;
952 const_iterator begin() const _NOEXCEPT;
953 iterator end() _NOEXCEPT;
954 const_iterator end() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000955
956 _LIBCPP_INLINE_VISIBILITY size_type& size() {return __size_.first();}
Howard Hinnantda2df912011-06-02 16:10:22 +0000957 _LIBCPP_INLINE_VISIBILITY
958 const size_type& size() const _NOEXCEPT {return __size_.first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000959 _LIBCPP_INLINE_VISIBILITY allocator_type& __alloc() {return __size_.second();}
Howard Hinnantda2df912011-06-02 16:10:22 +0000960 _LIBCPP_INLINE_VISIBILITY
961 const allocator_type& __alloc() const _NOEXCEPT {return __size_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000962
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000963 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantad979ba2011-06-03 15:16:49 +0000964 __deque_base()
965 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000966 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000967 explicit __deque_base(const allocator_type& __a);
Howard Hinnantca2fc222011-06-02 20:00:14 +0000968public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000969 ~__deque_base();
970
Howard Hinnant74279a52010-09-04 23:28:19 +0000971#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000972
Howard Hinnantca2fc222011-06-02 20:00:14 +0000973 __deque_base(__deque_base&& __c)
974 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000975 __deque_base(__deque_base&& __c, const allocator_type& __a);
976
Howard Hinnant74279a52010-09-04 23:28:19 +0000977#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantca2fc222011-06-02 20:00:14 +0000978 void swap(__deque_base& __c)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000979#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 Hinnantca2fc222011-06-02 20:00:14 +0000985protected:
Howard Hinnantda2df912011-06-02 16:10:22 +0000986 void clear() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000987
988 bool __invariants() const;
989
Howard Hinnant874ad9a2010-09-21 21:28:23 +0000990 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000991 void __move_assign(__deque_base& __c)
Howard Hinnant4931e092011-06-02 21:38:57 +0000992 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
993 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000994 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000995 __map_ = _VSTD::move(__c.__map_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000996 __start_ = __c.__start_;
997 size() = __c.size();
998 __move_assign_alloc(__c);
999 __c.__start_ = __c.size() = 0;
1000 }
1001
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001002 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001003 void __move_assign_alloc(__deque_base& __c)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001004 _NOEXCEPT_(!__alloc_traits::propagate_on_container_move_assignment::value ||
1005 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001006 {__move_assign_alloc(__c, integral_constant<bool,
1007 __alloc_traits::propagate_on_container_move_assignment::value>());}
1008
1009private:
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001010 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001011 void __move_assign_alloc(__deque_base& __c, true_type)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001012 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001013 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001014 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001015 }
1016
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001017 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001018 void __move_assign_alloc(__deque_base&, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001019 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001020};
1021
1022template <class _Tp, class _Allocator>
Evgeniy Stepanovc299de22015-11-06 22:02:29 +00001023const 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 Hinnantc51e1022010-05-11 19:42:16 +00001028bool
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 Hinnantda2df912011-06-02 16:10:22 +00001058__deque_base<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001059{
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 Hinnantda2df912011-06-02 16:10:22 +00001066__deque_base<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001067{
Howard Hinnantdcb73a32013-06-23 21:17:24 +00001068 __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __start_ / __block_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001069 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 Hinnantda2df912011-06-02 16:10:22 +00001074__deque_base<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001075{
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 Hinnantda2df912011-06-02 16:10:22 +00001083__deque_base<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001084{
1085 size_type __p = size() + __start_;
Howard Hinnantdcb73a32013-06-23 21:17:24 +00001086 __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __p / __block_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001087 return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
1088}
1089
1090template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001091inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001092__deque_base<_Tp, _Allocator>::__deque_base()
Howard Hinnantad979ba2011-06-03 15:16:49 +00001093 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001094 : __start_(0), __size_(0) {}
1095
1096template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001097inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001098__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 Hinnant74279a52010-09-04 23:28:19 +00001111#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001112
1113template <class _Tp, class _Allocator>
1114__deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001115 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001116 : __map_(_VSTD::move(__c.__map_)),
1117 __start_(_VSTD::move(__c.__start_)),
1118 __size_(_VSTD::move(__c.__size_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001119{
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 Hinnantb1ad5a82011-06-30 21:18:19 +00001126 : __map_(_VSTD::move(__c.__map_), __pointer_allocator(__a)),
1127 __start_(_VSTD::move(__c.__start_)),
1128 __size_(_VSTD::move(__c.size()), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001129{
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 Hinnant74279a52010-09-04 23:28:19 +00001143#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001144
1145template <class _Tp, class _Allocator>
1146void
1147__deque_base<_Tp, _Allocator>::swap(__deque_base& __c)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001148#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 Hinnantc51e1022010-05-11 19:42:16 +00001154{
1155 __map_.swap(__c.__map_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001156 _VSTD::swap(__start_, __c.__start_);
1157 _VSTD::swap(size(), __c.size());
Marshall Clow8982dcd2015-07-13 20:04:56 +00001158 __swap_allocator(__alloc(), __c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159}
1160
1161template <class _Tp, class _Allocator>
1162void
Howard Hinnantda2df912011-06-02 16:10:22 +00001163__deque_base<_Tp, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001164{
1165 allocator_type& __a = __alloc();
1166 for (iterator __i = begin(), __e = end(); __i != __e; ++__i)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001167 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001168 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 Clow65cd4c62015-02-18 17:24:08 +00001185template <class _Tp, class _Allocator /*= allocator<_Tp>*/>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001186class _LIBCPP_TEMPLATE_VIS deque
Howard Hinnantc51e1022010-05-11 19:42:16 +00001187 : private __deque_base<_Tp, _Allocator>
1188{
1189public:
1190 // types:
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001191
Howard Hinnantc51e1022010-05-11 19:42:16 +00001192 typedef _Tp value_type;
1193 typedef _Allocator allocator_type;
1194
Marshall Clow5128cf32015-11-26 01:24:04 +00001195 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 Hinnantc51e1022010-05-11 19:42:16 +00001198 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 Hinnantb1ad5a82011-06-30 21:18:19 +00001210 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
1211 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001212
1213 // construct/copy/destroy:
Howard Hinnantad979ba2011-06-03 15:16:49 +00001214 _LIBCPP_INLINE_VISIBILITY
1215 deque()
1216 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
1217 {}
Marshall Clow7ed23a72014-03-05 19:06:20 +00001218 _LIBCPP_INLINE_VISIBILITY explicit deque(const allocator_type& __a) : __base(__a) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001219 explicit deque(size_type __n);
Marshall Clowbc4fcb42013-09-07 16:16:19 +00001220#if _LIBCPP_STD_VER > 11
1221 explicit deque(size_type __n, const _Allocator& __a);
1222#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001223 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 Hinnant33711792011-08-12 21:56:02 +00001233#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001234 deque(initializer_list<value_type> __il);
1235 deque(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnant33711792011-08-12 21:56:02 +00001236#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001237
1238 deque& operator=(const deque& __c);
Howard Hinnant33711792011-08-12 21:56:02 +00001239#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001240 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001241 deque& operator=(initializer_list<value_type> __il) {assign(__il); return *this;}
Howard Hinnant33711792011-08-12 21:56:02 +00001242#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001243
Howard Hinnant74279a52010-09-04 23:28:19 +00001244#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001245 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantca2fc222011-06-02 20:00:14 +00001246 deque(deque&& __c) _NOEXCEPT_(is_nothrow_move_constructible<__base>::value);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001247 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001248 deque(deque&& __c, const allocator_type& __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001249 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantca2fc222011-06-02 20:00:14 +00001250 deque& operator=(deque&& __c)
Howard Hinnant4931e092011-06-02 21:38:57 +00001251 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1252 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant74279a52010-09-04 23:28:19 +00001253#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001254
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 Hinnant33711792011-08-12 21:56:02 +00001263#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001264 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001265 void assign(initializer_list<value_type> __il) {assign(__il.begin(), __il.end());}
Howard Hinnant33711792011-08-12 21:56:02 +00001266#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001267
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001268 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001269 allocator_type get_allocator() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001270
1271 // iterators:
1272
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001273 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001274 iterator begin() _NOEXCEPT {return __base::begin();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001275 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001276 const_iterator begin() const _NOEXCEPT {return __base::begin();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001277 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001278 iterator end() _NOEXCEPT {return __base::end();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001279 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001280 const_iterator end() const _NOEXCEPT {return __base::end();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001281
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001282 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001283 reverse_iterator rbegin() _NOEXCEPT
1284 {return reverse_iterator(__base::end());}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001286 const_reverse_iterator rbegin() const _NOEXCEPT
1287 {return const_reverse_iterator(__base::end());}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001288 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001289 reverse_iterator rend() _NOEXCEPT
1290 {return reverse_iterator(__base::begin());}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001291 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001292 const_reverse_iterator rend() const _NOEXCEPT
1293 {return const_reverse_iterator(__base::begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001294
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001295 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001296 const_iterator cbegin() const _NOEXCEPT
1297 {return __base::begin();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001298 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001299 const_iterator cend() const _NOEXCEPT
1300 {return __base::end();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001302 const_reverse_iterator crbegin() const _NOEXCEPT
1303 {return const_reverse_iterator(__base::end());}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001304 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001305 const_reverse_iterator crend() const _NOEXCEPT
1306 {return const_reverse_iterator(__base::begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001307
1308 // capacity:
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001309 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001310 size_type size() const _NOEXCEPT {return __base::size();}
1311 _LIBCPP_INLINE_VISIBILITY
1312 size_type max_size() const _NOEXCEPT
Eric Fiselierb5d9f442016-11-23 01:18:56 +00001313 {return std::min<size_type>(
1314 __alloc_traits::max_size(__base::__alloc()),
1315 numeric_limits<difference_type>::max());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001316 void resize(size_type __n);
1317 void resize(size_type __n, const value_type& __v);
Howard Hinnant4931e092011-06-02 21:38:57 +00001318 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantda2df912011-06-02 16:10:22 +00001319 _LIBCPP_INLINE_VISIBILITY
1320 bool empty() const _NOEXCEPT {return __base::size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001321
1322 // element access:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001323 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001324 reference operator[](size_type __i);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001325 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001326 const_reference operator[](size_type __i) const;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001327 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001328 reference at(size_type __i);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001329 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001330 const_reference at(size_type __i) const;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001331 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001332 reference front();
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001333 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001334 const_reference front() const;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001335 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001336 reference back();
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001337 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001338 const_reference back() const;
1339
1340 // 23.2.2.3 modifiers:
1341 void push_front(const value_type& __v);
1342 void push_back(const value_type& __v);
Howard Hinnant74279a52010-09-04 23:28:19 +00001343#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1344#ifndef _LIBCPP_HAS_NO_VARIADICS
Marshall Clowea52cc42017-01-24 23:09:12 +00001345#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001346 template <class... _Args> reference emplace_front(_Args&&... __args);
Marshall Clowea52cc42017-01-24 23:09:12 +00001347 template <class... _Args> reference emplace_back (_Args&&... __args);
1348#else
1349 template <class... _Args> void emplace_front(_Args&&... __args);
1350 template <class... _Args> void emplace_back (_Args&&... __args);
1351#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001352 template <class... _Args> iterator emplace(const_iterator __p, _Args&&... __args);
Howard Hinnant74279a52010-09-04 23:28:19 +00001353#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001354 void push_front(value_type&& __v);
1355 void push_back(value_type&& __v);
1356 iterator insert(const_iterator __p, value_type&& __v);
Howard Hinnant74279a52010-09-04 23:28:19 +00001357#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001358 iterator insert(const_iterator __p, const value_type& __v);
1359 iterator insert(const_iterator __p, size_type __n, const value_type& __v);
1360 template <class _InputIter>
Marshall Clow6b612cf2015-01-22 18:33:29 +00001361 iterator insert(const_iterator __p, _InputIter __f, _InputIter __l,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001362 typename enable_if<__is_input_iterator<_InputIter>::value
Marshall Clow6b612cf2015-01-22 18:33:29 +00001363 &&!__is_forward_iterator<_InputIter>::value>::type* = 0);
1364 template <class _ForwardIterator>
1365 iterator insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l,
1366 typename enable_if<__is_forward_iterator<_ForwardIterator>::value
1367 &&!__is_bidirectional_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001368 template <class _BiIter>
Marshall Clow6b612cf2015-01-22 18:33:29 +00001369 iterator insert(const_iterator __p, _BiIter __f, _BiIter __l,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001370 typename enable_if<__is_bidirectional_iterator<_BiIter>::value>::type* = 0);
Howard Hinnant33711792011-08-12 21:56:02 +00001371#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001372 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001373 iterator insert(const_iterator __p, initializer_list<value_type> __il)
1374 {return insert(__p, __il.begin(), __il.end());}
Howard Hinnant33711792011-08-12 21:56:02 +00001375#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001376 void pop_front();
1377 void pop_back();
1378 iterator erase(const_iterator __p);
1379 iterator erase(const_iterator __f, const_iterator __l);
1380
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001381 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantca2fc222011-06-02 20:00:14 +00001382 void swap(deque& __c)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001383#if _LIBCPP_STD_VER >= 14
1384 _NOEXCEPT;
1385#else
Howard Hinnantca2fc222011-06-02 20:00:14 +00001386 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1387 __is_nothrow_swappable<allocator_type>::value);
Marshall Clow8982dcd2015-07-13 20:04:56 +00001388#endif
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001389 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001390 void clear() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001391
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001392 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001393 bool __invariants() const {return __base::__invariants();}
1394private:
Howard Hinnantdcb73a32013-06-23 21:17:24 +00001395 typedef typename __base::__map_const_pointer __map_const_pointer;
1396
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001397 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001398 static size_type __recommend_blocks(size_type __n)
1399 {
1400 return __n / __base::__block_size + (__n % __base::__block_size != 0);
1401 }
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001402 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001403 size_type __capacity() const
1404 {
1405 return __base::__map_.size() == 0 ? 0 : __base::__map_.size() * __base::__block_size - 1;
1406 }
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001407 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001408 size_type __front_spare() const
1409 {
1410 return __base::__start_;
1411 }
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001412 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001413 size_type __back_spare() const
1414 {
1415 return __capacity() - (__base::__start_ + __base::size());
1416 }
1417
1418 template <class _InpIter>
1419 void __append(_InpIter __f, _InpIter __l,
1420 typename enable_if<__is_input_iterator<_InpIter>::value &&
1421 !__is_forward_iterator<_InpIter>::value>::type* = 0);
1422 template <class _ForIter>
1423 void __append(_ForIter __f, _ForIter __l,
1424 typename enable_if<__is_forward_iterator<_ForIter>::value>::type* = 0);
1425 void __append(size_type __n);
1426 void __append(size_type __n, const value_type& __v);
1427 void __erase_to_end(const_iterator __f);
1428 void __add_front_capacity();
1429 void __add_front_capacity(size_type __n);
1430 void __add_back_capacity();
1431 void __add_back_capacity(size_type __n);
1432 iterator __move_and_check(iterator __f, iterator __l, iterator __r,
1433 const_pointer& __vt);
1434 iterator __move_backward_and_check(iterator __f, iterator __l, iterator __r,
1435 const_pointer& __vt);
1436 void __move_construct_and_check(iterator __f, iterator __l,
1437 iterator __r, const_pointer& __vt);
1438 void __move_construct_backward_and_check(iterator __f, iterator __l,
1439 iterator __r, const_pointer& __vt);
1440
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001441 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001442 void __copy_assign_alloc(const deque& __c)
1443 {__copy_assign_alloc(__c, integral_constant<bool,
1444 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1445
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001446 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001447 void __copy_assign_alloc(const deque& __c, true_type)
1448 {
1449 if (__base::__alloc() != __c.__alloc())
1450 {
1451 clear();
1452 shrink_to_fit();
1453 }
1454 __base::__alloc() = __c.__alloc();
1455 __base::__map_.__alloc() = __c.__map_.__alloc();
1456 }
1457
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001458 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001459 void __copy_assign_alloc(const deque&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001460 {}
1461
Howard Hinnant4931e092011-06-02 21:38:57 +00001462 void __move_assign(deque& __c, true_type)
1463 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001464 void __move_assign(deque& __c, false_type);
1465};
1466
1467template <class _Tp, class _Allocator>
1468deque<_Tp, _Allocator>::deque(size_type __n)
1469{
1470 if (__n > 0)
1471 __append(__n);
1472}
1473
Marshall Clowbc4fcb42013-09-07 16:16:19 +00001474#if _LIBCPP_STD_VER > 11
1475template <class _Tp, class _Allocator>
1476deque<_Tp, _Allocator>::deque(size_type __n, const _Allocator& __a)
1477 : __base(__a)
1478{
1479 if (__n > 0)
1480 __append(__n);
1481}
1482#endif
1483
Howard Hinnantc51e1022010-05-11 19:42:16 +00001484template <class _Tp, class _Allocator>
1485deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v)
1486{
1487 if (__n > 0)
1488 __append(__n, __v);
1489}
1490
1491template <class _Tp, class _Allocator>
1492deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v, const allocator_type& __a)
1493 : __base(__a)
1494{
1495 if (__n > 0)
1496 __append(__n, __v);
1497}
1498
1499template <class _Tp, class _Allocator>
1500template <class _InputIter>
1501deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l,
1502 typename enable_if<__is_input_iterator<_InputIter>::value>::type*)
1503{
1504 __append(__f, __l);
1505}
1506
1507template <class _Tp, class _Allocator>
1508template <class _InputIter>
1509deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
1510 typename enable_if<__is_input_iterator<_InputIter>::value>::type*)
1511 : __base(__a)
1512{
1513 __append(__f, __l);
1514}
1515
1516template <class _Tp, class _Allocator>
1517deque<_Tp, _Allocator>::deque(const deque& __c)
1518 : __base(__alloc_traits::select_on_container_copy_construction(__c.__alloc()))
1519{
1520 __append(__c.begin(), __c.end());
1521}
1522
1523template <class _Tp, class _Allocator>
1524deque<_Tp, _Allocator>::deque(const deque& __c, const allocator_type& __a)
1525 : __base(__a)
1526{
1527 __append(__c.begin(), __c.end());
1528}
1529
Howard Hinnant33711792011-08-12 21:56:02 +00001530#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1531
Howard Hinnantc51e1022010-05-11 19:42:16 +00001532template <class _Tp, class _Allocator>
1533deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il)
1534{
1535 __append(__il.begin(), __il.end());
1536}
1537
1538template <class _Tp, class _Allocator>
1539deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il, const allocator_type& __a)
1540 : __base(__a)
1541{
1542 __append(__il.begin(), __il.end());
1543}
1544
Howard Hinnant33711792011-08-12 21:56:02 +00001545#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1546
Howard Hinnantc51e1022010-05-11 19:42:16 +00001547template <class _Tp, class _Allocator>
1548deque<_Tp, _Allocator>&
1549deque<_Tp, _Allocator>::operator=(const deque& __c)
1550{
1551 if (this != &__c)
1552 {
1553 __copy_assign_alloc(__c);
1554 assign(__c.begin(), __c.end());
1555 }
1556 return *this;
1557}
1558
Howard Hinnant74279a52010-09-04 23:28:19 +00001559#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001560
1561template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001562inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001563deque<_Tp, _Allocator>::deque(deque&& __c)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001564 _NOEXCEPT_(is_nothrow_move_constructible<__base>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001565 : __base(_VSTD::move(__c))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001566{
1567}
1568
1569template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001570inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001571deque<_Tp, _Allocator>::deque(deque&& __c, const allocator_type& __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001572 : __base(_VSTD::move(__c), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001573{
1574 if (__a != __c.__alloc())
1575 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001576 typedef move_iterator<iterator> _Ip;
1577 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001578 }
1579}
1580
1581template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001582inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001583deque<_Tp, _Allocator>&
1584deque<_Tp, _Allocator>::operator=(deque&& __c)
Howard Hinnant4931e092011-06-02 21:38:57 +00001585 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1586 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001587{
1588 __move_assign(__c, integral_constant<bool,
1589 __alloc_traits::propagate_on_container_move_assignment::value>());
1590 return *this;
1591}
1592
1593template <class _Tp, class _Allocator>
1594void
1595deque<_Tp, _Allocator>::__move_assign(deque& __c, false_type)
1596{
1597 if (__base::__alloc() != __c.__alloc())
1598 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001599 typedef move_iterator<iterator> _Ip;
1600 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001601 }
1602 else
1603 __move_assign(__c, true_type());
1604}
1605
1606template <class _Tp, class _Allocator>
1607void
1608deque<_Tp, _Allocator>::__move_assign(deque& __c, true_type)
Howard Hinnant4931e092011-06-02 21:38:57 +00001609 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001610{
1611 clear();
1612 shrink_to_fit();
1613 __base::__move_assign(__c);
1614}
1615
Howard Hinnant74279a52010-09-04 23:28:19 +00001616#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001617
1618template <class _Tp, class _Allocator>
1619template <class _InputIter>
1620void
1621deque<_Tp, _Allocator>::assign(_InputIter __f, _InputIter __l,
1622 typename enable_if<__is_input_iterator<_InputIter>::value &&
1623 !__is_random_access_iterator<_InputIter>::value>::type*)
1624{
1625 iterator __i = __base::begin();
1626 iterator __e = __base::end();
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001627 for (; __f != __l && __i != __e; ++__f, (void) ++__i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001628 *__i = *__f;
1629 if (__f != __l)
1630 __append(__f, __l);
1631 else
1632 __erase_to_end(__i);
1633}
1634
1635template <class _Tp, class _Allocator>
1636template <class _RAIter>
1637void
1638deque<_Tp, _Allocator>::assign(_RAIter __f, _RAIter __l,
1639 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
1640{
1641 if (static_cast<size_type>(__l - __f) > __base::size())
1642 {
1643 _RAIter __m = __f + __base::size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001644 _VSTD::copy(__f, __m, __base::begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001645 __append(__m, __l);
1646 }
1647 else
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001648 __erase_to_end(_VSTD::copy(__f, __l, __base::begin()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001649}
1650
1651template <class _Tp, class _Allocator>
1652void
1653deque<_Tp, _Allocator>::assign(size_type __n, const value_type& __v)
1654{
1655 if (__n > __base::size())
1656 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001657 _VSTD::fill_n(__base::begin(), __base::size(), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001658 __n -= __base::size();
1659 __append(__n, __v);
1660 }
1661 else
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001662 __erase_to_end(_VSTD::fill_n(__base::begin(), __n, __v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001663}
1664
1665template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001666inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001667_Allocator
Howard Hinnantda2df912011-06-02 16:10:22 +00001668deque<_Tp, _Allocator>::get_allocator() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001669{
1670 return __base::__alloc();
1671}
1672
1673template <class _Tp, class _Allocator>
1674void
1675deque<_Tp, _Allocator>::resize(size_type __n)
1676{
1677 if (__n > __base::size())
1678 __append(__n - __base::size());
1679 else if (__n < __base::size())
1680 __erase_to_end(__base::begin() + __n);
1681}
1682
1683template <class _Tp, class _Allocator>
1684void
1685deque<_Tp, _Allocator>::resize(size_type __n, const value_type& __v)
1686{
1687 if (__n > __base::size())
1688 __append(__n - __base::size(), __v);
1689 else if (__n < __base::size())
1690 __erase_to_end(__base::begin() + __n);
1691}
1692
1693template <class _Tp, class _Allocator>
1694void
Howard Hinnant4931e092011-06-02 21:38:57 +00001695deque<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001696{
1697 allocator_type& __a = __base::__alloc();
1698 if (empty())
1699 {
1700 while (__base::__map_.size() > 0)
1701 {
1702 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
1703 __base::__map_.pop_back();
1704 }
1705 __base::__start_ = 0;
1706 }
1707 else
1708 {
1709 if (__front_spare() >= __base::__block_size)
1710 {
1711 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
1712 __base::__map_.pop_front();
1713 __base::__start_ -= __base::__block_size;
1714 }
1715 if (__back_spare() >= __base::__block_size)
1716 {
1717 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
1718 __base::__map_.pop_back();
1719 }
1720 }
1721 __base::__map_.shrink_to_fit();
1722}
1723
1724template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001725inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001726typename deque<_Tp, _Allocator>::reference
1727deque<_Tp, _Allocator>::operator[](size_type __i)
1728{
1729 size_type __p = __base::__start_ + __i;
1730 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1731}
1732
1733template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001734inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001735typename deque<_Tp, _Allocator>::const_reference
1736deque<_Tp, _Allocator>::operator[](size_type __i) const
1737{
1738 size_type __p = __base::__start_ + __i;
1739 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1740}
1741
1742template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001743inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001744typename deque<_Tp, _Allocator>::reference
1745deque<_Tp, _Allocator>::at(size_type __i)
1746{
1747 if (__i >= __base::size())
1748 __base::__throw_out_of_range();
1749 size_type __p = __base::__start_ + __i;
1750 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1751}
1752
1753template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001754inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001755typename deque<_Tp, _Allocator>::const_reference
1756deque<_Tp, _Allocator>::at(size_type __i) const
1757{
1758 if (__i >= __base::size())
1759 __base::__throw_out_of_range();
1760 size_type __p = __base::__start_ + __i;
1761 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1762}
1763
1764template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001765inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001766typename deque<_Tp, _Allocator>::reference
1767deque<_Tp, _Allocator>::front()
1768{
1769 return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1770 + __base::__start_ % __base::__block_size);
1771}
1772
1773template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001774inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001775typename deque<_Tp, _Allocator>::const_reference
1776deque<_Tp, _Allocator>::front() const
1777{
1778 return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1779 + __base::__start_ % __base::__block_size);
1780}
1781
1782template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001783inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001784typename deque<_Tp, _Allocator>::reference
1785deque<_Tp, _Allocator>::back()
1786{
1787 size_type __p = __base::size() + __base::__start_ - 1;
1788 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1789}
1790
1791template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001792inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001793typename deque<_Tp, _Allocator>::const_reference
1794deque<_Tp, _Allocator>::back() const
1795{
1796 size_type __p = __base::size() + __base::__start_ - 1;
1797 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1798}
1799
1800template <class _Tp, class _Allocator>
1801void
1802deque<_Tp, _Allocator>::push_back(const value_type& __v)
1803{
1804 allocator_type& __a = __base::__alloc();
1805 if (__back_spare() == 0)
1806 __add_back_capacity();
1807 // __back_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001808 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001809 ++__base::size();
1810}
1811
Howard Hinnant74279a52010-09-04 23:28:19 +00001812#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001813
1814template <class _Tp, class _Allocator>
1815void
1816deque<_Tp, _Allocator>::push_back(value_type&& __v)
1817{
1818 allocator_type& __a = __base::__alloc();
1819 if (__back_spare() == 0)
1820 __add_back_capacity();
1821 // __back_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001822 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001823 ++__base::size();
1824}
1825
Howard Hinnant74279a52010-09-04 23:28:19 +00001826#ifndef _LIBCPP_HAS_NO_VARIADICS
1827
Howard Hinnantc51e1022010-05-11 19:42:16 +00001828template <class _Tp, class _Allocator>
1829template <class... _Args>
Marshall Clowea52cc42017-01-24 23:09:12 +00001830#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001831typename deque<_Tp, _Allocator>::reference
Marshall Clowea52cc42017-01-24 23:09:12 +00001832#else
1833void
1834#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001835deque<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1836{
1837 allocator_type& __a = __base::__alloc();
1838 if (__back_spare() == 0)
1839 __add_back_capacity();
1840 // __back_spare() >= 1
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001841 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()),
1842 _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001843 ++__base::size();
Marshall Clowea52cc42017-01-24 23:09:12 +00001844#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001845 return *--__base::end();
Marshall Clowea52cc42017-01-24 23:09:12 +00001846#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001847}
1848
Howard Hinnant74279a52010-09-04 23:28:19 +00001849#endif // _LIBCPP_HAS_NO_VARIADICS
1850#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001851
1852template <class _Tp, class _Allocator>
1853void
1854deque<_Tp, _Allocator>::push_front(const value_type& __v)
1855{
1856 allocator_type& __a = __base::__alloc();
1857 if (__front_spare() == 0)
1858 __add_front_capacity();
1859 // __front_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001860 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001861 --__base::__start_;
1862 ++__base::size();
1863}
1864
Howard Hinnant74279a52010-09-04 23:28:19 +00001865#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001866
1867template <class _Tp, class _Allocator>
1868void
1869deque<_Tp, _Allocator>::push_front(value_type&& __v)
1870{
1871 allocator_type& __a = __base::__alloc();
1872 if (__front_spare() == 0)
1873 __add_front_capacity();
1874 // __front_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001875 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001876 --__base::__start_;
1877 ++__base::size();
1878}
1879
Howard Hinnant74279a52010-09-04 23:28:19 +00001880#ifndef _LIBCPP_HAS_NO_VARIADICS
1881
Howard Hinnantc51e1022010-05-11 19:42:16 +00001882template <class _Tp, class _Allocator>
1883template <class... _Args>
Marshall Clowea52cc42017-01-24 23:09:12 +00001884#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001885typename deque<_Tp, _Allocator>::reference
Marshall Clowea52cc42017-01-24 23:09:12 +00001886#else
1887void
1888#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001889deque<_Tp, _Allocator>::emplace_front(_Args&&... __args)
1890{
1891 allocator_type& __a = __base::__alloc();
1892 if (__front_spare() == 0)
1893 __add_front_capacity();
1894 // __front_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001895 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001896 --__base::__start_;
1897 ++__base::size();
Marshall Clowea52cc42017-01-24 23:09:12 +00001898#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001899 return *__base::begin();
Marshall Clowea52cc42017-01-24 23:09:12 +00001900#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001901}
1902
Howard Hinnant74279a52010-09-04 23:28:19 +00001903#endif // _LIBCPP_HAS_NO_VARIADICS
1904#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001905
1906template <class _Tp, class _Allocator>
1907typename deque<_Tp, _Allocator>::iterator
1908deque<_Tp, _Allocator>::insert(const_iterator __p, const value_type& __v)
1909{
1910 size_type __pos = __p - __base::begin();
1911 size_type __to_end = __base::size() - __pos;
1912 allocator_type& __a = __base::__alloc();
1913 if (__pos < __to_end)
1914 { // insert by shifting things backward
1915 if (__front_spare() == 0)
1916 __add_front_capacity();
1917 // __front_spare() >= 1
1918 if (__pos == 0)
1919 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001920 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001921 --__base::__start_;
1922 ++__base::size();
1923 }
1924 else
1925 {
1926 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1927 iterator __b = __base::begin();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001928 iterator __bm1 = _VSTD::prev(__b);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001929 if (__vt == pointer_traits<const_pointer>::pointer_to(*__b))
1930 __vt = pointer_traits<const_pointer>::pointer_to(*__bm1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001931 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001932 --__base::__start_;
1933 ++__base::size();
1934 if (__pos > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001935 __b = __move_and_check(_VSTD::next(__b), __b + __pos, __b, __vt);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001936 *__b = *__vt;
1937 }
1938 }
1939 else
1940 { // insert by shifting things forward
1941 if (__back_spare() == 0)
1942 __add_back_capacity();
1943 // __back_capacity >= 1
1944 size_type __de = __base::size() - __pos;
1945 if (__de == 0)
1946 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001947 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001948 ++__base::size();
1949 }
1950 else
1951 {
1952 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1953 iterator __e = __base::end();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001954 iterator __em1 = _VSTD::prev(__e);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001955 if (__vt == pointer_traits<const_pointer>::pointer_to(*__em1))
1956 __vt = pointer_traits<const_pointer>::pointer_to(*__e);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001957 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001958 ++__base::size();
1959 if (__de > 1)
1960 __e = __move_backward_and_check(__e - __de, __em1, __e, __vt);
1961 *--__e = *__vt;
1962 }
1963 }
1964 return __base::begin() + __pos;
1965}
1966
Howard Hinnant74279a52010-09-04 23:28:19 +00001967#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001968
1969template <class _Tp, class _Allocator>
1970typename deque<_Tp, _Allocator>::iterator
1971deque<_Tp, _Allocator>::insert(const_iterator __p, value_type&& __v)
1972{
1973 size_type __pos = __p - __base::begin();
1974 size_type __to_end = __base::size() - __pos;
1975 allocator_type& __a = __base::__alloc();
1976 if (__pos < __to_end)
1977 { // insert by shifting things backward
1978 if (__front_spare() == 0)
1979 __add_front_capacity();
1980 // __front_spare() >= 1
1981 if (__pos == 0)
1982 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001983 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001984 --__base::__start_;
1985 ++__base::size();
1986 }
1987 else
1988 {
1989 iterator __b = __base::begin();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001990 iterator __bm1 = _VSTD::prev(__b);
1991 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001992 --__base::__start_;
1993 ++__base::size();
1994 if (__pos > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001995 __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
1996 *__b = _VSTD::move(__v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001997 }
1998 }
1999 else
2000 { // insert by shifting things forward
2001 if (__back_spare() == 0)
2002 __add_back_capacity();
2003 // __back_capacity >= 1
2004 size_type __de = __base::size() - __pos;
2005 if (__de == 0)
2006 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002007 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002008 ++__base::size();
2009 }
2010 else
2011 {
2012 iterator __e = __base::end();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002013 iterator __em1 = _VSTD::prev(__e);
2014 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002015 ++__base::size();
2016 if (__de > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002017 __e = _VSTD::move_backward(__e - __de, __em1, __e);
2018 *--__e = _VSTD::move(__v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002019 }
2020 }
2021 return __base::begin() + __pos;
2022}
2023
Howard Hinnant74279a52010-09-04 23:28:19 +00002024#ifndef _LIBCPP_HAS_NO_VARIADICS
2025
Howard Hinnantc51e1022010-05-11 19:42:16 +00002026template <class _Tp, class _Allocator>
2027template <class... _Args>
2028typename deque<_Tp, _Allocator>::iterator
2029deque<_Tp, _Allocator>::emplace(const_iterator __p, _Args&&... __args)
2030{
2031 size_type __pos = __p - __base::begin();
2032 size_type __to_end = __base::size() - __pos;
2033 allocator_type& __a = __base::__alloc();
2034 if (__pos < __to_end)
2035 { // insert by shifting things backward
2036 if (__front_spare() == 0)
2037 __add_front_capacity();
2038 // __front_spare() >= 1
2039 if (__pos == 0)
2040 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002041 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002042 --__base::__start_;
2043 ++__base::size();
2044 }
2045 else
2046 {
Marshall Clowa591b9a2016-07-11 21:38:08 +00002047 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002048 iterator __b = __base::begin();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002049 iterator __bm1 = _VSTD::prev(__b);
2050 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002051 --__base::__start_;
2052 ++__base::size();
2053 if (__pos > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002054 __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
Marshall Clowa591b9a2016-07-11 21:38:08 +00002055 *__b = _VSTD::move(__tmp.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002056 }
2057 }
2058 else
2059 { // insert by shifting things forward
2060 if (__back_spare() == 0)
2061 __add_back_capacity();
2062 // __back_capacity >= 1
2063 size_type __de = __base::size() - __pos;
2064 if (__de == 0)
2065 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002066 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002067 ++__base::size();
2068 }
2069 else
2070 {
Marshall Clowa591b9a2016-07-11 21:38:08 +00002071 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002072 iterator __e = __base::end();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002073 iterator __em1 = _VSTD::prev(__e);
2074 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002075 ++__base::size();
2076 if (__de > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002077 __e = _VSTD::move_backward(__e - __de, __em1, __e);
Marshall Clowa591b9a2016-07-11 21:38:08 +00002078 *--__e = _VSTD::move(__tmp.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002079 }
2080 }
2081 return __base::begin() + __pos;
2082}
2083
Howard Hinnant74279a52010-09-04 23:28:19 +00002084#endif // _LIBCPP_HAS_NO_VARIADICS
2085#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002086
2087template <class _Tp, class _Allocator>
2088typename deque<_Tp, _Allocator>::iterator
2089deque<_Tp, _Allocator>::insert(const_iterator __p, size_type __n, const value_type& __v)
2090{
2091 size_type __pos = __p - __base::begin();
2092 size_type __to_end = __base::size() - __pos;
2093 allocator_type& __a = __base::__alloc();
2094 if (__pos < __to_end)
2095 { // insert by shifting things backward
2096 if (__n > __front_spare())
2097 __add_front_capacity(__n - __front_spare());
2098 // __n <= __front_spare()
Howard Hinnantc51e1022010-05-11 19:42:16 +00002099 iterator __old_begin = __base::begin();
2100 iterator __i = __old_begin;
2101 if (__n > __pos)
2102 {
2103 for (size_type __m = __n - __pos; __m; --__m, --__base::__start_, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002104 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002105 __n = __pos;
2106 }
2107 if (__n > 0)
2108 {
2109 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2110 iterator __obn = __old_begin + __n;
2111 __move_construct_backward_and_check(__old_begin, __obn, __i, __vt);
2112 if (__n < __pos)
2113 __old_begin = __move_and_check(__obn, __old_begin + __pos, __old_begin, __vt);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002114 _VSTD::fill_n(__old_begin, __n, *__vt);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002115 }
2116 }
2117 else
2118 { // insert by shifting things forward
2119 size_type __back_capacity = __back_spare();
2120 if (__n > __back_capacity)
2121 __add_back_capacity(__n - __back_capacity);
2122 // __n <= __back_capacity
Howard Hinnantc51e1022010-05-11 19:42:16 +00002123 iterator __old_end = __base::end();
2124 iterator __i = __old_end;
2125 size_type __de = __base::size() - __pos;
2126 if (__n > __de)
2127 {
2128 for (size_type __m = __n - __de; __m; --__m, ++__i, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002129 __alloc_traits::construct(__a, _VSTD::addressof(*__i), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002130 __n = __de;
2131 }
2132 if (__n > 0)
2133 {
2134 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2135 iterator __oen = __old_end - __n;
2136 __move_construct_and_check(__oen, __old_end, __i, __vt);
2137 if (__n < __de)
2138 __old_end = __move_backward_and_check(__old_end - __de, __oen, __old_end, __vt);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002139 _VSTD::fill_n(__old_end - __n, __n, *__vt);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002140 }
2141 }
2142 return __base::begin() + __pos;
2143}
2144
2145template <class _Tp, class _Allocator>
2146template <class _InputIter>
2147typename deque<_Tp, _Allocator>::iterator
2148deque<_Tp, _Allocator>::insert(const_iterator __p, _InputIter __f, _InputIter __l,
2149 typename enable_if<__is_input_iterator<_InputIter>::value
Marshall Clow6b612cf2015-01-22 18:33:29 +00002150 &&!__is_forward_iterator<_InputIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002151{
2152 __split_buffer<value_type, allocator_type&> __buf(__base::__alloc());
2153 __buf.__construct_at_end(__f, __l);
2154 typedef typename __split_buffer<value_type, allocator_type&>::iterator __bi;
2155 return insert(__p, move_iterator<__bi>(__buf.begin()), move_iterator<__bi>(__buf.end()));
2156}
2157
2158template <class _Tp, class _Allocator>
Marshall Clow6b612cf2015-01-22 18:33:29 +00002159template <class _ForwardIterator>
2160typename deque<_Tp, _Allocator>::iterator
2161deque<_Tp, _Allocator>::insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l,
2162 typename enable_if<__is_forward_iterator<_ForwardIterator>::value
2163 &&!__is_bidirectional_iterator<_ForwardIterator>::value>::type*)
2164{
2165 size_type __n = _VSTD::distance(__f, __l);
2166 __split_buffer<value_type, allocator_type&> __buf(__n, 0, __base::__alloc());
2167 __buf.__construct_at_end(__f, __l);
2168 typedef typename __split_buffer<value_type, allocator_type&>::iterator __fwd;
2169 return insert(__p, move_iterator<__fwd>(__buf.begin()), move_iterator<__fwd>(__buf.end()));
2170}
2171
2172template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002173template <class _BiIter>
2174typename deque<_Tp, _Allocator>::iterator
2175deque<_Tp, _Allocator>::insert(const_iterator __p, _BiIter __f, _BiIter __l,
2176 typename enable_if<__is_bidirectional_iterator<_BiIter>::value>::type*)
2177{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002178 size_type __n = _VSTD::distance(__f, __l);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002179 size_type __pos = __p - __base::begin();
2180 size_type __to_end = __base::size() - __pos;
2181 allocator_type& __a = __base::__alloc();
2182 if (__pos < __to_end)
2183 { // insert by shifting things backward
2184 if (__n > __front_spare())
2185 __add_front_capacity(__n - __front_spare());
2186 // __n <= __front_spare()
Howard Hinnantc51e1022010-05-11 19:42:16 +00002187 iterator __old_begin = __base::begin();
2188 iterator __i = __old_begin;
2189 _BiIter __m = __f;
2190 if (__n > __pos)
2191 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002192 __m = __pos < __n / 2 ? _VSTD::prev(__l, __pos) : _VSTD::next(__f, __n - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002193 for (_BiIter __j = __m; __j != __f; --__base::__start_, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002194 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), *--__j);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002195 __n = __pos;
2196 }
2197 if (__n > 0)
2198 {
2199 iterator __obn = __old_begin + __n;
2200 for (iterator __j = __obn; __j != __old_begin;)
2201 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002202 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), _VSTD::move(*--__j));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002203 --__base::__start_;
2204 ++__base::size();
2205 }
2206 if (__n < __pos)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002207 __old_begin = _VSTD::move(__obn, __old_begin + __pos, __old_begin);
2208 _VSTD::copy(__m, __l, __old_begin);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002209 }
2210 }
2211 else
2212 { // insert by shifting things forward
2213 size_type __back_capacity = __back_spare();
2214 if (__n > __back_capacity)
2215 __add_back_capacity(__n - __back_capacity);
2216 // __n <= __back_capacity
Howard Hinnantc51e1022010-05-11 19:42:16 +00002217 iterator __old_end = __base::end();
2218 iterator __i = __old_end;
2219 _BiIter __m = __l;
2220 size_type __de = __base::size() - __pos;
2221 if (__n > __de)
2222 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002223 __m = __de < __n / 2 ? _VSTD::next(__f, __de) : _VSTD::prev(__l, __n - __de);
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002224 for (_BiIter __j = __m; __j != __l; ++__i, (void) ++__j, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002225 __alloc_traits::construct(__a, _VSTD::addressof(*__i), *__j);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002226 __n = __de;
2227 }
2228 if (__n > 0)
2229 {
2230 iterator __oen = __old_end - __n;
2231 for (iterator __j = __oen; __j != __old_end; ++__i, ++__j, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002232 __alloc_traits::construct(__a, _VSTD::addressof(*__i), _VSTD::move(*__j));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002233 if (__n < __de)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002234 __old_end = _VSTD::move_backward(__old_end - __de, __oen, __old_end);
2235 _VSTD::copy_backward(__f, __m, __old_end);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002236 }
2237 }
2238 return __base::begin() + __pos;
2239}
2240
2241template <class _Tp, class _Allocator>
2242template <class _InpIter>
2243void
2244deque<_Tp, _Allocator>::__append(_InpIter __f, _InpIter __l,
2245 typename enable_if<__is_input_iterator<_InpIter>::value &&
2246 !__is_forward_iterator<_InpIter>::value>::type*)
2247{
2248 for (; __f != __l; ++__f)
2249 push_back(*__f);
2250}
2251
2252template <class _Tp, class _Allocator>
2253template <class _ForIter>
2254void
2255deque<_Tp, _Allocator>::__append(_ForIter __f, _ForIter __l,
2256 typename enable_if<__is_forward_iterator<_ForIter>::value>::type*)
2257{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002258 size_type __n = _VSTD::distance(__f, __l);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002259 allocator_type& __a = __base::__alloc();
2260 size_type __back_capacity = __back_spare();
2261 if (__n > __back_capacity)
2262 __add_back_capacity(__n - __back_capacity);
2263 // __n <= __back_capacity
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002264 for (iterator __i = __base::end(); __f != __l; ++__i, (void) ++__f, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002265 __alloc_traits::construct(__a, _VSTD::addressof(*__i), *__f);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002266}
2267
2268template <class _Tp, class _Allocator>
2269void
2270deque<_Tp, _Allocator>::__append(size_type __n)
2271{
2272 allocator_type& __a = __base::__alloc();
2273 size_type __back_capacity = __back_spare();
2274 if (__n > __back_capacity)
2275 __add_back_capacity(__n - __back_capacity);
2276 // __n <= __back_capacity
2277 for (iterator __i = __base::end(); __n; --__n, ++__i, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002278 __alloc_traits::construct(__a, _VSTD::addressof(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002279}
2280
2281template <class _Tp, class _Allocator>
2282void
2283deque<_Tp, _Allocator>::__append(size_type __n, const value_type& __v)
2284{
2285 allocator_type& __a = __base::__alloc();
2286 size_type __back_capacity = __back_spare();
2287 if (__n > __back_capacity)
2288 __add_back_capacity(__n - __back_capacity);
2289 // __n <= __back_capacity
2290 for (iterator __i = __base::end(); __n; --__n, ++__i, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002291 __alloc_traits::construct(__a, _VSTD::addressof(*__i), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002292}
2293
2294// Create front capacity for one block of elements.
2295// Strong guarantee. Either do it or don't touch anything.
2296template <class _Tp, class _Allocator>
2297void
2298deque<_Tp, _Allocator>::__add_front_capacity()
2299{
2300 allocator_type& __a = __base::__alloc();
2301 if (__back_spare() >= __base::__block_size)
2302 {
2303 __base::__start_ += __base::__block_size;
2304 pointer __pt = __base::__map_.back();
2305 __base::__map_.pop_back();
2306 __base::__map_.push_front(__pt);
2307 }
2308 // Else if __base::__map_.size() < __base::__map_.capacity() then we need to allocate 1 buffer
2309 else if (__base::__map_.size() < __base::__map_.capacity())
2310 { // we can put the new buffer into the map, but don't shift things around
2311 // until all buffers are allocated. If we throw, we don't need to fix
2312 // anything up (any added buffers are undetectible)
2313 if (__base::__map_.__front_spare() > 0)
2314 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2315 else
2316 {
2317 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2318 // Done allocating, reorder capacity
2319 pointer __pt = __base::__map_.back();
2320 __base::__map_.pop_back();
2321 __base::__map_.push_front(__pt);
2322 }
2323 __base::__start_ = __base::__map_.size() == 1 ?
2324 __base::__block_size / 2 :
2325 __base::__start_ + __base::__block_size;
2326 }
2327 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2328 else
2329 {
2330 __split_buffer<pointer, typename __base::__pointer_allocator&>
2331 __buf(max<size_type>(2 * __base::__map_.capacity(), 1),
2332 0, __base::__map_.__alloc());
Marshall Clow5fd466b2015-03-09 17:08:51 +00002333
Marshall Clow8982dcd2015-07-13 20:04:56 +00002334 typedef __allocator_destructor<_Allocator> _Dp;
2335 unique_ptr<pointer, _Dp> __hold(
2336 __alloc_traits::allocate(__a, __base::__block_size),
2337 _Dp(__a, __base::__block_size));
2338 __buf.push_back(__hold.get());
2339 __hold.release();
2340
Howard Hinnantc51e1022010-05-11 19:42:16 +00002341 for (typename __base::__map_pointer __i = __base::__map_.begin();
2342 __i != __base::__map_.end(); ++__i)
2343 __buf.push_back(*__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002344 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2345 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2346 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2347 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002348 __base::__start_ = __base::__map_.size() == 1 ?
2349 __base::__block_size / 2 :
2350 __base::__start_ + __base::__block_size;
2351 }
2352}
2353
2354// Create front capacity for __n elements.
2355// Strong guarantee. Either do it or don't touch anything.
2356template <class _Tp, class _Allocator>
2357void
2358deque<_Tp, _Allocator>::__add_front_capacity(size_type __n)
2359{
2360 allocator_type& __a = __base::__alloc();
2361 size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2362 // Number of unused blocks at back:
2363 size_type __back_capacity = __back_spare() / __base::__block_size;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002364 __back_capacity = _VSTD::min(__back_capacity, __nb); // don't take more than you need
Howard Hinnantc51e1022010-05-11 19:42:16 +00002365 __nb -= __back_capacity; // number of blocks need to allocate
2366 // If __nb == 0, then we have sufficient capacity.
2367 if (__nb == 0)
2368 {
2369 __base::__start_ += __base::__block_size * __back_capacity;
2370 for (; __back_capacity > 0; --__back_capacity)
2371 {
2372 pointer __pt = __base::__map_.back();
2373 __base::__map_.pop_back();
2374 __base::__map_.push_front(__pt);
2375 }
2376 }
2377 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2378 else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2379 { // we can put the new buffers into the map, but don't shift things around
2380 // until all buffers are allocated. If we throw, we don't need to fix
2381 // anything up (any added buffers are undetectible)
2382 for (; __nb > 0; --__nb, __base::__start_ += __base::__block_size - (__base::__map_.size() == 1))
2383 {
2384 if (__base::__map_.__front_spare() == 0)
2385 break;
2386 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2387 }
2388 for (; __nb > 0; --__nb, ++__back_capacity)
2389 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2390 // Done allocating, reorder capacity
2391 __base::__start_ += __back_capacity * __base::__block_size;
2392 for (; __back_capacity > 0; --__back_capacity)
2393 {
2394 pointer __pt = __base::__map_.back();
2395 __base::__map_.pop_back();
2396 __base::__map_.push_front(__pt);
2397 }
2398 }
2399 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2400 else
2401 {
2402 size_type __ds = (__nb + __back_capacity) * __base::__block_size - __base::__map_.empty();
2403 __split_buffer<pointer, typename __base::__pointer_allocator&>
2404 __buf(max<size_type>(2* __base::__map_.capacity(),
2405 __nb + __base::__map_.size()),
2406 0, __base::__map_.__alloc());
2407#ifndef _LIBCPP_NO_EXCEPTIONS
2408 try
2409 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002410#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002411 for (; __nb > 0; --__nb)
2412 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2413#ifndef _LIBCPP_NO_EXCEPTIONS
2414 }
2415 catch (...)
2416 {
2417 for (typename __base::__map_pointer __i = __buf.begin();
2418 __i != __buf.end(); ++__i)
2419 __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2420 throw;
2421 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002422#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002423 for (; __back_capacity > 0; --__back_capacity)
2424 {
2425 __buf.push_back(__base::__map_.back());
2426 __base::__map_.pop_back();
2427 }
2428 for (typename __base::__map_pointer __i = __base::__map_.begin();
2429 __i != __base::__map_.end(); ++__i)
2430 __buf.push_back(*__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002431 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2432 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2433 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2434 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002435 __base::__start_ += __ds;
2436 }
2437}
2438
2439// Create back capacity for one block of elements.
2440// Strong guarantee. Either do it or don't touch anything.
2441template <class _Tp, class _Allocator>
2442void
2443deque<_Tp, _Allocator>::__add_back_capacity()
2444{
2445 allocator_type& __a = __base::__alloc();
2446 if (__front_spare() >= __base::__block_size)
2447 {
2448 __base::__start_ -= __base::__block_size;
2449 pointer __pt = __base::__map_.front();
2450 __base::__map_.pop_front();
2451 __base::__map_.push_back(__pt);
2452 }
2453 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2454 else if (__base::__map_.size() < __base::__map_.capacity())
2455 { // we can put the new buffer into the map, but don't shift things around
2456 // until it is allocated. If we throw, we don't need to fix
2457 // anything up (any added buffers are undetectible)
2458 if (__base::__map_.__back_spare() != 0)
2459 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2460 else
2461 {
2462 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2463 // Done allocating, reorder capacity
2464 pointer __pt = __base::__map_.front();
2465 __base::__map_.pop_front();
2466 __base::__map_.push_back(__pt);
2467 }
2468 }
2469 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2470 else
2471 {
2472 __split_buffer<pointer, typename __base::__pointer_allocator&>
2473 __buf(max<size_type>(2* __base::__map_.capacity(), 1),
2474 __base::__map_.size(),
2475 __base::__map_.__alloc());
Marshall Clow5fd466b2015-03-09 17:08:51 +00002476
Marshall Clow8982dcd2015-07-13 20:04:56 +00002477 typedef __allocator_destructor<_Allocator> _Dp;
2478 unique_ptr<pointer, _Dp> __hold(
2479 __alloc_traits::allocate(__a, __base::__block_size),
2480 _Dp(__a, __base::__block_size));
2481 __buf.push_back(__hold.get());
2482 __hold.release();
Marshall Clow5fd466b2015-03-09 17:08:51 +00002483
Howard Hinnantc51e1022010-05-11 19:42:16 +00002484 for (typename __base::__map_pointer __i = __base::__map_.end();
2485 __i != __base::__map_.begin();)
2486 __buf.push_front(*--__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002487 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2488 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2489 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2490 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002491 }
2492}
2493
2494// Create back capacity for __n elements.
2495// Strong guarantee. Either do it or don't touch anything.
2496template <class _Tp, class _Allocator>
2497void
2498deque<_Tp, _Allocator>::__add_back_capacity(size_type __n)
2499{
2500 allocator_type& __a = __base::__alloc();
2501 size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2502 // Number of unused blocks at front:
2503 size_type __front_capacity = __front_spare() / __base::__block_size;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002504 __front_capacity = _VSTD::min(__front_capacity, __nb); // don't take more than you need
Howard Hinnantc51e1022010-05-11 19:42:16 +00002505 __nb -= __front_capacity; // number of blocks need to allocate
2506 // If __nb == 0, then we have sufficient capacity.
2507 if (__nb == 0)
2508 {
2509 __base::__start_ -= __base::__block_size * __front_capacity;
2510 for (; __front_capacity > 0; --__front_capacity)
2511 {
2512 pointer __pt = __base::__map_.front();
2513 __base::__map_.pop_front();
2514 __base::__map_.push_back(__pt);
2515 }
2516 }
2517 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2518 else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2519 { // we can put the new buffers into the map, but don't shift things around
2520 // until all buffers are allocated. If we throw, we don't need to fix
2521 // anything up (any added buffers are undetectible)
2522 for (; __nb > 0; --__nb)
2523 {
2524 if (__base::__map_.__back_spare() == 0)
2525 break;
2526 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2527 }
2528 for (; __nb > 0; --__nb, ++__front_capacity, __base::__start_ +=
2529 __base::__block_size - (__base::__map_.size() == 1))
2530 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2531 // Done allocating, reorder capacity
2532 __base::__start_ -= __base::__block_size * __front_capacity;
2533 for (; __front_capacity > 0; --__front_capacity)
2534 {
2535 pointer __pt = __base::__map_.front();
2536 __base::__map_.pop_front();
2537 __base::__map_.push_back(__pt);
2538 }
2539 }
2540 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2541 else
2542 {
2543 size_type __ds = __front_capacity * __base::__block_size;
2544 __split_buffer<pointer, typename __base::__pointer_allocator&>
2545 __buf(max<size_type>(2* __base::__map_.capacity(),
2546 __nb + __base::__map_.size()),
2547 __base::__map_.size() - __front_capacity,
2548 __base::__map_.__alloc());
2549#ifndef _LIBCPP_NO_EXCEPTIONS
2550 try
2551 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002552#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002553 for (; __nb > 0; --__nb)
2554 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2555#ifndef _LIBCPP_NO_EXCEPTIONS
2556 }
2557 catch (...)
2558 {
2559 for (typename __base::__map_pointer __i = __buf.begin();
2560 __i != __buf.end(); ++__i)
2561 __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2562 throw;
2563 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002564#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002565 for (; __front_capacity > 0; --__front_capacity)
2566 {
2567 __buf.push_back(__base::__map_.front());
2568 __base::__map_.pop_front();
2569 }
2570 for (typename __base::__map_pointer __i = __base::__map_.end();
2571 __i != __base::__map_.begin();)
2572 __buf.push_front(*--__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002573 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2574 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2575 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2576 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002577 __base::__start_ -= __ds;
2578 }
2579}
2580
2581template <class _Tp, class _Allocator>
2582void
2583deque<_Tp, _Allocator>::pop_front()
2584{
2585 allocator_type& __a = __base::__alloc();
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002586 __alloc_traits::destroy(__a, __to_raw_pointer(*(__base::__map_.begin() +
2587 __base::__start_ / __base::__block_size) +
2588 __base::__start_ % __base::__block_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002589 --__base::size();
2590 if (++__base::__start_ >= 2 * __base::__block_size)
2591 {
2592 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2593 __base::__map_.pop_front();
2594 __base::__start_ -= __base::__block_size;
2595 }
2596}
2597
2598template <class _Tp, class _Allocator>
2599void
2600deque<_Tp, _Allocator>::pop_back()
2601{
2602 allocator_type& __a = __base::__alloc();
2603 size_type __p = __base::size() + __base::__start_ - 1;
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002604 __alloc_traits::destroy(__a, __to_raw_pointer(*(__base::__map_.begin() +
2605 __p / __base::__block_size) +
2606 __p % __base::__block_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002607 --__base::size();
2608 if (__back_spare() >= 2 * __base::__block_size)
2609 {
2610 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2611 __base::__map_.pop_back();
2612 }
2613}
2614
2615// move assign [__f, __l) to [__r, __r + (__l-__f)).
2616// If __vt points into [__f, __l), then subtract (__f - __r) from __vt.
2617template <class _Tp, class _Allocator>
2618typename deque<_Tp, _Allocator>::iterator
2619deque<_Tp, _Allocator>::__move_and_check(iterator __f, iterator __l, iterator __r,
2620 const_pointer& __vt)
2621{
2622 // as if
2623 // for (; __f != __l; ++__f, ++__r)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002624 // *__r = _VSTD::move(*__f);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002625 difference_type __n = __l - __f;
2626 while (__n > 0)
2627 {
2628 pointer __fb = __f.__ptr_;
2629 pointer __fe = *__f.__m_iter_ + __base::__block_size;
2630 difference_type __bs = __fe - __fb;
2631 if (__bs > __n)
2632 {
2633 __bs = __n;
2634 __fe = __fb + __bs;
2635 }
2636 if (__fb <= __vt && __vt < __fe)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002637 __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) -= __f - __r).__ptr_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002638 __r = _VSTD::move(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002639 __n -= __bs;
2640 __f += __bs;
2641 }
2642 return __r;
2643}
2644
2645// move assign [__f, __l) to [__r - (__l-__f), __r) backwards.
2646// If __vt points into [__f, __l), then add (__r - __l) to __vt.
2647template <class _Tp, class _Allocator>
2648typename deque<_Tp, _Allocator>::iterator
2649deque<_Tp, _Allocator>::__move_backward_and_check(iterator __f, iterator __l, iterator __r,
2650 const_pointer& __vt)
2651{
2652 // as if
2653 // while (__f != __l)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002654 // *--__r = _VSTD::move(*--__l);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002655 difference_type __n = __l - __f;
2656 while (__n > 0)
2657 {
2658 --__l;
2659 pointer __lb = *__l.__m_iter_;
2660 pointer __le = __l.__ptr_ + 1;
2661 difference_type __bs = __le - __lb;
2662 if (__bs > __n)
2663 {
2664 __bs = __n;
2665 __lb = __le - __bs;
2666 }
2667 if (__lb <= __vt && __vt < __le)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002668 __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) += __r - __l - 1).__ptr_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002669 __r = _VSTD::move_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002670 __n -= __bs;
2671 __l -= __bs - 1;
2672 }
2673 return __r;
2674}
2675
2676// move construct [__f, __l) to [__r, __r + (__l-__f)).
2677// If __vt points into [__f, __l), then add (__r - __f) to __vt.
2678template <class _Tp, class _Allocator>
2679void
2680deque<_Tp, _Allocator>::__move_construct_and_check(iterator __f, iterator __l,
2681 iterator __r, const_pointer& __vt)
2682{
2683 allocator_type& __a = __base::__alloc();
2684 // as if
2685 // for (; __f != __l; ++__r, ++__f, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002686 // __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__f));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002687 difference_type __n = __l - __f;
2688 while (__n > 0)
2689 {
2690 pointer __fb = __f.__ptr_;
2691 pointer __fe = *__f.__m_iter_ + __base::__block_size;
2692 difference_type __bs = __fe - __fb;
2693 if (__bs > __n)
2694 {
2695 __bs = __n;
2696 __fe = __fb + __bs;
2697 }
2698 if (__fb <= __vt && __vt < __fe)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002699 __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) += __r - __f).__ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002700 for (; __fb != __fe; ++__fb, ++__r, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002701 __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__fb));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002702 __n -= __bs;
2703 __f += __bs;
2704 }
2705}
2706
2707// move construct [__f, __l) to [__r - (__l-__f), __r) backwards.
2708// If __vt points into [__f, __l), then subtract (__l - __r) from __vt.
2709template <class _Tp, class _Allocator>
2710void
2711deque<_Tp, _Allocator>::__move_construct_backward_and_check(iterator __f, iterator __l,
2712 iterator __r, const_pointer& __vt)
2713{
2714 allocator_type& __a = __base::__alloc();
2715 // as if
2716 // for (iterator __j = __l; __j != __f;)
2717 // {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002718 // __alloc_traitsconstruct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__j));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002719 // --__base::__start_;
2720 // ++__base::size();
2721 // }
2722 difference_type __n = __l - __f;
2723 while (__n > 0)
2724 {
2725 --__l;
2726 pointer __lb = *__l.__m_iter_;
2727 pointer __le = __l.__ptr_ + 1;
2728 difference_type __bs = __le - __lb;
2729 if (__bs > __n)
2730 {
2731 __bs = __n;
2732 __lb = __le - __bs;
2733 }
2734 if (__lb <= __vt && __vt < __le)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002735 __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) -= __l - __r + 1).__ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002736 while (__le != __lb)
2737 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002738 __alloc_traits::construct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__le));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002739 --__base::__start_;
2740 ++__base::size();
2741 }
2742 __n -= __bs;
2743 __l -= __bs - 1;
2744 }
2745}
2746
2747template <class _Tp, class _Allocator>
2748typename deque<_Tp, _Allocator>::iterator
2749deque<_Tp, _Allocator>::erase(const_iterator __f)
2750{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002751 iterator __b = __base::begin();
2752 difference_type __pos = __f - __b;
2753 iterator __p = __b + __pos;
2754 allocator_type& __a = __base::__alloc();
Eric Fiselier37c22152016-12-24 00:24:44 +00002755 if (static_cast<size_t>(__pos) <= (__base::size() - 1) / 2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002756 { // erase from front
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002757 _VSTD::move_backward(__b, __p, _VSTD::next(__p));
2758 __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002759 --__base::size();
2760 ++__base::__start_;
2761 if (__front_spare() >= 2 * __base::__block_size)
2762 {
2763 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2764 __base::__map_.pop_front();
2765 __base::__start_ -= __base::__block_size;
2766 }
2767 }
2768 else
2769 { // erase from back
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002770 iterator __i = _VSTD::move(_VSTD::next(__p), __base::end(), __p);
2771 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002772 --__base::size();
2773 if (__back_spare() >= 2 * __base::__block_size)
2774 {
2775 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2776 __base::__map_.pop_back();
2777 }
2778 }
2779 return __base::begin() + __pos;
2780}
2781
2782template <class _Tp, class _Allocator>
2783typename deque<_Tp, _Allocator>::iterator
2784deque<_Tp, _Allocator>::erase(const_iterator __f, const_iterator __l)
2785{
2786 difference_type __n = __l - __f;
2787 iterator __b = __base::begin();
2788 difference_type __pos = __f - __b;
2789 iterator __p = __b + __pos;
2790 if (__n > 0)
2791 {
2792 allocator_type& __a = __base::__alloc();
Eric Fiselier37c22152016-12-24 00:24:44 +00002793 if (static_cast<size_t>(__pos) <= (__base::size() - __n) / 2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002794 { // erase from front
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002795 iterator __i = _VSTD::move_backward(__b, __p, __p + __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002796 for (; __b != __i; ++__b)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002797 __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002798 __base::size() -= __n;
2799 __base::__start_ += __n;
2800 while (__front_spare() >= 2 * __base::__block_size)
2801 {
2802 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2803 __base::__map_.pop_front();
2804 __base::__start_ -= __base::__block_size;
2805 }
2806 }
2807 else
2808 { // erase from back
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002809 iterator __i = _VSTD::move(__p + __n, __base::end(), __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002810 for (iterator __e = __base::end(); __i != __e; ++__i)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002811 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002812 __base::size() -= __n;
2813 while (__back_spare() >= 2 * __base::__block_size)
2814 {
2815 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2816 __base::__map_.pop_back();
2817 }
2818 }
2819 }
2820 return __base::begin() + __pos;
2821}
2822
2823template <class _Tp, class _Allocator>
2824void
2825deque<_Tp, _Allocator>::__erase_to_end(const_iterator __f)
2826{
2827 iterator __e = __base::end();
2828 difference_type __n = __e - __f;
2829 if (__n > 0)
2830 {
2831 allocator_type& __a = __base::__alloc();
2832 iterator __b = __base::begin();
2833 difference_type __pos = __f - __b;
2834 for (iterator __p = __b + __pos; __p != __e; ++__p)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002835 __alloc_traits::destroy(__a, _VSTD::addressof(*__p));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002836 __base::size() -= __n;
2837 while (__back_spare() >= 2 * __base::__block_size)
2838 {
2839 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2840 __base::__map_.pop_back();
2841 }
2842 }
2843}
2844
2845template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002846inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002847void
2848deque<_Tp, _Allocator>::swap(deque& __c)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002849#if _LIBCPP_STD_VER >= 14
2850 _NOEXCEPT
2851#else
2852 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2853 __is_nothrow_swappable<allocator_type>::value)
2854#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002855{
2856 __base::swap(__c);
2857}
2858
2859template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002860inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002861void
Howard Hinnantda2df912011-06-02 16:10:22 +00002862deque<_Tp, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002863{
2864 __base::clear();
2865}
2866
2867template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002868inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002869bool
2870operator==(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2871{
2872 const typename deque<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002873 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002874}
2875
2876template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002877inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002878bool
2879operator!=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2880{
2881 return !(__x == __y);
2882}
2883
2884template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002885inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002886bool
2887operator< (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2888{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002889 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002890}
2891
2892template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002893inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002894bool
2895operator> (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2896{
2897 return __y < __x;
2898}
2899
2900template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002901inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002902bool
2903operator>=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2904{
2905 return !(__x < __y);
2906}
2907
2908template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002909inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002910bool
2911operator<=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2912{
2913 return !(__y < __x);
2914}
2915
2916template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002917inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002918void
2919swap(deque<_Tp, _Allocator>& __x, deque<_Tp, _Allocator>& __y)
Howard Hinnantca2fc222011-06-02 20:00:14 +00002920 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002921{
2922 __x.swap(__y);
2923}
2924
2925_LIBCPP_END_NAMESPACE_STD
2926
2927#endif // _LIBCPP_DEQUE