blob: 9c72d24c2893716f8220184d149855550bbbb251 [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);
Eric Fiselier34ba5b92016-07-21 03:20:17 +0000113 template <class... Args> reference emplace_front(Args&&... args);
114 template <class... Args> reference emplace_back(Args&&... args);
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;
Marshall Clow65cd4c62015-02-18 17:24:08 +0000170template <class _Tp, class _Allocator = allocator<_Tp> > class _LIBCPP_TYPE_VIS_ONLY deque;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000171
172template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
173 class _DiffType, _DiffType _BlockSize>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000174class _LIBCPP_TYPE_VIS_ONLY __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 >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000279class _LIBCPP_TYPE_VIS_ONLY __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;
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000431 template <class _Tp, class _Ap> friend class _LIBCPP_TYPE_VIS_ONLY deque;
Howard Hinnantc834c512011-11-29 18:15:50 +0000432 template <class _Vp, class _Pp, class _Rp, class _MP, class _Dp, _Dp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000433 friend class _LIBCPP_TYPE_VIS_ONLY __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>*/>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001186class _LIBCPP_TYPE_VIS_ONLY 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
1313 {return __alloc_traits::max_size(__base::__alloc());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001314 void resize(size_type __n);
1315 void resize(size_type __n, const value_type& __v);
Howard Hinnant4931e092011-06-02 21:38:57 +00001316 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantda2df912011-06-02 16:10:22 +00001317 _LIBCPP_INLINE_VISIBILITY
1318 bool empty() const _NOEXCEPT {return __base::size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001319
1320 // element access:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001321 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001322 reference operator[](size_type __i);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001323 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001324 const_reference operator[](size_type __i) const;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001325 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001326 reference at(size_type __i);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001327 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001328 const_reference at(size_type __i) const;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001329 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001330 reference front();
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001331 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001332 const_reference front() const;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001333 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001334 reference back();
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001335 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001336 const_reference back() const;
1337
1338 // 23.2.2.3 modifiers:
1339 void push_front(const value_type& __v);
1340 void push_back(const value_type& __v);
Howard Hinnant74279a52010-09-04 23:28:19 +00001341#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1342#ifndef _LIBCPP_HAS_NO_VARIADICS
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001343 template <class... _Args> reference emplace_front(_Args&&... __args);
1344 template <class... _Args> reference emplace_back(_Args&&... __args);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001345 template <class... _Args> iterator emplace(const_iterator __p, _Args&&... __args);
Howard Hinnant74279a52010-09-04 23:28:19 +00001346#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001347 void push_front(value_type&& __v);
1348 void push_back(value_type&& __v);
1349 iterator insert(const_iterator __p, value_type&& __v);
Howard Hinnant74279a52010-09-04 23:28:19 +00001350#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001351 iterator insert(const_iterator __p, const value_type& __v);
1352 iterator insert(const_iterator __p, size_type __n, const value_type& __v);
1353 template <class _InputIter>
Marshall Clow6b612cf2015-01-22 18:33:29 +00001354 iterator insert(const_iterator __p, _InputIter __f, _InputIter __l,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001355 typename enable_if<__is_input_iterator<_InputIter>::value
Marshall Clow6b612cf2015-01-22 18:33:29 +00001356 &&!__is_forward_iterator<_InputIter>::value>::type* = 0);
1357 template <class _ForwardIterator>
1358 iterator insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l,
1359 typename enable_if<__is_forward_iterator<_ForwardIterator>::value
1360 &&!__is_bidirectional_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001361 template <class _BiIter>
Marshall Clow6b612cf2015-01-22 18:33:29 +00001362 iterator insert(const_iterator __p, _BiIter __f, _BiIter __l,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001363 typename enable_if<__is_bidirectional_iterator<_BiIter>::value>::type* = 0);
Howard Hinnant33711792011-08-12 21:56:02 +00001364#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001365 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001366 iterator insert(const_iterator __p, initializer_list<value_type> __il)
1367 {return insert(__p, __il.begin(), __il.end());}
Howard Hinnant33711792011-08-12 21:56:02 +00001368#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001369 void pop_front();
1370 void pop_back();
1371 iterator erase(const_iterator __p);
1372 iterator erase(const_iterator __f, const_iterator __l);
1373
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001374 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantca2fc222011-06-02 20:00:14 +00001375 void swap(deque& __c)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001376#if _LIBCPP_STD_VER >= 14
1377 _NOEXCEPT;
1378#else
Howard Hinnantca2fc222011-06-02 20:00:14 +00001379 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1380 __is_nothrow_swappable<allocator_type>::value);
Marshall Clow8982dcd2015-07-13 20:04:56 +00001381#endif
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001383 void clear() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001384
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001385 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001386 bool __invariants() const {return __base::__invariants();}
1387private:
Howard Hinnantdcb73a32013-06-23 21:17:24 +00001388 typedef typename __base::__map_const_pointer __map_const_pointer;
1389
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001390 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001391 static size_type __recommend_blocks(size_type __n)
1392 {
1393 return __n / __base::__block_size + (__n % __base::__block_size != 0);
1394 }
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001395 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001396 size_type __capacity() const
1397 {
1398 return __base::__map_.size() == 0 ? 0 : __base::__map_.size() * __base::__block_size - 1;
1399 }
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001400 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001401 size_type __front_spare() const
1402 {
1403 return __base::__start_;
1404 }
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001405 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001406 size_type __back_spare() const
1407 {
1408 return __capacity() - (__base::__start_ + __base::size());
1409 }
1410
1411 template <class _InpIter>
1412 void __append(_InpIter __f, _InpIter __l,
1413 typename enable_if<__is_input_iterator<_InpIter>::value &&
1414 !__is_forward_iterator<_InpIter>::value>::type* = 0);
1415 template <class _ForIter>
1416 void __append(_ForIter __f, _ForIter __l,
1417 typename enable_if<__is_forward_iterator<_ForIter>::value>::type* = 0);
1418 void __append(size_type __n);
1419 void __append(size_type __n, const value_type& __v);
1420 void __erase_to_end(const_iterator __f);
1421 void __add_front_capacity();
1422 void __add_front_capacity(size_type __n);
1423 void __add_back_capacity();
1424 void __add_back_capacity(size_type __n);
1425 iterator __move_and_check(iterator __f, iterator __l, iterator __r,
1426 const_pointer& __vt);
1427 iterator __move_backward_and_check(iterator __f, iterator __l, iterator __r,
1428 const_pointer& __vt);
1429 void __move_construct_and_check(iterator __f, iterator __l,
1430 iterator __r, const_pointer& __vt);
1431 void __move_construct_backward_and_check(iterator __f, iterator __l,
1432 iterator __r, const_pointer& __vt);
1433
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001434 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001435 void __copy_assign_alloc(const deque& __c)
1436 {__copy_assign_alloc(__c, integral_constant<bool,
1437 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1438
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001439 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001440 void __copy_assign_alloc(const deque& __c, true_type)
1441 {
1442 if (__base::__alloc() != __c.__alloc())
1443 {
1444 clear();
1445 shrink_to_fit();
1446 }
1447 __base::__alloc() = __c.__alloc();
1448 __base::__map_.__alloc() = __c.__map_.__alloc();
1449 }
1450
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001451 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001452 void __copy_assign_alloc(const deque&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001453 {}
1454
Howard Hinnant4931e092011-06-02 21:38:57 +00001455 void __move_assign(deque& __c, true_type)
1456 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001457 void __move_assign(deque& __c, false_type);
1458};
1459
1460template <class _Tp, class _Allocator>
1461deque<_Tp, _Allocator>::deque(size_type __n)
1462{
1463 if (__n > 0)
1464 __append(__n);
1465}
1466
Marshall Clowbc4fcb42013-09-07 16:16:19 +00001467#if _LIBCPP_STD_VER > 11
1468template <class _Tp, class _Allocator>
1469deque<_Tp, _Allocator>::deque(size_type __n, const _Allocator& __a)
1470 : __base(__a)
1471{
1472 if (__n > 0)
1473 __append(__n);
1474}
1475#endif
1476
Howard Hinnantc51e1022010-05-11 19:42:16 +00001477template <class _Tp, class _Allocator>
1478deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v)
1479{
1480 if (__n > 0)
1481 __append(__n, __v);
1482}
1483
1484template <class _Tp, class _Allocator>
1485deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v, const allocator_type& __a)
1486 : __base(__a)
1487{
1488 if (__n > 0)
1489 __append(__n, __v);
1490}
1491
1492template <class _Tp, class _Allocator>
1493template <class _InputIter>
1494deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l,
1495 typename enable_if<__is_input_iterator<_InputIter>::value>::type*)
1496{
1497 __append(__f, __l);
1498}
1499
1500template <class _Tp, class _Allocator>
1501template <class _InputIter>
1502deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
1503 typename enable_if<__is_input_iterator<_InputIter>::value>::type*)
1504 : __base(__a)
1505{
1506 __append(__f, __l);
1507}
1508
1509template <class _Tp, class _Allocator>
1510deque<_Tp, _Allocator>::deque(const deque& __c)
1511 : __base(__alloc_traits::select_on_container_copy_construction(__c.__alloc()))
1512{
1513 __append(__c.begin(), __c.end());
1514}
1515
1516template <class _Tp, class _Allocator>
1517deque<_Tp, _Allocator>::deque(const deque& __c, const allocator_type& __a)
1518 : __base(__a)
1519{
1520 __append(__c.begin(), __c.end());
1521}
1522
Howard Hinnant33711792011-08-12 21:56:02 +00001523#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1524
Howard Hinnantc51e1022010-05-11 19:42:16 +00001525template <class _Tp, class _Allocator>
1526deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il)
1527{
1528 __append(__il.begin(), __il.end());
1529}
1530
1531template <class _Tp, class _Allocator>
1532deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il, const allocator_type& __a)
1533 : __base(__a)
1534{
1535 __append(__il.begin(), __il.end());
1536}
1537
Howard Hinnant33711792011-08-12 21:56:02 +00001538#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1539
Howard Hinnantc51e1022010-05-11 19:42:16 +00001540template <class _Tp, class _Allocator>
1541deque<_Tp, _Allocator>&
1542deque<_Tp, _Allocator>::operator=(const deque& __c)
1543{
1544 if (this != &__c)
1545 {
1546 __copy_assign_alloc(__c);
1547 assign(__c.begin(), __c.end());
1548 }
1549 return *this;
1550}
1551
Howard Hinnant74279a52010-09-04 23:28:19 +00001552#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001553
1554template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001555inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001556deque<_Tp, _Allocator>::deque(deque&& __c)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001557 _NOEXCEPT_(is_nothrow_move_constructible<__base>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001558 : __base(_VSTD::move(__c))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001559{
1560}
1561
1562template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001563inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001564deque<_Tp, _Allocator>::deque(deque&& __c, const allocator_type& __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001565 : __base(_VSTD::move(__c), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001566{
1567 if (__a != __c.__alloc())
1568 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001569 typedef move_iterator<iterator> _Ip;
1570 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001571 }
1572}
1573
1574template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001575inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001576deque<_Tp, _Allocator>&
1577deque<_Tp, _Allocator>::operator=(deque&& __c)
Howard Hinnant4931e092011-06-02 21:38:57 +00001578 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1579 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580{
1581 __move_assign(__c, integral_constant<bool,
1582 __alloc_traits::propagate_on_container_move_assignment::value>());
1583 return *this;
1584}
1585
1586template <class _Tp, class _Allocator>
1587void
1588deque<_Tp, _Allocator>::__move_assign(deque& __c, false_type)
1589{
1590 if (__base::__alloc() != __c.__alloc())
1591 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001592 typedef move_iterator<iterator> _Ip;
1593 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001594 }
1595 else
1596 __move_assign(__c, true_type());
1597}
1598
1599template <class _Tp, class _Allocator>
1600void
1601deque<_Tp, _Allocator>::__move_assign(deque& __c, true_type)
Howard Hinnant4931e092011-06-02 21:38:57 +00001602 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001603{
1604 clear();
1605 shrink_to_fit();
1606 __base::__move_assign(__c);
1607}
1608
Howard Hinnant74279a52010-09-04 23:28:19 +00001609#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001610
1611template <class _Tp, class _Allocator>
1612template <class _InputIter>
1613void
1614deque<_Tp, _Allocator>::assign(_InputIter __f, _InputIter __l,
1615 typename enable_if<__is_input_iterator<_InputIter>::value &&
1616 !__is_random_access_iterator<_InputIter>::value>::type*)
1617{
1618 iterator __i = __base::begin();
1619 iterator __e = __base::end();
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001620 for (; __f != __l && __i != __e; ++__f, (void) ++__i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001621 *__i = *__f;
1622 if (__f != __l)
1623 __append(__f, __l);
1624 else
1625 __erase_to_end(__i);
1626}
1627
1628template <class _Tp, class _Allocator>
1629template <class _RAIter>
1630void
1631deque<_Tp, _Allocator>::assign(_RAIter __f, _RAIter __l,
1632 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
1633{
1634 if (static_cast<size_type>(__l - __f) > __base::size())
1635 {
1636 _RAIter __m = __f + __base::size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001637 _VSTD::copy(__f, __m, __base::begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001638 __append(__m, __l);
1639 }
1640 else
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001641 __erase_to_end(_VSTD::copy(__f, __l, __base::begin()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001642}
1643
1644template <class _Tp, class _Allocator>
1645void
1646deque<_Tp, _Allocator>::assign(size_type __n, const value_type& __v)
1647{
1648 if (__n > __base::size())
1649 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001650 _VSTD::fill_n(__base::begin(), __base::size(), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001651 __n -= __base::size();
1652 __append(__n, __v);
1653 }
1654 else
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001655 __erase_to_end(_VSTD::fill_n(__base::begin(), __n, __v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001656}
1657
1658template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001659inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001660_Allocator
Howard Hinnantda2df912011-06-02 16:10:22 +00001661deque<_Tp, _Allocator>::get_allocator() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001662{
1663 return __base::__alloc();
1664}
1665
1666template <class _Tp, class _Allocator>
1667void
1668deque<_Tp, _Allocator>::resize(size_type __n)
1669{
1670 if (__n > __base::size())
1671 __append(__n - __base::size());
1672 else if (__n < __base::size())
1673 __erase_to_end(__base::begin() + __n);
1674}
1675
1676template <class _Tp, class _Allocator>
1677void
1678deque<_Tp, _Allocator>::resize(size_type __n, const value_type& __v)
1679{
1680 if (__n > __base::size())
1681 __append(__n - __base::size(), __v);
1682 else if (__n < __base::size())
1683 __erase_to_end(__base::begin() + __n);
1684}
1685
1686template <class _Tp, class _Allocator>
1687void
Howard Hinnant4931e092011-06-02 21:38:57 +00001688deque<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001689{
1690 allocator_type& __a = __base::__alloc();
1691 if (empty())
1692 {
1693 while (__base::__map_.size() > 0)
1694 {
1695 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
1696 __base::__map_.pop_back();
1697 }
1698 __base::__start_ = 0;
1699 }
1700 else
1701 {
1702 if (__front_spare() >= __base::__block_size)
1703 {
1704 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
1705 __base::__map_.pop_front();
1706 __base::__start_ -= __base::__block_size;
1707 }
1708 if (__back_spare() >= __base::__block_size)
1709 {
1710 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
1711 __base::__map_.pop_back();
1712 }
1713 }
1714 __base::__map_.shrink_to_fit();
1715}
1716
1717template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001718inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001719typename deque<_Tp, _Allocator>::reference
1720deque<_Tp, _Allocator>::operator[](size_type __i)
1721{
1722 size_type __p = __base::__start_ + __i;
1723 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1724}
1725
1726template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001727inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001728typename deque<_Tp, _Allocator>::const_reference
1729deque<_Tp, _Allocator>::operator[](size_type __i) const
1730{
1731 size_type __p = __base::__start_ + __i;
1732 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1733}
1734
1735template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001736inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001737typename deque<_Tp, _Allocator>::reference
1738deque<_Tp, _Allocator>::at(size_type __i)
1739{
1740 if (__i >= __base::size())
1741 __base::__throw_out_of_range();
1742 size_type __p = __base::__start_ + __i;
1743 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1744}
1745
1746template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001747inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001748typename deque<_Tp, _Allocator>::const_reference
1749deque<_Tp, _Allocator>::at(size_type __i) const
1750{
1751 if (__i >= __base::size())
1752 __base::__throw_out_of_range();
1753 size_type __p = __base::__start_ + __i;
1754 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1755}
1756
1757template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001758inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001759typename deque<_Tp, _Allocator>::reference
1760deque<_Tp, _Allocator>::front()
1761{
1762 return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1763 + __base::__start_ % __base::__block_size);
1764}
1765
1766template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001767inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001768typename deque<_Tp, _Allocator>::const_reference
1769deque<_Tp, _Allocator>::front() const
1770{
1771 return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1772 + __base::__start_ % __base::__block_size);
1773}
1774
1775template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001776inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001777typename deque<_Tp, _Allocator>::reference
1778deque<_Tp, _Allocator>::back()
1779{
1780 size_type __p = __base::size() + __base::__start_ - 1;
1781 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1782}
1783
1784template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001785inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001786typename deque<_Tp, _Allocator>::const_reference
1787deque<_Tp, _Allocator>::back() const
1788{
1789 size_type __p = __base::size() + __base::__start_ - 1;
1790 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1791}
1792
1793template <class _Tp, class _Allocator>
1794void
1795deque<_Tp, _Allocator>::push_back(const value_type& __v)
1796{
1797 allocator_type& __a = __base::__alloc();
1798 if (__back_spare() == 0)
1799 __add_back_capacity();
1800 // __back_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001801 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001802 ++__base::size();
1803}
1804
Howard Hinnant74279a52010-09-04 23:28:19 +00001805#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001806
1807template <class _Tp, class _Allocator>
1808void
1809deque<_Tp, _Allocator>::push_back(value_type&& __v)
1810{
1811 allocator_type& __a = __base::__alloc();
1812 if (__back_spare() == 0)
1813 __add_back_capacity();
1814 // __back_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001815 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001816 ++__base::size();
1817}
1818
Howard Hinnant74279a52010-09-04 23:28:19 +00001819#ifndef _LIBCPP_HAS_NO_VARIADICS
1820
Howard Hinnantc51e1022010-05-11 19:42:16 +00001821template <class _Tp, class _Allocator>
1822template <class... _Args>
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001823typename deque<_Tp, _Allocator>::reference
Howard Hinnantc51e1022010-05-11 19:42:16 +00001824deque<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1825{
1826 allocator_type& __a = __base::__alloc();
1827 if (__back_spare() == 0)
1828 __add_back_capacity();
1829 // __back_spare() >= 1
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001830 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()),
1831 _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001832 ++__base::size();
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001833 return *--__base::end();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001834}
1835
Howard Hinnant74279a52010-09-04 23:28:19 +00001836#endif // _LIBCPP_HAS_NO_VARIADICS
1837#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001838
1839template <class _Tp, class _Allocator>
1840void
1841deque<_Tp, _Allocator>::push_front(const value_type& __v)
1842{
1843 allocator_type& __a = __base::__alloc();
1844 if (__front_spare() == 0)
1845 __add_front_capacity();
1846 // __front_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001847 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001848 --__base::__start_;
1849 ++__base::size();
1850}
1851
Howard Hinnant74279a52010-09-04 23:28:19 +00001852#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001853
1854template <class _Tp, class _Allocator>
1855void
1856deque<_Tp, _Allocator>::push_front(value_type&& __v)
1857{
1858 allocator_type& __a = __base::__alloc();
1859 if (__front_spare() == 0)
1860 __add_front_capacity();
1861 // __front_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001862 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001863 --__base::__start_;
1864 ++__base::size();
1865}
1866
Howard Hinnant74279a52010-09-04 23:28:19 +00001867#ifndef _LIBCPP_HAS_NO_VARIADICS
1868
Howard Hinnantc51e1022010-05-11 19:42:16 +00001869template <class _Tp, class _Allocator>
1870template <class... _Args>
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001871typename deque<_Tp, _Allocator>::reference
Howard Hinnantc51e1022010-05-11 19:42:16 +00001872deque<_Tp, _Allocator>::emplace_front(_Args&&... __args)
1873{
1874 allocator_type& __a = __base::__alloc();
1875 if (__front_spare() == 0)
1876 __add_front_capacity();
1877 // __front_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001878 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001879 --__base::__start_;
1880 ++__base::size();
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001881 return *__base::begin();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001882}
1883
Howard Hinnant74279a52010-09-04 23:28:19 +00001884#endif // _LIBCPP_HAS_NO_VARIADICS
1885#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001886
1887template <class _Tp, class _Allocator>
1888typename deque<_Tp, _Allocator>::iterator
1889deque<_Tp, _Allocator>::insert(const_iterator __p, const value_type& __v)
1890{
1891 size_type __pos = __p - __base::begin();
1892 size_type __to_end = __base::size() - __pos;
1893 allocator_type& __a = __base::__alloc();
1894 if (__pos < __to_end)
1895 { // insert by shifting things backward
1896 if (__front_spare() == 0)
1897 __add_front_capacity();
1898 // __front_spare() >= 1
1899 if (__pos == 0)
1900 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001901 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001902 --__base::__start_;
1903 ++__base::size();
1904 }
1905 else
1906 {
1907 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1908 iterator __b = __base::begin();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001909 iterator __bm1 = _VSTD::prev(__b);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001910 if (__vt == pointer_traits<const_pointer>::pointer_to(*__b))
1911 __vt = pointer_traits<const_pointer>::pointer_to(*__bm1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001912 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001913 --__base::__start_;
1914 ++__base::size();
1915 if (__pos > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001916 __b = __move_and_check(_VSTD::next(__b), __b + __pos, __b, __vt);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001917 *__b = *__vt;
1918 }
1919 }
1920 else
1921 { // insert by shifting things forward
1922 if (__back_spare() == 0)
1923 __add_back_capacity();
1924 // __back_capacity >= 1
1925 size_type __de = __base::size() - __pos;
1926 if (__de == 0)
1927 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001928 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001929 ++__base::size();
1930 }
1931 else
1932 {
1933 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1934 iterator __e = __base::end();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001935 iterator __em1 = _VSTD::prev(__e);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001936 if (__vt == pointer_traits<const_pointer>::pointer_to(*__em1))
1937 __vt = pointer_traits<const_pointer>::pointer_to(*__e);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001938 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001939 ++__base::size();
1940 if (__de > 1)
1941 __e = __move_backward_and_check(__e - __de, __em1, __e, __vt);
1942 *--__e = *__vt;
1943 }
1944 }
1945 return __base::begin() + __pos;
1946}
1947
Howard Hinnant74279a52010-09-04 23:28:19 +00001948#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001949
1950template <class _Tp, class _Allocator>
1951typename deque<_Tp, _Allocator>::iterator
1952deque<_Tp, _Allocator>::insert(const_iterator __p, value_type&& __v)
1953{
1954 size_type __pos = __p - __base::begin();
1955 size_type __to_end = __base::size() - __pos;
1956 allocator_type& __a = __base::__alloc();
1957 if (__pos < __to_end)
1958 { // insert by shifting things backward
1959 if (__front_spare() == 0)
1960 __add_front_capacity();
1961 // __front_spare() >= 1
1962 if (__pos == 0)
1963 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001964 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001965 --__base::__start_;
1966 ++__base::size();
1967 }
1968 else
1969 {
1970 iterator __b = __base::begin();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001971 iterator __bm1 = _VSTD::prev(__b);
1972 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001973 --__base::__start_;
1974 ++__base::size();
1975 if (__pos > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001976 __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
1977 *__b = _VSTD::move(__v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001978 }
1979 }
1980 else
1981 { // insert by shifting things forward
1982 if (__back_spare() == 0)
1983 __add_back_capacity();
1984 // __back_capacity >= 1
1985 size_type __de = __base::size() - __pos;
1986 if (__de == 0)
1987 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001988 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001989 ++__base::size();
1990 }
1991 else
1992 {
1993 iterator __e = __base::end();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001994 iterator __em1 = _VSTD::prev(__e);
1995 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001996 ++__base::size();
1997 if (__de > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001998 __e = _VSTD::move_backward(__e - __de, __em1, __e);
1999 *--__e = _VSTD::move(__v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002000 }
2001 }
2002 return __base::begin() + __pos;
2003}
2004
Howard Hinnant74279a52010-09-04 23:28:19 +00002005#ifndef _LIBCPP_HAS_NO_VARIADICS
2006
Howard Hinnantc51e1022010-05-11 19:42:16 +00002007template <class _Tp, class _Allocator>
2008template <class... _Args>
2009typename deque<_Tp, _Allocator>::iterator
2010deque<_Tp, _Allocator>::emplace(const_iterator __p, _Args&&... __args)
2011{
2012 size_type __pos = __p - __base::begin();
2013 size_type __to_end = __base::size() - __pos;
2014 allocator_type& __a = __base::__alloc();
2015 if (__pos < __to_end)
2016 { // insert by shifting things backward
2017 if (__front_spare() == 0)
2018 __add_front_capacity();
2019 // __front_spare() >= 1
2020 if (__pos == 0)
2021 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002022 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002023 --__base::__start_;
2024 ++__base::size();
2025 }
2026 else
2027 {
Marshall Clowa591b9a2016-07-11 21:38:08 +00002028 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002029 iterator __b = __base::begin();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002030 iterator __bm1 = _VSTD::prev(__b);
2031 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002032 --__base::__start_;
2033 ++__base::size();
2034 if (__pos > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002035 __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
Marshall Clowa591b9a2016-07-11 21:38:08 +00002036 *__b = _VSTD::move(__tmp.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002037 }
2038 }
2039 else
2040 { // insert by shifting things forward
2041 if (__back_spare() == 0)
2042 __add_back_capacity();
2043 // __back_capacity >= 1
2044 size_type __de = __base::size() - __pos;
2045 if (__de == 0)
2046 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002047 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002048 ++__base::size();
2049 }
2050 else
2051 {
Marshall Clowa591b9a2016-07-11 21:38:08 +00002052 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002053 iterator __e = __base::end();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002054 iterator __em1 = _VSTD::prev(__e);
2055 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002056 ++__base::size();
2057 if (__de > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002058 __e = _VSTD::move_backward(__e - __de, __em1, __e);
Marshall Clowa591b9a2016-07-11 21:38:08 +00002059 *--__e = _VSTD::move(__tmp.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002060 }
2061 }
2062 return __base::begin() + __pos;
2063}
2064
Howard Hinnant74279a52010-09-04 23:28:19 +00002065#endif // _LIBCPP_HAS_NO_VARIADICS
2066#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002067
2068template <class _Tp, class _Allocator>
2069typename deque<_Tp, _Allocator>::iterator
2070deque<_Tp, _Allocator>::insert(const_iterator __p, size_type __n, const value_type& __v)
2071{
2072 size_type __pos = __p - __base::begin();
2073 size_type __to_end = __base::size() - __pos;
2074 allocator_type& __a = __base::__alloc();
2075 if (__pos < __to_end)
2076 { // insert by shifting things backward
2077 if (__n > __front_spare())
2078 __add_front_capacity(__n - __front_spare());
2079 // __n <= __front_spare()
Howard Hinnantc51e1022010-05-11 19:42:16 +00002080 iterator __old_begin = __base::begin();
2081 iterator __i = __old_begin;
2082 if (__n > __pos)
2083 {
2084 for (size_type __m = __n - __pos; __m; --__m, --__base::__start_, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002085 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002086 __n = __pos;
2087 }
2088 if (__n > 0)
2089 {
2090 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2091 iterator __obn = __old_begin + __n;
2092 __move_construct_backward_and_check(__old_begin, __obn, __i, __vt);
2093 if (__n < __pos)
2094 __old_begin = __move_and_check(__obn, __old_begin + __pos, __old_begin, __vt);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002095 _VSTD::fill_n(__old_begin, __n, *__vt);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002096 }
2097 }
2098 else
2099 { // insert by shifting things forward
2100 size_type __back_capacity = __back_spare();
2101 if (__n > __back_capacity)
2102 __add_back_capacity(__n - __back_capacity);
2103 // __n <= __back_capacity
Howard Hinnantc51e1022010-05-11 19:42:16 +00002104 iterator __old_end = __base::end();
2105 iterator __i = __old_end;
2106 size_type __de = __base::size() - __pos;
2107 if (__n > __de)
2108 {
2109 for (size_type __m = __n - __de; __m; --__m, ++__i, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002110 __alloc_traits::construct(__a, _VSTD::addressof(*__i), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002111 __n = __de;
2112 }
2113 if (__n > 0)
2114 {
2115 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2116 iterator __oen = __old_end - __n;
2117 __move_construct_and_check(__oen, __old_end, __i, __vt);
2118 if (__n < __de)
2119 __old_end = __move_backward_and_check(__old_end - __de, __oen, __old_end, __vt);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002120 _VSTD::fill_n(__old_end - __n, __n, *__vt);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002121 }
2122 }
2123 return __base::begin() + __pos;
2124}
2125
2126template <class _Tp, class _Allocator>
2127template <class _InputIter>
2128typename deque<_Tp, _Allocator>::iterator
2129deque<_Tp, _Allocator>::insert(const_iterator __p, _InputIter __f, _InputIter __l,
2130 typename enable_if<__is_input_iterator<_InputIter>::value
Marshall Clow6b612cf2015-01-22 18:33:29 +00002131 &&!__is_forward_iterator<_InputIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002132{
2133 __split_buffer<value_type, allocator_type&> __buf(__base::__alloc());
2134 __buf.__construct_at_end(__f, __l);
2135 typedef typename __split_buffer<value_type, allocator_type&>::iterator __bi;
2136 return insert(__p, move_iterator<__bi>(__buf.begin()), move_iterator<__bi>(__buf.end()));
2137}
2138
2139template <class _Tp, class _Allocator>
Marshall Clow6b612cf2015-01-22 18:33:29 +00002140template <class _ForwardIterator>
2141typename deque<_Tp, _Allocator>::iterator
2142deque<_Tp, _Allocator>::insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l,
2143 typename enable_if<__is_forward_iterator<_ForwardIterator>::value
2144 &&!__is_bidirectional_iterator<_ForwardIterator>::value>::type*)
2145{
2146 size_type __n = _VSTD::distance(__f, __l);
2147 __split_buffer<value_type, allocator_type&> __buf(__n, 0, __base::__alloc());
2148 __buf.__construct_at_end(__f, __l);
2149 typedef typename __split_buffer<value_type, allocator_type&>::iterator __fwd;
2150 return insert(__p, move_iterator<__fwd>(__buf.begin()), move_iterator<__fwd>(__buf.end()));
2151}
2152
2153template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002154template <class _BiIter>
2155typename deque<_Tp, _Allocator>::iterator
2156deque<_Tp, _Allocator>::insert(const_iterator __p, _BiIter __f, _BiIter __l,
2157 typename enable_if<__is_bidirectional_iterator<_BiIter>::value>::type*)
2158{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002159 size_type __n = _VSTD::distance(__f, __l);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002160 size_type __pos = __p - __base::begin();
2161 size_type __to_end = __base::size() - __pos;
2162 allocator_type& __a = __base::__alloc();
2163 if (__pos < __to_end)
2164 { // insert by shifting things backward
2165 if (__n > __front_spare())
2166 __add_front_capacity(__n - __front_spare());
2167 // __n <= __front_spare()
Howard Hinnantc51e1022010-05-11 19:42:16 +00002168 iterator __old_begin = __base::begin();
2169 iterator __i = __old_begin;
2170 _BiIter __m = __f;
2171 if (__n > __pos)
2172 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002173 __m = __pos < __n / 2 ? _VSTD::prev(__l, __pos) : _VSTD::next(__f, __n - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002174 for (_BiIter __j = __m; __j != __f; --__base::__start_, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002175 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), *--__j);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002176 __n = __pos;
2177 }
2178 if (__n > 0)
2179 {
2180 iterator __obn = __old_begin + __n;
2181 for (iterator __j = __obn; __j != __old_begin;)
2182 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002183 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), _VSTD::move(*--__j));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002184 --__base::__start_;
2185 ++__base::size();
2186 }
2187 if (__n < __pos)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002188 __old_begin = _VSTD::move(__obn, __old_begin + __pos, __old_begin);
2189 _VSTD::copy(__m, __l, __old_begin);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002190 }
2191 }
2192 else
2193 { // insert by shifting things forward
2194 size_type __back_capacity = __back_spare();
2195 if (__n > __back_capacity)
2196 __add_back_capacity(__n - __back_capacity);
2197 // __n <= __back_capacity
Howard Hinnantc51e1022010-05-11 19:42:16 +00002198 iterator __old_end = __base::end();
2199 iterator __i = __old_end;
2200 _BiIter __m = __l;
2201 size_type __de = __base::size() - __pos;
2202 if (__n > __de)
2203 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002204 __m = __de < __n / 2 ? _VSTD::next(__f, __de) : _VSTD::prev(__l, __n - __de);
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002205 for (_BiIter __j = __m; __j != __l; ++__i, (void) ++__j, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002206 __alloc_traits::construct(__a, _VSTD::addressof(*__i), *__j);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002207 __n = __de;
2208 }
2209 if (__n > 0)
2210 {
2211 iterator __oen = __old_end - __n;
2212 for (iterator __j = __oen; __j != __old_end; ++__i, ++__j, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002213 __alloc_traits::construct(__a, _VSTD::addressof(*__i), _VSTD::move(*__j));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002214 if (__n < __de)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002215 __old_end = _VSTD::move_backward(__old_end - __de, __oen, __old_end);
2216 _VSTD::copy_backward(__f, __m, __old_end);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002217 }
2218 }
2219 return __base::begin() + __pos;
2220}
2221
2222template <class _Tp, class _Allocator>
2223template <class _InpIter>
2224void
2225deque<_Tp, _Allocator>::__append(_InpIter __f, _InpIter __l,
2226 typename enable_if<__is_input_iterator<_InpIter>::value &&
2227 !__is_forward_iterator<_InpIter>::value>::type*)
2228{
2229 for (; __f != __l; ++__f)
2230 push_back(*__f);
2231}
2232
2233template <class _Tp, class _Allocator>
2234template <class _ForIter>
2235void
2236deque<_Tp, _Allocator>::__append(_ForIter __f, _ForIter __l,
2237 typename enable_if<__is_forward_iterator<_ForIter>::value>::type*)
2238{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002239 size_type __n = _VSTD::distance(__f, __l);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002240 allocator_type& __a = __base::__alloc();
2241 size_type __back_capacity = __back_spare();
2242 if (__n > __back_capacity)
2243 __add_back_capacity(__n - __back_capacity);
2244 // __n <= __back_capacity
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002245 for (iterator __i = __base::end(); __f != __l; ++__i, (void) ++__f, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002246 __alloc_traits::construct(__a, _VSTD::addressof(*__i), *__f);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002247}
2248
2249template <class _Tp, class _Allocator>
2250void
2251deque<_Tp, _Allocator>::__append(size_type __n)
2252{
2253 allocator_type& __a = __base::__alloc();
2254 size_type __back_capacity = __back_spare();
2255 if (__n > __back_capacity)
2256 __add_back_capacity(__n - __back_capacity);
2257 // __n <= __back_capacity
2258 for (iterator __i = __base::end(); __n; --__n, ++__i, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002259 __alloc_traits::construct(__a, _VSTD::addressof(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002260}
2261
2262template <class _Tp, class _Allocator>
2263void
2264deque<_Tp, _Allocator>::__append(size_type __n, const value_type& __v)
2265{
2266 allocator_type& __a = __base::__alloc();
2267 size_type __back_capacity = __back_spare();
2268 if (__n > __back_capacity)
2269 __add_back_capacity(__n - __back_capacity);
2270 // __n <= __back_capacity
2271 for (iterator __i = __base::end(); __n; --__n, ++__i, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002272 __alloc_traits::construct(__a, _VSTD::addressof(*__i), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002273}
2274
2275// Create front capacity for one block of elements.
2276// Strong guarantee. Either do it or don't touch anything.
2277template <class _Tp, class _Allocator>
2278void
2279deque<_Tp, _Allocator>::__add_front_capacity()
2280{
2281 allocator_type& __a = __base::__alloc();
2282 if (__back_spare() >= __base::__block_size)
2283 {
2284 __base::__start_ += __base::__block_size;
2285 pointer __pt = __base::__map_.back();
2286 __base::__map_.pop_back();
2287 __base::__map_.push_front(__pt);
2288 }
2289 // Else if __base::__map_.size() < __base::__map_.capacity() then we need to allocate 1 buffer
2290 else if (__base::__map_.size() < __base::__map_.capacity())
2291 { // we can put the new buffer into the map, but don't shift things around
2292 // until all buffers are allocated. If we throw, we don't need to fix
2293 // anything up (any added buffers are undetectible)
2294 if (__base::__map_.__front_spare() > 0)
2295 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2296 else
2297 {
2298 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2299 // Done allocating, reorder capacity
2300 pointer __pt = __base::__map_.back();
2301 __base::__map_.pop_back();
2302 __base::__map_.push_front(__pt);
2303 }
2304 __base::__start_ = __base::__map_.size() == 1 ?
2305 __base::__block_size / 2 :
2306 __base::__start_ + __base::__block_size;
2307 }
2308 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2309 else
2310 {
2311 __split_buffer<pointer, typename __base::__pointer_allocator&>
2312 __buf(max<size_type>(2 * __base::__map_.capacity(), 1),
2313 0, __base::__map_.__alloc());
Marshall Clow5fd466b2015-03-09 17:08:51 +00002314
Marshall Clow8982dcd2015-07-13 20:04:56 +00002315 typedef __allocator_destructor<_Allocator> _Dp;
2316 unique_ptr<pointer, _Dp> __hold(
2317 __alloc_traits::allocate(__a, __base::__block_size),
2318 _Dp(__a, __base::__block_size));
2319 __buf.push_back(__hold.get());
2320 __hold.release();
2321
Howard Hinnantc51e1022010-05-11 19:42:16 +00002322 for (typename __base::__map_pointer __i = __base::__map_.begin();
2323 __i != __base::__map_.end(); ++__i)
2324 __buf.push_back(*__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002325 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2326 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2327 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2328 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002329 __base::__start_ = __base::__map_.size() == 1 ?
2330 __base::__block_size / 2 :
2331 __base::__start_ + __base::__block_size;
2332 }
2333}
2334
2335// Create front capacity for __n elements.
2336// Strong guarantee. Either do it or don't touch anything.
2337template <class _Tp, class _Allocator>
2338void
2339deque<_Tp, _Allocator>::__add_front_capacity(size_type __n)
2340{
2341 allocator_type& __a = __base::__alloc();
2342 size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2343 // Number of unused blocks at back:
2344 size_type __back_capacity = __back_spare() / __base::__block_size;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002345 __back_capacity = _VSTD::min(__back_capacity, __nb); // don't take more than you need
Howard Hinnantc51e1022010-05-11 19:42:16 +00002346 __nb -= __back_capacity; // number of blocks need to allocate
2347 // If __nb == 0, then we have sufficient capacity.
2348 if (__nb == 0)
2349 {
2350 __base::__start_ += __base::__block_size * __back_capacity;
2351 for (; __back_capacity > 0; --__back_capacity)
2352 {
2353 pointer __pt = __base::__map_.back();
2354 __base::__map_.pop_back();
2355 __base::__map_.push_front(__pt);
2356 }
2357 }
2358 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2359 else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2360 { // we can put the new buffers into the map, but don't shift things around
2361 // until all buffers are allocated. If we throw, we don't need to fix
2362 // anything up (any added buffers are undetectible)
2363 for (; __nb > 0; --__nb, __base::__start_ += __base::__block_size - (__base::__map_.size() == 1))
2364 {
2365 if (__base::__map_.__front_spare() == 0)
2366 break;
2367 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2368 }
2369 for (; __nb > 0; --__nb, ++__back_capacity)
2370 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2371 // Done allocating, reorder capacity
2372 __base::__start_ += __back_capacity * __base::__block_size;
2373 for (; __back_capacity > 0; --__back_capacity)
2374 {
2375 pointer __pt = __base::__map_.back();
2376 __base::__map_.pop_back();
2377 __base::__map_.push_front(__pt);
2378 }
2379 }
2380 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2381 else
2382 {
2383 size_type __ds = (__nb + __back_capacity) * __base::__block_size - __base::__map_.empty();
2384 __split_buffer<pointer, typename __base::__pointer_allocator&>
2385 __buf(max<size_type>(2* __base::__map_.capacity(),
2386 __nb + __base::__map_.size()),
2387 0, __base::__map_.__alloc());
2388#ifndef _LIBCPP_NO_EXCEPTIONS
2389 try
2390 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002391#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002392 for (; __nb > 0; --__nb)
2393 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2394#ifndef _LIBCPP_NO_EXCEPTIONS
2395 }
2396 catch (...)
2397 {
2398 for (typename __base::__map_pointer __i = __buf.begin();
2399 __i != __buf.end(); ++__i)
2400 __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2401 throw;
2402 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002403#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002404 for (; __back_capacity > 0; --__back_capacity)
2405 {
2406 __buf.push_back(__base::__map_.back());
2407 __base::__map_.pop_back();
2408 }
2409 for (typename __base::__map_pointer __i = __base::__map_.begin();
2410 __i != __base::__map_.end(); ++__i)
2411 __buf.push_back(*__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002412 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2413 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2414 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2415 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002416 __base::__start_ += __ds;
2417 }
2418}
2419
2420// Create back capacity for one block of elements.
2421// Strong guarantee. Either do it or don't touch anything.
2422template <class _Tp, class _Allocator>
2423void
2424deque<_Tp, _Allocator>::__add_back_capacity()
2425{
2426 allocator_type& __a = __base::__alloc();
2427 if (__front_spare() >= __base::__block_size)
2428 {
2429 __base::__start_ -= __base::__block_size;
2430 pointer __pt = __base::__map_.front();
2431 __base::__map_.pop_front();
2432 __base::__map_.push_back(__pt);
2433 }
2434 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2435 else if (__base::__map_.size() < __base::__map_.capacity())
2436 { // we can put the new buffer into the map, but don't shift things around
2437 // until it is allocated. If we throw, we don't need to fix
2438 // anything up (any added buffers are undetectible)
2439 if (__base::__map_.__back_spare() != 0)
2440 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2441 else
2442 {
2443 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2444 // Done allocating, reorder capacity
2445 pointer __pt = __base::__map_.front();
2446 __base::__map_.pop_front();
2447 __base::__map_.push_back(__pt);
2448 }
2449 }
2450 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2451 else
2452 {
2453 __split_buffer<pointer, typename __base::__pointer_allocator&>
2454 __buf(max<size_type>(2* __base::__map_.capacity(), 1),
2455 __base::__map_.size(),
2456 __base::__map_.__alloc());
Marshall Clow5fd466b2015-03-09 17:08:51 +00002457
Marshall Clow8982dcd2015-07-13 20:04:56 +00002458 typedef __allocator_destructor<_Allocator> _Dp;
2459 unique_ptr<pointer, _Dp> __hold(
2460 __alloc_traits::allocate(__a, __base::__block_size),
2461 _Dp(__a, __base::__block_size));
2462 __buf.push_back(__hold.get());
2463 __hold.release();
Marshall Clow5fd466b2015-03-09 17:08:51 +00002464
Howard Hinnantc51e1022010-05-11 19:42:16 +00002465 for (typename __base::__map_pointer __i = __base::__map_.end();
2466 __i != __base::__map_.begin();)
2467 __buf.push_front(*--__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002468 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2469 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2470 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2471 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002472 }
2473}
2474
2475// Create back capacity for __n elements.
2476// Strong guarantee. Either do it or don't touch anything.
2477template <class _Tp, class _Allocator>
2478void
2479deque<_Tp, _Allocator>::__add_back_capacity(size_type __n)
2480{
2481 allocator_type& __a = __base::__alloc();
2482 size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2483 // Number of unused blocks at front:
2484 size_type __front_capacity = __front_spare() / __base::__block_size;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002485 __front_capacity = _VSTD::min(__front_capacity, __nb); // don't take more than you need
Howard Hinnantc51e1022010-05-11 19:42:16 +00002486 __nb -= __front_capacity; // number of blocks need to allocate
2487 // If __nb == 0, then we have sufficient capacity.
2488 if (__nb == 0)
2489 {
2490 __base::__start_ -= __base::__block_size * __front_capacity;
2491 for (; __front_capacity > 0; --__front_capacity)
2492 {
2493 pointer __pt = __base::__map_.front();
2494 __base::__map_.pop_front();
2495 __base::__map_.push_back(__pt);
2496 }
2497 }
2498 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2499 else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2500 { // we can put the new buffers into the map, but don't shift things around
2501 // until all buffers are allocated. If we throw, we don't need to fix
2502 // anything up (any added buffers are undetectible)
2503 for (; __nb > 0; --__nb)
2504 {
2505 if (__base::__map_.__back_spare() == 0)
2506 break;
2507 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2508 }
2509 for (; __nb > 0; --__nb, ++__front_capacity, __base::__start_ +=
2510 __base::__block_size - (__base::__map_.size() == 1))
2511 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2512 // Done allocating, reorder capacity
2513 __base::__start_ -= __base::__block_size * __front_capacity;
2514 for (; __front_capacity > 0; --__front_capacity)
2515 {
2516 pointer __pt = __base::__map_.front();
2517 __base::__map_.pop_front();
2518 __base::__map_.push_back(__pt);
2519 }
2520 }
2521 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2522 else
2523 {
2524 size_type __ds = __front_capacity * __base::__block_size;
2525 __split_buffer<pointer, typename __base::__pointer_allocator&>
2526 __buf(max<size_type>(2* __base::__map_.capacity(),
2527 __nb + __base::__map_.size()),
2528 __base::__map_.size() - __front_capacity,
2529 __base::__map_.__alloc());
2530#ifndef _LIBCPP_NO_EXCEPTIONS
2531 try
2532 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002533#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002534 for (; __nb > 0; --__nb)
2535 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2536#ifndef _LIBCPP_NO_EXCEPTIONS
2537 }
2538 catch (...)
2539 {
2540 for (typename __base::__map_pointer __i = __buf.begin();
2541 __i != __buf.end(); ++__i)
2542 __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2543 throw;
2544 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002545#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002546 for (; __front_capacity > 0; --__front_capacity)
2547 {
2548 __buf.push_back(__base::__map_.front());
2549 __base::__map_.pop_front();
2550 }
2551 for (typename __base::__map_pointer __i = __base::__map_.end();
2552 __i != __base::__map_.begin();)
2553 __buf.push_front(*--__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002554 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2555 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2556 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2557 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002558 __base::__start_ -= __ds;
2559 }
2560}
2561
2562template <class _Tp, class _Allocator>
2563void
2564deque<_Tp, _Allocator>::pop_front()
2565{
2566 allocator_type& __a = __base::__alloc();
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002567 __alloc_traits::destroy(__a, __to_raw_pointer(*(__base::__map_.begin() +
2568 __base::__start_ / __base::__block_size) +
2569 __base::__start_ % __base::__block_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002570 --__base::size();
2571 if (++__base::__start_ >= 2 * __base::__block_size)
2572 {
2573 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2574 __base::__map_.pop_front();
2575 __base::__start_ -= __base::__block_size;
2576 }
2577}
2578
2579template <class _Tp, class _Allocator>
2580void
2581deque<_Tp, _Allocator>::pop_back()
2582{
2583 allocator_type& __a = __base::__alloc();
2584 size_type __p = __base::size() + __base::__start_ - 1;
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002585 __alloc_traits::destroy(__a, __to_raw_pointer(*(__base::__map_.begin() +
2586 __p / __base::__block_size) +
2587 __p % __base::__block_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002588 --__base::size();
2589 if (__back_spare() >= 2 * __base::__block_size)
2590 {
2591 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2592 __base::__map_.pop_back();
2593 }
2594}
2595
2596// move assign [__f, __l) to [__r, __r + (__l-__f)).
2597// If __vt points into [__f, __l), then subtract (__f - __r) from __vt.
2598template <class _Tp, class _Allocator>
2599typename deque<_Tp, _Allocator>::iterator
2600deque<_Tp, _Allocator>::__move_and_check(iterator __f, iterator __l, iterator __r,
2601 const_pointer& __vt)
2602{
2603 // as if
2604 // for (; __f != __l; ++__f, ++__r)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002605 // *__r = _VSTD::move(*__f);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002606 difference_type __n = __l - __f;
2607 while (__n > 0)
2608 {
2609 pointer __fb = __f.__ptr_;
2610 pointer __fe = *__f.__m_iter_ + __base::__block_size;
2611 difference_type __bs = __fe - __fb;
2612 if (__bs > __n)
2613 {
2614 __bs = __n;
2615 __fe = __fb + __bs;
2616 }
2617 if (__fb <= __vt && __vt < __fe)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002618 __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) -= __f - __r).__ptr_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002619 __r = _VSTD::move(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002620 __n -= __bs;
2621 __f += __bs;
2622 }
2623 return __r;
2624}
2625
2626// move assign [__f, __l) to [__r - (__l-__f), __r) backwards.
2627// If __vt points into [__f, __l), then add (__r - __l) to __vt.
2628template <class _Tp, class _Allocator>
2629typename deque<_Tp, _Allocator>::iterator
2630deque<_Tp, _Allocator>::__move_backward_and_check(iterator __f, iterator __l, iterator __r,
2631 const_pointer& __vt)
2632{
2633 // as if
2634 // while (__f != __l)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002635 // *--__r = _VSTD::move(*--__l);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002636 difference_type __n = __l - __f;
2637 while (__n > 0)
2638 {
2639 --__l;
2640 pointer __lb = *__l.__m_iter_;
2641 pointer __le = __l.__ptr_ + 1;
2642 difference_type __bs = __le - __lb;
2643 if (__bs > __n)
2644 {
2645 __bs = __n;
2646 __lb = __le - __bs;
2647 }
2648 if (__lb <= __vt && __vt < __le)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002649 __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) += __r - __l - 1).__ptr_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002650 __r = _VSTD::move_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002651 __n -= __bs;
2652 __l -= __bs - 1;
2653 }
2654 return __r;
2655}
2656
2657// move construct [__f, __l) to [__r, __r + (__l-__f)).
2658// If __vt points into [__f, __l), then add (__r - __f) to __vt.
2659template <class _Tp, class _Allocator>
2660void
2661deque<_Tp, _Allocator>::__move_construct_and_check(iterator __f, iterator __l,
2662 iterator __r, const_pointer& __vt)
2663{
2664 allocator_type& __a = __base::__alloc();
2665 // as if
2666 // for (; __f != __l; ++__r, ++__f, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002667 // __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__f));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002668 difference_type __n = __l - __f;
2669 while (__n > 0)
2670 {
2671 pointer __fb = __f.__ptr_;
2672 pointer __fe = *__f.__m_iter_ + __base::__block_size;
2673 difference_type __bs = __fe - __fb;
2674 if (__bs > __n)
2675 {
2676 __bs = __n;
2677 __fe = __fb + __bs;
2678 }
2679 if (__fb <= __vt && __vt < __fe)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002680 __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) += __r - __f).__ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002681 for (; __fb != __fe; ++__fb, ++__r, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002682 __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__fb));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002683 __n -= __bs;
2684 __f += __bs;
2685 }
2686}
2687
2688// move construct [__f, __l) to [__r - (__l-__f), __r) backwards.
2689// If __vt points into [__f, __l), then subtract (__l - __r) from __vt.
2690template <class _Tp, class _Allocator>
2691void
2692deque<_Tp, _Allocator>::__move_construct_backward_and_check(iterator __f, iterator __l,
2693 iterator __r, const_pointer& __vt)
2694{
2695 allocator_type& __a = __base::__alloc();
2696 // as if
2697 // for (iterator __j = __l; __j != __f;)
2698 // {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002699 // __alloc_traitsconstruct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__j));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002700 // --__base::__start_;
2701 // ++__base::size();
2702 // }
2703 difference_type __n = __l - __f;
2704 while (__n > 0)
2705 {
2706 --__l;
2707 pointer __lb = *__l.__m_iter_;
2708 pointer __le = __l.__ptr_ + 1;
2709 difference_type __bs = __le - __lb;
2710 if (__bs > __n)
2711 {
2712 __bs = __n;
2713 __lb = __le - __bs;
2714 }
2715 if (__lb <= __vt && __vt < __le)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002716 __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) -= __l - __r + 1).__ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002717 while (__le != __lb)
2718 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002719 __alloc_traits::construct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__le));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002720 --__base::__start_;
2721 ++__base::size();
2722 }
2723 __n -= __bs;
2724 __l -= __bs - 1;
2725 }
2726}
2727
2728template <class _Tp, class _Allocator>
2729typename deque<_Tp, _Allocator>::iterator
2730deque<_Tp, _Allocator>::erase(const_iterator __f)
2731{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002732 iterator __b = __base::begin();
2733 difference_type __pos = __f - __b;
2734 iterator __p = __b + __pos;
2735 allocator_type& __a = __base::__alloc();
Marshall Clowe7e39922015-06-05 22:34:19 +00002736 if (__pos <= (__base::size() - 1) / 2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002737 { // erase from front
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002738 _VSTD::move_backward(__b, __p, _VSTD::next(__p));
2739 __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002740 --__base::size();
2741 ++__base::__start_;
2742 if (__front_spare() >= 2 * __base::__block_size)
2743 {
2744 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2745 __base::__map_.pop_front();
2746 __base::__start_ -= __base::__block_size;
2747 }
2748 }
2749 else
2750 { // erase from back
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002751 iterator __i = _VSTD::move(_VSTD::next(__p), __base::end(), __p);
2752 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002753 --__base::size();
2754 if (__back_spare() >= 2 * __base::__block_size)
2755 {
2756 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2757 __base::__map_.pop_back();
2758 }
2759 }
2760 return __base::begin() + __pos;
2761}
2762
2763template <class _Tp, class _Allocator>
2764typename deque<_Tp, _Allocator>::iterator
2765deque<_Tp, _Allocator>::erase(const_iterator __f, const_iterator __l)
2766{
2767 difference_type __n = __l - __f;
2768 iterator __b = __base::begin();
2769 difference_type __pos = __f - __b;
2770 iterator __p = __b + __pos;
2771 if (__n > 0)
2772 {
2773 allocator_type& __a = __base::__alloc();
Marshall Clowe7e39922015-06-05 22:34:19 +00002774 if (__pos <= (__base::size() - __n) / 2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002775 { // erase from front
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002776 iterator __i = _VSTD::move_backward(__b, __p, __p + __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002777 for (; __b != __i; ++__b)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002778 __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002779 __base::size() -= __n;
2780 __base::__start_ += __n;
2781 while (__front_spare() >= 2 * __base::__block_size)
2782 {
2783 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2784 __base::__map_.pop_front();
2785 __base::__start_ -= __base::__block_size;
2786 }
2787 }
2788 else
2789 { // erase from back
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002790 iterator __i = _VSTD::move(__p + __n, __base::end(), __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002791 for (iterator __e = __base::end(); __i != __e; ++__i)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002792 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002793 __base::size() -= __n;
2794 while (__back_spare() >= 2 * __base::__block_size)
2795 {
2796 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2797 __base::__map_.pop_back();
2798 }
2799 }
2800 }
2801 return __base::begin() + __pos;
2802}
2803
2804template <class _Tp, class _Allocator>
2805void
2806deque<_Tp, _Allocator>::__erase_to_end(const_iterator __f)
2807{
2808 iterator __e = __base::end();
2809 difference_type __n = __e - __f;
2810 if (__n > 0)
2811 {
2812 allocator_type& __a = __base::__alloc();
2813 iterator __b = __base::begin();
2814 difference_type __pos = __f - __b;
2815 for (iterator __p = __b + __pos; __p != __e; ++__p)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002816 __alloc_traits::destroy(__a, _VSTD::addressof(*__p));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002817 __base::size() -= __n;
2818 while (__back_spare() >= 2 * __base::__block_size)
2819 {
2820 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2821 __base::__map_.pop_back();
2822 }
2823 }
2824}
2825
2826template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002827inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002828void
2829deque<_Tp, _Allocator>::swap(deque& __c)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002830#if _LIBCPP_STD_VER >= 14
2831 _NOEXCEPT
2832#else
2833 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2834 __is_nothrow_swappable<allocator_type>::value)
2835#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002836{
2837 __base::swap(__c);
2838}
2839
2840template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002841inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002842void
Howard Hinnantda2df912011-06-02 16:10:22 +00002843deque<_Tp, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002844{
2845 __base::clear();
2846}
2847
2848template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002849inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002850bool
2851operator==(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2852{
2853 const typename deque<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002854 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002855}
2856
2857template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002858inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002859bool
2860operator!=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2861{
2862 return !(__x == __y);
2863}
2864
2865template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002866inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002867bool
2868operator< (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2869{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002870 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002871}
2872
2873template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002874inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002875bool
2876operator> (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2877{
2878 return __y < __x;
2879}
2880
2881template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002882inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002883bool
2884operator>=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2885{
2886 return !(__x < __y);
2887}
2888
2889template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002890inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002891bool
2892operator<=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2893{
2894 return !(__y < __x);
2895}
2896
2897template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002898inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002899void
2900swap(deque<_Tp, _Allocator>& __x, deque<_Tp, _Allocator>& __y)
Howard Hinnantca2fc222011-06-02 20:00:14 +00002901 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002902{
2903 __x.swap(__y);
2904}
2905
2906_LIBCPP_END_NAMESPACE_STD
2907
2908#endif // _LIBCPP_DEQUE