stl_deque.h

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

Generated on 11 Jan 2010 for libstdc++ by  doxygen 1.6.1