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