home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 14 / hacker14.iso / programacao / cwin / c.exe / $INSTDIR / include / c++ / bits / stl_deque.h < prev    next >
Encoding:
C/C++ Source or Header  |  2003-12-15  |  52.0 KB  |  1,683 lines

  1. // deque implementation -*- C++ -*-
  2.  
  3. // Copyright (C) 2001, 2002 Free Software Foundation, Inc.
  4. //
  5. // This file is part of the GNU ISO C++ Library.  This library is free
  6. // software; you can redistribute it and/or modify it under the
  7. // terms of the GNU General Public License as published by the
  8. // Free Software Foundation; either version 2, or (at your option)
  9. // any later version.
  10.  
  11. // This library is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. // GNU General Public License for more details.
  15.  
  16. // You should have received a copy of the GNU General Public License along
  17. // with this library; see the file COPYING.  If not, write to the Free
  18. // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  19. // USA.
  20.  
  21. // As a special exception, you may use this file as part of a free software
  22. // library without restriction.  Specifically, if other files instantiate
  23. // templates or use macros or inline functions from this file, or you compile
  24. // this file and link it with other files to produce an executable, this
  25. // file does not by itself cause the resulting executable to be covered by
  26. // the GNU General Public License.  This exception does not however
  27. // invalidate any other reasons why the executable file might be covered by
  28. // the GNU General Public License.
  29.  
  30. /*
  31.  *
  32.  * Copyright (c) 1994
  33.  * Hewlett-Packard Company
  34.  *
  35.  * Permission to use, copy, modify, distribute and sell this software
  36.  * and its documentation for any purpose is hereby granted without fee,
  37.  * provided that the above copyright notice appear in all copies and
  38.  * that both that copyright notice and this permission notice appear
  39.  * in supporting documentation.  Hewlett-Packard Company makes no
  40.  * representations about the suitability of this software for any
  41.  * purpose.  It is provided "as is" without express or implied warranty.
  42.  *
  43.  *
  44.  * Copyright (c) 1997
  45.  * Silicon Graphics Computer Systems, Inc.
  46.  *
  47.  * Permission to use, copy, modify, distribute and sell this software
  48.  * and its documentation for any purpose is hereby granted without fee,
  49.  * provided that the above copyright notice appear in all copies and
  50.  * that both that copyright notice and this permission notice appear
  51.  * in supporting documentation.  Silicon Graphics makes no
  52.  * representations about the suitability of this software for any
  53.  * purpose.  It is provided "as is" without express or implied warranty.
  54.  */
  55.  
  56. /** @file stl_deque.h
  57.  *  This is an internal header file, included by other library headers.
  58.  *  You should not attempt to use it directly.
  59.  */
  60.  
  61. #include <bits/concept_check.h>
  62. #include <bits/stl_iterator_base_types.h>
  63. #include <bits/stl_iterator_base_funcs.h>
  64.  
  65. #ifndef __GLIBCPP_INTERNAL_DEQUE_H
  66. #define __GLIBCPP_INTERNAL_DEQUE_H
  67.  
  68.  
  69. // Since this entire file is within namespace std, there's no reason to
  70. // waste two spaces along the left column.  Thus the leading indentation is
  71. // slightly violated from here on.
  72. namespace std
  73.  
  74. /**
  75.  *  @if maint
  76.  *  @brief This function controls the size of memory nodes.
  77.  *  @param  size  The size of an element.
  78.  *  @return   The number (not bytesize) of elements per node.
  79.  *
  80.  *  This function started off as a compiler kludge from SGI, but seems to
  81.  *  be a useful wrapper around a repeated constant expression.
  82.  *  @endif
  83. */
  84. inline size_t 
  85. __deque_buf_size(size_t __size) 
  86. { return __size < 512 ? size_t(512 / __size) : size_t(1); }
  87.  
  88.  
  89. /// A deque::iterator.
  90. /**
  91.  *  Quite a bit of intelligence here.  Much of the functionality of deque is
  92.  *  actually passed off to this class.  A deque holds two of these internally,
  93.  *  marking its valid range.  Access to elements is done as offsets of either
  94.  *  of those two, relying on operator overloading in this class.
  95.  *
  96.  *  @if maint
  97.  *  All the functions are op overloads except for _M_set_node.
  98.  *  @endif
  99. */
  100. template <class _Tp, class _Ref, class _Ptr>
  101. struct _Deque_iterator
  102. {
  103.   typedef _Deque_iterator<_Tp, _Tp&, _Tp*>             iterator;
  104.   typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
  105.   static size_t _S_buffer_size() { return __deque_buf_size(sizeof(_Tp)); }
  106.  
  107.   typedef random_access_iterator_tag iterator_category;
  108.   typedef _Tp                        value_type;
  109.   typedef _Ptr                       pointer;
  110.   typedef _Ref                       reference;
  111.   typedef size_t                     size_type;
  112.   typedef ptrdiff_t                  difference_type;
  113.   typedef _Tp**                      _Map_pointer;
  114.   typedef _Deque_iterator            _Self;
  115.  
  116.   _Tp* _M_cur;
  117.   _Tp* _M_first;
  118.   _Tp* _M_last;
  119.   _Map_pointer _M_node;
  120.  
  121.   _Deque_iterator(_Tp* __x, _Map_pointer __y) 
  122.     : _M_cur(__x), _M_first(*__y),
  123.       _M_last(*__y + _S_buffer_size()), _M_node(__y) {}
  124.   _Deque_iterator() : _M_cur(0), _M_first(0), _M_last(0), _M_node(0) {}
  125.   _Deque_iterator(const iterator& __x)
  126.     : _M_cur(__x._M_cur), _M_first(__x._M_first), 
  127.       _M_last(__x._M_last), _M_node(__x._M_node) {}
  128.  
  129.   reference operator*() const { return *_M_cur; }
  130.   pointer operator->() const { return _M_cur; }
  131.  
  132.   _Self& operator++() {
  133.     ++_M_cur;
  134.     if (_M_cur == _M_last) {
  135.       _M_set_node(_M_node + 1);
  136.       _M_cur = _M_first;
  137.     }
  138.     return *this; 
  139.   }
  140.   _Self operator++(int)  {
  141.     _Self __tmp = *this;
  142.     ++*this;
  143.     return __tmp;
  144.   }
  145.  
  146.   _Self& operator--() {
  147.     if (_M_cur == _M_first) {
  148.       _M_set_node(_M_node - 1);
  149.       _M_cur = _M_last;
  150.     }
  151.     --_M_cur;
  152.     return *this;
  153.   }
  154.   _Self operator--(int) {
  155.     _Self __tmp = *this;
  156.     --*this;
  157.     return __tmp;
  158.   }
  159.  
  160.   _Self& operator+=(difference_type __n)
  161.   {
  162.     difference_type __offset = __n + (_M_cur - _M_first);
  163.     if (__offset >= 0 && __offset < difference_type(_S_buffer_size()))
  164.       _M_cur += __n;
  165.     else {
  166.       difference_type __node_offset =
  167.         __offset > 0 ? __offset / difference_type(_S_buffer_size())
  168.                    : -difference_type((-__offset - 1) / _S_buffer_size()) - 1;
  169.       _M_set_node(_M_node + __node_offset);
  170.       _M_cur = _M_first + 
  171.         (__offset - __node_offset * difference_type(_S_buffer_size()));
  172.     }
  173.     return *this;
  174.   }
  175.  
  176.   _Self operator+(difference_type __n) const
  177.   {
  178.     _Self __tmp = *this;
  179.     return __tmp += __n;
  180.   }
  181.  
  182.   _Self& operator-=(difference_type __n) { return *this += -__n; }
  183.  
  184.   _Self operator-(difference_type __n) const {
  185.     _Self __tmp = *this;
  186.     return __tmp -= __n;
  187.   }
  188.  
  189.   reference operator[](difference_type __n) const { return *(*this + __n); }
  190.  
  191.   /** @if maint
  192.    *  Prepares to traverse new_node.  Sets everything except _M_cur, which
  193.    *  should therefore be set by the caller immediately afterwards, based on
  194.    *  _M_first and _M_last.
  195.    *  @endif
  196.   */
  197.   void _M_set_node(_Map_pointer __new_node) {
  198.     _M_node = __new_node;
  199.     _M_first = *__new_node;
  200.     _M_last = _M_first + difference_type(_S_buffer_size());
  201.   }
  202. };
  203.  
  204. // Note: we also provide overloads whose operands are of the same type in
  205. // order to avoid ambiguos overload resolution when std::rel_ops operators
  206. // are in scope (for additional details, see libstdc++/3628)
  207. template <class _Tp, class _Ref, class _Ptr>
  208. inline bool
  209. operator==(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
  210.        const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
  211. {
  212.   return __x._M_cur == __y._M_cur;
  213. }
  214.  
  215. template <class _Tp, class _RefL, class _PtrL, class _RefR, class _PtrR>
  216. inline bool
  217. operator==(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
  218.        const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
  219. {
  220.   return __x._M_cur == __y._M_cur;
  221. }
  222.  
  223. template <class _Tp, class _Ref, class _Ptr>
  224. inline bool
  225. operator!=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
  226.        const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
  227. {
  228.   return !(__x == __y);
  229. }
  230.  
  231. template <class _Tp, class _RefL, class _PtrL, class _RefR, class _PtrR>
  232. inline bool
  233. operator!=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
  234.        const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
  235. {
  236.   return !(__x == __y);
  237. }
  238.  
  239. template <class _Tp, class _Ref, class _Ptr>
  240. inline bool
  241. operator<(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
  242.        const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
  243. {
  244.   return (__x._M_node == __y._M_node) ? 
  245.     (__x._M_cur < __y._M_cur) : (__x._M_node < __y._M_node);
  246. }
  247.  
  248. template <class _Tp, class _RefL, class _PtrL, class _RefR, class _PtrR>
  249. inline bool
  250. operator<(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
  251.        const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
  252. {
  253.   return (__x._M_node == __y._M_node) ? 
  254.     (__x._M_cur < __y._M_cur) : (__x._M_node < __y._M_node);
  255. }
  256.  
  257. template <class _Tp, class _Ref, class _Ptr>
  258. inline bool
  259. operator>(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
  260.        const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
  261. {
  262.   return __y < __x;
  263. }
  264.  
  265. template <class _Tp, class _RefL, class _PtrL, class _RefR, class _PtrR>
  266. inline bool
  267. operator>(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
  268.        const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
  269. {
  270.   return __y < __x;
  271. }
  272.  
  273. template <class _Tp, class _Ref, class _Ptr>
  274. inline bool
  275. operator<=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
  276.        const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
  277. {
  278.   return !(__y < __x);
  279. }
  280.  
  281. template <class _Tp, class _RefL, class _PtrL, class _RefR, class _PtrR>
  282. inline bool
  283. operator<=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
  284.        const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
  285. {
  286.   return !(__y < __x);
  287. }
  288.  
  289. template <class _Tp, class _Ref, class _Ptr>
  290. inline bool
  291. operator>=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
  292.        const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
  293. {
  294.   return !(__x < __y);
  295. }
  296.  
  297. template <class _Tp, class _RefL, class _PtrL, class _RefR, class _PtrR>
  298. inline bool
  299. operator>=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
  300.        const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
  301. {
  302.   return !(__x < __y);
  303. }
  304.  
  305. // _GLIBCPP_RESOLVE_LIB_DEFECTS
  306. // According to the resolution of DR179 not only the various comparison
  307. // operators but also operator- must accept mixed iterator/const_iterator
  308. // parameters.
  309. template <typename _Tp, typename _RefL, typename _PtrL,
  310.                         typename _RefR, typename _PtrR>
  311. inline typename _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type
  312. operator-(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
  313.       const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
  314. {
  315.   return _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type
  316.     (_Deque_iterator<_Tp, _RefL, _PtrL>::_S_buffer_size()) *
  317.     (__x._M_node - __y._M_node - 1) + (__x._M_cur - __x._M_first) +
  318.     (__y._M_last - __y._M_cur);
  319. }
  320.  
  321. template <class _Tp, class _Ref, class _Ptr>
  322. inline _Deque_iterator<_Tp, _Ref, _Ptr>
  323. operator+(ptrdiff_t __n, const _Deque_iterator<_Tp, _Ref, _Ptr>& __x)
  324. {
  325.   return __x + __n;
  326. }
  327.  
  328.  
  329. /// @if maint Primary default version.  @endif
  330. /**
  331.  *  @if maint
  332.  *  Deque base class.  It has two purposes.  First, its constructor
  333.  *  and destructor allocate (but don't initialize) storage.  This makes
  334.  *  exception safety easier.  Second, the base class encapsulates all of
  335.  *  the differences between SGI-style allocators and standard-conforming
  336.  *  allocators.  There are two versions:  this ordinary one, and the
  337.  *  space-saving specialization for instanceless allocators.
  338.  *  @endif
  339. */
  340. template <class _Tp, class _Alloc, bool __is_static>
  341. class _Deque_alloc_base
  342. {
  343. public:
  344.   typedef typename _Alloc_traits<_Tp,_Alloc>::allocator_type allocator_type;
  345.   allocator_type get_allocator() const { return _M_node_allocator; }
  346.  
  347.   _Deque_alloc_base(const allocator_type& __a)
  348.     : _M_node_allocator(__a), _M_map_allocator(__a),
  349.       _M_map(0), _M_map_size(0)
  350.   {}
  351.   
  352. protected:
  353.   typedef typename _Alloc_traits<_Tp*, _Alloc>::allocator_type
  354.           _Map_allocator_type;
  355.  
  356.   allocator_type      _M_node_allocator;
  357.   _Map_allocator_type _M_map_allocator;
  358.  
  359.   _Tp* _M_allocate_node() {
  360.     return _M_node_allocator.allocate(__deque_buf_size(sizeof(_Tp)));
  361.   }
  362.   void _M_deallocate_node(_Tp* __p) {
  363.     _M_node_allocator.deallocate(__p, __deque_buf_size(sizeof(_Tp)));
  364.   }
  365.   _Tp** _M_allocate_map(size_t __n) 
  366.     { return _M_map_allocator.allocate(__n); }
  367.   void _M_deallocate_map(_Tp** __p, size_t __n) 
  368.     { _M_map_allocator.deallocate(__p, __n); }
  369.  
  370.   _Tp** _M_map;
  371.   size_t _M_map_size;
  372. };
  373.  
  374. /// @if maint Specialization for instanceless allocators.  @endif
  375. template <class _Tp, class _Alloc>
  376. class _Deque_alloc_base<_Tp, _Alloc, true>
  377. {
  378. public:
  379.   typedef typename _Alloc_traits<_Tp,_Alloc>::allocator_type allocator_type;
  380.   allocator_type get_allocator() const { return allocator_type(); }
  381.  
  382.   _Deque_alloc_base(const allocator_type&) : _M_map(0), _M_map_size(0) {}
  383.   
  384. protected:
  385.   typedef typename _Alloc_traits<_Tp, _Alloc>::_Alloc_type _Node_alloc_type;
  386.   typedef typename _Alloc_traits<_Tp*, _Alloc>::_Alloc_type _Map_alloc_type;
  387.  
  388.   _Tp* _M_allocate_node() {
  389.     return _Node_alloc_type::allocate(__deque_buf_size(sizeof(_Tp)));
  390.   }
  391.   void _M_deallocate_node(_Tp* __p) {
  392.     _Node_alloc_type::deallocate(__p, __deque_buf_size(sizeof(_Tp)));
  393.   }
  394.   _Tp** _M_allocate_map(size_t __n) 
  395.     { return _Map_alloc_type::allocate(__n); }
  396.   void _M_deallocate_map(_Tp** __p, size_t __n) 
  397.     { _Map_alloc_type::deallocate(__p, __n); }
  398.  
  399.   _Tp** _M_map;
  400.   size_t _M_map_size;
  401. };
  402.  
  403.  
  404. /**
  405.  *  @if maint
  406.  *  Deque base class.  Using _Alloc_traits in the instantiation of the parent
  407.  *  class provides the compile-time dispatching mentioned in the parent's docs.
  408.  *  This class provides the unified face for deque's allocation.
  409.  *
  410.  *  Nothing in this class ever constructs or destroys an actual Tp element.
  411.  *  (Deque handles that itself.)  Only/All memory management is performed here.
  412.  *  @endif
  413. */
  414. template <class _Tp, class _Alloc>
  415. class _Deque_base
  416.   : public _Deque_alloc_base<_Tp,_Alloc,
  417.                               _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
  418. {
  419. public:
  420.   typedef _Deque_alloc_base<_Tp,_Alloc,
  421.                              _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
  422.           _Base;
  423.   typedef typename _Base::allocator_type             allocator_type;
  424.   typedef _Deque_iterator<_Tp,_Tp&,_Tp*>             iterator;
  425.   typedef _Deque_iterator<_Tp,const _Tp&,const _Tp*> const_iterator;
  426.  
  427.   _Deque_base(const allocator_type& __a, size_t __num_elements)
  428.     : _Base(__a), _M_start(), _M_finish()
  429.     { _M_initialize_map(__num_elements); }
  430.   _Deque_base(const allocator_type& __a) 
  431.     : _Base(__a), _M_start(), _M_finish() {}
  432.   ~_Deque_base();    
  433.  
  434. protected:
  435.   void _M_initialize_map(size_t);
  436.   void _M_create_nodes(_Tp** __nstart, _Tp** __nfinish);
  437.   void _M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish);
  438.   enum { _S_initial_map_size = 8 };
  439.  
  440. protected:
  441.   iterator _M_start;
  442.   iterator _M_finish;
  443. };
  444.  
  445.  
  446. template <class _Tp, class _Alloc>
  447. _Deque_base<_Tp,_Alloc>::~_Deque_base()
  448. {
  449.   if (_M_map) {
  450.     _M_destroy_nodes(_M_start._M_node, _M_finish._M_node + 1);
  451.     _M_deallocate_map(_M_map, _M_map_size);
  452.   }
  453. }
  454.  
  455. /**
  456.  *  @if maint
  457.  *  @brief Layout storage.
  458.  *  @param  num_elements  The count of T's for which to allocate space at first.
  459.  *  @return   Nothing.
  460.  *
  461.  *  The initial underlying memory layout is a bit complicated...
  462.  *  @endif
  463. */
  464. template <class _Tp, class _Alloc>
  465. void
  466. _Deque_base<_Tp,_Alloc>::_M_initialize_map(size_t __num_elements)
  467. {
  468.   size_t __num_nodes = 
  469.     __num_elements / __deque_buf_size(sizeof(_Tp)) + 1;
  470.  
  471.   _M_map_size = max((size_t) _S_initial_map_size, __num_nodes + 2);
  472.   _M_map = _M_allocate_map(_M_map_size);
  473.  
  474.   _Tp** __nstart = _M_map + (_M_map_size - __num_nodes) / 2;
  475.   _Tp** __nfinish = __nstart + __num_nodes;
  476.     
  477.   try 
  478.     { _M_create_nodes(__nstart, __nfinish); }
  479.   catch(...)
  480.     {
  481.       _M_deallocate_map(_M_map, _M_map_size);
  482.       _M_map = 0;
  483.       _M_map_size = 0;
  484.       __throw_exception_again;
  485.     }
  486.   
  487.   _M_start._M_set_node(__nstart);
  488.   _M_finish._M_set_node(__nfinish - 1);
  489.   _M_start._M_cur = _M_start._M_first;
  490.   _M_finish._M_cur = _M_finish._M_first +
  491.                __num_elements % __deque_buf_size(sizeof(_Tp));
  492. }
  493.  
  494. template <class _Tp, class _Alloc>
  495. void _Deque_base<_Tp,_Alloc>::_M_create_nodes(_Tp** __nstart, _Tp** __nfinish)
  496. {
  497.   _Tp** __cur;
  498.   try {
  499.     for (__cur = __nstart; __cur < __nfinish; ++__cur)
  500.       *__cur = _M_allocate_node();
  501.   }
  502.   catch(...)
  503.     { 
  504.       _M_destroy_nodes(__nstart, __cur);
  505.       __throw_exception_again; 
  506.     }
  507. }
  508.  
  509. template <class _Tp, class _Alloc>
  510. void
  511. _Deque_base<_Tp,_Alloc>::_M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish)
  512. {
  513.   for (_Tp** __n = __nstart; __n < __nfinish; ++__n)
  514.     _M_deallocate_node(*__n);
  515. }
  516.  
  517.  
  518. /**
  519.  *  @ingroup Containers
  520.  *  @ingroup Sequences
  521.  *
  522.  *  Meets the requirements of a <a href="tables.html#65">container</a>, a
  523.  *  <a href="tables.html#66">reversible container</a>, and a
  524.  *  <a href="tables.html#67">sequence</a>, including the
  525.  *  <a href="tables.html#68">optional sequence requirements</a>.
  526.  *
  527.  *  Placeholder:  see http://www.sgi.com/tech/stl/Deque.html for now.
  528.  *
  529.  *  In previous HP/SGI versions of deque, there was an extra template parameter
  530.  *  so users could control the node size.  This extension turned out to violate
  531.  *  the C++ standard (it can be detected using template template parameters),
  532.  *  and it was removed.
  533.  *
  534.  *  @if maint
  535.  *  Here's how a deque<Tp> manages memory.  Each deque has 4 members:
  536.  *  
  537.  *  - Tp**        _M_map
  538.  *  - size_t      _M_map_size
  539.  *  - iterator    _M_start, _M_finish
  540.  *  
  541.  *  map_size is at least 8.  %map is an array of map_size pointers-to-"nodes".
  542.  *  (The name has nothing to do with the std::map class.)
  543.  *  
  544.  *  A "node" has no specific type name as such, but it is referred to as
  545.  *  "node" in this file.  It is a simple array-of-Tp.  If Tp is very large,
  546.  *  there will be one Tp element per node (i.e., an "array" of one).
  547.  *  For non-huge Tp's, node size is inversely related to Tp size:  the
  548.  *  larger the Tp, the fewer Tp's will fit in a node.  The goal here is to
  549.  *  keep the total size of a node relatively small and constant over different
  550.  *  Tp's, to improve allocator efficiency.
  551.  *  
  552.  *  **** As I write this, the nodes are /not/ allocated using the high-speed
  553.  *  memory pool.  There are 20 hours left in the year; perhaps I can fix
  554.  *  this before 2002.
  555.  *  
  556.  *  Not every pointer in the %map array will point to a node.  If the initial
  557.  *  number of elements in the deque is small, the /middle/ %map pointers will
  558.  *  be valid, and the ones at the edges will be unused.  This same situation
  559.  *  will arise as the %map grows:  available %map pointers, if any, will be on
  560.  *  the ends.  As new nodes are created, only a subset of the %map's pointers
  561.  *  need to be copied "outward".
  562.  *
  563.  *  Class invariants:
  564.  * - For any nonsingular iterator i:
  565.  *    - i.node points to a member of the %map array.  (Yes, you read that
  566.  *      correctly:  i.node does not actually point to a node.)  The member of
  567.  *      the %map array is what actually points to the node.
  568.  *    - i.first == *(i.node)    (This points to the node (first Tp element).)
  569.  *    - i.last  == i.first + node_size
  570.  *    - i.cur is a pointer in the range [i.first, i.last).  NOTE:
  571.  *      the implication of this is that i.cur is always a dereferenceable
  572.  *      pointer, even if i is a past-the-end iterator.
  573.  * - Start and Finish are always nonsingular iterators.  NOTE: this means that
  574.  *   an empty deque must have one node, a deque with <N elements (where N is
  575.  *   the node buffer size) must have one node, a deque with N through (2N-1)
  576.  *   elements must have two nodes, etc.
  577.  * - For every node other than start.node and finish.node, every element in the
  578.  *   node is an initialized object.  If start.node == finish.node, then
  579.  *   [start.cur, finish.cur) are initialized objects, and the elements outside
  580.  *   that range are uninitialized storage.  Otherwise, [start.cur, start.last)
  581.  *   and [finish.first, finish.cur) are initialized objects, and [start.first,
  582.  *   start.cur) and [finish.cur, finish.last) are uninitialized storage.
  583.  * - [%map, %map + map_size) is a valid, non-empty range.  
  584.  * - [start.node, finish.node] is a valid range contained within 
  585.  *   [%map, %map + map_size).  
  586.  * - A pointer in the range [%map, %map + map_size) points to an allocated node
  587.  *   if and only if the pointer is in the range [start.node, finish.node].
  588.  *
  589.  *  Here's the magic:  nothing in deque is "aware" of the discontiguous storage!
  590.  *
  591.  *  The memory setup and layout occurs in the parent, _Base, and the iterator
  592.  *  class is entirely responsible for "leaping" from one node to the next.  All
  593.  *  the implementation routines for deque itself work only through the start
  594.  *  and finish iterators.  This keeps the routines simple and sane, and we can
  595.  *  use other standard algorithms as well.
  596.  *  @endif
  597. */
  598. template <class _Tp, class _Alloc = allocator<_Tp> >
  599. class deque : protected _Deque_base<_Tp, _Alloc>
  600. {
  601.   // concept requirements
  602.   __glibcpp_class_requires(_Tp, _SGIAssignableConcept)
  603.  
  604.   typedef _Deque_base<_Tp, _Alloc> _Base;
  605.  
  606. public:
  607.   typedef _Tp                                value_type;
  608.   typedef value_type*                        pointer;
  609.   typedef const value_type*                  const_pointer;
  610.   typedef value_type&                        reference;
  611.   typedef const value_type&                  const_reference;
  612.   typedef size_t                             size_type;
  613.   typedef ptrdiff_t                          difference_type;
  614.  
  615.   typedef typename _Base::allocator_type allocator_type;
  616.   allocator_type get_allocator() const { return _Base::get_allocator(); }
  617.  
  618.   typedef typename _Base::iterator           iterator;
  619.   typedef typename _Base::const_iterator     const_iterator;
  620.   typedef reverse_iterator<const_iterator>   const_reverse_iterator;
  621.   typedef reverse_iterator<iterator>         reverse_iterator;
  622.  
  623. protected:
  624.   typedef pointer* _Map_pointer;
  625.   static size_t _S_buffer_size() { return __deque_buf_size(sizeof(_Tp)); }
  626.  
  627.   // Functions controlling memory layout, and nothing else.
  628.   using _Base::_M_initialize_map;
  629.   using _Base::_M_create_nodes;
  630.   using _Base::_M_destroy_nodes;
  631.   using _Base::_M_allocate_node;
  632.   using _Base::_M_deallocate_node;
  633.   using _Base::_M_allocate_map;
  634.   using _Base::_M_deallocate_map;
  635.  
  636.   /** @if maint
  637.    *  A total of four data members accumulated down the heirarchy.  If the
  638.    *  _Alloc type requires separate instances, then two of them will also be
  639.    *  included in each deque.
  640.    *  @endif
  641.   */
  642.   using _Base::_M_map;
  643.   using _Base::_M_map_size;
  644.   using _Base::_M_start;
  645.   using _Base::_M_finish;
  646.  
  647. public:                         // Basic accessors
  648.   iterator begin() { return _M_start; }
  649.   iterator end() { return _M_finish; }
  650.   const_iterator begin() const { return _M_start; }
  651.   const_iterator end() const { return _M_finish; }
  652.  
  653.   reverse_iterator rbegin() { return reverse_iterator(_M_finish); }
  654.   reverse_iterator rend() { return reverse_iterator(_M_start); }
  655.   const_reverse_iterator rbegin() const 
  656.     { return const_reverse_iterator(_M_finish); }
  657.   const_reverse_iterator rend() const 
  658.     { return const_reverse_iterator(_M_start); }
  659.  
  660.   reference operator[](size_type __n)
  661.     { return _M_start[difference_type(__n)]; }
  662.   const_reference operator[](size_type __n) const 
  663.     { return _M_start[difference_type(__n)]; }
  664.  
  665.   void _M_range_check(size_type __n) const {
  666.     if (__n >= this->size())
  667.       __throw_range_error("deque");
  668.   }
  669.  
  670.   reference at(size_type __n)
  671.     { _M_range_check(__n); return (*this)[__n]; }
  672.   const_reference at(size_type __n) const
  673.     { _M_range_check(__n); return (*this)[__n]; }
  674.  
  675.   reference front() { return *_M_start; }
  676.   reference back() {
  677.     iterator __tmp = _M_finish;
  678.     --__tmp;
  679.     return *__tmp;
  680.   }
  681.   const_reference front() const { return *_M_start; }
  682.   const_reference back() const {
  683.     const_iterator __tmp = _M_finish;
  684.     --__tmp;
  685.     return *__tmp;
  686.   }
  687.  
  688.   size_type size() const { return _M_finish - _M_start; }
  689.   size_type max_size() const { return size_type(-1); }
  690.   bool empty() const { return _M_finish == _M_start; }
  691.  
  692. public:                         // Constructor, destructor.
  693.   explicit deque(const allocator_type& __a = allocator_type()) 
  694.     : _Base(__a, 0) {}
  695.   deque(const deque& __x) : _Base(__x.get_allocator(), __x.size()) 
  696.     { uninitialized_copy(__x.begin(), __x.end(), _M_start); }
  697.   deque(size_type __n, const value_type& __value,
  698.         const allocator_type& __a = allocator_type()) : _Base(__a, __n)
  699.     { _M_fill_initialize(__value); }
  700.  
  701.   explicit
  702.   deque(size_type __n)
  703.   : _Base(allocator_type(), __n)
  704.   { _M_fill_initialize(value_type()); }
  705.  
  706.   // Check whether it's an integral type.  If so, it's not an iterator.
  707.   template<class _InputIterator>
  708.     deque(_InputIterator __first, _InputIterator __last,
  709.           const allocator_type& __a = allocator_type())
  710.     : _Base(__a)
  711.     {
  712.       typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
  713.       _M_initialize_dispatch(__first, __last, _Integral());
  714.     }
  715.  
  716.   template<class _Integer>
  717.     void
  718.     _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
  719.     {
  720.       _M_initialize_map(__n);
  721.       _M_fill_initialize(__x);
  722.     }
  723.  
  724.   template<class _InputIter>
  725.     void
  726.     _M_initialize_dispatch(_InputIter __first, _InputIter __last, __false_type)
  727.     {
  728.       typedef typename iterator_traits<_InputIter>::iterator_category _IterCategory;
  729.       _M_range_initialize(__first, __last, _IterCategory());
  730.     }
  731.  
  732.   ~deque()
  733.   { _Destroy(_M_start, _M_finish); }
  734.  
  735.   deque& operator= (const deque& __x) {
  736.     const size_type __len = size();
  737.     if (&__x != this) {
  738.       if (__len >= __x.size())
  739.         erase(copy(__x.begin(), __x.end(), _M_start), _M_finish);
  740.       else {
  741.         const_iterator __mid = __x.begin() + difference_type(__len);
  742.         copy(__x.begin(), __mid, _M_start);
  743.         insert(_M_finish, __mid, __x.end());
  744.       }
  745.     }
  746.     return *this;
  747.   }        
  748.  
  749.   void swap(deque& __x) {
  750.     std::swap(_M_start, __x._M_start);
  751.     std::swap(_M_finish, __x._M_finish);
  752.     std::swap(_M_map, __x._M_map);
  753.     std::swap(_M_map_size, __x._M_map_size);
  754.   }
  755.  
  756. public: 
  757.   // assign(), a generalized assignment member function.  Two
  758.   // versions: one that takes a count, and one that takes a range.
  759.   // The range version is a member template, so we dispatch on whether
  760.   // or not the type is an integer.
  761.  
  762.   void _M_fill_assign(size_type __n, const _Tp& __val) {
  763.     if (__n > size()) {
  764.       fill(begin(), end(), __val);
  765.       insert(end(), __n - size(), __val);
  766.     }
  767.     else {
  768.       erase(begin() + __n, end());
  769.       fill(begin(), end(), __val);
  770.     }
  771.   }
  772.  
  773.   void
  774.   assign(size_type __n, const _Tp& __val)
  775.   { _M_fill_assign(__n, __val); }
  776.  
  777.   template<class _InputIterator>
  778.     void
  779.     assign(_InputIterator __first, _InputIterator __last)
  780.     {
  781.       typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
  782.       _M_assign_dispatch(__first, __last, _Integral());
  783.     }
  784.  
  785. private:                        // helper functions for assign() 
  786.  
  787.   template<class _Integer>
  788.     void
  789.     _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
  790.     { _M_fill_assign(static_cast<size_type>(__n), static_cast<_Tp>(__val)); }
  791.  
  792.   template<class _InputIterator>
  793.     void
  794.     _M_assign_dispatch(_InputIterator __first, _InputIterator __last, __false_type)
  795.     {
  796.       typedef typename iterator_traits<_InputIterator>::iterator_category _IterCategory;
  797.       _M_assign_aux(__first, __last, _IterCategory());
  798.     }
  799.  
  800.   template <class _InputIterator>
  801.   void _M_assign_aux(_InputIterator __first, _InputIterator __last,
  802.                      input_iterator_tag);
  803.  
  804.   template <class _ForwardIterator>
  805.   void _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
  806.                      forward_iterator_tag) {
  807.     size_type __len = distance(__first, __last);
  808.     if (__len > size()) {
  809.       _ForwardIterator __mid = __first;
  810.       advance(__mid, size());
  811.       copy(__first, __mid, begin());
  812.       insert(end(), __mid, __last);
  813.     }
  814.     else
  815.       erase(copy(__first, __last, begin()), end());
  816.   }
  817.  
  818. public:                         // push_* and pop_*
  819.   
  820.   void
  821.   push_back(const value_type& __t)
  822.   {
  823.     if (_M_finish._M_cur != _M_finish._M_last - 1) {
  824.       _Construct(_M_finish._M_cur, __t);
  825.       ++_M_finish._M_cur;
  826.     }
  827.     else
  828.       _M_push_back_aux(__t);
  829.   }
  830.  
  831.   void
  832.   push_back()
  833.   {
  834.     if (_M_finish._M_cur != _M_finish._M_last - 1) {
  835.       _Construct(_M_finish._M_cur);
  836.       ++_M_finish._M_cur;
  837.     }
  838.     else
  839.       _M_push_back_aux();
  840.   }
  841.  
  842.   void
  843.   push_front(const value_type& __t) 
  844.   {
  845.     if (_M_start._M_cur != _M_start._M_first) {
  846.       _Construct(_M_start._M_cur - 1, __t);
  847.       --_M_start._M_cur;
  848.     }
  849.     else
  850.       _M_push_front_aux(__t);
  851.   }
  852.  
  853.   void
  854.   push_front()
  855.   {
  856.     if (_M_start._M_cur != _M_start._M_first) {
  857.       _Construct(_M_start._M_cur - 1);
  858.       --_M_start._M_cur;
  859.     }
  860.     else
  861.       _M_push_front_aux();
  862.   }
  863.  
  864.  
  865.   void
  866.   pop_back()
  867.   {
  868.     if (_M_finish._M_cur != _M_finish._M_first) {
  869.       --_M_finish._M_cur;
  870.       _Destroy(_M_finish._M_cur);
  871.     }
  872.     else
  873.       _M_pop_back_aux();
  874.   }
  875.  
  876.   void
  877.   pop_front()
  878.   {
  879.     if (_M_start._M_cur != _M_start._M_last - 1) {
  880.       _Destroy(_M_start._M_cur);
  881.       ++_M_start._M_cur;
  882.     }
  883.     else 
  884.       _M_pop_front_aux();
  885.   }
  886.  
  887. public:                         // Insert
  888.  
  889.   iterator
  890.   insert(iterator position, const value_type& __x)
  891.   {
  892.     if (position._M_cur == _M_start._M_cur) {
  893.       push_front(__x);
  894.       return _M_start;
  895.     }
  896.     else if (position._M_cur == _M_finish._M_cur) {
  897.       push_back(__x);
  898.       iterator __tmp = _M_finish;
  899.       --__tmp;
  900.       return __tmp;
  901.     }
  902.     else {
  903.       return _M_insert_aux(position, __x);
  904.     }
  905.   }
  906.  
  907.   iterator
  908.   insert(iterator __position)
  909.   { return insert(__position, value_type()); }
  910.  
  911.   void
  912.   insert(iterator __pos, size_type __n, const value_type& __x)
  913.   { _M_fill_insert(__pos, __n, __x); }
  914.  
  915.   void
  916.   _M_fill_insert(iterator __pos, size_type __n, const value_type& __x); 
  917.  
  918.   // Check whether it's an integral type.  If so, it's not an iterator.
  919.   template<class _InputIterator>
  920.     void
  921.     insert(iterator __pos, _InputIterator __first, _InputIterator __last)
  922.     {
  923.       typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
  924.       _M_insert_dispatch(__pos, __first, __last, _Integral());
  925.     }
  926.  
  927.   template<class _Integer>
  928.     void
  929.     _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x, __true_type)
  930.     { _M_fill_insert(__pos, static_cast<size_type>(__n), static_cast<value_type>(__x)); }
  931.  
  932.   template<class _InputIterator>
  933.     void
  934.     _M_insert_dispatch(iterator __pos,
  935.                        _InputIterator __first, _InputIterator __last,
  936.                        __false_type)
  937.     {
  938.       typedef typename iterator_traits<_InputIterator>::iterator_category _IterCategory;
  939.       insert(__pos, __first, __last, _IterCategory());
  940.     }
  941.  
  942.   void resize(size_type __new_size, const value_type& __x) {
  943.     const size_type __len = size();
  944.     if (__new_size < __len) 
  945.       erase(_M_start + __new_size, _M_finish);
  946.     else
  947.       insert(_M_finish, __new_size - __len, __x);
  948.   }
  949.  
  950.   void resize(size_type new_size) { resize(new_size, value_type()); }
  951.  
  952. public:                         // Erase
  953.   iterator erase(iterator __pos) {
  954.     iterator __next = __pos;
  955.     ++__next;
  956.     size_type __index = __pos - _M_start;
  957.     if (__index < (size() >> 1)) {
  958.       copy_backward(_M_start, __pos, __next);
  959.       pop_front();
  960.     }
  961.     else {
  962.       copy(__next, _M_finish, __pos);
  963.       pop_back();
  964.     }
  965.     return _M_start + __index;
  966.   }
  967.  
  968.   iterator erase(iterator __first, iterator __last);
  969.   void clear(); 
  970.  
  971. protected:                        // Internal construction/destruction
  972.  
  973.   void _M_fill_initialize(const value_type& __value);
  974.  
  975.   template <class _InputIterator>
  976.   void _M_range_initialize(_InputIterator __first, _InputIterator __last,
  977.                         input_iterator_tag);
  978.  
  979.   template <class _ForwardIterator>
  980.   void _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
  981.                         forward_iterator_tag);
  982.  
  983. protected:                        // Internal push_* and pop_*
  984.  
  985.   void _M_push_back_aux(const value_type&);
  986.   void _M_push_back_aux();
  987.   void _M_push_front_aux(const value_type&);
  988.   void _M_push_front_aux();
  989.   void _M_pop_back_aux();
  990.   void _M_pop_front_aux();
  991.  
  992. protected:                        // Internal insert functions
  993.  
  994.   template <class _InputIterator>
  995.   void insert(iterator __pos, _InputIterator __first, _InputIterator __last,
  996.               input_iterator_tag);
  997.  
  998.   template <class _ForwardIterator>
  999.   void insert(iterator __pos,
  1000.               _ForwardIterator __first, _ForwardIterator __last,
  1001.               forward_iterator_tag);
  1002.  
  1003.   iterator _M_insert_aux(iterator __pos, const value_type& __x);
  1004.   iterator _M_insert_aux(iterator __pos);
  1005.   void _M_insert_aux(iterator __pos, size_type __n, const value_type& __x);
  1006.  
  1007.   template <class _ForwardIterator>
  1008.   void _M_insert_aux(iterator __pos, 
  1009.                      _ForwardIterator __first, _ForwardIterator __last,
  1010.                      size_type __n);
  1011.  
  1012.   iterator _M_reserve_elements_at_front(size_type __n) {
  1013.     size_type __vacancies = _M_start._M_cur - _M_start._M_first;
  1014.     if (__n > __vacancies) 
  1015.       _M_new_elements_at_front(__n - __vacancies);
  1016.     return _M_start - difference_type(__n);
  1017.   }
  1018.  
  1019.   iterator _M_reserve_elements_at_back(size_type __n) {
  1020.     size_type __vacancies = (_M_finish._M_last - _M_finish._M_cur) - 1;
  1021.     if (__n > __vacancies)
  1022.       _M_new_elements_at_back(__n - __vacancies);
  1023.     return _M_finish + difference_type(__n);
  1024.   }
  1025.  
  1026.   void _M_new_elements_at_front(size_type __new_elements);
  1027.   void _M_new_elements_at_back(size_type __new_elements);
  1028.  
  1029. protected:                      // Allocation of _M_map and nodes
  1030.  
  1031.   // Makes sure the _M_map has space for new nodes.  Does not actually
  1032.   //  add the nodes.  Can invalidate _M_map pointers.  (And consequently, 
  1033.   //  deque iterators.)
  1034.  
  1035.   void _M_reserve_map_at_back (size_type __nodes_to_add = 1) {
  1036.     if (__nodes_to_add + 1 > _M_map_size - (_M_finish._M_node - _M_map))
  1037.       _M_reallocate_map(__nodes_to_add, false);
  1038.   }
  1039.  
  1040.   void _M_reserve_map_at_front (size_type __nodes_to_add = 1) {
  1041.     if (__nodes_to_add > size_type(_M_start._M_node - _M_map))
  1042.       _M_reallocate_map(__nodes_to_add, true);
  1043.   }
  1044.  
  1045.   void _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front);
  1046. };
  1047.  
  1048. // Non-inline member functions
  1049.  
  1050. template <class _Tp, class _Alloc>
  1051. template <class _InputIter>
  1052. void deque<_Tp, _Alloc>
  1053.   ::_M_assign_aux(_InputIter __first, _InputIter __last, input_iterator_tag)
  1054. {
  1055.   iterator __cur = begin();
  1056.   for ( ; __first != __last && __cur != end(); ++__cur, ++__first)
  1057.     *__cur = *__first;
  1058.   if (__first == __last)
  1059.     erase(__cur, end());
  1060.   else
  1061.     insert(end(), __first, __last);
  1062. }
  1063.  
  1064. template <class _Tp, class _Alloc>
  1065. void deque<_Tp, _Alloc>::_M_fill_insert(iterator __pos,
  1066.                                         size_type __n, const value_type& __x)
  1067. {
  1068.   if (__pos._M_cur == _M_start._M_cur) {
  1069.     iterator __new_start = _M_reserve_elements_at_front(__n);
  1070.     try {
  1071.       uninitialized_fill(__new_start, _M_start, __x);
  1072.       _M_start = __new_start;
  1073.     }
  1074.     catch(...)
  1075.       {
  1076.     _M_destroy_nodes(__new_start._M_node, _M_start._M_node);
  1077.     __throw_exception_again;
  1078.       }
  1079.   }
  1080.   else if (__pos._M_cur == _M_finish._M_cur) {
  1081.     iterator __new_finish = _M_reserve_elements_at_back(__n);
  1082.     try {
  1083.       uninitialized_fill(_M_finish, __new_finish, __x);
  1084.       _M_finish = __new_finish;
  1085.     }
  1086.     catch(...)
  1087.       {
  1088.     _M_destroy_nodes(_M_finish._M_node + 1, __new_finish._M_node + 1);    
  1089.     __throw_exception_again;
  1090.       }
  1091.   }
  1092.   else 
  1093.     _M_insert_aux(__pos, __n, __x);
  1094. }
  1095.  
  1096. template <class _Tp, class _Alloc>
  1097. typename deque<_Tp,_Alloc>::iterator 
  1098. deque<_Tp,_Alloc>::erase(iterator __first, iterator __last)
  1099. {
  1100.   if (__first == _M_start && __last == _M_finish) {
  1101.     clear();
  1102.     return _M_finish;
  1103.   }
  1104.   else {
  1105.     difference_type __n = __last - __first;
  1106.     difference_type __elems_before = __first - _M_start;
  1107.     if (static_cast<size_type>(__elems_before) < (size() - __n) / 2) {
  1108.       copy_backward(_M_start, __first, __last);
  1109.       iterator __new_start = _M_start + __n;
  1110.       _Destroy(_M_start, __new_start);
  1111.       _M_destroy_nodes(_M_start._M_node, __new_start._M_node);
  1112.       _M_start = __new_start;
  1113.     }
  1114.     else {
  1115.       copy(__last, _M_finish, __first);
  1116.       iterator __new_finish = _M_finish - __n;
  1117.       _Destroy(__new_finish, _M_finish);
  1118.       _M_destroy_nodes(__new_finish._M_node + 1, _M_finish._M_node + 1);
  1119.       _M_finish = __new_finish;
  1120.     }
  1121.     return _M_start + __elems_before;
  1122.   }
  1123. }
  1124.  
  1125. template <class _Tp, class _Alloc> 
  1126. void deque<_Tp,_Alloc>::clear()
  1127. {
  1128.   for (_Map_pointer __node = _M_start._M_node + 1;
  1129.        __node < _M_finish._M_node;
  1130.        ++__node) {
  1131.     _Destroy(*__node, *__node + _S_buffer_size());
  1132.     _M_deallocate_node(*__node);
  1133.   }
  1134.  
  1135.   if (_M_start._M_node != _M_finish._M_node) {
  1136.     _Destroy(_M_start._M_cur, _M_start._M_last);
  1137.     _Destroy(_M_finish._M_first, _M_finish._M_cur);
  1138.     _M_deallocate_node(_M_finish._M_first);
  1139.   }
  1140.   else
  1141.     _Destroy(_M_start._M_cur, _M_finish._M_cur);
  1142.  
  1143.   _M_finish = _M_start;
  1144. }
  1145.  
  1146. /**
  1147.  *  @if maint
  1148.  *  @brief Fills the deque with copies of value.
  1149.  *  @param  value  Initial value.
  1150.  *  @return   Nothing.
  1151.  *  @pre _M_start and _M_finish have already been initialized, but none of the
  1152.  *       deque's elements have yet been constructed.
  1153.  *
  1154.  *  This function is called only when the user provides an explicit size (with
  1155.  *  or without an explicit exemplar value).
  1156.  *  @endif
  1157. */
  1158. template <class _Tp, class _Alloc>
  1159. void deque<_Tp,_Alloc>::_M_fill_initialize(const value_type& __value)
  1160. {
  1161.   _Map_pointer __cur;
  1162.   try {
  1163.     for (__cur = _M_start._M_node; __cur < _M_finish._M_node; ++__cur)
  1164.       uninitialized_fill(*__cur, *__cur + _S_buffer_size(), __value);
  1165.     uninitialized_fill(_M_finish._M_first, _M_finish._M_cur, __value);
  1166.   }
  1167.   catch(...)
  1168.     {
  1169.       _Destroy(_M_start, iterator(*__cur, __cur));
  1170.       __throw_exception_again;
  1171.     }
  1172. }
  1173.  
  1174. /** @{
  1175.  *  @if maint
  1176.  *  @brief Fills the deque with whatever is in [first,last).
  1177.  *  @param  first  An input iterator.
  1178.  *  @param  last  An input iterator.
  1179.  *  @return   Nothing.
  1180.  *
  1181.  *  If the iterators are actually forward iterators (or better), then the
  1182.  *  memory layout can be done all at once.  Else we move forward using
  1183.  *  push_back on each value from the iterator.
  1184.  *  @endif
  1185. */
  1186. template <class _Tp, class _Alloc> template <class _InputIterator>
  1187. void deque<_Tp,_Alloc>::_M_range_initialize(_InputIterator __first,
  1188.                                             _InputIterator __last,
  1189.                                             input_iterator_tag)
  1190. {
  1191.   _M_initialize_map(0);
  1192.   try {
  1193.     for ( ; __first != __last; ++__first)
  1194.       push_back(*__first);
  1195.   }
  1196.   catch(...)
  1197.     {
  1198.       clear();
  1199.       __throw_exception_again;
  1200.     }
  1201. }
  1202.  
  1203. template <class _Tp, class _Alloc> template <class _ForwardIterator>
  1204. void deque<_Tp,_Alloc>::_M_range_initialize(_ForwardIterator __first,
  1205.                                             _ForwardIterator __last,
  1206.                                             forward_iterator_tag)
  1207. {
  1208.   size_type __n = distance(__first, __last);
  1209.   _M_initialize_map(__n);
  1210.  
  1211.   _Map_pointer __cur_node;
  1212.   try {
  1213.     for (__cur_node = _M_start._M_node; 
  1214.          __cur_node < _M_finish._M_node; 
  1215.          ++__cur_node) {
  1216.       _ForwardIterator __mid = __first;
  1217.       advance(__mid, _S_buffer_size());
  1218.       uninitialized_copy(__first, __mid, *__cur_node);
  1219.       __first = __mid;
  1220.     }
  1221.     uninitialized_copy(__first, __last, _M_finish._M_first);
  1222.   }
  1223.   catch(...)
  1224.     {
  1225.       _Destroy(_M_start, iterator(*__cur_node, __cur_node));
  1226.       __throw_exception_again;
  1227.     }
  1228. }
  1229. /** @} */
  1230.  
  1231. // Called only if _M_finish._M_cur == _M_finish._M_last - 1.
  1232. template <class _Tp, class _Alloc>
  1233. void
  1234. deque<_Tp,_Alloc>::_M_push_back_aux(const value_type& __t)
  1235. {
  1236.   value_type __t_copy = __t;
  1237.   _M_reserve_map_at_back();
  1238.   *(_M_finish._M_node + 1) = _M_allocate_node();
  1239.   try {
  1240.     _Construct(_M_finish._M_cur, __t_copy);
  1241.     _M_finish._M_set_node(_M_finish._M_node + 1);
  1242.     _M_finish._M_cur = _M_finish._M_first;
  1243.   }
  1244.   catch(...)
  1245.     {
  1246.       _M_deallocate_node(*(_M_finish._M_node + 1));
  1247.       __throw_exception_again;
  1248.     }
  1249. }
  1250.  
  1251. // Called only if _M_finish._M_cur == _M_finish._M_last - 1.
  1252. template <class _Tp, class _Alloc>
  1253. void
  1254. deque<_Tp,_Alloc>::_M_push_back_aux()
  1255. {
  1256.   _M_reserve_map_at_back();
  1257.   *(_M_finish._M_node + 1) = _M_allocate_node();
  1258.   try {
  1259.     _Construct(_M_finish._M_cur);
  1260.     _M_finish._M_set_node(_M_finish._M_node + 1);
  1261.     _M_finish._M_cur = _M_finish._M_first;
  1262.   }
  1263.   catch(...)
  1264.     {
  1265.       _M_deallocate_node(*(_M_finish._M_node + 1));
  1266.       __throw_exception_again;
  1267.     }
  1268. }
  1269.  
  1270. // Called only if _M_start._M_cur == _M_start._M_first.
  1271. template <class _Tp, class _Alloc>
  1272. void
  1273. deque<_Tp,_Alloc>::_M_push_front_aux(const value_type& __t)
  1274. {
  1275.   value_type __t_copy = __t;
  1276.   _M_reserve_map_at_front();
  1277.   *(_M_start._M_node - 1) = _M_allocate_node();
  1278.   try {
  1279.     _M_start._M_set_node(_M_start._M_node - 1);
  1280.     _M_start._M_cur = _M_start._M_last - 1;
  1281.     _Construct(_M_start._M_cur, __t_copy);
  1282.   }
  1283.   catch(...)
  1284.     {
  1285.       ++_M_start;
  1286.       _M_deallocate_node(*(_M_start._M_node - 1));
  1287.       __throw_exception_again;
  1288.     }
  1289.  
  1290. // Called only if _M_start._M_cur == _M_start._M_first.
  1291. template <class _Tp, class _Alloc>
  1292. void
  1293. deque<_Tp,_Alloc>::_M_push_front_aux()
  1294. {
  1295.   _M_reserve_map_at_front();
  1296.   *(_M_start._M_node - 1) = _M_allocate_node();
  1297.   try {
  1298.     _M_start._M_set_node(_M_start._M_node - 1);
  1299.     _M_start._M_cur = _M_start._M_last - 1;
  1300.     _Construct(_M_start._M_cur);
  1301.   }
  1302.   catch(...)
  1303.     {
  1304.       ++_M_start;
  1305.       _M_deallocate_node(*(_M_start._M_node - 1));
  1306.       __throw_exception_again;
  1307.     }
  1308.  
  1309. // Called only if _M_finish._M_cur == _M_finish._M_first.
  1310. template <class _Tp, class _Alloc>
  1311. void deque<_Tp,_Alloc>::_M_pop_back_aux()
  1312. {
  1313.   _M_deallocate_node(_M_finish._M_first);
  1314.   _M_finish._M_set_node(_M_finish._M_node - 1);
  1315.   _M_finish._M_cur = _M_finish._M_last - 1;
  1316.   _Destroy(_M_finish._M_cur);
  1317. }
  1318.  
  1319. // Called only if _M_start._M_cur == _M_start._M_last - 1.  Note that 
  1320. // if the deque has at least one element (a precondition for this member 
  1321. // function), and if _M_start._M_cur == _M_start._M_last, then the deque 
  1322. // must have at least two nodes.
  1323. template <class _Tp, class _Alloc>
  1324. void deque<_Tp,_Alloc>::_M_pop_front_aux()
  1325. {
  1326.   _Destroy(_M_start._M_cur);
  1327.   _M_deallocate_node(_M_start._M_first);
  1328.   _M_start._M_set_node(_M_start._M_node + 1);
  1329.   _M_start._M_cur = _M_start._M_first;
  1330. }      
  1331.  
  1332. template <class _Tp, class _Alloc> template <class _InputIterator>
  1333. void deque<_Tp,_Alloc>::insert(iterator __pos,
  1334.                                _InputIterator __first, _InputIterator __last,
  1335.                                input_iterator_tag)
  1336. {
  1337.   copy(__first, __last, inserter(*this, __pos));
  1338. }
  1339.  
  1340. template <class _Tp, class _Alloc> template <class _ForwardIterator>
  1341. void
  1342. deque<_Tp,_Alloc>::insert(iterator __pos,
  1343.                           _ForwardIterator __first, _ForwardIterator __last,
  1344.                           forward_iterator_tag) {
  1345.   size_type __n = distance(__first, __last);
  1346.   if (__pos._M_cur == _M_start._M_cur) {
  1347.     iterator __new_start = _M_reserve_elements_at_front(__n);
  1348.     try {
  1349.       uninitialized_copy(__first, __last, __new_start);
  1350.       _M_start = __new_start;
  1351.     }
  1352.     catch(...)
  1353.       {
  1354.     _M_destroy_nodes(__new_start._M_node, _M_start._M_node);
  1355.     __throw_exception_again;
  1356.       }
  1357.   }
  1358.   else if (__pos._M_cur == _M_finish._M_cur) {
  1359.     iterator __new_finish = _M_reserve_elements_at_back(__n);
  1360.     try {
  1361.       uninitialized_copy(__first, __last, _M_finish);
  1362.       _M_finish = __new_finish;
  1363.     }
  1364.     catch(...)
  1365.       {
  1366.     _M_destroy_nodes(_M_finish._M_node + 1, __new_finish._M_node + 1);
  1367.     __throw_exception_again;
  1368.       }
  1369.   }
  1370.   else
  1371.     _M_insert_aux(__pos, __first, __last, __n);
  1372. }
  1373.  
  1374. template <class _Tp, class _Alloc>
  1375. typename deque<_Tp, _Alloc>::iterator
  1376. deque<_Tp,_Alloc>::_M_insert_aux(iterator __pos, const value_type& __x)
  1377. {
  1378.   difference_type __index = __pos - _M_start;
  1379.   value_type __x_copy = __x;
  1380.   if (static_cast<size_type>(__index) < size() / 2) {
  1381.     push_front(front());
  1382.     iterator __front1 = _M_start;
  1383.     ++__front1;
  1384.     iterator __front2 = __front1;
  1385.     ++__front2;
  1386.     __pos = _M_start + __index;
  1387.     iterator __pos1 = __pos;
  1388.     ++__pos1;
  1389.     copy(__front2, __pos1, __front1);
  1390.   }
  1391.   else {
  1392.     push_back(back());
  1393.     iterator __back1 = _M_finish;
  1394.     --__back1;
  1395.     iterator __back2 = __back1;
  1396.     --__back2;
  1397.     __pos = _M_start + __index;
  1398.     copy_backward(__pos, __back2, __back1);
  1399.   }
  1400.   *__pos = __x_copy;
  1401.   return __pos;
  1402. }
  1403.  
  1404. template <class _Tp, class _Alloc>
  1405. typename deque<_Tp,_Alloc>::iterator 
  1406. deque<_Tp,_Alloc>::_M_insert_aux(iterator __pos)
  1407. {
  1408.   difference_type __index = __pos - _M_start;
  1409.   if (static_cast<size_type>(__index) < size() / 2) {
  1410.     push_front(front());
  1411.     iterator __front1 = _M_start;
  1412.     ++__front1;
  1413.     iterator __front2 = __front1;
  1414.     ++__front2;
  1415.     __pos = _M_start + __index;
  1416.     iterator __pos1 = __pos;
  1417.     ++__pos1;
  1418.     copy(__front2, __pos1, __front1);
  1419.   }
  1420.   else {
  1421.     push_back(back());
  1422.     iterator __back1 = _M_finish;
  1423.     --__back1;
  1424.     iterator __back2 = __back1;
  1425.     --__back2;
  1426.     __pos = _M_start + __index;
  1427.     copy_backward(__pos, __back2, __back1);
  1428.   }
  1429.   *__pos = value_type();
  1430.   return __pos;
  1431. }
  1432.  
  1433. template <class _Tp, class _Alloc>
  1434. void deque<_Tp,_Alloc>::_M_insert_aux(iterator __pos,
  1435.                                       size_type __n,
  1436.                                       const value_type& __x)
  1437. {
  1438.   const difference_type __elems_before = __pos - _M_start;
  1439.   size_type __length = this->size();
  1440.   value_type __x_copy = __x;
  1441.   if (__elems_before < difference_type(__length / 2)) {
  1442.     iterator __new_start = _M_reserve_elements_at_front(__n);
  1443.     iterator __old_start = _M_start;
  1444.     __pos = _M_start + __elems_before;
  1445.     try {
  1446.       if (__elems_before >= difference_type(__n)) {
  1447.         iterator __start_n = _M_start + difference_type(__n);
  1448.         uninitialized_copy(_M_start, __start_n, __new_start);
  1449.         _M_start = __new_start;
  1450.         copy(__start_n, __pos, __old_start);
  1451.         fill(__pos - difference_type(__n), __pos, __x_copy);
  1452.       }
  1453.       else {
  1454.         __uninitialized_copy_fill(_M_start, __pos, __new_start, 
  1455.                                   _M_start, __x_copy);
  1456.         _M_start = __new_start;
  1457.         fill(__old_start, __pos, __x_copy);
  1458.       }
  1459.     }
  1460.     catch(...)
  1461.       { 
  1462.     _M_destroy_nodes(__new_start._M_node, _M_start._M_node);
  1463.     __throw_exception_again;
  1464.       }
  1465.   }
  1466.   else {
  1467.     iterator __new_finish = _M_reserve_elements_at_back(__n);
  1468.     iterator __old_finish = _M_finish;
  1469.     const difference_type __elems_after = 
  1470.       difference_type(__length) - __elems_before;
  1471.     __pos = _M_finish - __elems_after;
  1472.     try {
  1473.       if (__elems_after > difference_type(__n)) {
  1474.         iterator __finish_n = _M_finish - difference_type(__n);
  1475.         uninitialized_copy(__finish_n, _M_finish, _M_finish);
  1476.         _M_finish = __new_finish;
  1477.         copy_backward(__pos, __finish_n, __old_finish);
  1478.         fill(__pos, __pos + difference_type(__n), __x_copy);
  1479.       }
  1480.       else {
  1481.         __uninitialized_fill_copy(_M_finish, __pos + difference_type(__n),
  1482.                                   __x_copy, __pos, _M_finish);
  1483.         _M_finish = __new_finish;
  1484.         fill(__pos, __old_finish, __x_copy);
  1485.       }
  1486.     }
  1487.     catch(...)
  1488.       { 
  1489.     _M_destroy_nodes(_M_finish._M_node + 1, __new_finish._M_node + 1);
  1490.     __throw_exception_again;
  1491.       }
  1492.   }
  1493. }
  1494.  
  1495. template <class _Tp, class _Alloc> template <class _ForwardIterator>
  1496. void deque<_Tp,_Alloc>::_M_insert_aux(iterator __pos,
  1497.                                       _ForwardIterator __first,
  1498.                                       _ForwardIterator __last,
  1499.                                       size_type __n)
  1500. {
  1501.   const difference_type __elemsbefore = __pos - _M_start;
  1502.   size_type __length = size();
  1503.   if (static_cast<size_type>(__elemsbefore) < __length / 2) {
  1504.     iterator __new_start = _M_reserve_elements_at_front(__n);
  1505.     iterator __old_start = _M_start;
  1506.     __pos = _M_start + __elemsbefore;
  1507.     try {
  1508.       if (__elemsbefore >= difference_type(__n)) {
  1509.         iterator __start_n = _M_start + difference_type(__n); 
  1510.         uninitialized_copy(_M_start, __start_n, __new_start);
  1511.         _M_start = __new_start;
  1512.         copy(__start_n, __pos, __old_start);
  1513.         copy(__first, __last, __pos - difference_type(__n));
  1514.       }
  1515.       else {
  1516.         _ForwardIterator __mid = __first;
  1517.         advance(__mid, difference_type(__n) - __elemsbefore);
  1518.         __uninitialized_copy_copy(_M_start, __pos, __first, __mid,
  1519.                                   __new_start);
  1520.         _M_start = __new_start;
  1521.         copy(__mid, __last, __old_start);
  1522.       }
  1523.     }
  1524.     catch(...)
  1525.       {
  1526.     _M_destroy_nodes(__new_start._M_node, _M_start._M_node);
  1527.     __throw_exception_again;
  1528.       }
  1529.   }
  1530.   else {
  1531.     iterator __new_finish = _M_reserve_elements_at_back(__n);
  1532.     iterator __old_finish = _M_finish;
  1533.     const difference_type __elemsafter = 
  1534.       difference_type(__length) - __elemsbefore;
  1535.     __pos = _M_finish - __elemsafter;
  1536.     try {
  1537.       if (__elemsafter > difference_type(__n)) {
  1538.         iterator __finish_n = _M_finish - difference_type(__n);
  1539.         uninitialized_copy(__finish_n, _M_finish, _M_finish);
  1540.         _M_finish = __new_finish;
  1541.         copy_backward(__pos, __finish_n, __old_finish);
  1542.         copy(__first, __last, __pos);
  1543.       }
  1544.       else {
  1545.         _ForwardIterator __mid = __first;
  1546.         advance(__mid, __elemsafter);
  1547.         __uninitialized_copy_copy(__mid, __last, __pos, _M_finish, _M_finish);
  1548.         _M_finish = __new_finish;
  1549.         copy(__first, __mid, __pos);
  1550.       }
  1551.     }
  1552.     catch(...)
  1553.       {
  1554.     _M_destroy_nodes(_M_finish._M_node + 1, __new_finish._M_node + 1);
  1555.     __throw_exception_again;
  1556.       }
  1557.   }
  1558. }
  1559.  
  1560. template <class _Tp, class _Alloc>
  1561. void deque<_Tp,_Alloc>::_M_new_elements_at_front(size_type __new_elems)
  1562. {
  1563.   size_type __new_nodes
  1564.       = (__new_elems + _S_buffer_size() - 1) / _S_buffer_size();
  1565.   _M_reserve_map_at_front(__new_nodes);
  1566.   size_type __i;
  1567.   try {
  1568.     for (__i = 1; __i <= __new_nodes; ++__i)
  1569.       *(_M_start._M_node - __i) = _M_allocate_node();
  1570.   }
  1571.   catch(...) {
  1572.     for (size_type __j = 1; __j < __i; ++__j)
  1573.       _M_deallocate_node(*(_M_start._M_node - __j));      
  1574.     __throw_exception_again;
  1575.   }
  1576. }
  1577.  
  1578. template <class _Tp, class _Alloc>
  1579. void deque<_Tp,_Alloc>::_M_new_elements_at_back(size_type __new_elems)
  1580. {
  1581.   size_type __new_nodes
  1582.       = (__new_elems + _S_buffer_size() - 1) / _S_buffer_size();
  1583.   _M_reserve_map_at_back(__new_nodes);
  1584.   size_type __i;
  1585.   try {
  1586.     for (__i = 1; __i <= __new_nodes; ++__i)
  1587.       *(_M_finish._M_node + __i) = _M_allocate_node();
  1588.   }
  1589.   catch(...) {
  1590.     for (size_type __j = 1; __j < __i; ++__j)
  1591.       _M_deallocate_node(*(_M_finish._M_node + __j));      
  1592.     __throw_exception_again;
  1593.   }
  1594. }
  1595.  
  1596. template <class _Tp, class _Alloc>
  1597. void deque<_Tp,_Alloc>::_M_reallocate_map(size_type __nodes_to_add,
  1598.                                           bool __add_at_front)
  1599. {
  1600.   size_type __old_num_nodes = _M_finish._M_node - _M_start._M_node + 1;
  1601.   size_type __new_num_nodes = __old_num_nodes + __nodes_to_add;
  1602.  
  1603.   _Map_pointer __new_nstart;
  1604.   if (_M_map_size > 2 * __new_num_nodes) {
  1605.     __new_nstart = _M_map + (_M_map_size - __new_num_nodes) / 2 
  1606.                      + (__add_at_front ? __nodes_to_add : 0);
  1607.     if (__new_nstart < _M_start._M_node)
  1608.       copy(_M_start._M_node, _M_finish._M_node + 1, __new_nstart);
  1609.     else
  1610.       copy_backward(_M_start._M_node, _M_finish._M_node + 1, 
  1611.                     __new_nstart + __old_num_nodes);
  1612.   }
  1613.   else {
  1614.     size_type __new_map_size = 
  1615.       _M_map_size + max(_M_map_size, __nodes_to_add) + 2;
  1616.  
  1617.     _Map_pointer __new_map = _M_allocate_map(__new_map_size);
  1618.     __new_nstart = __new_map + (__new_map_size - __new_num_nodes) / 2
  1619.                          + (__add_at_front ? __nodes_to_add : 0);
  1620.     copy(_M_start._M_node, _M_finish._M_node + 1, __new_nstart);
  1621.     _M_deallocate_map(_M_map, _M_map_size);
  1622.  
  1623.     _M_map = __new_map;
  1624.     _M_map_size = __new_map_size;
  1625.   }
  1626.  
  1627.   _M_start._M_set_node(__new_nstart);
  1628.   _M_finish._M_set_node(__new_nstart + __old_num_nodes - 1);
  1629. }
  1630.  
  1631.  
  1632. // Nonmember functions.
  1633.  
  1634. template <class _Tp, class _Alloc>
  1635. inline bool operator==(const deque<_Tp, _Alloc>& __x,
  1636.                        const deque<_Tp, _Alloc>& __y) {
  1637.   return __x.size() == __y.size() &&
  1638.          equal(__x.begin(), __x.end(), __y.begin());
  1639. }
  1640.  
  1641. template <class _Tp, class _Alloc>
  1642. inline bool operator<(const deque<_Tp, _Alloc>& __x,
  1643.                       const deque<_Tp, _Alloc>& __y) {
  1644.   return lexicographical_compare(__x.begin(), __x.end(), 
  1645.                                  __y.begin(), __y.end());
  1646. }
  1647.  
  1648. template <class _Tp, class _Alloc>
  1649. inline bool operator!=(const deque<_Tp, _Alloc>& __x,
  1650.                        const deque<_Tp, _Alloc>& __y) {
  1651.   return !(__x == __y);
  1652. }
  1653.  
  1654. template <class _Tp, class _Alloc>
  1655. inline bool operator>(const deque<_Tp, _Alloc>& __x,
  1656.                       const deque<_Tp, _Alloc>& __y) {
  1657.   return __y < __x;
  1658. }
  1659.  
  1660. template <class _Tp, class _Alloc>
  1661. inline bool operator<=(const deque<_Tp, _Alloc>& __x,
  1662.                        const deque<_Tp, _Alloc>& __y) {
  1663.   return !(__y < __x);
  1664. }
  1665. template <class _Tp, class _Alloc>
  1666. inline bool operator>=(const deque<_Tp, _Alloc>& __x,
  1667.                        const deque<_Tp, _Alloc>& __y) {
  1668.   return !(__x < __y);
  1669. }
  1670.  
  1671. template <class _Tp, class _Alloc>
  1672. inline void swap(deque<_Tp,_Alloc>& __x, deque<_Tp,_Alloc>& __y) {
  1673.   __x.swap(__y);
  1674. }
  1675.  
  1676. } // namespace std 
  1677.   
  1678. #endif /* __GLIBCPP_INTERNAL_DEQUE_H */
  1679.  
  1680.