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