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