# stl_deque.h

Go to the documentation of this file.

    00001 // Deque implementation -*- C++ -*-
    00002
    00003 // Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
    00004 //
    00005 // This file is part of the GNU ISO C++ Library.  This library is free
    00006 // software; you can redistribute it and/or modify it under the
    00007 // terms of the GNU General Public License as published by the
    00008 // Free Software Foundation; either version 2, or (at your option)
    00009 // any later version.
    00010
    00011 // This library is distributed in the hope that it will be useful,
    00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00014 // GNU General Public License for more details.
    00015
    00016 // You should have received a copy of the GNU General Public License along
    00017 // with this library; see the file COPYING.  If not, write to the Free
    00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
    00019 // USA.
    00020
    00021 // As a special exception, you may use this file as part of a free software
    00022 // library without restriction.  Specifically, if other files instantiate
    00023 // templates or use macros or inline functions from this file, or you compile
    00024 // this file and link it with other files to produce an executable, this
    00025 // file does not by itself cause the resulting executable to be covered by
    00026 // the GNU General Public License.  This exception does not however
    00027 // invalidate any other reasons why the executable file might be covered by
    00028 // the GNU General Public License.
    00029
    00030 /*
    00031  *
    00032  * Copyright (c) 1994
    00033  * Hewlett-Packard Company
    00034  *
    00035  * Permission to use, copy, modify, distribute and sell this software
    00036  * and its documentation for any purpose is hereby granted without fee,
    00037  * provided that the above copyright notice appear in all copies and
    00038  * that both that copyright notice and this permission notice appear
    00039  * in supporting documentation.  Hewlett-Packard Company makes no
    00040  * representations about the suitability of this software for any
    00041  * purpose.  It is provided "as is" without express or implied warranty.
    00042  *
    00043  *
    00044  * Copyright (c) 1997
    00045  * Silicon Graphics Computer Systems, Inc.
    00046  *
    00047  * Permission to use, copy, modify, distribute and sell this software
    00048  * and its documentation for any purpose is hereby granted without fee,
    00049  * provided that the above copyright notice appear in all copies and
    00050  * that both that copyright notice and this permission notice appear
    00051  * in supporting documentation.  Silicon Graphics makes no
    00052  * representations about the suitability of this software for any
    00053  * purpose.  It is provided "as is" without express or implied warranty.
    00054  */
    00055
    00056 /** @file stl_deque.h
    00057  *  This is an internal header file, included by other library headers.
    00058  *  You should not attempt to use it directly.
    00059  */
    00060
    00061 #ifndef _DEQUE_H
    00062 #define _DEQUE_H 1
    00063
    00064 #include <bits/concept_check.h>
    00065 #include <bits/stl_iterator_base_types.h>
    00066 #include <bits/stl_iterator_base_funcs.h>
    00067
    00068 namespace _GLIBCXX_STD
    00069 {
    00070   /**
    00071    *  @if maint
    00072    *  @brief This function controls the size of memory nodes.
    00073    *  @param  size  The size of an element.
    00074    *  @return   The number (not byte size) of elements per node.
    00075    *
    00076    *  This function started off as a compiler kludge from SGI, but seems to
    00077    *  be a useful wrapper around a repeated constant expression.  The '512' is
    00078    *  tuneable (and no other code needs to change), but no investigation has
    00079    *  been done since inheriting the SGI code.
    00080    *  @endif
    00081   */
    00082   inline size_t
    00083   __deque_buf_size(size_t __size)
    00084   { return __size < 512 ? size_t(512 / __size) : size_t(1); }
    00085
    00086
    00087   /**
    00088    *  @brief A deque::iterator.
    00089    *
    00090    *  Quite a bit of intelligence here.  Much of the functionality of deque is
    00091    *  actually passed off to this class.  A deque holds two of these internally,
    00092    *  marking its valid range.  Access to elements is done as offsets of either
    00093    *  of those two, relying on operator overloading in this class.
    00094    *
    00095    *  @if maint
    00096    *  All the functions are op overloads except for _M_set_node.
    00097    *  @endif
    00098   */
    00099   template<typename _Tp, typename _Ref, typename _Ptr>
    00100     struct _Deque_iterator
    00101     {
    00102       typedef _Deque_iterator<_Tp, _Tp&, _Tp*>             iterator;
    00103       typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
    00104
    00105       static size_t _S_buffer_size()
    00106       { return __deque_buf_size(sizeof(_Tp)); }
    00107
    00108       typedef random_access_iterator_tag iterator_category;
    00109       typedef _Tp                        value_type;
    00110       typedef _Ptr                       pointer;
    00111       typedef _Ref                       reference;
    00112       typedef size_t                     size_type;
    00113       typedef ptrdiff_t                  difference_type;
    00114       typedef _Tp**                      _Map_pointer;
    00115       typedef _Deque_iterator            _Self;
    00116
    00117       _Tp* _M_cur;
    00118       _Tp* _M_first;
    00119       _Tp* _M_last;
    00120       _Map_pointer _M_node;
    00121
    00122       _Deque_iterator(_Tp* __x, _Map_pointer __y)
    00123       : _M_cur(__x), _M_first(*__y),
    00124         _M_last(*__y + _S_buffer_size()), _M_node(__y) {}
    00125
    00126       _Deque_iterator() : _M_cur(0), _M_first(0), _M_last(0), _M_node(0) {}
    00127
    00128       _Deque_iterator(const iterator& __x)
    00129       : _M_cur(__x._M_cur), _M_first(__x._M_first),
    00130         _M_last(__x._M_last), _M_node(__x._M_node) {}
    00131
    00132       reference
    00133       operator*() const
    00134       { return *_M_cur; }
    00135
    00136       pointer
    00137       operator->() const
    00138       { return _M_cur; }
    00139
    00140       _Self&
    00141       operator++()
    00142       {
    00143     ++_M_cur;
    00144     if (_M_cur == _M_last)
    00145       {
    00146         _M_set_node(_M_node + 1);
    00147         _M_cur = _M_first;
    00148       }
    00149     return *this;
    00150       }
    00151
    00152       _Self
    00153       operator++(int)
    00154       {
    00155     _Self __tmp = *this;
    00156     ++*this;
    00157     return __tmp;
    00158       }
    00159
    00160       _Self&
    00161       operator--()
    00162       {
    00163     if (_M_cur == _M_first)
    00164       {
    00165         _M_set_node(_M_node - 1);
    00166         _M_cur = _M_last;
    00167       }
    00168     --_M_cur;
    00169     return *this;
    00170       }
    00171
    00172       _Self
    00173       operator--(int)
    00174       {
    00175     _Self __tmp = *this;
    00176     --*this;
    00177     return __tmp;
    00178       }
    00179
    00180       _Self&
    00181       operator+=(difference_type __n)
    00182       {
    00183     const difference_type __offset = __n + (_M_cur - _M_first);
    00184     if (__offset >= 0 && __offset < difference_type(_S_buffer_size()))
    00185       _M_cur += __n;
    00186     else
    00187       {
    00188         const difference_type __node_offset =
    00189           __offset > 0 ? __offset / difference_type(_S_buffer_size())
    00190                        : -difference_type((-__offset - 1)
    00191                           / _S_buffer_size()) - 1;
    00192         _M_set_node(_M_node + __node_offset);
    00193         _M_cur = _M_first + (__offset - __node_offset
    00194                  * difference_type(_S_buffer_size()));
    00195       }
    00196     return *this;
    00197       }
    00198
    00199       _Self
    00200       operator+(difference_type __n) const
    00201       {
    00202     _Self __tmp = *this;
    00203     return __tmp += __n;
    00204       }
    00205
    00206       _Self&
    00207       operator-=(difference_type __n)
    00208       { return *this += -__n; }
    00209
    00210       _Self
    00211       operator-(difference_type __n) const
    00212       {
    00213     _Self __tmp = *this;
    00214     return __tmp -= __n;
    00215       }
    00216
    00217       reference
    00218       operator[](difference_type __n) const
    00219       { return *(*this + __n); }
    00220
    00221       /** @if maint
    00222        *  Prepares to traverse new_node.  Sets everything except _M_cur, which
    00223        *  should therefore be set by the caller immediately afterwards, based on
    00224        *  _M_first and _M_last.
    00225        *  @endif
    00226        */
    00227       void
    00228       _M_set_node(_Map_pointer __new_node)
    00229       {
    00230     _M_node = __new_node;
    00231     _M_first = *__new_node;
    00232     _M_last = _M_first + difference_type(_S_buffer_size());
    00233       }
    00234     };
    00235
    00236   // Note: we also provide overloads whose operands are of the same type in
    00237   // order to avoid ambiguous overload resolution when std::rel_ops operators
    00238   // are in scope (for additional details, see libstdc++/3628)
    00239   template<typename _Tp, typename _Ref, typename _Ptr>
    00240     inline bool
    00241     operator==(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
    00242            const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
    00243     { return __x._M_cur == __y._M_cur; }
    00244
    00245   template<typename _Tp, typename _RefL, typename _PtrL,
    00246        typename _RefR, typename _PtrR>
    00247     inline bool
    00248     operator==(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
    00249            const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
    00250     { return __x._M_cur == __y._M_cur; }
    00251
    00252   template<typename _Tp, typename _Ref, typename _Ptr>
    00253     inline bool
    00254     operator!=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
    00255            const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
    00256     { return !(__x == __y); }
    00257
    00258   template<typename _Tp, typename _RefL, typename _PtrL,
    00259        typename _RefR, typename _PtrR>
    00260     inline bool
    00261     operator!=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
    00262            const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
    00263     { return !(__x == __y); }
    00264
    00265   template<typename _Tp, typename _Ref, typename _Ptr>
    00266     inline bool
    00267     operator<(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
    00268           const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
    00269     { return (__x._M_node == __y._M_node) ? (__x._M_cur < __y._M_cur)
    00270                                           : (__x._M_node < __y._M_node); }
    00271
    00272   template<typename _Tp, typename _RefL, typename _PtrL,
    00273        typename _RefR, typename _PtrR>
    00274     inline bool
    00275     operator<(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
    00276           const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
    00277     { return (__x._M_node == __y._M_node) ? (__x._M_cur < __y._M_cur)
    00278                                       : (__x._M_node < __y._M_node); }
    00279
    00280   template<typename _Tp, typename _Ref, typename _Ptr>
    00281     inline bool
    00282     operator>(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
    00283           const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
    00284     { return __y < __x; }
    00285
    00286   template<typename _Tp, typename _RefL, typename _PtrL,
    00287        typename _RefR, typename _PtrR>
    00288     inline bool
    00289     operator>(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
    00290           const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
    00291     { return __y < __x; }
    00292
    00293   template<typename _Tp, typename _Ref, typename _Ptr>
    00294     inline bool
    00295     operator<=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
    00296            const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
    00297     { return !(__y < __x); }
    00298
    00299   template<typename _Tp, typename _RefL, typename _PtrL,
    00300        typename _RefR, typename _PtrR>
    00301     inline bool
    00302     operator<=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
    00303            const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
    00304     { return !(__y < __x); }
    00305
    00306   template<typename _Tp, typename _Ref, typename _Ptr>
    00307     inline bool
    00308     operator>=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
    00309            const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
    00310     { return !(__x < __y); }
    00311
    00312   template<typename _Tp, typename _RefL, typename _PtrL,
    00313        typename _RefR, typename _PtrR>
    00314     inline bool
    00315     operator>=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
    00316            const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
    00317     { return !(__x < __y); }
    00318
    00319   // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00320   // According to the resolution of DR179 not only the various comparison
    00321   // operators but also operator- must accept mixed iterator/const_iterator
    00322   // parameters.
    00323   template<typename _Tp, typename _RefL, typename _PtrL,
    00324        typename _RefR, typename _PtrR>
    00325     inline typename _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type
    00326     operator-(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
    00327           const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
    00328     {
    00329       return typename _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type
    00330     (_Deque_iterator<_Tp, _RefL, _PtrL>::_S_buffer_size())
    00331     * (__x._M_node - __y._M_node - 1) + (__x._M_cur - __x._M_first)
    00332     + (__y._M_last - __y._M_cur);
    00333     }
    00334
    00335   template<typename _Tp, typename _Ref, typename _Ptr>
    00336     inline _Deque_iterator<_Tp, _Ref, _Ptr>
    00337     operator+(ptrdiff_t __n, const _Deque_iterator<_Tp, _Ref, _Ptr>& __x)
    00338     { return __x + __n; }
    00339
    00340   /**
    00341    *  @if maint
    00342    *  Deque base class.  This class provides the unified face for %deque's
    00343    *  allocation.  This class's constructor and destructor allocate and
    00344    *  deallocate (but do not initialize) storage.  This makes %exception
    00345    *  safety easier.
    00346    *
    00347    *  Nothing in this class ever constructs or destroys an actual Tp element.
    00348    *  (Deque handles that itself.)  Only/All memory management is performed
    00349    *  here.
    00350    *  @endif
    00351   */
    00352   template<typename _Tp, typename _Alloc>
    00353     class _Deque_base
    00354     {
    00355     public:
    00356       typedef _Alloc                  allocator_type;
    00357
    00358       allocator_type
    00359       get_allocator() const
    00360       { return *static_cast<const _Alloc*>(&this->_M_impl); }
    00361
    00362       typedef _Deque_iterator<_Tp, _Tp&, _Tp*>             iterator;
    00363       typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
    00364
    00365       _Deque_base(const allocator_type& __a, size_t __num_elements)
    00366       : _M_impl(__a)
    00367       { _M_initialize_map(__num_elements); }
    00368
    00369       _Deque_base(const allocator_type& __a)
    00370       : _M_impl(__a)
    00371       { }
    00372
    00373       ~_Deque_base();
    00374
    00375     protected:
    00376       //This struct encapsulates the implementation of the std::deque
    00377       //standard container and at the same time makes use of the EBO
    00378       //for empty allocators.
    00379       struct _Deque_impl
    00380       : public _Alloc
    00381       {
    00382     _Tp** _M_map;
    00383     size_t _M_map_size;
    00384     iterator _M_start;
    00385     iterator _M_finish;
    00386
    00387     _Deque_impl(const _Alloc& __a)
    00388     : _Alloc(__a), _M_map(0), _M_map_size(0), _M_start(), _M_finish()
    00389     { }
    00390       };
    00391
    00392       typedef typename _Alloc::template rebind<_Tp*>::other _Map_alloc_type;
    00393       _Map_alloc_type _M_get_map_allocator() const
    00394       { return _Map_alloc_type(this->get_allocator()); }
    00395
    00396       _Tp*
    00397       _M_allocate_node()
    00398       { return _M_impl._Alloc::allocate(__deque_buf_size(sizeof(_Tp))); }
    00399
    00400       void
    00401       _M_deallocate_node(_Tp* __p)
    00402       { _M_impl._Alloc::deallocate(__p, __deque_buf_size(sizeof(_Tp))); }
    00403
    00404       _Tp**
    00405       _M_allocate_map(size_t __n)
    00406       { return _M_get_map_allocator().allocate(__n); }
    00407
    00408       void
    00409       _M_deallocate_map(_Tp** __p, size_t __n)
    00410       { _M_get_map_allocator().deallocate(__p, __n); }
    00411
    00412     protected:
    00413       void _M_initialize_map(size_t);
    00414       void _M_create_nodes(_Tp** __nstart, _Tp** __nfinish);
    00415       void _M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish);
    00416       enum { _S_initial_map_size = 8 };
    00417
    00418       _Deque_impl _M_impl;
    00419     };
    00420
    00421   template<typename _Tp, typename _Alloc>
    00422   _Deque_base<_Tp,_Alloc>::~_Deque_base()
    00423   {
    00424     if (this->_M_impl._M_map)
    00425     {
    00426       _M_destroy_nodes(this->_M_impl._M_start._M_node,
    00427                this->_M_impl._M_finish._M_node + 1);
    00428       _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
    00429     }
    00430   }
    00431
    00432   /**
    00433    *  @if maint
    00434    *  @brief Layout storage.
    00435    *  @param  num_elements  The count of T's for which to allocate space
    00436    *                        at first.
    00437    *  @return   Nothing.
    00438    *
    00439    *  The initial underlying memory layout is a bit complicated...
    00440    *  @endif
    00441   */
    00442   template<typename _Tp, typename _Alloc>
    00443     void
    00444     _Deque_base<_Tp,_Alloc>::_M_initialize_map(size_t __num_elements)
    00445     {
    00446       size_t __num_nodes = __num_elements / __deque_buf_size(sizeof(_Tp)) + 1;
    00447
    00448       this->_M_impl._M_map_size = std::max((size_t) _S_initial_map_size,
    00449                    __num_nodes + 2);
    00450       this->_M_impl._M_map = _M_allocate_map(this->_M_impl._M_map_size);
    00451
    00452       // For "small" maps (needing less than _M_map_size nodes), allocation
    00453       // starts in the middle elements and grows outwards.  So nstart may be
    00454       // the beginning of _M_map, but for small maps it may be as far in as
    00455       // _M_map+3.
    00456
    00457       _Tp** __nstart = (this->_M_impl._M_map
    00458             + (this->_M_impl._M_map_size - __num_nodes) / 2);
    00459       _Tp** __nfinish = __nstart + __num_nodes;
    00460
    00461       try
    00462     { _M_create_nodes(__nstart, __nfinish); }
    00463       catch(...)
    00464     {
    00465       _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
    00466       this->_M_impl._M_map = 0;
    00467       this->_M_impl._M_map_size = 0;
    00468       __throw_exception_again;
    00469     }
    00470
    00471       this->_M_impl._M_start._M_set_node(__nstart);
    00472       this->_M_impl._M_finish._M_set_node(__nfinish - 1);
    00473       this->_M_impl._M_start._M_cur = _M_impl._M_start._M_first;
    00474       this->_M_impl._M_finish._M_cur = (this->_M_impl._M_finish._M_first
    00475                     + __num_elements
    00476                     % __deque_buf_size(sizeof(_Tp)));
    00477     }
    00478
    00479   template<typename _Tp, typename _Alloc>
    00480     void
    00481     _Deque_base<_Tp,_Alloc>::_M_create_nodes(_Tp** __nstart, _Tp** __nfinish)
    00482     {
    00483       _Tp** __cur;
    00484       try
    00485     {
    00486       for (__cur = __nstart; __cur < __nfinish; ++__cur)
    00487         *__cur = this->_M_allocate_node();
    00488     }
    00489       catch(...)
    00490     {
    00491       _M_destroy_nodes(__nstart, __cur);
    00492       __throw_exception_again;
    00493     }
    00494     }
    00495
    00496   template<typename _Tp, typename _Alloc>
    00497     void
    00498     _Deque_base<_Tp,_Alloc>::_M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish)
    00499     {
    00500       for (_Tp** __n = __nstart; __n < __nfinish; ++__n)
    00501     _M_deallocate_node(*__n);
    00502     }
    00503
    00504   /**
    00505    *  @brief  A standard container using fixed-size memory allocation and
    00506    *  constant-time manipulation of elements at either end.
    00507    *
    00508    *  @ingroup Containers
    00509    *  @ingroup Sequences
    00510    *
    00511    *  Meets the requirements of a <a href="tables.html#65">container</a>, a
    00512    *  <a href="tables.html#66">reversible container</a>, and a
    00513    *  <a href="tables.html#67">sequence</a>, including the
    00514    *  <a href="tables.html#68">optional sequence requirements</a>.
    00515    *
    00516    *  In previous HP/SGI versions of deque, there was an extra template
    00517    *  parameter so users could control the node size.  This extension turned
    00518    *  out to violate the C++ standard (it can be detected using template
    00519    *  template parameters), and it was removed.
    00520    *
    00521    *  @if maint
    00522    *  Here's how a deque<Tp> manages memory.  Each deque has 4 members:
    00523    *
    00524    *  - Tp**        _M_map
    00525    *  - size_t      _M_map_size
    00526    *  - iterator    _M_start, _M_finish
    00527    *
    00528    *  map_size is at least 8.  %map is an array of map_size pointers-to-"nodes".
    00529    *  (The name %map has nothing to do with the std::map class, and "nodes"
    00530    *  should not be confused with std::list's usage of "node".)
    00531    *
    00532    *  A "node" has no specific type name as such, but it is referred to as
    00533    *  "node" in this file.  It is a simple array-of-Tp.  If Tp is very large,
    00534    *  there will be one Tp element per node (i.e., an "array" of one).
    00535    *  For non-huge Tp's, node size is inversely related to Tp size:  the
    00536    *  larger the Tp, the fewer Tp's will fit in a node.  The goal here is to
    00537    *  keep the total size of a node relatively small and constant over different
    00538    *  Tp's, to improve allocator efficiency.
    00539    *
    00540    *  **** As I write this, the nodes are /not/ allocated using the high-speed
    00541    *  memory pool.  There are 20 hours left in the year; perhaps I can fix
    00542    *  this before 2002.
    00543    *
    00544    *  Not every pointer in the %map array will point to a node.  If the initial
    00545    *  number of elements in the deque is small, the /middle/ %map pointers will
    00546    *  be valid, and the ones at the edges will be unused.  This same situation
    00547    *  will arise as the %map grows:  available %map pointers, if any, will be on
    00548    *  the ends.  As new nodes are created, only a subset of the %map's pointers
    00549    *  need to be copied "outward".
    00550    *
    00551    *  Class invariants:
    00552    * - For any nonsingular iterator i:
    00553    *    - i.node points to a member of the %map array.  (Yes, you read that
    00554    *      correctly:  i.node does not actually point to a node.)  The member of
    00555    *      the %map array is what actually points to the node.
    00556    *    - i.first == *(i.node)    (This points to the node (first Tp element).)
    00557    *    - i.last  == i.first + node_size
    00558    *    - i.cur is a pointer in the range [i.first, i.last).  NOTE:
    00559    *      the implication of this is that i.cur is always a dereferenceable
    00560    *      pointer, even if i is a past-the-end iterator.
    00561    * - Start and Finish are always nonsingular iterators.  NOTE: this means that
    00562    *   an empty deque must have one node, a deque with <N elements (where N is
    00563    *   the node buffer size) must have one node, a deque with N through (2N-1)
    00564    *   elements must have two nodes, etc.
    00565    * - For every node other than start.node and finish.node, every element in
    00566    *   the node is an initialized object.  If start.node == finish.node, then
    00567    *   [start.cur, finish.cur) are initialized objects, and the elements outside
    00568    *   that range are uninitialized storage.  Otherwise, [start.cur, start.last)
    00569    *   and [finish.first, finish.cur) are initialized objects, and [start.first,
    00570    *   start.cur) and [finish.cur, finish.last) are uninitialized storage.
    00571    * - [%map, %map + map_size) is a valid, non-empty range.
    00572    * - [start.node, finish.node] is a valid range contained within
    00573    *   [%map, %map + map_size).
    00574    * - A pointer in the range [%map, %map + map_size) points to an allocated
    00575    *   node if and only if the pointer is in the range
    00576    *   [start.node, finish.node].
    00577    *
    00578    *  Here's the magic:  nothing in deque is "aware" of the discontiguous
    00579    *  storage!
    00580    *
    00581    *  The memory setup and layout occurs in the parent, _Base, and the iterator
    00582    *  class is entirely responsible for "leaping" from one node to the next.
    00583    *  All the implementation routines for deque itself work only through the
    00584    *  start and finish iterators.  This keeps the routines simple and sane,
    00585    *  and we can use other standard algorithms as well.
    00586    *  @endif
    00587   */
    00588   template<typename _Tp, typename _Alloc = allocator<_Tp> >
    00589     class deque : protected _Deque_base<_Tp, _Alloc>
    00590     {
    00591       // concept requirements
    00592       __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
    00593
    00594       typedef _Deque_base<_Tp, _Alloc>           _Base;
    00595
    00596     public:
    00597       typedef _Tp                                value_type;
    00598       typedef value_type*                        pointer;
    00599       typedef const value_type*                  const_pointer;
    00600       typedef typename _Base::iterator           iterator;
    00601       typedef typename _Base::const_iterator     const_iterator;
    00602       typedef std::reverse_iterator<const_iterator>   const_reverse_iterator;
    00603       typedef std::reverse_iterator<iterator>         reverse_iterator;
    00604       typedef value_type&                        reference;
    00605       typedef const value_type&                  const_reference;
    00606       typedef size_t                             size_type;
    00607       typedef ptrdiff_t                          difference_type;
    00608       typedef typename _Base::allocator_type     allocator_type;
    00609
    00610     protected:
    00611       typedef pointer*                           _Map_pointer;
    00612
    00613       static size_t _S_buffer_size()
    00614       { return __deque_buf_size(sizeof(_Tp)); }
    00615
    00616       // Functions controlling memory layout, and nothing else.
    00617       using _Base::_M_initialize_map;
    00618       using _Base::_M_create_nodes;
    00619       using _Base::_M_destroy_nodes;
    00620       using _Base::_M_allocate_node;
    00621       using _Base::_M_deallocate_node;
    00622       using _Base::_M_allocate_map;
    00623       using _Base::_M_deallocate_map;
    00624
    00625       /** @if maint
    00626        *  A total of four data members accumulated down the heirarchy.
    00627        *  May be accessed via _M_impl.*
    00628        *  @endif
    00629        */
    00630       using _Base::_M_impl;
    00631
    00632     public:
    00633       // [23.2.1.1] construct/copy/destroy
    00634       // (assign() and get_allocator() are also listed in this section)
    00635       /**
    00636        *  @brief  Default constructor creates no elements.
    00637        */
    00638       explicit
    00639       deque(const allocator_type& __a = allocator_type())
    00640       : _Base(__a, 0) {}
    00641
    00642       /**
    00643        *  @brief  Create a %deque with copies of an exemplar element.
    00644        *  @param  n  The number of elements to initially create.
    00645        *  @param  value  An element to copy.
    00646        *
    00647        *  This constructor fills the %deque with @a n copies of @a value.
    00648        */
    00649       deque(size_type __n, const value_type& __value,
    00650         const allocator_type& __a = allocator_type())
    00651       : _Base(__a, __n)
    00652       { _M_fill_initialize(__value); }
    00653
    00654       /**
    00655        *  @brief  Create a %deque with default elements.
    00656        *  @param  n  The number of elements to initially create.
    00657        *
    00658        *  This constructor fills the %deque with @a n copies of a
    00659        *  default-constructed element.
    00660        */
    00661       explicit
    00662       deque(size_type __n)
    00663       : _Base(allocator_type(), __n)
    00664       { _M_fill_initialize(value_type()); }
    00665
    00666       /**
    00667        *  @brief  %Deque copy constructor.
    00668        *  @param  x  A %deque of identical element and allocator types.
    00669        *
    00670        *  The newly-created %deque uses a copy of the allocation object used
    00671        *  by @a x.
    00672        */
    00673       deque(const deque& __x)
    00674       : _Base(__x.get_allocator(), __x.size())
    00675       { std::uninitialized_copy(__x.begin(), __x.end(),
    00676                 this->_M_impl._M_start); }
    00677
    00678       /**
    00679        *  @brief  Builds a %deque from a range.
    00680        *  @param  first  An input iterator.
    00681        *  @param  last  An input iterator.
    00682        *
    00683        *  Create a %deque consisting of copies of the elements from [first,
    00684        *  last).
    00685        *
    00686        *  If the iterators are forward, bidirectional, or random-access, then
    00687        *  this will call the elements' copy constructor N times (where N is
    00688        *  distance(first,last)) and do no memory reallocation.  But if only
    00689        *  input iterators are used, then this will do at most 2N calls to the
    00690        *  copy constructor, and logN memory reallocations.
    00691        */
    00692       template<typename _InputIterator>
    00693         deque(_InputIterator __first, _InputIterator __last,
    00694           const allocator_type& __a = allocator_type())
    00695     : _Base(__a)
    00696         {
    00697       // Check whether it's an integral type.  If so, it's not an iterator.
    00698       typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
    00699       _M_initialize_dispatch(__first, __last, _Integral());
    00700     }
    00701
    00702       /**
    00703        *  The dtor only erases the elements, and note that if the elements
    00704        *  themselves are pointers, the pointed-to memory is not touched in any
    00705        *  way.  Managing the pointer is the user's responsibilty.
    00706        */
    00707       ~deque()
    00708       { std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish); }
    00709
    00710       /**
    00711        *  @brief  %Deque assignment operator.
    00712        *  @param  x  A %deque of identical element and allocator types.
    00713        *
    00714        *  All the elements of @a x are copied, but unlike the copy constructor,
    00715        *  the allocator object is not copied.
    00716        */
    00717       deque&
    00718       operator=(const deque& __x);
    00719
    00720       /**
    00721        *  @brief  Assigns a given value to a %deque.
    00722        *  @param  n  Number of elements to be assigned.
    00723        *  @param  val  Value to be assigned.
    00724        *
    00725        *  This function fills a %deque with @a n copies of the given value.
    00726        *  Note that the assignment completely changes the %deque and that the
    00727        *  resulting %deque's size is the same as the number of elements assigned.
    00728        *  Old data may be lost.
    00729        */
    00730       void
    00731       assign(size_type __n, const value_type& __val)
    00732       { _M_fill_assign(__n, __val); }
    00733
    00734       /**
    00735        *  @brief  Assigns a range to a %deque.
    00736        *  @param  first  An input iterator.
    00737        *  @param  last   An input iterator.
    00738        *
    00739        *  This function fills a %deque with copies of the elements in the
    00740        *  range [first,last).
    00741        *
    00742        *  Note that the assignment completely changes the %deque and that the
    00743        *  resulting %deque's size is the same as the number of elements
    00744        *  assigned.  Old data may be lost.
    00745        */
    00746       template<typename _InputIterator>
    00747         void
    00748         assign(_InputIterator __first, _InputIterator __last)
    00749         {
    00750       typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
    00751       _M_assign_dispatch(__first, __last, _Integral());
    00752     }
    00753
    00754       /// Get a copy of the memory allocation object.
    00755       allocator_type
    00756       get_allocator() const
    00757       { return _Base::get_allocator(); }
    00758
    00759       // iterators
    00760       /**
    00761        *  Returns a read/write iterator that points to the first element in the
    00762        *  %deque.  Iteration is done in ordinary element order.
    00763        */
    00764       iterator
    00765       begin()
    00766       { return this->_M_impl._M_start; }
    00767
    00768       /**
    00769        *  Returns a read-only (constant) iterator that points to the first
    00770        *  element in the %deque.  Iteration is done in ordinary element order.
    00771        */
    00772       const_iterator
    00773       begin() const
    00774       { return this->_M_impl._M_start; }
    00775
    00776       /**
    00777        *  Returns a read/write iterator that points one past the last element in
    00778        *  the %deque.  Iteration is done in ordinary element order.
    00779        */
    00780       iterator
    00781       end()
    00782       { return this->_M_impl._M_finish; }
    00783
    00784       /**
    00785        *  Returns a read-only (constant) iterator that points one past the last
    00786        *  element in the %deque.  Iteration is done in ordinary element order.
    00787        */
    00788       const_iterator
    00789       end() const
    00790       { return this->_M_impl._M_finish; }
    00791
    00792       /**
    00793        *  Returns a read/write reverse iterator that points to the last element
    00794        *  in the %deque.  Iteration is done in reverse element order.
    00795        */
    00796       reverse_iterator
    00797       rbegin()
    00798       { return reverse_iterator(this->_M_impl._M_finish); }
    00799
    00800       /**
    00801        *  Returns a read-only (constant) reverse iterator that points to the
    00802        *  last element in the %deque.  Iteration is done in reverse element
    00803        *  order.
    00804        */
    00805       const_reverse_iterator
    00806       rbegin() const
    00807       { return const_reverse_iterator(this->_M_impl._M_finish); }
    00808
    00809       /**
    00810        *  Returns a read/write reverse iterator that points to one before the
    00811        *  first element in the %deque.  Iteration is done in reverse element
    00812        *  order.
    00813        */
    00814       reverse_iterator
    00815       rend() { return reverse_iterator(this->_M_impl._M_start); }
    00816
    00817       /**
    00818        *  Returns a read-only (constant) reverse iterator that points to one
    00819        *  before the first element in the %deque.  Iteration is done in reverse
    00820        *  element order.
    00821        */
    00822       const_reverse_iterator
    00823       rend() const
    00824       { return const_reverse_iterator(this->_M_impl._M_start); }
    00825
    00826       // [23.2.1.2] capacity
    00827       /**  Returns the number of elements in the %deque.  */
    00828       size_type
    00829       size() const
    00830       { return this->_M_impl._M_finish - this->_M_impl._M_start; }
    00831
    00832       /**  Returns the size() of the largest possible %deque.  */
    00833       size_type
    00834       max_size() const
    00835       { return size_type(-1); }
    00836
    00837       /**
    00838        *  @brief  Resizes the %deque to the specified number of elements.
    00839        *  @param  new_size  Number of elements the %deque should contain.
    00840        *  @param  x  Data with which new elements should be populated.
    00841        *
    00842        *  This function will %resize the %deque to the specified number of
    00843        *  elements.  If the number is smaller than the %deque's current size the
    00844        *  %deque is truncated, otherwise the %deque is extended and new elements
    00845        *  are populated with given data.
    00846        */
    00847       void
    00848       resize(size_type __new_size, const value_type& __x)
    00849       {
    00850     const size_type __len = size();
    00851     if (__new_size < __len)
    00852       erase(this->_M_impl._M_start + __new_size, this->_M_impl._M_finish);
    00853     else
    00854       insert(this->_M_impl._M_finish, __new_size - __len, __x);
    00855       }
    00856
    00857       /**
    00858        *  @brief  Resizes the %deque to the specified number of elements.
    00859        *  @param  new_size  Number of elements the %deque should contain.
    00860        *
    00861        *  This function will resize the %deque to the specified number of
    00862        *  elements.  If the number is smaller than the %deque's current size the
    00863        *  %deque is truncated, otherwise the %deque is extended and new elements
    00864        *  are default-constructed.
    00865        */
    00866       void
    00867       resize(size_type new_size)
    00868       { resize(new_size, value_type()); }
    00869
    00870       /**
    00871        *  Returns true if the %deque is empty.  (Thus begin() would equal end().)
    00872        */
    00873       bool
    00874       empty() const
    00875       { return this->_M_impl._M_finish == this->_M_impl._M_start; }
    00876
    00877       // element access
    00878       /**
    00879        *  @brief  Subscript access to the data contained in the %deque.
    00880        *  @param  n  The index of the element for which data should be accessed.
    00881        *  @return  Read/write reference to data.
    00882        *
    00883        *  This operator allows for easy, array-style, data access.
    00884        *  Note that data access with this operator is unchecked and out_of_range
    00885        *  lookups are not defined. (For checked lookups see at().)
    00886        */
    00887       reference
    00888       operator[](size_type __n)
    00889       { return this->_M_impl._M_start[difference_type(__n)]; }
    00890
    00891       /**
    00892        *  @brief  Subscript access to the data contained in the %deque.
    00893        *  @param  n  The index of the element for which data should be accessed.
    00894        *  @return  Read-only (constant) reference to data.
    00895        *
    00896        *  This operator allows for easy, array-style, data access.
    00897        *  Note that data access with this operator is unchecked and out_of_range
    00898        *  lookups are not defined. (For checked lookups see at().)
    00899        */
    00900       const_reference
    00901       operator[](size_type __n) const
    00902       { return this->_M_impl._M_start[difference_type(__n)]; }
    00903
    00904     protected:
    00905       /// @if maint Safety check used only from at().  @endif
    00906       void
    00907       _M_range_check(size_type __n) const
    00908       {
    00909     if (__n >= this->size())
    00910       __throw_out_of_range(__N("deque::_M_range_check"));
    00911       }
    00912
    00913     public:
    00914       /**
    00915        *  @brief  Provides access to the data contained in the %deque.
    00916        *  @param  n  The index of the element for which data should be accessed.
    00917        *  @return  Read/write reference to data.
    00918        *  @throw  std::out_of_range  If @a n is an invalid index.
    00919        *
    00920        *  This function provides for safer data access.  The parameter is first
    00921        *  checked that it is in the range of the deque.  The function throws
    00922        *  out_of_range if the check fails.
    00923        */
    00924       reference
    00925       at(size_type __n)
    00926       { _M_range_check(__n); return (*this)[__n]; }
    00927
    00928       /**
    00929        *  @brief  Provides access to the data contained in the %deque.
    00930        *  @param  n  The index of the element for which data should be accessed.
    00931        *  @return  Read-only (constant) reference to data.
    00932        *  @throw  std::out_of_range  If @a n is an invalid index.
    00933        *
    00934        *  This function provides for safer data access.  The parameter is first
    00935        *  checked that it is in the range of the deque.  The function throws
    00936        *  out_of_range if the check fails.
    00937        */
    00938       const_reference
    00939       at(size_type __n) const
    00940       {
    00941     _M_range_check(__n);
    00942     return (*this)[__n];
    00943       }
    00944
    00945       /**
    00946        *  Returns a read/write reference to the data at the first element of the
    00947        *  %deque.
    00948        */
    00949       reference
    00950       front()
    00951       { return *this->_M_impl._M_start; }
    00952
    00953       /**
    00954        *  Returns a read-only (constant) reference to the data at the first
    00955        *  element of the %deque.
    00956        */
    00957       const_reference
    00958       front() const
    00959       { return *this->_M_impl._M_start; }
    00960
    00961       /**
    00962        *  Returns a read/write reference to the data at the last element of the
    00963        *  %deque.
    00964        */
    00965       reference
    00966       back()
    00967       {
    00968     iterator __tmp = this->_M_impl._M_finish;
    00969     --__tmp;
    00970     return *__tmp;
    00971       }
    00972
    00973       /**
    00974        *  Returns a read-only (constant) reference to the data at the last
    00975        *  element of the %deque.
    00976        */
    00977       const_reference
    00978       back() const
    00979       {
    00980     const_iterator __tmp = this->_M_impl._M_finish;
    00981     --__tmp;
    00982     return *__tmp;
    00983       }
    00984
    00985       // [23.2.1.2] modifiers
    00986       /**
    00987        *  @brief  Add data to the front of the %deque.
    00988        *  @param  x  Data to be added.
    00989        *
    00990        *  This is a typical stack operation.  The function creates an element at
    00991        *  the front of the %deque and assigns the given data to it.  Due to the
    00992        *  nature of a %deque this operation can be done in constant time.
    00993        */
    00994       void
    00995       push_front(const value_type& __x)
    00996       {
    00997     if (this->_M_impl._M_start._M_cur != this->_M_impl._M_start._M_first)
    00998       {
    00999         std::_Construct(this->_M_impl._M_start._M_cur - 1, __x);
    01000         --this->_M_impl._M_start._M_cur;
    01001       }
    01002     else
    01003       _M_push_front_aux(__x);
    01004       }
    01005
    01006       /**
    01007        *  @brief  Add data to the end of the %deque.
    01008        *  @param  x  Data to be added.
    01009        *
    01010        *  This is a typical stack operation.  The function creates an element at
    01011        *  the end of the %deque and assigns the given data to it.  Due to the
    01012        *  nature of a %deque this operation can be done in constant time.
    01013        */
    01014       void
    01015       push_back(const value_type& __x)
    01016       {
    01017     if (this->_M_impl._M_finish._M_cur
    01018         != this->_M_impl._M_finish._M_last - 1)
    01019       {
    01020         std::_Construct(this->_M_impl._M_finish._M_cur, __x);
    01021         ++this->_M_impl._M_finish._M_cur;
    01022       }
    01023     else
    01024       _M_push_back_aux(__x);
    01025       }
    01026
    01027       /**
    01028        *  @brief  Removes first element.
    01029        *
    01030        *  This is a typical stack operation.  It shrinks the %deque by one.
    01031        *
    01032        *  Note that no data is returned, and if the first element's data is
    01033        *  needed, it should be retrieved before pop_front() is called.
    01034        */
    01035       void
    01036       pop_front()
    01037       {
    01038     if (this->_M_impl._M_start._M_cur
    01039         != this->_M_impl._M_start._M_last - 1)
    01040       {
    01041         std::_Destroy(this->_M_impl._M_start._M_cur);
    01042         ++this->_M_impl._M_start._M_cur;
    01043       }
    01044     else
    01045       _M_pop_front_aux();
    01046       }
    01047
    01048       /**
    01049        *  @brief  Removes last element.
    01050        *
    01051        *  This is a typical stack operation.  It shrinks the %deque by one.
    01052        *
    01053        *  Note that no data is returned, and if the last element's data is
    01054        *  needed, it should be retrieved before pop_back() is called.
    01055        */
    01056       void
    01057       pop_back()
    01058       {
    01059     if (this->_M_impl._M_finish._M_cur
    01060         != this->_M_impl._M_finish._M_first)
    01061       {
    01062         --this->_M_impl._M_finish._M_cur;
    01063         std::_Destroy(this->_M_impl._M_finish._M_cur);
    01064       }
    01065     else
    01066       _M_pop_back_aux();
    01067       }
    01068
    01069       /**
    01070        *  @brief  Inserts given value into %deque before specified iterator.
    01071        *  @param  position  An iterator into the %deque.
    01072        *  @param  x  Data to be inserted.
    01073        *  @return  An iterator that points to the inserted data.
    01074        *
    01075        *  This function will insert a copy of the given value before the
    01076        *  specified location.
    01077        */
    01078       iterator
    01079       insert(iterator position, const value_type& __x);
    01080
    01081       /**
    01082        *  @brief  Inserts a number of copies of given data into the %deque.
    01083        *  @param  position  An iterator into the %deque.
    01084        *  @param  n  Number of elements to be inserted.
    01085        *  @param  x  Data to be inserted.
    01086        *
    01087        *  This function will insert a specified number of copies of the given
    01088        *  data before the location specified by @a position.
    01089        */
    01090       void
    01091       insert(iterator __position, size_type __n, const value_type& __x)
    01092       { _M_fill_insert(__position, __n, __x); }
    01093
    01094       /**
    01095        *  @brief  Inserts a range into the %deque.
    01096        *  @param  position  An iterator into the %deque.
    01097        *  @param  first  An input iterator.
    01098        *  @param  last   An input iterator.
    01099        *
    01100        *  This function will insert copies of the data in the range [first,last)
    01101        *  into the %deque before the location specified by @a pos.  This is
    01102        *  known as "range insert."
    01103        */
    01104       template<typename _InputIterator>
    01105         void
    01106         insert(iterator __position, _InputIterator __first,
    01107            _InputIterator __last)
    01108         {
    01109       // Check whether it's an integral type.  If so, it's not an iterator.
    01110       typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
    01111       _M_insert_dispatch(__position, __first, __last, _Integral());
    01112     }
    01113
    01114       /**
    01115        *  @brief  Remove element at given position.
    01116        *  @param  position  Iterator pointing to element to be erased.
    01117        *  @return  An iterator pointing to the next element (or end()).
    01118        *
    01119        *  This function will erase the element at the given position and thus
    01120        *  shorten the %deque by one.
    01121        *
    01122        *  The user is cautioned that
    01123        *  this function only erases the element, and that if the element is
    01124        *  itself a pointer, the pointed-to memory is not touched in any way.
    01125        *  Managing the pointer is the user's responsibilty.
    01126        */
    01127       iterator
    01128       erase(iterator __position);
    01129
    01130       /**
    01131        *  @brief  Remove a range of elements.
    01132        *  @param  first  Iterator pointing to the first element to be erased.
    01133        *  @param  last  Iterator pointing to one past the last element to be
    01134        *                erased.
    01135        *  @return  An iterator pointing to the element pointed to by @a last
    01136        *           prior to erasing (or end()).
    01137        *
    01138        *  This function will erase the elements in the range [first,last) and
    01139        *  shorten the %deque accordingly.
    01140        *
    01141        *  The user is cautioned that
    01142        *  this function only erases the elements, and that if the elements
    01143        *  themselves are pointers, the pointed-to memory is not touched in any
    01144        *  way.  Managing the pointer is the user's responsibilty.
    01145        */
    01146       iterator
    01147       erase(iterator __first, iterator __last);
    01148
    01149       /**
    01150        *  @brief  Swaps data with another %deque.
    01151        *  @param  x  A %deque of the same element and allocator types.
    01152        *
    01153        *  This exchanges the elements between two deques in constant time.
    01154        *  (Four pointers, so it should be quite fast.)
    01155        *  Note that the global std::swap() function is specialized such that
    01156        *  std::swap(d1,d2) will feed to this function.
    01157        */
    01158       void
    01159       swap(deque& __x)
    01160       {
    01161     std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
    01162     std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
    01163     std::swap(this->_M_impl._M_map, __x._M_impl._M_map);
    01164     std::swap(this->_M_impl._M_map_size, __x._M_impl._M_map_size);
    01165       }
    01166
    01167       /**
    01168        *  Erases all the elements.  Note that this function only erases the
    01169        *  elements, and that if the elements themselves are pointers, the
    01170        *  pointed-to memory is not touched in any way.  Managing the pointer is
    01171        *  the user's responsibilty.
    01172        */
    01173       void clear();
    01174
    01175     protected:
    01176       // Internal constructor functions follow.
    01177
    01178       // called by the range constructor to implement [23.1.1]/9
    01179       template<typename _Integer>
    01180         void
    01181         _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
    01182         {
    01183       _M_initialize_map(__n);
    01184       _M_fill_initialize(__x);
    01185     }
    01186
    01187       // called by the range constructor to implement [23.1.1]/9
    01188       template<typename _InputIterator>
    01189         void
    01190         _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
    01191                    __false_type)
    01192         {
    01193       typedef typename iterator_traits<_InputIterator>::iterator_category
    01194         _IterCategory;
    01195       _M_range_initialize(__first, __last, _IterCategory());
    01196     }
    01197
    01198       // called by the second initialize_dispatch above
    01199       //@{
    01200       /**
    01201        *  @if maint
    01202        *  @brief Fills the deque with whatever is in [first,last).
    01203        *  @param  first  An input iterator.
    01204        *  @param  last  An input iterator.
    01205        *  @return   Nothing.
    01206        *
    01207        *  If the iterators are actually forward iterators (or better), then the
    01208        *  memory layout can be done all at once.  Else we move forward using
    01209        *  push_back on each value from the iterator.
    01210        *  @endif
    01211        */
    01212       template<typename _InputIterator>
    01213         void
    01214         _M_range_initialize(_InputIterator __first, _InputIterator __last,
    01215                 input_iterator_tag);
    01216
    01217       // called by the second initialize_dispatch above
    01218       template<typename _ForwardIterator>
    01219         void
    01220         _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
    01221                 forward_iterator_tag);
    01222       //@}
    01223
    01224       /**
    01225        *  @if maint
    01226        *  @brief Fills the %deque with copies of value.
    01227        *  @param  value  Initial value.
    01228        *  @return   Nothing.
    01229        *  @pre _M_start and _M_finish have already been initialized, but none of
    01230        *       the %deque's elements have yet been constructed.
    01231        *
    01232        *  This function is called only when the user provides an explicit size
    01233        *  (with or without an explicit exemplar value).
    01234        *  @endif
    01235        */
    01236       void
    01237       _M_fill_initialize(const value_type& __value);
    01238
    01239       // Internal assign functions follow.  The *_aux functions do the actual
    01240       // assignment work for the range versions.
    01241
    01242       // called by the range assign to implement [23.1.1]/9
    01243       template<typename _Integer>
    01244         void
    01245         _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
    01246         {
    01247       _M_fill_assign(static_cast<size_type>(__n),
    01248              static_cast<value_type>(__val));
    01249     }
    01250
    01251       // called by the range assign to implement [23.1.1]/9
    01252       template<typename _InputIterator>
    01253         void
    01254         _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
    01255                __false_type)
    01256         {
    01257       typedef typename iterator_traits<_InputIterator>::iterator_category
    01258         _IterCategory;
    01259       _M_assign_aux(__first, __last, _IterCategory());
    01260     }
    01261
    01262       // called by the second assign_dispatch above
    01263       template<typename _InputIterator>
    01264         void
    01265         _M_assign_aux(_InputIterator __first, _InputIterator __last,
    01266               input_iterator_tag);
    01267
    01268       // called by the second assign_dispatch above
    01269       template<typename _ForwardIterator>
    01270         void
    01271         _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
    01272               forward_iterator_tag)
    01273         {
    01274       const size_type __len = std::distance(__first, __last);
    01275       if (__len > size())
    01276         {
    01277           _ForwardIterator __mid = __first;
    01278           std::advance(__mid, size());
    01279           std::copy(__first, __mid, begin());
    01280           insert(end(), __mid, __last);
    01281         }
    01282       else
    01283         erase(std::copy(__first, __last, begin()), end());
    01284     }
    01285
    01286       // Called by assign(n,t), and the range assign when it turns out to be the
    01287       // same thing.
    01288       void
    01289       _M_fill_assign(size_type __n, const value_type& __val)
    01290       {
    01291     if (__n > size())
    01292       {
    01293         std::fill(begin(), end(), __val);
    01294         insert(end(), __n - size(), __val);
    01295       }
    01296     else
    01297       {
    01298         erase(begin() + __n, end());
    01299         std::fill(begin(), end(), __val);
    01300       }
    01301       }
    01302
    01303       //@{
    01304       /**
    01305        *  @if maint
    01306        *  @brief Helper functions for push_* and pop_*.
    01307        *  @endif
    01308        */
    01309       void _M_push_back_aux(const value_type&);
    01310       void _M_push_front_aux(const value_type&);
    01311       void _M_pop_back_aux();
    01312       void _M_pop_front_aux();
    01313       //@}
    01314
    01315       // Internal insert functions follow.  The *_aux functions do the actual
    01316       // insertion work when all shortcuts fail.
    01317
    01318       // called by the range insert to implement [23.1.1]/9
    01319       template<typename _Integer>
    01320         void
    01321         _M_insert_dispatch(iterator __pos,
    01322                _Integer __n, _Integer __x, __true_type)
    01323         {
    01324       _M_fill_insert(__pos, static_cast<size_type>(__n),
    01325              static_cast<value_type>(__x));
    01326     }
    01327
    01328       // called by the range insert to implement [23.1.1]/9
    01329       template<typename _InputIterator>
    01330         void
    01331         _M_insert_dispatch(iterator __pos,
    01332                _InputIterator __first, _InputIterator __last,
    01333                __false_type)
    01334         {
    01335       typedef typename iterator_traits<_InputIterator>::iterator_category
    01336         _IterCategory;
    01337           _M_range_insert_aux(__pos, __first, __last, _IterCategory());
    01338     }
    01339
    01340       // called by the second insert_dispatch above
    01341       template<typename _InputIterator>
    01342         void
    01343         _M_range_insert_aux(iterator __pos, _InputIterator __first,
    01344                 _InputIterator __last, input_iterator_tag);
    01345
    01346       // called by the second insert_dispatch above
    01347       template<typename _ForwardIterator>
    01348         void
    01349         _M_range_insert_aux(iterator __pos, _ForwardIterator __first,
    01350                 _ForwardIterator __last, forward_iterator_tag);
    01351
    01352       // Called by insert(p,n,x), and the range insert when it turns out to be
    01353       // the same thing.  Can use fill functions in optimal situations,
    01354       // otherwise passes off to insert_aux(p,n,x).
    01355       void
    01356       _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
    01357
    01358       // called by insert(p,x)
    01359       iterator
    01360       _M_insert_aux(iterator __pos, const value_type& __x);
    01361
    01362       // called by insert(p,n,x) via fill_insert
    01363       void
    01364       _M_insert_aux(iterator __pos, size_type __n, const value_type& __x);
    01365
    01366       // called by range_insert_aux for forward iterators
    01367       template<typename _ForwardIterator>
    01368         void
    01369         _M_insert_aux(iterator __pos,
    01370               _ForwardIterator __first, _ForwardIterator __last,
    01371               size_type __n);
    01372
    01373       //@{
    01374       /**
    01375        *  @if maint
    01376        *  @brief Memory-handling helpers for the previous internal insert
    01377        *         functions.
    01378        *  @endif
    01379        */
    01380       iterator
    01381       _M_reserve_elements_at_front(size_type __n)
    01382       {
    01383     const size_type __vacancies = this->_M_impl._M_start._M_cur
    01384                                   - this->_M_impl._M_start._M_first;
    01385     if (__n > __vacancies)
    01386       _M_new_elements_at_front(__n - __vacancies);
    01387     return this->_M_impl._M_start - difference_type(__n);
    01388       }
    01389
    01390       iterator
    01391       _M_reserve_elements_at_back(size_type __n)
    01392       {
    01393     const size_type __vacancies = (this->_M_impl._M_finish._M_last
    01394                        - this->_M_impl._M_finish._M_cur) - 1;
    01395     if (__n > __vacancies)
    01396       _M_new_elements_at_back(__n - __vacancies);
    01397     return this->_M_impl._M_finish + difference_type(__n);
    01398       }
    01399
    01400       void
    01401       _M_new_elements_at_front(size_type __new_elements);
    01402
    01403       void
    01404       _M_new_elements_at_back(size_type __new_elements);
    01405       //@}
    01406
    01407
    01408       //@{
    01409       /**
    01410        *  @if maint
    01411        *  @brief Memory-handling helpers for the major %map.
    01412        *
    01413        *  Makes sure the _M_map has space for new nodes.  Does not actually add
    01414        *  the nodes.  Can invalidate _M_map pointers.  (And consequently, %deque
    01415        *  iterators.)
    01416        *  @endif
    01417        */
    01418       void
    01419       _M_reserve_map_at_back (size_type __nodes_to_add = 1)
    01420       {
    01421     if (__nodes_to_add + 1 > this->_M_impl._M_map_size
    01422         - (this->_M_impl._M_finish._M_node - this->_M_impl._M_map))
    01423       _M_reallocate_map(__nodes_to_add, false);
    01424       }
    01425
    01426       void
    01427       _M_reserve_map_at_front (size_type __nodes_to_add = 1)
    01428       {
    01429     if (__nodes_to_add > size_type(this->_M_impl._M_start._M_node
    01430                        - this->_M_impl._M_map))
    01431       _M_reallocate_map(__nodes_to_add, true);
    01432       }
    01433
    01434       void
    01435       _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front);
    01436       //@}
    01437     };
    01438
    01439
    01440   /**
    01441    *  @brief  Deque equality comparison.
    01442    *  @param  x  A %deque.
    01443    *  @param  y  A %deque of the same type as @a x.
    01444    *  @return  True iff the size and elements of the deques are equal.
    01445    *
    01446    *  This is an equivalence relation.  It is linear in the size of the
    01447    *  deques.  Deques are considered equivalent if their sizes are equal,
    01448    *  and if corresponding elements compare equal.
    01449   */
    01450   template<typename _Tp, typename _Alloc>
    01451     inline bool
    01452     operator==(const deque<_Tp, _Alloc>& __x,
    01453                          const deque<_Tp, _Alloc>& __y)
    01454     { return __x.size() == __y.size()
    01455              && std::equal(__x.begin(), __x.end(), __y.begin()); }
    01456
    01457   /**
    01458    *  @brief  Deque ordering relation.
    01459    *  @param  x  A %deque.
    01460    *  @param  y  A %deque of the same type as @a x.
    01461    *  @return  True iff @a x is lexicographically less than @a y.
    01462    *
    01463    *  This is a total ordering relation.  It is linear in the size of the
    01464    *  deques.  The elements must be comparable with @c <.
    01465    *
    01466    *  See std::lexicographical_compare() for how the determination is made.
    01467   */
    01468   template<typename _Tp, typename _Alloc>
    01469     inline bool
    01470     operator<(const deque<_Tp, _Alloc>& __x,
    01471           const deque<_Tp, _Alloc>& __y)
    01472     { return lexicographical_compare(__x.begin(), __x.end(),
    01473                      __y.begin(), __y.end()); }
    01474
    01475   /// Based on operator==
    01476   template<typename _Tp, typename _Alloc>
    01477     inline bool
    01478     operator!=(const deque<_Tp, _Alloc>& __x,
    01479            const deque<_Tp, _Alloc>& __y)
    01480     { return !(__x == __y); }
    01481
    01482   /// Based on operator<
    01483   template<typename _Tp, typename _Alloc>
    01484     inline bool
    01485     operator>(const deque<_Tp, _Alloc>& __x,
    01486           const deque<_Tp, _Alloc>& __y)
    01487     { return __y < __x; }
    01488
    01489   /// Based on operator<
    01490   template<typename _Tp, typename _Alloc>
    01491     inline bool
    01492     operator<=(const deque<_Tp, _Alloc>& __x,
    01493            const deque<_Tp, _Alloc>& __y)
    01494     { return !(__y < __x); }
    01495
    01496   /// Based on operator<
    01497   template<typename _Tp, typename _Alloc>
    01498     inline bool
    01499     operator>=(const deque<_Tp, _Alloc>& __x,
    01500            const deque<_Tp, _Alloc>& __y)
    01501     { return !(__x < __y); }
    01502
    01503   /// See std::deque::swap().
    01504   template<typename _Tp, typename _Alloc>
    01505     inline void
    01506     swap(deque<_Tp,_Alloc>& __x, deque<_Tp,_Alloc>& __y)
    01507     { __x.swap(__y); }
    01508 } // namespace std
    01509
    01510 #endif /* _DEQUE_H */