blob: 8d0d2a8f4990a41d89313f9454208a06f9913a36 [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;
Marshall Clow65cd4c62015-02-18 17:24:08 +0000171template <class _Tp, class _Allocator = allocator<_Tp> > class _LIBCPP_TYPE_VIS_ONLY deque;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000172
173template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
174 class _DiffType, _DiffType _BlockSize>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000175class _LIBCPP_TYPE_VIS_ONLY __deque_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000176
177template <class _RAIter,
178 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
179__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
180copy(_RAIter __f,
181 _RAIter __l,
182 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
183 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
184
185template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
186 class _OutputIterator>
187_OutputIterator
188copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
189 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
190 _OutputIterator __r);
191
192template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
193 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
194__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
195copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
196 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
197 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
198
199template <class _RAIter,
200 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
201__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
202copy_backward(_RAIter __f,
203 _RAIter __l,
204 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
205 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
206
207template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
208 class _OutputIterator>
209_OutputIterator
210copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
211 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
212 _OutputIterator __r);
213
214template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
215 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
216__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
217copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
218 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
219 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
220
221template <class _RAIter,
222 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
223__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
224move(_RAIter __f,
225 _RAIter __l,
226 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
227 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
228
229template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
230 class _OutputIterator>
231_OutputIterator
232move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
233 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
234 _OutputIterator __r);
235
236template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
237 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
238__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
239move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
240 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
241 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
242
243template <class _RAIter,
244 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
245__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
246move_backward(_RAIter __f,
247 _RAIter __l,
248 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
249 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
250
251template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
252 class _OutputIterator>
253_OutputIterator
254move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
255 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
256 _OutputIterator __r);
257
258template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
259 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
260__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
261move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
262 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
263 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
264
265template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
266 class _DiffType, _DiffType _BlockSize>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000267class _LIBCPP_TYPE_VIS_ONLY __deque_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000268{
269 typedef _MapPointer __map_iterator;
270public:
271 typedef _Pointer pointer;
272 typedef _DiffType difference_type;
273private:
274 __map_iterator __m_iter_;
275 pointer __ptr_;
276
277 static const difference_type __block_size = _BlockSize;
278public:
279 typedef _ValueType value_type;
280 typedef random_access_iterator_tag iterator_category;
281 typedef _Reference reference;
282
Marshall Clow7a3a61d2013-08-06 16:14:36 +0000283 _LIBCPP_INLINE_VISIBILITY __deque_iterator() _NOEXCEPT
284#if _LIBCPP_STD_VER > 11
285 : __m_iter_(nullptr), __ptr_(nullptr)
286#endif
287 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000288
Howard Hinnantc834c512011-11-29 18:15:50 +0000289 template <class _Pp, class _Rp, class _MP>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000290 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +0000291 __deque_iterator(const __deque_iterator<value_type, _Pp, _Rp, _MP, difference_type, __block_size>& __it,
292 typename enable_if<is_convertible<_Pp, pointer>::value>::type* = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000293 : __m_iter_(__it.__m_iter_), __ptr_(__it.__ptr_) {}
294
295 _LIBCPP_INLINE_VISIBILITY reference operator*() const {return *__ptr_;}
296 _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return __ptr_;}
297
298 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator++()
299 {
300 if (++__ptr_ - *__m_iter_ == __block_size)
301 {
302 ++__m_iter_;
303 __ptr_ = *__m_iter_;
304 }
305 return *this;
306 }
307
308 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator++(int)
309 {
310 __deque_iterator __tmp = *this;
311 ++(*this);
312 return __tmp;
313 }
314
315 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator--()
316 {
317 if (__ptr_ == *__m_iter_)
318 {
319 --__m_iter_;
320 __ptr_ = *__m_iter_ + __block_size;
321 }
322 --__ptr_;
323 return *this;
324 }
325
326 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator--(int)
327 {
328 __deque_iterator __tmp = *this;
329 --(*this);
330 return __tmp;
331 }
332
333 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator+=(difference_type __n)
334 {
335 if (__n != 0)
336 {
337 __n += __ptr_ - *__m_iter_;
338 if (__n > 0)
339 {
340 __m_iter_ += __n / __block_size;
341 __ptr_ = *__m_iter_ + __n % __block_size;
342 }
343 else // (__n < 0)
344 {
345 difference_type __z = __block_size - 1 - __n;
346 __m_iter_ -= __z / __block_size;
347 __ptr_ = *__m_iter_ + (__block_size - 1 - __z % __block_size);
348 }
349 }
350 return *this;
351 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000352
Howard Hinnantc51e1022010-05-11 19:42:16 +0000353 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator-=(difference_type __n)
354 {
355 return *this += -__n;
356 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000357
Howard Hinnantc51e1022010-05-11 19:42:16 +0000358 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator+(difference_type __n) const
359 {
360 __deque_iterator __t(*this);
361 __t += __n;
362 return __t;
363 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000364
Howard Hinnantc51e1022010-05-11 19:42:16 +0000365 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator-(difference_type __n) const
366 {
367 __deque_iterator __t(*this);
368 __t -= __n;
369 return __t;
370 }
371
372 _LIBCPP_INLINE_VISIBILITY
373 friend __deque_iterator operator+(difference_type __n, const __deque_iterator& __it)
374 {return __it + __n;}
375
376 _LIBCPP_INLINE_VISIBILITY
377 friend difference_type operator-(const __deque_iterator& __x, const __deque_iterator& __y)
378 {
379 if (__x != __y)
380 return (__x.__m_iter_ - __y.__m_iter_) * __block_size
381 + (__x.__ptr_ - *__x.__m_iter_)
382 - (__y.__ptr_ - *__y.__m_iter_);
383 return 0;
384 }
385
386 _LIBCPP_INLINE_VISIBILITY reference operator[](difference_type __n) const
387 {return *(*this + __n);}
388
389 _LIBCPP_INLINE_VISIBILITY friend
390 bool operator==(const __deque_iterator& __x, const __deque_iterator& __y)
391 {return __x.__ptr_ == __y.__ptr_;}
392
393 _LIBCPP_INLINE_VISIBILITY friend
394 bool operator!=(const __deque_iterator& __x, const __deque_iterator& __y)
395 {return !(__x == __y);}
396
397 _LIBCPP_INLINE_VISIBILITY friend
398 bool operator<(const __deque_iterator& __x, const __deque_iterator& __y)
399 {return __x.__m_iter_ < __y.__m_iter_ ||
400 (__x.__m_iter_ == __y.__m_iter_ && __x.__ptr_ < __y.__ptr_);}
401
402 _LIBCPP_INLINE_VISIBILITY friend
403 bool operator>(const __deque_iterator& __x, const __deque_iterator& __y)
404 {return __y < __x;}
405
406 _LIBCPP_INLINE_VISIBILITY friend
407 bool operator<=(const __deque_iterator& __x, const __deque_iterator& __y)
408 {return !(__y < __x);}
409
410 _LIBCPP_INLINE_VISIBILITY friend
411 bool operator>=(const __deque_iterator& __x, const __deque_iterator& __y)
412 {return !(__x < __y);}
413
414private:
Howard Hinnantda2df912011-06-02 16:10:22 +0000415 _LIBCPP_INLINE_VISIBILITY __deque_iterator(__map_iterator __m, pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000416 : __m_iter_(__m), __ptr_(__p) {}
417
Howard Hinnantc834c512011-11-29 18:15:50 +0000418 template <class _Tp, class _Ap> friend class __deque_base;
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000419 template <class _Tp, class _Ap> friend class _LIBCPP_TYPE_VIS_ONLY deque;
Howard Hinnantc834c512011-11-29 18:15:50 +0000420 template <class _Vp, class _Pp, class _Rp, class _MP, class _Dp, _Dp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000421 friend class _LIBCPP_TYPE_VIS_ONLY __deque_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000422
423 template <class _RAIter,
424 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
425 friend
426 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
427 copy(_RAIter __f,
428 _RAIter __l,
429 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
430 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
431
432 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
433 class _OutputIterator>
434 friend
435 _OutputIterator
436 copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
437 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
438 _OutputIterator __r);
439
440 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
441 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
442 friend
443 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
444 copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
445 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
446 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
447
448 template <class _RAIter,
449 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
450 friend
451 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
452 copy_backward(_RAIter __f,
453 _RAIter __l,
454 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
455 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
456
457 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
458 class _OutputIterator>
459 friend
460 _OutputIterator
461 copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
462 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
463 _OutputIterator __r);
464
465 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
466 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
467 friend
468 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
469 copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
470 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
471 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
472
473 template <class _RAIter,
474 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
475 friend
476 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
477 move(_RAIter __f,
478 _RAIter __l,
479 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
480 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
481
482 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
483 class _OutputIterator>
484 friend
485 _OutputIterator
486 move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
487 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
488 _OutputIterator __r);
489
490 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
491 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
492 friend
493 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
494 move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
495 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
496 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
497
498 template <class _RAIter,
499 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
500 friend
501 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
502 move_backward(_RAIter __f,
503 _RAIter __l,
504 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
505 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
506
507 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
508 class _OutputIterator>
509 friend
510 _OutputIterator
511 move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
512 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
513 _OutputIterator __r);
514
515 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
516 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
517 friend
518 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
519 move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
520 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
521 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
522};
523
524// copy
525
526template <class _RAIter,
527 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
528__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
529copy(_RAIter __f,
530 _RAIter __l,
531 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
532 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
533{
534 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
535 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
536 while (__f != __l)
537 {
538 pointer __rb = __r.__ptr_;
539 pointer __re = *__r.__m_iter_ + _B2;
540 difference_type __bs = __re - __rb;
541 difference_type __n = __l - __f;
542 _RAIter __m = __l;
543 if (__n > __bs)
544 {
545 __n = __bs;
546 __m = __f + __n;
547 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000548 _VSTD::copy(__f, __m, __rb);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000549 __f = __m;
550 __r += __n;
551 }
552 return __r;
553}
554
555template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
556 class _OutputIterator>
557_OutputIterator
558copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
559 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
560 _OutputIterator __r)
561{
562 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
563 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
564 difference_type __n = __l - __f;
565 while (__n > 0)
566 {
567 pointer __fb = __f.__ptr_;
568 pointer __fe = *__f.__m_iter_ + _B1;
569 difference_type __bs = __fe - __fb;
570 if (__bs > __n)
571 {
572 __bs = __n;
573 __fe = __fb + __bs;
574 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000575 __r = _VSTD::copy(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000576 __n -= __bs;
577 __f += __bs;
578 }
579 return __r;
580}
581
582template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
583 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
584__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
585copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
586 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
587 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
588{
589 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
590 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
591 difference_type __n = __l - __f;
592 while (__n > 0)
593 {
594 pointer __fb = __f.__ptr_;
595 pointer __fe = *__f.__m_iter_ + _B1;
596 difference_type __bs = __fe - __fb;
597 if (__bs > __n)
598 {
599 __bs = __n;
600 __fe = __fb + __bs;
601 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000602 __r = _VSTD::copy(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000603 __n -= __bs;
604 __f += __bs;
605 }
606 return __r;
607}
608
609// copy_backward
610
611template <class _RAIter,
612 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
613__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
614copy_backward(_RAIter __f,
615 _RAIter __l,
616 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
617 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
618{
619 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
620 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
621 while (__f != __l)
622 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000623 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _VSTD::prev(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000624 pointer __rb = *__rp.__m_iter_;
625 pointer __re = __rp.__ptr_ + 1;
626 difference_type __bs = __re - __rb;
627 difference_type __n = __l - __f;
628 _RAIter __m = __f;
629 if (__n > __bs)
630 {
631 __n = __bs;
632 __m = __l - __n;
633 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000634 _VSTD::copy_backward(__m, __l, __re);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000635 __l = __m;
636 __r -= __n;
637 }
638 return __r;
639}
640
641template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
642 class _OutputIterator>
643_OutputIterator
644copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
645 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
646 _OutputIterator __r)
647{
648 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
649 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
650 difference_type __n = __l - __f;
651 while (__n > 0)
652 {
653 --__l;
654 pointer __lb = *__l.__m_iter_;
655 pointer __le = __l.__ptr_ + 1;
656 difference_type __bs = __le - __lb;
657 if (__bs > __n)
658 {
659 __bs = __n;
660 __lb = __le - __bs;
661 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000662 __r = _VSTD::copy_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000663 __n -= __bs;
664 __l -= __bs - 1;
665 }
666 return __r;
667}
668
669template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
670 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
671__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
672copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
673 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
674 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
675{
676 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
677 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
678 difference_type __n = __l - __f;
679 while (__n > 0)
680 {
681 --__l;
682 pointer __lb = *__l.__m_iter_;
683 pointer __le = __l.__ptr_ + 1;
684 difference_type __bs = __le - __lb;
685 if (__bs > __n)
686 {
687 __bs = __n;
688 __lb = __le - __bs;
689 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000690 __r = _VSTD::copy_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000691 __n -= __bs;
692 __l -= __bs - 1;
693 }
694 return __r;
695}
696
697// move
698
699template <class _RAIter,
700 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
701__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
702move(_RAIter __f,
703 _RAIter __l,
704 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
705 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
706{
707 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
708 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
709 while (__f != __l)
710 {
711 pointer __rb = __r.__ptr_;
712 pointer __re = *__r.__m_iter_ + _B2;
713 difference_type __bs = __re - __rb;
714 difference_type __n = __l - __f;
715 _RAIter __m = __l;
716 if (__n > __bs)
717 {
718 __n = __bs;
719 __m = __f + __n;
720 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000721 _VSTD::move(__f, __m, __rb);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000722 __f = __m;
723 __r += __n;
724 }
725 return __r;
726}
727
728template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
729 class _OutputIterator>
730_OutputIterator
731move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
732 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
733 _OutputIterator __r)
734{
735 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
736 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
737 difference_type __n = __l - __f;
738 while (__n > 0)
739 {
740 pointer __fb = __f.__ptr_;
741 pointer __fe = *__f.__m_iter_ + _B1;
742 difference_type __bs = __fe - __fb;
743 if (__bs > __n)
744 {
745 __bs = __n;
746 __fe = __fb + __bs;
747 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000748 __r = _VSTD::move(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000749 __n -= __bs;
750 __f += __bs;
751 }
752 return __r;
753}
754
755template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
756 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
757__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
758move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
759 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
760 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
761{
762 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
763 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
764 difference_type __n = __l - __f;
765 while (__n > 0)
766 {
767 pointer __fb = __f.__ptr_;
768 pointer __fe = *__f.__m_iter_ + _B1;
769 difference_type __bs = __fe - __fb;
770 if (__bs > __n)
771 {
772 __bs = __n;
773 __fe = __fb + __bs;
774 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000775 __r = _VSTD::move(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000776 __n -= __bs;
777 __f += __bs;
778 }
779 return __r;
780}
781
782// move_backward
783
784template <class _RAIter,
785 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
786__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
787move_backward(_RAIter __f,
788 _RAIter __l,
789 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
790 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
791{
792 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
793 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
794 while (__f != __l)
795 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000796 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _VSTD::prev(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000797 pointer __rb = *__rp.__m_iter_;
798 pointer __re = __rp.__ptr_ + 1;
799 difference_type __bs = __re - __rb;
800 difference_type __n = __l - __f;
801 _RAIter __m = __f;
802 if (__n > __bs)
803 {
804 __n = __bs;
805 __m = __l - __n;
806 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000807 _VSTD::move_backward(__m, __l, __re);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000808 __l = __m;
809 __r -= __n;
810 }
811 return __r;
812}
813
814template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
815 class _OutputIterator>
816_OutputIterator
817move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
818 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
819 _OutputIterator __r)
820{
821 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
822 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
823 difference_type __n = __l - __f;
824 while (__n > 0)
825 {
826 --__l;
827 pointer __lb = *__l.__m_iter_;
828 pointer __le = __l.__ptr_ + 1;
829 difference_type __bs = __le - __lb;
830 if (__bs > __n)
831 {
832 __bs = __n;
833 __lb = __le - __bs;
834 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000835 __r = _VSTD::move_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000836 __n -= __bs;
837 __l -= __bs - 1;
838 }
839 return __r;
840}
841
842template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
843 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
844__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
845move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
846 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
847 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
848{
849 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
850 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
851 difference_type __n = __l - __f;
852 while (__n > 0)
853 {
854 --__l;
855 pointer __lb = *__l.__m_iter_;
856 pointer __le = __l.__ptr_ + 1;
857 difference_type __bs = __le - __lb;
858 if (__bs > __n)
859 {
860 __bs = __n;
861 __lb = __le - __bs;
862 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000863 __r = _VSTD::move_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000864 __n -= __bs;
865 __l -= __bs - 1;
866 }
867 return __r;
868}
869
870template <bool>
871class __deque_base_common
872{
873protected:
874 void __throw_length_error() const;
875 void __throw_out_of_range() const;
876};
877
878template <bool __b>
879void
880__deque_base_common<__b>::__throw_length_error() const
881{
882#ifndef _LIBCPP_NO_EXCEPTIONS
883 throw length_error("deque");
884#endif
885}
886
887template <bool __b>
888void
889__deque_base_common<__b>::__throw_out_of_range() const
890{
891#ifndef _LIBCPP_NO_EXCEPTIONS
892 throw out_of_range("deque");
893#endif
894}
895
896template <class _Tp, class _Allocator>
897class __deque_base
898 : protected __deque_base_common<true>
899{
900 __deque_base(const __deque_base& __c);
901 __deque_base& operator=(const __deque_base& __c);
902protected:
903 typedef _Tp value_type;
904 typedef _Allocator allocator_type;
905 typedef allocator_traits<allocator_type> __alloc_traits;
906 typedef value_type& reference;
907 typedef const value_type& const_reference;
908 typedef typename __alloc_traits::size_type size_type;
909 typedef typename __alloc_traits::difference_type difference_type;
910 typedef typename __alloc_traits::pointer pointer;
911 typedef typename __alloc_traits::const_pointer const_pointer;
912
913 static const difference_type __block_size = sizeof(value_type) < 256 ? 4096 / sizeof(value_type) : 16;
914
915 typedef typename __alloc_traits::template
916#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
917 rebind_alloc<pointer>
918#else
919 rebind_alloc<pointer>::other
920#endif
921 __pointer_allocator;
922 typedef allocator_traits<__pointer_allocator> __map_traits;
923 typedef typename __map_traits::pointer __map_pointer;
Howard Hinnantdcb73a32013-06-23 21:17:24 +0000924 typedef typename __alloc_traits::template
925#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
926 rebind_alloc<const_pointer>
927#else
928 rebind_alloc<const_pointer>::other
929#endif
930 __const_pointer_allocator;
931 typedef typename allocator_traits<__const_pointer_allocator>::const_pointer __map_const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000932 typedef __split_buffer<pointer, __pointer_allocator> __map;
933
934 typedef __deque_iterator<value_type, pointer, reference, __map_pointer,
935 difference_type, __block_size> iterator;
936 typedef __deque_iterator<value_type, const_pointer, const_reference, __map_const_pointer,
937 difference_type, __block_size> const_iterator;
938
939 __map __map_;
940 size_type __start_;
941 __compressed_pair<size_type, allocator_type> __size_;
942
Howard Hinnantda2df912011-06-02 16:10:22 +0000943 iterator begin() _NOEXCEPT;
944 const_iterator begin() const _NOEXCEPT;
945 iterator end() _NOEXCEPT;
946 const_iterator end() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000947
948 _LIBCPP_INLINE_VISIBILITY size_type& size() {return __size_.first();}
Howard Hinnantda2df912011-06-02 16:10:22 +0000949 _LIBCPP_INLINE_VISIBILITY
950 const size_type& size() const _NOEXCEPT {return __size_.first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000951 _LIBCPP_INLINE_VISIBILITY allocator_type& __alloc() {return __size_.second();}
Howard Hinnantda2df912011-06-02 16:10:22 +0000952 _LIBCPP_INLINE_VISIBILITY
953 const allocator_type& __alloc() const _NOEXCEPT {return __size_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000954
Howard Hinnantad979ba2011-06-03 15:16:49 +0000955 __deque_base()
956 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000957 explicit __deque_base(const allocator_type& __a);
Howard Hinnantca2fc222011-06-02 20:00:14 +0000958public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000959 ~__deque_base();
960
Howard Hinnant74279a52010-09-04 23:28:19 +0000961#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000962
Howard Hinnantca2fc222011-06-02 20:00:14 +0000963 __deque_base(__deque_base&& __c)
964 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000965 __deque_base(__deque_base&& __c, const allocator_type& __a);
966
Howard Hinnant74279a52010-09-04 23:28:19 +0000967#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantca2fc222011-06-02 20:00:14 +0000968 void swap(__deque_base& __c)
Howard Hinnantbc47c7f2011-06-03 16:20:53 +0000969 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Howard Hinnantca2fc222011-06-02 20:00:14 +0000970 __is_nothrow_swappable<allocator_type>::value);
971protected:
Howard Hinnantda2df912011-06-02 16:10:22 +0000972 void clear() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000973
974 bool __invariants() const;
975
Howard Hinnant874ad9a2010-09-21 21:28:23 +0000976 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000977 void __move_assign(__deque_base& __c)
Howard Hinnant4931e092011-06-02 21:38:57 +0000978 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
979 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000980 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000981 __map_ = _VSTD::move(__c.__map_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000982 __start_ = __c.__start_;
983 size() = __c.size();
984 __move_assign_alloc(__c);
985 __c.__start_ = __c.size() = 0;
986 }
987
Howard Hinnant874ad9a2010-09-21 21:28:23 +0000988 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000989 void __move_assign_alloc(__deque_base& __c)
Howard Hinnantca2fc222011-06-02 20:00:14 +0000990 _NOEXCEPT_(!__alloc_traits::propagate_on_container_move_assignment::value ||
991 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000992 {__move_assign_alloc(__c, integral_constant<bool,
993 __alloc_traits::propagate_on_container_move_assignment::value>());}
994
995private:
Howard Hinnant874ad9a2010-09-21 21:28:23 +0000996 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +0000997 void __move_assign_alloc(__deque_base& __c, true_type)
Howard Hinnantca2fc222011-06-02 20:00:14 +0000998 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000999 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001000 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001001 }
1002
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001003 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001004 void __move_assign_alloc(__deque_base&, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001005 {}
1006
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001007 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008 static void __swap_alloc(allocator_type& __x, allocator_type& __y)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001009 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1010 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001011 {__swap_alloc(__x, __y, integral_constant<bool,
1012 __alloc_traits::propagate_on_container_swap::value>());}
1013
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001014 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001015 static void __swap_alloc(allocator_type& __x, allocator_type& __y, true_type)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001016 _NOEXCEPT_(__is_nothrow_swappable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001017 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001018 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001019 swap(__x, __y);
1020 }
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001021
1022 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001023 static void __swap_alloc(allocator_type&, allocator_type&, false_type)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001024 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001025 {}
1026};
1027
1028template <class _Tp, class _Allocator>
1029bool
1030__deque_base<_Tp, _Allocator>::__invariants() const
1031{
1032 if (!__map_.__invariants())
1033 return false;
1034 if (__map_.size() >= size_type(-1) / __block_size)
1035 return false;
1036 for (typename __map::const_iterator __i = __map_.begin(), __e = __map_.end();
1037 __i != __e; ++__i)
1038 if (*__i == nullptr)
1039 return false;
1040 if (__map_.size() != 0)
1041 {
1042 if (size() >= __map_.size() * __block_size)
1043 return false;
1044 if (__start_ >= __map_.size() * __block_size - size())
1045 return false;
1046 }
1047 else
1048 {
1049 if (size() != 0)
1050 return false;
1051 if (__start_ != 0)
1052 return false;
1053 }
1054 return true;
1055}
1056
1057template <class _Tp, class _Allocator>
1058typename __deque_base<_Tp, _Allocator>::iterator
Howard Hinnantda2df912011-06-02 16:10:22 +00001059__deque_base<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001060{
1061 __map_pointer __mp = __map_.begin() + __start_ / __block_size;
1062 return iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
1063}
1064
1065template <class _Tp, class _Allocator>
1066typename __deque_base<_Tp, _Allocator>::const_iterator
Howard Hinnantda2df912011-06-02 16:10:22 +00001067__deque_base<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001068{
Howard Hinnantdcb73a32013-06-23 21:17:24 +00001069 __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __start_ / __block_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001070 return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
1071}
1072
1073template <class _Tp, class _Allocator>
1074typename __deque_base<_Tp, _Allocator>::iterator
Howard Hinnantda2df912011-06-02 16:10:22 +00001075__deque_base<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001076{
1077 size_type __p = size() + __start_;
1078 __map_pointer __mp = __map_.begin() + __p / __block_size;
1079 return iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
1080}
1081
1082template <class _Tp, class _Allocator>
1083typename __deque_base<_Tp, _Allocator>::const_iterator
Howard Hinnantda2df912011-06-02 16:10:22 +00001084__deque_base<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001085{
1086 size_type __p = size() + __start_;
Howard Hinnantdcb73a32013-06-23 21:17:24 +00001087 __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __p / __block_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001088 return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
1089}
1090
1091template <class _Tp, class _Allocator>
1092inline _LIBCPP_INLINE_VISIBILITY
1093__deque_base<_Tp, _Allocator>::__deque_base()
Howard Hinnantad979ba2011-06-03 15:16:49 +00001094 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001095 : __start_(0), __size_(0) {}
1096
1097template <class _Tp, class _Allocator>
1098inline _LIBCPP_INLINE_VISIBILITY
1099__deque_base<_Tp, _Allocator>::__deque_base(const allocator_type& __a)
1100 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a) {}
1101
1102template <class _Tp, class _Allocator>
1103__deque_base<_Tp, _Allocator>::~__deque_base()
1104{
1105 clear();
1106 typename __map::iterator __i = __map_.begin();
1107 typename __map::iterator __e = __map_.end();
1108 for (; __i != __e; ++__i)
1109 __alloc_traits::deallocate(__alloc(), *__i, __block_size);
1110}
1111
Howard Hinnant74279a52010-09-04 23:28:19 +00001112#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001113
1114template <class _Tp, class _Allocator>
1115__deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001116 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001117 : __map_(_VSTD::move(__c.__map_)),
1118 __start_(_VSTD::move(__c.__start_)),
1119 __size_(_VSTD::move(__c.__size_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001120{
1121 __c.__start_ = 0;
1122 __c.size() = 0;
1123}
1124
1125template <class _Tp, class _Allocator>
1126__deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c, const allocator_type& __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001127 : __map_(_VSTD::move(__c.__map_), __pointer_allocator(__a)),
1128 __start_(_VSTD::move(__c.__start_)),
1129 __size_(_VSTD::move(__c.size()), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001130{
1131 if (__a == __c.__alloc())
1132 {
1133 __c.__start_ = 0;
1134 __c.size() = 0;
1135 }
1136 else
1137 {
1138 __map_.clear();
1139 __start_ = 0;
1140 size() = 0;
1141 }
1142}
1143
Howard Hinnant74279a52010-09-04 23:28:19 +00001144#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001145
1146template <class _Tp, class _Allocator>
1147void
1148__deque_base<_Tp, _Allocator>::swap(__deque_base& __c)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001149 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value||
1150 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001151{
1152 __map_.swap(__c.__map_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001153 _VSTD::swap(__start_, __c.__start_);
1154 _VSTD::swap(size(), __c.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001155 __swap_alloc(__alloc(), __c.__alloc());
1156}
1157
1158template <class _Tp, class _Allocator>
1159void
Howard Hinnantda2df912011-06-02 16:10:22 +00001160__deque_base<_Tp, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001161{
1162 allocator_type& __a = __alloc();
1163 for (iterator __i = begin(), __e = end(); __i != __e; ++__i)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001164 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001165 size() = 0;
1166 while (__map_.size() > 2)
1167 {
1168 __alloc_traits::deallocate(__a, __map_.front(), __block_size);
1169 __map_.pop_front();
1170 }
1171 switch (__map_.size())
1172 {
1173 case 1:
1174 __start_ = __block_size / 2;
1175 break;
1176 case 2:
1177 __start_ = __block_size;
1178 break;
1179 }
1180}
1181
Marshall Clow65cd4c62015-02-18 17:24:08 +00001182template <class _Tp, class _Allocator /*= allocator<_Tp>*/>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001183class _LIBCPP_TYPE_VIS_ONLY deque
Howard Hinnantc51e1022010-05-11 19:42:16 +00001184 : private __deque_base<_Tp, _Allocator>
1185{
1186public:
1187 // types:
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001188
Howard Hinnantc51e1022010-05-11 19:42:16 +00001189 typedef _Tp value_type;
1190 typedef _Allocator allocator_type;
1191
1192 typedef __deque_base<value_type, allocator_type> __base;
1193
1194 typedef typename __base::__alloc_traits __alloc_traits;
1195 typedef typename __base::reference reference;
1196 typedef typename __base::const_reference const_reference;
1197 typedef typename __base::iterator iterator;
1198 typedef typename __base::const_iterator const_iterator;
1199 typedef typename __base::size_type size_type;
1200 typedef typename __base::difference_type difference_type;
1201
1202 typedef typename __base::pointer pointer;
1203 typedef typename __base::const_pointer const_pointer;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001204 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
1205 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001206
1207 // construct/copy/destroy:
Howard Hinnantad979ba2011-06-03 15:16:49 +00001208 _LIBCPP_INLINE_VISIBILITY
1209 deque()
1210 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
1211 {}
Marshall Clow7ed23a72014-03-05 19:06:20 +00001212 _LIBCPP_INLINE_VISIBILITY explicit deque(const allocator_type& __a) : __base(__a) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001213 explicit deque(size_type __n);
Marshall Clowbc4fcb42013-09-07 16:16:19 +00001214#if _LIBCPP_STD_VER > 11
1215 explicit deque(size_type __n, const _Allocator& __a);
1216#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001217 deque(size_type __n, const value_type& __v);
1218 deque(size_type __n, const value_type& __v, const allocator_type& __a);
1219 template <class _InputIter>
1220 deque(_InputIter __f, _InputIter __l,
1221 typename enable_if<__is_input_iterator<_InputIter>::value>::type* = 0);
1222 template <class _InputIter>
1223 deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
1224 typename enable_if<__is_input_iterator<_InputIter>::value>::type* = 0);
1225 deque(const deque& __c);
1226 deque(const deque& __c, const allocator_type& __a);
Howard Hinnant33711792011-08-12 21:56:02 +00001227#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001228 deque(initializer_list<value_type> __il);
1229 deque(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnant33711792011-08-12 21:56:02 +00001230#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001231
1232 deque& operator=(const deque& __c);
Howard Hinnant33711792011-08-12 21:56:02 +00001233#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001234 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001235 deque& operator=(initializer_list<value_type> __il) {assign(__il); return *this;}
Howard Hinnant33711792011-08-12 21:56:02 +00001236#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001237
Howard Hinnant74279a52010-09-04 23:28:19 +00001238#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantca2fc222011-06-02 20:00:14 +00001239 deque(deque&& __c) _NOEXCEPT_(is_nothrow_move_constructible<__base>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001240 deque(deque&& __c, const allocator_type& __a);
Howard Hinnantca2fc222011-06-02 20:00:14 +00001241 deque& operator=(deque&& __c)
Howard Hinnant4931e092011-06-02 21:38:57 +00001242 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1243 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant74279a52010-09-04 23:28:19 +00001244#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001245
1246 template <class _InputIter>
1247 void assign(_InputIter __f, _InputIter __l,
1248 typename enable_if<__is_input_iterator<_InputIter>::value &&
1249 !__is_random_access_iterator<_InputIter>::value>::type* = 0);
1250 template <class _RAIter>
1251 void assign(_RAIter __f, _RAIter __l,
1252 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
1253 void assign(size_type __n, const value_type& __v);
Howard Hinnant33711792011-08-12 21:56:02 +00001254#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001255 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001256 void assign(initializer_list<value_type> __il) {assign(__il.begin(), __il.end());}
Howard Hinnant33711792011-08-12 21:56:02 +00001257#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001258
Howard Hinnantda2df912011-06-02 16:10:22 +00001259 allocator_type get_allocator() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001260
1261 // iterators:
1262
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001263 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001264 iterator begin() _NOEXCEPT {return __base::begin();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001265 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001266 const_iterator begin() const _NOEXCEPT {return __base::begin();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001267 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001268 iterator end() _NOEXCEPT {return __base::end();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001269 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001270 const_iterator end() const _NOEXCEPT {return __base::end();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001271
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001272 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001273 reverse_iterator rbegin() _NOEXCEPT
1274 {return reverse_iterator(__base::end());}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001275 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001276 const_reverse_iterator rbegin() const _NOEXCEPT
1277 {return const_reverse_iterator(__base::end());}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001278 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001279 reverse_iterator rend() _NOEXCEPT
1280 {return reverse_iterator(__base::begin());}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001281 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001282 const_reverse_iterator rend() const _NOEXCEPT
1283 {return const_reverse_iterator(__base::begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001284
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001286 const_iterator cbegin() const _NOEXCEPT
1287 {return __base::begin();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001288 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001289 const_iterator cend() const _NOEXCEPT
1290 {return __base::end();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001291 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001292 const_reverse_iterator crbegin() const _NOEXCEPT
1293 {return const_reverse_iterator(__base::end());}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001294 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001295 const_reverse_iterator crend() const _NOEXCEPT
1296 {return const_reverse_iterator(__base::begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001297
1298 // capacity:
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001300 size_type size() const _NOEXCEPT {return __base::size();}
1301 _LIBCPP_INLINE_VISIBILITY
1302 size_type max_size() const _NOEXCEPT
1303 {return __alloc_traits::max_size(__base::__alloc());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001304 void resize(size_type __n);
1305 void resize(size_type __n, const value_type& __v);
Howard Hinnant4931e092011-06-02 21:38:57 +00001306 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantda2df912011-06-02 16:10:22 +00001307 _LIBCPP_INLINE_VISIBILITY
1308 bool empty() const _NOEXCEPT {return __base::size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001309
1310 // element access:
1311 reference operator[](size_type __i);
1312 const_reference operator[](size_type __i) const;
1313 reference at(size_type __i);
1314 const_reference at(size_type __i) const;
1315 reference front();
1316 const_reference front() const;
1317 reference back();
1318 const_reference back() const;
1319
1320 // 23.2.2.3 modifiers:
1321 void push_front(const value_type& __v);
1322 void push_back(const value_type& __v);
Howard Hinnant74279a52010-09-04 23:28:19 +00001323#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1324#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001325 template <class... _Args> void emplace_front(_Args&&... __args);
1326 template <class... _Args> void emplace_back(_Args&&... __args);
1327 template <class... _Args> iterator emplace(const_iterator __p, _Args&&... __args);
Howard Hinnant74279a52010-09-04 23:28:19 +00001328#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001329 void push_front(value_type&& __v);
1330 void push_back(value_type&& __v);
1331 iterator insert(const_iterator __p, value_type&& __v);
Howard Hinnant74279a52010-09-04 23:28:19 +00001332#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001333 iterator insert(const_iterator __p, const value_type& __v);
1334 iterator insert(const_iterator __p, size_type __n, const value_type& __v);
1335 template <class _InputIter>
Marshall Clow6b612cf2015-01-22 18:33:29 +00001336 iterator insert(const_iterator __p, _InputIter __f, _InputIter __l,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001337 typename enable_if<__is_input_iterator<_InputIter>::value
Marshall Clow6b612cf2015-01-22 18:33:29 +00001338 &&!__is_forward_iterator<_InputIter>::value>::type* = 0);
1339 template <class _ForwardIterator>
1340 iterator insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l,
1341 typename enable_if<__is_forward_iterator<_ForwardIterator>::value
1342 &&!__is_bidirectional_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001343 template <class _BiIter>
Marshall Clow6b612cf2015-01-22 18:33:29 +00001344 iterator insert(const_iterator __p, _BiIter __f, _BiIter __l,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001345 typename enable_if<__is_bidirectional_iterator<_BiIter>::value>::type* = 0);
Howard Hinnant33711792011-08-12 21:56:02 +00001346#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001347 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001348 iterator insert(const_iterator __p, initializer_list<value_type> __il)
1349 {return insert(__p, __il.begin(), __il.end());}
Howard Hinnant33711792011-08-12 21:56:02 +00001350#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001351 void pop_front();
1352 void pop_back();
1353 iterator erase(const_iterator __p);
1354 iterator erase(const_iterator __f, const_iterator __l);
1355
Howard Hinnantca2fc222011-06-02 20:00:14 +00001356 void swap(deque& __c)
1357 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1358 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantda2df912011-06-02 16:10:22 +00001359 void clear() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001360
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001361 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001362 bool __invariants() const {return __base::__invariants();}
1363private:
Howard Hinnantdcb73a32013-06-23 21:17:24 +00001364 typedef typename __base::__map_const_pointer __map_const_pointer;
1365
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001367 static size_type __recommend_blocks(size_type __n)
1368 {
1369 return __n / __base::__block_size + (__n % __base::__block_size != 0);
1370 }
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001371 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001372 size_type __capacity() const
1373 {
1374 return __base::__map_.size() == 0 ? 0 : __base::__map_.size() * __base::__block_size - 1;
1375 }
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001376 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001377 size_type __front_spare() const
1378 {
1379 return __base::__start_;
1380 }
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001381 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001382 size_type __back_spare() const
1383 {
1384 return __capacity() - (__base::__start_ + __base::size());
1385 }
1386
1387 template <class _InpIter>
1388 void __append(_InpIter __f, _InpIter __l,
1389 typename enable_if<__is_input_iterator<_InpIter>::value &&
1390 !__is_forward_iterator<_InpIter>::value>::type* = 0);
1391 template <class _ForIter>
1392 void __append(_ForIter __f, _ForIter __l,
1393 typename enable_if<__is_forward_iterator<_ForIter>::value>::type* = 0);
1394 void __append(size_type __n);
1395 void __append(size_type __n, const value_type& __v);
1396 void __erase_to_end(const_iterator __f);
1397 void __add_front_capacity();
1398 void __add_front_capacity(size_type __n);
1399 void __add_back_capacity();
1400 void __add_back_capacity(size_type __n);
1401 iterator __move_and_check(iterator __f, iterator __l, iterator __r,
1402 const_pointer& __vt);
1403 iterator __move_backward_and_check(iterator __f, iterator __l, iterator __r,
1404 const_pointer& __vt);
1405 void __move_construct_and_check(iterator __f, iterator __l,
1406 iterator __r, const_pointer& __vt);
1407 void __move_construct_backward_and_check(iterator __f, iterator __l,
1408 iterator __r, const_pointer& __vt);
1409
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001410 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001411 void __copy_assign_alloc(const deque& __c)
1412 {__copy_assign_alloc(__c, integral_constant<bool,
1413 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1414
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001415 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001416 void __copy_assign_alloc(const deque& __c, true_type)
1417 {
1418 if (__base::__alloc() != __c.__alloc())
1419 {
1420 clear();
1421 shrink_to_fit();
1422 }
1423 __base::__alloc() = __c.__alloc();
1424 __base::__map_.__alloc() = __c.__map_.__alloc();
1425 }
1426
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001427 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001428 void __copy_assign_alloc(const deque&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001429 {}
1430
Howard Hinnant4931e092011-06-02 21:38:57 +00001431 void __move_assign(deque& __c, true_type)
1432 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001433 void __move_assign(deque& __c, false_type);
1434};
1435
1436template <class _Tp, class _Allocator>
1437deque<_Tp, _Allocator>::deque(size_type __n)
1438{
1439 if (__n > 0)
1440 __append(__n);
1441}
1442
Marshall Clowbc4fcb42013-09-07 16:16:19 +00001443#if _LIBCPP_STD_VER > 11
1444template <class _Tp, class _Allocator>
1445deque<_Tp, _Allocator>::deque(size_type __n, const _Allocator& __a)
1446 : __base(__a)
1447{
1448 if (__n > 0)
1449 __append(__n);
1450}
1451#endif
1452
Howard Hinnantc51e1022010-05-11 19:42:16 +00001453template <class _Tp, class _Allocator>
1454deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v)
1455{
1456 if (__n > 0)
1457 __append(__n, __v);
1458}
1459
1460template <class _Tp, class _Allocator>
1461deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v, const allocator_type& __a)
1462 : __base(__a)
1463{
1464 if (__n > 0)
1465 __append(__n, __v);
1466}
1467
1468template <class _Tp, class _Allocator>
1469template <class _InputIter>
1470deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l,
1471 typename enable_if<__is_input_iterator<_InputIter>::value>::type*)
1472{
1473 __append(__f, __l);
1474}
1475
1476template <class _Tp, class _Allocator>
1477template <class _InputIter>
1478deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
1479 typename enable_if<__is_input_iterator<_InputIter>::value>::type*)
1480 : __base(__a)
1481{
1482 __append(__f, __l);
1483}
1484
1485template <class _Tp, class _Allocator>
1486deque<_Tp, _Allocator>::deque(const deque& __c)
1487 : __base(__alloc_traits::select_on_container_copy_construction(__c.__alloc()))
1488{
1489 __append(__c.begin(), __c.end());
1490}
1491
1492template <class _Tp, class _Allocator>
1493deque<_Tp, _Allocator>::deque(const deque& __c, const allocator_type& __a)
1494 : __base(__a)
1495{
1496 __append(__c.begin(), __c.end());
1497}
1498
Howard Hinnant33711792011-08-12 21:56:02 +00001499#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1500
Howard Hinnantc51e1022010-05-11 19:42:16 +00001501template <class _Tp, class _Allocator>
1502deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il)
1503{
1504 __append(__il.begin(), __il.end());
1505}
1506
1507template <class _Tp, class _Allocator>
1508deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il, const allocator_type& __a)
1509 : __base(__a)
1510{
1511 __append(__il.begin(), __il.end());
1512}
1513
Howard Hinnant33711792011-08-12 21:56:02 +00001514#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1515
Howard Hinnantc51e1022010-05-11 19:42:16 +00001516template <class _Tp, class _Allocator>
1517deque<_Tp, _Allocator>&
1518deque<_Tp, _Allocator>::operator=(const deque& __c)
1519{
1520 if (this != &__c)
1521 {
1522 __copy_assign_alloc(__c);
1523 assign(__c.begin(), __c.end());
1524 }
1525 return *this;
1526}
1527
Howard Hinnant74279a52010-09-04 23:28:19 +00001528#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001529
1530template <class _Tp, class _Allocator>
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001531inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001532deque<_Tp, _Allocator>::deque(deque&& __c)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001533 _NOEXCEPT_(is_nothrow_move_constructible<__base>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001534 : __base(_VSTD::move(__c))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001535{
1536}
1537
1538template <class _Tp, class _Allocator>
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001539inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001540deque<_Tp, _Allocator>::deque(deque&& __c, const allocator_type& __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001541 : __base(_VSTD::move(__c), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001542{
1543 if (__a != __c.__alloc())
1544 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001545 typedef move_iterator<iterator> _Ip;
1546 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001547 }
1548}
1549
1550template <class _Tp, class _Allocator>
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001551inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001552deque<_Tp, _Allocator>&
1553deque<_Tp, _Allocator>::operator=(deque&& __c)
Howard Hinnant4931e092011-06-02 21:38:57 +00001554 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1555 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001556{
1557 __move_assign(__c, integral_constant<bool,
1558 __alloc_traits::propagate_on_container_move_assignment::value>());
1559 return *this;
1560}
1561
1562template <class _Tp, class _Allocator>
1563void
1564deque<_Tp, _Allocator>::__move_assign(deque& __c, false_type)
1565{
1566 if (__base::__alloc() != __c.__alloc())
1567 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001568 typedef move_iterator<iterator> _Ip;
1569 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001570 }
1571 else
1572 __move_assign(__c, true_type());
1573}
1574
1575template <class _Tp, class _Allocator>
1576void
1577deque<_Tp, _Allocator>::__move_assign(deque& __c, true_type)
Howard Hinnant4931e092011-06-02 21:38:57 +00001578 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001579{
1580 clear();
1581 shrink_to_fit();
1582 __base::__move_assign(__c);
1583}
1584
Howard Hinnant74279a52010-09-04 23:28:19 +00001585#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001586
1587template <class _Tp, class _Allocator>
1588template <class _InputIter>
1589void
1590deque<_Tp, _Allocator>::assign(_InputIter __f, _InputIter __l,
1591 typename enable_if<__is_input_iterator<_InputIter>::value &&
1592 !__is_random_access_iterator<_InputIter>::value>::type*)
1593{
1594 iterator __i = __base::begin();
1595 iterator __e = __base::end();
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001596 for (; __f != __l && __i != __e; ++__f, (void) ++__i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001597 *__i = *__f;
1598 if (__f != __l)
1599 __append(__f, __l);
1600 else
1601 __erase_to_end(__i);
1602}
1603
1604template <class _Tp, class _Allocator>
1605template <class _RAIter>
1606void
1607deque<_Tp, _Allocator>::assign(_RAIter __f, _RAIter __l,
1608 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
1609{
1610 if (static_cast<size_type>(__l - __f) > __base::size())
1611 {
1612 _RAIter __m = __f + __base::size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001613 _VSTD::copy(__f, __m, __base::begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001614 __append(__m, __l);
1615 }
1616 else
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001617 __erase_to_end(_VSTD::copy(__f, __l, __base::begin()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001618}
1619
1620template <class _Tp, class _Allocator>
1621void
1622deque<_Tp, _Allocator>::assign(size_type __n, const value_type& __v)
1623{
1624 if (__n > __base::size())
1625 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001626 _VSTD::fill_n(__base::begin(), __base::size(), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001627 __n -= __base::size();
1628 __append(__n, __v);
1629 }
1630 else
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001631 __erase_to_end(_VSTD::fill_n(__base::begin(), __n, __v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001632}
1633
1634template <class _Tp, class _Allocator>
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001635inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001636_Allocator
Howard Hinnantda2df912011-06-02 16:10:22 +00001637deque<_Tp, _Allocator>::get_allocator() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001638{
1639 return __base::__alloc();
1640}
1641
1642template <class _Tp, class _Allocator>
1643void
1644deque<_Tp, _Allocator>::resize(size_type __n)
1645{
1646 if (__n > __base::size())
1647 __append(__n - __base::size());
1648 else if (__n < __base::size())
1649 __erase_to_end(__base::begin() + __n);
1650}
1651
1652template <class _Tp, class _Allocator>
1653void
1654deque<_Tp, _Allocator>::resize(size_type __n, const value_type& __v)
1655{
1656 if (__n > __base::size())
1657 __append(__n - __base::size(), __v);
1658 else if (__n < __base::size())
1659 __erase_to_end(__base::begin() + __n);
1660}
1661
1662template <class _Tp, class _Allocator>
1663void
Howard Hinnant4931e092011-06-02 21:38:57 +00001664deque<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001665{
1666 allocator_type& __a = __base::__alloc();
1667 if (empty())
1668 {
1669 while (__base::__map_.size() > 0)
1670 {
1671 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
1672 __base::__map_.pop_back();
1673 }
1674 __base::__start_ = 0;
1675 }
1676 else
1677 {
1678 if (__front_spare() >= __base::__block_size)
1679 {
1680 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
1681 __base::__map_.pop_front();
1682 __base::__start_ -= __base::__block_size;
1683 }
1684 if (__back_spare() >= __base::__block_size)
1685 {
1686 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
1687 __base::__map_.pop_back();
1688 }
1689 }
1690 __base::__map_.shrink_to_fit();
1691}
1692
1693template <class _Tp, class _Allocator>
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001694inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001695typename deque<_Tp, _Allocator>::reference
1696deque<_Tp, _Allocator>::operator[](size_type __i)
1697{
1698 size_type __p = __base::__start_ + __i;
1699 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1700}
1701
1702template <class _Tp, class _Allocator>
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001703inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001704typename deque<_Tp, _Allocator>::const_reference
1705deque<_Tp, _Allocator>::operator[](size_type __i) const
1706{
1707 size_type __p = __base::__start_ + __i;
1708 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1709}
1710
1711template <class _Tp, class _Allocator>
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001712inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001713typename deque<_Tp, _Allocator>::reference
1714deque<_Tp, _Allocator>::at(size_type __i)
1715{
1716 if (__i >= __base::size())
1717 __base::__throw_out_of_range();
1718 size_type __p = __base::__start_ + __i;
1719 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1720}
1721
1722template <class _Tp, class _Allocator>
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001723inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001724typename deque<_Tp, _Allocator>::const_reference
1725deque<_Tp, _Allocator>::at(size_type __i) const
1726{
1727 if (__i >= __base::size())
1728 __base::__throw_out_of_range();
1729 size_type __p = __base::__start_ + __i;
1730 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1731}
1732
1733template <class _Tp, class _Allocator>
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001734inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001735typename deque<_Tp, _Allocator>::reference
1736deque<_Tp, _Allocator>::front()
1737{
1738 return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1739 + __base::__start_ % __base::__block_size);
1740}
1741
1742template <class _Tp, class _Allocator>
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001743inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001744typename deque<_Tp, _Allocator>::const_reference
1745deque<_Tp, _Allocator>::front() const
1746{
1747 return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1748 + __base::__start_ % __base::__block_size);
1749}
1750
1751template <class _Tp, class _Allocator>
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001752inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001753typename deque<_Tp, _Allocator>::reference
1754deque<_Tp, _Allocator>::back()
1755{
1756 size_type __p = __base::size() + __base::__start_ - 1;
1757 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1758}
1759
1760template <class _Tp, class _Allocator>
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001761inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001762typename deque<_Tp, _Allocator>::const_reference
1763deque<_Tp, _Allocator>::back() const
1764{
1765 size_type __p = __base::size() + __base::__start_ - 1;
1766 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1767}
1768
1769template <class _Tp, class _Allocator>
1770void
1771deque<_Tp, _Allocator>::push_back(const value_type& __v)
1772{
1773 allocator_type& __a = __base::__alloc();
1774 if (__back_spare() == 0)
1775 __add_back_capacity();
1776 // __back_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001777 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001778 ++__base::size();
1779}
1780
Howard Hinnant74279a52010-09-04 23:28:19 +00001781#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001782
1783template <class _Tp, class _Allocator>
1784void
1785deque<_Tp, _Allocator>::push_back(value_type&& __v)
1786{
1787 allocator_type& __a = __base::__alloc();
1788 if (__back_spare() == 0)
1789 __add_back_capacity();
1790 // __back_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001791 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001792 ++__base::size();
1793}
1794
Howard Hinnant74279a52010-09-04 23:28:19 +00001795#ifndef _LIBCPP_HAS_NO_VARIADICS
1796
Howard Hinnantc51e1022010-05-11 19:42:16 +00001797template <class _Tp, class _Allocator>
1798template <class... _Args>
1799void
1800deque<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1801{
1802 allocator_type& __a = __base::__alloc();
1803 if (__back_spare() == 0)
1804 __add_back_capacity();
1805 // __back_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001806 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001807 ++__base::size();
1808}
1809
Howard Hinnant74279a52010-09-04 23:28:19 +00001810#endif // _LIBCPP_HAS_NO_VARIADICS
1811#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001812
1813template <class _Tp, class _Allocator>
1814void
1815deque<_Tp, _Allocator>::push_front(const value_type& __v)
1816{
1817 allocator_type& __a = __base::__alloc();
1818 if (__front_spare() == 0)
1819 __add_front_capacity();
1820 // __front_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001821 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001822 --__base::__start_;
1823 ++__base::size();
1824}
1825
Howard Hinnant74279a52010-09-04 23:28:19 +00001826#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001827
1828template <class _Tp, class _Allocator>
1829void
1830deque<_Tp, _Allocator>::push_front(value_type&& __v)
1831{
1832 allocator_type& __a = __base::__alloc();
1833 if (__front_spare() == 0)
1834 __add_front_capacity();
1835 // __front_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001836 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001837 --__base::__start_;
1838 ++__base::size();
1839}
1840
Howard Hinnant74279a52010-09-04 23:28:19 +00001841#ifndef _LIBCPP_HAS_NO_VARIADICS
1842
Howard Hinnantc51e1022010-05-11 19:42:16 +00001843template <class _Tp, class _Allocator>
1844template <class... _Args>
1845void
1846deque<_Tp, _Allocator>::emplace_front(_Args&&... __args)
1847{
1848 allocator_type& __a = __base::__alloc();
1849 if (__front_spare() == 0)
1850 __add_front_capacity();
1851 // __front_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001852 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001853 --__base::__start_;
1854 ++__base::size();
1855}
1856
Howard Hinnant74279a52010-09-04 23:28:19 +00001857#endif // _LIBCPP_HAS_NO_VARIADICS
1858#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001859
1860template <class _Tp, class _Allocator>
1861typename deque<_Tp, _Allocator>::iterator
1862deque<_Tp, _Allocator>::insert(const_iterator __p, const value_type& __v)
1863{
1864 size_type __pos = __p - __base::begin();
1865 size_type __to_end = __base::size() - __pos;
1866 allocator_type& __a = __base::__alloc();
1867 if (__pos < __to_end)
1868 { // insert by shifting things backward
1869 if (__front_spare() == 0)
1870 __add_front_capacity();
1871 // __front_spare() >= 1
1872 if (__pos == 0)
1873 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001874 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001875 --__base::__start_;
1876 ++__base::size();
1877 }
1878 else
1879 {
1880 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1881 iterator __b = __base::begin();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001882 iterator __bm1 = _VSTD::prev(__b);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001883 if (__vt == pointer_traits<const_pointer>::pointer_to(*__b))
1884 __vt = pointer_traits<const_pointer>::pointer_to(*__bm1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001885 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001886 --__base::__start_;
1887 ++__base::size();
1888 if (__pos > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001889 __b = __move_and_check(_VSTD::next(__b), __b + __pos, __b, __vt);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001890 *__b = *__vt;
1891 }
1892 }
1893 else
1894 { // insert by shifting things forward
1895 if (__back_spare() == 0)
1896 __add_back_capacity();
1897 // __back_capacity >= 1
1898 size_type __de = __base::size() - __pos;
1899 if (__de == 0)
1900 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001901 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001902 ++__base::size();
1903 }
1904 else
1905 {
1906 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1907 iterator __e = __base::end();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001908 iterator __em1 = _VSTD::prev(__e);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001909 if (__vt == pointer_traits<const_pointer>::pointer_to(*__em1))
1910 __vt = pointer_traits<const_pointer>::pointer_to(*__e);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001911 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001912 ++__base::size();
1913 if (__de > 1)
1914 __e = __move_backward_and_check(__e - __de, __em1, __e, __vt);
1915 *--__e = *__vt;
1916 }
1917 }
1918 return __base::begin() + __pos;
1919}
1920
Howard Hinnant74279a52010-09-04 23:28:19 +00001921#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001922
1923template <class _Tp, class _Allocator>
1924typename deque<_Tp, _Allocator>::iterator
1925deque<_Tp, _Allocator>::insert(const_iterator __p, value_type&& __v)
1926{
1927 size_type __pos = __p - __base::begin();
1928 size_type __to_end = __base::size() - __pos;
1929 allocator_type& __a = __base::__alloc();
1930 if (__pos < __to_end)
1931 { // insert by shifting things backward
1932 if (__front_spare() == 0)
1933 __add_front_capacity();
1934 // __front_spare() >= 1
1935 if (__pos == 0)
1936 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001937 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001938 --__base::__start_;
1939 ++__base::size();
1940 }
1941 else
1942 {
1943 iterator __b = __base::begin();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001944 iterator __bm1 = _VSTD::prev(__b);
1945 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001946 --__base::__start_;
1947 ++__base::size();
1948 if (__pos > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001949 __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
1950 *__b = _VSTD::move(__v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001951 }
1952 }
1953 else
1954 { // insert by shifting things forward
1955 if (__back_spare() == 0)
1956 __add_back_capacity();
1957 // __back_capacity >= 1
1958 size_type __de = __base::size() - __pos;
1959 if (__de == 0)
1960 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001961 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001962 ++__base::size();
1963 }
1964 else
1965 {
1966 iterator __e = __base::end();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001967 iterator __em1 = _VSTD::prev(__e);
1968 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001969 ++__base::size();
1970 if (__de > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001971 __e = _VSTD::move_backward(__e - __de, __em1, __e);
1972 *--__e = _VSTD::move(__v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001973 }
1974 }
1975 return __base::begin() + __pos;
1976}
1977
Howard Hinnant74279a52010-09-04 23:28:19 +00001978#ifndef _LIBCPP_HAS_NO_VARIADICS
1979
Howard Hinnantc51e1022010-05-11 19:42:16 +00001980template <class _Tp, class _Allocator>
1981template <class... _Args>
1982typename deque<_Tp, _Allocator>::iterator
1983deque<_Tp, _Allocator>::emplace(const_iterator __p, _Args&&... __args)
1984{
1985 size_type __pos = __p - __base::begin();
1986 size_type __to_end = __base::size() - __pos;
1987 allocator_type& __a = __base::__alloc();
1988 if (__pos < __to_end)
1989 { // insert by shifting things backward
1990 if (__front_spare() == 0)
1991 __add_front_capacity();
1992 // __front_spare() >= 1
1993 if (__pos == 0)
1994 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001995 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001996 --__base::__start_;
1997 ++__base::size();
1998 }
1999 else
2000 {
Howard Hinnant800d2d22012-07-08 23:23:04 +00002001 value_type __tmp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002002 iterator __b = __base::begin();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002003 iterator __bm1 = _VSTD::prev(__b);
2004 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002005 --__base::__start_;
2006 ++__base::size();
2007 if (__pos > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002008 __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
Howard Hinnant800d2d22012-07-08 23:23:04 +00002009 *__b = _VSTD::move(__tmp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002010 }
2011 }
2012 else
2013 { // insert by shifting things forward
2014 if (__back_spare() == 0)
2015 __add_back_capacity();
2016 // __back_capacity >= 1
2017 size_type __de = __base::size() - __pos;
2018 if (__de == 0)
2019 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002020 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002021 ++__base::size();
2022 }
2023 else
2024 {
Howard Hinnant800d2d22012-07-08 23:23:04 +00002025 value_type __tmp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002026 iterator __e = __base::end();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002027 iterator __em1 = _VSTD::prev(__e);
2028 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002029 ++__base::size();
2030 if (__de > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002031 __e = _VSTD::move_backward(__e - __de, __em1, __e);
Howard Hinnant800d2d22012-07-08 23:23:04 +00002032 *--__e = _VSTD::move(__tmp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002033 }
2034 }
2035 return __base::begin() + __pos;
2036}
2037
Howard Hinnant74279a52010-09-04 23:28:19 +00002038#endif // _LIBCPP_HAS_NO_VARIADICS
2039#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002040
2041template <class _Tp, class _Allocator>
2042typename deque<_Tp, _Allocator>::iterator
2043deque<_Tp, _Allocator>::insert(const_iterator __p, size_type __n, const value_type& __v)
2044{
2045 size_type __pos = __p - __base::begin();
2046 size_type __to_end = __base::size() - __pos;
2047 allocator_type& __a = __base::__alloc();
2048 if (__pos < __to_end)
2049 { // insert by shifting things backward
2050 if (__n > __front_spare())
2051 __add_front_capacity(__n - __front_spare());
2052 // __n <= __front_spare()
2053 size_type __old_n = __n;
2054 iterator __old_begin = __base::begin();
2055 iterator __i = __old_begin;
2056 if (__n > __pos)
2057 {
2058 for (size_type __m = __n - __pos; __m; --__m, --__base::__start_, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002059 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002060 __n = __pos;
2061 }
2062 if (__n > 0)
2063 {
2064 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2065 iterator __obn = __old_begin + __n;
2066 __move_construct_backward_and_check(__old_begin, __obn, __i, __vt);
2067 if (__n < __pos)
2068 __old_begin = __move_and_check(__obn, __old_begin + __pos, __old_begin, __vt);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002069 _VSTD::fill_n(__old_begin, __n, *__vt);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002070 }
2071 }
2072 else
2073 { // insert by shifting things forward
2074 size_type __back_capacity = __back_spare();
2075 if (__n > __back_capacity)
2076 __add_back_capacity(__n - __back_capacity);
2077 // __n <= __back_capacity
2078 size_type __old_n = __n;
2079 iterator __old_end = __base::end();
2080 iterator __i = __old_end;
2081 size_type __de = __base::size() - __pos;
2082 if (__n > __de)
2083 {
2084 for (size_type __m = __n - __de; __m; --__m, ++__i, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002085 __alloc_traits::construct(__a, _VSTD::addressof(*__i), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002086 __n = __de;
2087 }
2088 if (__n > 0)
2089 {
2090 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2091 iterator __oen = __old_end - __n;
2092 __move_construct_and_check(__oen, __old_end, __i, __vt);
2093 if (__n < __de)
2094 __old_end = __move_backward_and_check(__old_end - __de, __oen, __old_end, __vt);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002095 _VSTD::fill_n(__old_end - __n, __n, *__vt);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002096 }
2097 }
2098 return __base::begin() + __pos;
2099}
2100
2101template <class _Tp, class _Allocator>
2102template <class _InputIter>
2103typename deque<_Tp, _Allocator>::iterator
2104deque<_Tp, _Allocator>::insert(const_iterator __p, _InputIter __f, _InputIter __l,
2105 typename enable_if<__is_input_iterator<_InputIter>::value
Marshall Clow6b612cf2015-01-22 18:33:29 +00002106 &&!__is_forward_iterator<_InputIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002107{
2108 __split_buffer<value_type, allocator_type&> __buf(__base::__alloc());
2109 __buf.__construct_at_end(__f, __l);
2110 typedef typename __split_buffer<value_type, allocator_type&>::iterator __bi;
2111 return insert(__p, move_iterator<__bi>(__buf.begin()), move_iterator<__bi>(__buf.end()));
2112}
2113
2114template <class _Tp, class _Allocator>
Marshall Clow6b612cf2015-01-22 18:33:29 +00002115template <class _ForwardIterator>
2116typename deque<_Tp, _Allocator>::iterator
2117deque<_Tp, _Allocator>::insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l,
2118 typename enable_if<__is_forward_iterator<_ForwardIterator>::value
2119 &&!__is_bidirectional_iterator<_ForwardIterator>::value>::type*)
2120{
2121 size_type __n = _VSTD::distance(__f, __l);
2122 __split_buffer<value_type, allocator_type&> __buf(__n, 0, __base::__alloc());
2123 __buf.__construct_at_end(__f, __l);
2124 typedef typename __split_buffer<value_type, allocator_type&>::iterator __fwd;
2125 return insert(__p, move_iterator<__fwd>(__buf.begin()), move_iterator<__fwd>(__buf.end()));
2126}
2127
2128template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002129template <class _BiIter>
2130typename deque<_Tp, _Allocator>::iterator
2131deque<_Tp, _Allocator>::insert(const_iterator __p, _BiIter __f, _BiIter __l,
2132 typename enable_if<__is_bidirectional_iterator<_BiIter>::value>::type*)
2133{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002134 size_type __n = _VSTD::distance(__f, __l);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002135 size_type __pos = __p - __base::begin();
2136 size_type __to_end = __base::size() - __pos;
2137 allocator_type& __a = __base::__alloc();
2138 if (__pos < __to_end)
2139 { // insert by shifting things backward
2140 if (__n > __front_spare())
2141 __add_front_capacity(__n - __front_spare());
2142 // __n <= __front_spare()
2143 size_type __old_n = __n;
2144 iterator __old_begin = __base::begin();
2145 iterator __i = __old_begin;
2146 _BiIter __m = __f;
2147 if (__n > __pos)
2148 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002149 __m = __pos < __n / 2 ? _VSTD::prev(__l, __pos) : _VSTD::next(__f, __n - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002150 for (_BiIter __j = __m; __j != __f; --__base::__start_, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002151 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), *--__j);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002152 __n = __pos;
2153 }
2154 if (__n > 0)
2155 {
2156 iterator __obn = __old_begin + __n;
2157 for (iterator __j = __obn; __j != __old_begin;)
2158 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002159 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), _VSTD::move(*--__j));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002160 --__base::__start_;
2161 ++__base::size();
2162 }
2163 if (__n < __pos)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002164 __old_begin = _VSTD::move(__obn, __old_begin + __pos, __old_begin);
2165 _VSTD::copy(__m, __l, __old_begin);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002166 }
2167 }
2168 else
2169 { // insert by shifting things forward
2170 size_type __back_capacity = __back_spare();
2171 if (__n > __back_capacity)
2172 __add_back_capacity(__n - __back_capacity);
2173 // __n <= __back_capacity
2174 size_type __old_n = __n;
2175 iterator __old_end = __base::end();
2176 iterator __i = __old_end;
2177 _BiIter __m = __l;
2178 size_type __de = __base::size() - __pos;
2179 if (__n > __de)
2180 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002181 __m = __de < __n / 2 ? _VSTD::next(__f, __de) : _VSTD::prev(__l, __n - __de);
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002182 for (_BiIter __j = __m; __j != __l; ++__i, (void) ++__j, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002183 __alloc_traits::construct(__a, _VSTD::addressof(*__i), *__j);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002184 __n = __de;
2185 }
2186 if (__n > 0)
2187 {
2188 iterator __oen = __old_end - __n;
2189 for (iterator __j = __oen; __j != __old_end; ++__i, ++__j, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002190 __alloc_traits::construct(__a, _VSTD::addressof(*__i), _VSTD::move(*__j));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002191 if (__n < __de)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002192 __old_end = _VSTD::move_backward(__old_end - __de, __oen, __old_end);
2193 _VSTD::copy_backward(__f, __m, __old_end);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002194 }
2195 }
2196 return __base::begin() + __pos;
2197}
2198
2199template <class _Tp, class _Allocator>
2200template <class _InpIter>
2201void
2202deque<_Tp, _Allocator>::__append(_InpIter __f, _InpIter __l,
2203 typename enable_if<__is_input_iterator<_InpIter>::value &&
2204 !__is_forward_iterator<_InpIter>::value>::type*)
2205{
2206 for (; __f != __l; ++__f)
2207 push_back(*__f);
2208}
2209
2210template <class _Tp, class _Allocator>
2211template <class _ForIter>
2212void
2213deque<_Tp, _Allocator>::__append(_ForIter __f, _ForIter __l,
2214 typename enable_if<__is_forward_iterator<_ForIter>::value>::type*)
2215{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002216 size_type __n = _VSTD::distance(__f, __l);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002217 allocator_type& __a = __base::__alloc();
2218 size_type __back_capacity = __back_spare();
2219 if (__n > __back_capacity)
2220 __add_back_capacity(__n - __back_capacity);
2221 // __n <= __back_capacity
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002222 for (iterator __i = __base::end(); __f != __l; ++__i, (void) ++__f, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002223 __alloc_traits::construct(__a, _VSTD::addressof(*__i), *__f);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002224}
2225
2226template <class _Tp, class _Allocator>
2227void
2228deque<_Tp, _Allocator>::__append(size_type __n)
2229{
2230 allocator_type& __a = __base::__alloc();
2231 size_type __back_capacity = __back_spare();
2232 if (__n > __back_capacity)
2233 __add_back_capacity(__n - __back_capacity);
2234 // __n <= __back_capacity
2235 for (iterator __i = __base::end(); __n; --__n, ++__i, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002236 __alloc_traits::construct(__a, _VSTD::addressof(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002237}
2238
2239template <class _Tp, class _Allocator>
2240void
2241deque<_Tp, _Allocator>::__append(size_type __n, const value_type& __v)
2242{
2243 allocator_type& __a = __base::__alloc();
2244 size_type __back_capacity = __back_spare();
2245 if (__n > __back_capacity)
2246 __add_back_capacity(__n - __back_capacity);
2247 // __n <= __back_capacity
2248 for (iterator __i = __base::end(); __n; --__n, ++__i, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002249 __alloc_traits::construct(__a, _VSTD::addressof(*__i), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002250}
2251
2252// Create front capacity for one block of elements.
2253// Strong guarantee. Either do it or don't touch anything.
2254template <class _Tp, class _Allocator>
2255void
2256deque<_Tp, _Allocator>::__add_front_capacity()
2257{
2258 allocator_type& __a = __base::__alloc();
2259 if (__back_spare() >= __base::__block_size)
2260 {
2261 __base::__start_ += __base::__block_size;
2262 pointer __pt = __base::__map_.back();
2263 __base::__map_.pop_back();
2264 __base::__map_.push_front(__pt);
2265 }
2266 // Else if __base::__map_.size() < __base::__map_.capacity() then we need to allocate 1 buffer
2267 else if (__base::__map_.size() < __base::__map_.capacity())
2268 { // we can put the new buffer into the map, but don't shift things around
2269 // until all buffers are allocated. If we throw, we don't need to fix
2270 // anything up (any added buffers are undetectible)
2271 if (__base::__map_.__front_spare() > 0)
2272 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2273 else
2274 {
2275 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2276 // Done allocating, reorder capacity
2277 pointer __pt = __base::__map_.back();
2278 __base::__map_.pop_back();
2279 __base::__map_.push_front(__pt);
2280 }
2281 __base::__start_ = __base::__map_.size() == 1 ?
2282 __base::__block_size / 2 :
2283 __base::__start_ + __base::__block_size;
2284 }
2285 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2286 else
2287 {
2288 __split_buffer<pointer, typename __base::__pointer_allocator&>
2289 __buf(max<size_type>(2 * __base::__map_.capacity(), 1),
2290 0, __base::__map_.__alloc());
Marshall Clow5fd466b2015-03-09 17:08:51 +00002291
2292 typedef __allocator_destructor<_Allocator> _Dp;
2293 unique_ptr<pointer, _Dp> __hold(
2294 __alloc_traits::allocate(__a, __base::__block_size),
2295 _Dp(__a, __base::__block_size));
2296 __buf.push_back(__hold.get());
2297 __hold.release();
2298
Howard Hinnantc51e1022010-05-11 19:42:16 +00002299 for (typename __base::__map_pointer __i = __base::__map_.begin();
2300 __i != __base::__map_.end(); ++__i)
2301 __buf.push_back(*__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002302 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2303 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2304 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2305 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002306 __base::__start_ = __base::__map_.size() == 1 ?
2307 __base::__block_size / 2 :
2308 __base::__start_ + __base::__block_size;
2309 }
2310}
2311
2312// Create front capacity for __n elements.
2313// Strong guarantee. Either do it or don't touch anything.
2314template <class _Tp, class _Allocator>
2315void
2316deque<_Tp, _Allocator>::__add_front_capacity(size_type __n)
2317{
2318 allocator_type& __a = __base::__alloc();
2319 size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2320 // Number of unused blocks at back:
2321 size_type __back_capacity = __back_spare() / __base::__block_size;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002322 __back_capacity = _VSTD::min(__back_capacity, __nb); // don't take more than you need
Howard Hinnantc51e1022010-05-11 19:42:16 +00002323 __nb -= __back_capacity; // number of blocks need to allocate
2324 // If __nb == 0, then we have sufficient capacity.
2325 if (__nb == 0)
2326 {
2327 __base::__start_ += __base::__block_size * __back_capacity;
2328 for (; __back_capacity > 0; --__back_capacity)
2329 {
2330 pointer __pt = __base::__map_.back();
2331 __base::__map_.pop_back();
2332 __base::__map_.push_front(__pt);
2333 }
2334 }
2335 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2336 else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2337 { // we can put the new buffers into the map, but don't shift things around
2338 // until all buffers are allocated. If we throw, we don't need to fix
2339 // anything up (any added buffers are undetectible)
2340 for (; __nb > 0; --__nb, __base::__start_ += __base::__block_size - (__base::__map_.size() == 1))
2341 {
2342 if (__base::__map_.__front_spare() == 0)
2343 break;
2344 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2345 }
2346 for (; __nb > 0; --__nb, ++__back_capacity)
2347 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2348 // Done allocating, reorder capacity
2349 __base::__start_ += __back_capacity * __base::__block_size;
2350 for (; __back_capacity > 0; --__back_capacity)
2351 {
2352 pointer __pt = __base::__map_.back();
2353 __base::__map_.pop_back();
2354 __base::__map_.push_front(__pt);
2355 }
2356 }
2357 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2358 else
2359 {
2360 size_type __ds = (__nb + __back_capacity) * __base::__block_size - __base::__map_.empty();
2361 __split_buffer<pointer, typename __base::__pointer_allocator&>
2362 __buf(max<size_type>(2* __base::__map_.capacity(),
2363 __nb + __base::__map_.size()),
2364 0, __base::__map_.__alloc());
2365#ifndef _LIBCPP_NO_EXCEPTIONS
2366 try
2367 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002368#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002369 for (; __nb > 0; --__nb)
2370 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2371#ifndef _LIBCPP_NO_EXCEPTIONS
2372 }
2373 catch (...)
2374 {
2375 for (typename __base::__map_pointer __i = __buf.begin();
2376 __i != __buf.end(); ++__i)
2377 __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2378 throw;
2379 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002380#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002381 for (; __back_capacity > 0; --__back_capacity)
2382 {
2383 __buf.push_back(__base::__map_.back());
2384 __base::__map_.pop_back();
2385 }
2386 for (typename __base::__map_pointer __i = __base::__map_.begin();
2387 __i != __base::__map_.end(); ++__i)
2388 __buf.push_back(*__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002389 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2390 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2391 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2392 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002393 __base::__start_ += __ds;
2394 }
2395}
2396
2397// Create back capacity for one block of elements.
2398// Strong guarantee. Either do it or don't touch anything.
2399template <class _Tp, class _Allocator>
2400void
2401deque<_Tp, _Allocator>::__add_back_capacity()
2402{
2403 allocator_type& __a = __base::__alloc();
2404 if (__front_spare() >= __base::__block_size)
2405 {
2406 __base::__start_ -= __base::__block_size;
2407 pointer __pt = __base::__map_.front();
2408 __base::__map_.pop_front();
2409 __base::__map_.push_back(__pt);
2410 }
2411 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2412 else if (__base::__map_.size() < __base::__map_.capacity())
2413 { // we can put the new buffer into the map, but don't shift things around
2414 // until it is allocated. If we throw, we don't need to fix
2415 // anything up (any added buffers are undetectible)
2416 if (__base::__map_.__back_spare() != 0)
2417 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2418 else
2419 {
2420 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2421 // Done allocating, reorder capacity
2422 pointer __pt = __base::__map_.front();
2423 __base::__map_.pop_front();
2424 __base::__map_.push_back(__pt);
2425 }
2426 }
2427 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2428 else
2429 {
2430 __split_buffer<pointer, typename __base::__pointer_allocator&>
2431 __buf(max<size_type>(2* __base::__map_.capacity(), 1),
2432 __base::__map_.size(),
2433 __base::__map_.__alloc());
Marshall Clow5fd466b2015-03-09 17:08:51 +00002434
2435 typedef __allocator_destructor<_Allocator> _Dp;
2436 unique_ptr<pointer, _Dp> __hold(
2437 __alloc_traits::allocate(__a, __base::__block_size),
2438 _Dp(__a, __base::__block_size));
2439 __buf.push_back(__hold.get());
2440 __hold.release();
2441
Howard Hinnantc51e1022010-05-11 19:42:16 +00002442 for (typename __base::__map_pointer __i = __base::__map_.end();
2443 __i != __base::__map_.begin();)
2444 __buf.push_front(*--__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002445 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2446 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2447 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2448 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002449 }
2450}
2451
2452// Create back capacity for __n elements.
2453// Strong guarantee. Either do it or don't touch anything.
2454template <class _Tp, class _Allocator>
2455void
2456deque<_Tp, _Allocator>::__add_back_capacity(size_type __n)
2457{
2458 allocator_type& __a = __base::__alloc();
2459 size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2460 // Number of unused blocks at front:
2461 size_type __front_capacity = __front_spare() / __base::__block_size;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002462 __front_capacity = _VSTD::min(__front_capacity, __nb); // don't take more than you need
Howard Hinnantc51e1022010-05-11 19:42:16 +00002463 __nb -= __front_capacity; // number of blocks need to allocate
2464 // If __nb == 0, then we have sufficient capacity.
2465 if (__nb == 0)
2466 {
2467 __base::__start_ -= __base::__block_size * __front_capacity;
2468 for (; __front_capacity > 0; --__front_capacity)
2469 {
2470 pointer __pt = __base::__map_.front();
2471 __base::__map_.pop_front();
2472 __base::__map_.push_back(__pt);
2473 }
2474 }
2475 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2476 else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2477 { // we can put the new buffers into the map, but don't shift things around
2478 // until all buffers are allocated. If we throw, we don't need to fix
2479 // anything up (any added buffers are undetectible)
2480 for (; __nb > 0; --__nb)
2481 {
2482 if (__base::__map_.__back_spare() == 0)
2483 break;
2484 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2485 }
2486 for (; __nb > 0; --__nb, ++__front_capacity, __base::__start_ +=
2487 __base::__block_size - (__base::__map_.size() == 1))
2488 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2489 // Done allocating, reorder capacity
2490 __base::__start_ -= __base::__block_size * __front_capacity;
2491 for (; __front_capacity > 0; --__front_capacity)
2492 {
2493 pointer __pt = __base::__map_.front();
2494 __base::__map_.pop_front();
2495 __base::__map_.push_back(__pt);
2496 }
2497 }
2498 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2499 else
2500 {
2501 size_type __ds = __front_capacity * __base::__block_size;
2502 __split_buffer<pointer, typename __base::__pointer_allocator&>
2503 __buf(max<size_type>(2* __base::__map_.capacity(),
2504 __nb + __base::__map_.size()),
2505 __base::__map_.size() - __front_capacity,
2506 __base::__map_.__alloc());
2507#ifndef _LIBCPP_NO_EXCEPTIONS
2508 try
2509 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002510#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002511 for (; __nb > 0; --__nb)
2512 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2513#ifndef _LIBCPP_NO_EXCEPTIONS
2514 }
2515 catch (...)
2516 {
2517 for (typename __base::__map_pointer __i = __buf.begin();
2518 __i != __buf.end(); ++__i)
2519 __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2520 throw;
2521 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002522#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002523 for (; __front_capacity > 0; --__front_capacity)
2524 {
2525 __buf.push_back(__base::__map_.front());
2526 __base::__map_.pop_front();
2527 }
2528 for (typename __base::__map_pointer __i = __base::__map_.end();
2529 __i != __base::__map_.begin();)
2530 __buf.push_front(*--__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002531 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2532 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2533 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2534 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002535 __base::__start_ -= __ds;
2536 }
2537}
2538
2539template <class _Tp, class _Allocator>
2540void
2541deque<_Tp, _Allocator>::pop_front()
2542{
2543 allocator_type& __a = __base::__alloc();
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002544 __alloc_traits::destroy(__a, __to_raw_pointer(*(__base::__map_.begin() +
2545 __base::__start_ / __base::__block_size) +
2546 __base::__start_ % __base::__block_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002547 --__base::size();
2548 if (++__base::__start_ >= 2 * __base::__block_size)
2549 {
2550 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2551 __base::__map_.pop_front();
2552 __base::__start_ -= __base::__block_size;
2553 }
2554}
2555
2556template <class _Tp, class _Allocator>
2557void
2558deque<_Tp, _Allocator>::pop_back()
2559{
2560 allocator_type& __a = __base::__alloc();
2561 size_type __p = __base::size() + __base::__start_ - 1;
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002562 __alloc_traits::destroy(__a, __to_raw_pointer(*(__base::__map_.begin() +
2563 __p / __base::__block_size) +
2564 __p % __base::__block_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002565 --__base::size();
2566 if (__back_spare() >= 2 * __base::__block_size)
2567 {
2568 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2569 __base::__map_.pop_back();
2570 }
2571}
2572
2573// move assign [__f, __l) to [__r, __r + (__l-__f)).
2574// If __vt points into [__f, __l), then subtract (__f - __r) from __vt.
2575template <class _Tp, class _Allocator>
2576typename deque<_Tp, _Allocator>::iterator
2577deque<_Tp, _Allocator>::__move_and_check(iterator __f, iterator __l, iterator __r,
2578 const_pointer& __vt)
2579{
2580 // as if
2581 // for (; __f != __l; ++__f, ++__r)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002582 // *__r = _VSTD::move(*__f);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002583 difference_type __n = __l - __f;
2584 while (__n > 0)
2585 {
2586 pointer __fb = __f.__ptr_;
2587 pointer __fe = *__f.__m_iter_ + __base::__block_size;
2588 difference_type __bs = __fe - __fb;
2589 if (__bs > __n)
2590 {
2591 __bs = __n;
2592 __fe = __fb + __bs;
2593 }
2594 if (__fb <= __vt && __vt < __fe)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002595 __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) -= __f - __r).__ptr_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002596 __r = _VSTD::move(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002597 __n -= __bs;
2598 __f += __bs;
2599 }
2600 return __r;
2601}
2602
2603// move assign [__f, __l) to [__r - (__l-__f), __r) backwards.
2604// If __vt points into [__f, __l), then add (__r - __l) to __vt.
2605template <class _Tp, class _Allocator>
2606typename deque<_Tp, _Allocator>::iterator
2607deque<_Tp, _Allocator>::__move_backward_and_check(iterator __f, iterator __l, iterator __r,
2608 const_pointer& __vt)
2609{
2610 // as if
2611 // while (__f != __l)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002612 // *--__r = _VSTD::move(*--__l);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002613 difference_type __n = __l - __f;
2614 while (__n > 0)
2615 {
2616 --__l;
2617 pointer __lb = *__l.__m_iter_;
2618 pointer __le = __l.__ptr_ + 1;
2619 difference_type __bs = __le - __lb;
2620 if (__bs > __n)
2621 {
2622 __bs = __n;
2623 __lb = __le - __bs;
2624 }
2625 if (__lb <= __vt && __vt < __le)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002626 __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) += __r - __l - 1).__ptr_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002627 __r = _VSTD::move_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002628 __n -= __bs;
2629 __l -= __bs - 1;
2630 }
2631 return __r;
2632}
2633
2634// move construct [__f, __l) to [__r, __r + (__l-__f)).
2635// If __vt points into [__f, __l), then add (__r - __f) to __vt.
2636template <class _Tp, class _Allocator>
2637void
2638deque<_Tp, _Allocator>::__move_construct_and_check(iterator __f, iterator __l,
2639 iterator __r, const_pointer& __vt)
2640{
2641 allocator_type& __a = __base::__alloc();
2642 // as if
2643 // for (; __f != __l; ++__r, ++__f, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002644 // __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__f));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002645 difference_type __n = __l - __f;
2646 while (__n > 0)
2647 {
2648 pointer __fb = __f.__ptr_;
2649 pointer __fe = *__f.__m_iter_ + __base::__block_size;
2650 difference_type __bs = __fe - __fb;
2651 if (__bs > __n)
2652 {
2653 __bs = __n;
2654 __fe = __fb + __bs;
2655 }
2656 if (__fb <= __vt && __vt < __fe)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002657 __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) += __r - __f).__ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002658 for (; __fb != __fe; ++__fb, ++__r, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002659 __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__fb));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002660 __n -= __bs;
2661 __f += __bs;
2662 }
2663}
2664
2665// move construct [__f, __l) to [__r - (__l-__f), __r) backwards.
2666// If __vt points into [__f, __l), then subtract (__l - __r) from __vt.
2667template <class _Tp, class _Allocator>
2668void
2669deque<_Tp, _Allocator>::__move_construct_backward_and_check(iterator __f, iterator __l,
2670 iterator __r, const_pointer& __vt)
2671{
2672 allocator_type& __a = __base::__alloc();
2673 // as if
2674 // for (iterator __j = __l; __j != __f;)
2675 // {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002676 // __alloc_traitsconstruct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__j));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002677 // --__base::__start_;
2678 // ++__base::size();
2679 // }
2680 difference_type __n = __l - __f;
2681 while (__n > 0)
2682 {
2683 --__l;
2684 pointer __lb = *__l.__m_iter_;
2685 pointer __le = __l.__ptr_ + 1;
2686 difference_type __bs = __le - __lb;
2687 if (__bs > __n)
2688 {
2689 __bs = __n;
2690 __lb = __le - __bs;
2691 }
2692 if (__lb <= __vt && __vt < __le)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002693 __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) -= __l - __r + 1).__ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002694 while (__le != __lb)
2695 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002696 __alloc_traits::construct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__le));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002697 --__base::__start_;
2698 ++__base::size();
2699 }
2700 __n -= __bs;
2701 __l -= __bs - 1;
2702 }
2703}
2704
2705template <class _Tp, class _Allocator>
2706typename deque<_Tp, _Allocator>::iterator
2707deque<_Tp, _Allocator>::erase(const_iterator __f)
2708{
2709 difference_type __n = 1;
2710 iterator __b = __base::begin();
2711 difference_type __pos = __f - __b;
2712 iterator __p = __b + __pos;
2713 allocator_type& __a = __base::__alloc();
2714 if (__pos < (__base::size() - 1) / 2)
2715 { // erase from front
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002716 _VSTD::move_backward(__b, __p, _VSTD::next(__p));
2717 __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002718 --__base::size();
2719 ++__base::__start_;
2720 if (__front_spare() >= 2 * __base::__block_size)
2721 {
2722 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2723 __base::__map_.pop_front();
2724 __base::__start_ -= __base::__block_size;
2725 }
2726 }
2727 else
2728 { // erase from back
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002729 iterator __i = _VSTD::move(_VSTD::next(__p), __base::end(), __p);
2730 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002731 --__base::size();
2732 if (__back_spare() >= 2 * __base::__block_size)
2733 {
2734 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2735 __base::__map_.pop_back();
2736 }
2737 }
2738 return __base::begin() + __pos;
2739}
2740
2741template <class _Tp, class _Allocator>
2742typename deque<_Tp, _Allocator>::iterator
2743deque<_Tp, _Allocator>::erase(const_iterator __f, const_iterator __l)
2744{
2745 difference_type __n = __l - __f;
2746 iterator __b = __base::begin();
2747 difference_type __pos = __f - __b;
2748 iterator __p = __b + __pos;
2749 if (__n > 0)
2750 {
2751 allocator_type& __a = __base::__alloc();
2752 if (__pos < (__base::size() - __n) / 2)
2753 { // erase from front
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002754 iterator __i = _VSTD::move_backward(__b, __p, __p + __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002755 for (; __b != __i; ++__b)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002756 __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002757 __base::size() -= __n;
2758 __base::__start_ += __n;
2759 while (__front_spare() >= 2 * __base::__block_size)
2760 {
2761 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2762 __base::__map_.pop_front();
2763 __base::__start_ -= __base::__block_size;
2764 }
2765 }
2766 else
2767 { // erase from back
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002768 iterator __i = _VSTD::move(__p + __n, __base::end(), __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002769 for (iterator __e = __base::end(); __i != __e; ++__i)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002770 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002771 __base::size() -= __n;
2772 while (__back_spare() >= 2 * __base::__block_size)
2773 {
2774 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2775 __base::__map_.pop_back();
2776 }
2777 }
2778 }
2779 return __base::begin() + __pos;
2780}
2781
2782template <class _Tp, class _Allocator>
2783void
2784deque<_Tp, _Allocator>::__erase_to_end(const_iterator __f)
2785{
2786 iterator __e = __base::end();
2787 difference_type __n = __e - __f;
2788 if (__n > 0)
2789 {
2790 allocator_type& __a = __base::__alloc();
2791 iterator __b = __base::begin();
2792 difference_type __pos = __f - __b;
2793 for (iterator __p = __b + __pos; __p != __e; ++__p)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002794 __alloc_traits::destroy(__a, _VSTD::addressof(*__p));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002795 __base::size() -= __n;
2796 while (__back_spare() >= 2 * __base::__block_size)
2797 {
2798 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2799 __base::__map_.pop_back();
2800 }
2801 }
2802}
2803
2804template <class _Tp, class _Allocator>
Howard Hinnant874ad9a2010-09-21 21:28:23 +00002805inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002806void
2807deque<_Tp, _Allocator>::swap(deque& __c)
Howard Hinnantca2fc222011-06-02 20:00:14 +00002808 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2809 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002810{
2811 __base::swap(__c);
2812}
2813
2814template <class _Tp, class _Allocator>
Howard Hinnant874ad9a2010-09-21 21:28:23 +00002815inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002816void
Howard Hinnantda2df912011-06-02 16:10:22 +00002817deque<_Tp, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002818{
2819 __base::clear();
2820}
2821
2822template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002823inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002824bool
2825operator==(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2826{
2827 const typename deque<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002828 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002829}
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 return !(__x == __y);
2837}
2838
2839template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002840inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002841bool
2842operator< (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2843{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002844 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002845}
2846
2847template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002848inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002849bool
2850operator> (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2851{
2852 return __y < __x;
2853}
2854
2855template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002856inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002857bool
2858operator>=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2859{
2860 return !(__x < __y);
2861}
2862
2863template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002864inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002865bool
2866operator<=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2867{
2868 return !(__y < __x);
2869}
2870
2871template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002872inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002873void
2874swap(deque<_Tp, _Allocator>& __x, deque<_Tp, _Allocator>& __y)
Howard Hinnantca2fc222011-06-02 20:00:14 +00002875 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002876{
2877 __x.swap(__y);
2878}
2879
2880_LIBCPP_END_NAMESPACE_STD
2881
2882#endif // _LIBCPP_DEQUE