blob: f4caa9ef0af24c15456b55ccbccbdfb036b62685 [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>
Arthur O'Dwyer597cac42021-05-12 23:04:03 -0400164#include <__debug>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000165#include <__split_buffer>
Christopher Di Bella41f26e82021-06-05 02:47:47 +0000166#include <__utility/forward.h>
Arthur O'Dwyer597cac42021-05-12 23:04:03 -0400167#include <algorithm>
Arthur O'Dwyer7deec122021-03-24 18:19:12 -0400168#include <compare>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000169#include <initializer_list>
170#include <iterator>
Christopher Di Bella599a6c62021-06-09 23:10:17 +0000171#include <limits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000172#include <stdexcept>
Arthur O'Dwyer597cac42021-05-12 23:04:03 -0400173#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000174#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000175
Eric Fiselierf4433a32017-05-31 22:07:49 +0000176#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
177#pragma GCC system_header
178#endif
179
180_LIBCPP_PUSH_MACROS
181#include <__undef_macros>
182
Howard Hinnantc5a5fbd2011-11-29 16:45:27 +0000183
Howard Hinnantc51e1022010-05-11 19:42:16 +0000184_LIBCPP_BEGIN_NAMESPACE_STD
185
186template <class _Tp, class _Allocator> class __deque_base;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000187template <class _Tp, class _Allocator = allocator<_Tp> > class _LIBCPP_TEMPLATE_VIS deque;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000188
189template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
190 class _DiffType, _DiffType _BlockSize>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000191class _LIBCPP_TEMPLATE_VIS __deque_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000192
193template <class _RAIter,
194 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
195__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
196copy(_RAIter __f,
197 _RAIter __l,
198 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500199 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000200
201template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
202 class _OutputIterator>
203_OutputIterator
204copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
205 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
206 _OutputIterator __r);
207
208template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
209 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
210__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
211copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
212 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
213 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
214
215template <class _RAIter,
216 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
217__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
218copy_backward(_RAIter __f,
219 _RAIter __l,
220 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500221 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000222
223template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
224 class _OutputIterator>
225_OutputIterator
226copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
227 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
228 _OutputIterator __r);
229
230template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
231 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
232__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
233copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
234 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
235 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
236
237template <class _RAIter,
238 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
239__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
240move(_RAIter __f,
241 _RAIter __l,
242 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500243 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000244
245template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
246 class _OutputIterator>
247_OutputIterator
248move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
249 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
250 _OutputIterator __r);
251
252template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
253 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
254__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
255move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
256 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
257 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
258
259template <class _RAIter,
260 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
261__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
262move_backward(_RAIter __f,
263 _RAIter __l,
264 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500265 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000266
267template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
268 class _OutputIterator>
269_OutputIterator
270move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
271 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
272 _OutputIterator __r);
273
274template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
275 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
276__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
277move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
278 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
279 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
280
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000281template <class _ValueType, class _DiffType>
282struct __deque_block_size {
283 static const _DiffType value = sizeof(_ValueType) < 256 ? 4096 / sizeof(_ValueType) : 16;
284};
285
Howard Hinnantc51e1022010-05-11 19:42:16 +0000286template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000287 class _DiffType, _DiffType _BS =
288#ifdef _LIBCPP_ABI_INCOMPLETE_TYPES_IN_DEQUE
289// Keep template parameter to avoid changing all template declarations thoughout
290// this file.
291 0
292#else
293 __deque_block_size<_ValueType, _DiffType>::value
294#endif
295 >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000296class _LIBCPP_TEMPLATE_VIS __deque_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000297{
298 typedef _MapPointer __map_iterator;
299public:
300 typedef _Pointer pointer;
301 typedef _DiffType difference_type;
302private:
303 __map_iterator __m_iter_;
304 pointer __ptr_;
305
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000306 static const difference_type __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000307public:
308 typedef _ValueType value_type;
309 typedef random_access_iterator_tag iterator_category;
310 typedef _Reference reference;
311
Marshall Clow7a3a61d2013-08-06 16:14:36 +0000312 _LIBCPP_INLINE_VISIBILITY __deque_iterator() _NOEXCEPT
313#if _LIBCPP_STD_VER > 11
314 : __m_iter_(nullptr), __ptr_(nullptr)
315#endif
316 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000317
Howard Hinnantc834c512011-11-29 18:15:50 +0000318 template <class _Pp, class _Rp, class _MP>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000319 _LIBCPP_INLINE_VISIBILITY
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000320 __deque_iterator(const __deque_iterator<value_type, _Pp, _Rp, _MP, difference_type, _BS>& __it,
Howard Hinnantc834c512011-11-29 18:15:50 +0000321 typename enable_if<is_convertible<_Pp, pointer>::value>::type* = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000322 : __m_iter_(__it.__m_iter_), __ptr_(__it.__ptr_) {}
323
324 _LIBCPP_INLINE_VISIBILITY reference operator*() const {return *__ptr_;}
325 _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return __ptr_;}
326
327 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator++()
328 {
329 if (++__ptr_ - *__m_iter_ == __block_size)
330 {
331 ++__m_iter_;
332 __ptr_ = *__m_iter_;
333 }
334 return *this;
335 }
336
337 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator++(int)
338 {
339 __deque_iterator __tmp = *this;
340 ++(*this);
341 return __tmp;
342 }
343
344 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator--()
345 {
346 if (__ptr_ == *__m_iter_)
347 {
348 --__m_iter_;
349 __ptr_ = *__m_iter_ + __block_size;
350 }
351 --__ptr_;
352 return *this;
353 }
354
355 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator--(int)
356 {
357 __deque_iterator __tmp = *this;
358 --(*this);
359 return __tmp;
360 }
361
362 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator+=(difference_type __n)
363 {
364 if (__n != 0)
365 {
366 __n += __ptr_ - *__m_iter_;
367 if (__n > 0)
368 {
369 __m_iter_ += __n / __block_size;
370 __ptr_ = *__m_iter_ + __n % __block_size;
371 }
372 else // (__n < 0)
373 {
374 difference_type __z = __block_size - 1 - __n;
375 __m_iter_ -= __z / __block_size;
376 __ptr_ = *__m_iter_ + (__block_size - 1 - __z % __block_size);
377 }
378 }
379 return *this;
380 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000381
Howard Hinnantc51e1022010-05-11 19:42:16 +0000382 _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator-=(difference_type __n)
383 {
384 return *this += -__n;
385 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000386
Howard Hinnantc51e1022010-05-11 19:42:16 +0000387 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator+(difference_type __n) const
388 {
389 __deque_iterator __t(*this);
390 __t += __n;
391 return __t;
392 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000393
Howard Hinnantc51e1022010-05-11 19:42:16 +0000394 _LIBCPP_INLINE_VISIBILITY __deque_iterator operator-(difference_type __n) const
395 {
396 __deque_iterator __t(*this);
397 __t -= __n;
398 return __t;
399 }
400
401 _LIBCPP_INLINE_VISIBILITY
402 friend __deque_iterator operator+(difference_type __n, const __deque_iterator& __it)
403 {return __it + __n;}
404
405 _LIBCPP_INLINE_VISIBILITY
406 friend difference_type operator-(const __deque_iterator& __x, const __deque_iterator& __y)
407 {
408 if (__x != __y)
409 return (__x.__m_iter_ - __y.__m_iter_) * __block_size
410 + (__x.__ptr_ - *__x.__m_iter_)
411 - (__y.__ptr_ - *__y.__m_iter_);
412 return 0;
413 }
414
415 _LIBCPP_INLINE_VISIBILITY reference operator[](difference_type __n) const
416 {return *(*this + __n);}
417
418 _LIBCPP_INLINE_VISIBILITY friend
419 bool operator==(const __deque_iterator& __x, const __deque_iterator& __y)
420 {return __x.__ptr_ == __y.__ptr_;}
421
422 _LIBCPP_INLINE_VISIBILITY friend
423 bool operator!=(const __deque_iterator& __x, const __deque_iterator& __y)
424 {return !(__x == __y);}
425
426 _LIBCPP_INLINE_VISIBILITY friend
427 bool operator<(const __deque_iterator& __x, const __deque_iterator& __y)
428 {return __x.__m_iter_ < __y.__m_iter_ ||
429 (__x.__m_iter_ == __y.__m_iter_ && __x.__ptr_ < __y.__ptr_);}
430
431 _LIBCPP_INLINE_VISIBILITY friend
432 bool operator>(const __deque_iterator& __x, const __deque_iterator& __y)
433 {return __y < __x;}
434
435 _LIBCPP_INLINE_VISIBILITY friend
436 bool operator<=(const __deque_iterator& __x, const __deque_iterator& __y)
437 {return !(__y < __x);}
438
439 _LIBCPP_INLINE_VISIBILITY friend
440 bool operator>=(const __deque_iterator& __x, const __deque_iterator& __y)
441 {return !(__x < __y);}
442
443private:
Howard Hinnantda2df912011-06-02 16:10:22 +0000444 _LIBCPP_INLINE_VISIBILITY __deque_iterator(__map_iterator __m, pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000445 : __m_iter_(__m), __ptr_(__p) {}
446
Howard Hinnantc834c512011-11-29 18:15:50 +0000447 template <class _Tp, class _Ap> friend class __deque_base;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000448 template <class _Tp, class _Ap> friend class _LIBCPP_TEMPLATE_VIS deque;
Howard Hinnantc834c512011-11-29 18:15:50 +0000449 template <class _Vp, class _Pp, class _Rp, class _MP, class _Dp, _Dp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000450 friend class _LIBCPP_TEMPLATE_VIS __deque_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000451
452 template <class _RAIter,
453 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
454 friend
455 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
456 copy(_RAIter __f,
457 _RAIter __l,
458 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500459 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000460
461 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
462 class _OutputIterator>
463 friend
464 _OutputIterator
465 copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
466 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
467 _OutputIterator __r);
468
469 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
470 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
471 friend
472 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
473 copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
474 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
475 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
476
477 template <class _RAIter,
478 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
479 friend
480 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
481 copy_backward(_RAIter __f,
482 _RAIter __l,
483 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500484 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000485
486 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
487 class _OutputIterator>
488 friend
489 _OutputIterator
490 copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
491 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
492 _OutputIterator __r);
493
494 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
495 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
496 friend
497 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
498 copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
499 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
500 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
501
502 template <class _RAIter,
503 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
504 friend
505 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
506 move(_RAIter __f,
507 _RAIter __l,
508 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500509 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000510
511 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
512 class _OutputIterator>
513 friend
514 _OutputIterator
515 move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
516 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
517 _OutputIterator __r);
518
519 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
520 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
521 friend
522 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
523 move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
524 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
525 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
526
527 template <class _RAIter,
528 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
529 friend
530 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
531 move_backward(_RAIter __f,
532 _RAIter __l,
533 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500534 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000535
536 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
537 class _OutputIterator>
538 friend
539 _OutputIterator
540 move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
541 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
542 _OutputIterator __r);
543
544 template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
545 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
546 friend
547 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
548 move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
549 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
550 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
551};
552
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000553template <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
554 class _DiffType, _DiffType _BlockSize>
555const _DiffType __deque_iterator<_ValueType, _Pointer, _Reference, _MapPointer,
556 _DiffType, _BlockSize>::__block_size =
557 __deque_block_size<_ValueType, _DiffType>::value;
558
Howard Hinnantc51e1022010-05-11 19:42:16 +0000559// copy
560
561template <class _RAIter,
562 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
563__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
564copy(_RAIter __f,
565 _RAIter __l,
566 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500567 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000568{
569 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
570 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000571 const difference_type __block_size = __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::__block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000572 while (__f != __l)
573 {
574 pointer __rb = __r.__ptr_;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000575 pointer __re = *__r.__m_iter_ + __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000576 difference_type __bs = __re - __rb;
577 difference_type __n = __l - __f;
578 _RAIter __m = __l;
579 if (__n > __bs)
580 {
581 __n = __bs;
582 __m = __f + __n;
583 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000584 _VSTD::copy(__f, __m, __rb);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000585 __f = __m;
586 __r += __n;
587 }
588 return __r;
589}
590
591template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
592 class _OutputIterator>
593_OutputIterator
594copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
595 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
596 _OutputIterator __r)
597{
598 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
599 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000600 const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000601 difference_type __n = __l - __f;
602 while (__n > 0)
603 {
604 pointer __fb = __f.__ptr_;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000605 pointer __fe = *__f.__m_iter_ + __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000606 difference_type __bs = __fe - __fb;
607 if (__bs > __n)
608 {
609 __bs = __n;
610 __fe = __fb + __bs;
611 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000612 __r = _VSTD::copy(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000613 __n -= __bs;
614 __f += __bs;
615 }
616 return __r;
617}
618
619template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
620 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
621__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
622copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
623 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
624 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
625{
626 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
627 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000628 const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000629 difference_type __n = __l - __f;
630 while (__n > 0)
631 {
632 pointer __fb = __f.__ptr_;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000633 pointer __fe = *__f.__m_iter_ + __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000634 difference_type __bs = __fe - __fb;
635 if (__bs > __n)
636 {
637 __bs = __n;
638 __fe = __fb + __bs;
639 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000640 __r = _VSTD::copy(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000641 __n -= __bs;
642 __f += __bs;
643 }
644 return __r;
645}
646
647// copy_backward
648
649template <class _RAIter,
650 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
651__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
652copy_backward(_RAIter __f,
653 _RAIter __l,
654 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500655 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000656{
657 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
658 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
659 while (__f != __l)
660 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000661 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _VSTD::prev(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000662 pointer __rb = *__rp.__m_iter_;
663 pointer __re = __rp.__ptr_ + 1;
664 difference_type __bs = __re - __rb;
665 difference_type __n = __l - __f;
666 _RAIter __m = __f;
667 if (__n > __bs)
668 {
669 __n = __bs;
670 __m = __l - __n;
671 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000672 _VSTD::copy_backward(__m, __l, __re);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000673 __l = __m;
674 __r -= __n;
675 }
676 return __r;
677}
678
679template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
680 class _OutputIterator>
681_OutputIterator
682copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
683 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
684 _OutputIterator __r)
685{
686 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
687 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
688 difference_type __n = __l - __f;
689 while (__n > 0)
690 {
691 --__l;
692 pointer __lb = *__l.__m_iter_;
693 pointer __le = __l.__ptr_ + 1;
694 difference_type __bs = __le - __lb;
695 if (__bs > __n)
696 {
697 __bs = __n;
698 __lb = __le - __bs;
699 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000700 __r = _VSTD::copy_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000701 __n -= __bs;
702 __l -= __bs - 1;
703 }
704 return __r;
705}
706
707template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
708 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
709__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
710copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
711 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
712 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
713{
714 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
715 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
716 difference_type __n = __l - __f;
717 while (__n > 0)
718 {
719 --__l;
720 pointer __lb = *__l.__m_iter_;
721 pointer __le = __l.__ptr_ + 1;
722 difference_type __bs = __le - __lb;
723 if (__bs > __n)
724 {
725 __bs = __n;
726 __lb = __le - __bs;
727 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000728 __r = _VSTD::copy_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000729 __n -= __bs;
730 __l -= __bs - 1;
731 }
732 return __r;
733}
734
735// move
736
737template <class _RAIter,
738 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
739__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
740move(_RAIter __f,
741 _RAIter __l,
742 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500743 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000744{
745 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
746 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000747 const difference_type __block_size = __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::__block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000748 while (__f != __l)
749 {
750 pointer __rb = __r.__ptr_;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000751 pointer __re = *__r.__m_iter_ + __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000752 difference_type __bs = __re - __rb;
753 difference_type __n = __l - __f;
754 _RAIter __m = __l;
755 if (__n > __bs)
756 {
757 __n = __bs;
758 __m = __f + __n;
759 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000760 _VSTD::move(__f, __m, __rb);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000761 __f = __m;
762 __r += __n;
763 }
764 return __r;
765}
766
767template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
768 class _OutputIterator>
769_OutputIterator
770move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
771 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
772 _OutputIterator __r)
773{
774 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
775 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000776 const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000777 difference_type __n = __l - __f;
778 while (__n > 0)
779 {
780 pointer __fb = __f.__ptr_;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000781 pointer __fe = *__f.__m_iter_ + __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000782 difference_type __bs = __fe - __fb;
783 if (__bs > __n)
784 {
785 __bs = __n;
786 __fe = __fb + __bs;
787 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000788 __r = _VSTD::move(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000789 __n -= __bs;
790 __f += __bs;
791 }
792 return __r;
793}
794
795template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
796 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
797__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
798move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
799 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
800 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
801{
802 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
803 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000804 const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000805 difference_type __n = __l - __f;
806 while (__n > 0)
807 {
808 pointer __fb = __f.__ptr_;
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000809 pointer __fe = *__f.__m_iter_ + __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000810 difference_type __bs = __fe - __fb;
811 if (__bs > __n)
812 {
813 __bs = __n;
814 __fe = __fb + __bs;
815 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000816 __r = _VSTD::move(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000817 __n -= __bs;
818 __f += __bs;
819 }
820 return __r;
821}
822
823// move_backward
824
825template <class _RAIter,
826 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
827__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
828move_backward(_RAIter __f,
829 _RAIter __l,
830 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500831 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000832{
833 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
834 typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
835 while (__f != __l)
836 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000837 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _VSTD::prev(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000838 pointer __rb = *__rp.__m_iter_;
839 pointer __re = __rp.__ptr_ + 1;
840 difference_type __bs = __re - __rb;
841 difference_type __n = __l - __f;
842 _RAIter __m = __f;
843 if (__n > __bs)
844 {
845 __n = __bs;
846 __m = __l - __n;
847 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000848 _VSTD::move_backward(__m, __l, __re);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000849 __l = __m;
850 __r -= __n;
851 }
852 return __r;
853}
854
855template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
856 class _OutputIterator>
857_OutputIterator
858move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
859 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
860 _OutputIterator __r)
861{
862 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
863 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
864 difference_type __n = __l - __f;
865 while (__n > 0)
866 {
867 --__l;
868 pointer __lb = *__l.__m_iter_;
869 pointer __le = __l.__ptr_ + 1;
870 difference_type __bs = __le - __lb;
871 if (__bs > __n)
872 {
873 __bs = __n;
874 __lb = __le - __bs;
875 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000876 __r = _VSTD::move_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000877 __n -= __bs;
878 __l -= __bs - 1;
879 }
880 return __r;
881}
882
883template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
884 class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
885__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
886move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
887 __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
888 __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
889{
890 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
891 typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
892 difference_type __n = __l - __f;
893 while (__n > 0)
894 {
895 --__l;
896 pointer __lb = *__l.__m_iter_;
897 pointer __le = __l.__ptr_ + 1;
898 difference_type __bs = __le - __lb;
899 if (__bs > __n)
900 {
901 __bs = __n;
902 __lb = __le - __bs;
903 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000904 __r = _VSTD::move_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000905 __n -= __bs;
906 __l -= __bs - 1;
907 }
908 return __r;
909}
910
Howard Hinnantc51e1022010-05-11 19:42:16 +0000911template <class _Tp, class _Allocator>
912class __deque_base
Howard Hinnantc51e1022010-05-11 19:42:16 +0000913{
914 __deque_base(const __deque_base& __c);
915 __deque_base& operator=(const __deque_base& __c);
Marshall Clow4cc3a452018-05-18 23:44:13 +0000916public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000917 typedef _Allocator allocator_type;
918 typedef allocator_traits<allocator_type> __alloc_traits;
Marshall Clow4cc3a452018-05-18 23:44:13 +0000919 typedef typename __alloc_traits::size_type size_type;
Eric Fiselier27e03622019-08-01 23:11:18 +0000920
Marshall Clow4cc3a452018-05-18 23:44:13 +0000921 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000922 typedef value_type& reference;
923 typedef const value_type& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000924 typedef typename __alloc_traits::difference_type difference_type;
925 typedef typename __alloc_traits::pointer pointer;
926 typedef typename __alloc_traits::const_pointer const_pointer;
927
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000928 static const difference_type __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000929
Marshall Clow940e01c2015-04-07 05:21:38 +0000930 typedef typename __rebind_alloc_helper<__alloc_traits, pointer>::type __pointer_allocator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000931 typedef allocator_traits<__pointer_allocator> __map_traits;
932 typedef typename __map_traits::pointer __map_pointer;
Marshall Clow940e01c2015-04-07 05:21:38 +0000933 typedef typename __rebind_alloc_helper<__alloc_traits, const_pointer>::type __const_pointer_allocator;
Howard Hinnantdcb73a32013-06-23 21:17:24 +0000934 typedef typename allocator_traits<__const_pointer_allocator>::const_pointer __map_const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000935 typedef __split_buffer<pointer, __pointer_allocator> __map;
936
937 typedef __deque_iterator<value_type, pointer, reference, __map_pointer,
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000938 difference_type> iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000939 typedef __deque_iterator<value_type, const_pointer, const_reference, __map_const_pointer,
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000940 difference_type> const_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000941
Eric Fiselierc52db212019-08-12 07:51:05 +0000942 struct __deque_block_range {
943 explicit __deque_block_range(pointer __b, pointer __e) _NOEXCEPT : __begin_(__b), __end_(__e) {}
944 const pointer __begin_;
945 const pointer __end_;
946 };
947
948 struct __deque_range {
949 iterator __pos_;
950 const iterator __end_;
951
952 __deque_range(iterator __pos, iterator __e) _NOEXCEPT
953 : __pos_(__pos), __end_(__e) {}
954
955 explicit operator bool() const _NOEXCEPT {
956 return __pos_ != __end_;
957 }
958
959 __deque_range begin() const {
960 return *this;
961 }
962
963 __deque_range end() const {
964 return __deque_range(__end_, __end_);
965 }
966 __deque_block_range operator*() const _NOEXCEPT {
967 if (__pos_.__m_iter_ == __end_.__m_iter_) {
968 return __deque_block_range(__pos_.__ptr_, __end_.__ptr_);
969 }
970 return __deque_block_range(__pos_.__ptr_, *__pos_.__m_iter_ + __block_size);
971 }
972
973 __deque_range& operator++() _NOEXCEPT {
974 if (__pos_.__m_iter_ == __end_.__m_iter_) {
975 __pos_ = __end_;
976 } else {
977 ++__pos_.__m_iter_;
978 __pos_.__ptr_ = *__pos_.__m_iter_;
979 }
980 return *this;
981 }
982
983
984 friend bool operator==(__deque_range const& __lhs, __deque_range const& __rhs) {
985 return __lhs.__pos_ == __rhs.__pos_;
986 }
987 friend bool operator!=(__deque_range const& __lhs, __deque_range const& __rhs) {
988 return !(__lhs == __rhs);
989 }
990 };
991
992
993
994 struct _ConstructTransaction {
995 _ConstructTransaction(__deque_base* __db, __deque_block_range& __r)
996 : __pos_(__r.__begin_), __end_(__r.__end_), __begin_(__r.__begin_), __base_(__db) {}
997
998
999 ~_ConstructTransaction() {
1000 __base_->size() += (__pos_ - __begin_);
1001 }
1002
1003 pointer __pos_;
1004 const pointer __end_;
1005 private:
1006 const pointer __begin_;
1007 __deque_base * const __base_;
1008 };
1009
Marshall Clow4cc3a452018-05-18 23:44:13 +00001010protected:
Howard Hinnantc51e1022010-05-11 19:42:16 +00001011 __map __map_;
1012 size_type __start_;
1013 __compressed_pair<size_type, allocator_type> __size_;
1014
Howard Hinnantda2df912011-06-02 16:10:22 +00001015 iterator begin() _NOEXCEPT;
1016 const_iterator begin() const _NOEXCEPT;
1017 iterator end() _NOEXCEPT;
1018 const_iterator end() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001019
1020 _LIBCPP_INLINE_VISIBILITY size_type& size() {return __size_.first();}
Howard Hinnantda2df912011-06-02 16:10:22 +00001021 _LIBCPP_INLINE_VISIBILITY
1022 const size_type& size() const _NOEXCEPT {return __size_.first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001023 _LIBCPP_INLINE_VISIBILITY allocator_type& __alloc() {return __size_.second();}
Howard Hinnantda2df912011-06-02 16:10:22 +00001024 _LIBCPP_INLINE_VISIBILITY
1025 const allocator_type& __alloc() const _NOEXCEPT {return __size_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001026
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001027 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantad979ba2011-06-03 15:16:49 +00001028 __deque_base()
1029 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001030 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001031 explicit __deque_base(const allocator_type& __a);
Howard Hinnantca2fc222011-06-02 20:00:14 +00001032public:
Howard Hinnantc51e1022010-05-11 19:42:16 +00001033 ~__deque_base();
1034
Eric Fiselier212e3f22017-04-16 03:17:01 +00001035#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantca2fc222011-06-02 20:00:14 +00001036 __deque_base(__deque_base&& __c)
1037 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001038 __deque_base(__deque_base&& __c, const allocator_type& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001039#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001040
Howard Hinnantca2fc222011-06-02 20:00:14 +00001041 void swap(__deque_base& __c)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001042#if _LIBCPP_STD_VER >= 14
1043 _NOEXCEPT;
1044#else
Louis Dionne72f24392018-12-12 23:58:25 +00001045 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001046 __is_nothrow_swappable<allocator_type>::value);
1047#endif
Howard Hinnantca2fc222011-06-02 20:00:14 +00001048protected:
Howard Hinnantda2df912011-06-02 16:10:22 +00001049 void clear() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001050
1051 bool __invariants() const;
1052
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001053 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001054 void __move_assign(__deque_base& __c)
Howard Hinnant4931e092011-06-02 21:38:57 +00001055 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1056 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001057 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001058 __map_ = _VSTD::move(__c.__map_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001059 __start_ = __c.__start_;
1060 size() = __c.size();
1061 __move_assign_alloc(__c);
1062 __c.__start_ = __c.size() = 0;
1063 }
1064
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001065 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001066 void __move_assign_alloc(__deque_base& __c)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001067 _NOEXCEPT_(!__alloc_traits::propagate_on_container_move_assignment::value ||
1068 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001069 {__move_assign_alloc(__c, integral_constant<bool,
1070 __alloc_traits::propagate_on_container_move_assignment::value>());}
1071
1072private:
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001073 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001074 void __move_assign_alloc(__deque_base& __c, true_type)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001075 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001076 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001077 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001078 }
1079
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001080 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001081 void __move_assign_alloc(__deque_base&, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001082 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001083};
1084
1085template <class _Tp, class _Allocator>
Evgeniy Stepanovc299de22015-11-06 22:02:29 +00001086const typename __deque_base<_Tp, _Allocator>::difference_type
1087 __deque_base<_Tp, _Allocator>::__block_size =
1088 __deque_block_size<value_type, difference_type>::value;
1089
1090template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001091bool
1092__deque_base<_Tp, _Allocator>::__invariants() const
1093{
1094 if (!__map_.__invariants())
1095 return false;
1096 if (__map_.size() >= size_type(-1) / __block_size)
1097 return false;
1098 for (typename __map::const_iterator __i = __map_.begin(), __e = __map_.end();
1099 __i != __e; ++__i)
1100 if (*__i == nullptr)
1101 return false;
1102 if (__map_.size() != 0)
1103 {
1104 if (size() >= __map_.size() * __block_size)
1105 return false;
1106 if (__start_ >= __map_.size() * __block_size - size())
1107 return false;
1108 }
1109 else
1110 {
1111 if (size() != 0)
1112 return false;
1113 if (__start_ != 0)
1114 return false;
1115 }
1116 return true;
1117}
1118
1119template <class _Tp, class _Allocator>
1120typename __deque_base<_Tp, _Allocator>::iterator
Howard Hinnantda2df912011-06-02 16:10:22 +00001121__deque_base<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001122{
1123 __map_pointer __mp = __map_.begin() + __start_ / __block_size;
1124 return iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
1125}
1126
1127template <class _Tp, class _Allocator>
1128typename __deque_base<_Tp, _Allocator>::const_iterator
Howard Hinnantda2df912011-06-02 16:10:22 +00001129__deque_base<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001130{
Howard Hinnantdcb73a32013-06-23 21:17:24 +00001131 __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __start_ / __block_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001132 return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
1133}
1134
1135template <class _Tp, class _Allocator>
1136typename __deque_base<_Tp, _Allocator>::iterator
Howard Hinnantda2df912011-06-02 16:10:22 +00001137__deque_base<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001138{
1139 size_type __p = size() + __start_;
1140 __map_pointer __mp = __map_.begin() + __p / __block_size;
1141 return iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
1142}
1143
1144template <class _Tp, class _Allocator>
1145typename __deque_base<_Tp, _Allocator>::const_iterator
Howard Hinnantda2df912011-06-02 16:10:22 +00001146__deque_base<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001147{
1148 size_type __p = size() + __start_;
Howard Hinnantdcb73a32013-06-23 21:17:24 +00001149 __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __p / __block_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150 return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
1151}
1152
1153template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001154inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001155__deque_base<_Tp, _Allocator>::__deque_base()
Howard Hinnantad979ba2011-06-03 15:16:49 +00001156 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001157 : __start_(0), __size_(0, __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001158
1159template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001160inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001161__deque_base<_Tp, _Allocator>::__deque_base(const allocator_type& __a)
1162 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a) {}
1163
1164template <class _Tp, class _Allocator>
1165__deque_base<_Tp, _Allocator>::~__deque_base()
1166{
1167 clear();
1168 typename __map::iterator __i = __map_.begin();
1169 typename __map::iterator __e = __map_.end();
1170 for (; __i != __e; ++__i)
1171 __alloc_traits::deallocate(__alloc(), *__i, __block_size);
1172}
1173
Eric Fiselier212e3f22017-04-16 03:17:01 +00001174#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001175
1176template <class _Tp, class _Allocator>
1177__deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001178 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001179 : __map_(_VSTD::move(__c.__map_)),
1180 __start_(_VSTD::move(__c.__start_)),
1181 __size_(_VSTD::move(__c.__size_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001182{
1183 __c.__start_ = 0;
1184 __c.size() = 0;
1185}
1186
1187template <class _Tp, class _Allocator>
1188__deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c, const allocator_type& __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001189 : __map_(_VSTD::move(__c.__map_), __pointer_allocator(__a)),
1190 __start_(_VSTD::move(__c.__start_)),
1191 __size_(_VSTD::move(__c.size()), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001192{
1193 if (__a == __c.__alloc())
1194 {
1195 __c.__start_ = 0;
1196 __c.size() = 0;
1197 }
1198 else
1199 {
1200 __map_.clear();
1201 __start_ = 0;
1202 size() = 0;
1203 }
1204}
1205
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001206#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001207
1208template <class _Tp, class _Allocator>
1209void
1210__deque_base<_Tp, _Allocator>::swap(__deque_base& __c)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001211#if _LIBCPP_STD_VER >= 14
1212 _NOEXCEPT
1213#else
Louis Dionne72f24392018-12-12 23:58:25 +00001214 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001215 __is_nothrow_swappable<allocator_type>::value)
1216#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001217{
1218 __map_.swap(__c.__map_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001219 _VSTD::swap(__start_, __c.__start_);
1220 _VSTD::swap(size(), __c.size());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05001221 _VSTD::__swap_allocator(__alloc(), __c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001222}
1223
1224template <class _Tp, class _Allocator>
1225void
Howard Hinnantda2df912011-06-02 16:10:22 +00001226__deque_base<_Tp, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001227{
1228 allocator_type& __a = __alloc();
1229 for (iterator __i = begin(), __e = end(); __i != __e; ++__i)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001230 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001231 size() = 0;
1232 while (__map_.size() > 2)
1233 {
1234 __alloc_traits::deallocate(__a, __map_.front(), __block_size);
1235 __map_.pop_front();
1236 }
1237 switch (__map_.size())
1238 {
1239 case 1:
1240 __start_ = __block_size / 2;
1241 break;
1242 case 2:
1243 __start_ = __block_size;
1244 break;
1245 }
1246}
1247
Marshall Clow65cd4c62015-02-18 17:24:08 +00001248template <class _Tp, class _Allocator /*= allocator<_Tp>*/>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001249class _LIBCPP_TEMPLATE_VIS deque
Howard Hinnantc51e1022010-05-11 19:42:16 +00001250 : private __deque_base<_Tp, _Allocator>
1251{
1252public:
1253 // types:
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001254
Howard Hinnantc51e1022010-05-11 19:42:16 +00001255 typedef _Tp value_type;
1256 typedef _Allocator allocator_type;
1257
Marshall Clow5128cf32015-11-26 01:24:04 +00001258 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
1259 "Allocator::value_type must be same type as value_type");
1260
Howard Hinnantc51e1022010-05-11 19:42:16 +00001261 typedef __deque_base<value_type, allocator_type> __base;
1262
1263 typedef typename __base::__alloc_traits __alloc_traits;
1264 typedef typename __base::reference reference;
1265 typedef typename __base::const_reference const_reference;
1266 typedef typename __base::iterator iterator;
1267 typedef typename __base::const_iterator const_iterator;
1268 typedef typename __base::size_type size_type;
1269 typedef typename __base::difference_type difference_type;
1270
1271 typedef typename __base::pointer pointer;
1272 typedef typename __base::const_pointer const_pointer;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001273 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
1274 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001275
Eric Fiselierc52db212019-08-12 07:51:05 +00001276 using typename __base::__deque_range;
1277 using typename __base::__deque_block_range;
1278 using typename __base::_ConstructTransaction;
1279
Howard Hinnantc51e1022010-05-11 19:42:16 +00001280 // construct/copy/destroy:
Howard Hinnantad979ba2011-06-03 15:16:49 +00001281 _LIBCPP_INLINE_VISIBILITY
1282 deque()
1283 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
1284 {}
Marshall Clow7ed23a72014-03-05 19:06:20 +00001285 _LIBCPP_INLINE_VISIBILITY explicit deque(const allocator_type& __a) : __base(__a) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001286 explicit deque(size_type __n);
Marshall Clowbc4fcb42013-09-07 16:16:19 +00001287#if _LIBCPP_STD_VER > 11
1288 explicit deque(size_type __n, const _Allocator& __a);
1289#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001290 deque(size_type __n, const value_type& __v);
1291 deque(size_type __n, const value_type& __v, const allocator_type& __a);
1292 template <class _InputIter>
1293 deque(_InputIter __f, _InputIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001294 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001295 template <class _InputIter>
1296 deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001297 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001298 deque(const deque& __c);
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05001299 deque(const deque& __c, const __identity_t<allocator_type>& __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001300
1301 deque& operator=(const deque& __c);
Eric Fiselier212e3f22017-04-16 03:17:01 +00001302
1303#ifndef _LIBCPP_CXX03_LANG
1304 deque(initializer_list<value_type> __il);
1305 deque(initializer_list<value_type> __il, const allocator_type& __a);
1306
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001307 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001308 deque& operator=(initializer_list<value_type> __il) {assign(__il); return *this;}
1309
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001310 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantca2fc222011-06-02 20:00:14 +00001311 deque(deque&& __c) _NOEXCEPT_(is_nothrow_move_constructible<__base>::value);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001312 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05001313 deque(deque&& __c, const __identity_t<allocator_type>& __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001314 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantca2fc222011-06-02 20:00:14 +00001315 deque& operator=(deque&& __c)
Howard Hinnant4931e092011-06-02 21:38:57 +00001316 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1317 is_nothrow_move_assignable<allocator_type>::value);
Eric Fiselier212e3f22017-04-16 03:17:01 +00001318
1319 _LIBCPP_INLINE_VISIBILITY
1320 void assign(initializer_list<value_type> __il) {assign(__il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001321#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001322
1323 template <class _InputIter>
1324 void assign(_InputIter __f, _InputIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001325 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value &&
1326 !__is_cpp17_random_access_iterator<_InputIter>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001327 template <class _RAIter>
1328 void assign(_RAIter __f, _RAIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001329 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001330 void assign(size_type __n, const value_type& __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001331
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001332 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001333 allocator_type get_allocator() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001334
1335 // iterators:
1336
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001337 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001338 iterator begin() _NOEXCEPT {return __base::begin();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001339 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001340 const_iterator begin() const _NOEXCEPT {return __base::begin();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001341 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001342 iterator end() _NOEXCEPT {return __base::end();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001343 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001344 const_iterator end() const _NOEXCEPT {return __base::end();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001345
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001346 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001347 reverse_iterator rbegin() _NOEXCEPT
1348 {return reverse_iterator(__base::end());}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001349 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001350 const_reverse_iterator rbegin() const _NOEXCEPT
1351 {return const_reverse_iterator(__base::end());}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001352 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001353 reverse_iterator rend() _NOEXCEPT
1354 {return reverse_iterator(__base::begin());}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001355 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001356 const_reverse_iterator rend() const _NOEXCEPT
1357 {return const_reverse_iterator(__base::begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001358
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001359 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001360 const_iterator cbegin() const _NOEXCEPT
1361 {return __base::begin();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001362 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001363 const_iterator cend() const _NOEXCEPT
1364 {return __base::end();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001365 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001366 const_reverse_iterator crbegin() const _NOEXCEPT
1367 {return const_reverse_iterator(__base::end());}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001368 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001369 const_reverse_iterator crend() const _NOEXCEPT
1370 {return const_reverse_iterator(__base::begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001371
1372 // capacity:
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001373 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001374 size_type size() const _NOEXCEPT {return __base::size();}
1375 _LIBCPP_INLINE_VISIBILITY
1376 size_type max_size() const _NOEXCEPT
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001377 {return _VSTD::min<size_type>(
Eric Fiselierb5d9f442016-11-23 01:18:56 +00001378 __alloc_traits::max_size(__base::__alloc()),
1379 numeric_limits<difference_type>::max());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001380 void resize(size_type __n);
1381 void resize(size_type __n, const value_type& __v);
Howard Hinnant4931e092011-06-02 21:38:57 +00001382 void shrink_to_fit() _NOEXCEPT;
Marshall Clow425f5752017-11-15 05:51:26 +00001383 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001384 bool empty() const _NOEXCEPT {return __base::size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001385
1386 // element access:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001387 _LIBCPP_INLINE_VISIBILITY
Marshall Clow8d7c9f12019-03-14 21:56:57 +00001388 reference operator[](size_type __i) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001389 _LIBCPP_INLINE_VISIBILITY
Marshall Clow8d7c9f12019-03-14 21:56:57 +00001390 const_reference operator[](size_type __i) const _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001391 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001392 reference at(size_type __i);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001393 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001394 const_reference at(size_type __i) const;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001395 _LIBCPP_INLINE_VISIBILITY
Marshall Clow05cf6692019-03-19 03:30:07 +00001396 reference front() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001397 _LIBCPP_INLINE_VISIBILITY
Marshall Clow05cf6692019-03-19 03:30:07 +00001398 const_reference front() const _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001399 _LIBCPP_INLINE_VISIBILITY
Marshall Clow05cf6692019-03-19 03:30:07 +00001400 reference back() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001401 _LIBCPP_INLINE_VISIBILITY
Marshall Clow05cf6692019-03-19 03:30:07 +00001402 const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001403
1404 // 23.2.2.3 modifiers:
1405 void push_front(const value_type& __v);
1406 void push_back(const value_type& __v);
Eric Fiselier212e3f22017-04-16 03:17:01 +00001407#ifndef _LIBCPP_CXX03_LANG
Marshall Clowea52cc42017-01-24 23:09:12 +00001408#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001409 template <class... _Args> reference emplace_front(_Args&&... __args);
Marshall Clowea52cc42017-01-24 23:09:12 +00001410 template <class... _Args> reference emplace_back (_Args&&... __args);
1411#else
1412 template <class... _Args> void emplace_front(_Args&&... __args);
1413 template <class... _Args> void emplace_back (_Args&&... __args);
1414#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001415 template <class... _Args> iterator emplace(const_iterator __p, _Args&&... __args);
Eric Fiselier212e3f22017-04-16 03:17:01 +00001416
Howard Hinnantc51e1022010-05-11 19:42:16 +00001417 void push_front(value_type&& __v);
1418 void push_back(value_type&& __v);
1419 iterator insert(const_iterator __p, value_type&& __v);
Eric Fiselier212e3f22017-04-16 03:17:01 +00001420
1421 _LIBCPP_INLINE_VISIBILITY
1422 iterator insert(const_iterator __p, initializer_list<value_type> __il)
1423 {return insert(__p, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001424#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001425 iterator insert(const_iterator __p, const value_type& __v);
1426 iterator insert(const_iterator __p, size_type __n, const value_type& __v);
1427 template <class _InputIter>
Marshall Clow6b612cf2015-01-22 18:33:29 +00001428 iterator insert(const_iterator __p, _InputIter __f, _InputIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001429 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value
1430 &&!__is_cpp17_forward_iterator<_InputIter>::value>::type* = 0);
Marshall Clow6b612cf2015-01-22 18:33:29 +00001431 template <class _ForwardIterator>
1432 iterator insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001433 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value
1434 &&!__is_cpp17_bidirectional_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001435 template <class _BiIter>
Marshall Clow6b612cf2015-01-22 18:33:29 +00001436 iterator insert(const_iterator __p, _BiIter __f, _BiIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001437 typename enable_if<__is_cpp17_bidirectional_iterator<_BiIter>::value>::type* = 0);
Eric Fiselier212e3f22017-04-16 03:17:01 +00001438
Howard Hinnantc51e1022010-05-11 19:42:16 +00001439 void pop_front();
1440 void pop_back();
1441 iterator erase(const_iterator __p);
1442 iterator erase(const_iterator __f, const_iterator __l);
1443
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001444 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantca2fc222011-06-02 20:00:14 +00001445 void swap(deque& __c)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001446#if _LIBCPP_STD_VER >= 14
1447 _NOEXCEPT;
1448#else
Howard Hinnantca2fc222011-06-02 20:00:14 +00001449 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1450 __is_nothrow_swappable<allocator_type>::value);
Marshall Clow8982dcd2015-07-13 20:04:56 +00001451#endif
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001452 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001453 void clear() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001454
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001455 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001456 bool __invariants() const {return __base::__invariants();}
Eric Fiselier27e03622019-08-01 23:11:18 +00001457
Howard Hinnantdcb73a32013-06-23 21:17:24 +00001458 typedef typename __base::__map_const_pointer __map_const_pointer;
1459
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001460 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001461 static size_type __recommend_blocks(size_type __n)
1462 {
1463 return __n / __base::__block_size + (__n % __base::__block_size != 0);
1464 }
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001465 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001466 size_type __capacity() const
1467 {
1468 return __base::__map_.size() == 0 ? 0 : __base::__map_.size() * __base::__block_size - 1;
1469 }
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001470 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier27e03622019-08-01 23:11:18 +00001471 size_type __block_count() const
1472 {
1473 return __base::__map_.size();
1474 }
1475
1476 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001477 size_type __front_spare() const
1478 {
1479 return __base::__start_;
1480 }
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001481 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier27e03622019-08-01 23:11:18 +00001482 size_type __front_spare_blocks() const {
1483 return __front_spare() / __base::__block_size;
1484 }
1485 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001486 size_type __back_spare() const
1487 {
1488 return __capacity() - (__base::__start_ + __base::size());
1489 }
Eric Fiselier27e03622019-08-01 23:11:18 +00001490 _LIBCPP_INLINE_VISIBILITY
1491 size_type __back_spare_blocks() const {
1492 return __back_spare() / __base::__block_size;
1493 }
1494
1495 private:
1496 _LIBCPP_INLINE_VISIBILITY
1497 bool __maybe_remove_front_spare(bool __keep_one = true) {
1498 if (__front_spare_blocks() >= 2 || (!__keep_one && __front_spare_blocks())) {
1499 __alloc_traits::deallocate(__base::__alloc(), __base::__map_.front(),
1500 __base::__block_size);
1501 __base::__map_.pop_front();
1502 __base::__start_ -= __base::__block_size;
1503 return true;
1504 }
1505 return false;
1506 }
1507
1508 _LIBCPP_INLINE_VISIBILITY
1509 bool __maybe_remove_back_spare(bool __keep_one = true) {
1510 if (__back_spare_blocks() >= 2 || (!__keep_one && __back_spare_blocks())) {
1511 __alloc_traits::deallocate(__base::__alloc(), __base::__map_.back(),
1512 __base::__block_size);
1513 __base::__map_.pop_back();
1514 return true;
1515 }
1516 return false;
1517 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001518
1519 template <class _InpIter>
1520 void __append(_InpIter __f, _InpIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001521 typename enable_if<__is_cpp17_input_iterator<_InpIter>::value &&
1522 !__is_cpp17_forward_iterator<_InpIter>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001523 template <class _ForIter>
1524 void __append(_ForIter __f, _ForIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001525 typename enable_if<__is_cpp17_forward_iterator<_ForIter>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001526 void __append(size_type __n);
1527 void __append(size_type __n, const value_type& __v);
1528 void __erase_to_end(const_iterator __f);
1529 void __add_front_capacity();
1530 void __add_front_capacity(size_type __n);
1531 void __add_back_capacity();
1532 void __add_back_capacity(size_type __n);
1533 iterator __move_and_check(iterator __f, iterator __l, iterator __r,
1534 const_pointer& __vt);
1535 iterator __move_backward_and_check(iterator __f, iterator __l, iterator __r,
1536 const_pointer& __vt);
1537 void __move_construct_and_check(iterator __f, iterator __l,
1538 iterator __r, const_pointer& __vt);
1539 void __move_construct_backward_and_check(iterator __f, iterator __l,
1540 iterator __r, const_pointer& __vt);
1541
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001542 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001543 void __copy_assign_alloc(const deque& __c)
1544 {__copy_assign_alloc(__c, integral_constant<bool,
1545 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1546
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001547 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001548 void __copy_assign_alloc(const deque& __c, true_type)
1549 {
1550 if (__base::__alloc() != __c.__alloc())
1551 {
1552 clear();
1553 shrink_to_fit();
1554 }
1555 __base::__alloc() = __c.__alloc();
1556 __base::__map_.__alloc() = __c.__map_.__alloc();
1557 }
1558
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001559 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001560 void __copy_assign_alloc(const deque&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001561 {}
1562
Howard Hinnant4931e092011-06-02 21:38:57 +00001563 void __move_assign(deque& __c, true_type)
1564 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001565 void __move_assign(deque& __c, false_type);
1566};
1567
Louis Dionned59f8a52021-08-17 11:59:07 -04001568#if _LIBCPP_STD_VER >= 17
Marshall Clow4cc3a452018-05-18 23:44:13 +00001569template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001570 class _Alloc = allocator<__iter_value_type<_InputIterator>>,
1571 class = _EnableIf<__is_allocator<_Alloc>::value>
Marshall Clow4cc3a452018-05-18 23:44:13 +00001572 >
1573deque(_InputIterator, _InputIterator)
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001574 -> deque<__iter_value_type<_InputIterator>, _Alloc>;
Marshall Clow4cc3a452018-05-18 23:44:13 +00001575
1576template<class _InputIterator,
1577 class _Alloc,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001578 class = _EnableIf<__is_allocator<_Alloc>::value>
Marshall Clow4cc3a452018-05-18 23:44:13 +00001579 >
1580deque(_InputIterator, _InputIterator, _Alloc)
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001581 -> deque<__iter_value_type<_InputIterator>, _Alloc>;
Marshall Clow4cc3a452018-05-18 23:44:13 +00001582#endif
1583
1584
Howard Hinnantc51e1022010-05-11 19:42:16 +00001585template <class _Tp, class _Allocator>
1586deque<_Tp, _Allocator>::deque(size_type __n)
1587{
1588 if (__n > 0)
1589 __append(__n);
1590}
1591
Marshall Clowbc4fcb42013-09-07 16:16:19 +00001592#if _LIBCPP_STD_VER > 11
1593template <class _Tp, class _Allocator>
1594deque<_Tp, _Allocator>::deque(size_type __n, const _Allocator& __a)
1595 : __base(__a)
1596{
1597 if (__n > 0)
1598 __append(__n);
1599}
1600#endif
1601
Howard Hinnantc51e1022010-05-11 19:42:16 +00001602template <class _Tp, class _Allocator>
1603deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v)
1604{
1605 if (__n > 0)
1606 __append(__n, __v);
1607}
1608
1609template <class _Tp, class _Allocator>
1610deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v, const allocator_type& __a)
1611 : __base(__a)
1612{
1613 if (__n > 0)
1614 __append(__n, __v);
1615}
1616
1617template <class _Tp, class _Allocator>
1618template <class _InputIter>
1619deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001620 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001621{
1622 __append(__f, __l);
1623}
1624
1625template <class _Tp, class _Allocator>
1626template <class _InputIter>
1627deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001628 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001629 : __base(__a)
1630{
1631 __append(__f, __l);
1632}
1633
1634template <class _Tp, class _Allocator>
1635deque<_Tp, _Allocator>::deque(const deque& __c)
1636 : __base(__alloc_traits::select_on_container_copy_construction(__c.__alloc()))
1637{
1638 __append(__c.begin(), __c.end());
1639}
1640
1641template <class _Tp, class _Allocator>
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05001642deque<_Tp, _Allocator>::deque(const deque& __c, const __identity_t<allocator_type>& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001643 : __base(__a)
1644{
1645 __append(__c.begin(), __c.end());
1646}
1647
Eric Fiselier212e3f22017-04-16 03:17:01 +00001648template <class _Tp, class _Allocator>
1649deque<_Tp, _Allocator>&
1650deque<_Tp, _Allocator>::operator=(const deque& __c)
1651{
1652 if (this != &__c)
1653 {
1654 __copy_assign_alloc(__c);
1655 assign(__c.begin(), __c.end());
1656 }
1657 return *this;
1658}
1659
1660#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00001661
Howard Hinnantc51e1022010-05-11 19:42:16 +00001662template <class _Tp, class _Allocator>
1663deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il)
1664{
1665 __append(__il.begin(), __il.end());
1666}
1667
1668template <class _Tp, class _Allocator>
1669deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il, const allocator_type& __a)
1670 : __base(__a)
1671{
1672 __append(__il.begin(), __il.end());
1673}
1674
Howard Hinnantc51e1022010-05-11 19:42:16 +00001675template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001676inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001677deque<_Tp, _Allocator>::deque(deque&& __c)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001678 _NOEXCEPT_(is_nothrow_move_constructible<__base>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001679 : __base(_VSTD::move(__c))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001680{
1681}
1682
1683template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001684inline
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05001685deque<_Tp, _Allocator>::deque(deque&& __c, const __identity_t<allocator_type>& __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001686 : __base(_VSTD::move(__c), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001687{
1688 if (__a != __c.__alloc())
1689 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001690 typedef move_iterator<iterator> _Ip;
1691 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001692 }
1693}
1694
1695template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001696inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001697deque<_Tp, _Allocator>&
1698deque<_Tp, _Allocator>::operator=(deque&& __c)
Howard Hinnant4931e092011-06-02 21:38:57 +00001699 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1700 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001701{
1702 __move_assign(__c, integral_constant<bool,
1703 __alloc_traits::propagate_on_container_move_assignment::value>());
1704 return *this;
1705}
1706
1707template <class _Tp, class _Allocator>
1708void
1709deque<_Tp, _Allocator>::__move_assign(deque& __c, false_type)
1710{
1711 if (__base::__alloc() != __c.__alloc())
1712 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001713 typedef move_iterator<iterator> _Ip;
1714 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001715 }
1716 else
1717 __move_assign(__c, true_type());
1718}
1719
1720template <class _Tp, class _Allocator>
1721void
1722deque<_Tp, _Allocator>::__move_assign(deque& __c, true_type)
Howard Hinnant4931e092011-06-02 21:38:57 +00001723 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001724{
1725 clear();
1726 shrink_to_fit();
1727 __base::__move_assign(__c);
1728}
1729
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001730#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001731
1732template <class _Tp, class _Allocator>
1733template <class _InputIter>
1734void
1735deque<_Tp, _Allocator>::assign(_InputIter __f, _InputIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001736 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value &&
1737 !__is_cpp17_random_access_iterator<_InputIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001738{
1739 iterator __i = __base::begin();
1740 iterator __e = __base::end();
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001741 for (; __f != __l && __i != __e; ++__f, (void) ++__i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001742 *__i = *__f;
1743 if (__f != __l)
1744 __append(__f, __l);
1745 else
1746 __erase_to_end(__i);
1747}
1748
1749template <class _Tp, class _Allocator>
1750template <class _RAIter>
1751void
1752deque<_Tp, _Allocator>::assign(_RAIter __f, _RAIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001753 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001754{
1755 if (static_cast<size_type>(__l - __f) > __base::size())
1756 {
1757 _RAIter __m = __f + __base::size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001758 _VSTD::copy(__f, __m, __base::begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001759 __append(__m, __l);
1760 }
1761 else
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001762 __erase_to_end(_VSTD::copy(__f, __l, __base::begin()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001763}
1764
1765template <class _Tp, class _Allocator>
1766void
1767deque<_Tp, _Allocator>::assign(size_type __n, const value_type& __v)
1768{
1769 if (__n > __base::size())
1770 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001771 _VSTD::fill_n(__base::begin(), __base::size(), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001772 __n -= __base::size();
1773 __append(__n, __v);
1774 }
1775 else
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001776 __erase_to_end(_VSTD::fill_n(__base::begin(), __n, __v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001777}
1778
1779template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001780inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001781_Allocator
Howard Hinnantda2df912011-06-02 16:10:22 +00001782deque<_Tp, _Allocator>::get_allocator() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001783{
1784 return __base::__alloc();
1785}
1786
1787template <class _Tp, class _Allocator>
1788void
1789deque<_Tp, _Allocator>::resize(size_type __n)
1790{
1791 if (__n > __base::size())
1792 __append(__n - __base::size());
1793 else if (__n < __base::size())
1794 __erase_to_end(__base::begin() + __n);
1795}
1796
1797template <class _Tp, class _Allocator>
1798void
1799deque<_Tp, _Allocator>::resize(size_type __n, const value_type& __v)
1800{
1801 if (__n > __base::size())
1802 __append(__n - __base::size(), __v);
1803 else if (__n < __base::size())
1804 __erase_to_end(__base::begin() + __n);
1805}
1806
1807template <class _Tp, class _Allocator>
1808void
Howard Hinnant4931e092011-06-02 21:38:57 +00001809deque<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001810{
1811 allocator_type& __a = __base::__alloc();
1812 if (empty())
1813 {
1814 while (__base::__map_.size() > 0)
1815 {
1816 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
1817 __base::__map_.pop_back();
1818 }
1819 __base::__start_ = 0;
1820 }
1821 else
1822 {
Eric Fiselier27e03622019-08-01 23:11:18 +00001823 __maybe_remove_front_spare(/*__keep_one=*/false);
1824 __maybe_remove_back_spare(/*__keep_one=*/false);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001825 }
1826 __base::__map_.shrink_to_fit();
1827}
1828
1829template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001830inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001831typename deque<_Tp, _Allocator>::reference
Marshall Clow8d7c9f12019-03-14 21:56:57 +00001832deque<_Tp, _Allocator>::operator[](size_type __i) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001833{
1834 size_type __p = __base::__start_ + __i;
1835 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1836}
1837
1838template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001839inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001840typename deque<_Tp, _Allocator>::const_reference
Marshall Clow8d7c9f12019-03-14 21:56:57 +00001841deque<_Tp, _Allocator>::operator[](size_type __i) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001842{
1843 size_type __p = __base::__start_ + __i;
1844 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1845}
1846
1847template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001848inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001849typename deque<_Tp, _Allocator>::reference
1850deque<_Tp, _Allocator>::at(size_type __i)
1851{
1852 if (__i >= __base::size())
Louis Dionnef2cd0a12021-08-19 12:15:31 -04001853 _VSTD::__throw_out_of_range("deque");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001854 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
1861deque<_Tp, _Allocator>::at(size_type __i) const
1862{
1863 if (__i >= __base::size())
Louis Dionnef2cd0a12021-08-19 12:15:31 -04001864 _VSTD::__throw_out_of_range("deque");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001865 size_type __p = __base::__start_ + __i;
1866 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1867}
1868
1869template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001870inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001871typename deque<_Tp, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00001872deque<_Tp, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001873{
1874 return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1875 + __base::__start_ % __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
Marshall Clow05cf6692019-03-19 03:30:07 +00001881deque<_Tp, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001882{
1883 return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1884 + __base::__start_ % __base::__block_size);
1885}
1886
1887template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001888inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001889typename deque<_Tp, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00001890deque<_Tp, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001891{
1892 size_type __p = __base::size() + __base::__start_ - 1;
1893 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1894}
1895
1896template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001897inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001898typename deque<_Tp, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00001899deque<_Tp, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001900{
1901 size_type __p = __base::size() + __base::__start_ - 1;
1902 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1903}
1904
1905template <class _Tp, class _Allocator>
1906void
1907deque<_Tp, _Allocator>::push_back(const value_type& __v)
1908{
1909 allocator_type& __a = __base::__alloc();
1910 if (__back_spare() == 0)
1911 __add_back_capacity();
1912 // __back_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001913 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001914 ++__base::size();
1915}
1916
Eric Fiselier212e3f22017-04-16 03:17:01 +00001917template <class _Tp, class _Allocator>
1918void
1919deque<_Tp, _Allocator>::push_front(const value_type& __v)
1920{
1921 allocator_type& __a = __base::__alloc();
1922 if (__front_spare() == 0)
1923 __add_front_capacity();
1924 // __front_spare() >= 1
1925 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v);
1926 --__base::__start_;
1927 ++__base::size();
1928}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001929
Eric Fiselier212e3f22017-04-16 03:17:01 +00001930#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001931template <class _Tp, class _Allocator>
1932void
1933deque<_Tp, _Allocator>::push_back(value_type&& __v)
1934{
1935 allocator_type& __a = __base::__alloc();
1936 if (__back_spare() == 0)
1937 __add_back_capacity();
1938 // __back_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001939 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001940 ++__base::size();
1941}
1942
1943template <class _Tp, class _Allocator>
1944template <class... _Args>
Marshall Clowea52cc42017-01-24 23:09:12 +00001945#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001946typename deque<_Tp, _Allocator>::reference
Marshall Clowea52cc42017-01-24 23:09:12 +00001947#else
1948void
1949#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001950deque<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1951{
1952 allocator_type& __a = __base::__alloc();
1953 if (__back_spare() == 0)
1954 __add_back_capacity();
1955 // __back_spare() >= 1
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001956 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()),
1957 _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001958 ++__base::size();
Marshall Clowea52cc42017-01-24 23:09:12 +00001959#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001960 return *--__base::end();
Marshall Clowea52cc42017-01-24 23:09:12 +00001961#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001962}
1963
Howard Hinnantc51e1022010-05-11 19:42:16 +00001964template <class _Tp, class _Allocator>
1965void
1966deque<_Tp, _Allocator>::push_front(value_type&& __v)
1967{
1968 allocator_type& __a = __base::__alloc();
1969 if (__front_spare() == 0)
1970 __add_front_capacity();
1971 // __front_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001972 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001973 --__base::__start_;
1974 ++__base::size();
1975}
1976
Howard Hinnant74279a52010-09-04 23:28:19 +00001977
Howard Hinnantc51e1022010-05-11 19:42:16 +00001978template <class _Tp, class _Allocator>
1979template <class... _Args>
Marshall Clowea52cc42017-01-24 23:09:12 +00001980#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001981typename deque<_Tp, _Allocator>::reference
Marshall Clowea52cc42017-01-24 23:09:12 +00001982#else
1983void
1984#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001985deque<_Tp, _Allocator>::emplace_front(_Args&&... __args)
1986{
1987 allocator_type& __a = __base::__alloc();
1988 if (__front_spare() == 0)
1989 __add_front_capacity();
1990 // __front_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001991 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001992 --__base::__start_;
1993 ++__base::size();
Marshall Clowea52cc42017-01-24 23:09:12 +00001994#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001995 return *__base::begin();
Marshall Clowea52cc42017-01-24 23:09:12 +00001996#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001997}
1998
Eric Fiselier212e3f22017-04-16 03:17:01 +00001999template <class _Tp, class _Allocator>
2000typename deque<_Tp, _Allocator>::iterator
2001deque<_Tp, _Allocator>::insert(const_iterator __p, value_type&& __v)
2002{
2003 size_type __pos = __p - __base::begin();
2004 size_type __to_end = __base::size() - __pos;
2005 allocator_type& __a = __base::__alloc();
2006 if (__pos < __to_end)
2007 { // insert by shifting things backward
2008 if (__front_spare() == 0)
2009 __add_front_capacity();
2010 // __front_spare() >= 1
2011 if (__pos == 0)
2012 {
2013 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v));
2014 --__base::__start_;
2015 ++__base::size();
2016 }
2017 else
2018 {
2019 iterator __b = __base::begin();
2020 iterator __bm1 = _VSTD::prev(__b);
2021 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
2022 --__base::__start_;
2023 ++__base::size();
2024 if (__pos > 1)
2025 __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
2026 *__b = _VSTD::move(__v);
2027 }
2028 }
2029 else
2030 { // insert by shifting things forward
2031 if (__back_spare() == 0)
2032 __add_back_capacity();
2033 // __back_capacity >= 1
2034 size_type __de = __base::size() - __pos;
2035 if (__de == 0)
2036 {
2037 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v));
2038 ++__base::size();
2039 }
2040 else
2041 {
2042 iterator __e = __base::end();
2043 iterator __em1 = _VSTD::prev(__e);
2044 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
2045 ++__base::size();
2046 if (__de > 1)
2047 __e = _VSTD::move_backward(__e - __de, __em1, __e);
2048 *--__e = _VSTD::move(__v);
2049 }
2050 }
2051 return __base::begin() + __pos;
2052}
2053
2054template <class _Tp, class _Allocator>
2055template <class... _Args>
2056typename deque<_Tp, _Allocator>::iterator
2057deque<_Tp, _Allocator>::emplace(const_iterator __p, _Args&&... __args)
2058{
2059 size_type __pos = __p - __base::begin();
2060 size_type __to_end = __base::size() - __pos;
2061 allocator_type& __a = __base::__alloc();
2062 if (__pos < __to_end)
2063 { // insert by shifting things backward
2064 if (__front_spare() == 0)
2065 __add_front_capacity();
2066 // __front_spare() >= 1
2067 if (__pos == 0)
2068 {
2069 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...);
2070 --__base::__start_;
2071 ++__base::size();
2072 }
2073 else
2074 {
2075 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
2076 iterator __b = __base::begin();
2077 iterator __bm1 = _VSTD::prev(__b);
2078 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
2079 --__base::__start_;
2080 ++__base::size();
2081 if (__pos > 1)
2082 __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
2083 *__b = _VSTD::move(__tmp.get());
2084 }
2085 }
2086 else
2087 { // insert by shifting things forward
2088 if (__back_spare() == 0)
2089 __add_back_capacity();
2090 // __back_capacity >= 1
2091 size_type __de = __base::size() - __pos;
2092 if (__de == 0)
2093 {
2094 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::forward<_Args>(__args)...);
2095 ++__base::size();
2096 }
2097 else
2098 {
2099 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
2100 iterator __e = __base::end();
2101 iterator __em1 = _VSTD::prev(__e);
2102 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
2103 ++__base::size();
2104 if (__de > 1)
2105 __e = _VSTD::move_backward(__e - __de, __em1, __e);
2106 *--__e = _VSTD::move(__tmp.get());
2107 }
2108 }
2109 return __base::begin() + __pos;
2110}
2111
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002112#endif // _LIBCPP_CXX03_LANG
Eric Fiselier212e3f22017-04-16 03:17:01 +00002113
Howard Hinnantc51e1022010-05-11 19:42:16 +00002114
2115template <class _Tp, class _Allocator>
2116typename deque<_Tp, _Allocator>::iterator
2117deque<_Tp, _Allocator>::insert(const_iterator __p, const value_type& __v)
2118{
2119 size_type __pos = __p - __base::begin();
2120 size_type __to_end = __base::size() - __pos;
2121 allocator_type& __a = __base::__alloc();
2122 if (__pos < __to_end)
2123 { // insert by shifting things backward
2124 if (__front_spare() == 0)
2125 __add_front_capacity();
2126 // __front_spare() >= 1
2127 if (__pos == 0)
2128 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002129 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002130 --__base::__start_;
2131 ++__base::size();
2132 }
2133 else
2134 {
2135 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2136 iterator __b = __base::begin();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002137 iterator __bm1 = _VSTD::prev(__b);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002138 if (__vt == pointer_traits<const_pointer>::pointer_to(*__b))
2139 __vt = pointer_traits<const_pointer>::pointer_to(*__bm1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002140 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002141 --__base::__start_;
2142 ++__base::size();
2143 if (__pos > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002144 __b = __move_and_check(_VSTD::next(__b), __b + __pos, __b, __vt);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002145 *__b = *__vt;
2146 }
2147 }
2148 else
2149 { // insert by shifting things forward
2150 if (__back_spare() == 0)
2151 __add_back_capacity();
2152 // __back_capacity >= 1
2153 size_type __de = __base::size() - __pos;
2154 if (__de == 0)
2155 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002156 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002157 ++__base::size();
2158 }
2159 else
2160 {
2161 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2162 iterator __e = __base::end();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002163 iterator __em1 = _VSTD::prev(__e);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002164 if (__vt == pointer_traits<const_pointer>::pointer_to(*__em1))
2165 __vt = pointer_traits<const_pointer>::pointer_to(*__e);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002166 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002167 ++__base::size();
2168 if (__de > 1)
2169 __e = __move_backward_and_check(__e - __de, __em1, __e, __vt);
2170 *--__e = *__vt;
2171 }
2172 }
2173 return __base::begin() + __pos;
2174}
2175
Howard Hinnantc51e1022010-05-11 19:42:16 +00002176template <class _Tp, class _Allocator>
2177typename deque<_Tp, _Allocator>::iterator
2178deque<_Tp, _Allocator>::insert(const_iterator __p, size_type __n, const value_type& __v)
2179{
2180 size_type __pos = __p - __base::begin();
2181 size_type __to_end = __base::size() - __pos;
2182 allocator_type& __a = __base::__alloc();
2183 if (__pos < __to_end)
2184 { // insert by shifting things backward
2185 if (__n > __front_spare())
2186 __add_front_capacity(__n - __front_spare());
2187 // __n <= __front_spare()
Howard Hinnantc51e1022010-05-11 19:42:16 +00002188 iterator __old_begin = __base::begin();
2189 iterator __i = __old_begin;
2190 if (__n > __pos)
2191 {
2192 for (size_type __m = __n - __pos; __m; --__m, --__base::__start_, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002193 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002194 __n = __pos;
2195 }
2196 if (__n > 0)
2197 {
2198 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2199 iterator __obn = __old_begin + __n;
2200 __move_construct_backward_and_check(__old_begin, __obn, __i, __vt);
2201 if (__n < __pos)
2202 __old_begin = __move_and_check(__obn, __old_begin + __pos, __old_begin, __vt);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002203 _VSTD::fill_n(__old_begin, __n, *__vt);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002204 }
2205 }
2206 else
2207 { // insert by shifting things forward
2208 size_type __back_capacity = __back_spare();
2209 if (__n > __back_capacity)
2210 __add_back_capacity(__n - __back_capacity);
2211 // __n <= __back_capacity
Howard Hinnantc51e1022010-05-11 19:42:16 +00002212 iterator __old_end = __base::end();
2213 iterator __i = __old_end;
2214 size_type __de = __base::size() - __pos;
2215 if (__n > __de)
2216 {
2217 for (size_type __m = __n - __de; __m; --__m, ++__i, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002218 __alloc_traits::construct(__a, _VSTD::addressof(*__i), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002219 __n = __de;
2220 }
2221 if (__n > 0)
2222 {
2223 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2224 iterator __oen = __old_end - __n;
2225 __move_construct_and_check(__oen, __old_end, __i, __vt);
2226 if (__n < __de)
2227 __old_end = __move_backward_and_check(__old_end - __de, __oen, __old_end, __vt);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002228 _VSTD::fill_n(__old_end - __n, __n, *__vt);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002229 }
2230 }
2231 return __base::begin() + __pos;
2232}
2233
2234template <class _Tp, class _Allocator>
2235template <class _InputIter>
2236typename deque<_Tp, _Allocator>::iterator
2237deque<_Tp, _Allocator>::insert(const_iterator __p, _InputIter __f, _InputIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002238 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value
2239 &&!__is_cpp17_forward_iterator<_InputIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002240{
2241 __split_buffer<value_type, allocator_type&> __buf(__base::__alloc());
2242 __buf.__construct_at_end(__f, __l);
2243 typedef typename __split_buffer<value_type, allocator_type&>::iterator __bi;
2244 return insert(__p, move_iterator<__bi>(__buf.begin()), move_iterator<__bi>(__buf.end()));
2245}
2246
2247template <class _Tp, class _Allocator>
Marshall Clow6b612cf2015-01-22 18:33:29 +00002248template <class _ForwardIterator>
2249typename deque<_Tp, _Allocator>::iterator
2250deque<_Tp, _Allocator>::insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002251 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value
2252 &&!__is_cpp17_bidirectional_iterator<_ForwardIterator>::value>::type*)
Marshall Clow6b612cf2015-01-22 18:33:29 +00002253{
2254 size_type __n = _VSTD::distance(__f, __l);
2255 __split_buffer<value_type, allocator_type&> __buf(__n, 0, __base::__alloc());
2256 __buf.__construct_at_end(__f, __l);
2257 typedef typename __split_buffer<value_type, allocator_type&>::iterator __fwd;
2258 return insert(__p, move_iterator<__fwd>(__buf.begin()), move_iterator<__fwd>(__buf.end()));
2259}
2260
2261template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002262template <class _BiIter>
2263typename deque<_Tp, _Allocator>::iterator
2264deque<_Tp, _Allocator>::insert(const_iterator __p, _BiIter __f, _BiIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002265 typename enable_if<__is_cpp17_bidirectional_iterator<_BiIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002266{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002267 size_type __n = _VSTD::distance(__f, __l);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002268 size_type __pos = __p - __base::begin();
2269 size_type __to_end = __base::size() - __pos;
2270 allocator_type& __a = __base::__alloc();
2271 if (__pos < __to_end)
2272 { // insert by shifting things backward
2273 if (__n > __front_spare())
2274 __add_front_capacity(__n - __front_spare());
2275 // __n <= __front_spare()
Howard Hinnantc51e1022010-05-11 19:42:16 +00002276 iterator __old_begin = __base::begin();
2277 iterator __i = __old_begin;
2278 _BiIter __m = __f;
2279 if (__n > __pos)
2280 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002281 __m = __pos < __n / 2 ? _VSTD::prev(__l, __pos) : _VSTD::next(__f, __n - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002282 for (_BiIter __j = __m; __j != __f; --__base::__start_, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002283 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), *--__j);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002284 __n = __pos;
2285 }
2286 if (__n > 0)
2287 {
2288 iterator __obn = __old_begin + __n;
2289 for (iterator __j = __obn; __j != __old_begin;)
2290 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002291 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), _VSTD::move(*--__j));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002292 --__base::__start_;
2293 ++__base::size();
2294 }
2295 if (__n < __pos)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002296 __old_begin = _VSTD::move(__obn, __old_begin + __pos, __old_begin);
2297 _VSTD::copy(__m, __l, __old_begin);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002298 }
2299 }
2300 else
2301 { // insert by shifting things forward
2302 size_type __back_capacity = __back_spare();
2303 if (__n > __back_capacity)
2304 __add_back_capacity(__n - __back_capacity);
2305 // __n <= __back_capacity
Howard Hinnantc51e1022010-05-11 19:42:16 +00002306 iterator __old_end = __base::end();
2307 iterator __i = __old_end;
2308 _BiIter __m = __l;
2309 size_type __de = __base::size() - __pos;
2310 if (__n > __de)
2311 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002312 __m = __de < __n / 2 ? _VSTD::next(__f, __de) : _VSTD::prev(__l, __n - __de);
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002313 for (_BiIter __j = __m; __j != __l; ++__i, (void) ++__j, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002314 __alloc_traits::construct(__a, _VSTD::addressof(*__i), *__j);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002315 __n = __de;
2316 }
2317 if (__n > 0)
2318 {
2319 iterator __oen = __old_end - __n;
2320 for (iterator __j = __oen; __j != __old_end; ++__i, ++__j, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002321 __alloc_traits::construct(__a, _VSTD::addressof(*__i), _VSTD::move(*__j));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002322 if (__n < __de)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002323 __old_end = _VSTD::move_backward(__old_end - __de, __oen, __old_end);
2324 _VSTD::copy_backward(__f, __m, __old_end);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002325 }
2326 }
2327 return __base::begin() + __pos;
2328}
2329
2330template <class _Tp, class _Allocator>
2331template <class _InpIter>
2332void
2333deque<_Tp, _Allocator>::__append(_InpIter __f, _InpIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002334 typename enable_if<__is_cpp17_input_iterator<_InpIter>::value &&
2335 !__is_cpp17_forward_iterator<_InpIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002336{
2337 for (; __f != __l; ++__f)
Eric Fiselier96919722017-10-17 13:03:17 +00002338#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002339 push_back(*__f);
Eric Fiselier96919722017-10-17 13:03:17 +00002340#else
2341 emplace_back(*__f);
2342#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002343}
2344
2345template <class _Tp, class _Allocator>
2346template <class _ForIter>
2347void
2348deque<_Tp, _Allocator>::__append(_ForIter __f, _ForIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002349 typename enable_if<__is_cpp17_forward_iterator<_ForIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002350{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002351 size_type __n = _VSTD::distance(__f, __l);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002352 allocator_type& __a = __base::__alloc();
2353 size_type __back_capacity = __back_spare();
2354 if (__n > __back_capacity)
2355 __add_back_capacity(__n - __back_capacity);
2356 // __n <= __back_capacity
Eric Fiselierc52db212019-08-12 07:51:05 +00002357 for (__deque_block_range __br : __deque_range(__base::end(), __base::end() + __n)) {
2358 _ConstructTransaction __tx(this, __br);
2359 for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, (void)++__f) {
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05002360 __alloc_traits::construct(__a, _VSTD::__to_address(__tx.__pos_), *__f);
Eric Fiselierc52db212019-08-12 07:51:05 +00002361 }
2362 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002363}
2364
2365template <class _Tp, class _Allocator>
2366void
2367deque<_Tp, _Allocator>::__append(size_type __n)
2368{
2369 allocator_type& __a = __base::__alloc();
2370 size_type __back_capacity = __back_spare();
2371 if (__n > __back_capacity)
2372 __add_back_capacity(__n - __back_capacity);
2373 // __n <= __back_capacity
Eric Fiselierc52db212019-08-12 07:51:05 +00002374 for (__deque_block_range __br : __deque_range(__base::end(), __base::end() + __n)) {
2375 _ConstructTransaction __tx(this, __br);
2376 for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05002377 __alloc_traits::construct(__a, _VSTD::__to_address(__tx.__pos_));
Eric Fiselierc52db212019-08-12 07:51:05 +00002378 }
2379 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002380}
2381
2382template <class _Tp, class _Allocator>
2383void
2384deque<_Tp, _Allocator>::__append(size_type __n, const value_type& __v)
2385{
2386 allocator_type& __a = __base::__alloc();
2387 size_type __back_capacity = __back_spare();
2388 if (__n > __back_capacity)
2389 __add_back_capacity(__n - __back_capacity);
2390 // __n <= __back_capacity
Eric Fiselierc52db212019-08-12 07:51:05 +00002391 for (__deque_block_range __br : __deque_range(__base::end(), __base::end() + __n)) {
2392 _ConstructTransaction __tx(this, __br);
2393 for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05002394 __alloc_traits::construct(__a, _VSTD::__to_address(__tx.__pos_), __v);
Eric Fiselierc52db212019-08-12 07:51:05 +00002395 }
2396 }
2397
Howard Hinnantc51e1022010-05-11 19:42:16 +00002398}
2399
2400// Create front capacity for one block of elements.
2401// Strong guarantee. Either do it or don't touch anything.
2402template <class _Tp, class _Allocator>
2403void
2404deque<_Tp, _Allocator>::__add_front_capacity()
2405{
2406 allocator_type& __a = __base::__alloc();
2407 if (__back_spare() >= __base::__block_size)
2408 {
2409 __base::__start_ += __base::__block_size;
2410 pointer __pt = __base::__map_.back();
2411 __base::__map_.pop_back();
2412 __base::__map_.push_front(__pt);
2413 }
2414 // Else if __base::__map_.size() < __base::__map_.capacity() then we need to allocate 1 buffer
2415 else if (__base::__map_.size() < __base::__map_.capacity())
2416 { // we can put the new buffer into the map, but don't shift things around
2417 // until all buffers are allocated. If we throw, we don't need to fix
2418 // anything up (any added buffers are undetectible)
2419 if (__base::__map_.__front_spare() > 0)
2420 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2421 else
2422 {
2423 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2424 // Done allocating, reorder capacity
2425 pointer __pt = __base::__map_.back();
2426 __base::__map_.pop_back();
2427 __base::__map_.push_front(__pt);
2428 }
2429 __base::__start_ = __base::__map_.size() == 1 ?
2430 __base::__block_size / 2 :
2431 __base::__start_ + __base::__block_size;
2432 }
2433 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2434 else
2435 {
2436 __split_buffer<pointer, typename __base::__pointer_allocator&>
2437 __buf(max<size_type>(2 * __base::__map_.capacity(), 1),
2438 0, __base::__map_.__alloc());
Marshall Clow5fd466b2015-03-09 17:08:51 +00002439
Marshall Clow8982dcd2015-07-13 20:04:56 +00002440 typedef __allocator_destructor<_Allocator> _Dp;
2441 unique_ptr<pointer, _Dp> __hold(
2442 __alloc_traits::allocate(__a, __base::__block_size),
2443 _Dp(__a, __base::__block_size));
2444 __buf.push_back(__hold.get());
2445 __hold.release();
Louis Dionne72f24392018-12-12 23:58:25 +00002446
Howard Hinnantc51e1022010-05-11 19:42:16 +00002447 for (typename __base::__map_pointer __i = __base::__map_.begin();
2448 __i != __base::__map_.end(); ++__i)
2449 __buf.push_back(*__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002450 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2451 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2452 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2453 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002454 __base::__start_ = __base::__map_.size() == 1 ?
2455 __base::__block_size / 2 :
2456 __base::__start_ + __base::__block_size;
2457 }
2458}
2459
2460// Create front capacity for __n elements.
2461// Strong guarantee. Either do it or don't touch anything.
2462template <class _Tp, class _Allocator>
2463void
2464deque<_Tp, _Allocator>::__add_front_capacity(size_type __n)
2465{
2466 allocator_type& __a = __base::__alloc();
2467 size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2468 // Number of unused blocks at back:
2469 size_type __back_capacity = __back_spare() / __base::__block_size;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002470 __back_capacity = _VSTD::min(__back_capacity, __nb); // don't take more than you need
Howard Hinnantc51e1022010-05-11 19:42:16 +00002471 __nb -= __back_capacity; // number of blocks need to allocate
2472 // If __nb == 0, then we have sufficient capacity.
2473 if (__nb == 0)
2474 {
2475 __base::__start_ += __base::__block_size * __back_capacity;
2476 for (; __back_capacity > 0; --__back_capacity)
2477 {
2478 pointer __pt = __base::__map_.back();
2479 __base::__map_.pop_back();
2480 __base::__map_.push_front(__pt);
2481 }
2482 }
2483 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2484 else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2485 { // we can put the new buffers into the map, but don't shift things around
2486 // until all buffers are allocated. If we throw, we don't need to fix
2487 // anything up (any added buffers are undetectible)
2488 for (; __nb > 0; --__nb, __base::__start_ += __base::__block_size - (__base::__map_.size() == 1))
2489 {
2490 if (__base::__map_.__front_spare() == 0)
2491 break;
2492 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2493 }
2494 for (; __nb > 0; --__nb, ++__back_capacity)
2495 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2496 // Done allocating, reorder capacity
2497 __base::__start_ += __back_capacity * __base::__block_size;
2498 for (; __back_capacity > 0; --__back_capacity)
2499 {
2500 pointer __pt = __base::__map_.back();
2501 __base::__map_.pop_back();
2502 __base::__map_.push_front(__pt);
2503 }
2504 }
2505 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2506 else
2507 {
2508 size_type __ds = (__nb + __back_capacity) * __base::__block_size - __base::__map_.empty();
2509 __split_buffer<pointer, typename __base::__pointer_allocator&>
2510 __buf(max<size_type>(2* __base::__map_.capacity(),
2511 __nb + __base::__map_.size()),
2512 0, __base::__map_.__alloc());
2513#ifndef _LIBCPP_NO_EXCEPTIONS
2514 try
2515 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002516#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002517 for (; __nb > 0; --__nb)
2518 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2519#ifndef _LIBCPP_NO_EXCEPTIONS
2520 }
2521 catch (...)
2522 {
2523 for (typename __base::__map_pointer __i = __buf.begin();
2524 __i != __buf.end(); ++__i)
2525 __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2526 throw;
2527 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002528#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002529 for (; __back_capacity > 0; --__back_capacity)
2530 {
2531 __buf.push_back(__base::__map_.back());
2532 __base::__map_.pop_back();
2533 }
2534 for (typename __base::__map_pointer __i = __base::__map_.begin();
2535 __i != __base::__map_.end(); ++__i)
2536 __buf.push_back(*__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002537 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2538 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2539 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2540 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002541 __base::__start_ += __ds;
2542 }
2543}
2544
2545// Create back capacity for one block of elements.
2546// Strong guarantee. Either do it or don't touch anything.
2547template <class _Tp, class _Allocator>
2548void
2549deque<_Tp, _Allocator>::__add_back_capacity()
2550{
2551 allocator_type& __a = __base::__alloc();
2552 if (__front_spare() >= __base::__block_size)
2553 {
2554 __base::__start_ -= __base::__block_size;
2555 pointer __pt = __base::__map_.front();
2556 __base::__map_.pop_front();
2557 __base::__map_.push_back(__pt);
2558 }
2559 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2560 else if (__base::__map_.size() < __base::__map_.capacity())
2561 { // we can put the new buffer into the map, but don't shift things around
2562 // until it is allocated. If we throw, we don't need to fix
2563 // anything up (any added buffers are undetectible)
2564 if (__base::__map_.__back_spare() != 0)
2565 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2566 else
2567 {
2568 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2569 // Done allocating, reorder capacity
2570 pointer __pt = __base::__map_.front();
2571 __base::__map_.pop_front();
2572 __base::__map_.push_back(__pt);
2573 }
2574 }
2575 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2576 else
2577 {
2578 __split_buffer<pointer, typename __base::__pointer_allocator&>
2579 __buf(max<size_type>(2* __base::__map_.capacity(), 1),
2580 __base::__map_.size(),
2581 __base::__map_.__alloc());
Marshall Clow5fd466b2015-03-09 17:08:51 +00002582
Marshall Clow8982dcd2015-07-13 20:04:56 +00002583 typedef __allocator_destructor<_Allocator> _Dp;
2584 unique_ptr<pointer, _Dp> __hold(
2585 __alloc_traits::allocate(__a, __base::__block_size),
2586 _Dp(__a, __base::__block_size));
2587 __buf.push_back(__hold.get());
2588 __hold.release();
Marshall Clow5fd466b2015-03-09 17:08:51 +00002589
Howard Hinnantc51e1022010-05-11 19:42:16 +00002590 for (typename __base::__map_pointer __i = __base::__map_.end();
2591 __i != __base::__map_.begin();)
2592 __buf.push_front(*--__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002593 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2594 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2595 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2596 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002597 }
2598}
2599
2600// Create back capacity for __n elements.
2601// Strong guarantee. Either do it or don't touch anything.
2602template <class _Tp, class _Allocator>
2603void
2604deque<_Tp, _Allocator>::__add_back_capacity(size_type __n)
2605{
2606 allocator_type& __a = __base::__alloc();
2607 size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2608 // Number of unused blocks at front:
2609 size_type __front_capacity = __front_spare() / __base::__block_size;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002610 __front_capacity = _VSTD::min(__front_capacity, __nb); // don't take more than you need
Howard Hinnantc51e1022010-05-11 19:42:16 +00002611 __nb -= __front_capacity; // number of blocks need to allocate
2612 // If __nb == 0, then we have sufficient capacity.
2613 if (__nb == 0)
2614 {
2615 __base::__start_ -= __base::__block_size * __front_capacity;
2616 for (; __front_capacity > 0; --__front_capacity)
2617 {
2618 pointer __pt = __base::__map_.front();
2619 __base::__map_.pop_front();
2620 __base::__map_.push_back(__pt);
2621 }
2622 }
2623 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2624 else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2625 { // we can put the new buffers into the map, but don't shift things around
2626 // until all buffers are allocated. If we throw, we don't need to fix
2627 // anything up (any added buffers are undetectible)
2628 for (; __nb > 0; --__nb)
2629 {
2630 if (__base::__map_.__back_spare() == 0)
2631 break;
2632 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2633 }
2634 for (; __nb > 0; --__nb, ++__front_capacity, __base::__start_ +=
2635 __base::__block_size - (__base::__map_.size() == 1))
2636 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2637 // Done allocating, reorder capacity
2638 __base::__start_ -= __base::__block_size * __front_capacity;
2639 for (; __front_capacity > 0; --__front_capacity)
2640 {
2641 pointer __pt = __base::__map_.front();
2642 __base::__map_.pop_front();
2643 __base::__map_.push_back(__pt);
2644 }
2645 }
2646 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2647 else
2648 {
2649 size_type __ds = __front_capacity * __base::__block_size;
2650 __split_buffer<pointer, typename __base::__pointer_allocator&>
2651 __buf(max<size_type>(2* __base::__map_.capacity(),
2652 __nb + __base::__map_.size()),
2653 __base::__map_.size() - __front_capacity,
2654 __base::__map_.__alloc());
2655#ifndef _LIBCPP_NO_EXCEPTIONS
2656 try
2657 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002658#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002659 for (; __nb > 0; --__nb)
2660 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2661#ifndef _LIBCPP_NO_EXCEPTIONS
2662 }
2663 catch (...)
2664 {
2665 for (typename __base::__map_pointer __i = __buf.begin();
2666 __i != __buf.end(); ++__i)
2667 __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2668 throw;
2669 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002670#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002671 for (; __front_capacity > 0; --__front_capacity)
2672 {
2673 __buf.push_back(__base::__map_.front());
2674 __base::__map_.pop_front();
2675 }
2676 for (typename __base::__map_pointer __i = __base::__map_.end();
2677 __i != __base::__map_.begin();)
2678 __buf.push_front(*--__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002679 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2680 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2681 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2682 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002683 __base::__start_ -= __ds;
2684 }
2685}
2686
2687template <class _Tp, class _Allocator>
2688void
2689deque<_Tp, _Allocator>::pop_front()
2690{
2691 allocator_type& __a = __base::__alloc();
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05002692 __alloc_traits::destroy(__a, _VSTD::__to_address(*(__base::__map_.begin() +
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002693 __base::__start_ / __base::__block_size) +
2694 __base::__start_ % __base::__block_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002695 --__base::size();
Eric Fiselier27e03622019-08-01 23:11:18 +00002696 ++__base::__start_;
2697 __maybe_remove_front_spare();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002698}
2699
2700template <class _Tp, class _Allocator>
2701void
2702deque<_Tp, _Allocator>::pop_back()
2703{
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +02002704 _LIBCPP_ASSERT(!empty(), "deque::pop_back called on an empty deque");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002705 allocator_type& __a = __base::__alloc();
2706 size_type __p = __base::size() + __base::__start_ - 1;
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05002707 __alloc_traits::destroy(__a, _VSTD::__to_address(*(__base::__map_.begin() +
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002708 __p / __base::__block_size) +
2709 __p % __base::__block_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002710 --__base::size();
Eric Fiselier27e03622019-08-01 23:11:18 +00002711 __maybe_remove_back_spare();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002712}
2713
2714// move assign [__f, __l) to [__r, __r + (__l-__f)).
2715// If __vt points into [__f, __l), then subtract (__f - __r) from __vt.
2716template <class _Tp, class _Allocator>
2717typename deque<_Tp, _Allocator>::iterator
2718deque<_Tp, _Allocator>::__move_and_check(iterator __f, iterator __l, iterator __r,
2719 const_pointer& __vt)
2720{
2721 // as if
2722 // for (; __f != __l; ++__f, ++__r)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002723 // *__r = _VSTD::move(*__f);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002724 difference_type __n = __l - __f;
2725 while (__n > 0)
2726 {
2727 pointer __fb = __f.__ptr_;
2728 pointer __fe = *__f.__m_iter_ + __base::__block_size;
2729 difference_type __bs = __fe - __fb;
2730 if (__bs > __n)
2731 {
2732 __bs = __n;
2733 __fe = __fb + __bs;
2734 }
2735 if (__fb <= __vt && __vt < __fe)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002736 __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) -= __f - __r).__ptr_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002737 __r = _VSTD::move(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002738 __n -= __bs;
2739 __f += __bs;
2740 }
2741 return __r;
2742}
2743
2744// move assign [__f, __l) to [__r - (__l-__f), __r) backwards.
2745// If __vt points into [__f, __l), then add (__r - __l) to __vt.
2746template <class _Tp, class _Allocator>
2747typename deque<_Tp, _Allocator>::iterator
2748deque<_Tp, _Allocator>::__move_backward_and_check(iterator __f, iterator __l, iterator __r,
2749 const_pointer& __vt)
2750{
2751 // as if
2752 // while (__f != __l)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002753 // *--__r = _VSTD::move(*--__l);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002754 difference_type __n = __l - __f;
2755 while (__n > 0)
2756 {
2757 --__l;
2758 pointer __lb = *__l.__m_iter_;
2759 pointer __le = __l.__ptr_ + 1;
2760 difference_type __bs = __le - __lb;
2761 if (__bs > __n)
2762 {
2763 __bs = __n;
2764 __lb = __le - __bs;
2765 }
2766 if (__lb <= __vt && __vt < __le)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002767 __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) += __r - __l - 1).__ptr_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002768 __r = _VSTD::move_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002769 __n -= __bs;
2770 __l -= __bs - 1;
2771 }
2772 return __r;
2773}
2774
2775// move construct [__f, __l) to [__r, __r + (__l-__f)).
2776// If __vt points into [__f, __l), then add (__r - __f) to __vt.
2777template <class _Tp, class _Allocator>
2778void
2779deque<_Tp, _Allocator>::__move_construct_and_check(iterator __f, iterator __l,
2780 iterator __r, const_pointer& __vt)
2781{
2782 allocator_type& __a = __base::__alloc();
2783 // as if
2784 // for (; __f != __l; ++__r, ++__f, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002785 // __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__f));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002786 difference_type __n = __l - __f;
2787 while (__n > 0)
2788 {
2789 pointer __fb = __f.__ptr_;
2790 pointer __fe = *__f.__m_iter_ + __base::__block_size;
2791 difference_type __bs = __fe - __fb;
2792 if (__bs > __n)
2793 {
2794 __bs = __n;
2795 __fe = __fb + __bs;
2796 }
2797 if (__fb <= __vt && __vt < __fe)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002798 __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) += __r - __f).__ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002799 for (; __fb != __fe; ++__fb, ++__r, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002800 __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__fb));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002801 __n -= __bs;
2802 __f += __bs;
2803 }
2804}
2805
2806// move construct [__f, __l) to [__r - (__l-__f), __r) backwards.
2807// If __vt points into [__f, __l), then subtract (__l - __r) from __vt.
2808template <class _Tp, class _Allocator>
2809void
2810deque<_Tp, _Allocator>::__move_construct_backward_and_check(iterator __f, iterator __l,
2811 iterator __r, const_pointer& __vt)
2812{
2813 allocator_type& __a = __base::__alloc();
2814 // as if
2815 // for (iterator __j = __l; __j != __f;)
2816 // {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002817 // __alloc_traitsconstruct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__j));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002818 // --__base::__start_;
2819 // ++__base::size();
2820 // }
2821 difference_type __n = __l - __f;
2822 while (__n > 0)
2823 {
2824 --__l;
2825 pointer __lb = *__l.__m_iter_;
2826 pointer __le = __l.__ptr_ + 1;
2827 difference_type __bs = __le - __lb;
2828 if (__bs > __n)
2829 {
2830 __bs = __n;
2831 __lb = __le - __bs;
2832 }
2833 if (__lb <= __vt && __vt < __le)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002834 __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) -= __l - __r + 1).__ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002835 while (__le != __lb)
2836 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002837 __alloc_traits::construct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__le));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002838 --__base::__start_;
2839 ++__base::size();
2840 }
2841 __n -= __bs;
2842 __l -= __bs - 1;
2843 }
2844}
2845
2846template <class _Tp, class _Allocator>
2847typename deque<_Tp, _Allocator>::iterator
2848deque<_Tp, _Allocator>::erase(const_iterator __f)
2849{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002850 iterator __b = __base::begin();
2851 difference_type __pos = __f - __b;
2852 iterator __p = __b + __pos;
2853 allocator_type& __a = __base::__alloc();
Eric Fiselier37c22152016-12-24 00:24:44 +00002854 if (static_cast<size_t>(__pos) <= (__base::size() - 1) / 2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002855 { // erase from front
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002856 _VSTD::move_backward(__b, __p, _VSTD::next(__p));
2857 __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002858 --__base::size();
2859 ++__base::__start_;
Eric Fiselier27e03622019-08-01 23:11:18 +00002860 __maybe_remove_front_spare();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002861 }
2862 else
2863 { // erase from back
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002864 iterator __i = _VSTD::move(_VSTD::next(__p), __base::end(), __p);
2865 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002866 --__base::size();
Eric Fiselier27e03622019-08-01 23:11:18 +00002867 __maybe_remove_back_spare();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002868 }
2869 return __base::begin() + __pos;
2870}
2871
2872template <class _Tp, class _Allocator>
2873typename deque<_Tp, _Allocator>::iterator
2874deque<_Tp, _Allocator>::erase(const_iterator __f, const_iterator __l)
2875{
2876 difference_type __n = __l - __f;
2877 iterator __b = __base::begin();
2878 difference_type __pos = __f - __b;
2879 iterator __p = __b + __pos;
2880 if (__n > 0)
2881 {
2882 allocator_type& __a = __base::__alloc();
Eric Fiselier37c22152016-12-24 00:24:44 +00002883 if (static_cast<size_t>(__pos) <= (__base::size() - __n) / 2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002884 { // erase from front
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002885 iterator __i = _VSTD::move_backward(__b, __p, __p + __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002886 for (; __b != __i; ++__b)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002887 __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002888 __base::size() -= __n;
2889 __base::__start_ += __n;
Eric Fiselier27e03622019-08-01 23:11:18 +00002890 while (__maybe_remove_front_spare()) {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002891 }
2892 }
2893 else
2894 { // erase from back
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002895 iterator __i = _VSTD::move(__p + __n, __base::end(), __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002896 for (iterator __e = __base::end(); __i != __e; ++__i)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002897 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002898 __base::size() -= __n;
Eric Fiselier27e03622019-08-01 23:11:18 +00002899 while (__maybe_remove_back_spare()) {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002900 }
2901 }
2902 }
2903 return __base::begin() + __pos;
2904}
2905
2906template <class _Tp, class _Allocator>
2907void
2908deque<_Tp, _Allocator>::__erase_to_end(const_iterator __f)
2909{
2910 iterator __e = __base::end();
2911 difference_type __n = __e - __f;
2912 if (__n > 0)
2913 {
2914 allocator_type& __a = __base::__alloc();
2915 iterator __b = __base::begin();
2916 difference_type __pos = __f - __b;
2917 for (iterator __p = __b + __pos; __p != __e; ++__p)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002918 __alloc_traits::destroy(__a, _VSTD::addressof(*__p));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002919 __base::size() -= __n;
Eric Fiselier27e03622019-08-01 23:11:18 +00002920 while (__maybe_remove_back_spare()) {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002921 }
2922 }
2923}
2924
2925template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002926inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002927void
2928deque<_Tp, _Allocator>::swap(deque& __c)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002929#if _LIBCPP_STD_VER >= 14
2930 _NOEXCEPT
2931#else
Louis Dionne72f24392018-12-12 23:58:25 +00002932 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002933 __is_nothrow_swappable<allocator_type>::value)
2934#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002935{
2936 __base::swap(__c);
2937}
2938
2939template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002940inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002941void
Howard Hinnantda2df912011-06-02 16:10:22 +00002942deque<_Tp, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002943{
2944 __base::clear();
2945}
2946
2947template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002948inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002949bool
2950operator==(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2951{
2952 const typename deque<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002953 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002954}
2955
2956template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002957inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002958bool
2959operator!=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2960{
2961 return !(__x == __y);
2962}
2963
2964template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002965inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002966bool
2967operator< (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2968{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002969 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002970}
2971
2972template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002973inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002974bool
2975operator> (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2976{
2977 return __y < __x;
2978}
2979
2980template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002981inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002982bool
2983operator>=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2984{
2985 return !(__x < __y);
2986}
2987
2988template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002989inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002990bool
2991operator<=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2992{
2993 return !(__y < __x);
2994}
2995
2996template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002997inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002998void
2999swap(deque<_Tp, _Allocator>& __x, deque<_Tp, _Allocator>& __y)
Howard Hinnantca2fc222011-06-02 20:00:14 +00003000 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003001{
3002 __x.swap(__y);
3003}
3004
Marshall Clow29b53f22018-12-14 18:49:35 +00003005#if _LIBCPP_STD_VER > 17
3006template <class _Tp, class _Allocator, class _Up>
Marek Kurdeja98b1412020-05-02 13:58:03 +02003007inline _LIBCPP_INLINE_VISIBILITY typename deque<_Tp, _Allocator>::size_type
3008erase(deque<_Tp, _Allocator>& __c, const _Up& __v) {
3009 auto __old_size = __c.size();
3010 __c.erase(_VSTD::remove(__c.begin(), __c.end(), __v), __c.end());
3011 return __old_size - __c.size();
3012}
Marshall Clow29b53f22018-12-14 18:49:35 +00003013
3014template <class _Tp, class _Allocator, class _Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +02003015inline _LIBCPP_INLINE_VISIBILITY typename deque<_Tp, _Allocator>::size_type
3016erase_if(deque<_Tp, _Allocator>& __c, _Predicate __pred) {
3017 auto __old_size = __c.size();
3018 __c.erase(_VSTD::remove_if(__c.begin(), __c.end(), __pred), __c.end());
3019 return __old_size - __c.size();
3020}
Marshall Clow29b53f22018-12-14 18:49:35 +00003021#endif
3022
3023
Howard Hinnantc51e1022010-05-11 19:42:16 +00003024_LIBCPP_END_NAMESPACE_STD
3025
Eric Fiselierf4433a32017-05-31 22:07:49 +00003026_LIBCPP_POP_MACROS
3027
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003028#endif // _LIBCPP_DEQUE