stl_deque.h

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

Generated on Fri May 6 01:09:08 2005 for libstdc++-v3 Source by  doxygen 1.4.2