blob: 37ba58684ac0d25d2efebc48e7ed027d225ea455 [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
911template <bool>
912class __deque_base_common
913{
914protected:
Marshall Clow8fea1612016-08-25 15:09:01 +0000915 _LIBCPP_NORETURN void __throw_length_error() const;
916 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000917};
918
919template <bool __b>
920void
921__deque_base_common<__b>::__throw_length_error() const
922{
Marshall Clow8fea1612016-08-25 15:09:01 +0000923 _VSTD::__throw_length_error("deque");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000924}
925
926template <bool __b>
927void
928__deque_base_common<__b>::__throw_out_of_range() const
929{
Marshall Clow8fea1612016-08-25 15:09:01 +0000930 _VSTD::__throw_out_of_range("deque");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000931}
932
933template <class _Tp, class _Allocator>
934class __deque_base
935 : protected __deque_base_common<true>
936{
937 __deque_base(const __deque_base& __c);
938 __deque_base& operator=(const __deque_base& __c);
Marshall Clow4cc3a452018-05-18 23:44:13 +0000939public:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000940 typedef _Allocator allocator_type;
941 typedef allocator_traits<allocator_type> __alloc_traits;
Marshall Clow4cc3a452018-05-18 23:44:13 +0000942 typedef typename __alloc_traits::size_type size_type;
Eric Fiselier27e03622019-08-01 23:11:18 +0000943
Marshall Clow4cc3a452018-05-18 23:44:13 +0000944 typedef _Tp value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000945 typedef value_type& reference;
946 typedef const value_type& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000947 typedef typename __alloc_traits::difference_type difference_type;
948 typedef typename __alloc_traits::pointer pointer;
949 typedef typename __alloc_traits::const_pointer const_pointer;
950
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000951 static const difference_type __block_size;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000952
Marshall Clow940e01c2015-04-07 05:21:38 +0000953 typedef typename __rebind_alloc_helper<__alloc_traits, pointer>::type __pointer_allocator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000954 typedef allocator_traits<__pointer_allocator> __map_traits;
955 typedef typename __map_traits::pointer __map_pointer;
Marshall Clow940e01c2015-04-07 05:21:38 +0000956 typedef typename __rebind_alloc_helper<__alloc_traits, const_pointer>::type __const_pointer_allocator;
Howard Hinnantdcb73a32013-06-23 21:17:24 +0000957 typedef typename allocator_traits<__const_pointer_allocator>::const_pointer __map_const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000958 typedef __split_buffer<pointer, __pointer_allocator> __map;
959
960 typedef __deque_iterator<value_type, pointer, reference, __map_pointer,
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000961 difference_type> iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000962 typedef __deque_iterator<value_type, const_pointer, const_reference, __map_const_pointer,
Evgeniy Stepanovc299de22015-11-06 22:02:29 +0000963 difference_type> const_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000964
Eric Fiselierc52db212019-08-12 07:51:05 +0000965 struct __deque_block_range {
966 explicit __deque_block_range(pointer __b, pointer __e) _NOEXCEPT : __begin_(__b), __end_(__e) {}
967 const pointer __begin_;
968 const pointer __end_;
969 };
970
971 struct __deque_range {
972 iterator __pos_;
973 const iterator __end_;
974
975 __deque_range(iterator __pos, iterator __e) _NOEXCEPT
976 : __pos_(__pos), __end_(__e) {}
977
978 explicit operator bool() const _NOEXCEPT {
979 return __pos_ != __end_;
980 }
981
982 __deque_range begin() const {
983 return *this;
984 }
985
986 __deque_range end() const {
987 return __deque_range(__end_, __end_);
988 }
989 __deque_block_range operator*() const _NOEXCEPT {
990 if (__pos_.__m_iter_ == __end_.__m_iter_) {
991 return __deque_block_range(__pos_.__ptr_, __end_.__ptr_);
992 }
993 return __deque_block_range(__pos_.__ptr_, *__pos_.__m_iter_ + __block_size);
994 }
995
996 __deque_range& operator++() _NOEXCEPT {
997 if (__pos_.__m_iter_ == __end_.__m_iter_) {
998 __pos_ = __end_;
999 } else {
1000 ++__pos_.__m_iter_;
1001 __pos_.__ptr_ = *__pos_.__m_iter_;
1002 }
1003 return *this;
1004 }
1005
1006
1007 friend bool operator==(__deque_range const& __lhs, __deque_range const& __rhs) {
1008 return __lhs.__pos_ == __rhs.__pos_;
1009 }
1010 friend bool operator!=(__deque_range const& __lhs, __deque_range const& __rhs) {
1011 return !(__lhs == __rhs);
1012 }
1013 };
1014
1015
1016
1017 struct _ConstructTransaction {
1018 _ConstructTransaction(__deque_base* __db, __deque_block_range& __r)
1019 : __pos_(__r.__begin_), __end_(__r.__end_), __begin_(__r.__begin_), __base_(__db) {}
1020
1021
1022 ~_ConstructTransaction() {
1023 __base_->size() += (__pos_ - __begin_);
1024 }
1025
1026 pointer __pos_;
1027 const pointer __end_;
1028 private:
1029 const pointer __begin_;
1030 __deque_base * const __base_;
1031 };
1032
Marshall Clow4cc3a452018-05-18 23:44:13 +00001033protected:
Howard Hinnantc51e1022010-05-11 19:42:16 +00001034 __map __map_;
1035 size_type __start_;
1036 __compressed_pair<size_type, allocator_type> __size_;
1037
Howard Hinnantda2df912011-06-02 16:10:22 +00001038 iterator begin() _NOEXCEPT;
1039 const_iterator begin() const _NOEXCEPT;
1040 iterator end() _NOEXCEPT;
1041 const_iterator end() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001042
1043 _LIBCPP_INLINE_VISIBILITY size_type& size() {return __size_.first();}
Howard Hinnantda2df912011-06-02 16:10:22 +00001044 _LIBCPP_INLINE_VISIBILITY
1045 const size_type& size() const _NOEXCEPT {return __size_.first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001046 _LIBCPP_INLINE_VISIBILITY allocator_type& __alloc() {return __size_.second();}
Howard Hinnantda2df912011-06-02 16:10:22 +00001047 _LIBCPP_INLINE_VISIBILITY
1048 const allocator_type& __alloc() const _NOEXCEPT {return __size_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001049
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001050 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantad979ba2011-06-03 15:16:49 +00001051 __deque_base()
1052 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001053 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001054 explicit __deque_base(const allocator_type& __a);
Howard Hinnantca2fc222011-06-02 20:00:14 +00001055public:
Howard Hinnantc51e1022010-05-11 19:42:16 +00001056 ~__deque_base();
1057
Eric Fiselier212e3f22017-04-16 03:17:01 +00001058#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantca2fc222011-06-02 20:00:14 +00001059 __deque_base(__deque_base&& __c)
1060 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001061 __deque_base(__deque_base&& __c, const allocator_type& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001062#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001063
Howard Hinnantca2fc222011-06-02 20:00:14 +00001064 void swap(__deque_base& __c)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001065#if _LIBCPP_STD_VER >= 14
1066 _NOEXCEPT;
1067#else
Louis Dionne72f24392018-12-12 23:58:25 +00001068 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001069 __is_nothrow_swappable<allocator_type>::value);
1070#endif
Howard Hinnantca2fc222011-06-02 20:00:14 +00001071protected:
Howard Hinnantda2df912011-06-02 16:10:22 +00001072 void clear() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001073
1074 bool __invariants() const;
1075
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001076 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001077 void __move_assign(__deque_base& __c)
Howard Hinnant4931e092011-06-02 21:38:57 +00001078 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1079 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001080 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001081 __map_ = _VSTD::move(__c.__map_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001082 __start_ = __c.__start_;
1083 size() = __c.size();
1084 __move_assign_alloc(__c);
1085 __c.__start_ = __c.size() = 0;
1086 }
1087
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001088 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001089 void __move_assign_alloc(__deque_base& __c)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001090 _NOEXCEPT_(!__alloc_traits::propagate_on_container_move_assignment::value ||
1091 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001092 {__move_assign_alloc(__c, integral_constant<bool,
1093 __alloc_traits::propagate_on_container_move_assignment::value>());}
1094
1095private:
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001096 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001097 void __move_assign_alloc(__deque_base& __c, true_type)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001098 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001099 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001100 __alloc() = _VSTD::move(__c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001101 }
1102
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001103 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001104 void __move_assign_alloc(__deque_base&, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001105 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001106};
1107
1108template <class _Tp, class _Allocator>
Evgeniy Stepanovc299de22015-11-06 22:02:29 +00001109const typename __deque_base<_Tp, _Allocator>::difference_type
1110 __deque_base<_Tp, _Allocator>::__block_size =
1111 __deque_block_size<value_type, difference_type>::value;
1112
1113template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001114bool
1115__deque_base<_Tp, _Allocator>::__invariants() const
1116{
1117 if (!__map_.__invariants())
1118 return false;
1119 if (__map_.size() >= size_type(-1) / __block_size)
1120 return false;
1121 for (typename __map::const_iterator __i = __map_.begin(), __e = __map_.end();
1122 __i != __e; ++__i)
1123 if (*__i == nullptr)
1124 return false;
1125 if (__map_.size() != 0)
1126 {
1127 if (size() >= __map_.size() * __block_size)
1128 return false;
1129 if (__start_ >= __map_.size() * __block_size - size())
1130 return false;
1131 }
1132 else
1133 {
1134 if (size() != 0)
1135 return false;
1136 if (__start_ != 0)
1137 return false;
1138 }
1139 return true;
1140}
1141
1142template <class _Tp, class _Allocator>
1143typename __deque_base<_Tp, _Allocator>::iterator
Howard Hinnantda2df912011-06-02 16:10:22 +00001144__deque_base<_Tp, _Allocator>::begin() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001145{
1146 __map_pointer __mp = __map_.begin() + __start_ / __block_size;
1147 return iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
1148}
1149
1150template <class _Tp, class _Allocator>
1151typename __deque_base<_Tp, _Allocator>::const_iterator
Howard Hinnantda2df912011-06-02 16:10:22 +00001152__deque_base<_Tp, _Allocator>::begin() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001153{
Howard Hinnantdcb73a32013-06-23 21:17:24 +00001154 __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __start_ / __block_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001155 return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
1156}
1157
1158template <class _Tp, class _Allocator>
1159typename __deque_base<_Tp, _Allocator>::iterator
Howard Hinnantda2df912011-06-02 16:10:22 +00001160__deque_base<_Tp, _Allocator>::end() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001161{
1162 size_type __p = size() + __start_;
1163 __map_pointer __mp = __map_.begin() + __p / __block_size;
1164 return iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
1165}
1166
1167template <class _Tp, class _Allocator>
1168typename __deque_base<_Tp, _Allocator>::const_iterator
Howard Hinnantda2df912011-06-02 16:10:22 +00001169__deque_base<_Tp, _Allocator>::end() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001170{
1171 size_type __p = size() + __start_;
Howard Hinnantdcb73a32013-06-23 21:17:24 +00001172 __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __p / __block_size);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001173 return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
1174}
1175
1176template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001177inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001178__deque_base<_Tp, _Allocator>::__deque_base()
Howard Hinnantad979ba2011-06-03 15:16:49 +00001179 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001180 : __start_(0), __size_(0, __default_init_tag()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001181
1182template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001183inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001184__deque_base<_Tp, _Allocator>::__deque_base(const allocator_type& __a)
1185 : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a) {}
1186
1187template <class _Tp, class _Allocator>
1188__deque_base<_Tp, _Allocator>::~__deque_base()
1189{
1190 clear();
1191 typename __map::iterator __i = __map_.begin();
1192 typename __map::iterator __e = __map_.end();
1193 for (; __i != __e; ++__i)
1194 __alloc_traits::deallocate(__alloc(), *__i, __block_size);
1195}
1196
Eric Fiselier212e3f22017-04-16 03:17:01 +00001197#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001198
1199template <class _Tp, class _Allocator>
1200__deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001201 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001202 : __map_(_VSTD::move(__c.__map_)),
1203 __start_(_VSTD::move(__c.__start_)),
1204 __size_(_VSTD::move(__c.__size_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001205{
1206 __c.__start_ = 0;
1207 __c.size() = 0;
1208}
1209
1210template <class _Tp, class _Allocator>
1211__deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c, const allocator_type& __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001212 : __map_(_VSTD::move(__c.__map_), __pointer_allocator(__a)),
1213 __start_(_VSTD::move(__c.__start_)),
1214 __size_(_VSTD::move(__c.size()), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001215{
1216 if (__a == __c.__alloc())
1217 {
1218 __c.__start_ = 0;
1219 __c.size() = 0;
1220 }
1221 else
1222 {
1223 __map_.clear();
1224 __start_ = 0;
1225 size() = 0;
1226 }
1227}
1228
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001229#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001230
1231template <class _Tp, class _Allocator>
1232void
1233__deque_base<_Tp, _Allocator>::swap(__deque_base& __c)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001234#if _LIBCPP_STD_VER >= 14
1235 _NOEXCEPT
1236#else
Louis Dionne72f24392018-12-12 23:58:25 +00001237 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001238 __is_nothrow_swappable<allocator_type>::value)
1239#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001240{
1241 __map_.swap(__c.__map_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001242 _VSTD::swap(__start_, __c.__start_);
1243 _VSTD::swap(size(), __c.size());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05001244 _VSTD::__swap_allocator(__alloc(), __c.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001245}
1246
1247template <class _Tp, class _Allocator>
1248void
Howard Hinnantda2df912011-06-02 16:10:22 +00001249__deque_base<_Tp, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001250{
1251 allocator_type& __a = __alloc();
1252 for (iterator __i = begin(), __e = end(); __i != __e; ++__i)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001253 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001254 size() = 0;
1255 while (__map_.size() > 2)
1256 {
1257 __alloc_traits::deallocate(__a, __map_.front(), __block_size);
1258 __map_.pop_front();
1259 }
1260 switch (__map_.size())
1261 {
1262 case 1:
1263 __start_ = __block_size / 2;
1264 break;
1265 case 2:
1266 __start_ = __block_size;
1267 break;
1268 }
1269}
1270
Marshall Clow65cd4c62015-02-18 17:24:08 +00001271template <class _Tp, class _Allocator /*= allocator<_Tp>*/>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001272class _LIBCPP_TEMPLATE_VIS deque
Howard Hinnantc51e1022010-05-11 19:42:16 +00001273 : private __deque_base<_Tp, _Allocator>
1274{
1275public:
1276 // types:
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001277
Howard Hinnantc51e1022010-05-11 19:42:16 +00001278 typedef _Tp value_type;
1279 typedef _Allocator allocator_type;
1280
Marshall Clow5128cf32015-11-26 01:24:04 +00001281 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
1282 "Allocator::value_type must be same type as value_type");
1283
Howard Hinnantc51e1022010-05-11 19:42:16 +00001284 typedef __deque_base<value_type, allocator_type> __base;
1285
1286 typedef typename __base::__alloc_traits __alloc_traits;
1287 typedef typename __base::reference reference;
1288 typedef typename __base::const_reference const_reference;
1289 typedef typename __base::iterator iterator;
1290 typedef typename __base::const_iterator const_iterator;
1291 typedef typename __base::size_type size_type;
1292 typedef typename __base::difference_type difference_type;
1293
1294 typedef typename __base::pointer pointer;
1295 typedef typename __base::const_pointer const_pointer;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001296 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
1297 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001298
Eric Fiselierc52db212019-08-12 07:51:05 +00001299 using typename __base::__deque_range;
1300 using typename __base::__deque_block_range;
1301 using typename __base::_ConstructTransaction;
1302
Howard Hinnantc51e1022010-05-11 19:42:16 +00001303 // construct/copy/destroy:
Howard Hinnantad979ba2011-06-03 15:16:49 +00001304 _LIBCPP_INLINE_VISIBILITY
1305 deque()
1306 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
1307 {}
Marshall Clow7ed23a72014-03-05 19:06:20 +00001308 _LIBCPP_INLINE_VISIBILITY explicit deque(const allocator_type& __a) : __base(__a) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001309 explicit deque(size_type __n);
Marshall Clowbc4fcb42013-09-07 16:16:19 +00001310#if _LIBCPP_STD_VER > 11
1311 explicit deque(size_type __n, const _Allocator& __a);
1312#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001313 deque(size_type __n, const value_type& __v);
1314 deque(size_type __n, const value_type& __v, const allocator_type& __a);
1315 template <class _InputIter>
1316 deque(_InputIter __f, _InputIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001317 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001318 template <class _InputIter>
1319 deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001320 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001321 deque(const deque& __c);
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05001322 deque(const deque& __c, const __identity_t<allocator_type>& __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001323
1324 deque& operator=(const deque& __c);
Eric Fiselier212e3f22017-04-16 03:17:01 +00001325
1326#ifndef _LIBCPP_CXX03_LANG
1327 deque(initializer_list<value_type> __il);
1328 deque(initializer_list<value_type> __il, const allocator_type& __a);
1329
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001330 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001331 deque& operator=(initializer_list<value_type> __il) {assign(__il); return *this;}
1332
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001333 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantca2fc222011-06-02 20:00:14 +00001334 deque(deque&& __c) _NOEXCEPT_(is_nothrow_move_constructible<__base>::value);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001335 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05001336 deque(deque&& __c, const __identity_t<allocator_type>& __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001337 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantca2fc222011-06-02 20:00:14 +00001338 deque& operator=(deque&& __c)
Howard Hinnant4931e092011-06-02 21:38:57 +00001339 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1340 is_nothrow_move_assignable<allocator_type>::value);
Eric Fiselier212e3f22017-04-16 03:17:01 +00001341
1342 _LIBCPP_INLINE_VISIBILITY
1343 void assign(initializer_list<value_type> __il) {assign(__il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001344#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001345
1346 template <class _InputIter>
1347 void assign(_InputIter __f, _InputIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001348 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value &&
1349 !__is_cpp17_random_access_iterator<_InputIter>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001350 template <class _RAIter>
1351 void assign(_RAIter __f, _RAIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001352 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001353 void assign(size_type __n, const value_type& __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001354
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001355 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001356 allocator_type get_allocator() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001357
1358 // iterators:
1359
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001360 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001361 iterator begin() _NOEXCEPT {return __base::begin();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001362 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001363 const_iterator begin() const _NOEXCEPT {return __base::begin();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001364 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001365 iterator end() _NOEXCEPT {return __base::end();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001367 const_iterator end() const _NOEXCEPT {return __base::end();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001368
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001369 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001370 reverse_iterator rbegin() _NOEXCEPT
1371 {return reverse_iterator(__base::end());}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001372 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001373 const_reverse_iterator rbegin() const _NOEXCEPT
1374 {return const_reverse_iterator(__base::end());}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001375 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001376 reverse_iterator rend() _NOEXCEPT
1377 {return reverse_iterator(__base::begin());}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001378 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001379 const_reverse_iterator rend() const _NOEXCEPT
1380 {return const_reverse_iterator(__base::begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001381
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001383 const_iterator cbegin() const _NOEXCEPT
1384 {return __base::begin();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001385 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001386 const_iterator cend() const _NOEXCEPT
1387 {return __base::end();}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001388 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001389 const_reverse_iterator crbegin() const _NOEXCEPT
1390 {return const_reverse_iterator(__base::end());}
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001391 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001392 const_reverse_iterator crend() const _NOEXCEPT
1393 {return const_reverse_iterator(__base::begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001394
1395 // capacity:
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001396 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001397 size_type size() const _NOEXCEPT {return __base::size();}
1398 _LIBCPP_INLINE_VISIBILITY
1399 size_type max_size() const _NOEXCEPT
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001400 {return _VSTD::min<size_type>(
Eric Fiselierb5d9f442016-11-23 01:18:56 +00001401 __alloc_traits::max_size(__base::__alloc()),
1402 numeric_limits<difference_type>::max());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001403 void resize(size_type __n);
1404 void resize(size_type __n, const value_type& __v);
Howard Hinnant4931e092011-06-02 21:38:57 +00001405 void shrink_to_fit() _NOEXCEPT;
Marshall Clow425f5752017-11-15 05:51:26 +00001406 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001407 bool empty() const _NOEXCEPT {return __base::size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001408
1409 // element access:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001410 _LIBCPP_INLINE_VISIBILITY
Marshall Clow8d7c9f12019-03-14 21:56:57 +00001411 reference operator[](size_type __i) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001412 _LIBCPP_INLINE_VISIBILITY
Marshall Clow8d7c9f12019-03-14 21:56:57 +00001413 const_reference operator[](size_type __i) const _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001414 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001415 reference at(size_type __i);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001416 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001417 const_reference at(size_type __i) const;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001418 _LIBCPP_INLINE_VISIBILITY
Marshall Clow05cf6692019-03-19 03:30:07 +00001419 reference front() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001420 _LIBCPP_INLINE_VISIBILITY
Marshall Clow05cf6692019-03-19 03:30:07 +00001421 const_reference front() const _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001422 _LIBCPP_INLINE_VISIBILITY
Marshall Clow05cf6692019-03-19 03:30:07 +00001423 reference back() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001424 _LIBCPP_INLINE_VISIBILITY
Marshall Clow05cf6692019-03-19 03:30:07 +00001425 const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001426
1427 // 23.2.2.3 modifiers:
1428 void push_front(const value_type& __v);
1429 void push_back(const value_type& __v);
Eric Fiselier212e3f22017-04-16 03:17:01 +00001430#ifndef _LIBCPP_CXX03_LANG
Marshall Clowea52cc42017-01-24 23:09:12 +00001431#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001432 template <class... _Args> reference emplace_front(_Args&&... __args);
Marshall Clowea52cc42017-01-24 23:09:12 +00001433 template <class... _Args> reference emplace_back (_Args&&... __args);
1434#else
1435 template <class... _Args> void emplace_front(_Args&&... __args);
1436 template <class... _Args> void emplace_back (_Args&&... __args);
1437#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001438 template <class... _Args> iterator emplace(const_iterator __p, _Args&&... __args);
Eric Fiselier212e3f22017-04-16 03:17:01 +00001439
Howard Hinnantc51e1022010-05-11 19:42:16 +00001440 void push_front(value_type&& __v);
1441 void push_back(value_type&& __v);
1442 iterator insert(const_iterator __p, value_type&& __v);
Eric Fiselier212e3f22017-04-16 03:17:01 +00001443
1444 _LIBCPP_INLINE_VISIBILITY
1445 iterator insert(const_iterator __p, initializer_list<value_type> __il)
1446 {return insert(__p, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001447#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001448 iterator insert(const_iterator __p, const value_type& __v);
1449 iterator insert(const_iterator __p, size_type __n, const value_type& __v);
1450 template <class _InputIter>
Marshall Clow6b612cf2015-01-22 18:33:29 +00001451 iterator insert(const_iterator __p, _InputIter __f, _InputIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001452 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value
1453 &&!__is_cpp17_forward_iterator<_InputIter>::value>::type* = 0);
Marshall Clow6b612cf2015-01-22 18:33:29 +00001454 template <class _ForwardIterator>
1455 iterator insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001456 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value
1457 &&!__is_cpp17_bidirectional_iterator<_ForwardIterator>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001458 template <class _BiIter>
Marshall Clow6b612cf2015-01-22 18:33:29 +00001459 iterator insert(const_iterator __p, _BiIter __f, _BiIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001460 typename enable_if<__is_cpp17_bidirectional_iterator<_BiIter>::value>::type* = 0);
Eric Fiselier212e3f22017-04-16 03:17:01 +00001461
Howard Hinnantc51e1022010-05-11 19:42:16 +00001462 void pop_front();
1463 void pop_back();
1464 iterator erase(const_iterator __p);
1465 iterator erase(const_iterator __f, const_iterator __l);
1466
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001467 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantca2fc222011-06-02 20:00:14 +00001468 void swap(deque& __c)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001469#if _LIBCPP_STD_VER >= 14
1470 _NOEXCEPT;
1471#else
Howard Hinnantca2fc222011-06-02 20:00:14 +00001472 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1473 __is_nothrow_swappable<allocator_type>::value);
Marshall Clow8982dcd2015-07-13 20:04:56 +00001474#endif
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001475 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantda2df912011-06-02 16:10:22 +00001476 void clear() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001477
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001478 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001479 bool __invariants() const {return __base::__invariants();}
Eric Fiselier27e03622019-08-01 23:11:18 +00001480
Howard Hinnantdcb73a32013-06-23 21:17:24 +00001481 typedef typename __base::__map_const_pointer __map_const_pointer;
1482
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001483 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001484 static size_type __recommend_blocks(size_type __n)
1485 {
1486 return __n / __base::__block_size + (__n % __base::__block_size != 0);
1487 }
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001488 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001489 size_type __capacity() const
1490 {
1491 return __base::__map_.size() == 0 ? 0 : __base::__map_.size() * __base::__block_size - 1;
1492 }
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001493 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier27e03622019-08-01 23:11:18 +00001494 size_type __block_count() const
1495 {
1496 return __base::__map_.size();
1497 }
1498
1499 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001500 size_type __front_spare() const
1501 {
1502 return __base::__start_;
1503 }
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001504 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier27e03622019-08-01 23:11:18 +00001505 size_type __front_spare_blocks() const {
1506 return __front_spare() / __base::__block_size;
1507 }
1508 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001509 size_type __back_spare() const
1510 {
1511 return __capacity() - (__base::__start_ + __base::size());
1512 }
Eric Fiselier27e03622019-08-01 23:11:18 +00001513 _LIBCPP_INLINE_VISIBILITY
1514 size_type __back_spare_blocks() const {
1515 return __back_spare() / __base::__block_size;
1516 }
1517
1518 private:
1519 _LIBCPP_INLINE_VISIBILITY
1520 bool __maybe_remove_front_spare(bool __keep_one = true) {
1521 if (__front_spare_blocks() >= 2 || (!__keep_one && __front_spare_blocks())) {
1522 __alloc_traits::deallocate(__base::__alloc(), __base::__map_.front(),
1523 __base::__block_size);
1524 __base::__map_.pop_front();
1525 __base::__start_ -= __base::__block_size;
1526 return true;
1527 }
1528 return false;
1529 }
1530
1531 _LIBCPP_INLINE_VISIBILITY
1532 bool __maybe_remove_back_spare(bool __keep_one = true) {
1533 if (__back_spare_blocks() >= 2 || (!__keep_one && __back_spare_blocks())) {
1534 __alloc_traits::deallocate(__base::__alloc(), __base::__map_.back(),
1535 __base::__block_size);
1536 __base::__map_.pop_back();
1537 return true;
1538 }
1539 return false;
1540 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001541
1542 template <class _InpIter>
1543 void __append(_InpIter __f, _InpIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001544 typename enable_if<__is_cpp17_input_iterator<_InpIter>::value &&
1545 !__is_cpp17_forward_iterator<_InpIter>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001546 template <class _ForIter>
1547 void __append(_ForIter __f, _ForIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001548 typename enable_if<__is_cpp17_forward_iterator<_ForIter>::value>::type* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001549 void __append(size_type __n);
1550 void __append(size_type __n, const value_type& __v);
1551 void __erase_to_end(const_iterator __f);
1552 void __add_front_capacity();
1553 void __add_front_capacity(size_type __n);
1554 void __add_back_capacity();
1555 void __add_back_capacity(size_type __n);
1556 iterator __move_and_check(iterator __f, iterator __l, iterator __r,
1557 const_pointer& __vt);
1558 iterator __move_backward_and_check(iterator __f, iterator __l, iterator __r,
1559 const_pointer& __vt);
1560 void __move_construct_and_check(iterator __f, iterator __l,
1561 iterator __r, const_pointer& __vt);
1562 void __move_construct_backward_and_check(iterator __f, iterator __l,
1563 iterator __r, const_pointer& __vt);
1564
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001565 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001566 void __copy_assign_alloc(const deque& __c)
1567 {__copy_assign_alloc(__c, integral_constant<bool,
1568 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1569
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001570 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001571 void __copy_assign_alloc(const deque& __c, true_type)
1572 {
1573 if (__base::__alloc() != __c.__alloc())
1574 {
1575 clear();
1576 shrink_to_fit();
1577 }
1578 __base::__alloc() = __c.__alloc();
1579 __base::__map_.__alloc() = __c.__map_.__alloc();
1580 }
1581
Howard Hinnant874ad9a2010-09-21 21:28:23 +00001582 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001583 void __copy_assign_alloc(const deque&, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001584 {}
1585
Howard Hinnant4931e092011-06-02 21:38:57 +00001586 void __move_assign(deque& __c, true_type)
1587 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001588 void __move_assign(deque& __c, false_type);
1589};
1590
Louis Dionned59f8a52021-08-17 11:59:07 -04001591#if _LIBCPP_STD_VER >= 17
Marshall Clow4cc3a452018-05-18 23:44:13 +00001592template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001593 class _Alloc = allocator<__iter_value_type<_InputIterator>>,
1594 class = _EnableIf<__is_allocator<_Alloc>::value>
Marshall Clow4cc3a452018-05-18 23:44:13 +00001595 >
1596deque(_InputIterator, _InputIterator)
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001597 -> deque<__iter_value_type<_InputIterator>, _Alloc>;
Marshall Clow4cc3a452018-05-18 23:44:13 +00001598
1599template<class _InputIterator,
1600 class _Alloc,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001601 class = _EnableIf<__is_allocator<_Alloc>::value>
Marshall Clow4cc3a452018-05-18 23:44:13 +00001602 >
1603deque(_InputIterator, _InputIterator, _Alloc)
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001604 -> deque<__iter_value_type<_InputIterator>, _Alloc>;
Marshall Clow4cc3a452018-05-18 23:44:13 +00001605#endif
1606
1607
Howard Hinnantc51e1022010-05-11 19:42:16 +00001608template <class _Tp, class _Allocator>
1609deque<_Tp, _Allocator>::deque(size_type __n)
1610{
1611 if (__n > 0)
1612 __append(__n);
1613}
1614
Marshall Clowbc4fcb42013-09-07 16:16:19 +00001615#if _LIBCPP_STD_VER > 11
1616template <class _Tp, class _Allocator>
1617deque<_Tp, _Allocator>::deque(size_type __n, const _Allocator& __a)
1618 : __base(__a)
1619{
1620 if (__n > 0)
1621 __append(__n);
1622}
1623#endif
1624
Howard Hinnantc51e1022010-05-11 19:42:16 +00001625template <class _Tp, class _Allocator>
1626deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v)
1627{
1628 if (__n > 0)
1629 __append(__n, __v);
1630}
1631
1632template <class _Tp, class _Allocator>
1633deque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v, const allocator_type& __a)
1634 : __base(__a)
1635{
1636 if (__n > 0)
1637 __append(__n, __v);
1638}
1639
1640template <class _Tp, class _Allocator>
1641template <class _InputIter>
1642deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001643 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001644{
1645 __append(__f, __l);
1646}
1647
1648template <class _Tp, class _Allocator>
1649template <class _InputIter>
1650deque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001651 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001652 : __base(__a)
1653{
1654 __append(__f, __l);
1655}
1656
1657template <class _Tp, class _Allocator>
1658deque<_Tp, _Allocator>::deque(const deque& __c)
1659 : __base(__alloc_traits::select_on_container_copy_construction(__c.__alloc()))
1660{
1661 __append(__c.begin(), __c.end());
1662}
1663
1664template <class _Tp, class _Allocator>
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05001665deque<_Tp, _Allocator>::deque(const deque& __c, const __identity_t<allocator_type>& __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001666 : __base(__a)
1667{
1668 __append(__c.begin(), __c.end());
1669}
1670
Eric Fiselier212e3f22017-04-16 03:17:01 +00001671template <class _Tp, class _Allocator>
1672deque<_Tp, _Allocator>&
1673deque<_Tp, _Allocator>::operator=(const deque& __c)
1674{
1675 if (this != &__c)
1676 {
1677 __copy_assign_alloc(__c);
1678 assign(__c.begin(), __c.end());
1679 }
1680 return *this;
1681}
1682
1683#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00001684
Howard Hinnantc51e1022010-05-11 19:42:16 +00001685template <class _Tp, class _Allocator>
1686deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il)
1687{
1688 __append(__il.begin(), __il.end());
1689}
1690
1691template <class _Tp, class _Allocator>
1692deque<_Tp, _Allocator>::deque(initializer_list<value_type> __il, const allocator_type& __a)
1693 : __base(__a)
1694{
1695 __append(__il.begin(), __il.end());
1696}
1697
Howard Hinnantc51e1022010-05-11 19:42:16 +00001698template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001699inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001700deque<_Tp, _Allocator>::deque(deque&& __c)
Howard Hinnantca2fc222011-06-02 20:00:14 +00001701 _NOEXCEPT_(is_nothrow_move_constructible<__base>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001702 : __base(_VSTD::move(__c))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001703{
1704}
1705
1706template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001707inline
Arthur O'Dwyer9b9662f2021-03-01 17:08:24 -05001708deque<_Tp, _Allocator>::deque(deque&& __c, const __identity_t<allocator_type>& __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001709 : __base(_VSTD::move(__c), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001710{
1711 if (__a != __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}
1717
1718template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001719inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001720deque<_Tp, _Allocator>&
1721deque<_Tp, _Allocator>::operator=(deque&& __c)
Howard Hinnant4931e092011-06-02 21:38:57 +00001722 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1723 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001724{
1725 __move_assign(__c, integral_constant<bool,
1726 __alloc_traits::propagate_on_container_move_assignment::value>());
1727 return *this;
1728}
1729
1730template <class _Tp, class _Allocator>
1731void
1732deque<_Tp, _Allocator>::__move_assign(deque& __c, false_type)
1733{
1734 if (__base::__alloc() != __c.__alloc())
1735 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001736 typedef move_iterator<iterator> _Ip;
1737 assign(_Ip(__c.begin()), _Ip(__c.end()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001738 }
1739 else
1740 __move_assign(__c, true_type());
1741}
1742
1743template <class _Tp, class _Allocator>
1744void
1745deque<_Tp, _Allocator>::__move_assign(deque& __c, true_type)
Howard Hinnant4931e092011-06-02 21:38:57 +00001746 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001747{
1748 clear();
1749 shrink_to_fit();
1750 __base::__move_assign(__c);
1751}
1752
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001753#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001754
1755template <class _Tp, class _Allocator>
1756template <class _InputIter>
1757void
1758deque<_Tp, _Allocator>::assign(_InputIter __f, _InputIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001759 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value &&
1760 !__is_cpp17_random_access_iterator<_InputIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001761{
1762 iterator __i = __base::begin();
1763 iterator __e = __base::end();
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001764 for (; __f != __l && __i != __e; ++__f, (void) ++__i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001765 *__i = *__f;
1766 if (__f != __l)
1767 __append(__f, __l);
1768 else
1769 __erase_to_end(__i);
1770}
1771
1772template <class _Tp, class _Allocator>
1773template <class _RAIter>
1774void
1775deque<_Tp, _Allocator>::assign(_RAIter __f, _RAIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001776 typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001777{
1778 if (static_cast<size_type>(__l - __f) > __base::size())
1779 {
1780 _RAIter __m = __f + __base::size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001781 _VSTD::copy(__f, __m, __base::begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001782 __append(__m, __l);
1783 }
1784 else
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001785 __erase_to_end(_VSTD::copy(__f, __l, __base::begin()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001786}
1787
1788template <class _Tp, class _Allocator>
1789void
1790deque<_Tp, _Allocator>::assign(size_type __n, const value_type& __v)
1791{
1792 if (__n > __base::size())
1793 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001794 _VSTD::fill_n(__base::begin(), __base::size(), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001795 __n -= __base::size();
1796 __append(__n, __v);
1797 }
1798 else
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001799 __erase_to_end(_VSTD::fill_n(__base::begin(), __n, __v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001800}
1801
1802template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001803inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001804_Allocator
Howard Hinnantda2df912011-06-02 16:10:22 +00001805deque<_Tp, _Allocator>::get_allocator() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001806{
1807 return __base::__alloc();
1808}
1809
1810template <class _Tp, class _Allocator>
1811void
1812deque<_Tp, _Allocator>::resize(size_type __n)
1813{
1814 if (__n > __base::size())
1815 __append(__n - __base::size());
1816 else if (__n < __base::size())
1817 __erase_to_end(__base::begin() + __n);
1818}
1819
1820template <class _Tp, class _Allocator>
1821void
1822deque<_Tp, _Allocator>::resize(size_type __n, const value_type& __v)
1823{
1824 if (__n > __base::size())
1825 __append(__n - __base::size(), __v);
1826 else if (__n < __base::size())
1827 __erase_to_end(__base::begin() + __n);
1828}
1829
1830template <class _Tp, class _Allocator>
1831void
Howard Hinnant4931e092011-06-02 21:38:57 +00001832deque<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001833{
1834 allocator_type& __a = __base::__alloc();
1835 if (empty())
1836 {
1837 while (__base::__map_.size() > 0)
1838 {
1839 __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
1840 __base::__map_.pop_back();
1841 }
1842 __base::__start_ = 0;
1843 }
1844 else
1845 {
Eric Fiselier27e03622019-08-01 23:11:18 +00001846 __maybe_remove_front_spare(/*__keep_one=*/false);
1847 __maybe_remove_back_spare(/*__keep_one=*/false);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001848 }
1849 __base::__map_.shrink_to_fit();
1850}
1851
1852template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001853inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001854typename deque<_Tp, _Allocator>::reference
Marshall Clow8d7c9f12019-03-14 21:56:57 +00001855deque<_Tp, _Allocator>::operator[](size_type __i) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001856{
1857 size_type __p = __base::__start_ + __i;
1858 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1859}
1860
1861template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001862inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001863typename deque<_Tp, _Allocator>::const_reference
Marshall Clow8d7c9f12019-03-14 21:56:57 +00001864deque<_Tp, _Allocator>::operator[](size_type __i) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001865{
1866 size_type __p = __base::__start_ + __i;
1867 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1868}
1869
1870template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001871inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001872typename deque<_Tp, _Allocator>::reference
1873deque<_Tp, _Allocator>::at(size_type __i)
1874{
1875 if (__i >= __base::size())
1876 __base::__throw_out_of_range();
1877 size_type __p = __base::__start_ + __i;
1878 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1879}
1880
1881template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001882inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001883typename deque<_Tp, _Allocator>::const_reference
1884deque<_Tp, _Allocator>::at(size_type __i) const
1885{
1886 if (__i >= __base::size())
1887 __base::__throw_out_of_range();
1888 size_type __p = __base::__start_ + __i;
1889 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1890}
1891
1892template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001893inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001894typename deque<_Tp, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00001895deque<_Tp, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001896{
1897 return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1898 + __base::__start_ % __base::__block_size);
1899}
1900
1901template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001902inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001903typename deque<_Tp, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00001904deque<_Tp, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001905{
1906 return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
1907 + __base::__start_ % __base::__block_size);
1908}
1909
1910template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001911inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001912typename deque<_Tp, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00001913deque<_Tp, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001914{
1915 size_type __p = __base::size() + __base::__start_ - 1;
1916 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1917}
1918
1919template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001920inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001921typename deque<_Tp, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00001922deque<_Tp, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001923{
1924 size_type __p = __base::size() + __base::__start_ - 1;
1925 return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
1926}
1927
1928template <class _Tp, class _Allocator>
1929void
1930deque<_Tp, _Allocator>::push_back(const value_type& __v)
1931{
1932 allocator_type& __a = __base::__alloc();
1933 if (__back_spare() == 0)
1934 __add_back_capacity();
1935 // __back_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001936 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001937 ++__base::size();
1938}
1939
Eric Fiselier212e3f22017-04-16 03:17:01 +00001940template <class _Tp, class _Allocator>
1941void
1942deque<_Tp, _Allocator>::push_front(const value_type& __v)
1943{
1944 allocator_type& __a = __base::__alloc();
1945 if (__front_spare() == 0)
1946 __add_front_capacity();
1947 // __front_spare() >= 1
1948 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v);
1949 --__base::__start_;
1950 ++__base::size();
1951}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001952
Eric Fiselier212e3f22017-04-16 03:17:01 +00001953#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001954template <class _Tp, class _Allocator>
1955void
1956deque<_Tp, _Allocator>::push_back(value_type&& __v)
1957{
1958 allocator_type& __a = __base::__alloc();
1959 if (__back_spare() == 0)
1960 __add_back_capacity();
1961 // __back_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001962 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001963 ++__base::size();
1964}
1965
1966template <class _Tp, class _Allocator>
1967template <class... _Args>
Marshall Clowea52cc42017-01-24 23:09:12 +00001968#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001969typename deque<_Tp, _Allocator>::reference
Marshall Clowea52cc42017-01-24 23:09:12 +00001970#else
1971void
1972#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001973deque<_Tp, _Allocator>::emplace_back(_Args&&... __args)
1974{
1975 allocator_type& __a = __base::__alloc();
1976 if (__back_spare() == 0)
1977 __add_back_capacity();
1978 // __back_spare() >= 1
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001979 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()),
1980 _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001981 ++__base::size();
Marshall Clowea52cc42017-01-24 23:09:12 +00001982#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00001983 return *--__base::end();
Marshall Clowea52cc42017-01-24 23:09:12 +00001984#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001985}
1986
Howard Hinnantc51e1022010-05-11 19:42:16 +00001987template <class _Tp, class _Allocator>
1988void
1989deque<_Tp, _Allocator>::push_front(value_type&& __v)
1990{
1991 allocator_type& __a = __base::__alloc();
1992 if (__front_spare() == 0)
1993 __add_front_capacity();
1994 // __front_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001995 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001996 --__base::__start_;
1997 ++__base::size();
1998}
1999
Howard Hinnant74279a52010-09-04 23:28:19 +00002000
Howard Hinnantc51e1022010-05-11 19:42:16 +00002001template <class _Tp, class _Allocator>
2002template <class... _Args>
Marshall Clowea52cc42017-01-24 23:09:12 +00002003#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002004typename deque<_Tp, _Allocator>::reference
Marshall Clowea52cc42017-01-24 23:09:12 +00002005#else
2006void
2007#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002008deque<_Tp, _Allocator>::emplace_front(_Args&&... __args)
2009{
2010 allocator_type& __a = __base::__alloc();
2011 if (__front_spare() == 0)
2012 __add_front_capacity();
2013 // __front_spare() >= 1
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002014 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002015 --__base::__start_;
2016 ++__base::size();
Marshall Clowea52cc42017-01-24 23:09:12 +00002017#if _LIBCPP_STD_VER > 14
Eric Fiselier34ba5b92016-07-21 03:20:17 +00002018 return *__base::begin();
Marshall Clowea52cc42017-01-24 23:09:12 +00002019#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002020}
2021
Eric Fiselier212e3f22017-04-16 03:17:01 +00002022template <class _Tp, class _Allocator>
2023typename deque<_Tp, _Allocator>::iterator
2024deque<_Tp, _Allocator>::insert(const_iterator __p, value_type&& __v)
2025{
2026 size_type __pos = __p - __base::begin();
2027 size_type __to_end = __base::size() - __pos;
2028 allocator_type& __a = __base::__alloc();
2029 if (__pos < __to_end)
2030 { // insert by shifting things backward
2031 if (__front_spare() == 0)
2032 __add_front_capacity();
2033 // __front_spare() >= 1
2034 if (__pos == 0)
2035 {
2036 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v));
2037 --__base::__start_;
2038 ++__base::size();
2039 }
2040 else
2041 {
2042 iterator __b = __base::begin();
2043 iterator __bm1 = _VSTD::prev(__b);
2044 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
2045 --__base::__start_;
2046 ++__base::size();
2047 if (__pos > 1)
2048 __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
2049 *__b = _VSTD::move(__v);
2050 }
2051 }
2052 else
2053 { // insert by shifting things forward
2054 if (__back_spare() == 0)
2055 __add_back_capacity();
2056 // __back_capacity >= 1
2057 size_type __de = __base::size() - __pos;
2058 if (__de == 0)
2059 {
2060 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v));
2061 ++__base::size();
2062 }
2063 else
2064 {
2065 iterator __e = __base::end();
2066 iterator __em1 = _VSTD::prev(__e);
2067 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
2068 ++__base::size();
2069 if (__de > 1)
2070 __e = _VSTD::move_backward(__e - __de, __em1, __e);
2071 *--__e = _VSTD::move(__v);
2072 }
2073 }
2074 return __base::begin() + __pos;
2075}
2076
2077template <class _Tp, class _Allocator>
2078template <class... _Args>
2079typename deque<_Tp, _Allocator>::iterator
2080deque<_Tp, _Allocator>::emplace(const_iterator __p, _Args&&... __args)
2081{
2082 size_type __pos = __p - __base::begin();
2083 size_type __to_end = __base::size() - __pos;
2084 allocator_type& __a = __base::__alloc();
2085 if (__pos < __to_end)
2086 { // insert by shifting things backward
2087 if (__front_spare() == 0)
2088 __add_front_capacity();
2089 // __front_spare() >= 1
2090 if (__pos == 0)
2091 {
2092 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...);
2093 --__base::__start_;
2094 ++__base::size();
2095 }
2096 else
2097 {
2098 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
2099 iterator __b = __base::begin();
2100 iterator __bm1 = _VSTD::prev(__b);
2101 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
2102 --__base::__start_;
2103 ++__base::size();
2104 if (__pos > 1)
2105 __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
2106 *__b = _VSTD::move(__tmp.get());
2107 }
2108 }
2109 else
2110 { // insert by shifting things forward
2111 if (__back_spare() == 0)
2112 __add_back_capacity();
2113 // __back_capacity >= 1
2114 size_type __de = __base::size() - __pos;
2115 if (__de == 0)
2116 {
2117 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::forward<_Args>(__args)...);
2118 ++__base::size();
2119 }
2120 else
2121 {
2122 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
2123 iterator __e = __base::end();
2124 iterator __em1 = _VSTD::prev(__e);
2125 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
2126 ++__base::size();
2127 if (__de > 1)
2128 __e = _VSTD::move_backward(__e - __de, __em1, __e);
2129 *--__e = _VSTD::move(__tmp.get());
2130 }
2131 }
2132 return __base::begin() + __pos;
2133}
2134
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002135#endif // _LIBCPP_CXX03_LANG
Eric Fiselier212e3f22017-04-16 03:17:01 +00002136
Howard Hinnantc51e1022010-05-11 19:42:16 +00002137
2138template <class _Tp, class _Allocator>
2139typename deque<_Tp, _Allocator>::iterator
2140deque<_Tp, _Allocator>::insert(const_iterator __p, const value_type& __v)
2141{
2142 size_type __pos = __p - __base::begin();
2143 size_type __to_end = __base::size() - __pos;
2144 allocator_type& __a = __base::__alloc();
2145 if (__pos < __to_end)
2146 { // insert by shifting things backward
2147 if (__front_spare() == 0)
2148 __add_front_capacity();
2149 // __front_spare() >= 1
2150 if (__pos == 0)
2151 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002152 __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002153 --__base::__start_;
2154 ++__base::size();
2155 }
2156 else
2157 {
2158 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2159 iterator __b = __base::begin();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002160 iterator __bm1 = _VSTD::prev(__b);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002161 if (__vt == pointer_traits<const_pointer>::pointer_to(*__b))
2162 __vt = pointer_traits<const_pointer>::pointer_to(*__bm1);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002163 __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002164 --__base::__start_;
2165 ++__base::size();
2166 if (__pos > 1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002167 __b = __move_and_check(_VSTD::next(__b), __b + __pos, __b, __vt);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002168 *__b = *__vt;
2169 }
2170 }
2171 else
2172 { // insert by shifting things forward
2173 if (__back_spare() == 0)
2174 __add_back_capacity();
2175 // __back_capacity >= 1
2176 size_type __de = __base::size() - __pos;
2177 if (__de == 0)
2178 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002179 __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002180 ++__base::size();
2181 }
2182 else
2183 {
2184 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2185 iterator __e = __base::end();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002186 iterator __em1 = _VSTD::prev(__e);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002187 if (__vt == pointer_traits<const_pointer>::pointer_to(*__em1))
2188 __vt = pointer_traits<const_pointer>::pointer_to(*__e);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002189 __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002190 ++__base::size();
2191 if (__de > 1)
2192 __e = __move_backward_and_check(__e - __de, __em1, __e, __vt);
2193 *--__e = *__vt;
2194 }
2195 }
2196 return __base::begin() + __pos;
2197}
2198
Howard Hinnantc51e1022010-05-11 19:42:16 +00002199template <class _Tp, class _Allocator>
2200typename deque<_Tp, _Allocator>::iterator
2201deque<_Tp, _Allocator>::insert(const_iterator __p, size_type __n, const value_type& __v)
2202{
2203 size_type __pos = __p - __base::begin();
2204 size_type __to_end = __base::size() - __pos;
2205 allocator_type& __a = __base::__alloc();
2206 if (__pos < __to_end)
2207 { // insert by shifting things backward
2208 if (__n > __front_spare())
2209 __add_front_capacity(__n - __front_spare());
2210 // __n <= __front_spare()
Howard Hinnantc51e1022010-05-11 19:42:16 +00002211 iterator __old_begin = __base::begin();
2212 iterator __i = __old_begin;
2213 if (__n > __pos)
2214 {
2215 for (size_type __m = __n - __pos; __m; --__m, --__base::__start_, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002216 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002217 __n = __pos;
2218 }
2219 if (__n > 0)
2220 {
2221 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2222 iterator __obn = __old_begin + __n;
2223 __move_construct_backward_and_check(__old_begin, __obn, __i, __vt);
2224 if (__n < __pos)
2225 __old_begin = __move_and_check(__obn, __old_begin + __pos, __old_begin, __vt);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002226 _VSTD::fill_n(__old_begin, __n, *__vt);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002227 }
2228 }
2229 else
2230 { // insert by shifting things forward
2231 size_type __back_capacity = __back_spare();
2232 if (__n > __back_capacity)
2233 __add_back_capacity(__n - __back_capacity);
2234 // __n <= __back_capacity
Howard Hinnantc51e1022010-05-11 19:42:16 +00002235 iterator __old_end = __base::end();
2236 iterator __i = __old_end;
2237 size_type __de = __base::size() - __pos;
2238 if (__n > __de)
2239 {
2240 for (size_type __m = __n - __de; __m; --__m, ++__i, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002241 __alloc_traits::construct(__a, _VSTD::addressof(*__i), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002242 __n = __de;
2243 }
2244 if (__n > 0)
2245 {
2246 const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
2247 iterator __oen = __old_end - __n;
2248 __move_construct_and_check(__oen, __old_end, __i, __vt);
2249 if (__n < __de)
2250 __old_end = __move_backward_and_check(__old_end - __de, __oen, __old_end, __vt);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002251 _VSTD::fill_n(__old_end - __n, __n, *__vt);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002252 }
2253 }
2254 return __base::begin() + __pos;
2255}
2256
2257template <class _Tp, class _Allocator>
2258template <class _InputIter>
2259typename deque<_Tp, _Allocator>::iterator
2260deque<_Tp, _Allocator>::insert(const_iterator __p, _InputIter __f, _InputIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002261 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value
2262 &&!__is_cpp17_forward_iterator<_InputIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002263{
2264 __split_buffer<value_type, allocator_type&> __buf(__base::__alloc());
2265 __buf.__construct_at_end(__f, __l);
2266 typedef typename __split_buffer<value_type, allocator_type&>::iterator __bi;
2267 return insert(__p, move_iterator<__bi>(__buf.begin()), move_iterator<__bi>(__buf.end()));
2268}
2269
2270template <class _Tp, class _Allocator>
Marshall Clow6b612cf2015-01-22 18:33:29 +00002271template <class _ForwardIterator>
2272typename deque<_Tp, _Allocator>::iterator
2273deque<_Tp, _Allocator>::insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002274 typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value
2275 &&!__is_cpp17_bidirectional_iterator<_ForwardIterator>::value>::type*)
Marshall Clow6b612cf2015-01-22 18:33:29 +00002276{
2277 size_type __n = _VSTD::distance(__f, __l);
2278 __split_buffer<value_type, allocator_type&> __buf(__n, 0, __base::__alloc());
2279 __buf.__construct_at_end(__f, __l);
2280 typedef typename __split_buffer<value_type, allocator_type&>::iterator __fwd;
2281 return insert(__p, move_iterator<__fwd>(__buf.begin()), move_iterator<__fwd>(__buf.end()));
2282}
2283
2284template <class _Tp, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002285template <class _BiIter>
2286typename deque<_Tp, _Allocator>::iterator
2287deque<_Tp, _Allocator>::insert(const_iterator __p, _BiIter __f, _BiIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002288 typename enable_if<__is_cpp17_bidirectional_iterator<_BiIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002289{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002290 size_type __n = _VSTD::distance(__f, __l);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002291 size_type __pos = __p - __base::begin();
2292 size_type __to_end = __base::size() - __pos;
2293 allocator_type& __a = __base::__alloc();
2294 if (__pos < __to_end)
2295 { // insert by shifting things backward
2296 if (__n > __front_spare())
2297 __add_front_capacity(__n - __front_spare());
2298 // __n <= __front_spare()
Howard Hinnantc51e1022010-05-11 19:42:16 +00002299 iterator __old_begin = __base::begin();
2300 iterator __i = __old_begin;
2301 _BiIter __m = __f;
2302 if (__n > __pos)
2303 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002304 __m = __pos < __n / 2 ? _VSTD::prev(__l, __pos) : _VSTD::next(__f, __n - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002305 for (_BiIter __j = __m; __j != __f; --__base::__start_, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002306 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), *--__j);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002307 __n = __pos;
2308 }
2309 if (__n > 0)
2310 {
2311 iterator __obn = __old_begin + __n;
2312 for (iterator __j = __obn; __j != __old_begin;)
2313 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002314 __alloc_traits::construct(__a, _VSTD::addressof(*--__i), _VSTD::move(*--__j));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002315 --__base::__start_;
2316 ++__base::size();
2317 }
2318 if (__n < __pos)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002319 __old_begin = _VSTD::move(__obn, __old_begin + __pos, __old_begin);
2320 _VSTD::copy(__m, __l, __old_begin);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002321 }
2322 }
2323 else
2324 { // insert by shifting things forward
2325 size_type __back_capacity = __back_spare();
2326 if (__n > __back_capacity)
2327 __add_back_capacity(__n - __back_capacity);
2328 // __n <= __back_capacity
Howard Hinnantc51e1022010-05-11 19:42:16 +00002329 iterator __old_end = __base::end();
2330 iterator __i = __old_end;
2331 _BiIter __m = __l;
2332 size_type __de = __base::size() - __pos;
2333 if (__n > __de)
2334 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002335 __m = __de < __n / 2 ? _VSTD::next(__f, __de) : _VSTD::prev(__l, __n - __de);
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002336 for (_BiIter __j = __m; __j != __l; ++__i, (void) ++__j, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002337 __alloc_traits::construct(__a, _VSTD::addressof(*__i), *__j);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002338 __n = __de;
2339 }
2340 if (__n > 0)
2341 {
2342 iterator __oen = __old_end - __n;
2343 for (iterator __j = __oen; __j != __old_end; ++__i, ++__j, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002344 __alloc_traits::construct(__a, _VSTD::addressof(*__i), _VSTD::move(*__j));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002345 if (__n < __de)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002346 __old_end = _VSTD::move_backward(__old_end - __de, __oen, __old_end);
2347 _VSTD::copy_backward(__f, __m, __old_end);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002348 }
2349 }
2350 return __base::begin() + __pos;
2351}
2352
2353template <class _Tp, class _Allocator>
2354template <class _InpIter>
2355void
2356deque<_Tp, _Allocator>::__append(_InpIter __f, _InpIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002357 typename enable_if<__is_cpp17_input_iterator<_InpIter>::value &&
2358 !__is_cpp17_forward_iterator<_InpIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002359{
2360 for (; __f != __l; ++__f)
Eric Fiselier96919722017-10-17 13:03:17 +00002361#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002362 push_back(*__f);
Eric Fiselier96919722017-10-17 13:03:17 +00002363#else
2364 emplace_back(*__f);
2365#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002366}
2367
2368template <class _Tp, class _Allocator>
2369template <class _ForIter>
2370void
2371deque<_Tp, _Allocator>::__append(_ForIter __f, _ForIter __l,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002372 typename enable_if<__is_cpp17_forward_iterator<_ForIter>::value>::type*)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002373{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002374 size_type __n = _VSTD::distance(__f, __l);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002375 allocator_type& __a = __base::__alloc();
2376 size_type __back_capacity = __back_spare();
2377 if (__n > __back_capacity)
2378 __add_back_capacity(__n - __back_capacity);
2379 // __n <= __back_capacity
Eric Fiselierc52db212019-08-12 07:51:05 +00002380 for (__deque_block_range __br : __deque_range(__base::end(), __base::end() + __n)) {
2381 _ConstructTransaction __tx(this, __br);
2382 for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, (void)++__f) {
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05002383 __alloc_traits::construct(__a, _VSTD::__to_address(__tx.__pos_), *__f);
Eric Fiselierc52db212019-08-12 07:51:05 +00002384 }
2385 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002386}
2387
2388template <class _Tp, class _Allocator>
2389void
2390deque<_Tp, _Allocator>::__append(size_type __n)
2391{
2392 allocator_type& __a = __base::__alloc();
2393 size_type __back_capacity = __back_spare();
2394 if (__n > __back_capacity)
2395 __add_back_capacity(__n - __back_capacity);
2396 // __n <= __back_capacity
Eric Fiselierc52db212019-08-12 07:51:05 +00002397 for (__deque_block_range __br : __deque_range(__base::end(), __base::end() + __n)) {
2398 _ConstructTransaction __tx(this, __br);
2399 for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05002400 __alloc_traits::construct(__a, _VSTD::__to_address(__tx.__pos_));
Eric Fiselierc52db212019-08-12 07:51:05 +00002401 }
2402 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002403}
2404
2405template <class _Tp, class _Allocator>
2406void
2407deque<_Tp, _Allocator>::__append(size_type __n, const value_type& __v)
2408{
2409 allocator_type& __a = __base::__alloc();
2410 size_type __back_capacity = __back_spare();
2411 if (__n > __back_capacity)
2412 __add_back_capacity(__n - __back_capacity);
2413 // __n <= __back_capacity
Eric Fiselierc52db212019-08-12 07:51:05 +00002414 for (__deque_block_range __br : __deque_range(__base::end(), __base::end() + __n)) {
2415 _ConstructTransaction __tx(this, __br);
2416 for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05002417 __alloc_traits::construct(__a, _VSTD::__to_address(__tx.__pos_), __v);
Eric Fiselierc52db212019-08-12 07:51:05 +00002418 }
2419 }
2420
Howard Hinnantc51e1022010-05-11 19:42:16 +00002421}
2422
2423// Create front capacity for one block of elements.
2424// Strong guarantee. Either do it or don't touch anything.
2425template <class _Tp, class _Allocator>
2426void
2427deque<_Tp, _Allocator>::__add_front_capacity()
2428{
2429 allocator_type& __a = __base::__alloc();
2430 if (__back_spare() >= __base::__block_size)
2431 {
2432 __base::__start_ += __base::__block_size;
2433 pointer __pt = __base::__map_.back();
2434 __base::__map_.pop_back();
2435 __base::__map_.push_front(__pt);
2436 }
2437 // Else if __base::__map_.size() < __base::__map_.capacity() then we need to allocate 1 buffer
2438 else if (__base::__map_.size() < __base::__map_.capacity())
2439 { // we can put the new buffer into the map, but don't shift things around
2440 // until all buffers are allocated. If we throw, we don't need to fix
2441 // anything up (any added buffers are undetectible)
2442 if (__base::__map_.__front_spare() > 0)
2443 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2444 else
2445 {
2446 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2447 // Done allocating, reorder capacity
2448 pointer __pt = __base::__map_.back();
2449 __base::__map_.pop_back();
2450 __base::__map_.push_front(__pt);
2451 }
2452 __base::__start_ = __base::__map_.size() == 1 ?
2453 __base::__block_size / 2 :
2454 __base::__start_ + __base::__block_size;
2455 }
2456 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2457 else
2458 {
2459 __split_buffer<pointer, typename __base::__pointer_allocator&>
2460 __buf(max<size_type>(2 * __base::__map_.capacity(), 1),
2461 0, __base::__map_.__alloc());
Marshall Clow5fd466b2015-03-09 17:08:51 +00002462
Marshall Clow8982dcd2015-07-13 20:04:56 +00002463 typedef __allocator_destructor<_Allocator> _Dp;
2464 unique_ptr<pointer, _Dp> __hold(
2465 __alloc_traits::allocate(__a, __base::__block_size),
2466 _Dp(__a, __base::__block_size));
2467 __buf.push_back(__hold.get());
2468 __hold.release();
Louis Dionne72f24392018-12-12 23:58:25 +00002469
Howard Hinnantc51e1022010-05-11 19:42:16 +00002470 for (typename __base::__map_pointer __i = __base::__map_.begin();
2471 __i != __base::__map_.end(); ++__i)
2472 __buf.push_back(*__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002473 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2474 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2475 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2476 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002477 __base::__start_ = __base::__map_.size() == 1 ?
2478 __base::__block_size / 2 :
2479 __base::__start_ + __base::__block_size;
2480 }
2481}
2482
2483// Create front capacity for __n elements.
2484// Strong guarantee. Either do it or don't touch anything.
2485template <class _Tp, class _Allocator>
2486void
2487deque<_Tp, _Allocator>::__add_front_capacity(size_type __n)
2488{
2489 allocator_type& __a = __base::__alloc();
2490 size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2491 // Number of unused blocks at back:
2492 size_type __back_capacity = __back_spare() / __base::__block_size;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002493 __back_capacity = _VSTD::min(__back_capacity, __nb); // don't take more than you need
Howard Hinnantc51e1022010-05-11 19:42:16 +00002494 __nb -= __back_capacity; // number of blocks need to allocate
2495 // If __nb == 0, then we have sufficient capacity.
2496 if (__nb == 0)
2497 {
2498 __base::__start_ += __base::__block_size * __back_capacity;
2499 for (; __back_capacity > 0; --__back_capacity)
2500 {
2501 pointer __pt = __base::__map_.back();
2502 __base::__map_.pop_back();
2503 __base::__map_.push_front(__pt);
2504 }
2505 }
2506 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2507 else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2508 { // we can put the new buffers into the map, but don't shift things around
2509 // until all buffers are allocated. If we throw, we don't need to fix
2510 // anything up (any added buffers are undetectible)
2511 for (; __nb > 0; --__nb, __base::__start_ += __base::__block_size - (__base::__map_.size() == 1))
2512 {
2513 if (__base::__map_.__front_spare() == 0)
2514 break;
2515 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2516 }
2517 for (; __nb > 0; --__nb, ++__back_capacity)
2518 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2519 // Done allocating, reorder capacity
2520 __base::__start_ += __back_capacity * __base::__block_size;
2521 for (; __back_capacity > 0; --__back_capacity)
2522 {
2523 pointer __pt = __base::__map_.back();
2524 __base::__map_.pop_back();
2525 __base::__map_.push_front(__pt);
2526 }
2527 }
2528 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2529 else
2530 {
2531 size_type __ds = (__nb + __back_capacity) * __base::__block_size - __base::__map_.empty();
2532 __split_buffer<pointer, typename __base::__pointer_allocator&>
2533 __buf(max<size_type>(2* __base::__map_.capacity(),
2534 __nb + __base::__map_.size()),
2535 0, __base::__map_.__alloc());
2536#ifndef _LIBCPP_NO_EXCEPTIONS
2537 try
2538 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002539#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002540 for (; __nb > 0; --__nb)
2541 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2542#ifndef _LIBCPP_NO_EXCEPTIONS
2543 }
2544 catch (...)
2545 {
2546 for (typename __base::__map_pointer __i = __buf.begin();
2547 __i != __buf.end(); ++__i)
2548 __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2549 throw;
2550 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002551#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002552 for (; __back_capacity > 0; --__back_capacity)
2553 {
2554 __buf.push_back(__base::__map_.back());
2555 __base::__map_.pop_back();
2556 }
2557 for (typename __base::__map_pointer __i = __base::__map_.begin();
2558 __i != __base::__map_.end(); ++__i)
2559 __buf.push_back(*__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002560 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2561 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2562 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2563 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002564 __base::__start_ += __ds;
2565 }
2566}
2567
2568// Create back capacity for one block of elements.
2569// Strong guarantee. Either do it or don't touch anything.
2570template <class _Tp, class _Allocator>
2571void
2572deque<_Tp, _Allocator>::__add_back_capacity()
2573{
2574 allocator_type& __a = __base::__alloc();
2575 if (__front_spare() >= __base::__block_size)
2576 {
2577 __base::__start_ -= __base::__block_size;
2578 pointer __pt = __base::__map_.front();
2579 __base::__map_.pop_front();
2580 __base::__map_.push_back(__pt);
2581 }
2582 // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2583 else if (__base::__map_.size() < __base::__map_.capacity())
2584 { // we can put the new buffer into the map, but don't shift things around
2585 // until it is allocated. If we throw, we don't need to fix
2586 // anything up (any added buffers are undetectible)
2587 if (__base::__map_.__back_spare() != 0)
2588 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2589 else
2590 {
2591 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2592 // Done allocating, reorder capacity
2593 pointer __pt = __base::__map_.front();
2594 __base::__map_.pop_front();
2595 __base::__map_.push_back(__pt);
2596 }
2597 }
2598 // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
2599 else
2600 {
2601 __split_buffer<pointer, typename __base::__pointer_allocator&>
2602 __buf(max<size_type>(2* __base::__map_.capacity(), 1),
2603 __base::__map_.size(),
2604 __base::__map_.__alloc());
Marshall Clow5fd466b2015-03-09 17:08:51 +00002605
Marshall Clow8982dcd2015-07-13 20:04:56 +00002606 typedef __allocator_destructor<_Allocator> _Dp;
2607 unique_ptr<pointer, _Dp> __hold(
2608 __alloc_traits::allocate(__a, __base::__block_size),
2609 _Dp(__a, __base::__block_size));
2610 __buf.push_back(__hold.get());
2611 __hold.release();
Marshall Clow5fd466b2015-03-09 17:08:51 +00002612
Howard Hinnantc51e1022010-05-11 19:42:16 +00002613 for (typename __base::__map_pointer __i = __base::__map_.end();
2614 __i != __base::__map_.begin();)
2615 __buf.push_front(*--__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002616 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2617 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2618 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2619 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002620 }
2621}
2622
2623// Create back capacity for __n elements.
2624// Strong guarantee. Either do it or don't touch anything.
2625template <class _Tp, class _Allocator>
2626void
2627deque<_Tp, _Allocator>::__add_back_capacity(size_type __n)
2628{
2629 allocator_type& __a = __base::__alloc();
2630 size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
2631 // Number of unused blocks at front:
2632 size_type __front_capacity = __front_spare() / __base::__block_size;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002633 __front_capacity = _VSTD::min(__front_capacity, __nb); // don't take more than you need
Howard Hinnantc51e1022010-05-11 19:42:16 +00002634 __nb -= __front_capacity; // number of blocks need to allocate
2635 // If __nb == 0, then we have sufficient capacity.
2636 if (__nb == 0)
2637 {
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 if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
2647 else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
2648 { // we can put the new buffers into the map, but don't shift things around
2649 // until all buffers are allocated. If we throw, we don't need to fix
2650 // anything up (any added buffers are undetectible)
2651 for (; __nb > 0; --__nb)
2652 {
2653 if (__base::__map_.__back_spare() == 0)
2654 break;
2655 __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2656 }
2657 for (; __nb > 0; --__nb, ++__front_capacity, __base::__start_ +=
2658 __base::__block_size - (__base::__map_.size() == 1))
2659 __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
2660 // Done allocating, reorder capacity
2661 __base::__start_ -= __base::__block_size * __front_capacity;
2662 for (; __front_capacity > 0; --__front_capacity)
2663 {
2664 pointer __pt = __base::__map_.front();
2665 __base::__map_.pop_front();
2666 __base::__map_.push_back(__pt);
2667 }
2668 }
2669 // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
2670 else
2671 {
2672 size_type __ds = __front_capacity * __base::__block_size;
2673 __split_buffer<pointer, typename __base::__pointer_allocator&>
2674 __buf(max<size_type>(2* __base::__map_.capacity(),
2675 __nb + __base::__map_.size()),
2676 __base::__map_.size() - __front_capacity,
2677 __base::__map_.__alloc());
2678#ifndef _LIBCPP_NO_EXCEPTIONS
2679 try
2680 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002681#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002682 for (; __nb > 0; --__nb)
2683 __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
2684#ifndef _LIBCPP_NO_EXCEPTIONS
2685 }
2686 catch (...)
2687 {
2688 for (typename __base::__map_pointer __i = __buf.begin();
2689 __i != __buf.end(); ++__i)
2690 __alloc_traits::deallocate(__a, *__i, __base::__block_size);
2691 throw;
2692 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002693#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002694 for (; __front_capacity > 0; --__front_capacity)
2695 {
2696 __buf.push_back(__base::__map_.front());
2697 __base::__map_.pop_front();
2698 }
2699 for (typename __base::__map_pointer __i = __base::__map_.end();
2700 __i != __base::__map_.begin();)
2701 __buf.push_front(*--__i);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002702 _VSTD::swap(__base::__map_.__first_, __buf.__first_);
2703 _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
2704 _VSTD::swap(__base::__map_.__end_, __buf.__end_);
2705 _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002706 __base::__start_ -= __ds;
2707 }
2708}
2709
2710template <class _Tp, class _Allocator>
2711void
2712deque<_Tp, _Allocator>::pop_front()
2713{
2714 allocator_type& __a = __base::__alloc();
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05002715 __alloc_traits::destroy(__a, _VSTD::__to_address(*(__base::__map_.begin() +
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002716 __base::__start_ / __base::__block_size) +
2717 __base::__start_ % __base::__block_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002718 --__base::size();
Eric Fiselier27e03622019-08-01 23:11:18 +00002719 ++__base::__start_;
2720 __maybe_remove_front_spare();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002721}
2722
2723template <class _Tp, class _Allocator>
2724void
2725deque<_Tp, _Allocator>::pop_back()
2726{
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +02002727 _LIBCPP_ASSERT(!empty(), "deque::pop_back called on an empty deque");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002728 allocator_type& __a = __base::__alloc();
2729 size_type __p = __base::size() + __base::__start_ - 1;
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05002730 __alloc_traits::destroy(__a, _VSTD::__to_address(*(__base::__map_.begin() +
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002731 __p / __base::__block_size) +
2732 __p % __base::__block_size));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002733 --__base::size();
Eric Fiselier27e03622019-08-01 23:11:18 +00002734 __maybe_remove_back_spare();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002735}
2736
2737// move assign [__f, __l) to [__r, __r + (__l-__f)).
2738// If __vt points into [__f, __l), then subtract (__f - __r) from __vt.
2739template <class _Tp, class _Allocator>
2740typename deque<_Tp, _Allocator>::iterator
2741deque<_Tp, _Allocator>::__move_and_check(iterator __f, iterator __l, iterator __r,
2742 const_pointer& __vt)
2743{
2744 // as if
2745 // for (; __f != __l; ++__f, ++__r)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002746 // *__r = _VSTD::move(*__f);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002747 difference_type __n = __l - __f;
2748 while (__n > 0)
2749 {
2750 pointer __fb = __f.__ptr_;
2751 pointer __fe = *__f.__m_iter_ + __base::__block_size;
2752 difference_type __bs = __fe - __fb;
2753 if (__bs > __n)
2754 {
2755 __bs = __n;
2756 __fe = __fb + __bs;
2757 }
2758 if (__fb <= __vt && __vt < __fe)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002759 __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) -= __f - __r).__ptr_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002760 __r = _VSTD::move(__fb, __fe, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002761 __n -= __bs;
2762 __f += __bs;
2763 }
2764 return __r;
2765}
2766
2767// move assign [__f, __l) to [__r - (__l-__f), __r) backwards.
2768// If __vt points into [__f, __l), then add (__r - __l) to __vt.
2769template <class _Tp, class _Allocator>
2770typename deque<_Tp, _Allocator>::iterator
2771deque<_Tp, _Allocator>::__move_backward_and_check(iterator __f, iterator __l, iterator __r,
2772 const_pointer& __vt)
2773{
2774 // as if
2775 // while (__f != __l)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002776 // *--__r = _VSTD::move(*--__l);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002777 difference_type __n = __l - __f;
2778 while (__n > 0)
2779 {
2780 --__l;
2781 pointer __lb = *__l.__m_iter_;
2782 pointer __le = __l.__ptr_ + 1;
2783 difference_type __bs = __le - __lb;
2784 if (__bs > __n)
2785 {
2786 __bs = __n;
2787 __lb = __le - __bs;
2788 }
2789 if (__lb <= __vt && __vt < __le)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002790 __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) += __r - __l - 1).__ptr_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002791 __r = _VSTD::move_backward(__lb, __le, __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002792 __n -= __bs;
2793 __l -= __bs - 1;
2794 }
2795 return __r;
2796}
2797
2798// move construct [__f, __l) to [__r, __r + (__l-__f)).
2799// If __vt points into [__f, __l), then add (__r - __f) to __vt.
2800template <class _Tp, class _Allocator>
2801void
2802deque<_Tp, _Allocator>::__move_construct_and_check(iterator __f, iterator __l,
2803 iterator __r, const_pointer& __vt)
2804{
2805 allocator_type& __a = __base::__alloc();
2806 // as if
2807 // for (; __f != __l; ++__r, ++__f, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002808 // __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__f));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002809 difference_type __n = __l - __f;
2810 while (__n > 0)
2811 {
2812 pointer __fb = __f.__ptr_;
2813 pointer __fe = *__f.__m_iter_ + __base::__block_size;
2814 difference_type __bs = __fe - __fb;
2815 if (__bs > __n)
2816 {
2817 __bs = __n;
2818 __fe = __fb + __bs;
2819 }
2820 if (__fb <= __vt && __vt < __fe)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002821 __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) += __r - __f).__ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002822 for (; __fb != __fe; ++__fb, ++__r, ++__base::size())
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002823 __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__fb));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002824 __n -= __bs;
2825 __f += __bs;
2826 }
2827}
2828
2829// move construct [__f, __l) to [__r - (__l-__f), __r) backwards.
2830// If __vt points into [__f, __l), then subtract (__l - __r) from __vt.
2831template <class _Tp, class _Allocator>
2832void
2833deque<_Tp, _Allocator>::__move_construct_backward_and_check(iterator __f, iterator __l,
2834 iterator __r, const_pointer& __vt)
2835{
2836 allocator_type& __a = __base::__alloc();
2837 // as if
2838 // for (iterator __j = __l; __j != __f;)
2839 // {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002840 // __alloc_traitsconstruct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__j));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002841 // --__base::__start_;
2842 // ++__base::size();
2843 // }
2844 difference_type __n = __l - __f;
2845 while (__n > 0)
2846 {
2847 --__l;
2848 pointer __lb = *__l.__m_iter_;
2849 pointer __le = __l.__ptr_ + 1;
2850 difference_type __bs = __le - __lb;
2851 if (__bs > __n)
2852 {
2853 __bs = __n;
2854 __lb = __le - __bs;
2855 }
2856 if (__lb <= __vt && __vt < __le)
Howard Hinnantdcb73a32013-06-23 21:17:24 +00002857 __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) -= __l - __r + 1).__ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002858 while (__le != __lb)
2859 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002860 __alloc_traits::construct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__le));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002861 --__base::__start_;
2862 ++__base::size();
2863 }
2864 __n -= __bs;
2865 __l -= __bs - 1;
2866 }
2867}
2868
2869template <class _Tp, class _Allocator>
2870typename deque<_Tp, _Allocator>::iterator
2871deque<_Tp, _Allocator>::erase(const_iterator __f)
2872{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002873 iterator __b = __base::begin();
2874 difference_type __pos = __f - __b;
2875 iterator __p = __b + __pos;
2876 allocator_type& __a = __base::__alloc();
Eric Fiselier37c22152016-12-24 00:24:44 +00002877 if (static_cast<size_t>(__pos) <= (__base::size() - 1) / 2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002878 { // erase from front
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002879 _VSTD::move_backward(__b, __p, _VSTD::next(__p));
2880 __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002881 --__base::size();
2882 ++__base::__start_;
Eric Fiselier27e03622019-08-01 23:11:18 +00002883 __maybe_remove_front_spare();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002884 }
2885 else
2886 { // erase from back
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002887 iterator __i = _VSTD::move(_VSTD::next(__p), __base::end(), __p);
2888 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002889 --__base::size();
Eric Fiselier27e03622019-08-01 23:11:18 +00002890 __maybe_remove_back_spare();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002891 }
2892 return __base::begin() + __pos;
2893}
2894
2895template <class _Tp, class _Allocator>
2896typename deque<_Tp, _Allocator>::iterator
2897deque<_Tp, _Allocator>::erase(const_iterator __f, const_iterator __l)
2898{
2899 difference_type __n = __l - __f;
2900 iterator __b = __base::begin();
2901 difference_type __pos = __f - __b;
2902 iterator __p = __b + __pos;
2903 if (__n > 0)
2904 {
2905 allocator_type& __a = __base::__alloc();
Eric Fiselier37c22152016-12-24 00:24:44 +00002906 if (static_cast<size_t>(__pos) <= (__base::size() - __n) / 2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002907 { // erase from front
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002908 iterator __i = _VSTD::move_backward(__b, __p, __p + __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002909 for (; __b != __i; ++__b)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002910 __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002911 __base::size() -= __n;
2912 __base::__start_ += __n;
Eric Fiselier27e03622019-08-01 23:11:18 +00002913 while (__maybe_remove_front_spare()) {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002914 }
2915 }
2916 else
2917 { // erase from back
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002918 iterator __i = _VSTD::move(__p + __n, __base::end(), __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002919 for (iterator __e = __base::end(); __i != __e; ++__i)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002920 __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002921 __base::size() -= __n;
Eric Fiselier27e03622019-08-01 23:11:18 +00002922 while (__maybe_remove_back_spare()) {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002923 }
2924 }
2925 }
2926 return __base::begin() + __pos;
2927}
2928
2929template <class _Tp, class _Allocator>
2930void
2931deque<_Tp, _Allocator>::__erase_to_end(const_iterator __f)
2932{
2933 iterator __e = __base::end();
2934 difference_type __n = __e - __f;
2935 if (__n > 0)
2936 {
2937 allocator_type& __a = __base::__alloc();
2938 iterator __b = __base::begin();
2939 difference_type __pos = __f - __b;
2940 for (iterator __p = __b + __pos; __p != __e; ++__p)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002941 __alloc_traits::destroy(__a, _VSTD::addressof(*__p));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002942 __base::size() -= __n;
Eric Fiselier27e03622019-08-01 23:11:18 +00002943 while (__maybe_remove_back_spare()) {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002944 }
2945 }
2946}
2947
2948template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002949inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002950void
2951deque<_Tp, _Allocator>::swap(deque& __c)
Marshall Clow8982dcd2015-07-13 20:04:56 +00002952#if _LIBCPP_STD_VER >= 14
2953 _NOEXCEPT
2954#else
Louis Dionne72f24392018-12-12 23:58:25 +00002955 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00002956 __is_nothrow_swappable<allocator_type>::value)
2957#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002958{
2959 __base::swap(__c);
2960}
2961
2962template <class _Tp, class _Allocator>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00002963inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002964void
Howard Hinnantda2df912011-06-02 16:10:22 +00002965deque<_Tp, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002966{
2967 __base::clear();
2968}
2969
2970template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002971inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002972bool
2973operator==(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2974{
2975 const typename deque<_Tp, _Allocator>::size_type __sz = __x.size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002976 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002977}
2978
2979template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002980inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002981bool
2982operator!=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2983{
2984 return !(__x == __y);
2985}
2986
2987template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002988inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002989bool
2990operator< (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2991{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002992 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002993}
2994
2995template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00002996inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002997bool
2998operator> (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
2999{
3000 return __y < __x;
3001}
3002
3003template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003004inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003005bool
3006operator>=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
3007{
3008 return !(__x < __y);
3009}
3010
3011template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003012inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003013bool
3014operator<=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
3015{
3016 return !(__y < __x);
3017}
3018
3019template <class _Tp, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003020inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003021void
3022swap(deque<_Tp, _Allocator>& __x, deque<_Tp, _Allocator>& __y)
Howard Hinnantca2fc222011-06-02 20:00:14 +00003023 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003024{
3025 __x.swap(__y);
3026}
3027
Marshall Clow29b53f22018-12-14 18:49:35 +00003028#if _LIBCPP_STD_VER > 17
3029template <class _Tp, class _Allocator, class _Up>
Marek Kurdeja98b1412020-05-02 13:58:03 +02003030inline _LIBCPP_INLINE_VISIBILITY typename deque<_Tp, _Allocator>::size_type
3031erase(deque<_Tp, _Allocator>& __c, const _Up& __v) {
3032 auto __old_size = __c.size();
3033 __c.erase(_VSTD::remove(__c.begin(), __c.end(), __v), __c.end());
3034 return __old_size - __c.size();
3035}
Marshall Clow29b53f22018-12-14 18:49:35 +00003036
3037template <class _Tp, class _Allocator, class _Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +02003038inline _LIBCPP_INLINE_VISIBILITY typename deque<_Tp, _Allocator>::size_type
3039erase_if(deque<_Tp, _Allocator>& __c, _Predicate __pred) {
3040 auto __old_size = __c.size();
3041 __c.erase(_VSTD::remove_if(__c.begin(), __c.end(), __pred), __c.end());
3042 return __old_size - __c.size();
3043}
Marshall Clow29b53f22018-12-14 18:49:35 +00003044#endif
3045
3046
Howard Hinnantc51e1022010-05-11 19:42:16 +00003047_LIBCPP_END_NAMESPACE_STD
3048
Eric Fiselierf4433a32017-05-31 22:07:49 +00003049_LIBCPP_POP_MACROS
3050
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003051#endif // _LIBCPP_DEQUE