blob: 4c688132b52108240c5c23b5e032cb83337e703e [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------------- deque -----------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_DEQUE
12#define _LIBCPP_DEQUE
13
14/*
15 deque synopsis
16
17namespace std
18{
19
20template <class T, class Allocator = allocator<T> >
21class deque
22{
23public:
24 // types:
25 typedef T value_type;
26 typedef Allocator allocator_type;
27
28 typedef typename allocator_type::reference reference;
29 typedef typename allocator_type::const_reference const_reference;
30 typedef implementation-defined iterator;
31 typedef implementation-defined const_iterator;
32 typedef typename allocator_type::size_type size_type;
33 typedef typename allocator_type::difference_type difference_type;
34
35 typedef typename allocator_type::pointer pointer;
36 typedef typename allocator_type::const_pointer const_pointer;
37 typedef std::reverse_iterator<iterator> reverse_iterator;
38 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
39
40 // construct/copy/destroy:
Howard Hinnantad979ba2011-06-03 15:16:49 +000041 deque() noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +000042 explicit deque(const allocator_type& a);
43 explicit deque(size_type n);
Marshall Clow68098692013-09-09 18:19:45 +000044 explicit deque(size_type n, const allocator_type& a); // C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000045 deque(size_type n, const value_type& v);
46 deque(size_type n, const value_type& v, const allocator_type& a);
47 template <class InputIterator>
48 deque(InputIterator f, InputIterator l);
49 template <class InputIterator>
50 deque(InputIterator f, InputIterator l, const allocator_type& a);
51 deque(const deque& c);
Howard Hinnant4931e092011-06-02 21:38:57 +000052 deque(deque&& c)
53 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +000054 deque(initializer_list<value_type> il, const Allocator& a = allocator_type());
55 deque(const deque& c, const allocator_type& a);
56 deque(deque&& c, const allocator_type& a);
57 ~deque();
58
59 deque& operator=(const deque& c);
Howard Hinnant4931e092011-06-02 21:38:57 +000060 deque& operator=(deque&& c)
61 noexcept(
62 allocator_type::propagate_on_container_move_assignment::value &&
63 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +000064 deque& operator=(initializer_list<value_type> il);
65
66 template <class InputIterator>
67 void assign(InputIterator f, InputIterator l);
68 void assign(size_type n, const value_type& v);
69 void assign(initializer_list<value_type> il);
70
Howard Hinnantda2df912011-06-02 16:10:22 +000071 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000072
73 // iterators:
74
Howard Hinnantda2df912011-06-02 16:10:22 +000075 iterator begin() noexcept;
76 const_iterator begin() const noexcept;
77 iterator end() noexcept;
78 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000079
Howard Hinnantda2df912011-06-02 16:10:22 +000080 reverse_iterator rbegin() noexcept;
81 const_reverse_iterator rbegin() const noexcept;
82 reverse_iterator rend() noexcept;
83 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000084
Howard Hinnantda2df912011-06-02 16:10:22 +000085 const_iterator cbegin() const noexcept;
86 const_iterator cend() const noexcept;
87 const_reverse_iterator crbegin() const noexcept;
88 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000089
90 // capacity:
Howard Hinnantda2df912011-06-02 16:10:22 +000091 size_type size() const noexcept;
92 size_type max_size() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000093 void resize(size_type n);
94 void resize(size_type n, const value_type& v);
95 void shrink_to_fit();
Howard Hinnantda2df912011-06-02 16:10:22 +000096 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000097
98 // element access:
99 reference operator[](size_type i);
100 const_reference operator[](size_type i) const;
101 reference at(size_type i);
102 const_reference at(size_type i) const;
103 reference front();
104 const_reference front() const;
105 reference back();
106 const_reference back() const;
107
108 // modifiers:
109 void push_front(const value_type& v);
110 void push_front(value_type&& v);
111 void push_back(const value_type& v);
112 void push_back(value_type&& v);
Eric Fiselier34ba5b92016-07-21 03:20:17 +0000113 template <class... Args> reference emplace_front(Args&&... args);
114 template <class... Args> reference emplace_back(Args&&... args);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000115 template <class... Args> iterator emplace(const_iterator p, Args&&... args);
116 iterator insert(const_iterator p, const value_type& v);
117 iterator insert(const_iterator p, value_type&& v);
118 iterator insert(const_iterator p, size_type n, const value_type& v);
119 template <class InputIterator>
Marshall Clow6b612cf2015-01-22 18:33:29 +0000120 iterator insert(const_iterator p, InputIterator f, InputIterator l);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000121 iterator insert(const_iterator p, initializer_list<value_type> il);
122 void pop_front();
123 void pop_back();
124 iterator erase(const_iterator p);
125 iterator erase(const_iterator f, const_iterator l);
Howard Hinnant4931e092011-06-02 21:38:57 +0000126 void swap(deque& c)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000127 noexcept(allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantda2df912011-06-02 16:10:22 +0000128 void clear() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000129};
130
131template <class T, class Allocator>
132 bool operator==(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
133template <class T, class Allocator>
134 bool operator< (const deque<T,Allocator>& x, const deque<T,Allocator>& y);
135template <class T, class Allocator>
136 bool operator!=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
137template <class T, class Allocator>
138 bool operator> (const deque<T,Allocator>& x, const deque<T,Allocator>& y);
139template <class T, class Allocator>
140 bool operator>=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
141template <class T, class Allocator>
142 bool operator<=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
143
144// specialized algorithms:
145template <class T, class Allocator>
Howard Hinnant287548a2011-06-03 17:30:28 +0000146 void swap(deque<T,Allocator>& x, deque<T,Allocator>& y)
147 noexcept(noexcept(x.swap(y)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000148
149} // std
150
151*/
152
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000153#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000154#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000155#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000156
157#include <__config>
158#include <__split_buffer>
159#include <type_traits>
160#include <initializer_list>
161#include <iterator>
162#include <algorithm>
163#include <stdexcept>
164
Howard Hinnantc5a5fbd2011-11-29 16:45:27 +0000165#include <__undef_min_max>
166
Howard Hinnantc51e1022010-05-11 19:42:16 +0000167_LIBCPP_BEGIN_NAMESPACE_STD
168
169template <class _Tp, class _Allocator> class __deque_base;
Marshall Clow65cd4c62015-02-18 17:24:08 +0000170template <class _Tp, class _Allocator = allocator<_Tp> > class _LIBCPP_TYPE_VIS_ONLY deque;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000171
172template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
173 class _DiffType, _DiffType _BlockSize>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000174class _LIBCPP_TYPE_VIS_ONLY __deque_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000175
176template <class _RAIter,
177 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
178__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
179copy(_RAIter __f,
180 _RAIter __l,
181 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
182 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
183
184template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
185 class _OutputIterator>
186_OutputIterator
187copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
188 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
189 _OutputIterator __r);
190
191template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
192 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
193__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
194copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
195 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
196 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
197
198template <class _RAIter,
199 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
200__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
201copy_backward(_RAIter __f,
202 _RAIter __l,
203 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
204 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
205
206template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
207 class _OutputIterator>
208_OutputIterator
209copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
210 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
211 _OutputIterator __r);
212
213template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
214 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
215__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
216copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
217 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
218 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
219
220template <class _RAIter,
221 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
222__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
223move(_RAIter __f,
224 _RAIter __l,
225 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
226 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
227
228template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
229 class _OutputIterator>
230_OutputIterator
231move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
232 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
233 _OutputIterator __r);
234
235template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
236 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
237__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
238move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
239 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
240 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
241
242template <class _RAIter,
243 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
244__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
245move_backward(_RAIter __f,
246 _RAIter __l,
247 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
248 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
249
250template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
251 class _OutputIterator>
252_OutputIterator
253move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
254 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
255 _OutputIterator __r);
256
257template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
258 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
259__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
260move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
261 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
262 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
263
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000264template <class _ValueType, class _DiffType>
265struct __deque_block_size {
266 static const _DiffType value = sizeof(_ValueType) < 256 ? 4096 / sizeof(_ValueType) : 16;
267};
268
Howard Hinnantc51e1022010-05-11 19:42:16 +0000269template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000270 class _DiffType, _DiffType _BS =
271#ifdef _LIBCPP_ABI_INCOMPLETE_TYPES_IN_DEQUE
272// Keep template parameter to avoid changing all template declarations thoughout
273// this file.
274 0
275#else
276 __deque_block_size<_ValueType, _DiffType>::value
277#endif
278 >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000279class _LIBCPP_TYPE_VIS_ONLY __deque_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000280{
281 typedef _MapPointer __map_iterator;
282public:
283 typedef _Pointer pointer;
284 typedef _DiffType difference_type;
285private:
286 __map_iterator __m_iter_;
287 pointer __ptr_;
288
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000289 static const difference_type __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000290public:
291 typedef _ValueType value_type;
292 typedef random_access_iterator_tag iterator_category;
293 typedef _Reference reference;
294
Marshall Clow7a3a61d2013-08-06 16:14:36 +0000295 _LIBCPP_INLINE_VISIBILITY __deque_iterator() _NOEXCEPT
296#if _LIBCPP_STD_VER > 11
297 : __m_iter_(nullptr), __ptr_(nullptr)
298#endif
299 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000300
Howard Hinnantc834c512011-11-29 18:15:50 +0000301 template <class _Pp, class _Rp, class _MP>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000302 _LIBCPP_INLINE_VISIBILITY
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000303 __deque_iterator(const __deque_iterator<value_type, _Pp, _Rp, _MP, difference_type, _BS>& __it,
Howard Hinnantc834c512011-11-29 18:15:50 +0000304 typename enable_if<is_convertible<_Pp, pointer>::value>::type* = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000305 : __m_iter_(__it.__m_iter_), __ptr_(__it.__ptr_) {}
306
307 _LIBCPP_INLINE_VISIBILITY reference operator*() const {return *__ptr_;}
308 _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return __ptr_;}
309
310 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator++()
311 {
312 if (++__ptr_ - *__m_iter_ == __block_size)
313 {
314 ++__m_iter_;
315 __ptr_ = *__m_iter_;
316 }
317 return *this;
318 }
319
320 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator++(int)
321 {
322 __deque_iterator __tmp = *this;
323 ++(*this);
324 return __tmp;
325 }
326
327 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator--()
328 {
329 if (__ptr_ == *__m_iter_)
330 {
331 --__m_iter_;
332 __ptr_ = *__m_iter_ + __block_size;
333 }
334 --__ptr_;
335 return *this;
336 }
337
338 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator--(int)
339 {
340 __deque_iterator __tmp = *this;
341 --(*this);
342 return __tmp;
343 }
344
345 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator+=(difference_type __n)
346 {
347 if (__n != 0)
348 {
349 __n += __ptr_ - *__m_iter_;
350 if (__n > 0)
351 {
352 __m_iter_ += __n / __block_size;
353 __ptr_ = *__m_iter_ + __n % __block_size;
354 }
355 else // (__n < 0)
356 {
357 difference_type __z = __block_size - 1 - __n;
358 __m_iter_ -= __z / __block_size;
359 __ptr_ = *__m_iter_ + (__block_size - 1 - __z % __block_size);
360 }
361 }
362 return *this;
363 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000364
Howard Hinnantc51e1022010-05-11 19:42:16 +0000365 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator-=(difference_type __n)
366 {
367 return *this += -__n;
368 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000369
Howard Hinnantc51e1022010-05-11 19:42:16 +0000370 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator+(difference_type __n) const
371 {
372 __deque_iterator __t(*this);
373 __t += __n;
374 return __t;
375 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000376
Howard Hinnantc51e1022010-05-11 19:42:16 +0000377 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator-(difference_type __n) const
378 {
379 __deque_iterator __t(*this);
380 __t -= __n;
381 return __t;
382 }
383
384 _LIBCPP_INLINE_VISIBILITY
385 friend __deque_iterator operator+(difference_type __n, const __deque_iterator& __it)
386 {return __it + __n;}
387
388 _LIBCPP_INLINE_VISIBILITY
389 friend difference_type operator-(const __deque_iterator& __x, const __deque_iterator& __y)
390 {
391 if (__x != __y)
392 return (__x.__m_iter_ - __y.__m_iter_) * __block_size
393 + (__x.__ptr_ - *__x.__m_iter_)
394 - (__y.__ptr_ - *__y.__m_iter_);
395 return 0;
396 }
397
398 _LIBCPP_INLINE_VISIBILITY reference operator[](difference_type __n) const
399 {return *(*this + __n);}
400
401 _LIBCPP_INLINE_VISIBILITY friend
402 bool operator==(const __deque_iterator& __x, const __deque_iterator& __y)
403 {return __x.__ptr_ == __y.__ptr_;}
404
405 _LIBCPP_INLINE_VISIBILITY friend
406 bool operator!=(const __deque_iterator& __x, const __deque_iterator& __y)
407 {return !(__x == __y);}
408
409 _LIBCPP_INLINE_VISIBILITY friend
410 bool operator<(const __deque_iterator& __x, const __deque_iterator& __y)
411 {return __x.__m_iter_ < __y.__m_iter_ ||
412 (__x.__m_iter_ == __y.__m_iter_ && __x.__ptr_ < __y.__ptr_);}
413
414 _LIBCPP_INLINE_VISIBILITY friend
415 bool operator>(const __deque_iterator& __x, const __deque_iterator& __y)
416 {return __y < __x;}
417
418 _LIBCPP_INLINE_VISIBILITY friend
419 bool operator<=(const __deque_iterator& __x, const __deque_iterator& __y)
420 {return !(__y < __x);}
421
422 _LIBCPP_INLINE_VISIBILITY friend
423 bool operator>=(const __deque_iterator& __x, const __deque_iterator& __y)
424 {return !(__x < __y);}
425
426private:
Howard Hinnantda2df912011-06-02 16:10:22 +0000427 _LIBCPP_INLINE_VISIBILITY __deque_iterator(__map_iterator __m, pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000428 : __m_iter_(__m), __ptr_(__p) {}
429
Howard Hinnantc834c512011-11-29 18:15:50 +0000430 template <class _Tp, class _Ap> friend class __deque_base;
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000431 template <class _Tp, class _Ap> friend class _LIBCPP_TYPE_VIS_ONLY deque;
Howard Hinnantc834c512011-11-29 18:15:50 +0000432 template <class _Vp, class _Pp, class _Rp, class _MP, class _Dp, _Dp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000433 friend class _LIBCPP_TYPE_VIS_ONLY __deque_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000434
435 template <class _RAIter,
436 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
437 friend
438 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
439 copy(_RAIter __f,
440 _RAIter __l,
441 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
442 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
443
444 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
445 class _OutputIterator>
446 friend
447 _OutputIterator
448 copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
449 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
450 _OutputIterator __r);
451
452 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
453 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
454 friend
455 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
456 copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
457 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
458 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
459
460 template <class _RAIter,
461 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
462 friend
463 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
464 copy_backward(_RAIter __f,
465 _RAIter __l,
466 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
467 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
468
469 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
470 class _OutputIterator>
471 friend
472 _OutputIterator
473 copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
474 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
475 _OutputIterator __r);
476
477 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
478 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
479 friend
480 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
481 copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
482 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
483 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
484
485 template <class _RAIter,
486 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
487 friend
488 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
489 move(_RAIter __f,
490 _RAIter __l,
491 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
492 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
493
494 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
495 class _OutputIterator>
496 friend
497 _OutputIterator
498 move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
499 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
500 _OutputIterator __r);
501
502 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
503 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
504 friend
505 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
506 move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
507 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
508 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
509
510 template <class _RAIter,
511 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
512 friend
513 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
514 move_backward(_RAIter __f,
515 _RAIter __l,
516 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
517 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
518
519 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
520 class _OutputIterator>
521 friend
522 _OutputIterator
523 move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
524 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
525 _OutputIterator __r);
526
527 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
528 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
529 friend
530 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
531 move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
532 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
533 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
534};
535
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000536template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
537 class _DiffType, _DiffType _BlockSize>
538const _DiffType __deque_iterator<_ValueType, _Pointer, _Reference, _MapPointer,
539 _DiffType, _BlockSize>::__block_size =
540 __deque_block_size<_ValueType, _DiffType>::value;
541
Howard Hinnantc51e1022010-05-11 19:42:16 +0000542// copy
543
544template <class _RAIter,
545 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
546__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
547copy(_RAIter __f,
548 _RAIter __l,
549 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
550 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
551{
552 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
553 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000554 const difference_type __block_size = __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::__block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000555 while (__f != __l)
556 {
557 pointer __rb = __r.__ptr_;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000558 pointer __re = *__r.__m_iter_ + __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000559 difference_type __bs = __re - __rb;
560 difference_type __n = __l - __f;
561 _RAIter __m = __l;
562 if (__n > __bs)
563 {
564 __n = __bs;
565 __m = __f + __n;
566 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000567 _VSTD::copy(__f, __m, __rb);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000568 __f = __m;
569 __r += __n;
570 }
571 return __r;
572}
573
574template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
575 class _OutputIterator>
576_OutputIterator
577copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
578 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
579 _OutputIterator __r)
580{
581 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
582 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000583 const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000584 difference_type __n = __l - __f;
585 while (__n > 0)
586 {
587 pointer __fb = __f.__ptr_;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000588 pointer __fe = *__f.__m_iter_ + __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000589 difference_type __bs = __fe - __fb;
590 if (__bs > __n)
591 {
592 __bs = __n;
593 __fe = __fb + __bs;
594 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000595 __r = _VSTD::copy(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000596 __n -= __bs;
597 __f += __bs;
598 }
599 return __r;
600}
601
602template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
603 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
604__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
605copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
606 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
607 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
608{
609 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
610 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000611 const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000612 difference_type __n = __l - __f;
613 while (__n > 0)
614 {
615 pointer __fb = __f.__ptr_;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000616 pointer __fe = *__f.__m_iter_ + __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000617 difference_type __bs = __fe - __fb;
618 if (__bs > __n)
619 {
620 __bs = __n;
621 __fe = __fb + __bs;
622 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000623 __r = _VSTD::copy(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000624 __n -= __bs;
625 __f += __bs;
626 }
627 return __r;
628}
629
630// copy_backward
631
632template <class _RAIter,
633 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
634__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
635copy_backward(_RAIter __f,
636 _RAIter __l,
637 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
638 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
639{
640 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
641 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
642 while (__f != __l)
643 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000644 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _VSTD::prev(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000645 pointer __rb = *__rp.__m_iter_;
646 pointer __re = __rp.__ptr_ + 1;
647 difference_type __bs = __re - __rb;
648 difference_type __n = __l - __f;
649 _RAIter __m = __f;
650 if (__n > __bs)
651 {
652 __n = __bs;
653 __m = __l - __n;
654 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000655 _VSTD::copy_backward(__m, __l, __re);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000656 __l = __m;
657 __r -= __n;
658 }
659 return __r;
660}
661
662template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
663 class _OutputIterator>
664_OutputIterator
665copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
666 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
667 _OutputIterator __r)
668{
669 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
670 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
671 difference_type __n = __l - __f;
672 while (__n > 0)
673 {
674 --__l;
675 pointer __lb = *__l.__m_iter_;
676 pointer __le = __l.__ptr_ + 1;
677 difference_type __bs = __le - __lb;
678 if (__bs > __n)
679 {
680 __bs = __n;
681 __lb = __le - __bs;
682 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000683 __r = _VSTD::copy_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000684 __n -= __bs;
685 __l -= __bs - 1;
686 }
687 return __r;
688}
689
690template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
691 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
692__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
693copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
694 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
695 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
696{
697 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
698 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
699 difference_type __n = __l - __f;
700 while (__n > 0)
701 {
702 --__l;
703 pointer __lb = *__l.__m_iter_;
704 pointer __le = __l.__ptr_ + 1;
705 difference_type __bs = __le - __lb;
706 if (__bs > __n)
707 {
708 __bs = __n;
709 __lb = __le - __bs;
710 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000711 __r = _VSTD::copy_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000712 __n -= __bs;
713 __l -= __bs - 1;
714 }
715 return __r;
716}
717
718// move
719
720template <class _RAIter,
721 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
722__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
723move(_RAIter __f,
724 _RAIter __l,
725 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
726 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
727{
728 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
729 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000730 const difference_type __block_size = __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::__block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000731 while (__f != __l)
732 {
733 pointer __rb = __r.__ptr_;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000734 pointer __re = *__r.__m_iter_ + __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000735 difference_type __bs = __re - __rb;
736 difference_type __n = __l - __f;
737 _RAIter __m = __l;
738 if (__n > __bs)
739 {
740 __n = __bs;
741 __m = __f + __n;
742 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000743 _VSTD::move(__f, __m, __rb);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000744 __f = __m;
745 __r += __n;
746 }
747 return __r;
748}
749
750template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
751 class _OutputIterator>
752_OutputIterator
753move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
754 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
755 _OutputIterator __r)
756{
757 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
758 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000759 const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000760 difference_type __n = __l - __f;
761 while (__n > 0)
762 {
763 pointer __fb = __f.__ptr_;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000764 pointer __fe = *__f.__m_iter_ + __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000765 difference_type __bs = __fe - __fb;
766 if (__bs > __n)
767 {
768 __bs = __n;
769 __fe = __fb + __bs;
770 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000771 __r = _VSTD::move(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000772 __n -= __bs;
773 __f += __bs;
774 }
775 return __r;
776}
777
778template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
779 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
780__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
781move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
782 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
783 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
784{
785 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
786 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000787 const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000788 difference_type __n = __l - __f;
789 while (__n > 0)
790 {
791 pointer __fb = __f.__ptr_;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000792 pointer __fe = *__f.__m_iter_ + __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000793 difference_type __bs = __fe - __fb;
794 if (__bs > __n)
795 {
796 __bs = __n;
797 __fe = __fb + __bs;
798 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000799 __r = _VSTD::move(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000800 __n -= __bs;
801 __f += __bs;
802 }
803 return __r;
804}
805
806// move_backward
807
808template <class _RAIter,
809 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
810__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
811move_backward(_RAIter __f,
812 _RAIter __l,
813 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
814 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
815{
816 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
817 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
818 while (__f != __l)
819 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000820 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _VSTD::prev(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000821 pointer __rb = *__rp.__m_iter_;
822 pointer __re = __rp.__ptr_ + 1;
823 difference_type __bs = __re - __rb;
824 difference_type __n = __l - __f;
825 _RAIter __m = __f;
826 if (__n > __bs)
827 {
828 __n = __bs;
829 __m = __l - __n;
830 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000831 _VSTD::move_backward(__m, __l, __re);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000832 __l = __m;
833 __r -= __n;
834 }
835 return __r;
836}
837
838template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
839 class _OutputIterator>
840_OutputIterator
841move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
842 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
843 _OutputIterator __r)
844{
845 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
846 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
847 difference_type __n = __l - __f;
848 while (__n > 0)
849 {
850 --__l;
851 pointer __lb = *__l.__m_iter_;
852 pointer __le = __l.__ptr_ + 1;
853 difference_type __bs = __le - __lb;
854 if (__bs > __n)
855 {
856 __bs = __n;
857 __lb = __le - __bs;
858 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000859 __r = _VSTD::move_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000860 __n -= __bs;
861 __l -= __bs - 1;
862 }
863 return __r;
864}
865
866template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
867 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
868__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
869move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
870 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
871 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
872{
873 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
874 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
875 difference_type __n = __l - __f;
876 while (__n > 0)
877 {
878 --__l;
879 pointer __lb = *__l.__m_iter_;
880 pointer __le = __l.__ptr_ + 1;
881 difference_type __bs = __le - __lb;
882 if (__bs > __n)
883 {
884 __bs = __n;
885 __lb = __le - __bs;
886 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000887 __r = _VSTD::move_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000888 __n -= __bs;
889 __l -= __bs - 1;
890 }
891 return __r;
892}
893
894template <bool>
895class __deque_base_common
896{
897protected:
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 Stepanovc299de22015-11-06 22:02:29 +0000937 static const difference_type __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000938
Marshall Clow940e01c2015-04-07 05:21:38 +0000939 typedef typename __rebind_alloc_helper<__alloc_traits, pointer>::type __pointer_allocator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000940 typedef allocator_traits<__pointer_allocator> __map_traits;
941 typedef typename __map_traits::pointer __map_pointer;
Marshall Clow940e01c2015-04-07 05:21:38 +0000942 typedef typename __rebind_alloc_helper<__alloc_traits, const_pointer>::type __const_pointer_allocator;
Howard Hinnantdcb73a32013-06-23 21:17:24 +0000943 typedef typename allocator_traits<__const_pointer_allocator>::const_pointer __map_const_pointer;
Howard Hinnantc51e1022010-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 Stepanovc299de22015-11-06 22:02:29 +0000947 difference_type> iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000948 typedef __deque_iterator<value_type, const_pointer, const_reference, __map_const_pointer,
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000949 difference_type> const_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000950
951 __map __map_;
952 size_type __start_;
953 __compressed_pair<size_type, allocator_type> __size_;
954
Howard Hinnantda2df912011-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 Hinnantc51e1022010-05-11 19:42:16 +0000959
960 _LIBCPP_INLINE_VISIBILITY size_type& size() {return __size_.first();}
Howard Hinnantda2df912011-06-02 16:10:22 +0000961 _LIBCPP_INLINE_VISIBILITY
962 const size_type& size() const _NOEXCEPT {return __size_.first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000963 _LIBCPP_INLINE_VISIBILITY allocator_type& __alloc() {return __size_.second();}
Howard Hinnantda2df912011-06-02 16:10:22 +0000964 _LIBCPP_INLINE_VISIBILITY
965 const allocator_type& __alloc() const _NOEXCEPT {return __size_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000966
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000967 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantad979ba2011-06-03 15:16:49 +0000968 __deque_base()
969 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000970 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000971 explicit __deque_base(const allocator_type& __a);
Howard Hinnantca2fc222011-06-02 20:00:14 +0000972public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000973 ~__deque_base();
974
Howard Hinnant74279a52010-09-04 23:28:19 +0000975#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000976
Howard Hinnantca2fc222011-06-02 20:00:14 +0000977 __deque_base(__deque_base&& __c)
978 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000979 __deque_base(__deque_base&& __c, const allocator_type& __a);
980
Howard Hinnant74279a52010-09-04 23:28:19 +0000981#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantca2fc222011-06-02 20:00:14 +0000982 void swap(__deque_base& __c)
Marshall Clow8982dcd2015-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 Hinnantca2fc222011-06-02 20:00:14 +0000989protected:
Howard Hinnantda2df912011-06-02 16:10:22 +0000990 void clear() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000991
992 bool __invariants() const;
993
Howard Hinnant874ad9a2010-09-21 21:28:23 +0000994 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000995 void __move_assign(__deque_base& __c)
Howard Hinnant4931e092011-06-02 21:38:57 +0000996 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
997 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000998 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000999 __map_ = _VSTD::move(__c.__map_);
Howard Hinnantc51e1022010-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 Hinnant874ad9a2010-09-21 21:28:23 +00001006 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001007 void __move_assign_alloc(__deque_base& __c)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001008 _NOEXCEPT_(!__alloc_traits::propagate_on_container_move_assignment::value ||
1009 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-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 Hinnant874ad9a2010-09-21 21:28:23 +00001014 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001015 void __move_assign_alloc(__deque_base& __c, true_type)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001016 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001017 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001018 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001019 }
1020
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001021 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001022 void __move_assign_alloc(__deque_base&, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001023 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001024};
1025
1026template <class _Tp, class _Allocator>
Evgeniy Stepanovc299de22015-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 Hinnantc51e1022010-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 Hinnantda2df912011-06-02 16:10:22 +00001062__deque_base<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantc51e1022010-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 Hinnantda2df912011-06-02 16:10:22 +00001070__deque_base<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001071{
Howard Hinnantdcb73a32013-06-23 21:17:24 +00001072 __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __start_ / __block_size);
Howard Hinnantc51e1022010-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 Hinnantda2df912011-06-02 16:10:22 +00001078__deque_base<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantc51e1022010-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 Hinnantda2df912011-06-02 16:10:22 +00001087__deque_base<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001088{
1089 size_type __p = size() + __start_;
Howard Hinnantdcb73a32013-06-23 21:17:24 +00001090 __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __p / __block_size);
Howard Hinnantc51e1022010-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 Stepanov2fb1cb62015-11-07 01:22:13 +00001095inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001096__deque_base<_Tp, _Allocator>::__deque_base()
Howard Hinnantad979ba2011-06-03 15:16:49 +00001097 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001098 : __start_(0), __size_(0) {}
1099
1100template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001101inline
Howard Hinnantc51e1022010-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 Hinnant74279a52010-09-04 23:28:19 +00001115#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001116
1117template <class _Tp, class _Allocator>
1118__deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001119 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001120 : __map_(_VSTD::move(__c.__map_)),
1121 __start_(_VSTD::move(__c.__start_)),
1122 __size_(_VSTD::move(__c.__size_))
Howard Hinnantc51e1022010-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 Hinnantb1ad5a82011-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 Hinnantc51e1022010-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 Hinnant74279a52010-09-04 23:28:19 +00001147#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001148
1149template <class _Tp, class _Allocator>
1150void
1151__deque_base<_Tp, _Allocator>::swap(__deque_base& __c)
Marshall Clow8982dcd2015-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 Hinnantc51e1022010-05-11 19:42:16 +00001158{
1159 __map_.swap(__c.__map_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001160 _VSTD::swap(__start_, __c.__start_);
1161 _VSTD::swap(size(), __c.size());
Marshall Clow8982dcd2015-07-13 20:04:56 +00001162 __swap_allocator(__alloc(), __c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001163}
1164
1165template <class _Tp, class _Allocator>
1166void
Howard Hinnantda2df912011-06-02 16:10:22 +00001167__deque_base<_Tp, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001168{
1169 allocator_type& __a = __alloc();
1170 for (iterator __i = begin(), __e = end(); __i != __e; ++__i)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001171 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnantc51e1022010-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 Clow65cd4c62015-02-18 17:24:08 +00001189template <class _Tp, class _Allocator /*= allocator<_Tp>*/>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001190class _LIBCPP_TYPE_VIS_ONLY deque
Howard Hinnantc51e1022010-05-11 19:42:16 +00001191 : private __deque_base<_Tp, _Allocator>
1192{
1193public:
1194 // types:
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001195
Howard Hinnantc51e1022010-05-11 19:42:16 +00001196 typedef _Tp value_type;
1197 typedef _Allocator allocator_type;
1198
Marshall Clow5128cf32015-11-26 01:24:04 +00001199 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
1200 "Allocator::value_type must be same type as value_type");
1201
Howard Hinnantc51e1022010-05-11 19:42:16 +00001202 typedef __deque_base<value_type, allocator_type> __base;
1203
1204 typedef typename __base::__alloc_traits __alloc_traits;
1205 typedef typename __base::reference reference;
1206 typedef typename __base::const_reference const_reference;
1207 typedef typename __base::iterator iterator;
1208 typedef typename __base::const_iterator const_iterator;
1209 typedef typename __base::size_type size_type;
1210 typedef typename __base::difference_type difference_type;
1211
1212 typedef typename __base::pointer pointer;
1213 typedef typename __base::const_pointer const_pointer;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001214 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
1215 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001216
1217 // construct/copy/destroy:
Howard Hinnantad979ba2011-06-03 15:16:49 +00001218 _LIBCPP_INLINE_VISIBILITY
1219 deque()
1220 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
1221 {}
Marshall Clow7ed23a72014-03-05 19:06:20 +00001222 _LIBCPP_INLINE_VISIBILITY explicit deque(const allocator_type& __a) : __base(__a) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001223 explicit deque(size_type __n);
Marshall Clowbc4fcb42013-09-07 16:16:19 +00001224#if _LIBCPP_STD_VER > 11
1225 explicit deque(size_type __n, const _Allocator& __a);
1226#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001227 deque(size_type __n, const value_type& __v);
1228 deque(size_type __n, const value_type& __v, const allocator_type& __a);
1229 template <class _InputIter>
1230 deque(_InputIter __f, _InputIter __l,
1231 typename enable_if<__is_input_iterator<_InputIter>::value>::type* = 0);
1232 template <class _InputIter>
1233 deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
1234 typename enable_if<__is_input_iterator<_InputIter>::value>::type* = 0);
1235 deque(const deque& __c);
1236 deque(const deque& __c, const allocator_type& __a);
Howard Hinnant33711792011-08-12 21:56:02 +00001237#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001238 deque(initializer_list<value_type> __il);
1239 deque(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnant33711792011-08-12 21:56:02 +00001240#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001241
1242 deque& operator=(const deque& __c);
Howard Hinnant33711792011-08-12 21:56:02 +00001243#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001244 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001245 deque& operator=(initializer_list<value_type> __il) {assign(__il); return *this;}
Howard Hinnant33711792011-08-12 21:56:02 +00001246#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001247
Howard Hinnant74279a52010-09-04 23:28:19 +00001248#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001249 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantca2fc222011-06-02 20:00:14 +00001250 deque(deque&& __c) _NOEXCEPT_(is_nothrow_move_constructible<__base>::value);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001251 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001252 deque(deque&& __c, const allocator_type& __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001253 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantca2fc222011-06-02 20:00:14 +00001254 deque& operator=(deque&& __c)
Howard Hinnant4931e092011-06-02 21:38:57 +00001255 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1256 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnant74279a52010-09-04 23:28:19 +00001257#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001258
1259 template <class _InputIter>
1260 void assign(_InputIter __f, _InputIter __l,
1261 typename enable_if<__is_input_iterator<_InputIter>::value &&
1262 !__is_random_access_iterator<_InputIter>::value>::type* = 0);
1263 template <class _RAIter>
1264 void assign(_RAIter __f, _RAIter __l,
1265 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
1266 void assign(size_type __n, const value_type& __v);
Howard Hinnant33711792011-08-12 21:56:02 +00001267#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001268 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001269 void assign(initializer_list<value_type> __il) {assign(__il.begin(), __il.end());}
Howard Hinnant33711792011-08-12 21:56:02 +00001270#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001271
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001272 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001273 allocator_type get_allocator() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001274
1275 // iterators:
1276
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001277 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001278 iterator begin() _NOEXCEPT {return __base::begin();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001279 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001280 const_iterator begin() const _NOEXCEPT {return __base::begin();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001281 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001282 iterator end() _NOEXCEPT {return __base::end();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001284 const_iterator end() const _NOEXCEPT {return __base::end();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001285
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001286 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001287 reverse_iterator rbegin() _NOEXCEPT
1288 {return reverse_iterator(__base::end());}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001289 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001290 const_reverse_iterator rbegin() const _NOEXCEPT
1291 {return const_reverse_iterator(__base::end());}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001292 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001293 reverse_iterator rend() _NOEXCEPT
1294 {return reverse_iterator(__base::begin());}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001295 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001296 const_reverse_iterator rend() const _NOEXCEPT
1297 {return const_reverse_iterator(__base::begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001298
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001300 const_iterator cbegin() const _NOEXCEPT
1301 {return __base::begin();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001302 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001303 const_iterator cend() const _NOEXCEPT
1304 {return __base::end();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001305 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001306 const_reverse_iterator crbegin() const _NOEXCEPT
1307 {return const_reverse_iterator(__base::end());}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001308 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001309 const_reverse_iterator crend() const _NOEXCEPT
1310 {return const_reverse_iterator(__base::begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001311
1312 // capacity:
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001313 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001314 size_type size() const _NOEXCEPT {return __base::size();}
1315 _LIBCPP_INLINE_VISIBILITY
1316 size_type max_size() const _NOEXCEPT
1317 {return __alloc_traits::max_size(__base::__alloc());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001318 void resize(size_type __n);
1319 void resize(size_type __n, const value_type& __v);
Howard Hinnant4931e092011-06-02 21:38:57 +00001320 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantda2df912011-06-02 16:10:22 +00001321 _LIBCPP_INLINE_VISIBILITY
1322 bool empty() const _NOEXCEPT {return __base::size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001323
1324 // element access:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001325 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001326 reference operator[](size_type __i);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001327 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001328 const_reference operator[](size_type __i) const;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001329 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001330 reference at(size_type __i);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001331 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001332 const_reference at(size_type __i) const;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001333 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001334 reference front();
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001335 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001336 const_reference front() const;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001337 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001338 reference back();
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001339 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001340 const_reference back() const;
1341
1342 // 23.2.2.3 modifiers:
1343 void push_front(const value_type& __v);
1344 void push_back(const value_type& __v);
Howard Hinnant74279a52010-09-04 23:28:19 +00001345#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1346#ifndef _LIBCPP_HAS_NO_VARIADICS
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001347 template <class... _Args> reference emplace_front(_Args&&... __args);
1348 template <class... _Args> reference emplace_back(_Args&&... __args);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001349 template <class... _Args> iterator emplace(const_iterator __p, _Args&&... __args);
Howard Hinnant74279a52010-09-04 23:28:19 +00001350#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001351 void push_front(value_type&& __v);
1352 void push_back(value_type&& __v);
1353 iterator insert(const_iterator __p, value_type&& __v);
Howard Hinnant74279a52010-09-04 23:28:19 +00001354#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001355 iterator insert(const_iterator __p, const value_type& __v);
1356 iterator insert(const_iterator __p, size_type __n, const value_type& __v);
1357 template <class _InputIter>
Marshall Clow6b612cf2015-01-22 18:33:29 +00001358 iterator insert(const_iterator __p, _InputIter __f, _InputIter __l,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001359 typename enable_if<__is_input_iterator<_InputIter>::value
Marshall Clow6b612cf2015-01-22 18:33:29 +00001360 &&!__is_forward_iterator<_InputIter>::value>::type* = 0);
1361 template <class _ForwardIterator>
1362 iterator insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l,
1363 typename enable_if<__is_forward_iterator<_ForwardIterator>::value
1364 &&!__is_bidirectional_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001365 template <class _BiIter>
Marshall Clow6b612cf2015-01-22 18:33:29 +00001366 iterator insert(const_iterator __p, _BiIter __f, _BiIter __l,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001367 typename enable_if<__is_bidirectional_iterator<_BiIter>::value>::type* = 0);
Howard Hinnant33711792011-08-12 21:56:02 +00001368#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001369 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001370 iterator insert(const_iterator __p, initializer_list<value_type> __il)
1371 {return insert(__p, __il.begin(), __il.end());}
Howard Hinnant33711792011-08-12 21:56:02 +00001372#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001373 void pop_front();
1374 void pop_back();
1375 iterator erase(const_iterator __p);
1376 iterator erase(const_iterator __f, const_iterator __l);
1377
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001378 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantca2fc222011-06-02 20:00:14 +00001379 void swap(deque& __c)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001380#if _LIBCPP_STD_VER >= 14
1381 _NOEXCEPT;
1382#else
Howard Hinnantca2fc222011-06-02 20:00:14 +00001383 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1384 __is_nothrow_swappable<allocator_type>::value);
Marshall Clow8982dcd2015-07-13 20:04:56 +00001385#endif
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001386 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001387 void clear() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001388
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001389 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001390 bool __invariants() const {return __base::__invariants();}
1391private:
Howard Hinnantdcb73a32013-06-23 21:17:24 +00001392 typedef typename __base::__map_const_pointer __map_const_pointer;
1393
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001394 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001395 static size_type __recommend_blocks(size_type __n)
1396 {
1397 return __n / __base::__block_size + (__n % __base::__block_size != 0);
1398 }
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001399 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001400 size_type __capacity() const
1401 {
1402 return __base::__map_.size() == 0 ? 0 : __base::__map_.size() * __base::__block_size - 1;
1403 }
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001404 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001405 size_type __front_spare() const
1406 {
1407 return __base::__start_;
1408 }
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001409 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001410 size_type __back_spare() const
1411 {
1412 return __capacity() - (__base::__start_ + __base::size());
1413 }
1414
1415 template <class _InpIter>
1416 void __append(_InpIter __f, _InpIter __l,
1417 typename enable_if<__is_input_iterator<_InpIter>::value &&
1418 !__is_forward_iterator<_InpIter>::value>::type* = 0);
1419 template <class _ForIter>
1420 void __append(_ForIter __f, _ForIter __l,
1421 typename enable_if<__is_forward_iterator<_ForIter>::value>::type* = 0);
1422 void __append(size_type __n);
1423 void __append(size_type __n, const value_type& __v);
1424 void __erase_to_end(const_iterator __f);
1425 void __add_front_capacity();
1426 void __add_front_capacity(size_type __n);
1427 void __add_back_capacity();
1428 void __add_back_capacity(size_type __n);
1429 iterator __move_and_check(iterator __f, iterator __l, iterator __r,
1430 const_pointer& __vt);
1431 iterator __move_backward_and_check(iterator __f, iterator __l, iterator __r,
1432 const_pointer& __vt);
1433 void __move_construct_and_check(iterator __f, iterator __l,
1434 iterator __r, const_pointer& __vt);
1435 void __move_construct_backward_and_check(iterator __f, iterator __l,
1436 iterator __r, const_pointer& __vt);
1437
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001438 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001439 void __copy_assign_alloc(const deque& __c)
1440 {__copy_assign_alloc(__c, integral_constant<bool,
1441 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1442
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001443 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001444 void __copy_assign_alloc(const deque& __c, true_type)
1445 {
1446 if (__base::__alloc() != __c.__alloc())
1447 {
1448 clear();
1449 shrink_to_fit();
1450 }
1451 __base::__alloc() = __c.__alloc();
1452 __base::__map_.__alloc() = __c.__map_.__alloc();
1453 }
1454
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001455 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001456 void __copy_assign_alloc(const deque&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001457 {}
1458
Howard Hinnant4931e092011-06-02 21:38:57 +00001459 void __move_assign(deque& __c, true_type)
1460 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001461 void __move_assign(deque& __c, false_type);
1462};
1463
1464template <class _Tp, class _Allocator>
1465deque<_Tp, _Allocator>::deque(size_type __n)
1466{
1467 if (__n > 0)
1468 __append(__n);
1469}
1470
Marshall Clowbc4fcb42013-09-07 16:16:19 +00001471#if _LIBCPP_STD_VER > 11
1472template <class _Tp, class _Allocator>
1473deque<_Tp, _Allocator>::deque(size_type __n, const _Allocator& __a)
1474 : __base(__a)
1475{
1476 if (__n > 0)
1477 __append(__n);
1478}
1479#endif
1480
Howard Hinnantc51e1022010-05-11 19:42:16 +00001481template <class _Tp, class _Allocator>
1482deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v)
1483{
1484 if (__n > 0)
1485 __append(__n, __v);
1486}
1487
1488template <class _Tp, class _Allocator>
1489deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v, const allocator_type& __a)
1490 : __base(__a)
1491{
1492 if (__n > 0)
1493 __append(__n, __v);
1494}
1495
1496template <class _Tp, class _Allocator>
1497template <class _InputIter>
1498deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l,
1499 typename enable_if<__is_input_iterator<_InputIter>::value>::type*)
1500{
1501 __append(__f, __l);
1502}
1503
1504template <class _Tp, class _Allocator>
1505template <class _InputIter>
1506deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
1507 typename enable_if<__is_input_iterator<_InputIter>::value>::type*)
1508 : __base(__a)
1509{
1510 __append(__f, __l);
1511}
1512
1513template <class _Tp, class _Allocator>
1514deque<_Tp, _Allocator>::deque(const deque& __c)
1515 : __base(__alloc_traits::select_on_container_copy_construction(__c.__alloc()))
1516{
1517 __append(__c.begin(), __c.end());
1518}
1519
1520template <class _Tp, class _Allocator>
1521deque<_Tp, _Allocator>::deque(const deque& __c, const allocator_type& __a)
1522 : __base(__a)
1523{
1524 __append(__c.begin(), __c.end());
1525}
1526
Howard Hinnant33711792011-08-12 21:56:02 +00001527#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1528
Howard Hinnantc51e1022010-05-11 19:42:16 +00001529template <class _Tp, class _Allocator>
1530deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il)
1531{
1532 __append(__il.begin(), __il.end());
1533}
1534
1535template <class _Tp, class _Allocator>
1536deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il, const allocator_type& __a)
1537 : __base(__a)
1538{
1539 __append(__il.begin(), __il.end());
1540}
1541
Howard Hinnant33711792011-08-12 21:56:02 +00001542#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1543
Howard Hinnantc51e1022010-05-11 19:42:16 +00001544template <class _Tp, class _Allocator>
1545deque<_Tp, _Allocator>&
1546deque<_Tp, _Allocator>::operator=(const deque& __c)
1547{
1548 if (this != &__c)
1549 {
1550 __copy_assign_alloc(__c);
1551 assign(__c.begin(), __c.end());
1552 }
1553 return *this;
1554}
1555
Howard Hinnant74279a52010-09-04 23:28:19 +00001556#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001557
1558template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001559inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001560deque<_Tp, _Allocator>::deque(deque&& __c)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001561 _NOEXCEPT_(is_nothrow_move_constructible<__base>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001562 : __base(_VSTD::move(__c))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001563{
1564}
1565
1566template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001567inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001568deque<_Tp, _Allocator>::deque(deque&& __c, const allocator_type& __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001569 : __base(_VSTD::move(__c), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001570{
1571 if (__a != __c.__alloc())
1572 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001573 typedef move_iterator<iterator> _Ip;
1574 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001575 }
1576}
1577
1578template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001579inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580deque<_Tp, _Allocator>&
1581deque<_Tp, _Allocator>::operator=(deque&& __c)
Howard Hinnant4931e092011-06-02 21:38:57 +00001582 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1583 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001584{
1585 __move_assign(__c, integral_constant<bool,
1586 __alloc_traits::propagate_on_container_move_assignment::value>());
1587 return *this;
1588}
1589
1590template <class _Tp, class _Allocator>
1591void
1592deque<_Tp, _Allocator>::__move_assign(deque& __c, false_type)
1593{
1594 if (__base::__alloc() != __c.__alloc())
1595 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001596 typedef move_iterator<iterator> _Ip;
1597 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001598 }
1599 else
1600 __move_assign(__c, true_type());
1601}
1602
1603template <class _Tp, class _Allocator>
1604void
1605deque<_Tp, _Allocator>::__move_assign(deque& __c, true_type)
Howard Hinnant4931e092011-06-02 21:38:57 +00001606 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001607{
1608 clear();
1609 shrink_to_fit();
1610 __base::__move_assign(__c);
1611}
1612
Howard Hinnant74279a52010-09-04 23:28:19 +00001613#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001614
1615template <class _Tp, class _Allocator>
1616template <class _InputIter>
1617void
1618deque<_Tp, _Allocator>::assign(_InputIter __f, _InputIter __l,
1619 typename enable_if<__is_input_iterator<_InputIter>::value &&
1620 !__is_random_access_iterator<_InputIter>::value>::type*)
1621{
1622 iterator __i = __base::begin();
1623 iterator __e = __base::end();
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001624 for (; __f != __l && __i != __e; ++__f, (void) ++__i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001625 *__i = *__f;
1626 if (__f != __l)
1627 __append(__f, __l);
1628 else
1629 __erase_to_end(__i);
1630}
1631
1632template <class _Tp, class _Allocator>
1633template <class _RAIter>
1634void
1635deque<_Tp, _Allocator>::assign(_RAIter __f, _RAIter __l,
1636 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
1637{
1638 if (static_cast<size_type>(__l - __f) > __base::size())
1639 {
1640 _RAIter __m = __f + __base::size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001641 _VSTD::copy(__f, __m, __base::begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001642 __append(__m, __l);
1643 }
1644 else
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001645 __erase_to_end(_VSTD::copy(__f, __l, __base::begin()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001646}
1647
1648template <class _Tp, class _Allocator>
1649void
1650deque<_Tp, _Allocator>::assign(size_type __n, const value_type& __v)
1651{
1652 if (__n > __base::size())
1653 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001654 _VSTD::fill_n(__base::begin(), __base::size(), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001655 __n -= __base::size();
1656 __append(__n, __v);
1657 }
1658 else
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001659 __erase_to_end(_VSTD::fill_n(__base::begin(), __n, __v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001660}
1661
1662template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001663inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001664_Allocator
Howard Hinnantda2df912011-06-02 16:10:22 +00001665deque<_Tp, _Allocator>::get_allocator() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001666{
1667 return __base::__alloc();
1668}
1669
1670template <class _Tp, class _Allocator>
1671void
1672deque<_Tp, _Allocator>::resize(size_type __n)
1673{
1674 if (__n > __base::size())
1675 __append(__n - __base::size());
1676 else if (__n < __base::size())
1677 __erase_to_end(__base::begin() + __n);
1678}
1679
1680template <class _Tp, class _Allocator>
1681void
1682deque<_Tp, _Allocator>::resize(size_type __n, const value_type& __v)
1683{
1684 if (__n > __base::size())
1685 __append(__n - __base::size(), __v);
1686 else if (__n < __base::size())
1687 __erase_to_end(__base::begin() + __n);
1688}
1689
1690template <class _Tp, class _Allocator>
1691void
Howard Hinnant4931e092011-06-02 21:38:57 +00001692deque<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001693{
1694 allocator_type& __a = __base::__alloc();
1695 if (empty())
1696 {
1697 while (__base::__map_.size() > 0)
1698 {
1699 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
1700 __base::__map_.pop_back();
1701 }
1702 __base::__start_ = 0;
1703 }
1704 else
1705 {
1706 if (__front_spare() >= __base::__block_size)
1707 {
1708 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
1709 __base::__map_.pop_front();
1710 __base::__start_ -= __base::__block_size;
1711 }
1712 if (__back_spare() >= __base::__block_size)
1713 {
1714 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
1715 __base::__map_.pop_back();
1716 }
1717 }
1718 __base::__map_.shrink_to_fit();
1719}
1720
1721template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001722inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001723typename deque<_Tp, _Allocator>::reference
1724deque<_Tp, _Allocator>::operator[](size_type __i)
1725{
1726 size_type __p = __base::__start_ + __i;
1727 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1728}
1729
1730template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001731inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001732typename deque<_Tp, _Allocator>::const_reference
1733deque<_Tp, _Allocator>::operator[](size_type __i) const
1734{
1735 size_type __p = __base::__start_ + __i;
1736 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1737}
1738
1739template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001740inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001741typename deque<_Tp, _Allocator>::reference
1742deque<_Tp, _Allocator>::at(size_type __i)
1743{
1744 if (__i >= __base::size())
1745 __base::__throw_out_of_range();
1746 size_type __p = __base::__start_ + __i;
1747 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1748}
1749
1750template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001751inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001752typename deque<_Tp, _Allocator>::const_reference
1753deque<_Tp, _Allocator>::at(size_type __i) const
1754{
1755 if (__i >= __base::size())
1756 __base::__throw_out_of_range();
1757 size_type __p = __base::__start_ + __i;
1758 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1759}
1760
1761template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001762inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001763typename deque<_Tp, _Allocator>::reference
1764deque<_Tp, _Allocator>::front()
1765{
1766 return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1767 + __base::__start_ % __base::__block_size);
1768}
1769
1770template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001771inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001772typename deque<_Tp, _Allocator>::const_reference
1773deque<_Tp, _Allocator>::front() const
1774{
1775 return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1776 + __base::__start_ % __base::__block_size);
1777}
1778
1779template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001780inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001781typename deque<_Tp, _Allocator>::reference
1782deque<_Tp, _Allocator>::back()
1783{
1784 size_type __p = __base::size() + __base::__start_ - 1;
1785 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1786}
1787
1788template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001789inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001790typename deque<_Tp, _Allocator>::const_reference
1791deque<_Tp, _Allocator>::back() const
1792{
1793 size_type __p = __base::size() + __base::__start_ - 1;
1794 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1795}
1796
1797template <class _Tp, class _Allocator>
1798void
1799deque<_Tp, _Allocator>::push_back(const value_type& __v)
1800{
1801 allocator_type& __a = __base::__alloc();
1802 if (__back_spare() == 0)
1803 __add_back_capacity();
1804 // __back_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001805 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001806 ++__base::size();
1807}
1808
Howard Hinnant74279a52010-09-04 23:28:19 +00001809#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001810
1811template <class _Tp, class _Allocator>
1812void
1813deque<_Tp, _Allocator>::push_back(value_type&& __v)
1814{
1815 allocator_type& __a = __base::__alloc();
1816 if (__back_spare() == 0)
1817 __add_back_capacity();
1818 // __back_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001819 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001820 ++__base::size();
1821}
1822
Howard Hinnant74279a52010-09-04 23:28:19 +00001823#ifndef _LIBCPP_HAS_NO_VARIADICS
1824
Howard Hinnantc51e1022010-05-11 19:42:16 +00001825template <class _Tp, class _Allocator>
1826template <class... _Args>
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001827typename deque<_Tp, _Allocator>::reference
Howard Hinnantc51e1022010-05-11 19:42:16 +00001828deque<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1829{
1830 allocator_type& __a = __base::__alloc();
1831 if (__back_spare() == 0)
1832 __add_back_capacity();
1833 // __back_spare() >= 1
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001834 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()),
1835 _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001836 ++__base::size();
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001837 return *--__base::end();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001838}
1839
Howard Hinnant74279a52010-09-04 23:28:19 +00001840#endif // _LIBCPP_HAS_NO_VARIADICS
1841#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001842
1843template <class _Tp, class _Allocator>
1844void
1845deque<_Tp, _Allocator>::push_front(const value_type& __v)
1846{
1847 allocator_type& __a = __base::__alloc();
1848 if (__front_spare() == 0)
1849 __add_front_capacity();
1850 // __front_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001851 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001852 --__base::__start_;
1853 ++__base::size();
1854}
1855
Howard Hinnant74279a52010-09-04 23:28:19 +00001856#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001857
1858template <class _Tp, class _Allocator>
1859void
1860deque<_Tp, _Allocator>::push_front(value_type&& __v)
1861{
1862 allocator_type& __a = __base::__alloc();
1863 if (__front_spare() == 0)
1864 __add_front_capacity();
1865 // __front_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001866 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001867 --__base::__start_;
1868 ++__base::size();
1869}
1870
Howard Hinnant74279a52010-09-04 23:28:19 +00001871#ifndef _LIBCPP_HAS_NO_VARIADICS
1872
Howard Hinnantc51e1022010-05-11 19:42:16 +00001873template <class _Tp, class _Allocator>
1874template <class... _Args>
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001875typename deque<_Tp, _Allocator>::reference
Howard Hinnantc51e1022010-05-11 19:42:16 +00001876deque<_Tp, _Allocator>::emplace_front(_Args&&... __args)
1877{
1878 allocator_type& __a = __base::__alloc();
1879 if (__front_spare() == 0)
1880 __add_front_capacity();
1881 // __front_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001882 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001883 --__base::__start_;
1884 ++__base::size();
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001885 return *__base::begin();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001886}
1887
Howard Hinnant74279a52010-09-04 23:28:19 +00001888#endif // _LIBCPP_HAS_NO_VARIADICS
1889#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001890
1891template <class _Tp, class _Allocator>
1892typename deque<_Tp, _Allocator>::iterator
1893deque<_Tp, _Allocator>::insert(const_iterator __p, const value_type& __v)
1894{
1895 size_type __pos = __p - __base::begin();
1896 size_type __to_end = __base::size() - __pos;
1897 allocator_type& __a = __base::__alloc();
1898 if (__pos < __to_end)
1899 { // insert by shifting things backward
1900 if (__front_spare() == 0)
1901 __add_front_capacity();
1902 // __front_spare() >= 1
1903 if (__pos == 0)
1904 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001905 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001906 --__base::__start_;
1907 ++__base::size();
1908 }
1909 else
1910 {
1911 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1912 iterator __b = __base::begin();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001913 iterator __bm1 = _VSTD::prev(__b);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001914 if (__vt == pointer_traits<const_pointer>::pointer_to(*__b))
1915 __vt = pointer_traits<const_pointer>::pointer_to(*__bm1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001916 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001917 --__base::__start_;
1918 ++__base::size();
1919 if (__pos > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001920 __b = __move_and_check(_VSTD::next(__b), __b + __pos, __b, __vt);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001921 *__b = *__vt;
1922 }
1923 }
1924 else
1925 { // insert by shifting things forward
1926 if (__back_spare() == 0)
1927 __add_back_capacity();
1928 // __back_capacity >= 1
1929 size_type __de = __base::size() - __pos;
1930 if (__de == 0)
1931 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001932 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001933 ++__base::size();
1934 }
1935 else
1936 {
1937 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
1938 iterator __e = __base::end();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001939 iterator __em1 = _VSTD::prev(__e);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001940 if (__vt == pointer_traits<const_pointer>::pointer_to(*__em1))
1941 __vt = pointer_traits<const_pointer>::pointer_to(*__e);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001942 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001943 ++__base::size();
1944 if (__de > 1)
1945 __e = __move_backward_and_check(__e - __de, __em1, __e, __vt);
1946 *--__e = *__vt;
1947 }
1948 }
1949 return __base::begin() + __pos;
1950}
1951
Howard Hinnant74279a52010-09-04 23:28:19 +00001952#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001953
1954template <class _Tp, class _Allocator>
1955typename deque<_Tp, _Allocator>::iterator
1956deque<_Tp, _Allocator>::insert(const_iterator __p, value_type&& __v)
1957{
1958 size_type __pos = __p - __base::begin();
1959 size_type __to_end = __base::size() - __pos;
1960 allocator_type& __a = __base::__alloc();
1961 if (__pos < __to_end)
1962 { // insert by shifting things backward
1963 if (__front_spare() == 0)
1964 __add_front_capacity();
1965 // __front_spare() >= 1
1966 if (__pos == 0)
1967 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001968 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001969 --__base::__start_;
1970 ++__base::size();
1971 }
1972 else
1973 {
1974 iterator __b = __base::begin();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001975 iterator __bm1 = _VSTD::prev(__b);
1976 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001977 --__base::__start_;
1978 ++__base::size();
1979 if (__pos > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001980 __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
1981 *__b = _VSTD::move(__v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001982 }
1983 }
1984 else
1985 { // insert by shifting things forward
1986 if (__back_spare() == 0)
1987 __add_back_capacity();
1988 // __back_capacity >= 1
1989 size_type __de = __base::size() - __pos;
1990 if (__de == 0)
1991 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001992 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001993 ++__base::size();
1994 }
1995 else
1996 {
1997 iterator __e = __base::end();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001998 iterator __em1 = _VSTD::prev(__e);
1999 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002000 ++__base::size();
2001 if (__de > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002002 __e = _VSTD::move_backward(__e - __de, __em1, __e);
2003 *--__e = _VSTD::move(__v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002004 }
2005 }
2006 return __base::begin() + __pos;
2007}
2008
Howard Hinnant74279a52010-09-04 23:28:19 +00002009#ifndef _LIBCPP_HAS_NO_VARIADICS
2010
Howard Hinnantc51e1022010-05-11 19:42:16 +00002011template <class _Tp, class _Allocator>
2012template <class... _Args>
2013typename deque<_Tp, _Allocator>::iterator
2014deque<_Tp, _Allocator>::emplace(const_iterator __p, _Args&&... __args)
2015{
2016 size_type __pos = __p - __base::begin();
2017 size_type __to_end = __base::size() - __pos;
2018 allocator_type& __a = __base::__alloc();
2019 if (__pos < __to_end)
2020 { // insert by shifting things backward
2021 if (__front_spare() == 0)
2022 __add_front_capacity();
2023 // __front_spare() >= 1
2024 if (__pos == 0)
2025 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002026 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002027 --__base::__start_;
2028 ++__base::size();
2029 }
2030 else
2031 {
Marshall Clowa591b9a2016-07-11 21:38:08 +00002032 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002033 iterator __b = __base::begin();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002034 iterator __bm1 = _VSTD::prev(__b);
2035 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002036 --__base::__start_;
2037 ++__base::size();
2038 if (__pos > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002039 __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
Marshall Clowa591b9a2016-07-11 21:38:08 +00002040 *__b = _VSTD::move(__tmp.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002041 }
2042 }
2043 else
2044 { // insert by shifting things forward
2045 if (__back_spare() == 0)
2046 __add_back_capacity();
2047 // __back_capacity >= 1
2048 size_type __de = __base::size() - __pos;
2049 if (__de == 0)
2050 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002051 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002052 ++__base::size();
2053 }
2054 else
2055 {
Marshall Clowa591b9a2016-07-11 21:38:08 +00002056 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002057 iterator __e = __base::end();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002058 iterator __em1 = _VSTD::prev(__e);
2059 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002060 ++__base::size();
2061 if (__de > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002062 __e = _VSTD::move_backward(__e - __de, __em1, __e);
Marshall Clowa591b9a2016-07-11 21:38:08 +00002063 *--__e = _VSTD::move(__tmp.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002064 }
2065 }
2066 return __base::begin() + __pos;
2067}
2068
Howard Hinnant74279a52010-09-04 23:28:19 +00002069#endif // _LIBCPP_HAS_NO_VARIADICS
2070#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002071
2072template <class _Tp, class _Allocator>
2073typename deque<_Tp, _Allocator>::iterator
2074deque<_Tp, _Allocator>::insert(const_iterator __p, size_type __n, const value_type& __v)
2075{
2076 size_type __pos = __p - __base::begin();
2077 size_type __to_end = __base::size() - __pos;
2078 allocator_type& __a = __base::__alloc();
2079 if (__pos < __to_end)
2080 { // insert by shifting things backward
2081 if (__n > __front_spare())
2082 __add_front_capacity(__n - __front_spare());
2083 // __n <= __front_spare()
Howard Hinnantc51e1022010-05-11 19:42:16 +00002084 iterator __old_begin = __base::begin();
2085 iterator __i = __old_begin;
2086 if (__n > __pos)
2087 {
2088 for (size_type __m = __n - __pos; __m; --__m, --__base::__start_, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002089 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002090 __n = __pos;
2091 }
2092 if (__n > 0)
2093 {
2094 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2095 iterator __obn = __old_begin + __n;
2096 __move_construct_backward_and_check(__old_begin, __obn, __i, __vt);
2097 if (__n < __pos)
2098 __old_begin = __move_and_check(__obn, __old_begin + __pos, __old_begin, __vt);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002099 _VSTD::fill_n(__old_begin, __n, *__vt);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002100 }
2101 }
2102 else
2103 { // insert by shifting things forward
2104 size_type __back_capacity = __back_spare();
2105 if (__n > __back_capacity)
2106 __add_back_capacity(__n - __back_capacity);
2107 // __n <= __back_capacity
Howard Hinnantc51e1022010-05-11 19:42:16 +00002108 iterator __old_end = __base::end();
2109 iterator __i = __old_end;
2110 size_type __de = __base::size() - __pos;
2111 if (__n > __de)
2112 {
2113 for (size_type __m = __n - __de; __m; --__m, ++__i, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002114 __alloc_traits::construct(__a, _VSTD::addressof(*__i), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002115 __n = __de;
2116 }
2117 if (__n > 0)
2118 {
2119 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2120 iterator __oen = __old_end - __n;
2121 __move_construct_and_check(__oen, __old_end, __i, __vt);
2122 if (__n < __de)
2123 __old_end = __move_backward_and_check(__old_end - __de, __oen, __old_end, __vt);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002124 _VSTD::fill_n(__old_end - __n, __n, *__vt);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002125 }
2126 }
2127 return __base::begin() + __pos;
2128}
2129
2130template <class _Tp, class _Allocator>
2131template <class _InputIter>
2132typename deque<_Tp, _Allocator>::iterator
2133deque<_Tp, _Allocator>::insert(const_iterator __p, _InputIter __f, _InputIter __l,
2134 typename enable_if<__is_input_iterator<_InputIter>::value
Marshall Clow6b612cf2015-01-22 18:33:29 +00002135 &&!__is_forward_iterator<_InputIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002136{
2137 __split_buffer<value_type, allocator_type&> __buf(__base::__alloc());
2138 __buf.__construct_at_end(__f, __l);
2139 typedef typename __split_buffer<value_type, allocator_type&>::iterator __bi;
2140 return insert(__p, move_iterator<__bi>(__buf.begin()), move_iterator<__bi>(__buf.end()));
2141}
2142
2143template <class _Tp, class _Allocator>
Marshall Clow6b612cf2015-01-22 18:33:29 +00002144template <class _ForwardIterator>
2145typename deque<_Tp, _Allocator>::iterator
2146deque<_Tp, _Allocator>::insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l,
2147 typename enable_if<__is_forward_iterator<_ForwardIterator>::value
2148 &&!__is_bidirectional_iterator<_ForwardIterator>::value>::type*)
2149{
2150 size_type __n = _VSTD::distance(__f, __l);
2151 __split_buffer<value_type, allocator_type&> __buf(__n, 0, __base::__alloc());
2152 __buf.__construct_at_end(__f, __l);
2153 typedef typename __split_buffer<value_type, allocator_type&>::iterator __fwd;
2154 return insert(__p, move_iterator<__fwd>(__buf.begin()), move_iterator<__fwd>(__buf.end()));
2155}
2156
2157template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002158template <class _BiIter>
2159typename deque<_Tp, _Allocator>::iterator
2160deque<_Tp, _Allocator>::insert(const_iterator __p, _BiIter __f, _BiIter __l,
2161 typename enable_if<__is_bidirectional_iterator<_BiIter>::value>::type*)
2162{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002163 size_type __n = _VSTD::distance(__f, __l);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002164 size_type __pos = __p - __base::begin();
2165 size_type __to_end = __base::size() - __pos;
2166 allocator_type& __a = __base::__alloc();
2167 if (__pos < __to_end)
2168 { // insert by shifting things backward
2169 if (__n > __front_spare())
2170 __add_front_capacity(__n - __front_spare());
2171 // __n <= __front_spare()
Howard Hinnantc51e1022010-05-11 19:42:16 +00002172 iterator __old_begin = __base::begin();
2173 iterator __i = __old_begin;
2174 _BiIter __m = __f;
2175 if (__n > __pos)
2176 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002177 __m = __pos < __n / 2 ? _VSTD::prev(__l, __pos) : _VSTD::next(__f, __n - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002178 for (_BiIter __j = __m; __j != __f; --__base::__start_, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002179 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), *--__j);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002180 __n = __pos;
2181 }
2182 if (__n > 0)
2183 {
2184 iterator __obn = __old_begin + __n;
2185 for (iterator __j = __obn; __j != __old_begin;)
2186 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002187 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), _VSTD::move(*--__j));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002188 --__base::__start_;
2189 ++__base::size();
2190 }
2191 if (__n < __pos)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002192 __old_begin = _VSTD::move(__obn, __old_begin + __pos, __old_begin);
2193 _VSTD::copy(__m, __l, __old_begin);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002194 }
2195 }
2196 else
2197 { // insert by shifting things forward
2198 size_type __back_capacity = __back_spare();
2199 if (__n > __back_capacity)
2200 __add_back_capacity(__n - __back_capacity);
2201 // __n <= __back_capacity
Howard Hinnantc51e1022010-05-11 19:42:16 +00002202 iterator __old_end = __base::end();
2203 iterator __i = __old_end;
2204 _BiIter __m = __l;
2205 size_type __de = __base::size() - __pos;
2206 if (__n > __de)
2207 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002208 __m = __de < __n / 2 ? _VSTD::next(__f, __de) : _VSTD::prev(__l, __n - __de);
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002209 for (_BiIter __j = __m; __j != __l; ++__i, (void) ++__j, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002210 __alloc_traits::construct(__a, _VSTD::addressof(*__i), *__j);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002211 __n = __de;
2212 }
2213 if (__n > 0)
2214 {
2215 iterator __oen = __old_end - __n;
2216 for (iterator __j = __oen; __j != __old_end; ++__i, ++__j, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002217 __alloc_traits::construct(__a, _VSTD::addressof(*__i), _VSTD::move(*__j));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002218 if (__n < __de)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002219 __old_end = _VSTD::move_backward(__old_end - __de, __oen, __old_end);
2220 _VSTD::copy_backward(__f, __m, __old_end);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002221 }
2222 }
2223 return __base::begin() + __pos;
2224}
2225
2226template <class _Tp, class _Allocator>
2227template <class _InpIter>
2228void
2229deque<_Tp, _Allocator>::__append(_InpIter __f, _InpIter __l,
2230 typename enable_if<__is_input_iterator<_InpIter>::value &&
2231 !__is_forward_iterator<_InpIter>::value>::type*)
2232{
2233 for (; __f != __l; ++__f)
2234 push_back(*__f);
2235}
2236
2237template <class _Tp, class _Allocator>
2238template <class _ForIter>
2239void
2240deque<_Tp, _Allocator>::__append(_ForIter __f, _ForIter __l,
2241 typename enable_if<__is_forward_iterator<_ForIter>::value>::type*)
2242{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002243 size_type __n = _VSTD::distance(__f, __l);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002244 allocator_type& __a = __base::__alloc();
2245 size_type __back_capacity = __back_spare();
2246 if (__n > __back_capacity)
2247 __add_back_capacity(__n - __back_capacity);
2248 // __n <= __back_capacity
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002249 for (iterator __i = __base::end(); __f != __l; ++__i, (void) ++__f, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002250 __alloc_traits::construct(__a, _VSTD::addressof(*__i), *__f);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002251}
2252
2253template <class _Tp, class _Allocator>
2254void
2255deque<_Tp, _Allocator>::__append(size_type __n)
2256{
2257 allocator_type& __a = __base::__alloc();
2258 size_type __back_capacity = __back_spare();
2259 if (__n > __back_capacity)
2260 __add_back_capacity(__n - __back_capacity);
2261 // __n <= __back_capacity
2262 for (iterator __i = __base::end(); __n; --__n, ++__i, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002263 __alloc_traits::construct(__a, _VSTD::addressof(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002264}
2265
2266template <class _Tp, class _Allocator>
2267void
2268deque<_Tp, _Allocator>::__append(size_type __n, const value_type& __v)
2269{
2270 allocator_type& __a = __base::__alloc();
2271 size_type __back_capacity = __back_spare();
2272 if (__n > __back_capacity)
2273 __add_back_capacity(__n - __back_capacity);
2274 // __n <= __back_capacity
2275 for (iterator __i = __base::end(); __n; --__n, ++__i, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002276 __alloc_traits::construct(__a, _VSTD::addressof(*__i), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002277}
2278
2279// Create front capacity for one block of elements.
2280// Strong guarantee. Either do it or don't touch anything.
2281template <class _Tp, class _Allocator>
2282void
2283deque<_Tp, _Allocator>::__add_front_capacity()
2284{
2285 allocator_type& __a = __base::__alloc();
2286 if (__back_spare() >= __base::__block_size)
2287 {
2288 __base::__start_ += __base::__block_size;
2289 pointer __pt = __base::__map_.back();
2290 __base::__map_.pop_back();
2291 __base::__map_.push_front(__pt);
2292 }
2293 // Else if __base::__map_.size() < __base::__map_.capacity() then we need to allocate 1 buffer
2294 else if (__base::__map_.size() < __base::__map_.capacity())
2295 { // we can put the new buffer into the map, but don't shift things around
2296 // until all buffers are allocated. If we throw, we don't need to fix
2297 // anything up (any added buffers are undetectible)
2298 if (__base::__map_.__front_spare() > 0)
2299 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2300 else
2301 {
2302 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2303 // Done allocating, reorder capacity
2304 pointer __pt = __base::__map_.back();
2305 __base::__map_.pop_back();
2306 __base::__map_.push_front(__pt);
2307 }
2308 __base::__start_ = __base::__map_.size() == 1 ?
2309 __base::__block_size / 2 :
2310 __base::__start_ + __base::__block_size;
2311 }
2312 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2313 else
2314 {
2315 __split_buffer<pointer, typename __base::__pointer_allocator&>
2316 __buf(max<size_type>(2 * __base::__map_.capacity(), 1),
2317 0, __base::__map_.__alloc());
Marshall Clow5fd466b2015-03-09 17:08:51 +00002318
Marshall Clow8982dcd2015-07-13 20:04:56 +00002319 typedef __allocator_destructor<_Allocator> _Dp;
2320 unique_ptr<pointer, _Dp> __hold(
2321 __alloc_traits::allocate(__a, __base::__block_size),
2322 _Dp(__a, __base::__block_size));
2323 __buf.push_back(__hold.get());
2324 __hold.release();
2325
Howard Hinnantc51e1022010-05-11 19:42:16 +00002326 for (typename __base::__map_pointer __i = __base::__map_.begin();
2327 __i != __base::__map_.end(); ++__i)
2328 __buf.push_back(*__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002329 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2330 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2331 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2332 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002333 __base::__start_ = __base::__map_.size() == 1 ?
2334 __base::__block_size / 2 :
2335 __base::__start_ + __base::__block_size;
2336 }
2337}
2338
2339// Create front capacity for __n elements.
2340// Strong guarantee. Either do it or don't touch anything.
2341template <class _Tp, class _Allocator>
2342void
2343deque<_Tp, _Allocator>::__add_front_capacity(size_type __n)
2344{
2345 allocator_type& __a = __base::__alloc();
2346 size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2347 // Number of unused blocks at back:
2348 size_type __back_capacity = __back_spare() / __base::__block_size;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002349 __back_capacity = _VSTD::min(__back_capacity, __nb); // don't take more than you need
Howard Hinnantc51e1022010-05-11 19:42:16 +00002350 __nb -= __back_capacity; // number of blocks need to allocate
2351 // If __nb == 0, then we have sufficient capacity.
2352 if (__nb == 0)
2353 {
2354 __base::__start_ += __base::__block_size * __back_capacity;
2355 for (; __back_capacity > 0; --__back_capacity)
2356 {
2357 pointer __pt = __base::__map_.back();
2358 __base::__map_.pop_back();
2359 __base::__map_.push_front(__pt);
2360 }
2361 }
2362 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2363 else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2364 { // we can put the new buffers into the map, but don't shift things around
2365 // until all buffers are allocated. If we throw, we don't need to fix
2366 // anything up (any added buffers are undetectible)
2367 for (; __nb > 0; --__nb, __base::__start_ += __base::__block_size - (__base::__map_.size() == 1))
2368 {
2369 if (__base::__map_.__front_spare() == 0)
2370 break;
2371 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2372 }
2373 for (; __nb > 0; --__nb, ++__back_capacity)
2374 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2375 // Done allocating, reorder capacity
2376 __base::__start_ += __back_capacity * __base::__block_size;
2377 for (; __back_capacity > 0; --__back_capacity)
2378 {
2379 pointer __pt = __base::__map_.back();
2380 __base::__map_.pop_back();
2381 __base::__map_.push_front(__pt);
2382 }
2383 }
2384 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2385 else
2386 {
2387 size_type __ds = (__nb + __back_capacity) * __base::__block_size - __base::__map_.empty();
2388 __split_buffer<pointer, typename __base::__pointer_allocator&>
2389 __buf(max<size_type>(2* __base::__map_.capacity(),
2390 __nb + __base::__map_.size()),
2391 0, __base::__map_.__alloc());
2392#ifndef _LIBCPP_NO_EXCEPTIONS
2393 try
2394 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002395#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002396 for (; __nb > 0; --__nb)
2397 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2398#ifndef _LIBCPP_NO_EXCEPTIONS
2399 }
2400 catch (...)
2401 {
2402 for (typename __base::__map_pointer __i = __buf.begin();
2403 __i != __buf.end(); ++__i)
2404 __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2405 throw;
2406 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002407#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002408 for (; __back_capacity > 0; --__back_capacity)
2409 {
2410 __buf.push_back(__base::__map_.back());
2411 __base::__map_.pop_back();
2412 }
2413 for (typename __base::__map_pointer __i = __base::__map_.begin();
2414 __i != __base::__map_.end(); ++__i)
2415 __buf.push_back(*__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002416 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2417 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2418 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2419 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002420 __base::__start_ += __ds;
2421 }
2422}
2423
2424// Create back capacity for one block of elements.
2425// Strong guarantee. Either do it or don't touch anything.
2426template <class _Tp, class _Allocator>
2427void
2428deque<_Tp, _Allocator>::__add_back_capacity()
2429{
2430 allocator_type& __a = __base::__alloc();
2431 if (__front_spare() >= __base::__block_size)
2432 {
2433 __base::__start_ -= __base::__block_size;
2434 pointer __pt = __base::__map_.front();
2435 __base::__map_.pop_front();
2436 __base::__map_.push_back(__pt);
2437 }
2438 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2439 else if (__base::__map_.size() < __base::__map_.capacity())
2440 { // we can put the new buffer into the map, but don't shift things around
2441 // until it is allocated. If we throw, we don't need to fix
2442 // anything up (any added buffers are undetectible)
2443 if (__base::__map_.__back_spare() != 0)
2444 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2445 else
2446 {
2447 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2448 // Done allocating, reorder capacity
2449 pointer __pt = __base::__map_.front();
2450 __base::__map_.pop_front();
2451 __base::__map_.push_back(__pt);
2452 }
2453 }
2454 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2455 else
2456 {
2457 __split_buffer<pointer, typename __base::__pointer_allocator&>
2458 __buf(max<size_type>(2* __base::__map_.capacity(), 1),
2459 __base::__map_.size(),
2460 __base::__map_.__alloc());
Marshall Clow5fd466b2015-03-09 17:08:51 +00002461
Marshall Clow8982dcd2015-07-13 20:04:56 +00002462 typedef __allocator_destructor<_Allocator> _Dp;
2463 unique_ptr<pointer, _Dp> __hold(
2464 __alloc_traits::allocate(__a, __base::__block_size),
2465 _Dp(__a, __base::__block_size));
2466 __buf.push_back(__hold.get());
2467 __hold.release();
Marshall Clow5fd466b2015-03-09 17:08:51 +00002468
Howard Hinnantc51e1022010-05-11 19:42:16 +00002469 for (typename __base::__map_pointer __i = __base::__map_.end();
2470 __i != __base::__map_.begin();)
2471 __buf.push_front(*--__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002472 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2473 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2474 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2475 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002476 }
2477}
2478
2479// Create back capacity for __n elements.
2480// Strong guarantee. Either do it or don't touch anything.
2481template <class _Tp, class _Allocator>
2482void
2483deque<_Tp, _Allocator>::__add_back_capacity(size_type __n)
2484{
2485 allocator_type& __a = __base::__alloc();
2486 size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2487 // Number of unused blocks at front:
2488 size_type __front_capacity = __front_spare() / __base::__block_size;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002489 __front_capacity = _VSTD::min(__front_capacity, __nb); // don't take more than you need
Howard Hinnantc51e1022010-05-11 19:42:16 +00002490 __nb -= __front_capacity; // number of blocks need to allocate
2491 // If __nb == 0, then we have sufficient capacity.
2492 if (__nb == 0)
2493 {
2494 __base::__start_ -= __base::__block_size * __front_capacity;
2495 for (; __front_capacity > 0; --__front_capacity)
2496 {
2497 pointer __pt = __base::__map_.front();
2498 __base::__map_.pop_front();
2499 __base::__map_.push_back(__pt);
2500 }
2501 }
2502 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2503 else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2504 { // we can put the new buffers into the map, but don't shift things around
2505 // until all buffers are allocated. If we throw, we don't need to fix
2506 // anything up (any added buffers are undetectible)
2507 for (; __nb > 0; --__nb)
2508 {
2509 if (__base::__map_.__back_spare() == 0)
2510 break;
2511 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2512 }
2513 for (; __nb > 0; --__nb, ++__front_capacity, __base::__start_ +=
2514 __base::__block_size - (__base::__map_.size() == 1))
2515 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2516 // Done allocating, reorder capacity
2517 __base::__start_ -= __base::__block_size * __front_capacity;
2518 for (; __front_capacity > 0; --__front_capacity)
2519 {
2520 pointer __pt = __base::__map_.front();
2521 __base::__map_.pop_front();
2522 __base::__map_.push_back(__pt);
2523 }
2524 }
2525 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2526 else
2527 {
2528 size_type __ds = __front_capacity * __base::__block_size;
2529 __split_buffer<pointer, typename __base::__pointer_allocator&>
2530 __buf(max<size_type>(2* __base::__map_.capacity(),
2531 __nb + __base::__map_.size()),
2532 __base::__map_.size() - __front_capacity,
2533 __base::__map_.__alloc());
2534#ifndef _LIBCPP_NO_EXCEPTIONS
2535 try
2536 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002537#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002538 for (; __nb > 0; --__nb)
2539 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2540#ifndef _LIBCPP_NO_EXCEPTIONS
2541 }
2542 catch (...)
2543 {
2544 for (typename __base::__map_pointer __i = __buf.begin();
2545 __i != __buf.end(); ++__i)
2546 __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2547 throw;
2548 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002549#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002550 for (; __front_capacity > 0; --__front_capacity)
2551 {
2552 __buf.push_back(__base::__map_.front());
2553 __base::__map_.pop_front();
2554 }
2555 for (typename __base::__map_pointer __i = __base::__map_.end();
2556 __i != __base::__map_.begin();)
2557 __buf.push_front(*--__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002558 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2559 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2560 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2561 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002562 __base::__start_ -= __ds;
2563 }
2564}
2565
2566template <class _Tp, class _Allocator>
2567void
2568deque<_Tp, _Allocator>::pop_front()
2569{
2570 allocator_type& __a = __base::__alloc();
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002571 __alloc_traits::destroy(__a, __to_raw_pointer(*(__base::__map_.begin() +
2572 __base::__start_ / __base::__block_size) +
2573 __base::__start_ % __base::__block_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002574 --__base::size();
2575 if (++__base::__start_ >= 2 * __base::__block_size)
2576 {
2577 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2578 __base::__map_.pop_front();
2579 __base::__start_ -= __base::__block_size;
2580 }
2581}
2582
2583template <class _Tp, class _Allocator>
2584void
2585deque<_Tp, _Allocator>::pop_back()
2586{
2587 allocator_type& __a = __base::__alloc();
2588 size_type __p = __base::size() + __base::__start_ - 1;
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002589 __alloc_traits::destroy(__a, __to_raw_pointer(*(__base::__map_.begin() +
2590 __p / __base::__block_size) +
2591 __p % __base::__block_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002592 --__base::size();
2593 if (__back_spare() >= 2 * __base::__block_size)
2594 {
2595 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2596 __base::__map_.pop_back();
2597 }
2598}
2599
2600// move assign [__f, __l) to [__r, __r + (__l-__f)).
2601// If __vt points into [__f, __l), then subtract (__f - __r) from __vt.
2602template <class _Tp, class _Allocator>
2603typename deque<_Tp, _Allocator>::iterator
2604deque<_Tp, _Allocator>::__move_and_check(iterator __f, iterator __l, iterator __r,
2605 const_pointer& __vt)
2606{
2607 // as if
2608 // for (; __f != __l; ++__f, ++__r)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002609 // *__r = _VSTD::move(*__f);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002610 difference_type __n = __l - __f;
2611 while (__n > 0)
2612 {
2613 pointer __fb = __f.__ptr_;
2614 pointer __fe = *__f.__m_iter_ + __base::__block_size;
2615 difference_type __bs = __fe - __fb;
2616 if (__bs > __n)
2617 {
2618 __bs = __n;
2619 __fe = __fb + __bs;
2620 }
2621 if (__fb <= __vt && __vt < __fe)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002622 __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) -= __f - __r).__ptr_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002623 __r = _VSTD::move(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002624 __n -= __bs;
2625 __f += __bs;
2626 }
2627 return __r;
2628}
2629
2630// move assign [__f, __l) to [__r - (__l-__f), __r) backwards.
2631// If __vt points into [__f, __l), then add (__r - __l) to __vt.
2632template <class _Tp, class _Allocator>
2633typename deque<_Tp, _Allocator>::iterator
2634deque<_Tp, _Allocator>::__move_backward_and_check(iterator __f, iterator __l, iterator __r,
2635 const_pointer& __vt)
2636{
2637 // as if
2638 // while (__f != __l)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002639 // *--__r = _VSTD::move(*--__l);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002640 difference_type __n = __l - __f;
2641 while (__n > 0)
2642 {
2643 --__l;
2644 pointer __lb = *__l.__m_iter_;
2645 pointer __le = __l.__ptr_ + 1;
2646 difference_type __bs = __le - __lb;
2647 if (__bs > __n)
2648 {
2649 __bs = __n;
2650 __lb = __le - __bs;
2651 }
2652 if (__lb <= __vt && __vt < __le)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002653 __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) += __r - __l - 1).__ptr_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002654 __r = _VSTD::move_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002655 __n -= __bs;
2656 __l -= __bs - 1;
2657 }
2658 return __r;
2659}
2660
2661// move construct [__f, __l) to [__r, __r + (__l-__f)).
2662// If __vt points into [__f, __l), then add (__r - __f) to __vt.
2663template <class _Tp, class _Allocator>
2664void
2665deque<_Tp, _Allocator>::__move_construct_and_check(iterator __f, iterator __l,
2666 iterator __r, const_pointer& __vt)
2667{
2668 allocator_type& __a = __base::__alloc();
2669 // as if
2670 // for (; __f != __l; ++__r, ++__f, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002671 // __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__f));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002672 difference_type __n = __l - __f;
2673 while (__n > 0)
2674 {
2675 pointer __fb = __f.__ptr_;
2676 pointer __fe = *__f.__m_iter_ + __base::__block_size;
2677 difference_type __bs = __fe - __fb;
2678 if (__bs > __n)
2679 {
2680 __bs = __n;
2681 __fe = __fb + __bs;
2682 }
2683 if (__fb <= __vt && __vt < __fe)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002684 __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) += __r - __f).__ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002685 for (; __fb != __fe; ++__fb, ++__r, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002686 __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__fb));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002687 __n -= __bs;
2688 __f += __bs;
2689 }
2690}
2691
2692// move construct [__f, __l) to [__r - (__l-__f), __r) backwards.
2693// If __vt points into [__f, __l), then subtract (__l - __r) from __vt.
2694template <class _Tp, class _Allocator>
2695void
2696deque<_Tp, _Allocator>::__move_construct_backward_and_check(iterator __f, iterator __l,
2697 iterator __r, const_pointer& __vt)
2698{
2699 allocator_type& __a = __base::__alloc();
2700 // as if
2701 // for (iterator __j = __l; __j != __f;)
2702 // {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002703 // __alloc_traitsconstruct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__j));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002704 // --__base::__start_;
2705 // ++__base::size();
2706 // }
2707 difference_type __n = __l - __f;
2708 while (__n > 0)
2709 {
2710 --__l;
2711 pointer __lb = *__l.__m_iter_;
2712 pointer __le = __l.__ptr_ + 1;
2713 difference_type __bs = __le - __lb;
2714 if (__bs > __n)
2715 {
2716 __bs = __n;
2717 __lb = __le - __bs;
2718 }
2719 if (__lb <= __vt && __vt < __le)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002720 __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) -= __l - __r + 1).__ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002721 while (__le != __lb)
2722 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002723 __alloc_traits::construct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__le));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002724 --__base::__start_;
2725 ++__base::size();
2726 }
2727 __n -= __bs;
2728 __l -= __bs - 1;
2729 }
2730}
2731
2732template <class _Tp, class _Allocator>
2733typename deque<_Tp, _Allocator>::iterator
2734deque<_Tp, _Allocator>::erase(const_iterator __f)
2735{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002736 iterator __b = __base::begin();
2737 difference_type __pos = __f - __b;
2738 iterator __p = __b + __pos;
2739 allocator_type& __a = __base::__alloc();
Marshall Clowe7e39922015-06-05 22:34:19 +00002740 if (__pos <= (__base::size() - 1) / 2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002741 { // erase from front
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002742 _VSTD::move_backward(__b, __p, _VSTD::next(__p));
2743 __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002744 --__base::size();
2745 ++__base::__start_;
2746 if (__front_spare() >= 2 * __base::__block_size)
2747 {
2748 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2749 __base::__map_.pop_front();
2750 __base::__start_ -= __base::__block_size;
2751 }
2752 }
2753 else
2754 { // erase from back
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002755 iterator __i = _VSTD::move(_VSTD::next(__p), __base::end(), __p);
2756 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002757 --__base::size();
2758 if (__back_spare() >= 2 * __base::__block_size)
2759 {
2760 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2761 __base::__map_.pop_back();
2762 }
2763 }
2764 return __base::begin() + __pos;
2765}
2766
2767template <class _Tp, class _Allocator>
2768typename deque<_Tp, _Allocator>::iterator
2769deque<_Tp, _Allocator>::erase(const_iterator __f, const_iterator __l)
2770{
2771 difference_type __n = __l - __f;
2772 iterator __b = __base::begin();
2773 difference_type __pos = __f - __b;
2774 iterator __p = __b + __pos;
2775 if (__n > 0)
2776 {
2777 allocator_type& __a = __base::__alloc();
Marshall Clowe7e39922015-06-05 22:34:19 +00002778 if (__pos <= (__base::size() - __n) / 2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002779 { // erase from front
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002780 iterator __i = _VSTD::move_backward(__b, __p, __p + __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002781 for (; __b != __i; ++__b)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002782 __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002783 __base::size() -= __n;
2784 __base::__start_ += __n;
2785 while (__front_spare() >= 2 * __base::__block_size)
2786 {
2787 __alloc_traits::deallocate(__a, __base::__map_.front(), __base::__block_size);
2788 __base::__map_.pop_front();
2789 __base::__start_ -= __base::__block_size;
2790 }
2791 }
2792 else
2793 { // erase from back
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002794 iterator __i = _VSTD::move(__p + __n, __base::end(), __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002795 for (iterator __e = __base::end(); __i != __e; ++__i)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002796 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002797 __base::size() -= __n;
2798 while (__back_spare() >= 2 * __base::__block_size)
2799 {
2800 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2801 __base::__map_.pop_back();
2802 }
2803 }
2804 }
2805 return __base::begin() + __pos;
2806}
2807
2808template <class _Tp, class _Allocator>
2809void
2810deque<_Tp, _Allocator>::__erase_to_end(const_iterator __f)
2811{
2812 iterator __e = __base::end();
2813 difference_type __n = __e - __f;
2814 if (__n > 0)
2815 {
2816 allocator_type& __a = __base::__alloc();
2817 iterator __b = __base::begin();
2818 difference_type __pos = __f - __b;
2819 for (iterator __p = __b + __pos; __p != __e; ++__p)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002820 __alloc_traits::destroy(__a, _VSTD::addressof(*__p));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002821 __base::size() -= __n;
2822 while (__back_spare() >= 2 * __base::__block_size)
2823 {
2824 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
2825 __base::__map_.pop_back();
2826 }
2827 }
2828}
2829
2830template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002831inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002832void
2833deque<_Tp, _Allocator>::swap(deque& __c)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002834#if _LIBCPP_STD_VER >= 14
2835 _NOEXCEPT
2836#else
2837 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2838 __is_nothrow_swappable<allocator_type>::value)
2839#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002840{
2841 __base::swap(__c);
2842}
2843
2844template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002845inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002846void
Howard Hinnantda2df912011-06-02 16:10:22 +00002847deque<_Tp, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002848{
2849 __base::clear();
2850}
2851
2852template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002853inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002854bool
2855operator==(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2856{
2857 const typename deque<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002858 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002859}
2860
2861template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002862inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002863bool
2864operator!=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2865{
2866 return !(__x == __y);
2867}
2868
2869template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002870inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002871bool
2872operator< (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2873{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002874 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002875}
2876
2877template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002878inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002879bool
2880operator> (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2881{
2882 return __y < __x;
2883}
2884
2885template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002886inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002887bool
2888operator>=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2889{
2890 return !(__x < __y);
2891}
2892
2893template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002894inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002895bool
2896operator<=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2897{
2898 return !(__y < __x);
2899}
2900
2901template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002902inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002903void
2904swap(deque<_Tp, _Allocator>& __x, deque<_Tp, _Allocator>& __y)
Howard Hinnantca2fc222011-06-02 20:00:14 +00002905 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002906{
2907 __x.swap(__y);
2908}
2909
2910_LIBCPP_END_NAMESPACE_STD
2911
2912#endif // _LIBCPP_DEQUE