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  { _M_initialize_map(0); }
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  //This struct encapsulates the implementation of the std::deque
491  //standard container and at the same time makes use of the EBO
492  //for empty allocators.
493  typedef typename _Alloc::template rebind<_Tp*>::other _Map_alloc_type;
494 
495  typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
496 
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  * @brief Creates a %deque with no elements.
786  * @param __a An allocator object.
787  */
788  explicit
789  deque(const allocator_type& __a = allocator_type())
790  : _Base(__a) { }
791 
792 #if __cplusplus >= 201103L
793  /**
794  * @brief Creates a %deque with default constructed elements.
795  * @param __n The number of elements to initially create.
796  *
797  * This constructor fills the %deque with @a n default
798  * constructed elements.
799  */
800  explicit
801  deque(size_type __n)
802  : _Base(__n)
803  { _M_default_initialize(); }
804 
805  /**
806  * @brief Creates a %deque with copies of an exemplar element.
807  * @param __n The number of elements to initially create.
808  * @param __value An element to copy.
809  * @param __a An allocator.
810  *
811  * This constructor fills the %deque with @a __n copies of @a __value.
812  */
813  deque(size_type __n, const value_type& __value,
814  const allocator_type& __a = allocator_type())
815  : _Base(__a, __n)
816  { _M_fill_initialize(__value); }
817 #else
818  /**
819  * @brief Creates a %deque with copies of an exemplar element.
820  * @param __n The number of elements to initially create.
821  * @param __value An element to copy.
822  * @param __a An allocator.
823  *
824  * This constructor fills the %deque with @a __n copies of @a __value.
825  */
826  explicit
827  deque(size_type __n, const value_type& __value = value_type(),
828  const allocator_type& __a = allocator_type())
829  : _Base(__a, __n)
830  { _M_fill_initialize(__value); }
831 #endif
832 
833  /**
834  * @brief %Deque copy constructor.
835  * @param __x A %deque of identical element and allocator types.
836  *
837  * The newly-created %deque uses a copy of the allocation object used
838  * by @a __x.
839  */
840  deque(const deque& __x)
841  : _Base(__x._M_get_Tp_allocator(), __x.size())
842  { std::__uninitialized_copy_a(__x.begin(), __x.end(),
843  this->_M_impl._M_start,
844  _M_get_Tp_allocator()); }
845 
846 #if __cplusplus >= 201103L
847  /**
848  * @brief %Deque move constructor.
849  * @param __x A %deque of identical element and allocator types.
850  *
851  * The newly-created %deque contains the exact contents of @a __x.
852  * The contents of @a __x are a valid, but unspecified %deque.
853  */
854  deque(deque&& __x)
855  : _Base(std::move(__x)) { }
856 
857  /**
858  * @brief Builds a %deque from an initializer list.
859  * @param __l An initializer_list.
860  * @param __a An allocator object.
861  *
862  * Create a %deque consisting of copies of the elements in the
863  * initializer_list @a __l.
864  *
865  * This will call the element type's copy constructor N times
866  * (where N is __l.size()) and do no memory reallocation.
867  */
869  const allocator_type& __a = allocator_type())
870  : _Base(__a)
871  {
872  _M_range_initialize(__l.begin(), __l.end(),
874  }
875 #endif
876 
877  /**
878  * @brief Builds a %deque from a range.
879  * @param __first An input iterator.
880  * @param __last An input iterator.
881  * @param __a An allocator object.
882  *
883  * Create a %deque consisting of copies of the elements from [__first,
884  * __last).
885  *
886  * If the iterators are forward, bidirectional, or random-access, then
887  * this will call the elements' copy constructor N times (where N is
888  * distance(__first,__last)) and do no memory reallocation. But if only
889  * input iterators are used, then this will do at most 2N calls to the
890  * copy constructor, and logN memory reallocations.
891  */
892 #if __cplusplus >= 201103L
893  template<typename _InputIterator,
894  typename = std::_RequireInputIter<_InputIterator>>
895  deque(_InputIterator __first, _InputIterator __last,
896  const allocator_type& __a = allocator_type())
897  : _Base(__a)
898  { _M_initialize_dispatch(__first, __last, __false_type()); }
899 #else
900  template<typename _InputIterator>
901  deque(_InputIterator __first, _InputIterator __last,
902  const allocator_type& __a = allocator_type())
903  : _Base(__a)
904  {
905  // Check whether it's an integral type. If so, it's not an iterator.
906  typedef typename std::__is_integer<_InputIterator>::__type _Integral;
907  _M_initialize_dispatch(__first, __last, _Integral());
908  }
909 #endif
910 
911  /**
912  * The dtor only erases the elements, and note that if the elements
913  * themselves are pointers, the pointed-to memory is not touched in any
914  * way. Managing the pointer is the user's responsibility.
915  */
916  ~deque() _GLIBCXX_NOEXCEPT
917  { _M_destroy_data(begin(), end(), _M_get_Tp_allocator()); }
918 
919  /**
920  * @brief %Deque assignment operator.
921  * @param __x A %deque of identical element and allocator types.
922  *
923  * All the elements of @a x are copied, but unlike the copy constructor,
924  * the allocator object is not copied.
925  */
926  deque&
927  operator=(const deque& __x);
928 
929 #if __cplusplus >= 201103L
930  /**
931  * @brief %Deque move assignment operator.
932  * @param __x A %deque of identical element and allocator types.
933  *
934  * The contents of @a __x are moved into this deque (without copying).
935  * @a __x is a valid, but unspecified %deque.
936  */
937  deque&
938  operator=(deque&& __x) noexcept
939  {
940  // NB: DR 1204.
941  // NB: DR 675.
942  this->clear();
943  this->swap(__x);
944  return *this;
945  }
946 
947  /**
948  * @brief Assigns an initializer list to a %deque.
949  * @param __l An initializer_list.
950  *
951  * This function fills a %deque with copies of the elements in the
952  * initializer_list @a __l.
953  *
954  * Note that the assignment completely changes the %deque and that the
955  * resulting %deque's size is the same as the number of elements
956  * assigned. Old data may be lost.
957  */
958  deque&
960  {
961  this->assign(__l.begin(), __l.end());
962  return *this;
963  }
964 #endif
965 
966  /**
967  * @brief Assigns a given value to a %deque.
968  * @param __n Number of elements to be assigned.
969  * @param __val Value to be assigned.
970  *
971  * This function fills a %deque with @a n copies of the given
972  * value. Note that the assignment completely changes the
973  * %deque and that the resulting %deque's size is the same as
974  * the number of elements assigned. Old data may be lost.
975  */
976  void
977  assign(size_type __n, const value_type& __val)
978  { _M_fill_assign(__n, __val); }
979 
980  /**
981  * @brief Assigns a range to a %deque.
982  * @param __first An input iterator.
983  * @param __last An input iterator.
984  *
985  * This function fills a %deque with copies of the elements in the
986  * range [__first,__last).
987  *
988  * Note that the assignment completely changes the %deque and that the
989  * resulting %deque's size is the same as the number of elements
990  * assigned. Old data may be lost.
991  */
992 #if __cplusplus >= 201103L
993  template<typename _InputIterator,
994  typename = std::_RequireInputIter<_InputIterator>>
995  void
996  assign(_InputIterator __first, _InputIterator __last)
997  { _M_assign_dispatch(__first, __last, __false_type()); }
998 #else
999  template<typename _InputIterator>
1000  void
1001  assign(_InputIterator __first, _InputIterator __last)
1002  {
1003  typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1004  _M_assign_dispatch(__first, __last, _Integral());
1005  }
1006 #endif
1007 
1008 #if __cplusplus >= 201103L
1009  /**
1010  * @brief Assigns an initializer list to a %deque.
1011  * @param __l An initializer_list.
1012  *
1013  * This function fills a %deque with copies of the elements in the
1014  * initializer_list @a __l.
1015  *
1016  * Note that the assignment completely changes the %deque and that the
1017  * resulting %deque's size is the same as the number of elements
1018  * assigned. Old data may be lost.
1019  */
1020  void
1022  { this->assign(__l.begin(), __l.end()); }
1023 #endif
1024 
1025  /// Get a copy of the memory allocation object.
1026  allocator_type
1027  get_allocator() const _GLIBCXX_NOEXCEPT
1028  { return _Base::get_allocator(); }
1029 
1030  // iterators
1031  /**
1032  * Returns a read/write iterator that points to the first element in the
1033  * %deque. Iteration is done in ordinary element order.
1034  */
1035  iterator
1036  begin() _GLIBCXX_NOEXCEPT
1037  { return this->_M_impl._M_start; }
1038 
1039  /**
1040  * Returns a read-only (constant) iterator that points to the first
1041  * element in the %deque. Iteration is done in ordinary element order.
1042  */
1043  const_iterator
1044  begin() const _GLIBCXX_NOEXCEPT
1045  { return this->_M_impl._M_start; }
1046 
1047  /**
1048  * Returns a read/write iterator that points one past the last
1049  * element in the %deque. Iteration is done in ordinary
1050  * element order.
1051  */
1052  iterator
1053  end() _GLIBCXX_NOEXCEPT
1054  { return this->_M_impl._M_finish; }
1055 
1056  /**
1057  * Returns a read-only (constant) iterator that points one past
1058  * the last element in the %deque. Iteration is done in
1059  * ordinary element order.
1060  */
1061  const_iterator
1062  end() const _GLIBCXX_NOEXCEPT
1063  { return this->_M_impl._M_finish; }
1064 
1065  /**
1066  * Returns a read/write reverse iterator that points to the
1067  * last element in the %deque. Iteration is done in reverse
1068  * element order.
1069  */
1071  rbegin() _GLIBCXX_NOEXCEPT
1072  { return reverse_iterator(this->_M_impl._M_finish); }
1073 
1074  /**
1075  * Returns a read-only (constant) reverse iterator that points
1076  * to the last element in the %deque. Iteration is done in
1077  * reverse element order.
1078  */
1079  const_reverse_iterator
1080  rbegin() const _GLIBCXX_NOEXCEPT
1081  { return const_reverse_iterator(this->_M_impl._M_finish); }
1082 
1083  /**
1084  * Returns a read/write reverse iterator that points to one
1085  * before the first element in the %deque. Iteration is done
1086  * in reverse element order.
1087  */
1089  rend() _GLIBCXX_NOEXCEPT
1090  { return reverse_iterator(this->_M_impl._M_start); }
1091 
1092  /**
1093  * Returns a read-only (constant) reverse iterator that points
1094  * to one before the first element in the %deque. Iteration is
1095  * done in reverse element order.
1096  */
1097  const_reverse_iterator
1098  rend() const _GLIBCXX_NOEXCEPT
1099  { return const_reverse_iterator(this->_M_impl._M_start); }
1100 
1101 #if __cplusplus >= 201103L
1102  /**
1103  * Returns a read-only (constant) iterator that points to the first
1104  * element in the %deque. Iteration is done in ordinary element order.
1105  */
1106  const_iterator
1107  cbegin() const noexcept
1108  { return this->_M_impl._M_start; }
1109 
1110  /**
1111  * Returns a read-only (constant) iterator that points one past
1112  * the last element in the %deque. Iteration is done in
1113  * ordinary element order.
1114  */
1115  const_iterator
1116  cend() const noexcept
1117  { return this->_M_impl._M_finish; }
1118 
1119  /**
1120  * Returns a read-only (constant) reverse iterator that points
1121  * to the last element in the %deque. Iteration is done in
1122  * reverse element order.
1123  */
1124  const_reverse_iterator
1125  crbegin() const noexcept
1126  { return const_reverse_iterator(this->_M_impl._M_finish); }
1127 
1128  /**
1129  * Returns a read-only (constant) reverse iterator that points
1130  * to one before the first element in the %deque. Iteration is
1131  * done in reverse element order.
1132  */
1133  const_reverse_iterator
1134  crend() const noexcept
1135  { return const_reverse_iterator(this->_M_impl._M_start); }
1136 #endif
1137 
1138  // [23.2.1.2] capacity
1139  /** Returns the number of elements in the %deque. */
1140  size_type
1141  size() const _GLIBCXX_NOEXCEPT
1142  { return this->_M_impl._M_finish - this->_M_impl._M_start; }
1143 
1144  /** Returns the size() of the largest possible %deque. */
1145  size_type
1146  max_size() const _GLIBCXX_NOEXCEPT
1147  { return _M_get_Tp_allocator().max_size(); }
1148 
1149 #if __cplusplus >= 201103L
1150  /**
1151  * @brief Resizes the %deque to the specified number of elements.
1152  * @param __new_size Number of elements the %deque should contain.
1153  *
1154  * This function will %resize the %deque to the specified
1155  * number of elements. If the number is smaller than the
1156  * %deque's current size the %deque is truncated, otherwise
1157  * default constructed elements are appended.
1158  */
1159  void
1160  resize(size_type __new_size)
1161  {
1162  const size_type __len = size();
1163  if (__new_size > __len)
1164  _M_default_append(__new_size - __len);
1165  else if (__new_size < __len)
1166  _M_erase_at_end(this->_M_impl._M_start
1167  + difference_type(__new_size));
1168  }
1169 
1170  /**
1171  * @brief Resizes the %deque to the specified number of elements.
1172  * @param __new_size Number of elements the %deque should contain.
1173  * @param __x Data with which new elements should be populated.
1174  *
1175  * This function will %resize the %deque to the specified
1176  * number of elements. If the number is smaller than the
1177  * %deque's current size the %deque is truncated, otherwise the
1178  * %deque is extended and new elements are populated with given
1179  * data.
1180  */
1181  void
1182  resize(size_type __new_size, const value_type& __x)
1183  {
1184  const size_type __len = size();
1185  if (__new_size > __len)
1186  insert(this->_M_impl._M_finish, __new_size - __len, __x);
1187  else if (__new_size < __len)
1188  _M_erase_at_end(this->_M_impl._M_start
1189  + difference_type(__new_size));
1190  }
1191 #else
1192  /**
1193  * @brief Resizes the %deque to the specified number of elements.
1194  * @param __new_size Number of elements the %deque should contain.
1195  * @param __x Data with which new elements should be populated.
1196  *
1197  * This function will %resize the %deque to the specified
1198  * number of elements. If the number is smaller than the
1199  * %deque's current size the %deque is truncated, otherwise the
1200  * %deque is extended and new elements are populated with given
1201  * data.
1202  */
1203  void
1204  resize(size_type __new_size, value_type __x = value_type())
1205  {
1206  const size_type __len = size();
1207  if (__new_size > __len)
1208  insert(this->_M_impl._M_finish, __new_size - __len, __x);
1209  else if (__new_size < __len)
1210  _M_erase_at_end(this->_M_impl._M_start
1211  + difference_type(__new_size));
1212  }
1213 #endif
1214 
1215 #if __cplusplus >= 201103L
1216  /** A non-binding request to reduce memory use. */
1217  void
1218  shrink_to_fit() noexcept
1219  { _M_shrink_to_fit(); }
1220 #endif
1221 
1222  /**
1223  * Returns true if the %deque is empty. (Thus begin() would
1224  * equal end().)
1225  */
1226  bool
1227  empty() const _GLIBCXX_NOEXCEPT
1228  { return this->_M_impl._M_finish == this->_M_impl._M_start; }
1229 
1230  // element access
1231  /**
1232  * @brief Subscript access to the data contained in the %deque.
1233  * @param __n The index of the element for which data should be
1234  * accessed.
1235  * @return Read/write reference to data.
1236  *
1237  * This operator allows for easy, array-style, data access.
1238  * Note that data access with this operator is unchecked and
1239  * out_of_range lookups are not defined. (For checked lookups
1240  * see at().)
1241  */
1242  reference
1243  operator[](size_type __n) _GLIBCXX_NOEXCEPT
1244  { return this->_M_impl._M_start[difference_type(__n)]; }
1245 
1246  /**
1247  * @brief Subscript access to the data contained in the %deque.
1248  * @param __n The index of the element for which data should be
1249  * accessed.
1250  * @return Read-only (constant) reference to data.
1251  *
1252  * This operator allows for easy, array-style, data access.
1253  * Note that data access with this operator is unchecked and
1254  * out_of_range lookups are not defined. (For checked lookups
1255  * see at().)
1256  */
1257  const_reference
1258  operator[](size_type __n) const _GLIBCXX_NOEXCEPT
1259  { return this->_M_impl._M_start[difference_type(__n)]; }
1260 
1261  protected:
1262  /// Safety check used only from at().
1263  void
1264  _M_range_check(size_type __n) const
1265  {
1266  if (__n >= this->size())
1267  __throw_out_of_range_fmt(__N("deque::_M_range_check: __n "
1268  "(which is %zu)>= this->size() "
1269  "(which is %zu)"),
1270  __n, this->size());
1271  }
1272 
1273  public:
1274  /**
1275  * @brief Provides access to the data contained in the %deque.
1276  * @param __n The index of the element for which data should be
1277  * accessed.
1278  * @return Read/write reference to data.
1279  * @throw std::out_of_range If @a __n is an invalid index.
1280  *
1281  * This function provides for safer data access. The parameter
1282  * is first checked that it is in the range of the deque. The
1283  * function throws out_of_range if the check fails.
1284  */
1285  reference
1286  at(size_type __n)
1287  {
1288  _M_range_check(__n);
1289  return (*this)[__n];
1290  }
1291 
1292  /**
1293  * @brief Provides access to the data contained in the %deque.
1294  * @param __n The index of the element for which data should be
1295  * accessed.
1296  * @return Read-only (constant) reference to data.
1297  * @throw std::out_of_range If @a __n is an invalid index.
1298  *
1299  * This function provides for safer data access. The parameter is first
1300  * checked that it is in the range of the deque. The function throws
1301  * out_of_range if the check fails.
1302  */
1303  const_reference
1304  at(size_type __n) const
1305  {
1306  _M_range_check(__n);
1307  return (*this)[__n];
1308  }
1309 
1310  /**
1311  * Returns a read/write reference to the data at the first
1312  * element of the %deque.
1313  */
1314  reference
1315  front() _GLIBCXX_NOEXCEPT
1316  { return *begin(); }
1317 
1318  /**
1319  * Returns a read-only (constant) reference to the data at the first
1320  * element of the %deque.
1321  */
1322  const_reference
1323  front() const _GLIBCXX_NOEXCEPT
1324  { return *begin(); }
1325 
1326  /**
1327  * Returns a read/write reference to the data at the last element of the
1328  * %deque.
1329  */
1330  reference
1331  back() _GLIBCXX_NOEXCEPT
1332  {
1333  iterator __tmp = end();
1334  --__tmp;
1335  return *__tmp;
1336  }
1337 
1338  /**
1339  * Returns a read-only (constant) reference to the data at the last
1340  * element of the %deque.
1341  */
1342  const_reference
1343  back() const _GLIBCXX_NOEXCEPT
1344  {
1345  const_iterator __tmp = end();
1346  --__tmp;
1347  return *__tmp;
1348  }
1349 
1350  // [23.2.1.2] modifiers
1351  /**
1352  * @brief Add data to the front of the %deque.
1353  * @param __x Data to be added.
1354  *
1355  * This is a typical stack operation. The function creates an
1356  * element at the front of the %deque and assigns the given
1357  * data to it. Due to the nature of a %deque this operation
1358  * can be done in constant time.
1359  */
1360  void
1361  push_front(const value_type& __x)
1362  {
1363  if (this->_M_impl._M_start._M_cur != this->_M_impl._M_start._M_first)
1364  {
1365  this->_M_impl.construct(this->_M_impl._M_start._M_cur - 1, __x);
1366  --this->_M_impl._M_start._M_cur;
1367  }
1368  else
1369  _M_push_front_aux(__x);
1370  }
1371 
1372 #if __cplusplus >= 201103L
1373  void
1374  push_front(value_type&& __x)
1375  { emplace_front(std::move(__x)); }
1376 
1377  template<typename... _Args>
1378  void
1379  emplace_front(_Args&&... __args);
1380 #endif
1381 
1382  /**
1383  * @brief Add data to the end of the %deque.
1384  * @param __x Data to be added.
1385  *
1386  * This is a typical stack operation. The function creates an
1387  * element at the end of the %deque and assigns the given data
1388  * to it. Due to the nature of a %deque this operation can be
1389  * done in constant time.
1390  */
1391  void
1392  push_back(const value_type& __x)
1393  {
1394  if (this->_M_impl._M_finish._M_cur
1395  != this->_M_impl._M_finish._M_last - 1)
1396  {
1397  this->_M_impl.construct(this->_M_impl._M_finish._M_cur, __x);
1398  ++this->_M_impl._M_finish._M_cur;
1399  }
1400  else
1401  _M_push_back_aux(__x);
1402  }
1403 
1404 #if __cplusplus >= 201103L
1405  void
1406  push_back(value_type&& __x)
1407  { emplace_back(std::move(__x)); }
1408 
1409  template<typename... _Args>
1410  void
1411  emplace_back(_Args&&... __args);
1412 #endif
1413 
1414  /**
1415  * @brief Removes first element.
1416  *
1417  * This is a typical stack operation. It shrinks the %deque by one.
1418  *
1419  * Note that no data is returned, and if the first element's data is
1420  * needed, it should be retrieved before pop_front() is called.
1421  */
1422  void
1423  pop_front() _GLIBCXX_NOEXCEPT
1424  {
1425  if (this->_M_impl._M_start._M_cur
1426  != this->_M_impl._M_start._M_last - 1)
1427  {
1428  this->_M_impl.destroy(this->_M_impl._M_start._M_cur);
1429  ++this->_M_impl._M_start._M_cur;
1430  }
1431  else
1432  _M_pop_front_aux();
1433  }
1434 
1435  /**
1436  * @brief Removes last element.
1437  *
1438  * This is a typical stack operation. It shrinks the %deque by one.
1439  *
1440  * Note that no data is returned, and if the last element's data is
1441  * needed, it should be retrieved before pop_back() is called.
1442  */
1443  void
1444  pop_back() _GLIBCXX_NOEXCEPT
1445  {
1446  if (this->_M_impl._M_finish._M_cur
1447  != this->_M_impl._M_finish._M_first)
1448  {
1449  --this->_M_impl._M_finish._M_cur;
1450  this->_M_impl.destroy(this->_M_impl._M_finish._M_cur);
1451  }
1452  else
1453  _M_pop_back_aux();
1454  }
1455 
1456 #if __cplusplus >= 201103L
1457  /**
1458  * @brief Inserts an object in %deque before specified iterator.
1459  * @param __position A const_iterator into the %deque.
1460  * @param __args Arguments.
1461  * @return An iterator that points to the inserted data.
1462  *
1463  * This function will insert an object of type T constructed
1464  * with T(std::forward<Args>(args)...) before the specified location.
1465  */
1466  template<typename... _Args>
1467  iterator
1468  emplace(const_iterator __position, _Args&&... __args);
1469 
1470  /**
1471  * @brief Inserts given value into %deque before specified iterator.
1472  * @param __position A const_iterator into the %deque.
1473  * @param __x Data to be inserted.
1474  * @return An iterator that points to the inserted data.
1475  *
1476  * This function will insert a copy of the given value before the
1477  * specified location.
1478  */
1479  iterator
1480  insert(const_iterator __position, const value_type& __x);
1481 #else
1482  /**
1483  * @brief Inserts given value into %deque before specified iterator.
1484  * @param __position An iterator into the %deque.
1485  * @param __x Data to be inserted.
1486  * @return An iterator that points to the inserted data.
1487  *
1488  * This function will insert a copy of the given value before the
1489  * specified location.
1490  */
1491  iterator
1492  insert(iterator __position, const value_type& __x);
1493 #endif
1494 
1495 #if __cplusplus >= 201103L
1496  /**
1497  * @brief Inserts given rvalue into %deque before specified iterator.
1498  * @param __position A const_iterator into the %deque.
1499  * @param __x Data to be inserted.
1500  * @return An iterator that points to the inserted data.
1501  *
1502  * This function will insert a copy of the given rvalue before the
1503  * specified location.
1504  */
1505  iterator
1506  insert(const_iterator __position, value_type&& __x)
1507  { return emplace(__position, std::move(__x)); }
1508 
1509  /**
1510  * @brief Inserts an initializer list into the %deque.
1511  * @param __p An iterator into the %deque.
1512  * @param __l An initializer_list.
1513  *
1514  * This function will insert copies of the data in the
1515  * initializer_list @a __l into the %deque before the location
1516  * specified by @a __p. This is known as <em>list insert</em>.
1517  */
1518  iterator
1520  { return this->insert(__p, __l.begin(), __l.end()); }
1521 #endif
1522 
1523 #if __cplusplus >= 201103L
1524  /**
1525  * @brief Inserts a number of copies of given data into the %deque.
1526  * @param __position A const_iterator into the %deque.
1527  * @param __n Number of elements to be inserted.
1528  * @param __x Data to be inserted.
1529  * @return An iterator that points to the inserted data.
1530  *
1531  * This function will insert a specified number of copies of the given
1532  * data before the location specified by @a __position.
1533  */
1534  iterator
1535  insert(const_iterator __position, size_type __n, const value_type& __x)
1536  {
1537  difference_type __offset = __position - cbegin();
1538  _M_fill_insert(__position._M_const_cast(), __n, __x);
1539  return begin() + __offset;
1540  }
1541 #else
1542  /**
1543  * @brief Inserts a number of copies of given data into the %deque.
1544  * @param __position An iterator into the %deque.
1545  * @param __n Number of elements to be inserted.
1546  * @param __x Data to be inserted.
1547  *
1548  * This function will insert a specified number of copies of the given
1549  * data before the location specified by @a __position.
1550  */
1551  void
1552  insert(iterator __position, size_type __n, const value_type& __x)
1553  { _M_fill_insert(__position, __n, __x); }
1554 #endif
1555 
1556 #if __cplusplus >= 201103L
1557  /**
1558  * @brief Inserts a range into the %deque.
1559  * @param __position A const_iterator into the %deque.
1560  * @param __first An input iterator.
1561  * @param __last An input iterator.
1562  * @return An iterator that points to the inserted data.
1563  *
1564  * This function will insert copies of the data in the range
1565  * [__first,__last) into the %deque before the location specified
1566  * by @a __position. This is known as <em>range insert</em>.
1567  */
1568  template<typename _InputIterator,
1569  typename = std::_RequireInputIter<_InputIterator>>
1570  iterator
1571  insert(const_iterator __position, _InputIterator __first,
1572  _InputIterator __last)
1573  {
1574  difference_type __offset = __position - cbegin();
1575  _M_insert_dispatch(__position._M_const_cast(),
1576  __first, __last, __false_type());
1577  return begin() + __offset;
1578  }
1579 #else
1580  /**
1581  * @brief Inserts a range into the %deque.
1582  * @param __position An iterator into the %deque.
1583  * @param __first An input iterator.
1584  * @param __last An input iterator.
1585  *
1586  * This function will insert copies of the data in the range
1587  * [__first,__last) into the %deque before the location specified
1588  * by @a __position. This is known as <em>range insert</em>.
1589  */
1590  template<typename _InputIterator>
1591  void
1592  insert(iterator __position, _InputIterator __first,
1593  _InputIterator __last)
1594  {
1595  // Check whether it's an integral type. If so, it's not an iterator.
1596  typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1597  _M_insert_dispatch(__position, __first, __last, _Integral());
1598  }
1599 #endif
1600 
1601  /**
1602  * @brief Remove element at given position.
1603  * @param __position Iterator pointing to element to be erased.
1604  * @return An iterator pointing to the next element (or end()).
1605  *
1606  * This function will erase the element at the given position and thus
1607  * shorten the %deque by one.
1608  *
1609  * The user is cautioned that
1610  * this function only erases the element, and that if the element is
1611  * itself a pointer, the pointed-to memory is not touched in any way.
1612  * Managing the pointer is the user's responsibility.
1613  */
1614  iterator
1615 #if __cplusplus >= 201103L
1616  erase(const_iterator __position)
1617 #else
1618  erase(iterator __position)
1619 #endif
1620  { return _M_erase(__position._M_const_cast()); }
1621 
1622  /**
1623  * @brief Remove a range of elements.
1624  * @param __first Iterator pointing to the first element to be erased.
1625  * @param __last Iterator pointing to one past the last element to be
1626  * erased.
1627  * @return An iterator pointing to the element pointed to by @a last
1628  * prior to erasing (or end()).
1629  *
1630  * This function will erase the elements in the range
1631  * [__first,__last) and shorten the %deque accordingly.
1632  *
1633  * The user is cautioned that
1634  * this function only erases the elements, and that if the elements
1635  * themselves are pointers, the pointed-to memory is not touched in any
1636  * way. Managing the pointer is the user's responsibility.
1637  */
1638  iterator
1639 #if __cplusplus >= 201103L
1640  erase(const_iterator __first, const_iterator __last)
1641 #else
1642  erase(iterator __first, iterator __last)
1643 #endif
1644  { return _M_erase(__first._M_const_cast(), __last._M_const_cast()); }
1645 
1646  /**
1647  * @brief Swaps data with another %deque.
1648  * @param __x A %deque of the same element and allocator types.
1649  *
1650  * This exchanges the elements between two deques in constant time.
1651  * (Four pointers, so it should be quite fast.)
1652  * Note that the global std::swap() function is specialized such that
1653  * std::swap(d1,d2) will feed to this function.
1654  */
1655  void
1656  swap(deque& __x) _GLIBCXX_NOEXCEPT
1657  {
1658  std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
1659  std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
1660  std::swap(this->_M_impl._M_map, __x._M_impl._M_map);
1661  std::swap(this->_M_impl._M_map_size, __x._M_impl._M_map_size);
1662 
1663  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1664  // 431. Swapping containers with unequal allocators.
1665  std::__alloc_swap<_Tp_alloc_type>::_S_do_it(_M_get_Tp_allocator(),
1666  __x._M_get_Tp_allocator());
1667  }
1668 
1669  /**
1670  * Erases all the elements. Note that this function only erases the
1671  * elements, and that if the elements themselves are pointers, the
1672  * pointed-to memory is not touched in any way. Managing the pointer is
1673  * the user's responsibility.
1674  */
1675  void
1676  clear() _GLIBCXX_NOEXCEPT
1677  { _M_erase_at_end(begin()); }
1678 
1679  protected:
1680  // Internal constructor functions follow.
1681 
1682  // called by the range constructor to implement [23.1.1]/9
1683 
1684  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1685  // 438. Ambiguity in the "do the right thing" clause
1686  template<typename _Integer>
1687  void
1688  _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1689  {
1690  _M_initialize_map(static_cast<size_type>(__n));
1691  _M_fill_initialize(__x);
1692  }
1693 
1694  // called by the range constructor to implement [23.1.1]/9
1695  template<typename _InputIterator>
1696  void
1697  _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1698  __false_type)
1699  {
1700  typedef typename std::iterator_traits<_InputIterator>::
1701  iterator_category _IterCategory;
1702  _M_range_initialize(__first, __last, _IterCategory());
1703  }
1704 
1705  // called by the second initialize_dispatch above
1706  //@{
1707  /**
1708  * @brief Fills the deque with whatever is in [first,last).
1709  * @param __first An input iterator.
1710  * @param __last An input iterator.
1711  * @return Nothing.
1712  *
1713  * If the iterators are actually forward iterators (or better), then the
1714  * memory layout can be done all at once. Else we move forward using
1715  * push_back on each value from the iterator.
1716  */
1717  template<typename _InputIterator>
1718  void
1719  _M_range_initialize(_InputIterator __first, _InputIterator __last,
1721 
1722  // called by the second initialize_dispatch above
1723  template<typename _ForwardIterator>
1724  void
1725  _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
1727  //@}
1728 
1729  /**
1730  * @brief Fills the %deque with copies of value.
1731  * @param __value Initial value.
1732  * @return Nothing.
1733  * @pre _M_start and _M_finish have already been initialized,
1734  * but none of the %deque's elements have yet been constructed.
1735  *
1736  * This function is called only when the user provides an explicit size
1737  * (with or without an explicit exemplar value).
1738  */
1739  void
1740  _M_fill_initialize(const value_type& __value);
1741 
1742 #if __cplusplus >= 201103L
1743  // called by deque(n).
1744  void
1745  _M_default_initialize();
1746 #endif
1747 
1748  // Internal assign functions follow. The *_aux functions do the actual
1749  // assignment work for the range versions.
1750 
1751  // called by the range assign to implement [23.1.1]/9
1752 
1753  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1754  // 438. Ambiguity in the "do the right thing" clause
1755  template<typename _Integer>
1756  void
1757  _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1758  { _M_fill_assign(__n, __val); }
1759 
1760  // called by the range assign to implement [23.1.1]/9
1761  template<typename _InputIterator>
1762  void
1763  _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1764  __false_type)
1765  {
1766  typedef typename std::iterator_traits<_InputIterator>::
1767  iterator_category _IterCategory;
1768  _M_assign_aux(__first, __last, _IterCategory());
1769  }
1770 
1771  // called by the second assign_dispatch above
1772  template<typename _InputIterator>
1773  void
1774  _M_assign_aux(_InputIterator __first, _InputIterator __last,
1776 
1777  // called by the second assign_dispatch above
1778  template<typename _ForwardIterator>
1779  void
1780  _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
1782  {
1783  const size_type __len = std::distance(__first, __last);
1784  if (__len > size())
1785  {
1786  _ForwardIterator __mid = __first;
1787  std::advance(__mid, size());
1788  std::copy(__first, __mid, begin());
1789  insert(end(), __mid, __last);
1790  }
1791  else
1792  _M_erase_at_end(std::copy(__first, __last, begin()));
1793  }
1794 
1795  // Called by assign(n,t), and the range assign when it turns out
1796  // to be the same thing.
1797  void
1798  _M_fill_assign(size_type __n, const value_type& __val)
1799  {
1800  if (__n > size())
1801  {
1802  std::fill(begin(), end(), __val);
1803  insert(end(), __n - size(), __val);
1804  }
1805  else
1806  {
1807  _M_erase_at_end(begin() + difference_type(__n));
1808  std::fill(begin(), end(), __val);
1809  }
1810  }
1811 
1812  //@{
1813  /// Helper functions for push_* and pop_*.
1814 #if __cplusplus < 201103L
1815  void _M_push_back_aux(const value_type&);
1816 
1817  void _M_push_front_aux(const value_type&);
1818 #else
1819  template<typename... _Args>
1820  void _M_push_back_aux(_Args&&... __args);
1821 
1822  template<typename... _Args>
1823  void _M_push_front_aux(_Args&&... __args);
1824 #endif
1825 
1826  void _M_pop_back_aux();
1827 
1828  void _M_pop_front_aux();
1829  //@}
1830 
1831  // Internal insert functions follow. The *_aux functions do the actual
1832  // insertion work when all shortcuts fail.
1833 
1834  // called by the range insert to implement [23.1.1]/9
1835 
1836  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1837  // 438. Ambiguity in the "do the right thing" clause
1838  template<typename _Integer>
1839  void
1840  _M_insert_dispatch(iterator __pos,
1841  _Integer __n, _Integer __x, __true_type)
1842  { _M_fill_insert(__pos, __n, __x); }
1843 
1844  // called by the range insert to implement [23.1.1]/9
1845  template<typename _InputIterator>
1846  void
1847  _M_insert_dispatch(iterator __pos,
1848  _InputIterator __first, _InputIterator __last,
1849  __false_type)
1850  {
1851  typedef typename std::iterator_traits<_InputIterator>::
1852  iterator_category _IterCategory;
1853  _M_range_insert_aux(__pos, __first, __last, _IterCategory());
1854  }
1855 
1856  // called by the second insert_dispatch above
1857  template<typename _InputIterator>
1858  void
1859  _M_range_insert_aux(iterator __pos, _InputIterator __first,
1860  _InputIterator __last, std::input_iterator_tag);
1861 
1862  // called by the second insert_dispatch above
1863  template<typename _ForwardIterator>
1864  void
1865  _M_range_insert_aux(iterator __pos, _ForwardIterator __first,
1866  _ForwardIterator __last, std::forward_iterator_tag);
1867 
1868  // Called by insert(p,n,x), and the range insert when it turns out to be
1869  // the same thing. Can use fill functions in optimal situations,
1870  // otherwise passes off to insert_aux(p,n,x).
1871  void
1872  _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
1873 
1874  // called by insert(p,x)
1875 #if __cplusplus < 201103L
1876  iterator
1877  _M_insert_aux(iterator __pos, const value_type& __x);
1878 #else
1879  template<typename... _Args>
1880  iterator
1881  _M_insert_aux(iterator __pos, _Args&&... __args);
1882 #endif
1883 
1884  // called by insert(p,n,x) via fill_insert
1885  void
1886  _M_insert_aux(iterator __pos, size_type __n, const value_type& __x);
1887 
1888  // called by range_insert_aux for forward iterators
1889  template<typename _ForwardIterator>
1890  void
1891  _M_insert_aux(iterator __pos,
1892  _ForwardIterator __first, _ForwardIterator __last,
1893  size_type __n);
1894 
1895 
1896  // Internal erase functions follow.
1897 
1898  void
1899  _M_destroy_data_aux(iterator __first, iterator __last);
1900 
1901  // Called by ~deque().
1902  // NB: Doesn't deallocate the nodes.
1903  template<typename _Alloc1>
1904  void
1905  _M_destroy_data(iterator __first, iterator __last, const _Alloc1&)
1906  { _M_destroy_data_aux(__first, __last); }
1907 
1908  void
1909  _M_destroy_data(iterator __first, iterator __last,
1910  const std::allocator<_Tp>&)
1911  {
1912  if (!__has_trivial_destructor(value_type))
1913  _M_destroy_data_aux(__first, __last);
1914  }
1915 
1916  // Called by erase(q1, q2).
1917  void
1918  _M_erase_at_begin(iterator __pos)
1919  {
1920  _M_destroy_data(begin(), __pos, _M_get_Tp_allocator());
1921  _M_destroy_nodes(this->_M_impl._M_start._M_node, __pos._M_node);
1922  this->_M_impl._M_start = __pos;
1923  }
1924 
1925  // Called by erase(q1, q2), resize(), clear(), _M_assign_aux,
1926  // _M_fill_assign, operator=.
1927  void
1928  _M_erase_at_end(iterator __pos)
1929  {
1930  _M_destroy_data(__pos, end(), _M_get_Tp_allocator());
1931  _M_destroy_nodes(__pos._M_node + 1,
1932  this->_M_impl._M_finish._M_node + 1);
1933  this->_M_impl._M_finish = __pos;
1934  }
1935 
1936  iterator
1937  _M_erase(iterator __pos);
1938 
1939  iterator
1940  _M_erase(iterator __first, iterator __last);
1941 
1942 #if __cplusplus >= 201103L
1943  // Called by resize(sz).
1944  void
1945  _M_default_append(size_type __n);
1946 
1947  bool
1948  _M_shrink_to_fit();
1949 #endif
1950 
1951  //@{
1952  /// Memory-handling helpers for the previous internal insert functions.
1953  iterator
1955  {
1956  const size_type __vacancies = this->_M_impl._M_start._M_cur
1957  - this->_M_impl._M_start._M_first;
1958  if (__n > __vacancies)
1959  _M_new_elements_at_front(__n - __vacancies);
1960  return this->_M_impl._M_start - difference_type(__n);
1961  }
1962 
1963  iterator
1965  {
1966  const size_type __vacancies = (this->_M_impl._M_finish._M_last
1967  - this->_M_impl._M_finish._M_cur) - 1;
1968  if (__n > __vacancies)
1969  _M_new_elements_at_back(__n - __vacancies);
1970  return this->_M_impl._M_finish + difference_type(__n);
1971  }
1972 
1973  void
1974  _M_new_elements_at_front(size_type __new_elements);
1975 
1976  void
1977  _M_new_elements_at_back(size_type __new_elements);
1978  //@}
1979 
1980 
1981  //@{
1982  /**
1983  * @brief Memory-handling helpers for the major %map.
1984  *
1985  * Makes sure the _M_map has space for new nodes. Does not
1986  * actually add the nodes. Can invalidate _M_map pointers.
1987  * (And consequently, %deque iterators.)
1988  */
1989  void
1990  _M_reserve_map_at_back(size_type __nodes_to_add = 1)
1991  {
1992  if (__nodes_to_add + 1 > this->_M_impl._M_map_size
1993  - (this->_M_impl._M_finish._M_node - this->_M_impl._M_map))
1994  _M_reallocate_map(__nodes_to_add, false);
1995  }
1996 
1997  void
1998  _M_reserve_map_at_front(size_type __nodes_to_add = 1)
1999  {
2000  if (__nodes_to_add > size_type(this->_M_impl._M_start._M_node
2001  - this->_M_impl._M_map))
2002  _M_reallocate_map(__nodes_to_add, true);
2003  }
2004 
2005  void
2006  _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front);
2007  //@}
2008  };
2009 
2010 
2011  /**
2012  * @brief Deque equality comparison.
2013  * @param __x A %deque.
2014  * @param __y A %deque of the same type as @a __x.
2015  * @return True iff the size and elements of the deques are equal.
2016  *
2017  * This is an equivalence relation. It is linear in the size of the
2018  * deques. Deques are considered equivalent if their sizes are equal,
2019  * and if corresponding elements compare equal.
2020  */
2021  template<typename _Tp, typename _Alloc>
2022  inline bool
2023  operator==(const deque<_Tp, _Alloc>& __x,
2024  const deque<_Tp, _Alloc>& __y)
2025  { return __x.size() == __y.size()
2026  && std::equal(__x.begin(), __x.end(), __y.begin()); }
2027 
2028  /**
2029  * @brief Deque ordering relation.
2030  * @param __x A %deque.
2031  * @param __y A %deque of the same type as @a __x.
2032  * @return True iff @a x is lexicographically less than @a __y.
2033  *
2034  * This is a total ordering relation. It is linear in the size of the
2035  * deques. The elements must be comparable with @c <.
2036  *
2037  * See std::lexicographical_compare() for how the determination is made.
2038  */
2039  template<typename _Tp, typename _Alloc>
2040  inline bool
2041  operator<(const deque<_Tp, _Alloc>& __x,
2042  const deque<_Tp, _Alloc>& __y)
2043  { return std::lexicographical_compare(__x.begin(), __x.end(),
2044  __y.begin(), __y.end()); }
2045 
2046  /// Based on operator==
2047  template<typename _Tp, typename _Alloc>
2048  inline bool
2049  operator!=(const deque<_Tp, _Alloc>& __x,
2050  const deque<_Tp, _Alloc>& __y)
2051  { return !(__x == __y); }
2052 
2053  /// Based on operator<
2054  template<typename _Tp, typename _Alloc>
2055  inline bool
2056  operator>(const deque<_Tp, _Alloc>& __x,
2057  const deque<_Tp, _Alloc>& __y)
2058  { return __y < __x; }
2059 
2060  /// Based on operator<
2061  template<typename _Tp, typename _Alloc>
2062  inline bool
2063  operator<=(const deque<_Tp, _Alloc>& __x,
2064  const deque<_Tp, _Alloc>& __y)
2065  { return !(__y < __x); }
2066 
2067  /// Based on operator<
2068  template<typename _Tp, typename _Alloc>
2069  inline bool
2070  operator>=(const deque<_Tp, _Alloc>& __x,
2071  const deque<_Tp, _Alloc>& __y)
2072  { return !(__x < __y); }
2073 
2074  /// See std::deque::swap().
2075  template<typename _Tp, typename _Alloc>
2076  inline void
2078  { __x.swap(__y); }
2079 
2080 #undef _GLIBCXX_DEQUE_BUF_SIZE
2081 
2082 _GLIBCXX_END_NAMESPACE_CONTAINER
2083 } // namespace std
2084 
2085 #endif /* _STL_DEQUE_H */
const_reverse_iterator rend() const noexcept
Definition: stl_deque.h:1098
complex< _Tp > operator+(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x plus y.
Definition: complex:321
iterator insert(const_iterator __position, value_type &&__x)
Inserts given rvalue into deque before specified iterator.
Definition: stl_deque.h:1506
reference back() noexcept
Definition: stl_deque.h:1331
size_type size() const noexcept
Definition: stl_deque.h:1141
void _M_pop_back_aux()
Helper functions for push_* and pop_*.
Definition: deque.tcc:510
iterator begin() noexcept
Definition: stl_deque.h:1036
void shrink_to_fit() noexcept
Definition: stl_deque.h:1218
void push_back(const value_type &__x)
Add data to the end of the deque.
Definition: stl_deque.h:1392
void _M_reserve_map_at_front(size_type __nodes_to_add=1)
Memory-handling helpers for the major map.
Definition: stl_deque.h:1998
iterator _M_reserve_elements_at_back(size_type __n)
Memory-handling helpers for the previous internal insert functions.
Definition: stl_deque.h:1964
const_iterator cend() const noexcept
Definition: stl_deque.h:1116
Forward iterators support a superset of input iterator operations.
reverse_iterator rend() noexcept
Definition: stl_deque.h:1089
reference operator[](size_type __n) noexcept
Subscript access to the data contained in the deque.
Definition: stl_deque.h:1243
deque(initializer_list< value_type > __l, const allocator_type &__a=allocator_type())
Builds a deque from an initializer list.
Definition: stl_deque.h:868
deque(deque &&__x)
Deque move constructor.
Definition: stl_deque.h:854
reference front() noexcept
Definition: stl_deque.h:1315
const_reference back() const noexcept
Definition: stl_deque.h:1343
A deque::iterator.
Definition: stl_deque.h:106
const_iterator begin() const noexcept
Definition: stl_deque.h:1044
const_reverse_iterator crend() const noexcept
Definition: stl_deque.h:1134
deque(_InputIterator __first, _InputIterator __last, const allocator_type &__a=allocator_type())
Builds a deque from a range.
Definition: stl_deque.h:895
allocator_type get_allocator() const noexcept
Get a copy of the memory allocation object.
Definition: stl_deque.h:1027
void _M_reserve_map_at_back(size_type __nodes_to_add=1)
Memory-handling helpers for the major map.
Definition: stl_deque.h:1990
Random-access iterators support a superset of bidirectional iterator operations.
iterator insert(const_iterator __position, const value_type &__x)
Inserts given value into deque before specified iterator.
iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
~deque() noexcept
Definition: stl_deque.h:916
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:813
iterator emplace(const_iterator __position, _Args &&...__args)
Inserts an object in deque before specified iterator.
reference at(size_type __n)
Provides access to the data contained in the deque.
Definition: stl_deque.h:1286
void _M_set_node(_Map_pointer __new_node) noexcept
Definition: stl_deque.h:238
const_iterator cbegin() const noexcept
Definition: stl_deque.h:1107
void assign(initializer_list< value_type > __l)
Assigns an initializer list to a deque.
Definition: stl_deque.h:1021
iterator insert(const_iterator __p, initializer_list< value_type > __l)
Inserts an initializer list into the deque.
Definition: stl_deque.h:1519
void clear() noexcept
Definition: stl_deque.h:1676
Marking input iterators.
const _Tp & max(const _Tp &, const _Tp &)
This does what you think it does.
Definition: stl_algobase.h:217
void _M_new_elements_at_front(size_type __new_elements)
Memory-handling helpers for the previous internal insert functions.
Definition: deque.tcc:829
deque & operator=(deque &&__x) noexcept
Deque move assignment operator.
Definition: stl_deque.h:938
deque & operator=(initializer_list< value_type > __l)
Assigns an initializer list to a deque.
Definition: stl_deque.h:959
void _M_range_initialize(_InputIterator __first, _InputIterator __last, std::input_iterator_tag)
Fills the deque with whatever is in [first,last).
Definition: deque.tcc:382
void _M_initialize_map(size_t)
Layout storage.
Definition: stl_deque.h:587
void _M_new_elements_at_back(size_type __new_elements)
Memory-handling helpers for the previous internal insert functions.
Definition: deque.tcc:854
bool equal(_II1 __first1, _II1 __last1, _II2 __first2)
Tests a range for element-wise equality.
void pop_back() noexcept
Removes last element.
Definition: stl_deque.h:1444
iterator end() noexcept
Definition: stl_deque.h:1053
const_reverse_iterator rbegin() const noexcept
Definition: stl_deque.h:1080
void _M_pop_front_aux()
Helper functions for push_* and pop_*.
Definition: deque.tcc:525
const_reference front() const noexcept
Definition: stl_deque.h:1323
void resize(size_type __new_size)
Resizes the deque to the specified number of elements.
Definition: stl_deque.h:1160
#define _GLIBCXX_DEQUE_BUF_SIZE
This function controls the size of memory nodes.
Definition: stl_deque.h:85
deque & operator=(const deque &__x)
Deque assignment operator.
Definition: deque.tcc:93
void _M_fill_initialize(const value_type &__value)
Fills the deque with copies of value.
Definition: deque.tcc:356
reverse_iterator rbegin() noexcept
Definition: stl_deque.h:1071
void swap(deque &__x) noexcept
Swaps data with another deque.
Definition: stl_deque.h:1656
size_type max_size() const noexcept
Definition: stl_deque.h:1146
iterator _M_reserve_elements_at_front(size_type __n)
Memory-handling helpers for the previous internal insert functions.
Definition: stl_deque.h:1954
complex< _Tp > operator-(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x minus y.
Definition: complex:351
void _M_range_check(size_type __n) const
Safety check used only from at().
Definition: stl_deque.h:1264
void advance(_InputIterator &__i, _Distance __n)
A generalization of pointer arithmetic.
The standard allocator, as per [20.4].
Definition: allocator.h:92
void pop_front() noexcept
Removes first element.
Definition: stl_deque.h:1423
void resize(size_type __new_size, const value_type &__x)
Resizes the deque to the specified number of elements.
Definition: stl_deque.h:1182
const_reference at(size_type __n) const
Provides access to the data contained in the deque.
Definition: stl_deque.h:1304
void swap(function< _Res(_Args...)> &__x, function< _Res(_Args...)> &__y)
Swap the targets of two polymorphic function object wrappers.
Definition: functional:2534
Common iterator class.
void _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front)
Memory-handling helpers for the major map.
Definition: deque.tcc:879
initializer_list
void _M_push_back_aux(_Args &&...__args)
Helper functions for push_* and pop_*.
void _M_push_front_aux(_Args &&...__args)
Helper functions for push_* and pop_*.
const_reverse_iterator crbegin() const noexcept
Definition: stl_deque.h:1125
deque(const deque &__x)
Deque copy constructor.
Definition: stl_deque.h:840
void assign(size_type __n, const value_type &__val)
Assigns a given value to a deque.
Definition: stl_deque.h:977
void assign(_InputIterator __first, _InputIterator __last)
Assigns a range to a deque.
Definition: stl_deque.h:996
bool lexicographical_compare(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2, _Compare __comp)
Performs dictionary comparison on ranges.
iterator insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
Inserts a range into the deque.
Definition: stl_deque.h:1571
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:1535
void push_front(const value_type &__x)
Add data to the front of the deque.
Definition: stl_deque.h:1361
const_iterator end() const noexcept
Definition: stl_deque.h:1062
A standard container using fixed-size memory allocation and constant-time manipulation of elements at...
Definition: stl_deque.h:735
bool empty() const noexcept
Definition: stl_deque.h:1227
const_reference operator[](size_type __n) const noexcept
Subscript access to the data contained in the deque.
Definition: stl_deque.h:1258