blob: fe988d093ef8a6f79f4498bfb047d62917f7b859 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------------- deque -----------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_DEQUE
11#define _LIBCPP_DEQUE
12
13/*
14 deque synopsis
15
16namespace std
17{
18
19template <class T, class Allocator = allocator<T> >
20class deque
21{
22public:
23 // types:
24 typedef T value_type;
25 typedef Allocator allocator_type;
26
27 typedef typename allocator_type::reference reference;
28 typedef typename allocator_type::const_reference const_reference;
29 typedef implementation-defined iterator;
30 typedef implementation-defined const_iterator;
31 typedef typename allocator_type::size_type size_type;
32 typedef typename allocator_type::difference_type difference_type;
33
34 typedef typename allocator_type::pointer pointer;
35 typedef typename allocator_type::const_pointer const_pointer;
36 typedef std::reverse_iterator<iterator> reverse_iterator;
37 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
38
39 // construct/copy/destroy:
Howard Hinnantad979ba2011-06-03 15:16:49 +000040 deque() noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +000041 explicit deque(const allocator_type& a);
42 explicit deque(size_type n);
Marshall Clow68098692013-09-09 18:19:45 +000043 explicit deque(size_type n, const allocator_type& a); // C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000044 deque(size_type n, const value_type& v);
45 deque(size_type n, const value_type& v, const allocator_type& a);
46 template <class InputIterator>
47 deque(InputIterator f, InputIterator l);
48 template <class InputIterator>
49 deque(InputIterator f, InputIterator l, const allocator_type& a);
50 deque(const deque& c);
Howard Hinnant4931e092011-06-02 21:38:57 +000051 deque(deque&& c)
52 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +000053 deque(initializer_list<value_type> il, const Allocator& a = allocator_type());
54 deque(const deque& c, const allocator_type& a);
55 deque(deque&& c, const allocator_type& a);
56 ~deque();
57
58 deque& operator=(const deque& c);
Howard Hinnant4931e092011-06-02 21:38:57 +000059 deque& operator=(deque&& c)
60 noexcept(
61 allocator_type::propagate_on_container_move_assignment::value &&
62 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +000063 deque& operator=(initializer_list<value_type> il);
64
65 template <class InputIterator>
66 void assign(InputIterator f, InputIterator l);
67 void assign(size_type n, const value_type& v);
68 void assign(initializer_list<value_type> il);
69
Howard Hinnantda2df912011-06-02 16:10:22 +000070 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000071
72 // iterators:
73
Howard Hinnantda2df912011-06-02 16:10:22 +000074 iterator begin() noexcept;
75 const_iterator begin() const noexcept;
76 iterator end() noexcept;
77 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000078
Howard Hinnantda2df912011-06-02 16:10:22 +000079 reverse_iterator rbegin() noexcept;
80 const_reverse_iterator rbegin() const noexcept;
81 reverse_iterator rend() noexcept;
82 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000083
Howard Hinnantda2df912011-06-02 16:10:22 +000084 const_iterator cbegin() const noexcept;
85 const_iterator cend() const noexcept;
86 const_reverse_iterator crbegin() const noexcept;
87 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000088
89 // capacity:
Howard Hinnantda2df912011-06-02 16:10:22 +000090 size_type size() const noexcept;
91 size_type max_size() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000092 void resize(size_type n);
93 void resize(size_type n, const value_type& v);
94 void shrink_to_fit();
Howard Hinnantda2df912011-06-02 16:10:22 +000095 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000096
97 // element access:
98 reference operator[](size_type i);
99 const_reference operator[](size_type i) const;
100 reference at(size_type i);
101 const_reference at(size_type i) const;
102 reference front();
103 const_reference front() const;
104 reference back();
105 const_reference back() const;
106
107 // modifiers:
108 void push_front(const value_type& v);
109 void push_front(value_type&& v);
110 void push_back(const value_type& v);
111 void push_back(value_type&& v);
Marshall Clowea52cc42017-01-24 23:09:12 +0000112 template <class... Args> reference emplace_front(Args&&... args); // reference in C++17
113 template <class... Args> reference emplace_back(Args&&... args); // reference in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000114 template <class... Args> iterator emplace(const_iterator p, Args&&... args);
115 iterator insert(const_iterator p, const value_type& v);
116 iterator insert(const_iterator p, value_type&& v);
117 iterator insert(const_iterator p, size_type n, const value_type& v);
118 template <class InputIterator>
Marshall Clow6b612cf2015-01-22 18:33:29 +0000119 iterator insert(const_iterator p, InputIterator f, InputIterator l);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000120 iterator insert(const_iterator p, initializer_list<value_type> il);
121 void pop_front();
122 void pop_back();
123 iterator erase(const_iterator p);
124 iterator erase(const_iterator f, const_iterator l);
Howard Hinnant4931e092011-06-02 21:38:57 +0000125 void swap(deque& c)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000126 noexcept(allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantda2df912011-06-02 16:10:22 +0000127 void clear() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000128};
129
Marshall Clow4cc3a452018-05-18 23:44:13 +0000130template <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
131 deque(InputIterator, InputIterator, Allocator = Allocator())
132 -> deque<typename iterator_traits<InputIterator>::value_type, Allocator>;
133
Howard Hinnantc51e1022010-05-11 19:42:16 +0000134template <class T, class Allocator>
135 bool operator==(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
136template <class T, class Allocator>
137 bool operator< (const deque<T,Allocator>& x, const deque<T,Allocator>& y);
138template <class T, class Allocator>
139 bool operator!=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
140template <class T, class Allocator>
141 bool operator> (const deque<T,Allocator>& x, const deque<T,Allocator>& y);
142template <class T, class Allocator>
143 bool operator>=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
144template <class T, class Allocator>
145 bool operator<=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
146
147// specialized algorithms:
148template <class T, class Allocator>
Howard Hinnant287548a2011-06-03 17:30:28 +0000149 void swap(deque<T,Allocator>& x, deque<T,Allocator>& y)
150 noexcept(noexcept(x.swap(y)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000151
Marshall Clow29b53f22018-12-14 18:49:35 +0000152template <class T, class Allocator, class U>
153 void erase(deque<T, Allocator>& c, const U& value); // C++20
154template <class T, class Allocator, class Predicate>
155 void erase_if(deque<T, Allocator>& c, Predicate pred); // C++20
156
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157} // std
158
159*/
160
Howard Hinnantc51e1022010-05-11 19:42:16 +0000161#include <__config>
162#include <__split_buffer>
163#include <type_traits>
164#include <initializer_list>
165#include <iterator>
166#include <algorithm>
167#include <stdexcept>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000168#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000169
Eric Fiselierf4433a32017-05-31 22:07:49 +0000170#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
171#pragma GCC system_header
172#endif
173
174_LIBCPP_PUSH_MACROS
175#include <__undef_macros>
176
Howard Hinnantc5a5fbd2011-11-29 16:45:27 +0000177
Howard Hinnantc51e1022010-05-11 19:42:16 +0000178_LIBCPP_BEGIN_NAMESPACE_STD
179
180template <class _Tp, class _Allocator> class __deque_base;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000181template <class _Tp, class _Allocator = allocator<_Tp> > class _LIBCPP_TEMPLATE_VIS deque;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000182
183template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
184 class _DiffType, _DiffType _BlockSize>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000185class _LIBCPP_TEMPLATE_VIS __deque_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000186
187template <class _RAIter,
188 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
189__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
190copy(_RAIter __f,
191 _RAIter __l,
192 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
193 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
194
195template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
196 class _OutputIterator>
197_OutputIterator
198copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
199 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
200 _OutputIterator __r);
201
202template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
203 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
204__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
205copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
206 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
207 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
208
209template <class _RAIter,
210 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
211__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
212copy_backward(_RAIter __f,
213 _RAIter __l,
214 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
215 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
216
217template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
218 class _OutputIterator>
219_OutputIterator
220copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
221 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
222 _OutputIterator __r);
223
224template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
225 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
226__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
227copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
228 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
229 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
230
231template <class _RAIter,
232 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
233__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
234move(_RAIter __f,
235 _RAIter __l,
236 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
237 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
238
239template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
240 class _OutputIterator>
241_OutputIterator
242move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
243 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
244 _OutputIterator __r);
245
246template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
247 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
248__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
249move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
250 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
251 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
252
253template <class _RAIter,
254 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
255__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
256move_backward(_RAIter __f,
257 _RAIter __l,
258 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
259 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
260
261template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
262 class _OutputIterator>
263_OutputIterator
264move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
265 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
266 _OutputIterator __r);
267
268template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
269 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
270__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
271move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
272 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
273 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
274
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000275template <class _ValueType, class _DiffType>
276struct __deque_block_size {
277 static const _DiffType value = sizeof(_ValueType) < 256 ? 4096 / sizeof(_ValueType) : 16;
278};
279
Howard Hinnantc51e1022010-05-11 19:42:16 +0000280template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000281 class _DiffType, _DiffType _BS =
282#ifdef _LIBCPP_ABI_INCOMPLETE_TYPES_IN_DEQUE
283// Keep template parameter to avoid changing all template declarations thoughout
284// this file.
285 0
286#else
287 __deque_block_size<_ValueType, _DiffType>::value
288#endif
289 >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000290class _LIBCPP_TEMPLATE_VIS __deque_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000291{
292 typedef _MapPointer __map_iterator;
293public:
294 typedef _Pointer pointer;
295 typedef _DiffType difference_type;
296private:
297 __map_iterator __m_iter_;
298 pointer __ptr_;
299
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000300 static const difference_type __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000301public:
302 typedef _ValueType value_type;
303 typedef random_access_iterator_tag iterator_category;
304 typedef _Reference reference;
305
Marshall Clow7a3a61d2013-08-06 16:14:36 +0000306 _LIBCPP_INLINE_VISIBILITY __deque_iterator() _NOEXCEPT
307#if _LIBCPP_STD_VER > 11
308 : __m_iter_(nullptr), __ptr_(nullptr)
309#endif
310 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000311
Howard Hinnantc834c512011-11-29 18:15:50 +0000312 template <class _Pp, class _Rp, class _MP>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000313 _LIBCPP_INLINE_VISIBILITY
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000314 __deque_iterator(const __deque_iterator<value_type, _Pp, _Rp, _MP, difference_type, _BS>& __it,
Howard Hinnantc834c512011-11-29 18:15:50 +0000315 typename enable_if<is_convertible<_Pp, pointer>::value>::type* = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000316 : __m_iter_(__it.__m_iter_), __ptr_(__it.__ptr_) {}
317
318 _LIBCPP_INLINE_VISIBILITY reference operator*() const {return *__ptr_;}
319 _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return __ptr_;}
320
321 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator++()
322 {
323 if (++__ptr_ - *__m_iter_ == __block_size)
324 {
325 ++__m_iter_;
326 __ptr_ = *__m_iter_;
327 }
328 return *this;
329 }
330
331 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator++(int)
332 {
333 __deque_iterator __tmp = *this;
334 ++(*this);
335 return __tmp;
336 }
337
338 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator--()
339 {
340 if (__ptr_ == *__m_iter_)
341 {
342 --__m_iter_;
343 __ptr_ = *__m_iter_ + __block_size;
344 }
345 --__ptr_;
346 return *this;
347 }
348
349 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator--(int)
350 {
351 __deque_iterator __tmp = *this;
352 --(*this);
353 return __tmp;
354 }
355
356 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator+=(difference_type __n)
357 {
358 if (__n != 0)
359 {
360 __n += __ptr_ - *__m_iter_;
361 if (__n > 0)
362 {
363 __m_iter_ += __n / __block_size;
364 __ptr_ = *__m_iter_ + __n % __block_size;
365 }
366 else // (__n < 0)
367 {
368 difference_type __z = __block_size - 1 - __n;
369 __m_iter_ -= __z / __block_size;
370 __ptr_ = *__m_iter_ + (__block_size - 1 - __z % __block_size);
371 }
372 }
373 return *this;
374 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000375
Howard Hinnantc51e1022010-05-11 19:42:16 +0000376 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator-=(difference_type __n)
377 {
378 return *this += -__n;
379 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000380
Howard Hinnantc51e1022010-05-11 19:42:16 +0000381 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator+(difference_type __n) const
382 {
383 __deque_iterator __t(*this);
384 __t += __n;
385 return __t;
386 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000387
Howard Hinnantc51e1022010-05-11 19:42:16 +0000388 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator-(difference_type __n) const
389 {
390 __deque_iterator __t(*this);
391 __t -= __n;
392 return __t;
393 }
394
395 _LIBCPP_INLINE_VISIBILITY
396 friend __deque_iterator operator+(difference_type __n, const __deque_iterator& __it)
397 {return __it + __n;}
398
399 _LIBCPP_INLINE_VISIBILITY
400 friend difference_type operator-(const __deque_iterator& __x, const __deque_iterator& __y)
401 {
402 if (__x != __y)
403 return (__x.__m_iter_ - __y.__m_iter_) * __block_size
404 + (__x.__ptr_ - *__x.__m_iter_)
405 - (__y.__ptr_ - *__y.__m_iter_);
406 return 0;
407 }
408
409 _LIBCPP_INLINE_VISIBILITY reference operator[](difference_type __n) const
410 {return *(*this + __n);}
411
412 _LIBCPP_INLINE_VISIBILITY friend
413 bool operator==(const __deque_iterator& __x, const __deque_iterator& __y)
414 {return __x.__ptr_ == __y.__ptr_;}
415
416 _LIBCPP_INLINE_VISIBILITY friend
417 bool operator!=(const __deque_iterator& __x, const __deque_iterator& __y)
418 {return !(__x == __y);}
419
420 _LIBCPP_INLINE_VISIBILITY friend
421 bool operator<(const __deque_iterator& __x, const __deque_iterator& __y)
422 {return __x.__m_iter_ < __y.__m_iter_ ||
423 (__x.__m_iter_ == __y.__m_iter_ && __x.__ptr_ < __y.__ptr_);}
424
425 _LIBCPP_INLINE_VISIBILITY friend
426 bool operator>(const __deque_iterator& __x, const __deque_iterator& __y)
427 {return __y < __x;}
428
429 _LIBCPP_INLINE_VISIBILITY friend
430 bool operator<=(const __deque_iterator& __x, const __deque_iterator& __y)
431 {return !(__y < __x);}
432
433 _LIBCPP_INLINE_VISIBILITY friend
434 bool operator>=(const __deque_iterator& __x, const __deque_iterator& __y)
435 {return !(__x < __y);}
436
437private:
Howard Hinnantda2df912011-06-02 16:10:22 +0000438 _LIBCPP_INLINE_VISIBILITY __deque_iterator(__map_iterator __m, pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000439 : __m_iter_(__m), __ptr_(__p) {}
440
Howard Hinnantc834c512011-11-29 18:15:50 +0000441 template <class _Tp, class _Ap> friend class __deque_base;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000442 template <class _Tp, class _Ap> friend class _LIBCPP_TEMPLATE_VIS deque;
Howard Hinnantc834c512011-11-29 18:15:50 +0000443 template <class _Vp, class _Pp, class _Rp, class _MP, class _Dp, _Dp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000444 friend class _LIBCPP_TEMPLATE_VIS __deque_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000445
446 template <class _RAIter,
447 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
448 friend
449 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
450 copy(_RAIter __f,
451 _RAIter __l,
452 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
453 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
454
455 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
456 class _OutputIterator>
457 friend
458 _OutputIterator
459 copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
460 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
461 _OutputIterator __r);
462
463 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
464 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
465 friend
466 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
467 copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
468 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
469 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
470
471 template <class _RAIter,
472 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
473 friend
474 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
475 copy_backward(_RAIter __f,
476 _RAIter __l,
477 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
478 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
479
480 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
481 class _OutputIterator>
482 friend
483 _OutputIterator
484 copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
485 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
486 _OutputIterator __r);
487
488 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
489 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
490 friend
491 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
492 copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
493 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
494 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
495
496 template <class _RAIter,
497 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
498 friend
499 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
500 move(_RAIter __f,
501 _RAIter __l,
502 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
503 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
504
505 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
506 class _OutputIterator>
507 friend
508 _OutputIterator
509 move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
510 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
511 _OutputIterator __r);
512
513 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
514 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
515 friend
516 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
517 move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
518 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
519 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
520
521 template <class _RAIter,
522 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
523 friend
524 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
525 move_backward(_RAIter __f,
526 _RAIter __l,
527 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
528 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*);
529
530 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
531 class _OutputIterator>
532 friend
533 _OutputIterator
534 move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
535 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
536 _OutputIterator __r);
537
538 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
539 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
540 friend
541 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
542 move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
543 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
544 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
545};
546
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000547template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
548 class _DiffType, _DiffType _BlockSize>
549const _DiffType __deque_iterator<_ValueType, _Pointer, _Reference, _MapPointer,
550 _DiffType, _BlockSize>::__block_size =
551 __deque_block_size<_ValueType, _DiffType>::value;
552
Howard Hinnantc51e1022010-05-11 19:42:16 +0000553// copy
554
555template <class _RAIter,
556 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
557__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
558copy(_RAIter __f,
559 _RAIter __l,
560 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
561 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
562{
563 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
564 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000565 const difference_type __block_size = __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::__block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000566 while (__f != __l)
567 {
568 pointer __rb = __r.__ptr_;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000569 pointer __re = *__r.__m_iter_ + __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000570 difference_type __bs = __re - __rb;
571 difference_type __n = __l - __f;
572 _RAIter __m = __l;
573 if (__n > __bs)
574 {
575 __n = __bs;
576 __m = __f + __n;
577 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000578 _VSTD::copy(__f, __m, __rb);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000579 __f = __m;
580 __r += __n;
581 }
582 return __r;
583}
584
585template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
586 class _OutputIterator>
587_OutputIterator
588copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
589 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
590 _OutputIterator __r)
591{
592 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
593 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000594 const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000595 difference_type __n = __l - __f;
596 while (__n > 0)
597 {
598 pointer __fb = __f.__ptr_;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000599 pointer __fe = *__f.__m_iter_ + __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000600 difference_type __bs = __fe - __fb;
601 if (__bs > __n)
602 {
603 __bs = __n;
604 __fe = __fb + __bs;
605 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000606 __r = _VSTD::copy(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000607 __n -= __bs;
608 __f += __bs;
609 }
610 return __r;
611}
612
613template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
614 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
615__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
616copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
617 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
618 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
619{
620 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
621 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000622 const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000623 difference_type __n = __l - __f;
624 while (__n > 0)
625 {
626 pointer __fb = __f.__ptr_;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000627 pointer __fe = *__f.__m_iter_ + __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000628 difference_type __bs = __fe - __fb;
629 if (__bs > __n)
630 {
631 __bs = __n;
632 __fe = __fb + __bs;
633 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000634 __r = _VSTD::copy(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000635 __n -= __bs;
636 __f += __bs;
637 }
638 return __r;
639}
640
641// copy_backward
642
643template <class _RAIter,
644 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
645__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
646copy_backward(_RAIter __f,
647 _RAIter __l,
648 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
649 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
650{
651 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
652 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
653 while (__f != __l)
654 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000655 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _VSTD::prev(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000656 pointer __rb = *__rp.__m_iter_;
657 pointer __re = __rp.__ptr_ + 1;
658 difference_type __bs = __re - __rb;
659 difference_type __n = __l - __f;
660 _RAIter __m = __f;
661 if (__n > __bs)
662 {
663 __n = __bs;
664 __m = __l - __n;
665 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000666 _VSTD::copy_backward(__m, __l, __re);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000667 __l = __m;
668 __r -= __n;
669 }
670 return __r;
671}
672
673template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
674 class _OutputIterator>
675_OutputIterator
676copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
677 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
678 _OutputIterator __r)
679{
680 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
681 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
682 difference_type __n = __l - __f;
683 while (__n > 0)
684 {
685 --__l;
686 pointer __lb = *__l.__m_iter_;
687 pointer __le = __l.__ptr_ + 1;
688 difference_type __bs = __le - __lb;
689 if (__bs > __n)
690 {
691 __bs = __n;
692 __lb = __le - __bs;
693 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000694 __r = _VSTD::copy_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000695 __n -= __bs;
696 __l -= __bs - 1;
697 }
698 return __r;
699}
700
701template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
702 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
703__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
704copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
705 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
706 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
707{
708 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
709 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
710 difference_type __n = __l - __f;
711 while (__n > 0)
712 {
713 --__l;
714 pointer __lb = *__l.__m_iter_;
715 pointer __le = __l.__ptr_ + 1;
716 difference_type __bs = __le - __lb;
717 if (__bs > __n)
718 {
719 __bs = __n;
720 __lb = __le - __bs;
721 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000722 __r = _VSTD::copy_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000723 __n -= __bs;
724 __l -= __bs - 1;
725 }
726 return __r;
727}
728
729// move
730
731template <class _RAIter,
732 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
733__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
734move(_RAIter __f,
735 _RAIter __l,
736 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
737 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
738{
739 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
740 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000741 const difference_type __block_size = __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::__block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000742 while (__f != __l)
743 {
744 pointer __rb = __r.__ptr_;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000745 pointer __re = *__r.__m_iter_ + __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000746 difference_type __bs = __re - __rb;
747 difference_type __n = __l - __f;
748 _RAIter __m = __l;
749 if (__n > __bs)
750 {
751 __n = __bs;
752 __m = __f + __n;
753 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000754 _VSTD::move(__f, __m, __rb);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000755 __f = __m;
756 __r += __n;
757 }
758 return __r;
759}
760
761template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
762 class _OutputIterator>
763_OutputIterator
764move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
765 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
766 _OutputIterator __r)
767{
768 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
769 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000770 const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000771 difference_type __n = __l - __f;
772 while (__n > 0)
773 {
774 pointer __fb = __f.__ptr_;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000775 pointer __fe = *__f.__m_iter_ + __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000776 difference_type __bs = __fe - __fb;
777 if (__bs > __n)
778 {
779 __bs = __n;
780 __fe = __fb + __bs;
781 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000782 __r = _VSTD::move(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000783 __n -= __bs;
784 __f += __bs;
785 }
786 return __r;
787}
788
789template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
790 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
791__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
792move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
793 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
794 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
795{
796 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
797 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000798 const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000799 difference_type __n = __l - __f;
800 while (__n > 0)
801 {
802 pointer __fb = __f.__ptr_;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000803 pointer __fe = *__f.__m_iter_ + __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000804 difference_type __bs = __fe - __fb;
805 if (__bs > __n)
806 {
807 __bs = __n;
808 __fe = __fb + __bs;
809 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000810 __r = _VSTD::move(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000811 __n -= __bs;
812 __f += __bs;
813 }
814 return __r;
815}
816
817// move_backward
818
819template <class _RAIter,
820 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
821__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
822move_backward(_RAIter __f,
823 _RAIter __l,
824 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
825 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
826{
827 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
828 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
829 while (__f != __l)
830 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000831 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _VSTD::prev(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000832 pointer __rb = *__rp.__m_iter_;
833 pointer __re = __rp.__ptr_ + 1;
834 difference_type __bs = __re - __rb;
835 difference_type __n = __l - __f;
836 _RAIter __m = __f;
837 if (__n > __bs)
838 {
839 __n = __bs;
840 __m = __l - __n;
841 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000842 _VSTD::move_backward(__m, __l, __re);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000843 __l = __m;
844 __r -= __n;
845 }
846 return __r;
847}
848
849template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
850 class _OutputIterator>
851_OutputIterator
852move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
853 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
854 _OutputIterator __r)
855{
856 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
857 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
858 difference_type __n = __l - __f;
859 while (__n > 0)
860 {
861 --__l;
862 pointer __lb = *__l.__m_iter_;
863 pointer __le = __l.__ptr_ + 1;
864 difference_type __bs = __le - __lb;
865 if (__bs > __n)
866 {
867 __bs = __n;
868 __lb = __le - __bs;
869 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000870 __r = _VSTD::move_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000871 __n -= __bs;
872 __l -= __bs - 1;
873 }
874 return __r;
875}
876
877template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
878 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
879__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
880move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
881 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
882 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
883{
884 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
885 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
886 difference_type __n = __l - __f;
887 while (__n > 0)
888 {
889 --__l;
890 pointer __lb = *__l.__m_iter_;
891 pointer __le = __l.__ptr_ + 1;
892 difference_type __bs = __le - __lb;
893 if (__bs > __n)
894 {
895 __bs = __n;
896 __lb = __le - __bs;
897 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000898 __r = _VSTD::move_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000899 __n -= __bs;
900 __l -= __bs - 1;
901 }
902 return __r;
903}
904
905template <bool>
906class __deque_base_common
907{
908protected:
Marshall Clow8fea1612016-08-25 15:09:01 +0000909 _LIBCPP_NORETURN void __throw_length_error() const;
910 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000911};
912
913template <bool __b>
914void
915__deque_base_common<__b>::__throw_length_error() const
916{
Marshall Clow8fea1612016-08-25 15:09:01 +0000917 _VSTD::__throw_length_error("deque");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000918}
919
920template <bool __b>
921void
922__deque_base_common<__b>::__throw_out_of_range() const
923{
Marshall Clow8fea1612016-08-25 15:09:01 +0000924 _VSTD::__throw_out_of_range("deque");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000925}
926
927template <class _Tp, class _Allocator>
928class __deque_base
929 : protected __deque_base_common<true>
930{
931 __deque_base(const __deque_base& __c);
932 __deque_base& operator=(const __deque_base& __c);
Marshall Clow4cc3a452018-05-18 23:44:13 +0000933public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000934 typedef _Allocator allocator_type;
935 typedef allocator_traits<allocator_type> __alloc_traits;
Marshall Clow4cc3a452018-05-18 23:44:13 +0000936 typedef typename __alloc_traits::size_type size_type;
Eric Fiselier27e03622019-08-01 23:11:18 +0000937
Marshall Clow4cc3a452018-05-18 23:44:13 +0000938 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000939 typedef value_type& reference;
940 typedef const value_type& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000941 typedef typename __alloc_traits::difference_type difference_type;
942 typedef typename __alloc_traits::pointer pointer;
943 typedef typename __alloc_traits::const_pointer const_pointer;
944
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000945 static const difference_type __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000946
Marshall Clow940e01c2015-04-07 05:21:38 +0000947 typedef typename __rebind_alloc_helper<__alloc_traits, pointer>::type __pointer_allocator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000948 typedef allocator_traits<__pointer_allocator> __map_traits;
949 typedef typename __map_traits::pointer __map_pointer;
Marshall Clow940e01c2015-04-07 05:21:38 +0000950 typedef typename __rebind_alloc_helper<__alloc_traits, const_pointer>::type __const_pointer_allocator;
Howard Hinnantdcb73a32013-06-23 21:17:24 +0000951 typedef typename allocator_traits<__const_pointer_allocator>::const_pointer __map_const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000952 typedef __split_buffer<pointer, __pointer_allocator> __map;
953
954 typedef __deque_iterator<value_type, pointer, reference, __map_pointer,
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000955 difference_type> iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000956 typedef __deque_iterator<value_type, const_pointer, const_reference, __map_const_pointer,
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000957 difference_type> const_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000958
Marshall Clow4cc3a452018-05-18 23:44:13 +0000959protected:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000960 __map __map_;
961 size_type __start_;
962 __compressed_pair<size_type, allocator_type> __size_;
963
Howard Hinnantda2df912011-06-02 16:10:22 +0000964 iterator begin() _NOEXCEPT;
965 const_iterator begin() const _NOEXCEPT;
966 iterator end() _NOEXCEPT;
967 const_iterator end() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000968
969 _LIBCPP_INLINE_VISIBILITY size_type& size() {return __size_.first();}
Howard Hinnantda2df912011-06-02 16:10:22 +0000970 _LIBCPP_INLINE_VISIBILITY
971 const size_type& size() const _NOEXCEPT {return __size_.first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000972 _LIBCPP_INLINE_VISIBILITY allocator_type& __alloc() {return __size_.second();}
Howard Hinnantda2df912011-06-02 16:10:22 +0000973 _LIBCPP_INLINE_VISIBILITY
974 const allocator_type& __alloc() const _NOEXCEPT {return __size_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000975
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000976 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantad979ba2011-06-03 15:16:49 +0000977 __deque_base()
978 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000979 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000980 explicit __deque_base(const allocator_type& __a);
Howard Hinnantca2fc222011-06-02 20:00:14 +0000981public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000982 ~__deque_base();
983
Eric Fiselier212e3f22017-04-16 03:17:01 +0000984#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantca2fc222011-06-02 20:00:14 +0000985 __deque_base(__deque_base&& __c)
986 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000987 __deque_base(__deque_base&& __c, const allocator_type& __a);
Eric Fiselier212e3f22017-04-16 03:17:01 +0000988#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000989
Howard Hinnantca2fc222011-06-02 20:00:14 +0000990 void swap(__deque_base& __c)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000991#if _LIBCPP_STD_VER >= 14
992 _NOEXCEPT;
993#else
Louis Dionne72f24392018-12-12 23:58:25 +0000994 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +0000995 __is_nothrow_swappable<allocator_type>::value);
996#endif
Howard Hinnantca2fc222011-06-02 20:00:14 +0000997protected:
Howard Hinnantda2df912011-06-02 16:10:22 +0000998 void clear() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000999
1000 bool __invariants() const;
1001
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001002 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001003 void __move_assign(__deque_base& __c)
Howard Hinnant4931e092011-06-02 21:38:57 +00001004 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1005 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001006 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001007 __map_ = _VSTD::move(__c.__map_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008 __start_ = __c.__start_;
1009 size() = __c.size();
1010 __move_assign_alloc(__c);
1011 __c.__start_ = __c.size() = 0;
1012 }
1013
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001014 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001015 void __move_assign_alloc(__deque_base& __c)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001016 _NOEXCEPT_(!__alloc_traits::propagate_on_container_move_assignment::value ||
1017 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001018 {__move_assign_alloc(__c, integral_constant<bool,
1019 __alloc_traits::propagate_on_container_move_assignment::value>());}
1020
1021private:
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001022 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001023 void __move_assign_alloc(__deque_base& __c, true_type)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001024 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001025 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001026 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001027 }
1028
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001029 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001030 void __move_assign_alloc(__deque_base&, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001031 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001032};
1033
1034template <class _Tp, class _Allocator>
Evgeniy Stepanovc299de22015-11-06 22:02:29 +00001035const typename __deque_base<_Tp, _Allocator>::difference_type
1036 __deque_base<_Tp, _Allocator>::__block_size =
1037 __deque_block_size<value_type, difference_type>::value;
1038
1039template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001040bool
1041__deque_base<_Tp, _Allocator>::__invariants() const
1042{
1043 if (!__map_.__invariants())
1044 return false;
1045 if (__map_.size() >= size_type(-1) / __block_size)
1046 return false;
1047 for (typename __map::const_iterator __i = __map_.begin(), __e = __map_.end();
1048 __i != __e; ++__i)
1049 if (*__i == nullptr)
1050 return false;
1051 if (__map_.size() != 0)
1052 {
1053 if (size() >= __map_.size() * __block_size)
1054 return false;
1055 if (__start_ >= __map_.size() * __block_size - size())
1056 return false;
1057 }
1058 else
1059 {
1060 if (size() != 0)
1061 return false;
1062 if (__start_ != 0)
1063 return false;
1064 }
1065 return true;
1066}
1067
1068template <class _Tp, class _Allocator>
1069typename __deque_base<_Tp, _Allocator>::iterator
Howard Hinnantda2df912011-06-02 16:10:22 +00001070__deque_base<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001071{
1072 __map_pointer __mp = __map_.begin() + __start_ / __block_size;
1073 return iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
1074}
1075
1076template <class _Tp, class _Allocator>
1077typename __deque_base<_Tp, _Allocator>::const_iterator
Howard Hinnantda2df912011-06-02 16:10:22 +00001078__deque_base<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001079{
Howard Hinnantdcb73a32013-06-23 21:17:24 +00001080 __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __start_ / __block_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001081 return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
1082}
1083
1084template <class _Tp, class _Allocator>
1085typename __deque_base<_Tp, _Allocator>::iterator
Howard Hinnantda2df912011-06-02 16:10:22 +00001086__deque_base<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001087{
1088 size_type __p = size() + __start_;
1089 __map_pointer __mp = __map_.begin() + __p / __block_size;
1090 return iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
1091}
1092
1093template <class _Tp, class _Allocator>
1094typename __deque_base<_Tp, _Allocator>::const_iterator
Howard Hinnantda2df912011-06-02 16:10:22 +00001095__deque_base<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001096{
1097 size_type __p = size() + __start_;
Howard Hinnantdcb73a32013-06-23 21:17:24 +00001098 __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __p / __block_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001099 return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
1100}
1101
1102template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001103inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001104__deque_base<_Tp, _Allocator>::__deque_base()
Howard Hinnantad979ba2011-06-03 15:16:49 +00001105 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001106 : __start_(0), __size_(0) {}
1107
1108template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001109inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001110__deque_base<_Tp, _Allocator>::__deque_base(const allocator_type& __a)
1111 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a) {}
1112
1113template <class _Tp, class _Allocator>
1114__deque_base<_Tp, _Allocator>::~__deque_base()
1115{
1116 clear();
1117 typename __map::iterator __i = __map_.begin();
1118 typename __map::iterator __e = __map_.end();
1119 for (; __i != __e; ++__i)
1120 __alloc_traits::deallocate(__alloc(), *__i, __block_size);
1121}
1122
Eric Fiselier212e3f22017-04-16 03:17:01 +00001123#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001124
1125template <class _Tp, class _Allocator>
1126__deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001127 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001128 : __map_(_VSTD::move(__c.__map_)),
1129 __start_(_VSTD::move(__c.__start_)),
1130 __size_(_VSTD::move(__c.__size_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001131{
1132 __c.__start_ = 0;
1133 __c.size() = 0;
1134}
1135
1136template <class _Tp, class _Allocator>
1137__deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c, const allocator_type& __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001138 : __map_(_VSTD::move(__c.__map_), __pointer_allocator(__a)),
1139 __start_(_VSTD::move(__c.__start_)),
1140 __size_(_VSTD::move(__c.size()), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001141{
1142 if (__a == __c.__alloc())
1143 {
1144 __c.__start_ = 0;
1145 __c.size() = 0;
1146 }
1147 else
1148 {
1149 __map_.clear();
1150 __start_ = 0;
1151 size() = 0;
1152 }
1153}
1154
Eric Fiselier212e3f22017-04-16 03:17:01 +00001155#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001156
1157template <class _Tp, class _Allocator>
1158void
1159__deque_base<_Tp, _Allocator>::swap(__deque_base& __c)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001160#if _LIBCPP_STD_VER >= 14
1161 _NOEXCEPT
1162#else
Louis Dionne72f24392018-12-12 23:58:25 +00001163 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001164 __is_nothrow_swappable<allocator_type>::value)
1165#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001166{
1167 __map_.swap(__c.__map_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001168 _VSTD::swap(__start_, __c.__start_);
1169 _VSTD::swap(size(), __c.size());
Marshall Clow8982dcd2015-07-13 20:04:56 +00001170 __swap_allocator(__alloc(), __c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001171}
1172
1173template <class _Tp, class _Allocator>
1174void
Howard Hinnantda2df912011-06-02 16:10:22 +00001175__deque_base<_Tp, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001176{
1177 allocator_type& __a = __alloc();
1178 for (iterator __i = begin(), __e = end(); __i != __e; ++__i)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001179 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001180 size() = 0;
1181 while (__map_.size() > 2)
1182 {
1183 __alloc_traits::deallocate(__a, __map_.front(), __block_size);
1184 __map_.pop_front();
1185 }
1186 switch (__map_.size())
1187 {
1188 case 1:
1189 __start_ = __block_size / 2;
1190 break;
1191 case 2:
1192 __start_ = __block_size;
1193 break;
1194 }
1195}
1196
Marshall Clow65cd4c62015-02-18 17:24:08 +00001197template <class _Tp, class _Allocator /*= allocator<_Tp>*/>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001198class _LIBCPP_TEMPLATE_VIS deque
Howard Hinnantc51e1022010-05-11 19:42:16 +00001199 : private __deque_base<_Tp, _Allocator>
1200{
1201public:
1202 // types:
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001203
Howard Hinnantc51e1022010-05-11 19:42:16 +00001204 typedef _Tp value_type;
1205 typedef _Allocator allocator_type;
1206
Marshall Clow5128cf32015-11-26 01:24:04 +00001207 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
1208 "Allocator::value_type must be same type as value_type");
1209
Howard Hinnantc51e1022010-05-11 19:42:16 +00001210 typedef __deque_base<value_type, allocator_type> __base;
1211
1212 typedef typename __base::__alloc_traits __alloc_traits;
1213 typedef typename __base::reference reference;
1214 typedef typename __base::const_reference const_reference;
1215 typedef typename __base::iterator iterator;
1216 typedef typename __base::const_iterator const_iterator;
1217 typedef typename __base::size_type size_type;
1218 typedef typename __base::difference_type difference_type;
1219
1220 typedef typename __base::pointer pointer;
1221 typedef typename __base::const_pointer const_pointer;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001222 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
1223 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001224
1225 // construct/copy/destroy:
Howard Hinnantad979ba2011-06-03 15:16:49 +00001226 _LIBCPP_INLINE_VISIBILITY
1227 deque()
1228 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
1229 {}
Marshall Clow7ed23a72014-03-05 19:06:20 +00001230 _LIBCPP_INLINE_VISIBILITY explicit deque(const allocator_type& __a) : __base(__a) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001231 explicit deque(size_type __n);
Marshall Clowbc4fcb42013-09-07 16:16:19 +00001232#if _LIBCPP_STD_VER > 11
1233 explicit deque(size_type __n, const _Allocator& __a);
1234#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001235 deque(size_type __n, const value_type& __v);
1236 deque(size_type __n, const value_type& __v, const allocator_type& __a);
1237 template <class _InputIter>
1238 deque(_InputIter __f, _InputIter __l,
1239 typename enable_if<__is_input_iterator<_InputIter>::value>::type* = 0);
1240 template <class _InputIter>
1241 deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
1242 typename enable_if<__is_input_iterator<_InputIter>::value>::type* = 0);
1243 deque(const deque& __c);
1244 deque(const deque& __c, const allocator_type& __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001245
1246 deque& operator=(const deque& __c);
Eric Fiselier212e3f22017-04-16 03:17:01 +00001247
1248#ifndef _LIBCPP_CXX03_LANG
1249 deque(initializer_list<value_type> __il);
1250 deque(initializer_list<value_type> __il, const allocator_type& __a);
1251
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001252 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001253 deque& operator=(initializer_list<value_type> __il) {assign(__il); return *this;}
1254
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001255 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantca2fc222011-06-02 20:00:14 +00001256 deque(deque&& __c) _NOEXCEPT_(is_nothrow_move_constructible<__base>::value);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001257 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001258 deque(deque&& __c, const allocator_type& __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001259 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantca2fc222011-06-02 20:00:14 +00001260 deque& operator=(deque&& __c)
Howard Hinnant4931e092011-06-02 21:38:57 +00001261 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1262 is_nothrow_move_assignable<allocator_type>::value);
Eric Fiselier212e3f22017-04-16 03:17:01 +00001263
1264 _LIBCPP_INLINE_VISIBILITY
1265 void assign(initializer_list<value_type> __il) {assign(__il.begin(), __il.end());}
1266#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001267
1268 template <class _InputIter>
1269 void assign(_InputIter __f, _InputIter __l,
1270 typename enable_if<__is_input_iterator<_InputIter>::value &&
1271 !__is_random_access_iterator<_InputIter>::value>::type* = 0);
1272 template <class _RAIter>
1273 void assign(_RAIter __f, _RAIter __l,
1274 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type* = 0);
1275 void assign(size_type __n, const value_type& __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001276
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001277 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001278 allocator_type get_allocator() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001279
1280 // iterators:
1281
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001282 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001283 iterator begin() _NOEXCEPT {return __base::begin();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001284 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001285 const_iterator begin() const _NOEXCEPT {return __base::begin();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001286 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001287 iterator end() _NOEXCEPT {return __base::end();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001288 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001289 const_iterator end() const _NOEXCEPT {return __base::end();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001290
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001291 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001292 reverse_iterator rbegin() _NOEXCEPT
1293 {return reverse_iterator(__base::end());}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001294 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001295 const_reverse_iterator rbegin() const _NOEXCEPT
1296 {return const_reverse_iterator(__base::end());}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001297 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001298 reverse_iterator rend() _NOEXCEPT
1299 {return reverse_iterator(__base::begin());}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001300 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001301 const_reverse_iterator rend() const _NOEXCEPT
1302 {return const_reverse_iterator(__base::begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001303
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001304 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001305 const_iterator cbegin() const _NOEXCEPT
1306 {return __base::begin();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001307 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001308 const_iterator cend() const _NOEXCEPT
1309 {return __base::end();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001310 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001311 const_reverse_iterator crbegin() const _NOEXCEPT
1312 {return const_reverse_iterator(__base::end());}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001313 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001314 const_reverse_iterator crend() const _NOEXCEPT
1315 {return const_reverse_iterator(__base::begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001316
1317 // capacity:
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001318 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001319 size_type size() const _NOEXCEPT {return __base::size();}
1320 _LIBCPP_INLINE_VISIBILITY
1321 size_type max_size() const _NOEXCEPT
Eric Fiselierb5d9f442016-11-23 01:18:56 +00001322 {return std::min<size_type>(
1323 __alloc_traits::max_size(__base::__alloc()),
1324 numeric_limits<difference_type>::max());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001325 void resize(size_type __n);
1326 void resize(size_type __n, const value_type& __v);
Howard Hinnant4931e092011-06-02 21:38:57 +00001327 void shrink_to_fit() _NOEXCEPT;
Marshall Clow425f5752017-11-15 05:51:26 +00001328 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001329 bool empty() const _NOEXCEPT {return __base::size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001330
1331 // element access:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001332 _LIBCPP_INLINE_VISIBILITY
Marshall Clow8d7c9f12019-03-14 21:56:57 +00001333 reference operator[](size_type __i) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001334 _LIBCPP_INLINE_VISIBILITY
Marshall Clow8d7c9f12019-03-14 21:56:57 +00001335 const_reference operator[](size_type __i) const _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001336 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001337 reference at(size_type __i);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001338 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001339 const_reference at(size_type __i) const;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001340 _LIBCPP_INLINE_VISIBILITY
Marshall Clow05cf6692019-03-19 03:30:07 +00001341 reference front() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001342 _LIBCPP_INLINE_VISIBILITY
Marshall Clow05cf6692019-03-19 03:30:07 +00001343 const_reference front() const _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001344 _LIBCPP_INLINE_VISIBILITY
Marshall Clow05cf6692019-03-19 03:30:07 +00001345 reference back() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001346 _LIBCPP_INLINE_VISIBILITY
Marshall Clow05cf6692019-03-19 03:30:07 +00001347 const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001348
1349 // 23.2.2.3 modifiers:
1350 void push_front(const value_type& __v);
1351 void push_back(const value_type& __v);
Eric Fiselier212e3f22017-04-16 03:17:01 +00001352#ifndef _LIBCPP_CXX03_LANG
Marshall Clowea52cc42017-01-24 23:09:12 +00001353#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001354 template <class... _Args> reference emplace_front(_Args&&... __args);
Marshall Clowea52cc42017-01-24 23:09:12 +00001355 template <class... _Args> reference emplace_back (_Args&&... __args);
1356#else
1357 template <class... _Args> void emplace_front(_Args&&... __args);
1358 template <class... _Args> void emplace_back (_Args&&... __args);
1359#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001360 template <class... _Args> iterator emplace(const_iterator __p, _Args&&... __args);
Eric Fiselier212e3f22017-04-16 03:17:01 +00001361
Howard Hinnantc51e1022010-05-11 19:42:16 +00001362 void push_front(value_type&& __v);
1363 void push_back(value_type&& __v);
1364 iterator insert(const_iterator __p, value_type&& __v);
Eric Fiselier212e3f22017-04-16 03:17:01 +00001365
1366 _LIBCPP_INLINE_VISIBILITY
1367 iterator insert(const_iterator __p, initializer_list<value_type> __il)
1368 {return insert(__p, __il.begin(), __il.end());}
1369#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001370 iterator insert(const_iterator __p, const value_type& __v);
1371 iterator insert(const_iterator __p, size_type __n, const value_type& __v);
1372 template <class _InputIter>
Marshall Clow6b612cf2015-01-22 18:33:29 +00001373 iterator insert(const_iterator __p, _InputIter __f, _InputIter __l,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001374 typename enable_if<__is_input_iterator<_InputIter>::value
Marshall Clow6b612cf2015-01-22 18:33:29 +00001375 &&!__is_forward_iterator<_InputIter>::value>::type* = 0);
1376 template <class _ForwardIterator>
1377 iterator insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l,
1378 typename enable_if<__is_forward_iterator<_ForwardIterator>::value
1379 &&!__is_bidirectional_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001380 template <class _BiIter>
Marshall Clow6b612cf2015-01-22 18:33:29 +00001381 iterator insert(const_iterator __p, _BiIter __f, _BiIter __l,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001382 typename enable_if<__is_bidirectional_iterator<_BiIter>::value>::type* = 0);
Eric Fiselier212e3f22017-04-16 03:17:01 +00001383
Howard Hinnantc51e1022010-05-11 19:42:16 +00001384 void pop_front();
1385 void pop_back();
1386 iterator erase(const_iterator __p);
1387 iterator erase(const_iterator __f, const_iterator __l);
1388
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001389 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantca2fc222011-06-02 20:00:14 +00001390 void swap(deque& __c)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001391#if _LIBCPP_STD_VER >= 14
1392 _NOEXCEPT;
1393#else
Howard Hinnantca2fc222011-06-02 20:00:14 +00001394 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1395 __is_nothrow_swappable<allocator_type>::value);
Marshall Clow8982dcd2015-07-13 20:04:56 +00001396#endif
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001397 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001398 void clear() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001399
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001400 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001401 bool __invariants() const {return __base::__invariants();}
Eric Fiselier27e03622019-08-01 23:11:18 +00001402
Howard Hinnantdcb73a32013-06-23 21:17:24 +00001403 typedef typename __base::__map_const_pointer __map_const_pointer;
1404
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001405 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001406 static size_type __recommend_blocks(size_type __n)
1407 {
1408 return __n / __base::__block_size + (__n % __base::__block_size != 0);
1409 }
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001410 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001411 size_type __capacity() const
1412 {
1413 return __base::__map_.size() == 0 ? 0 : __base::__map_.size() * __base::__block_size - 1;
1414 }
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001415 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier27e03622019-08-01 23:11:18 +00001416 size_type __block_count() const
1417 {
1418 return __base::__map_.size();
1419 }
1420
1421 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001422 size_type __front_spare() const
1423 {
1424 return __base::__start_;
1425 }
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001426 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier27e03622019-08-01 23:11:18 +00001427 size_type __front_spare_blocks() const {
1428 return __front_spare() / __base::__block_size;
1429 }
1430 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001431 size_type __back_spare() const
1432 {
1433 return __capacity() - (__base::__start_ + __base::size());
1434 }
Eric Fiselier27e03622019-08-01 23:11:18 +00001435 _LIBCPP_INLINE_VISIBILITY
1436 size_type __back_spare_blocks() const {
1437 return __back_spare() / __base::__block_size;
1438 }
1439
1440 private:
1441 _LIBCPP_INLINE_VISIBILITY
1442 bool __maybe_remove_front_spare(bool __keep_one = true) {
1443 if (__front_spare_blocks() >= 2 || (!__keep_one && __front_spare_blocks())) {
1444 __alloc_traits::deallocate(__base::__alloc(), __base::__map_.front(),
1445 __base::__block_size);
1446 __base::__map_.pop_front();
1447 __base::__start_ -= __base::__block_size;
1448 return true;
1449 }
1450 return false;
1451 }
1452
1453 _LIBCPP_INLINE_VISIBILITY
1454 bool __maybe_remove_back_spare(bool __keep_one = true) {
1455 if (__back_spare_blocks() >= 2 || (!__keep_one && __back_spare_blocks())) {
1456 __alloc_traits::deallocate(__base::__alloc(), __base::__map_.back(),
1457 __base::__block_size);
1458 __base::__map_.pop_back();
1459 return true;
1460 }
1461 return false;
1462 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001463
1464 template <class _InpIter>
1465 void __append(_InpIter __f, _InpIter __l,
1466 typename enable_if<__is_input_iterator<_InpIter>::value &&
1467 !__is_forward_iterator<_InpIter>::value>::type* = 0);
1468 template <class _ForIter>
1469 void __append(_ForIter __f, _ForIter __l,
1470 typename enable_if<__is_forward_iterator<_ForIter>::value>::type* = 0);
1471 void __append(size_type __n);
1472 void __append(size_type __n, const value_type& __v);
1473 void __erase_to_end(const_iterator __f);
1474 void __add_front_capacity();
1475 void __add_front_capacity(size_type __n);
1476 void __add_back_capacity();
1477 void __add_back_capacity(size_type __n);
1478 iterator __move_and_check(iterator __f, iterator __l, iterator __r,
1479 const_pointer& __vt);
1480 iterator __move_backward_and_check(iterator __f, iterator __l, iterator __r,
1481 const_pointer& __vt);
1482 void __move_construct_and_check(iterator __f, iterator __l,
1483 iterator __r, const_pointer& __vt);
1484 void __move_construct_backward_and_check(iterator __f, iterator __l,
1485 iterator __r, const_pointer& __vt);
1486
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001487 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001488 void __copy_assign_alloc(const deque& __c)
1489 {__copy_assign_alloc(__c, integral_constant<bool,
1490 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1491
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001492 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001493 void __copy_assign_alloc(const deque& __c, true_type)
1494 {
1495 if (__base::__alloc() != __c.__alloc())
1496 {
1497 clear();
1498 shrink_to_fit();
1499 }
1500 __base::__alloc() = __c.__alloc();
1501 __base::__map_.__alloc() = __c.__map_.__alloc();
1502 }
1503
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001504 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001505 void __copy_assign_alloc(const deque&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001506 {}
1507
Howard Hinnant4931e092011-06-02 21:38:57 +00001508 void __move_assign(deque& __c, true_type)
1509 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001510 void __move_assign(deque& __c, false_type);
1511};
1512
Marshall Clow4cc3a452018-05-18 23:44:13 +00001513#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
1514template<class _InputIterator,
1515 class _Alloc = typename std::allocator<typename iterator_traits<_InputIterator>::value_type>,
1516 class = typename enable_if<__is_allocator<_Alloc>::value, void>::type
1517 >
1518deque(_InputIterator, _InputIterator)
1519 -> deque<typename iterator_traits<_InputIterator>::value_type, _Alloc>;
1520
1521template<class _InputIterator,
1522 class _Alloc,
1523 class = typename enable_if<__is_allocator<_Alloc>::value, void>::type
1524 >
1525deque(_InputIterator, _InputIterator, _Alloc)
1526 -> deque<typename iterator_traits<_InputIterator>::value_type, _Alloc>;
1527#endif
1528
1529
Howard Hinnantc51e1022010-05-11 19:42:16 +00001530template <class _Tp, class _Allocator>
1531deque<_Tp, _Allocator>::deque(size_type __n)
1532{
1533 if (__n > 0)
1534 __append(__n);
1535}
1536
Marshall Clowbc4fcb42013-09-07 16:16:19 +00001537#if _LIBCPP_STD_VER > 11
1538template <class _Tp, class _Allocator>
1539deque<_Tp, _Allocator>::deque(size_type __n, const _Allocator& __a)
1540 : __base(__a)
1541{
1542 if (__n > 0)
1543 __append(__n);
1544}
1545#endif
1546
Howard Hinnantc51e1022010-05-11 19:42:16 +00001547template <class _Tp, class _Allocator>
1548deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v)
1549{
1550 if (__n > 0)
1551 __append(__n, __v);
1552}
1553
1554template <class _Tp, class _Allocator>
1555deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v, const allocator_type& __a)
1556 : __base(__a)
1557{
1558 if (__n > 0)
1559 __append(__n, __v);
1560}
1561
1562template <class _Tp, class _Allocator>
1563template <class _InputIter>
1564deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l,
1565 typename enable_if<__is_input_iterator<_InputIter>::value>::type*)
1566{
1567 __append(__f, __l);
1568}
1569
1570template <class _Tp, class _Allocator>
1571template <class _InputIter>
1572deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
1573 typename enable_if<__is_input_iterator<_InputIter>::value>::type*)
1574 : __base(__a)
1575{
1576 __append(__f, __l);
1577}
1578
1579template <class _Tp, class _Allocator>
1580deque<_Tp, _Allocator>::deque(const deque& __c)
1581 : __base(__alloc_traits::select_on_container_copy_construction(__c.__alloc()))
1582{
1583 __append(__c.begin(), __c.end());
1584}
1585
1586template <class _Tp, class _Allocator>
1587deque<_Tp, _Allocator>::deque(const deque& __c, const allocator_type& __a)
1588 : __base(__a)
1589{
1590 __append(__c.begin(), __c.end());
1591}
1592
Eric Fiselier212e3f22017-04-16 03:17:01 +00001593template <class _Tp, class _Allocator>
1594deque<_Tp, _Allocator>&
1595deque<_Tp, _Allocator>::operator=(const deque& __c)
1596{
1597 if (this != &__c)
1598 {
1599 __copy_assign_alloc(__c);
1600 assign(__c.begin(), __c.end());
1601 }
1602 return *this;
1603}
1604
1605#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00001606
Howard Hinnantc51e1022010-05-11 19:42:16 +00001607template <class _Tp, class _Allocator>
1608deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il)
1609{
1610 __append(__il.begin(), __il.end());
1611}
1612
1613template <class _Tp, class _Allocator>
1614deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il, const allocator_type& __a)
1615 : __base(__a)
1616{
1617 __append(__il.begin(), __il.end());
1618}
1619
Howard Hinnantc51e1022010-05-11 19:42:16 +00001620template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001621inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001622deque<_Tp, _Allocator>::deque(deque&& __c)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001623 _NOEXCEPT_(is_nothrow_move_constructible<__base>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001624 : __base(_VSTD::move(__c))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001625{
1626}
1627
1628template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001629inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001630deque<_Tp, _Allocator>::deque(deque&& __c, const allocator_type& __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001631 : __base(_VSTD::move(__c), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001632{
1633 if (__a != __c.__alloc())
1634 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001635 typedef move_iterator<iterator> _Ip;
1636 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001637 }
1638}
1639
1640template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001641inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001642deque<_Tp, _Allocator>&
1643deque<_Tp, _Allocator>::operator=(deque&& __c)
Howard Hinnant4931e092011-06-02 21:38:57 +00001644 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1645 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001646{
1647 __move_assign(__c, integral_constant<bool,
1648 __alloc_traits::propagate_on_container_move_assignment::value>());
1649 return *this;
1650}
1651
1652template <class _Tp, class _Allocator>
1653void
1654deque<_Tp, _Allocator>::__move_assign(deque& __c, false_type)
1655{
1656 if (__base::__alloc() != __c.__alloc())
1657 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001658 typedef move_iterator<iterator> _Ip;
1659 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001660 }
1661 else
1662 __move_assign(__c, true_type());
1663}
1664
1665template <class _Tp, class _Allocator>
1666void
1667deque<_Tp, _Allocator>::__move_assign(deque& __c, true_type)
Howard Hinnant4931e092011-06-02 21:38:57 +00001668 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001669{
1670 clear();
1671 shrink_to_fit();
1672 __base::__move_assign(__c);
1673}
1674
Eric Fiselier212e3f22017-04-16 03:17:01 +00001675#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001676
1677template <class _Tp, class _Allocator>
1678template <class _InputIter>
1679void
1680deque<_Tp, _Allocator>::assign(_InputIter __f, _InputIter __l,
1681 typename enable_if<__is_input_iterator<_InputIter>::value &&
1682 !__is_random_access_iterator<_InputIter>::value>::type*)
1683{
1684 iterator __i = __base::begin();
1685 iterator __e = __base::end();
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001686 for (; __f != __l && __i != __e; ++__f, (void) ++__i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001687 *__i = *__f;
1688 if (__f != __l)
1689 __append(__f, __l);
1690 else
1691 __erase_to_end(__i);
1692}
1693
1694template <class _Tp, class _Allocator>
1695template <class _RAIter>
1696void
1697deque<_Tp, _Allocator>::assign(_RAIter __f, _RAIter __l,
1698 typename enable_if<__is_random_access_iterator<_RAIter>::value>::type*)
1699{
1700 if (static_cast<size_type>(__l - __f) > __base::size())
1701 {
1702 _RAIter __m = __f + __base::size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001703 _VSTD::copy(__f, __m, __base::begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001704 __append(__m, __l);
1705 }
1706 else
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001707 __erase_to_end(_VSTD::copy(__f, __l, __base::begin()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001708}
1709
1710template <class _Tp, class _Allocator>
1711void
1712deque<_Tp, _Allocator>::assign(size_type __n, const value_type& __v)
1713{
1714 if (__n > __base::size())
1715 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001716 _VSTD::fill_n(__base::begin(), __base::size(), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001717 __n -= __base::size();
1718 __append(__n, __v);
1719 }
1720 else
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001721 __erase_to_end(_VSTD::fill_n(__base::begin(), __n, __v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001722}
1723
1724template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001725inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001726_Allocator
Howard Hinnantda2df912011-06-02 16:10:22 +00001727deque<_Tp, _Allocator>::get_allocator() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001728{
1729 return __base::__alloc();
1730}
1731
1732template <class _Tp, class _Allocator>
1733void
1734deque<_Tp, _Allocator>::resize(size_type __n)
1735{
1736 if (__n > __base::size())
1737 __append(__n - __base::size());
1738 else if (__n < __base::size())
1739 __erase_to_end(__base::begin() + __n);
1740}
1741
1742template <class _Tp, class _Allocator>
1743void
1744deque<_Tp, _Allocator>::resize(size_type __n, const value_type& __v)
1745{
1746 if (__n > __base::size())
1747 __append(__n - __base::size(), __v);
1748 else if (__n < __base::size())
1749 __erase_to_end(__base::begin() + __n);
1750}
1751
1752template <class _Tp, class _Allocator>
1753void
Howard Hinnant4931e092011-06-02 21:38:57 +00001754deque<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001755{
1756 allocator_type& __a = __base::__alloc();
1757 if (empty())
1758 {
1759 while (__base::__map_.size() > 0)
1760 {
1761 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
1762 __base::__map_.pop_back();
1763 }
1764 __base::__start_ = 0;
1765 }
1766 else
1767 {
Eric Fiselier27e03622019-08-01 23:11:18 +00001768 __maybe_remove_front_spare(/*__keep_one=*/false);
1769 __maybe_remove_back_spare(/*__keep_one=*/false);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001770 }
1771 __base::__map_.shrink_to_fit();
1772}
1773
1774template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001775inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001776typename deque<_Tp, _Allocator>::reference
Marshall Clow8d7c9f12019-03-14 21:56:57 +00001777deque<_Tp, _Allocator>::operator[](size_type __i) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001778{
1779 size_type __p = __base::__start_ + __i;
1780 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1781}
1782
1783template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001784inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001785typename deque<_Tp, _Allocator>::const_reference
Marshall Clow8d7c9f12019-03-14 21:56:57 +00001786deque<_Tp, _Allocator>::operator[](size_type __i) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001787{
1788 size_type __p = __base::__start_ + __i;
1789 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1790}
1791
1792template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001793inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001794typename deque<_Tp, _Allocator>::reference
1795deque<_Tp, _Allocator>::at(size_type __i)
1796{
1797 if (__i >= __base::size())
1798 __base::__throw_out_of_range();
1799 size_type __p = __base::__start_ + __i;
1800 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1801}
1802
1803template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001804inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001805typename deque<_Tp, _Allocator>::const_reference
1806deque<_Tp, _Allocator>::at(size_type __i) const
1807{
1808 if (__i >= __base::size())
1809 __base::__throw_out_of_range();
1810 size_type __p = __base::__start_ + __i;
1811 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1812}
1813
1814template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001815inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001816typename deque<_Tp, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00001817deque<_Tp, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001818{
1819 return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1820 + __base::__start_ % __base::__block_size);
1821}
1822
1823template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001824inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001825typename deque<_Tp, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00001826deque<_Tp, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001827{
1828 return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1829 + __base::__start_ % __base::__block_size);
1830}
1831
1832template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001833inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001834typename deque<_Tp, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00001835deque<_Tp, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001836{
1837 size_type __p = __base::size() + __base::__start_ - 1;
1838 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1839}
1840
1841template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001842inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001843typename deque<_Tp, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00001844deque<_Tp, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001845{
1846 size_type __p = __base::size() + __base::__start_ - 1;
1847 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1848}
1849
1850template <class _Tp, class _Allocator>
1851void
1852deque<_Tp, _Allocator>::push_back(const value_type& __v)
1853{
1854 allocator_type& __a = __base::__alloc();
1855 if (__back_spare() == 0)
1856 __add_back_capacity();
1857 // __back_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001858 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001859 ++__base::size();
1860}
1861
Eric Fiselier212e3f22017-04-16 03:17:01 +00001862template <class _Tp, class _Allocator>
1863void
1864deque<_Tp, _Allocator>::push_front(const value_type& __v)
1865{
1866 allocator_type& __a = __base::__alloc();
1867 if (__front_spare() == 0)
1868 __add_front_capacity();
1869 // __front_spare() >= 1
1870 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v);
1871 --__base::__start_;
1872 ++__base::size();
1873}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001874
Eric Fiselier212e3f22017-04-16 03:17:01 +00001875#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001876template <class _Tp, class _Allocator>
1877void
1878deque<_Tp, _Allocator>::push_back(value_type&& __v)
1879{
1880 allocator_type& __a = __base::__alloc();
1881 if (__back_spare() == 0)
1882 __add_back_capacity();
1883 // __back_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001884 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001885 ++__base::size();
1886}
1887
1888template <class _Tp, class _Allocator>
1889template <class... _Args>
Marshall Clowea52cc42017-01-24 23:09:12 +00001890#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001891typename deque<_Tp, _Allocator>::reference
Marshall Clowea52cc42017-01-24 23:09:12 +00001892#else
1893void
1894#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001895deque<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1896{
1897 allocator_type& __a = __base::__alloc();
1898 if (__back_spare() == 0)
1899 __add_back_capacity();
1900 // __back_spare() >= 1
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001901 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()),
1902 _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001903 ++__base::size();
Marshall Clowea52cc42017-01-24 23:09:12 +00001904#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001905 return *--__base::end();
Marshall Clowea52cc42017-01-24 23:09:12 +00001906#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001907}
1908
Howard Hinnantc51e1022010-05-11 19:42:16 +00001909template <class _Tp, class _Allocator>
1910void
1911deque<_Tp, _Allocator>::push_front(value_type&& __v)
1912{
1913 allocator_type& __a = __base::__alloc();
1914 if (__front_spare() == 0)
1915 __add_front_capacity();
1916 // __front_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001917 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001918 --__base::__start_;
1919 ++__base::size();
1920}
1921
Howard Hinnant74279a52010-09-04 23:28:19 +00001922
Howard Hinnantc51e1022010-05-11 19:42:16 +00001923template <class _Tp, class _Allocator>
1924template <class... _Args>
Marshall Clowea52cc42017-01-24 23:09:12 +00001925#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001926typename deque<_Tp, _Allocator>::reference
Marshall Clowea52cc42017-01-24 23:09:12 +00001927#else
1928void
1929#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001930deque<_Tp, _Allocator>::emplace_front(_Args&&... __args)
1931{
1932 allocator_type& __a = __base::__alloc();
1933 if (__front_spare() == 0)
1934 __add_front_capacity();
1935 // __front_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001936 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001937 --__base::__start_;
1938 ++__base::size();
Marshall Clowea52cc42017-01-24 23:09:12 +00001939#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001940 return *__base::begin();
Marshall Clowea52cc42017-01-24 23:09:12 +00001941#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001942}
1943
Eric Fiselier212e3f22017-04-16 03:17:01 +00001944template <class _Tp, class _Allocator>
1945typename deque<_Tp, _Allocator>::iterator
1946deque<_Tp, _Allocator>::insert(const_iterator __p, value_type&& __v)
1947{
1948 size_type __pos = __p - __base::begin();
1949 size_type __to_end = __base::size() - __pos;
1950 allocator_type& __a = __base::__alloc();
1951 if (__pos < __to_end)
1952 { // insert by shifting things backward
1953 if (__front_spare() == 0)
1954 __add_front_capacity();
1955 // __front_spare() >= 1
1956 if (__pos == 0)
1957 {
1958 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v));
1959 --__base::__start_;
1960 ++__base::size();
1961 }
1962 else
1963 {
1964 iterator __b = __base::begin();
1965 iterator __bm1 = _VSTD::prev(__b);
1966 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
1967 --__base::__start_;
1968 ++__base::size();
1969 if (__pos > 1)
1970 __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
1971 *__b = _VSTD::move(__v);
1972 }
1973 }
1974 else
1975 { // insert by shifting things forward
1976 if (__back_spare() == 0)
1977 __add_back_capacity();
1978 // __back_capacity >= 1
1979 size_type __de = __base::size() - __pos;
1980 if (__de == 0)
1981 {
1982 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v));
1983 ++__base::size();
1984 }
1985 else
1986 {
1987 iterator __e = __base::end();
1988 iterator __em1 = _VSTD::prev(__e);
1989 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
1990 ++__base::size();
1991 if (__de > 1)
1992 __e = _VSTD::move_backward(__e - __de, __em1, __e);
1993 *--__e = _VSTD::move(__v);
1994 }
1995 }
1996 return __base::begin() + __pos;
1997}
1998
1999template <class _Tp, class _Allocator>
2000template <class... _Args>
2001typename deque<_Tp, _Allocator>::iterator
2002deque<_Tp, _Allocator>::emplace(const_iterator __p, _Args&&... __args)
2003{
2004 size_type __pos = __p - __base::begin();
2005 size_type __to_end = __base::size() - __pos;
2006 allocator_type& __a = __base::__alloc();
2007 if (__pos < __to_end)
2008 { // insert by shifting things backward
2009 if (__front_spare() == 0)
2010 __add_front_capacity();
2011 // __front_spare() >= 1
2012 if (__pos == 0)
2013 {
2014 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...);
2015 --__base::__start_;
2016 ++__base::size();
2017 }
2018 else
2019 {
2020 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
2021 iterator __b = __base::begin();
2022 iterator __bm1 = _VSTD::prev(__b);
2023 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
2024 --__base::__start_;
2025 ++__base::size();
2026 if (__pos > 1)
2027 __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
2028 *__b = _VSTD::move(__tmp.get());
2029 }
2030 }
2031 else
2032 { // insert by shifting things forward
2033 if (__back_spare() == 0)
2034 __add_back_capacity();
2035 // __back_capacity >= 1
2036 size_type __de = __base::size() - __pos;
2037 if (__de == 0)
2038 {
2039 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::forward<_Args>(__args)...);
2040 ++__base::size();
2041 }
2042 else
2043 {
2044 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
2045 iterator __e = __base::end();
2046 iterator __em1 = _VSTD::prev(__e);
2047 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
2048 ++__base::size();
2049 if (__de > 1)
2050 __e = _VSTD::move_backward(__e - __de, __em1, __e);
2051 *--__e = _VSTD::move(__tmp.get());
2052 }
2053 }
2054 return __base::begin() + __pos;
2055}
2056
2057#endif // _LIBCPP_CXX03_LANG
2058
Howard Hinnantc51e1022010-05-11 19:42:16 +00002059
2060template <class _Tp, class _Allocator>
2061typename deque<_Tp, _Allocator>::iterator
2062deque<_Tp, _Allocator>::insert(const_iterator __p, const value_type& __v)
2063{
2064 size_type __pos = __p - __base::begin();
2065 size_type __to_end = __base::size() - __pos;
2066 allocator_type& __a = __base::__alloc();
2067 if (__pos < __to_end)
2068 { // insert by shifting things backward
2069 if (__front_spare() == 0)
2070 __add_front_capacity();
2071 // __front_spare() >= 1
2072 if (__pos == 0)
2073 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002074 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002075 --__base::__start_;
2076 ++__base::size();
2077 }
2078 else
2079 {
2080 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2081 iterator __b = __base::begin();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002082 iterator __bm1 = _VSTD::prev(__b);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002083 if (__vt == pointer_traits<const_pointer>::pointer_to(*__b))
2084 __vt = pointer_traits<const_pointer>::pointer_to(*__bm1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002085 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002086 --__base::__start_;
2087 ++__base::size();
2088 if (__pos > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002089 __b = __move_and_check(_VSTD::next(__b), __b + __pos, __b, __vt);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002090 *__b = *__vt;
2091 }
2092 }
2093 else
2094 { // insert by shifting things forward
2095 if (__back_spare() == 0)
2096 __add_back_capacity();
2097 // __back_capacity >= 1
2098 size_type __de = __base::size() - __pos;
2099 if (__de == 0)
2100 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002101 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002102 ++__base::size();
2103 }
2104 else
2105 {
2106 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2107 iterator __e = __base::end();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002108 iterator __em1 = _VSTD::prev(__e);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002109 if (__vt == pointer_traits<const_pointer>::pointer_to(*__em1))
2110 __vt = pointer_traits<const_pointer>::pointer_to(*__e);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002111 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002112 ++__base::size();
2113 if (__de > 1)
2114 __e = __move_backward_and_check(__e - __de, __em1, __e, __vt);
2115 *--__e = *__vt;
2116 }
2117 }
2118 return __base::begin() + __pos;
2119}
2120
Howard Hinnantc51e1022010-05-11 19:42:16 +00002121template <class _Tp, class _Allocator>
2122typename deque<_Tp, _Allocator>::iterator
2123deque<_Tp, _Allocator>::insert(const_iterator __p, size_type __n, const value_type& __v)
2124{
2125 size_type __pos = __p - __base::begin();
2126 size_type __to_end = __base::size() - __pos;
2127 allocator_type& __a = __base::__alloc();
2128 if (__pos < __to_end)
2129 { // insert by shifting things backward
2130 if (__n > __front_spare())
2131 __add_front_capacity(__n - __front_spare());
2132 // __n <= __front_spare()
Howard Hinnantc51e1022010-05-11 19:42:16 +00002133 iterator __old_begin = __base::begin();
2134 iterator __i = __old_begin;
2135 if (__n > __pos)
2136 {
2137 for (size_type __m = __n - __pos; __m; --__m, --__base::__start_, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002138 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002139 __n = __pos;
2140 }
2141 if (__n > 0)
2142 {
2143 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2144 iterator __obn = __old_begin + __n;
2145 __move_construct_backward_and_check(__old_begin, __obn, __i, __vt);
2146 if (__n < __pos)
2147 __old_begin = __move_and_check(__obn, __old_begin + __pos, __old_begin, __vt);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002148 _VSTD::fill_n(__old_begin, __n, *__vt);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002149 }
2150 }
2151 else
2152 { // insert by shifting things forward
2153 size_type __back_capacity = __back_spare();
2154 if (__n > __back_capacity)
2155 __add_back_capacity(__n - __back_capacity);
2156 // __n <= __back_capacity
Howard Hinnantc51e1022010-05-11 19:42:16 +00002157 iterator __old_end = __base::end();
2158 iterator __i = __old_end;
2159 size_type __de = __base::size() - __pos;
2160 if (__n > __de)
2161 {
2162 for (size_type __m = __n - __de; __m; --__m, ++__i, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002163 __alloc_traits::construct(__a, _VSTD::addressof(*__i), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002164 __n = __de;
2165 }
2166 if (__n > 0)
2167 {
2168 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2169 iterator __oen = __old_end - __n;
2170 __move_construct_and_check(__oen, __old_end, __i, __vt);
2171 if (__n < __de)
2172 __old_end = __move_backward_and_check(__old_end - __de, __oen, __old_end, __vt);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002173 _VSTD::fill_n(__old_end - __n, __n, *__vt);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002174 }
2175 }
2176 return __base::begin() + __pos;
2177}
2178
2179template <class _Tp, class _Allocator>
2180template <class _InputIter>
2181typename deque<_Tp, _Allocator>::iterator
2182deque<_Tp, _Allocator>::insert(const_iterator __p, _InputIter __f, _InputIter __l,
2183 typename enable_if<__is_input_iterator<_InputIter>::value
Marshall Clow6b612cf2015-01-22 18:33:29 +00002184 &&!__is_forward_iterator<_InputIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002185{
2186 __split_buffer<value_type, allocator_type&> __buf(__base::__alloc());
2187 __buf.__construct_at_end(__f, __l);
2188 typedef typename __split_buffer<value_type, allocator_type&>::iterator __bi;
2189 return insert(__p, move_iterator<__bi>(__buf.begin()), move_iterator<__bi>(__buf.end()));
2190}
2191
2192template <class _Tp, class _Allocator>
Marshall Clow6b612cf2015-01-22 18:33:29 +00002193template <class _ForwardIterator>
2194typename deque<_Tp, _Allocator>::iterator
2195deque<_Tp, _Allocator>::insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l,
2196 typename enable_if<__is_forward_iterator<_ForwardIterator>::value
2197 &&!__is_bidirectional_iterator<_ForwardIterator>::value>::type*)
2198{
2199 size_type __n = _VSTD::distance(__f, __l);
2200 __split_buffer<value_type, allocator_type&> __buf(__n, 0, __base::__alloc());
2201 __buf.__construct_at_end(__f, __l);
2202 typedef typename __split_buffer<value_type, allocator_type&>::iterator __fwd;
2203 return insert(__p, move_iterator<__fwd>(__buf.begin()), move_iterator<__fwd>(__buf.end()));
2204}
2205
2206template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002207template <class _BiIter>
2208typename deque<_Tp, _Allocator>::iterator
2209deque<_Tp, _Allocator>::insert(const_iterator __p, _BiIter __f, _BiIter __l,
2210 typename enable_if<__is_bidirectional_iterator<_BiIter>::value>::type*)
2211{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002212 size_type __n = _VSTD::distance(__f, __l);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002213 size_type __pos = __p - __base::begin();
2214 size_type __to_end = __base::size() - __pos;
2215 allocator_type& __a = __base::__alloc();
2216 if (__pos < __to_end)
2217 { // insert by shifting things backward
2218 if (__n > __front_spare())
2219 __add_front_capacity(__n - __front_spare());
2220 // __n <= __front_spare()
Howard Hinnantc51e1022010-05-11 19:42:16 +00002221 iterator __old_begin = __base::begin();
2222 iterator __i = __old_begin;
2223 _BiIter __m = __f;
2224 if (__n > __pos)
2225 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002226 __m = __pos < __n / 2 ? _VSTD::prev(__l, __pos) : _VSTD::next(__f, __n - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002227 for (_BiIter __j = __m; __j != __f; --__base::__start_, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002228 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), *--__j);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002229 __n = __pos;
2230 }
2231 if (__n > 0)
2232 {
2233 iterator __obn = __old_begin + __n;
2234 for (iterator __j = __obn; __j != __old_begin;)
2235 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002236 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), _VSTD::move(*--__j));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002237 --__base::__start_;
2238 ++__base::size();
2239 }
2240 if (__n < __pos)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002241 __old_begin = _VSTD::move(__obn, __old_begin + __pos, __old_begin);
2242 _VSTD::copy(__m, __l, __old_begin);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002243 }
2244 }
2245 else
2246 { // insert by shifting things forward
2247 size_type __back_capacity = __back_spare();
2248 if (__n > __back_capacity)
2249 __add_back_capacity(__n - __back_capacity);
2250 // __n <= __back_capacity
Howard Hinnantc51e1022010-05-11 19:42:16 +00002251 iterator __old_end = __base::end();
2252 iterator __i = __old_end;
2253 _BiIter __m = __l;
2254 size_type __de = __base::size() - __pos;
2255 if (__n > __de)
2256 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002257 __m = __de < __n / 2 ? _VSTD::next(__f, __de) : _VSTD::prev(__l, __n - __de);
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002258 for (_BiIter __j = __m; __j != __l; ++__i, (void) ++__j, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002259 __alloc_traits::construct(__a, _VSTD::addressof(*__i), *__j);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002260 __n = __de;
2261 }
2262 if (__n > 0)
2263 {
2264 iterator __oen = __old_end - __n;
2265 for (iterator __j = __oen; __j != __old_end; ++__i, ++__j, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002266 __alloc_traits::construct(__a, _VSTD::addressof(*__i), _VSTD::move(*__j));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002267 if (__n < __de)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002268 __old_end = _VSTD::move_backward(__old_end - __de, __oen, __old_end);
2269 _VSTD::copy_backward(__f, __m, __old_end);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002270 }
2271 }
2272 return __base::begin() + __pos;
2273}
2274
2275template <class _Tp, class _Allocator>
2276template <class _InpIter>
2277void
2278deque<_Tp, _Allocator>::__append(_InpIter __f, _InpIter __l,
2279 typename enable_if<__is_input_iterator<_InpIter>::value &&
2280 !__is_forward_iterator<_InpIter>::value>::type*)
2281{
2282 for (; __f != __l; ++__f)
Eric Fiselier96919722017-10-17 13:03:17 +00002283#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002284 push_back(*__f);
Eric Fiselier96919722017-10-17 13:03:17 +00002285#else
2286 emplace_back(*__f);
2287#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002288}
2289
2290template <class _Tp, class _Allocator>
2291template <class _ForIter>
2292void
2293deque<_Tp, _Allocator>::__append(_ForIter __f, _ForIter __l,
2294 typename enable_if<__is_forward_iterator<_ForIter>::value>::type*)
2295{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002296 size_type __n = _VSTD::distance(__f, __l);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002297 allocator_type& __a = __base::__alloc();
2298 size_type __back_capacity = __back_spare();
2299 if (__n > __back_capacity)
2300 __add_back_capacity(__n - __back_capacity);
2301 // __n <= __back_capacity
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002302 for (iterator __i = __base::end(); __f != __l; ++__i, (void) ++__f, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002303 __alloc_traits::construct(__a, _VSTD::addressof(*__i), *__f);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002304}
2305
2306template <class _Tp, class _Allocator>
2307void
2308deque<_Tp, _Allocator>::__append(size_type __n)
2309{
2310 allocator_type& __a = __base::__alloc();
2311 size_type __back_capacity = __back_spare();
2312 if (__n > __back_capacity)
2313 __add_back_capacity(__n - __back_capacity);
2314 // __n <= __back_capacity
2315 for (iterator __i = __base::end(); __n; --__n, ++__i, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002316 __alloc_traits::construct(__a, _VSTD::addressof(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002317}
2318
2319template <class _Tp, class _Allocator>
2320void
2321deque<_Tp, _Allocator>::__append(size_type __n, const value_type& __v)
2322{
2323 allocator_type& __a = __base::__alloc();
2324 size_type __back_capacity = __back_spare();
2325 if (__n > __back_capacity)
2326 __add_back_capacity(__n - __back_capacity);
2327 // __n <= __back_capacity
2328 for (iterator __i = __base::end(); __n; --__n, ++__i, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002329 __alloc_traits::construct(__a, _VSTD::addressof(*__i), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002330}
2331
2332// Create front capacity for one block of elements.
2333// Strong guarantee. Either do it or don't touch anything.
2334template <class _Tp, class _Allocator>
2335void
2336deque<_Tp, _Allocator>::__add_front_capacity()
2337{
2338 allocator_type& __a = __base::__alloc();
2339 if (__back_spare() >= __base::__block_size)
2340 {
2341 __base::__start_ += __base::__block_size;
2342 pointer __pt = __base::__map_.back();
2343 __base::__map_.pop_back();
2344 __base::__map_.push_front(__pt);
2345 }
2346 // Else if __base::__map_.size() < __base::__map_.capacity() then we need to allocate 1 buffer
2347 else if (__base::__map_.size() < __base::__map_.capacity())
2348 { // we can put the new buffer into the map, but don't shift things around
2349 // until all buffers are allocated. If we throw, we don't need to fix
2350 // anything up (any added buffers are undetectible)
2351 if (__base::__map_.__front_spare() > 0)
2352 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2353 else
2354 {
2355 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2356 // Done allocating, reorder capacity
2357 pointer __pt = __base::__map_.back();
2358 __base::__map_.pop_back();
2359 __base::__map_.push_front(__pt);
2360 }
2361 __base::__start_ = __base::__map_.size() == 1 ?
2362 __base::__block_size / 2 :
2363 __base::__start_ + __base::__block_size;
2364 }
2365 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2366 else
2367 {
2368 __split_buffer<pointer, typename __base::__pointer_allocator&>
2369 __buf(max<size_type>(2 * __base::__map_.capacity(), 1),
2370 0, __base::__map_.__alloc());
Marshall Clow5fd466b2015-03-09 17:08:51 +00002371
Marshall Clow8982dcd2015-07-13 20:04:56 +00002372 typedef __allocator_destructor<_Allocator> _Dp;
2373 unique_ptr<pointer, _Dp> __hold(
2374 __alloc_traits::allocate(__a, __base::__block_size),
2375 _Dp(__a, __base::__block_size));
2376 __buf.push_back(__hold.get());
2377 __hold.release();
Louis Dionne72f24392018-12-12 23:58:25 +00002378
Howard Hinnantc51e1022010-05-11 19:42:16 +00002379 for (typename __base::__map_pointer __i = __base::__map_.begin();
2380 __i != __base::__map_.end(); ++__i)
2381 __buf.push_back(*__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002382 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2383 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2384 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2385 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002386 __base::__start_ = __base::__map_.size() == 1 ?
2387 __base::__block_size / 2 :
2388 __base::__start_ + __base::__block_size;
2389 }
2390}
2391
2392// Create front capacity for __n elements.
2393// Strong guarantee. Either do it or don't touch anything.
2394template <class _Tp, class _Allocator>
2395void
2396deque<_Tp, _Allocator>::__add_front_capacity(size_type __n)
2397{
2398 allocator_type& __a = __base::__alloc();
2399 size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2400 // Number of unused blocks at back:
2401 size_type __back_capacity = __back_spare() / __base::__block_size;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002402 __back_capacity = _VSTD::min(__back_capacity, __nb); // don't take more than you need
Howard Hinnantc51e1022010-05-11 19:42:16 +00002403 __nb -= __back_capacity; // number of blocks need to allocate
2404 // If __nb == 0, then we have sufficient capacity.
2405 if (__nb == 0)
2406 {
2407 __base::__start_ += __base::__block_size * __back_capacity;
2408 for (; __back_capacity > 0; --__back_capacity)
2409 {
2410 pointer __pt = __base::__map_.back();
2411 __base::__map_.pop_back();
2412 __base::__map_.push_front(__pt);
2413 }
2414 }
2415 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2416 else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2417 { // we can put the new buffers into the map, but don't shift things around
2418 // until all buffers are allocated. If we throw, we don't need to fix
2419 // anything up (any added buffers are undetectible)
2420 for (; __nb > 0; --__nb, __base::__start_ += __base::__block_size - (__base::__map_.size() == 1))
2421 {
2422 if (__base::__map_.__front_spare() == 0)
2423 break;
2424 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2425 }
2426 for (; __nb > 0; --__nb, ++__back_capacity)
2427 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2428 // Done allocating, reorder capacity
2429 __base::__start_ += __back_capacity * __base::__block_size;
2430 for (; __back_capacity > 0; --__back_capacity)
2431 {
2432 pointer __pt = __base::__map_.back();
2433 __base::__map_.pop_back();
2434 __base::__map_.push_front(__pt);
2435 }
2436 }
2437 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2438 else
2439 {
2440 size_type __ds = (__nb + __back_capacity) * __base::__block_size - __base::__map_.empty();
2441 __split_buffer<pointer, typename __base::__pointer_allocator&>
2442 __buf(max<size_type>(2* __base::__map_.capacity(),
2443 __nb + __base::__map_.size()),
2444 0, __base::__map_.__alloc());
2445#ifndef _LIBCPP_NO_EXCEPTIONS
2446 try
2447 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002448#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002449 for (; __nb > 0; --__nb)
2450 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2451#ifndef _LIBCPP_NO_EXCEPTIONS
2452 }
2453 catch (...)
2454 {
2455 for (typename __base::__map_pointer __i = __buf.begin();
2456 __i != __buf.end(); ++__i)
2457 __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2458 throw;
2459 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002460#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002461 for (; __back_capacity > 0; --__back_capacity)
2462 {
2463 __buf.push_back(__base::__map_.back());
2464 __base::__map_.pop_back();
2465 }
2466 for (typename __base::__map_pointer __i = __base::__map_.begin();
2467 __i != __base::__map_.end(); ++__i)
2468 __buf.push_back(*__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002469 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2470 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2471 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2472 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002473 __base::__start_ += __ds;
2474 }
2475}
2476
2477// Create back capacity for one block of elements.
2478// Strong guarantee. Either do it or don't touch anything.
2479template <class _Tp, class _Allocator>
2480void
2481deque<_Tp, _Allocator>::__add_back_capacity()
2482{
2483 allocator_type& __a = __base::__alloc();
2484 if (__front_spare() >= __base::__block_size)
2485 {
2486 __base::__start_ -= __base::__block_size;
2487 pointer __pt = __base::__map_.front();
2488 __base::__map_.pop_front();
2489 __base::__map_.push_back(__pt);
2490 }
2491 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2492 else if (__base::__map_.size() < __base::__map_.capacity())
2493 { // we can put the new buffer into the map, but don't shift things around
2494 // until it is allocated. If we throw, we don't need to fix
2495 // anything up (any added buffers are undetectible)
2496 if (__base::__map_.__back_spare() != 0)
2497 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2498 else
2499 {
2500 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2501 // Done allocating, reorder capacity
2502 pointer __pt = __base::__map_.front();
2503 __base::__map_.pop_front();
2504 __base::__map_.push_back(__pt);
2505 }
2506 }
2507 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2508 else
2509 {
2510 __split_buffer<pointer, typename __base::__pointer_allocator&>
2511 __buf(max<size_type>(2* __base::__map_.capacity(), 1),
2512 __base::__map_.size(),
2513 __base::__map_.__alloc());
Marshall Clow5fd466b2015-03-09 17:08:51 +00002514
Marshall Clow8982dcd2015-07-13 20:04:56 +00002515 typedef __allocator_destructor<_Allocator> _Dp;
2516 unique_ptr<pointer, _Dp> __hold(
2517 __alloc_traits::allocate(__a, __base::__block_size),
2518 _Dp(__a, __base::__block_size));
2519 __buf.push_back(__hold.get());
2520 __hold.release();
Marshall Clow5fd466b2015-03-09 17:08:51 +00002521
Howard Hinnantc51e1022010-05-11 19:42:16 +00002522 for (typename __base::__map_pointer __i = __base::__map_.end();
2523 __i != __base::__map_.begin();)
2524 __buf.push_front(*--__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002525 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2526 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2527 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2528 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002529 }
2530}
2531
2532// Create back capacity for __n elements.
2533// Strong guarantee. Either do it or don't touch anything.
2534template <class _Tp, class _Allocator>
2535void
2536deque<_Tp, _Allocator>::__add_back_capacity(size_type __n)
2537{
2538 allocator_type& __a = __base::__alloc();
2539 size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2540 // Number of unused blocks at front:
2541 size_type __front_capacity = __front_spare() / __base::__block_size;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002542 __front_capacity = _VSTD::min(__front_capacity, __nb); // don't take more than you need
Howard Hinnantc51e1022010-05-11 19:42:16 +00002543 __nb -= __front_capacity; // number of blocks need to allocate
2544 // If __nb == 0, then we have sufficient capacity.
2545 if (__nb == 0)
2546 {
2547 __base::__start_ -= __base::__block_size * __front_capacity;
2548 for (; __front_capacity > 0; --__front_capacity)
2549 {
2550 pointer __pt = __base::__map_.front();
2551 __base::__map_.pop_front();
2552 __base::__map_.push_back(__pt);
2553 }
2554 }
2555 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2556 else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2557 { // we can put the new buffers into the map, but don't shift things around
2558 // until all buffers are allocated. If we throw, we don't need to fix
2559 // anything up (any added buffers are undetectible)
2560 for (; __nb > 0; --__nb)
2561 {
2562 if (__base::__map_.__back_spare() == 0)
2563 break;
2564 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2565 }
2566 for (; __nb > 0; --__nb, ++__front_capacity, __base::__start_ +=
2567 __base::__block_size - (__base::__map_.size() == 1))
2568 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2569 // Done allocating, reorder capacity
2570 __base::__start_ -= __base::__block_size * __front_capacity;
2571 for (; __front_capacity > 0; --__front_capacity)
2572 {
2573 pointer __pt = __base::__map_.front();
2574 __base::__map_.pop_front();
2575 __base::__map_.push_back(__pt);
2576 }
2577 }
2578 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2579 else
2580 {
2581 size_type __ds = __front_capacity * __base::__block_size;
2582 __split_buffer<pointer, typename __base::__pointer_allocator&>
2583 __buf(max<size_type>(2* __base::__map_.capacity(),
2584 __nb + __base::__map_.size()),
2585 __base::__map_.size() - __front_capacity,
2586 __base::__map_.__alloc());
2587#ifndef _LIBCPP_NO_EXCEPTIONS
2588 try
2589 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002590#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002591 for (; __nb > 0; --__nb)
2592 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2593#ifndef _LIBCPP_NO_EXCEPTIONS
2594 }
2595 catch (...)
2596 {
2597 for (typename __base::__map_pointer __i = __buf.begin();
2598 __i != __buf.end(); ++__i)
2599 __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2600 throw;
2601 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002602#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002603 for (; __front_capacity > 0; --__front_capacity)
2604 {
2605 __buf.push_back(__base::__map_.front());
2606 __base::__map_.pop_front();
2607 }
2608 for (typename __base::__map_pointer __i = __base::__map_.end();
2609 __i != __base::__map_.begin();)
2610 __buf.push_front(*--__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002611 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2612 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2613 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2614 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002615 __base::__start_ -= __ds;
2616 }
2617}
2618
2619template <class _Tp, class _Allocator>
2620void
2621deque<_Tp, _Allocator>::pop_front()
2622{
2623 allocator_type& __a = __base::__alloc();
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002624 __alloc_traits::destroy(__a, __to_raw_pointer(*(__base::__map_.begin() +
2625 __base::__start_ / __base::__block_size) +
2626 __base::__start_ % __base::__block_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002627 --__base::size();
Eric Fiselier27e03622019-08-01 23:11:18 +00002628 ++__base::__start_;
2629 __maybe_remove_front_spare();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002630}
2631
2632template <class _Tp, class _Allocator>
2633void
2634deque<_Tp, _Allocator>::pop_back()
2635{
Louis Dionne72f24392018-12-12 23:58:25 +00002636 _LIBCPP_ASSERT(!empty(), "deque::pop_back called for empty deque");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002637 allocator_type& __a = __base::__alloc();
2638 size_type __p = __base::size() + __base::__start_ - 1;
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002639 __alloc_traits::destroy(__a, __to_raw_pointer(*(__base::__map_.begin() +
2640 __p / __base::__block_size) +
2641 __p % __base::__block_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002642 --__base::size();
Eric Fiselier27e03622019-08-01 23:11:18 +00002643 __maybe_remove_back_spare();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002644}
2645
2646// move assign [__f, __l) to [__r, __r + (__l-__f)).
2647// If __vt points into [__f, __l), then subtract (__f - __r) from __vt.
2648template <class _Tp, class _Allocator>
2649typename deque<_Tp, _Allocator>::iterator
2650deque<_Tp, _Allocator>::__move_and_check(iterator __f, iterator __l, iterator __r,
2651 const_pointer& __vt)
2652{
2653 // as if
2654 // for (; __f != __l; ++__f, ++__r)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002655 // *__r = _VSTD::move(*__f);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002656 difference_type __n = __l - __f;
2657 while (__n > 0)
2658 {
2659 pointer __fb = __f.__ptr_;
2660 pointer __fe = *__f.__m_iter_ + __base::__block_size;
2661 difference_type __bs = __fe - __fb;
2662 if (__bs > __n)
2663 {
2664 __bs = __n;
2665 __fe = __fb + __bs;
2666 }
2667 if (__fb <= __vt && __vt < __fe)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002668 __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) -= __f - __r).__ptr_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002669 __r = _VSTD::move(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002670 __n -= __bs;
2671 __f += __bs;
2672 }
2673 return __r;
2674}
2675
2676// move assign [__f, __l) to [__r - (__l-__f), __r) backwards.
2677// If __vt points into [__f, __l), then add (__r - __l) to __vt.
2678template <class _Tp, class _Allocator>
2679typename deque<_Tp, _Allocator>::iterator
2680deque<_Tp, _Allocator>::__move_backward_and_check(iterator __f, iterator __l, iterator __r,
2681 const_pointer& __vt)
2682{
2683 // as if
2684 // while (__f != __l)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002685 // *--__r = _VSTD::move(*--__l);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002686 difference_type __n = __l - __f;
2687 while (__n > 0)
2688 {
2689 --__l;
2690 pointer __lb = *__l.__m_iter_;
2691 pointer __le = __l.__ptr_ + 1;
2692 difference_type __bs = __le - __lb;
2693 if (__bs > __n)
2694 {
2695 __bs = __n;
2696 __lb = __le - __bs;
2697 }
2698 if (__lb <= __vt && __vt < __le)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002699 __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) += __r - __l - 1).__ptr_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002700 __r = _VSTD::move_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002701 __n -= __bs;
2702 __l -= __bs - 1;
2703 }
2704 return __r;
2705}
2706
2707// move construct [__f, __l) to [__r, __r + (__l-__f)).
2708// If __vt points into [__f, __l), then add (__r - __f) to __vt.
2709template <class _Tp, class _Allocator>
2710void
2711deque<_Tp, _Allocator>::__move_construct_and_check(iterator __f, iterator __l,
2712 iterator __r, const_pointer& __vt)
2713{
2714 allocator_type& __a = __base::__alloc();
2715 // as if
2716 // for (; __f != __l; ++__r, ++__f, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002717 // __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__f));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002718 difference_type __n = __l - __f;
2719 while (__n > 0)
2720 {
2721 pointer __fb = __f.__ptr_;
2722 pointer __fe = *__f.__m_iter_ + __base::__block_size;
2723 difference_type __bs = __fe - __fb;
2724 if (__bs > __n)
2725 {
2726 __bs = __n;
2727 __fe = __fb + __bs;
2728 }
2729 if (__fb <= __vt && __vt < __fe)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002730 __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) += __r - __f).__ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002731 for (; __fb != __fe; ++__fb, ++__r, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002732 __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__fb));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002733 __n -= __bs;
2734 __f += __bs;
2735 }
2736}
2737
2738// move construct [__f, __l) to [__r - (__l-__f), __r) backwards.
2739// If __vt points into [__f, __l), then subtract (__l - __r) from __vt.
2740template <class _Tp, class _Allocator>
2741void
2742deque<_Tp, _Allocator>::__move_construct_backward_and_check(iterator __f, iterator __l,
2743 iterator __r, const_pointer& __vt)
2744{
2745 allocator_type& __a = __base::__alloc();
2746 // as if
2747 // for (iterator __j = __l; __j != __f;)
2748 // {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002749 // __alloc_traitsconstruct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__j));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002750 // --__base::__start_;
2751 // ++__base::size();
2752 // }
2753 difference_type __n = __l - __f;
2754 while (__n > 0)
2755 {
2756 --__l;
2757 pointer __lb = *__l.__m_iter_;
2758 pointer __le = __l.__ptr_ + 1;
2759 difference_type __bs = __le - __lb;
2760 if (__bs > __n)
2761 {
2762 __bs = __n;
2763 __lb = __le - __bs;
2764 }
2765 if (__lb <= __vt && __vt < __le)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002766 __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) -= __l - __r + 1).__ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002767 while (__le != __lb)
2768 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002769 __alloc_traits::construct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__le));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002770 --__base::__start_;
2771 ++__base::size();
2772 }
2773 __n -= __bs;
2774 __l -= __bs - 1;
2775 }
2776}
2777
2778template <class _Tp, class _Allocator>
2779typename deque<_Tp, _Allocator>::iterator
2780deque<_Tp, _Allocator>::erase(const_iterator __f)
2781{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002782 iterator __b = __base::begin();
2783 difference_type __pos = __f - __b;
2784 iterator __p = __b + __pos;
2785 allocator_type& __a = __base::__alloc();
Eric Fiselier37c22152016-12-24 00:24:44 +00002786 if (static_cast<size_t>(__pos) <= (__base::size() - 1) / 2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002787 { // erase from front
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002788 _VSTD::move_backward(__b, __p, _VSTD::next(__p));
2789 __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002790 --__base::size();
2791 ++__base::__start_;
Eric Fiselier27e03622019-08-01 23:11:18 +00002792 __maybe_remove_front_spare();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002793 }
2794 else
2795 { // erase from back
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002796 iterator __i = _VSTD::move(_VSTD::next(__p), __base::end(), __p);
2797 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002798 --__base::size();
Eric Fiselier27e03622019-08-01 23:11:18 +00002799 __maybe_remove_back_spare();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002800 }
2801 return __base::begin() + __pos;
2802}
2803
2804template <class _Tp, class _Allocator>
2805typename deque<_Tp, _Allocator>::iterator
2806deque<_Tp, _Allocator>::erase(const_iterator __f, const_iterator __l)
2807{
2808 difference_type __n = __l - __f;
2809 iterator __b = __base::begin();
2810 difference_type __pos = __f - __b;
2811 iterator __p = __b + __pos;
2812 if (__n > 0)
2813 {
2814 allocator_type& __a = __base::__alloc();
Eric Fiselier37c22152016-12-24 00:24:44 +00002815 if (static_cast<size_t>(__pos) <= (__base::size() - __n) / 2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002816 { // erase from front
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002817 iterator __i = _VSTD::move_backward(__b, __p, __p + __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002818 for (; __b != __i; ++__b)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002819 __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002820 __base::size() -= __n;
2821 __base::__start_ += __n;
Eric Fiselier27e03622019-08-01 23:11:18 +00002822 while (__maybe_remove_front_spare()) {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002823 }
2824 }
2825 else
2826 { // erase from back
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002827 iterator __i = _VSTD::move(__p + __n, __base::end(), __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002828 for (iterator __e = __base::end(); __i != __e; ++__i)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002829 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002830 __base::size() -= __n;
Eric Fiselier27e03622019-08-01 23:11:18 +00002831 while (__maybe_remove_back_spare()) {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002832 }
2833 }
2834 }
2835 return __base::begin() + __pos;
2836}
2837
2838template <class _Tp, class _Allocator>
2839void
2840deque<_Tp, _Allocator>::__erase_to_end(const_iterator __f)
2841{
2842 iterator __e = __base::end();
2843 difference_type __n = __e - __f;
2844 if (__n > 0)
2845 {
2846 allocator_type& __a = __base::__alloc();
2847 iterator __b = __base::begin();
2848 difference_type __pos = __f - __b;
2849 for (iterator __p = __b + __pos; __p != __e; ++__p)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002850 __alloc_traits::destroy(__a, _VSTD::addressof(*__p));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002851 __base::size() -= __n;
Eric Fiselier27e03622019-08-01 23:11:18 +00002852 while (__maybe_remove_back_spare()) {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002853 }
2854 }
2855}
2856
2857template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002858inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002859void
2860deque<_Tp, _Allocator>::swap(deque& __c)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002861#if _LIBCPP_STD_VER >= 14
2862 _NOEXCEPT
2863#else
Louis Dionne72f24392018-12-12 23:58:25 +00002864 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002865 __is_nothrow_swappable<allocator_type>::value)
2866#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002867{
2868 __base::swap(__c);
2869}
2870
2871template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002872inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002873void
Howard Hinnantda2df912011-06-02 16:10:22 +00002874deque<_Tp, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002875{
2876 __base::clear();
2877}
2878
2879template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002880inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002881bool
2882operator==(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2883{
2884 const typename deque<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002885 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002886}
2887
2888template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002889inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002890bool
2891operator!=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2892{
2893 return !(__x == __y);
2894}
2895
2896template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002897inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002898bool
2899operator< (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2900{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002901 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002902}
2903
2904template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002905inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002906bool
2907operator> (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2908{
2909 return __y < __x;
2910}
2911
2912template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002913inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002914bool
2915operator>=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2916{
2917 return !(__x < __y);
2918}
2919
2920template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002921inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002922bool
2923operator<=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2924{
2925 return !(__y < __x);
2926}
2927
2928template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002929inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002930void
2931swap(deque<_Tp, _Allocator>& __x, deque<_Tp, _Allocator>& __y)
Howard Hinnantca2fc222011-06-02 20:00:14 +00002932 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002933{
2934 __x.swap(__y);
2935}
2936
Marshall Clow29b53f22018-12-14 18:49:35 +00002937#if _LIBCPP_STD_VER > 17
2938template <class _Tp, class _Allocator, class _Up>
2939inline _LIBCPP_INLINE_VISIBILITY
2940void erase(deque<_Tp, _Allocator>& __c, const _Up& __v)
2941{ __c.erase(_VSTD::remove(__c.begin(), __c.end(), __v), __c.end()); }
2942
2943template <class _Tp, class _Allocator, class _Predicate>
2944inline _LIBCPP_INLINE_VISIBILITY
2945void erase_if(deque<_Tp, _Allocator>& __c, _Predicate __pred)
2946{ __c.erase(_VSTD::remove_if(__c.begin(), __c.end(), __pred), __c.end()); }
2947#endif
2948
2949
Howard Hinnantc51e1022010-05-11 19:42:16 +00002950_LIBCPP_END_NAMESPACE_STD
2951
Eric Fiselierf4433a32017-05-31 22:07:49 +00002952_LIBCPP_POP_MACROS
2953
Howard Hinnantc51e1022010-05-11 19:42:16 +00002954#endif // _LIBCPP_DEQUE