blob: 9b256b74d4f2c697ce7ccd265d908122e429416c [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------------- deque -----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-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 Hinnantbc8d3f92010-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 Hinnant009b2c42011-06-03 15:16:49 +000041 deque() noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000042 explicit deque(const allocator_type& a);
43 explicit deque(size_type n);
Marshall Clowe00f53b2013-09-09 18:19:45 +000044 explicit deque(size_type n, const allocator_type& a); // C++14
Howard Hinnantbc8d3f92010-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 Hinnant18884f42011-06-02 21:38:57 +000052 deque(deque&& c)
53 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-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 Hinnant18884f42011-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 Hinnantbc8d3f92010-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 Hinnanta12beb32011-06-02 16:10:22 +000071 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000072
73 // iterators:
74
Howard Hinnanta12beb32011-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 Hinnantbc8d3f92010-05-11 19:42:16 +000079
Howard Hinnanta12beb32011-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 Hinnantbc8d3f92010-05-11 19:42:16 +000084
Howard Hinnanta12beb32011-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 Hinnantbc8d3f92010-05-11 19:42:16 +000089
90 // capacity:
Howard Hinnanta12beb32011-06-02 16:10:22 +000091 size_type size() const noexcept;
92 size_type max_size() const noexcept;
Howard Hinnantbc8d3f92010-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 Hinnanta12beb32011-06-02 16:10:22 +000096 bool empty() const noexcept;
Howard Hinnantbc8d3f92010-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>
120 iterator insert (const_iterator p, InputIterator f, InputIterator l);
121 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 Hinnant18884f42011-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 Hinnanta12beb32011-06-02 16:10:22 +0000129 void clear() noexcept;
Howard Hinnantbc8d3f92010-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 Hinnantc5607272011-06-03 17:30:28 +0000147 void swap(deque<T,Allocator>& x, deque<T,Allocator>& y)
148 noexcept(noexcept(x.swap(y)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000149
150} // std
151
152*/
153
Howard Hinnant08e17472011-10-17 20:05:10 +0000154#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000155#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000156#endif
Howard Hinnantbc8d3f92010-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 Hinnant66c6f972011-11-29 16:45:27 +0000166#include <__undef_min_max>
167
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000168_LIBCPP_BEGIN_NAMESPACE_STD
169
170template <class _Tp, class _Allocator> class __deque_base;
171
172template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
173 class _DiffType, _DiffType _BlockSize>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000174class _LIBCPP_TYPE_VIS_ONLY __deque_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000175
176template <class _RAIter,
177 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
178__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
179copy(_RAIter __f,
180 _RAIter __l,
181 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
182 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
183
184template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
185 class _OutputIterator>
186_OutputIterator
187copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
188 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
189 _OutputIterator __r);
190
191template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
192 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
193__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
194copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
195 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
196 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
197
198template <class _RAIter,
199 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
200__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
201copy_backward(_RAIter __f,
202 _RAIter __l,
203 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
204 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
205
206template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
207 class _OutputIterator>
208_OutputIterator
209copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
210 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
211 _OutputIterator __r);
212
213template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
214 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
215__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
216copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
217 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
218 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
219
220template <class _RAIter,
221 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
222__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
223move(_RAIter __f,
224 _RAIter __l,
225 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
226 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
227
228template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
229 class _OutputIterator>
230_OutputIterator
231move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
232 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
233 _OutputIterator __r);
234
235template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
236 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
237__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
238move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
239 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
240 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
241
242template <class _RAIter,
243 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
244__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
245move_backward(_RAIter __f,
246 _RAIter __l,
247 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
248 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
249
250template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
251 class _OutputIterator>
252_OutputIterator
253move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
254 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
255 _OutputIterator __r);
256
257template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
258 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
259__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
260move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
261 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
262 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
263
264template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
265 class _DiffType, _DiffType _BlockSize>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000266class _LIBCPP_TYPE_VIS_ONLY __deque_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000267{
268 typedef _MapPointer __map_iterator;
269public:
270 typedef _Pointer pointer;
271 typedef _DiffType difference_type;
272private:
273 __map_iterator __m_iter_;
274 pointer __ptr_;
275
276 static const difference_type __block_size = _BlockSize;
277public:
278 typedef _ValueType value_type;
279 typedef random_access_iterator_tag iterator_category;
280 typedef _Reference reference;
281
Marshall Clow5a11f942013-08-06 16:14:36 +0000282 _LIBCPP_INLINE_VISIBILITY __deque_iterator() _NOEXCEPT
283#if _LIBCPP_STD_VER > 11
284 : __m_iter_(nullptr), __ptr_(nullptr)
285#endif
286 {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000287
Howard Hinnant99968442011-11-29 18:15:50 +0000288 template <class _Pp, class _Rp, class _MP>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000289 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +0000290 __deque_iterator(const __deque_iterator<value_type, _Pp, _Rp, _MP, difference_type, __block_size>& __it,
291 typename enable_if<is_convertible<_Pp, pointer>::value>::type* = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000292 : __m_iter_(__it.__m_iter_), __ptr_(__it.__ptr_) {}
293
294 _LIBCPP_INLINE_VISIBILITY reference operator*() const {return *__ptr_;}
295 _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return __ptr_;}
296
297 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator++()
298 {
299 if (++__ptr_ - *__m_iter_ == __block_size)
300 {
301 ++__m_iter_;
302 __ptr_ = *__m_iter_;
303 }
304 return *this;
305 }
306
307 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator++(int)
308 {
309 __deque_iterator __tmp = *this;
310 ++(*this);
311 return __tmp;
312 }
313
314 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator--()
315 {
316 if (__ptr_ == *__m_iter_)
317 {
318 --__m_iter_;
319 __ptr_ = *__m_iter_ + __block_size;
320 }
321 --__ptr_;
322 return *this;
323 }
324
325 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator--(int)
326 {
327 __deque_iterator __tmp = *this;
328 --(*this);
329 return __tmp;
330 }
331
332 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator+=(difference_type __n)
333 {
334 if (__n != 0)
335 {
336 __n += __ptr_ - *__m_iter_;
337 if (__n > 0)
338 {
339 __m_iter_ += __n / __block_size;
340 __ptr_ = *__m_iter_ + __n % __block_size;
341 }
342 else // (__n < 0)
343 {
344 difference_type __z = __block_size - 1 - __n;
345 __m_iter_ -= __z / __block_size;
346 __ptr_ = *__m_iter_ + (__block_size - 1 - __z % __block_size);
347 }
348 }
349 return *this;
350 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000351
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000352 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator-=(difference_type __n)
353 {
354 return *this += -__n;
355 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000356
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000357 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator+(difference_type __n) const
358 {
359 __deque_iterator __t(*this);
360 __t += __n;
361 return __t;
362 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000363
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000364 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator-(difference_type __n) const
365 {
366 __deque_iterator __t(*this);
367 __t -= __n;
368 return __t;
369 }
370
371 _LIBCPP_INLINE_VISIBILITY
372 friend __deque_iterator operator+(difference_type __n, const __deque_iterator& __it)
373 {return __it + __n;}
374
375 _LIBCPP_INLINE_VISIBILITY
376 friend difference_type operator-(const __deque_iterator& __x, const __deque_iterator& __y)
377 {
378 if (__x != __y)
379 return (__x.__m_iter_ - __y.__m_iter_) * __block_size
380 + (__x.__ptr_ - *__x.__m_iter_)
381 - (__y.__ptr_ - *__y.__m_iter_);
382 return 0;
383 }
384
385 _LIBCPP_INLINE_VISIBILITY reference operator[](difference_type __n) const
386 {return *(*this + __n);}
387
388 _LIBCPP_INLINE_VISIBILITY friend
389 bool operator==(const __deque_iterator& __x, const __deque_iterator& __y)
390 {return __x.__ptr_ == __y.__ptr_;}
391
392 _LIBCPP_INLINE_VISIBILITY friend
393 bool operator!=(const __deque_iterator& __x, const __deque_iterator& __y)
394 {return !(__x == __y);}
395
396 _LIBCPP_INLINE_VISIBILITY friend
397 bool operator<(const __deque_iterator& __x, const __deque_iterator& __y)
398 {return __x.__m_iter_ < __y.__m_iter_ ||
399 (__x.__m_iter_ == __y.__m_iter_ && __x.__ptr_ < __y.__ptr_);}
400
401 _LIBCPP_INLINE_VISIBILITY friend
402 bool operator>(const __deque_iterator& __x, const __deque_iterator& __y)
403 {return __y < __x;}
404
405 _LIBCPP_INLINE_VISIBILITY friend
406 bool operator<=(const __deque_iterator& __x, const __deque_iterator& __y)
407 {return !(__y < __x);}
408
409 _LIBCPP_INLINE_VISIBILITY friend
410 bool operator>=(const __deque_iterator& __x, const __deque_iterator& __y)
411 {return !(__x < __y);}
412
413private:
Howard Hinnanta12beb32011-06-02 16:10:22 +0000414 _LIBCPP_INLINE_VISIBILITY __deque_iterator(__map_iterator __m, pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000415 : __m_iter_(__m), __ptr_(__p) {}
416
Howard Hinnant99968442011-11-29 18:15:50 +0000417 template <class _Tp, class _Ap> friend class __deque_base;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000418 template <class _Tp, class _Ap> friend class _LIBCPP_TYPE_VIS_ONLY deque;
Howard Hinnant99968442011-11-29 18:15:50 +0000419 template <class _Vp, class _Pp, class _Rp, class _MP, class _Dp, _Dp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000420 friend class _LIBCPP_TYPE_VIS_ONLY __deque_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000421
422 template <class _RAIter,
423 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
424 friend
425 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
426 copy(_RAIter __f,
427 _RAIter __l,
428 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
429 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
430
431 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
432 class _OutputIterator>
433 friend
434 _OutputIterator
435 copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
436 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
437 _OutputIterator __r);
438
439 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
440 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
441 friend
442 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
443 copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
444 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
445 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
446
447 template <class _RAIter,
448 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
449 friend
450 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
451 copy_backward(_RAIter __f,
452 _RAIter __l,
453 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
454 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
455
456 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
457 class _OutputIterator>
458 friend
459 _OutputIterator
460 copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
461 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
462 _OutputIterator __r);
463
464 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
465 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
466 friend
467 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
468 copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
469 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
470 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
471
472 template <class _RAIter,
473 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
474 friend
475 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
476 move(_RAIter __f,
477 _RAIter __l,
478 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
479 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
480
481 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
482 class _OutputIterator>
483 friend
484 _OutputIterator
485 move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
486 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
487 _OutputIterator __r);
488
489 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
490 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
491 friend
492 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
493 move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
494 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
495 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
496
497 template <class _RAIter,
498 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
499 friend
500 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
501 move_backward(_RAIter __f,
502 _RAIter __l,
503 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
504 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
505
506 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
507 class _OutputIterator>
508 friend
509 _OutputIterator
510 move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
511 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
512 _OutputIterator __r);
513
514 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
515 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
516 friend
517 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
518 move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
519 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
520 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
521};
522
523// copy
524
525template <class _RAIter,
526 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
527__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
528copy(_RAIter __f,
529 _RAIter __l,
530 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
531 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
532{
533 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
534 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
535 while (__f != __l)
536 {
537 pointer __rb = __r.__ptr_;
538 pointer __re = *__r.__m_iter_ + _B2;
539 difference_type __bs = __re - __rb;
540 difference_type __n = __l - __f;
541 _RAIter __m = __l;
542 if (__n > __bs)
543 {
544 __n = __bs;
545 __m = __f + __n;
546 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000547 _VSTD::copy(__f, __m, __rb);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000548 __f = __m;
549 __r += __n;
550 }
551 return __r;
552}
553
554template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
555 class _OutputIterator>
556_OutputIterator
557copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
558 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
559 _OutputIterator __r)
560{
561 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
562 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
563 difference_type __n = __l - __f;
564 while (__n > 0)
565 {
566 pointer __fb = __f.__ptr_;
567 pointer __fe = *__f.__m_iter_ + _B1;
568 difference_type __bs = __fe - __fb;
569 if (__bs > __n)
570 {
571 __bs = __n;
572 __fe = __fb + __bs;
573 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000574 __r = _VSTD::copy(__fb, __fe, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000575 __n -= __bs;
576 __f += __bs;
577 }
578 return __r;
579}
580
581template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
582 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
583__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
584copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
585 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
586 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
587{
588 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
589 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
590 difference_type __n = __l - __f;
591 while (__n > 0)
592 {
593 pointer __fb = __f.__ptr_;
594 pointer __fe = *__f.__m_iter_ + _B1;
595 difference_type __bs = __fe - __fb;
596 if (__bs > __n)
597 {
598 __bs = __n;
599 __fe = __fb + __bs;
600 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000601 __r = _VSTD::copy(__fb, __fe, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000602 __n -= __bs;
603 __f += __bs;
604 }
605 return __r;
606}
607
608// copy_backward
609
610template <class _RAIter,
611 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
612__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
613copy_backward(_RAIter __f,
614 _RAIter __l,
615 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
616 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
617{
618 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
619 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
620 while (__f != __l)
621 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000622 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _VSTD::prev(__r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000623 pointer __rb = *__rp.__m_iter_;
624 pointer __re = __rp.__ptr_ + 1;
625 difference_type __bs = __re - __rb;
626 difference_type __n = __l - __f;
627 _RAIter __m = __f;
628 if (__n > __bs)
629 {
630 __n = __bs;
631 __m = __l - __n;
632 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000633 _VSTD::copy_backward(__m, __l, __re);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000634 __l = __m;
635 __r -= __n;
636 }
637 return __r;
638}
639
640template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
641 class _OutputIterator>
642_OutputIterator
643copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
644 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
645 _OutputIterator __r)
646{
647 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
648 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
649 difference_type __n = __l - __f;
650 while (__n > 0)
651 {
652 --__l;
653 pointer __lb = *__l.__m_iter_;
654 pointer __le = __l.__ptr_ + 1;
655 difference_type __bs = __le - __lb;
656 if (__bs > __n)
657 {
658 __bs = __n;
659 __lb = __le - __bs;
660 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000661 __r = _VSTD::copy_backward(__lb, __le, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000662 __n -= __bs;
663 __l -= __bs - 1;
664 }
665 return __r;
666}
667
668template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
669 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
670__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
671copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
672 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
673 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
674{
675 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
676 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
677 difference_type __n = __l - __f;
678 while (__n > 0)
679 {
680 --__l;
681 pointer __lb = *__l.__m_iter_;
682 pointer __le = __l.__ptr_ + 1;
683 difference_type __bs = __le - __lb;
684 if (__bs > __n)
685 {
686 __bs = __n;
687 __lb = __le - __bs;
688 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000689 __r = _VSTD::copy_backward(__lb, __le, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000690 __n -= __bs;
691 __l -= __bs - 1;
692 }
693 return __r;
694}
695
696// move
697
698template <class _RAIter,
699 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
700__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
701move(_RAIter __f,
702 _RAIter __l,
703 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
704 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
705{
706 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
707 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
708 while (__f != __l)
709 {
710 pointer __rb = __r.__ptr_;
711 pointer __re = *__r.__m_iter_ + _B2;
712 difference_type __bs = __re - __rb;
713 difference_type __n = __l - __f;
714 _RAIter __m = __l;
715 if (__n > __bs)
716 {
717 __n = __bs;
718 __m = __f + __n;
719 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000720 _VSTD::move(__f, __m, __rb);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000721 __f = __m;
722 __r += __n;
723 }
724 return __r;
725}
726
727template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
728 class _OutputIterator>
729_OutputIterator
730move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
731 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
732 _OutputIterator __r)
733{
734 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
735 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
736 difference_type __n = __l - __f;
737 while (__n > 0)
738 {
739 pointer __fb = __f.__ptr_;
740 pointer __fe = *__f.__m_iter_ + _B1;
741 difference_type __bs = __fe - __fb;
742 if (__bs > __n)
743 {
744 __bs = __n;
745 __fe = __fb + __bs;
746 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000747 __r = _VSTD::move(__fb, __fe, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000748 __n -= __bs;
749 __f += __bs;
750 }
751 return __r;
752}
753
754template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
755 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
756__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
757move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
758 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
759 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
760{
761 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
762 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
763 difference_type __n = __l - __f;
764 while (__n > 0)
765 {
766 pointer __fb = __f.__ptr_;
767 pointer __fe = *__f.__m_iter_ + _B1;
768 difference_type __bs = __fe - __fb;
769 if (__bs > __n)
770 {
771 __bs = __n;
772 __fe = __fb + __bs;
773 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000774 __r = _VSTD::move(__fb, __fe, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000775 __n -= __bs;
776 __f += __bs;
777 }
778 return __r;
779}
780
781// move_backward
782
783template <class _RAIter,
784 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
785__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
786move_backward(_RAIter __f,
787 _RAIter __l,
788 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
789 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
790{
791 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
792 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
793 while (__f != __l)
794 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000795 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _VSTD::prev(__r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000796 pointer __rb = *__rp.__m_iter_;
797 pointer __re = __rp.__ptr_ + 1;
798 difference_type __bs = __re - __rb;
799 difference_type __n = __l - __f;
800 _RAIter __m = __f;
801 if (__n > __bs)
802 {
803 __n = __bs;
804 __m = __l - __n;
805 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000806 _VSTD::move_backward(__m, __l, __re);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000807 __l = __m;
808 __r -= __n;
809 }
810 return __r;
811}
812
813template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
814 class _OutputIterator>
815_OutputIterator
816move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
817 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
818 _OutputIterator __r)
819{
820 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
821 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
822 difference_type __n = __l - __f;
823 while (__n > 0)
824 {
825 --__l;
826 pointer __lb = *__l.__m_iter_;
827 pointer __le = __l.__ptr_ + 1;
828 difference_type __bs = __le - __lb;
829 if (__bs > __n)
830 {
831 __bs = __n;
832 __lb = __le - __bs;
833 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000834 __r = _VSTD::move_backward(__lb, __le, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000835 __n -= __bs;
836 __l -= __bs - 1;
837 }
838 return __r;
839}
840
841template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
842 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
843__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
844move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
845 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
846 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
847{
848 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
849 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
850 difference_type __n = __l - __f;
851 while (__n > 0)
852 {
853 --__l;
854 pointer __lb = *__l.__m_iter_;
855 pointer __le = __l.__ptr_ + 1;
856 difference_type __bs = __le - __lb;
857 if (__bs > __n)
858 {
859 __bs = __n;
860 __lb = __le - __bs;
861 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000862 __r = _VSTD::move_backward(__lb, __le, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000863 __n -= __bs;
864 __l -= __bs - 1;
865 }
866 return __r;
867}
868
869template <bool>
870class __deque_base_common
871{
872protected:
873 void __throw_length_error() const;
874 void __throw_out_of_range() const;
875};
876
877template <bool __b>
878void
879__deque_base_common<__b>::__throw_length_error() const
880{
881#ifndef _LIBCPP_NO_EXCEPTIONS
882 throw length_error("deque");
883#endif
884}
885
886template <bool __b>
887void
888__deque_base_common<__b>::__throw_out_of_range() const
889{
890#ifndef _LIBCPP_NO_EXCEPTIONS
891 throw out_of_range("deque");
892#endif
893}
894
895template <class _Tp, class _Allocator>
896class __deque_base
897 : protected __deque_base_common<true>
898{
899 __deque_base(const __deque_base& __c);
900 __deque_base& operator=(const __deque_base& __c);
901protected:
902 typedef _Tp value_type;
903 typedef _Allocator allocator_type;
904 typedef allocator_traits<allocator_type> __alloc_traits;
905 typedef value_type& reference;
906 typedef const value_type& const_reference;
907 typedef typename __alloc_traits::size_type size_type;
908 typedef typename __alloc_traits::difference_type difference_type;
909 typedef typename __alloc_traits::pointer pointer;
910 typedef typename __alloc_traits::const_pointer const_pointer;
911
912 static const difference_type __block_size = sizeof(value_type) < 256 ? 4096 / sizeof(value_type) : 16;
913
914 typedef typename __alloc_traits::template
915#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
916 rebind_alloc<pointer>
917#else
918 rebind_alloc<pointer>::other
919#endif
920 __pointer_allocator;
921 typedef allocator_traits<__pointer_allocator> __map_traits;
922 typedef typename __map_traits::pointer __map_pointer;
Howard Hinnantfcd8db72013-06-23 21:17:24 +0000923 typedef typename __alloc_traits::template
924#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
925 rebind_alloc<const_pointer>
926#else
927 rebind_alloc<const_pointer>::other
928#endif
929 __const_pointer_allocator;
930 typedef typename allocator_traits<__const_pointer_allocator>::const_pointer __map_const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000931 typedef __split_buffer<pointer, __pointer_allocator> __map;
932
933 typedef __deque_iterator<value_type, pointer, reference, __map_pointer,
934 difference_type, __block_size> iterator;
935 typedef __deque_iterator<value_type, const_pointer, const_reference, __map_const_pointer,
936 difference_type, __block_size> const_iterator;
937
938 __map __map_;
939 size_type __start_;
940 __compressed_pair<size_type, allocator_type> __size_;
941
Howard Hinnanta12beb32011-06-02 16:10:22 +0000942 iterator begin() _NOEXCEPT;
943 const_iterator begin() const _NOEXCEPT;
944 iterator end() _NOEXCEPT;
945 const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000946
947 _LIBCPP_INLINE_VISIBILITY size_type& size() {return __size_.first();}
Howard Hinnanta12beb32011-06-02 16:10:22 +0000948 _LIBCPP_INLINE_VISIBILITY
949 const size_type& size() const _NOEXCEPT {return __size_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000950 _LIBCPP_INLINE_VISIBILITY allocator_type& __alloc() {return __size_.second();}
Howard Hinnanta12beb32011-06-02 16:10:22 +0000951 _LIBCPP_INLINE_VISIBILITY
952 const allocator_type& __alloc() const _NOEXCEPT {return __size_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000953
Howard Hinnant009b2c42011-06-03 15:16:49 +0000954 __deque_base()
955 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000956 explicit __deque_base(const allocator_type& __a);
Howard Hinnant0a612b02011-06-02 20:00:14 +0000957public:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000958 ~__deque_base();
959
Howard Hinnant73d21a42010-09-04 23:28:19 +0000960#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000961
Howard Hinnant0a612b02011-06-02 20:00:14 +0000962 __deque_base(__deque_base&& __c)
963 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000964 __deque_base(__deque_base&& __c, const allocator_type& __a);
965
Howard Hinnant73d21a42010-09-04 23:28:19 +0000966#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant0a612b02011-06-02 20:00:14 +0000967 void swap(__deque_base& __c)
Howard Hinnantb965fed2011-06-03 16:20:53 +0000968 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Howard Hinnant0a612b02011-06-02 20:00:14 +0000969 __is_nothrow_swappable<allocator_type>::value);
970protected:
Howard Hinnanta12beb32011-06-02 16:10:22 +0000971 void clear() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000972
973 bool __invariants() const;
974
Howard Hinnant422a53f2010-09-21 21:28:23 +0000975 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000976 void __move_assign(__deque_base& __c)
Howard Hinnant18884f42011-06-02 21:38:57 +0000977 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
978 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000979 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000980 __map_ = _VSTD::move(__c.__map_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000981 __start_ = __c.__start_;
982 size() = __c.size();
983 __move_assign_alloc(__c);
984 __c.__start_ = __c.size() = 0;
985 }
986
Howard Hinnant422a53f2010-09-21 21:28:23 +0000987 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000988 void __move_assign_alloc(__deque_base& __c)
Howard Hinnant0a612b02011-06-02 20:00:14 +0000989 _NOEXCEPT_(!__alloc_traits::propagate_on_container_move_assignment::value ||
990 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000991 {__move_assign_alloc(__c, integral_constant<bool,
992 __alloc_traits::propagate_on_container_move_assignment::value>());}
993
994private:
Howard Hinnant422a53f2010-09-21 21:28:23 +0000995 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +0000996 void __move_assign_alloc(__deque_base& __c, true_type)
Howard Hinnant0a612b02011-06-02 20:00:14 +0000997 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000998 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000999 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001000 }
1001
Howard Hinnant422a53f2010-09-21 21:28:23 +00001002 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001003 void __move_assign_alloc(__deque_base&, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001004 {}
1005
Howard Hinnant422a53f2010-09-21 21:28:23 +00001006 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001007 static void __swap_alloc(allocator_type& __x, allocator_type& __y)
Howard Hinnant0a612b02011-06-02 20:00:14 +00001008 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1009 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001010 {__swap_alloc(__x, __y, integral_constant<bool,
1011 __alloc_traits::propagate_on_container_swap::value>());}
1012
Howard Hinnant422a53f2010-09-21 21:28:23 +00001013 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001014 static void __swap_alloc(allocator_type& __x, allocator_type& __y, true_type)
Howard Hinnant0a612b02011-06-02 20:00:14 +00001015 _NOEXCEPT_(__is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001016 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001017 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001018 swap(__x, __y);
1019 }
Howard Hinnant422a53f2010-09-21 21:28:23 +00001020
1021 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001022 static void __swap_alloc(allocator_type&, allocator_type&, false_type)
Howard Hinnant0a612b02011-06-02 20:00:14 +00001023 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001024 {}
1025};
1026
1027template <class _Tp, class _Allocator>
1028bool
1029__deque_base<_Tp, _Allocator>::__invariants() const
1030{
1031 if (!__map_.__invariants())
1032 return false;
1033 if (__map_.size() >= size_type(-1) / __block_size)
1034 return false;
1035 for (typename __map::const_iterator __i = __map_.begin(), __e = __map_.end();
1036 __i != __e; ++__i)
1037 if (*__i == nullptr)
1038 return false;
1039 if (__map_.size() != 0)
1040 {
1041 if (size() >= __map_.size() * __block_size)
1042 return false;
1043 if (__start_ >= __map_.size() * __block_size - size())
1044 return false;
1045 }
1046 else
1047 {
1048 if (size() != 0)
1049 return false;
1050 if (__start_ != 0)
1051 return false;
1052 }
1053 return true;
1054}
1055
1056template <class _Tp, class _Allocator>
1057typename __deque_base<_Tp, _Allocator>::iterator
Howard Hinnanta12beb32011-06-02 16:10:22 +00001058__deque_base<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001059{
1060 __map_pointer __mp = __map_.begin() + __start_ / __block_size;
1061 return iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
1062}
1063
1064template <class _Tp, class _Allocator>
1065typename __deque_base<_Tp, _Allocator>::const_iterator
Howard Hinnanta12beb32011-06-02 16:10:22 +00001066__deque_base<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001067{
Howard Hinnantfcd8db72013-06-23 21:17:24 +00001068 __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __start_ / __block_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001069 return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
1070}
1071
1072template <class _Tp, class _Allocator>
1073typename __deque_base<_Tp, _Allocator>::iterator
Howard Hinnanta12beb32011-06-02 16:10:22 +00001074__deque_base<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001075{
1076 size_type __p = size() + __start_;
1077 __map_pointer __mp = __map_.begin() + __p / __block_size;
1078 return iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
1079}
1080
1081template <class _Tp, class _Allocator>
1082typename __deque_base<_Tp, _Allocator>::const_iterator
Howard Hinnanta12beb32011-06-02 16:10:22 +00001083__deque_base<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001084{
1085 size_type __p = size() + __start_;
Howard Hinnantfcd8db72013-06-23 21:17:24 +00001086 __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __p / __block_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001087 return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
1088}
1089
1090template <class _Tp, class _Allocator>
1091inline _LIBCPP_INLINE_VISIBILITY
1092__deque_base<_Tp, _Allocator>::__deque_base()
Howard Hinnant009b2c42011-06-03 15:16:49 +00001093 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001094 : __start_(0), __size_(0) {}
1095
1096template <class _Tp, class _Allocator>
1097inline _LIBCPP_INLINE_VISIBILITY
1098__deque_base<_Tp, _Allocator>::__deque_base(const allocator_type& __a)
1099 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a) {}
1100
1101template <class _Tp, class _Allocator>
1102__deque_base<_Tp, _Allocator>::~__deque_base()
1103{
1104 clear();
1105 typename __map::iterator __i = __map_.begin();
1106 typename __map::iterator __e = __map_.end();
1107 for (; __i != __e; ++__i)
1108 __alloc_traits::deallocate(__alloc(), *__i, __block_size);
1109}
1110
Howard Hinnant73d21a42010-09-04 23:28:19 +00001111#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001112
1113template <class _Tp, class _Allocator>
1114__deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c)
Howard Hinnant0a612b02011-06-02 20:00:14 +00001115 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001116 : __map_(_VSTD::move(__c.__map_)),
1117 __start_(_VSTD::move(__c.__start_)),
1118 __size_(_VSTD::move(__c.__size_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001119{
1120 __c.__start_ = 0;
1121 __c.size() = 0;
1122}
1123
1124template <class _Tp, class _Allocator>
1125__deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c, const allocator_type& __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001126 : __map_(_VSTD::move(__c.__map_), __pointer_allocator(__a)),
1127 __start_(_VSTD::move(__c.__start_)),
1128 __size_(_VSTD::move(__c.size()), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001129{
1130 if (__a == __c.__alloc())
1131 {
1132 __c.__start_ = 0;
1133 __c.size() = 0;
1134 }
1135 else
1136 {
1137 __map_.clear();
1138 __start_ = 0;
1139 size() = 0;
1140 }
1141}
1142
Howard Hinnant73d21a42010-09-04 23:28:19 +00001143#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001144
1145template <class _Tp, class _Allocator>
1146void
1147__deque_base<_Tp, _Allocator>::swap(__deque_base& __c)
Howard Hinnant0a612b02011-06-02 20:00:14 +00001148 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value||
1149 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001150{
1151 __map_.swap(__c.__map_);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001152 _VSTD::swap(__start_, __c.__start_);
1153 _VSTD::swap(size(), __c.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001154 __swap_alloc(__alloc(), __c.__alloc());
1155}
1156
1157template <class _Tp, class _Allocator>
1158void
Howard Hinnanta12beb32011-06-02 16:10:22 +00001159__deque_base<_Tp, _Allocator>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001160{
1161 allocator_type& __a = __alloc();
1162 for (iterator __i = begin(), __e = end(); __i != __e; ++__i)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001163 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001164 size() = 0;
1165 while (__map_.size() > 2)
1166 {
1167 __alloc_traits::deallocate(__a, __map_.front(), __block_size);
1168 __map_.pop_front();
1169 }
1170 switch (__map_.size())
1171 {
1172 case 1:
1173 __start_ = __block_size / 2;
1174 break;
1175 case 2:
1176 __start_ = __block_size;
1177 break;
1178 }
1179}
1180
1181template <class _Tp, class _Allocator = allocator<_Tp> >
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001182class _LIBCPP_TYPE_VIS_ONLY deque
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001183 : private __deque_base<_Tp, _Allocator>
1184{
1185public:
1186 // types:
Howard Hinnant324bb032010-08-22 00:02:43 +00001187
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001188 typedef _Tp value_type;
1189 typedef _Allocator allocator_type;
1190
1191 typedef __deque_base<value_type, allocator_type> __base;
1192
1193 typedef typename __base::__alloc_traits __alloc_traits;
1194 typedef typename __base::reference reference;
1195 typedef typename __base::const_reference const_reference;
1196 typedef typename __base::iterator iterator;
1197 typedef typename __base::const_iterator const_iterator;
1198 typedef typename __base::size_type size_type;
1199 typedef typename __base::difference_type difference_type;
1200
1201 typedef typename __base::pointer pointer;
1202 typedef typename __base::const_pointer const_pointer;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001203 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
1204 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001205
1206 // construct/copy/destroy:
Howard Hinnant009b2c42011-06-03 15:16:49 +00001207 _LIBCPP_INLINE_VISIBILITY
1208 deque()
1209 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
1210 {}
Marshall Clow48c74702014-03-05 19:06:20 +00001211 _LIBCPP_INLINE_VISIBILITY explicit deque(const allocator_type& __a) : __base(__a) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001212 explicit deque(size_type __n);
Marshall Clowab04aad2013-09-07 16:16:19 +00001213#if _LIBCPP_STD_VER > 11
1214 explicit deque(size_type __n, const _Allocator& __a);
1215#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001216 deque(size_type __n, const value_type& __v);
1217 deque(size_type __n, const value_type& __v, const allocator_type& __a);
1218 template <class _InputIter>
1219 deque(_InputIter __f, _InputIter __l,
1220 typename enable_if<__is_input_iterator<_InputIter>::value>::type* = 0);
1221 template <class _InputIter>
1222 deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
1223 typename enable_if<__is_input_iterator<_InputIter>::value>::type* = 0);
1224 deque(const deque& __c);
1225 deque(const deque& __c, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +00001226#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001227 deque(initializer_list<value_type> __il);
1228 deque(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +00001229#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001230
1231 deque& operator=(const deque& __c);
Howard Hinnante3e32912011-08-12 21:56:02 +00001232#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant422a53f2010-09-21 21:28:23 +00001233 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001234 deque& operator=(initializer_list<value_type> __il) {assign(__il); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +00001235#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001236
Howard Hinnant73d21a42010-09-04 23:28:19 +00001237#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant0a612b02011-06-02 20:00:14 +00001238 deque(deque&& __c) _NOEXCEPT_(is_nothrow_move_constructible<__base>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001239 deque(deque&& __c, const allocator_type& __a);
Howard Hinnant0a612b02011-06-02 20:00:14 +00001240 deque& operator=(deque&& __c)
Howard Hinnant18884f42011-06-02 21:38:57 +00001241 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1242 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001243#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001244
1245 template <class _InputIter>
1246 void assign(_InputIter __f, _InputIter __l,
1247 typename enable_if<__is_input_iterator<_InputIter>::value &&
1248 !__is_random_access_iterator<_InputIter>::value>::type* = 0);
1249 template <class _RAIter>
1250 void assign(_RAIter __f, _RAIter __l,
1251 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
1252 void assign(size_type __n, const value_type& __v);
Howard Hinnante3e32912011-08-12 21:56:02 +00001253#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant422a53f2010-09-21 21:28:23 +00001254 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001255 void assign(initializer_list<value_type> __il) {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00001256#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001257
Howard Hinnanta12beb32011-06-02 16:10:22 +00001258 allocator_type get_allocator() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001259
1260 // iterators:
1261
Howard Hinnant422a53f2010-09-21 21:28:23 +00001262 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001263 iterator begin() _NOEXCEPT {return __base::begin();}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001264 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001265 const_iterator begin() const _NOEXCEPT {return __base::begin();}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001266 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001267 iterator end() _NOEXCEPT {return __base::end();}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001268 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001269 const_iterator end() const _NOEXCEPT {return __base::end();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001270
Howard Hinnant422a53f2010-09-21 21:28:23 +00001271 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001272 reverse_iterator rbegin() _NOEXCEPT
1273 {return reverse_iterator(__base::end());}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001274 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001275 const_reverse_iterator rbegin() const _NOEXCEPT
1276 {return const_reverse_iterator(__base::end());}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001277 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001278 reverse_iterator rend() _NOEXCEPT
1279 {return reverse_iterator(__base::begin());}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001280 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001281 const_reverse_iterator rend() const _NOEXCEPT
1282 {return const_reverse_iterator(__base::begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001283
Howard Hinnant422a53f2010-09-21 21:28:23 +00001284 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001285 const_iterator cbegin() const _NOEXCEPT
1286 {return __base::begin();}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001287 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001288 const_iterator cend() const _NOEXCEPT
1289 {return __base::end();}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001290 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001291 const_reverse_iterator crbegin() const _NOEXCEPT
1292 {return const_reverse_iterator(__base::end());}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001293 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001294 const_reverse_iterator crend() const _NOEXCEPT
1295 {return const_reverse_iterator(__base::begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001296
1297 // capacity:
Howard Hinnant422a53f2010-09-21 21:28:23 +00001298 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001299 size_type size() const _NOEXCEPT {return __base::size();}
1300 _LIBCPP_INLINE_VISIBILITY
1301 size_type max_size() const _NOEXCEPT
1302 {return __alloc_traits::max_size(__base::__alloc());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001303 void resize(size_type __n);
1304 void resize(size_type __n, const value_type& __v);
Howard Hinnant18884f42011-06-02 21:38:57 +00001305 void shrink_to_fit() _NOEXCEPT;
Howard Hinnanta12beb32011-06-02 16:10:22 +00001306 _LIBCPP_INLINE_VISIBILITY
1307 bool empty() const _NOEXCEPT {return __base::size() == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001308
1309 // element access:
1310 reference operator[](size_type __i);
1311 const_reference operator[](size_type __i) const;
1312 reference at(size_type __i);
1313 const_reference at(size_type __i) const;
1314 reference front();
1315 const_reference front() const;
1316 reference back();
1317 const_reference back() const;
1318
1319 // 23.2.2.3 modifiers:
1320 void push_front(const value_type& __v);
1321 void push_back(const value_type& __v);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001322#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1323#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001324 template <class... _Args> void emplace_front(_Args&&... __args);
1325 template <class... _Args> void emplace_back(_Args&&... __args);
1326 template <class... _Args> iterator emplace(const_iterator __p, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001327#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001328 void push_front(value_type&& __v);
1329 void push_back(value_type&& __v);
1330 iterator insert(const_iterator __p, value_type&& __v);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001331#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001332 iterator insert(const_iterator __p, const value_type& __v);
1333 iterator insert(const_iterator __p, size_type __n, const value_type& __v);
1334 template <class _InputIter>
1335 iterator insert (const_iterator __p, _InputIter __f, _InputIter __l,
1336 typename enable_if<__is_input_iterator<_InputIter>::value
1337 &&!__is_bidirectional_iterator<_InputIter>::value>::type* = 0);
1338 template <class _BiIter>
1339 iterator insert (const_iterator __p, _BiIter __f, _BiIter __l,
1340 typename enable_if<__is_bidirectional_iterator<_BiIter>::value>::type* = 0);
Howard Hinnante3e32912011-08-12 21:56:02 +00001341#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant422a53f2010-09-21 21:28:23 +00001342 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001343 iterator insert(const_iterator __p, initializer_list<value_type> __il)
1344 {return insert(__p, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00001345#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001346 void pop_front();
1347 void pop_back();
1348 iterator erase(const_iterator __p);
1349 iterator erase(const_iterator __f, const_iterator __l);
1350
Howard Hinnant0a612b02011-06-02 20:00:14 +00001351 void swap(deque& __c)
1352 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1353 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnanta12beb32011-06-02 16:10:22 +00001354 void clear() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001355
Howard Hinnant422a53f2010-09-21 21:28:23 +00001356 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001357 bool __invariants() const {return __base::__invariants();}
1358private:
Howard Hinnantfcd8db72013-06-23 21:17:24 +00001359 typedef typename __base::__map_const_pointer __map_const_pointer;
1360
Howard Hinnant422a53f2010-09-21 21:28:23 +00001361 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001362 static size_type __recommend_blocks(size_type __n)
1363 {
1364 return __n / __base::__block_size + (__n % __base::__block_size != 0);
1365 }
Howard Hinnant422a53f2010-09-21 21:28:23 +00001366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001367 size_type __capacity() const
1368 {
1369 return __base::__map_.size() == 0 ? 0 : __base::__map_.size() * __base::__block_size - 1;
1370 }
Howard Hinnant422a53f2010-09-21 21:28:23 +00001371 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001372 size_type __front_spare() const
1373 {
1374 return __base::__start_;
1375 }
Howard Hinnant422a53f2010-09-21 21:28:23 +00001376 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001377 size_type __back_spare() const
1378 {
1379 return __capacity() - (__base::__start_ + __base::size());
1380 }
1381
1382 template <class _InpIter>
1383 void __append(_InpIter __f, _InpIter __l,
1384 typename enable_if<__is_input_iterator<_InpIter>::value &&
1385 !__is_forward_iterator<_InpIter>::value>::type* = 0);
1386 template <class _ForIter>
1387 void __append(_ForIter __f, _ForIter __l,
1388 typename enable_if<__is_forward_iterator<_ForIter>::value>::type* = 0);
1389 void __append(size_type __n);
1390 void __append(size_type __n, const value_type& __v);
1391 void __erase_to_end(const_iterator __f);
1392 void __add_front_capacity();
1393 void __add_front_capacity(size_type __n);
1394 void __add_back_capacity();
1395 void __add_back_capacity(size_type __n);
1396 iterator __move_and_check(iterator __f, iterator __l, iterator __r,
1397 const_pointer& __vt);
1398 iterator __move_backward_and_check(iterator __f, iterator __l, iterator __r,
1399 const_pointer& __vt);
1400 void __move_construct_and_check(iterator __f, iterator __l,
1401 iterator __r, const_pointer& __vt);
1402 void __move_construct_backward_and_check(iterator __f, iterator __l,
1403 iterator __r, const_pointer& __vt);
1404
Howard Hinnant422a53f2010-09-21 21:28:23 +00001405 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001406 void __copy_assign_alloc(const deque& __c)
1407 {__copy_assign_alloc(__c, integral_constant<bool,
1408 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1409
Howard Hinnant422a53f2010-09-21 21:28:23 +00001410 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001411 void __copy_assign_alloc(const deque& __c, true_type)
1412 {
1413 if (__base::__alloc() != __c.__alloc())
1414 {
1415 clear();
1416 shrink_to_fit();
1417 }
1418 __base::__alloc() = __c.__alloc();
1419 __base::__map_.__alloc() = __c.__map_.__alloc();
1420 }
1421
Howard Hinnant422a53f2010-09-21 21:28:23 +00001422 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001423 void __copy_assign_alloc(const deque&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001424 {}
1425
Howard Hinnant18884f42011-06-02 21:38:57 +00001426 void __move_assign(deque& __c, true_type)
1427 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001428 void __move_assign(deque& __c, false_type);
1429};
1430
1431template <class _Tp, class _Allocator>
1432deque<_Tp, _Allocator>::deque(size_type __n)
1433{
1434 if (__n > 0)
1435 __append(__n);
1436}
1437
Marshall Clowab04aad2013-09-07 16:16:19 +00001438#if _LIBCPP_STD_VER > 11
1439template <class _Tp, class _Allocator>
1440deque<_Tp, _Allocator>::deque(size_type __n, const _Allocator& __a)
1441 : __base(__a)
1442{
1443 if (__n > 0)
1444 __append(__n);
1445}
1446#endif
1447
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001448template <class _Tp, class _Allocator>
1449deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v)
1450{
1451 if (__n > 0)
1452 __append(__n, __v);
1453}
1454
1455template <class _Tp, class _Allocator>
1456deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v, const allocator_type& __a)
1457 : __base(__a)
1458{
1459 if (__n > 0)
1460 __append(__n, __v);
1461}
1462
1463template <class _Tp, class _Allocator>
1464template <class _InputIter>
1465deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l,
1466 typename enable_if<__is_input_iterator<_InputIter>::value>::type*)
1467{
1468 __append(__f, __l);
1469}
1470
1471template <class _Tp, class _Allocator>
1472template <class _InputIter>
1473deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
1474 typename enable_if<__is_input_iterator<_InputIter>::value>::type*)
1475 : __base(__a)
1476{
1477 __append(__f, __l);
1478}
1479
1480template <class _Tp, class _Allocator>
1481deque<_Tp, _Allocator>::deque(const deque& __c)
1482 : __base(__alloc_traits::select_on_container_copy_construction(__c.__alloc()))
1483{
1484 __append(__c.begin(), __c.end());
1485}
1486
1487template <class _Tp, class _Allocator>
1488deque<_Tp, _Allocator>::deque(const deque& __c, const allocator_type& __a)
1489 : __base(__a)
1490{
1491 __append(__c.begin(), __c.end());
1492}
1493
Howard Hinnante3e32912011-08-12 21:56:02 +00001494#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1495
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001496template <class _Tp, class _Allocator>
1497deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il)
1498{
1499 __append(__il.begin(), __il.end());
1500}
1501
1502template <class _Tp, class _Allocator>
1503deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il, const allocator_type& __a)
1504 : __base(__a)
1505{
1506 __append(__il.begin(), __il.end());
1507}
1508
Howard Hinnante3e32912011-08-12 21:56:02 +00001509#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1510
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001511template <class _Tp, class _Allocator>
1512deque<_Tp, _Allocator>&
1513deque<_Tp, _Allocator>::operator=(const deque& __c)
1514{
1515 if (this != &__c)
1516 {
1517 __copy_assign_alloc(__c);
1518 assign(__c.begin(), __c.end());
1519 }
1520 return *this;
1521}
1522
Howard Hinnant73d21a42010-09-04 23:28:19 +00001523#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001524
1525template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001526inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001527deque<_Tp, _Allocator>::deque(deque&& __c)
Howard Hinnant0a612b02011-06-02 20:00:14 +00001528 _NOEXCEPT_(is_nothrow_move_constructible<__base>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001529 : __base(_VSTD::move(__c))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001530{
1531}
1532
1533template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001534inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001535deque<_Tp, _Allocator>::deque(deque&& __c, const allocator_type& __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001536 : __base(_VSTD::move(__c), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001537{
1538 if (__a != __c.__alloc())
1539 {
Howard Hinnant99968442011-11-29 18:15:50 +00001540 typedef move_iterator<iterator> _Ip;
1541 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001542 }
1543}
1544
1545template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001546inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001547deque<_Tp, _Allocator>&
1548deque<_Tp, _Allocator>::operator=(deque&& __c)
Howard Hinnant18884f42011-06-02 21:38:57 +00001549 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1550 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001551{
1552 __move_assign(__c, integral_constant<bool,
1553 __alloc_traits::propagate_on_container_move_assignment::value>());
1554 return *this;
1555}
1556
1557template <class _Tp, class _Allocator>
1558void
1559deque<_Tp, _Allocator>::__move_assign(deque& __c, false_type)
1560{
1561 if (__base::__alloc() != __c.__alloc())
1562 {
Howard Hinnant99968442011-11-29 18:15:50 +00001563 typedef move_iterator<iterator> _Ip;
1564 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001565 }
1566 else
1567 __move_assign(__c, true_type());
1568}
1569
1570template <class _Tp, class _Allocator>
1571void
1572deque<_Tp, _Allocator>::__move_assign(deque& __c, true_type)
Howard Hinnant18884f42011-06-02 21:38:57 +00001573 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001574{
1575 clear();
1576 shrink_to_fit();
1577 __base::__move_assign(__c);
1578}
1579
Howard Hinnant73d21a42010-09-04 23:28:19 +00001580#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001581
1582template <class _Tp, class _Allocator>
1583template <class _InputIter>
1584void
1585deque<_Tp, _Allocator>::assign(_InputIter __f, _InputIter __l,
1586 typename enable_if<__is_input_iterator<_InputIter>::value &&
1587 !__is_random_access_iterator<_InputIter>::value>::type*)
1588{
1589 iterator __i = __base::begin();
1590 iterator __e = __base::end();
1591 for (; __f != __l && __i != __e; ++__f, ++__i)
1592 *__i = *__f;
1593 if (__f != __l)
1594 __append(__f, __l);
1595 else
1596 __erase_to_end(__i);
1597}
1598
1599template <class _Tp, class _Allocator>
1600template <class _RAIter>
1601void
1602deque<_Tp, _Allocator>::assign(_RAIter __f, _RAIter __l,
1603 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
1604{
1605 if (static_cast<size_type>(__l - __f) > __base::size())
1606 {
1607 _RAIter __m = __f + __base::size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001608 _VSTD::copy(__f, __m, __base::begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001609 __append(__m, __l);
1610 }
1611 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00001612 __erase_to_end(_VSTD::copy(__f, __l, __base::begin()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001613}
1614
1615template <class _Tp, class _Allocator>
1616void
1617deque<_Tp, _Allocator>::assign(size_type __n, const value_type& __v)
1618{
1619 if (__n > __base::size())
1620 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001621 _VSTD::fill_n(__base::begin(), __base::size(), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001622 __n -= __base::size();
1623 __append(__n, __v);
1624 }
1625 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00001626 __erase_to_end(_VSTD::fill_n(__base::begin(), __n, __v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001627}
1628
1629template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001630inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001631_Allocator
Howard Hinnanta12beb32011-06-02 16:10:22 +00001632deque<_Tp, _Allocator>::get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001633{
1634 return __base::__alloc();
1635}
1636
1637template <class _Tp, class _Allocator>
1638void
1639deque<_Tp, _Allocator>::resize(size_type __n)
1640{
1641 if (__n > __base::size())
1642 __append(__n - __base::size());
1643 else if (__n < __base::size())
1644 __erase_to_end(__base::begin() + __n);
1645}
1646
1647template <class _Tp, class _Allocator>
1648void
1649deque<_Tp, _Allocator>::resize(size_type __n, const value_type& __v)
1650{
1651 if (__n > __base::size())
1652 __append(__n - __base::size(), __v);
1653 else if (__n < __base::size())
1654 __erase_to_end(__base::begin() + __n);
1655}
1656
1657template <class _Tp, class _Allocator>
1658void
Howard Hinnant18884f42011-06-02 21:38:57 +00001659deque<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001660{
1661 allocator_type& __a = __base::__alloc();
1662 if (empty())
1663 {
1664 while (__base::__map_.size() > 0)
1665 {
1666 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
1667 __base::__map_.pop_back();
1668 }
1669 __base::__start_ = 0;
1670 }
1671 else
1672 {
1673 if (__front_spare() >= __base::__block_size)
1674 {
1675 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
1676 __base::__map_.pop_front();
1677 __base::__start_ -= __base::__block_size;
1678 }
1679 if (__back_spare() >= __base::__block_size)
1680 {
1681 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
1682 __base::__map_.pop_back();
1683 }
1684 }
1685 __base::__map_.shrink_to_fit();
1686}
1687
1688template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001689inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001690typename deque<_Tp, _Allocator>::reference
1691deque<_Tp, _Allocator>::operator[](size_type __i)
1692{
1693 size_type __p = __base::__start_ + __i;
1694 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1695}
1696
1697template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001698inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001699typename deque<_Tp, _Allocator>::const_reference
1700deque<_Tp, _Allocator>::operator[](size_type __i) const
1701{
1702 size_type __p = __base::__start_ + __i;
1703 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1704}
1705
1706template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001707inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001708typename deque<_Tp, _Allocator>::reference
1709deque<_Tp, _Allocator>::at(size_type __i)
1710{
1711 if (__i >= __base::size())
1712 __base::__throw_out_of_range();
1713 size_type __p = __base::__start_ + __i;
1714 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1715}
1716
1717template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001718inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001719typename deque<_Tp, _Allocator>::const_reference
1720deque<_Tp, _Allocator>::at(size_type __i) const
1721{
1722 if (__i >= __base::size())
1723 __base::__throw_out_of_range();
1724 size_type __p = __base::__start_ + __i;
1725 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1726}
1727
1728template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001729inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001730typename deque<_Tp, _Allocator>::reference
1731deque<_Tp, _Allocator>::front()
1732{
1733 return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1734 + __base::__start_ % __base::__block_size);
1735}
1736
1737template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001738inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001739typename deque<_Tp, _Allocator>::const_reference
1740deque<_Tp, _Allocator>::front() const
1741{
1742 return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1743 + __base::__start_ % __base::__block_size);
1744}
1745
1746template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001747inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001748typename deque<_Tp, _Allocator>::reference
1749deque<_Tp, _Allocator>::back()
1750{
1751 size_type __p = __base::size() + __base::__start_ - 1;
1752 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1753}
1754
1755template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001756inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001757typename deque<_Tp, _Allocator>::const_reference
1758deque<_Tp, _Allocator>::back() const
1759{
1760 size_type __p = __base::size() + __base::__start_ - 1;
1761 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1762}
1763
1764template <class _Tp, class _Allocator>
1765void
1766deque<_Tp, _Allocator>::push_back(const value_type& __v)
1767{
1768 allocator_type& __a = __base::__alloc();
1769 if (__back_spare() == 0)
1770 __add_back_capacity();
1771 // __back_spare() >= 1
Howard Hinnant0949eed2011-06-30 21:18:19 +00001772 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001773 ++__base::size();
1774}
1775
Howard Hinnant73d21a42010-09-04 23:28:19 +00001776#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001777
1778template <class _Tp, class _Allocator>
1779void
1780deque<_Tp, _Allocator>::push_back(value_type&& __v)
1781{
1782 allocator_type& __a = __base::__alloc();
1783 if (__back_spare() == 0)
1784 __add_back_capacity();
1785 // __back_spare() >= 1
Howard Hinnant0949eed2011-06-30 21:18:19 +00001786 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001787 ++__base::size();
1788}
1789
Howard Hinnant73d21a42010-09-04 23:28:19 +00001790#ifndef _LIBCPP_HAS_NO_VARIADICS
1791
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001792template <class _Tp, class _Allocator>
1793template <class... _Args>
1794void
1795deque<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1796{
1797 allocator_type& __a = __base::__alloc();
1798 if (__back_spare() == 0)
1799 __add_back_capacity();
1800 // __back_spare() >= 1
Howard Hinnant0949eed2011-06-30 21:18:19 +00001801 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001802 ++__base::size();
1803}
1804
Howard Hinnant73d21a42010-09-04 23:28:19 +00001805#endif // _LIBCPP_HAS_NO_VARIADICS
1806#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001807
1808template <class _Tp, class _Allocator>
1809void
1810deque<_Tp, _Allocator>::push_front(const value_type& __v)
1811{
1812 allocator_type& __a = __base::__alloc();
1813 if (__front_spare() == 0)
1814 __add_front_capacity();
1815 // __front_spare() >= 1
Howard Hinnant0949eed2011-06-30 21:18:19 +00001816 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001817 --__base::__start_;
1818 ++__base::size();
1819}
1820
Howard Hinnant73d21a42010-09-04 23:28:19 +00001821#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001822
1823template <class _Tp, class _Allocator>
1824void
1825deque<_Tp, _Allocator>::push_front(value_type&& __v)
1826{
1827 allocator_type& __a = __base::__alloc();
1828 if (__front_spare() == 0)
1829 __add_front_capacity();
1830 // __front_spare() >= 1
Howard Hinnant0949eed2011-06-30 21:18:19 +00001831 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001832 --__base::__start_;
1833 ++__base::size();
1834}
1835
Howard Hinnant73d21a42010-09-04 23:28:19 +00001836#ifndef _LIBCPP_HAS_NO_VARIADICS
1837
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001838template <class _Tp, class _Allocator>
1839template <class... _Args>
1840void
1841deque<_Tp, _Allocator>::emplace_front(_Args&&... __args)
1842{
1843 allocator_type& __a = __base::__alloc();
1844 if (__front_spare() == 0)
1845 __add_front_capacity();
1846 // __front_spare() >= 1
Howard Hinnant0949eed2011-06-30 21:18:19 +00001847 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001848 --__base::__start_;
1849 ++__base::size();
1850}
1851
Howard Hinnant73d21a42010-09-04 23:28:19 +00001852#endif // _LIBCPP_HAS_NO_VARIADICS
1853#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001854
1855template <class _Tp, class _Allocator>
1856typename deque<_Tp, _Allocator>::iterator
1857deque<_Tp, _Allocator>::insert(const_iterator __p, const value_type& __v)
1858{
1859 size_type __pos = __p - __base::begin();
1860 size_type __to_end = __base::size() - __pos;
1861 allocator_type& __a = __base::__alloc();
1862 if (__pos < __to_end)
1863 { // insert by shifting things backward
1864 if (__front_spare() == 0)
1865 __add_front_capacity();
1866 // __front_spare() >= 1
1867 if (__pos == 0)
1868 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001869 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001870 --__base::__start_;
1871 ++__base::size();
1872 }
1873 else
1874 {
1875 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1876 iterator __b = __base::begin();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001877 iterator __bm1 = _VSTD::prev(__b);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001878 if (__vt == pointer_traits<const_pointer>::pointer_to(*__b))
1879 __vt = pointer_traits<const_pointer>::pointer_to(*__bm1);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001880 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001881 --__base::__start_;
1882 ++__base::size();
1883 if (__pos > 1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001884 __b = __move_and_check(_VSTD::next(__b), __b + __pos, __b, __vt);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001885 *__b = *__vt;
1886 }
1887 }
1888 else
1889 { // insert by shifting things forward
1890 if (__back_spare() == 0)
1891 __add_back_capacity();
1892 // __back_capacity >= 1
1893 size_type __de = __base::size() - __pos;
1894 if (__de == 0)
1895 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001896 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001897 ++__base::size();
1898 }
1899 else
1900 {
1901 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1902 iterator __e = __base::end();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001903 iterator __em1 = _VSTD::prev(__e);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001904 if (__vt == pointer_traits<const_pointer>::pointer_to(*__em1))
1905 __vt = pointer_traits<const_pointer>::pointer_to(*__e);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001906 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001907 ++__base::size();
1908 if (__de > 1)
1909 __e = __move_backward_and_check(__e - __de, __em1, __e, __vt);
1910 *--__e = *__vt;
1911 }
1912 }
1913 return __base::begin() + __pos;
1914}
1915
Howard Hinnant73d21a42010-09-04 23:28:19 +00001916#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001917
1918template <class _Tp, class _Allocator>
1919typename deque<_Tp, _Allocator>::iterator
1920deque<_Tp, _Allocator>::insert(const_iterator __p, value_type&& __v)
1921{
1922 size_type __pos = __p - __base::begin();
1923 size_type __to_end = __base::size() - __pos;
1924 allocator_type& __a = __base::__alloc();
1925 if (__pos < __to_end)
1926 { // insert by shifting things backward
1927 if (__front_spare() == 0)
1928 __add_front_capacity();
1929 // __front_spare() >= 1
1930 if (__pos == 0)
1931 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001932 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001933 --__base::__start_;
1934 ++__base::size();
1935 }
1936 else
1937 {
1938 iterator __b = __base::begin();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001939 iterator __bm1 = _VSTD::prev(__b);
1940 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001941 --__base::__start_;
1942 ++__base::size();
1943 if (__pos > 1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001944 __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
1945 *__b = _VSTD::move(__v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001946 }
1947 }
1948 else
1949 { // insert by shifting things forward
1950 if (__back_spare() == 0)
1951 __add_back_capacity();
1952 // __back_capacity >= 1
1953 size_type __de = __base::size() - __pos;
1954 if (__de == 0)
1955 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001956 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001957 ++__base::size();
1958 }
1959 else
1960 {
1961 iterator __e = __base::end();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001962 iterator __em1 = _VSTD::prev(__e);
1963 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001964 ++__base::size();
1965 if (__de > 1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001966 __e = _VSTD::move_backward(__e - __de, __em1, __e);
1967 *--__e = _VSTD::move(__v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001968 }
1969 }
1970 return __base::begin() + __pos;
1971}
1972
Howard Hinnant73d21a42010-09-04 23:28:19 +00001973#ifndef _LIBCPP_HAS_NO_VARIADICS
1974
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001975template <class _Tp, class _Allocator>
1976template <class... _Args>
1977typename deque<_Tp, _Allocator>::iterator
1978deque<_Tp, _Allocator>::emplace(const_iterator __p, _Args&&... __args)
1979{
1980 size_type __pos = __p - __base::begin();
1981 size_type __to_end = __base::size() - __pos;
1982 allocator_type& __a = __base::__alloc();
1983 if (__pos < __to_end)
1984 { // insert by shifting things backward
1985 if (__front_spare() == 0)
1986 __add_front_capacity();
1987 // __front_spare() >= 1
1988 if (__pos == 0)
1989 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001990 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001991 --__base::__start_;
1992 ++__base::size();
1993 }
1994 else
1995 {
Howard Hinnanta58402a2012-07-08 23:23:04 +00001996 value_type __tmp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001997 iterator __b = __base::begin();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001998 iterator __bm1 = _VSTD::prev(__b);
1999 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002000 --__base::__start_;
2001 ++__base::size();
2002 if (__pos > 1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002003 __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
Howard Hinnanta58402a2012-07-08 23:23:04 +00002004 *__b = _VSTD::move(__tmp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002005 }
2006 }
2007 else
2008 { // insert by shifting things forward
2009 if (__back_spare() == 0)
2010 __add_back_capacity();
2011 // __back_capacity >= 1
2012 size_type __de = __base::size() - __pos;
2013 if (__de == 0)
2014 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002015 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002016 ++__base::size();
2017 }
2018 else
2019 {
Howard Hinnanta58402a2012-07-08 23:23:04 +00002020 value_type __tmp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002021 iterator __e = __base::end();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002022 iterator __em1 = _VSTD::prev(__e);
2023 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002024 ++__base::size();
2025 if (__de > 1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002026 __e = _VSTD::move_backward(__e - __de, __em1, __e);
Howard Hinnanta58402a2012-07-08 23:23:04 +00002027 *--__e = _VSTD::move(__tmp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002028 }
2029 }
2030 return __base::begin() + __pos;
2031}
2032
Howard Hinnant73d21a42010-09-04 23:28:19 +00002033#endif // _LIBCPP_HAS_NO_VARIADICS
2034#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002035
2036template <class _Tp, class _Allocator>
2037typename deque<_Tp, _Allocator>::iterator
2038deque<_Tp, _Allocator>::insert(const_iterator __p, size_type __n, const value_type& __v)
2039{
2040 size_type __pos = __p - __base::begin();
2041 size_type __to_end = __base::size() - __pos;
2042 allocator_type& __a = __base::__alloc();
2043 if (__pos < __to_end)
2044 { // insert by shifting things backward
2045 if (__n > __front_spare())
2046 __add_front_capacity(__n - __front_spare());
2047 // __n <= __front_spare()
2048 size_type __old_n = __n;
2049 iterator __old_begin = __base::begin();
2050 iterator __i = __old_begin;
2051 if (__n > __pos)
2052 {
2053 for (size_type __m = __n - __pos; __m; --__m, --__base::__start_, ++__base::size())
Howard Hinnant0949eed2011-06-30 21:18:19 +00002054 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002055 __n = __pos;
2056 }
2057 if (__n > 0)
2058 {
2059 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2060 iterator __obn = __old_begin + __n;
2061 __move_construct_backward_and_check(__old_begin, __obn, __i, __vt);
2062 if (__n < __pos)
2063 __old_begin = __move_and_check(__obn, __old_begin + __pos, __old_begin, __vt);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002064 _VSTD::fill_n(__old_begin, __n, *__vt);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002065 }
2066 }
2067 else
2068 { // insert by shifting things forward
2069 size_type __back_capacity = __back_spare();
2070 if (__n > __back_capacity)
2071 __add_back_capacity(__n - __back_capacity);
2072 // __n <= __back_capacity
2073 size_type __old_n = __n;
2074 iterator __old_end = __base::end();
2075 iterator __i = __old_end;
2076 size_type __de = __base::size() - __pos;
2077 if (__n > __de)
2078 {
2079 for (size_type __m = __n - __de; __m; --__m, ++__i, ++__base::size())
Howard Hinnant0949eed2011-06-30 21:18:19 +00002080 __alloc_traits::construct(__a, _VSTD::addressof(*__i), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002081 __n = __de;
2082 }
2083 if (__n > 0)
2084 {
2085 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2086 iterator __oen = __old_end - __n;
2087 __move_construct_and_check(__oen, __old_end, __i, __vt);
2088 if (__n < __de)
2089 __old_end = __move_backward_and_check(__old_end - __de, __oen, __old_end, __vt);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002090 _VSTD::fill_n(__old_end - __n, __n, *__vt);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002091 }
2092 }
2093 return __base::begin() + __pos;
2094}
2095
2096template <class _Tp, class _Allocator>
2097template <class _InputIter>
2098typename deque<_Tp, _Allocator>::iterator
2099deque<_Tp, _Allocator>::insert(const_iterator __p, _InputIter __f, _InputIter __l,
2100 typename enable_if<__is_input_iterator<_InputIter>::value
2101 &&!__is_bidirectional_iterator<_InputIter>::value>::type*)
2102{
2103 __split_buffer<value_type, allocator_type&> __buf(__base::__alloc());
2104 __buf.__construct_at_end(__f, __l);
2105 typedef typename __split_buffer<value_type, allocator_type&>::iterator __bi;
2106 return insert(__p, move_iterator<__bi>(__buf.begin()), move_iterator<__bi>(__buf.end()));
2107}
2108
2109template <class _Tp, class _Allocator>
2110template <class _BiIter>
2111typename deque<_Tp, _Allocator>::iterator
2112deque<_Tp, _Allocator>::insert(const_iterator __p, _BiIter __f, _BiIter __l,
2113 typename enable_if<__is_bidirectional_iterator<_BiIter>::value>::type*)
2114{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002115 size_type __n = _VSTD::distance(__f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002116 size_type __pos = __p - __base::begin();
2117 size_type __to_end = __base::size() - __pos;
2118 allocator_type& __a = __base::__alloc();
2119 if (__pos < __to_end)
2120 { // insert by shifting things backward
2121 if (__n > __front_spare())
2122 __add_front_capacity(__n - __front_spare());
2123 // __n <= __front_spare()
2124 size_type __old_n = __n;
2125 iterator __old_begin = __base::begin();
2126 iterator __i = __old_begin;
2127 _BiIter __m = __f;
2128 if (__n > __pos)
2129 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002130 __m = __pos < __n / 2 ? _VSTD::prev(__l, __pos) : _VSTD::next(__f, __n - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002131 for (_BiIter __j = __m; __j != __f; --__base::__start_, ++__base::size())
Howard Hinnant0949eed2011-06-30 21:18:19 +00002132 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), *--__j);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002133 __n = __pos;
2134 }
2135 if (__n > 0)
2136 {
2137 iterator __obn = __old_begin + __n;
2138 for (iterator __j = __obn; __j != __old_begin;)
2139 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002140 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), _VSTD::move(*--__j));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002141 --__base::__start_;
2142 ++__base::size();
2143 }
2144 if (__n < __pos)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002145 __old_begin = _VSTD::move(__obn, __old_begin + __pos, __old_begin);
2146 _VSTD::copy(__m, __l, __old_begin);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002147 }
2148 }
2149 else
2150 { // insert by shifting things forward
2151 size_type __back_capacity = __back_spare();
2152 if (__n > __back_capacity)
2153 __add_back_capacity(__n - __back_capacity);
2154 // __n <= __back_capacity
2155 size_type __old_n = __n;
2156 iterator __old_end = __base::end();
2157 iterator __i = __old_end;
2158 _BiIter __m = __l;
2159 size_type __de = __base::size() - __pos;
2160 if (__n > __de)
2161 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002162 __m = __de < __n / 2 ? _VSTD::next(__f, __de) : _VSTD::prev(__l, __n - __de);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002163 for (_BiIter __j = __m; __j != __l; ++__i, ++__j, ++__base::size())
Howard Hinnant0949eed2011-06-30 21:18:19 +00002164 __alloc_traits::construct(__a, _VSTD::addressof(*__i), *__j);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002165 __n = __de;
2166 }
2167 if (__n > 0)
2168 {
2169 iterator __oen = __old_end - __n;
2170 for (iterator __j = __oen; __j != __old_end; ++__i, ++__j, ++__base::size())
Howard Hinnant0949eed2011-06-30 21:18:19 +00002171 __alloc_traits::construct(__a, _VSTD::addressof(*__i), _VSTD::move(*__j));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002172 if (__n < __de)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002173 __old_end = _VSTD::move_backward(__old_end - __de, __oen, __old_end);
2174 _VSTD::copy_backward(__f, __m, __old_end);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002175 }
2176 }
2177 return __base::begin() + __pos;
2178}
2179
2180template <class _Tp, class _Allocator>
2181template <class _InpIter>
2182void
2183deque<_Tp, _Allocator>::__append(_InpIter __f, _InpIter __l,
2184 typename enable_if<__is_input_iterator<_InpIter>::value &&
2185 !__is_forward_iterator<_InpIter>::value>::type*)
2186{
2187 for (; __f != __l; ++__f)
2188 push_back(*__f);
2189}
2190
2191template <class _Tp, class _Allocator>
2192template <class _ForIter>
2193void
2194deque<_Tp, _Allocator>::__append(_ForIter __f, _ForIter __l,
2195 typename enable_if<__is_forward_iterator<_ForIter>::value>::type*)
2196{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002197 size_type __n = _VSTD::distance(__f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002198 allocator_type& __a = __base::__alloc();
2199 size_type __back_capacity = __back_spare();
2200 if (__n > __back_capacity)
2201 __add_back_capacity(__n - __back_capacity);
2202 // __n <= __back_capacity
2203 for (iterator __i = __base::end(); __f != __l; ++__i, ++__f, ++__base::size())
Howard Hinnant0949eed2011-06-30 21:18:19 +00002204 __alloc_traits::construct(__a, _VSTD::addressof(*__i), *__f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002205}
2206
2207template <class _Tp, class _Allocator>
2208void
2209deque<_Tp, _Allocator>::__append(size_type __n)
2210{
2211 allocator_type& __a = __base::__alloc();
2212 size_type __back_capacity = __back_spare();
2213 if (__n > __back_capacity)
2214 __add_back_capacity(__n - __back_capacity);
2215 // __n <= __back_capacity
2216 for (iterator __i = __base::end(); __n; --__n, ++__i, ++__base::size())
Howard Hinnant0949eed2011-06-30 21:18:19 +00002217 __alloc_traits::construct(__a, _VSTD::addressof(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002218}
2219
2220template <class _Tp, class _Allocator>
2221void
2222deque<_Tp, _Allocator>::__append(size_type __n, const value_type& __v)
2223{
2224 allocator_type& __a = __base::__alloc();
2225 size_type __back_capacity = __back_spare();
2226 if (__n > __back_capacity)
2227 __add_back_capacity(__n - __back_capacity);
2228 // __n <= __back_capacity
2229 for (iterator __i = __base::end(); __n; --__n, ++__i, ++__base::size())
Howard Hinnant0949eed2011-06-30 21:18:19 +00002230 __alloc_traits::construct(__a, _VSTD::addressof(*__i), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002231}
2232
2233// Create front capacity for one block of elements.
2234// Strong guarantee. Either do it or don't touch anything.
2235template <class _Tp, class _Allocator>
2236void
2237deque<_Tp, _Allocator>::__add_front_capacity()
2238{
2239 allocator_type& __a = __base::__alloc();
2240 if (__back_spare() >= __base::__block_size)
2241 {
2242 __base::__start_ += __base::__block_size;
2243 pointer __pt = __base::__map_.back();
2244 __base::__map_.pop_back();
2245 __base::__map_.push_front(__pt);
2246 }
2247 // Else if __base::__map_.size() < __base::__map_.capacity() then we need to allocate 1 buffer
2248 else if (__base::__map_.size() < __base::__map_.capacity())
2249 { // we can put the new buffer into the map, but don't shift things around
2250 // until all buffers are allocated. If we throw, we don't need to fix
2251 // anything up (any added buffers are undetectible)
2252 if (__base::__map_.__front_spare() > 0)
2253 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2254 else
2255 {
2256 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2257 // Done allocating, reorder capacity
2258 pointer __pt = __base::__map_.back();
2259 __base::__map_.pop_back();
2260 __base::__map_.push_front(__pt);
2261 }
2262 __base::__start_ = __base::__map_.size() == 1 ?
2263 __base::__block_size / 2 :
2264 __base::__start_ + __base::__block_size;
2265 }
2266 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2267 else
2268 {
2269 __split_buffer<pointer, typename __base::__pointer_allocator&>
2270 __buf(max<size_type>(2 * __base::__map_.capacity(), 1),
2271 0, __base::__map_.__alloc());
2272#ifndef _LIBCPP_NO_EXCEPTIONS
2273 try
2274 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002275#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002276 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2277#ifndef _LIBCPP_NO_EXCEPTIONS
2278 }
2279 catch (...)
2280 {
2281 __alloc_traits::deallocate(__a, __buf.front(), __base::__block_size);
2282 throw;
2283 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002284#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002285 for (typename __base::__map_pointer __i = __base::__map_.begin();
2286 __i != __base::__map_.end(); ++__i)
2287 __buf.push_back(*__i);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002288 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2289 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2290 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2291 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002292 __base::__start_ = __base::__map_.size() == 1 ?
2293 __base::__block_size / 2 :
2294 __base::__start_ + __base::__block_size;
2295 }
2296}
2297
2298// Create front capacity for __n elements.
2299// Strong guarantee. Either do it or don't touch anything.
2300template <class _Tp, class _Allocator>
2301void
2302deque<_Tp, _Allocator>::__add_front_capacity(size_type __n)
2303{
2304 allocator_type& __a = __base::__alloc();
2305 size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2306 // Number of unused blocks at back:
2307 size_type __back_capacity = __back_spare() / __base::__block_size;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002308 __back_capacity = _VSTD::min(__back_capacity, __nb); // don't take more than you need
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002309 __nb -= __back_capacity; // number of blocks need to allocate
2310 // If __nb == 0, then we have sufficient capacity.
2311 if (__nb == 0)
2312 {
2313 __base::__start_ += __base::__block_size * __back_capacity;
2314 for (; __back_capacity > 0; --__back_capacity)
2315 {
2316 pointer __pt = __base::__map_.back();
2317 __base::__map_.pop_back();
2318 __base::__map_.push_front(__pt);
2319 }
2320 }
2321 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2322 else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2323 { // we can put the new buffers into the map, but don't shift things around
2324 // until all buffers are allocated. If we throw, we don't need to fix
2325 // anything up (any added buffers are undetectible)
2326 for (; __nb > 0; --__nb, __base::__start_ += __base::__block_size - (__base::__map_.size() == 1))
2327 {
2328 if (__base::__map_.__front_spare() == 0)
2329 break;
2330 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2331 }
2332 for (; __nb > 0; --__nb, ++__back_capacity)
2333 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2334 // Done allocating, reorder capacity
2335 __base::__start_ += __back_capacity * __base::__block_size;
2336 for (; __back_capacity > 0; --__back_capacity)
2337 {
2338 pointer __pt = __base::__map_.back();
2339 __base::__map_.pop_back();
2340 __base::__map_.push_front(__pt);
2341 }
2342 }
2343 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2344 else
2345 {
2346 size_type __ds = (__nb + __back_capacity) * __base::__block_size - __base::__map_.empty();
2347 __split_buffer<pointer, typename __base::__pointer_allocator&>
2348 __buf(max<size_type>(2* __base::__map_.capacity(),
2349 __nb + __base::__map_.size()),
2350 0, __base::__map_.__alloc());
2351#ifndef _LIBCPP_NO_EXCEPTIONS
2352 try
2353 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002354#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002355 for (; __nb > 0; --__nb)
2356 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2357#ifndef _LIBCPP_NO_EXCEPTIONS
2358 }
2359 catch (...)
2360 {
2361 for (typename __base::__map_pointer __i = __buf.begin();
2362 __i != __buf.end(); ++__i)
2363 __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2364 throw;
2365 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002366#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002367 for (; __back_capacity > 0; --__back_capacity)
2368 {
2369 __buf.push_back(__base::__map_.back());
2370 __base::__map_.pop_back();
2371 }
2372 for (typename __base::__map_pointer __i = __base::__map_.begin();
2373 __i != __base::__map_.end(); ++__i)
2374 __buf.push_back(*__i);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002375 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2376 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2377 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2378 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002379 __base::__start_ += __ds;
2380 }
2381}
2382
2383// Create back capacity for one block of elements.
2384// Strong guarantee. Either do it or don't touch anything.
2385template <class _Tp, class _Allocator>
2386void
2387deque<_Tp, _Allocator>::__add_back_capacity()
2388{
2389 allocator_type& __a = __base::__alloc();
2390 if (__front_spare() >= __base::__block_size)
2391 {
2392 __base::__start_ -= __base::__block_size;
2393 pointer __pt = __base::__map_.front();
2394 __base::__map_.pop_front();
2395 __base::__map_.push_back(__pt);
2396 }
2397 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2398 else if (__base::__map_.size() < __base::__map_.capacity())
2399 { // we can put the new buffer into the map, but don't shift things around
2400 // until it is allocated. If we throw, we don't need to fix
2401 // anything up (any added buffers are undetectible)
2402 if (__base::__map_.__back_spare() != 0)
2403 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2404 else
2405 {
2406 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2407 // Done allocating, reorder capacity
2408 pointer __pt = __base::__map_.front();
2409 __base::__map_.pop_front();
2410 __base::__map_.push_back(__pt);
2411 }
2412 }
2413 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2414 else
2415 {
2416 __split_buffer<pointer, typename __base::__pointer_allocator&>
2417 __buf(max<size_type>(2* __base::__map_.capacity(), 1),
2418 __base::__map_.size(),
2419 __base::__map_.__alloc());
2420#ifndef _LIBCPP_NO_EXCEPTIONS
2421 try
2422 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002423#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002424 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2425#ifndef _LIBCPP_NO_EXCEPTIONS
2426 }
2427 catch (...)
2428 {
2429 __alloc_traits::deallocate(__a, __buf.back(), __base::__block_size);
2430 throw;
2431 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002432#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002433 for (typename __base::__map_pointer __i = __base::__map_.end();
2434 __i != __base::__map_.begin();)
2435 __buf.push_front(*--__i);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002436 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2437 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2438 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2439 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002440 }
2441}
2442
2443// Create back capacity for __n elements.
2444// Strong guarantee. Either do it or don't touch anything.
2445template <class _Tp, class _Allocator>
2446void
2447deque<_Tp, _Allocator>::__add_back_capacity(size_type __n)
2448{
2449 allocator_type& __a = __base::__alloc();
2450 size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2451 // Number of unused blocks at front:
2452 size_type __front_capacity = __front_spare() / __base::__block_size;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002453 __front_capacity = _VSTD::min(__front_capacity, __nb); // don't take more than you need
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002454 __nb -= __front_capacity; // number of blocks need to allocate
2455 // If __nb == 0, then we have sufficient capacity.
2456 if (__nb == 0)
2457 {
2458 __base::__start_ -= __base::__block_size * __front_capacity;
2459 for (; __front_capacity > 0; --__front_capacity)
2460 {
2461 pointer __pt = __base::__map_.front();
2462 __base::__map_.pop_front();
2463 __base::__map_.push_back(__pt);
2464 }
2465 }
2466 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2467 else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2468 { // we can put the new buffers into the map, but don't shift things around
2469 // until all buffers are allocated. If we throw, we don't need to fix
2470 // anything up (any added buffers are undetectible)
2471 for (; __nb > 0; --__nb)
2472 {
2473 if (__base::__map_.__back_spare() == 0)
2474 break;
2475 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2476 }
2477 for (; __nb > 0; --__nb, ++__front_capacity, __base::__start_ +=
2478 __base::__block_size - (__base::__map_.size() == 1))
2479 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2480 // Done allocating, reorder capacity
2481 __base::__start_ -= __base::__block_size * __front_capacity;
2482 for (; __front_capacity > 0; --__front_capacity)
2483 {
2484 pointer __pt = __base::__map_.front();
2485 __base::__map_.pop_front();
2486 __base::__map_.push_back(__pt);
2487 }
2488 }
2489 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2490 else
2491 {
2492 size_type __ds = __front_capacity * __base::__block_size;
2493 __split_buffer<pointer, typename __base::__pointer_allocator&>
2494 __buf(max<size_type>(2* __base::__map_.capacity(),
2495 __nb + __base::__map_.size()),
2496 __base::__map_.size() - __front_capacity,
2497 __base::__map_.__alloc());
2498#ifndef _LIBCPP_NO_EXCEPTIONS
2499 try
2500 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002501#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002502 for (; __nb > 0; --__nb)
2503 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2504#ifndef _LIBCPP_NO_EXCEPTIONS
2505 }
2506 catch (...)
2507 {
2508 for (typename __base::__map_pointer __i = __buf.begin();
2509 __i != __buf.end(); ++__i)
2510 __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2511 throw;
2512 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002513#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002514 for (; __front_capacity > 0; --__front_capacity)
2515 {
2516 __buf.push_back(__base::__map_.front());
2517 __base::__map_.pop_front();
2518 }
2519 for (typename __base::__map_pointer __i = __base::__map_.end();
2520 __i != __base::__map_.begin();)
2521 __buf.push_front(*--__i);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002522 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2523 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2524 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2525 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002526 __base::__start_ -= __ds;
2527 }
2528}
2529
2530template <class _Tp, class _Allocator>
2531void
2532deque<_Tp, _Allocator>::pop_front()
2533{
2534 allocator_type& __a = __base::__alloc();
Howard Hinnantfcd8db72013-06-23 21:17:24 +00002535 __alloc_traits::destroy(__a, __to_raw_pointer(*(__base::__map_.begin() +
2536 __base::__start_ / __base::__block_size) +
2537 __base::__start_ % __base::__block_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002538 --__base::size();
2539 if (++__base::__start_ >= 2 * __base::__block_size)
2540 {
2541 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2542 __base::__map_.pop_front();
2543 __base::__start_ -= __base::__block_size;
2544 }
2545}
2546
2547template <class _Tp, class _Allocator>
2548void
2549deque<_Tp, _Allocator>::pop_back()
2550{
2551 allocator_type& __a = __base::__alloc();
2552 size_type __p = __base::size() + __base::__start_ - 1;
Howard Hinnantfcd8db72013-06-23 21:17:24 +00002553 __alloc_traits::destroy(__a, __to_raw_pointer(*(__base::__map_.begin() +
2554 __p / __base::__block_size) +
2555 __p % __base::__block_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002556 --__base::size();
2557 if (__back_spare() >= 2 * __base::__block_size)
2558 {
2559 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2560 __base::__map_.pop_back();
2561 }
2562}
2563
2564// move assign [__f, __l) to [__r, __r + (__l-__f)).
2565// If __vt points into [__f, __l), then subtract (__f - __r) from __vt.
2566template <class _Tp, class _Allocator>
2567typename deque<_Tp, _Allocator>::iterator
2568deque<_Tp, _Allocator>::__move_and_check(iterator __f, iterator __l, iterator __r,
2569 const_pointer& __vt)
2570{
2571 // as if
2572 // for (; __f != __l; ++__f, ++__r)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002573 // *__r = _VSTD::move(*__f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002574 difference_type __n = __l - __f;
2575 while (__n > 0)
2576 {
2577 pointer __fb = __f.__ptr_;
2578 pointer __fe = *__f.__m_iter_ + __base::__block_size;
2579 difference_type __bs = __fe - __fb;
2580 if (__bs > __n)
2581 {
2582 __bs = __n;
2583 __fe = __fb + __bs;
2584 }
2585 if (__fb <= __vt && __vt < __fe)
Howard Hinnantfcd8db72013-06-23 21:17:24 +00002586 __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) -= __f - __r).__ptr_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002587 __r = _VSTD::move(__fb, __fe, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002588 __n -= __bs;
2589 __f += __bs;
2590 }
2591 return __r;
2592}
2593
2594// move assign [__f, __l) to [__r - (__l-__f), __r) backwards.
2595// If __vt points into [__f, __l), then add (__r - __l) to __vt.
2596template <class _Tp, class _Allocator>
2597typename deque<_Tp, _Allocator>::iterator
2598deque<_Tp, _Allocator>::__move_backward_and_check(iterator __f, iterator __l, iterator __r,
2599 const_pointer& __vt)
2600{
2601 // as if
2602 // while (__f != __l)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002603 // *--__r = _VSTD::move(*--__l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002604 difference_type __n = __l - __f;
2605 while (__n > 0)
2606 {
2607 --__l;
2608 pointer __lb = *__l.__m_iter_;
2609 pointer __le = __l.__ptr_ + 1;
2610 difference_type __bs = __le - __lb;
2611 if (__bs > __n)
2612 {
2613 __bs = __n;
2614 __lb = __le - __bs;
2615 }
2616 if (__lb <= __vt && __vt < __le)
Howard Hinnantfcd8db72013-06-23 21:17:24 +00002617 __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) += __r - __l - 1).__ptr_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002618 __r = _VSTD::move_backward(__lb, __le, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002619 __n -= __bs;
2620 __l -= __bs - 1;
2621 }
2622 return __r;
2623}
2624
2625// move construct [__f, __l) to [__r, __r + (__l-__f)).
2626// If __vt points into [__f, __l), then add (__r - __f) to __vt.
2627template <class _Tp, class _Allocator>
2628void
2629deque<_Tp, _Allocator>::__move_construct_and_check(iterator __f, iterator __l,
2630 iterator __r, const_pointer& __vt)
2631{
2632 allocator_type& __a = __base::__alloc();
2633 // as if
2634 // for (; __f != __l; ++__r, ++__f, ++__base::size())
Howard Hinnant0949eed2011-06-30 21:18:19 +00002635 // __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__f));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002636 difference_type __n = __l - __f;
2637 while (__n > 0)
2638 {
2639 pointer __fb = __f.__ptr_;
2640 pointer __fe = *__f.__m_iter_ + __base::__block_size;
2641 difference_type __bs = __fe - __fb;
2642 if (__bs > __n)
2643 {
2644 __bs = __n;
2645 __fe = __fb + __bs;
2646 }
2647 if (__fb <= __vt && __vt < __fe)
Howard Hinnantfcd8db72013-06-23 21:17:24 +00002648 __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) += __r - __f).__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002649 for (; __fb != __fe; ++__fb, ++__r, ++__base::size())
Howard Hinnant0949eed2011-06-30 21:18:19 +00002650 __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__fb));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002651 __n -= __bs;
2652 __f += __bs;
2653 }
2654}
2655
2656// move construct [__f, __l) to [__r - (__l-__f), __r) backwards.
2657// If __vt points into [__f, __l), then subtract (__l - __r) from __vt.
2658template <class _Tp, class _Allocator>
2659void
2660deque<_Tp, _Allocator>::__move_construct_backward_and_check(iterator __f, iterator __l,
2661 iterator __r, const_pointer& __vt)
2662{
2663 allocator_type& __a = __base::__alloc();
2664 // as if
2665 // for (iterator __j = __l; __j != __f;)
2666 // {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002667 // __alloc_traitsconstruct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__j));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002668 // --__base::__start_;
2669 // ++__base::size();
2670 // }
2671 difference_type __n = __l - __f;
2672 while (__n > 0)
2673 {
2674 --__l;
2675 pointer __lb = *__l.__m_iter_;
2676 pointer __le = __l.__ptr_ + 1;
2677 difference_type __bs = __le - __lb;
2678 if (__bs > __n)
2679 {
2680 __bs = __n;
2681 __lb = __le - __bs;
2682 }
2683 if (__lb <= __vt && __vt < __le)
Howard Hinnantfcd8db72013-06-23 21:17:24 +00002684 __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) -= __l - __r + 1).__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002685 while (__le != __lb)
2686 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002687 __alloc_traits::construct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__le));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002688 --__base::__start_;
2689 ++__base::size();
2690 }
2691 __n -= __bs;
2692 __l -= __bs - 1;
2693 }
2694}
2695
2696template <class _Tp, class _Allocator>
2697typename deque<_Tp, _Allocator>::iterator
2698deque<_Tp, _Allocator>::erase(const_iterator __f)
2699{
2700 difference_type __n = 1;
2701 iterator __b = __base::begin();
2702 difference_type __pos = __f - __b;
2703 iterator __p = __b + __pos;
2704 allocator_type& __a = __base::__alloc();
2705 if (__pos < (__base::size() - 1) / 2)
2706 { // erase from front
Howard Hinnant0949eed2011-06-30 21:18:19 +00002707 _VSTD::move_backward(__b, __p, _VSTD::next(__p));
2708 __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002709 --__base::size();
2710 ++__base::__start_;
2711 if (__front_spare() >= 2 * __base::__block_size)
2712 {
2713 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2714 __base::__map_.pop_front();
2715 __base::__start_ -= __base::__block_size;
2716 }
2717 }
2718 else
2719 { // erase from back
Howard Hinnant0949eed2011-06-30 21:18:19 +00002720 iterator __i = _VSTD::move(_VSTD::next(__p), __base::end(), __p);
2721 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002722 --__base::size();
2723 if (__back_spare() >= 2 * __base::__block_size)
2724 {
2725 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2726 __base::__map_.pop_back();
2727 }
2728 }
2729 return __base::begin() + __pos;
2730}
2731
2732template <class _Tp, class _Allocator>
2733typename deque<_Tp, _Allocator>::iterator
2734deque<_Tp, _Allocator>::erase(const_iterator __f, const_iterator __l)
2735{
2736 difference_type __n = __l - __f;
2737 iterator __b = __base::begin();
2738 difference_type __pos = __f - __b;
2739 iterator __p = __b + __pos;
2740 if (__n > 0)
2741 {
2742 allocator_type& __a = __base::__alloc();
2743 if (__pos < (__base::size() - __n) / 2)
2744 { // erase from front
Howard Hinnant0949eed2011-06-30 21:18:19 +00002745 iterator __i = _VSTD::move_backward(__b, __p, __p + __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002746 for (; __b != __i; ++__b)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002747 __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002748 __base::size() -= __n;
2749 __base::__start_ += __n;
2750 while (__front_spare() >= 2 * __base::__block_size)
2751 {
2752 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2753 __base::__map_.pop_front();
2754 __base::__start_ -= __base::__block_size;
2755 }
2756 }
2757 else
2758 { // erase from back
Howard Hinnant0949eed2011-06-30 21:18:19 +00002759 iterator __i = _VSTD::move(__p + __n, __base::end(), __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002760 for (iterator __e = __base::end(); __i != __e; ++__i)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002761 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002762 __base::size() -= __n;
2763 while (__back_spare() >= 2 * __base::__block_size)
2764 {
2765 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2766 __base::__map_.pop_back();
2767 }
2768 }
2769 }
2770 return __base::begin() + __pos;
2771}
2772
2773template <class _Tp, class _Allocator>
2774void
2775deque<_Tp, _Allocator>::__erase_to_end(const_iterator __f)
2776{
2777 iterator __e = __base::end();
2778 difference_type __n = __e - __f;
2779 if (__n > 0)
2780 {
2781 allocator_type& __a = __base::__alloc();
2782 iterator __b = __base::begin();
2783 difference_type __pos = __f - __b;
2784 for (iterator __p = __b + __pos; __p != __e; ++__p)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002785 __alloc_traits::destroy(__a, _VSTD::addressof(*__p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002786 __base::size() -= __n;
2787 while (__back_spare() >= 2 * __base::__block_size)
2788 {
2789 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2790 __base::__map_.pop_back();
2791 }
2792 }
2793}
2794
2795template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00002796inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002797void
2798deque<_Tp, _Allocator>::swap(deque& __c)
Howard Hinnant0a612b02011-06-02 20:00:14 +00002799 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2800 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002801{
2802 __base::swap(__c);
2803}
2804
2805template <class _Tp, class _Allocator>
Howard Hinnant422a53f2010-09-21 21:28:23 +00002806inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002807void
Howard Hinnanta12beb32011-06-02 16:10:22 +00002808deque<_Tp, _Allocator>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002809{
2810 __base::clear();
2811}
2812
2813template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002814inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002815bool
2816operator==(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2817{
2818 const typename deque<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002819 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002820}
2821
2822template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002823inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002824bool
2825operator!=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2826{
2827 return !(__x == __y);
2828}
2829
2830template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002831inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002832bool
2833operator< (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2834{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002835 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002836}
2837
2838template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002839inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002840bool
2841operator> (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2842{
2843 return __y < __x;
2844}
2845
2846template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002847inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002848bool
2849operator>=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2850{
2851 return !(__x < __y);
2852}
2853
2854template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002855inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002856bool
2857operator<=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2858{
2859 return !(__y < __x);
2860}
2861
2862template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002863inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002864void
2865swap(deque<_Tp, _Allocator>& __x, deque<_Tp, _Allocator>& __y)
Howard Hinnant0a612b02011-06-02 20:00:14 +00002866 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002867{
2868 __x.swap(__y);
2869}
2870
2871_LIBCPP_END_NAMESPACE_STD
2872
2873#endif // _LIBCPP_DEQUE