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