2 //===---------------------------- deque -----------------------------------===//
4 // 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
8 //===----------------------------------------------------------------------===//
19 template <class T, class Allocator = allocator<T> >
25 typedef Allocator allocator_type;
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;
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;
39 // construct/copy/destroy:
40 deque() noexcept(is_nothrow_default_constructible<allocator_type>::value);
41 explicit deque(const allocator_type& a);
42 explicit deque(size_type n);
43 explicit deque(size_type n, const allocator_type& a); // C++14
44 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);
52 noexcept(is_nothrow_move_constructible<allocator_type>::value);
53 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);
58 deque& operator=(const deque& c);
59 deque& operator=(deque&& c)
61 allocator_type::propagate_on_container_move_assignment::value &&
62 is_nothrow_move_assignable<allocator_type>::value);
63 deque& operator=(initializer_list<value_type> il);
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);
70 allocator_type get_allocator() const noexcept;
74 iterator begin() noexcept;
75 const_iterator begin() const noexcept;
76 iterator end() noexcept;
77 const_iterator end() const noexcept;
79 reverse_iterator rbegin() noexcept;
80 const_reverse_iterator rbegin() const noexcept;
81 reverse_iterator rend() noexcept;
82 const_reverse_iterator rend() const noexcept;
84 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;
90 size_type size() const noexcept;
91 size_type max_size() const noexcept;
92 void resize(size_type n);
93 void resize(size_type n, const value_type& v);
95 bool empty() const noexcept;
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;
103 const_reference front() const;
105 const_reference back() const;
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);
112 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
114 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>
119 iterator insert(const_iterator p, InputIterator f, InputIterator l);
120 iterator insert(const_iterator p, initializer_list<value_type> il);
123 iterator erase(const_iterator p);
124 iterator erase(const_iterator f, const_iterator l);
126 noexcept(allocator_traits<allocator_type>::is_always_equal::value); // C++17
127 void clear() noexcept;
130 template <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>;
134 template <class T, class Allocator>
135 bool operator==(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
136 template <class T, class Allocator>
137 bool operator< (const deque<T,Allocator>& x, const deque<T,Allocator>& y);
138 template <class T, class Allocator>
139 bool operator!=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
140 template <class T, class Allocator>
141 bool operator> (const deque<T,Allocator>& x, const deque<T,Allocator>& y);
142 template <class T, class Allocator>
143 bool operator>=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
144 template <class T, class Allocator>
145 bool operator<=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
147 // specialized algorithms:
148 template <class T, class Allocator>
149 void swap(deque<T,Allocator>& x, deque<T,Allocator>& y)
150 noexcept(noexcept(x.swap(y)));
152 template <class T, class Allocator, class U>
153 typename deque<T, Allocator>::size_type
154 erase(deque<T, Allocator>& c, const U& value); // C++20
155 template <class T, class Allocator, class Predicate>
156 typename deque<T, Allocator>::size_type
157 erase_if(deque<T, Allocator>& c, Predicate pred); // C++20
164 #include <__split_buffer>
165 #include <type_traits>
166 #include <initializer_list>
172 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
173 #pragma GCC system_header
177 #include <__undef_macros>
180 _LIBCPP_BEGIN_NAMESPACE_STD
182 template <class _Tp, class _Allocator> class __deque_base;
183 template <class _Tp, class _Allocator = allocator<_Tp> > class _LIBCPP_TEMPLATE_VIS deque;
185 template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
186 class _DiffType, _DiffType _BlockSize>
187 class _LIBCPP_TEMPLATE_VIS __deque_iterator;
189 template <class _RAIter,
190 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
191 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
194 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
195 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type* = 0);
197 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
198 class _OutputIterator>
200 copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
201 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
202 _OutputIterator __r);
204 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
205 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
206 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
207 copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
208 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
209 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
211 template <class _RAIter,
212 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
213 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
214 copy_backward(_RAIter __f,
216 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
217 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type* = 0);
219 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
220 class _OutputIterator>
222 copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
223 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
224 _OutputIterator __r);
226 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
227 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
228 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
229 copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
230 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
231 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
233 template <class _RAIter,
234 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
235 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
238 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
239 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type* = 0);
241 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
242 class _OutputIterator>
244 move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
245 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
246 _OutputIterator __r);
248 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
249 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
250 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
251 move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
252 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
253 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
255 template <class _RAIter,
256 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
257 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
258 move_backward(_RAIter __f,
260 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
261 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type* = 0);
263 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
264 class _OutputIterator>
266 move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
267 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
268 _OutputIterator __r);
270 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
271 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
272 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
273 move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
274 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
275 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
277 template <class _ValueType, class _DiffType>
278 struct __deque_block_size {
279 static const _DiffType value = sizeof(_ValueType) < 256 ? 4096 / sizeof(_ValueType) : 16;
282 template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
283 class _DiffType, _DiffType _BS =
284 #ifdef _LIBCPP_ABI_INCOMPLETE_TYPES_IN_DEQUE
285 // Keep template parameter to avoid changing all template declarations thoughout
289 __deque_block_size<_ValueType, _DiffType>::value
292 class _LIBCPP_TEMPLATE_VIS __deque_iterator
294 typedef _MapPointer __map_iterator;
296 typedef _Pointer pointer;
297 typedef _DiffType difference_type;
299 __map_iterator __m_iter_;
302 static const difference_type __block_size;
304 typedef _ValueType value_type;
305 typedef random_access_iterator_tag iterator_category;
306 typedef _Reference reference;
308 _LIBCPP_INLINE_VISIBILITY __deque_iterator() _NOEXCEPT
309 #if _LIBCPP_STD_VER > 11
310 : __m_iter_(nullptr), __ptr_(nullptr)
314 template <class _Pp, class _Rp, class _MP>
315 _LIBCPP_INLINE_VISIBILITY
316 __deque_iterator(const __deque_iterator<value_type, _Pp, _Rp, _MP, difference_type, _BS>& __it,
317 typename enable_if<is_convertible<_Pp, pointer>::value>::type* = 0) _NOEXCEPT
318 : __m_iter_(__it.__m_iter_), __ptr_(__it.__ptr_) {}
320 _LIBCPP_INLINE_VISIBILITY reference operator*() const {return *__ptr_;}
321 _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return __ptr_;}
323 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator++()
325 if (++__ptr_ - *__m_iter_ == __block_size)
333 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator++(int)
335 __deque_iterator __tmp = *this;
340 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator--()
342 if (__ptr_ == *__m_iter_)
345 __ptr_ = *__m_iter_ + __block_size;
351 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator--(int)
353 __deque_iterator __tmp = *this;
358 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator+=(difference_type __n)
362 __n += __ptr_ - *__m_iter_;
365 __m_iter_ += __n / __block_size;
366 __ptr_ = *__m_iter_ + __n % __block_size;
370 difference_type __z = __block_size - 1 - __n;
371 __m_iter_ -= __z / __block_size;
372 __ptr_ = *__m_iter_ + (__block_size - 1 - __z % __block_size);
378 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator-=(difference_type __n)
380 return *this += -__n;
383 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator+(difference_type __n) const
385 __deque_iterator __t(*this);
390 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator-(difference_type __n) const
392 __deque_iterator __t(*this);
397 _LIBCPP_INLINE_VISIBILITY
398 friend __deque_iterator operator+(difference_type __n, const __deque_iterator& __it)
401 _LIBCPP_INLINE_VISIBILITY
402 friend difference_type operator-(const __deque_iterator& __x, const __deque_iterator& __y)
405 return (__x.__m_iter_ - __y.__m_iter_) * __block_size
406 + (__x.__ptr_ - *__x.__m_iter_)
407 - (__y.__ptr_ - *__y.__m_iter_);
411 _LIBCPP_INLINE_VISIBILITY reference operator[](difference_type __n) const
412 {return *(*this + __n);}
414 _LIBCPP_INLINE_VISIBILITY friend
415 bool operator==(const __deque_iterator& __x, const __deque_iterator& __y)
416 {return __x.__ptr_ == __y.__ptr_;}
418 _LIBCPP_INLINE_VISIBILITY friend
419 bool operator!=(const __deque_iterator& __x, const __deque_iterator& __y)
420 {return !(__x == __y);}
422 _LIBCPP_INLINE_VISIBILITY friend
423 bool operator<(const __deque_iterator& __x, const __deque_iterator& __y)
424 {return __x.__m_iter_ < __y.__m_iter_ ||
425 (__x.__m_iter_ == __y.__m_iter_ && __x.__ptr_ < __y.__ptr_);}
427 _LIBCPP_INLINE_VISIBILITY friend
428 bool operator>(const __deque_iterator& __x, const __deque_iterator& __y)
431 _LIBCPP_INLINE_VISIBILITY friend
432 bool operator<=(const __deque_iterator& __x, const __deque_iterator& __y)
433 {return !(__y < __x);}
435 _LIBCPP_INLINE_VISIBILITY friend
436 bool operator>=(const __deque_iterator& __x, const __deque_iterator& __y)
437 {return !(__x < __y);}
440 _LIBCPP_INLINE_VISIBILITY __deque_iterator(__map_iterator __m, pointer __p) _NOEXCEPT
441 : __m_iter_(__m), __ptr_(__p) {}
443 template <class _Tp, class _Ap> friend class __deque_base;
444 template <class _Tp, class _Ap> friend class _LIBCPP_TEMPLATE_VIS deque;
445 template <class _Vp, class _Pp, class _Rp, class _MP, class _Dp, _Dp>
446 friend class _LIBCPP_TEMPLATE_VIS __deque_iterator;
448 template <class _RAIter,
449 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
451 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
454 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
455 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*);
457 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
458 class _OutputIterator>
461 copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
462 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
463 _OutputIterator __r);
465 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
466 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
468 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
469 copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
470 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
471 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
473 template <class _RAIter,
474 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
476 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
477 copy_backward(_RAIter __f,
479 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
480 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*);
482 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
483 class _OutputIterator>
486 copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
487 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
488 _OutputIterator __r);
490 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
491 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
493 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
494 copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
495 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
496 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
498 template <class _RAIter,
499 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
501 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
504 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
505 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*);
507 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
508 class _OutputIterator>
511 move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
512 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
513 _OutputIterator __r);
515 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
516 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
518 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
519 move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
520 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
521 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
523 template <class _RAIter,
524 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
526 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
527 move_backward(_RAIter __f,
529 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
530 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*);
532 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
533 class _OutputIterator>
536 move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
537 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
538 _OutputIterator __r);
540 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
541 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
543 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
544 move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
545 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
546 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
549 template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
550 class _DiffType, _DiffType _BlockSize>
551 const _DiffType __deque_iterator<_ValueType, _Pointer, _Reference, _MapPointer,
552 _DiffType, _BlockSize>::__block_size =
553 __deque_block_size<_ValueType, _DiffType>::value;
557 template <class _RAIter,
558 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
559 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
562 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
563 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*)
565 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
566 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
567 const difference_type __block_size = __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::__block_size;
570 pointer __rb = __r.__ptr_;
571 pointer __re = *__r.__m_iter_ + __block_size;
572 difference_type __bs = __re - __rb;
573 difference_type __n = __l - __f;
580 _VSTD::copy(__f, __m, __rb);
587 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
588 class _OutputIterator>
590 copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
591 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
594 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
595 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
596 const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
597 difference_type __n = __l - __f;
600 pointer __fb = __f.__ptr_;
601 pointer __fe = *__f.__m_iter_ + __block_size;
602 difference_type __bs = __fe - __fb;
608 __r = _VSTD::copy(__fb, __fe, __r);
615 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
616 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
617 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
618 copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
619 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
620 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
622 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
623 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
624 const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
625 difference_type __n = __l - __f;
628 pointer __fb = __f.__ptr_;
629 pointer __fe = *__f.__m_iter_ + __block_size;
630 difference_type __bs = __fe - __fb;
636 __r = _VSTD::copy(__fb, __fe, __r);
645 template <class _RAIter,
646 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
647 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
648 copy_backward(_RAIter __f,
650 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
651 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*)
653 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
654 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
657 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _VSTD::prev(__r);
658 pointer __rb = *__rp.__m_iter_;
659 pointer __re = __rp.__ptr_ + 1;
660 difference_type __bs = __re - __rb;
661 difference_type __n = __l - __f;
668 _VSTD::copy_backward(__m, __l, __re);
675 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
676 class _OutputIterator>
678 copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
679 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
682 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
683 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
684 difference_type __n = __l - __f;
688 pointer __lb = *__l.__m_iter_;
689 pointer __le = __l.__ptr_ + 1;
690 difference_type __bs = __le - __lb;
696 __r = _VSTD::copy_backward(__lb, __le, __r);
703 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
704 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
705 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
706 copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
707 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
708 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
710 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
711 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
712 difference_type __n = __l - __f;
716 pointer __lb = *__l.__m_iter_;
717 pointer __le = __l.__ptr_ + 1;
718 difference_type __bs = __le - __lb;
724 __r = _VSTD::copy_backward(__lb, __le, __r);
733 template <class _RAIter,
734 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
735 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
738 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
739 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*)
741 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
742 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
743 const difference_type __block_size = __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::__block_size;
746 pointer __rb = __r.__ptr_;
747 pointer __re = *__r.__m_iter_ + __block_size;
748 difference_type __bs = __re - __rb;
749 difference_type __n = __l - __f;
756 _VSTD::move(__f, __m, __rb);
763 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
764 class _OutputIterator>
766 move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
767 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
770 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
771 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
772 const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
773 difference_type __n = __l - __f;
776 pointer __fb = __f.__ptr_;
777 pointer __fe = *__f.__m_iter_ + __block_size;
778 difference_type __bs = __fe - __fb;
784 __r = _VSTD::move(__fb, __fe, __r);
791 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
792 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
793 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
794 move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
795 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
796 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
798 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
799 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
800 const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
801 difference_type __n = __l - __f;
804 pointer __fb = __f.__ptr_;
805 pointer __fe = *__f.__m_iter_ + __block_size;
806 difference_type __bs = __fe - __fb;
812 __r = _VSTD::move(__fb, __fe, __r);
821 template <class _RAIter,
822 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
823 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
824 move_backward(_RAIter __f,
826 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
827 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*)
829 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
830 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
833 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _VSTD::prev(__r);
834 pointer __rb = *__rp.__m_iter_;
835 pointer __re = __rp.__ptr_ + 1;
836 difference_type __bs = __re - __rb;
837 difference_type __n = __l - __f;
844 _VSTD::move_backward(__m, __l, __re);
851 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
852 class _OutputIterator>
854 move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
855 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
858 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
859 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
860 difference_type __n = __l - __f;
864 pointer __lb = *__l.__m_iter_;
865 pointer __le = __l.__ptr_ + 1;
866 difference_type __bs = __le - __lb;
872 __r = _VSTD::move_backward(__lb, __le, __r);
879 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
880 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
881 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
882 move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
883 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
884 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
886 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
887 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
888 difference_type __n = __l - __f;
892 pointer __lb = *__l.__m_iter_;
893 pointer __le = __l.__ptr_ + 1;
894 difference_type __bs = __le - __lb;
900 __r = _VSTD::move_backward(__lb, __le, __r);
908 class __deque_base_common
911 _LIBCPP_NORETURN void __throw_length_error() const;
912 _LIBCPP_NORETURN void __throw_out_of_range() const;
917 __deque_base_common<__b>::__throw_length_error() const
919 _VSTD::__throw_length_error("deque");
924 __deque_base_common<__b>::__throw_out_of_range() const
926 _VSTD::__throw_out_of_range("deque");
929 template <class _Tp, class _Allocator>
931 : protected __deque_base_common<true>
933 __deque_base(const __deque_base& __c);
934 __deque_base& operator=(const __deque_base& __c);
936 typedef _Allocator allocator_type;
937 typedef allocator_traits<allocator_type> __alloc_traits;
938 typedef typename __alloc_traits::size_type size_type;
940 typedef _Tp value_type;
941 typedef value_type& reference;
942 typedef const value_type& const_reference;
943 typedef typename __alloc_traits::difference_type difference_type;
944 typedef typename __alloc_traits::pointer pointer;
945 typedef typename __alloc_traits::const_pointer const_pointer;
947 static const difference_type __block_size;
949 typedef typename __rebind_alloc_helper<__alloc_traits, pointer>::type __pointer_allocator;
950 typedef allocator_traits<__pointer_allocator> __map_traits;
951 typedef typename __map_traits::pointer __map_pointer;
952 typedef typename __rebind_alloc_helper<__alloc_traits, const_pointer>::type __const_pointer_allocator;
953 typedef typename allocator_traits<__const_pointer_allocator>::const_pointer __map_const_pointer;
954 typedef __split_buffer<pointer, __pointer_allocator> __map;
956 typedef __deque_iterator<value_type, pointer, reference, __map_pointer,
957 difference_type> iterator;
958 typedef __deque_iterator<value_type, const_pointer, const_reference, __map_const_pointer,
959 difference_type> const_iterator;
961 struct __deque_block_range {
962 explicit __deque_block_range(pointer __b, pointer __e) _NOEXCEPT : __begin_(__b), __end_(__e) {}
963 const pointer __begin_;
964 const pointer __end_;
967 struct __deque_range {
969 const iterator __end_;
971 __deque_range(iterator __pos, iterator __e) _NOEXCEPT
972 : __pos_(__pos), __end_(__e) {}
974 explicit operator bool() const _NOEXCEPT {
975 return __pos_ != __end_;
978 __deque_range begin() const {
982 __deque_range end() const {
983 return __deque_range(__end_, __end_);
985 __deque_block_range operator*() const _NOEXCEPT {
986 if (__pos_.__m_iter_ == __end_.__m_iter_) {
987 return __deque_block_range(__pos_.__ptr_, __end_.__ptr_);
989 return __deque_block_range(__pos_.__ptr_, *__pos_.__m_iter_ + __block_size);
992 __deque_range& operator++() _NOEXCEPT {
993 if (__pos_.__m_iter_ == __end_.__m_iter_) {
997 __pos_.__ptr_ = *__pos_.__m_iter_;
1003 friend bool operator==(__deque_range const& __lhs, __deque_range const& __rhs) {
1004 return __lhs.__pos_ == __rhs.__pos_;
1006 friend bool operator!=(__deque_range const& __lhs, __deque_range const& __rhs) {
1007 return !(__lhs == __rhs);
1013 struct _ConstructTransaction {
1014 _ConstructTransaction(__deque_base* __db, __deque_block_range& __r)
1015 : __pos_(__r.__begin_), __end_(__r.__end_), __begin_(__r.__begin_), __base_(__db) {}
1018 ~_ConstructTransaction() {
1019 __base_->size() += (__pos_ - __begin_);
1023 const pointer __end_;
1025 const pointer __begin_;
1026 __deque_base * const __base_;
1032 __compressed_pair<size_type, allocator_type> __size_;
1034 iterator begin() _NOEXCEPT;
1035 const_iterator begin() const _NOEXCEPT;
1036 iterator end() _NOEXCEPT;
1037 const_iterator end() const _NOEXCEPT;
1039 _LIBCPP_INLINE_VISIBILITY size_type& size() {return __size_.first();}
1040 _LIBCPP_INLINE_VISIBILITY
1041 const size_type& size() const _NOEXCEPT {return __size_.first();}
1042 _LIBCPP_INLINE_VISIBILITY allocator_type& __alloc() {return __size_.second();}
1043 _LIBCPP_INLINE_VISIBILITY
1044 const allocator_type& __alloc() const _NOEXCEPT {return __size_.second();}
1046 _LIBCPP_INLINE_VISIBILITY
1048 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
1049 _LIBCPP_INLINE_VISIBILITY
1050 explicit __deque_base(const allocator_type& __a);
1054 #ifndef _LIBCPP_CXX03_LANG
1055 __deque_base(__deque_base&& __c)
1056 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
1057 __deque_base(__deque_base&& __c, const allocator_type& __a);
1058 #endif // _LIBCPP_CXX03_LANG
1060 void swap(__deque_base& __c)
1061 #if _LIBCPP_STD_VER >= 14
1064 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1065 __is_nothrow_swappable<allocator_type>::value);
1068 void clear() _NOEXCEPT;
1070 bool __invariants() const;
1072 _LIBCPP_INLINE_VISIBILITY
1073 void __move_assign(__deque_base& __c)
1074 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1075 is_nothrow_move_assignable<allocator_type>::value)
1077 __map_ = _VSTD::move(__c.__map_);
1078 __start_ = __c.__start_;
1079 size() = __c.size();
1080 __move_assign_alloc(__c);
1081 __c.__start_ = __c.size() = 0;
1084 _LIBCPP_INLINE_VISIBILITY
1085 void __move_assign_alloc(__deque_base& __c)
1086 _NOEXCEPT_(!__alloc_traits::propagate_on_container_move_assignment::value ||
1087 is_nothrow_move_assignable<allocator_type>::value)
1088 {__move_assign_alloc(__c, integral_constant<bool,
1089 __alloc_traits::propagate_on_container_move_assignment::value>());}
1092 _LIBCPP_INLINE_VISIBILITY
1093 void __move_assign_alloc(__deque_base& __c, true_type)
1094 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1096 __alloc() = _VSTD::move(__c.__alloc());
1099 _LIBCPP_INLINE_VISIBILITY
1100 void __move_assign_alloc(__deque_base&, false_type) _NOEXCEPT
1104 template <class _Tp, class _Allocator>
1105 const typename __deque_base<_Tp, _Allocator>::difference_type
1106 __deque_base<_Tp, _Allocator>::__block_size =
1107 __deque_block_size<value_type, difference_type>::value;
1109 template <class _Tp, class _Allocator>
1111 __deque_base<_Tp, _Allocator>::__invariants() const
1113 if (!__map_.__invariants())
1115 if (__map_.size() >= size_type(-1) / __block_size)
1117 for (typename __map::const_iterator __i = __map_.begin(), __e = __map_.end();
1119 if (*__i == nullptr)
1121 if (__map_.size() != 0)
1123 if (size() >= __map_.size() * __block_size)
1125 if (__start_ >= __map_.size() * __block_size - size())
1138 template <class _Tp, class _Allocator>
1139 typename __deque_base<_Tp, _Allocator>::iterator
1140 __deque_base<_Tp, _Allocator>::begin() _NOEXCEPT
1142 __map_pointer __mp = __map_.begin() + __start_ / __block_size;
1143 return iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
1146 template <class _Tp, class _Allocator>
1147 typename __deque_base<_Tp, _Allocator>::const_iterator
1148 __deque_base<_Tp, _Allocator>::begin() const _NOEXCEPT
1150 __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __start_ / __block_size);
1151 return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
1154 template <class _Tp, class _Allocator>
1155 typename __deque_base<_Tp, _Allocator>::iterator
1156 __deque_base<_Tp, _Allocator>::end() _NOEXCEPT
1158 size_type __p = size() + __start_;
1159 __map_pointer __mp = __map_.begin() + __p / __block_size;
1160 return iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
1163 template <class _Tp, class _Allocator>
1164 typename __deque_base<_Tp, _Allocator>::const_iterator
1165 __deque_base<_Tp, _Allocator>::end() const _NOEXCEPT
1167 size_type __p = size() + __start_;
1168 __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __p / __block_size);
1169 return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
1172 template <class _Tp, class _Allocator>
1174 __deque_base<_Tp, _Allocator>::__deque_base()
1175 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
1176 : __start_(0), __size_(0, __default_init_tag()) {}
1178 template <class _Tp, class _Allocator>
1180 __deque_base<_Tp, _Allocator>::__deque_base(const allocator_type& __a)
1181 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a) {}
1183 template <class _Tp, class _Allocator>
1184 __deque_base<_Tp, _Allocator>::~__deque_base()
1187 typename __map::iterator __i = __map_.begin();
1188 typename __map::iterator __e = __map_.end();
1189 for (; __i != __e; ++__i)
1190 __alloc_traits::deallocate(__alloc(), *__i, __block_size);
1193 #ifndef _LIBCPP_CXX03_LANG
1195 template <class _Tp, class _Allocator>
1196 __deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c)
1197 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
1198 : __map_(_VSTD::move(__c.__map_)),
1199 __start_(_VSTD::move(__c.__start_)),
1200 __size_(_VSTD::move(__c.__size_))
1206 template <class _Tp, class _Allocator>
1207 __deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c, const allocator_type& __a)
1208 : __map_(_VSTD::move(__c.__map_), __pointer_allocator(__a)),
1209 __start_(_VSTD::move(__c.__start_)),
1210 __size_(_VSTD::move(__c.size()), __a)
1212 if (__a == __c.__alloc())
1225 #endif // _LIBCPP_CXX03_LANG
1227 template <class _Tp, class _Allocator>
1229 __deque_base<_Tp, _Allocator>::swap(__deque_base& __c)
1230 #if _LIBCPP_STD_VER >= 14
1233 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1234 __is_nothrow_swappable<allocator_type>::value)
1237 __map_.swap(__c.__map_);
1238 _VSTD::swap(__start_, __c.__start_);
1239 _VSTD::swap(size(), __c.size());
1240 __swap_allocator(__alloc(), __c.__alloc());
1243 template <class _Tp, class _Allocator>
1245 __deque_base<_Tp, _Allocator>::clear() _NOEXCEPT
1247 allocator_type& __a = __alloc();
1248 for (iterator __i = begin(), __e = end(); __i != __e; ++__i)
1249 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
1251 while (__map_.size() > 2)
1253 __alloc_traits::deallocate(__a, __map_.front(), __block_size);
1256 switch (__map_.size())
1259 __start_ = __block_size / 2;
1262 __start_ = __block_size;
1267 template <class _Tp, class _Allocator /*= allocator<_Tp>*/>
1268 class _LIBCPP_TEMPLATE_VIS deque
1269 : private __deque_base<_Tp, _Allocator>
1274 typedef _Tp value_type;
1275 typedef _Allocator allocator_type;
1277 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
1278 "Allocator::value_type must be same type as value_type");
1280 typedef __deque_base<value_type, allocator_type> __base;
1282 typedef typename __base::__alloc_traits __alloc_traits;
1283 typedef typename __base::reference reference;
1284 typedef typename __base::const_reference const_reference;
1285 typedef typename __base::iterator iterator;
1286 typedef typename __base::const_iterator const_iterator;
1287 typedef typename __base::size_type size_type;
1288 typedef typename __base::difference_type difference_type;
1290 typedef typename __base::pointer pointer;
1291 typedef typename __base::const_pointer const_pointer;
1292 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
1293 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
1295 using typename __base::__deque_range;
1296 using typename __base::__deque_block_range;
1297 using typename __base::_ConstructTransaction;
1299 // construct/copy/destroy:
1300 _LIBCPP_INLINE_VISIBILITY
1302 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
1304 _LIBCPP_INLINE_VISIBILITY explicit deque(const allocator_type& __a) : __base(__a) {}
1305 explicit deque(size_type __n);
1306 #if _LIBCPP_STD_VER > 11
1307 explicit deque(size_type __n, const _Allocator& __a);
1309 deque(size_type __n, const value_type& __v);
1310 deque(size_type __n, const value_type& __v, const allocator_type& __a);
1311 template <class _InputIter>
1312 deque(_InputIter __f, _InputIter __l,
1313 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value>::type* = 0);
1314 template <class _InputIter>
1315 deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
1316 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value>::type* = 0);
1317 deque(const deque& __c);
1318 deque(const deque& __c, const allocator_type& __a);
1320 deque& operator=(const deque& __c);
1322 #ifndef _LIBCPP_CXX03_LANG
1323 deque(initializer_list<value_type> __il);
1324 deque(initializer_list<value_type> __il, const allocator_type& __a);
1326 _LIBCPP_INLINE_VISIBILITY
1327 deque& operator=(initializer_list<value_type> __il) {assign(__il); return *this;}
1329 _LIBCPP_INLINE_VISIBILITY
1330 deque(deque&& __c) _NOEXCEPT_(is_nothrow_move_constructible<__base>::value);
1331 _LIBCPP_INLINE_VISIBILITY
1332 deque(deque&& __c, const allocator_type& __a);
1333 _LIBCPP_INLINE_VISIBILITY
1334 deque& operator=(deque&& __c)
1335 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1336 is_nothrow_move_assignable<allocator_type>::value);
1338 _LIBCPP_INLINE_VISIBILITY
1339 void assign(initializer_list<value_type> __il) {assign(__il.begin(), __il.end());}
1340 #endif // _LIBCPP_CXX03_LANG
1342 template <class _InputIter>
1343 void assign(_InputIter __f, _InputIter __l,
1344 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value &&
1345 !__is_cpp17_random_access_iterator<_InputIter>::value>::type* = 0);
1346 template <class _RAIter>
1347 void assign(_RAIter __f, _RAIter __l,
1348 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type* = 0);
1349 void assign(size_type __n, const value_type& __v);
1351 _LIBCPP_INLINE_VISIBILITY
1352 allocator_type get_allocator() const _NOEXCEPT;
1356 _LIBCPP_INLINE_VISIBILITY
1357 iterator begin() _NOEXCEPT {return __base::begin();}
1358 _LIBCPP_INLINE_VISIBILITY
1359 const_iterator begin() const _NOEXCEPT {return __base::begin();}
1360 _LIBCPP_INLINE_VISIBILITY
1361 iterator end() _NOEXCEPT {return __base::end();}
1362 _LIBCPP_INLINE_VISIBILITY
1363 const_iterator end() const _NOEXCEPT {return __base::end();}
1365 _LIBCPP_INLINE_VISIBILITY
1366 reverse_iterator rbegin() _NOEXCEPT
1367 {return reverse_iterator(__base::end());}
1368 _LIBCPP_INLINE_VISIBILITY
1369 const_reverse_iterator rbegin() const _NOEXCEPT
1370 {return const_reverse_iterator(__base::end());}
1371 _LIBCPP_INLINE_VISIBILITY
1372 reverse_iterator rend() _NOEXCEPT
1373 {return reverse_iterator(__base::begin());}
1374 _LIBCPP_INLINE_VISIBILITY
1375 const_reverse_iterator rend() const _NOEXCEPT
1376 {return const_reverse_iterator(__base::begin());}
1378 _LIBCPP_INLINE_VISIBILITY
1379 const_iterator cbegin() const _NOEXCEPT
1380 {return __base::begin();}
1381 _LIBCPP_INLINE_VISIBILITY
1382 const_iterator cend() const _NOEXCEPT
1383 {return __base::end();}
1384 _LIBCPP_INLINE_VISIBILITY
1385 const_reverse_iterator crbegin() const _NOEXCEPT
1386 {return const_reverse_iterator(__base::end());}
1387 _LIBCPP_INLINE_VISIBILITY
1388 const_reverse_iterator crend() const _NOEXCEPT
1389 {return const_reverse_iterator(__base::begin());}
1392 _LIBCPP_INLINE_VISIBILITY
1393 size_type size() const _NOEXCEPT {return __base::size();}
1394 _LIBCPP_INLINE_VISIBILITY
1395 size_type max_size() const _NOEXCEPT
1396 {return std::min<size_type>(
1397 __alloc_traits::max_size(__base::__alloc()),
1398 numeric_limits<difference_type>::max());}
1399 void resize(size_type __n);
1400 void resize(size_type __n, const value_type& __v);
1401 void shrink_to_fit() _NOEXCEPT;
1402 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
1403 bool empty() const _NOEXCEPT {return __base::size() == 0;}
1406 _LIBCPP_INLINE_VISIBILITY
1407 reference operator[](size_type __i) _NOEXCEPT;
1408 _LIBCPP_INLINE_VISIBILITY
1409 const_reference operator[](size_type __i) const _NOEXCEPT;
1410 _LIBCPP_INLINE_VISIBILITY
1411 reference at(size_type __i);
1412 _LIBCPP_INLINE_VISIBILITY
1413 const_reference at(size_type __i) const;
1414 _LIBCPP_INLINE_VISIBILITY
1415 reference front() _NOEXCEPT;
1416 _LIBCPP_INLINE_VISIBILITY
1417 const_reference front() const _NOEXCEPT;
1418 _LIBCPP_INLINE_VISIBILITY
1419 reference back() _NOEXCEPT;
1420 _LIBCPP_INLINE_VISIBILITY
1421 const_reference back() const _NOEXCEPT;
1423 // 23.2.2.3 modifiers:
1424 void push_front(const value_type& __v);
1425 void push_back(const value_type& __v);
1426 #ifndef _LIBCPP_CXX03_LANG
1427 #if _LIBCPP_STD_VER > 14
1428 template <class... _Args> reference emplace_front(_Args&&... __args);
1429 template <class... _Args> reference emplace_back (_Args&&... __args);
1431 template <class... _Args> void emplace_front(_Args&&... __args);
1432 template <class... _Args> void emplace_back (_Args&&... __args);
1434 template <class... _Args> iterator emplace(const_iterator __p, _Args&&... __args);
1436 void push_front(value_type&& __v);
1437 void push_back(value_type&& __v);
1438 iterator insert(const_iterator __p, value_type&& __v);
1440 _LIBCPP_INLINE_VISIBILITY
1441 iterator insert(const_iterator __p, initializer_list<value_type> __il)
1442 {return insert(__p, __il.begin(), __il.end());}
1443 #endif // _LIBCPP_CXX03_LANG
1444 iterator insert(const_iterator __p, const value_type& __v);
1445 iterator insert(const_iterator __p, size_type __n, const value_type& __v);
1446 template <class _InputIter>
1447 iterator insert(const_iterator __p, _InputIter __f, _InputIter __l,
1448 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value
1449 &&!__is_cpp17_forward_iterator<_InputIter>::value>::type* = 0);
1450 template <class _ForwardIterator>
1451 iterator insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l,
1452 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value
1453 &&!__is_cpp17_bidirectional_iterator<_ForwardIterator>::value>::type* = 0);
1454 template <class _BiIter>
1455 iterator insert(const_iterator __p, _BiIter __f, _BiIter __l,
1456 typename enable_if<__is_cpp17_bidirectional_iterator<_BiIter>::value>::type* = 0);
1460 iterator erase(const_iterator __p);
1461 iterator erase(const_iterator __f, const_iterator __l);
1463 _LIBCPP_INLINE_VISIBILITY
1464 void swap(deque& __c)
1465 #if _LIBCPP_STD_VER >= 14
1468 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1469 __is_nothrow_swappable<allocator_type>::value);
1471 _LIBCPP_INLINE_VISIBILITY
1472 void clear() _NOEXCEPT;
1474 _LIBCPP_INLINE_VISIBILITY
1475 bool __invariants() const {return __base::__invariants();}
1477 typedef typename __base::__map_const_pointer __map_const_pointer;
1479 _LIBCPP_INLINE_VISIBILITY
1480 static size_type __recommend_blocks(size_type __n)
1482 return __n / __base::__block_size + (__n % __base::__block_size != 0);
1484 _LIBCPP_INLINE_VISIBILITY
1485 size_type __capacity() const
1487 return __base::__map_.size() == 0 ? 0 : __base::__map_.size() * __base::__block_size - 1;
1489 _LIBCPP_INLINE_VISIBILITY
1490 size_type __block_count() const
1492 return __base::__map_.size();
1495 _LIBCPP_INLINE_VISIBILITY
1496 size_type __front_spare() const
1498 return __base::__start_;
1500 _LIBCPP_INLINE_VISIBILITY
1501 size_type __front_spare_blocks() const {
1502 return __front_spare() / __base::__block_size;
1504 _LIBCPP_INLINE_VISIBILITY
1505 size_type __back_spare() const
1507 return __capacity() - (__base::__start_ + __base::size());
1509 _LIBCPP_INLINE_VISIBILITY
1510 size_type __back_spare_blocks() const {
1511 return __back_spare() / __base::__block_size;
1515 _LIBCPP_INLINE_VISIBILITY
1516 bool __maybe_remove_front_spare(bool __keep_one = true) {
1517 if (__front_spare_blocks() >= 2 || (!__keep_one && __front_spare_blocks())) {
1518 __alloc_traits::deallocate(__base::__alloc(), __base::__map_.front(),
1519 __base::__block_size);
1520 __base::__map_.pop_front();
1521 __base::__start_ -= __base::__block_size;
1527 _LIBCPP_INLINE_VISIBILITY
1528 bool __maybe_remove_back_spare(bool __keep_one = true) {
1529 if (__back_spare_blocks() >= 2 || (!__keep_one && __back_spare_blocks())) {
1530 __alloc_traits::deallocate(__base::__alloc(), __base::__map_.back(),
1531 __base::__block_size);
1532 __base::__map_.pop_back();
1538 template <class _InpIter>
1539 void __append(_InpIter __f, _InpIter __l,
1540 typename enable_if<__is_cpp17_input_iterator<_InpIter>::value &&
1541 !__is_cpp17_forward_iterator<_InpIter>::value>::type* = 0);
1542 template <class _ForIter>
1543 void __append(_ForIter __f, _ForIter __l,
1544 typename enable_if<__is_cpp17_forward_iterator<_ForIter>::value>::type* = 0);
1545 void __append(size_type __n);
1546 void __append(size_type __n, const value_type& __v);
1547 void __erase_to_end(const_iterator __f);
1548 void __add_front_capacity();
1549 void __add_front_capacity(size_type __n);
1550 void __add_back_capacity();
1551 void __add_back_capacity(size_type __n);
1552 iterator __move_and_check(iterator __f, iterator __l, iterator __r,
1553 const_pointer& __vt);
1554 iterator __move_backward_and_check(iterator __f, iterator __l, iterator __r,
1555 const_pointer& __vt);
1556 void __move_construct_and_check(iterator __f, iterator __l,
1557 iterator __r, const_pointer& __vt);
1558 void __move_construct_backward_and_check(iterator __f, iterator __l,
1559 iterator __r, const_pointer& __vt);
1561 _LIBCPP_INLINE_VISIBILITY
1562 void __copy_assign_alloc(const deque& __c)
1563 {__copy_assign_alloc(__c, integral_constant<bool,
1564 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1566 _LIBCPP_INLINE_VISIBILITY
1567 void __copy_assign_alloc(const deque& __c, true_type)
1569 if (__base::__alloc() != __c.__alloc())
1574 __base::__alloc() = __c.__alloc();
1575 __base::__map_.__alloc() = __c.__map_.__alloc();
1578 _LIBCPP_INLINE_VISIBILITY
1579 void __copy_assign_alloc(const deque&, false_type)
1582 void __move_assign(deque& __c, true_type)
1583 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
1584 void __move_assign(deque& __c, false_type);
1587 #ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
1588 template<class _InputIterator,
1589 class _Alloc = typename std::allocator<typename iterator_traits<_InputIterator>::value_type>,
1590 class = typename enable_if<__is_allocator<_Alloc>::value, void>::type
1592 deque(_InputIterator, _InputIterator)
1593 -> deque<typename iterator_traits<_InputIterator>::value_type, _Alloc>;
1595 template<class _InputIterator,
1597 class = typename enable_if<__is_allocator<_Alloc>::value, void>::type
1599 deque(_InputIterator, _InputIterator, _Alloc)
1600 -> deque<typename iterator_traits<_InputIterator>::value_type, _Alloc>;
1604 template <class _Tp, class _Allocator>
1605 deque<_Tp, _Allocator>::deque(size_type __n)
1611 #if _LIBCPP_STD_VER > 11
1612 template <class _Tp, class _Allocator>
1613 deque<_Tp, _Allocator>::deque(size_type __n, const _Allocator& __a)
1621 template <class _Tp, class _Allocator>
1622 deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v)
1628 template <class _Tp, class _Allocator>
1629 deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v, const allocator_type& __a)
1636 template <class _Tp, class _Allocator>
1637 template <class _InputIter>
1638 deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l,
1639 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value>::type*)
1644 template <class _Tp, class _Allocator>
1645 template <class _InputIter>
1646 deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
1647 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value>::type*)
1653 template <class _Tp, class _Allocator>
1654 deque<_Tp, _Allocator>::deque(const deque& __c)
1655 : __base(__alloc_traits::select_on_container_copy_construction(__c.__alloc()))
1657 __append(__c.begin(), __c.end());
1660 template <class _Tp, class _Allocator>
1661 deque<_Tp, _Allocator>::deque(const deque& __c, const allocator_type& __a)
1664 __append(__c.begin(), __c.end());
1667 template <class _Tp, class _Allocator>
1668 deque<_Tp, _Allocator>&
1669 deque<_Tp, _Allocator>::operator=(const deque& __c)
1673 __copy_assign_alloc(__c);
1674 assign(__c.begin(), __c.end());
1679 #ifndef _LIBCPP_CXX03_LANG
1681 template <class _Tp, class _Allocator>
1682 deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il)
1684 __append(__il.begin(), __il.end());
1687 template <class _Tp, class _Allocator>
1688 deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il, const allocator_type& __a)
1691 __append(__il.begin(), __il.end());
1694 template <class _Tp, class _Allocator>
1696 deque<_Tp, _Allocator>::deque(deque&& __c)
1697 _NOEXCEPT_(is_nothrow_move_constructible<__base>::value)
1698 : __base(_VSTD::move(__c))
1702 template <class _Tp, class _Allocator>
1704 deque<_Tp, _Allocator>::deque(deque&& __c, const allocator_type& __a)
1705 : __base(_VSTD::move(__c), __a)
1707 if (__a != __c.__alloc())
1709 typedef move_iterator<iterator> _Ip;
1710 assign(_Ip(__c.begin()), _Ip(__c.end()));
1714 template <class _Tp, class _Allocator>
1716 deque<_Tp, _Allocator>&
1717 deque<_Tp, _Allocator>::operator=(deque&& __c)
1718 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1719 is_nothrow_move_assignable<allocator_type>::value)
1721 __move_assign(__c, integral_constant<bool,
1722 __alloc_traits::propagate_on_container_move_assignment::value>());
1726 template <class _Tp, class _Allocator>
1728 deque<_Tp, _Allocator>::__move_assign(deque& __c, false_type)
1730 if (__base::__alloc() != __c.__alloc())
1732 typedef move_iterator<iterator> _Ip;
1733 assign(_Ip(__c.begin()), _Ip(__c.end()));
1736 __move_assign(__c, true_type());
1739 template <class _Tp, class _Allocator>
1741 deque<_Tp, _Allocator>::__move_assign(deque& __c, true_type)
1742 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1746 __base::__move_assign(__c);
1749 #endif // _LIBCPP_CXX03_LANG
1751 template <class _Tp, class _Allocator>
1752 template <class _InputIter>
1754 deque<_Tp, _Allocator>::assign(_InputIter __f, _InputIter __l,
1755 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value &&
1756 !__is_cpp17_random_access_iterator<_InputIter>::value>::type*)
1758 iterator __i = __base::begin();
1759 iterator __e = __base::end();
1760 for (; __f != __l && __i != __e; ++__f, (void) ++__i)
1765 __erase_to_end(__i);
1768 template <class _Tp, class _Allocator>
1769 template <class _RAIter>
1771 deque<_Tp, _Allocator>::assign(_RAIter __f, _RAIter __l,
1772 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*)
1774 if (static_cast<size_type>(__l - __f) > __base::size())
1776 _RAIter __m = __f + __base::size();
1777 _VSTD::copy(__f, __m, __base::begin());
1781 __erase_to_end(_VSTD::copy(__f, __l, __base::begin()));
1784 template <class _Tp, class _Allocator>
1786 deque<_Tp, _Allocator>::assign(size_type __n, const value_type& __v)
1788 if (__n > __base::size())
1790 _VSTD::fill_n(__base::begin(), __base::size(), __v);
1791 __n -= __base::size();
1795 __erase_to_end(_VSTD::fill_n(__base::begin(), __n, __v));
1798 template <class _Tp, class _Allocator>
1801 deque<_Tp, _Allocator>::get_allocator() const _NOEXCEPT
1803 return __base::__alloc();
1806 template <class _Tp, class _Allocator>
1808 deque<_Tp, _Allocator>::resize(size_type __n)
1810 if (__n > __base::size())
1811 __append(__n - __base::size());
1812 else if (__n < __base::size())
1813 __erase_to_end(__base::begin() + __n);
1816 template <class _Tp, class _Allocator>
1818 deque<_Tp, _Allocator>::resize(size_type __n, const value_type& __v)
1820 if (__n > __base::size())
1821 __append(__n - __base::size(), __v);
1822 else if (__n < __base::size())
1823 __erase_to_end(__base::begin() + __n);
1826 template <class _Tp, class _Allocator>
1828 deque<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
1830 allocator_type& __a = __base::__alloc();
1833 while (__base::__map_.size() > 0)
1835 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
1836 __base::__map_.pop_back();
1838 __base::__start_ = 0;
1842 __maybe_remove_front_spare(/*__keep_one=*/false);
1843 __maybe_remove_back_spare(/*__keep_one=*/false);
1845 __base::__map_.shrink_to_fit();
1848 template <class _Tp, class _Allocator>
1850 typename deque<_Tp, _Allocator>::reference
1851 deque<_Tp, _Allocator>::operator[](size_type __i) _NOEXCEPT
1853 size_type __p = __base::__start_ + __i;
1854 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1857 template <class _Tp, class _Allocator>
1859 typename deque<_Tp, _Allocator>::const_reference
1860 deque<_Tp, _Allocator>::operator[](size_type __i) const _NOEXCEPT
1862 size_type __p = __base::__start_ + __i;
1863 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1866 template <class _Tp, class _Allocator>
1868 typename deque<_Tp, _Allocator>::reference
1869 deque<_Tp, _Allocator>::at(size_type __i)
1871 if (__i >= __base::size())
1872 __base::__throw_out_of_range();
1873 size_type __p = __base::__start_ + __i;
1874 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1877 template <class _Tp, class _Allocator>
1879 typename deque<_Tp, _Allocator>::const_reference
1880 deque<_Tp, _Allocator>::at(size_type __i) const
1882 if (__i >= __base::size())
1883 __base::__throw_out_of_range();
1884 size_type __p = __base::__start_ + __i;
1885 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1888 template <class _Tp, class _Allocator>
1890 typename deque<_Tp, _Allocator>::reference
1891 deque<_Tp, _Allocator>::front() _NOEXCEPT
1893 return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1894 + __base::__start_ % __base::__block_size);
1897 template <class _Tp, class _Allocator>
1899 typename deque<_Tp, _Allocator>::const_reference
1900 deque<_Tp, _Allocator>::front() const _NOEXCEPT
1902 return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1903 + __base::__start_ % __base::__block_size);
1906 template <class _Tp, class _Allocator>
1908 typename deque<_Tp, _Allocator>::reference
1909 deque<_Tp, _Allocator>::back() _NOEXCEPT
1911 size_type __p = __base::size() + __base::__start_ - 1;
1912 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1915 template <class _Tp, class _Allocator>
1917 typename deque<_Tp, _Allocator>::const_reference
1918 deque<_Tp, _Allocator>::back() const _NOEXCEPT
1920 size_type __p = __base::size() + __base::__start_ - 1;
1921 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1924 template <class _Tp, class _Allocator>
1926 deque<_Tp, _Allocator>::push_back(const value_type& __v)
1928 allocator_type& __a = __base::__alloc();
1929 if (__back_spare() == 0)
1930 __add_back_capacity();
1931 // __back_spare() >= 1
1932 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v);
1936 template <class _Tp, class _Allocator>
1938 deque<_Tp, _Allocator>::push_front(const value_type& __v)
1940 allocator_type& __a = __base::__alloc();
1941 if (__front_spare() == 0)
1942 __add_front_capacity();
1943 // __front_spare() >= 1
1944 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v);
1949 #ifndef _LIBCPP_CXX03_LANG
1950 template <class _Tp, class _Allocator>
1952 deque<_Tp, _Allocator>::push_back(value_type&& __v)
1954 allocator_type& __a = __base::__alloc();
1955 if (__back_spare() == 0)
1956 __add_back_capacity();
1957 // __back_spare() >= 1
1958 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v));
1962 template <class _Tp, class _Allocator>
1963 template <class... _Args>
1964 #if _LIBCPP_STD_VER > 14
1965 typename deque<_Tp, _Allocator>::reference
1969 deque<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1971 allocator_type& __a = __base::__alloc();
1972 if (__back_spare() == 0)
1973 __add_back_capacity();
1974 // __back_spare() >= 1
1975 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()),
1976 _VSTD::forward<_Args>(__args)...);
1978 #if _LIBCPP_STD_VER > 14
1979 return *--__base::end();
1983 template <class _Tp, class _Allocator>
1985 deque<_Tp, _Allocator>::push_front(value_type&& __v)
1987 allocator_type& __a = __base::__alloc();
1988 if (__front_spare() == 0)
1989 __add_front_capacity();
1990 // __front_spare() >= 1
1991 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v));
1997 template <class _Tp, class _Allocator>
1998 template <class... _Args>
1999 #if _LIBCPP_STD_VER > 14
2000 typename deque<_Tp, _Allocator>::reference
2004 deque<_Tp, _Allocator>::emplace_front(_Args&&... __args)
2006 allocator_type& __a = __base::__alloc();
2007 if (__front_spare() == 0)
2008 __add_front_capacity();
2009 // __front_spare() >= 1
2010 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...);
2013 #if _LIBCPP_STD_VER > 14
2014 return *__base::begin();
2018 template <class _Tp, class _Allocator>
2019 typename deque<_Tp, _Allocator>::iterator
2020 deque<_Tp, _Allocator>::insert(const_iterator __p, value_type&& __v)
2022 size_type __pos = __p - __base::begin();
2023 size_type __to_end = __base::size() - __pos;
2024 allocator_type& __a = __base::__alloc();
2025 if (__pos < __to_end)
2026 { // insert by shifting things backward
2027 if (__front_spare() == 0)
2028 __add_front_capacity();
2029 // __front_spare() >= 1
2032 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v));
2038 iterator __b = __base::begin();
2039 iterator __bm1 = _VSTD::prev(__b);
2040 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
2044 __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
2045 *__b = _VSTD::move(__v);
2049 { // insert by shifting things forward
2050 if (__back_spare() == 0)
2051 __add_back_capacity();
2052 // __back_capacity >= 1
2053 size_type __de = __base::size() - __pos;
2056 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v));
2061 iterator __e = __base::end();
2062 iterator __em1 = _VSTD::prev(__e);
2063 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
2066 __e = _VSTD::move_backward(__e - __de, __em1, __e);
2067 *--__e = _VSTD::move(__v);
2070 return __base::begin() + __pos;
2073 template <class _Tp, class _Allocator>
2074 template <class... _Args>
2075 typename deque<_Tp, _Allocator>::iterator
2076 deque<_Tp, _Allocator>::emplace(const_iterator __p, _Args&&... __args)
2078 size_type __pos = __p - __base::begin();
2079 size_type __to_end = __base::size() - __pos;
2080 allocator_type& __a = __base::__alloc();
2081 if (__pos < __to_end)
2082 { // insert by shifting things backward
2083 if (__front_spare() == 0)
2084 __add_front_capacity();
2085 // __front_spare() >= 1
2088 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...);
2094 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
2095 iterator __b = __base::begin();
2096 iterator __bm1 = _VSTD::prev(__b);
2097 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
2101 __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
2102 *__b = _VSTD::move(__tmp.get());
2106 { // insert by shifting things forward
2107 if (__back_spare() == 0)
2108 __add_back_capacity();
2109 // __back_capacity >= 1
2110 size_type __de = __base::size() - __pos;
2113 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::forward<_Args>(__args)...);
2118 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
2119 iterator __e = __base::end();
2120 iterator __em1 = _VSTD::prev(__e);
2121 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
2124 __e = _VSTD::move_backward(__e - __de, __em1, __e);
2125 *--__e = _VSTD::move(__tmp.get());
2128 return __base::begin() + __pos;
2131 #endif // _LIBCPP_CXX03_LANG
2134 template <class _Tp, class _Allocator>
2135 typename deque<_Tp, _Allocator>::iterator
2136 deque<_Tp, _Allocator>::insert(const_iterator __p, const value_type& __v)
2138 size_type __pos = __p - __base::begin();
2139 size_type __to_end = __base::size() - __pos;
2140 allocator_type& __a = __base::__alloc();
2141 if (__pos < __to_end)
2142 { // insert by shifting things backward
2143 if (__front_spare() == 0)
2144 __add_front_capacity();
2145 // __front_spare() >= 1
2148 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v);
2154 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2155 iterator __b = __base::begin();
2156 iterator __bm1 = _VSTD::prev(__b);
2157 if (__vt == pointer_traits<const_pointer>::pointer_to(*__b))
2158 __vt = pointer_traits<const_pointer>::pointer_to(*__bm1);
2159 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
2163 __b = __move_and_check(_VSTD::next(__b), __b + __pos, __b, __vt);
2168 { // insert by shifting things forward
2169 if (__back_spare() == 0)
2170 __add_back_capacity();
2171 // __back_capacity >= 1
2172 size_type __de = __base::size() - __pos;
2175 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v);
2180 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2181 iterator __e = __base::end();
2182 iterator __em1 = _VSTD::prev(__e);
2183 if (__vt == pointer_traits<const_pointer>::pointer_to(*__em1))
2184 __vt = pointer_traits<const_pointer>::pointer_to(*__e);
2185 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
2188 __e = __move_backward_and_check(__e - __de, __em1, __e, __vt);
2192 return __base::begin() + __pos;
2195 template <class _Tp, class _Allocator>
2196 typename deque<_Tp, _Allocator>::iterator
2197 deque<_Tp, _Allocator>::insert(const_iterator __p, size_type __n, const value_type& __v)
2199 size_type __pos = __p - __base::begin();
2200 size_type __to_end = __base::size() - __pos;
2201 allocator_type& __a = __base::__alloc();
2202 if (__pos < __to_end)
2203 { // insert by shifting things backward
2204 if (__n > __front_spare())
2205 __add_front_capacity(__n - __front_spare());
2206 // __n <= __front_spare()
2207 iterator __old_begin = __base::begin();
2208 iterator __i = __old_begin;
2211 for (size_type __m = __n - __pos; __m; --__m, --__base::__start_, ++__base::size())
2212 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), __v);
2217 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2218 iterator __obn = __old_begin + __n;
2219 __move_construct_backward_and_check(__old_begin, __obn, __i, __vt);
2221 __old_begin = __move_and_check(__obn, __old_begin + __pos, __old_begin, __vt);
2222 _VSTD::fill_n(__old_begin, __n, *__vt);
2226 { // insert by shifting things forward
2227 size_type __back_capacity = __back_spare();
2228 if (__n > __back_capacity)
2229 __add_back_capacity(__n - __back_capacity);
2230 // __n <= __back_capacity
2231 iterator __old_end = __base::end();
2232 iterator __i = __old_end;
2233 size_type __de = __base::size() - __pos;
2236 for (size_type __m = __n - __de; __m; --__m, ++__i, ++__base::size())
2237 __alloc_traits::construct(__a, _VSTD::addressof(*__i), __v);
2242 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2243 iterator __oen = __old_end - __n;
2244 __move_construct_and_check(__oen, __old_end, __i, __vt);
2246 __old_end = __move_backward_and_check(__old_end - __de, __oen, __old_end, __vt);
2247 _VSTD::fill_n(__old_end - __n, __n, *__vt);
2250 return __base::begin() + __pos;
2253 template <class _Tp, class _Allocator>
2254 template <class _InputIter>
2255 typename deque<_Tp, _Allocator>::iterator
2256 deque<_Tp, _Allocator>::insert(const_iterator __p, _InputIter __f, _InputIter __l,
2257 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value
2258 &&!__is_cpp17_forward_iterator<_InputIter>::value>::type*)
2260 __split_buffer<value_type, allocator_type&> __buf(__base::__alloc());
2261 __buf.__construct_at_end(__f, __l);
2262 typedef typename __split_buffer<value_type, allocator_type&>::iterator __bi;
2263 return insert(__p, move_iterator<__bi>(__buf.begin()), move_iterator<__bi>(__buf.end()));
2266 template <class _Tp, class _Allocator>
2267 template <class _ForwardIterator>
2268 typename deque<_Tp, _Allocator>::iterator
2269 deque<_Tp, _Allocator>::insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l,
2270 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value
2271 &&!__is_cpp17_bidirectional_iterator<_ForwardIterator>::value>::type*)
2273 size_type __n = _VSTD::distance(__f, __l);
2274 __split_buffer<value_type, allocator_type&> __buf(__n, 0, __base::__alloc());
2275 __buf.__construct_at_end(__f, __l);
2276 typedef typename __split_buffer<value_type, allocator_type&>::iterator __fwd;
2277 return insert(__p, move_iterator<__fwd>(__buf.begin()), move_iterator<__fwd>(__buf.end()));
2280 template <class _Tp, class _Allocator>
2281 template <class _BiIter>
2282 typename deque<_Tp, _Allocator>::iterator
2283 deque<_Tp, _Allocator>::insert(const_iterator __p, _BiIter __f, _BiIter __l,
2284 typename enable_if<__is_cpp17_bidirectional_iterator<_BiIter>::value>::type*)
2286 size_type __n = _VSTD::distance(__f, __l);
2287 size_type __pos = __p - __base::begin();
2288 size_type __to_end = __base::size() - __pos;
2289 allocator_type& __a = __base::__alloc();
2290 if (__pos < __to_end)
2291 { // insert by shifting things backward
2292 if (__n > __front_spare())
2293 __add_front_capacity(__n - __front_spare());
2294 // __n <= __front_spare()
2295 iterator __old_begin = __base::begin();
2296 iterator __i = __old_begin;
2300 __m = __pos < __n / 2 ? _VSTD::prev(__l, __pos) : _VSTD::next(__f, __n - __pos);
2301 for (_BiIter __j = __m; __j != __f; --__base::__start_, ++__base::size())
2302 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), *--__j);
2307 iterator __obn = __old_begin + __n;
2308 for (iterator __j = __obn; __j != __old_begin;)
2310 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), _VSTD::move(*--__j));
2315 __old_begin = _VSTD::move(__obn, __old_begin + __pos, __old_begin);
2316 _VSTD::copy(__m, __l, __old_begin);
2320 { // insert by shifting things forward
2321 size_type __back_capacity = __back_spare();
2322 if (__n > __back_capacity)
2323 __add_back_capacity(__n - __back_capacity);
2324 // __n <= __back_capacity
2325 iterator __old_end = __base::end();
2326 iterator __i = __old_end;
2328 size_type __de = __base::size() - __pos;
2331 __m = __de < __n / 2 ? _VSTD::next(__f, __de) : _VSTD::prev(__l, __n - __de);
2332 for (_BiIter __j = __m; __j != __l; ++__i, (void) ++__j, ++__base::size())
2333 __alloc_traits::construct(__a, _VSTD::addressof(*__i), *__j);
2338 iterator __oen = __old_end - __n;
2339 for (iterator __j = __oen; __j != __old_end; ++__i, ++__j, ++__base::size())
2340 __alloc_traits::construct(__a, _VSTD::addressof(*__i), _VSTD::move(*__j));
2342 __old_end = _VSTD::move_backward(__old_end - __de, __oen, __old_end);
2343 _VSTD::copy_backward(__f, __m, __old_end);
2346 return __base::begin() + __pos;
2349 template <class _Tp, class _Allocator>
2350 template <class _InpIter>
2352 deque<_Tp, _Allocator>::__append(_InpIter __f, _InpIter __l,
2353 typename enable_if<__is_cpp17_input_iterator<_InpIter>::value &&
2354 !__is_cpp17_forward_iterator<_InpIter>::value>::type*)
2356 for (; __f != __l; ++__f)
2357 #ifdef _LIBCPP_CXX03_LANG
2364 template <class _Tp, class _Allocator>
2365 template <class _ForIter>
2367 deque<_Tp, _Allocator>::__append(_ForIter __f, _ForIter __l,
2368 typename enable_if<__is_cpp17_forward_iterator<_ForIter>::value>::type*)
2370 size_type __n = _VSTD::distance(__f, __l);
2371 allocator_type& __a = __base::__alloc();
2372 size_type __back_capacity = __back_spare();
2373 if (__n > __back_capacity)
2374 __add_back_capacity(__n - __back_capacity);
2375 // __n <= __back_capacity
2376 for (__deque_block_range __br : __deque_range(__base::end(), __base::end() + __n)) {
2377 _ConstructTransaction __tx(this, __br);
2378 for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, (void)++__f) {
2379 __alloc_traits::construct(__a, std::__to_address(__tx.__pos_), *__f);
2384 template <class _Tp, class _Allocator>
2386 deque<_Tp, _Allocator>::__append(size_type __n)
2388 allocator_type& __a = __base::__alloc();
2389 size_type __back_capacity = __back_spare();
2390 if (__n > __back_capacity)
2391 __add_back_capacity(__n - __back_capacity);
2392 // __n <= __back_capacity
2393 for (__deque_block_range __br : __deque_range(__base::end(), __base::end() + __n)) {
2394 _ConstructTransaction __tx(this, __br);
2395 for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
2396 __alloc_traits::construct(__a, std::__to_address(__tx.__pos_));
2401 template <class _Tp, class _Allocator>
2403 deque<_Tp, _Allocator>::__append(size_type __n, const value_type& __v)
2405 allocator_type& __a = __base::__alloc();
2406 size_type __back_capacity = __back_spare();
2407 if (__n > __back_capacity)
2408 __add_back_capacity(__n - __back_capacity);
2409 // __n <= __back_capacity
2410 for (__deque_block_range __br : __deque_range(__base::end(), __base::end() + __n)) {
2411 _ConstructTransaction __tx(this, __br);
2412 for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
2413 __alloc_traits::construct(__a, std::__to_address(__tx.__pos_), __v);
2419 // Create front capacity for one block of elements.
2420 // Strong guarantee. Either do it or don't touch anything.
2421 template <class _Tp, class _Allocator>
2423 deque<_Tp, _Allocator>::__add_front_capacity()
2425 allocator_type& __a = __base::__alloc();
2426 if (__back_spare() >= __base::__block_size)
2428 __base::__start_ += __base::__block_size;
2429 pointer __pt = __base::__map_.back();
2430 __base::__map_.pop_back();
2431 __base::__map_.push_front(__pt);
2433 // Else if __base::__map_.size() < __base::__map_.capacity() then we need to allocate 1 buffer
2434 else if (__base::__map_.size() < __base::__map_.capacity())
2435 { // we can put the new buffer into the map, but don't shift things around
2436 // until all buffers are allocated. If we throw, we don't need to fix
2437 // anything up (any added buffers are undetectible)
2438 if (__base::__map_.__front_spare() > 0)
2439 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2442 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2443 // Done allocating, reorder capacity
2444 pointer __pt = __base::__map_.back();
2445 __base::__map_.pop_back();
2446 __base::__map_.push_front(__pt);
2448 __base::__start_ = __base::__map_.size() == 1 ?
2449 __base::__block_size / 2 :
2450 __base::__start_ + __base::__block_size;
2452 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2455 __split_buffer<pointer, typename __base::__pointer_allocator&>
2456 __buf(max<size_type>(2 * __base::__map_.capacity(), 1),
2457 0, __base::__map_.__alloc());
2459 typedef __allocator_destructor<_Allocator> _Dp;
2460 unique_ptr<pointer, _Dp> __hold(
2461 __alloc_traits::allocate(__a, __base::__block_size),
2462 _Dp(__a, __base::__block_size));
2463 __buf.push_back(__hold.get());
2466 for (typename __base::__map_pointer __i = __base::__map_.begin();
2467 __i != __base::__map_.end(); ++__i)
2468 __buf.push_back(*__i);
2469 _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());
2473 __base::__start_ = __base::__map_.size() == 1 ?
2474 __base::__block_size / 2 :
2475 __base::__start_ + __base::__block_size;
2479 // Create front capacity for __n elements.
2480 // Strong guarantee. Either do it or don't touch anything.
2481 template <class _Tp, class _Allocator>
2483 deque<_Tp, _Allocator>::__add_front_capacity(size_type __n)
2485 allocator_type& __a = __base::__alloc();
2486 size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2487 // Number of unused blocks at back:
2488 size_type __back_capacity = __back_spare() / __base::__block_size;
2489 __back_capacity = _VSTD::min(__back_capacity, __nb); // don't take more than you need
2490 __nb -= __back_capacity; // number of blocks need to allocate
2491 // If __nb == 0, then we have sufficient capacity.
2494 __base::__start_ += __base::__block_size * __back_capacity;
2495 for (; __back_capacity > 0; --__back_capacity)
2497 pointer __pt = __base::__map_.back();
2498 __base::__map_.pop_back();
2499 __base::__map_.push_front(__pt);
2502 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2503 else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2504 { // we can put the new buffers into the map, but don't shift things around
2505 // until all buffers are allocated. If we throw, we don't need to fix
2506 // anything up (any added buffers are undetectible)
2507 for (; __nb > 0; --__nb, __base::__start_ += __base::__block_size - (__base::__map_.size() == 1))
2509 if (__base::__map_.__front_spare() == 0)
2511 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2513 for (; __nb > 0; --__nb, ++__back_capacity)
2514 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2515 // Done allocating, reorder capacity
2516 __base::__start_ += __back_capacity * __base::__block_size;
2517 for (; __back_capacity > 0; --__back_capacity)
2519 pointer __pt = __base::__map_.back();
2520 __base::__map_.pop_back();
2521 __base::__map_.push_front(__pt);
2524 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2527 size_type __ds = (__nb + __back_capacity) * __base::__block_size - __base::__map_.empty();
2528 __split_buffer<pointer, typename __base::__pointer_allocator&>
2529 __buf(max<size_type>(2* __base::__map_.capacity(),
2530 __nb + __base::__map_.size()),
2531 0, __base::__map_.__alloc());
2532 #ifndef _LIBCPP_NO_EXCEPTIONS
2535 #endif // _LIBCPP_NO_EXCEPTIONS
2536 for (; __nb > 0; --__nb)
2537 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2538 #ifndef _LIBCPP_NO_EXCEPTIONS
2542 for (typename __base::__map_pointer __i = __buf.begin();
2543 __i != __buf.end(); ++__i)
2544 __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2547 #endif // _LIBCPP_NO_EXCEPTIONS
2548 for (; __back_capacity > 0; --__back_capacity)
2550 __buf.push_back(__base::__map_.back());
2551 __base::__map_.pop_back();
2553 for (typename __base::__map_pointer __i = __base::__map_.begin();
2554 __i != __base::__map_.end(); ++__i)
2555 __buf.push_back(*__i);
2556 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2557 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2558 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2559 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
2560 __base::__start_ += __ds;
2564 // Create back capacity for one block of elements.
2565 // Strong guarantee. Either do it or don't touch anything.
2566 template <class _Tp, class _Allocator>
2568 deque<_Tp, _Allocator>::__add_back_capacity()
2570 allocator_type& __a = __base::__alloc();
2571 if (__front_spare() >= __base::__block_size)
2573 __base::__start_ -= __base::__block_size;
2574 pointer __pt = __base::__map_.front();
2575 __base::__map_.pop_front();
2576 __base::__map_.push_back(__pt);
2578 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2579 else if (__base::__map_.size() < __base::__map_.capacity())
2580 { // we can put the new buffer into the map, but don't shift things around
2581 // until it is allocated. If we throw, we don't need to fix
2582 // anything up (any added buffers are undetectible)
2583 if (__base::__map_.__back_spare() != 0)
2584 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2587 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2588 // Done allocating, reorder capacity
2589 pointer __pt = __base::__map_.front();
2590 __base::__map_.pop_front();
2591 __base::__map_.push_back(__pt);
2594 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2597 __split_buffer<pointer, typename __base::__pointer_allocator&>
2598 __buf(max<size_type>(2* __base::__map_.capacity(), 1),
2599 __base::__map_.size(),
2600 __base::__map_.__alloc());
2602 typedef __allocator_destructor<_Allocator> _Dp;
2603 unique_ptr<pointer, _Dp> __hold(
2604 __alloc_traits::allocate(__a, __base::__block_size),
2605 _Dp(__a, __base::__block_size));
2606 __buf.push_back(__hold.get());
2609 for (typename __base::__map_pointer __i = __base::__map_.end();
2610 __i != __base::__map_.begin();)
2611 __buf.push_front(*--__i);
2612 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2613 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2614 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2615 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
2619 // Create back capacity for __n elements.
2620 // Strong guarantee. Either do it or don't touch anything.
2621 template <class _Tp, class _Allocator>
2623 deque<_Tp, _Allocator>::__add_back_capacity(size_type __n)
2625 allocator_type& __a = __base::__alloc();
2626 size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2627 // Number of unused blocks at front:
2628 size_type __front_capacity = __front_spare() / __base::__block_size;
2629 __front_capacity = _VSTD::min(__front_capacity, __nb); // don't take more than you need
2630 __nb -= __front_capacity; // number of blocks need to allocate
2631 // If __nb == 0, then we have sufficient capacity.
2634 __base::__start_ -= __base::__block_size * __front_capacity;
2635 for (; __front_capacity > 0; --__front_capacity)
2637 pointer __pt = __base::__map_.front();
2638 __base::__map_.pop_front();
2639 __base::__map_.push_back(__pt);
2642 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2643 else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2644 { // we can put the new buffers into the map, but don't shift things around
2645 // until all buffers are allocated. If we throw, we don't need to fix
2646 // anything up (any added buffers are undetectible)
2647 for (; __nb > 0; --__nb)
2649 if (__base::__map_.__back_spare() == 0)
2651 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2653 for (; __nb > 0; --__nb, ++__front_capacity, __base::__start_ +=
2654 __base::__block_size - (__base::__map_.size() == 1))
2655 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2656 // Done allocating, reorder capacity
2657 __base::__start_ -= __base::__block_size * __front_capacity;
2658 for (; __front_capacity > 0; --__front_capacity)
2660 pointer __pt = __base::__map_.front();
2661 __base::__map_.pop_front();
2662 __base::__map_.push_back(__pt);
2665 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2668 size_type __ds = __front_capacity * __base::__block_size;
2669 __split_buffer<pointer, typename __base::__pointer_allocator&>
2670 __buf(max<size_type>(2* __base::__map_.capacity(),
2671 __nb + __base::__map_.size()),
2672 __base::__map_.size() - __front_capacity,
2673 __base::__map_.__alloc());
2674 #ifndef _LIBCPP_NO_EXCEPTIONS
2677 #endif // _LIBCPP_NO_EXCEPTIONS
2678 for (; __nb > 0; --__nb)
2679 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2680 #ifndef _LIBCPP_NO_EXCEPTIONS
2684 for (typename __base::__map_pointer __i = __buf.begin();
2685 __i != __buf.end(); ++__i)
2686 __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2689 #endif // _LIBCPP_NO_EXCEPTIONS
2690 for (; __front_capacity > 0; --__front_capacity)
2692 __buf.push_back(__base::__map_.front());
2693 __base::__map_.pop_front();
2695 for (typename __base::__map_pointer __i = __base::__map_.end();
2696 __i != __base::__map_.begin();)
2697 __buf.push_front(*--__i);
2698 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2699 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2700 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2701 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
2702 __base::__start_ -= __ds;
2706 template <class _Tp, class _Allocator>
2708 deque<_Tp, _Allocator>::pop_front()
2710 allocator_type& __a = __base::__alloc();
2711 __alloc_traits::destroy(__a, __to_address(*(__base::__map_.begin() +
2712 __base::__start_ / __base::__block_size) +
2713 __base::__start_ % __base::__block_size));
2716 __maybe_remove_front_spare();
2719 template <class _Tp, class _Allocator>
2721 deque<_Tp, _Allocator>::pop_back()
2723 _LIBCPP_ASSERT(!empty(), "deque::pop_back called for empty deque");
2724 allocator_type& __a = __base::__alloc();
2725 size_type __p = __base::size() + __base::__start_ - 1;
2726 __alloc_traits::destroy(__a, __to_address(*(__base::__map_.begin() +
2727 __p / __base::__block_size) +
2728 __p % __base::__block_size));
2730 __maybe_remove_back_spare();
2733 // move assign [__f, __l) to [__r, __r + (__l-__f)).
2734 // If __vt points into [__f, __l), then subtract (__f - __r) from __vt.
2735 template <class _Tp, class _Allocator>
2736 typename deque<_Tp, _Allocator>::iterator
2737 deque<_Tp, _Allocator>::__move_and_check(iterator __f, iterator __l, iterator __r,
2738 const_pointer& __vt)
2741 // for (; __f != __l; ++__f, ++__r)
2742 // *__r = _VSTD::move(*__f);
2743 difference_type __n = __l - __f;
2746 pointer __fb = __f.__ptr_;
2747 pointer __fe = *__f.__m_iter_ + __base::__block_size;
2748 difference_type __bs = __fe - __fb;
2754 if (__fb <= __vt && __vt < __fe)
2755 __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) -= __f - __r).__ptr_;
2756 __r = _VSTD::move(__fb, __fe, __r);
2763 // move assign [__f, __l) to [__r - (__l-__f), __r) backwards.
2764 // If __vt points into [__f, __l), then add (__r - __l) to __vt.
2765 template <class _Tp, class _Allocator>
2766 typename deque<_Tp, _Allocator>::iterator
2767 deque<_Tp, _Allocator>::__move_backward_and_check(iterator __f, iterator __l, iterator __r,
2768 const_pointer& __vt)
2771 // while (__f != __l)
2772 // *--__r = _VSTD::move(*--__l);
2773 difference_type __n = __l - __f;
2777 pointer __lb = *__l.__m_iter_;
2778 pointer __le = __l.__ptr_ + 1;
2779 difference_type __bs = __le - __lb;
2785 if (__lb <= __vt && __vt < __le)
2786 __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) += __r - __l - 1).__ptr_;
2787 __r = _VSTD::move_backward(__lb, __le, __r);
2794 // move construct [__f, __l) to [__r, __r + (__l-__f)).
2795 // If __vt points into [__f, __l), then add (__r - __f) to __vt.
2796 template <class _Tp, class _Allocator>
2798 deque<_Tp, _Allocator>::__move_construct_and_check(iterator __f, iterator __l,
2799 iterator __r, const_pointer& __vt)
2801 allocator_type& __a = __base::__alloc();
2803 // for (; __f != __l; ++__r, ++__f, ++__base::size())
2804 // __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__f));
2805 difference_type __n = __l - __f;
2808 pointer __fb = __f.__ptr_;
2809 pointer __fe = *__f.__m_iter_ + __base::__block_size;
2810 difference_type __bs = __fe - __fb;
2816 if (__fb <= __vt && __vt < __fe)
2817 __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) += __r - __f).__ptr_;
2818 for (; __fb != __fe; ++__fb, ++__r, ++__base::size())
2819 __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__fb));
2825 // move construct [__f, __l) to [__r - (__l-__f), __r) backwards.
2826 // If __vt points into [__f, __l), then subtract (__l - __r) from __vt.
2827 template <class _Tp, class _Allocator>
2829 deque<_Tp, _Allocator>::__move_construct_backward_and_check(iterator __f, iterator __l,
2830 iterator __r, const_pointer& __vt)
2832 allocator_type& __a = __base::__alloc();
2834 // for (iterator __j = __l; __j != __f;)
2836 // __alloc_traitsconstruct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__j));
2837 // --__base::__start_;
2838 // ++__base::size();
2840 difference_type __n = __l - __f;
2844 pointer __lb = *__l.__m_iter_;
2845 pointer __le = __l.__ptr_ + 1;
2846 difference_type __bs = __le - __lb;
2852 if (__lb <= __vt && __vt < __le)
2853 __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) -= __l - __r + 1).__ptr_;
2854 while (__le != __lb)
2856 __alloc_traits::construct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__le));
2865 template <class _Tp, class _Allocator>
2866 typename deque<_Tp, _Allocator>::iterator
2867 deque<_Tp, _Allocator>::erase(const_iterator __f)
2869 iterator __b = __base::begin();
2870 difference_type __pos = __f - __b;
2871 iterator __p = __b + __pos;
2872 allocator_type& __a = __base::__alloc();
2873 if (static_cast<size_t>(__pos) <= (__base::size() - 1) / 2)
2874 { // erase from front
2875 _VSTD::move_backward(__b, __p, _VSTD::next(__p));
2876 __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
2879 __maybe_remove_front_spare();
2882 { // erase from back
2883 iterator __i = _VSTD::move(_VSTD::next(__p), __base::end(), __p);
2884 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
2886 __maybe_remove_back_spare();
2888 return __base::begin() + __pos;
2891 template <class _Tp, class _Allocator>
2892 typename deque<_Tp, _Allocator>::iterator
2893 deque<_Tp, _Allocator>::erase(const_iterator __f, const_iterator __l)
2895 difference_type __n = __l - __f;
2896 iterator __b = __base::begin();
2897 difference_type __pos = __f - __b;
2898 iterator __p = __b + __pos;
2901 allocator_type& __a = __base::__alloc();
2902 if (static_cast<size_t>(__pos) <= (__base::size() - __n) / 2)
2903 { // erase from front
2904 iterator __i = _VSTD::move_backward(__b, __p, __p + __n);
2905 for (; __b != __i; ++__b)
2906 __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
2907 __base::size() -= __n;
2908 __base::__start_ += __n;
2909 while (__maybe_remove_front_spare()) {
2913 { // erase from back
2914 iterator __i = _VSTD::move(__p + __n, __base::end(), __p);
2915 for (iterator __e = __base::end(); __i != __e; ++__i)
2916 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
2917 __base::size() -= __n;
2918 while (__maybe_remove_back_spare()) {
2922 return __base::begin() + __pos;
2925 template <class _Tp, class _Allocator>
2927 deque<_Tp, _Allocator>::__erase_to_end(const_iterator __f)
2929 iterator __e = __base::end();
2930 difference_type __n = __e - __f;
2933 allocator_type& __a = __base::__alloc();
2934 iterator __b = __base::begin();
2935 difference_type __pos = __f - __b;
2936 for (iterator __p = __b + __pos; __p != __e; ++__p)
2937 __alloc_traits::destroy(__a, _VSTD::addressof(*__p));
2938 __base::size() -= __n;
2939 while (__maybe_remove_back_spare()) {
2944 template <class _Tp, class _Allocator>
2947 deque<_Tp, _Allocator>::swap(deque& __c)
2948 #if _LIBCPP_STD_VER >= 14
2951 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
2952 __is_nothrow_swappable<allocator_type>::value)
2958 template <class _Tp, class _Allocator>
2961 deque<_Tp, _Allocator>::clear() _NOEXCEPT
2966 template <class _Tp, class _Allocator>
2967 inline _LIBCPP_INLINE_VISIBILITY
2969 operator==(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2971 const typename deque<_Tp, _Allocator>::size_type __sz = __x.size();
2972 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
2975 template <class _Tp, class _Allocator>
2976 inline _LIBCPP_INLINE_VISIBILITY
2978 operator!=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2980 return !(__x == __y);
2983 template <class _Tp, class _Allocator>
2984 inline _LIBCPP_INLINE_VISIBILITY
2986 operator< (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2988 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
2991 template <class _Tp, class _Allocator>
2992 inline _LIBCPP_INLINE_VISIBILITY
2994 operator> (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2999 template <class _Tp, class _Allocator>
3000 inline _LIBCPP_INLINE_VISIBILITY
3002 operator>=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
3004 return !(__x < __y);
3007 template <class _Tp, class _Allocator>
3008 inline _LIBCPP_INLINE_VISIBILITY
3010 operator<=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
3012 return !(__y < __x);
3015 template <class _Tp, class _Allocator>
3016 inline _LIBCPP_INLINE_VISIBILITY
3018 swap(deque<_Tp, _Allocator>& __x, deque<_Tp, _Allocator>& __y)
3019 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
3024 #if _LIBCPP_STD_VER > 17
3025 template <class _Tp, class _Allocator, class _Up>
3026 inline _LIBCPP_INLINE_VISIBILITY typename deque<_Tp, _Allocator>::size_type
3027 erase(deque<_Tp, _Allocator>& __c, const _Up& __v) {
3028 auto __old_size = __c.size();
3029 __c.erase(_VSTD::remove(__c.begin(), __c.end(), __v), __c.end());
3030 return __old_size - __c.size();
3033 template <class _Tp, class _Allocator, class _Predicate>
3034 inline _LIBCPP_INLINE_VISIBILITY typename deque<_Tp, _Allocator>::size_type
3035 erase_if(deque<_Tp, _Allocator>& __c, _Predicate __pred) {
3036 auto __old_size = __c.size();
3037 __c.erase(_VSTD::remove_if(__c.begin(), __c.end(), __pred), __c.end());
3038 return __old_size - __c.size();
3043 _LIBCPP_END_NAMESPACE_STD
3047 #endif // _LIBCPP_DEQUE