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