blob: b0b778a56ce97fe2dbc7de398482506c3fe72064 [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>
Marshall Clow3150c352015-01-22 18:33:29 +0000120 iterator insert(const_iterator p, InputIterator f, InputIterator l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000121 iterator insert(const_iterator p, initializer_list<value_type> il);
122 void pop_front();
123 void pop_back();
124 iterator erase(const_iterator p);
125 iterator erase(const_iterator f, const_iterator l);
Howard Hinnant18884f42011-06-02 21:38:57 +0000126 void swap(deque& c)
Marshall Clow7d914d12015-07-13 20:04:56 +0000127 noexcept(allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnanta12beb32011-06-02 16:10:22 +0000128 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000129};
130
131template <class T, class Allocator>
132 bool operator==(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
133template <class T, class Allocator>
134 bool operator< (const deque<T,Allocator>& x, const deque<T,Allocator>& y);
135template <class T, class Allocator>
136 bool operator!=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
137template <class T, class Allocator>
138 bool operator> (const deque<T,Allocator>& x, const deque<T,Allocator>& y);
139template <class T, class Allocator>
140 bool operator>=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
141template <class T, class Allocator>
142 bool operator<=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
143
144// specialized algorithms:
145template <class T, class Allocator>
Howard Hinnantc5607272011-06-03 17:30:28 +0000146 void swap(deque<T,Allocator>& x, deque<T,Allocator>& y)
147 noexcept(noexcept(x.swap(y)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000148
149} // std
150
151*/
152
Howard Hinnant08e17472011-10-17 20:05:10 +0000153#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000154#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000155#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000156
157#include <__config>
158#include <__split_buffer>
159#include <type_traits>
160#include <initializer_list>
161#include <iterator>
162#include <algorithm>
163#include <stdexcept>
164
Howard Hinnant66c6f972011-11-29 16:45:27 +0000165#include <__undef_min_max>
166
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000167_LIBCPP_BEGIN_NAMESPACE_STD
168
169template <class _Tp, class _Allocator> class __deque_base;
Marshall Clowceead9c2015-02-18 17:24:08 +0000170template <class _Tp, class _Allocator = allocator<_Tp> > class _LIBCPP_TYPE_VIS_ONLY deque;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000171
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
Evgeniy Stepanov746572b2015-11-06 22:02:29 +0000264template <class _ValueType, class _DiffType>
265struct __deque_block_size {
266 static const _DiffType value = sizeof(_ValueType) < 256 ? 4096 / sizeof(_ValueType) : 16;
267};
268
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000269template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
Evgeniy Stepanov746572b2015-11-06 22:02:29 +0000270 class _DiffType, _DiffType _BS =
271#ifdef _LIBCPP_ABI_INCOMPLETE_TYPES_IN_DEQUE
272// Keep template parameter to avoid changing all template declarations thoughout
273// this file.
274 0
275#else
276 __deque_block_size<_ValueType, _DiffType>::value
277#endif
278 >
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000279class _LIBCPP_TYPE_VIS_ONLY __deque_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000280{
281 typedef _MapPointer __map_iterator;
282public:
283 typedef _Pointer pointer;
284 typedef _DiffType difference_type;
285private:
286 __map_iterator __m_iter_;
287 pointer __ptr_;
288
Evgeniy Stepanov746572b2015-11-06 22:02:29 +0000289 static const difference_type __block_size;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000290public:
291 typedef _ValueType value_type;
292 typedef random_access_iterator_tag iterator_category;
293 typedef _Reference reference;
294
Marshall Clow5a11f942013-08-06 16:14:36 +0000295 _LIBCPP_INLINE_VISIBILITY __deque_iterator() _NOEXCEPT
296#if _LIBCPP_STD_VER > 11
297 : __m_iter_(nullptr), __ptr_(nullptr)
298#endif
299 {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000300
Howard Hinnant99968442011-11-29 18:15:50 +0000301 template <class _Pp, class _Rp, class _MP>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000302 _LIBCPP_INLINE_VISIBILITY
Evgeniy Stepanov746572b2015-11-06 22:02:29 +0000303 __deque_iterator(const __deque_iterator<value_type, _Pp, _Rp, _MP, difference_type, _BS>& __it,
Howard Hinnant99968442011-11-29 18:15:50 +0000304 typename enable_if<is_convertible<_Pp, pointer>::value>::type* = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000305 : __m_iter_(__it.__m_iter_), __ptr_(__it.__ptr_) {}
306
307 _LIBCPP_INLINE_VISIBILITY reference operator*() const {return *__ptr_;}
308 _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return __ptr_;}
309
310 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator++()
311 {
312 if (++__ptr_ - *__m_iter_ == __block_size)
313 {
314 ++__m_iter_;
315 __ptr_ = *__m_iter_;
316 }
317 return *this;
318 }
319
320 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator++(int)
321 {
322 __deque_iterator __tmp = *this;
323 ++(*this);
324 return __tmp;
325 }
326
327 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator--()
328 {
329 if (__ptr_ == *__m_iter_)
330 {
331 --__m_iter_;
332 __ptr_ = *__m_iter_ + __block_size;
333 }
334 --__ptr_;
335 return *this;
336 }
337
338 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator--(int)
339 {
340 __deque_iterator __tmp = *this;
341 --(*this);
342 return __tmp;
343 }
344
345 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator+=(difference_type __n)
346 {
347 if (__n != 0)
348 {
349 __n += __ptr_ - *__m_iter_;
350 if (__n > 0)
351 {
352 __m_iter_ += __n / __block_size;
353 __ptr_ = *__m_iter_ + __n % __block_size;
354 }
355 else // (__n < 0)
356 {
357 difference_type __z = __block_size - 1 - __n;
358 __m_iter_ -= __z / __block_size;
359 __ptr_ = *__m_iter_ + (__block_size - 1 - __z % __block_size);
360 }
361 }
362 return *this;
363 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000364
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000365 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator-=(difference_type __n)
366 {
367 return *this += -__n;
368 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000369
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000370 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator+(difference_type __n) const
371 {
372 __deque_iterator __t(*this);
373 __t += __n;
374 return __t;
375 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000376
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000377 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator-(difference_type __n) const
378 {
379 __deque_iterator __t(*this);
380 __t -= __n;
381 return __t;
382 }
383
384 _LIBCPP_INLINE_VISIBILITY
385 friend __deque_iterator operator+(difference_type __n, const __deque_iterator& __it)
386 {return __it + __n;}
387
388 _LIBCPP_INLINE_VISIBILITY
389 friend difference_type operator-(const __deque_iterator& __x, const __deque_iterator& __y)
390 {
391 if (__x != __y)
392 return (__x.__m_iter_ - __y.__m_iter_) * __block_size
393 + (__x.__ptr_ - *__x.__m_iter_)
394 - (__y.__ptr_ - *__y.__m_iter_);
395 return 0;
396 }
397
398 _LIBCPP_INLINE_VISIBILITY reference operator[](difference_type __n) const
399 {return *(*this + __n);}
400
401 _LIBCPP_INLINE_VISIBILITY friend
402 bool operator==(const __deque_iterator& __x, const __deque_iterator& __y)
403 {return __x.__ptr_ == __y.__ptr_;}
404
405 _LIBCPP_INLINE_VISIBILITY friend
406 bool operator!=(const __deque_iterator& __x, const __deque_iterator& __y)
407 {return !(__x == __y);}
408
409 _LIBCPP_INLINE_VISIBILITY friend
410 bool operator<(const __deque_iterator& __x, const __deque_iterator& __y)
411 {return __x.__m_iter_ < __y.__m_iter_ ||
412 (__x.__m_iter_ == __y.__m_iter_ && __x.__ptr_ < __y.__ptr_);}
413
414 _LIBCPP_INLINE_VISIBILITY friend
415 bool operator>(const __deque_iterator& __x, const __deque_iterator& __y)
416 {return __y < __x;}
417
418 _LIBCPP_INLINE_VISIBILITY friend
419 bool operator<=(const __deque_iterator& __x, const __deque_iterator& __y)
420 {return !(__y < __x);}
421
422 _LIBCPP_INLINE_VISIBILITY friend
423 bool operator>=(const __deque_iterator& __x, const __deque_iterator& __y)
424 {return !(__x < __y);}
425
426private:
Howard Hinnanta12beb32011-06-02 16:10:22 +0000427 _LIBCPP_INLINE_VISIBILITY __deque_iterator(__map_iterator __m, pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000428 : __m_iter_(__m), __ptr_(__p) {}
429
Howard Hinnant99968442011-11-29 18:15:50 +0000430 template <class _Tp, class _Ap> friend class __deque_base;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000431 template <class _Tp, class _Ap> friend class _LIBCPP_TYPE_VIS_ONLY deque;
Howard Hinnant99968442011-11-29 18:15:50 +0000432 template <class _Vp, class _Pp, class _Rp, class _MP, class _Dp, _Dp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000433 friend class _LIBCPP_TYPE_VIS_ONLY __deque_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000434
435 template <class _RAIter,
436 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
437 friend
438 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
439 copy(_RAIter __f,
440 _RAIter __l,
441 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
442 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
443
444 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
445 class _OutputIterator>
446 friend
447 _OutputIterator
448 copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
449 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
450 _OutputIterator __r);
451
452 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
453 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
454 friend
455 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
456 copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
457 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
458 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
459
460 template <class _RAIter,
461 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
462 friend
463 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
464 copy_backward(_RAIter __f,
465 _RAIter __l,
466 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
467 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
468
469 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
470 class _OutputIterator>
471 friend
472 _OutputIterator
473 copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
474 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
475 _OutputIterator __r);
476
477 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
478 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
479 friend
480 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
481 copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
482 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
483 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
484
485 template <class _RAIter,
486 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
487 friend
488 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
489 move(_RAIter __f,
490 _RAIter __l,
491 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
492 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
493
494 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
495 class _OutputIterator>
496 friend
497 _OutputIterator
498 move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
499 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
500 _OutputIterator __r);
501
502 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
503 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
504 friend
505 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
506 move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
507 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
508 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
509
510 template <class _RAIter,
511 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
512 friend
513 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
514 move_backward(_RAIter __f,
515 _RAIter __l,
516 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
517 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
518
519 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
520 class _OutputIterator>
521 friend
522 _OutputIterator
523 move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
524 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
525 _OutputIterator __r);
526
527 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
528 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
529 friend
530 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
531 move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
532 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
533 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
534};
535
Evgeniy Stepanov746572b2015-11-06 22:02:29 +0000536template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
537 class _DiffType, _DiffType _BlockSize>
538const _DiffType __deque_iterator<_ValueType, _Pointer, _Reference, _MapPointer,
539 _DiffType, _BlockSize>::__block_size =
540 __deque_block_size<_ValueType, _DiffType>::value;
541
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000542// copy
543
544template <class _RAIter,
545 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
546__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
547copy(_RAIter __f,
548 _RAIter __l,
549 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
550 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
551{
552 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
553 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
Evgeniy Stepanov746572b2015-11-06 22:02:29 +0000554 const difference_type __block_size = __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::__block_size;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000555 while (__f != __l)
556 {
557 pointer __rb = __r.__ptr_;
Evgeniy Stepanov746572b2015-11-06 22:02:29 +0000558 pointer __re = *__r.__m_iter_ + __block_size;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000559 difference_type __bs = __re - __rb;
560 difference_type __n = __l - __f;
561 _RAIter __m = __l;
562 if (__n > __bs)
563 {
564 __n = __bs;
565 __m = __f + __n;
566 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000567 _VSTD::copy(__f, __m, __rb);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000568 __f = __m;
569 __r += __n;
570 }
571 return __r;
572}
573
574template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
575 class _OutputIterator>
576_OutputIterator
577copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
578 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
579 _OutputIterator __r)
580{
581 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
582 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
Evgeniy Stepanov746572b2015-11-06 22:02:29 +0000583 const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000584 difference_type __n = __l - __f;
585 while (__n > 0)
586 {
587 pointer __fb = __f.__ptr_;
Evgeniy Stepanov746572b2015-11-06 22:02:29 +0000588 pointer __fe = *__f.__m_iter_ + __block_size;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000589 difference_type __bs = __fe - __fb;
590 if (__bs > __n)
591 {
592 __bs = __n;
593 __fe = __fb + __bs;
594 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000595 __r = _VSTD::copy(__fb, __fe, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000596 __n -= __bs;
597 __f += __bs;
598 }
599 return __r;
600}
601
602template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
603 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
604__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
605copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
606 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
607 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
608{
609 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
610 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
Evgeniy Stepanov746572b2015-11-06 22:02:29 +0000611 const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000612 difference_type __n = __l - __f;
613 while (__n > 0)
614 {
615 pointer __fb = __f.__ptr_;
Evgeniy Stepanov746572b2015-11-06 22:02:29 +0000616 pointer __fe = *__f.__m_iter_ + __block_size;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000617 difference_type __bs = __fe - __fb;
618 if (__bs > __n)
619 {
620 __bs = __n;
621 __fe = __fb + __bs;
622 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000623 __r = _VSTD::copy(__fb, __fe, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000624 __n -= __bs;
625 __f += __bs;
626 }
627 return __r;
628}
629
630// copy_backward
631
632template <class _RAIter,
633 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
634__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
635copy_backward(_RAIter __f,
636 _RAIter __l,
637 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
638 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
639{
640 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
641 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
642 while (__f != __l)
643 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000644 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _VSTD::prev(__r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000645 pointer __rb = *__rp.__m_iter_;
646 pointer __re = __rp.__ptr_ + 1;
647 difference_type __bs = __re - __rb;
648 difference_type __n = __l - __f;
649 _RAIter __m = __f;
650 if (__n > __bs)
651 {
652 __n = __bs;
653 __m = __l - __n;
654 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000655 _VSTD::copy_backward(__m, __l, __re);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000656 __l = __m;
657 __r -= __n;
658 }
659 return __r;
660}
661
662template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
663 class _OutputIterator>
664_OutputIterator
665copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
666 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
667 _OutputIterator __r)
668{
669 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
670 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
671 difference_type __n = __l - __f;
672 while (__n > 0)
673 {
674 --__l;
675 pointer __lb = *__l.__m_iter_;
676 pointer __le = __l.__ptr_ + 1;
677 difference_type __bs = __le - __lb;
678 if (__bs > __n)
679 {
680 __bs = __n;
681 __lb = __le - __bs;
682 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000683 __r = _VSTD::copy_backward(__lb, __le, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000684 __n -= __bs;
685 __l -= __bs - 1;
686 }
687 return __r;
688}
689
690template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
691 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
692__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
693copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
694 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
695 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
696{
697 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
698 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
699 difference_type __n = __l - __f;
700 while (__n > 0)
701 {
702 --__l;
703 pointer __lb = *__l.__m_iter_;
704 pointer __le = __l.__ptr_ + 1;
705 difference_type __bs = __le - __lb;
706 if (__bs > __n)
707 {
708 __bs = __n;
709 __lb = __le - __bs;
710 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000711 __r = _VSTD::copy_backward(__lb, __le, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000712 __n -= __bs;
713 __l -= __bs - 1;
714 }
715 return __r;
716}
717
718// move
719
720template <class _RAIter,
721 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
722__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
723move(_RAIter __f,
724 _RAIter __l,
725 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
726 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
727{
728 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
729 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
Evgeniy Stepanov746572b2015-11-06 22:02:29 +0000730 const difference_type __block_size = __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::__block_size;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000731 while (__f != __l)
732 {
733 pointer __rb = __r.__ptr_;
Evgeniy Stepanov746572b2015-11-06 22:02:29 +0000734 pointer __re = *__r.__m_iter_ + __block_size;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000735 difference_type __bs = __re - __rb;
736 difference_type __n = __l - __f;
737 _RAIter __m = __l;
738 if (__n > __bs)
739 {
740 __n = __bs;
741 __m = __f + __n;
742 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000743 _VSTD::move(__f, __m, __rb);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000744 __f = __m;
745 __r += __n;
746 }
747 return __r;
748}
749
750template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
751 class _OutputIterator>
752_OutputIterator
753move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
754 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
755 _OutputIterator __r)
756{
757 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
758 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
Evgeniy Stepanov746572b2015-11-06 22:02:29 +0000759 const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000760 difference_type __n = __l - __f;
761 while (__n > 0)
762 {
763 pointer __fb = __f.__ptr_;
Evgeniy Stepanov746572b2015-11-06 22:02:29 +0000764 pointer __fe = *__f.__m_iter_ + __block_size;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000765 difference_type __bs = __fe - __fb;
766 if (__bs > __n)
767 {
768 __bs = __n;
769 __fe = __fb + __bs;
770 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000771 __r = _VSTD::move(__fb, __fe, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000772 __n -= __bs;
773 __f += __bs;
774 }
775 return __r;
776}
777
778template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
779 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
780__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
781move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
782 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
783 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
784{
785 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
786 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
Evgeniy Stepanov746572b2015-11-06 22:02:29 +0000787 const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000788 difference_type __n = __l - __f;
789 while (__n > 0)
790 {
791 pointer __fb = __f.__ptr_;
Evgeniy Stepanov746572b2015-11-06 22:02:29 +0000792 pointer __fe = *__f.__m_iter_ + __block_size;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000793 difference_type __bs = __fe - __fb;
794 if (__bs > __n)
795 {
796 __bs = __n;
797 __fe = __fb + __bs;
798 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000799 __r = _VSTD::move(__fb, __fe, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000800 __n -= __bs;
801 __f += __bs;
802 }
803 return __r;
804}
805
806// move_backward
807
808template <class _RAIter,
809 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
810__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
811move_backward(_RAIter __f,
812 _RAIter __l,
813 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
814 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
815{
816 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
817 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
818 while (__f != __l)
819 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000820 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _VSTD::prev(__r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000821 pointer __rb = *__rp.__m_iter_;
822 pointer __re = __rp.__ptr_ + 1;
823 difference_type __bs = __re - __rb;
824 difference_type __n = __l - __f;
825 _RAIter __m = __f;
826 if (__n > __bs)
827 {
828 __n = __bs;
829 __m = __l - __n;
830 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000831 _VSTD::move_backward(__m, __l, __re);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000832 __l = __m;
833 __r -= __n;
834 }
835 return __r;
836}
837
838template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
839 class _OutputIterator>
840_OutputIterator
841move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
842 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
843 _OutputIterator __r)
844{
845 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
846 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
847 difference_type __n = __l - __f;
848 while (__n > 0)
849 {
850 --__l;
851 pointer __lb = *__l.__m_iter_;
852 pointer __le = __l.__ptr_ + 1;
853 difference_type __bs = __le - __lb;
854 if (__bs > __n)
855 {
856 __bs = __n;
857 __lb = __le - __bs;
858 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000859 __r = _VSTD::move_backward(__lb, __le, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000860 __n -= __bs;
861 __l -= __bs - 1;
862 }
863 return __r;
864}
865
866template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
867 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
868__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
869move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
870 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
871 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
872{
873 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
874 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
875 difference_type __n = __l - __f;
876 while (__n > 0)
877 {
878 --__l;
879 pointer __lb = *__l.__m_iter_;
880 pointer __le = __l.__ptr_ + 1;
881 difference_type __bs = __le - __lb;
882 if (__bs > __n)
883 {
884 __bs = __n;
885 __lb = __le - __bs;
886 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000887 __r = _VSTD::move_backward(__lb, __le, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000888 __n -= __bs;
889 __l -= __bs - 1;
890 }
891 return __r;
892}
893
894template <bool>
895class __deque_base_common
896{
897protected:
898 void __throw_length_error() const;
899 void __throw_out_of_range() const;
900};
901
902template <bool __b>
903void
904__deque_base_common<__b>::__throw_length_error() const
905{
906#ifndef _LIBCPP_NO_EXCEPTIONS
907 throw length_error("deque");
908#endif
909}
910
911template <bool __b>
912void
913__deque_base_common<__b>::__throw_out_of_range() const
914{
915#ifndef _LIBCPP_NO_EXCEPTIONS
916 throw out_of_range("deque");
917#endif
918}
919
920template <class _Tp, class _Allocator>
921class __deque_base
922 : protected __deque_base_common<true>
923{
924 __deque_base(const __deque_base& __c);
925 __deque_base& operator=(const __deque_base& __c);
926protected:
927 typedef _Tp value_type;
928 typedef _Allocator allocator_type;
929 typedef allocator_traits<allocator_type> __alloc_traits;
930 typedef value_type& reference;
931 typedef const value_type& const_reference;
932 typedef typename __alloc_traits::size_type size_type;
933 typedef typename __alloc_traits::difference_type difference_type;
934 typedef typename __alloc_traits::pointer pointer;
935 typedef typename __alloc_traits::const_pointer const_pointer;
936
Evgeniy Stepanov746572b2015-11-06 22:02:29 +0000937 static const difference_type __block_size;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000938
Marshall Clow66302c62015-04-07 05:21:38 +0000939 typedef typename __rebind_alloc_helper<__alloc_traits, pointer>::type __pointer_allocator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000940 typedef allocator_traits<__pointer_allocator> __map_traits;
941 typedef typename __map_traits::pointer __map_pointer;
Marshall Clow66302c62015-04-07 05:21:38 +0000942 typedef typename __rebind_alloc_helper<__alloc_traits, const_pointer>::type __const_pointer_allocator;
Howard Hinnantfcd8db72013-06-23 21:17:24 +0000943 typedef typename allocator_traits<__const_pointer_allocator>::const_pointer __map_const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000944 typedef __split_buffer<pointer, __pointer_allocator> __map;
945
946 typedef __deque_iterator<value_type, pointer, reference, __map_pointer,
Evgeniy Stepanov746572b2015-11-06 22:02:29 +0000947 difference_type> iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000948 typedef __deque_iterator<value_type, const_pointer, const_reference, __map_const_pointer,
Evgeniy Stepanov746572b2015-11-06 22:02:29 +0000949 difference_type> const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000950
951 __map __map_;
952 size_type __start_;
953 __compressed_pair<size_type, allocator_type> __size_;
954
Howard Hinnanta12beb32011-06-02 16:10:22 +0000955 iterator begin() _NOEXCEPT;
956 const_iterator begin() const _NOEXCEPT;
957 iterator end() _NOEXCEPT;
958 const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000959
960 _LIBCPP_INLINE_VISIBILITY size_type& size() {return __size_.first();}
Howard Hinnanta12beb32011-06-02 16:10:22 +0000961 _LIBCPP_INLINE_VISIBILITY
962 const size_type& size() const _NOEXCEPT {return __size_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000963 _LIBCPP_INLINE_VISIBILITY allocator_type& __alloc() {return __size_.second();}
Howard Hinnanta12beb32011-06-02 16:10:22 +0000964 _LIBCPP_INLINE_VISIBILITY
965 const allocator_type& __alloc() const _NOEXCEPT {return __size_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000966
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000967 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant009b2c42011-06-03 15:16:49 +0000968 __deque_base()
969 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000970 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000971 explicit __deque_base(const allocator_type& __a);
Howard Hinnant0a612b02011-06-02 20:00:14 +0000972public:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000973 ~__deque_base();
974
Howard Hinnant73d21a42010-09-04 23:28:19 +0000975#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000976
Howard Hinnant0a612b02011-06-02 20:00:14 +0000977 __deque_base(__deque_base&& __c)
978 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000979 __deque_base(__deque_base&& __c, const allocator_type& __a);
980
Howard Hinnant73d21a42010-09-04 23:28:19 +0000981#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant0a612b02011-06-02 20:00:14 +0000982 void swap(__deque_base& __c)
Marshall Clow7d914d12015-07-13 20:04:56 +0000983#if _LIBCPP_STD_VER >= 14
984 _NOEXCEPT;
985#else
986 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
987 __is_nothrow_swappable<allocator_type>::value);
988#endif
Howard Hinnant0a612b02011-06-02 20:00:14 +0000989protected:
Howard Hinnanta12beb32011-06-02 16:10:22 +0000990 void clear() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000991
992 bool __invariants() const;
993
Howard Hinnant422a53f2010-09-21 21:28:23 +0000994 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000995 void __move_assign(__deque_base& __c)
Howard Hinnant18884f42011-06-02 21:38:57 +0000996 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
997 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000998 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000999 __map_ = _VSTD::move(__c.__map_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001000 __start_ = __c.__start_;
1001 size() = __c.size();
1002 __move_assign_alloc(__c);
1003 __c.__start_ = __c.size() = 0;
1004 }
1005
Howard Hinnant422a53f2010-09-21 21:28:23 +00001006 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001007 void __move_assign_alloc(__deque_base& __c)
Howard Hinnant0a612b02011-06-02 20:00:14 +00001008 _NOEXCEPT_(!__alloc_traits::propagate_on_container_move_assignment::value ||
1009 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001010 {__move_assign_alloc(__c, integral_constant<bool,
1011 __alloc_traits::propagate_on_container_move_assignment::value>());}
1012
1013private:
Howard Hinnant422a53f2010-09-21 21:28:23 +00001014 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +00001015 void __move_assign_alloc(__deque_base& __c, true_type)
Howard Hinnant0a612b02011-06-02 20:00:14 +00001016 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001017 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001018 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001019 }
1020
Howard Hinnant422a53f2010-09-21 21:28:23 +00001021 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001022 void __move_assign_alloc(__deque_base&, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001023 {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001024};
1025
1026template <class _Tp, class _Allocator>
Evgeniy Stepanov746572b2015-11-06 22:02:29 +00001027const typename __deque_base<_Tp, _Allocator>::difference_type
1028 __deque_base<_Tp, _Allocator>::__block_size =
1029 __deque_block_size<value_type, difference_type>::value;
1030
1031template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001032bool
1033__deque_base<_Tp, _Allocator>::__invariants() const
1034{
1035 if (!__map_.__invariants())
1036 return false;
1037 if (__map_.size() >= size_type(-1) / __block_size)
1038 return false;
1039 for (typename __map::const_iterator __i = __map_.begin(), __e = __map_.end();
1040 __i != __e; ++__i)
1041 if (*__i == nullptr)
1042 return false;
1043 if (__map_.size() != 0)
1044 {
1045 if (size() >= __map_.size() * __block_size)
1046 return false;
1047 if (__start_ >= __map_.size() * __block_size - size())
1048 return false;
1049 }
1050 else
1051 {
1052 if (size() != 0)
1053 return false;
1054 if (__start_ != 0)
1055 return false;
1056 }
1057 return true;
1058}
1059
1060template <class _Tp, class _Allocator>
1061typename __deque_base<_Tp, _Allocator>::iterator
Howard Hinnanta12beb32011-06-02 16:10:22 +00001062__deque_base<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001063{
1064 __map_pointer __mp = __map_.begin() + __start_ / __block_size;
1065 return iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
1066}
1067
1068template <class _Tp, class _Allocator>
1069typename __deque_base<_Tp, _Allocator>::const_iterator
Howard Hinnanta12beb32011-06-02 16:10:22 +00001070__deque_base<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001071{
Howard Hinnantfcd8db72013-06-23 21:17:24 +00001072 __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __start_ / __block_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001073 return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
1074}
1075
1076template <class _Tp, class _Allocator>
1077typename __deque_base<_Tp, _Allocator>::iterator
Howard Hinnanta12beb32011-06-02 16:10:22 +00001078__deque_base<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001079{
1080 size_type __p = size() + __start_;
1081 __map_pointer __mp = __map_.begin() + __p / __block_size;
1082 return iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
1083}
1084
1085template <class _Tp, class _Allocator>
1086typename __deque_base<_Tp, _Allocator>::const_iterator
Howard Hinnanta12beb32011-06-02 16:10:22 +00001087__deque_base<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001088{
1089 size_type __p = size() + __start_;
Howard Hinnantfcd8db72013-06-23 21:17:24 +00001090 __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __p / __block_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001091 return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
1092}
1093
1094template <class _Tp, class _Allocator>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001095inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001096__deque_base<_Tp, _Allocator>::__deque_base()
Howard Hinnant009b2c42011-06-03 15:16:49 +00001097 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001098 : __start_(0), __size_(0) {}
1099
1100template <class _Tp, class _Allocator>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001101inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001102__deque_base<_Tp, _Allocator>::__deque_base(const allocator_type& __a)
1103 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a) {}
1104
1105template <class _Tp, class _Allocator>
1106__deque_base<_Tp, _Allocator>::~__deque_base()
1107{
1108 clear();
1109 typename __map::iterator __i = __map_.begin();
1110 typename __map::iterator __e = __map_.end();
1111 for (; __i != __e; ++__i)
1112 __alloc_traits::deallocate(__alloc(), *__i, __block_size);
1113}
1114
Howard Hinnant73d21a42010-09-04 23:28:19 +00001115#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001116
1117template <class _Tp, class _Allocator>
1118__deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c)
Howard Hinnant0a612b02011-06-02 20:00:14 +00001119 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001120 : __map_(_VSTD::move(__c.__map_)),
1121 __start_(_VSTD::move(__c.__start_)),
1122 __size_(_VSTD::move(__c.__size_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001123{
1124 __c.__start_ = 0;
1125 __c.size() = 0;
1126}
1127
1128template <class _Tp, class _Allocator>
1129__deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c, const allocator_type& __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001130 : __map_(_VSTD::move(__c.__map_), __pointer_allocator(__a)),
1131 __start_(_VSTD::move(__c.__start_)),
1132 __size_(_VSTD::move(__c.size()), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001133{
1134 if (__a == __c.__alloc())
1135 {
1136 __c.__start_ = 0;
1137 __c.size() = 0;
1138 }
1139 else
1140 {
1141 __map_.clear();
1142 __start_ = 0;
1143 size() = 0;
1144 }
1145}
1146
Howard Hinnant73d21a42010-09-04 23:28:19 +00001147#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001148
1149template <class _Tp, class _Allocator>
1150void
1151__deque_base<_Tp, _Allocator>::swap(__deque_base& __c)
Marshall Clow7d914d12015-07-13 20:04:56 +00001152#if _LIBCPP_STD_VER >= 14
1153 _NOEXCEPT
1154#else
1155 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1156 __is_nothrow_swappable<allocator_type>::value)
1157#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001158{
1159 __map_.swap(__c.__map_);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001160 _VSTD::swap(__start_, __c.__start_);
1161 _VSTD::swap(size(), __c.size());
Marshall Clow7d914d12015-07-13 20:04:56 +00001162 __swap_allocator(__alloc(), __c.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001163}
1164
1165template <class _Tp, class _Allocator>
1166void
Howard Hinnanta12beb32011-06-02 16:10:22 +00001167__deque_base<_Tp, _Allocator>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001168{
1169 allocator_type& __a = __alloc();
1170 for (iterator __i = begin(), __e = end(); __i != __e; ++__i)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001171 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001172 size() = 0;
1173 while (__map_.size() > 2)
1174 {
1175 __alloc_traits::deallocate(__a, __map_.front(), __block_size);
1176 __map_.pop_front();
1177 }
1178 switch (__map_.size())
1179 {
1180 case 1:
1181 __start_ = __block_size / 2;
1182 break;
1183 case 2:
1184 __start_ = __block_size;
1185 break;
1186 }
1187}
1188
Marshall Clowceead9c2015-02-18 17:24:08 +00001189template <class _Tp, class _Allocator /*= allocator<_Tp>*/>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001190class _LIBCPP_TYPE_VIS_ONLY deque
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001191 : private __deque_base<_Tp, _Allocator>
1192{
1193public:
1194 // types:
Howard Hinnant324bb032010-08-22 00:02:43 +00001195
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001196 typedef _Tp value_type;
1197 typedef _Allocator allocator_type;
1198
1199 typedef __deque_base<value_type, allocator_type> __base;
1200
1201 typedef typename __base::__alloc_traits __alloc_traits;
1202 typedef typename __base::reference reference;
1203 typedef typename __base::const_reference const_reference;
1204 typedef typename __base::iterator iterator;
1205 typedef typename __base::const_iterator const_iterator;
1206 typedef typename __base::size_type size_type;
1207 typedef typename __base::difference_type difference_type;
1208
1209 typedef typename __base::pointer pointer;
1210 typedef typename __base::const_pointer const_pointer;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001211 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
1212 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001213
1214 // construct/copy/destroy:
Howard Hinnant009b2c42011-06-03 15:16:49 +00001215 _LIBCPP_INLINE_VISIBILITY
1216 deque()
1217 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
1218 {}
Marshall Clow48c74702014-03-05 19:06:20 +00001219 _LIBCPP_INLINE_VISIBILITY explicit deque(const allocator_type& __a) : __base(__a) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001220 explicit deque(size_type __n);
Marshall Clowab04aad2013-09-07 16:16:19 +00001221#if _LIBCPP_STD_VER > 11
1222 explicit deque(size_type __n, const _Allocator& __a);
1223#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001224 deque(size_type __n, const value_type& __v);
1225 deque(size_type __n, const value_type& __v, const allocator_type& __a);
1226 template <class _InputIter>
1227 deque(_InputIter __f, _InputIter __l,
1228 typename enable_if<__is_input_iterator<_InputIter>::value>::type* = 0);
1229 template <class _InputIter>
1230 deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
1231 typename enable_if<__is_input_iterator<_InputIter>::value>::type* = 0);
1232 deque(const deque& __c);
1233 deque(const deque& __c, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +00001234#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001235 deque(initializer_list<value_type> __il);
1236 deque(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +00001237#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001238
1239 deque& operator=(const deque& __c);
Howard Hinnante3e32912011-08-12 21:56:02 +00001240#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant422a53f2010-09-21 21:28:23 +00001241 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001242 deque& operator=(initializer_list<value_type> __il) {assign(__il); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +00001243#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001244
Howard Hinnant73d21a42010-09-04 23:28:19 +00001245#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001246 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0a612b02011-06-02 20:00:14 +00001247 deque(deque&& __c) _NOEXCEPT_(is_nothrow_move_constructible<__base>::value);
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001248 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001249 deque(deque&& __c, const allocator_type& __a);
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001250 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0a612b02011-06-02 20:00:14 +00001251 deque& operator=(deque&& __c)
Howard Hinnant18884f42011-06-02 21:38:57 +00001252 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1253 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001254#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001255
1256 template <class _InputIter>
1257 void assign(_InputIter __f, _InputIter __l,
1258 typename enable_if<__is_input_iterator<_InputIter>::value &&
1259 !__is_random_access_iterator<_InputIter>::value>::type* = 0);
1260 template <class _RAIter>
1261 void assign(_RAIter __f, _RAIter __l,
1262 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
1263 void assign(size_type __n, const value_type& __v);
Howard Hinnante3e32912011-08-12 21:56:02 +00001264#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant422a53f2010-09-21 21:28:23 +00001265 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001266 void assign(initializer_list<value_type> __il) {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00001267#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001268
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001269 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001270 allocator_type get_allocator() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001271
1272 // iterators:
1273
Howard Hinnant422a53f2010-09-21 21:28:23 +00001274 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001275 iterator begin() _NOEXCEPT {return __base::begin();}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001276 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001277 const_iterator begin() const _NOEXCEPT {return __base::begin();}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001278 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001279 iterator end() _NOEXCEPT {return __base::end();}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001280 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001281 const_iterator end() const _NOEXCEPT {return __base::end();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001282
Howard Hinnant422a53f2010-09-21 21:28:23 +00001283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001284 reverse_iterator rbegin() _NOEXCEPT
1285 {return reverse_iterator(__base::end());}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001286 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001287 const_reverse_iterator rbegin() const _NOEXCEPT
1288 {return const_reverse_iterator(__base::end());}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001289 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001290 reverse_iterator rend() _NOEXCEPT
1291 {return reverse_iterator(__base::begin());}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001292 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001293 const_reverse_iterator rend() const _NOEXCEPT
1294 {return const_reverse_iterator(__base::begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001295
Howard Hinnant422a53f2010-09-21 21:28:23 +00001296 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001297 const_iterator cbegin() const _NOEXCEPT
1298 {return __base::begin();}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001300 const_iterator cend() const _NOEXCEPT
1301 {return __base::end();}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001302 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001303 const_reverse_iterator crbegin() const _NOEXCEPT
1304 {return const_reverse_iterator(__base::end());}
Howard Hinnant422a53f2010-09-21 21:28:23 +00001305 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001306 const_reverse_iterator crend() const _NOEXCEPT
1307 {return const_reverse_iterator(__base::begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001308
1309 // capacity:
Howard Hinnant422a53f2010-09-21 21:28:23 +00001310 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001311 size_type size() const _NOEXCEPT {return __base::size();}
1312 _LIBCPP_INLINE_VISIBILITY
1313 size_type max_size() const _NOEXCEPT
1314 {return __alloc_traits::max_size(__base::__alloc());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001315 void resize(size_type __n);
1316 void resize(size_type __n, const value_type& __v);
Howard Hinnant18884f42011-06-02 21:38:57 +00001317 void shrink_to_fit() _NOEXCEPT;
Howard Hinnanta12beb32011-06-02 16:10:22 +00001318 _LIBCPP_INLINE_VISIBILITY
1319 bool empty() const _NOEXCEPT {return __base::size() == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001320
1321 // element access:
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001322 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001323 reference operator[](size_type __i);
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001324 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001325 const_reference operator[](size_type __i) const;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001326 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001327 reference at(size_type __i);
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001328 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001329 const_reference at(size_type __i) const;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001330 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001331 reference front();
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001332 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001333 const_reference front() const;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001334 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001335 reference back();
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001336 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001337 const_reference back() const;
1338
1339 // 23.2.2.3 modifiers:
1340 void push_front(const value_type& __v);
1341 void push_back(const value_type& __v);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001342#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1343#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001344 template <class... _Args> void emplace_front(_Args&&... __args);
1345 template <class... _Args> void emplace_back(_Args&&... __args);
1346 template <class... _Args> iterator emplace(const_iterator __p, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001347#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001348 void push_front(value_type&& __v);
1349 void push_back(value_type&& __v);
1350 iterator insert(const_iterator __p, value_type&& __v);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001351#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001352 iterator insert(const_iterator __p, const value_type& __v);
1353 iterator insert(const_iterator __p, size_type __n, const value_type& __v);
1354 template <class _InputIter>
Marshall Clow3150c352015-01-22 18:33:29 +00001355 iterator insert(const_iterator __p, _InputIter __f, _InputIter __l,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001356 typename enable_if<__is_input_iterator<_InputIter>::value
Marshall Clow3150c352015-01-22 18:33:29 +00001357 &&!__is_forward_iterator<_InputIter>::value>::type* = 0);
1358 template <class _ForwardIterator>
1359 iterator insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l,
1360 typename enable_if<__is_forward_iterator<_ForwardIterator>::value
1361 &&!__is_bidirectional_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001362 template <class _BiIter>
Marshall Clow3150c352015-01-22 18:33:29 +00001363 iterator insert(const_iterator __p, _BiIter __f, _BiIter __l,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001364 typename enable_if<__is_bidirectional_iterator<_BiIter>::value>::type* = 0);
Howard Hinnante3e32912011-08-12 21:56:02 +00001365#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant422a53f2010-09-21 21:28:23 +00001366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001367 iterator insert(const_iterator __p, initializer_list<value_type> __il)
1368 {return insert(__p, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00001369#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001370 void pop_front();
1371 void pop_back();
1372 iterator erase(const_iterator __p);
1373 iterator erase(const_iterator __f, const_iterator __l);
1374
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001375 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0a612b02011-06-02 20:00:14 +00001376 void swap(deque& __c)
Marshall Clow7d914d12015-07-13 20:04:56 +00001377#if _LIBCPP_STD_VER >= 14
1378 _NOEXCEPT;
1379#else
Howard Hinnant0a612b02011-06-02 20:00:14 +00001380 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1381 __is_nothrow_swappable<allocator_type>::value);
Marshall Clow7d914d12015-07-13 20:04:56 +00001382#endif
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001383 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta12beb32011-06-02 16:10:22 +00001384 void clear() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001385
Howard Hinnant422a53f2010-09-21 21:28:23 +00001386 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001387 bool __invariants() const {return __base::__invariants();}
1388private:
Howard Hinnantfcd8db72013-06-23 21:17:24 +00001389 typedef typename __base::__map_const_pointer __map_const_pointer;
1390
Howard Hinnant422a53f2010-09-21 21:28:23 +00001391 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001392 static size_type __recommend_blocks(size_type __n)
1393 {
1394 return __n / __base::__block_size + (__n % __base::__block_size != 0);
1395 }
Howard Hinnant422a53f2010-09-21 21:28:23 +00001396 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001397 size_type __capacity() const
1398 {
1399 return __base::__map_.size() == 0 ? 0 : __base::__map_.size() * __base::__block_size - 1;
1400 }
Howard Hinnant422a53f2010-09-21 21:28:23 +00001401 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001402 size_type __front_spare() const
1403 {
1404 return __base::__start_;
1405 }
Howard Hinnant422a53f2010-09-21 21:28:23 +00001406 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001407 size_type __back_spare() const
1408 {
1409 return __capacity() - (__base::__start_ + __base::size());
1410 }
1411
1412 template <class _InpIter>
1413 void __append(_InpIter __f, _InpIter __l,
1414 typename enable_if<__is_input_iterator<_InpIter>::value &&
1415 !__is_forward_iterator<_InpIter>::value>::type* = 0);
1416 template <class _ForIter>
1417 void __append(_ForIter __f, _ForIter __l,
1418 typename enable_if<__is_forward_iterator<_ForIter>::value>::type* = 0);
1419 void __append(size_type __n);
1420 void __append(size_type __n, const value_type& __v);
1421 void __erase_to_end(const_iterator __f);
1422 void __add_front_capacity();
1423 void __add_front_capacity(size_type __n);
1424 void __add_back_capacity();
1425 void __add_back_capacity(size_type __n);
1426 iterator __move_and_check(iterator __f, iterator __l, iterator __r,
1427 const_pointer& __vt);
1428 iterator __move_backward_and_check(iterator __f, iterator __l, iterator __r,
1429 const_pointer& __vt);
1430 void __move_construct_and_check(iterator __f, iterator __l,
1431 iterator __r, const_pointer& __vt);
1432 void __move_construct_backward_and_check(iterator __f, iterator __l,
1433 iterator __r, const_pointer& __vt);
1434
Howard Hinnant422a53f2010-09-21 21:28:23 +00001435 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001436 void __copy_assign_alloc(const deque& __c)
1437 {__copy_assign_alloc(__c, integral_constant<bool,
1438 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1439
Howard Hinnant422a53f2010-09-21 21:28:23 +00001440 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001441 void __copy_assign_alloc(const deque& __c, true_type)
1442 {
1443 if (__base::__alloc() != __c.__alloc())
1444 {
1445 clear();
1446 shrink_to_fit();
1447 }
1448 __base::__alloc() = __c.__alloc();
1449 __base::__map_.__alloc() = __c.__map_.__alloc();
1450 }
1451
Howard Hinnant422a53f2010-09-21 21:28:23 +00001452 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001453 void __copy_assign_alloc(const deque&, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001454 {}
1455
Howard Hinnant18884f42011-06-02 21:38:57 +00001456 void __move_assign(deque& __c, true_type)
1457 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001458 void __move_assign(deque& __c, false_type);
1459};
1460
1461template <class _Tp, class _Allocator>
1462deque<_Tp, _Allocator>::deque(size_type __n)
1463{
1464 if (__n > 0)
1465 __append(__n);
1466}
1467
Marshall Clowab04aad2013-09-07 16:16:19 +00001468#if _LIBCPP_STD_VER > 11
1469template <class _Tp, class _Allocator>
1470deque<_Tp, _Allocator>::deque(size_type __n, const _Allocator& __a)
1471 : __base(__a)
1472{
1473 if (__n > 0)
1474 __append(__n);
1475}
1476#endif
1477
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001478template <class _Tp, class _Allocator>
1479deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v)
1480{
1481 if (__n > 0)
1482 __append(__n, __v);
1483}
1484
1485template <class _Tp, class _Allocator>
1486deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v, const allocator_type& __a)
1487 : __base(__a)
1488{
1489 if (__n > 0)
1490 __append(__n, __v);
1491}
1492
1493template <class _Tp, class _Allocator>
1494template <class _InputIter>
1495deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l,
1496 typename enable_if<__is_input_iterator<_InputIter>::value>::type*)
1497{
1498 __append(__f, __l);
1499}
1500
1501template <class _Tp, class _Allocator>
1502template <class _InputIter>
1503deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
1504 typename enable_if<__is_input_iterator<_InputIter>::value>::type*)
1505 : __base(__a)
1506{
1507 __append(__f, __l);
1508}
1509
1510template <class _Tp, class _Allocator>
1511deque<_Tp, _Allocator>::deque(const deque& __c)
1512 : __base(__alloc_traits::select_on_container_copy_construction(__c.__alloc()))
1513{
1514 __append(__c.begin(), __c.end());
1515}
1516
1517template <class _Tp, class _Allocator>
1518deque<_Tp, _Allocator>::deque(const deque& __c, const allocator_type& __a)
1519 : __base(__a)
1520{
1521 __append(__c.begin(), __c.end());
1522}
1523
Howard Hinnante3e32912011-08-12 21:56:02 +00001524#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1525
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001526template <class _Tp, class _Allocator>
1527deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il)
1528{
1529 __append(__il.begin(), __il.end());
1530}
1531
1532template <class _Tp, class _Allocator>
1533deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il, const allocator_type& __a)
1534 : __base(__a)
1535{
1536 __append(__il.begin(), __il.end());
1537}
1538
Howard Hinnante3e32912011-08-12 21:56:02 +00001539#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1540
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001541template <class _Tp, class _Allocator>
1542deque<_Tp, _Allocator>&
1543deque<_Tp, _Allocator>::operator=(const deque& __c)
1544{
1545 if (this != &__c)
1546 {
1547 __copy_assign_alloc(__c);
1548 assign(__c.begin(), __c.end());
1549 }
1550 return *this;
1551}
1552
Howard Hinnant73d21a42010-09-04 23:28:19 +00001553#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001554
1555template <class _Tp, class _Allocator>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001556inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001557deque<_Tp, _Allocator>::deque(deque&& __c)
Howard Hinnant0a612b02011-06-02 20:00:14 +00001558 _NOEXCEPT_(is_nothrow_move_constructible<__base>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001559 : __base(_VSTD::move(__c))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001560{
1561}
1562
1563template <class _Tp, class _Allocator>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001564inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001565deque<_Tp, _Allocator>::deque(deque&& __c, const allocator_type& __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001566 : __base(_VSTD::move(__c), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001567{
1568 if (__a != __c.__alloc())
1569 {
Howard Hinnant99968442011-11-29 18:15:50 +00001570 typedef move_iterator<iterator> _Ip;
1571 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001572 }
1573}
1574
1575template <class _Tp, class _Allocator>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001576inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001577deque<_Tp, _Allocator>&
1578deque<_Tp, _Allocator>::operator=(deque&& __c)
Howard Hinnant18884f42011-06-02 21:38:57 +00001579 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1580 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001581{
1582 __move_assign(__c, integral_constant<bool,
1583 __alloc_traits::propagate_on_container_move_assignment::value>());
1584 return *this;
1585}
1586
1587template <class _Tp, class _Allocator>
1588void
1589deque<_Tp, _Allocator>::__move_assign(deque& __c, false_type)
1590{
1591 if (__base::__alloc() != __c.__alloc())
1592 {
Howard Hinnant99968442011-11-29 18:15:50 +00001593 typedef move_iterator<iterator> _Ip;
1594 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001595 }
1596 else
1597 __move_assign(__c, true_type());
1598}
1599
1600template <class _Tp, class _Allocator>
1601void
1602deque<_Tp, _Allocator>::__move_assign(deque& __c, true_type)
Howard Hinnant18884f42011-06-02 21:38:57 +00001603 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001604{
1605 clear();
1606 shrink_to_fit();
1607 __base::__move_assign(__c);
1608}
1609
Howard Hinnant73d21a42010-09-04 23:28:19 +00001610#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001611
1612template <class _Tp, class _Allocator>
1613template <class _InputIter>
1614void
1615deque<_Tp, _Allocator>::assign(_InputIter __f, _InputIter __l,
1616 typename enable_if<__is_input_iterator<_InputIter>::value &&
1617 !__is_random_access_iterator<_InputIter>::value>::type*)
1618{
1619 iterator __i = __base::begin();
1620 iterator __e = __base::end();
Eric Fiselierb9919752014-10-27 19:28:20 +00001621 for (; __f != __l && __i != __e; ++__f, (void) ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001622 *__i = *__f;
1623 if (__f != __l)
1624 __append(__f, __l);
1625 else
1626 __erase_to_end(__i);
1627}
1628
1629template <class _Tp, class _Allocator>
1630template <class _RAIter>
1631void
1632deque<_Tp, _Allocator>::assign(_RAIter __f, _RAIter __l,
1633 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
1634{
1635 if (static_cast<size_type>(__l - __f) > __base::size())
1636 {
1637 _RAIter __m = __f + __base::size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001638 _VSTD::copy(__f, __m, __base::begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001639 __append(__m, __l);
1640 }
1641 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00001642 __erase_to_end(_VSTD::copy(__f, __l, __base::begin()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001643}
1644
1645template <class _Tp, class _Allocator>
1646void
1647deque<_Tp, _Allocator>::assign(size_type __n, const value_type& __v)
1648{
1649 if (__n > __base::size())
1650 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001651 _VSTD::fill_n(__base::begin(), __base::size(), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001652 __n -= __base::size();
1653 __append(__n, __v);
1654 }
1655 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00001656 __erase_to_end(_VSTD::fill_n(__base::begin(), __n, __v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001657}
1658
1659template <class _Tp, class _Allocator>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001660inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001661_Allocator
Howard Hinnanta12beb32011-06-02 16:10:22 +00001662deque<_Tp, _Allocator>::get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001663{
1664 return __base::__alloc();
1665}
1666
1667template <class _Tp, class _Allocator>
1668void
1669deque<_Tp, _Allocator>::resize(size_type __n)
1670{
1671 if (__n > __base::size())
1672 __append(__n - __base::size());
1673 else if (__n < __base::size())
1674 __erase_to_end(__base::begin() + __n);
1675}
1676
1677template <class _Tp, class _Allocator>
1678void
1679deque<_Tp, _Allocator>::resize(size_type __n, const value_type& __v)
1680{
1681 if (__n > __base::size())
1682 __append(__n - __base::size(), __v);
1683 else if (__n < __base::size())
1684 __erase_to_end(__base::begin() + __n);
1685}
1686
1687template <class _Tp, class _Allocator>
1688void
Howard Hinnant18884f42011-06-02 21:38:57 +00001689deque<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001690{
1691 allocator_type& __a = __base::__alloc();
1692 if (empty())
1693 {
1694 while (__base::__map_.size() > 0)
1695 {
1696 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
1697 __base::__map_.pop_back();
1698 }
1699 __base::__start_ = 0;
1700 }
1701 else
1702 {
1703 if (__front_spare() >= __base::__block_size)
1704 {
1705 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
1706 __base::__map_.pop_front();
1707 __base::__start_ -= __base::__block_size;
1708 }
1709 if (__back_spare() >= __base::__block_size)
1710 {
1711 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
1712 __base::__map_.pop_back();
1713 }
1714 }
1715 __base::__map_.shrink_to_fit();
1716}
1717
1718template <class _Tp, class _Allocator>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001719inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001720typename deque<_Tp, _Allocator>::reference
1721deque<_Tp, _Allocator>::operator[](size_type __i)
1722{
1723 size_type __p = __base::__start_ + __i;
1724 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1725}
1726
1727template <class _Tp, class _Allocator>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001728inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001729typename deque<_Tp, _Allocator>::const_reference
1730deque<_Tp, _Allocator>::operator[](size_type __i) const
1731{
1732 size_type __p = __base::__start_ + __i;
1733 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1734}
1735
1736template <class _Tp, class _Allocator>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001737inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001738typename deque<_Tp, _Allocator>::reference
1739deque<_Tp, _Allocator>::at(size_type __i)
1740{
1741 if (__i >= __base::size())
1742 __base::__throw_out_of_range();
1743 size_type __p = __base::__start_ + __i;
1744 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1745}
1746
1747template <class _Tp, class _Allocator>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001748inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001749typename deque<_Tp, _Allocator>::const_reference
1750deque<_Tp, _Allocator>::at(size_type __i) const
1751{
1752 if (__i >= __base::size())
1753 __base::__throw_out_of_range();
1754 size_type __p = __base::__start_ + __i;
1755 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1756}
1757
1758template <class _Tp, class _Allocator>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001759inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001760typename deque<_Tp, _Allocator>::reference
1761deque<_Tp, _Allocator>::front()
1762{
1763 return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1764 + __base::__start_ % __base::__block_size);
1765}
1766
1767template <class _Tp, class _Allocator>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001768inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001769typename deque<_Tp, _Allocator>::const_reference
1770deque<_Tp, _Allocator>::front() const
1771{
1772 return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1773 + __base::__start_ % __base::__block_size);
1774}
1775
1776template <class _Tp, class _Allocator>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001777inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001778typename deque<_Tp, _Allocator>::reference
1779deque<_Tp, _Allocator>::back()
1780{
1781 size_type __p = __base::size() + __base::__start_ - 1;
1782 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1783}
1784
1785template <class _Tp, class _Allocator>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001786inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001787typename deque<_Tp, _Allocator>::const_reference
1788deque<_Tp, _Allocator>::back() const
1789{
1790 size_type __p = __base::size() + __base::__start_ - 1;
1791 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1792}
1793
1794template <class _Tp, class _Allocator>
1795void
1796deque<_Tp, _Allocator>::push_back(const value_type& __v)
1797{
1798 allocator_type& __a = __base::__alloc();
1799 if (__back_spare() == 0)
1800 __add_back_capacity();
1801 // __back_spare() >= 1
Howard Hinnant0949eed2011-06-30 21:18:19 +00001802 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001803 ++__base::size();
1804}
1805
Howard Hinnant73d21a42010-09-04 23:28:19 +00001806#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001807
1808template <class _Tp, class _Allocator>
1809void
1810deque<_Tp, _Allocator>::push_back(value_type&& __v)
1811{
1812 allocator_type& __a = __base::__alloc();
1813 if (__back_spare() == 0)
1814 __add_back_capacity();
1815 // __back_spare() >= 1
Howard Hinnant0949eed2011-06-30 21:18:19 +00001816 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001817 ++__base::size();
1818}
1819
Howard Hinnant73d21a42010-09-04 23:28:19 +00001820#ifndef _LIBCPP_HAS_NO_VARIADICS
1821
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001822template <class _Tp, class _Allocator>
1823template <class... _Args>
1824void
1825deque<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1826{
1827 allocator_type& __a = __base::__alloc();
1828 if (__back_spare() == 0)
1829 __add_back_capacity();
1830 // __back_spare() >= 1
Howard Hinnant0949eed2011-06-30 21:18:19 +00001831 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001832 ++__base::size();
1833}
1834
Howard Hinnant73d21a42010-09-04 23:28:19 +00001835#endif // _LIBCPP_HAS_NO_VARIADICS
1836#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001837
1838template <class _Tp, class _Allocator>
1839void
1840deque<_Tp, _Allocator>::push_front(const value_type& __v)
1841{
1842 allocator_type& __a = __base::__alloc();
1843 if (__front_spare() == 0)
1844 __add_front_capacity();
1845 // __front_spare() >= 1
Howard Hinnant0949eed2011-06-30 21:18:19 +00001846 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001847 --__base::__start_;
1848 ++__base::size();
1849}
1850
Howard Hinnant73d21a42010-09-04 23:28:19 +00001851#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001852
1853template <class _Tp, class _Allocator>
1854void
1855deque<_Tp, _Allocator>::push_front(value_type&& __v)
1856{
1857 allocator_type& __a = __base::__alloc();
1858 if (__front_spare() == 0)
1859 __add_front_capacity();
1860 // __front_spare() >= 1
Howard Hinnant0949eed2011-06-30 21:18:19 +00001861 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001862 --__base::__start_;
1863 ++__base::size();
1864}
1865
Howard Hinnant73d21a42010-09-04 23:28:19 +00001866#ifndef _LIBCPP_HAS_NO_VARIADICS
1867
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001868template <class _Tp, class _Allocator>
1869template <class... _Args>
1870void
1871deque<_Tp, _Allocator>::emplace_front(_Args&&... __args)
1872{
1873 allocator_type& __a = __base::__alloc();
1874 if (__front_spare() == 0)
1875 __add_front_capacity();
1876 // __front_spare() >= 1
Howard Hinnant0949eed2011-06-30 21:18:19 +00001877 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001878 --__base::__start_;
1879 ++__base::size();
1880}
1881
Howard Hinnant73d21a42010-09-04 23:28:19 +00001882#endif // _LIBCPP_HAS_NO_VARIADICS
1883#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001884
1885template <class _Tp, class _Allocator>
1886typename deque<_Tp, _Allocator>::iterator
1887deque<_Tp, _Allocator>::insert(const_iterator __p, const value_type& __v)
1888{
1889 size_type __pos = __p - __base::begin();
1890 size_type __to_end = __base::size() - __pos;
1891 allocator_type& __a = __base::__alloc();
1892 if (__pos < __to_end)
1893 { // insert by shifting things backward
1894 if (__front_spare() == 0)
1895 __add_front_capacity();
1896 // __front_spare() >= 1
1897 if (__pos == 0)
1898 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001899 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001900 --__base::__start_;
1901 ++__base::size();
1902 }
1903 else
1904 {
1905 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1906 iterator __b = __base::begin();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001907 iterator __bm1 = _VSTD::prev(__b);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001908 if (__vt == pointer_traits<const_pointer>::pointer_to(*__b))
1909 __vt = pointer_traits<const_pointer>::pointer_to(*__bm1);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001910 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001911 --__base::__start_;
1912 ++__base::size();
1913 if (__pos > 1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001914 __b = __move_and_check(_VSTD::next(__b), __b + __pos, __b, __vt);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001915 *__b = *__vt;
1916 }
1917 }
1918 else
1919 { // insert by shifting things forward
1920 if (__back_spare() == 0)
1921 __add_back_capacity();
1922 // __back_capacity >= 1
1923 size_type __de = __base::size() - __pos;
1924 if (__de == 0)
1925 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001926 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001927 ++__base::size();
1928 }
1929 else
1930 {
1931 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1932 iterator __e = __base::end();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001933 iterator __em1 = _VSTD::prev(__e);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001934 if (__vt == pointer_traits<const_pointer>::pointer_to(*__em1))
1935 __vt = pointer_traits<const_pointer>::pointer_to(*__e);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001936 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001937 ++__base::size();
1938 if (__de > 1)
1939 __e = __move_backward_and_check(__e - __de, __em1, __e, __vt);
1940 *--__e = *__vt;
1941 }
1942 }
1943 return __base::begin() + __pos;
1944}
1945
Howard Hinnant73d21a42010-09-04 23:28:19 +00001946#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001947
1948template <class _Tp, class _Allocator>
1949typename deque<_Tp, _Allocator>::iterator
1950deque<_Tp, _Allocator>::insert(const_iterator __p, value_type&& __v)
1951{
1952 size_type __pos = __p - __base::begin();
1953 size_type __to_end = __base::size() - __pos;
1954 allocator_type& __a = __base::__alloc();
1955 if (__pos < __to_end)
1956 { // insert by shifting things backward
1957 if (__front_spare() == 0)
1958 __add_front_capacity();
1959 // __front_spare() >= 1
1960 if (__pos == 0)
1961 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001962 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001963 --__base::__start_;
1964 ++__base::size();
1965 }
1966 else
1967 {
1968 iterator __b = __base::begin();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001969 iterator __bm1 = _VSTD::prev(__b);
1970 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001971 --__base::__start_;
1972 ++__base::size();
1973 if (__pos > 1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001974 __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
1975 *__b = _VSTD::move(__v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001976 }
1977 }
1978 else
1979 { // insert by shifting things forward
1980 if (__back_spare() == 0)
1981 __add_back_capacity();
1982 // __back_capacity >= 1
1983 size_type __de = __base::size() - __pos;
1984 if (__de == 0)
1985 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001986 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001987 ++__base::size();
1988 }
1989 else
1990 {
1991 iterator __e = __base::end();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001992 iterator __em1 = _VSTD::prev(__e);
1993 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001994 ++__base::size();
1995 if (__de > 1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001996 __e = _VSTD::move_backward(__e - __de, __em1, __e);
1997 *--__e = _VSTD::move(__v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001998 }
1999 }
2000 return __base::begin() + __pos;
2001}
2002
Howard Hinnant73d21a42010-09-04 23:28:19 +00002003#ifndef _LIBCPP_HAS_NO_VARIADICS
2004
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002005template <class _Tp, class _Allocator>
2006template <class... _Args>
2007typename deque<_Tp, _Allocator>::iterator
2008deque<_Tp, _Allocator>::emplace(const_iterator __p, _Args&&... __args)
2009{
2010 size_type __pos = __p - __base::begin();
2011 size_type __to_end = __base::size() - __pos;
2012 allocator_type& __a = __base::__alloc();
2013 if (__pos < __to_end)
2014 { // insert by shifting things backward
2015 if (__front_spare() == 0)
2016 __add_front_capacity();
2017 // __front_spare() >= 1
2018 if (__pos == 0)
2019 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002020 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002021 --__base::__start_;
2022 ++__base::size();
2023 }
2024 else
2025 {
Howard Hinnanta58402a2012-07-08 23:23:04 +00002026 value_type __tmp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002027 iterator __b = __base::begin();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002028 iterator __bm1 = _VSTD::prev(__b);
2029 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002030 --__base::__start_;
2031 ++__base::size();
2032 if (__pos > 1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002033 __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
Howard Hinnanta58402a2012-07-08 23:23:04 +00002034 *__b = _VSTD::move(__tmp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002035 }
2036 }
2037 else
2038 { // insert by shifting things forward
2039 if (__back_spare() == 0)
2040 __add_back_capacity();
2041 // __back_capacity >= 1
2042 size_type __de = __base::size() - __pos;
2043 if (__de == 0)
2044 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002045 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002046 ++__base::size();
2047 }
2048 else
2049 {
Howard Hinnanta58402a2012-07-08 23:23:04 +00002050 value_type __tmp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002051 iterator __e = __base::end();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002052 iterator __em1 = _VSTD::prev(__e);
2053 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002054 ++__base::size();
2055 if (__de > 1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002056 __e = _VSTD::move_backward(__e - __de, __em1, __e);
Howard Hinnanta58402a2012-07-08 23:23:04 +00002057 *--__e = _VSTD::move(__tmp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002058 }
2059 }
2060 return __base::begin() + __pos;
2061}
2062
Howard Hinnant73d21a42010-09-04 23:28:19 +00002063#endif // _LIBCPP_HAS_NO_VARIADICS
2064#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002065
2066template <class _Tp, class _Allocator>
2067typename deque<_Tp, _Allocator>::iterator
2068deque<_Tp, _Allocator>::insert(const_iterator __p, size_type __n, const value_type& __v)
2069{
2070 size_type __pos = __p - __base::begin();
2071 size_type __to_end = __base::size() - __pos;
2072 allocator_type& __a = __base::__alloc();
2073 if (__pos < __to_end)
2074 { // insert by shifting things backward
2075 if (__n > __front_spare())
2076 __add_front_capacity(__n - __front_spare());
2077 // __n <= __front_spare()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002078 iterator __old_begin = __base::begin();
2079 iterator __i = __old_begin;
2080 if (__n > __pos)
2081 {
2082 for (size_type __m = __n - __pos; __m; --__m, --__base::__start_, ++__base::size())
Howard Hinnant0949eed2011-06-30 21:18:19 +00002083 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002084 __n = __pos;
2085 }
2086 if (__n > 0)
2087 {
2088 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2089 iterator __obn = __old_begin + __n;
2090 __move_construct_backward_and_check(__old_begin, __obn, __i, __vt);
2091 if (__n < __pos)
2092 __old_begin = __move_and_check(__obn, __old_begin + __pos, __old_begin, __vt);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002093 _VSTD::fill_n(__old_begin, __n, *__vt);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002094 }
2095 }
2096 else
2097 { // insert by shifting things forward
2098 size_type __back_capacity = __back_spare();
2099 if (__n > __back_capacity)
2100 __add_back_capacity(__n - __back_capacity);
2101 // __n <= __back_capacity
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002102 iterator __old_end = __base::end();
2103 iterator __i = __old_end;
2104 size_type __de = __base::size() - __pos;
2105 if (__n > __de)
2106 {
2107 for (size_type __m = __n - __de; __m; --__m, ++__i, ++__base::size())
Howard Hinnant0949eed2011-06-30 21:18:19 +00002108 __alloc_traits::construct(__a, _VSTD::addressof(*__i), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002109 __n = __de;
2110 }
2111 if (__n > 0)
2112 {
2113 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2114 iterator __oen = __old_end - __n;
2115 __move_construct_and_check(__oen, __old_end, __i, __vt);
2116 if (__n < __de)
2117 __old_end = __move_backward_and_check(__old_end - __de, __oen, __old_end, __vt);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002118 _VSTD::fill_n(__old_end - __n, __n, *__vt);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002119 }
2120 }
2121 return __base::begin() + __pos;
2122}
2123
2124template <class _Tp, class _Allocator>
2125template <class _InputIter>
2126typename deque<_Tp, _Allocator>::iterator
2127deque<_Tp, _Allocator>::insert(const_iterator __p, _InputIter __f, _InputIter __l,
2128 typename enable_if<__is_input_iterator<_InputIter>::value
Marshall Clow3150c352015-01-22 18:33:29 +00002129 &&!__is_forward_iterator<_InputIter>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002130{
2131 __split_buffer<value_type, allocator_type&> __buf(__base::__alloc());
2132 __buf.__construct_at_end(__f, __l);
2133 typedef typename __split_buffer<value_type, allocator_type&>::iterator __bi;
2134 return insert(__p, move_iterator<__bi>(__buf.begin()), move_iterator<__bi>(__buf.end()));
2135}
2136
2137template <class _Tp, class _Allocator>
Marshall Clow3150c352015-01-22 18:33:29 +00002138template <class _ForwardIterator>
2139typename deque<_Tp, _Allocator>::iterator
2140deque<_Tp, _Allocator>::insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l,
2141 typename enable_if<__is_forward_iterator<_ForwardIterator>::value
2142 &&!__is_bidirectional_iterator<_ForwardIterator>::value>::type*)
2143{
2144 size_type __n = _VSTD::distance(__f, __l);
2145 __split_buffer<value_type, allocator_type&> __buf(__n, 0, __base::__alloc());
2146 __buf.__construct_at_end(__f, __l);
2147 typedef typename __split_buffer<value_type, allocator_type&>::iterator __fwd;
2148 return insert(__p, move_iterator<__fwd>(__buf.begin()), move_iterator<__fwd>(__buf.end()));
2149}
2150
2151template <class _Tp, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002152template <class _BiIter>
2153typename deque<_Tp, _Allocator>::iterator
2154deque<_Tp, _Allocator>::insert(const_iterator __p, _BiIter __f, _BiIter __l,
2155 typename enable_if<__is_bidirectional_iterator<_BiIter>::value>::type*)
2156{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002157 size_type __n = _VSTD::distance(__f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002158 size_type __pos = __p - __base::begin();
2159 size_type __to_end = __base::size() - __pos;
2160 allocator_type& __a = __base::__alloc();
2161 if (__pos < __to_end)
2162 { // insert by shifting things backward
2163 if (__n > __front_spare())
2164 __add_front_capacity(__n - __front_spare());
2165 // __n <= __front_spare()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002166 iterator __old_begin = __base::begin();
2167 iterator __i = __old_begin;
2168 _BiIter __m = __f;
2169 if (__n > __pos)
2170 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002171 __m = __pos < __n / 2 ? _VSTD::prev(__l, __pos) : _VSTD::next(__f, __n - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002172 for (_BiIter __j = __m; __j != __f; --__base::__start_, ++__base::size())
Howard Hinnant0949eed2011-06-30 21:18:19 +00002173 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), *--__j);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002174 __n = __pos;
2175 }
2176 if (__n > 0)
2177 {
2178 iterator __obn = __old_begin + __n;
2179 for (iterator __j = __obn; __j != __old_begin;)
2180 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002181 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), _VSTD::move(*--__j));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002182 --__base::__start_;
2183 ++__base::size();
2184 }
2185 if (__n < __pos)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002186 __old_begin = _VSTD::move(__obn, __old_begin + __pos, __old_begin);
2187 _VSTD::copy(__m, __l, __old_begin);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002188 }
2189 }
2190 else
2191 { // insert by shifting things forward
2192 size_type __back_capacity = __back_spare();
2193 if (__n > __back_capacity)
2194 __add_back_capacity(__n - __back_capacity);
2195 // __n <= __back_capacity
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002196 iterator __old_end = __base::end();
2197 iterator __i = __old_end;
2198 _BiIter __m = __l;
2199 size_type __de = __base::size() - __pos;
2200 if (__n > __de)
2201 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002202 __m = __de < __n / 2 ? _VSTD::next(__f, __de) : _VSTD::prev(__l, __n - __de);
Eric Fiselierb9919752014-10-27 19:28:20 +00002203 for (_BiIter __j = __m; __j != __l; ++__i, (void) ++__j, ++__base::size())
Howard Hinnant0949eed2011-06-30 21:18:19 +00002204 __alloc_traits::construct(__a, _VSTD::addressof(*__i), *__j);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002205 __n = __de;
2206 }
2207 if (__n > 0)
2208 {
2209 iterator __oen = __old_end - __n;
2210 for (iterator __j = __oen; __j != __old_end; ++__i, ++__j, ++__base::size())
Howard Hinnant0949eed2011-06-30 21:18:19 +00002211 __alloc_traits::construct(__a, _VSTD::addressof(*__i), _VSTD::move(*__j));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002212 if (__n < __de)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002213 __old_end = _VSTD::move_backward(__old_end - __de, __oen, __old_end);
2214 _VSTD::copy_backward(__f, __m, __old_end);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002215 }
2216 }
2217 return __base::begin() + __pos;
2218}
2219
2220template <class _Tp, class _Allocator>
2221template <class _InpIter>
2222void
2223deque<_Tp, _Allocator>::__append(_InpIter __f, _InpIter __l,
2224 typename enable_if<__is_input_iterator<_InpIter>::value &&
2225 !__is_forward_iterator<_InpIter>::value>::type*)
2226{
2227 for (; __f != __l; ++__f)
2228 push_back(*__f);
2229}
2230
2231template <class _Tp, class _Allocator>
2232template <class _ForIter>
2233void
2234deque<_Tp, _Allocator>::__append(_ForIter __f, _ForIter __l,
2235 typename enable_if<__is_forward_iterator<_ForIter>::value>::type*)
2236{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002237 size_type __n = _VSTD::distance(__f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002238 allocator_type& __a = __base::__alloc();
2239 size_type __back_capacity = __back_spare();
2240 if (__n > __back_capacity)
2241 __add_back_capacity(__n - __back_capacity);
2242 // __n <= __back_capacity
Eric Fiselierb9919752014-10-27 19:28:20 +00002243 for (iterator __i = __base::end(); __f != __l; ++__i, (void) ++__f, ++__base::size())
Howard Hinnant0949eed2011-06-30 21:18:19 +00002244 __alloc_traits::construct(__a, _VSTD::addressof(*__i), *__f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002245}
2246
2247template <class _Tp, class _Allocator>
2248void
2249deque<_Tp, _Allocator>::__append(size_type __n)
2250{
2251 allocator_type& __a = __base::__alloc();
2252 size_type __back_capacity = __back_spare();
2253 if (__n > __back_capacity)
2254 __add_back_capacity(__n - __back_capacity);
2255 // __n <= __back_capacity
2256 for (iterator __i = __base::end(); __n; --__n, ++__i, ++__base::size())
Howard Hinnant0949eed2011-06-30 21:18:19 +00002257 __alloc_traits::construct(__a, _VSTD::addressof(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002258}
2259
2260template <class _Tp, class _Allocator>
2261void
2262deque<_Tp, _Allocator>::__append(size_type __n, const value_type& __v)
2263{
2264 allocator_type& __a = __base::__alloc();
2265 size_type __back_capacity = __back_spare();
2266 if (__n > __back_capacity)
2267 __add_back_capacity(__n - __back_capacity);
2268 // __n <= __back_capacity
2269 for (iterator __i = __base::end(); __n; --__n, ++__i, ++__base::size())
Howard Hinnant0949eed2011-06-30 21:18:19 +00002270 __alloc_traits::construct(__a, _VSTD::addressof(*__i), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002271}
2272
2273// Create front capacity for one block of elements.
2274// Strong guarantee. Either do it or don't touch anything.
2275template <class _Tp, class _Allocator>
2276void
2277deque<_Tp, _Allocator>::__add_front_capacity()
2278{
2279 allocator_type& __a = __base::__alloc();
2280 if (__back_spare() >= __base::__block_size)
2281 {
2282 __base::__start_ += __base::__block_size;
2283 pointer __pt = __base::__map_.back();
2284 __base::__map_.pop_back();
2285 __base::__map_.push_front(__pt);
2286 }
2287 // Else if __base::__map_.size() < __base::__map_.capacity() then we need to allocate 1 buffer
2288 else if (__base::__map_.size() < __base::__map_.capacity())
2289 { // we can put the new buffer into the map, but don't shift things around
2290 // until all buffers are allocated. If we throw, we don't need to fix
2291 // anything up (any added buffers are undetectible)
2292 if (__base::__map_.__front_spare() > 0)
2293 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2294 else
2295 {
2296 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2297 // Done allocating, reorder capacity
2298 pointer __pt = __base::__map_.back();
2299 __base::__map_.pop_back();
2300 __base::__map_.push_front(__pt);
2301 }
2302 __base::__start_ = __base::__map_.size() == 1 ?
2303 __base::__block_size / 2 :
2304 __base::__start_ + __base::__block_size;
2305 }
2306 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2307 else
2308 {
2309 __split_buffer<pointer, typename __base::__pointer_allocator&>
2310 __buf(max<size_type>(2 * __base::__map_.capacity(), 1),
2311 0, __base::__map_.__alloc());
Marshall Clowd07fcd62015-03-09 17:08:51 +00002312
Marshall Clow7d914d12015-07-13 20:04:56 +00002313 typedef __allocator_destructor<_Allocator> _Dp;
2314 unique_ptr<pointer, _Dp> __hold(
2315 __alloc_traits::allocate(__a, __base::__block_size),
2316 _Dp(__a, __base::__block_size));
2317 __buf.push_back(__hold.get());
2318 __hold.release();
2319
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002320 for (typename __base::__map_pointer __i = __base::__map_.begin();
2321 __i != __base::__map_.end(); ++__i)
2322 __buf.push_back(*__i);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002323 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2324 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2325 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2326 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002327 __base::__start_ = __base::__map_.size() == 1 ?
2328 __base::__block_size / 2 :
2329 __base::__start_ + __base::__block_size;
2330 }
2331}
2332
2333// Create front capacity for __n elements.
2334// Strong guarantee. Either do it or don't touch anything.
2335template <class _Tp, class _Allocator>
2336void
2337deque<_Tp, _Allocator>::__add_front_capacity(size_type __n)
2338{
2339 allocator_type& __a = __base::__alloc();
2340 size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2341 // Number of unused blocks at back:
2342 size_type __back_capacity = __back_spare() / __base::__block_size;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002343 __back_capacity = _VSTD::min(__back_capacity, __nb); // don't take more than you need
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002344 __nb -= __back_capacity; // number of blocks need to allocate
2345 // If __nb == 0, then we have sufficient capacity.
2346 if (__nb == 0)
2347 {
2348 __base::__start_ += __base::__block_size * __back_capacity;
2349 for (; __back_capacity > 0; --__back_capacity)
2350 {
2351 pointer __pt = __base::__map_.back();
2352 __base::__map_.pop_back();
2353 __base::__map_.push_front(__pt);
2354 }
2355 }
2356 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2357 else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2358 { // we can put the new buffers into the map, but don't shift things around
2359 // until all buffers are allocated. If we throw, we don't need to fix
2360 // anything up (any added buffers are undetectible)
2361 for (; __nb > 0; --__nb, __base::__start_ += __base::__block_size - (__base::__map_.size() == 1))
2362 {
2363 if (__base::__map_.__front_spare() == 0)
2364 break;
2365 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2366 }
2367 for (; __nb > 0; --__nb, ++__back_capacity)
2368 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2369 // Done allocating, reorder capacity
2370 __base::__start_ += __back_capacity * __base::__block_size;
2371 for (; __back_capacity > 0; --__back_capacity)
2372 {
2373 pointer __pt = __base::__map_.back();
2374 __base::__map_.pop_back();
2375 __base::__map_.push_front(__pt);
2376 }
2377 }
2378 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2379 else
2380 {
2381 size_type __ds = (__nb + __back_capacity) * __base::__block_size - __base::__map_.empty();
2382 __split_buffer<pointer, typename __base::__pointer_allocator&>
2383 __buf(max<size_type>(2* __base::__map_.capacity(),
2384 __nb + __base::__map_.size()),
2385 0, __base::__map_.__alloc());
2386#ifndef _LIBCPP_NO_EXCEPTIONS
2387 try
2388 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002389#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002390 for (; __nb > 0; --__nb)
2391 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2392#ifndef _LIBCPP_NO_EXCEPTIONS
2393 }
2394 catch (...)
2395 {
2396 for (typename __base::__map_pointer __i = __buf.begin();
2397 __i != __buf.end(); ++__i)
2398 __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2399 throw;
2400 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002401#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002402 for (; __back_capacity > 0; --__back_capacity)
2403 {
2404 __buf.push_back(__base::__map_.back());
2405 __base::__map_.pop_back();
2406 }
2407 for (typename __base::__map_pointer __i = __base::__map_.begin();
2408 __i != __base::__map_.end(); ++__i)
2409 __buf.push_back(*__i);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002410 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2411 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2412 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2413 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002414 __base::__start_ += __ds;
2415 }
2416}
2417
2418// Create back capacity for one block of elements.
2419// Strong guarantee. Either do it or don't touch anything.
2420template <class _Tp, class _Allocator>
2421void
2422deque<_Tp, _Allocator>::__add_back_capacity()
2423{
2424 allocator_type& __a = __base::__alloc();
2425 if (__front_spare() >= __base::__block_size)
2426 {
2427 __base::__start_ -= __base::__block_size;
2428 pointer __pt = __base::__map_.front();
2429 __base::__map_.pop_front();
2430 __base::__map_.push_back(__pt);
2431 }
2432 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2433 else if (__base::__map_.size() < __base::__map_.capacity())
2434 { // we can put the new buffer into the map, but don't shift things around
2435 // until it is allocated. If we throw, we don't need to fix
2436 // anything up (any added buffers are undetectible)
2437 if (__base::__map_.__back_spare() != 0)
2438 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2439 else
2440 {
2441 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2442 // Done allocating, reorder capacity
2443 pointer __pt = __base::__map_.front();
2444 __base::__map_.pop_front();
2445 __base::__map_.push_back(__pt);
2446 }
2447 }
2448 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2449 else
2450 {
2451 __split_buffer<pointer, typename __base::__pointer_allocator&>
2452 __buf(max<size_type>(2* __base::__map_.capacity(), 1),
2453 __base::__map_.size(),
2454 __base::__map_.__alloc());
Marshall Clowd07fcd62015-03-09 17:08:51 +00002455
Marshall Clow7d914d12015-07-13 20:04:56 +00002456 typedef __allocator_destructor<_Allocator> _Dp;
2457 unique_ptr<pointer, _Dp> __hold(
2458 __alloc_traits::allocate(__a, __base::__block_size),
2459 _Dp(__a, __base::__block_size));
2460 __buf.push_back(__hold.get());
2461 __hold.release();
Marshall Clowd07fcd62015-03-09 17:08:51 +00002462
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002463 for (typename __base::__map_pointer __i = __base::__map_.end();
2464 __i != __base::__map_.begin();)
2465 __buf.push_front(*--__i);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002466 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2467 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2468 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2469 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002470 }
2471}
2472
2473// Create back capacity for __n elements.
2474// Strong guarantee. Either do it or don't touch anything.
2475template <class _Tp, class _Allocator>
2476void
2477deque<_Tp, _Allocator>::__add_back_capacity(size_type __n)
2478{
2479 allocator_type& __a = __base::__alloc();
2480 size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2481 // Number of unused blocks at front:
2482 size_type __front_capacity = __front_spare() / __base::__block_size;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002483 __front_capacity = _VSTD::min(__front_capacity, __nb); // don't take more than you need
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002484 __nb -= __front_capacity; // number of blocks need to allocate
2485 // If __nb == 0, then we have sufficient capacity.
2486 if (__nb == 0)
2487 {
2488 __base::__start_ -= __base::__block_size * __front_capacity;
2489 for (; __front_capacity > 0; --__front_capacity)
2490 {
2491 pointer __pt = __base::__map_.front();
2492 __base::__map_.pop_front();
2493 __base::__map_.push_back(__pt);
2494 }
2495 }
2496 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2497 else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2498 { // we can put the new buffers into the map, but don't shift things around
2499 // until all buffers are allocated. If we throw, we don't need to fix
2500 // anything up (any added buffers are undetectible)
2501 for (; __nb > 0; --__nb)
2502 {
2503 if (__base::__map_.__back_spare() == 0)
2504 break;
2505 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2506 }
2507 for (; __nb > 0; --__nb, ++__front_capacity, __base::__start_ +=
2508 __base::__block_size - (__base::__map_.size() == 1))
2509 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2510 // Done allocating, reorder capacity
2511 __base::__start_ -= __base::__block_size * __front_capacity;
2512 for (; __front_capacity > 0; --__front_capacity)
2513 {
2514 pointer __pt = __base::__map_.front();
2515 __base::__map_.pop_front();
2516 __base::__map_.push_back(__pt);
2517 }
2518 }
2519 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2520 else
2521 {
2522 size_type __ds = __front_capacity * __base::__block_size;
2523 __split_buffer<pointer, typename __base::__pointer_allocator&>
2524 __buf(max<size_type>(2* __base::__map_.capacity(),
2525 __nb + __base::__map_.size()),
2526 __base::__map_.size() - __front_capacity,
2527 __base::__map_.__alloc());
2528#ifndef _LIBCPP_NO_EXCEPTIONS
2529 try
2530 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002531#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002532 for (; __nb > 0; --__nb)
2533 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2534#ifndef _LIBCPP_NO_EXCEPTIONS
2535 }
2536 catch (...)
2537 {
2538 for (typename __base::__map_pointer __i = __buf.begin();
2539 __i != __buf.end(); ++__i)
2540 __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2541 throw;
2542 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002543#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002544 for (; __front_capacity > 0; --__front_capacity)
2545 {
2546 __buf.push_back(__base::__map_.front());
2547 __base::__map_.pop_front();
2548 }
2549 for (typename __base::__map_pointer __i = __base::__map_.end();
2550 __i != __base::__map_.begin();)
2551 __buf.push_front(*--__i);
Howard Hinnant0949eed2011-06-30 21:18:19 +00002552 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2553 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2554 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2555 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002556 __base::__start_ -= __ds;
2557 }
2558}
2559
2560template <class _Tp, class _Allocator>
2561void
2562deque<_Tp, _Allocator>::pop_front()
2563{
2564 allocator_type& __a = __base::__alloc();
Howard Hinnantfcd8db72013-06-23 21:17:24 +00002565 __alloc_traits::destroy(__a, __to_raw_pointer(*(__base::__map_.begin() +
2566 __base::__start_ / __base::__block_size) +
2567 __base::__start_ % __base::__block_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002568 --__base::size();
2569 if (++__base::__start_ >= 2 * __base::__block_size)
2570 {
2571 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2572 __base::__map_.pop_front();
2573 __base::__start_ -= __base::__block_size;
2574 }
2575}
2576
2577template <class _Tp, class _Allocator>
2578void
2579deque<_Tp, _Allocator>::pop_back()
2580{
2581 allocator_type& __a = __base::__alloc();
2582 size_type __p = __base::size() + __base::__start_ - 1;
Howard Hinnantfcd8db72013-06-23 21:17:24 +00002583 __alloc_traits::destroy(__a, __to_raw_pointer(*(__base::__map_.begin() +
2584 __p / __base::__block_size) +
2585 __p % __base::__block_size));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002586 --__base::size();
2587 if (__back_spare() >= 2 * __base::__block_size)
2588 {
2589 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2590 __base::__map_.pop_back();
2591 }
2592}
2593
2594// move assign [__f, __l) to [__r, __r + (__l-__f)).
2595// If __vt points into [__f, __l), then subtract (__f - __r) from __vt.
2596template <class _Tp, class _Allocator>
2597typename deque<_Tp, _Allocator>::iterator
2598deque<_Tp, _Allocator>::__move_and_check(iterator __f, iterator __l, iterator __r,
2599 const_pointer& __vt)
2600{
2601 // as if
2602 // for (; __f != __l; ++__f, ++__r)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002603 // *__r = _VSTD::move(*__f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002604 difference_type __n = __l - __f;
2605 while (__n > 0)
2606 {
2607 pointer __fb = __f.__ptr_;
2608 pointer __fe = *__f.__m_iter_ + __base::__block_size;
2609 difference_type __bs = __fe - __fb;
2610 if (__bs > __n)
2611 {
2612 __bs = __n;
2613 __fe = __fb + __bs;
2614 }
2615 if (__fb <= __vt && __vt < __fe)
Howard Hinnantfcd8db72013-06-23 21:17:24 +00002616 __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) -= __f - __r).__ptr_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002617 __r = _VSTD::move(__fb, __fe, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002618 __n -= __bs;
2619 __f += __bs;
2620 }
2621 return __r;
2622}
2623
2624// move assign [__f, __l) to [__r - (__l-__f), __r) backwards.
2625// If __vt points into [__f, __l), then add (__r - __l) to __vt.
2626template <class _Tp, class _Allocator>
2627typename deque<_Tp, _Allocator>::iterator
2628deque<_Tp, _Allocator>::__move_backward_and_check(iterator __f, iterator __l, iterator __r,
2629 const_pointer& __vt)
2630{
2631 // as if
2632 // while (__f != __l)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002633 // *--__r = _VSTD::move(*--__l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002634 difference_type __n = __l - __f;
2635 while (__n > 0)
2636 {
2637 --__l;
2638 pointer __lb = *__l.__m_iter_;
2639 pointer __le = __l.__ptr_ + 1;
2640 difference_type __bs = __le - __lb;
2641 if (__bs > __n)
2642 {
2643 __bs = __n;
2644 __lb = __le - __bs;
2645 }
2646 if (__lb <= __vt && __vt < __le)
Howard Hinnantfcd8db72013-06-23 21:17:24 +00002647 __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) += __r - __l - 1).__ptr_;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002648 __r = _VSTD::move_backward(__lb, __le, __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002649 __n -= __bs;
2650 __l -= __bs - 1;
2651 }
2652 return __r;
2653}
2654
2655// move construct [__f, __l) to [__r, __r + (__l-__f)).
2656// If __vt points into [__f, __l), then add (__r - __f) to __vt.
2657template <class _Tp, class _Allocator>
2658void
2659deque<_Tp, _Allocator>::__move_construct_and_check(iterator __f, iterator __l,
2660 iterator __r, const_pointer& __vt)
2661{
2662 allocator_type& __a = __base::__alloc();
2663 // as if
2664 // for (; __f != __l; ++__r, ++__f, ++__base::size())
Howard Hinnant0949eed2011-06-30 21:18:19 +00002665 // __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__f));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002666 difference_type __n = __l - __f;
2667 while (__n > 0)
2668 {
2669 pointer __fb = __f.__ptr_;
2670 pointer __fe = *__f.__m_iter_ + __base::__block_size;
2671 difference_type __bs = __fe - __fb;
2672 if (__bs > __n)
2673 {
2674 __bs = __n;
2675 __fe = __fb + __bs;
2676 }
2677 if (__fb <= __vt && __vt < __fe)
Howard Hinnantfcd8db72013-06-23 21:17:24 +00002678 __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) += __r - __f).__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002679 for (; __fb != __fe; ++__fb, ++__r, ++__base::size())
Howard Hinnant0949eed2011-06-30 21:18:19 +00002680 __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__fb));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002681 __n -= __bs;
2682 __f += __bs;
2683 }
2684}
2685
2686// move construct [__f, __l) to [__r - (__l-__f), __r) backwards.
2687// If __vt points into [__f, __l), then subtract (__l - __r) from __vt.
2688template <class _Tp, class _Allocator>
2689void
2690deque<_Tp, _Allocator>::__move_construct_backward_and_check(iterator __f, iterator __l,
2691 iterator __r, const_pointer& __vt)
2692{
2693 allocator_type& __a = __base::__alloc();
2694 // as if
2695 // for (iterator __j = __l; __j != __f;)
2696 // {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002697 // __alloc_traitsconstruct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__j));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002698 // --__base::__start_;
2699 // ++__base::size();
2700 // }
2701 difference_type __n = __l - __f;
2702 while (__n > 0)
2703 {
2704 --__l;
2705 pointer __lb = *__l.__m_iter_;
2706 pointer __le = __l.__ptr_ + 1;
2707 difference_type __bs = __le - __lb;
2708 if (__bs > __n)
2709 {
2710 __bs = __n;
2711 __lb = __le - __bs;
2712 }
2713 if (__lb <= __vt && __vt < __le)
Howard Hinnantfcd8db72013-06-23 21:17:24 +00002714 __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) -= __l - __r + 1).__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002715 while (__le != __lb)
2716 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002717 __alloc_traits::construct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__le));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002718 --__base::__start_;
2719 ++__base::size();
2720 }
2721 __n -= __bs;
2722 __l -= __bs - 1;
2723 }
2724}
2725
2726template <class _Tp, class _Allocator>
2727typename deque<_Tp, _Allocator>::iterator
2728deque<_Tp, _Allocator>::erase(const_iterator __f)
2729{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002730 iterator __b = __base::begin();
2731 difference_type __pos = __f - __b;
2732 iterator __p = __b + __pos;
2733 allocator_type& __a = __base::__alloc();
Marshall Clow4356f632015-06-05 22:34:19 +00002734 if (__pos <= (__base::size() - 1) / 2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002735 { // erase from front
Howard Hinnant0949eed2011-06-30 21:18:19 +00002736 _VSTD::move_backward(__b, __p, _VSTD::next(__p));
2737 __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002738 --__base::size();
2739 ++__base::__start_;
2740 if (__front_spare() >= 2 * __base::__block_size)
2741 {
2742 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2743 __base::__map_.pop_front();
2744 __base::__start_ -= __base::__block_size;
2745 }
2746 }
2747 else
2748 { // erase from back
Howard Hinnant0949eed2011-06-30 21:18:19 +00002749 iterator __i = _VSTD::move(_VSTD::next(__p), __base::end(), __p);
2750 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002751 --__base::size();
2752 if (__back_spare() >= 2 * __base::__block_size)
2753 {
2754 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2755 __base::__map_.pop_back();
2756 }
2757 }
2758 return __base::begin() + __pos;
2759}
2760
2761template <class _Tp, class _Allocator>
2762typename deque<_Tp, _Allocator>::iterator
2763deque<_Tp, _Allocator>::erase(const_iterator __f, const_iterator __l)
2764{
2765 difference_type __n = __l - __f;
2766 iterator __b = __base::begin();
2767 difference_type __pos = __f - __b;
2768 iterator __p = __b + __pos;
2769 if (__n > 0)
2770 {
2771 allocator_type& __a = __base::__alloc();
Marshall Clow4356f632015-06-05 22:34:19 +00002772 if (__pos <= (__base::size() - __n) / 2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002773 { // erase from front
Howard Hinnant0949eed2011-06-30 21:18:19 +00002774 iterator __i = _VSTD::move_backward(__b, __p, __p + __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002775 for (; __b != __i; ++__b)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002776 __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002777 __base::size() -= __n;
2778 __base::__start_ += __n;
2779 while (__front_spare() >= 2 * __base::__block_size)
2780 {
2781 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2782 __base::__map_.pop_front();
2783 __base::__start_ -= __base::__block_size;
2784 }
2785 }
2786 else
2787 { // erase from back
Howard Hinnant0949eed2011-06-30 21:18:19 +00002788 iterator __i = _VSTD::move(__p + __n, __base::end(), __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002789 for (iterator __e = __base::end(); __i != __e; ++__i)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002790 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002791 __base::size() -= __n;
2792 while (__back_spare() >= 2 * __base::__block_size)
2793 {
2794 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2795 __base::__map_.pop_back();
2796 }
2797 }
2798 }
2799 return __base::begin() + __pos;
2800}
2801
2802template <class _Tp, class _Allocator>
2803void
2804deque<_Tp, _Allocator>::__erase_to_end(const_iterator __f)
2805{
2806 iterator __e = __base::end();
2807 difference_type __n = __e - __f;
2808 if (__n > 0)
2809 {
2810 allocator_type& __a = __base::__alloc();
2811 iterator __b = __base::begin();
2812 difference_type __pos = __f - __b;
2813 for (iterator __p = __b + __pos; __p != __e; ++__p)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002814 __alloc_traits::destroy(__a, _VSTD::addressof(*__p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002815 __base::size() -= __n;
2816 while (__back_spare() >= 2 * __base::__block_size)
2817 {
2818 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2819 __base::__map_.pop_back();
2820 }
2821 }
2822}
2823
2824template <class _Tp, class _Allocator>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00002825inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002826void
2827deque<_Tp, _Allocator>::swap(deque& __c)
Marshall Clow7d914d12015-07-13 20:04:56 +00002828#if _LIBCPP_STD_VER >= 14
2829 _NOEXCEPT
2830#else
2831 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2832 __is_nothrow_swappable<allocator_type>::value)
2833#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002834{
2835 __base::swap(__c);
2836}
2837
2838template <class _Tp, class _Allocator>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00002839inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002840void
Howard Hinnanta12beb32011-06-02 16:10:22 +00002841deque<_Tp, _Allocator>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002842{
2843 __base::clear();
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 const typename deque<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002852 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002853}
2854
2855template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002856inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002857bool
2858operator!=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2859{
2860 return !(__x == __y);
2861}
2862
2863template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002864inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002865bool
2866operator< (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2867{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002868 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002869}
2870
2871template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002872inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002873bool
2874operator> (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2875{
2876 return __y < __x;
2877}
2878
2879template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002880inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002881bool
2882operator>=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2883{
2884 return !(__x < __y);
2885}
2886
2887template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002888inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002889bool
2890operator<=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2891{
2892 return !(__y < __x);
2893}
2894
2895template <class _Tp, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002896inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002897void
2898swap(deque<_Tp, _Allocator>& __x, deque<_Tp, _Allocator>& __y)
Howard Hinnant0a612b02011-06-02 20:00:14 +00002899 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002900{
2901 __x.swap(__y);
2902}
2903
2904_LIBCPP_END_NAMESPACE_STD
2905
2906#endif // _LIBCPP_DEQUE