]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/stl_deque.h
Fix after P0600.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / stl_deque.h
CommitLineData
5cb6369d 1// Deque implementation -*- C++ -*-
42526146 2
a5544970 3// Copyright (C) 2001-2019 Free Software Foundation, Inc.
42526146
PE
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
748086b7 8// Free Software Foundation; either version 3, or (at your option)
42526146
PE
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
748086b7
JJ
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
42526146 19
748086b7
JJ
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
42526146 24
725dc051
BK
25/*
26 *
27 * Copyright (c) 1994
28 * Hewlett-Packard Company
29 *
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
37 *
38 *
39 * Copyright (c) 1997
40 * Silicon Graphics Computer Systems, Inc.
41 *
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
49 */
50
f910786b 51/** @file bits/stl_deque.h
729e3d3f 52 * This is an internal header file, included by other library headers.
f910786b 53 * Do not attempt to use it directly. @headername{deque}
725dc051
BK
54 */
55
046d30f4
PC
56#ifndef _STL_DEQUE_H
57#define _STL_DEQUE_H 1
725dc051 58
5cb6369d
PE
59#include <bits/concept_check.h>
60#include <bits/stl_iterator_base_types.h>
61#include <bits/stl_iterator_base_funcs.h>
734f5023 62#if __cplusplus >= 201103L
988499f4 63#include <initializer_list>
ff2e7f19 64#include <bits/stl_uninitialized.h> // for __is_trivially_relocatable
a7d5d7e2 65#endif
725dc051 66
bd2ee798
FD
67#include <debug/assertions.h>
68
12ffa228
BK
69namespace std _GLIBCXX_VISIBILITY(default)
70{
4a15d842 71_GLIBCXX_BEGIN_NAMESPACE_VERSION
12ffa228 72_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
3cbc7af0 73
3971a4d2 74 /**
3971a4d2 75 * @brief This function controls the size of memory nodes.
93c66bc6 76 * @param __size The size of an element.
3971a4d2
PE
77 * @return The number (not byte size) of elements per node.
78 *
2a60a9f6
BK
79 * This function started off as a compiler kludge from SGI, but
80 * seems to be a useful wrapper around a repeated constant
81 * expression. The @b 512 is tunable (and no other code needs to
82 * change), but no investigation has been done since inheriting the
83 * SGI code. Touch _GLIBCXX_DEQUE_BUF_SIZE only if you know what
84 * you are doing, however: changing it breaks the binary
85 * compatibility!!
3971a4d2 86 */
465d76b7
PC
87
88#ifndef _GLIBCXX_DEQUE_BUF_SIZE
89#define _GLIBCXX_DEQUE_BUF_SIZE 512
90#endif
91
fd18c76a 92 _GLIBCXX_CONSTEXPR inline size_t
ed6814f7 93 __deque_buf_size(size_t __size)
465d76b7
PC
94 { return (__size < _GLIBCXX_DEQUE_BUF_SIZE
95 ? size_t(_GLIBCXX_DEQUE_BUF_SIZE / __size) : size_t(1)); }
ed6814f7
BI
96
97
3971a4d2
PE
98 /**
99 * @brief A deque::iterator.
100 *
5a1e5472
BK
101 * Quite a bit of intelligence here. Much of the functionality of
102 * deque is actually passed off to this class. A deque holds two
103 * of these internally, marking its valid range. Access to
104 * elements is done as offsets of either of those two, relying on
105 * operator overloading in this class.
3971a4d2 106 *
3971a4d2 107 * All the functions are op overloads except for _M_set_node.
3971a4d2 108 */
285b36d6 109 template<typename _Tp, typename _Ref, typename _Ptr>
3971a4d2 110 struct _Deque_iterator
f6592a9e 111 {
fd18c76a 112#if __cplusplus < 201103L
fe62dd04 113 typedef _Deque_iterator<_Tp, _Tp&, _Tp*> iterator;
f6592a9e 114 typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
fe62dd04
FD
115 typedef _Tp* _Elt_pointer;
116 typedef _Tp** _Map_pointer;
fd18c76a
JW
117#else
118 private:
119 template<typename _Up>
120 using __ptr_to = typename pointer_traits<_Ptr>::template rebind<_Up>;
121 template<typename _CvTp>
122 using __iter = _Deque_iterator<_Tp, _CvTp&, __ptr_to<_CvTp>>;
123 public:
124 typedef __iter<_Tp> iterator;
125 typedef __iter<const _Tp> const_iterator;
126 typedef __ptr_to<_Tp> _Elt_pointer;
127 typedef __ptr_to<_Elt_pointer> _Map_pointer;
128#endif
f6592a9e 129
d15ac9d9 130 static size_t _S_buffer_size() _GLIBCXX_NOEXCEPT
f6592a9e 131 { return __deque_buf_size(sizeof(_Tp)); }
ed6814f7 132
fe62dd04
FD
133 typedef std::random_access_iterator_tag iterator_category;
134 typedef _Tp value_type;
135 typedef _Ptr pointer;
136 typedef _Ref reference;
137 typedef size_t size_type;
138 typedef ptrdiff_t difference_type;
139 typedef _Deque_iterator _Self;
ed6814f7 140
fd18c76a
JW
141 _Elt_pointer _M_cur;
142 _Elt_pointer _M_first;
143 _Elt_pointer _M_last;
f6592a9e 144 _Map_pointer _M_node;
ed6814f7 145
fd18c76a 146 _Deque_iterator(_Elt_pointer __x, _Map_pointer __y) _GLIBCXX_NOEXCEPT
3971a4d2 147 : _M_cur(__x), _M_first(*__y),
fe62dd04 148 _M_last(*__y + _S_buffer_size()), _M_node(__y) { }
f6592a9e 149
d15ac9d9 150 _Deque_iterator() _GLIBCXX_NOEXCEPT
fd18c76a 151 : _M_cur(), _M_first(), _M_last(), _M_node() { }
f6592a9e 152
f07c2237
JM
153#if __cplusplus < 201103L
154 // Conversion from iterator to const_iterator.
d15ac9d9 155 _Deque_iterator(const iterator& __x) _GLIBCXX_NOEXCEPT
ed6814f7 156 : _M_cur(__x._M_cur), _M_first(__x._M_first),
fe62dd04 157 _M_last(__x._M_last), _M_node(__x._M_node) { }
f07c2237
JM
158#else
159 // Conversion from iterator to const_iterator.
160 template<typename _Iter,
161 typename = _Require<is_same<_Self, const_iterator>,
162 is_same<_Iter, iterator>>>
163 _Deque_iterator(const _Iter& __x) noexcept
164 : _M_cur(__x._M_cur), _M_first(__x._M_first),
165 _M_last(__x._M_last), _M_node(__x._M_node) { }
166
167 _Deque_iterator(const _Deque_iterator&) = default;
168 _Deque_iterator& operator=(const _Deque_iterator&) = default;
169#endif
ed6814f7 170
94938aec 171 iterator
d15ac9d9 172 _M_const_cast() const _GLIBCXX_NOEXCEPT
94938aec
PC
173 { return iterator(_M_cur, _M_node); }
174
f6592a9e 175 reference
d15ac9d9 176 operator*() const _GLIBCXX_NOEXCEPT
f6592a9e
PC
177 { return *_M_cur; }
178
179 pointer
d15ac9d9 180 operator->() const _GLIBCXX_NOEXCEPT
f6592a9e 181 { return _M_cur; }
ed6814f7 182
f6592a9e 183 _Self&
d15ac9d9 184 operator++() _GLIBCXX_NOEXCEPT
f6592a9e
PC
185 {
186 ++_M_cur;
187 if (_M_cur == _M_last)
188 {
189 _M_set_node(_M_node + 1);
190 _M_cur = _M_first;
191 }
ed6814f7 192 return *this;
3971a4d2 193 }
f6592a9e
PC
194
195 _Self
d15ac9d9 196 operator++(int) _GLIBCXX_NOEXCEPT
f6592a9e
PC
197 {
198 _Self __tmp = *this;
199 ++*this;
200 return __tmp;
3971a4d2 201 }
ed6814f7 202
f6592a9e 203 _Self&
d15ac9d9 204 operator--() _GLIBCXX_NOEXCEPT
f6592a9e
PC
205 {
206 if (_M_cur == _M_first)
207 {
208 _M_set_node(_M_node - 1);
209 _M_cur = _M_last;
210 }
211 --_M_cur;
212 return *this;
213 }
ed6814f7 214
f6592a9e 215 _Self
d15ac9d9 216 operator--(int) _GLIBCXX_NOEXCEPT
f6592a9e
PC
217 {
218 _Self __tmp = *this;
219 --*this;
220 return __tmp;
221 }
ed6814f7 222
f6592a9e 223 _Self&
d15ac9d9 224 operator+=(difference_type __n) _GLIBCXX_NOEXCEPT
f6592a9e
PC
225 {
226 const difference_type __offset = __n + (_M_cur - _M_first);
227 if (__offset >= 0 && __offset < difference_type(_S_buffer_size()))
228 _M_cur += __n;
229 else
230 {
231 const difference_type __node_offset =
232 __offset > 0 ? __offset / difference_type(_S_buffer_size())
fe62dd04 233 : -difference_type((-__offset - 1)
f6592a9e
PC
234 / _S_buffer_size()) - 1;
235 _M_set_node(_M_node + __node_offset);
236 _M_cur = _M_first + (__offset - __node_offset
237 * difference_type(_S_buffer_size()));
238 }
239 return *this;
3971a4d2 240 }
ed6814f7 241
f6592a9e 242 _Self
d15ac9d9 243 operator+(difference_type __n) const _GLIBCXX_NOEXCEPT
f6592a9e
PC
244 {
245 _Self __tmp = *this;
246 return __tmp += __n;
247 }
ed6814f7 248
f6592a9e 249 _Self&
d15ac9d9 250 operator-=(difference_type __n) _GLIBCXX_NOEXCEPT
f6592a9e 251 { return *this += -__n; }
ed6814f7 252
f6592a9e 253 _Self
d15ac9d9 254 operator-(difference_type __n) const _GLIBCXX_NOEXCEPT
f6592a9e
PC
255 {
256 _Self __tmp = *this;
257 return __tmp -= __n;
258 }
ed6814f7 259
f6592a9e 260 reference
d15ac9d9 261 operator[](difference_type __n) const _GLIBCXX_NOEXCEPT
f6592a9e 262 { return *(*this + __n); }
ed6814f7 263
fe62dd04 264 /**
5a1e5472
BK
265 * Prepares to traverse new_node. Sets everything except
266 * _M_cur, which should therefore be set by the caller
267 * immediately afterwards, based on _M_first and _M_last.
f6592a9e
PC
268 */
269 void
d15ac9d9 270 _M_set_node(_Map_pointer __new_node) _GLIBCXX_NOEXCEPT
f6592a9e
PC
271 {
272 _M_node = __new_node;
273 _M_first = *__new_node;
274 _M_last = _M_first + difference_type(_S_buffer_size());
275 }
276 };
ed6814f7 277
3971a4d2
PE
278 // Note: we also provide overloads whose operands are of the same type in
279 // order to avoid ambiguous overload resolution when std::rel_ops operators
280 // are in scope (for additional details, see libstdc++/3628)
285b36d6 281 template<typename _Tp, typename _Ref, typename _Ptr>
f6592a9e
PC
282 inline bool
283 operator==(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
d15ac9d9 284 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
f6592a9e 285 { return __x._M_cur == __y._M_cur; }
ed6814f7 286
285b36d6 287 template<typename _Tp, typename _RefL, typename _PtrL,
f6592a9e
PC
288 typename _RefR, typename _PtrR>
289 inline bool
290 operator==(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
d15ac9d9 291 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
f6592a9e 292 { return __x._M_cur == __y._M_cur; }
ed6814f7 293
285b36d6 294 template<typename _Tp, typename _Ref, typename _Ptr>
f6592a9e
PC
295 inline bool
296 operator!=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
d15ac9d9 297 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
f6592a9e 298 { return !(__x == __y); }
ed6814f7 299
285b36d6 300 template<typename _Tp, typename _RefL, typename _PtrL,
f6592a9e
PC
301 typename _RefR, typename _PtrR>
302 inline bool
303 operator!=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
d15ac9d9 304 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
f6592a9e 305 { return !(__x == __y); }
ed6814f7 306
285b36d6 307 template<typename _Tp, typename _Ref, typename _Ptr>
f6592a9e
PC
308 inline bool
309 operator<(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
d15ac9d9 310 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
f6592a9e 311 { return (__x._M_node == __y._M_node) ? (__x._M_cur < __y._M_cur)
fe62dd04 312 : (__x._M_node < __y._M_node); }
ed6814f7 313
285b36d6 314 template<typename _Tp, typename _RefL, typename _PtrL,
f6592a9e
PC
315 typename _RefR, typename _PtrR>
316 inline bool
317 operator<(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
d15ac9d9 318 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
f6592a9e 319 { return (__x._M_node == __y._M_node) ? (__x._M_cur < __y._M_cur)
fe62dd04 320 : (__x._M_node < __y._M_node); }
ed6814f7 321
285b36d6 322 template<typename _Tp, typename _Ref, typename _Ptr>
f6592a9e
PC
323 inline bool
324 operator>(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
d15ac9d9 325 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
f6592a9e 326 { return __y < __x; }
ed6814f7 327
285b36d6 328 template<typename _Tp, typename _RefL, typename _PtrL,
f6592a9e
PC
329 typename _RefR, typename _PtrR>
330 inline bool
331 operator>(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
d15ac9d9 332 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
f6592a9e 333 { return __y < __x; }
ed6814f7 334
285b36d6 335 template<typename _Tp, typename _Ref, typename _Ptr>
f6592a9e
PC
336 inline bool
337 operator<=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
d15ac9d9 338 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
f6592a9e 339 { return !(__y < __x); }
ed6814f7 340
285b36d6 341 template<typename _Tp, typename _RefL, typename _PtrL,
f6592a9e
PC
342 typename _RefR, typename _PtrR>
343 inline bool
344 operator<=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
d15ac9d9 345 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
f6592a9e 346 { return !(__y < __x); }
ed6814f7 347
285b36d6 348 template<typename _Tp, typename _Ref, typename _Ptr>
f6592a9e
PC
349 inline bool
350 operator>=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
d15ac9d9 351 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
f6592a9e 352 { return !(__x < __y); }
ed6814f7 353
285b36d6 354 template<typename _Tp, typename _RefL, typename _PtrL,
f6592a9e
PC
355 typename _RefR, typename _PtrR>
356 inline bool
357 operator>=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
d15ac9d9 358 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
f6592a9e 359 { return !(__x < __y); }
ed6814f7 360
3d7c150e 361 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3971a4d2
PE
362 // According to the resolution of DR179 not only the various comparison
363 // operators but also operator- must accept mixed iterator/const_iterator
364 // parameters.
1b4454bf
PC
365 template<typename _Tp, typename _Ref, typename _Ptr>
366 inline typename _Deque_iterator<_Tp, _Ref, _Ptr>::difference_type
367 operator-(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
d15ac9d9 368 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
1b4454bf
PC
369 {
370 return typename _Deque_iterator<_Tp, _Ref, _Ptr>::difference_type
371 (_Deque_iterator<_Tp, _Ref, _Ptr>::_S_buffer_size())
372 * (__x._M_node - __y._M_node - 1) + (__x._M_cur - __x._M_first)
373 + (__y._M_last - __y._M_cur);
374 }
375
285b36d6 376 template<typename _Tp, typename _RefL, typename _PtrL,
f6592a9e
PC
377 typename _RefR, typename _PtrR>
378 inline typename _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type
379 operator-(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
d15ac9d9 380 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
f6592a9e
PC
381 {
382 return typename _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type
383 (_Deque_iterator<_Tp, _RefL, _PtrL>::_S_buffer_size())
384 * (__x._M_node - __y._M_node - 1) + (__x._M_cur - __x._M_first)
385 + (__y._M_last - __y._M_cur);
386 }
ed6814f7 387
285b36d6 388 template<typename _Tp, typename _Ref, typename _Ptr>
f6592a9e
PC
389 inline _Deque_iterator<_Tp, _Ref, _Ptr>
390 operator+(ptrdiff_t __n, const _Deque_iterator<_Tp, _Ref, _Ptr>& __x)
d15ac9d9 391 _GLIBCXX_NOEXCEPT
f6592a9e 392 { return __x + __n; }
ed6814f7 393
62c7a041
PC
394 template<typename _Tp>
395 void
e2bf2007
PC
396 fill(const _Deque_iterator<_Tp, _Tp&, _Tp*>&,
397 const _Deque_iterator<_Tp, _Tp&, _Tp*>&, const _Tp&);
398
399 template<typename _Tp>
400 _Deque_iterator<_Tp, _Tp&, _Tp*>
401 copy(_Deque_iterator<_Tp, const _Tp&, const _Tp*>,
402 _Deque_iterator<_Tp, const _Tp&, const _Tp*>,
403 _Deque_iterator<_Tp, _Tp&, _Tp*>);
404
405 template<typename _Tp>
406 inline _Deque_iterator<_Tp, _Tp&, _Tp*>
407 copy(_Deque_iterator<_Tp, _Tp&, _Tp*> __first,
408 _Deque_iterator<_Tp, _Tp&, _Tp*> __last,
409 _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
410 { return std::copy(_Deque_iterator<_Tp, const _Tp&, const _Tp*>(__first),
411 _Deque_iterator<_Tp, const _Tp&, const _Tp*>(__last),
412 __result); }
413
0800b8ea
PC
414 template<typename _Tp>
415 _Deque_iterator<_Tp, _Tp&, _Tp*>
416 copy_backward(_Deque_iterator<_Tp, const _Tp&, const _Tp*>,
417 _Deque_iterator<_Tp, const _Tp&, const _Tp*>,
418 _Deque_iterator<_Tp, _Tp&, _Tp*>);
419
420 template<typename _Tp>
421 inline _Deque_iterator<_Tp, _Tp&, _Tp*>
422 copy_backward(_Deque_iterator<_Tp, _Tp&, _Tp*> __first,
423 _Deque_iterator<_Tp, _Tp&, _Tp*> __last,
424 _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
425 { return std::copy_backward(_Deque_iterator<_Tp,
426 const _Tp&, const _Tp*>(__first),
427 _Deque_iterator<_Tp,
428 const _Tp&, const _Tp*>(__last),
429 __result); }
430
734f5023 431#if __cplusplus >= 201103L
e2bf2007
PC
432 template<typename _Tp>
433 _Deque_iterator<_Tp, _Tp&, _Tp*>
434 move(_Deque_iterator<_Tp, const _Tp&, const _Tp*>,
435 _Deque_iterator<_Tp, const _Tp&, const _Tp*>,
436 _Deque_iterator<_Tp, _Tp&, _Tp*>);
437
438 template<typename _Tp>
439 inline _Deque_iterator<_Tp, _Tp&, _Tp*>
440 move(_Deque_iterator<_Tp, _Tp&, _Tp*> __first,
441 _Deque_iterator<_Tp, _Tp&, _Tp*> __last,
442 _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
443 { return std::move(_Deque_iterator<_Tp, const _Tp&, const _Tp*>(__first),
444 _Deque_iterator<_Tp, const _Tp&, const _Tp*>(__last),
445 __result); }
0800b8ea
PC
446
447 template<typename _Tp>
448 _Deque_iterator<_Tp, _Tp&, _Tp*>
449 move_backward(_Deque_iterator<_Tp, const _Tp&, const _Tp*>,
450 _Deque_iterator<_Tp, const _Tp&, const _Tp*>,
451 _Deque_iterator<_Tp, _Tp&, _Tp*>);
452
453 template<typename _Tp>
454 inline _Deque_iterator<_Tp, _Tp&, _Tp*>
455 move_backward(_Deque_iterator<_Tp, _Tp&, _Tp*> __first,
456 _Deque_iterator<_Tp, _Tp&, _Tp*> __last,
457 _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
458 { return std::move_backward(_Deque_iterator<_Tp,
459 const _Tp&, const _Tp*>(__first),
460 _Deque_iterator<_Tp,
461 const _Tp&, const _Tp*>(__last),
462 __result); }
e2bf2007 463#endif
62c7a041 464
5cb6369d 465 /**
8a1d8dd9
MA
466 * Deque base class. This class provides the unified face for %deque's
467 * allocation. This class's constructor and destructor allocate and
468 * deallocate (but do not initialize) storage. This makes %exception
469 * safety easier.
470 *
471 * Nothing in this class ever constructs or destroys an actual Tp element.
472 * (Deque handles that itself.) Only/All memory management is performed
473 * here.
5cb6369d 474 */
285b36d6 475 template<typename _Tp, typename _Alloc>
8a1d8dd9 476 class _Deque_base
f6592a9e 477 {
fd18c76a
JW
478 protected:
479 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
480 rebind<_Tp>::other _Tp_alloc_type;
481 typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Alloc_traits;
482
483#if __cplusplus < 201103L
484 typedef _Tp* _Ptr;
485 typedef const _Tp* _Ptr_const;
486#else
487 typedef typename _Alloc_traits::pointer _Ptr;
488 typedef typename _Alloc_traits::const_pointer _Ptr_const;
489#endif
490
491 typedef typename _Alloc_traits::template rebind<_Ptr>::other
492 _Map_alloc_type;
493 typedef __gnu_cxx::__alloc_traits<_Map_alloc_type> _Map_alloc_traits;
494
f6592a9e 495 public:
fe62dd04 496 typedef _Alloc allocator_type;
f6592a9e
PC
497
498 allocator_type
d3677132 499 get_allocator() const _GLIBCXX_NOEXCEPT
31905f34 500 { return allocator_type(_M_get_Tp_allocator()); }
8a1d8dd9 501
fe62dd04 502 typedef _Deque_iterator<_Tp, _Tp&, _Ptr> iterator;
fd18c76a 503 typedef _Deque_iterator<_Tp, const _Tp&, _Ptr_const> const_iterator;
ed6814f7 504
78b36b70
PC
505 _Deque_base()
506 : _M_impl()
507 { _M_initialize_map(0); }
508
dc2cf706
PC
509 _Deque_base(size_t __num_elements)
510 : _M_impl()
511 { _M_initialize_map(__num_elements); }
512
f6592a9e 513 _Deque_base(const allocator_type& __a, size_t __num_elements)
0d8c9baf 514 : _M_impl(__a)
8a1d8dd9 515 { _M_initialize_map(__num_elements); }
8a1d8dd9 516
ed6814f7 517 _Deque_base(const allocator_type& __a)
0d8c9baf 518 : _M_impl(__a)
fd18c76a 519 { /* Caller must initialize map. */ }
f6592a9e 520
734f5023 521#if __cplusplus >= 201103L
2abd4ee6
JW
522 _Deque_base(_Deque_base&& __x, false_type)
523 : _M_impl(__x._M_move_impl())
524 { }
525
526 _Deque_base(_Deque_base&& __x, true_type)
527 : _M_impl(std::move(__x._M_get_Tp_allocator()))
053cc380 528 {
d3cf4189 529 _M_initialize_map(0);
053cc380 530 if (__x._M_impl._M_map)
d3cf4189 531 this->_M_impl._M_swap_data(__x._M_impl);
fd18c76a
JW
532 }
533
2abd4ee6 534 _Deque_base(_Deque_base&& __x)
a2b5fdcb 535 : _Deque_base(std::move(__x), typename _Alloc_traits::is_always_equal{})
2abd4ee6
JW
536 { }
537
af55b3af 538 _Deque_base(_Deque_base&& __x, const allocator_type& __a, size_t __n)
fd18c76a
JW
539 : _M_impl(__a)
540 {
541 if (__x.get_allocator() == __a)
542 {
543 if (__x._M_impl._M_map)
544 {
545 _M_initialize_map(0);
546 this->_M_impl._M_swap_data(__x._M_impl);
547 }
548 }
549 else
550 {
551 _M_initialize_map(__n);
053cc380
PC
552 }
553 }
554#endif
555
d15ac9d9 556 ~_Deque_base() _GLIBCXX_NOEXCEPT;
f6592a9e
PC
557
558 protected:
fd18c76a 559 typedef typename iterator::_Map_pointer _Map_pointer;
4fd20a8f 560
af0e0fa0
JW
561 //This struct encapsulates the implementation of the std::deque
562 //standard container and at the same time makes use of the EBO
563 //for empty allocators.
03f9ea44 564 struct _Deque_impl
4fd20a8f 565 : public _Tp_alloc_type
0d8c9baf 566 {
fd18c76a 567 _Map_pointer _M_map;
03f9ea44
DM
568 size_t _M_map_size;
569 iterator _M_start;
570 iterator _M_finish;
571
78b36b70 572 _Deque_impl()
fd18c76a 573 : _Tp_alloc_type(), _M_map(), _M_map_size(0),
78b36b70
PC
574 _M_start(), _M_finish()
575 { }
576
d15ac9d9 577 _Deque_impl(const _Tp_alloc_type& __a) _GLIBCXX_NOEXCEPT
fd18c76a 578 : _Tp_alloc_type(__a), _M_map(), _M_map_size(0),
4fd20a8f 579 _M_start(), _M_finish()
03f9ea44 580 { }
6f59ea25 581
734f5023 582#if __cplusplus >= 201103L
2abd4ee6
JW
583 _Deque_impl(_Deque_impl&&) = default;
584
585 _Deque_impl(_Tp_alloc_type&& __a) noexcept
fd18c76a 586 : _Tp_alloc_type(std::move(__a)), _M_map(), _M_map_size(0),
6f59ea25
PC
587 _M_start(), _M_finish()
588 { }
589#endif
fd18c76a 590
2abd4ee6 591 void _M_swap_data(_Deque_impl& __x) _GLIBCXX_NOEXCEPT
fd18c76a 592 {
2abd4ee6
JW
593 using std::swap;
594 swap(this->_M_start, __x._M_start);
595 swap(this->_M_finish, __x._M_finish);
596 swap(this->_M_map, __x._M_map);
597 swap(this->_M_map_size, __x._M_map_size);
fd18c76a 598 }
03f9ea44
DM
599 };
600
8d46ce60 601 _Tp_alloc_type&
d3677132 602 _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT
8d46ce60
PC
603 { return *static_cast<_Tp_alloc_type*>(&this->_M_impl); }
604
605 const _Tp_alloc_type&
d3677132 606 _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
4fd20a8f
PC
607 { return *static_cast<const _Tp_alloc_type*>(&this->_M_impl); }
608
609 _Map_alloc_type
d3677132 610 _M_get_map_allocator() const _GLIBCXX_NOEXCEPT
31905f34 611 { return _Map_alloc_type(_M_get_Tp_allocator()); }
8a1d8dd9 612
fd18c76a 613 _Ptr
f6592a9e 614 _M_allocate_node()
fe62dd04 615 {
fd18c76a
JW
616 typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Traits;
617 return _Traits::allocate(_M_impl, __deque_buf_size(sizeof(_Tp)));
4fd20a8f 618 }
ed6814f7 619
f6592a9e 620 void
fd18c76a 621 _M_deallocate_node(_Ptr __p) _GLIBCXX_NOEXCEPT
4fd20a8f 622 {
fd18c76a
JW
623 typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Traits;
624 _Traits::deallocate(_M_impl, __p, __deque_buf_size(sizeof(_Tp)));
4fd20a8f 625 }
ed6814f7 626
fd18c76a 627 _Map_pointer
f6592a9e 628 _M_allocate_map(size_t __n)
fd18c76a
JW
629 {
630 _Map_alloc_type __map_alloc = _M_get_map_allocator();
631 return _Map_alloc_traits::allocate(__map_alloc, __n);
632 }
ed6814f7 633
f6592a9e 634 void
fd18c76a
JW
635 _M_deallocate_map(_Map_pointer __p, size_t __n) _GLIBCXX_NOEXCEPT
636 {
637 _Map_alloc_type __map_alloc = _M_get_map_allocator();
638 _Map_alloc_traits::deallocate(__map_alloc, __p, __n);
639 }
ed6814f7 640
f6592a9e
PC
641 protected:
642 void _M_initialize_map(size_t);
fd18c76a
JW
643 void _M_create_nodes(_Map_pointer __nstart, _Map_pointer __nfinish);
644 void _M_destroy_nodes(_Map_pointer __nstart,
645 _Map_pointer __nfinish) _GLIBCXX_NOEXCEPT;
f6592a9e 646 enum { _S_initial_map_size = 8 };
ed6814f7 647
03f9ea44 648 _Deque_impl _M_impl;
2abd4ee6
JW
649
650#if __cplusplus >= 201103L
651 private:
652 _Deque_impl
653 _M_move_impl()
654 {
655 if (!_M_impl._M_map)
656 return std::move(_M_impl);
657
658 // Create a copy of the current allocator.
659 _Tp_alloc_type __alloc{_M_get_Tp_allocator()};
660 // Put that copy in a moved-from state.
e9c51193 661 _Tp_alloc_type __sink __attribute((__unused__)) {std::move(__alloc)};
2abd4ee6
JW
662 // Create an empty map that allocates using the moved-from allocator.
663 _Deque_base __empty{__alloc};
9bc50282 664 __empty._M_initialize_map(0);
2abd4ee6
JW
665 // Now safe to modify current allocator and perform non-throwing swaps.
666 _Deque_impl __ret{std::move(_M_get_Tp_allocator())};
667 _M_impl._M_swap_data(__ret);
668 _M_impl._M_swap_data(__empty._M_impl);
669 return __ret;
670 }
671#endif
f6592a9e 672 };
ed6814f7 673
285b36d6 674 template<typename _Tp, typename _Alloc>
43da93a7 675 _Deque_base<_Tp, _Alloc>::
d15ac9d9 676 ~_Deque_base() _GLIBCXX_NOEXCEPT
3971a4d2 677 {
43da93a7
PC
678 if (this->_M_impl._M_map)
679 {
680 _M_destroy_nodes(this->_M_impl._M_start._M_node,
681 this->_M_impl._M_finish._M_node + 1);
682 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
683 }
725dc051 684 }
ed6814f7 685
5cb6369d 686 /**
3971a4d2 687 * @brief Layout storage.
93c66bc6 688 * @param __num_elements The count of T's for which to allocate space
fd18c76a 689 * at first.
3971a4d2 690 * @return Nothing.
5cb6369d 691 *
3971a4d2 692 * The initial underlying memory layout is a bit complicated...
5cb6369d 693 */
285b36d6 694 template<typename _Tp, typename _Alloc>
f6592a9e 695 void
43da93a7
PC
696 _Deque_base<_Tp, _Alloc>::
697 _M_initialize_map(size_t __num_elements)
f6592a9e 698 {
5a1e5472 699 const size_t __num_nodes = (__num_elements/ __deque_buf_size(sizeof(_Tp))
43da93a7 700 + 1);
ed6814f7 701
03f9ea44 702 this->_M_impl._M_map_size = std::max((size_t) _S_initial_map_size,
43da93a7 703 size_t(__num_nodes + 2));
03f9ea44 704 this->_M_impl._M_map = _M_allocate_map(this->_M_impl._M_map_size);
ed6814f7 705
f6592a9e
PC
706 // For "small" maps (needing less than _M_map_size nodes), allocation
707 // starts in the middle elements and grows outwards. So nstart may be
708 // the beginning of _M_map, but for small maps it may be as far in as
709 // _M_map+3.
ed6814f7 710
fd18c76a
JW
711 _Map_pointer __nstart = (this->_M_impl._M_map
712 + (this->_M_impl._M_map_size - __num_nodes) / 2);
713 _Map_pointer __nfinish = __nstart + __num_nodes;
ed6814f7 714
bc2631e0 715 __try
f6592a9e 716 { _M_create_nodes(__nstart, __nfinish); }
bc2631e0 717 __catch(...)
f6592a9e 718 {
03f9ea44 719 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
fd18c76a 720 this->_M_impl._M_map = _Map_pointer();
03f9ea44 721 this->_M_impl._M_map_size = 0;
f6592a9e
PC
722 __throw_exception_again;
723 }
ed6814f7 724
03f9ea44
DM
725 this->_M_impl._M_start._M_set_node(__nstart);
726 this->_M_impl._M_finish._M_set_node(__nfinish - 1);
727 this->_M_impl._M_start._M_cur = _M_impl._M_start._M_first;
0d8c9baf
PC
728 this->_M_impl._M_finish._M_cur = (this->_M_impl._M_finish._M_first
729 + __num_elements
730 % __deque_buf_size(sizeof(_Tp)));
f6592a9e 731 }
ed6814f7 732
285b36d6 733 template<typename _Tp, typename _Alloc>
f6592a9e 734 void
43da93a7 735 _Deque_base<_Tp, _Alloc>::
fd18c76a 736 _M_create_nodes(_Map_pointer __nstart, _Map_pointer __nfinish)
f6592a9e 737 {
fd18c76a 738 _Map_pointer __cur;
bc2631e0 739 __try
f6592a9e
PC
740 {
741 for (__cur = __nstart; __cur < __nfinish; ++__cur)
742 *__cur = this->_M_allocate_node();
743 }
bc2631e0 744 __catch(...)
ed6814f7 745 {
f6592a9e 746 _M_destroy_nodes(__nstart, __cur);
ed6814f7 747 __throw_exception_again;
f6592a9e
PC
748 }
749 }
ed6814f7 750
285b36d6 751 template<typename _Tp, typename _Alloc>
f6592a9e 752 void
43da93a7 753 _Deque_base<_Tp, _Alloc>::
fd18c76a
JW
754 _M_destroy_nodes(_Map_pointer __nstart,
755 _Map_pointer __nfinish) _GLIBCXX_NOEXCEPT
f6592a9e 756 {
fd18c76a 757 for (_Map_pointer __n = __nstart; __n < __nfinish; ++__n)
f6592a9e
PC
758 _M_deallocate_node(*__n);
759 }
ed6814f7 760
5cb6369d 761 /**
3971a4d2
PE
762 * @brief A standard container using fixed-size memory allocation and
763 * constant-time manipulation of elements at either end.
5cb6369d 764 *
aac2878e 765 * @ingroup sequences
5cb6369d 766 *
d632488a
BK
767 * @tparam _Tp Type of element.
768 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
769 *
3971a4d2
PE
770 * Meets the requirements of a <a href="tables.html#65">container</a>, a
771 * <a href="tables.html#66">reversible container</a>, and a
772 * <a href="tables.html#67">sequence</a>, including the
773 * <a href="tables.html#68">optional sequence requirements</a>.
5cb6369d 774 *
3971a4d2
PE
775 * In previous HP/SGI versions of deque, there was an extra template
776 * parameter so users could control the node size. This extension turned
777 * out to violate the C++ standard (it can be detected using template
778 * template parameters), and it was removed.
5cb6369d 779 *
3971a4d2 780 * Here's how a deque<Tp> manages memory. Each deque has 4 members:
ed6814f7 781 *
3971a4d2
PE
782 * - Tp** _M_map
783 * - size_t _M_map_size
784 * - iterator _M_start, _M_finish
ed6814f7 785 *
5a1e5472 786 * map_size is at least 8. %map is an array of map_size
93c66bc6 787 * pointers-to-@a nodes. (The name %map has nothing to do with the
2a60a9f6
BK
788 * std::map class, and @b nodes should not be confused with
789 * std::list's usage of @a node.)
ed6814f7 790 *
2a60a9f6
BK
791 * A @a node has no specific type name as such, but it is referred
792 * to as @a node in this file. It is a simple array-of-Tp. If Tp
5a1e5472 793 * is very large, there will be one Tp element per node (i.e., an
2a60a9f6 794 * @a array of one). For non-huge Tp's, node size is inversely
5a1e5472
BK
795 * related to Tp size: the larger the Tp, the fewer Tp's will fit
796 * in a node. The goal here is to keep the total size of a node
797 * relatively small and constant over different Tp's, to improve
798 * allocator efficiency.
ed6814f7 799 *
5a1e5472
BK
800 * Not every pointer in the %map array will point to a node. If
801 * the initial number of elements in the deque is small, the
802 * /middle/ %map pointers will be valid, and the ones at the edges
803 * will be unused. This same situation will arise as the %map
804 * grows: available %map pointers, if any, will be on the ends. As
805 * new nodes are created, only a subset of the %map's pointers need
2a60a9f6 806 * to be copied @a outward.
5cb6369d 807 *
3971a4d2
PE
808 * Class invariants:
809 * - For any nonsingular iterator i:
810 * - i.node points to a member of the %map array. (Yes, you read that
811 * correctly: i.node does not actually point to a node.) The member of
812 * the %map array is what actually points to the node.
813 * - i.first == *(i.node) (This points to the node (first Tp element).)
814 * - i.last == i.first + node_size
815 * - i.cur is a pointer in the range [i.first, i.last). NOTE:
816 * the implication of this is that i.cur is always a dereferenceable
817 * pointer, even if i is a past-the-end iterator.
5a1e5472
BK
818 * - Start and Finish are always nonsingular iterators. NOTE: this
819 * means that an empty deque must have one node, a deque with <N
820 * elements (where N is the node buffer size) must have one node, a
821 * deque with N through (2N-1) elements must have two nodes, etc.
822 * - For every node other than start.node and finish.node, every
823 * element in the node is an initialized object. If start.node ==
824 * finish.node, then [start.cur, finish.cur) are initialized
825 * objects, and the elements outside that range are uninitialized
826 * storage. Otherwise, [start.cur, start.last) and [finish.first,
827 * finish.cur) are initialized objects, and [start.first, start.cur)
828 * and [finish.cur, finish.last) are uninitialized storage.
ed6814f7
BI
829 * - [%map, %map + map_size) is a valid, non-empty range.
830 * - [start.node, finish.node] is a valid range contained within
831 * [%map, %map + map_size).
3971a4d2
PE
832 * - A pointer in the range [%map, %map + map_size) points to an allocated
833 * node if and only if the pointer is in the range
834 * [start.node, finish.node].
5cb6369d 835 *
2a60a9f6 836 * Here's the magic: nothing in deque is @b aware of the discontiguous
3971a4d2
PE
837 * storage!
838 *
839 * The memory setup and layout occurs in the parent, _Base, and the iterator
2a60a9f6 840 * class is entirely responsible for @a leaping from one node to the next.
3971a4d2
PE
841 * All the implementation routines for deque itself work only through the
842 * start and finish iterators. This keeps the routines simple and sane,
843 * and we can use other standard algorithms as well.
5cb6369d 844 */
6323b34e 845 template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
3971a4d2 846 class deque : protected _Deque_base<_Tp, _Alloc>
f6592a9e 847 {
fe62dd04 848#ifdef _GLIBCXX_CONCEPT_CHECKS
f6592a9e 849 // concept requirements
fe62dd04
FD
850 typedef typename _Alloc::value_type _Alloc_value_type;
851# if __cplusplus < 201103L
f6592a9e 852 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
fe62dd04 853# endif
4fd20a8f 854 __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
fe62dd04 855#endif
ed6814f7 856
866e4d38
JW
857#if __cplusplus >= 201103L
858 static_assert(is_same<typename remove_cv<_Tp>::type, _Tp>::value,
859 "std::deque must have a non-const, non-volatile value_type");
860# ifdef __STRICT_ANSI__
861 static_assert(is_same<typename _Alloc::value_type, _Tp>::value,
862 "std::deque must have the same value_type as its allocator");
863# endif
864#endif
865
fd18c76a
JW
866 typedef _Deque_base<_Tp, _Alloc> _Base;
867 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
868 typedef typename _Base::_Alloc_traits _Alloc_traits;
869 typedef typename _Base::_Map_pointer _Map_pointer;
ed6814f7 870
f6592a9e 871 public:
fe62dd04
FD
872 typedef _Tp value_type;
873 typedef typename _Alloc_traits::pointer pointer;
874 typedef typename _Alloc_traits::const_pointer const_pointer;
875 typedef typename _Alloc_traits::reference reference;
876 typedef typename _Alloc_traits::const_reference const_reference;
877 typedef typename _Base::iterator iterator;
878 typedef typename _Base::const_iterator const_iterator;
879 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
880 typedef std::reverse_iterator<iterator> reverse_iterator;
881 typedef size_t size_type;
882 typedef ptrdiff_t difference_type;
883 typedef _Alloc allocator_type;
ed6814f7 884
f6592a9e 885 protected:
d15ac9d9 886 static size_t _S_buffer_size() _GLIBCXX_NOEXCEPT
f6592a9e 887 { return __deque_buf_size(sizeof(_Tp)); }
ed6814f7 888
f6592a9e
PC
889 // Functions controlling memory layout, and nothing else.
890 using _Base::_M_initialize_map;
891 using _Base::_M_create_nodes;
892 using _Base::_M_destroy_nodes;
893 using _Base::_M_allocate_node;
894 using _Base::_M_deallocate_node;
895 using _Base::_M_allocate_map;
896 using _Base::_M_deallocate_map;
4fd20a8f 897 using _Base::_M_get_Tp_allocator;
ed6814f7 898
fe62dd04 899 /**
28dac70a 900 * A total of four data members accumulated down the hierarchy.
03f9ea44 901 * May be accessed via _M_impl.*
f6592a9e 902 */
03f9ea44 903 using _Base::_M_impl;
ed6814f7 904
f6592a9e
PC
905 public:
906 // [23.2.1.1] construct/copy/destroy
907 // (assign() and get_allocator() are also listed in this section)
c3cdd71f
JW
908
909 /**
910 * @brief Creates a %deque with no elements.
911 */
912 deque() : _Base() { }
913
78b36b70
PC
914 /**
915 * @brief Creates a %deque with no elements.
93c66bc6 916 * @param __a An allocator object.
78b36b70 917 */
f6592a9e 918 explicit
c3cdd71f 919 deque(const allocator_type& __a)
6aba6941 920 : _Base(__a, 0) { }
ed6814f7 921
734f5023 922#if __cplusplus >= 201103L
dc2cf706
PC
923 /**
924 * @brief Creates a %deque with default constructed elements.
93c66bc6 925 * @param __n The number of elements to initially create.
6b4f8906 926 * @param __a An allocator.
dc2cf706
PC
927 *
928 * This constructor fills the %deque with @a n default
929 * constructed elements.
930 */
931 explicit
fd18c76a 932 deque(size_type __n, const allocator_type& __a = allocator_type())
af55b3af 933 : _Base(__a, _S_check_init_len(__n, __a))
dc2cf706
PC
934 { _M_default_initialize(); }
935
936 /**
937 * @brief Creates a %deque with copies of an exemplar element.
93c66bc6
BK
938 * @param __n The number of elements to initially create.
939 * @param __value An element to copy.
940 * @param __a An allocator.
dc2cf706 941 *
93c66bc6 942 * This constructor fills the %deque with @a __n copies of @a __value.
dc2cf706
PC
943 */
944 deque(size_type __n, const value_type& __value,
945 const allocator_type& __a = allocator_type())
af55b3af 946 : _Base(__a, _S_check_init_len(__n, __a))
dc2cf706
PC
947 { _M_fill_initialize(__value); }
948#else
f6592a9e 949 /**
78b36b70 950 * @brief Creates a %deque with copies of an exemplar element.
93c66bc6
BK
951 * @param __n The number of elements to initially create.
952 * @param __value An element to copy.
953 * @param __a An allocator.
ed6814f7 954 *
93c66bc6 955 * This constructor fills the %deque with @a __n copies of @a __value.
f6592a9e 956 */
2fecaef4
PC
957 explicit
958 deque(size_type __n, const value_type& __value = value_type(),
f6592a9e 959 const allocator_type& __a = allocator_type())
af55b3af 960 : _Base(__a, _S_check_init_len(__n, __a))
3971a4d2 961 { _M_fill_initialize(__value); }
dc2cf706 962#endif
ed6814f7 963
f6592a9e
PC
964 /**
965 * @brief %Deque copy constructor.
93c66bc6 966 * @param __x A %deque of identical element and allocator types.
ed6814f7 967 *
0a2bf188
JW
968 * The newly-created %deque uses a copy of the allocator object used
969 * by @a __x (unless the allocator traits dictate a different object).
f6592a9e
PC
970 */
971 deque(const deque& __x)
fd18c76a
JW
972 : _Base(_Alloc_traits::_S_select_on_copy(__x._M_get_Tp_allocator()),
973 __x.size())
fe62dd04 974 { std::__uninitialized_copy_a(__x.begin(), __x.end(),
5a1e5472 975 this->_M_impl._M_start,
4fd20a8f 976 _M_get_Tp_allocator()); }
ed6814f7 977
734f5023 978#if __cplusplus >= 201103L
78b36b70
PC
979 /**
980 * @brief %Deque move constructor.
93c66bc6 981 * @param __x A %deque of identical element and allocator types.
78b36b70 982 *
93c66bc6
BK
983 * The newly-created %deque contains the exact contents of @a __x.
984 * The contents of @a __x are a valid, but unspecified %deque.
78b36b70 985 */
5f1fd346
PC
986 deque(deque&& __x)
987 : _Base(std::move(__x)) { }
988499f4 988
fd18c76a
JW
989 /// Copy constructor with alternative allocator
990 deque(const deque& __x, const allocator_type& __a)
991 : _Base(__a, __x.size())
992 { std::__uninitialized_copy_a(__x.begin(), __x.end(),
993 this->_M_impl._M_start,
994 _M_get_Tp_allocator()); }
995
996 /// Move constructor with alternative allocator
997 deque(deque&& __x, const allocator_type& __a)
998 : _Base(std::move(__x), __a, __x.size())
999 {
1000 if (__x.get_allocator() != __a)
1001 {
1002 std::__uninitialized_move_a(__x.begin(), __x.end(),
1003 this->_M_impl._M_start,
1004 _M_get_Tp_allocator());
1005 __x.clear();
1006 }
1007 }
1008
988499f4
JM
1009 /**
1010 * @brief Builds a %deque from an initializer list.
93c66bc6
BK
1011 * @param __l An initializer_list.
1012 * @param __a An allocator object.
988499f4
JM
1013 *
1014 * Create a %deque consisting of copies of the elements in the
93c66bc6 1015 * initializer_list @a __l.
988499f4
JM
1016 *
1017 * This will call the element type's copy constructor N times
93c66bc6 1018 * (where N is __l.size()) and do no memory reallocation.
988499f4
JM
1019 */
1020 deque(initializer_list<value_type> __l,
1021 const allocator_type& __a = allocator_type())
dc2cf706
PC
1022 : _Base(__a)
1023 {
1024 _M_range_initialize(__l.begin(), __l.end(),
1025 random_access_iterator_tag());
1026 }
78b36b70
PC
1027#endif
1028
f6592a9e
PC
1029 /**
1030 * @brief Builds a %deque from a range.
93c66bc6
BK
1031 * @param __first An input iterator.
1032 * @param __last An input iterator.
1033 * @param __a An allocator object.
ed6814f7 1034 *
93c66bc6
BK
1035 * Create a %deque consisting of copies of the elements from [__first,
1036 * __last).
f6592a9e
PC
1037 *
1038 * If the iterators are forward, bidirectional, or random-access, then
1039 * this will call the elements' copy constructor N times (where N is
93c66bc6 1040 * distance(__first,__last)) and do no memory reallocation. But if only
f6592a9e
PC
1041 * input iterators are used, then this will do at most 2N calls to the
1042 * copy constructor, and logN memory reallocations.
1043 */
734f5023 1044#if __cplusplus >= 201103L
2203cb90
PC
1045 template<typename _InputIterator,
1046 typename = std::_RequireInputIter<_InputIterator>>
fe62dd04 1047 deque(_InputIterator __first, _InputIterator __last,
2203cb90
PC
1048 const allocator_type& __a = allocator_type())
1049 : _Base(__a)
fe62dd04 1050 { _M_initialize_dispatch(__first, __last, __false_type()); }
2203cb90 1051#else
f6592a9e 1052 template<typename _InputIterator>
fe62dd04 1053 deque(_InputIterator __first, _InputIterator __last,
f6592a9e
PC
1054 const allocator_type& __a = allocator_type())
1055 : _Base(__a)
fe62dd04 1056 {
f6592a9e 1057 // Check whether it's an integral type. If so, it's not an iterator.
c0736a9d 1058 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
f6592a9e
PC
1059 _M_initialize_dispatch(__first, __last, _Integral());
1060 }
2203cb90 1061#endif
ed6814f7 1062
f6592a9e
PC
1063 /**
1064 * The dtor only erases the elements, and note that if the elements
1065 * themselves are pointers, the pointed-to memory is not touched in any
28dac70a 1066 * way. Managing the pointer is the user's responsibility.
f6592a9e 1067 */
fd18c76a 1068 ~deque()
d2cc7f92 1069 { _M_destroy_data(begin(), end(), _M_get_Tp_allocator()); }
ed6814f7 1070
f6592a9e
PC
1071 /**
1072 * @brief %Deque assignment operator.
93c66bc6 1073 * @param __x A %deque of identical element and allocator types.
ed6814f7 1074 *
0a2bf188
JW
1075 * All the elements of @a x are copied.
1076 *
1077 * The newly-created %deque uses a copy of the allocator object used
1078 * by @a __x (unless the allocator traits dictate a different object).
f6592a9e
PC
1079 */
1080 deque&
1081 operator=(const deque& __x);
ed6814f7 1082
734f5023 1083#if __cplusplus >= 201103L
78b36b70
PC
1084 /**
1085 * @brief %Deque move assignment operator.
93c66bc6 1086 * @param __x A %deque of identical element and allocator types.
78b36b70 1087 *
fd18c76a
JW
1088 * The contents of @a __x are moved into this deque (without copying,
1089 * if the allocators permit it).
93c66bc6 1090 * @a __x is a valid, but unspecified %deque.
78b36b70
PC
1091 */
1092 deque&
fd18c76a 1093 operator=(deque&& __x) noexcept(_Alloc_traits::_S_always_equal())
78b36b70 1094 {
a2b5fdcb
JW
1095 using __always_equal = typename _Alloc_traits::is_always_equal;
1096 _M_move_assign1(std::move(__x), __always_equal{});
78b36b70
PC
1097 return *this;
1098 }
988499f4
JM
1099
1100 /**
1101 * @brief Assigns an initializer list to a %deque.
93c66bc6 1102 * @param __l An initializer_list.
988499f4
JM
1103 *
1104 * This function fills a %deque with copies of the elements in the
93c66bc6 1105 * initializer_list @a __l.
988499f4
JM
1106 *
1107 * Note that the assignment completely changes the %deque and that the
1108 * resulting %deque's size is the same as the number of elements
0a2bf188 1109 * assigned.
988499f4
JM
1110 */
1111 deque&
1112 operator=(initializer_list<value_type> __l)
1113 {
d7e16fc5
FD
1114 _M_assign_aux(__l.begin(), __l.end(),
1115 random_access_iterator_tag());
988499f4
JM
1116 return *this;
1117 }
78b36b70
PC
1118#endif
1119
f6592a9e
PC
1120 /**
1121 * @brief Assigns a given value to a %deque.
93c66bc6
BK
1122 * @param __n Number of elements to be assigned.
1123 * @param __val Value to be assigned.
f6592a9e 1124 *
5a1e5472
BK
1125 * This function fills a %deque with @a n copies of the given
1126 * value. Note that the assignment completely changes the
1127 * %deque and that the resulting %deque's size is the same as
0a2bf188 1128 * the number of elements assigned.
f6592a9e
PC
1129 */
1130 void
1131 assign(size_type __n, const value_type& __val)
1132 { _M_fill_assign(__n, __val); }
ed6814f7 1133
f6592a9e
PC
1134 /**
1135 * @brief Assigns a range to a %deque.
93c66bc6
BK
1136 * @param __first An input iterator.
1137 * @param __last An input iterator.
f6592a9e
PC
1138 *
1139 * This function fills a %deque with copies of the elements in the
93c66bc6 1140 * range [__first,__last).
f6592a9e
PC
1141 *
1142 * Note that the assignment completely changes the %deque and that the
1143 * resulting %deque's size is the same as the number of elements
0a2bf188 1144 * assigned.
f6592a9e 1145 */
734f5023 1146#if __cplusplus >= 201103L
2203cb90
PC
1147 template<typename _InputIterator,
1148 typename = std::_RequireInputIter<_InputIterator>>
fe62dd04
FD
1149 void
1150 assign(_InputIterator __first, _InputIterator __last)
1151 { _M_assign_dispatch(__first, __last, __false_type()); }
2203cb90 1152#else
f6592a9e 1153 template<typename _InputIterator>
fe62dd04
FD
1154 void
1155 assign(_InputIterator __first, _InputIterator __last)
1156 {
c0736a9d 1157 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
f6592a9e
PC
1158 _M_assign_dispatch(__first, __last, _Integral());
1159 }
2203cb90 1160#endif
ed6814f7 1161
734f5023 1162#if __cplusplus >= 201103L
988499f4
JM
1163 /**
1164 * @brief Assigns an initializer list to a %deque.
93c66bc6 1165 * @param __l An initializer_list.
988499f4
JM
1166 *
1167 * This function fills a %deque with copies of the elements in the
93c66bc6 1168 * initializer_list @a __l.
988499f4
JM
1169 *
1170 * Note that the assignment completely changes the %deque and that the
1171 * resulting %deque's size is the same as the number of elements
0a2bf188 1172 * assigned.
988499f4
JM
1173 */
1174 void
1175 assign(initializer_list<value_type> __l)
d7e16fc5 1176 { _M_assign_aux(__l.begin(), __l.end(), random_access_iterator_tag()); }
988499f4
JM
1177#endif
1178
f6592a9e
PC
1179 /// Get a copy of the memory allocation object.
1180 allocator_type
d3677132 1181 get_allocator() const _GLIBCXX_NOEXCEPT
f6592a9e 1182 { return _Base::get_allocator(); }
ed6814f7 1183
f6592a9e
PC
1184 // iterators
1185 /**
1186 * Returns a read/write iterator that points to the first element in the
1187 * %deque. Iteration is done in ordinary element order.
1188 */
1189 iterator
d3677132 1190 begin() _GLIBCXX_NOEXCEPT
03f9ea44 1191 { return this->_M_impl._M_start; }
ed6814f7 1192
f6592a9e
PC
1193 /**
1194 * Returns a read-only (constant) iterator that points to the first
1195 * element in the %deque. Iteration is done in ordinary element order.
1196 */
1197 const_iterator
d3677132 1198 begin() const _GLIBCXX_NOEXCEPT
03f9ea44 1199 { return this->_M_impl._M_start; }
ed6814f7 1200
f6592a9e 1201 /**
5a1e5472
BK
1202 * Returns a read/write iterator that points one past the last
1203 * element in the %deque. Iteration is done in ordinary
1204 * element order.
f6592a9e
PC
1205 */
1206 iterator
d3677132 1207 end() _GLIBCXX_NOEXCEPT
03f9ea44 1208 { return this->_M_impl._M_finish; }
ed6814f7 1209
f6592a9e 1210 /**
5a1e5472
BK
1211 * Returns a read-only (constant) iterator that points one past
1212 * the last element in the %deque. Iteration is done in
1213 * ordinary element order.
f6592a9e
PC
1214 */
1215 const_iterator
d3677132 1216 end() const _GLIBCXX_NOEXCEPT
03f9ea44 1217 { return this->_M_impl._M_finish; }
ed6814f7 1218
f6592a9e 1219 /**
5a1e5472
BK
1220 * Returns a read/write reverse iterator that points to the
1221 * last element in the %deque. Iteration is done in reverse
1222 * element order.
f6592a9e
PC
1223 */
1224 reverse_iterator
d3677132 1225 rbegin() _GLIBCXX_NOEXCEPT
03f9ea44 1226 { return reverse_iterator(this->_M_impl._M_finish); }
ed6814f7 1227
f6592a9e 1228 /**
5a1e5472
BK
1229 * Returns a read-only (constant) reverse iterator that points
1230 * to the last element in the %deque. Iteration is done in
1231 * reverse element order.
f6592a9e
PC
1232 */
1233 const_reverse_iterator
d3677132 1234 rbegin() const _GLIBCXX_NOEXCEPT
03f9ea44 1235 { return const_reverse_iterator(this->_M_impl._M_finish); }
ed6814f7 1236
f6592a9e 1237 /**
5a1e5472
BK
1238 * Returns a read/write reverse iterator that points to one
1239 * before the first element in the %deque. Iteration is done
1240 * in reverse element order.
f6592a9e
PC
1241 */
1242 reverse_iterator
d3677132 1243 rend() _GLIBCXX_NOEXCEPT
d2cc7f92 1244 { return reverse_iterator(this->_M_impl._M_start); }
ed6814f7 1245
f6592a9e 1246 /**
5a1e5472
BK
1247 * Returns a read-only (constant) reverse iterator that points
1248 * to one before the first element in the %deque. Iteration is
1249 * done in reverse element order.
f6592a9e
PC
1250 */
1251 const_reverse_iterator
d3677132 1252 rend() const _GLIBCXX_NOEXCEPT
03f9ea44 1253 { return const_reverse_iterator(this->_M_impl._M_start); }
ed6814f7 1254
734f5023 1255#if __cplusplus >= 201103L
0cd50f89
PC
1256 /**
1257 * Returns a read-only (constant) iterator that points to the first
1258 * element in the %deque. Iteration is done in ordinary element order.
1259 */
1260 const_iterator
d3677132 1261 cbegin() const noexcept
0cd50f89
PC
1262 { return this->_M_impl._M_start; }
1263
1264 /**
1265 * Returns a read-only (constant) iterator that points one past
1266 * the last element in the %deque. Iteration is done in
1267 * ordinary element order.
1268 */
1269 const_iterator
d3677132 1270 cend() const noexcept
0cd50f89
PC
1271 { return this->_M_impl._M_finish; }
1272
1273 /**
1274 * Returns a read-only (constant) reverse iterator that points
1275 * to the last element in the %deque. Iteration is done in
1276 * reverse element order.
1277 */
1278 const_reverse_iterator
d3677132 1279 crbegin() const noexcept
0cd50f89
PC
1280 { return const_reverse_iterator(this->_M_impl._M_finish); }
1281
1282 /**
1283 * Returns a read-only (constant) reverse iterator that points
1284 * to one before the first element in the %deque. Iteration is
1285 * done in reverse element order.
1286 */
1287 const_reverse_iterator
d3677132 1288 crend() const noexcept
0cd50f89
PC
1289 { return const_reverse_iterator(this->_M_impl._M_start); }
1290#endif
1291
f6592a9e
PC
1292 // [23.2.1.2] capacity
1293 /** Returns the number of elements in the %deque. */
1294 size_type
d3677132 1295 size() const _GLIBCXX_NOEXCEPT
03f9ea44 1296 { return this->_M_impl._M_finish - this->_M_impl._M_start; }
ed6814f7 1297
f6592a9e
PC
1298 /** Returns the size() of the largest possible %deque. */
1299 size_type
d3677132 1300 max_size() const _GLIBCXX_NOEXCEPT
af55b3af 1301 { return _S_max_size(_M_get_Tp_allocator()); }
ed6814f7 1302
734f5023 1303#if __cplusplus >= 201103L
dc2cf706
PC
1304 /**
1305 * @brief Resizes the %deque to the specified number of elements.
93c66bc6 1306 * @param __new_size Number of elements the %deque should contain.
dc2cf706
PC
1307 *
1308 * This function will %resize the %deque to the specified
1309 * number of elements. If the number is smaller than the
1310 * %deque's current size the %deque is truncated, otherwise
1311 * default constructed elements are appended.
1312 */
1313 void
1314 resize(size_type __new_size)
1315 {
1316 const size_type __len = size();
1317 if (__new_size > __len)
1318 _M_default_append(__new_size - __len);
1319 else if (__new_size < __len)
1320 _M_erase_at_end(this->_M_impl._M_start
1321 + difference_type(__new_size));
1322 }
1323
1324 /**
1325 * @brief Resizes the %deque to the specified number of elements.
93c66bc6
BK
1326 * @param __new_size Number of elements the %deque should contain.
1327 * @param __x Data with which new elements should be populated.
dc2cf706
PC
1328 *
1329 * This function will %resize the %deque to the specified
1330 * number of elements. If the number is smaller than the
1331 * %deque's current size the %deque is truncated, otherwise the
1332 * %deque is extended and new elements are populated with given
1333 * data.
1334 */
1335 void
1336 resize(size_type __new_size, const value_type& __x)
1337 {
1338 const size_type __len = size();
1339 if (__new_size > __len)
d7e16fc5 1340 _M_fill_insert(this->_M_impl._M_finish, __new_size - __len, __x);
dc2cf706
PC
1341 else if (__new_size < __len)
1342 _M_erase_at_end(this->_M_impl._M_start
1343 + difference_type(__new_size));
1344 }
1345#else
f6592a9e
PC
1346 /**
1347 * @brief Resizes the %deque to the specified number of elements.
93c66bc6
BK
1348 * @param __new_size Number of elements the %deque should contain.
1349 * @param __x Data with which new elements should be populated.
f6592a9e 1350 *
5a1e5472
BK
1351 * This function will %resize the %deque to the specified
1352 * number of elements. If the number is smaller than the
1353 * %deque's current size the %deque is truncated, otherwise the
1354 * %deque is extended and new elements are populated with given
1355 * data.
f6592a9e
PC
1356 */
1357 void
2fecaef4 1358 resize(size_type __new_size, value_type __x = value_type())
3971a4d2 1359 {
f6592a9e 1360 const size_type __len = size();
dc2cf706 1361 if (__new_size > __len)
d7e16fc5 1362 _M_fill_insert(this->_M_impl._M_finish, __new_size - __len, __x);
dc2cf706
PC
1363 else if (__new_size < __len)
1364 _M_erase_at_end(this->_M_impl._M_start
1365 + difference_type(__new_size));
3971a4d2 1366 }
dc2cf706 1367#endif
ed6814f7 1368
734f5023 1369#if __cplusplus >= 201103L
79667f82
PC
1370 /** A non-binding request to reduce memory use. */
1371 void
d15ac9d9 1372 shrink_to_fit() noexcept
8a752dfe 1373 { _M_shrink_to_fit(); }
79667f82
PC
1374#endif
1375
f6592a9e 1376 /**
5a1e5472
BK
1377 * Returns true if the %deque is empty. (Thus begin() would
1378 * equal end().)
f6592a9e 1379 */
d715f554 1380 _GLIBCXX_NODISCARD bool
d3677132 1381 empty() const _GLIBCXX_NOEXCEPT
03f9ea44 1382 { return this->_M_impl._M_finish == this->_M_impl._M_start; }
ed6814f7 1383
f6592a9e
PC
1384 // element access
1385 /**
5a1e5472 1386 * @brief Subscript access to the data contained in the %deque.
93c66bc6 1387 * @param __n The index of the element for which data should be
5a1e5472 1388 * accessed.
f6592a9e
PC
1389 * @return Read/write reference to data.
1390 *
1391 * This operator allows for easy, array-style, data access.
5a1e5472
BK
1392 * Note that data access with this operator is unchecked and
1393 * out_of_range lookups are not defined. (For checked lookups
1394 * see at().)
f6592a9e
PC
1395 */
1396 reference
d15ac9d9 1397 operator[](size_type __n) _GLIBCXX_NOEXCEPT
bd2ee798
FD
1398 {
1399 __glibcxx_requires_subscript(__n);
1400 return this->_M_impl._M_start[difference_type(__n)];
1401 }
ed6814f7 1402
f6592a9e 1403 /**
5a1e5472 1404 * @brief Subscript access to the data contained in the %deque.
93c66bc6 1405 * @param __n The index of the element for which data should be
5a1e5472 1406 * accessed.
f6592a9e
PC
1407 * @return Read-only (constant) reference to data.
1408 *
1409 * This operator allows for easy, array-style, data access.
5a1e5472
BK
1410 * Note that data access with this operator is unchecked and
1411 * out_of_range lookups are not defined. (For checked lookups
1412 * see at().)
f6592a9e
PC
1413 */
1414 const_reference
d15ac9d9 1415 operator[](size_type __n) const _GLIBCXX_NOEXCEPT
bd2ee798
FD
1416 {
1417 __glibcxx_requires_subscript(__n);
1418 return this->_M_impl._M_start[difference_type(__n)];
1419 }
ed6814f7 1420
f6592a9e 1421 protected:
4312e020 1422 /// Safety check used only from at().
f6592a9e
PC
1423 void
1424 _M_range_check(size_type __n) const
3971a4d2 1425 {
f6592a9e 1426 if (__n >= this->size())
9779c871
PP
1427 __throw_out_of_range_fmt(__N("deque::_M_range_check: __n "
1428 "(which is %zu)>= this->size() "
1429 "(which is %zu)"),
1430 __n, this->size());
3971a4d2 1431 }
ed6814f7 1432
f6592a9e
PC
1433 public:
1434 /**
1435 * @brief Provides access to the data contained in the %deque.
93c66bc6 1436 * @param __n The index of the element for which data should be
5a1e5472 1437 * accessed.
f6592a9e 1438 * @return Read/write reference to data.
93c66bc6 1439 * @throw std::out_of_range If @a __n is an invalid index.
f6592a9e 1440 *
5a1e5472
BK
1441 * This function provides for safer data access. The parameter
1442 * is first checked that it is in the range of the deque. The
1443 * function throws out_of_range if the check fails.
f6592a9e
PC
1444 */
1445 reference
1446 at(size_type __n)
43da93a7
PC
1447 {
1448 _M_range_check(__n);
1449 return (*this)[__n];
1450 }
ed6814f7 1451
f6592a9e
PC
1452 /**
1453 * @brief Provides access to the data contained in the %deque.
93c66bc6 1454 * @param __n The index of the element for which data should be
5a1e5472 1455 * accessed.
f6592a9e 1456 * @return Read-only (constant) reference to data.
93c66bc6 1457 * @throw std::out_of_range If @a __n is an invalid index.
f6592a9e
PC
1458 *
1459 * This function provides for safer data access. The parameter is first
1460 * checked that it is in the range of the deque. The function throws
1461 * out_of_range if the check fails.
1462 */
1463 const_reference
1464 at(size_type __n) const
1465 {
1466 _M_range_check(__n);
1467 return (*this)[__n];
3971a4d2 1468 }
ed6814f7 1469
f6592a9e 1470 /**
5a1e5472
BK
1471 * Returns a read/write reference to the data at the first
1472 * element of the %deque.
f6592a9e
PC
1473 */
1474 reference
d15ac9d9 1475 front() _GLIBCXX_NOEXCEPT
bd2ee798
FD
1476 {
1477 __glibcxx_requires_nonempty();
1478 return *begin();
1479 }
ed6814f7 1480
f6592a9e
PC
1481 /**
1482 * Returns a read-only (constant) reference to the data at the first
1483 * element of the %deque.
1484 */
1485 const_reference
d15ac9d9 1486 front() const _GLIBCXX_NOEXCEPT
bd2ee798
FD
1487 {
1488 __glibcxx_requires_nonempty();
1489 return *begin();
1490 }
ed6814f7 1491
f6592a9e
PC
1492 /**
1493 * Returns a read/write reference to the data at the last element of the
1494 * %deque.
1495 */
1496 reference
d15ac9d9 1497 back() _GLIBCXX_NOEXCEPT
f6592a9e 1498 {
bd2ee798 1499 __glibcxx_requires_nonempty();
f4e5280b 1500 iterator __tmp = end();
f6592a9e
PC
1501 --__tmp;
1502 return *__tmp;
3971a4d2 1503 }
ed6814f7 1504
f6592a9e
PC
1505 /**
1506 * Returns a read-only (constant) reference to the data at the last
1507 * element of the %deque.
1508 */
1509 const_reference
d15ac9d9 1510 back() const _GLIBCXX_NOEXCEPT
f6592a9e 1511 {
bd2ee798 1512 __glibcxx_requires_nonempty();
f4e5280b 1513 const_iterator __tmp = end();
f6592a9e
PC
1514 --__tmp;
1515 return *__tmp;
5cb6369d 1516 }
ed6814f7 1517
f6592a9e
PC
1518 // [23.2.1.2] modifiers
1519 /**
1520 * @brief Add data to the front of the %deque.
93c66bc6 1521 * @param __x Data to be added.
f6592a9e 1522 *
5a1e5472
BK
1523 * This is a typical stack operation. The function creates an
1524 * element at the front of the %deque and assigns the given
1525 * data to it. Due to the nature of a %deque this operation
1526 * can be done in constant time.
f6592a9e 1527 */
3971a4d2 1528 void
ed6814f7 1529 push_front(const value_type& __x)
3971a4d2 1530 {
03f9ea44 1531 if (this->_M_impl._M_start._M_cur != this->_M_impl._M_start._M_first)
f6592a9e 1532 {
fd18c76a 1533 _Alloc_traits::construct(this->_M_impl,
fe62dd04
FD
1534 this->_M_impl._M_start._M_cur - 1,
1535 __x);
03f9ea44 1536 --this->_M_impl._M_start._M_cur;
f6592a9e
PC
1537 }
1538 else
1539 _M_push_front_aux(__x);
3971a4d2 1540 }
4dc3e453 1541
734f5023 1542#if __cplusplus >= 201103L
4dc3e453
PC
1543 void
1544 push_front(value_type&& __x)
1545 { emplace_front(std::move(__x)); }
1546
7ffec97f 1547 template<typename... _Args>
594ef205
JW
1548#if __cplusplus > 201402L
1549 reference
1550#else
1551 void
1552#endif
fe62dd04 1553 emplace_front(_Args&&... __args);
7ffec97f 1554#endif
ed6814f7 1555
f6592a9e
PC
1556 /**
1557 * @brief Add data to the end of the %deque.
93c66bc6 1558 * @param __x Data to be added.
f6592a9e 1559 *
5a1e5472
BK
1560 * This is a typical stack operation. The function creates an
1561 * element at the end of the %deque and assigns the given data
1562 * to it. Due to the nature of a %deque this operation can be
1563 * done in constant time.
f6592a9e 1564 */
3971a4d2 1565 void
f6592a9e 1566 push_back(const value_type& __x)
3971a4d2 1567 {
0d8c9baf
PC
1568 if (this->_M_impl._M_finish._M_cur
1569 != this->_M_impl._M_finish._M_last - 1)
f6592a9e 1570 {
fd18c76a 1571 _Alloc_traits::construct(this->_M_impl,
fe62dd04 1572 this->_M_impl._M_finish._M_cur, __x);
03f9ea44 1573 ++this->_M_impl._M_finish._M_cur;
f6592a9e
PC
1574 }
1575 else
1576 _M_push_back_aux(__x);
3971a4d2 1577 }
4dc3e453 1578
734f5023 1579#if __cplusplus >= 201103L
4dc3e453
PC
1580 void
1581 push_back(value_type&& __x)
1582 { emplace_back(std::move(__x)); }
1583
7ffec97f 1584 template<typename... _Args>
594ef205
JW
1585#if __cplusplus > 201402L
1586 reference
1587#else
1588 void
1589#endif
fe62dd04 1590 emplace_back(_Args&&... __args);
7ffec97f 1591#endif
ed6814f7 1592
f6592a9e
PC
1593 /**
1594 * @brief Removes first element.
1595 *
1596 * This is a typical stack operation. It shrinks the %deque by one.
1597 *
1598 * Note that no data is returned, and if the first element's data is
1599 * needed, it should be retrieved before pop_front() is called.
1600 */
3971a4d2 1601 void
d15ac9d9 1602 pop_front() _GLIBCXX_NOEXCEPT
3971a4d2 1603 {
bd2ee798 1604 __glibcxx_requires_nonempty();
0d8c9baf
PC
1605 if (this->_M_impl._M_start._M_cur
1606 != this->_M_impl._M_start._M_last - 1)
f6592a9e 1607 {
fd18c76a 1608 _Alloc_traits::destroy(this->_M_impl,
fe62dd04 1609 this->_M_impl._M_start._M_cur);
03f9ea44 1610 ++this->_M_impl._M_start._M_cur;
f6592a9e 1611 }
ed6814f7 1612 else
f6592a9e 1613 _M_pop_front_aux();
3971a4d2 1614 }
ed6814f7 1615
f6592a9e
PC
1616 /**
1617 * @brief Removes last element.
1618 *
1619 * This is a typical stack operation. It shrinks the %deque by one.
1620 *
1621 * Note that no data is returned, and if the last element's data is
1622 * needed, it should be retrieved before pop_back() is called.
1623 */
3971a4d2 1624 void
d15ac9d9 1625 pop_back() _GLIBCXX_NOEXCEPT
f6592a9e 1626 {
bd2ee798 1627 __glibcxx_requires_nonempty();
0d8c9baf
PC
1628 if (this->_M_impl._M_finish._M_cur
1629 != this->_M_impl._M_finish._M_first)
f6592a9e 1630 {
03f9ea44 1631 --this->_M_impl._M_finish._M_cur;
fd18c76a 1632 _Alloc_traits::destroy(this->_M_impl,
fe62dd04 1633 this->_M_impl._M_finish._M_cur);
f6592a9e
PC
1634 }
1635 else
1636 _M_pop_back_aux();
1637 }
ed6814f7 1638
734f5023 1639#if __cplusplus >= 201103L
7ffec97f
CJ
1640 /**
1641 * @brief Inserts an object in %deque before specified iterator.
7b61c5a9 1642 * @param __position A const_iterator into the %deque.
93c66bc6 1643 * @param __args Arguments.
7ffec97f
CJ
1644 * @return An iterator that points to the inserted data.
1645 *
1646 * This function will insert an object of type T constructed
1647 * with T(std::forward<Args>(args)...) before the specified location.
1648 */
1649 template<typename... _Args>
fe62dd04
FD
1650 iterator
1651 emplace(const_iterator __position, _Args&&... __args);
7ffec97f 1652
7b61c5a9
PC
1653 /**
1654 * @brief Inserts given value into %deque before specified iterator.
1655 * @param __position A const_iterator into the %deque.
1656 * @param __x Data to be inserted.
1657 * @return An iterator that points to the inserted data.
1658 *
1659 * This function will insert a copy of the given value before the
1660 * specified location.
1661 */
1662 iterator
1663 insert(const_iterator __position, const value_type& __x);
1664#else
f6592a9e
PC
1665 /**
1666 * @brief Inserts given value into %deque before specified iterator.
93c66bc6
BK
1667 * @param __position An iterator into the %deque.
1668 * @param __x Data to be inserted.
f6592a9e
PC
1669 * @return An iterator that points to the inserted data.
1670 *
1671 * This function will insert a copy of the given value before the
1672 * specified location.
1673 */
1674 iterator
1b4454bf 1675 insert(iterator __position, const value_type& __x);
7b61c5a9 1676#endif
ed6814f7 1677
734f5023 1678#if __cplusplus >= 201103L
7ffec97f
CJ
1679 /**
1680 * @brief Inserts given rvalue into %deque before specified iterator.
7b61c5a9 1681 * @param __position A const_iterator into the %deque.
93c66bc6 1682 * @param __x Data to be inserted.
7ffec97f
CJ
1683 * @return An iterator that points to the inserted data.
1684 *
1685 * This function will insert a copy of the given rvalue before the
1686 * specified location.
1687 */
1688 iterator
7b61c5a9 1689 insert(const_iterator __position, value_type&& __x)
360b7bff 1690 { return emplace(__position, std::move(__x)); }
988499f4
JM
1691
1692 /**
1693 * @brief Inserts an initializer list into the %deque.
93c66bc6
BK
1694 * @param __p An iterator into the %deque.
1695 * @param __l An initializer_list.
988499f4
JM
1696 *
1697 * This function will insert copies of the data in the
93c66bc6
BK
1698 * initializer_list @a __l into the %deque before the location
1699 * specified by @a __p. This is known as <em>list insert</em>.
988499f4 1700 */
06eed9f5
PC
1701 iterator
1702 insert(const_iterator __p, initializer_list<value_type> __l)
d7e16fc5
FD
1703 {
1704 auto __offset = __p - cbegin();
1705 _M_range_insert_aux(__p._M_const_cast(), __l.begin(), __l.end(),
1706 std::random_access_iterator_tag());
1707 return begin() + __offset;
1708 }
7ffec97f
CJ
1709#endif
1710
06eed9f5
PC
1711#if __cplusplus >= 201103L
1712 /**
1713 * @brief Inserts a number of copies of given data into the %deque.
1714 * @param __position A const_iterator into the %deque.
1715 * @param __n Number of elements to be inserted.
1716 * @param __x Data to be inserted.
1717 * @return An iterator that points to the inserted data.
1718 *
1719 * This function will insert a specified number of copies of the given
1720 * data before the location specified by @a __position.
1721 */
1722 iterator
1723 insert(const_iterator __position, size_type __n, const value_type& __x)
1724 {
1725 difference_type __offset = __position - cbegin();
1726 _M_fill_insert(__position._M_const_cast(), __n, __x);
1727 return begin() + __offset;
1728 }
1729#else
f6592a9e
PC
1730 /**
1731 * @brief Inserts a number of copies of given data into the %deque.
93c66bc6
BK
1732 * @param __position An iterator into the %deque.
1733 * @param __n Number of elements to be inserted.
1734 * @param __x Data to be inserted.
f6592a9e
PC
1735 *
1736 * This function will insert a specified number of copies of the given
93c66bc6 1737 * data before the location specified by @a __position.
f6592a9e 1738 */
3971a4d2 1739 void
f6592a9e
PC
1740 insert(iterator __position, size_type __n, const value_type& __x)
1741 { _M_fill_insert(__position, __n, __x); }
06eed9f5 1742#endif
ed6814f7 1743
06eed9f5 1744#if __cplusplus >= 201103L
f6592a9e
PC
1745 /**
1746 * @brief Inserts a range into the %deque.
06eed9f5 1747 * @param __position A const_iterator into the %deque.
93c66bc6
BK
1748 * @param __first An input iterator.
1749 * @param __last An input iterator.
06eed9f5 1750 * @return An iterator that points to the inserted data.
f6592a9e 1751 *
5a1e5472 1752 * This function will insert copies of the data in the range
93c66bc6
BK
1753 * [__first,__last) into the %deque before the location specified
1754 * by @a __position. This is known as <em>range insert</em>.
f6592a9e 1755 */
2203cb90
PC
1756 template<typename _InputIterator,
1757 typename = std::_RequireInputIter<_InputIterator>>
fe62dd04
FD
1758 iterator
1759 insert(const_iterator __position, _InputIterator __first,
2203cb90 1760 _InputIterator __last)
fe62dd04 1761 {
06eed9f5
PC
1762 difference_type __offset = __position - cbegin();
1763 _M_insert_dispatch(__position._M_const_cast(),
1764 __first, __last, __false_type());
1765 return begin() + __offset;
1766 }
2203cb90 1767#else
06eed9f5
PC
1768 /**
1769 * @brief Inserts a range into the %deque.
1770 * @param __position An iterator into the %deque.
1771 * @param __first An input iterator.
1772 * @param __last An input iterator.
1773 *
1774 * This function will insert copies of the data in the range
1775 * [__first,__last) into the %deque before the location specified
1776 * by @a __position. This is known as <em>range insert</em>.
1777 */
f6592a9e 1778 template<typename _InputIterator>
fe62dd04
FD
1779 void
1780 insert(iterator __position, _InputIterator __first,
f6592a9e 1781 _InputIterator __last)
fe62dd04 1782 {
f6592a9e 1783 // Check whether it's an integral type. If so, it's not an iterator.
c0736a9d 1784 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
f6592a9e
PC
1785 _M_insert_dispatch(__position, __first, __last, _Integral());
1786 }
2203cb90 1787#endif
ed6814f7 1788
f6592a9e
PC
1789 /**
1790 * @brief Remove element at given position.
93c66bc6 1791 * @param __position Iterator pointing to element to be erased.
f6592a9e
PC
1792 * @return An iterator pointing to the next element (or end()).
1793 *
1794 * This function will erase the element at the given position and thus
1795 * shorten the %deque by one.
1796 *
1797 * The user is cautioned that
1798 * this function only erases the element, and that if the element is
1799 * itself a pointer, the pointed-to memory is not touched in any way.
28dac70a 1800 * Managing the pointer is the user's responsibility.
f6592a9e
PC
1801 */
1802 iterator
94938aec
PC
1803#if __cplusplus >= 201103L
1804 erase(const_iterator __position)
94938aec
PC
1805#else
1806 erase(iterator __position)
94938aec 1807#endif
7b61c5a9 1808 { return _M_erase(__position._M_const_cast()); }
ed6814f7 1809
f6592a9e
PC
1810 /**
1811 * @brief Remove a range of elements.
93c66bc6
BK
1812 * @param __first Iterator pointing to the first element to be erased.
1813 * @param __last Iterator pointing to one past the last element to be
f6592a9e
PC
1814 * erased.
1815 * @return An iterator pointing to the element pointed to by @a last
1816 * prior to erasing (or end()).
1817 *
93c66bc6
BK
1818 * This function will erase the elements in the range
1819 * [__first,__last) and shorten the %deque accordingly.
f6592a9e
PC
1820 *
1821 * The user is cautioned that
1822 * this function only erases the elements, and that if the elements
1823 * themselves are pointers, the pointed-to memory is not touched in any
28dac70a 1824 * way. Managing the pointer is the user's responsibility.
f6592a9e
PC
1825 */
1826 iterator
94938aec
PC
1827#if __cplusplus >= 201103L
1828 erase(const_iterator __first, const_iterator __last)
94938aec
PC
1829#else
1830 erase(iterator __first, iterator __last)
94938aec 1831#endif
7b61c5a9 1832 { return _M_erase(__first._M_const_cast(), __last._M_const_cast()); }
ed6814f7 1833
f6592a9e
PC
1834 /**
1835 * @brief Swaps data with another %deque.
93c66bc6 1836 * @param __x A %deque of the same element and allocator types.
f6592a9e
PC
1837 *
1838 * This exchanges the elements between two deques in constant time.
1839 * (Four pointers, so it should be quite fast.)
1840 * Note that the global std::swap() function is specialized such that
1841 * std::swap(d1,d2) will feed to this function.
0a2bf188
JW
1842 *
1843 * Whether the allocators are swapped depends on the allocator traits.
f6592a9e 1844 */
3971a4d2 1845 void
c5d9ec56 1846 swap(deque& __x) _GLIBCXX_NOEXCEPT
3971a4d2 1847 {
bd2ee798
FD
1848#if __cplusplus >= 201103L
1849 __glibcxx_assert(_Alloc_traits::propagate_on_container_swap::value
1850 || _M_get_Tp_allocator() == __x._M_get_Tp_allocator());
1851#endif
fd18c76a
JW
1852 _M_impl._M_swap_data(__x._M_impl);
1853 _Alloc_traits::_S_on_swap(_M_get_Tp_allocator(),
1854 __x._M_get_Tp_allocator());
3971a4d2 1855 }
ed6814f7 1856
f6592a9e
PC
1857 /**
1858 * Erases all the elements. Note that this function only erases the
1859 * elements, and that if the elements themselves are pointers, the
1860 * pointed-to memory is not touched in any way. Managing the pointer is
28dac70a 1861 * the user's responsibility.
f6592a9e 1862 */
d2cc7f92 1863 void
d3677132 1864 clear() _GLIBCXX_NOEXCEPT
d2cc7f92 1865 { _M_erase_at_end(begin()); }
ed6814f7 1866
f6592a9e
PC
1867 protected:
1868 // Internal constructor functions follow.
ed6814f7 1869
f6592a9e 1870 // called by the range constructor to implement [23.1.1]/9
25959e29
PC
1871
1872 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1873 // 438. Ambiguity in the "do the right thing" clause
f6592a9e 1874 template<typename _Integer>
fe62dd04
FD
1875 void
1876 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1877 {
af55b3af
JW
1878 _M_initialize_map(_S_check_init_len(static_cast<size_type>(__n),
1879 _M_get_Tp_allocator()));
f6592a9e
PC
1880 _M_fill_initialize(__x);
1881 }
ed6814f7 1882
af55b3af
JW
1883 static size_t
1884 _S_check_init_len(size_t __n, const allocator_type& __a)
1885 {
1886 if (__n > _S_max_size(__a))
1887 __throw_length_error(
1888 __N("cannot create std::deque larger than max_size()"));
1889 return __n;
1890 }
1891
1892 static size_type
1893 _S_max_size(const _Tp_alloc_type& __a) _GLIBCXX_NOEXCEPT
1894 {
1895 const size_t __diffmax = __gnu_cxx::__numeric_traits<ptrdiff_t>::__max;
1896 const size_t __allocmax = _Alloc_traits::max_size(__a);
1897 return (std::min)(__diffmax, __allocmax);
1898 }
1899
f6592a9e
PC
1900 // called by the range constructor to implement [23.1.1]/9
1901 template<typename _InputIterator>
fe62dd04
FD
1902 void
1903 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
f6592a9e 1904 __false_type)
fe62dd04 1905 {
d7e16fc5
FD
1906 _M_range_initialize(__first, __last,
1907 std::__iterator_category(__first));
f6592a9e 1908 }
ed6814f7 1909
f6592a9e
PC
1910 // called by the second initialize_dispatch above
1911 //@{
1912 /**
f6592a9e 1913 * @brief Fills the deque with whatever is in [first,last).
93c66bc6
BK
1914 * @param __first An input iterator.
1915 * @param __last An input iterator.
f6592a9e
PC
1916 * @return Nothing.
1917 *
1918 * If the iterators are actually forward iterators (or better), then the
1919 * memory layout can be done all at once. Else we move forward using
1920 * push_back on each value from the iterator.
f6592a9e
PC
1921 */
1922 template<typename _InputIterator>
fe62dd04
FD
1923 void
1924 _M_range_initialize(_InputIterator __first, _InputIterator __last,
6323b34e 1925 std::input_iterator_tag);
ed6814f7 1926
f6592a9e
PC
1927 // called by the second initialize_dispatch above
1928 template<typename _ForwardIterator>
fe62dd04
FD
1929 void
1930 _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
6323b34e 1931 std::forward_iterator_tag);
f6592a9e 1932 //@}
ed6814f7 1933
f6592a9e 1934 /**
f6592a9e 1935 * @brief Fills the %deque with copies of value.
93c66bc6 1936 * @param __value Initial value.
f6592a9e 1937 * @return Nothing.
5a1e5472
BK
1938 * @pre _M_start and _M_finish have already been initialized,
1939 * but none of the %deque's elements have yet been constructed.
f6592a9e
PC
1940 *
1941 * This function is called only when the user provides an explicit size
1942 * (with or without an explicit exemplar value).
f6592a9e 1943 */
3971a4d2 1944 void
f6592a9e 1945 _M_fill_initialize(const value_type& __value);
ed6814f7 1946
734f5023 1947#if __cplusplus >= 201103L
dc2cf706
PC
1948 // called by deque(n).
1949 void
1950 _M_default_initialize();
1951#endif
1952
f6592a9e
PC
1953 // Internal assign functions follow. The *_aux functions do the actual
1954 // assignment work for the range versions.
ed6814f7 1955
f6592a9e 1956 // called by the range assign to implement [23.1.1]/9
25959e29
PC
1957
1958 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1959 // 438. Ambiguity in the "do the right thing" clause
f6592a9e 1960 template<typename _Integer>
fe62dd04
FD
1961 void
1962 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1963 { _M_fill_assign(__n, __val); }
ed6814f7 1964
f6592a9e
PC
1965 // called by the range assign to implement [23.1.1]/9
1966 template<typename _InputIterator>
fe62dd04
FD
1967 void
1968 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
f6592a9e 1969 __false_type)
d7e16fc5 1970 { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
ed6814f7 1971
f6592a9e
PC
1972 // called by the second assign_dispatch above
1973 template<typename _InputIterator>
fe62dd04
FD
1974 void
1975 _M_assign_aux(_InputIterator __first, _InputIterator __last,
6323b34e 1976 std::input_iterator_tag);
ed6814f7 1977
f6592a9e
PC
1978 // called by the second assign_dispatch above
1979 template<typename _ForwardIterator>
fe62dd04
FD
1980 void
1981 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
6323b34e 1982 std::forward_iterator_tag)
fe62dd04 1983 {
f6592a9e
PC
1984 const size_type __len = std::distance(__first, __last);
1985 if (__len > size())
1986 {
1987 _ForwardIterator __mid = __first;
1988 std::advance(__mid, size());
1989 std::copy(__first, __mid, begin());
d7e16fc5
FD
1990 _M_range_insert_aux(end(), __mid, __last,
1991 std::__iterator_category(__first));
f6592a9e
PC
1992 }
1993 else
d2cc7f92 1994 _M_erase_at_end(std::copy(__first, __last, begin()));
f6592a9e 1995 }
ed6814f7 1996
5a1e5472
BK
1997 // Called by assign(n,t), and the range assign when it turns out
1998 // to be the same thing.
f6592a9e
PC
1999 void
2000 _M_fill_assign(size_type __n, const value_type& __val)
3971a4d2 2001 {
f6592a9e
PC
2002 if (__n > size())
2003 {
2004 std::fill(begin(), end(), __val);
d7e16fc5 2005 _M_fill_insert(end(), __n - size(), __val);
f6592a9e
PC
2006 }
2007 else
2008 {
1b4454bf 2009 _M_erase_at_end(begin() + difference_type(__n));
f6592a9e
PC
2010 std::fill(begin(), end(), __val);
2011 }
3971a4d2 2012 }
ed6814f7 2013
f6592a9e 2014 //@{
4312e020 2015 /// Helper functions for push_* and pop_*.
734f5023 2016#if __cplusplus < 201103L
f6592a9e 2017 void _M_push_back_aux(const value_type&);
d2cc7f92 2018
f6592a9e 2019 void _M_push_front_aux(const value_type&);
7ffec97f
CJ
2020#else
2021 template<typename... _Args>
fe62dd04 2022 void _M_push_back_aux(_Args&&... __args);
7ffec97f
CJ
2023
2024 template<typename... _Args>
fe62dd04 2025 void _M_push_front_aux(_Args&&... __args);
7ffec97f 2026#endif
d2cc7f92 2027
f6592a9e 2028 void _M_pop_back_aux();
d2cc7f92 2029
f6592a9e
PC
2030 void _M_pop_front_aux();
2031 //@}
ed6814f7 2032
f6592a9e
PC
2033 // Internal insert functions follow. The *_aux functions do the actual
2034 // insertion work when all shortcuts fail.
ed6814f7 2035
f6592a9e 2036 // called by the range insert to implement [23.1.1]/9
25959e29
PC
2037
2038 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2039 // 438. Ambiguity in the "do the right thing" clause
f6592a9e 2040 template<typename _Integer>
fe62dd04
FD
2041 void
2042 _M_insert_dispatch(iterator __pos,
f6592a9e 2043 _Integer __n, _Integer __x, __true_type)
fe62dd04 2044 { _M_fill_insert(__pos, __n, __x); }
ed6814f7 2045
f6592a9e
PC
2046 // called by the range insert to implement [23.1.1]/9
2047 template<typename _InputIterator>
fe62dd04
FD
2048 void
2049 _M_insert_dispatch(iterator __pos,
f6592a9e
PC
2050 _InputIterator __first, _InputIterator __last,
2051 __false_type)
fe62dd04
FD
2052 {
2053 _M_range_insert_aux(__pos, __first, __last,
d7e16fc5 2054 std::__iterator_category(__first));
f6592a9e 2055 }
ed6814f7 2056
f6592a9e
PC
2057 // called by the second insert_dispatch above
2058 template<typename _InputIterator>
fe62dd04
FD
2059 void
2060 _M_range_insert_aux(iterator __pos, _InputIterator __first,
6323b34e 2061 _InputIterator __last, std::input_iterator_tag);
ed6814f7 2062
f6592a9e
PC
2063 // called by the second insert_dispatch above
2064 template<typename _ForwardIterator>
fe62dd04
FD
2065 void
2066 _M_range_insert_aux(iterator __pos, _ForwardIterator __first,
6323b34e 2067 _ForwardIterator __last, std::forward_iterator_tag);
ed6814f7 2068
f6592a9e
PC
2069 // Called by insert(p,n,x), and the range insert when it turns out to be
2070 // the same thing. Can use fill functions in optimal situations,
2071 // otherwise passes off to insert_aux(p,n,x).
3971a4d2 2072 void
ed6814f7
BI
2073 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
2074
f6592a9e 2075 // called by insert(p,x)
734f5023 2076#if __cplusplus < 201103L
f6592a9e
PC
2077 iterator
2078 _M_insert_aux(iterator __pos, const value_type& __x);
7ffec97f
CJ
2079#else
2080 template<typename... _Args>
fe62dd04
FD
2081 iterator
2082 _M_insert_aux(iterator __pos, _Args&&... __args);
7ffec97f 2083#endif
ed6814f7 2084
f6592a9e
PC
2085 // called by insert(p,n,x) via fill_insert
2086 void
2087 _M_insert_aux(iterator __pos, size_type __n, const value_type& __x);
ed6814f7 2088
f6592a9e
PC
2089 // called by range_insert_aux for forward iterators
2090 template<typename _ForwardIterator>
fe62dd04
FD
2091 void
2092 _M_insert_aux(iterator __pos,
f6592a9e
PC
2093 _ForwardIterator __first, _ForwardIterator __last,
2094 size_type __n);
ed6814f7 2095
d2cc7f92
PC
2096
2097 // Internal erase functions follow.
2098
2099 void
2100 _M_destroy_data_aux(iterator __first, iterator __last);
2101
d2cc7f92
PC
2102 // Called by ~deque().
2103 // NB: Doesn't deallocate the nodes.
2104 template<typename _Alloc1>
fe62dd04
FD
2105 void
2106 _M_destroy_data(iterator __first, iterator __last, const _Alloc1&)
2107 { _M_destroy_data_aux(__first, __last); }
d2cc7f92
PC
2108
2109 void
2110 _M_destroy_data(iterator __first, iterator __last,
2111 const std::allocator<_Tp>&)
2112 {
ff2ea587
PC
2113 if (!__has_trivial_destructor(value_type))
2114 _M_destroy_data_aux(__first, __last);
d2cc7f92
PC
2115 }
2116
2117 // Called by erase(q1, q2).
2118 void
2119 _M_erase_at_begin(iterator __pos)
2120 {
2121 _M_destroy_data(begin(), __pos, _M_get_Tp_allocator());
2122 _M_destroy_nodes(this->_M_impl._M_start._M_node, __pos._M_node);
2123 this->_M_impl._M_start = __pos;
2124 }
2125
2126 // Called by erase(q1, q2), resize(), clear(), _M_assign_aux,
2127 // _M_fill_assign, operator=.
2128 void
2129 _M_erase_at_end(iterator __pos)
2130 {
2131 _M_destroy_data(__pos, end(), _M_get_Tp_allocator());
2132 _M_destroy_nodes(__pos._M_node + 1,
2133 this->_M_impl._M_finish._M_node + 1);
2134 this->_M_impl._M_finish = __pos;
2135 }
2136
94938aec
PC
2137 iterator
2138 _M_erase(iterator __pos);
2139
2140 iterator
2141 _M_erase(iterator __first, iterator __last);
2142
734f5023 2143#if __cplusplus >= 201103L
dc2cf706
PC
2144 // Called by resize(sz).
2145 void
2146 _M_default_append(size_type __n);
8a752dfe
FD
2147
2148 bool
2149 _M_shrink_to_fit();
dc2cf706
PC
2150#endif
2151
f6592a9e 2152 //@{
4312e020 2153 /// Memory-handling helpers for the previous internal insert functions.
f6592a9e
PC
2154 iterator
2155 _M_reserve_elements_at_front(size_type __n)
3971a4d2 2156 {
03f9ea44 2157 const size_type __vacancies = this->_M_impl._M_start._M_cur
fe62dd04 2158 - this->_M_impl._M_start._M_first;
f6592a9e
PC
2159 if (__n > __vacancies)
2160 _M_new_elements_at_front(__n - __vacancies);
03f9ea44 2161 return this->_M_impl._M_start - difference_type(__n);
3971a4d2 2162 }
ed6814f7 2163
f6592a9e
PC
2164 iterator
2165 _M_reserve_elements_at_back(size_type __n)
3971a4d2 2166 {
03f9ea44
DM
2167 const size_type __vacancies = (this->_M_impl._M_finish._M_last
2168 - this->_M_impl._M_finish._M_cur) - 1;
f6592a9e
PC
2169 if (__n > __vacancies)
2170 _M_new_elements_at_back(__n - __vacancies);
03f9ea44 2171 return this->_M_impl._M_finish + difference_type(__n);
3971a4d2 2172 }
ed6814f7 2173
f6592a9e
PC
2174 void
2175 _M_new_elements_at_front(size_type __new_elements);
ed6814f7 2176
f6592a9e
PC
2177 void
2178 _M_new_elements_at_back(size_type __new_elements);
2179 //@}
ed6814f7
BI
2180
2181
f6592a9e
PC
2182 //@{
2183 /**
f6592a9e
PC
2184 * @brief Memory-handling helpers for the major %map.
2185 *
5a1e5472
BK
2186 * Makes sure the _M_map has space for new nodes. Does not
2187 * actually add the nodes. Can invalidate _M_map pointers.
2188 * (And consequently, %deque iterators.)
f6592a9e 2189 */
3971a4d2 2190 void
1b4454bf 2191 _M_reserve_map_at_back(size_type __nodes_to_add = 1)
3971a4d2 2192 {
03f9ea44
DM
2193 if (__nodes_to_add + 1 > this->_M_impl._M_map_size
2194 - (this->_M_impl._M_finish._M_node - this->_M_impl._M_map))
f6592a9e 2195 _M_reallocate_map(__nodes_to_add, false);
3971a4d2 2196 }
ed6814f7 2197
3971a4d2 2198 void
1f9c69a9 2199 _M_reserve_map_at_front(size_type __nodes_to_add = 1)
3971a4d2 2200 {
0d8c9baf
PC
2201 if (__nodes_to_add > size_type(this->_M_impl._M_start._M_node
2202 - this->_M_impl._M_map))
f6592a9e 2203 _M_reallocate_map(__nodes_to_add, true);
3971a4d2 2204 }
ed6814f7 2205
3971a4d2 2206 void
f6592a9e
PC
2207 _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front);
2208 //@}
fd18c76a
JW
2209
2210#if __cplusplus >= 201103L
2211 // Constant-time, nothrow move assignment when source object's memory
2212 // can be moved because the allocators are equal.
2213 void
2214 _M_move_assign1(deque&& __x, /* always equal: */ true_type) noexcept
2215 {
2216 this->_M_impl._M_swap_data(__x._M_impl);
2217 __x.clear();
2218 std::__alloc_on_move(_M_get_Tp_allocator(), __x._M_get_Tp_allocator());
2219 }
2220
a2b5fdcb
JW
2221 // When the allocators are not equal the operation could throw, because
2222 // we might need to allocate a new map for __x after moving from it
2223 // or we might need to allocate new elements for *this.
fd18c76a
JW
2224 void
2225 _M_move_assign1(deque&& __x, /* always equal: */ false_type)
2226 {
2227 constexpr bool __move_storage =
2228 _Alloc_traits::_S_propagate_on_move_assign();
a2b5fdcb 2229 _M_move_assign2(std::move(__x), __bool_constant<__move_storage>());
fd18c76a
JW
2230 }
2231
2232 // Destroy all elements and deallocate all memory, then replace
2233 // with elements created from __args.
2234 template<typename... _Args>
2235 void
2236 _M_replace_map(_Args&&... __args)
2237 {
2238 // Create new data first, so if allocation fails there are no effects.
2239 deque __newobj(std::forward<_Args>(__args)...);
2240 // Free existing storage using existing allocator.
2241 clear();
2242 _M_deallocate_node(*begin()._M_node); // one node left after clear()
2243 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
2244 this->_M_impl._M_map = nullptr;
2245 this->_M_impl._M_map_size = 0;
2246 // Take ownership of replacement memory.
2247 this->_M_impl._M_swap_data(__newobj._M_impl);
2248 }
2249
2250 // Do move assignment when the allocator propagates.
2251 void
2252 _M_move_assign2(deque&& __x, /* propagate: */ true_type)
2253 {
2254 // Make a copy of the original allocator state.
2255 auto __alloc = __x._M_get_Tp_allocator();
2256 // The allocator propagates so storage can be moved from __x,
2257 // leaving __x in a valid empty state with a moved-from allocator.
2258 _M_replace_map(std::move(__x));
2259 // Move the corresponding allocator state too.
2260 _M_get_Tp_allocator() = std::move(__alloc);
2261 }
2262
2263 // Do move assignment when it may not be possible to move source
2264 // object's memory, resulting in a linear-time operation.
2265 void
2266 _M_move_assign2(deque&& __x, /* propagate: */ false_type)
2267 {
2268 if (__x._M_get_Tp_allocator() == this->_M_get_Tp_allocator())
2269 {
2270 // The allocators are equal so storage can be moved from __x,
2271 // leaving __x in a valid empty state with its current allocator.
2272 _M_replace_map(std::move(__x), __x.get_allocator());
2273 }
2274 else
2275 {
2276 // The rvalue's allocator cannot be moved and is not equal,
2277 // so we need to individually move each element.
d7e16fc5
FD
2278 _M_assign_aux(std::__make_move_if_noexcept_iterator(__x.begin()),
2279 std::__make_move_if_noexcept_iterator(__x.end()),
2280 std::random_access_iterator_tag());
fd18c76a
JW
2281 __x.clear();
2282 }
2283 }
2284#endif
f6592a9e 2285 };
ed6814f7 2286
225ab2b0
JW
2287#if __cpp_deduction_guides >= 201606
2288 template<typename _InputIterator, typename _ValT
2289 = typename iterator_traits<_InputIterator>::value_type,
2290 typename _Allocator = allocator<_ValT>,
2291 typename = _RequireInputIter<_InputIterator>,
2292 typename = _RequireAllocator<_Allocator>>
2293 deque(_InputIterator, _InputIterator, _Allocator = _Allocator())
2294 -> deque<_ValT, _Allocator>;
2295#endif
ed6814f7 2296
3971a4d2
PE
2297 /**
2298 * @brief Deque equality comparison.
93c66bc6
BK
2299 * @param __x A %deque.
2300 * @param __y A %deque of the same type as @a __x.
3971a4d2
PE
2301 * @return True iff the size and elements of the deques are equal.
2302 *
2303 * This is an equivalence relation. It is linear in the size of the
2304 * deques. Deques are considered equivalent if their sizes are equal,
2305 * and if corresponding elements compare equal.
5cb6369d 2306 */
285b36d6 2307 template<typename _Tp, typename _Alloc>
f6592a9e
PC
2308 inline bool
2309 operator==(const deque<_Tp, _Alloc>& __x,
3971a4d2 2310 const deque<_Tp, _Alloc>& __y)
f6592a9e 2311 { return __x.size() == __y.size()
fe62dd04 2312 && std::equal(__x.begin(), __x.end(), __y.begin()); }
ed6814f7 2313
3971a4d2
PE
2314 /**
2315 * @brief Deque ordering relation.
93c66bc6
BK
2316 * @param __x A %deque.
2317 * @param __y A %deque of the same type as @a __x.
2318 * @return True iff @a x is lexicographically less than @a __y.
5cb6369d 2319 *
3971a4d2
PE
2320 * This is a total ordering relation. It is linear in the size of the
2321 * deques. The elements must be comparable with @c <.
2322 *
9536ca34 2323 * See std::lexicographical_compare() for how the determination is made.
5cb6369d 2324 */
285b36d6 2325 template<typename _Tp, typename _Alloc>
f6592a9e
PC
2326 inline bool
2327 operator<(const deque<_Tp, _Alloc>& __x,
2328 const deque<_Tp, _Alloc>& __y)
1444936f
PC
2329 { return std::lexicographical_compare(__x.begin(), __x.end(),
2330 __y.begin(), __y.end()); }
ed6814f7 2331
3971a4d2 2332 /// Based on operator==
285b36d6 2333 template<typename _Tp, typename _Alloc>
f6592a9e
PC
2334 inline bool
2335 operator!=(const deque<_Tp, _Alloc>& __x,
2336 const deque<_Tp, _Alloc>& __y)
2337 { return !(__x == __y); }
ed6814f7 2338
3971a4d2 2339 /// Based on operator<
285b36d6 2340 template<typename _Tp, typename _Alloc>
f6592a9e
PC
2341 inline bool
2342 operator>(const deque<_Tp, _Alloc>& __x,
2343 const deque<_Tp, _Alloc>& __y)
2344 { return __y < __x; }
ed6814f7 2345
3971a4d2 2346 /// Based on operator<
285b36d6 2347 template<typename _Tp, typename _Alloc>
f6592a9e
PC
2348 inline bool
2349 operator<=(const deque<_Tp, _Alloc>& __x,
2350 const deque<_Tp, _Alloc>& __y)
2351 { return !(__y < __x); }
ed6814f7 2352
3971a4d2 2353 /// Based on operator<
285b36d6 2354 template<typename _Tp, typename _Alloc>
f6592a9e
PC
2355 inline bool
2356 operator>=(const deque<_Tp, _Alloc>& __x,
2357 const deque<_Tp, _Alloc>& __y)
2358 { return !(__x < __y); }
ed6814f7 2359
3971a4d2 2360 /// See std::deque::swap().
285b36d6 2361 template<typename _Tp, typename _Alloc>
f6592a9e
PC
2362 inline void
2363 swap(deque<_Tp,_Alloc>& __x, deque<_Tp,_Alloc>& __y)
c5d9ec56 2364 _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
f6592a9e 2365 { __x.swap(__y); }
3cbc7af0 2366
465d76b7
PC
2367#undef _GLIBCXX_DEQUE_BUF_SIZE
2368
12ffa228 2369_GLIBCXX_END_NAMESPACE_CONTAINER
ff2e7f19
MG
2370
2371#if __cplusplus >= 201103L
2372 // std::allocator is safe, but it is not the only allocator
2373 // for which this is valid.
2374 template<class _Tp>
2375 struct __is_trivially_relocatable<_GLIBCXX_STD_C::deque<_Tp>>
2376 : true_type { };
2377#endif
2378
4a15d842 2379_GLIBCXX_END_NAMESPACE_VERSION
12ffa228 2380} // namespace std
ed6814f7 2381
046d30f4 2382#endif /* _STL_DEQUE_H */