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

Generated on Tue Dec 2 03:59:28 2008 for libstdc++ by  doxygen 1.5.7.1