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