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