OSDN Git Service

2005-08-17 Kelley Cook <kcook@gcc.gnu.org>
[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 namespace _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 _RefL, typename _PtrL,
325            typename _RefR, typename _PtrR>
326     inline typename _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type
327     operator-(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
328               const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
329     {
330       return typename _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type
331         (_Deque_iterator<_Tp, _RefL, _PtrL>::_S_buffer_size())
332         * (__x._M_node - __y._M_node - 1) + (__x._M_cur - __x._M_first)
333         + (__y._M_last - __y._M_cur);
334     }
335
336   template<typename _Tp, typename _Ref, typename _Ptr>
337     inline _Deque_iterator<_Tp, _Ref, _Ptr>
338     operator+(ptrdiff_t __n, const _Deque_iterator<_Tp, _Ref, _Ptr>& __x)
339     { return __x + __n; }
340
341   /**
342    *  @if maint
343    *  Deque base class.  This class provides the unified face for %deque's
344    *  allocation.  This class's constructor and destructor allocate and
345    *  deallocate (but do not initialize) storage.  This makes %exception
346    *  safety easier.
347    *
348    *  Nothing in this class ever constructs or destroys an actual Tp element.
349    *  (Deque handles that itself.)  Only/All memory management is performed
350    *  here.
351    *  @endif
352   */
353   template<typename _Tp, typename _Alloc>
354     class _Deque_base
355     {
356     public:
357       typedef _Alloc                  allocator_type;
358
359       allocator_type
360       get_allocator() const
361       { return _M_get_Tp_allocator(); }
362
363       typedef _Deque_iterator<_Tp, _Tp&, _Tp*>             iterator;
364       typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
365
366       _Deque_base(const allocator_type& __a, size_t __num_elements)
367       : _M_impl(__a)
368       { _M_initialize_map(__num_elements); }
369
370       _Deque_base(const allocator_type& __a)
371       : _M_impl(__a)
372       { }
373
374       ~_Deque_base();
375
376     protected:
377       //This struct encapsulates the implementation of the std::deque
378       //standard container and at the same time makes use of the EBO
379       //for empty allocators.
380       typedef typename _Alloc::template rebind<_Tp*>::other _Map_alloc_type;
381
382       typedef typename _Alloc::template rebind<_Tp>::other  _Tp_alloc_type;
383
384       struct _Deque_impl
385       : public _Tp_alloc_type
386       {
387         _Tp** _M_map;
388         size_t _M_map_size;
389         iterator _M_start;
390         iterator _M_finish;
391
392         _Deque_impl(const _Tp_alloc_type& __a)
393         : _Tp_alloc_type(__a), _M_map(0), _M_map_size(0),
394           _M_start(), _M_finish()
395         { }
396       };
397
398       _Tp_alloc_type
399       _M_get_Tp_allocator() const
400       { return *static_cast<const _Tp_alloc_type*>(&this->_M_impl); }
401
402       _Map_alloc_type
403       _M_get_map_allocator() const
404       { return _M_get_Tp_allocator(); }
405
406       _Tp*
407       _M_allocate_node()
408       { 
409         return _M_impl._Tp_alloc_type::allocate(__deque_buf_size(sizeof(_Tp)));
410       }
411
412       void
413       _M_deallocate_node(_Tp* __p)
414       {
415         _M_impl._Tp_alloc_type::deallocate(__p, __deque_buf_size(sizeof(_Tp)));
416       }
417
418       _Tp**
419       _M_allocate_map(size_t __n)
420       { return _M_get_map_allocator().allocate(__n); }
421
422       void
423       _M_deallocate_map(_Tp** __p, size_t __n)
424       { _M_get_map_allocator().deallocate(__p, __n); }
425
426     protected:
427       void _M_initialize_map(size_t);
428       void _M_create_nodes(_Tp** __nstart, _Tp** __nfinish);
429       void _M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish);
430       enum { _S_initial_map_size = 8 };
431
432       _Deque_impl _M_impl;
433     };
434
435   template<typename _Tp, typename _Alloc>
436     _Deque_base<_Tp, _Alloc>::
437     ~_Deque_base()
438     {
439       if (this->_M_impl._M_map)
440         {
441           _M_destroy_nodes(this->_M_impl._M_start._M_node,
442                            this->_M_impl._M_finish._M_node + 1);
443           _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
444         }
445     }
446
447   /**
448    *  @if maint
449    *  @brief Layout storage.
450    *  @param  num_elements  The count of T's for which to allocate space
451    *                        at first.
452    *  @return   Nothing.
453    *
454    *  The initial underlying memory layout is a bit complicated...
455    *  @endif
456   */
457   template<typename _Tp, typename _Alloc>
458     void
459     _Deque_base<_Tp, _Alloc>::
460     _M_initialize_map(size_t __num_elements)
461     {
462       const size_t __num_nodes = (__num_elements/ __deque_buf_size(sizeof(_Tp))
463                                   + 1);
464
465       this->_M_impl._M_map_size = std::max((size_t) _S_initial_map_size,
466                                            size_t(__num_nodes + 2));
467       this->_M_impl._M_map = _M_allocate_map(this->_M_impl._M_map_size);
468
469       // For "small" maps (needing less than _M_map_size nodes), allocation
470       // starts in the middle elements and grows outwards.  So nstart may be
471       // the beginning of _M_map, but for small maps it may be as far in as
472       // _M_map+3.
473
474       _Tp** __nstart = (this->_M_impl._M_map
475                         + (this->_M_impl._M_map_size - __num_nodes) / 2);
476       _Tp** __nfinish = __nstart + __num_nodes;
477
478       try
479         { _M_create_nodes(__nstart, __nfinish); }
480       catch(...)
481         {
482           _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
483           this->_M_impl._M_map = 0;
484           this->_M_impl._M_map_size = 0;
485           __throw_exception_again;
486         }
487
488       this->_M_impl._M_start._M_set_node(__nstart);
489       this->_M_impl._M_finish._M_set_node(__nfinish - 1);
490       this->_M_impl._M_start._M_cur = _M_impl._M_start._M_first;
491       this->_M_impl._M_finish._M_cur = (this->_M_impl._M_finish._M_first
492                                         + __num_elements
493                                         % __deque_buf_size(sizeof(_Tp)));
494     }
495
496   template<typename _Tp, typename _Alloc>
497     void
498     _Deque_base<_Tp, _Alloc>::
499     _M_create_nodes(_Tp** __nstart, _Tp** __nfinish)
500     {
501       _Tp** __cur;
502       try
503         {
504           for (__cur = __nstart; __cur < __nfinish; ++__cur)
505             *__cur = this->_M_allocate_node();
506         }
507       catch(...)
508         {
509           _M_destroy_nodes(__nstart, __cur);
510           __throw_exception_again;
511         }
512     }
513
514   template<typename _Tp, typename _Alloc>
515     void
516     _Deque_base<_Tp, _Alloc>::
517     _M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish)
518     {
519       for (_Tp** __n = __nstart; __n < __nfinish; ++__n)
520         _M_deallocate_node(*__n);
521     }
522
523   /**
524    *  @brief  A standard container using fixed-size memory allocation and
525    *  constant-time manipulation of elements at either end.
526    *
527    *  @ingroup Containers
528    *  @ingroup Sequences
529    *
530    *  Meets the requirements of a <a href="tables.html#65">container</a>, a
531    *  <a href="tables.html#66">reversible container</a>, and a
532    *  <a href="tables.html#67">sequence</a>, including the
533    *  <a href="tables.html#68">optional sequence requirements</a>.
534    *
535    *  In previous HP/SGI versions of deque, there was an extra template
536    *  parameter so users could control the node size.  This extension turned
537    *  out to violate the C++ standard (it can be detected using template
538    *  template parameters), and it was removed.
539    *
540    *  @if maint
541    *  Here's how a deque<Tp> manages memory.  Each deque has 4 members:
542    *
543    *  - Tp**        _M_map
544    *  - size_t      _M_map_size
545    *  - iterator    _M_start, _M_finish
546    *
547    *  map_size is at least 8.  %map is an array of map_size
548    *  pointers-to-"nodes".  (The name %map has nothing to do with the
549    *  std::map class, and "nodes" should not be confused with
550    *  std::list's usage of "node".)
551    *
552    *  A "node" has no specific type name as such, but it is referred
553    *  to as "node" in this file.  It is a simple array-of-Tp.  If Tp
554    *  is very large, there will be one Tp element per node (i.e., an
555    *  "array" of one).  For non-huge Tp's, node size is inversely
556    *  related to Tp size: the larger the Tp, the fewer Tp's will fit
557    *  in a node.  The goal here is to keep the total size of a node
558    *  relatively small and constant over different Tp's, to improve
559    *  allocator efficiency.
560    *
561    *  Not every pointer in the %map array will point to a node.  If
562    *  the initial number of elements in the deque is small, the
563    *  /middle/ %map pointers will be valid, and the ones at the edges
564    *  will be unused.  This same situation will arise as the %map
565    *  grows: available %map pointers, if any, will be on the ends.  As
566    *  new nodes are created, only a subset of the %map's pointers need
567    *  to be copied "outward".
568    *
569    *  Class invariants:
570    * - For any nonsingular iterator i:
571    *    - i.node points to a member of the %map array.  (Yes, you read that
572    *      correctly:  i.node does not actually point to a node.)  The member of
573    *      the %map array is what actually points to the node.
574    *    - i.first == *(i.node)    (This points to the node (first Tp element).)
575    *    - i.last  == i.first + node_size
576    *    - i.cur is a pointer in the range [i.first, i.last).  NOTE:
577    *      the implication of this is that i.cur is always a dereferenceable
578    *      pointer, even if i is a past-the-end iterator.
579    * - Start and Finish are always nonsingular iterators.  NOTE: this
580    * means that an empty deque must have one node, a deque with <N
581    * elements (where N is the node buffer size) must have one node, a
582    * deque with N through (2N-1) elements must have two nodes, etc.
583    * - For every node other than start.node and finish.node, every
584    * element in the node is an initialized object.  If start.node ==
585    * finish.node, then [start.cur, finish.cur) are initialized
586    * objects, and the elements outside that range are uninitialized
587    * storage.  Otherwise, [start.cur, start.last) and [finish.first,
588    * finish.cur) are initialized objects, and [start.first, start.cur)
589    * and [finish.cur, finish.last) are uninitialized storage.
590    * - [%map, %map + map_size) is a valid, non-empty range.
591    * - [start.node, finish.node] is a valid range contained within
592    *   [%map, %map + map_size).
593    * - A pointer in the range [%map, %map + map_size) points to an allocated
594    *   node if and only if the pointer is in the range
595    *   [start.node, finish.node].
596    *
597    *  Here's the magic:  nothing in deque is "aware" of the discontiguous
598    *  storage!
599    *
600    *  The memory setup and layout occurs in the parent, _Base, and the iterator
601    *  class is entirely responsible for "leaping" from one node to the next.
602    *  All the implementation routines for deque itself work only through the
603    *  start and finish iterators.  This keeps the routines simple and sane,
604    *  and we can use other standard algorithms as well.
605    *  @endif
606   */
607   template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
608     class deque : protected _Deque_base<_Tp, _Alloc>
609     {
610       // concept requirements
611       typedef typename _Alloc::value_type        _Alloc_value_type;
612       __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
613       __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
614
615       typedef _Deque_base<_Tp, _Alloc>           _Base;
616       typedef typename _Base::_Tp_alloc_type     _Tp_alloc_type;
617
618     public:
619       typedef _Tp                                        value_type;
620       typedef typename _Tp_alloc_type::pointer           pointer;
621       typedef typename _Tp_alloc_type::const_pointer     const_pointer;
622       typedef typename _Tp_alloc_type::reference         reference;
623       typedef typename _Tp_alloc_type::const_reference   const_reference;
624       typedef typename _Base::iterator                   iterator;
625       typedef typename _Base::const_iterator             const_iterator;
626       typedef std::reverse_iterator<const_iterator>      const_reverse_iterator;
627       typedef std::reverse_iterator<iterator>            reverse_iterator;
628       typedef size_t                             size_type;
629       typedef ptrdiff_t                          difference_type;
630       typedef _Alloc                             allocator_type;
631
632     protected:
633       typedef pointer*                           _Map_pointer;
634
635       static size_t _S_buffer_size()
636       { return __deque_buf_size(sizeof(_Tp)); }
637
638       // Functions controlling memory layout, and nothing else.
639       using _Base::_M_initialize_map;
640       using _Base::_M_create_nodes;
641       using _Base::_M_destroy_nodes;
642       using _Base::_M_allocate_node;
643       using _Base::_M_deallocate_node;
644       using _Base::_M_allocate_map;
645       using _Base::_M_deallocate_map;
646       using _Base::_M_get_Tp_allocator;
647
648       /** @if maint
649        *  A total of four data members accumulated down the heirarchy.
650        *  May be accessed via _M_impl.*
651        *  @endif
652        */
653       using _Base::_M_impl;
654
655     public:
656       // [23.2.1.1] construct/copy/destroy
657       // (assign() and get_allocator() are also listed in this section)
658       /**
659        *  @brief  Default constructor creates no elements.
660        */
661       explicit
662       deque(const allocator_type& __a = allocator_type())
663       : _Base(__a, 0) {}
664
665       /**
666        *  @brief  Create a %deque with copies of an exemplar element.
667        *  @param  n  The number of elements to initially create.
668        *  @param  value  An element to copy.
669        *
670        *  This constructor fills the %deque with @a n copies of @a value.
671        */
672       explicit
673       deque(size_type __n, const value_type& __value = value_type(),
674             const allocator_type& __a = allocator_type())
675       : _Base(__a, __n)
676       { _M_fill_initialize(__value); }
677
678       /**
679        *  @brief  %Deque copy constructor.
680        *  @param  x  A %deque of identical element and allocator types.
681        *
682        *  The newly-created %deque uses a copy of the allocation object used
683        *  by @a x.
684        */
685       deque(const deque& __x)
686       : _Base(__x.get_allocator(), __x.size())
687       { std::__uninitialized_copy_a(__x.begin(), __x.end(), 
688                                     this->_M_impl._M_start,
689                                     _M_get_Tp_allocator()); }
690
691       /**
692        *  @brief  Builds a %deque from a range.
693        *  @param  first  An input iterator.
694        *  @param  last  An input iterator.
695        *
696        *  Create a %deque consisting of copies of the elements from [first,
697        *  last).
698        *
699        *  If the iterators are forward, bidirectional, or random-access, then
700        *  this will call the elements' copy constructor N times (where N is
701        *  distance(first,last)) and do no memory reallocation.  But if only
702        *  input iterators are used, then this will do at most 2N calls to the
703        *  copy constructor, and logN memory reallocations.
704        */
705       template<typename _InputIterator>
706         deque(_InputIterator __first, _InputIterator __last,
707               const allocator_type& __a = allocator_type())
708         : _Base(__a)
709         {
710           // Check whether it's an integral type.  If so, it's not an iterator.
711           typedef typename std::__is_integer<_InputIterator>::__type _Integral;
712           _M_initialize_dispatch(__first, __last, _Integral());
713         }
714
715       /**
716        *  The dtor only erases the elements, and note that if the elements
717        *  themselves are pointers, the pointed-to memory is not touched in any
718        *  way.  Managing the pointer is the user's responsibilty.
719        */
720       ~deque()
721       { std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
722                       _M_get_Tp_allocator()); }
723
724       /**
725        *  @brief  %Deque assignment operator.
726        *  @param  x  A %deque of identical element and allocator types.
727        *
728        *  All the elements of @a x are copied, but unlike the copy constructor,
729        *  the allocator object is not copied.
730        */
731       deque&
732       operator=(const deque& __x);
733
734       /**
735        *  @brief  Assigns a given value to a %deque.
736        *  @param  n  Number of elements to be assigned.
737        *  @param  val  Value to be assigned.
738        *
739        *  This function fills a %deque with @a n copies of the given
740        *  value.  Note that the assignment completely changes the
741        *  %deque and that the resulting %deque's size is the same as
742        *  the number of elements assigned.  Old data may be lost.
743        */
744       void
745       assign(size_type __n, const value_type& __val)
746       { _M_fill_assign(__n, __val); }
747
748       /**
749        *  @brief  Assigns a range to a %deque.
750        *  @param  first  An input iterator.
751        *  @param  last   An input iterator.
752        *
753        *  This function fills a %deque with copies of the elements in the
754        *  range [first,last).
755        *
756        *  Note that the assignment completely changes the %deque and that the
757        *  resulting %deque's size is the same as the number of elements
758        *  assigned.  Old data may be lost.
759        */
760       template<typename _InputIterator>
761         void
762         assign(_InputIterator __first, _InputIterator __last)
763         {
764           typedef typename std::__is_integer<_InputIterator>::__type _Integral;
765           _M_assign_dispatch(__first, __last, _Integral());
766         }
767
768       /// Get a copy of the memory allocation object.
769       allocator_type
770       get_allocator() const
771       { return _Base::get_allocator(); }
772
773       // iterators
774       /**
775        *  Returns a read/write iterator that points to the first element in the
776        *  %deque.  Iteration is done in ordinary element order.
777        */
778       iterator
779       begin()
780       { return this->_M_impl._M_start; }
781
782       /**
783        *  Returns a read-only (constant) iterator that points to the first
784        *  element in the %deque.  Iteration is done in ordinary element order.
785        */
786       const_iterator
787       begin() const
788       { return this->_M_impl._M_start; }
789
790       /**
791        *  Returns a read/write iterator that points one past the last
792        *  element in the %deque.  Iteration is done in ordinary
793        *  element order.
794        */
795       iterator
796       end()
797       { return this->_M_impl._M_finish; }
798
799       /**
800        *  Returns a read-only (constant) iterator that points one past
801        *  the last element in the %deque.  Iteration is done in
802        *  ordinary element order.
803        */
804       const_iterator
805       end() const
806       { return this->_M_impl._M_finish; }
807
808       /**
809        *  Returns a read/write reverse iterator that points to the
810        *  last element in the %deque.  Iteration is done in reverse
811        *  element order.
812        */
813       reverse_iterator
814       rbegin()
815       { return reverse_iterator(this->_M_impl._M_finish); }
816
817       /**
818        *  Returns a read-only (constant) reverse iterator that points
819        *  to the last element in the %deque.  Iteration is done in
820        *  reverse element order.
821        */
822       const_reverse_iterator
823       rbegin() const
824       { return const_reverse_iterator(this->_M_impl._M_finish); }
825
826       /**
827        *  Returns a read/write reverse iterator that points to one
828        *  before the first element in the %deque.  Iteration is done
829        *  in reverse element order.
830        */
831       reverse_iterator
832       rend() { return reverse_iterator(this->_M_impl._M_start); }
833
834       /**
835        *  Returns a read-only (constant) reverse iterator that points
836        *  to one before the first element in the %deque.  Iteration is
837        *  done in reverse element order.
838        */
839       const_reverse_iterator
840       rend() const
841       { return const_reverse_iterator(this->_M_impl._M_start); }
842
843       // [23.2.1.2] capacity
844       /**  Returns the number of elements in the %deque.  */
845       size_type
846       size() const
847       { return this->_M_impl._M_finish - this->_M_impl._M_start; }
848
849       /**  Returns the size() of the largest possible %deque.  */
850       size_type
851       max_size() const
852       { return size_type(-1); }
853
854       /**
855        *  @brief  Resizes the %deque to the specified number of elements.
856        *  @param  new_size  Number of elements the %deque should contain.
857        *  @param  x  Data with which new elements should be populated.
858        *
859        *  This function will %resize the %deque to the specified
860        *  number of elements.  If the number is smaller than the
861        *  %deque's current size the %deque is truncated, otherwise the
862        *  %deque is extended and new elements are populated with given
863        *  data.
864        */
865       void
866       resize(size_type __new_size, value_type __x = value_type())
867       {
868         const size_type __len = size();
869         if (__new_size < __len)
870           erase(this->_M_impl._M_start + __new_size, this->_M_impl._M_finish);
871         else
872           insert(this->_M_impl._M_finish, __new_size - __len, __x);
873       }
874
875       /**
876        *  Returns true if the %deque is empty.  (Thus begin() would
877        *  equal end().)
878        */
879       bool
880       empty() const
881       { return this->_M_impl._M_finish == this->_M_impl._M_start; }
882
883       // element access
884       /**
885        *  @brief Subscript access to the data contained in the %deque.
886        *  @param n The index of the element for which data should be
887        *  accessed.
888        *  @return  Read/write reference to data.
889        *
890        *  This operator allows for easy, array-style, data access.
891        *  Note that data access with this operator is unchecked and
892        *  out_of_range lookups are not defined. (For checked lookups
893        *  see at().)
894        */
895       reference
896       operator[](size_type __n)
897       { return this->_M_impl._M_start[difference_type(__n)]; }
898
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-only (constant) 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       const_reference
911       operator[](size_type __n) const
912       { return this->_M_impl._M_start[difference_type(__n)]; }
913
914     protected:
915       /// @if maint Safety check used only from at().  @endif
916       void
917       _M_range_check(size_type __n) const
918       {
919         if (__n >= this->size())
920           __throw_out_of_range(__N("deque::_M_range_check"));
921       }
922
923     public:
924       /**
925        *  @brief  Provides access to the data contained in the %deque.
926        *  @param n The index of the element for which data should be
927        *  accessed.
928        *  @return  Read/write reference to data.
929        *  @throw  std::out_of_range  If @a n is an invalid index.
930        *
931        *  This function provides for safer data access.  The parameter
932        *  is first checked that it is in the range of the deque.  The
933        *  function throws out_of_range if the check fails.
934        */
935       reference
936       at(size_type __n)
937       {
938         _M_range_check(__n);
939         return (*this)[__n];
940       }
941
942       /**
943        *  @brief  Provides access to the data contained in the %deque.
944        *  @param n The index of the element for which data should be
945        *  accessed.
946        *  @return  Read-only (constant) reference to data.
947        *  @throw  std::out_of_range  If @a n is an invalid index.
948        *
949        *  This function provides for safer data access.  The parameter is first
950        *  checked that it is in the range of the deque.  The function throws
951        *  out_of_range if the check fails.
952        */
953       const_reference
954       at(size_type __n) const
955       {
956         _M_range_check(__n);
957         return (*this)[__n];
958       }
959
960       /**
961        *  Returns a read/write reference to the data at the first
962        *  element of the %deque.
963        */
964       reference
965       front()
966       { return *begin(); }
967
968       /**
969        *  Returns a read-only (constant) reference to the data at the first
970        *  element of the %deque.
971        */
972       const_reference
973       front() const
974       { return *begin(); }
975
976       /**
977        *  Returns a read/write reference to the data at the last element of the
978        *  %deque.
979        */
980       reference
981       back()
982       {
983         iterator __tmp = end();
984         --__tmp;
985         return *__tmp;
986       }
987
988       /**
989        *  Returns a read-only (constant) reference to the data at the last
990        *  element of the %deque.
991        */
992       const_reference
993       back() const
994       {
995         const_iterator __tmp = end();
996         --__tmp;
997         return *__tmp;
998       }
999
1000       // [23.2.1.2] modifiers
1001       /**
1002        *  @brief  Add data to the front of the %deque.
1003        *  @param  x  Data to be added.
1004        *
1005        *  This is a typical stack operation.  The function creates an
1006        *  element at the front of the %deque and assigns the given
1007        *  data to it.  Due to the nature of a %deque this operation
1008        *  can be done in constant time.
1009        */
1010       void
1011       push_front(const value_type& __x)
1012       {
1013         if (this->_M_impl._M_start._M_cur != this->_M_impl._M_start._M_first)
1014           {
1015             this->_M_impl.construct(this->_M_impl._M_start._M_cur - 1, __x);
1016             --this->_M_impl._M_start._M_cur;
1017           }
1018         else
1019           _M_push_front_aux(__x);
1020       }
1021
1022       /**
1023        *  @brief  Add data to the end of the %deque.
1024        *  @param  x  Data to be added.
1025        *
1026        *  This is a typical stack operation.  The function creates an
1027        *  element at the end of the %deque and assigns the given data
1028        *  to it.  Due to the nature of a %deque this operation can be
1029        *  done in constant time.
1030        */
1031       void
1032       push_back(const value_type& __x)
1033       {
1034         if (this->_M_impl._M_finish._M_cur
1035             != this->_M_impl._M_finish._M_last - 1)
1036           {
1037             this->_M_impl.construct(this->_M_impl._M_finish._M_cur, __x);
1038             ++this->_M_impl._M_finish._M_cur;
1039           }
1040         else
1041           _M_push_back_aux(__x);
1042       }
1043
1044       /**
1045        *  @brief  Removes first element.
1046        *
1047        *  This is a typical stack operation.  It shrinks the %deque by one.
1048        *
1049        *  Note that no data is returned, and if the first element's data is
1050        *  needed, it should be retrieved before pop_front() is called.
1051        */
1052       void
1053       pop_front()
1054       {
1055         if (this->_M_impl._M_start._M_cur
1056             != this->_M_impl._M_start._M_last - 1)
1057           {
1058             this->_M_impl.destroy(this->_M_impl._M_start._M_cur);
1059             ++this->_M_impl._M_start._M_cur;
1060           }
1061         else
1062           _M_pop_front_aux();
1063       }
1064
1065       /**
1066        *  @brief  Removes last element.
1067        *
1068        *  This is a typical stack operation.  It shrinks the %deque by one.
1069        *
1070        *  Note that no data is returned, and if the last element's data is
1071        *  needed, it should be retrieved before pop_back() is called.
1072        */
1073       void
1074       pop_back()
1075       {
1076         if (this->_M_impl._M_finish._M_cur
1077             != this->_M_impl._M_finish._M_first)
1078           {
1079             --this->_M_impl._M_finish._M_cur;
1080             this->_M_impl.destroy(this->_M_impl._M_finish._M_cur);
1081           }
1082         else
1083           _M_pop_back_aux();
1084       }
1085
1086       /**
1087        *  @brief  Inserts given value into %deque before specified iterator.
1088        *  @param  position  An iterator into the %deque.
1089        *  @param  x  Data to be inserted.
1090        *  @return  An iterator that points to the inserted data.
1091        *
1092        *  This function will insert a copy of the given value before the
1093        *  specified location.
1094        */
1095       iterator
1096       insert(iterator position, const value_type& __x);
1097
1098       /**
1099        *  @brief  Inserts a number of copies of given data into the %deque.
1100        *  @param  position  An iterator into the %deque.
1101        *  @param  n  Number of elements to be inserted.
1102        *  @param  x  Data to be inserted.
1103        *
1104        *  This function will insert a specified number of copies of the given
1105        *  data before the location specified by @a position.
1106        */
1107       void
1108       insert(iterator __position, size_type __n, const value_type& __x)
1109       { _M_fill_insert(__position, __n, __x); }
1110
1111       /**
1112        *  @brief  Inserts a range into the %deque.
1113        *  @param  position  An iterator into the %deque.
1114        *  @param  first  An input iterator.
1115        *  @param  last   An input iterator.
1116        *
1117        *  This function will insert copies of the data in the range
1118        *  [first,last) into the %deque before the location specified
1119        *  by @a pos.  This is known as "range insert."
1120        */
1121       template<typename _InputIterator>
1122         void
1123         insert(iterator __position, _InputIterator __first,
1124                _InputIterator __last)
1125         {
1126           // Check whether it's an integral type.  If so, it's not an iterator.
1127           typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1128           _M_insert_dispatch(__position, __first, __last, _Integral());
1129         }
1130
1131       /**
1132        *  @brief  Remove element at given position.
1133        *  @param  position  Iterator pointing to element to be erased.
1134        *  @return  An iterator pointing to the next element (or end()).
1135        *
1136        *  This function will erase the element at the given position and thus
1137        *  shorten the %deque by one.
1138        *
1139        *  The user is cautioned that
1140        *  this function only erases the element, and that if the element is
1141        *  itself a pointer, the pointed-to memory is not touched in any way.
1142        *  Managing the pointer is the user's responsibilty.
1143        */
1144       iterator
1145       erase(iterator __position);
1146
1147       /**
1148        *  @brief  Remove a range of elements.
1149        *  @param  first  Iterator pointing to the first element to be erased.
1150        *  @param  last  Iterator pointing to one past the last element to be
1151        *                erased.
1152        *  @return  An iterator pointing to the element pointed to by @a last
1153        *           prior to erasing (or end()).
1154        *
1155        *  This function will erase the elements in the range [first,last) and
1156        *  shorten the %deque accordingly.
1157        *
1158        *  The user is cautioned that
1159        *  this function only erases the elements, and that if the elements
1160        *  themselves are pointers, the pointed-to memory is not touched in any
1161        *  way.  Managing the pointer is the user's responsibilty.
1162        */
1163       iterator
1164       erase(iterator __first, iterator __last);
1165
1166       /**
1167        *  @brief  Swaps data with another %deque.
1168        *  @param  x  A %deque of the same element and allocator types.
1169        *
1170        *  This exchanges the elements between two deques in constant time.
1171        *  (Four pointers, so it should be quite fast.)
1172        *  Note that the global std::swap() function is specialized such that
1173        *  std::swap(d1,d2) will feed to this function.
1174        */
1175       void
1176       swap(deque& __x)
1177       {
1178         std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
1179         std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
1180         std::swap(this->_M_impl._M_map, __x._M_impl._M_map);
1181         std::swap(this->_M_impl._M_map_size, __x._M_impl._M_map_size);
1182       }
1183
1184       /**
1185        *  Erases all the elements.  Note that this function only erases the
1186        *  elements, and that if the elements themselves are pointers, the
1187        *  pointed-to memory is not touched in any way.  Managing the pointer is
1188        *  the user's responsibilty.
1189        */
1190       void clear();
1191
1192     protected:
1193       // Internal constructor functions follow.
1194
1195       // called by the range constructor to implement [23.1.1]/9
1196       template<typename _Integer>
1197         void
1198         _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1199         {
1200           _M_initialize_map(__n);
1201           _M_fill_initialize(__x);
1202         }
1203
1204       // called by the range constructor to implement [23.1.1]/9
1205       template<typename _InputIterator>
1206         void
1207         _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1208                                __false_type)
1209         {
1210           typedef typename std::iterator_traits<_InputIterator>::
1211             iterator_category _IterCategory;
1212           _M_range_initialize(__first, __last, _IterCategory());
1213         }
1214
1215       // called by the second initialize_dispatch above
1216       //@{
1217       /**
1218        *  @if maint
1219        *  @brief Fills the deque with whatever is in [first,last).
1220        *  @param  first  An input iterator.
1221        *  @param  last  An input iterator.
1222        *  @return   Nothing.
1223        *
1224        *  If the iterators are actually forward iterators (or better), then the
1225        *  memory layout can be done all at once.  Else we move forward using
1226        *  push_back on each value from the iterator.
1227        *  @endif
1228        */
1229       template<typename _InputIterator>
1230         void
1231         _M_range_initialize(_InputIterator __first, _InputIterator __last,
1232                             std::input_iterator_tag);
1233
1234       // called by the second initialize_dispatch above
1235       template<typename _ForwardIterator>
1236         void
1237         _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
1238                             std::forward_iterator_tag);
1239       //@}
1240
1241       /**
1242        *  @if maint
1243        *  @brief Fills the %deque with copies of value.
1244        *  @param  value  Initial value.
1245        *  @return   Nothing.
1246        *  @pre _M_start and _M_finish have already been initialized,
1247        *  but none of the %deque's elements have yet been constructed.
1248        *
1249        *  This function is called only when the user provides an explicit size
1250        *  (with or without an explicit exemplar value).
1251        *  @endif
1252        */
1253       void
1254       _M_fill_initialize(const value_type& __value);
1255
1256       // Internal assign functions follow.  The *_aux functions do the actual
1257       // assignment work for the range versions.
1258
1259       // called by the range assign to implement [23.1.1]/9
1260       template<typename _Integer>
1261         void
1262         _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1263         {
1264           _M_fill_assign(static_cast<size_type>(__n),
1265                          static_cast<value_type>(__val));
1266         }
1267
1268       // called by the range assign to implement [23.1.1]/9
1269       template<typename _InputIterator>
1270         void
1271         _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1272                            __false_type)
1273         {
1274           typedef typename std::iterator_traits<_InputIterator>::
1275             iterator_category _IterCategory;
1276           _M_assign_aux(__first, __last, _IterCategory());
1277         }
1278
1279       // called by the second assign_dispatch above
1280       template<typename _InputIterator>
1281         void
1282         _M_assign_aux(_InputIterator __first, _InputIterator __last,
1283                       std::input_iterator_tag);
1284
1285       // called by the second assign_dispatch above
1286       template<typename _ForwardIterator>
1287         void
1288         _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
1289                       std::forward_iterator_tag)
1290         {
1291           const size_type __len = std::distance(__first, __last);
1292           if (__len > size())
1293             {
1294               _ForwardIterator __mid = __first;
1295               std::advance(__mid, size());
1296               std::copy(__first, __mid, begin());
1297               insert(end(), __mid, __last);
1298             }
1299           else
1300             erase(std::copy(__first, __last, begin()), end());
1301         }
1302
1303       // Called by assign(n,t), and the range assign when it turns out
1304       // to be the same thing.
1305       void
1306       _M_fill_assign(size_type __n, const value_type& __val)
1307       {
1308         if (__n > size())
1309           {
1310             std::fill(begin(), end(), __val);
1311             insert(end(), __n - size(), __val);
1312           }
1313         else
1314           {
1315             erase(begin() + __n, end());
1316             std::fill(begin(), end(), __val);
1317           }
1318       }
1319
1320       //@{
1321       /**
1322        *  @if maint
1323        *  @brief Helper functions for push_* and pop_*.
1324        *  @endif
1325        */
1326       void _M_push_back_aux(const value_type&);
1327       void _M_push_front_aux(const value_type&);
1328       void _M_pop_back_aux();
1329       void _M_pop_front_aux();
1330       //@}
1331
1332       // Internal insert functions follow.  The *_aux functions do the actual
1333       // insertion work when all shortcuts fail.
1334
1335       // called by the range insert to implement [23.1.1]/9
1336       template<typename _Integer>
1337         void
1338         _M_insert_dispatch(iterator __pos,
1339                            _Integer __n, _Integer __x, __true_type)
1340         {
1341           _M_fill_insert(__pos, static_cast<size_type>(__n),
1342                          static_cast<value_type>(__x));
1343         }
1344
1345       // called by the range insert to implement [23.1.1]/9
1346       template<typename _InputIterator>
1347         void
1348         _M_insert_dispatch(iterator __pos,
1349                            _InputIterator __first, _InputIterator __last,
1350                            __false_type)
1351         {
1352           typedef typename std::iterator_traits<_InputIterator>::
1353             iterator_category _IterCategory;
1354           _M_range_insert_aux(__pos, __first, __last, _IterCategory());
1355         }
1356
1357       // called by the second insert_dispatch above
1358       template<typename _InputIterator>
1359         void
1360         _M_range_insert_aux(iterator __pos, _InputIterator __first,
1361                             _InputIterator __last, std::input_iterator_tag);
1362
1363       // called by the second insert_dispatch above
1364       template<typename _ForwardIterator>
1365         void
1366         _M_range_insert_aux(iterator __pos, _ForwardIterator __first,
1367                             _ForwardIterator __last, std::forward_iterator_tag);
1368
1369       // Called by insert(p,n,x), and the range insert when it turns out to be
1370       // the same thing.  Can use fill functions in optimal situations,
1371       // otherwise passes off to insert_aux(p,n,x).
1372       void
1373       _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
1374
1375       // called by insert(p,x)
1376       iterator
1377       _M_insert_aux(iterator __pos, const value_type& __x);
1378
1379       // called by insert(p,n,x) via fill_insert
1380       void
1381       _M_insert_aux(iterator __pos, size_type __n, const value_type& __x);
1382
1383       // called by range_insert_aux for forward iterators
1384       template<typename _ForwardIterator>
1385         void
1386         _M_insert_aux(iterator __pos,
1387                       _ForwardIterator __first, _ForwardIterator __last,
1388                       size_type __n);
1389
1390       //@{
1391       /**
1392        *  @if maint
1393        *  @brief Memory-handling helpers for the previous internal insert
1394        *         functions.
1395        *  @endif
1396        */
1397       iterator
1398       _M_reserve_elements_at_front(size_type __n)
1399       {
1400         const size_type __vacancies = this->_M_impl._M_start._M_cur
1401                                       - this->_M_impl._M_start._M_first;
1402         if (__n > __vacancies)
1403           _M_new_elements_at_front(__n - __vacancies);
1404         return this->_M_impl._M_start - difference_type(__n);
1405       }
1406
1407       iterator
1408       _M_reserve_elements_at_back(size_type __n)
1409       {
1410         const size_type __vacancies = (this->_M_impl._M_finish._M_last
1411                                        - this->_M_impl._M_finish._M_cur) - 1;
1412         if (__n > __vacancies)
1413           _M_new_elements_at_back(__n - __vacancies);
1414         return this->_M_impl._M_finish + difference_type(__n);
1415       }
1416
1417       void
1418       _M_new_elements_at_front(size_type __new_elements);
1419
1420       void
1421       _M_new_elements_at_back(size_type __new_elements);
1422       //@}
1423
1424
1425       //@{
1426       /**
1427        *  @if maint
1428        *  @brief Memory-handling helpers for the major %map.
1429        *
1430        *  Makes sure the _M_map has space for new nodes.  Does not
1431        *  actually add the nodes.  Can invalidate _M_map pointers.
1432        *  (And consequently, %deque iterators.)
1433        *  @endif
1434        */
1435       void
1436       _M_reserve_map_at_back (size_type __nodes_to_add = 1)
1437       {
1438         if (__nodes_to_add + 1 > this->_M_impl._M_map_size
1439             - (this->_M_impl._M_finish._M_node - this->_M_impl._M_map))
1440           _M_reallocate_map(__nodes_to_add, false);
1441       }
1442
1443       void
1444       _M_reserve_map_at_front (size_type __nodes_to_add = 1)
1445       {
1446         if (__nodes_to_add > size_type(this->_M_impl._M_start._M_node
1447                                        - this->_M_impl._M_map))
1448           _M_reallocate_map(__nodes_to_add, true);
1449       }
1450
1451       void
1452       _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front);
1453       //@}
1454     };
1455
1456
1457   /**
1458    *  @brief  Deque equality comparison.
1459    *  @param  x  A %deque.
1460    *  @param  y  A %deque of the same type as @a x.
1461    *  @return  True iff the size and elements of the deques are equal.
1462    *
1463    *  This is an equivalence relation.  It is linear in the size of the
1464    *  deques.  Deques are considered equivalent if their sizes are equal,
1465    *  and if corresponding elements compare equal.
1466   */
1467   template<typename _Tp, typename _Alloc>
1468     inline bool
1469     operator==(const deque<_Tp, _Alloc>& __x,
1470                          const deque<_Tp, _Alloc>& __y)
1471     { return __x.size() == __y.size()
1472              && std::equal(__x.begin(), __x.end(), __y.begin()); }
1473
1474   /**
1475    *  @brief  Deque ordering relation.
1476    *  @param  x  A %deque.
1477    *  @param  y  A %deque of the same type as @a x.
1478    *  @return  True iff @a x is lexicographically less than @a y.
1479    *
1480    *  This is a total ordering relation.  It is linear in the size of the
1481    *  deques.  The elements must be comparable with @c <.
1482    *
1483    *  See std::lexicographical_compare() for how the determination is made.
1484   */
1485   template<typename _Tp, typename _Alloc>
1486     inline bool
1487     operator<(const deque<_Tp, _Alloc>& __x,
1488               const deque<_Tp, _Alloc>& __y)
1489     { return lexicographical_compare(__x.begin(), __x.end(),
1490                                      __y.begin(), __y.end()); }
1491
1492   /// Based on operator==
1493   template<typename _Tp, typename _Alloc>
1494     inline bool
1495     operator!=(const deque<_Tp, _Alloc>& __x,
1496                const deque<_Tp, _Alloc>& __y)
1497     { return !(__x == __y); }
1498
1499   /// Based on operator<
1500   template<typename _Tp, typename _Alloc>
1501     inline bool
1502     operator>(const deque<_Tp, _Alloc>& __x,
1503               const deque<_Tp, _Alloc>& __y)
1504     { return __y < __x; }
1505
1506   /// Based on operator<
1507   template<typename _Tp, typename _Alloc>
1508     inline bool
1509     operator<=(const deque<_Tp, _Alloc>& __x,
1510                const deque<_Tp, _Alloc>& __y)
1511     { return !(__y < __x); }
1512
1513   /// Based on operator<
1514   template<typename _Tp, typename _Alloc>
1515     inline bool
1516     operator>=(const deque<_Tp, _Alloc>& __x,
1517                const deque<_Tp, _Alloc>& __y)
1518     { return !(__x < __y); }
1519
1520   /// See std::deque::swap().
1521   template<typename _Tp, typename _Alloc>
1522     inline void
1523     swap(deque<_Tp,_Alloc>& __x, deque<_Tp,_Alloc>& __y)
1524     { __x.swap(__y); }
1525 } // namespace std
1526
1527 #endif /* _DEQUE_H */