stl_deque.h

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

Generated on Thu Nov 1 13:12:32 2007 for libstdc++ by  doxygen 1.5.1