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