# stl_deque.h

Go to the documentation of this file.

    00001 // Deque implementation -*- C++ -*-
    00002
    00003 // Copyright (C) 2001, 2002 Free Software Foundation, Inc.
    00004 //
    00005 // This file is part of the GNU ISO C++ Library.  This library is free
    00006 // software; you can redistribute it and/or modify it under the
    00007 // terms of the GNU General Public License as published by the
    00008 // Free Software Foundation; either version 2, or (at your option)
    00009 // any later version.
    00010
    00011 // This library is distributed in the hope that it will be useful,
    00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00014 // GNU General Public License for more details.
    00015
    00016 // You should have received a copy of the GNU General Public License along
    00017 // with this library; see the file COPYING.  If not, write to the Free
    00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
    00019 // USA.
    00020
    00021 // As a special exception, you may use this file as part of a free software
    00022 // library without restriction.  Specifically, if other files instantiate
    00023 // templates or use macros or inline functions from this file, or you compile
    00024 // this file and link it with other files to produce an executable, this
    00025 // file does not by itself cause the resulting executable to be covered by
    00026 // the GNU General Public License.  This exception does not however
    00027 // invalidate any other reasons why the executable file might be covered by
    00028 // the GNU General Public License.
    00029
    00030 /*
    00031  *
    00032  * Copyright (c) 1994
    00033  * Hewlett-Packard Company
    00034  *
    00035  * Permission to use, copy, modify, distribute and sell this software
    00036  * and its documentation for any purpose is hereby granted without fee,
    00037  * provided that the above copyright notice appear in all copies and
    00038  * that both that copyright notice and this permission notice appear
    00039  * in supporting documentation.  Hewlett-Packard Company makes no
    00040  * representations about the suitability of this software for any
    00041  * purpose.  It is provided "as is" without express or implied warranty.
    00042  *
    00043  *
    00044  * Copyright (c) 1997
    00045  * Silicon Graphics Computer Systems, Inc.
    00046  *
    00047  * Permission to use, copy, modify, distribute and sell this software
    00048  * and its documentation for any purpose is hereby granted without fee,
    00049  * provided that the above copyright notice appear in all copies and
    00050  * that both that copyright notice and this permission notice appear
    00051  * in supporting documentation.  Silicon Graphics makes no
    00052  * representations about the suitability of this software for any
    00053  * purpose.  It is provided "as is" without express or implied warranty.
    00054  */
    00055
    00061 #ifndef __GLIBCPP_INTERNAL_DEQUE_H
    00062 #define __GLIBCPP_INTERNAL_DEQUE_H
    00063
    00064 #include <bits/concept_check.h>
    00065 #include <bits/stl_iterator_base_types.h>
    00066 #include <bits/stl_iterator_base_funcs.h>
    00067
    00068 namespace std
    00069 {
    00082   inline size_t
    00083   __deque_buf_size(size_t __size)
    00084   { return __size < 512 ? size_t(512 / __size) : size_t(1); }
    00085
    00086
    00099   template <typename _Tp, typename _Ref, typename _Ptr>
    00100     struct _Deque_iterator
    00101   {
    00102     typedef _Deque_iterator<_Tp, _Tp&, _Tp*>             iterator;
    00103     typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
    00104     static size_t _S_buffer_size() { return __deque_buf_size(sizeof(_Tp)); }
    00105
    00106     typedef random_access_iterator_tag iterator_category;
    00107     typedef _Tp                        value_type;
    00108     typedef _Ptr                       pointer;
    00109     typedef _Ref                       reference;
    00110     typedef size_t                     size_type;
    00111     typedef ptrdiff_t                  difference_type;
    00112     typedef _Tp**                      _Map_pointer;
    00113     typedef _Deque_iterator            _Self;
    00114
    00115     _Tp* _M_cur;
    00116     _Tp* _M_first;
    00117     _Tp* _M_last;
    00118     _Map_pointer _M_node;
    00119
    00120     _Deque_iterator(_Tp* __x, _Map_pointer __y)
    00121       : _M_cur(__x), _M_first(*__y),
    00122         _M_last(*__y + _S_buffer_size()), _M_node(__y) {}
    00123     _Deque_iterator() : _M_cur(0), _M_first(0), _M_last(0), _M_node(0) {}
    00124     _Deque_iterator(const iterator& __x)
    00125       : _M_cur(__x._M_cur), _M_first(__x._M_first),
    00126         _M_last(__x._M_last), _M_node(__x._M_node) {}
    00127
    00128     reference operator*() const { return *_M_cur; }
    00129     pointer operator->() const { return _M_cur; }
    00130
    00131     _Self& operator++() {
    00132       ++_M_cur;
    00133       if (_M_cur == _M_last) {
    00134         _M_set_node(_M_node + 1);
    00135         _M_cur = _M_first;
    00136       }
    00137       return *this;
    00138     }
    00139     _Self operator++(int)  {
    00140       _Self __tmp = *this;
    00141       ++*this;
    00142       return __tmp;
    00143     }
    00144
    00145     _Self& operator--() {
    00146       if (_M_cur == _M_first) {
    00147         _M_set_node(_M_node - 1);
    00148         _M_cur = _M_last;
    00149       }
    00150       --_M_cur;
    00151       return *this;
    00152     }
    00153     _Self operator--(int) {
    00154       _Self __tmp = *this;
    00155       --*this;
    00156       return __tmp;
    00157     }
    00158
    00159     _Self& operator+=(difference_type __n)
    00160     {
    00161       difference_type __offset = __n + (_M_cur - _M_first);
    00162       if (__offset >= 0 && __offset < difference_type(_S_buffer_size()))
    00163         _M_cur += __n;
    00164       else {
    00165         difference_type __node_offset =
    00166           __offset > 0 ? __offset / difference_type(_S_buffer_size())
    00167                      : -difference_type((-__offset - 1) / _S_buffer_size()) - 1;
    00168         _M_set_node(_M_node + __node_offset);
    00169         _M_cur = _M_first +
    00170           (__offset - __node_offset * difference_type(_S_buffer_size()));
    00171       }
    00172       return *this;
    00173     }
    00174
    00175     _Self operator+(difference_type __n) const
    00176     {
    00177       _Self __tmp = *this;
    00178       return __tmp += __n;
    00179     }
    00180
    00181     _Self& operator-=(difference_type __n) { return *this += -__n; }
    00182
    00183     _Self operator-(difference_type __n) const {
    00184       _Self __tmp = *this;
    00185       return __tmp -= __n;
    00186     }
    00187
    00188     reference operator[](difference_type __n) const { return *(*this + __n); }
    00189
    00196     void
    00197     _M_set_node(_Map_pointer __new_node)
    00198     {
    00199       _M_node = __new_node;
    00200       _M_first = *__new_node;
    00201       _M_last = _M_first + difference_type(_S_buffer_size());
    00202     }
    00203   };
    00204
    00205   // Note: we also provide overloads whose operands are of the same type in
    00206   // order to avoid ambiguous overload resolution when std::rel_ops operators
    00207   // are in scope (for additional details, see libstdc++/3628)
    00208   template <typename _Tp, typename _Ref, typename _Ptr>
    00209   inline bool
    00210   operator==(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
    00211        const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
    00212   {
    00213     return __x._M_cur == __y._M_cur;
    00214   }
    00215
    00216   template <typename _Tp, typename _RefL, typename _PtrL,
    00217                           typename _RefR, typename _PtrR>
    00218   inline bool
    00219   operator==(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
    00220        const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
    00221   {
    00222     return __x._M_cur == __y._M_cur;
    00223   }
    00224
    00225   template <typename _Tp, typename _Ref, typename _Ptr>
    00226   inline bool
    00227   operator!=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
    00228        const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
    00229   {
    00230     return !(__x == __y);
    00231   }
    00232
    00233   template <typename _Tp, typename _RefL, typename _PtrL,
    00234                           typename _RefR, typename _PtrR>
    00235   inline bool
    00236   operator!=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
    00237        const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
    00238   {
    00239     return !(__x == __y);
    00240   }
    00241
    00242   template <typename _Tp, typename _Ref, typename _Ptr>
    00243   inline bool
    00244   operator<(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
    00245        const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
    00246   {
    00247     return (__x._M_node == __y._M_node) ?
    00248       (__x._M_cur < __y._M_cur) : (__x._M_node < __y._M_node);
    00249   }
    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   {
    00257     return (__x._M_node == __y._M_node) ?
    00258       (__x._M_cur < __y._M_cur) : (__x._M_node < __y._M_node);
    00259   }
    00260
    00261   template <typename _Tp, typename _Ref, typename _Ptr>
    00262   inline bool
    00263   operator>(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
    00264        const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
    00265   {
    00266     return __y < __x;
    00267   }
    00268
    00269   template <typename _Tp, typename _RefL, typename _PtrL,
    00270                           typename _RefR, typename _PtrR>
    00271   inline bool
    00272   operator>(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
    00273        const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
    00274   {
    00275     return __y < __x;
    00276   }
    00277
    00278   template <typename _Tp, typename _Ref, typename _Ptr>
    00279   inline bool
    00280   operator<=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
    00281        const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
    00282   {
    00283     return !(__y < __x);
    00284   }
    00285
    00286   template <typename _Tp, typename _RefL, typename _PtrL,
    00287                           typename _RefR, typename _PtrR>
    00288   inline bool
    00289   operator<=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
    00290        const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
    00291   {
    00292     return !(__y < __x);
    00293   }
    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   {
    00300     return !(__x < __y);
    00301   }
    00302
    00303   template <typename _Tp, typename _RefL, typename _PtrL,
    00304                           typename _RefR, typename _PtrR>
    00305   inline bool
    00306   operator>=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
    00307        const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
    00308   {
    00309     return !(__x < __y);
    00310   }
    00311
    00312   // _GLIBCPP_RESOLVE_LIB_DEFECTS
    00313   // According to the resolution of DR179 not only the various comparison
    00314   // operators but also operator- must accept mixed iterator/const_iterator
    00315   // parameters.
    00316   template <typename _Tp, typename _RefL, typename _PtrL,
    00317                           typename _RefR, typename _PtrR>
    00318   inline typename _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type
    00319   operator-(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
    00320       const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
    00321   {
    00322     return _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type
    00323       (_Deque_iterator<_Tp, _RefL, _PtrL>::_S_buffer_size()) *
    00324       (__x._M_node - __y._M_node - 1) + (__x._M_cur - __x._M_first) +
    00325       (__y._M_last - __y._M_cur);
    00326   }
    00327
    00328   template <typename _Tp, typename _Ref, typename _Ptr>
    00329   inline _Deque_iterator<_Tp, _Ref, _Ptr>
    00330   operator+(ptrdiff_t __n, const _Deque_iterator<_Tp, _Ref, _Ptr>& __x)
    00331   {
    00332     return __x + __n;
    00333   }
    00334
    00335
    00337
    00348   template <typename _Tp, typename _Alloc, bool __is_static>
    00349     class _Deque_alloc_base
    00350   {
    00351   public:
    00352     typedef typename _Alloc_traits<_Tp,_Alloc>::allocator_type allocator_type;
    00353     allocator_type get_allocator() const { return _M_node_allocator; }
    00354
    00355     _Deque_alloc_base(const allocator_type& __a)
    00356       : _M_node_allocator(__a), _M_map_allocator(__a),
    00357         _M_map(0), _M_map_size(0)
    00358     {}
    00359
    00360   protected:
    00361     typedef typename _Alloc_traits<_Tp*, _Alloc>::allocator_type
    00362             _Map_allocator_type;
    00363
    00364     _Tp*
    00365     _M_allocate_node()
    00366     {
    00367       return _M_node_allocator.allocate(__deque_buf_size(sizeof(_Tp)));
    00368     }
    00369
    00370     void
    00371     _M_deallocate_node(_Tp* __p)
    00372     {
    00373       _M_node_allocator.deallocate(__p, __deque_buf_size(sizeof(_Tp)));
    00374     }
    00375
    00376     _Tp**
    00377     _M_allocate_map(size_t __n)
    00378       { return _M_map_allocator.allocate(__n); }
    00379
    00380     void
    00381     _M_deallocate_map(_Tp** __p, size_t __n)
    00382       { _M_map_allocator.deallocate(__p, __n); }
    00383
    00384     allocator_type       _M_node_allocator;
    00385     _Map_allocator_type  _M_map_allocator;
    00386     _Tp**                _M_map;
    00387     size_t               _M_map_size;
    00388   };
    00389
    00391   template <typename _Tp, typename _Alloc>
    00392     class _Deque_alloc_base<_Tp, _Alloc, true>
    00393   {
    00394   public:
    00395     typedef typename _Alloc_traits<_Tp,_Alloc>::allocator_type allocator_type;
    00396     allocator_type get_allocator() const { return allocator_type(); }
    00397
    00398     _Deque_alloc_base(const allocator_type&)
    00399       : _M_map(0), _M_map_size(0)
    00400     {}
    00401
    00402   protected:
    00403     typedef typename _Alloc_traits<_Tp,_Alloc>::_Alloc_type  _Node_alloc_type;
    00404     typedef typename _Alloc_traits<_Tp*,_Alloc>::_Alloc_type _Map_alloc_type;
    00405
    00406     _Tp*
    00407     _M_allocate_node()
    00408     {
    00409       return _Node_alloc_type::allocate(__deque_buf_size(sizeof(_Tp)));
    00410     }
    00411
    00412     void
    00413     _M_deallocate_node(_Tp* __p)
    00414     {
    00415       _Node_alloc_type::deallocate(__p, __deque_buf_size(sizeof(_Tp)));
    00416     }
    00417
    00418     _Tp**
    00419     _M_allocate_map(size_t __n)
    00420       { return _Map_alloc_type::allocate(__n); }
    00421
    00422     void
    00423     _M_deallocate_map(_Tp** __p, size_t __n)
    00424       { _Map_alloc_type::deallocate(__p, __n); }
    00425
    00426     _Tp**   _M_map;
    00427     size_t  _M_map_size;
    00428   };
    00429
    00430
    00442   template <typename _Tp, typename _Alloc>
    00443     class _Deque_base
    00444     : public _Deque_alloc_base<_Tp,_Alloc,
    00445                                 _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
    00446   {
    00447   public:
    00448     typedef _Deque_alloc_base<_Tp,_Alloc,
    00449                                _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
    00450             _Base;
    00451     typedef typename _Base::allocator_type             allocator_type;
    00452     typedef _Deque_iterator<_Tp,_Tp&,_Tp*>             iterator;
    00453     typedef _Deque_iterator<_Tp,const _Tp&,const _Tp*> const_iterator;
    00454
    00455     _Deque_base(const allocator_type& __a, size_t __num_elements)
    00456       : _Base(__a), _M_start(), _M_finish()
    00457       { _M_initialize_map(__num_elements); }
    00458     _Deque_base(const allocator_type& __a)
    00459       : _Base(__a), _M_start(), _M_finish() {}
    00460     ~_Deque_base();
    00461
    00462   protected:
    00463     void _M_initialize_map(size_t);
    00464     void _M_create_nodes(_Tp** __nstart, _Tp** __nfinish);
    00465     void _M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish);
    00466     enum { _S_initial_map_size = 8 };
    00467
    00468     iterator _M_start;
    00469     iterator _M_finish;
    00470   };
    00471
    00472
    00473   template <typename _Tp, typename _Alloc>
    00474   _Deque_base<_Tp,_Alloc>::~_Deque_base()
    00475   {
    00476     if (_M_map)
    00477     {
    00478       _M_destroy_nodes(_M_start._M_node, _M_finish._M_node + 1);
    00479       _M_deallocate_map(_M_map, _M_map_size);
    00480     }
    00481   }
    00482
    00493   template <typename _Tp, typename _Alloc>
    00494   void
    00495   _Deque_base<_Tp,_Alloc>::_M_initialize_map(size_t __num_elements)
    00496   {
    00497     size_t __num_nodes =
    00498       __num_elements / __deque_buf_size(sizeof(_Tp)) + 1;
    00499
    00500     _M_map_size = max((size_t) _S_initial_map_size, __num_nodes + 2);
    00501     _M_map = _M_allocate_map(_M_map_size);
    00502
    00503     // For "small" maps (needing less than _M_map_size nodes), allocation
    00504     // starts in the middle elements and grows outwards.  So nstart may be the
    00505     // beginning of _M_map, but for small maps it may be as far in as _M_map+3.
    00506
    00507     _Tp** __nstart = _M_map + (_M_map_size - __num_nodes) / 2;
    00508     _Tp** __nfinish = __nstart + __num_nodes;
    00509
    00510     try
    00511       { _M_create_nodes(__nstart, __nfinish); }
    00512     catch(...)
    00513       {
    00514         _M_deallocate_map(_M_map, _M_map_size);
    00515         _M_map = 0;
    00516         _M_map_size = 0;
    00517         __throw_exception_again;
    00518       }
    00519
    00520     _M_start._M_set_node(__nstart);
    00521     _M_finish._M_set_node(__nfinish - 1);
    00522     _M_start._M_cur = _M_start._M_first;
    00523     _M_finish._M_cur = _M_finish._M_first +
    00524                        __num_elements % __deque_buf_size(sizeof(_Tp));
    00525   }
    00526
    00527   template <typename _Tp, typename _Alloc>
    00528   void _Deque_base<_Tp,_Alloc>::_M_create_nodes(_Tp** __nstart, _Tp** __nfinish)
    00529   {
    00530     _Tp** __cur;
    00531     try
    00532       {
    00533         for (__cur = __nstart; __cur < __nfinish; ++__cur)
    00534           *__cur = _M_allocate_node();
    00535       }
    00536     catch(...)
    00537       {
    00538         _M_destroy_nodes(__nstart, __cur);
    00539         __throw_exception_again;
    00540       }
    00541   }
    00542
    00543   template <typename _Tp, typename _Alloc>
    00544   void
    00545   _Deque_base<_Tp,_Alloc>::_M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish)
    00546   {
    00547     for (_Tp** __n = __nstart; __n < __nfinish; ++__n)
    00548       _M_deallocate_node(*__n);
    00549   }
    00550
    00551
    00636   template <typename _Tp, typename _Alloc = allocator<_Tp> >
    00637     class deque : protected _Deque_base<_Tp, _Alloc>
    00638   {
    00639     // concept requirements
    00640     __glibcpp_class_requires(_Tp, _SGIAssignableConcept)
    00641
    00642     typedef _Deque_base<_Tp, _Alloc>           _Base;
    00643
    00644   public:
    00645     typedef _Tp                                value_type;
    00646     typedef value_type*                        pointer;
    00647     typedef const value_type*                  const_pointer;
    00648     typedef typename _Base::iterator           iterator;
    00649     typedef typename _Base::const_iterator     const_iterator;
    00650     typedef std::reverse_iterator<const_iterator>   const_reverse_iterator;
    00651     typedef std::reverse_iterator<iterator>         reverse_iterator;
    00652     typedef value_type&                        reference;
    00653     typedef const value_type&                  const_reference;
    00654     typedef size_t                             size_type;
    00655     typedef ptrdiff_t                          difference_type;
    00656     typedef typename _Base::allocator_type     allocator_type;
    00657
    00658   protected:
    00659     typedef pointer*                           _Map_pointer;
    00660     static size_t _S_buffer_size() { return __deque_buf_size(sizeof(_Tp)); }
    00661
    00662     // Functions controlling memory layout, and nothing else.
    00663     using _Base::_M_initialize_map;
    00664     using _Base::_M_create_nodes;
    00665     using _Base::_M_destroy_nodes;
    00666     using _Base::_M_allocate_node;
    00667     using _Base::_M_deallocate_node;
    00668     using _Base::_M_allocate_map;
    00669     using _Base::_M_deallocate_map;
    00670
    00677     using _Base::_M_map;
    00678     using _Base::_M_map_size;
    00679     using _Base::_M_start;
    00680     using _Base::_M_finish;
    00681
    00682   public:
    00683     // [23.2.1.1] construct/copy/destroy
    00684     // (assign() and get_allocator() are also listed in this section)
    00688     explicit
    00689     deque(const allocator_type& __a = allocator_type())
    00690       : _Base(__a, 0) {}
    00691
    00699     deque(size_type __n, const value_type& __value,
    00700           const allocator_type& __a = allocator_type())
    00701       : _Base(__a, __n)
    00702       { _M_fill_initialize(__value); }
    00703
    00711     explicit
    00712     deque(size_type __n)
    00713       : _Base(allocator_type(), __n)
    00714       { _M_fill_initialize(value_type()); }
    00715
    00723     deque(const deque& __x)
    00724       : _Base(__x.get_allocator(), __x.size())
    00725       { uninitialized_copy(__x.begin(), __x.end(), _M_start); }
    00726
    00740     template<typename _InputIterator>
    00741       deque(_InputIterator __first, _InputIterator __last,
    00742             const allocator_type& __a = allocator_type())
    00743         : _Base(__a)
    00744       {
    00745         // Check whether it's an integral type.  If so, it's not an iterator.
    00746         typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
    00747         _M_initialize_dispatch(__first, __last, _Integral());
    00748       }
    00749
    00755     ~deque() { _Destroy(_M_start, _M_finish); }
    00756
    00764     deque&
    00765     operator=(const deque& __x);
    00766
    00777     void
    00778     assign(size_type __n, const value_type& __val) { _M_fill_assign(__n, __val); }
    00779
    00792     template<typename _InputIterator>
    00793       void
    00794       assign(_InputIterator __first, _InputIterator __last)
    00795       {
    00796         typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
    00797         _M_assign_dispatch(__first, __last, _Integral());
    00798       }
    00799
    00801     allocator_type
    00802     get_allocator() const { return _Base::get_allocator(); }
    00803
    00804     // iterators
    00809     iterator
    00810     begin() { return _M_start; }
    00811
    00816     const_iterator
    00817     begin() const { return _M_start; }
    00818
    00823     iterator
    00824     end() { return _M_finish; }
    00825
    00830     const_iterator
    00831     end() const { return _M_finish; }
    00832
    00837     reverse_iterator
    00838     rbegin() { return reverse_iterator(_M_finish); }
    00839
    00844     const_reverse_iterator
    00845     rbegin() const { return const_reverse_iterator(_M_finish); }
    00846
    00852     reverse_iterator
    00853     rend() { return reverse_iterator(_M_start); }
    00854
    00860     const_reverse_iterator
    00861     rend() const { return const_reverse_iterator(_M_start); }
    00862
    00863     // [23.2.1.2] capacity
    00865     size_type
    00866     size() const { return _M_finish - _M_start; }
    00867
    00869     size_type
    00870     max_size() const { return size_type(-1); }
    00871
    00882     void
    00883     resize(size_type __new_size, const value_type& __x)
    00884     {
    00885       const size_type __len = size();
    00886       if (__new_size < __len)
    00887         erase(_M_start + __new_size, _M_finish);
    00888       else
    00889         insert(_M_finish, __new_size - __len, __x);
    00890     }
    00891
    00901     void
    00902     resize(size_type new_size) { resize(new_size, value_type()); }
    00903
    00907     bool empty() const { return _M_finish == _M_start; }
    00908
    00909     // element access
    00919     reference
    00920     operator[](size_type __n) { return _M_start[difference_type(__n)]; }
    00921
    00931     const_reference
    00932     operator[](size_type __n) const { return _M_start[difference_type(__n)]; }
    00933
    00934   protected:
    00936     void
    00937     _M_range_check(size_type __n) const
    00938     {
    00939       if (__n >= this->size())
    00940         __throw_out_of_range("deque [] access out of range");
    00941     }
    00942
    00943   public:
    00954     reference
    00955     at(size_type __n) { _M_range_check(__n); return (*this)[__n]; }
    00956
    00967     const_reference
    00968     at(size_type __n) const { _M_range_check(__n); return (*this)[__n]; }
    00969
    00974     reference
    00975     front() { return *_M_start; }
    00976
    00981     const_reference
    00982     front() const { return *_M_start; }
    00983
    00988     reference
    00989     back()
    00990     {
    00991       iterator __tmp = _M_finish;
    00992       --__tmp;
    00993       return *__tmp;
    00994     }
    00995
    01000     const_reference
    01001     back() const
    01002     {
    01003       const_iterator __tmp = _M_finish;
    01004       --__tmp;
    01005       return *__tmp;
    01006     }
    01007
    01008     // [23.2.1.2] modifiers
    01017     void
    01018     push_front(const value_type& __x)
    01019     {
    01020       if (_M_start._M_cur != _M_start._M_first) {
    01021         _Construct(_M_start._M_cur - 1, __x);
    01022         --_M_start._M_cur;
    01023       }
    01024       else
    01025         _M_push_front_aux(__x);
    01026     }
    01027
    01028   #ifdef _GLIBCPP_DEPRECATED
    01029
    01041     void
    01042     push_front()
    01043     {
    01044       if (_M_start._M_cur != _M_start._M_first) {
    01045         _Construct(_M_start._M_cur - 1);
    01046         --_M_start._M_cur;
    01047       }
    01048       else
    01049         _M_push_front_aux();
    01050     }
    01051   #endif
    01052
    01061     void
    01062     push_back(const value_type& __x)
    01063     {
    01064       if (_M_finish._M_cur != _M_finish._M_last - 1) {
    01065         _Construct(_M_finish._M_cur, __x);
    01066         ++_M_finish._M_cur;
    01067       }
    01068       else
    01069         _M_push_back_aux(__x);
    01070     }
    01071
    01072   #ifdef _GLIBCPP_DEPRECATED
    01073
    01085     void
    01086     push_back()
    01087     {
    01088       if (_M_finish._M_cur != _M_finish._M_last - 1) {
    01089         _Construct(_M_finish._M_cur);
    01090         ++_M_finish._M_cur;
    01091       }
    01092       else
    01093         _M_push_back_aux();
    01094     }
    01095   #endif
    01096
    01105     void
    01106     pop_front()
    01107     {
    01108       if (_M_start._M_cur != _M_start._M_last - 1) {
    01109         _Destroy(_M_start._M_cur);
    01110         ++_M_start._M_cur;
    01111       }
    01112       else
    01113         _M_pop_front_aux();
    01114     }
    01115
    01124     void
    01125     pop_back()
    01126     {
    01127       if (_M_finish._M_cur != _M_finish._M_first) {
    01128         --_M_finish._M_cur;
    01129         _Destroy(_M_finish._M_cur);
    01130       }
    01131       else
    01132         _M_pop_back_aux();
    01133     }
    01134
    01144     iterator
    01145     insert(iterator position, const value_type& __x);
    01146
    01147   #ifdef _GLIBCPP_DEPRECATED
    01148
    01161     iterator
    01162     insert(iterator __position)
    01163     { return insert(__position, value_type()); }
    01164   #endif
    01165
    01175     void
    01176     insert(iterator __position, size_type __n, const value_type& __x)
    01177     { _M_fill_insert(__position, __n, __x); }
    01178
    01189     template<typename _InputIterator>
    01190       void
    01191       insert(iterator __pos, _InputIterator __first, _InputIterator __last)
    01192       {
    01193         // Check whether it's an integral type.  If so, it's not an iterator.
    01194         typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
    01195         _M_insert_dispatch(__pos, __first, __last, _Integral());
    01196       }
    01197
    01211     iterator
    01212     erase(iterator __position);
    01213
    01230     iterator
    01231     erase(iterator __first, iterator __last);
    01232
    01242     void
    01243     swap(deque& __x)
    01244     {
    01245       std::swap(_M_start, __x._M_start);
    01246       std::swap(_M_finish, __x._M_finish);
    01247       std::swap(_M_map, __x._M_map);
    01248       std::swap(_M_map_size, __x._M_map_size);
    01249     }
    01250
    01257     void clear();
    01258
    01259   protected:
    01260     // Internal constructor functions follow.
    01261
    01262     // called by the range constructor to implement [23.1.1]/9
    01263     template<typename _Integer>
    01264       void
    01265       _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
    01266       {
    01267         _M_initialize_map(__n);
    01268         _M_fill_initialize(__x);
    01269       }
    01270
    01271     // called by the range constructor to implement [23.1.1]/9
    01272     template<typename _InputIter>
    01273       void
    01274       _M_initialize_dispatch(_InputIter __first, _InputIter __last,
    01275                              __false_type)
    01276       {
    01277         typedef typename iterator_traits<_InputIter>::iterator_category
    01278                          _IterCategory;
    01279         _M_range_initialize(__first, __last, _IterCategory());
    01280       }
    01281
    01282     // called by the second initialize_dispatch above
    01284
    01296     template <typename _InputIterator>
    01297       void
    01298       _M_range_initialize(_InputIterator __first, _InputIterator __last,
    01299                           input_iterator_tag);
    01300
    01301     // called by the second initialize_dispatch above
    01302     template <typename _ForwardIterator>
    01303       void
    01304       _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
    01305                           forward_iterator_tag);
    01307
    01320     void
    01321     _M_fill_initialize(const value_type& __value);
    01322
    01323
    01324     // Internal assign functions follow.  The *_aux functions do the actual
    01325     // assignment work for the range versions.
    01326
    01327     // called by the range assign to implement [23.1.1]/9
    01328     template<typename _Integer>
    01329       void
    01330       _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
    01331       {
    01332         _M_fill_assign(static_cast<size_type>(__n),
    01333                        static_cast<value_type>(__val));
    01334       }
    01335
    01336     // called by the range assign to implement [23.1.1]/9
    01337     template<typename _InputIter>
    01338       void
    01339       _M_assign_dispatch(_InputIter __first, _InputIter __last, __false_type)
    01340       {
    01341         typedef typename iterator_traits<_InputIter>::iterator_category
    01342                          _IterCategory;
    01343         _M_assign_aux(__first, __last, _IterCategory());
    01344       }
    01345
    01346     // called by the second assign_dispatch above
    01347     template <typename _InputIterator>
    01348       void
    01349       _M_assign_aux(_InputIterator __first, _InputIterator __last,
    01350                     input_iterator_tag);
    01351
    01352     // called by the second assign_dispatch above
    01353     template <typename _ForwardIterator>
    01354       void
    01355       _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
    01356                     forward_iterator_tag)
    01357       {
    01358         size_type __len = distance(__first, __last);
    01359         if (__len > size()) {
    01360           _ForwardIterator __mid = __first;
    01361           advance(__mid, size());
    01362           copy(__first, __mid, begin());
    01363           insert(end(), __mid, __last);
    01364         }
    01365         else
    01366           erase(copy(__first, __last, begin()), end());
    01367       }
    01368
    01369     // Called by assign(n,t), and the range assign when it turns out to be the
    01370     // same thing.
    01371     void
    01372     _M_fill_assign(size_type __n, const value_type& __val)
    01373     {
    01374       if (__n > size())
    01375       {
    01376         fill(begin(), end(), __val);
    01377         insert(end(), __n - size(), __val);
    01378       }
    01379       else
    01380       {
    01381         erase(begin() + __n, end());
    01382         fill(begin(), end(), __val);
    01383       }
    01384     }
    01385
    01386
    01388
    01393     void _M_push_back_aux(const value_type&);
    01394     void _M_push_front_aux(const value_type&);
    01395   #ifdef _GLIBCPP_DEPRECATED
    01396     void _M_push_back_aux();
    01397     void _M_push_front_aux();
    01398   #endif
    01399     void _M_pop_back_aux();
    01400     void _M_pop_front_aux();
    01402
    01403
    01404     // Internal insert functions follow.  The *_aux functions do the actual
    01405     // insertion work when all shortcuts fail.
    01406
    01407     // called by the range insert to implement [23.1.1]/9
    01408     template<typename _Integer>
    01409       void
    01410       _M_insert_dispatch(iterator __pos,
    01411                          _Integer __n, _Integer __x, __true_type)
    01412       {
    01413         _M_fill_insert(__pos, static_cast<size_type>(__n),
    01414                        static_cast<value_type>(__x));
    01415       }
    01416
    01417     // called by the range insert to implement [23.1.1]/9
    01418     template<typename _InputIterator>
    01419       void
    01420       _M_insert_dispatch(iterator __pos,
    01421                          _InputIterator __first, _InputIterator __last,
    01422                          __false_type)
    01423       {
    01424         typedef typename iterator_traits<_InputIterator>::iterator_category
    01425                          _IterCategory;
    01426         _M_range_insert_aux(__pos, __first, __last, _IterCategory());
    01427       }
    01428
    01429     // called by the second insert_dispatch above
    01430     template <typename _InputIterator>
    01431       void
    01432       _M_range_insert_aux(iterator __pos, _InputIterator __first,
    01433                           _InputIterator __last, input_iterator_tag);
    01434
    01435     // called by the second insert_dispatch above
    01436     template <typename _ForwardIterator>
    01437       void
    01438       _M_range_insert_aux(iterator __pos, _ForwardIterator __first,
    01439                           _ForwardIterator __last, forward_iterator_tag);
    01440
    01441     // Called by insert(p,n,x), and the range insert when it turns out to be
    01442     // the same thing.  Can use fill functions in optimal situations, otherwise
    01443     // passes off to insert_aux(p,n,x).
    01444     void
    01445     _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
    01446
    01447     // called by insert(p,x)
    01448     iterator
    01449     _M_insert_aux(iterator __pos, const value_type& __x);
    01450
    01451     // called by insert(p,n,x) via fill_insert
    01452     void
    01453     _M_insert_aux(iterator __pos, size_type __n, const value_type& __x);
    01454
    01455     // called by range_insert_aux for forward iterators
    01456     template <typename _ForwardIterator>
    01457       void
    01458       _M_insert_aux(iterator __pos,
    01459                     _ForwardIterator __first, _ForwardIterator __last,
    01460                     size_type __n);
    01461
    01462   #ifdef _GLIBCPP_DEPRECATED
    01463     // unused, see comment in implementation
    01464     iterator _M_insert_aux(iterator __pos);
    01465   #endif
    01466
    01468
    01474     iterator
    01475     _M_reserve_elements_at_front(size_type __n)
    01476     {
    01477       size_type __vacancies = _M_start._M_cur - _M_start._M_first;
    01478       if (__n > __vacancies)
    01479         _M_new_elements_at_front(__n - __vacancies);
    01480       return _M_start - difference_type(__n);
    01481     }
    01482
    01483     iterator
    01484     _M_reserve_elements_at_back(size_type __n)
    01485     {
    01486       size_type __vacancies = (_M_finish._M_last - _M_finish._M_cur) - 1;
    01487       if (__n > __vacancies)
    01488         _M_new_elements_at_back(__n - __vacancies);
    01489       return _M_finish + difference_type(__n);
    01490     }
    01491
    01492     void
    01493     _M_new_elements_at_front(size_type __new_elements);
    01494
    01495     void
    01496     _M_new_elements_at_back(size_type __new_elements);
    01498
    01499
    01501
    01510     void
    01511     _M_reserve_map_at_back (size_type __nodes_to_add = 1)
    01512     {
    01513       if (__nodes_to_add + 1 > _M_map_size - (_M_finish._M_node - _M_map))
    01514         _M_reallocate_map(__nodes_to_add, false);
    01515     }
    01516
    01517     void
    01518     _M_reserve_map_at_front (size_type __nodes_to_add = 1)
    01519     {
    01520       if (__nodes_to_add > size_type(_M_start._M_node - _M_map))
    01521         _M_reallocate_map(__nodes_to_add, true);
    01522     }
    01523
    01524     void
    01525     _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front);
    01527   };
    01528
    01529
    01540   template <typename _Tp, typename _Alloc>
    01541   inline bool operator==(const deque<_Tp, _Alloc>& __x,
    01542                          const deque<_Tp, _Alloc>& __y)
    01543   {
    01544     return __x.size() == __y.size() &&
    01545            equal(__x.begin(), __x.end(), __y.begin());
    01546   }
    01547
    01559   template <typename _Tp, typename _Alloc>
    01560   inline bool operator<(const deque<_Tp, _Alloc>& __x,
    01561                         const deque<_Tp, _Alloc>& __y)
    01562   {
    01563     return lexicographical_compare(__x.begin(), __x.end(),
    01564                                    __y.begin(), __y.end());
    01565   }
    01566
    01568   template <typename _Tp, typename _Alloc>
    01569   inline bool operator!=(const deque<_Tp, _Alloc>& __x,
    01570                          const deque<_Tp, _Alloc>& __y) {
    01571     return !(__x == __y);
    01572   }
    01573
    01575   template <typename _Tp, typename _Alloc>
    01576   inline bool operator>(const deque<_Tp, _Alloc>& __x,
    01577                         const deque<_Tp, _Alloc>& __y) {
    01578     return __y < __x;
    01579   }
    01580
    01582   template <typename _Tp, typename _Alloc>
    01583   inline bool operator<=(const deque<_Tp, _Alloc>& __x,
    01584                          const deque<_Tp, _Alloc>& __y) {
    01585     return !(__y < __x);
    01586   }
    01587
    01589   template <typename _Tp, typename _Alloc>
    01590   inline bool operator>=(const deque<_Tp, _Alloc>& __x,
    01591                          const deque<_Tp, _Alloc>& __y) {
    01592     return !(__x < __y);
    01593   }
    01594
    01596   template <typename _Tp, typename _Alloc>
    01597   inline void swap(deque<_Tp,_Alloc>& __x, deque<_Tp,_Alloc>& __y)
    01598   {
    01599     __x.swap(__y);
    01600   }
    01601 } // namespace std
    01602
    01603 #endif /* __GLIBCPP_INTERNAL_DEQUE_H */