OSDN Git Service

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