epoc32/include/stdapis/stlport/stl/_deque.c
author William Roberts <williamr@symbian.org>
Tue, 16 Mar 2010 16:12:26 +0000
branchSymbian2
changeset 2 2fe1408b6811
parent 0 061f57f2323e
permissions -rw-r--r--
Final list of Symbian^2 public API header files
     1 /*
     2  *
     3  *
     4  * Copyright (c) 1994
     5  * Hewlett-Packard Company
     6  *
     7  * Copyright (c) 1996,1997
     8  * Silicon Graphics Computer Systems, Inc.
     9  *
    10  * Copyright (c) 1997
    11  * Moscow Center for SPARC Technology
    12  *
    13  * Copyright (c) 1999 
    14  * Boris Fomitchev
    15  *
    16  * This material is provided "as is", with absolutely no warranty expressed
    17  * or implied. Any use is at your own risk.
    18  *
    19  * Permission to use or copy this software for any purpose is hereby granted 
    20  * without fee, provided the above notices are retained on all copies.
    21  * Permission to modify the code and to distribute modified code is granted,
    22  * provided the above notices are retained, and a notice that the code was
    23  * modified is included with the above copyright notice.
    24  *
    25  */
    26 #ifndef _STLP_DEQUE_C
    27 # define _STLP_DEQUE_C
    28 
    29 # ifndef _STLP_INTERNAL_DEQUE_H
    30 #  include <stl/_deque.h>
    31 # endif
    32 
    33 _STLP_BEGIN_NAMESPACE
    34 
    35 // Non-inline member functions from _Deque_base.
    36 
    37 template <class _Tp, class _Alloc >
    38 _Deque_base<_Tp,_Alloc >::~_Deque_base() {
    39   if (_M_map._M_data) {
    40     if (_M_start._M_node) {
    41       _M_destroy_nodes(_M_start._M_node, this->_M_finish._M_node + 1);
    42     }
    43     _M_map.deallocate(_M_map._M_data, _M_map_size._M_data);
    44   }
    45 }
    46 
    47 template <class _Tp, class _Alloc >
    48 void
    49 _Deque_base<_Tp,_Alloc>::_M_initialize_map(size_t __num_elements)
    50 {
    51   size_t __num_nodes = 
    52     __num_elements / this->buffer_size() + 1 ;
    53 
    54   _M_map_size._M_data = (max)((size_t) _S_initial_map_size, __num_nodes + 2);
    55   _M_map._M_data = _M_map.allocate(_M_map_size._M_data);
    56 
    57   _Tp** __nstart = _M_map._M_data + (_M_map_size._M_data - __num_nodes) / 2;
    58   _Tp** __nfinish = __nstart + __num_nodes;
    59     
    60   _STLP_TRY {
    61     _M_create_nodes(__nstart, __nfinish);
    62   }
    63   _STLP_UNWIND((_M_map.deallocate(_M_map._M_data, _M_map_size._M_data), 
    64                 _M_map._M_data = 0, _M_map_size._M_data = 0));
    65   _M_start._M_set_node(__nstart);
    66   this->_M_finish._M_set_node(__nfinish - 1);
    67   _M_start._M_cur = _M_start._M_first;
    68   this->_M_finish._M_cur = this->_M_finish._M_first +
    69                __num_elements % this->buffer_size();
    70 }
    71 
    72 template <class _Tp, class _Alloc >
    73 void
    74 _Deque_base<_Tp,_Alloc>::_M_create_nodes(_Tp** __nstart,
    75                                                   _Tp** __nfinish)
    76 {
    77   _Tp** _STLP_LEAVE_VOLATILE __cur = 0;
    78   _STLP_TRY {
    79     for (__cur = __nstart; __cur < __nfinish; ++__cur)
    80       *__cur = _M_map_size.allocate(this->buffer_size());
    81   }
    82   _STLP_UNWIND(_M_destroy_nodes(__nstart, __cur));
    83 }
    84 
    85 template <class _Tp, class _Alloc >
    86 void 
    87 _Deque_base<_Tp,_Alloc>::_M_destroy_nodes(_Tp** __nstart,
    88                                                    _Tp** __nfinish)
    89 {
    90   for (_Tp** __n = __nstart; __n < __nfinish; ++__n)
    91     _M_map_size.deallocate(*__n, this->buffer_size());
    92 }
    93 
    94 
    95 
    96 // Non-inline member functions
    97 
    98 # if defined ( _STLP_NESTED_TYPE_PARAM_BUG )
    99 // qualified references 
   100 #   define __iterator__           _Deque_iterator<_Tp, _Nonconst_traits<_Tp> >
   101 #   define const_iterator         _Deque_iterator<_Tp, _Const_traits<_Tp>  > 
   102 #   define iterator               __iterator__
   103 #   define size_type              size_t
   104 #   define value_type             _Tp
   105 # else
   106 #  define __iterator__           _STLP_TYPENAME_ON_RETURN_TYPE __deque__<_Tp, _Alloc>::iterator
   107 # endif
   108 
   109 template <class _Tp, class _Alloc >
   110 __deque__<_Tp, _Alloc >&  
   111 __deque__<_Tp, _Alloc >::operator= (const __deque__<_Tp, _Alloc >& __x) {
   112   const size_type __len = size();
   113   if (&__x != this) {
   114     if (__len >= __x.size())
   115       erase(copy(__x.begin(), __x.end(), this->_M_start), this->_M_finish);
   116     else {
   117       const_iterator __mid = __x.begin() + difference_type(__len);
   118       copy(__x.begin(), __mid, this->_M_start);
   119       insert(this->_M_finish, __mid, __x.end());
   120     }
   121   }
   122   return *this;
   123 }        
   124 
   125 template <class _Tp, class _Alloc >
   126 void 
   127 __deque__<_Tp, _Alloc >::_M_fill_insert(iterator __pos,
   128 					     size_type __n, const value_type& __x)
   129 {
   130   if (__pos._M_cur == this->_M_start._M_cur) {
   131     iterator __new_start = _M_reserve_elements_at_front(__n);
   132     _STLP_TRY {
   133       uninitialized_fill(__new_start, this->_M_start, __x);
   134     }
   135     _STLP_UNWIND(this->_M_destroy_nodes(__new_start._M_node, this->_M_start._M_node));
   136     this->_M_start = __new_start;
   137   }
   138   else if (__pos._M_cur == this->_M_finish._M_cur) {
   139     iterator __new_finish = _M_reserve_elements_at_back(__n);
   140     _STLP_TRY {
   141       uninitialized_fill(this->_M_finish, __new_finish, __x);
   142     }
   143     _STLP_UNWIND(this->_M_destroy_nodes(this->_M_finish._M_node+1, __new_finish._M_node+1));
   144     this->_M_finish = __new_finish;
   145   }
   146   else 
   147     _M_insert_aux(__pos, __n, __x);
   148 }
   149 
   150 #ifndef _STLP_MEMBER_TEMPLATES  
   151 
   152 template <class _Tp, class _Alloc >
   153 void __deque__<_Tp, _Alloc>::insert(iterator __pos,
   154                                            const value_type* __first,
   155                                            const value_type* __last) {
   156   size_type __n = __last - __first;
   157   if (__pos._M_cur == this->_M_start._M_cur) {
   158     iterator __new_start = _M_reserve_elements_at_front(__n);
   159     _STLP_TRY {
   160       uninitialized_copy(__first, __last, __new_start);
   161     }
   162     _STLP_UNWIND(this->_M_destroy_nodes(__new_start._M_node, this->_M_start._M_node));
   163     this->_M_start = __new_start;
   164   }
   165   else if (__pos._M_cur == this->_M_finish._M_cur) {
   166     iterator __new_finish = _M_reserve_elements_at_back(__n);
   167     _STLP_TRY {
   168       uninitialized_copy(__first, __last, this->_M_finish);
   169     }
   170     _STLP_UNWIND(this->_M_destroy_nodes(this->_M_finish._M_node + 1, 
   171                                   __new_finish._M_node + 1));
   172     this->_M_finish = __new_finish;
   173   }
   174   else
   175     _M_insert_aux(__pos, __first, __last, __n);
   176 }
   177 
   178 template <class _Tp, class _Alloc >
   179 void __deque__<_Tp,_Alloc>::insert(iterator __pos,
   180                                          const_iterator __first,
   181                                          const_iterator __last)
   182 {
   183   size_type __n = __last - __first;
   184   if (__pos._M_cur == this->_M_start._M_cur) {
   185     iterator __new_start = _M_reserve_elements_at_front(__n);
   186     _STLP_TRY {
   187       uninitialized_copy(__first, __last, __new_start);
   188     }
   189     _STLP_UNWIND(this->_M_destroy_nodes(__new_start._M_node, this->_M_start._M_node));
   190     this->_M_start = __new_start;
   191   }
   192   else if (__pos._M_cur == this->_M_finish._M_cur) {
   193     iterator __new_finish = _M_reserve_elements_at_back(__n);
   194     _STLP_TRY {
   195       uninitialized_copy(__first, __last, this->_M_finish);
   196     }
   197     _STLP_UNWIND(this->_M_destroy_nodes(this->_M_finish._M_node + 1,__new_finish._M_node + 1));
   198     this->_M_finish = __new_finish;
   199   }
   200   else
   201     _M_insert_aux(__pos, __first, __last, __n);
   202 }
   203 
   204 #endif /* _STLP_MEMBER_TEMPLATES */
   205 
   206 template <class _Tp, class _Alloc >
   207 __iterator__ 
   208 __deque__<_Tp,_Alloc>::erase(iterator __first, iterator __last)
   209 {
   210   if (__first == this->_M_start && __last == this->_M_finish) {
   211     clear();
   212     return this->_M_finish;
   213   }
   214   else {
   215     difference_type __n = __last - __first;
   216     difference_type __elems_before = __first - this->_M_start;
   217     if (__elems_before < difference_type(this->size() - __n) / 2) {
   218       copy_backward(this->_M_start, __first, __last);
   219       iterator __new_start = this->_M_start + __n;
   220       _STLP_STD::_Destroy(this->_M_start, __new_start);
   221       this->_M_destroy_nodes(this->_M_start._M_node, __new_start._M_node);
   222       this->_M_start = __new_start;
   223     }
   224     else {
   225       copy(__last, this->_M_finish, __first);
   226       iterator __new_finish = this->_M_finish - __n;
   227       _STLP_STD::_Destroy(__new_finish, this->_M_finish);
   228       this->_M_destroy_nodes(__new_finish._M_node + 1, this->_M_finish._M_node + 1);
   229       this->_M_finish = __new_finish;
   230     }
   231     return this->_M_start + __elems_before;
   232   }
   233 }
   234 
   235 template <class _Tp, class _Alloc >
   236 void __deque__<_Tp,_Alloc>::clear()
   237 {
   238   for (_Map_pointer __node = this->_M_start._M_node + 1;
   239        __node < this->_M_finish._M_node;
   240        ++__node) {
   241     _STLP_STD::_Destroy(*__node, *__node + this->buffer_size());
   242     this->_M_map_size.deallocate(*__node, this->buffer_size());
   243   }
   244 
   245   if (this->_M_start._M_node != this->_M_finish._M_node) {
   246     _STLP_STD::_Destroy(this->_M_start._M_cur, this->_M_start._M_last);
   247     _STLP_STD::_Destroy(this->_M_finish._M_first, this->_M_finish._M_cur);
   248     this->_M_map_size.deallocate(this->_M_finish._M_first, this->buffer_size());
   249   }
   250   else
   251     _STLP_STD::_Destroy(this->_M_start._M_cur, this->_M_finish._M_cur);
   252 
   253   this->_M_finish = this->_M_start;
   254 }
   255 
   256 // Precondition: this->_M_start and this->_M_finish have already been initialized,
   257 // but none of the deque's elements have yet been constructed.
   258 template <class _Tp, class _Alloc >
   259 void 
   260 __deque__<_Tp,_Alloc>::_M_fill_initialize(const value_type& __val) {
   261   _STLP_LEAVE_VOLATILE _Map_pointer __cur = 0;
   262   _STLP_TRY {
   263     for (__cur = this->_M_start._M_node; __cur < this->_M_finish._M_node; ++__cur)
   264       uninitialized_fill(*__cur, *__cur + this->buffer_size(), __val);
   265     uninitialized_fill(this->_M_finish._M_first, this->_M_finish._M_cur, __val);
   266   }
   267   _STLP_UNWIND(_STLP_STD::_Destroy(this->_M_start, iterator(*__cur, __cur)));
   268 }
   269 
   270 
   271 // Called only if this->_M_finish._M_cur == this->_M_finish._M_last - 1.
   272 template <class _Tp, class _Alloc >
   273 void
   274 __deque__<_Tp,_Alloc>::_M_push_back_aux_v(const value_type& __t)
   275 {
   276   value_type __t_copy = __t;
   277   _STLP_PUSH_CLEANUP_ITEM(value_type, &__t_copy); 
   278   _M_reserve_map_at_back();
   279   *(this->_M_finish._M_node + 1) = this->_M_map_size.allocate(this->buffer_size());
   280   _STLP_TRY {
   281     _Construct(this->_M_finish._M_cur, __t_copy);
   282     this->_M_finish._M_set_node(this->_M_finish._M_node + 1);
   283     this->_M_finish._M_cur = this->_M_finish._M_first;
   284   }
   285   _STLP_UNWIND(this->_M_map_size.deallocate(*(this->_M_finish._M_node + 1), 
   286 				      this->buffer_size()));
   287 #ifdef _STLP_USE_TRAP_LEAVE
   288     CleanupStack::Pop(); 
   289 #endif  
   290 }
   291 
   292 # ifndef _STLP_NO_ANACHRONISMS
   293 // Called only if this->_M_finish._M_cur == this->_M_finish._M_last - 1.
   294 template <class _Tp, class _Alloc >
   295 void
   296 __deque__<_Tp,_Alloc>::_M_push_back_aux()
   297 {
   298   _M_reserve_map_at_back();
   299   *(this->_M_finish._M_node + 1) = this->_M_map_size.allocate(this->buffer_size());
   300   _STLP_TRY {
   301     _Construct(this->_M_finish._M_cur);
   302     this->_M_finish._M_set_node(this->_M_finish._M_node + 1);
   303     this->_M_finish._M_cur = this->_M_finish._M_first;
   304   }
   305   _STLP_UNWIND(this->_M_map_size.deallocate(*(this->_M_finish._M_node + 1), 
   306 				      this->buffer_size()));
   307 }
   308 # endif
   309 
   310 // Called only if this->_M_start._M_cur == this->_M_start._M_first.
   311 template <class _Tp, class _Alloc >
   312 void 
   313 __deque__<_Tp,_Alloc>::_M_push_front_aux_v(const value_type& __t)
   314 {
   315   value_type __t_copy = __t;
   316   _STLP_PUSH_CLEANUP_ITEM(value_type, &__t_copy); 
   317   _M_reserve_map_at_front();
   318   *(this->_M_start._M_node - 1) = this->_M_map_size.allocate(this->buffer_size());
   319   _STLP_TRY {
   320     this->_M_start._M_set_node(this->_M_start._M_node - 1);
   321     this->_M_start._M_cur = this->_M_start._M_last - 1;
   322     _Construct(this->_M_start._M_cur, __t_copy);
   323   }
   324   _STLP_UNWIND((++this->_M_start, 
   325 		this->_M_map_size.deallocate(*(this->_M_start._M_node - 1), this->buffer_size())));
   326 #ifdef _STLP_USE_TRAP_LEAVE
   327     CleanupStack::Pop(); 
   328 #endif  
   329 } 
   330 
   331 
   332 # ifndef _STLP_NO_ANACHRONISMS
   333 // Called only if this->_M_start._M_cur == this->_M_start._M_first.
   334 template <class _Tp, class _Alloc >
   335 void 
   336 __deque__<_Tp,_Alloc>::_M_push_front_aux()
   337 {
   338   _M_reserve_map_at_front();
   339   *(this->_M_start._M_node - 1) = this->_M_map_size.allocate(this->buffer_size());
   340   _STLP_TRY {
   341     this->_M_start._M_set_node(this->_M_start._M_node - 1);
   342     this->_M_start._M_cur = this->_M_start._M_last - 1;
   343     _Construct(this->_M_start._M_cur);
   344   }
   345   _STLP_UNWIND((++this->_M_start, this->_M_map_size.deallocate(*(this->_M_start._M_node - 1), 
   346 						   this->buffer_size() )));
   347 } 
   348 # endif
   349 
   350 // Called only if this->_M_finish._M_cur == this->_M_finish._M_first.
   351 template <class _Tp, class _Alloc >
   352 void 
   353 __deque__<_Tp,_Alloc>::_M_pop_back_aux()
   354 {
   355   this->_M_map_size.deallocate(this->_M_finish._M_first, this->buffer_size());
   356   this->_M_finish._M_set_node(this->_M_finish._M_node - 1);
   357   this->_M_finish._M_cur = this->_M_finish._M_last - 1;
   358   _STLP_STD::_Destroy(this->_M_finish._M_cur);
   359 }
   360 
   361 // Called only if this->_M_start._M_cur == this->_M_start._M_last - 1.  Note that 
   362 // if the deque has at least one element (a precondition for this member 
   363 // function), and if this->_M_start._M_cur == this->_M_start._M_last, then the deque 
   364 // must have at least two nodes.
   365 template <class _Tp, class _Alloc >
   366 void 
   367 __deque__<_Tp,_Alloc>::_M_pop_front_aux()
   368 {
   369   _STLP_STD::_Destroy(this->_M_start._M_cur);
   370   this->_M_map_size.deallocate(this->_M_start._M_first, this->buffer_size());
   371   this->_M_start._M_set_node(this->_M_start._M_node + 1);
   372   this->_M_start._M_cur = this->_M_start._M_first;
   373 }      
   374 
   375 
   376 
   377 template <class _Tp, class _Alloc >
   378 __iterator__
   379 __deque__<_Tp,_Alloc>::_M_insert_aux_prepare(iterator __pos) {
   380   difference_type __index = __pos - this->_M_start;
   381   if (__index < difference_type(size() / 2)) {
   382     push_front(front());
   383     iterator __front1 = this->_M_start;
   384     ++__front1;
   385     iterator __front2 = __front1;
   386     ++__front2;
   387     __pos = this->_M_start + __index;
   388     iterator __pos1 = __pos;
   389     ++__pos1;
   390     copy(__front2, __pos1, __front1);
   391   }
   392   else {
   393     push_back(back());
   394     iterator __back1 = this->_M_finish;
   395     --__back1;
   396     iterator __back2 = __back1;
   397     --__back2;
   398     __pos = this->_M_start + __index;
   399     copy_backward(__pos, __back2, __back1);
   400   }
   401   return __pos;
   402 }
   403 
   404 template <class _Tp, class _Alloc >
   405 __iterator__
   406 __deque__<_Tp,_Alloc>::_M_insert_aux(iterator __pos,
   407 				     const value_type& __x) {
   408   value_type __x_copy = __x;
   409   _STLP_PUSH_CLEANUP_ITEM(value_type, &__x_copy); 
   410   _STLP_MPWFIX_TRY		//*TY 06/01/2000 - mpw forget to call dtor on __x_copy without this try block
   411   __pos = _M_insert_aux_prepare(__pos);
   412   *__pos = __x_copy;
   413 #ifdef _STLP_USE_TRAP_LEAVE
   414     CleanupStack::Pop(); 
   415 #endif 
   416   return __pos;
   417   _STLP_MPWFIX_CATCH		//*TY 06/01/2000 - 
   418 }
   419 
   420 template <class _Tp, class _Alloc >
   421 __iterator__
   422 __deque__<_Tp,_Alloc>::_M_insert_aux(iterator __pos)
   423 {
   424   __pos = _M_insert_aux_prepare(__pos);
   425   *__pos = value_type();
   426   return __pos;
   427 }
   428 
   429 template <class _Tp, class _Alloc >
   430 void
   431 __deque__<_Tp,_Alloc>::_M_insert_aux(iterator __pos,
   432                                            size_type __n,
   433                                            const value_type& __x)
   434 {
   435   const difference_type __elems_before = __pos - this->_M_start;
   436   size_type __length = this->size();
   437   value_type __x_copy = __x;
   438   _STLP_PUSH_CLEANUP_ITEM(value_type, &__x_copy); 
   439 
   440   if (__elems_before < difference_type(__length / 2)) {
   441     iterator __new_start = _M_reserve_elements_at_front(__n);
   442     iterator __old_start = this->_M_start;
   443     __pos = this->_M_start + __elems_before;
   444     _STLP_TRY {
   445       if (__elems_before >= difference_type(__n)) {
   446         iterator __start_n = this->_M_start + difference_type(__n);
   447         uninitialized_copy(this->_M_start, __start_n, __new_start);
   448         this->_M_start = __new_start;
   449         copy(__start_n, __pos, __old_start);
   450         fill(__pos - difference_type(__n), __pos, __x_copy);
   451       }
   452       else {
   453         __uninitialized_copy_fill(this->_M_start, __pos, __new_start, 
   454 	                          this->_M_start, __x_copy);
   455         this->_M_start = __new_start;
   456         fill(__old_start, __pos, __x_copy);
   457       }
   458     }
   459     _STLP_UNWIND(this->_M_destroy_nodes(__new_start._M_node, this->_M_start._M_node));
   460   }
   461   else {
   462     iterator __new_finish = _M_reserve_elements_at_back(__n);
   463     iterator __old_finish = this->_M_finish;
   464     const difference_type __elems_after = 
   465       difference_type(__length) - __elems_before;
   466     __pos = this->_M_finish - __elems_after;
   467     _STLP_TRY {
   468       if (__elems_after > difference_type(__n)) {
   469         iterator __finish_n = this->_M_finish - difference_type(__n);
   470         uninitialized_copy(__finish_n, this->_M_finish, this->_M_finish);
   471         this->_M_finish = __new_finish;
   472         copy_backward(__pos, __finish_n, __old_finish);
   473         fill(__pos, __pos + difference_type(__n), __x_copy);
   474       }
   475       else {
   476         __uninitialized_fill_copy(this->_M_finish, __pos + difference_type(__n),
   477                                   __x_copy, __pos, this->_M_finish);
   478         this->_M_finish = __new_finish;
   479         fill(__pos, __old_finish, __x_copy);
   480       }
   481     }
   482     _STLP_UNWIND(this->_M_destroy_nodes(this->_M_finish._M_node + 1, __new_finish._M_node + 1));
   483   }
   484 #ifdef _STLP_USE_TRAP_LEAVE
   485   CleanupStack::Pop();
   486 #endif  
   487 }
   488 
   489 #ifndef _STLP_MEMBER_TEMPLATES 
   490 template <class _Tp, class _Alloc >
   491 void 
   492 __deque__<_Tp,_Alloc>::_M_insert_aux(iterator __pos,
   493                                            const value_type* __first,
   494                                            const value_type* __last,
   495                                            size_type __n)
   496 {
   497 
   498   const difference_type __elemsbefore = __pos - this->_M_start;
   499   size_type __length = size();
   500   if (__elemsbefore < difference_type(__length / 2)) {
   501     iterator __new_start = _M_reserve_elements_at_front(__n);
   502     iterator __old_start = this->_M_start;
   503     __pos = this->_M_start + __elemsbefore;
   504     _STLP_TRY {
   505       if (__elemsbefore >= difference_type(__n)) {
   506         iterator __start_n = this->_M_start + difference_type(__n);
   507         uninitialized_copy(this->_M_start, __start_n, __new_start);
   508         this->_M_start = __new_start;
   509         copy(__start_n, __pos, __old_start);
   510         copy(__first, __last, __pos - difference_type(__n));
   511       }
   512       else {
   513         const value_type* __mid = 
   514 	  __first + (difference_type(__n) - __elemsbefore);
   515         __uninitialized_copy_copy(this->_M_start, __pos, __first, __mid,
   516                                   __new_start, _IsPODType());
   517         this->_M_start = __new_start;
   518         copy(__mid, __last, __old_start);
   519       }
   520     }
   521     _STLP_UNWIND(this->_M_destroy_nodes(__new_start._M_node, this->_M_start._M_node));
   522   }
   523   else {
   524     iterator __new_finish = _M_reserve_elements_at_back(__n);
   525     iterator __old_finish = this->_M_finish;
   526     const difference_type __elemsafter = 
   527       difference_type(__length) - __elemsbefore;
   528     __pos = this->_M_finish - __elemsafter;
   529     _STLP_TRY {
   530       if (__elemsafter > difference_type(__n)) {
   531         iterator __finish_n = this->_M_finish - difference_type(__n);
   532         uninitialized_copy(__finish_n, this->_M_finish, this->_M_finish);
   533         this->_M_finish = __new_finish;
   534         copy_backward(__pos, __finish_n, __old_finish);
   535         copy(__first, __last, __pos);
   536       }
   537       else {
   538         const value_type* __mid = __first + __elemsafter;
   539         __uninitialized_copy_copy(__mid, __last, __pos, this->_M_finish, this->_M_finish, _IsPODType());
   540         this->_M_finish = __new_finish;
   541         copy(__first, __mid, __pos);
   542       }
   543     }
   544     _STLP_UNWIND(this->_M_destroy_nodes(this->_M_finish._M_node + 1, __new_finish._M_node + 1));
   545   }
   546 }
   547 
   548 template <class _Tp, class _Alloc >
   549 void
   550 __deque__<_Tp,_Alloc>::_M_insert_aux(iterator __pos,
   551                                            const_iterator __first,
   552                                            const_iterator __last,
   553                                            size_type __n)
   554 {
   555   const difference_type __elemsbefore = __pos - this->_M_start;
   556   size_type __length = size();
   557   if (__elemsbefore < difference_type(__length / 2)) {
   558     iterator __new_start = _M_reserve_elements_at_front(__n);
   559     iterator __old_start = this->_M_start;
   560     __pos = this->_M_start + __elemsbefore;
   561     _STLP_TRY {
   562       if (__elemsbefore >= difference_type(__n)) {
   563         iterator __start_n = this->_M_start + __n;
   564         uninitialized_copy(this->_M_start, __start_n, __new_start);
   565         this->_M_start = __new_start;
   566         copy(__start_n, __pos, __old_start);
   567         copy(__first, __last, __pos - difference_type(__n));
   568       }
   569       else {
   570         const_iterator __mid = __first + (__n - __elemsbefore);
   571         __uninitialized_copy_copy(this->_M_start, __pos, __first, __mid,
   572                                   __new_start, _IsPODType());
   573         this->_M_start = __new_start;
   574         copy(__mid, __last, __old_start);
   575       }
   576     }
   577     _STLP_UNWIND(this->_M_destroy_nodes(__new_start._M_node, this->_M_start._M_node));
   578   }
   579   else {
   580     iterator __new_finish = _M_reserve_elements_at_back(__n);
   581     iterator __old_finish = this->_M_finish;
   582     const difference_type __elemsafter = __length - __elemsbefore;
   583     __pos = this->_M_finish - __elemsafter;
   584     _STLP_TRY {
   585       if (__elemsafter > difference_type(__n)) {
   586         iterator __finish_n = this->_M_finish - difference_type(__n);
   587         uninitialized_copy(__finish_n, this->_M_finish, this->_M_finish);
   588         this->_M_finish = __new_finish;
   589         copy_backward(__pos, __finish_n, __old_finish);
   590         copy(__first, __last, __pos);
   591       }
   592       else {
   593         const_iterator __mid = __first + __elemsafter;
   594         __uninitialized_copy_copy(__mid, __last, __pos, this->_M_finish, this->_M_finish, _IsPODType());
   595         this->_M_finish = __new_finish;
   596         copy(__first, __mid, __pos);
   597       }
   598     }
   599     _STLP_UNWIND(this->_M_destroy_nodes(this->_M_finish._M_node + 1, __new_finish._M_node + 1));
   600   }
   601 }
   602 
   603 #endif /* _STLP_MEMBER_TEMPLATES */
   604 
   605 template <class _Tp, class _Alloc >
   606 void 
   607 __deque__<_Tp,_Alloc>::_M_new_elements_at_front(size_type __new_elems)
   608 {
   609   size_type __new_nodes
   610       = (__new_elems + this->buffer_size() - 1) / this->buffer_size();
   611   _M_reserve_map_at_front(__new_nodes);
   612   size_type __i =1;
   613   _STLP_TRY {
   614     for (; __i <= __new_nodes; ++__i)
   615       *(this->_M_start._M_node - __i) = this->_M_map_size.allocate(this->buffer_size());
   616   }
   617   _STLP_CATCH_ALL {
   618     for (size_type __j = 1; __j < __i; ++__j)
   619       this->_M_map_size.deallocate(*(this->_M_start._M_node - __j), this->buffer_size());
   620     _STLP_RETHROW;
   621   }
   622 }
   623 
   624 template <class _Tp, class _Alloc >
   625 void 
   626 __deque__<_Tp,_Alloc>::_M_new_elements_at_back(size_type __new_elems)
   627 {
   628   size_type __new_nodes
   629       = (__new_elems + this->buffer_size() - 1) / this->buffer_size();
   630   _M_reserve_map_at_back(__new_nodes);
   631   size_type __i = 1;
   632   _STLP_TRY {
   633     for (; __i <= __new_nodes; ++__i)
   634       *(this->_M_finish._M_node + __i) = this->_M_map_size.allocate(this->buffer_size());
   635   }
   636   _STLP_CATCH_ALL {
   637     for (size_type __j = 1; __j < __i; ++__j)
   638       this->_M_map_size.deallocate(*(this->_M_finish._M_node + __j), this->buffer_size());
   639     _STLP_RETHROW;
   640   }
   641 }
   642 
   643 template <class _Tp, class _Alloc >
   644 void 
   645 __deque__<_Tp,_Alloc>::_M_reallocate_map(size_type __nodes_to_add,
   646                                               bool __add_at_front)
   647 {
   648   size_type __old_num_nodes = this->_M_finish._M_node - this->_M_start._M_node + 1;
   649   size_type __new_num_nodes = __old_num_nodes + __nodes_to_add;
   650 
   651   _Map_pointer __new_nstart;
   652   if (this->_M_map_size._M_data > 2 * __new_num_nodes) {
   653     __new_nstart = this->_M_map._M_data + (this->_M_map_size._M_data - __new_num_nodes) / 2 
   654                      + (__add_at_front ? __nodes_to_add : 0);
   655     if (__new_nstart < this->_M_start._M_node)
   656       copy(this->_M_start._M_node, this->_M_finish._M_node + 1, __new_nstart);
   657     else
   658       copy_backward(this->_M_start._M_node, this->_M_finish._M_node + 1, 
   659                     __new_nstart + __old_num_nodes);
   660   }
   661   else {
   662     size_type __new_map_size = 
   663       this->_M_map_size._M_data + (max)((size_t)this->_M_map_size._M_data, __nodes_to_add) + 2;
   664 
   665     _Map_pointer __new_map = this->_M_map.allocate(__new_map_size);
   666     __new_nstart = __new_map + (__new_map_size - __new_num_nodes) / 2
   667                          + (__add_at_front ? __nodes_to_add : 0);
   668     copy(this->_M_start._M_node, this->_M_finish._M_node + 1, __new_nstart);
   669     this->_M_map.deallocate(this->_M_map._M_data, this->_M_map_size._M_data);
   670 
   671     this->_M_map._M_data = __new_map;
   672     this->_M_map_size._M_data = __new_map_size;
   673   }
   674 
   675   this->_M_start._M_set_node(__new_nstart);
   676   this->_M_finish._M_set_node(__new_nstart + __old_num_nodes - 1);
   677 }
   678 
   679 _STLP_END_NAMESPACE
   680 
   681 # undef __iterator__
   682 # undef iterator
   683 # undef const_iterator
   684 # undef size_type
   685 # undef value_type
   686 
   687 #endif /*  _STLP_DEQUE_C */
   688 
   689 // Local Variables:
   690 // mode:C++
   691 // End: