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