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