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