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

Generated on 21 Dec 2009 for libstdc++ by  doxygen 1.6.1