blob: 2ac7bf3826d6157e5021b95f47b75b684d7eb638 [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);
113 template <class... Args> void emplace_front(Args&&... args);
114 template <class... Args> void emplace_back(Args&&... args);
115 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)
127 noexcept(!allocator_type::propagate_on_container_swap::value ||
128 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantda2df912011-06-02 16:10:22 +0000129 void clear() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000130};
131
132template <class T, class Allocator>
133 bool operator==(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
134template <class T, class Allocator>
135 bool operator< (const deque<T,Allocator>& x, const deque<T,Allocator>& y);
136template <class T, class Allocator>
137 bool operator!=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
138template <class T, class Allocator>
139 bool operator> (const deque<T,Allocator>& x, const deque<T,Allocator>& y);
140template <class T, class Allocator>
141 bool operator>=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
142template <class T, class Allocator>
143 bool operator<=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
144
145// specialized algorithms:
146template <class T, class Allocator>
Howard Hinnant287548a2011-06-03 17:30:28 +0000147 void swap(deque<T,Allocator>& x, deque<T,Allocator>& y)
148 noexcept(noexcept(x.swap(y)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000149
150} // std
151
152*/
153
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000154#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000155#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000156#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157
158#include <__config>
159#include <__split_buffer>
160#include <type_traits>
161#include <initializer_list>
162#include <iterator>
163#include <algorithm>
164#include <stdexcept>
165
Howard Hinnantc5a5fbd2011-11-29 16:45:27 +0000166#include <__undef_min_max>
167
Howard Hinnantc51e1022010-05-11 19:42:16 +0000168_LIBCPP_BEGIN_NAMESPACE_STD
169
170template <class _Tp, class _Allocator> class __deque_base;
171
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
264template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
265 class _DiffType, _DiffType _BlockSize>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000266class _LIBCPP_TYPE_VIS_ONLY __deque_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000267{
268 typedef _MapPointer __map_iterator;
269public:
270 typedef _Pointer pointer;
271 typedef _DiffType difference_type;
272private:
273 __map_iterator __m_iter_;
274 pointer __ptr_;
275
276 static const difference_type __block_size = _BlockSize;
277public:
278 typedef _ValueType value_type;
279 typedef random_access_iterator_tag iterator_category;
280 typedef _Reference reference;
281
Marshall Clow7a3a61d2013-08-06 16:14:36 +0000282 _LIBCPP_INLINE_VISIBILITY __deque_iterator() _NOEXCEPT
283#if _LIBCPP_STD_VER > 11
284 : __m_iter_(nullptr), __ptr_(nullptr)
285#endif
286 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000287
Howard Hinnantc834c512011-11-29 18:15:50 +0000288 template <class _Pp, class _Rp, class _MP>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000289 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +0000290 __deque_iterator(const __deque_iterator<value_type, _Pp, _Rp, _MP, difference_type, __block_size>& __it,
291 typename enable_if<is_convertible<_Pp, pointer>::value>::type* = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000292 : __m_iter_(__it.__m_iter_), __ptr_(__it.__ptr_) {}
293
294 _LIBCPP_INLINE_VISIBILITY reference operator*() const {return *__ptr_;}
295 _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return __ptr_;}
296
297 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator++()
298 {
299 if (++__ptr_ - *__m_iter_ == __block_size)
300 {
301 ++__m_iter_;
302 __ptr_ = *__m_iter_;
303 }
304 return *this;
305 }
306
307 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator++(int)
308 {
309 __deque_iterator __tmp = *this;
310 ++(*this);
311 return __tmp;
312 }
313
314 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator--()
315 {
316 if (__ptr_ == *__m_iter_)
317 {
318 --__m_iter_;
319 __ptr_ = *__m_iter_ + __block_size;
320 }
321 --__ptr_;
322 return *this;
323 }
324
325 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator--(int)
326 {
327 __deque_iterator __tmp = *this;
328 --(*this);
329 return __tmp;
330 }
331
332 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator+=(difference_type __n)
333 {
334 if (__n != 0)
335 {
336 __n += __ptr_ - *__m_iter_;
337 if (__n > 0)
338 {
339 __m_iter_ += __n / __block_size;
340 __ptr_ = *__m_iter_ + __n % __block_size;
341 }
342 else // (__n < 0)
343 {
344 difference_type __z = __block_size - 1 - __n;
345 __m_iter_ -= __z / __block_size;
346 __ptr_ = *__m_iter_ + (__block_size - 1 - __z % __block_size);
347 }
348 }
349 return *this;
350 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000351
Howard Hinnantc51e1022010-05-11 19:42:16 +0000352 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator-=(difference_type __n)
353 {
354 return *this += -__n;
355 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000356
Howard Hinnantc51e1022010-05-11 19:42:16 +0000357 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator+(difference_type __n) const
358 {
359 __deque_iterator __t(*this);
360 __t += __n;
361 return __t;
362 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000363
Howard Hinnantc51e1022010-05-11 19:42:16 +0000364 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator-(difference_type __n) const
365 {
366 __deque_iterator __t(*this);
367 __t -= __n;
368 return __t;
369 }
370
371 _LIBCPP_INLINE_VISIBILITY
372 friend __deque_iterator operator+(difference_type __n, const __deque_iterator& __it)
373 {return __it + __n;}
374
375 _LIBCPP_INLINE_VISIBILITY
376 friend difference_type operator-(const __deque_iterator& __x, const __deque_iterator& __y)
377 {
378 if (__x != __y)
379 return (__x.__m_iter_ - __y.__m_iter_) * __block_size
380 + (__x.__ptr_ - *__x.__m_iter_)
381 - (__y.__ptr_ - *__y.__m_iter_);
382 return 0;
383 }
384
385 _LIBCPP_INLINE_VISIBILITY reference operator[](difference_type __n) const
386 {return *(*this + __n);}
387
388 _LIBCPP_INLINE_VISIBILITY friend
389 bool operator==(const __deque_iterator& __x, const __deque_iterator& __y)
390 {return __x.__ptr_ == __y.__ptr_;}
391
392 _LIBCPP_INLINE_VISIBILITY friend
393 bool operator!=(const __deque_iterator& __x, const __deque_iterator& __y)
394 {return !(__x == __y);}
395
396 _LIBCPP_INLINE_VISIBILITY friend
397 bool operator<(const __deque_iterator& __x, const __deque_iterator& __y)
398 {return __x.__m_iter_ < __y.__m_iter_ ||
399 (__x.__m_iter_ == __y.__m_iter_ && __x.__ptr_ < __y.__ptr_);}
400
401 _LIBCPP_INLINE_VISIBILITY friend
402 bool operator>(const __deque_iterator& __x, const __deque_iterator& __y)
403 {return __y < __x;}
404
405 _LIBCPP_INLINE_VISIBILITY friend
406 bool operator<=(const __deque_iterator& __x, const __deque_iterator& __y)
407 {return !(__y < __x);}
408
409 _LIBCPP_INLINE_VISIBILITY friend
410 bool operator>=(const __deque_iterator& __x, const __deque_iterator& __y)
411 {return !(__x < __y);}
412
413private:
Howard Hinnantda2df912011-06-02 16:10:22 +0000414 _LIBCPP_INLINE_VISIBILITY __deque_iterator(__map_iterator __m, pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000415 : __m_iter_(__m), __ptr_(__p) {}
416
Howard Hinnantc834c512011-11-29 18:15:50 +0000417 template <class _Tp, class _Ap> friend class __deque_base;
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000418 template <class _Tp, class _Ap> friend class _LIBCPP_TYPE_VIS_ONLY deque;
Howard Hinnantc834c512011-11-29 18:15:50 +0000419 template <class _Vp, class _Pp, class _Rp, class _MP, class _Dp, _Dp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000420 friend class _LIBCPP_TYPE_VIS_ONLY __deque_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000421
422 template <class _RAIter,
423 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
424 friend
425 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
426 copy(_RAIter __f,
427 _RAIter __l,
428 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
429 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
430
431 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
432 class _OutputIterator>
433 friend
434 _OutputIterator
435 copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
436 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
437 _OutputIterator __r);
438
439 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
440 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
441 friend
442 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
443 copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
444 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
445 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
446
447 template <class _RAIter,
448 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
449 friend
450 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
451 copy_backward(_RAIter __f,
452 _RAIter __l,
453 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
454 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
455
456 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
457 class _OutputIterator>
458 friend
459 _OutputIterator
460 copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
461 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
462 _OutputIterator __r);
463
464 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
465 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
466 friend
467 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
468 copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
469 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
470 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
471
472 template <class _RAIter,
473 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
474 friend
475 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
476 move(_RAIter __f,
477 _RAIter __l,
478 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
479 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
480
481 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
482 class _OutputIterator>
483 friend
484 _OutputIterator
485 move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
486 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
487 _OutputIterator __r);
488
489 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
490 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
491 friend
492 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
493 move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
494 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
495 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
496
497 template <class _RAIter,
498 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
499 friend
500 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
501 move_backward(_RAIter __f,
502 _RAIter __l,
503 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
504 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
505
506 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
507 class _OutputIterator>
508 friend
509 _OutputIterator
510 move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
511 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
512 _OutputIterator __r);
513
514 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
515 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
516 friend
517 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
518 move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
519 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
520 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
521};
522
523// copy
524
525template <class _RAIter,
526 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
527__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
528copy(_RAIter __f,
529 _RAIter __l,
530 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
531 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
532{
533 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
534 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
535 while (__f != __l)
536 {
537 pointer __rb = __r.__ptr_;
538 pointer __re = *__r.__m_iter_ + _B2;
539 difference_type __bs = __re - __rb;
540 difference_type __n = __l - __f;
541 _RAIter __m = __l;
542 if (__n > __bs)
543 {
544 __n = __bs;
545 __m = __f + __n;
546 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000547 _VSTD::copy(__f, __m, __rb);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000548 __f = __m;
549 __r += __n;
550 }
551 return __r;
552}
553
554template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
555 class _OutputIterator>
556_OutputIterator
557copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
558 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
559 _OutputIterator __r)
560{
561 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
562 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
563 difference_type __n = __l - __f;
564 while (__n > 0)
565 {
566 pointer __fb = __f.__ptr_;
567 pointer __fe = *__f.__m_iter_ + _B1;
568 difference_type __bs = __fe - __fb;
569 if (__bs > __n)
570 {
571 __bs = __n;
572 __fe = __fb + __bs;
573 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000574 __r = _VSTD::copy(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000575 __n -= __bs;
576 __f += __bs;
577 }
578 return __r;
579}
580
581template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
582 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
583__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
584copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
585 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
586 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
587{
588 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
589 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
590 difference_type __n = __l - __f;
591 while (__n > 0)
592 {
593 pointer __fb = __f.__ptr_;
594 pointer __fe = *__f.__m_iter_ + _B1;
595 difference_type __bs = __fe - __fb;
596 if (__bs > __n)
597 {
598 __bs = __n;
599 __fe = __fb + __bs;
600 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000601 __r = _VSTD::copy(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000602 __n -= __bs;
603 __f += __bs;
604 }
605 return __r;
606}
607
608// copy_backward
609
610template <class _RAIter,
611 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
612__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
613copy_backward(_RAIter __f,
614 _RAIter __l,
615 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
616 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
617{
618 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
619 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
620 while (__f != __l)
621 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000622 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _VSTD::prev(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000623 pointer __rb = *__rp.__m_iter_;
624 pointer __re = __rp.__ptr_ + 1;
625 difference_type __bs = __re - __rb;
626 difference_type __n = __l - __f;
627 _RAIter __m = __f;
628 if (__n > __bs)
629 {
630 __n = __bs;
631 __m = __l - __n;
632 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000633 _VSTD::copy_backward(__m, __l, __re);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000634 __l = __m;
635 __r -= __n;
636 }
637 return __r;
638}
639
640template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
641 class _OutputIterator>
642_OutputIterator
643copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
644 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
645 _OutputIterator __r)
646{
647 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
648 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
649 difference_type __n = __l - __f;
650 while (__n > 0)
651 {
652 --__l;
653 pointer __lb = *__l.__m_iter_;
654 pointer __le = __l.__ptr_ + 1;
655 difference_type __bs = __le - __lb;
656 if (__bs > __n)
657 {
658 __bs = __n;
659 __lb = __le - __bs;
660 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000661 __r = _VSTD::copy_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000662 __n -= __bs;
663 __l -= __bs - 1;
664 }
665 return __r;
666}
667
668template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
669 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
670__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
671copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
672 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
673 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
674{
675 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
676 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
677 difference_type __n = __l - __f;
678 while (__n > 0)
679 {
680 --__l;
681 pointer __lb = *__l.__m_iter_;
682 pointer __le = __l.__ptr_ + 1;
683 difference_type __bs = __le - __lb;
684 if (__bs > __n)
685 {
686 __bs = __n;
687 __lb = __le - __bs;
688 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000689 __r = _VSTD::copy_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000690 __n -= __bs;
691 __l -= __bs - 1;
692 }
693 return __r;
694}
695
696// move
697
698template <class _RAIter,
699 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
700__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
701move(_RAIter __f,
702 _RAIter __l,
703 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
704 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
705{
706 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
707 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
708 while (__f != __l)
709 {
710 pointer __rb = __r.__ptr_;
711 pointer __re = *__r.__m_iter_ + _B2;
712 difference_type __bs = __re - __rb;
713 difference_type __n = __l - __f;
714 _RAIter __m = __l;
715 if (__n > __bs)
716 {
717 __n = __bs;
718 __m = __f + __n;
719 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000720 _VSTD::move(__f, __m, __rb);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000721 __f = __m;
722 __r += __n;
723 }
724 return __r;
725}
726
727template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
728 class _OutputIterator>
729_OutputIterator
730move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
731 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
732 _OutputIterator __r)
733{
734 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
735 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
736 difference_type __n = __l - __f;
737 while (__n > 0)
738 {
739 pointer __fb = __f.__ptr_;
740 pointer __fe = *__f.__m_iter_ + _B1;
741 difference_type __bs = __fe - __fb;
742 if (__bs > __n)
743 {
744 __bs = __n;
745 __fe = __fb + __bs;
746 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000747 __r = _VSTD::move(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000748 __n -= __bs;
749 __f += __bs;
750 }
751 return __r;
752}
753
754template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
755 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
756__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
757move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
758 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
759 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
760{
761 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
762 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
763 difference_type __n = __l - __f;
764 while (__n > 0)
765 {
766 pointer __fb = __f.__ptr_;
767 pointer __fe = *__f.__m_iter_ + _B1;
768 difference_type __bs = __fe - __fb;
769 if (__bs > __n)
770 {
771 __bs = __n;
772 __fe = __fb + __bs;
773 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000774 __r = _VSTD::move(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000775 __n -= __bs;
776 __f += __bs;
777 }
778 return __r;
779}
780
781// move_backward
782
783template <class _RAIter,
784 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
785__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
786move_backward(_RAIter __f,
787 _RAIter __l,
788 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
789 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
790{
791 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
792 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
793 while (__f != __l)
794 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000795 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _VSTD::prev(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000796 pointer __rb = *__rp.__m_iter_;
797 pointer __re = __rp.__ptr_ + 1;
798 difference_type __bs = __re - __rb;
799 difference_type __n = __l - __f;
800 _RAIter __m = __f;
801 if (__n > __bs)
802 {
803 __n = __bs;
804 __m = __l - __n;
805 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000806 _VSTD::move_backward(__m, __l, __re);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000807 __l = __m;
808 __r -= __n;
809 }
810 return __r;
811}
812
813template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
814 class _OutputIterator>
815_OutputIterator
816move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
817 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
818 _OutputIterator __r)
819{
820 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
821 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
822 difference_type __n = __l - __f;
823 while (__n > 0)
824 {
825 --__l;
826 pointer __lb = *__l.__m_iter_;
827 pointer __le = __l.__ptr_ + 1;
828 difference_type __bs = __le - __lb;
829 if (__bs > __n)
830 {
831 __bs = __n;
832 __lb = __le - __bs;
833 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000834 __r = _VSTD::move_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000835 __n -= __bs;
836 __l -= __bs - 1;
837 }
838 return __r;
839}
840
841template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
842 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
843__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
844move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
845 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
846 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
847{
848 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
849 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
850 difference_type __n = __l - __f;
851 while (__n > 0)
852 {
853 --__l;
854 pointer __lb = *__l.__m_iter_;
855 pointer __le = __l.__ptr_ + 1;
856 difference_type __bs = __le - __lb;
857 if (__bs > __n)
858 {
859 __bs = __n;
860 __lb = __le - __bs;
861 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000862 __r = _VSTD::move_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000863 __n -= __bs;
864 __l -= __bs - 1;
865 }
866 return __r;
867}
868
869template <bool>
870class __deque_base_common
871{
872protected:
873 void __throw_length_error() const;
874 void __throw_out_of_range() const;
875};
876
877template <bool __b>
878void
879__deque_base_common<__b>::__throw_length_error() const
880{
881#ifndef _LIBCPP_NO_EXCEPTIONS
882 throw length_error("deque");
883#endif
884}
885
886template <bool __b>
887void
888__deque_base_common<__b>::__throw_out_of_range() const
889{
890#ifndef _LIBCPP_NO_EXCEPTIONS
891 throw out_of_range("deque");
892#endif
893}
894
895template <class _Tp, class _Allocator>
896class __deque_base
897 : protected __deque_base_common<true>
898{
899 __deque_base(const __deque_base& __c);
900 __deque_base& operator=(const __deque_base& __c);
901protected:
902 typedef _Tp value_type;
903 typedef _Allocator allocator_type;
904 typedef allocator_traits<allocator_type> __alloc_traits;
905 typedef value_type& reference;
906 typedef const value_type& const_reference;
907 typedef typename __alloc_traits::size_type size_type;
908 typedef typename __alloc_traits::difference_type difference_type;
909 typedef typename __alloc_traits::pointer pointer;
910 typedef typename __alloc_traits::const_pointer const_pointer;
911
912 static const difference_type __block_size = sizeof(value_type) < 256 ? 4096 / sizeof(value_type) : 16;
913
914 typedef typename __alloc_traits::template
915#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
916 rebind_alloc<pointer>
917#else
918 rebind_alloc<pointer>::other
919#endif
920 __pointer_allocator;
921 typedef allocator_traits<__pointer_allocator> __map_traits;
922 typedef typename __map_traits::pointer __map_pointer;
Howard Hinnantdcb73a32013-06-23 21:17:24 +0000923 typedef typename __alloc_traits::template
924#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
925 rebind_alloc<const_pointer>
926#else
927 rebind_alloc<const_pointer>::other
928#endif
929 __const_pointer_allocator;
930 typedef typename allocator_traits<__const_pointer_allocator>::const_pointer __map_const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000931 typedef __split_buffer<pointer, __pointer_allocator> __map;
932
933 typedef __deque_iterator<value_type, pointer, reference, __map_pointer,
934 difference_type, __block_size> iterator;
935 typedef __deque_iterator<value_type, const_pointer, const_reference, __map_const_pointer,
936 difference_type, __block_size> const_iterator;
937
938 __map __map_;
939 size_type __start_;
940 __compressed_pair<size_type, allocator_type> __size_;
941
Howard Hinnantda2df912011-06-02 16:10:22 +0000942 iterator begin() _NOEXCEPT;
943 const_iterator begin() const _NOEXCEPT;
944 iterator end() _NOEXCEPT;
945 const_iterator end() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000946
947 _LIBCPP_INLINE_VISIBILITY size_type& size() {return __size_.first();}
Howard Hinnantda2df912011-06-02 16:10:22 +0000948 _LIBCPP_INLINE_VISIBILITY
949 const size_type& size() const _NOEXCEPT {return __size_.first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000950 _LIBCPP_INLINE_VISIBILITY allocator_type& __alloc() {return __size_.second();}
Howard Hinnantda2df912011-06-02 16:10:22 +0000951 _LIBCPP_INLINE_VISIBILITY
952 const allocator_type& __alloc() const _NOEXCEPT {return __size_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000953
Howard Hinnantad979ba2011-06-03 15:16:49 +0000954 __deque_base()
955 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000956 explicit __deque_base(const allocator_type& __a);
Howard Hinnantca2fc222011-06-02 20:00:14 +0000957public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000958 ~__deque_base();
959
Howard Hinnant74279a52010-09-04 23:28:19 +0000960#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000961
Howard Hinnantca2fc222011-06-02 20:00:14 +0000962 __deque_base(__deque_base&& __c)
963 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000964 __deque_base(__deque_base&& __c, const allocator_type& __a);
965
Howard Hinnant74279a52010-09-04 23:28:19 +0000966#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantca2fc222011-06-02 20:00:14 +0000967 void swap(__deque_base& __c)
Howard Hinnantbc47c7f2011-06-03 16:20:53 +0000968 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Howard Hinnantca2fc222011-06-02 20:00:14 +0000969 __is_nothrow_swappable<allocator_type>::value);
970protected:
Howard Hinnantda2df912011-06-02 16:10:22 +0000971 void clear() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000972
973 bool __invariants() const;
974
Howard Hinnant874ad9a2010-09-21 21:28:23 +0000975 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000976 void __move_assign(__deque_base& __c)
Howard Hinnant4931e092011-06-02 21:38:57 +0000977 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
978 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000979 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000980 __map_ = _VSTD::move(__c.__map_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000981 __start_ = __c.__start_;
982 size() = __c.size();
983 __move_assign_alloc(__c);
984 __c.__start_ = __c.size() = 0;
985 }
986
Howard Hinnant874ad9a2010-09-21 21:28:23 +0000987 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000988 void __move_assign_alloc(__deque_base& __c)
Howard Hinnantca2fc222011-06-02 20:00:14 +0000989 _NOEXCEPT_(!__alloc_traits::propagate_on_container_move_assignment::value ||
990 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000991 {__move_assign_alloc(__c, integral_constant<bool,
992 __alloc_traits::propagate_on_container_move_assignment::value>());}
993
994private:
Howard Hinnant874ad9a2010-09-21 21:28:23 +0000995 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +0000996 void __move_assign_alloc(__deque_base& __c, true_type)
Howard Hinnantca2fc222011-06-02 20:00:14 +0000997 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000998 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000999 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001000 }
1001
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001002 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001003 void __move_assign_alloc(__deque_base&, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001004 {}
1005
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001006 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001007 static void __swap_alloc(allocator_type& __x, allocator_type& __y)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001008 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1009 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001010 {__swap_alloc(__x, __y, integral_constant<bool,
1011 __alloc_traits::propagate_on_container_swap::value>());}
1012
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001013 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001014 static void __swap_alloc(allocator_type& __x, allocator_type& __y, true_type)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001015 _NOEXCEPT_(__is_nothrow_swappable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001016 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001017 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001018 swap(__x, __y);
1019 }
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001020
1021 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001022 static void __swap_alloc(allocator_type&, allocator_type&, false_type)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001023 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001024 {}
1025};
1026
1027template <class _Tp, class _Allocator>
1028bool
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>
1091inline _LIBCPP_INLINE_VISIBILITY
1092__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>
1097inline _LIBCPP_INLINE_VISIBILITY
1098__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)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001148 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value||
1149 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150{
1151 __map_.swap(__c.__map_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001152 _VSTD::swap(__start_, __c.__start_);
1153 _VSTD::swap(size(), __c.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001154 __swap_alloc(__alloc(), __c.__alloc());
1155}
1156
1157template <class _Tp, class _Allocator>
1158void
Howard Hinnantda2df912011-06-02 16:10:22 +00001159__deque_base<_Tp, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001160{
1161 allocator_type& __a = __alloc();
1162 for (iterator __i = begin(), __e = end(); __i != __e; ++__i)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001163 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001164 size() = 0;
1165 while (__map_.size() > 2)
1166 {
1167 __alloc_traits::deallocate(__a, __map_.front(), __block_size);
1168 __map_.pop_front();
1169 }
1170 switch (__map_.size())
1171 {
1172 case 1:
1173 __start_ = __block_size / 2;
1174 break;
1175 case 2:
1176 __start_ = __block_size;
1177 break;
1178 }
1179}
1180
1181template <class _Tp, class _Allocator = allocator<_Tp> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001182class _LIBCPP_TYPE_VIS_ONLY deque
Howard Hinnantc51e1022010-05-11 19:42:16 +00001183 : private __deque_base<_Tp, _Allocator>
1184{
1185public:
1186 // types:
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001187
Howard Hinnantc51e1022010-05-11 19:42:16 +00001188 typedef _Tp value_type;
1189 typedef _Allocator allocator_type;
1190
1191 typedef __deque_base<value_type, allocator_type> __base;
1192
1193 typedef typename __base::__alloc_traits __alloc_traits;
1194 typedef typename __base::reference reference;
1195 typedef typename __base::const_reference const_reference;
1196 typedef typename __base::iterator iterator;
1197 typedef typename __base::const_iterator const_iterator;
1198 typedef typename __base::size_type size_type;
1199 typedef typename __base::difference_type difference_type;
1200
1201 typedef typename __base::pointer pointer;
1202 typedef typename __base::const_pointer const_pointer;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001203 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
1204 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001205
1206 // construct/copy/destroy:
Howard Hinnantad979ba2011-06-03 15:16:49 +00001207 _LIBCPP_INLINE_VISIBILITY
1208 deque()
1209 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
1210 {}
Marshall Clow7ed23a72014-03-05 19:06:20 +00001211 _LIBCPP_INLINE_VISIBILITY explicit deque(const allocator_type& __a) : __base(__a) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001212 explicit deque(size_type __n);
Marshall Clowbc4fcb42013-09-07 16:16:19 +00001213#if _LIBCPP_STD_VER > 11
1214 explicit deque(size_type __n, const _Allocator& __a);
1215#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001216 deque(size_type __n, const value_type& __v);
1217 deque(size_type __n, const value_type& __v, const allocator_type& __a);
1218 template <class _InputIter>
1219 deque(_InputIter __f, _InputIter __l,
1220 typename enable_if<__is_input_iterator<_InputIter>::value>::type* = 0);
1221 template <class _InputIter>
1222 deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
1223 typename enable_if<__is_input_iterator<_InputIter>::value>::type* = 0);
1224 deque(const deque& __c);
1225 deque(const deque& __c, const allocator_type& __a);
Howard Hinnant33711792011-08-12 21:56:02 +00001226#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001227 deque(initializer_list<value_type> __il);
1228 deque(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnant33711792011-08-12 21:56:02 +00001229#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001230
1231 deque& operator=(const deque& __c);
Howard Hinnant33711792011-08-12 21:56:02 +00001232#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001233 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001234 deque& operator=(initializer_list<value_type> __il) {assign(__il); return *this;}
Howard Hinnant33711792011-08-12 21:56:02 +00001235#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001236
Howard Hinnant74279a52010-09-04 23:28:19 +00001237#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantca2fc222011-06-02 20:00:14 +00001238 deque(deque&& __c) _NOEXCEPT_(is_nothrow_move_constructible<__base>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001239 deque(deque&& __c, const allocator_type& __a);
Howard Hinnantca2fc222011-06-02 20:00:14 +00001240 deque& operator=(deque&& __c)
Howard Hinnant4931e092011-06-02 21:38:57 +00001241 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1242 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant74279a52010-09-04 23:28:19 +00001243#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001244
1245 template <class _InputIter>
1246 void assign(_InputIter __f, _InputIter __l,
1247 typename enable_if<__is_input_iterator<_InputIter>::value &&
1248 !__is_random_access_iterator<_InputIter>::value>::type* = 0);
1249 template <class _RAIter>
1250 void assign(_RAIter __f, _RAIter __l,
1251 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
1252 void assign(size_type __n, const value_type& __v);
Howard Hinnant33711792011-08-12 21:56:02 +00001253#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001254 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001255 void assign(initializer_list<value_type> __il) {assign(__il.begin(), __il.end());}
Howard Hinnant33711792011-08-12 21:56:02 +00001256#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001257
Howard Hinnantda2df912011-06-02 16:10:22 +00001258 allocator_type get_allocator() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001259
1260 // iterators:
1261
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001262 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001263 iterator begin() _NOEXCEPT {return __base::begin();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001264 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001265 const_iterator begin() const _NOEXCEPT {return __base::begin();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001266 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001267 iterator end() _NOEXCEPT {return __base::end();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001268 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001269 const_iterator end() const _NOEXCEPT {return __base::end();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001270
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001271 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001272 reverse_iterator rbegin() _NOEXCEPT
1273 {return reverse_iterator(__base::end());}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001274 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001275 const_reverse_iterator rbegin() const _NOEXCEPT
1276 {return const_reverse_iterator(__base::end());}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001277 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001278 reverse_iterator rend() _NOEXCEPT
1279 {return reverse_iterator(__base::begin());}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001280 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001281 const_reverse_iterator rend() const _NOEXCEPT
1282 {return const_reverse_iterator(__base::begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001283
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001284 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001285 const_iterator cbegin() const _NOEXCEPT
1286 {return __base::begin();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001287 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001288 const_iterator cend() const _NOEXCEPT
1289 {return __base::end();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001290 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001291 const_reverse_iterator crbegin() const _NOEXCEPT
1292 {return const_reverse_iterator(__base::end());}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001293 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001294 const_reverse_iterator crend() const _NOEXCEPT
1295 {return const_reverse_iterator(__base::begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001296
1297 // capacity:
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001298 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001299 size_type size() const _NOEXCEPT {return __base::size();}
1300 _LIBCPP_INLINE_VISIBILITY
1301 size_type max_size() const _NOEXCEPT
1302 {return __alloc_traits::max_size(__base::__alloc());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001303 void resize(size_type __n);
1304 void resize(size_type __n, const value_type& __v);
Howard Hinnant4931e092011-06-02 21:38:57 +00001305 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantda2df912011-06-02 16:10:22 +00001306 _LIBCPP_INLINE_VISIBILITY
1307 bool empty() const _NOEXCEPT {return __base::size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001308
1309 // element access:
1310 reference operator[](size_type __i);
1311 const_reference operator[](size_type __i) const;
1312 reference at(size_type __i);
1313 const_reference at(size_type __i) const;
1314 reference front();
1315 const_reference front() const;
1316 reference back();
1317 const_reference back() const;
1318
1319 // 23.2.2.3 modifiers:
1320 void push_front(const value_type& __v);
1321 void push_back(const value_type& __v);
Howard Hinnant74279a52010-09-04 23:28:19 +00001322#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1323#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001324 template <class... _Args> void emplace_front(_Args&&... __args);
1325 template <class... _Args> void emplace_back(_Args&&... __args);
1326 template <class... _Args> iterator emplace(const_iterator __p, _Args&&... __args);
Howard Hinnant74279a52010-09-04 23:28:19 +00001327#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001328 void push_front(value_type&& __v);
1329 void push_back(value_type&& __v);
1330 iterator insert(const_iterator __p, value_type&& __v);
Howard Hinnant74279a52010-09-04 23:28:19 +00001331#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001332 iterator insert(const_iterator __p, const value_type& __v);
1333 iterator insert(const_iterator __p, size_type __n, const value_type& __v);
1334 template <class _InputIter>
Marshall Clow6b612cf2015-01-22 18:33:29 +00001335 iterator insert(const_iterator __p, _InputIter __f, _InputIter __l,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001336 typename enable_if<__is_input_iterator<_InputIter>::value
Marshall Clow6b612cf2015-01-22 18:33:29 +00001337 &&!__is_forward_iterator<_InputIter>::value>::type* = 0);
1338 template <class _ForwardIterator>
1339 iterator insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l,
1340 typename enable_if<__is_forward_iterator<_ForwardIterator>::value
1341 &&!__is_bidirectional_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001342 template <class _BiIter>
Marshall Clow6b612cf2015-01-22 18:33:29 +00001343 iterator insert(const_iterator __p, _BiIter __f, _BiIter __l,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001344 typename enable_if<__is_bidirectional_iterator<_BiIter>::value>::type* = 0);
Howard Hinnant33711792011-08-12 21:56:02 +00001345#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001346 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001347 iterator insert(const_iterator __p, initializer_list<value_type> __il)
1348 {return insert(__p, __il.begin(), __il.end());}
Howard Hinnant33711792011-08-12 21:56:02 +00001349#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001350 void pop_front();
1351 void pop_back();
1352 iterator erase(const_iterator __p);
1353 iterator erase(const_iterator __f, const_iterator __l);
1354
Howard Hinnantca2fc222011-06-02 20:00:14 +00001355 void swap(deque& __c)
1356 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1357 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantda2df912011-06-02 16:10:22 +00001358 void clear() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001359
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001360 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001361 bool __invariants() const {return __base::__invariants();}
1362private:
Howard Hinnantdcb73a32013-06-23 21:17:24 +00001363 typedef typename __base::__map_const_pointer __map_const_pointer;
1364
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001365 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001366 static size_type __recommend_blocks(size_type __n)
1367 {
1368 return __n / __base::__block_size + (__n % __base::__block_size != 0);
1369 }
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001370 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001371 size_type __capacity() const
1372 {
1373 return __base::__map_.size() == 0 ? 0 : __base::__map_.size() * __base::__block_size - 1;
1374 }
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001375 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001376 size_type __front_spare() const
1377 {
1378 return __base::__start_;
1379 }
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001380 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001381 size_type __back_spare() const
1382 {
1383 return __capacity() - (__base::__start_ + __base::size());
1384 }
1385
1386 template <class _InpIter>
1387 void __append(_InpIter __f, _InpIter __l,
1388 typename enable_if<__is_input_iterator<_InpIter>::value &&
1389 !__is_forward_iterator<_InpIter>::value>::type* = 0);
1390 template <class _ForIter>
1391 void __append(_ForIter __f, _ForIter __l,
1392 typename enable_if<__is_forward_iterator<_ForIter>::value>::type* = 0);
1393 void __append(size_type __n);
1394 void __append(size_type __n, const value_type& __v);
1395 void __erase_to_end(const_iterator __f);
1396 void __add_front_capacity();
1397 void __add_front_capacity(size_type __n);
1398 void __add_back_capacity();
1399 void __add_back_capacity(size_type __n);
1400 iterator __move_and_check(iterator __f, iterator __l, iterator __r,
1401 const_pointer& __vt);
1402 iterator __move_backward_and_check(iterator __f, iterator __l, iterator __r,
1403 const_pointer& __vt);
1404 void __move_construct_and_check(iterator __f, iterator __l,
1405 iterator __r, const_pointer& __vt);
1406 void __move_construct_backward_and_check(iterator __f, iterator __l,
1407 iterator __r, const_pointer& __vt);
1408
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001409 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001410 void __copy_assign_alloc(const deque& __c)
1411 {__copy_assign_alloc(__c, integral_constant<bool,
1412 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1413
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001414 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001415 void __copy_assign_alloc(const deque& __c, true_type)
1416 {
1417 if (__base::__alloc() != __c.__alloc())
1418 {
1419 clear();
1420 shrink_to_fit();
1421 }
1422 __base::__alloc() = __c.__alloc();
1423 __base::__map_.__alloc() = __c.__map_.__alloc();
1424 }
1425
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001426 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001427 void __copy_assign_alloc(const deque&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001428 {}
1429
Howard Hinnant4931e092011-06-02 21:38:57 +00001430 void __move_assign(deque& __c, true_type)
1431 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001432 void __move_assign(deque& __c, false_type);
1433};
1434
1435template <class _Tp, class _Allocator>
1436deque<_Tp, _Allocator>::deque(size_type __n)
1437{
1438 if (__n > 0)
1439 __append(__n);
1440}
1441
Marshall Clowbc4fcb42013-09-07 16:16:19 +00001442#if _LIBCPP_STD_VER > 11
1443template <class _Tp, class _Allocator>
1444deque<_Tp, _Allocator>::deque(size_type __n, const _Allocator& __a)
1445 : __base(__a)
1446{
1447 if (__n > 0)
1448 __append(__n);
1449}
1450#endif
1451
Howard Hinnantc51e1022010-05-11 19:42:16 +00001452template <class _Tp, class _Allocator>
1453deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v)
1454{
1455 if (__n > 0)
1456 __append(__n, __v);
1457}
1458
1459template <class _Tp, class _Allocator>
1460deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v, const allocator_type& __a)
1461 : __base(__a)
1462{
1463 if (__n > 0)
1464 __append(__n, __v);
1465}
1466
1467template <class _Tp, class _Allocator>
1468template <class _InputIter>
1469deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l,
1470 typename enable_if<__is_input_iterator<_InputIter>::value>::type*)
1471{
1472 __append(__f, __l);
1473}
1474
1475template <class _Tp, class _Allocator>
1476template <class _InputIter>
1477deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
1478 typename enable_if<__is_input_iterator<_InputIter>::value>::type*)
1479 : __base(__a)
1480{
1481 __append(__f, __l);
1482}
1483
1484template <class _Tp, class _Allocator>
1485deque<_Tp, _Allocator>::deque(const deque& __c)
1486 : __base(__alloc_traits::select_on_container_copy_construction(__c.__alloc()))
1487{
1488 __append(__c.begin(), __c.end());
1489}
1490
1491template <class _Tp, class _Allocator>
1492deque<_Tp, _Allocator>::deque(const deque& __c, const allocator_type& __a)
1493 : __base(__a)
1494{
1495 __append(__c.begin(), __c.end());
1496}
1497
Howard Hinnant33711792011-08-12 21:56:02 +00001498#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1499
Howard Hinnantc51e1022010-05-11 19:42:16 +00001500template <class _Tp, class _Allocator>
1501deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il)
1502{
1503 __append(__il.begin(), __il.end());
1504}
1505
1506template <class _Tp, class _Allocator>
1507deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il, const allocator_type& __a)
1508 : __base(__a)
1509{
1510 __append(__il.begin(), __il.end());
1511}
1512
Howard Hinnant33711792011-08-12 21:56:02 +00001513#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1514
Howard Hinnantc51e1022010-05-11 19:42:16 +00001515template <class _Tp, class _Allocator>
1516deque<_Tp, _Allocator>&
1517deque<_Tp, _Allocator>::operator=(const deque& __c)
1518{
1519 if (this != &__c)
1520 {
1521 __copy_assign_alloc(__c);
1522 assign(__c.begin(), __c.end());
1523 }
1524 return *this;
1525}
1526
Howard Hinnant74279a52010-09-04 23:28:19 +00001527#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001528
1529template <class _Tp, class _Allocator>
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001530inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001531deque<_Tp, _Allocator>::deque(deque&& __c)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001532 _NOEXCEPT_(is_nothrow_move_constructible<__base>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001533 : __base(_VSTD::move(__c))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001534{
1535}
1536
1537template <class _Tp, class _Allocator>
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001538inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001539deque<_Tp, _Allocator>::deque(deque&& __c, const allocator_type& __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001540 : __base(_VSTD::move(__c), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001541{
1542 if (__a != __c.__alloc())
1543 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001544 typedef move_iterator<iterator> _Ip;
1545 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001546 }
1547}
1548
1549template <class _Tp, class _Allocator>
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001550inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001551deque<_Tp, _Allocator>&
1552deque<_Tp, _Allocator>::operator=(deque&& __c)
Howard Hinnant4931e092011-06-02 21:38:57 +00001553 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1554 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001555{
1556 __move_assign(__c, integral_constant<bool,
1557 __alloc_traits::propagate_on_container_move_assignment::value>());
1558 return *this;
1559}
1560
1561template <class _Tp, class _Allocator>
1562void
1563deque<_Tp, _Allocator>::__move_assign(deque& __c, false_type)
1564{
1565 if (__base::__alloc() != __c.__alloc())
1566 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001567 typedef move_iterator<iterator> _Ip;
1568 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001569 }
1570 else
1571 __move_assign(__c, true_type());
1572}
1573
1574template <class _Tp, class _Allocator>
1575void
1576deque<_Tp, _Allocator>::__move_assign(deque& __c, true_type)
Howard Hinnant4931e092011-06-02 21:38:57 +00001577 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001578{
1579 clear();
1580 shrink_to_fit();
1581 __base::__move_assign(__c);
1582}
1583
Howard Hinnant74279a52010-09-04 23:28:19 +00001584#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001585
1586template <class _Tp, class _Allocator>
1587template <class _InputIter>
1588void
1589deque<_Tp, _Allocator>::assign(_InputIter __f, _InputIter __l,
1590 typename enable_if<__is_input_iterator<_InputIter>::value &&
1591 !__is_random_access_iterator<_InputIter>::value>::type*)
1592{
1593 iterator __i = __base::begin();
1594 iterator __e = __base::end();
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001595 for (; __f != __l && __i != __e; ++__f, (void) ++__i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001596 *__i = *__f;
1597 if (__f != __l)
1598 __append(__f, __l);
1599 else
1600 __erase_to_end(__i);
1601}
1602
1603template <class _Tp, class _Allocator>
1604template <class _RAIter>
1605void
1606deque<_Tp, _Allocator>::assign(_RAIter __f, _RAIter __l,
1607 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
1608{
1609 if (static_cast<size_type>(__l - __f) > __base::size())
1610 {
1611 _RAIter __m = __f + __base::size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001612 _VSTD::copy(__f, __m, __base::begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001613 __append(__m, __l);
1614 }
1615 else
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001616 __erase_to_end(_VSTD::copy(__f, __l, __base::begin()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001617}
1618
1619template <class _Tp, class _Allocator>
1620void
1621deque<_Tp, _Allocator>::assign(size_type __n, const value_type& __v)
1622{
1623 if (__n > __base::size())
1624 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001625 _VSTD::fill_n(__base::begin(), __base::size(), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001626 __n -= __base::size();
1627 __append(__n, __v);
1628 }
1629 else
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001630 __erase_to_end(_VSTD::fill_n(__base::begin(), __n, __v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001631}
1632
1633template <class _Tp, class _Allocator>
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001634inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001635_Allocator
Howard Hinnantda2df912011-06-02 16:10:22 +00001636deque<_Tp, _Allocator>::get_allocator() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001637{
1638 return __base::__alloc();
1639}
1640
1641template <class _Tp, class _Allocator>
1642void
1643deque<_Tp, _Allocator>::resize(size_type __n)
1644{
1645 if (__n > __base::size())
1646 __append(__n - __base::size());
1647 else if (__n < __base::size())
1648 __erase_to_end(__base::begin() + __n);
1649}
1650
1651template <class _Tp, class _Allocator>
1652void
1653deque<_Tp, _Allocator>::resize(size_type __n, const value_type& __v)
1654{
1655 if (__n > __base::size())
1656 __append(__n - __base::size(), __v);
1657 else if (__n < __base::size())
1658 __erase_to_end(__base::begin() + __n);
1659}
1660
1661template <class _Tp, class _Allocator>
1662void
Howard Hinnant4931e092011-06-02 21:38:57 +00001663deque<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001664{
1665 allocator_type& __a = __base::__alloc();
1666 if (empty())
1667 {
1668 while (__base::__map_.size() > 0)
1669 {
1670 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
1671 __base::__map_.pop_back();
1672 }
1673 __base::__start_ = 0;
1674 }
1675 else
1676 {
1677 if (__front_spare() >= __base::__block_size)
1678 {
1679 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
1680 __base::__map_.pop_front();
1681 __base::__start_ -= __base::__block_size;
1682 }
1683 if (__back_spare() >= __base::__block_size)
1684 {
1685 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
1686 __base::__map_.pop_back();
1687 }
1688 }
1689 __base::__map_.shrink_to_fit();
1690}
1691
1692template <class _Tp, class _Allocator>
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001693inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001694typename deque<_Tp, _Allocator>::reference
1695deque<_Tp, _Allocator>::operator[](size_type __i)
1696{
1697 size_type __p = __base::__start_ + __i;
1698 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1699}
1700
1701template <class _Tp, class _Allocator>
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001702inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001703typename deque<_Tp, _Allocator>::const_reference
1704deque<_Tp, _Allocator>::operator[](size_type __i) const
1705{
1706 size_type __p = __base::__start_ + __i;
1707 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1708}
1709
1710template <class _Tp, class _Allocator>
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001711inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001712typename deque<_Tp, _Allocator>::reference
1713deque<_Tp, _Allocator>::at(size_type __i)
1714{
1715 if (__i >= __base::size())
1716 __base::__throw_out_of_range();
1717 size_type __p = __base::__start_ + __i;
1718 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1719}
1720
1721template <class _Tp, class _Allocator>
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001722inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001723typename deque<_Tp, _Allocator>::const_reference
1724deque<_Tp, _Allocator>::at(size_type __i) const
1725{
1726 if (__i >= __base::size())
1727 __base::__throw_out_of_range();
1728 size_type __p = __base::__start_ + __i;
1729 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1730}
1731
1732template <class _Tp, class _Allocator>
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001733inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001734typename deque<_Tp, _Allocator>::reference
1735deque<_Tp, _Allocator>::front()
1736{
1737 return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1738 + __base::__start_ % __base::__block_size);
1739}
1740
1741template <class _Tp, class _Allocator>
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001742inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001743typename deque<_Tp, _Allocator>::const_reference
1744deque<_Tp, _Allocator>::front() const
1745{
1746 return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1747 + __base::__start_ % __base::__block_size);
1748}
1749
1750template <class _Tp, class _Allocator>
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001751inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001752typename deque<_Tp, _Allocator>::reference
1753deque<_Tp, _Allocator>::back()
1754{
1755 size_type __p = __base::size() + __base::__start_ - 1;
1756 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1757}
1758
1759template <class _Tp, class _Allocator>
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001760inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001761typename deque<_Tp, _Allocator>::const_reference
1762deque<_Tp, _Allocator>::back() const
1763{
1764 size_type __p = __base::size() + __base::__start_ - 1;
1765 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1766}
1767
1768template <class _Tp, class _Allocator>
1769void
1770deque<_Tp, _Allocator>::push_back(const value_type& __v)
1771{
1772 allocator_type& __a = __base::__alloc();
1773 if (__back_spare() == 0)
1774 __add_back_capacity();
1775 // __back_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001776 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001777 ++__base::size();
1778}
1779
Howard Hinnant74279a52010-09-04 23:28:19 +00001780#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001781
1782template <class _Tp, class _Allocator>
1783void
1784deque<_Tp, _Allocator>::push_back(value_type&& __v)
1785{
1786 allocator_type& __a = __base::__alloc();
1787 if (__back_spare() == 0)
1788 __add_back_capacity();
1789 // __back_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001790 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001791 ++__base::size();
1792}
1793
Howard Hinnant74279a52010-09-04 23:28:19 +00001794#ifndef _LIBCPP_HAS_NO_VARIADICS
1795
Howard Hinnantc51e1022010-05-11 19:42:16 +00001796template <class _Tp, class _Allocator>
1797template <class... _Args>
1798void
1799deque<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1800{
1801 allocator_type& __a = __base::__alloc();
1802 if (__back_spare() == 0)
1803 __add_back_capacity();
1804 // __back_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001805 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001806 ++__base::size();
1807}
1808
Howard Hinnant74279a52010-09-04 23:28:19 +00001809#endif // _LIBCPP_HAS_NO_VARIADICS
1810#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001811
1812template <class _Tp, class _Allocator>
1813void
1814deque<_Tp, _Allocator>::push_front(const value_type& __v)
1815{
1816 allocator_type& __a = __base::__alloc();
1817 if (__front_spare() == 0)
1818 __add_front_capacity();
1819 // __front_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001820 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001821 --__base::__start_;
1822 ++__base::size();
1823}
1824
Howard Hinnant74279a52010-09-04 23:28:19 +00001825#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001826
1827template <class _Tp, class _Allocator>
1828void
1829deque<_Tp, _Allocator>::push_front(value_type&& __v)
1830{
1831 allocator_type& __a = __base::__alloc();
1832 if (__front_spare() == 0)
1833 __add_front_capacity();
1834 // __front_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001835 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001836 --__base::__start_;
1837 ++__base::size();
1838}
1839
Howard Hinnant74279a52010-09-04 23:28:19 +00001840#ifndef _LIBCPP_HAS_NO_VARIADICS
1841
Howard Hinnantc51e1022010-05-11 19:42:16 +00001842template <class _Tp, class _Allocator>
1843template <class... _Args>
1844void
1845deque<_Tp, _Allocator>::emplace_front(_Args&&... __args)
1846{
1847 allocator_type& __a = __base::__alloc();
1848 if (__front_spare() == 0)
1849 __add_front_capacity();
1850 // __front_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001851 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001852 --__base::__start_;
1853 ++__base::size();
1854}
1855
Howard Hinnant74279a52010-09-04 23:28:19 +00001856#endif // _LIBCPP_HAS_NO_VARIADICS
1857#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001858
1859template <class _Tp, class _Allocator>
1860typename deque<_Tp, _Allocator>::iterator
1861deque<_Tp, _Allocator>::insert(const_iterator __p, const value_type& __v)
1862{
1863 size_type __pos = __p - __base::begin();
1864 size_type __to_end = __base::size() - __pos;
1865 allocator_type& __a = __base::__alloc();
1866 if (__pos < __to_end)
1867 { // insert by shifting things backward
1868 if (__front_spare() == 0)
1869 __add_front_capacity();
1870 // __front_spare() >= 1
1871 if (__pos == 0)
1872 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001873 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001874 --__base::__start_;
1875 ++__base::size();
1876 }
1877 else
1878 {
1879 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1880 iterator __b = __base::begin();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001881 iterator __bm1 = _VSTD::prev(__b);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001882 if (__vt == pointer_traits<const_pointer>::pointer_to(*__b))
1883 __vt = pointer_traits<const_pointer>::pointer_to(*__bm1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001884 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001885 --__base::__start_;
1886 ++__base::size();
1887 if (__pos > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001888 __b = __move_and_check(_VSTD::next(__b), __b + __pos, __b, __vt);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001889 *__b = *__vt;
1890 }
1891 }
1892 else
1893 { // insert by shifting things forward
1894 if (__back_spare() == 0)
1895 __add_back_capacity();
1896 // __back_capacity >= 1
1897 size_type __de = __base::size() - __pos;
1898 if (__de == 0)
1899 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001900 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001901 ++__base::size();
1902 }
1903 else
1904 {
1905 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1906 iterator __e = __base::end();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001907 iterator __em1 = _VSTD::prev(__e);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001908 if (__vt == pointer_traits<const_pointer>::pointer_to(*__em1))
1909 __vt = pointer_traits<const_pointer>::pointer_to(*__e);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001910 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001911 ++__base::size();
1912 if (__de > 1)
1913 __e = __move_backward_and_check(__e - __de, __em1, __e, __vt);
1914 *--__e = *__vt;
1915 }
1916 }
1917 return __base::begin() + __pos;
1918}
1919
Howard Hinnant74279a52010-09-04 23:28:19 +00001920#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001921
1922template <class _Tp, class _Allocator>
1923typename deque<_Tp, _Allocator>::iterator
1924deque<_Tp, _Allocator>::insert(const_iterator __p, value_type&& __v)
1925{
1926 size_type __pos = __p - __base::begin();
1927 size_type __to_end = __base::size() - __pos;
1928 allocator_type& __a = __base::__alloc();
1929 if (__pos < __to_end)
1930 { // insert by shifting things backward
1931 if (__front_spare() == 0)
1932 __add_front_capacity();
1933 // __front_spare() >= 1
1934 if (__pos == 0)
1935 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001936 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001937 --__base::__start_;
1938 ++__base::size();
1939 }
1940 else
1941 {
1942 iterator __b = __base::begin();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001943 iterator __bm1 = _VSTD::prev(__b);
1944 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001945 --__base::__start_;
1946 ++__base::size();
1947 if (__pos > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001948 __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
1949 *__b = _VSTD::move(__v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001950 }
1951 }
1952 else
1953 { // insert by shifting things forward
1954 if (__back_spare() == 0)
1955 __add_back_capacity();
1956 // __back_capacity >= 1
1957 size_type __de = __base::size() - __pos;
1958 if (__de == 0)
1959 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001960 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001961 ++__base::size();
1962 }
1963 else
1964 {
1965 iterator __e = __base::end();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001966 iterator __em1 = _VSTD::prev(__e);
1967 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001968 ++__base::size();
1969 if (__de > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001970 __e = _VSTD::move_backward(__e - __de, __em1, __e);
1971 *--__e = _VSTD::move(__v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001972 }
1973 }
1974 return __base::begin() + __pos;
1975}
1976
Howard Hinnant74279a52010-09-04 23:28:19 +00001977#ifndef _LIBCPP_HAS_NO_VARIADICS
1978
Howard Hinnantc51e1022010-05-11 19:42:16 +00001979template <class _Tp, class _Allocator>
1980template <class... _Args>
1981typename deque<_Tp, _Allocator>::iterator
1982deque<_Tp, _Allocator>::emplace(const_iterator __p, _Args&&... __args)
1983{
1984 size_type __pos = __p - __base::begin();
1985 size_type __to_end = __base::size() - __pos;
1986 allocator_type& __a = __base::__alloc();
1987 if (__pos < __to_end)
1988 { // insert by shifting things backward
1989 if (__front_spare() == 0)
1990 __add_front_capacity();
1991 // __front_spare() >= 1
1992 if (__pos == 0)
1993 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001994 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001995 --__base::__start_;
1996 ++__base::size();
1997 }
1998 else
1999 {
Howard Hinnant800d2d22012-07-08 23:23:04 +00002000 value_type __tmp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002001 iterator __b = __base::begin();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002002 iterator __bm1 = _VSTD::prev(__b);
2003 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002004 --__base::__start_;
2005 ++__base::size();
2006 if (__pos > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002007 __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
Howard Hinnant800d2d22012-07-08 23:23:04 +00002008 *__b = _VSTD::move(__tmp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002009 }
2010 }
2011 else
2012 { // insert by shifting things forward
2013 if (__back_spare() == 0)
2014 __add_back_capacity();
2015 // __back_capacity >= 1
2016 size_type __de = __base::size() - __pos;
2017 if (__de == 0)
2018 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002019 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002020 ++__base::size();
2021 }
2022 else
2023 {
Howard Hinnant800d2d22012-07-08 23:23:04 +00002024 value_type __tmp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002025 iterator __e = __base::end();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002026 iterator __em1 = _VSTD::prev(__e);
2027 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002028 ++__base::size();
2029 if (__de > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002030 __e = _VSTD::move_backward(__e - __de, __em1, __e);
Howard Hinnant800d2d22012-07-08 23:23:04 +00002031 *--__e = _VSTD::move(__tmp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002032 }
2033 }
2034 return __base::begin() + __pos;
2035}
2036
Howard Hinnant74279a52010-09-04 23:28:19 +00002037#endif // _LIBCPP_HAS_NO_VARIADICS
2038#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002039
2040template <class _Tp, class _Allocator>
2041typename deque<_Tp, _Allocator>::iterator
2042deque<_Tp, _Allocator>::insert(const_iterator __p, size_type __n, const value_type& __v)
2043{
2044 size_type __pos = __p - __base::begin();
2045 size_type __to_end = __base::size() - __pos;
2046 allocator_type& __a = __base::__alloc();
2047 if (__pos < __to_end)
2048 { // insert by shifting things backward
2049 if (__n > __front_spare())
2050 __add_front_capacity(__n - __front_spare());
2051 // __n <= __front_spare()
2052 size_type __old_n = __n;
2053 iterator __old_begin = __base::begin();
2054 iterator __i = __old_begin;
2055 if (__n > __pos)
2056 {
2057 for (size_type __m = __n - __pos; __m; --__m, --__base::__start_, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002058 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002059 __n = __pos;
2060 }
2061 if (__n > 0)
2062 {
2063 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2064 iterator __obn = __old_begin + __n;
2065 __move_construct_backward_and_check(__old_begin, __obn, __i, __vt);
2066 if (__n < __pos)
2067 __old_begin = __move_and_check(__obn, __old_begin + __pos, __old_begin, __vt);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002068 _VSTD::fill_n(__old_begin, __n, *__vt);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002069 }
2070 }
2071 else
2072 { // insert by shifting things forward
2073 size_type __back_capacity = __back_spare();
2074 if (__n > __back_capacity)
2075 __add_back_capacity(__n - __back_capacity);
2076 // __n <= __back_capacity
2077 size_type __old_n = __n;
2078 iterator __old_end = __base::end();
2079 iterator __i = __old_end;
2080 size_type __de = __base::size() - __pos;
2081 if (__n > __de)
2082 {
2083 for (size_type __m = __n - __de; __m; --__m, ++__i, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002084 __alloc_traits::construct(__a, _VSTD::addressof(*__i), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002085 __n = __de;
2086 }
2087 if (__n > 0)
2088 {
2089 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2090 iterator __oen = __old_end - __n;
2091 __move_construct_and_check(__oen, __old_end, __i, __vt);
2092 if (__n < __de)
2093 __old_end = __move_backward_and_check(__old_end - __de, __oen, __old_end, __vt);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002094 _VSTD::fill_n(__old_end - __n, __n, *__vt);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002095 }
2096 }
2097 return __base::begin() + __pos;
2098}
2099
2100template <class _Tp, class _Allocator>
2101template <class _InputIter>
2102typename deque<_Tp, _Allocator>::iterator
2103deque<_Tp, _Allocator>::insert(const_iterator __p, _InputIter __f, _InputIter __l,
2104 typename enable_if<__is_input_iterator<_InputIter>::value
Marshall Clow6b612cf2015-01-22 18:33:29 +00002105 &&!__is_forward_iterator<_InputIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002106{
2107 __split_buffer<value_type, allocator_type&> __buf(__base::__alloc());
2108 __buf.__construct_at_end(__f, __l);
2109 typedef typename __split_buffer<value_type, allocator_type&>::iterator __bi;
2110 return insert(__p, move_iterator<__bi>(__buf.begin()), move_iterator<__bi>(__buf.end()));
2111}
2112
2113template <class _Tp, class _Allocator>
Marshall Clow6b612cf2015-01-22 18:33:29 +00002114template <class _ForwardIterator>
2115typename deque<_Tp, _Allocator>::iterator
2116deque<_Tp, _Allocator>::insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l,
2117 typename enable_if<__is_forward_iterator<_ForwardIterator>::value
2118 &&!__is_bidirectional_iterator<_ForwardIterator>::value>::type*)
2119{
2120 size_type __n = _VSTD::distance(__f, __l);
2121 __split_buffer<value_type, allocator_type&> __buf(__n, 0, __base::__alloc());
2122 __buf.__construct_at_end(__f, __l);
2123 typedef typename __split_buffer<value_type, allocator_type&>::iterator __fwd;
2124 return insert(__p, move_iterator<__fwd>(__buf.begin()), move_iterator<__fwd>(__buf.end()));
2125}
2126
2127template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002128template <class _BiIter>
2129typename deque<_Tp, _Allocator>::iterator
2130deque<_Tp, _Allocator>::insert(const_iterator __p, _BiIter __f, _BiIter __l,
2131 typename enable_if<__is_bidirectional_iterator<_BiIter>::value>::type*)
2132{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002133 size_type __n = _VSTD::distance(__f, __l);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002134 size_type __pos = __p - __base::begin();
2135 size_type __to_end = __base::size() - __pos;
2136 allocator_type& __a = __base::__alloc();
2137 if (__pos < __to_end)
2138 { // insert by shifting things backward
2139 if (__n > __front_spare())
2140 __add_front_capacity(__n - __front_spare());
2141 // __n <= __front_spare()
2142 size_type __old_n = __n;
2143 iterator __old_begin = __base::begin();
2144 iterator __i = __old_begin;
2145 _BiIter __m = __f;
2146 if (__n > __pos)
2147 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002148 __m = __pos < __n / 2 ? _VSTD::prev(__l, __pos) : _VSTD::next(__f, __n - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002149 for (_BiIter __j = __m; __j != __f; --__base::__start_, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002150 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), *--__j);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002151 __n = __pos;
2152 }
2153 if (__n > 0)
2154 {
2155 iterator __obn = __old_begin + __n;
2156 for (iterator __j = __obn; __j != __old_begin;)
2157 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002158 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), _VSTD::move(*--__j));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002159 --__base::__start_;
2160 ++__base::size();
2161 }
2162 if (__n < __pos)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002163 __old_begin = _VSTD::move(__obn, __old_begin + __pos, __old_begin);
2164 _VSTD::copy(__m, __l, __old_begin);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002165 }
2166 }
2167 else
2168 { // insert by shifting things forward
2169 size_type __back_capacity = __back_spare();
2170 if (__n > __back_capacity)
2171 __add_back_capacity(__n - __back_capacity);
2172 // __n <= __back_capacity
2173 size_type __old_n = __n;
2174 iterator __old_end = __base::end();
2175 iterator __i = __old_end;
2176 _BiIter __m = __l;
2177 size_type __de = __base::size() - __pos;
2178 if (__n > __de)
2179 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002180 __m = __de < __n / 2 ? _VSTD::next(__f, __de) : _VSTD::prev(__l, __n - __de);
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002181 for (_BiIter __j = __m; __j != __l; ++__i, (void) ++__j, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002182 __alloc_traits::construct(__a, _VSTD::addressof(*__i), *__j);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002183 __n = __de;
2184 }
2185 if (__n > 0)
2186 {
2187 iterator __oen = __old_end - __n;
2188 for (iterator __j = __oen; __j != __old_end; ++__i, ++__j, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002189 __alloc_traits::construct(__a, _VSTD::addressof(*__i), _VSTD::move(*__j));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002190 if (__n < __de)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002191 __old_end = _VSTD::move_backward(__old_end - __de, __oen, __old_end);
2192 _VSTD::copy_backward(__f, __m, __old_end);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002193 }
2194 }
2195 return __base::begin() + __pos;
2196}
2197
2198template <class _Tp, class _Allocator>
2199template <class _InpIter>
2200void
2201deque<_Tp, _Allocator>::__append(_InpIter __f, _InpIter __l,
2202 typename enable_if<__is_input_iterator<_InpIter>::value &&
2203 !__is_forward_iterator<_InpIter>::value>::type*)
2204{
2205 for (; __f != __l; ++__f)
2206 push_back(*__f);
2207}
2208
2209template <class _Tp, class _Allocator>
2210template <class _ForIter>
2211void
2212deque<_Tp, _Allocator>::__append(_ForIter __f, _ForIter __l,
2213 typename enable_if<__is_forward_iterator<_ForIter>::value>::type*)
2214{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002215 size_type __n = _VSTD::distance(__f, __l);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002216 allocator_type& __a = __base::__alloc();
2217 size_type __back_capacity = __back_spare();
2218 if (__n > __back_capacity)
2219 __add_back_capacity(__n - __back_capacity);
2220 // __n <= __back_capacity
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002221 for (iterator __i = __base::end(); __f != __l; ++__i, (void) ++__f, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002222 __alloc_traits::construct(__a, _VSTD::addressof(*__i), *__f);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002223}
2224
2225template <class _Tp, class _Allocator>
2226void
2227deque<_Tp, _Allocator>::__append(size_type __n)
2228{
2229 allocator_type& __a = __base::__alloc();
2230 size_type __back_capacity = __back_spare();
2231 if (__n > __back_capacity)
2232 __add_back_capacity(__n - __back_capacity);
2233 // __n <= __back_capacity
2234 for (iterator __i = __base::end(); __n; --__n, ++__i, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002235 __alloc_traits::construct(__a, _VSTD::addressof(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002236}
2237
2238template <class _Tp, class _Allocator>
2239void
2240deque<_Tp, _Allocator>::__append(size_type __n, const value_type& __v)
2241{
2242 allocator_type& __a = __base::__alloc();
2243 size_type __back_capacity = __back_spare();
2244 if (__n > __back_capacity)
2245 __add_back_capacity(__n - __back_capacity);
2246 // __n <= __back_capacity
2247 for (iterator __i = __base::end(); __n; --__n, ++__i, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002248 __alloc_traits::construct(__a, _VSTD::addressof(*__i), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002249}
2250
2251// Create front capacity for one block of elements.
2252// Strong guarantee. Either do it or don't touch anything.
2253template <class _Tp, class _Allocator>
2254void
2255deque<_Tp, _Allocator>::__add_front_capacity()
2256{
2257 allocator_type& __a = __base::__alloc();
2258 if (__back_spare() >= __base::__block_size)
2259 {
2260 __base::__start_ += __base::__block_size;
2261 pointer __pt = __base::__map_.back();
2262 __base::__map_.pop_back();
2263 __base::__map_.push_front(__pt);
2264 }
2265 // Else if __base::__map_.size() < __base::__map_.capacity() then we need to allocate 1 buffer
2266 else if (__base::__map_.size() < __base::__map_.capacity())
2267 { // we can put the new buffer into the map, but don't shift things around
2268 // until all buffers are allocated. If we throw, we don't need to fix
2269 // anything up (any added buffers are undetectible)
2270 if (__base::__map_.__front_spare() > 0)
2271 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2272 else
2273 {
2274 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2275 // Done allocating, reorder capacity
2276 pointer __pt = __base::__map_.back();
2277 __base::__map_.pop_back();
2278 __base::__map_.push_front(__pt);
2279 }
2280 __base::__start_ = __base::__map_.size() == 1 ?
2281 __base::__block_size / 2 :
2282 __base::__start_ + __base::__block_size;
2283 }
2284 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2285 else
2286 {
2287 __split_buffer<pointer, typename __base::__pointer_allocator&>
2288 __buf(max<size_type>(2 * __base::__map_.capacity(), 1),
2289 0, __base::__map_.__alloc());
2290#ifndef _LIBCPP_NO_EXCEPTIONS
2291 try
2292 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002293#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002294 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2295#ifndef _LIBCPP_NO_EXCEPTIONS
2296 }
2297 catch (...)
2298 {
2299 __alloc_traits::deallocate(__a, __buf.front(), __base::__block_size);
2300 throw;
2301 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002302#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002303 for (typename __base::__map_pointer __i = __base::__map_.begin();
2304 __i != __base::__map_.end(); ++__i)
2305 __buf.push_back(*__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002306 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2307 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2308 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2309 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002310 __base::__start_ = __base::__map_.size() == 1 ?
2311 __base::__block_size / 2 :
2312 __base::__start_ + __base::__block_size;
2313 }
2314}
2315
2316// Create front capacity for __n elements.
2317// Strong guarantee. Either do it or don't touch anything.
2318template <class _Tp, class _Allocator>
2319void
2320deque<_Tp, _Allocator>::__add_front_capacity(size_type __n)
2321{
2322 allocator_type& __a = __base::__alloc();
2323 size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2324 // Number of unused blocks at back:
2325 size_type __back_capacity = __back_spare() / __base::__block_size;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002326 __back_capacity = _VSTD::min(__back_capacity, __nb); // don't take more than you need
Howard Hinnantc51e1022010-05-11 19:42:16 +00002327 __nb -= __back_capacity; // number of blocks need to allocate
2328 // If __nb == 0, then we have sufficient capacity.
2329 if (__nb == 0)
2330 {
2331 __base::__start_ += __base::__block_size * __back_capacity;
2332 for (; __back_capacity > 0; --__back_capacity)
2333 {
2334 pointer __pt = __base::__map_.back();
2335 __base::__map_.pop_back();
2336 __base::__map_.push_front(__pt);
2337 }
2338 }
2339 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2340 else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2341 { // we can put the new buffers into the map, but don't shift things around
2342 // until all buffers are allocated. If we throw, we don't need to fix
2343 // anything up (any added buffers are undetectible)
2344 for (; __nb > 0; --__nb, __base::__start_ += __base::__block_size - (__base::__map_.size() == 1))
2345 {
2346 if (__base::__map_.__front_spare() == 0)
2347 break;
2348 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2349 }
2350 for (; __nb > 0; --__nb, ++__back_capacity)
2351 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2352 // Done allocating, reorder capacity
2353 __base::__start_ += __back_capacity * __base::__block_size;
2354 for (; __back_capacity > 0; --__back_capacity)
2355 {
2356 pointer __pt = __base::__map_.back();
2357 __base::__map_.pop_back();
2358 __base::__map_.push_front(__pt);
2359 }
2360 }
2361 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2362 else
2363 {
2364 size_type __ds = (__nb + __back_capacity) * __base::__block_size - __base::__map_.empty();
2365 __split_buffer<pointer, typename __base::__pointer_allocator&>
2366 __buf(max<size_type>(2* __base::__map_.capacity(),
2367 __nb + __base::__map_.size()),
2368 0, __base::__map_.__alloc());
2369#ifndef _LIBCPP_NO_EXCEPTIONS
2370 try
2371 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002372#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002373 for (; __nb > 0; --__nb)
2374 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2375#ifndef _LIBCPP_NO_EXCEPTIONS
2376 }
2377 catch (...)
2378 {
2379 for (typename __base::__map_pointer __i = __buf.begin();
2380 __i != __buf.end(); ++__i)
2381 __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2382 throw;
2383 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002384#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002385 for (; __back_capacity > 0; --__back_capacity)
2386 {
2387 __buf.push_back(__base::__map_.back());
2388 __base::__map_.pop_back();
2389 }
2390 for (typename __base::__map_pointer __i = __base::__map_.begin();
2391 __i != __base::__map_.end(); ++__i)
2392 __buf.push_back(*__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002393 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2394 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2395 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2396 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002397 __base::__start_ += __ds;
2398 }
2399}
2400
2401// Create back capacity for one block of elements.
2402// Strong guarantee. Either do it or don't touch anything.
2403template <class _Tp, class _Allocator>
2404void
2405deque<_Tp, _Allocator>::__add_back_capacity()
2406{
2407 allocator_type& __a = __base::__alloc();
2408 if (__front_spare() >= __base::__block_size)
2409 {
2410 __base::__start_ -= __base::__block_size;
2411 pointer __pt = __base::__map_.front();
2412 __base::__map_.pop_front();
2413 __base::__map_.push_back(__pt);
2414 }
2415 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2416 else if (__base::__map_.size() < __base::__map_.capacity())
2417 { // we can put the new buffer into the map, but don't shift things around
2418 // until it is allocated. If we throw, we don't need to fix
2419 // anything up (any added buffers are undetectible)
2420 if (__base::__map_.__back_spare() != 0)
2421 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2422 else
2423 {
2424 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2425 // Done allocating, reorder capacity
2426 pointer __pt = __base::__map_.front();
2427 __base::__map_.pop_front();
2428 __base::__map_.push_back(__pt);
2429 }
2430 }
2431 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2432 else
2433 {
2434 __split_buffer<pointer, typename __base::__pointer_allocator&>
2435 __buf(max<size_type>(2* __base::__map_.capacity(), 1),
2436 __base::__map_.size(),
2437 __base::__map_.__alloc());
2438#ifndef _LIBCPP_NO_EXCEPTIONS
2439 try
2440 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002441#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002442 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2443#ifndef _LIBCPP_NO_EXCEPTIONS
2444 }
2445 catch (...)
2446 {
2447 __alloc_traits::deallocate(__a, __buf.back(), __base::__block_size);
2448 throw;
2449 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002450#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002451 for (typename __base::__map_pointer __i = __base::__map_.end();
2452 __i != __base::__map_.begin();)
2453 __buf.push_front(*--__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002454 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2455 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2456 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2457 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002458 }
2459}
2460
2461// Create back capacity for __n elements.
2462// Strong guarantee. Either do it or don't touch anything.
2463template <class _Tp, class _Allocator>
2464void
2465deque<_Tp, _Allocator>::__add_back_capacity(size_type __n)
2466{
2467 allocator_type& __a = __base::__alloc();
2468 size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2469 // Number of unused blocks at front:
2470 size_type __front_capacity = __front_spare() / __base::__block_size;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002471 __front_capacity = _VSTD::min(__front_capacity, __nb); // don't take more than you need
Howard Hinnantc51e1022010-05-11 19:42:16 +00002472 __nb -= __front_capacity; // number of blocks need to allocate
2473 // If __nb == 0, then we have sufficient capacity.
2474 if (__nb == 0)
2475 {
2476 __base::__start_ -= __base::__block_size * __front_capacity;
2477 for (; __front_capacity > 0; --__front_capacity)
2478 {
2479 pointer __pt = __base::__map_.front();
2480 __base::__map_.pop_front();
2481 __base::__map_.push_back(__pt);
2482 }
2483 }
2484 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2485 else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2486 { // we can put the new buffers into the map, but don't shift things around
2487 // until all buffers are allocated. If we throw, we don't need to fix
2488 // anything up (any added buffers are undetectible)
2489 for (; __nb > 0; --__nb)
2490 {
2491 if (__base::__map_.__back_spare() == 0)
2492 break;
2493 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2494 }
2495 for (; __nb > 0; --__nb, ++__front_capacity, __base::__start_ +=
2496 __base::__block_size - (__base::__map_.size() == 1))
2497 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2498 // Done allocating, reorder capacity
2499 __base::__start_ -= __base::__block_size * __front_capacity;
2500 for (; __front_capacity > 0; --__front_capacity)
2501 {
2502 pointer __pt = __base::__map_.front();
2503 __base::__map_.pop_front();
2504 __base::__map_.push_back(__pt);
2505 }
2506 }
2507 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2508 else
2509 {
2510 size_type __ds = __front_capacity * __base::__block_size;
2511 __split_buffer<pointer, typename __base::__pointer_allocator&>
2512 __buf(max<size_type>(2* __base::__map_.capacity(),
2513 __nb + __base::__map_.size()),
2514 __base::__map_.size() - __front_capacity,
2515 __base::__map_.__alloc());
2516#ifndef _LIBCPP_NO_EXCEPTIONS
2517 try
2518 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002519#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002520 for (; __nb > 0; --__nb)
2521 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2522#ifndef _LIBCPP_NO_EXCEPTIONS
2523 }
2524 catch (...)
2525 {
2526 for (typename __base::__map_pointer __i = __buf.begin();
2527 __i != __buf.end(); ++__i)
2528 __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2529 throw;
2530 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002531#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002532 for (; __front_capacity > 0; --__front_capacity)
2533 {
2534 __buf.push_back(__base::__map_.front());
2535 __base::__map_.pop_front();
2536 }
2537 for (typename __base::__map_pointer __i = __base::__map_.end();
2538 __i != __base::__map_.begin();)
2539 __buf.push_front(*--__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002540 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2541 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2542 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2543 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002544 __base::__start_ -= __ds;
2545 }
2546}
2547
2548template <class _Tp, class _Allocator>
2549void
2550deque<_Tp, _Allocator>::pop_front()
2551{
2552 allocator_type& __a = __base::__alloc();
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002553 __alloc_traits::destroy(__a, __to_raw_pointer(*(__base::__map_.begin() +
2554 __base::__start_ / __base::__block_size) +
2555 __base::__start_ % __base::__block_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002556 --__base::size();
2557 if (++__base::__start_ >= 2 * __base::__block_size)
2558 {
2559 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2560 __base::__map_.pop_front();
2561 __base::__start_ -= __base::__block_size;
2562 }
2563}
2564
2565template <class _Tp, class _Allocator>
2566void
2567deque<_Tp, _Allocator>::pop_back()
2568{
2569 allocator_type& __a = __base::__alloc();
2570 size_type __p = __base::size() + __base::__start_ - 1;
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002571 __alloc_traits::destroy(__a, __to_raw_pointer(*(__base::__map_.begin() +
2572 __p / __base::__block_size) +
2573 __p % __base::__block_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002574 --__base::size();
2575 if (__back_spare() >= 2 * __base::__block_size)
2576 {
2577 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2578 __base::__map_.pop_back();
2579 }
2580}
2581
2582// move assign [__f, __l) to [__r, __r + (__l-__f)).
2583// If __vt points into [__f, __l), then subtract (__f - __r) from __vt.
2584template <class _Tp, class _Allocator>
2585typename deque<_Tp, _Allocator>::iterator
2586deque<_Tp, _Allocator>::__move_and_check(iterator __f, iterator __l, iterator __r,
2587 const_pointer& __vt)
2588{
2589 // as if
2590 // for (; __f != __l; ++__f, ++__r)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002591 // *__r = _VSTD::move(*__f);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002592 difference_type __n = __l - __f;
2593 while (__n > 0)
2594 {
2595 pointer __fb = __f.__ptr_;
2596 pointer __fe = *__f.__m_iter_ + __base::__block_size;
2597 difference_type __bs = __fe - __fb;
2598 if (__bs > __n)
2599 {
2600 __bs = __n;
2601 __fe = __fb + __bs;
2602 }
2603 if (__fb <= __vt && __vt < __fe)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002604 __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) -= __f - __r).__ptr_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002605 __r = _VSTD::move(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002606 __n -= __bs;
2607 __f += __bs;
2608 }
2609 return __r;
2610}
2611
2612// move assign [__f, __l) to [__r - (__l-__f), __r) backwards.
2613// If __vt points into [__f, __l), then add (__r - __l) to __vt.
2614template <class _Tp, class _Allocator>
2615typename deque<_Tp, _Allocator>::iterator
2616deque<_Tp, _Allocator>::__move_backward_and_check(iterator __f, iterator __l, iterator __r,
2617 const_pointer& __vt)
2618{
2619 // as if
2620 // while (__f != __l)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002621 // *--__r = _VSTD::move(*--__l);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002622 difference_type __n = __l - __f;
2623 while (__n > 0)
2624 {
2625 --__l;
2626 pointer __lb = *__l.__m_iter_;
2627 pointer __le = __l.__ptr_ + 1;
2628 difference_type __bs = __le - __lb;
2629 if (__bs > __n)
2630 {
2631 __bs = __n;
2632 __lb = __le - __bs;
2633 }
2634 if (__lb <= __vt && __vt < __le)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002635 __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) += __r - __l - 1).__ptr_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002636 __r = _VSTD::move_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002637 __n -= __bs;
2638 __l -= __bs - 1;
2639 }
2640 return __r;
2641}
2642
2643// move construct [__f, __l) to [__r, __r + (__l-__f)).
2644// If __vt points into [__f, __l), then add (__r - __f) to __vt.
2645template <class _Tp, class _Allocator>
2646void
2647deque<_Tp, _Allocator>::__move_construct_and_check(iterator __f, iterator __l,
2648 iterator __r, const_pointer& __vt)
2649{
2650 allocator_type& __a = __base::__alloc();
2651 // as if
2652 // for (; __f != __l; ++__r, ++__f, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002653 // __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__f));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002654 difference_type __n = __l - __f;
2655 while (__n > 0)
2656 {
2657 pointer __fb = __f.__ptr_;
2658 pointer __fe = *__f.__m_iter_ + __base::__block_size;
2659 difference_type __bs = __fe - __fb;
2660 if (__bs > __n)
2661 {
2662 __bs = __n;
2663 __fe = __fb + __bs;
2664 }
2665 if (__fb <= __vt && __vt < __fe)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002666 __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) += __r - __f).__ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002667 for (; __fb != __fe; ++__fb, ++__r, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002668 __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__fb));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002669 __n -= __bs;
2670 __f += __bs;
2671 }
2672}
2673
2674// move construct [__f, __l) to [__r - (__l-__f), __r) backwards.
2675// If __vt points into [__f, __l), then subtract (__l - __r) from __vt.
2676template <class _Tp, class _Allocator>
2677void
2678deque<_Tp, _Allocator>::__move_construct_backward_and_check(iterator __f, iterator __l,
2679 iterator __r, const_pointer& __vt)
2680{
2681 allocator_type& __a = __base::__alloc();
2682 // as if
2683 // for (iterator __j = __l; __j != __f;)
2684 // {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002685 // __alloc_traitsconstruct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__j));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002686 // --__base::__start_;
2687 // ++__base::size();
2688 // }
2689 difference_type __n = __l - __f;
2690 while (__n > 0)
2691 {
2692 --__l;
2693 pointer __lb = *__l.__m_iter_;
2694 pointer __le = __l.__ptr_ + 1;
2695 difference_type __bs = __le - __lb;
2696 if (__bs > __n)
2697 {
2698 __bs = __n;
2699 __lb = __le - __bs;
2700 }
2701 if (__lb <= __vt && __vt < __le)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002702 __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) -= __l - __r + 1).__ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002703 while (__le != __lb)
2704 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002705 __alloc_traits::construct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__le));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002706 --__base::__start_;
2707 ++__base::size();
2708 }
2709 __n -= __bs;
2710 __l -= __bs - 1;
2711 }
2712}
2713
2714template <class _Tp, class _Allocator>
2715typename deque<_Tp, _Allocator>::iterator
2716deque<_Tp, _Allocator>::erase(const_iterator __f)
2717{
2718 difference_type __n = 1;
2719 iterator __b = __base::begin();
2720 difference_type __pos = __f - __b;
2721 iterator __p = __b + __pos;
2722 allocator_type& __a = __base::__alloc();
2723 if (__pos < (__base::size() - 1) / 2)
2724 { // erase from front
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002725 _VSTD::move_backward(__b, __p, _VSTD::next(__p));
2726 __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002727 --__base::size();
2728 ++__base::__start_;
2729 if (__front_spare() >= 2 * __base::__block_size)
2730 {
2731 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2732 __base::__map_.pop_front();
2733 __base::__start_ -= __base::__block_size;
2734 }
2735 }
2736 else
2737 { // erase from back
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002738 iterator __i = _VSTD::move(_VSTD::next(__p), __base::end(), __p);
2739 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002740 --__base::size();
2741 if (__back_spare() >= 2 * __base::__block_size)
2742 {
2743 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2744 __base::__map_.pop_back();
2745 }
2746 }
2747 return __base::begin() + __pos;
2748}
2749
2750template <class _Tp, class _Allocator>
2751typename deque<_Tp, _Allocator>::iterator
2752deque<_Tp, _Allocator>::erase(const_iterator __f, const_iterator __l)
2753{
2754 difference_type __n = __l - __f;
2755 iterator __b = __base::begin();
2756 difference_type __pos = __f - __b;
2757 iterator __p = __b + __pos;
2758 if (__n > 0)
2759 {
2760 allocator_type& __a = __base::__alloc();
2761 if (__pos < (__base::size() - __n) / 2)
2762 { // erase from front
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002763 iterator __i = _VSTD::move_backward(__b, __p, __p + __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002764 for (; __b != __i; ++__b)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002765 __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002766 __base::size() -= __n;
2767 __base::__start_ += __n;
2768 while (__front_spare() >= 2 * __base::__block_size)
2769 {
2770 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2771 __base::__map_.pop_front();
2772 __base::__start_ -= __base::__block_size;
2773 }
2774 }
2775 else
2776 { // erase from back
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002777 iterator __i = _VSTD::move(__p + __n, __base::end(), __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002778 for (iterator __e = __base::end(); __i != __e; ++__i)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002779 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002780 __base::size() -= __n;
2781 while (__back_spare() >= 2 * __base::__block_size)
2782 {
2783 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2784 __base::__map_.pop_back();
2785 }
2786 }
2787 }
2788 return __base::begin() + __pos;
2789}
2790
2791template <class _Tp, class _Allocator>
2792void
2793deque<_Tp, _Allocator>::__erase_to_end(const_iterator __f)
2794{
2795 iterator __e = __base::end();
2796 difference_type __n = __e - __f;
2797 if (__n > 0)
2798 {
2799 allocator_type& __a = __base::__alloc();
2800 iterator __b = __base::begin();
2801 difference_type __pos = __f - __b;
2802 for (iterator __p = __b + __pos; __p != __e; ++__p)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002803 __alloc_traits::destroy(__a, _VSTD::addressof(*__p));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002804 __base::size() -= __n;
2805 while (__back_spare() >= 2 * __base::__block_size)
2806 {
2807 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2808 __base::__map_.pop_back();
2809 }
2810 }
2811}
2812
2813template <class _Tp, class _Allocator>
Howard Hinnant874ad9a2010-09-21 21:28:23 +00002814inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002815void
2816deque<_Tp, _Allocator>::swap(deque& __c)
Howard Hinnantca2fc222011-06-02 20:00:14 +00002817 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2818 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002819{
2820 __base::swap(__c);
2821}
2822
2823template <class _Tp, class _Allocator>
Howard Hinnant874ad9a2010-09-21 21:28:23 +00002824inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002825void
Howard Hinnantda2df912011-06-02 16:10:22 +00002826deque<_Tp, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002827{
2828 __base::clear();
2829}
2830
2831template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002832inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002833bool
2834operator==(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2835{
2836 const typename deque<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002837 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002838}
2839
2840template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002841inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002842bool
2843operator!=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2844{
2845 return !(__x == __y);
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{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002853 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002854}
2855
2856template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002857inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002858bool
2859operator> (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2860{
2861 return __y < __x;
2862}
2863
2864template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002865inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002866bool
2867operator>=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2868{
2869 return !(__x < __y);
2870}
2871
2872template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002873inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002874bool
2875operator<=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2876{
2877 return !(__y < __x);
2878}
2879
2880template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002881inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002882void
2883swap(deque<_Tp, _Allocator>& __x, deque<_Tp, _Allocator>& __y)
Howard Hinnantca2fc222011-06-02 20:00:14 +00002884 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002885{
2886 __x.swap(__y);
2887}
2888
2889_LIBCPP_END_NAMESPACE_STD
2890
2891#endif // _LIBCPP_DEQUE