OSDN Git Service

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