stl_deque.h

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

Generated on Sat Apr 2 13:54:43 2005 for libstdc++ source by  doxygen 1.4.0