blob: 83575c33f3902d3bca96cb0f02e36ea06db793b4 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------------- deque -----------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_DEQUE
11#define _LIBCPP_DEQUE
12
13/*
14 deque synopsis
15
16namespace std
17{
18
19template <class T, class Allocator = allocator<T> >
20class deque
21{
22public:
23 // types:
24 typedef T value_type;
25 typedef Allocator allocator_type;
26
27 typedef typename allocator_type::reference reference;
28 typedef typename allocator_type::const_reference const_reference;
29 typedef implementation-defined iterator;
30 typedef implementation-defined const_iterator;
31 typedef typename allocator_type::size_type size_type;
32 typedef typename allocator_type::difference_type difference_type;
33
34 typedef typename allocator_type::pointer pointer;
35 typedef typename allocator_type::const_pointer const_pointer;
36 typedef std::reverse_iterator<iterator> reverse_iterator;
37 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
38
39 // construct/copy/destroy:
Howard Hinnantad979ba2011-06-03 15:16:49 +000040 deque() noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +000041 explicit deque(const allocator_type& a);
42 explicit deque(size_type n);
Marshall Clow68098692013-09-09 18:19:45 +000043 explicit deque(size_type n, const allocator_type& a); // C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000044 deque(size_type n, const value_type& v);
45 deque(size_type n, const value_type& v, const allocator_type& a);
46 template <class InputIterator>
47 deque(InputIterator f, InputIterator l);
48 template <class InputIterator>
49 deque(InputIterator f, InputIterator l, const allocator_type& a);
50 deque(const deque& c);
Howard Hinnant4931e092011-06-02 21:38:57 +000051 deque(deque&& c)
52 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +000053 deque(initializer_list<value_type> il, const Allocator& a = allocator_type());
54 deque(const deque& c, const allocator_type& a);
55 deque(deque&& c, const allocator_type& a);
56 ~deque();
57
58 deque& operator=(const deque& c);
Howard Hinnant4931e092011-06-02 21:38:57 +000059 deque& operator=(deque&& c)
60 noexcept(
61 allocator_type::propagate_on_container_move_assignment::value &&
62 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +000063 deque& operator=(initializer_list<value_type> il);
64
65 template <class InputIterator>
66 void assign(InputIterator f, InputIterator l);
67 void assign(size_type n, const value_type& v);
68 void assign(initializer_list<value_type> il);
69
Howard Hinnantda2df912011-06-02 16:10:22 +000070 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000071
72 // iterators:
73
Howard Hinnantda2df912011-06-02 16:10:22 +000074 iterator begin() noexcept;
75 const_iterator begin() const noexcept;
76 iterator end() noexcept;
77 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000078
Howard Hinnantda2df912011-06-02 16:10:22 +000079 reverse_iterator rbegin() noexcept;
80 const_reverse_iterator rbegin() const noexcept;
81 reverse_iterator rend() noexcept;
82 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000083
Howard Hinnantda2df912011-06-02 16:10:22 +000084 const_iterator cbegin() const noexcept;
85 const_iterator cend() const noexcept;
86 const_reverse_iterator crbegin() const noexcept;
87 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000088
89 // capacity:
Howard Hinnantda2df912011-06-02 16:10:22 +000090 size_type size() const noexcept;
91 size_type max_size() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000092 void resize(size_type n);
93 void resize(size_type n, const value_type& v);
94 void shrink_to_fit();
Howard Hinnantda2df912011-06-02 16:10:22 +000095 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000096
97 // element access:
98 reference operator[](size_type i);
99 const_reference operator[](size_type i) const;
100 reference at(size_type i);
101 const_reference at(size_type i) const;
102 reference front();
103 const_reference front() const;
104 reference back();
105 const_reference back() const;
106
107 // modifiers:
108 void push_front(const value_type& v);
109 void push_front(value_type&& v);
110 void push_back(const value_type& v);
111 void push_back(value_type&& v);
Marshall Clowea52cc42017-01-24 23:09:12 +0000112 template <class... Args> reference emplace_front(Args&&... args); // reference in C++17
113 template <class... Args> reference emplace_back(Args&&... args); // reference in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000114 template <class... Args> iterator emplace(const_iterator p, Args&&... args);
115 iterator insert(const_iterator p, const value_type& v);
116 iterator insert(const_iterator p, value_type&& v);
117 iterator insert(const_iterator p, size_type n, const value_type& v);
118 template <class InputIterator>
Marshall Clow6b612cf2015-01-22 18:33:29 +0000119 iterator insert(const_iterator p, InputIterator f, InputIterator l);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000120 iterator insert(const_iterator p, initializer_list<value_type> il);
121 void pop_front();
122 void pop_back();
123 iterator erase(const_iterator p);
124 iterator erase(const_iterator f, const_iterator l);
Howard Hinnant4931e092011-06-02 21:38:57 +0000125 void swap(deque& c)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000126 noexcept(allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantda2df912011-06-02 16:10:22 +0000127 void clear() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000128};
129
Marshall Clow4cc3a452018-05-18 23:44:13 +0000130template <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
131 deque(InputIterator, InputIterator, Allocator = Allocator())
132 -> deque<typename iterator_traits<InputIterator>::value_type, Allocator>;
133
Howard Hinnantc51e1022010-05-11 19:42:16 +0000134template <class T, class Allocator>
135 bool operator==(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
136template <class T, class Allocator>
137 bool operator< (const deque<T,Allocator>& x, const deque<T,Allocator>& y);
138template <class T, class Allocator>
139 bool operator!=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
140template <class T, class Allocator>
141 bool operator> (const deque<T,Allocator>& x, const deque<T,Allocator>& y);
142template <class T, class Allocator>
143 bool operator>=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
144template <class T, class Allocator>
145 bool operator<=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
146
147// specialized algorithms:
148template <class T, class Allocator>
Howard Hinnant287548a2011-06-03 17:30:28 +0000149 void swap(deque<T,Allocator>& x, deque<T,Allocator>& y)
150 noexcept(noexcept(x.swap(y)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000151
Marshall Clow29b53f22018-12-14 18:49:35 +0000152template <class T, class Allocator, class U>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200153 typename deque<T, Allocator>::size_type
154 erase(deque<T, Allocator>& c, const U& value); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000155template <class T, class Allocator, class Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200156 typename deque<T, Allocator>::size_type
157 erase_if(deque<T, Allocator>& c, Predicate pred); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000158
Howard Hinnantc51e1022010-05-11 19:42:16 +0000159} // std
160
161*/
162
Howard Hinnantc51e1022010-05-11 19:42:16 +0000163#include <__config>
164#include <__split_buffer>
Arthur O'Dwyer7deec122021-03-24 18:19:12 -0400165#include <compare>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000166#include <type_traits>
167#include <initializer_list>
168#include <iterator>
169#include <algorithm>
170#include <stdexcept>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000171#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000172
Eric Fiselierf4433a32017-05-31 22:07:49 +0000173#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
174#pragma GCC system_header
175#endif
176
177_LIBCPP_PUSH_MACROS
178#include <__undef_macros>
179
Howard Hinnantc5a5fbd2011-11-29 16:45:27 +0000180
Howard Hinnantc51e1022010-05-11 19:42:16 +0000181_LIBCPP_BEGIN_NAMESPACE_STD
182
183template <class _Tp, class _Allocator> class __deque_base;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000184template <class _Tp, class _Allocator = allocator<_Tp> > class _LIBCPP_TEMPLATE_VIS deque;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000185
186template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
187 class _DiffType, _DiffType _BlockSize>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000188class _LIBCPP_TEMPLATE_VIS __deque_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000189
190template <class _RAIter,
191 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
192__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
193copy(_RAIter __f,
194 _RAIter __l,
195 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500196 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000197
198template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
199 class _OutputIterator>
200_OutputIterator
201copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
202 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
203 _OutputIterator __r);
204
205template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
206 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
207__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
208copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
209 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
210 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
211
212template <class _RAIter,
213 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
214__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
215copy_backward(_RAIter __f,
216 _RAIter __l,
217 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500218 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000219
220template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
221 class _OutputIterator>
222_OutputIterator
223copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
224 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
225 _OutputIterator __r);
226
227template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
228 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
229__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
230copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
231 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
232 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
233
234template <class _RAIter,
235 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
236__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
237move(_RAIter __f,
238 _RAIter __l,
239 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500240 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000241
242template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
243 class _OutputIterator>
244_OutputIterator
245move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
246 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
247 _OutputIterator __r);
248
249template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
250 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
251__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
252move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
253 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
254 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
255
256template <class _RAIter,
257 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
258__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
259move_backward(_RAIter __f,
260 _RAIter __l,
261 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500262 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000263
264template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
265 class _OutputIterator>
266_OutputIterator
267move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
268 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
269 _OutputIterator __r);
270
271template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
272 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
273__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
274move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
275 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
276 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
277
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000278template <class _ValueType, class _DiffType>
279struct __deque_block_size {
280 static const _DiffType value = sizeof(_ValueType) < 256 ? 4096 / sizeof(_ValueType) : 16;
281};
282
Howard Hinnantc51e1022010-05-11 19:42:16 +0000283template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000284 class _DiffType, _DiffType _BS =
285#ifdef _LIBCPP_ABI_INCOMPLETE_TYPES_IN_DEQUE
286// Keep template parameter to avoid changing all template declarations thoughout
287// this file.
288 0
289#else
290 __deque_block_size<_ValueType, _DiffType>::value
291#endif
292 >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000293class _LIBCPP_TEMPLATE_VIS __deque_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000294{
295 typedef _MapPointer __map_iterator;
296public:
297 typedef _Pointer pointer;
298 typedef _DiffType difference_type;
299private:
300 __map_iterator __m_iter_;
301 pointer __ptr_;
302
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000303 static const difference_type __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000304public:
305 typedef _ValueType value_type;
306 typedef random_access_iterator_tag iterator_category;
307 typedef _Reference reference;
308
Marshall Clow7a3a61d2013-08-06 16:14:36 +0000309 _LIBCPP_INLINE_VISIBILITY __deque_iterator() _NOEXCEPT
310#if _LIBCPP_STD_VER > 11
311 : __m_iter_(nullptr), __ptr_(nullptr)
312#endif
313 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000314
Howard Hinnantc834c512011-11-29 18:15:50 +0000315 template <class _Pp, class _Rp, class _MP>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000316 _LIBCPP_INLINE_VISIBILITY
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000317 __deque_iterator(const __deque_iterator<value_type, _Pp, _Rp, _MP, difference_type, _BS>& __it,
Howard Hinnantc834c512011-11-29 18:15:50 +0000318 typename enable_if<is_convertible<_Pp, pointer>::value>::type* = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000319 : __m_iter_(__it.__m_iter_), __ptr_(__it.__ptr_) {}
320
321 _LIBCPP_INLINE_VISIBILITY reference operator*() const {return *__ptr_;}
322 _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return __ptr_;}
323
324 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator++()
325 {
326 if (++__ptr_ - *__m_iter_ == __block_size)
327 {
328 ++__m_iter_;
329 __ptr_ = *__m_iter_;
330 }
331 return *this;
332 }
333
334 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator++(int)
335 {
336 __deque_iterator __tmp = *this;
337 ++(*this);
338 return __tmp;
339 }
340
341 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator--()
342 {
343 if (__ptr_ == *__m_iter_)
344 {
345 --__m_iter_;
346 __ptr_ = *__m_iter_ + __block_size;
347 }
348 --__ptr_;
349 return *this;
350 }
351
352 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator--(int)
353 {
354 __deque_iterator __tmp = *this;
355 --(*this);
356 return __tmp;
357 }
358
359 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator+=(difference_type __n)
360 {
361 if (__n != 0)
362 {
363 __n += __ptr_ - *__m_iter_;
364 if (__n > 0)
365 {
366 __m_iter_ += __n / __block_size;
367 __ptr_ = *__m_iter_ + __n % __block_size;
368 }
369 else // (__n < 0)
370 {
371 difference_type __z = __block_size - 1 - __n;
372 __m_iter_ -= __z / __block_size;
373 __ptr_ = *__m_iter_ + (__block_size - 1 - __z % __block_size);
374 }
375 }
376 return *this;
377 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000378
Howard Hinnantc51e1022010-05-11 19:42:16 +0000379 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator-=(difference_type __n)
380 {
381 return *this += -__n;
382 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000383
Howard Hinnantc51e1022010-05-11 19:42:16 +0000384 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator+(difference_type __n) const
385 {
386 __deque_iterator __t(*this);
387 __t += __n;
388 return __t;
389 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000390
Howard Hinnantc51e1022010-05-11 19:42:16 +0000391 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator-(difference_type __n) const
392 {
393 __deque_iterator __t(*this);
394 __t -= __n;
395 return __t;
396 }
397
398 _LIBCPP_INLINE_VISIBILITY
399 friend __deque_iterator operator+(difference_type __n, const __deque_iterator& __it)
400 {return __it + __n;}
401
402 _LIBCPP_INLINE_VISIBILITY
403 friend difference_type operator-(const __deque_iterator& __x, const __deque_iterator& __y)
404 {
405 if (__x != __y)
406 return (__x.__m_iter_ - __y.__m_iter_) * __block_size
407 + (__x.__ptr_ - *__x.__m_iter_)
408 - (__y.__ptr_ - *__y.__m_iter_);
409 return 0;
410 }
411
412 _LIBCPP_INLINE_VISIBILITY reference operator[](difference_type __n) const
413 {return *(*this + __n);}
414
415 _LIBCPP_INLINE_VISIBILITY friend
416 bool operator==(const __deque_iterator& __x, const __deque_iterator& __y)
417 {return __x.__ptr_ == __y.__ptr_;}
418
419 _LIBCPP_INLINE_VISIBILITY friend
420 bool operator!=(const __deque_iterator& __x, const __deque_iterator& __y)
421 {return !(__x == __y);}
422
423 _LIBCPP_INLINE_VISIBILITY friend
424 bool operator<(const __deque_iterator& __x, const __deque_iterator& __y)
425 {return __x.__m_iter_ < __y.__m_iter_ ||
426 (__x.__m_iter_ == __y.__m_iter_ && __x.__ptr_ < __y.__ptr_);}
427
428 _LIBCPP_INLINE_VISIBILITY friend
429 bool operator>(const __deque_iterator& __x, const __deque_iterator& __y)
430 {return __y < __x;}
431
432 _LIBCPP_INLINE_VISIBILITY friend
433 bool operator<=(const __deque_iterator& __x, const __deque_iterator& __y)
434 {return !(__y < __x);}
435
436 _LIBCPP_INLINE_VISIBILITY friend
437 bool operator>=(const __deque_iterator& __x, const __deque_iterator& __y)
438 {return !(__x < __y);}
439
440private:
Howard Hinnantda2df912011-06-02 16:10:22 +0000441 _LIBCPP_INLINE_VISIBILITY __deque_iterator(__map_iterator __m, pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000442 : __m_iter_(__m), __ptr_(__p) {}
443
Howard Hinnantc834c512011-11-29 18:15:50 +0000444 template <class _Tp, class _Ap> friend class __deque_base;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000445 template <class _Tp, class _Ap> friend class _LIBCPP_TEMPLATE_VIS deque;
Howard Hinnantc834c512011-11-29 18:15:50 +0000446 template <class _Vp, class _Pp, class _Rp, class _MP, class _Dp, _Dp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000447 friend class _LIBCPP_TEMPLATE_VIS __deque_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000448
449 template <class _RAIter,
450 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
451 friend
452 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
453 copy(_RAIter __f,
454 _RAIter __l,
455 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500456 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000457
458 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
459 class _OutputIterator>
460 friend
461 _OutputIterator
462 copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
463 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
464 _OutputIterator __r);
465
466 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
467 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
468 friend
469 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
470 copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
471 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
472 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
473
474 template <class _RAIter,
475 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
476 friend
477 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
478 copy_backward(_RAIter __f,
479 _RAIter __l,
480 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500481 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000482
483 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
484 class _OutputIterator>
485 friend
486 _OutputIterator
487 copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
488 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
489 _OutputIterator __r);
490
491 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
492 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
493 friend
494 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
495 copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
496 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
497 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
498
499 template <class _RAIter,
500 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
501 friend
502 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
503 move(_RAIter __f,
504 _RAIter __l,
505 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500506 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000507
508 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
509 class _OutputIterator>
510 friend
511 _OutputIterator
512 move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
513 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
514 _OutputIterator __r);
515
516 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
517 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
518 friend
519 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
520 move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
521 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
522 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
523
524 template <class _RAIter,
525 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
526 friend
527 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
528 move_backward(_RAIter __f,
529 _RAIter __l,
530 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500531 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000532
533 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
534 class _OutputIterator>
535 friend
536 _OutputIterator
537 move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
538 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
539 _OutputIterator __r);
540
541 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
542 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
543 friend
544 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
545 move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
546 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
547 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
548};
549
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000550template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
551 class _DiffType, _DiffType _BlockSize>
552const _DiffType __deque_iterator<_ValueType, _Pointer, _Reference, _MapPointer,
553 _DiffType, _BlockSize>::__block_size =
554 __deque_block_size<_ValueType, _DiffType>::value;
555
Howard Hinnantc51e1022010-05-11 19:42:16 +0000556// copy
557
558template <class _RAIter,
559 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
560__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
561copy(_RAIter __f,
562 _RAIter __l,
563 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500564 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000565{
566 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
567 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000568 const difference_type __block_size = __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::__block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000569 while (__f != __l)
570 {
571 pointer __rb = __r.__ptr_;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000572 pointer __re = *__r.__m_iter_ + __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000573 difference_type __bs = __re - __rb;
574 difference_type __n = __l - __f;
575 _RAIter __m = __l;
576 if (__n > __bs)
577 {
578 __n = __bs;
579 __m = __f + __n;
580 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000581 _VSTD::copy(__f, __m, __rb);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000582 __f = __m;
583 __r += __n;
584 }
585 return __r;
586}
587
588template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
589 class _OutputIterator>
590_OutputIterator
591copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
592 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
593 _OutputIterator __r)
594{
595 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
596 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000597 const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000598 difference_type __n = __l - __f;
599 while (__n > 0)
600 {
601 pointer __fb = __f.__ptr_;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000602 pointer __fe = *__f.__m_iter_ + __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000603 difference_type __bs = __fe - __fb;
604 if (__bs > __n)
605 {
606 __bs = __n;
607 __fe = __fb + __bs;
608 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000609 __r = _VSTD::copy(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000610 __n -= __bs;
611 __f += __bs;
612 }
613 return __r;
614}
615
616template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
617 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
618__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
619copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
620 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
621 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
622{
623 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
624 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000625 const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000626 difference_type __n = __l - __f;
627 while (__n > 0)
628 {
629 pointer __fb = __f.__ptr_;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000630 pointer __fe = *__f.__m_iter_ + __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000631 difference_type __bs = __fe - __fb;
632 if (__bs > __n)
633 {
634 __bs = __n;
635 __fe = __fb + __bs;
636 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000637 __r = _VSTD::copy(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000638 __n -= __bs;
639 __f += __bs;
640 }
641 return __r;
642}
643
644// copy_backward
645
646template <class _RAIter,
647 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
648__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
649copy_backward(_RAIter __f,
650 _RAIter __l,
651 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500652 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000653{
654 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
655 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
656 while (__f != __l)
657 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000658 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _VSTD::prev(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000659 pointer __rb = *__rp.__m_iter_;
660 pointer __re = __rp.__ptr_ + 1;
661 difference_type __bs = __re - __rb;
662 difference_type __n = __l - __f;
663 _RAIter __m = __f;
664 if (__n > __bs)
665 {
666 __n = __bs;
667 __m = __l - __n;
668 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000669 _VSTD::copy_backward(__m, __l, __re);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000670 __l = __m;
671 __r -= __n;
672 }
673 return __r;
674}
675
676template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
677 class _OutputIterator>
678_OutputIterator
679copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
680 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
681 _OutputIterator __r)
682{
683 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
684 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
685 difference_type __n = __l - __f;
686 while (__n > 0)
687 {
688 --__l;
689 pointer __lb = *__l.__m_iter_;
690 pointer __le = __l.__ptr_ + 1;
691 difference_type __bs = __le - __lb;
692 if (__bs > __n)
693 {
694 __bs = __n;
695 __lb = __le - __bs;
696 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000697 __r = _VSTD::copy_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000698 __n -= __bs;
699 __l -= __bs - 1;
700 }
701 return __r;
702}
703
704template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
705 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
706__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
707copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
708 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
709 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
710{
711 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
712 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
713 difference_type __n = __l - __f;
714 while (__n > 0)
715 {
716 --__l;
717 pointer __lb = *__l.__m_iter_;
718 pointer __le = __l.__ptr_ + 1;
719 difference_type __bs = __le - __lb;
720 if (__bs > __n)
721 {
722 __bs = __n;
723 __lb = __le - __bs;
724 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000725 __r = _VSTD::copy_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000726 __n -= __bs;
727 __l -= __bs - 1;
728 }
729 return __r;
730}
731
732// move
733
734template <class _RAIter,
735 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
736__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
737move(_RAIter __f,
738 _RAIter __l,
739 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500740 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000741{
742 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
743 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000744 const difference_type __block_size = __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::__block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000745 while (__f != __l)
746 {
747 pointer __rb = __r.__ptr_;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000748 pointer __re = *__r.__m_iter_ + __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000749 difference_type __bs = __re - __rb;
750 difference_type __n = __l - __f;
751 _RAIter __m = __l;
752 if (__n > __bs)
753 {
754 __n = __bs;
755 __m = __f + __n;
756 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000757 _VSTD::move(__f, __m, __rb);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000758 __f = __m;
759 __r += __n;
760 }
761 return __r;
762}
763
764template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
765 class _OutputIterator>
766_OutputIterator
767move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
768 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
769 _OutputIterator __r)
770{
771 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
772 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000773 const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000774 difference_type __n = __l - __f;
775 while (__n > 0)
776 {
777 pointer __fb = __f.__ptr_;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000778 pointer __fe = *__f.__m_iter_ + __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000779 difference_type __bs = __fe - __fb;
780 if (__bs > __n)
781 {
782 __bs = __n;
783 __fe = __fb + __bs;
784 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000785 __r = _VSTD::move(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000786 __n -= __bs;
787 __f += __bs;
788 }
789 return __r;
790}
791
792template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
793 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
794__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
795move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
796 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
797 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
798{
799 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
800 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000801 const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000802 difference_type __n = __l - __f;
803 while (__n > 0)
804 {
805 pointer __fb = __f.__ptr_;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000806 pointer __fe = *__f.__m_iter_ + __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000807 difference_type __bs = __fe - __fb;
808 if (__bs > __n)
809 {
810 __bs = __n;
811 __fe = __fb + __bs;
812 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000813 __r = _VSTD::move(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000814 __n -= __bs;
815 __f += __bs;
816 }
817 return __r;
818}
819
820// move_backward
821
822template <class _RAIter,
823 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
824__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
825move_backward(_RAIter __f,
826 _RAIter __l,
827 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500828 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000829{
830 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
831 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
832 while (__f != __l)
833 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000834 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _VSTD::prev(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000835 pointer __rb = *__rp.__m_iter_;
836 pointer __re = __rp.__ptr_ + 1;
837 difference_type __bs = __re - __rb;
838 difference_type __n = __l - __f;
839 _RAIter __m = __f;
840 if (__n > __bs)
841 {
842 __n = __bs;
843 __m = __l - __n;
844 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000845 _VSTD::move_backward(__m, __l, __re);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000846 __l = __m;
847 __r -= __n;
848 }
849 return __r;
850}
851
852template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
853 class _OutputIterator>
854_OutputIterator
855move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
856 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
857 _OutputIterator __r)
858{
859 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
860 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
861 difference_type __n = __l - __f;
862 while (__n > 0)
863 {
864 --__l;
865 pointer __lb = *__l.__m_iter_;
866 pointer __le = __l.__ptr_ + 1;
867 difference_type __bs = __le - __lb;
868 if (__bs > __n)
869 {
870 __bs = __n;
871 __lb = __le - __bs;
872 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000873 __r = _VSTD::move_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000874 __n -= __bs;
875 __l -= __bs - 1;
876 }
877 return __r;
878}
879
880template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
881 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
882__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
883move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
884 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
885 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
886{
887 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
888 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
889 difference_type __n = __l - __f;
890 while (__n > 0)
891 {
892 --__l;
893 pointer __lb = *__l.__m_iter_;
894 pointer __le = __l.__ptr_ + 1;
895 difference_type __bs = __le - __lb;
896 if (__bs > __n)
897 {
898 __bs = __n;
899 __lb = __le - __bs;
900 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000901 __r = _VSTD::move_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000902 __n -= __bs;
903 __l -= __bs - 1;
904 }
905 return __r;
906}
907
908template <bool>
909class __deque_base_common
910{
911protected:
Marshall Clow8fea1612016-08-25 15:09:01 +0000912 _LIBCPP_NORETURN void __throw_length_error() const;
913 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000914};
915
916template <bool __b>
917void
918__deque_base_common<__b>::__throw_length_error() const
919{
Marshall Clow8fea1612016-08-25 15:09:01 +0000920 _VSTD::__throw_length_error("deque");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000921}
922
923template <bool __b>
924void
925__deque_base_common<__b>::__throw_out_of_range() const
926{
Marshall Clow8fea1612016-08-25 15:09:01 +0000927 _VSTD::__throw_out_of_range("deque");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000928}
929
930template <class _Tp, class _Allocator>
931class __deque_base
932 : protected __deque_base_common<true>
933{
934 __deque_base(const __deque_base& __c);
935 __deque_base& operator=(const __deque_base& __c);
Marshall Clow4cc3a452018-05-18 23:44:13 +0000936public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000937 typedef _Allocator allocator_type;
938 typedef allocator_traits<allocator_type> __alloc_traits;
Marshall Clow4cc3a452018-05-18 23:44:13 +0000939 typedef typename __alloc_traits::size_type size_type;
Eric Fiselier27e03622019-08-01 23:11:18 +0000940
Marshall Clow4cc3a452018-05-18 23:44:13 +0000941 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000942 typedef value_type& reference;
943 typedef const value_type& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000944 typedef typename __alloc_traits::difference_type difference_type;
945 typedef typename __alloc_traits::pointer pointer;
946 typedef typename __alloc_traits::const_pointer const_pointer;
947
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000948 static const difference_type __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000949
Marshall Clow940e01c2015-04-07 05:21:38 +0000950 typedef typename __rebind_alloc_helper<__alloc_traits, pointer>::type __pointer_allocator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000951 typedef allocator_traits<__pointer_allocator> __map_traits;
952 typedef typename __map_traits::pointer __map_pointer;
Marshall Clow940e01c2015-04-07 05:21:38 +0000953 typedef typename __rebind_alloc_helper<__alloc_traits, const_pointer>::type __const_pointer_allocator;
Howard Hinnantdcb73a32013-06-23 21:17:24 +0000954 typedef typename allocator_traits<__const_pointer_allocator>::const_pointer __map_const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000955 typedef __split_buffer<pointer, __pointer_allocator> __map;
956
957 typedef __deque_iterator<value_type, pointer, reference, __map_pointer,
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000958 difference_type> iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000959 typedef __deque_iterator<value_type, const_pointer, const_reference, __map_const_pointer,
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000960 difference_type> const_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000961
Eric Fiselierc52db212019-08-12 07:51:05 +0000962 struct __deque_block_range {
963 explicit __deque_block_range(pointer __b, pointer __e) _NOEXCEPT : __begin_(__b), __end_(__e) {}
964 const pointer __begin_;
965 const pointer __end_;
966 };
967
968 struct __deque_range {
969 iterator __pos_;
970 const iterator __end_;
971
972 __deque_range(iterator __pos, iterator __e) _NOEXCEPT
973 : __pos_(__pos), __end_(__e) {}
974
975 explicit operator bool() const _NOEXCEPT {
976 return __pos_ != __end_;
977 }
978
979 __deque_range begin() const {
980 return *this;
981 }
982
983 __deque_range end() const {
984 return __deque_range(__end_, __end_);
985 }
986 __deque_block_range operator*() const _NOEXCEPT {
987 if (__pos_.__m_iter_ == __end_.__m_iter_) {
988 return __deque_block_range(__pos_.__ptr_, __end_.__ptr_);
989 }
990 return __deque_block_range(__pos_.__ptr_, *__pos_.__m_iter_ + __block_size);
991 }
992
993 __deque_range& operator++() _NOEXCEPT {
994 if (__pos_.__m_iter_ == __end_.__m_iter_) {
995 __pos_ = __end_;
996 } else {
997 ++__pos_.__m_iter_;
998 __pos_.__ptr_ = *__pos_.__m_iter_;
999 }
1000 return *this;
1001 }
1002
1003
1004 friend bool operator==(__deque_range const& __lhs, __deque_range const& __rhs) {
1005 return __lhs.__pos_ == __rhs.__pos_;
1006 }
1007 friend bool operator!=(__deque_range const& __lhs, __deque_range const& __rhs) {
1008 return !(__lhs == __rhs);
1009 }
1010 };
1011
1012
1013
1014 struct _ConstructTransaction {
1015 _ConstructTransaction(__deque_base* __db, __deque_block_range& __r)
1016 : __pos_(__r.__begin_), __end_(__r.__end_), __begin_(__r.__begin_), __base_(__db) {}
1017
1018
1019 ~_ConstructTransaction() {
1020 __base_->size() += (__pos_ - __begin_);
1021 }
1022
1023 pointer __pos_;
1024 const pointer __end_;
1025 private:
1026 const pointer __begin_;
1027 __deque_base * const __base_;
1028 };
1029
Marshall Clow4cc3a452018-05-18 23:44:13 +00001030protected:
Howard Hinnantc51e1022010-05-11 19:42:16 +00001031 __map __map_;
1032 size_type __start_;
1033 __compressed_pair<size_type, allocator_type> __size_;
1034
Howard Hinnantda2df912011-06-02 16:10:22 +00001035 iterator begin() _NOEXCEPT;
1036 const_iterator begin() const _NOEXCEPT;
1037 iterator end() _NOEXCEPT;
1038 const_iterator end() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001039
1040 _LIBCPP_INLINE_VISIBILITY size_type& size() {return __size_.first();}
Howard Hinnantda2df912011-06-02 16:10:22 +00001041 _LIBCPP_INLINE_VISIBILITY
1042 const size_type& size() const _NOEXCEPT {return __size_.first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001043 _LIBCPP_INLINE_VISIBILITY allocator_type& __alloc() {return __size_.second();}
Howard Hinnantda2df912011-06-02 16:10:22 +00001044 _LIBCPP_INLINE_VISIBILITY
1045 const allocator_type& __alloc() const _NOEXCEPT {return __size_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001046
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001047 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantad979ba2011-06-03 15:16:49 +00001048 __deque_base()
1049 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001050 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001051 explicit __deque_base(const allocator_type& __a);
Howard Hinnantca2fc222011-06-02 20:00:14 +00001052public:
Howard Hinnantc51e1022010-05-11 19:42:16 +00001053 ~__deque_base();
1054
Eric Fiselier212e3f22017-04-16 03:17:01 +00001055#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantca2fc222011-06-02 20:00:14 +00001056 __deque_base(__deque_base&& __c)
1057 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001058 __deque_base(__deque_base&& __c, const allocator_type& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001059#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001060
Howard Hinnantca2fc222011-06-02 20:00:14 +00001061 void swap(__deque_base& __c)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001062#if _LIBCPP_STD_VER >= 14
1063 _NOEXCEPT;
1064#else
Louis Dionne72f24392018-12-12 23:58:25 +00001065 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001066 __is_nothrow_swappable<allocator_type>::value);
1067#endif
Howard Hinnantca2fc222011-06-02 20:00:14 +00001068protected:
Howard Hinnantda2df912011-06-02 16:10:22 +00001069 void clear() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001070
1071 bool __invariants() const;
1072
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001073 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001074 void __move_assign(__deque_base& __c)
Howard Hinnant4931e092011-06-02 21:38:57 +00001075 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1076 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001077 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001078 __map_ = _VSTD::move(__c.__map_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001079 __start_ = __c.__start_;
1080 size() = __c.size();
1081 __move_assign_alloc(__c);
1082 __c.__start_ = __c.size() = 0;
1083 }
1084
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001085 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001086 void __move_assign_alloc(__deque_base& __c)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001087 _NOEXCEPT_(!__alloc_traits::propagate_on_container_move_assignment::value ||
1088 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001089 {__move_assign_alloc(__c, integral_constant<bool,
1090 __alloc_traits::propagate_on_container_move_assignment::value>());}
1091
1092private:
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001093 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001094 void __move_assign_alloc(__deque_base& __c, true_type)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001095 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001096 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001097 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001098 }
1099
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001100 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001101 void __move_assign_alloc(__deque_base&, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001102 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001103};
1104
1105template <class _Tp, class _Allocator>
Evgeniy Stepanovc299de22015-11-06 22:02:29 +00001106const typename __deque_base<_Tp, _Allocator>::difference_type
1107 __deque_base<_Tp, _Allocator>::__block_size =
1108 __deque_block_size<value_type, difference_type>::value;
1109
1110template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001111bool
1112__deque_base<_Tp, _Allocator>::__invariants() const
1113{
1114 if (!__map_.__invariants())
1115 return false;
1116 if (__map_.size() >= size_type(-1) / __block_size)
1117 return false;
1118 for (typename __map::const_iterator __i = __map_.begin(), __e = __map_.end();
1119 __i != __e; ++__i)
1120 if (*__i == nullptr)
1121 return false;
1122 if (__map_.size() != 0)
1123 {
1124 if (size() >= __map_.size() * __block_size)
1125 return false;
1126 if (__start_ >= __map_.size() * __block_size - size())
1127 return false;
1128 }
1129 else
1130 {
1131 if (size() != 0)
1132 return false;
1133 if (__start_ != 0)
1134 return false;
1135 }
1136 return true;
1137}
1138
1139template <class _Tp, class _Allocator>
1140typename __deque_base<_Tp, _Allocator>::iterator
Howard Hinnantda2df912011-06-02 16:10:22 +00001141__deque_base<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001142{
1143 __map_pointer __mp = __map_.begin() + __start_ / __block_size;
1144 return iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
1145}
1146
1147template <class _Tp, class _Allocator>
1148typename __deque_base<_Tp, _Allocator>::const_iterator
Howard Hinnantda2df912011-06-02 16:10:22 +00001149__deque_base<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150{
Howard Hinnantdcb73a32013-06-23 21:17:24 +00001151 __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __start_ / __block_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152 return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
1153}
1154
1155template <class _Tp, class _Allocator>
1156typename __deque_base<_Tp, _Allocator>::iterator
Howard Hinnantda2df912011-06-02 16:10:22 +00001157__deque_base<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001158{
1159 size_type __p = size() + __start_;
1160 __map_pointer __mp = __map_.begin() + __p / __block_size;
1161 return iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
1162}
1163
1164template <class _Tp, class _Allocator>
1165typename __deque_base<_Tp, _Allocator>::const_iterator
Howard Hinnantda2df912011-06-02 16:10:22 +00001166__deque_base<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001167{
1168 size_type __p = size() + __start_;
Howard Hinnantdcb73a32013-06-23 21:17:24 +00001169 __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __p / __block_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001170 return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
1171}
1172
1173template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001174inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001175__deque_base<_Tp, _Allocator>::__deque_base()
Howard Hinnantad979ba2011-06-03 15:16:49 +00001176 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001177 : __start_(0), __size_(0, __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001178
1179template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001180inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001181__deque_base<_Tp, _Allocator>::__deque_base(const allocator_type& __a)
1182 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a) {}
1183
1184template <class _Tp, class _Allocator>
1185__deque_base<_Tp, _Allocator>::~__deque_base()
1186{
1187 clear();
1188 typename __map::iterator __i = __map_.begin();
1189 typename __map::iterator __e = __map_.end();
1190 for (; __i != __e; ++__i)
1191 __alloc_traits::deallocate(__alloc(), *__i, __block_size);
1192}
1193
Eric Fiselier212e3f22017-04-16 03:17:01 +00001194#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001195
1196template <class _Tp, class _Allocator>
1197__deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001198 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001199 : __map_(_VSTD::move(__c.__map_)),
1200 __start_(_VSTD::move(__c.__start_)),
1201 __size_(_VSTD::move(__c.__size_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001202{
1203 __c.__start_ = 0;
1204 __c.size() = 0;
1205}
1206
1207template <class _Tp, class _Allocator>
1208__deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c, const allocator_type& __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001209 : __map_(_VSTD::move(__c.__map_), __pointer_allocator(__a)),
1210 __start_(_VSTD::move(__c.__start_)),
1211 __size_(_VSTD::move(__c.size()), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001212{
1213 if (__a == __c.__alloc())
1214 {
1215 __c.__start_ = 0;
1216 __c.size() = 0;
1217 }
1218 else
1219 {
1220 __map_.clear();
1221 __start_ = 0;
1222 size() = 0;
1223 }
1224}
1225
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001226#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001227
1228template <class _Tp, class _Allocator>
1229void
1230__deque_base<_Tp, _Allocator>::swap(__deque_base& __c)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001231#if _LIBCPP_STD_VER >= 14
1232 _NOEXCEPT
1233#else
Louis Dionne72f24392018-12-12 23:58:25 +00001234 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001235 __is_nothrow_swappable<allocator_type>::value)
1236#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001237{
1238 __map_.swap(__c.__map_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001239 _VSTD::swap(__start_, __c.__start_);
1240 _VSTD::swap(size(), __c.size());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05001241 _VSTD::__swap_allocator(__alloc(), __c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001242}
1243
1244template <class _Tp, class _Allocator>
1245void
Howard Hinnantda2df912011-06-02 16:10:22 +00001246__deque_base<_Tp, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001247{
1248 allocator_type& __a = __alloc();
1249 for (iterator __i = begin(), __e = end(); __i != __e; ++__i)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001250 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001251 size() = 0;
1252 while (__map_.size() > 2)
1253 {
1254 __alloc_traits::deallocate(__a, __map_.front(), __block_size);
1255 __map_.pop_front();
1256 }
1257 switch (__map_.size())
1258 {
1259 case 1:
1260 __start_ = __block_size / 2;
1261 break;
1262 case 2:
1263 __start_ = __block_size;
1264 break;
1265 }
1266}
1267
Marshall Clow65cd4c62015-02-18 17:24:08 +00001268template <class _Tp, class _Allocator /*= allocator<_Tp>*/>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001269class _LIBCPP_TEMPLATE_VIS deque
Howard Hinnantc51e1022010-05-11 19:42:16 +00001270 : private __deque_base<_Tp, _Allocator>
1271{
1272public:
1273 // types:
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001274
Howard Hinnantc51e1022010-05-11 19:42:16 +00001275 typedef _Tp value_type;
1276 typedef _Allocator allocator_type;
1277
Marshall Clow5128cf32015-11-26 01:24:04 +00001278 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
1279 "Allocator::value_type must be same type as value_type");
1280
Howard Hinnantc51e1022010-05-11 19:42:16 +00001281 typedef __deque_base<value_type, allocator_type> __base;
1282
1283 typedef typename __base::__alloc_traits __alloc_traits;
1284 typedef typename __base::reference reference;
1285 typedef typename __base::const_reference const_reference;
1286 typedef typename __base::iterator iterator;
1287 typedef typename __base::const_iterator const_iterator;
1288 typedef typename __base::size_type size_type;
1289 typedef typename __base::difference_type difference_type;
1290
1291 typedef typename __base::pointer pointer;
1292 typedef typename __base::const_pointer const_pointer;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001293 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
1294 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001295
Eric Fiselierc52db212019-08-12 07:51:05 +00001296 using typename __base::__deque_range;
1297 using typename __base::__deque_block_range;
1298 using typename __base::_ConstructTransaction;
1299
Howard Hinnantc51e1022010-05-11 19:42:16 +00001300 // construct/copy/destroy:
Howard Hinnantad979ba2011-06-03 15:16:49 +00001301 _LIBCPP_INLINE_VISIBILITY
1302 deque()
1303 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
1304 {}
Marshall Clow7ed23a72014-03-05 19:06:20 +00001305 _LIBCPP_INLINE_VISIBILITY explicit deque(const allocator_type& __a) : __base(__a) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001306 explicit deque(size_type __n);
Marshall Clowbc4fcb42013-09-07 16:16:19 +00001307#if _LIBCPP_STD_VER > 11
1308 explicit deque(size_type __n, const _Allocator& __a);
1309#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001310 deque(size_type __n, const value_type& __v);
1311 deque(size_type __n, const value_type& __v, const allocator_type& __a);
1312 template <class _InputIter>
1313 deque(_InputIter __f, _InputIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001314 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001315 template <class _InputIter>
1316 deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001317 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001318 deque(const deque& __c);
1319 deque(const deque& __c, const allocator_type& __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001320
1321 deque& operator=(const deque& __c);
Eric Fiselier212e3f22017-04-16 03:17:01 +00001322
1323#ifndef _LIBCPP_CXX03_LANG
1324 deque(initializer_list<value_type> __il);
1325 deque(initializer_list<value_type> __il, const allocator_type& __a);
1326
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001327 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001328 deque& operator=(initializer_list<value_type> __il) {assign(__il); return *this;}
1329
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001330 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantca2fc222011-06-02 20:00:14 +00001331 deque(deque&& __c) _NOEXCEPT_(is_nothrow_move_constructible<__base>::value);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001332 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001333 deque(deque&& __c, const allocator_type& __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001334 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantca2fc222011-06-02 20:00:14 +00001335 deque& operator=(deque&& __c)
Howard Hinnant4931e092011-06-02 21:38:57 +00001336 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1337 is_nothrow_move_assignable<allocator_type>::value);
Eric Fiselier212e3f22017-04-16 03:17:01 +00001338
1339 _LIBCPP_INLINE_VISIBILITY
1340 void assign(initializer_list<value_type> __il) {assign(__il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001341#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001342
1343 template <class _InputIter>
1344 void assign(_InputIter __f, _InputIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001345 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value &&
1346 !__is_cpp17_random_access_iterator<_InputIter>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001347 template <class _RAIter>
1348 void assign(_RAIter __f, _RAIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001349 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001350 void assign(size_type __n, const value_type& __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001351
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001352 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001353 allocator_type get_allocator() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001354
1355 // iterators:
1356
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001357 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001358 iterator begin() _NOEXCEPT {return __base::begin();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001359 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001360 const_iterator begin() const _NOEXCEPT {return __base::begin();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001361 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001362 iterator end() _NOEXCEPT {return __base::end();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001363 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001364 const_iterator end() const _NOEXCEPT {return __base::end();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001365
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001367 reverse_iterator rbegin() _NOEXCEPT
1368 {return reverse_iterator(__base::end());}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001369 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001370 const_reverse_iterator rbegin() const _NOEXCEPT
1371 {return const_reverse_iterator(__base::end());}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001372 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001373 reverse_iterator rend() _NOEXCEPT
1374 {return reverse_iterator(__base::begin());}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001375 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001376 const_reverse_iterator rend() const _NOEXCEPT
1377 {return const_reverse_iterator(__base::begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001378
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001379 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001380 const_iterator cbegin() const _NOEXCEPT
1381 {return __base::begin();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001383 const_iterator cend() const _NOEXCEPT
1384 {return __base::end();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001385 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001386 const_reverse_iterator crbegin() const _NOEXCEPT
1387 {return const_reverse_iterator(__base::end());}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001388 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001389 const_reverse_iterator crend() const _NOEXCEPT
1390 {return const_reverse_iterator(__base::begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001391
1392 // capacity:
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001393 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001394 size_type size() const _NOEXCEPT {return __base::size();}
1395 _LIBCPP_INLINE_VISIBILITY
1396 size_type max_size() const _NOEXCEPT
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001397 {return _VSTD::min<size_type>(
Eric Fiselierb5d9f442016-11-23 01:18:56 +00001398 __alloc_traits::max_size(__base::__alloc()),
1399 numeric_limits<difference_type>::max());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001400 void resize(size_type __n);
1401 void resize(size_type __n, const value_type& __v);
Howard Hinnant4931e092011-06-02 21:38:57 +00001402 void shrink_to_fit() _NOEXCEPT;
Marshall Clow425f5752017-11-15 05:51:26 +00001403 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001404 bool empty() const _NOEXCEPT {return __base::size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001405
1406 // element access:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001407 _LIBCPP_INLINE_VISIBILITY
Marshall Clow8d7c9f12019-03-14 21:56:57 +00001408 reference operator[](size_type __i) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001409 _LIBCPP_INLINE_VISIBILITY
Marshall Clow8d7c9f12019-03-14 21:56:57 +00001410 const_reference operator[](size_type __i) const _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001411 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001412 reference at(size_type __i);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001413 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001414 const_reference at(size_type __i) const;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001415 _LIBCPP_INLINE_VISIBILITY
Marshall Clow05cf6692019-03-19 03:30:07 +00001416 reference front() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001417 _LIBCPP_INLINE_VISIBILITY
Marshall Clow05cf6692019-03-19 03:30:07 +00001418 const_reference front() const _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001419 _LIBCPP_INLINE_VISIBILITY
Marshall Clow05cf6692019-03-19 03:30:07 +00001420 reference back() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001421 _LIBCPP_INLINE_VISIBILITY
Marshall Clow05cf6692019-03-19 03:30:07 +00001422 const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001423
1424 // 23.2.2.3 modifiers:
1425 void push_front(const value_type& __v);
1426 void push_back(const value_type& __v);
Eric Fiselier212e3f22017-04-16 03:17:01 +00001427#ifndef _LIBCPP_CXX03_LANG
Marshall Clowea52cc42017-01-24 23:09:12 +00001428#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001429 template <class... _Args> reference emplace_front(_Args&&... __args);
Marshall Clowea52cc42017-01-24 23:09:12 +00001430 template <class... _Args> reference emplace_back (_Args&&... __args);
1431#else
1432 template <class... _Args> void emplace_front(_Args&&... __args);
1433 template <class... _Args> void emplace_back (_Args&&... __args);
1434#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001435 template <class... _Args> iterator emplace(const_iterator __p, _Args&&... __args);
Eric Fiselier212e3f22017-04-16 03:17:01 +00001436
Howard Hinnantc51e1022010-05-11 19:42:16 +00001437 void push_front(value_type&& __v);
1438 void push_back(value_type&& __v);
1439 iterator insert(const_iterator __p, value_type&& __v);
Eric Fiselier212e3f22017-04-16 03:17:01 +00001440
1441 _LIBCPP_INLINE_VISIBILITY
1442 iterator insert(const_iterator __p, initializer_list<value_type> __il)
1443 {return insert(__p, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001444#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001445 iterator insert(const_iterator __p, const value_type& __v);
1446 iterator insert(const_iterator __p, size_type __n, const value_type& __v);
1447 template <class _InputIter>
Marshall Clow6b612cf2015-01-22 18:33:29 +00001448 iterator insert(const_iterator __p, _InputIter __f, _InputIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001449 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value
1450 &&!__is_cpp17_forward_iterator<_InputIter>::value>::type* = 0);
Marshall Clow6b612cf2015-01-22 18:33:29 +00001451 template <class _ForwardIterator>
1452 iterator insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001453 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value
1454 &&!__is_cpp17_bidirectional_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001455 template <class _BiIter>
Marshall Clow6b612cf2015-01-22 18:33:29 +00001456 iterator insert(const_iterator __p, _BiIter __f, _BiIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001457 typename enable_if<__is_cpp17_bidirectional_iterator<_BiIter>::value>::type* = 0);
Eric Fiselier212e3f22017-04-16 03:17:01 +00001458
Howard Hinnantc51e1022010-05-11 19:42:16 +00001459 void pop_front();
1460 void pop_back();
1461 iterator erase(const_iterator __p);
1462 iterator erase(const_iterator __f, const_iterator __l);
1463
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001464 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantca2fc222011-06-02 20:00:14 +00001465 void swap(deque& __c)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001466#if _LIBCPP_STD_VER >= 14
1467 _NOEXCEPT;
1468#else
Howard Hinnantca2fc222011-06-02 20:00:14 +00001469 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1470 __is_nothrow_swappable<allocator_type>::value);
Marshall Clow8982dcd2015-07-13 20:04:56 +00001471#endif
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001472 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001473 void clear() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001474
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001475 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001476 bool __invariants() const {return __base::__invariants();}
Eric Fiselier27e03622019-08-01 23:11:18 +00001477
Howard Hinnantdcb73a32013-06-23 21:17:24 +00001478 typedef typename __base::__map_const_pointer __map_const_pointer;
1479
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001480 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001481 static size_type __recommend_blocks(size_type __n)
1482 {
1483 return __n / __base::__block_size + (__n % __base::__block_size != 0);
1484 }
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001485 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001486 size_type __capacity() const
1487 {
1488 return __base::__map_.size() == 0 ? 0 : __base::__map_.size() * __base::__block_size - 1;
1489 }
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001490 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier27e03622019-08-01 23:11:18 +00001491 size_type __block_count() const
1492 {
1493 return __base::__map_.size();
1494 }
1495
1496 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001497 size_type __front_spare() const
1498 {
1499 return __base::__start_;
1500 }
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001501 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier27e03622019-08-01 23:11:18 +00001502 size_type __front_spare_blocks() const {
1503 return __front_spare() / __base::__block_size;
1504 }
1505 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001506 size_type __back_spare() const
1507 {
1508 return __capacity() - (__base::__start_ + __base::size());
1509 }
Eric Fiselier27e03622019-08-01 23:11:18 +00001510 _LIBCPP_INLINE_VISIBILITY
1511 size_type __back_spare_blocks() const {
1512 return __back_spare() / __base::__block_size;
1513 }
1514
1515 private:
1516 _LIBCPP_INLINE_VISIBILITY
1517 bool __maybe_remove_front_spare(bool __keep_one = true) {
1518 if (__front_spare_blocks() >= 2 || (!__keep_one && __front_spare_blocks())) {
1519 __alloc_traits::deallocate(__base::__alloc(), __base::__map_.front(),
1520 __base::__block_size);
1521 __base::__map_.pop_front();
1522 __base::__start_ -= __base::__block_size;
1523 return true;
1524 }
1525 return false;
1526 }
1527
1528 _LIBCPP_INLINE_VISIBILITY
1529 bool __maybe_remove_back_spare(bool __keep_one = true) {
1530 if (__back_spare_blocks() >= 2 || (!__keep_one && __back_spare_blocks())) {
1531 __alloc_traits::deallocate(__base::__alloc(), __base::__map_.back(),
1532 __base::__block_size);
1533 __base::__map_.pop_back();
1534 return true;
1535 }
1536 return false;
1537 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001538
1539 template <class _InpIter>
1540 void __append(_InpIter __f, _InpIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001541 typename enable_if<__is_cpp17_input_iterator<_InpIter>::value &&
1542 !__is_cpp17_forward_iterator<_InpIter>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001543 template <class _ForIter>
1544 void __append(_ForIter __f, _ForIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001545 typename enable_if<__is_cpp17_forward_iterator<_ForIter>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001546 void __append(size_type __n);
1547 void __append(size_type __n, const value_type& __v);
1548 void __erase_to_end(const_iterator __f);
1549 void __add_front_capacity();
1550 void __add_front_capacity(size_type __n);
1551 void __add_back_capacity();
1552 void __add_back_capacity(size_type __n);
1553 iterator __move_and_check(iterator __f, iterator __l, iterator __r,
1554 const_pointer& __vt);
1555 iterator __move_backward_and_check(iterator __f, iterator __l, iterator __r,
1556 const_pointer& __vt);
1557 void __move_construct_and_check(iterator __f, iterator __l,
1558 iterator __r, const_pointer& __vt);
1559 void __move_construct_backward_and_check(iterator __f, iterator __l,
1560 iterator __r, const_pointer& __vt);
1561
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001562 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001563 void __copy_assign_alloc(const deque& __c)
1564 {__copy_assign_alloc(__c, integral_constant<bool,
1565 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1566
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001567 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001568 void __copy_assign_alloc(const deque& __c, true_type)
1569 {
1570 if (__base::__alloc() != __c.__alloc())
1571 {
1572 clear();
1573 shrink_to_fit();
1574 }
1575 __base::__alloc() = __c.__alloc();
1576 __base::__map_.__alloc() = __c.__map_.__alloc();
1577 }
1578
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001579 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001580 void __copy_assign_alloc(const deque&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001581 {}
1582
Howard Hinnant4931e092011-06-02 21:38:57 +00001583 void __move_assign(deque& __c, true_type)
1584 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001585 void __move_assign(deque& __c, false_type);
1586};
1587
Marshall Clow4cc3a452018-05-18 23:44:13 +00001588#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
1589template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001590 class _Alloc = allocator<__iter_value_type<_InputIterator>>,
1591 class = _EnableIf<__is_allocator<_Alloc>::value>
Marshall Clow4cc3a452018-05-18 23:44:13 +00001592 >
1593deque(_InputIterator, _InputIterator)
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001594 -> deque<__iter_value_type<_InputIterator>, _Alloc>;
Marshall Clow4cc3a452018-05-18 23:44:13 +00001595
1596template<class _InputIterator,
1597 class _Alloc,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001598 class = _EnableIf<__is_allocator<_Alloc>::value>
Marshall Clow4cc3a452018-05-18 23:44:13 +00001599 >
1600deque(_InputIterator, _InputIterator, _Alloc)
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001601 -> deque<__iter_value_type<_InputIterator>, _Alloc>;
Marshall Clow4cc3a452018-05-18 23:44:13 +00001602#endif
1603
1604
Howard Hinnantc51e1022010-05-11 19:42:16 +00001605template <class _Tp, class _Allocator>
1606deque<_Tp, _Allocator>::deque(size_type __n)
1607{
1608 if (__n > 0)
1609 __append(__n);
1610}
1611
Marshall Clowbc4fcb42013-09-07 16:16:19 +00001612#if _LIBCPP_STD_VER > 11
1613template <class _Tp, class _Allocator>
1614deque<_Tp, _Allocator>::deque(size_type __n, const _Allocator& __a)
1615 : __base(__a)
1616{
1617 if (__n > 0)
1618 __append(__n);
1619}
1620#endif
1621
Howard Hinnantc51e1022010-05-11 19:42:16 +00001622template <class _Tp, class _Allocator>
1623deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v)
1624{
1625 if (__n > 0)
1626 __append(__n, __v);
1627}
1628
1629template <class _Tp, class _Allocator>
1630deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v, const allocator_type& __a)
1631 : __base(__a)
1632{
1633 if (__n > 0)
1634 __append(__n, __v);
1635}
1636
1637template <class _Tp, class _Allocator>
1638template <class _InputIter>
1639deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001640 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001641{
1642 __append(__f, __l);
1643}
1644
1645template <class _Tp, class _Allocator>
1646template <class _InputIter>
1647deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001648 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001649 : __base(__a)
1650{
1651 __append(__f, __l);
1652}
1653
1654template <class _Tp, class _Allocator>
1655deque<_Tp, _Allocator>::deque(const deque& __c)
1656 : __base(__alloc_traits::select_on_container_copy_construction(__c.__alloc()))
1657{
1658 __append(__c.begin(), __c.end());
1659}
1660
1661template <class _Tp, class _Allocator>
1662deque<_Tp, _Allocator>::deque(const deque& __c, const allocator_type& __a)
1663 : __base(__a)
1664{
1665 __append(__c.begin(), __c.end());
1666}
1667
Eric Fiselier212e3f22017-04-16 03:17:01 +00001668template <class _Tp, class _Allocator>
1669deque<_Tp, _Allocator>&
1670deque<_Tp, _Allocator>::operator=(const deque& __c)
1671{
1672 if (this != &__c)
1673 {
1674 __copy_assign_alloc(__c);
1675 assign(__c.begin(), __c.end());
1676 }
1677 return *this;
1678}
1679
1680#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00001681
Howard Hinnantc51e1022010-05-11 19:42:16 +00001682template <class _Tp, class _Allocator>
1683deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il)
1684{
1685 __append(__il.begin(), __il.end());
1686}
1687
1688template <class _Tp, class _Allocator>
1689deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il, const allocator_type& __a)
1690 : __base(__a)
1691{
1692 __append(__il.begin(), __il.end());
1693}
1694
Howard Hinnantc51e1022010-05-11 19:42:16 +00001695template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001696inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001697deque<_Tp, _Allocator>::deque(deque&& __c)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001698 _NOEXCEPT_(is_nothrow_move_constructible<__base>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001699 : __base(_VSTD::move(__c))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001700{
1701}
1702
1703template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001704inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001705deque<_Tp, _Allocator>::deque(deque&& __c, const allocator_type& __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001706 : __base(_VSTD::move(__c), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001707{
1708 if (__a != __c.__alloc())
1709 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001710 typedef move_iterator<iterator> _Ip;
1711 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001712 }
1713}
1714
1715template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001716inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001717deque<_Tp, _Allocator>&
1718deque<_Tp, _Allocator>::operator=(deque&& __c)
Howard Hinnant4931e092011-06-02 21:38:57 +00001719 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1720 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001721{
1722 __move_assign(__c, integral_constant<bool,
1723 __alloc_traits::propagate_on_container_move_assignment::value>());
1724 return *this;
1725}
1726
1727template <class _Tp, class _Allocator>
1728void
1729deque<_Tp, _Allocator>::__move_assign(deque& __c, false_type)
1730{
1731 if (__base::__alloc() != __c.__alloc())
1732 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001733 typedef move_iterator<iterator> _Ip;
1734 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001735 }
1736 else
1737 __move_assign(__c, true_type());
1738}
1739
1740template <class _Tp, class _Allocator>
1741void
1742deque<_Tp, _Allocator>::__move_assign(deque& __c, true_type)
Howard Hinnant4931e092011-06-02 21:38:57 +00001743 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001744{
1745 clear();
1746 shrink_to_fit();
1747 __base::__move_assign(__c);
1748}
1749
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001750#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001751
1752template <class _Tp, class _Allocator>
1753template <class _InputIter>
1754void
1755deque<_Tp, _Allocator>::assign(_InputIter __f, _InputIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001756 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value &&
1757 !__is_cpp17_random_access_iterator<_InputIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001758{
1759 iterator __i = __base::begin();
1760 iterator __e = __base::end();
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001761 for (; __f != __l && __i != __e; ++__f, (void) ++__i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001762 *__i = *__f;
1763 if (__f != __l)
1764 __append(__f, __l);
1765 else
1766 __erase_to_end(__i);
1767}
1768
1769template <class _Tp, class _Allocator>
1770template <class _RAIter>
1771void
1772deque<_Tp, _Allocator>::assign(_RAIter __f, _RAIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001773 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001774{
1775 if (static_cast<size_type>(__l - __f) > __base::size())
1776 {
1777 _RAIter __m = __f + __base::size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001778 _VSTD::copy(__f, __m, __base::begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001779 __append(__m, __l);
1780 }
1781 else
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001782 __erase_to_end(_VSTD::copy(__f, __l, __base::begin()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001783}
1784
1785template <class _Tp, class _Allocator>
1786void
1787deque<_Tp, _Allocator>::assign(size_type __n, const value_type& __v)
1788{
1789 if (__n > __base::size())
1790 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001791 _VSTD::fill_n(__base::begin(), __base::size(), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001792 __n -= __base::size();
1793 __append(__n, __v);
1794 }
1795 else
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001796 __erase_to_end(_VSTD::fill_n(__base::begin(), __n, __v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001797}
1798
1799template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001800inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001801_Allocator
Howard Hinnantda2df912011-06-02 16:10:22 +00001802deque<_Tp, _Allocator>::get_allocator() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001803{
1804 return __base::__alloc();
1805}
1806
1807template <class _Tp, class _Allocator>
1808void
1809deque<_Tp, _Allocator>::resize(size_type __n)
1810{
1811 if (__n > __base::size())
1812 __append(__n - __base::size());
1813 else if (__n < __base::size())
1814 __erase_to_end(__base::begin() + __n);
1815}
1816
1817template <class _Tp, class _Allocator>
1818void
1819deque<_Tp, _Allocator>::resize(size_type __n, const value_type& __v)
1820{
1821 if (__n > __base::size())
1822 __append(__n - __base::size(), __v);
1823 else if (__n < __base::size())
1824 __erase_to_end(__base::begin() + __n);
1825}
1826
1827template <class _Tp, class _Allocator>
1828void
Howard Hinnant4931e092011-06-02 21:38:57 +00001829deque<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001830{
1831 allocator_type& __a = __base::__alloc();
1832 if (empty())
1833 {
1834 while (__base::__map_.size() > 0)
1835 {
1836 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
1837 __base::__map_.pop_back();
1838 }
1839 __base::__start_ = 0;
1840 }
1841 else
1842 {
Eric Fiselier27e03622019-08-01 23:11:18 +00001843 __maybe_remove_front_spare(/*__keep_one=*/false);
1844 __maybe_remove_back_spare(/*__keep_one=*/false);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001845 }
1846 __base::__map_.shrink_to_fit();
1847}
1848
1849template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001850inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001851typename deque<_Tp, _Allocator>::reference
Marshall Clow8d7c9f12019-03-14 21:56:57 +00001852deque<_Tp, _Allocator>::operator[](size_type __i) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001853{
1854 size_type __p = __base::__start_ + __i;
1855 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1856}
1857
1858template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001859inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001860typename deque<_Tp, _Allocator>::const_reference
Marshall Clow8d7c9f12019-03-14 21:56:57 +00001861deque<_Tp, _Allocator>::operator[](size_type __i) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001862{
1863 size_type __p = __base::__start_ + __i;
1864 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1865}
1866
1867template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001868inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001869typename deque<_Tp, _Allocator>::reference
1870deque<_Tp, _Allocator>::at(size_type __i)
1871{
1872 if (__i >= __base::size())
1873 __base::__throw_out_of_range();
1874 size_type __p = __base::__start_ + __i;
1875 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1876}
1877
1878template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001879inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001880typename deque<_Tp, _Allocator>::const_reference
1881deque<_Tp, _Allocator>::at(size_type __i) const
1882{
1883 if (__i >= __base::size())
1884 __base::__throw_out_of_range();
1885 size_type __p = __base::__start_ + __i;
1886 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1887}
1888
1889template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001890inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001891typename deque<_Tp, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00001892deque<_Tp, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001893{
1894 return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1895 + __base::__start_ % __base::__block_size);
1896}
1897
1898template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001899inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001900typename deque<_Tp, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00001901deque<_Tp, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001902{
1903 return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1904 + __base::__start_ % __base::__block_size);
1905}
1906
1907template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001908inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001909typename deque<_Tp, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00001910deque<_Tp, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001911{
1912 size_type __p = __base::size() + __base::__start_ - 1;
1913 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1914}
1915
1916template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001917inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001918typename deque<_Tp, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00001919deque<_Tp, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001920{
1921 size_type __p = __base::size() + __base::__start_ - 1;
1922 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1923}
1924
1925template <class _Tp, class _Allocator>
1926void
1927deque<_Tp, _Allocator>::push_back(const value_type& __v)
1928{
1929 allocator_type& __a = __base::__alloc();
1930 if (__back_spare() == 0)
1931 __add_back_capacity();
1932 // __back_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001933 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001934 ++__base::size();
1935}
1936
Eric Fiselier212e3f22017-04-16 03:17:01 +00001937template <class _Tp, class _Allocator>
1938void
1939deque<_Tp, _Allocator>::push_front(const value_type& __v)
1940{
1941 allocator_type& __a = __base::__alloc();
1942 if (__front_spare() == 0)
1943 __add_front_capacity();
1944 // __front_spare() >= 1
1945 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v);
1946 --__base::__start_;
1947 ++__base::size();
1948}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001949
Eric Fiselier212e3f22017-04-16 03:17:01 +00001950#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001951template <class _Tp, class _Allocator>
1952void
1953deque<_Tp, _Allocator>::push_back(value_type&& __v)
1954{
1955 allocator_type& __a = __base::__alloc();
1956 if (__back_spare() == 0)
1957 __add_back_capacity();
1958 // __back_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001959 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001960 ++__base::size();
1961}
1962
1963template <class _Tp, class _Allocator>
1964template <class... _Args>
Marshall Clowea52cc42017-01-24 23:09:12 +00001965#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001966typename deque<_Tp, _Allocator>::reference
Marshall Clowea52cc42017-01-24 23:09:12 +00001967#else
1968void
1969#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001970deque<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1971{
1972 allocator_type& __a = __base::__alloc();
1973 if (__back_spare() == 0)
1974 __add_back_capacity();
1975 // __back_spare() >= 1
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001976 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()),
1977 _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001978 ++__base::size();
Marshall Clowea52cc42017-01-24 23:09:12 +00001979#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001980 return *--__base::end();
Marshall Clowea52cc42017-01-24 23:09:12 +00001981#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001982}
1983
Howard Hinnantc51e1022010-05-11 19:42:16 +00001984template <class _Tp, class _Allocator>
1985void
1986deque<_Tp, _Allocator>::push_front(value_type&& __v)
1987{
1988 allocator_type& __a = __base::__alloc();
1989 if (__front_spare() == 0)
1990 __add_front_capacity();
1991 // __front_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001992 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001993 --__base::__start_;
1994 ++__base::size();
1995}
1996
Howard Hinnant74279a52010-09-04 23:28:19 +00001997
Howard Hinnantc51e1022010-05-11 19:42:16 +00001998template <class _Tp, class _Allocator>
1999template <class... _Args>
Marshall Clowea52cc42017-01-24 23:09:12 +00002000#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002001typename deque<_Tp, _Allocator>::reference
Marshall Clowea52cc42017-01-24 23:09:12 +00002002#else
2003void
2004#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002005deque<_Tp, _Allocator>::emplace_front(_Args&&... __args)
2006{
2007 allocator_type& __a = __base::__alloc();
2008 if (__front_spare() == 0)
2009 __add_front_capacity();
2010 // __front_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002011 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002012 --__base::__start_;
2013 ++__base::size();
Marshall Clowea52cc42017-01-24 23:09:12 +00002014#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002015 return *__base::begin();
Marshall Clowea52cc42017-01-24 23:09:12 +00002016#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002017}
2018
Eric Fiselier212e3f22017-04-16 03:17:01 +00002019template <class _Tp, class _Allocator>
2020typename deque<_Tp, _Allocator>::iterator
2021deque<_Tp, _Allocator>::insert(const_iterator __p, value_type&& __v)
2022{
2023 size_type __pos = __p - __base::begin();
2024 size_type __to_end = __base::size() - __pos;
2025 allocator_type& __a = __base::__alloc();
2026 if (__pos < __to_end)
2027 { // insert by shifting things backward
2028 if (__front_spare() == 0)
2029 __add_front_capacity();
2030 // __front_spare() >= 1
2031 if (__pos == 0)
2032 {
2033 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v));
2034 --__base::__start_;
2035 ++__base::size();
2036 }
2037 else
2038 {
2039 iterator __b = __base::begin();
2040 iterator __bm1 = _VSTD::prev(__b);
2041 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
2042 --__base::__start_;
2043 ++__base::size();
2044 if (__pos > 1)
2045 __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
2046 *__b = _VSTD::move(__v);
2047 }
2048 }
2049 else
2050 { // insert by shifting things forward
2051 if (__back_spare() == 0)
2052 __add_back_capacity();
2053 // __back_capacity >= 1
2054 size_type __de = __base::size() - __pos;
2055 if (__de == 0)
2056 {
2057 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v));
2058 ++__base::size();
2059 }
2060 else
2061 {
2062 iterator __e = __base::end();
2063 iterator __em1 = _VSTD::prev(__e);
2064 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
2065 ++__base::size();
2066 if (__de > 1)
2067 __e = _VSTD::move_backward(__e - __de, __em1, __e);
2068 *--__e = _VSTD::move(__v);
2069 }
2070 }
2071 return __base::begin() + __pos;
2072}
2073
2074template <class _Tp, class _Allocator>
2075template <class... _Args>
2076typename deque<_Tp, _Allocator>::iterator
2077deque<_Tp, _Allocator>::emplace(const_iterator __p, _Args&&... __args)
2078{
2079 size_type __pos = __p - __base::begin();
2080 size_type __to_end = __base::size() - __pos;
2081 allocator_type& __a = __base::__alloc();
2082 if (__pos < __to_end)
2083 { // insert by shifting things backward
2084 if (__front_spare() == 0)
2085 __add_front_capacity();
2086 // __front_spare() >= 1
2087 if (__pos == 0)
2088 {
2089 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...);
2090 --__base::__start_;
2091 ++__base::size();
2092 }
2093 else
2094 {
2095 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
2096 iterator __b = __base::begin();
2097 iterator __bm1 = _VSTD::prev(__b);
2098 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
2099 --__base::__start_;
2100 ++__base::size();
2101 if (__pos > 1)
2102 __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
2103 *__b = _VSTD::move(__tmp.get());
2104 }
2105 }
2106 else
2107 { // insert by shifting things forward
2108 if (__back_spare() == 0)
2109 __add_back_capacity();
2110 // __back_capacity >= 1
2111 size_type __de = __base::size() - __pos;
2112 if (__de == 0)
2113 {
2114 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::forward<_Args>(__args)...);
2115 ++__base::size();
2116 }
2117 else
2118 {
2119 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
2120 iterator __e = __base::end();
2121 iterator __em1 = _VSTD::prev(__e);
2122 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
2123 ++__base::size();
2124 if (__de > 1)
2125 __e = _VSTD::move_backward(__e - __de, __em1, __e);
2126 *--__e = _VSTD::move(__tmp.get());
2127 }
2128 }
2129 return __base::begin() + __pos;
2130}
2131
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002132#endif // _LIBCPP_CXX03_LANG
Eric Fiselier212e3f22017-04-16 03:17:01 +00002133
Howard Hinnantc51e1022010-05-11 19:42:16 +00002134
2135template <class _Tp, class _Allocator>
2136typename deque<_Tp, _Allocator>::iterator
2137deque<_Tp, _Allocator>::insert(const_iterator __p, const value_type& __v)
2138{
2139 size_type __pos = __p - __base::begin();
2140 size_type __to_end = __base::size() - __pos;
2141 allocator_type& __a = __base::__alloc();
2142 if (__pos < __to_end)
2143 { // insert by shifting things backward
2144 if (__front_spare() == 0)
2145 __add_front_capacity();
2146 // __front_spare() >= 1
2147 if (__pos == 0)
2148 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002149 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002150 --__base::__start_;
2151 ++__base::size();
2152 }
2153 else
2154 {
2155 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2156 iterator __b = __base::begin();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002157 iterator __bm1 = _VSTD::prev(__b);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002158 if (__vt == pointer_traits<const_pointer>::pointer_to(*__b))
2159 __vt = pointer_traits<const_pointer>::pointer_to(*__bm1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002160 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002161 --__base::__start_;
2162 ++__base::size();
2163 if (__pos > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002164 __b = __move_and_check(_VSTD::next(__b), __b + __pos, __b, __vt);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002165 *__b = *__vt;
2166 }
2167 }
2168 else
2169 { // insert by shifting things forward
2170 if (__back_spare() == 0)
2171 __add_back_capacity();
2172 // __back_capacity >= 1
2173 size_type __de = __base::size() - __pos;
2174 if (__de == 0)
2175 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002176 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002177 ++__base::size();
2178 }
2179 else
2180 {
2181 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2182 iterator __e = __base::end();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002183 iterator __em1 = _VSTD::prev(__e);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002184 if (__vt == pointer_traits<const_pointer>::pointer_to(*__em1))
2185 __vt = pointer_traits<const_pointer>::pointer_to(*__e);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002186 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002187 ++__base::size();
2188 if (__de > 1)
2189 __e = __move_backward_and_check(__e - __de, __em1, __e, __vt);
2190 *--__e = *__vt;
2191 }
2192 }
2193 return __base::begin() + __pos;
2194}
2195
Howard Hinnantc51e1022010-05-11 19:42:16 +00002196template <class _Tp, class _Allocator>
2197typename deque<_Tp, _Allocator>::iterator
2198deque<_Tp, _Allocator>::insert(const_iterator __p, size_type __n, const value_type& __v)
2199{
2200 size_type __pos = __p - __base::begin();
2201 size_type __to_end = __base::size() - __pos;
2202 allocator_type& __a = __base::__alloc();
2203 if (__pos < __to_end)
2204 { // insert by shifting things backward
2205 if (__n > __front_spare())
2206 __add_front_capacity(__n - __front_spare());
2207 // __n <= __front_spare()
Howard Hinnantc51e1022010-05-11 19:42:16 +00002208 iterator __old_begin = __base::begin();
2209 iterator __i = __old_begin;
2210 if (__n > __pos)
2211 {
2212 for (size_type __m = __n - __pos; __m; --__m, --__base::__start_, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002213 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002214 __n = __pos;
2215 }
2216 if (__n > 0)
2217 {
2218 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2219 iterator __obn = __old_begin + __n;
2220 __move_construct_backward_and_check(__old_begin, __obn, __i, __vt);
2221 if (__n < __pos)
2222 __old_begin = __move_and_check(__obn, __old_begin + __pos, __old_begin, __vt);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002223 _VSTD::fill_n(__old_begin, __n, *__vt);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002224 }
2225 }
2226 else
2227 { // insert by shifting things forward
2228 size_type __back_capacity = __back_spare();
2229 if (__n > __back_capacity)
2230 __add_back_capacity(__n - __back_capacity);
2231 // __n <= __back_capacity
Howard Hinnantc51e1022010-05-11 19:42:16 +00002232 iterator __old_end = __base::end();
2233 iterator __i = __old_end;
2234 size_type __de = __base::size() - __pos;
2235 if (__n > __de)
2236 {
2237 for (size_type __m = __n - __de; __m; --__m, ++__i, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002238 __alloc_traits::construct(__a, _VSTD::addressof(*__i), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002239 __n = __de;
2240 }
2241 if (__n > 0)
2242 {
2243 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2244 iterator __oen = __old_end - __n;
2245 __move_construct_and_check(__oen, __old_end, __i, __vt);
2246 if (__n < __de)
2247 __old_end = __move_backward_and_check(__old_end - __de, __oen, __old_end, __vt);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002248 _VSTD::fill_n(__old_end - __n, __n, *__vt);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002249 }
2250 }
2251 return __base::begin() + __pos;
2252}
2253
2254template <class _Tp, class _Allocator>
2255template <class _InputIter>
2256typename deque<_Tp, _Allocator>::iterator
2257deque<_Tp, _Allocator>::insert(const_iterator __p, _InputIter __f, _InputIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002258 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value
2259 &&!__is_cpp17_forward_iterator<_InputIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002260{
2261 __split_buffer<value_type, allocator_type&> __buf(__base::__alloc());
2262 __buf.__construct_at_end(__f, __l);
2263 typedef typename __split_buffer<value_type, allocator_type&>::iterator __bi;
2264 return insert(__p, move_iterator<__bi>(__buf.begin()), move_iterator<__bi>(__buf.end()));
2265}
2266
2267template <class _Tp, class _Allocator>
Marshall Clow6b612cf2015-01-22 18:33:29 +00002268template <class _ForwardIterator>
2269typename deque<_Tp, _Allocator>::iterator
2270deque<_Tp, _Allocator>::insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002271 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value
2272 &&!__is_cpp17_bidirectional_iterator<_ForwardIterator>::value>::type*)
Marshall Clow6b612cf2015-01-22 18:33:29 +00002273{
2274 size_type __n = _VSTD::distance(__f, __l);
2275 __split_buffer<value_type, allocator_type&> __buf(__n, 0, __base::__alloc());
2276 __buf.__construct_at_end(__f, __l);
2277 typedef typename __split_buffer<value_type, allocator_type&>::iterator __fwd;
2278 return insert(__p, move_iterator<__fwd>(__buf.begin()), move_iterator<__fwd>(__buf.end()));
2279}
2280
2281template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002282template <class _BiIter>
2283typename deque<_Tp, _Allocator>::iterator
2284deque<_Tp, _Allocator>::insert(const_iterator __p, _BiIter __f, _BiIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002285 typename enable_if<__is_cpp17_bidirectional_iterator<_BiIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002286{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002287 size_type __n = _VSTD::distance(__f, __l);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002288 size_type __pos = __p - __base::begin();
2289 size_type __to_end = __base::size() - __pos;
2290 allocator_type& __a = __base::__alloc();
2291 if (__pos < __to_end)
2292 { // insert by shifting things backward
2293 if (__n > __front_spare())
2294 __add_front_capacity(__n - __front_spare());
2295 // __n <= __front_spare()
Howard Hinnantc51e1022010-05-11 19:42:16 +00002296 iterator __old_begin = __base::begin();
2297 iterator __i = __old_begin;
2298 _BiIter __m = __f;
2299 if (__n > __pos)
2300 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002301 __m = __pos < __n / 2 ? _VSTD::prev(__l, __pos) : _VSTD::next(__f, __n - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002302 for (_BiIter __j = __m; __j != __f; --__base::__start_, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002303 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), *--__j);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002304 __n = __pos;
2305 }
2306 if (__n > 0)
2307 {
2308 iterator __obn = __old_begin + __n;
2309 for (iterator __j = __obn; __j != __old_begin;)
2310 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002311 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), _VSTD::move(*--__j));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002312 --__base::__start_;
2313 ++__base::size();
2314 }
2315 if (__n < __pos)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002316 __old_begin = _VSTD::move(__obn, __old_begin + __pos, __old_begin);
2317 _VSTD::copy(__m, __l, __old_begin);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002318 }
2319 }
2320 else
2321 { // insert by shifting things forward
2322 size_type __back_capacity = __back_spare();
2323 if (__n > __back_capacity)
2324 __add_back_capacity(__n - __back_capacity);
2325 // __n <= __back_capacity
Howard Hinnantc51e1022010-05-11 19:42:16 +00002326 iterator __old_end = __base::end();
2327 iterator __i = __old_end;
2328 _BiIter __m = __l;
2329 size_type __de = __base::size() - __pos;
2330 if (__n > __de)
2331 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002332 __m = __de < __n / 2 ? _VSTD::next(__f, __de) : _VSTD::prev(__l, __n - __de);
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002333 for (_BiIter __j = __m; __j != __l; ++__i, (void) ++__j, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002334 __alloc_traits::construct(__a, _VSTD::addressof(*__i), *__j);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002335 __n = __de;
2336 }
2337 if (__n > 0)
2338 {
2339 iterator __oen = __old_end - __n;
2340 for (iterator __j = __oen; __j != __old_end; ++__i, ++__j, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002341 __alloc_traits::construct(__a, _VSTD::addressof(*__i), _VSTD::move(*__j));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002342 if (__n < __de)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002343 __old_end = _VSTD::move_backward(__old_end - __de, __oen, __old_end);
2344 _VSTD::copy_backward(__f, __m, __old_end);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002345 }
2346 }
2347 return __base::begin() + __pos;
2348}
2349
2350template <class _Tp, class _Allocator>
2351template <class _InpIter>
2352void
2353deque<_Tp, _Allocator>::__append(_InpIter __f, _InpIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002354 typename enable_if<__is_cpp17_input_iterator<_InpIter>::value &&
2355 !__is_cpp17_forward_iterator<_InpIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002356{
2357 for (; __f != __l; ++__f)
Eric Fiselier96919722017-10-17 13:03:17 +00002358#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002359 push_back(*__f);
Eric Fiselier96919722017-10-17 13:03:17 +00002360#else
2361 emplace_back(*__f);
2362#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002363}
2364
2365template <class _Tp, class _Allocator>
2366template <class _ForIter>
2367void
2368deque<_Tp, _Allocator>::__append(_ForIter __f, _ForIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002369 typename enable_if<__is_cpp17_forward_iterator<_ForIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002370{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002371 size_type __n = _VSTD::distance(__f, __l);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002372 allocator_type& __a = __base::__alloc();
2373 size_type __back_capacity = __back_spare();
2374 if (__n > __back_capacity)
2375 __add_back_capacity(__n - __back_capacity);
2376 // __n <= __back_capacity
Eric Fiselierc52db212019-08-12 07:51:05 +00002377 for (__deque_block_range __br : __deque_range(__base::end(), __base::end() + __n)) {
2378 _ConstructTransaction __tx(this, __br);
2379 for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, (void)++__f) {
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05002380 __alloc_traits::construct(__a, _VSTD::__to_address(__tx.__pos_), *__f);
Eric Fiselierc52db212019-08-12 07:51:05 +00002381 }
2382 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002383}
2384
2385template <class _Tp, class _Allocator>
2386void
2387deque<_Tp, _Allocator>::__append(size_type __n)
2388{
2389 allocator_type& __a = __base::__alloc();
2390 size_type __back_capacity = __back_spare();
2391 if (__n > __back_capacity)
2392 __add_back_capacity(__n - __back_capacity);
2393 // __n <= __back_capacity
Eric Fiselierc52db212019-08-12 07:51:05 +00002394 for (__deque_block_range __br : __deque_range(__base::end(), __base::end() + __n)) {
2395 _ConstructTransaction __tx(this, __br);
2396 for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05002397 __alloc_traits::construct(__a, _VSTD::__to_address(__tx.__pos_));
Eric Fiselierc52db212019-08-12 07:51:05 +00002398 }
2399 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002400}
2401
2402template <class _Tp, class _Allocator>
2403void
2404deque<_Tp, _Allocator>::__append(size_type __n, const value_type& __v)
2405{
2406 allocator_type& __a = __base::__alloc();
2407 size_type __back_capacity = __back_spare();
2408 if (__n > __back_capacity)
2409 __add_back_capacity(__n - __back_capacity);
2410 // __n <= __back_capacity
Eric Fiselierc52db212019-08-12 07:51:05 +00002411 for (__deque_block_range __br : __deque_range(__base::end(), __base::end() + __n)) {
2412 _ConstructTransaction __tx(this, __br);
2413 for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05002414 __alloc_traits::construct(__a, _VSTD::__to_address(__tx.__pos_), __v);
Eric Fiselierc52db212019-08-12 07:51:05 +00002415 }
2416 }
2417
Howard Hinnantc51e1022010-05-11 19:42:16 +00002418}
2419
2420// Create front capacity for one block of elements.
2421// Strong guarantee. Either do it or don't touch anything.
2422template <class _Tp, class _Allocator>
2423void
2424deque<_Tp, _Allocator>::__add_front_capacity()
2425{
2426 allocator_type& __a = __base::__alloc();
2427 if (__back_spare() >= __base::__block_size)
2428 {
2429 __base::__start_ += __base::__block_size;
2430 pointer __pt = __base::__map_.back();
2431 __base::__map_.pop_back();
2432 __base::__map_.push_front(__pt);
2433 }
2434 // Else if __base::__map_.size() < __base::__map_.capacity() then we need to allocate 1 buffer
2435 else if (__base::__map_.size() < __base::__map_.capacity())
2436 { // we can put the new buffer into the map, but don't shift things around
2437 // until all buffers are allocated. If we throw, we don't need to fix
2438 // anything up (any added buffers are undetectible)
2439 if (__base::__map_.__front_spare() > 0)
2440 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2441 else
2442 {
2443 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2444 // Done allocating, reorder capacity
2445 pointer __pt = __base::__map_.back();
2446 __base::__map_.pop_back();
2447 __base::__map_.push_front(__pt);
2448 }
2449 __base::__start_ = __base::__map_.size() == 1 ?
2450 __base::__block_size / 2 :
2451 __base::__start_ + __base::__block_size;
2452 }
2453 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2454 else
2455 {
2456 __split_buffer<pointer, typename __base::__pointer_allocator&>
2457 __buf(max<size_type>(2 * __base::__map_.capacity(), 1),
2458 0, __base::__map_.__alloc());
Marshall Clow5fd466b2015-03-09 17:08:51 +00002459
Marshall Clow8982dcd2015-07-13 20:04:56 +00002460 typedef __allocator_destructor<_Allocator> _Dp;
2461 unique_ptr<pointer, _Dp> __hold(
2462 __alloc_traits::allocate(__a, __base::__block_size),
2463 _Dp(__a, __base::__block_size));
2464 __buf.push_back(__hold.get());
2465 __hold.release();
Louis Dionne72f24392018-12-12 23:58:25 +00002466
Howard Hinnantc51e1022010-05-11 19:42:16 +00002467 for (typename __base::__map_pointer __i = __base::__map_.begin();
2468 __i != __base::__map_.end(); ++__i)
2469 __buf.push_back(*__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002470 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2471 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2472 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2473 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002474 __base::__start_ = __base::__map_.size() == 1 ?
2475 __base::__block_size / 2 :
2476 __base::__start_ + __base::__block_size;
2477 }
2478}
2479
2480// Create front capacity for __n elements.
2481// Strong guarantee. Either do it or don't touch anything.
2482template <class _Tp, class _Allocator>
2483void
2484deque<_Tp, _Allocator>::__add_front_capacity(size_type __n)
2485{
2486 allocator_type& __a = __base::__alloc();
2487 size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2488 // Number of unused blocks at back:
2489 size_type __back_capacity = __back_spare() / __base::__block_size;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002490 __back_capacity = _VSTD::min(__back_capacity, __nb); // don't take more than you need
Howard Hinnantc51e1022010-05-11 19:42:16 +00002491 __nb -= __back_capacity; // number of blocks need to allocate
2492 // If __nb == 0, then we have sufficient capacity.
2493 if (__nb == 0)
2494 {
2495 __base::__start_ += __base::__block_size * __back_capacity;
2496 for (; __back_capacity > 0; --__back_capacity)
2497 {
2498 pointer __pt = __base::__map_.back();
2499 __base::__map_.pop_back();
2500 __base::__map_.push_front(__pt);
2501 }
2502 }
2503 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2504 else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2505 { // we can put the new buffers into the map, but don't shift things around
2506 // until all buffers are allocated. If we throw, we don't need to fix
2507 // anything up (any added buffers are undetectible)
2508 for (; __nb > 0; --__nb, __base::__start_ += __base::__block_size - (__base::__map_.size() == 1))
2509 {
2510 if (__base::__map_.__front_spare() == 0)
2511 break;
2512 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2513 }
2514 for (; __nb > 0; --__nb, ++__back_capacity)
2515 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2516 // Done allocating, reorder capacity
2517 __base::__start_ += __back_capacity * __base::__block_size;
2518 for (; __back_capacity > 0; --__back_capacity)
2519 {
2520 pointer __pt = __base::__map_.back();
2521 __base::__map_.pop_back();
2522 __base::__map_.push_front(__pt);
2523 }
2524 }
2525 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2526 else
2527 {
2528 size_type __ds = (__nb + __back_capacity) * __base::__block_size - __base::__map_.empty();
2529 __split_buffer<pointer, typename __base::__pointer_allocator&>
2530 __buf(max<size_type>(2* __base::__map_.capacity(),
2531 __nb + __base::__map_.size()),
2532 0, __base::__map_.__alloc());
2533#ifndef _LIBCPP_NO_EXCEPTIONS
2534 try
2535 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002536#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002537 for (; __nb > 0; --__nb)
2538 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2539#ifndef _LIBCPP_NO_EXCEPTIONS
2540 }
2541 catch (...)
2542 {
2543 for (typename __base::__map_pointer __i = __buf.begin();
2544 __i != __buf.end(); ++__i)
2545 __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2546 throw;
2547 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002548#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002549 for (; __back_capacity > 0; --__back_capacity)
2550 {
2551 __buf.push_back(__base::__map_.back());
2552 __base::__map_.pop_back();
2553 }
2554 for (typename __base::__map_pointer __i = __base::__map_.begin();
2555 __i != __base::__map_.end(); ++__i)
2556 __buf.push_back(*__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002557 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2558 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2559 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2560 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002561 __base::__start_ += __ds;
2562 }
2563}
2564
2565// Create back capacity for one block of elements.
2566// Strong guarantee. Either do it or don't touch anything.
2567template <class _Tp, class _Allocator>
2568void
2569deque<_Tp, _Allocator>::__add_back_capacity()
2570{
2571 allocator_type& __a = __base::__alloc();
2572 if (__front_spare() >= __base::__block_size)
2573 {
2574 __base::__start_ -= __base::__block_size;
2575 pointer __pt = __base::__map_.front();
2576 __base::__map_.pop_front();
2577 __base::__map_.push_back(__pt);
2578 }
2579 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2580 else if (__base::__map_.size() < __base::__map_.capacity())
2581 { // we can put the new buffer into the map, but don't shift things around
2582 // until it is allocated. If we throw, we don't need to fix
2583 // anything up (any added buffers are undetectible)
2584 if (__base::__map_.__back_spare() != 0)
2585 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2586 else
2587 {
2588 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2589 // Done allocating, reorder capacity
2590 pointer __pt = __base::__map_.front();
2591 __base::__map_.pop_front();
2592 __base::__map_.push_back(__pt);
2593 }
2594 }
2595 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2596 else
2597 {
2598 __split_buffer<pointer, typename __base::__pointer_allocator&>
2599 __buf(max<size_type>(2* __base::__map_.capacity(), 1),
2600 __base::__map_.size(),
2601 __base::__map_.__alloc());
Marshall Clow5fd466b2015-03-09 17:08:51 +00002602
Marshall Clow8982dcd2015-07-13 20:04:56 +00002603 typedef __allocator_destructor<_Allocator> _Dp;
2604 unique_ptr<pointer, _Dp> __hold(
2605 __alloc_traits::allocate(__a, __base::__block_size),
2606 _Dp(__a, __base::__block_size));
2607 __buf.push_back(__hold.get());
2608 __hold.release();
Marshall Clow5fd466b2015-03-09 17:08:51 +00002609
Howard Hinnantc51e1022010-05-11 19:42:16 +00002610 for (typename __base::__map_pointer __i = __base::__map_.end();
2611 __i != __base::__map_.begin();)
2612 __buf.push_front(*--__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002613 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2614 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2615 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2616 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002617 }
2618}
2619
2620// Create back capacity for __n elements.
2621// Strong guarantee. Either do it or don't touch anything.
2622template <class _Tp, class _Allocator>
2623void
2624deque<_Tp, _Allocator>::__add_back_capacity(size_type __n)
2625{
2626 allocator_type& __a = __base::__alloc();
2627 size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2628 // Number of unused blocks at front:
2629 size_type __front_capacity = __front_spare() / __base::__block_size;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002630 __front_capacity = _VSTD::min(__front_capacity, __nb); // don't take more than you need
Howard Hinnantc51e1022010-05-11 19:42:16 +00002631 __nb -= __front_capacity; // number of blocks need to allocate
2632 // If __nb == 0, then we have sufficient capacity.
2633 if (__nb == 0)
2634 {
2635 __base::__start_ -= __base::__block_size * __front_capacity;
2636 for (; __front_capacity > 0; --__front_capacity)
2637 {
2638 pointer __pt = __base::__map_.front();
2639 __base::__map_.pop_front();
2640 __base::__map_.push_back(__pt);
2641 }
2642 }
2643 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2644 else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2645 { // we can put the new buffers into the map, but don't shift things around
2646 // until all buffers are allocated. If we throw, we don't need to fix
2647 // anything up (any added buffers are undetectible)
2648 for (; __nb > 0; --__nb)
2649 {
2650 if (__base::__map_.__back_spare() == 0)
2651 break;
2652 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2653 }
2654 for (; __nb > 0; --__nb, ++__front_capacity, __base::__start_ +=
2655 __base::__block_size - (__base::__map_.size() == 1))
2656 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2657 // Done allocating, reorder capacity
2658 __base::__start_ -= __base::__block_size * __front_capacity;
2659 for (; __front_capacity > 0; --__front_capacity)
2660 {
2661 pointer __pt = __base::__map_.front();
2662 __base::__map_.pop_front();
2663 __base::__map_.push_back(__pt);
2664 }
2665 }
2666 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2667 else
2668 {
2669 size_type __ds = __front_capacity * __base::__block_size;
2670 __split_buffer<pointer, typename __base::__pointer_allocator&>
2671 __buf(max<size_type>(2* __base::__map_.capacity(),
2672 __nb + __base::__map_.size()),
2673 __base::__map_.size() - __front_capacity,
2674 __base::__map_.__alloc());
2675#ifndef _LIBCPP_NO_EXCEPTIONS
2676 try
2677 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002678#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002679 for (; __nb > 0; --__nb)
2680 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2681#ifndef _LIBCPP_NO_EXCEPTIONS
2682 }
2683 catch (...)
2684 {
2685 for (typename __base::__map_pointer __i = __buf.begin();
2686 __i != __buf.end(); ++__i)
2687 __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2688 throw;
2689 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002690#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002691 for (; __front_capacity > 0; --__front_capacity)
2692 {
2693 __buf.push_back(__base::__map_.front());
2694 __base::__map_.pop_front();
2695 }
2696 for (typename __base::__map_pointer __i = __base::__map_.end();
2697 __i != __base::__map_.begin();)
2698 __buf.push_front(*--__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002699 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2700 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2701 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2702 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002703 __base::__start_ -= __ds;
2704 }
2705}
2706
2707template <class _Tp, class _Allocator>
2708void
2709deque<_Tp, _Allocator>::pop_front()
2710{
2711 allocator_type& __a = __base::__alloc();
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05002712 __alloc_traits::destroy(__a, _VSTD::__to_address(*(__base::__map_.begin() +
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002713 __base::__start_ / __base::__block_size) +
2714 __base::__start_ % __base::__block_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002715 --__base::size();
Eric Fiselier27e03622019-08-01 23:11:18 +00002716 ++__base::__start_;
2717 __maybe_remove_front_spare();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002718}
2719
2720template <class _Tp, class _Allocator>
2721void
2722deque<_Tp, _Allocator>::pop_back()
2723{
Louis Dionne72f24392018-12-12 23:58:25 +00002724 _LIBCPP_ASSERT(!empty(), "deque::pop_back called for empty deque");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002725 allocator_type& __a = __base::__alloc();
2726 size_type __p = __base::size() + __base::__start_ - 1;
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05002727 __alloc_traits::destroy(__a, _VSTD::__to_address(*(__base::__map_.begin() +
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002728 __p / __base::__block_size) +
2729 __p % __base::__block_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002730 --__base::size();
Eric Fiselier27e03622019-08-01 23:11:18 +00002731 __maybe_remove_back_spare();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002732}
2733
2734// move assign [__f, __l) to [__r, __r + (__l-__f)).
2735// If __vt points into [__f, __l), then subtract (__f - __r) from __vt.
2736template <class _Tp, class _Allocator>
2737typename deque<_Tp, _Allocator>::iterator
2738deque<_Tp, _Allocator>::__move_and_check(iterator __f, iterator __l, iterator __r,
2739 const_pointer& __vt)
2740{
2741 // as if
2742 // for (; __f != __l; ++__f, ++__r)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002743 // *__r = _VSTD::move(*__f);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002744 difference_type __n = __l - __f;
2745 while (__n > 0)
2746 {
2747 pointer __fb = __f.__ptr_;
2748 pointer __fe = *__f.__m_iter_ + __base::__block_size;
2749 difference_type __bs = __fe - __fb;
2750 if (__bs > __n)
2751 {
2752 __bs = __n;
2753 __fe = __fb + __bs;
2754 }
2755 if (__fb <= __vt && __vt < __fe)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002756 __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) -= __f - __r).__ptr_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002757 __r = _VSTD::move(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002758 __n -= __bs;
2759 __f += __bs;
2760 }
2761 return __r;
2762}
2763
2764// move assign [__f, __l) to [__r - (__l-__f), __r) backwards.
2765// If __vt points into [__f, __l), then add (__r - __l) to __vt.
2766template <class _Tp, class _Allocator>
2767typename deque<_Tp, _Allocator>::iterator
2768deque<_Tp, _Allocator>::__move_backward_and_check(iterator __f, iterator __l, iterator __r,
2769 const_pointer& __vt)
2770{
2771 // as if
2772 // while (__f != __l)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002773 // *--__r = _VSTD::move(*--__l);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002774 difference_type __n = __l - __f;
2775 while (__n > 0)
2776 {
2777 --__l;
2778 pointer __lb = *__l.__m_iter_;
2779 pointer __le = __l.__ptr_ + 1;
2780 difference_type __bs = __le - __lb;
2781 if (__bs > __n)
2782 {
2783 __bs = __n;
2784 __lb = __le - __bs;
2785 }
2786 if (__lb <= __vt && __vt < __le)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002787 __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) += __r - __l - 1).__ptr_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002788 __r = _VSTD::move_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002789 __n -= __bs;
2790 __l -= __bs - 1;
2791 }
2792 return __r;
2793}
2794
2795// move construct [__f, __l) to [__r, __r + (__l-__f)).
2796// If __vt points into [__f, __l), then add (__r - __f) to __vt.
2797template <class _Tp, class _Allocator>
2798void
2799deque<_Tp, _Allocator>::__move_construct_and_check(iterator __f, iterator __l,
2800 iterator __r, const_pointer& __vt)
2801{
2802 allocator_type& __a = __base::__alloc();
2803 // as if
2804 // for (; __f != __l; ++__r, ++__f, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002805 // __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__f));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002806 difference_type __n = __l - __f;
2807 while (__n > 0)
2808 {
2809 pointer __fb = __f.__ptr_;
2810 pointer __fe = *__f.__m_iter_ + __base::__block_size;
2811 difference_type __bs = __fe - __fb;
2812 if (__bs > __n)
2813 {
2814 __bs = __n;
2815 __fe = __fb + __bs;
2816 }
2817 if (__fb <= __vt && __vt < __fe)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002818 __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) += __r - __f).__ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002819 for (; __fb != __fe; ++__fb, ++__r, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002820 __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__fb));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002821 __n -= __bs;
2822 __f += __bs;
2823 }
2824}
2825
2826// move construct [__f, __l) to [__r - (__l-__f), __r) backwards.
2827// If __vt points into [__f, __l), then subtract (__l - __r) from __vt.
2828template <class _Tp, class _Allocator>
2829void
2830deque<_Tp, _Allocator>::__move_construct_backward_and_check(iterator __f, iterator __l,
2831 iterator __r, const_pointer& __vt)
2832{
2833 allocator_type& __a = __base::__alloc();
2834 // as if
2835 // for (iterator __j = __l; __j != __f;)
2836 // {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002837 // __alloc_traitsconstruct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__j));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002838 // --__base::__start_;
2839 // ++__base::size();
2840 // }
2841 difference_type __n = __l - __f;
2842 while (__n > 0)
2843 {
2844 --__l;
2845 pointer __lb = *__l.__m_iter_;
2846 pointer __le = __l.__ptr_ + 1;
2847 difference_type __bs = __le - __lb;
2848 if (__bs > __n)
2849 {
2850 __bs = __n;
2851 __lb = __le - __bs;
2852 }
2853 if (__lb <= __vt && __vt < __le)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002854 __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) -= __l - __r + 1).__ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002855 while (__le != __lb)
2856 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002857 __alloc_traits::construct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__le));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002858 --__base::__start_;
2859 ++__base::size();
2860 }
2861 __n -= __bs;
2862 __l -= __bs - 1;
2863 }
2864}
2865
2866template <class _Tp, class _Allocator>
2867typename deque<_Tp, _Allocator>::iterator
2868deque<_Tp, _Allocator>::erase(const_iterator __f)
2869{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002870 iterator __b = __base::begin();
2871 difference_type __pos = __f - __b;
2872 iterator __p = __b + __pos;
2873 allocator_type& __a = __base::__alloc();
Eric Fiselier37c22152016-12-24 00:24:44 +00002874 if (static_cast<size_t>(__pos) <= (__base::size() - 1) / 2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002875 { // erase from front
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002876 _VSTD::move_backward(__b, __p, _VSTD::next(__p));
2877 __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002878 --__base::size();
2879 ++__base::__start_;
Eric Fiselier27e03622019-08-01 23:11:18 +00002880 __maybe_remove_front_spare();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002881 }
2882 else
2883 { // erase from back
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002884 iterator __i = _VSTD::move(_VSTD::next(__p), __base::end(), __p);
2885 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002886 --__base::size();
Eric Fiselier27e03622019-08-01 23:11:18 +00002887 __maybe_remove_back_spare();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002888 }
2889 return __base::begin() + __pos;
2890}
2891
2892template <class _Tp, class _Allocator>
2893typename deque<_Tp, _Allocator>::iterator
2894deque<_Tp, _Allocator>::erase(const_iterator __f, const_iterator __l)
2895{
2896 difference_type __n = __l - __f;
2897 iterator __b = __base::begin();
2898 difference_type __pos = __f - __b;
2899 iterator __p = __b + __pos;
2900 if (__n > 0)
2901 {
2902 allocator_type& __a = __base::__alloc();
Eric Fiselier37c22152016-12-24 00:24:44 +00002903 if (static_cast<size_t>(__pos) <= (__base::size() - __n) / 2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002904 { // erase from front
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002905 iterator __i = _VSTD::move_backward(__b, __p, __p + __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002906 for (; __b != __i; ++__b)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002907 __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002908 __base::size() -= __n;
2909 __base::__start_ += __n;
Eric Fiselier27e03622019-08-01 23:11:18 +00002910 while (__maybe_remove_front_spare()) {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002911 }
2912 }
2913 else
2914 { // erase from back
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002915 iterator __i = _VSTD::move(__p + __n, __base::end(), __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002916 for (iterator __e = __base::end(); __i != __e; ++__i)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002917 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002918 __base::size() -= __n;
Eric Fiselier27e03622019-08-01 23:11:18 +00002919 while (__maybe_remove_back_spare()) {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002920 }
2921 }
2922 }
2923 return __base::begin() + __pos;
2924}
2925
2926template <class _Tp, class _Allocator>
2927void
2928deque<_Tp, _Allocator>::__erase_to_end(const_iterator __f)
2929{
2930 iterator __e = __base::end();
2931 difference_type __n = __e - __f;
2932 if (__n > 0)
2933 {
2934 allocator_type& __a = __base::__alloc();
2935 iterator __b = __base::begin();
2936 difference_type __pos = __f - __b;
2937 for (iterator __p = __b + __pos; __p != __e; ++__p)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002938 __alloc_traits::destroy(__a, _VSTD::addressof(*__p));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002939 __base::size() -= __n;
Eric Fiselier27e03622019-08-01 23:11:18 +00002940 while (__maybe_remove_back_spare()) {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002941 }
2942 }
2943}
2944
2945template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002946inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002947void
2948deque<_Tp, _Allocator>::swap(deque& __c)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002949#if _LIBCPP_STD_VER >= 14
2950 _NOEXCEPT
2951#else
Louis Dionne72f24392018-12-12 23:58:25 +00002952 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002953 __is_nothrow_swappable<allocator_type>::value)
2954#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002955{
2956 __base::swap(__c);
2957}
2958
2959template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002960inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002961void
Howard Hinnantda2df912011-06-02 16:10:22 +00002962deque<_Tp, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002963{
2964 __base::clear();
2965}
2966
2967template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002968inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002969bool
2970operator==(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2971{
2972 const typename deque<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002973 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002974}
2975
2976template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002977inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002978bool
2979operator!=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2980{
2981 return !(__x == __y);
2982}
2983
2984template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002985inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002986bool
2987operator< (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2988{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002989 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002990}
2991
2992template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002993inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002994bool
2995operator> (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2996{
2997 return __y < __x;
2998}
2999
3000template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003001inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003002bool
3003operator>=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
3004{
3005 return !(__x < __y);
3006}
3007
3008template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003009inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003010bool
3011operator<=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
3012{
3013 return !(__y < __x);
3014}
3015
3016template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003017inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003018void
3019swap(deque<_Tp, _Allocator>& __x, deque<_Tp, _Allocator>& __y)
Howard Hinnantca2fc222011-06-02 20:00:14 +00003020 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003021{
3022 __x.swap(__y);
3023}
3024
Marshall Clow29b53f22018-12-14 18:49:35 +00003025#if _LIBCPP_STD_VER > 17
3026template <class _Tp, class _Allocator, class _Up>
Marek Kurdeja98b1412020-05-02 13:58:03 +02003027inline _LIBCPP_INLINE_VISIBILITY typename deque<_Tp, _Allocator>::size_type
3028erase(deque<_Tp, _Allocator>& __c, const _Up& __v) {
3029 auto __old_size = __c.size();
3030 __c.erase(_VSTD::remove(__c.begin(), __c.end(), __v), __c.end());
3031 return __old_size - __c.size();
3032}
Marshall Clow29b53f22018-12-14 18:49:35 +00003033
3034template <class _Tp, class _Allocator, class _Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +02003035inline _LIBCPP_INLINE_VISIBILITY typename deque<_Tp, _Allocator>::size_type
3036erase_if(deque<_Tp, _Allocator>& __c, _Predicate __pred) {
3037 auto __old_size = __c.size();
3038 __c.erase(_VSTD::remove_if(__c.begin(), __c.end(), __pred), __c.end());
3039 return __old_size - __c.size();
3040}
Marshall Clow29b53f22018-12-14 18:49:35 +00003041#endif
3042
3043
Howard Hinnantc51e1022010-05-11 19:42:16 +00003044_LIBCPP_END_NAMESPACE_STD
3045
Eric Fiselierf4433a32017-05-31 22:07:49 +00003046_LIBCPP_POP_MACROS
3047
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003048#endif // _LIBCPP_DEQUE