deque.tcc

Go to the documentation of this file.
00001 // Deque implementation (out of line) -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
00004 // 2009, 2010
00005 // Free Software Foundation, Inc.
00006 //
00007 // This file is part of the GNU ISO C++ Library.  This library is free
00008 // software; you can redistribute it and/or modify it under the
00009 // terms of the GNU General Public License as published by the
00010 // Free Software Foundation; either version 3, or (at your option)
00011 // any later version.
00012 
00013 // This library is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 
00018 // Under Section 7 of GPL version 3, you are granted additional
00019 // permissions described in the GCC Runtime Library Exception, version
00020 // 3.1, as published by the Free Software Foundation.
00021 
00022 // You should have received a copy of the GNU General Public License and
00023 // a copy of the GCC Runtime Library Exception along with this program;
00024 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00025 // <http://www.gnu.org/licenses/>.
00026 
00027 /*
00028  *
00029  * Copyright (c) 1994
00030  * Hewlett-Packard Company
00031  *
00032  * Permission to use, copy, modify, distribute and sell this software
00033  * and its documentation for any purpose is hereby granted without fee,
00034  * provided that the above copyright notice appear in all copies and
00035  * that both that copyright notice and this permission notice appear
00036  * in supporting documentation.  Hewlett-Packard Company makes no
00037  * representations about the suitability of this software for any
00038  * purpose.  It is provided "as is" without express or implied warranty.
00039  *
00040  *
00041  * Copyright (c) 1997
00042  * Silicon Graphics Computer Systems, Inc.
00043  *
00044  * Permission to use, copy, modify, distribute and sell this software
00045  * and its documentation for any purpose is hereby granted without fee,
00046  * provided that the above copyright notice appear in all copies and
00047  * that both that copyright notice and this permission notice appear
00048  * in supporting documentation.  Silicon Graphics makes no
00049  * representations about the suitability of this software for any
00050  * purpose.  It is provided "as is" without express or implied warranty.
00051  */
00052 
00053 /** @file deque.tcc
00054  *  This is an internal header file, included by other library headers.
00055  *  You should not attempt to use it directly.
00056  */
00057 
00058 #ifndef _DEQUE_TCC
00059 #define _DEQUE_TCC 1
00060 
00061 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
00062 
00063 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00064   template <typename _Tp, typename _Alloc>
00065     void
00066     deque<_Tp, _Alloc>::
00067     _M_default_initialize()
00068     {
00069       _Map_pointer __cur;
00070       __try
00071         {
00072           for (__cur = this->_M_impl._M_start._M_node;
00073            __cur < this->_M_impl._M_finish._M_node;
00074            ++__cur)
00075             std::__uninitialized_default_a(*__cur, *__cur + _S_buffer_size(),
00076                        _M_get_Tp_allocator());
00077           std::__uninitialized_default_a(this->_M_impl._M_finish._M_first,
00078                      this->_M_impl._M_finish._M_cur,
00079                      _M_get_Tp_allocator());
00080         }
00081       __catch(...)
00082         {
00083           std::_Destroy(this->_M_impl._M_start, iterator(*__cur, __cur),
00084             _M_get_Tp_allocator());
00085           __throw_exception_again;
00086         }
00087     }
00088 #endif
00089 
00090   template <typename _Tp, typename _Alloc>
00091     deque<_Tp, _Alloc>&
00092     deque<_Tp, _Alloc>::
00093     operator=(const deque& __x)
00094     {
00095       const size_type __len = size();
00096       if (&__x != this)
00097     {
00098       if (__len >= __x.size())
00099         _M_erase_at_end(std::copy(__x.begin(), __x.end(),
00100                       this->_M_impl._M_start));
00101       else
00102         {
00103           const_iterator __mid = __x.begin() + difference_type(__len);
00104           std::copy(__x.begin(), __mid, this->_M_impl._M_start);
00105           insert(this->_M_impl._M_finish, __mid, __x.end());
00106         }
00107     }
00108       return *this;
00109     }
00110 
00111 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00112   template<typename _Tp, typename _Alloc>
00113     template<typename... _Args>
00114       void
00115       deque<_Tp, _Alloc>::
00116       emplace_front(_Args&&... __args)
00117       {
00118     if (this->_M_impl._M_start._M_cur != this->_M_impl._M_start._M_first)
00119       {
00120         this->_M_impl.construct(this->_M_impl._M_start._M_cur - 1,
00121                     std::forward<_Args>(__args)...);
00122         --this->_M_impl._M_start._M_cur;
00123       }
00124     else
00125       _M_push_front_aux(std::forward<_Args>(__args)...);
00126       }
00127 
00128   template<typename _Tp, typename _Alloc>
00129     template<typename... _Args>
00130       void
00131       deque<_Tp, _Alloc>::
00132       emplace_back(_Args&&... __args)
00133       {
00134     if (this->_M_impl._M_finish._M_cur
00135         != this->_M_impl._M_finish._M_last - 1)
00136       {
00137         this->_M_impl.construct(this->_M_impl._M_finish._M_cur,
00138                     std::forward<_Args>(__args)...);
00139         ++this->_M_impl._M_finish._M_cur;
00140       }
00141     else
00142       _M_push_back_aux(std::forward<_Args>(__args)...);
00143       }
00144 #endif
00145 
00146   template <typename _Tp, typename _Alloc>
00147     typename deque<_Tp, _Alloc>::iterator
00148     deque<_Tp, _Alloc>::
00149     insert(iterator __position, const value_type& __x)
00150     {
00151       if (__position._M_cur == this->_M_impl._M_start._M_cur)
00152     {
00153       push_front(__x);
00154       return this->_M_impl._M_start;
00155     }
00156       else if (__position._M_cur == this->_M_impl._M_finish._M_cur)
00157     {
00158       push_back(__x);
00159       iterator __tmp = this->_M_impl._M_finish;
00160       --__tmp;
00161       return __tmp;
00162     }
00163       else
00164         return _M_insert_aux(__position, __x);
00165     }
00166 
00167 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00168   template<typename _Tp, typename _Alloc>
00169     template<typename... _Args>
00170       typename deque<_Tp, _Alloc>::iterator
00171       deque<_Tp, _Alloc>::
00172       emplace(iterator __position, _Args&&... __args)
00173       {
00174     if (__position._M_cur == this->_M_impl._M_start._M_cur)
00175       {
00176         push_front(std::forward<_Args>(__args)...);
00177         return this->_M_impl._M_start;
00178       }
00179     else if (__position._M_cur == this->_M_impl._M_finish._M_cur)
00180       {
00181         push_back(std::forward<_Args>(__args)...);
00182         iterator __tmp = this->_M_impl._M_finish;
00183         --__tmp;
00184         return __tmp;
00185       }
00186     else
00187       return _M_insert_aux(__position, std::forward<_Args>(__args)...);
00188       }
00189 #endif
00190 
00191   template <typename _Tp, typename _Alloc>
00192     typename deque<_Tp, _Alloc>::iterator
00193     deque<_Tp, _Alloc>::
00194     erase(iterator __position)
00195     {
00196       iterator __next = __position;
00197       ++__next;
00198       const difference_type __index = __position - begin();
00199       if (static_cast<size_type>(__index) < (size() >> 1))
00200     {
00201       if (__position != begin())
00202         _GLIBCXX_MOVE_BACKWARD3(begin(), __position, __next);
00203       pop_front();
00204     }
00205       else
00206     {
00207       if (__next != end())
00208         _GLIBCXX_MOVE3(__next, end(), __position);
00209       pop_back();
00210     }
00211       return begin() + __index;
00212     }
00213 
00214   template <typename _Tp, typename _Alloc>
00215     typename deque<_Tp, _Alloc>::iterator
00216     deque<_Tp, _Alloc>::
00217     erase(iterator __first, iterator __last)
00218     {
00219       if (__first == begin() && __last == end())
00220     {
00221       clear();
00222       return end();
00223     }
00224       else
00225     {
00226       const difference_type __n = __last - __first;
00227       const difference_type __elems_before = __first - begin();
00228       if (static_cast<size_type>(__elems_before) <= (size() - __n) / 2)
00229         {
00230           if (__first != begin())
00231         _GLIBCXX_MOVE_BACKWARD3(begin(), __first, __last);
00232           _M_erase_at_begin(begin() + __n);
00233         }
00234       else
00235         {
00236           if (__last != end())
00237         _GLIBCXX_MOVE3(__last, end(), __first);
00238           _M_erase_at_end(end() - __n);
00239         }
00240       return begin() + __elems_before;
00241     }
00242     }
00243 
00244   template <typename _Tp, class _Alloc>
00245     template <typename _InputIterator>
00246       void
00247       deque<_Tp, _Alloc>::
00248       _M_assign_aux(_InputIterator __first, _InputIterator __last,
00249             std::input_iterator_tag)
00250       {
00251         iterator __cur = begin();
00252         for (; __first != __last && __cur != end(); ++__cur, ++__first)
00253           *__cur = *__first;
00254         if (__first == __last)
00255           _M_erase_at_end(__cur);
00256         else
00257           insert(end(), __first, __last);
00258       }
00259 
00260   template <typename _Tp, typename _Alloc>
00261     void
00262     deque<_Tp, _Alloc>::
00263     _M_fill_insert(iterator __pos, size_type __n, const value_type& __x)
00264     {
00265       if (__pos._M_cur == this->_M_impl._M_start._M_cur)
00266     {
00267       iterator __new_start = _M_reserve_elements_at_front(__n);
00268       __try
00269         {
00270           std::__uninitialized_fill_a(__new_start, this->_M_impl._M_start,
00271                       __x, _M_get_Tp_allocator());
00272           this->_M_impl._M_start = __new_start;
00273         }
00274       __catch(...)
00275         {
00276           _M_destroy_nodes(__new_start._M_node,
00277                    this->_M_impl._M_start._M_node);
00278           __throw_exception_again;
00279         }
00280     }
00281       else if (__pos._M_cur == this->_M_impl._M_finish._M_cur)
00282     {
00283       iterator __new_finish = _M_reserve_elements_at_back(__n);
00284       __try
00285         {
00286           std::__uninitialized_fill_a(this->_M_impl._M_finish,
00287                       __new_finish, __x,
00288                       _M_get_Tp_allocator());
00289           this->_M_impl._M_finish = __new_finish;
00290         }
00291       __catch(...)
00292         {
00293           _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
00294                    __new_finish._M_node + 1);
00295           __throw_exception_again;
00296         }
00297     }
00298       else
00299         _M_insert_aux(__pos, __n, __x);
00300     }
00301 
00302 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00303   template <typename _Tp, typename _Alloc>
00304     void
00305     deque<_Tp, _Alloc>::
00306     _M_default_append(size_type __n)
00307     {
00308       if (__n)
00309     {
00310       iterator __new_finish = _M_reserve_elements_at_back(__n);
00311       __try
00312         {
00313           std::__uninitialized_default_a(this->_M_impl._M_finish,
00314                          __new_finish,
00315                          _M_get_Tp_allocator());
00316           this->_M_impl._M_finish = __new_finish;
00317         }
00318       __catch(...)
00319         {
00320           _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
00321                    __new_finish._M_node + 1);
00322           __throw_exception_again;
00323         }
00324     }
00325     }
00326 #endif
00327 
00328   template <typename _Tp, typename _Alloc>
00329     void
00330     deque<_Tp, _Alloc>::
00331     _M_fill_initialize(const value_type& __value)
00332     {
00333       _Map_pointer __cur;
00334       __try
00335         {
00336           for (__cur = this->_M_impl._M_start._M_node;
00337            __cur < this->_M_impl._M_finish._M_node;
00338            ++__cur)
00339             std::__uninitialized_fill_a(*__cur, *__cur + _S_buffer_size(),
00340                     __value, _M_get_Tp_allocator());
00341           std::__uninitialized_fill_a(this->_M_impl._M_finish._M_first,
00342                       this->_M_impl._M_finish._M_cur,
00343                       __value, _M_get_Tp_allocator());
00344         }
00345       __catch(...)
00346         {
00347           std::_Destroy(this->_M_impl._M_start, iterator(*__cur, __cur),
00348             _M_get_Tp_allocator());
00349           __throw_exception_again;
00350         }
00351     }
00352 
00353   template <typename _Tp, typename _Alloc>
00354     template <typename _InputIterator>
00355       void
00356       deque<_Tp, _Alloc>::
00357       _M_range_initialize(_InputIterator __first, _InputIterator __last,
00358                           std::input_iterator_tag)
00359       {
00360         this->_M_initialize_map(0);
00361         __try
00362           {
00363             for (; __first != __last; ++__first)
00364               push_back(*__first);
00365           }
00366         __catch(...)
00367           {
00368             clear();
00369             __throw_exception_again;
00370           }
00371       }
00372 
00373   template <typename _Tp, typename _Alloc>
00374     template <typename _ForwardIterator>
00375       void
00376       deque<_Tp, _Alloc>::
00377       _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
00378                           std::forward_iterator_tag)
00379       {
00380         const size_type __n = std::distance(__first, __last);
00381         this->_M_initialize_map(__n);
00382 
00383         _Map_pointer __cur_node;
00384         __try
00385           {
00386             for (__cur_node = this->_M_impl._M_start._M_node;
00387                  __cur_node < this->_M_impl._M_finish._M_node;
00388                  ++__cur_node)
00389           {
00390         _ForwardIterator __mid = __first;
00391         std::advance(__mid, _S_buffer_size());
00392         std::__uninitialized_copy_a(__first, __mid, *__cur_node,
00393                         _M_get_Tp_allocator());
00394         __first = __mid;
00395           }
00396             std::__uninitialized_copy_a(__first, __last,
00397                     this->_M_impl._M_finish._M_first,
00398                     _M_get_Tp_allocator());
00399           }
00400         __catch(...)
00401           {
00402             std::_Destroy(this->_M_impl._M_start,
00403               iterator(*__cur_node, __cur_node),
00404               _M_get_Tp_allocator());
00405             __throw_exception_again;
00406           }
00407       }
00408 
00409   // Called only if _M_impl._M_finish._M_cur == _M_impl._M_finish._M_last - 1.
00410   template<typename _Tp, typename _Alloc>
00411 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00412     template<typename... _Args>
00413       void
00414       deque<_Tp, _Alloc>::
00415       _M_push_back_aux(_Args&&... __args)
00416 #else
00417       void
00418       deque<_Tp, _Alloc>::
00419       _M_push_back_aux(const value_type& __t)
00420 #endif
00421       {
00422     _M_reserve_map_at_back();
00423     *(this->_M_impl._M_finish._M_node + 1) = this->_M_allocate_node();
00424     __try
00425       {
00426 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00427         this->_M_impl.construct(this->_M_impl._M_finish._M_cur,
00428                     std::forward<_Args>(__args)...);
00429 #else
00430         this->_M_impl.construct(this->_M_impl._M_finish._M_cur, __t);
00431 #endif
00432         this->_M_impl._M_finish._M_set_node(this->_M_impl._M_finish._M_node
00433                         + 1);
00434         this->_M_impl._M_finish._M_cur = this->_M_impl._M_finish._M_first;
00435       }
00436     __catch(...)
00437       {
00438         _M_deallocate_node(*(this->_M_impl._M_finish._M_node + 1));
00439         __throw_exception_again;
00440       }
00441       }
00442 
00443   // Called only if _M_impl._M_start._M_cur == _M_impl._M_start._M_first.
00444   template<typename _Tp, typename _Alloc>
00445 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00446     template<typename... _Args>
00447       void
00448       deque<_Tp, _Alloc>::
00449       _M_push_front_aux(_Args&&... __args)
00450 #else
00451       void
00452       deque<_Tp, _Alloc>::
00453       _M_push_front_aux(const value_type& __t)
00454 #endif
00455       {
00456     _M_reserve_map_at_front();
00457     *(this->_M_impl._M_start._M_node - 1) = this->_M_allocate_node();
00458     __try
00459       {
00460         this->_M_impl._M_start._M_set_node(this->_M_impl._M_start._M_node
00461                            - 1);
00462         this->_M_impl._M_start._M_cur = this->_M_impl._M_start._M_last - 1;
00463 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00464         this->_M_impl.construct(this->_M_impl._M_start._M_cur,
00465                     std::forward<_Args>(__args)...);
00466 #else
00467         this->_M_impl.construct(this->_M_impl._M_start._M_cur, __t);
00468 #endif
00469       }
00470     __catch(...)
00471       {
00472         ++this->_M_impl._M_start;
00473         _M_deallocate_node(*(this->_M_impl._M_start._M_node - 1));
00474         __throw_exception_again;
00475       }
00476       }
00477 
00478   // Called only if _M_impl._M_finish._M_cur == _M_impl._M_finish._M_first.
00479   template <typename _Tp, typename _Alloc>
00480     void deque<_Tp, _Alloc>::
00481     _M_pop_back_aux()
00482     {
00483       _M_deallocate_node(this->_M_impl._M_finish._M_first);
00484       this->_M_impl._M_finish._M_set_node(this->_M_impl._M_finish._M_node - 1);
00485       this->_M_impl._M_finish._M_cur = this->_M_impl._M_finish._M_last - 1;
00486       this->_M_impl.destroy(this->_M_impl._M_finish._M_cur);
00487     }
00488 
00489   // Called only if _M_impl._M_start._M_cur == _M_impl._M_start._M_last - 1.
00490   // Note that if the deque has at least one element (a precondition for this
00491   // member function), and if
00492   //   _M_impl._M_start._M_cur == _M_impl._M_start._M_last,
00493   // then the deque must have at least two nodes.
00494   template <typename _Tp, typename _Alloc>
00495     void deque<_Tp, _Alloc>::
00496     _M_pop_front_aux()
00497     {
00498       this->_M_impl.destroy(this->_M_impl._M_start._M_cur);
00499       _M_deallocate_node(this->_M_impl._M_start._M_first);
00500       this->_M_impl._M_start._M_set_node(this->_M_impl._M_start._M_node + 1);
00501       this->_M_impl._M_start._M_cur = this->_M_impl._M_start._M_first;
00502     }
00503 
00504   template <typename _Tp, typename _Alloc>
00505     template <typename _InputIterator>
00506       void
00507       deque<_Tp, _Alloc>::
00508       _M_range_insert_aux(iterator __pos,
00509                           _InputIterator __first, _InputIterator __last,
00510                           std::input_iterator_tag)
00511       { std::copy(__first, __last, std::inserter(*this, __pos)); }
00512 
00513   template <typename _Tp, typename _Alloc>
00514     template <typename _ForwardIterator>
00515       void
00516       deque<_Tp, _Alloc>::
00517       _M_range_insert_aux(iterator __pos,
00518                           _ForwardIterator __first, _ForwardIterator __last,
00519                           std::forward_iterator_tag)
00520       {
00521         const size_type __n = std::distance(__first, __last);
00522         if (__pos._M_cur == this->_M_impl._M_start._M_cur)
00523       {
00524         iterator __new_start = _M_reserve_elements_at_front(__n);
00525         __try
00526           {
00527         std::__uninitialized_copy_a(__first, __last, __new_start,
00528                         _M_get_Tp_allocator());
00529         this->_M_impl._M_start = __new_start;
00530           }
00531         __catch(...)
00532           {
00533         _M_destroy_nodes(__new_start._M_node,
00534                  this->_M_impl._M_start._M_node);
00535         __throw_exception_again;
00536           }
00537       }
00538         else if (__pos._M_cur == this->_M_impl._M_finish._M_cur)
00539       {
00540         iterator __new_finish = _M_reserve_elements_at_back(__n);
00541         __try
00542           {
00543         std::__uninitialized_copy_a(__first, __last,
00544                         this->_M_impl._M_finish,
00545                         _M_get_Tp_allocator());
00546         this->_M_impl._M_finish = __new_finish;
00547           }
00548         __catch(...)
00549           {
00550         _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
00551                  __new_finish._M_node + 1);
00552         __throw_exception_again;
00553           }
00554       }
00555         else
00556           _M_insert_aux(__pos, __first, __last, __n);
00557       }
00558 
00559   template<typename _Tp, typename _Alloc>
00560 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00561     template<typename... _Args>
00562       typename deque<_Tp, _Alloc>::iterator
00563       deque<_Tp, _Alloc>::
00564       _M_insert_aux(iterator __pos, _Args&&... __args)
00565       {
00566     value_type __x_copy(std::forward<_Args>(__args)...); // XXX copy
00567 #else
00568     typename deque<_Tp, _Alloc>::iterator
00569       deque<_Tp, _Alloc>::
00570       _M_insert_aux(iterator __pos, const value_type& __x)
00571       {
00572     value_type __x_copy = __x; // XXX copy
00573 #endif
00574     difference_type __index = __pos - this->_M_impl._M_start;
00575     if (static_cast<size_type>(__index) < size() / 2)
00576       {
00577         push_front(_GLIBCXX_MOVE(front()));
00578         iterator __front1 = this->_M_impl._M_start;
00579         ++__front1;
00580         iterator __front2 = __front1;
00581         ++__front2;
00582         __pos = this->_M_impl._M_start + __index;
00583         iterator __pos1 = __pos;
00584         ++__pos1;
00585         _GLIBCXX_MOVE3(__front2, __pos1, __front1);
00586       }
00587     else
00588       {
00589         push_back(_GLIBCXX_MOVE(back()));
00590         iterator __back1 = this->_M_impl._M_finish;
00591         --__back1;
00592         iterator __back2 = __back1;
00593         --__back2;
00594         __pos = this->_M_impl._M_start + __index;
00595         _GLIBCXX_MOVE_BACKWARD3(__pos, __back2, __back1);
00596       }
00597     *__pos = _GLIBCXX_MOVE(__x_copy);
00598     return __pos;
00599       }
00600 
00601   template <typename _Tp, typename _Alloc>
00602     void
00603     deque<_Tp, _Alloc>::
00604     _M_insert_aux(iterator __pos, size_type __n, const value_type& __x)
00605     {
00606       const difference_type __elems_before = __pos - this->_M_impl._M_start;
00607       const size_type __length = this->size();
00608       value_type __x_copy = __x;
00609       if (__elems_before < difference_type(__length / 2))
00610     {
00611       iterator __new_start = _M_reserve_elements_at_front(__n);
00612       iterator __old_start = this->_M_impl._M_start;
00613       __pos = this->_M_impl._M_start + __elems_before;
00614       __try
00615         {
00616           if (__elems_before >= difference_type(__n))
00617         {
00618           iterator __start_n = (this->_M_impl._M_start
00619                     + difference_type(__n));
00620           std::__uninitialized_move_a(this->_M_impl._M_start,
00621                           __start_n, __new_start,
00622                           _M_get_Tp_allocator());
00623           this->_M_impl._M_start = __new_start;
00624           _GLIBCXX_MOVE3(__start_n, __pos, __old_start);
00625           std::fill(__pos - difference_type(__n), __pos, __x_copy);
00626         }
00627           else
00628         {
00629           std::__uninitialized_move_fill(this->_M_impl._M_start,
00630                          __pos, __new_start,
00631                          this->_M_impl._M_start,
00632                          __x_copy,
00633                          _M_get_Tp_allocator());
00634           this->_M_impl._M_start = __new_start;
00635           std::fill(__old_start, __pos, __x_copy);
00636         }
00637         }
00638       __catch(...)
00639         {
00640           _M_destroy_nodes(__new_start._M_node,
00641                    this->_M_impl._M_start._M_node);
00642           __throw_exception_again;
00643         }
00644     }
00645       else
00646     {
00647       iterator __new_finish = _M_reserve_elements_at_back(__n);
00648       iterator __old_finish = this->_M_impl._M_finish;
00649       const difference_type __elems_after =
00650         difference_type(__length) - __elems_before;
00651       __pos = this->_M_impl._M_finish - __elems_after;
00652       __try
00653         {
00654           if (__elems_after > difference_type(__n))
00655         {
00656           iterator __finish_n = (this->_M_impl._M_finish
00657                      - difference_type(__n));
00658           std::__uninitialized_move_a(__finish_n,
00659                           this->_M_impl._M_finish,
00660                           this->_M_impl._M_finish,
00661                           _M_get_Tp_allocator());
00662           this->_M_impl._M_finish = __new_finish;
00663           _GLIBCXX_MOVE_BACKWARD3(__pos, __finish_n, __old_finish);
00664           std::fill(__pos, __pos + difference_type(__n), __x_copy);
00665         }
00666           else
00667         {
00668           std::__uninitialized_fill_move(this->_M_impl._M_finish,
00669                          __pos + difference_type(__n),
00670                          __x_copy, __pos,
00671                          this->_M_impl._M_finish,
00672                          _M_get_Tp_allocator());
00673           this->_M_impl._M_finish = __new_finish;
00674           std::fill(__pos, __old_finish, __x_copy);
00675         }
00676         }
00677       __catch(...)
00678         {
00679           _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
00680                    __new_finish._M_node + 1);
00681           __throw_exception_again;
00682         }
00683     }
00684     }
00685 
00686   template <typename _Tp, typename _Alloc>
00687     template <typename _ForwardIterator>
00688       void
00689       deque<_Tp, _Alloc>::
00690       _M_insert_aux(iterator __pos,
00691                     _ForwardIterator __first, _ForwardIterator __last,
00692                     size_type __n)
00693       {
00694         const difference_type __elemsbefore = __pos - this->_M_impl._M_start;
00695         const size_type __length = size();
00696         if (static_cast<size_type>(__elemsbefore) < __length / 2)
00697       {
00698         iterator __new_start = _M_reserve_elements_at_front(__n);
00699         iterator __old_start = this->_M_impl._M_start;
00700         __pos = this->_M_impl._M_start + __elemsbefore;
00701         __try
00702           {
00703         if (__elemsbefore >= difference_type(__n))
00704           {
00705             iterator __start_n = (this->_M_impl._M_start
00706                       + difference_type(__n));
00707             std::__uninitialized_move_a(this->_M_impl._M_start,
00708                         __start_n, __new_start,
00709                         _M_get_Tp_allocator());
00710             this->_M_impl._M_start = __new_start;
00711             _GLIBCXX_MOVE3(__start_n, __pos, __old_start);
00712             std::copy(__first, __last, __pos - difference_type(__n));
00713           }
00714         else
00715           {
00716             _ForwardIterator __mid = __first;
00717             std::advance(__mid, difference_type(__n) - __elemsbefore);
00718             std::__uninitialized_move_copy(this->_M_impl._M_start,
00719                            __pos, __first, __mid,
00720                            __new_start,
00721                            _M_get_Tp_allocator());
00722             this->_M_impl._M_start = __new_start;
00723             std::copy(__mid, __last, __old_start);
00724           }
00725           }
00726         __catch(...)
00727           {
00728         _M_destroy_nodes(__new_start._M_node,
00729                  this->_M_impl._M_start._M_node);
00730         __throw_exception_again;
00731           }
00732       }
00733         else
00734         {
00735           iterator __new_finish = _M_reserve_elements_at_back(__n);
00736           iterator __old_finish = this->_M_impl._M_finish;
00737           const difference_type __elemsafter =
00738             difference_type(__length) - __elemsbefore;
00739           __pos = this->_M_impl._M_finish - __elemsafter;
00740           __try
00741             {
00742               if (__elemsafter > difference_type(__n))
00743         {
00744           iterator __finish_n = (this->_M_impl._M_finish
00745                      - difference_type(__n));
00746           std::__uninitialized_move_a(__finish_n,
00747                           this->_M_impl._M_finish,
00748                           this->_M_impl._M_finish,
00749                           _M_get_Tp_allocator());
00750           this->_M_impl._M_finish = __new_finish;
00751           _GLIBCXX_MOVE_BACKWARD3(__pos, __finish_n, __old_finish);
00752           std::copy(__first, __last, __pos);
00753         }
00754               else
00755         {
00756           _ForwardIterator __mid = __first;
00757           std::advance(__mid, __elemsafter);
00758           std::__uninitialized_copy_move(__mid, __last, __pos,
00759                          this->_M_impl._M_finish,
00760                          this->_M_impl._M_finish,
00761                          _M_get_Tp_allocator());
00762           this->_M_impl._M_finish = __new_finish;
00763           std::copy(__first, __mid, __pos);
00764         }
00765             }
00766           __catch(...)
00767             {
00768               _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
00769                    __new_finish._M_node + 1);
00770               __throw_exception_again;
00771             }
00772         }
00773       }
00774 
00775    template<typename _Tp, typename _Alloc>
00776      void
00777      deque<_Tp, _Alloc>::
00778      _M_destroy_data_aux(iterator __first, iterator __last)
00779      {
00780        for (_Map_pointer __node = __first._M_node + 1;
00781         __node < __last._M_node; ++__node)
00782      std::_Destroy(*__node, *__node + _S_buffer_size(),
00783                _M_get_Tp_allocator());
00784 
00785        if (__first._M_node != __last._M_node)
00786      {
00787        std::_Destroy(__first._M_cur, __first._M_last,
00788              _M_get_Tp_allocator());
00789        std::_Destroy(__last._M_first, __last._M_cur,
00790              _M_get_Tp_allocator());
00791      }
00792        else
00793      std::_Destroy(__first._M_cur, __last._M_cur,
00794                _M_get_Tp_allocator());
00795      }
00796 
00797   template <typename _Tp, typename _Alloc>
00798     void
00799     deque<_Tp, _Alloc>::
00800     _M_new_elements_at_front(size_type __new_elems)
00801     {
00802       if (this->max_size() - this->size() < __new_elems)
00803     __throw_length_error(__N("deque::_M_new_elements_at_front"));
00804 
00805       const size_type __new_nodes = ((__new_elems + _S_buffer_size() - 1)
00806                      / _S_buffer_size());
00807       _M_reserve_map_at_front(__new_nodes);
00808       size_type __i;
00809       __try
00810         {
00811           for (__i = 1; __i <= __new_nodes; ++__i)
00812             *(this->_M_impl._M_start._M_node - __i) = this->_M_allocate_node();
00813         }
00814       __catch(...)
00815         {
00816           for (size_type __j = 1; __j < __i; ++__j)
00817             _M_deallocate_node(*(this->_M_impl._M_start._M_node - __j));
00818           __throw_exception_again;
00819         }
00820     }
00821 
00822   template <typename _Tp, typename _Alloc>
00823     void
00824     deque<_Tp, _Alloc>::
00825     _M_new_elements_at_back(size_type __new_elems)
00826     {
00827       if (this->max_size() - this->size() < __new_elems)
00828     __throw_length_error(__N("deque::_M_new_elements_at_back"));
00829 
00830       const size_type __new_nodes = ((__new_elems + _S_buffer_size() - 1)
00831                      / _S_buffer_size());
00832       _M_reserve_map_at_back(__new_nodes);
00833       size_type __i;
00834       __try
00835         {
00836           for (__i = 1; __i <= __new_nodes; ++__i)
00837             *(this->_M_impl._M_finish._M_node + __i) = this->_M_allocate_node();
00838         }
00839       __catch(...)
00840         {
00841           for (size_type __j = 1; __j < __i; ++__j)
00842             _M_deallocate_node(*(this->_M_impl._M_finish._M_node + __j));
00843           __throw_exception_again;
00844         }
00845     }
00846 
00847   template <typename _Tp, typename _Alloc>
00848     void
00849     deque<_Tp, _Alloc>::
00850     _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front)
00851     {
00852       const size_type __old_num_nodes
00853     = this->_M_impl._M_finish._M_node - this->_M_impl._M_start._M_node + 1;
00854       const size_type __new_num_nodes = __old_num_nodes + __nodes_to_add;
00855 
00856       _Map_pointer __new_nstart;
00857       if (this->_M_impl._M_map_size > 2 * __new_num_nodes)
00858     {
00859       __new_nstart = this->_M_impl._M_map + (this->_M_impl._M_map_size
00860                      - __new_num_nodes) / 2
00861                      + (__add_at_front ? __nodes_to_add : 0);
00862       if (__new_nstart < this->_M_impl._M_start._M_node)
00863         std::copy(this->_M_impl._M_start._M_node,
00864               this->_M_impl._M_finish._M_node + 1,
00865               __new_nstart);
00866       else
00867         std::copy_backward(this->_M_impl._M_start._M_node,
00868                    this->_M_impl._M_finish._M_node + 1,
00869                    __new_nstart + __old_num_nodes);
00870     }
00871       else
00872     {
00873       size_type __new_map_size = this->_M_impl._M_map_size
00874                                  + std::max(this->_M_impl._M_map_size,
00875                         __nodes_to_add) + 2;
00876 
00877       _Map_pointer __new_map = this->_M_allocate_map(__new_map_size);
00878       __new_nstart = __new_map + (__new_map_size - __new_num_nodes) / 2
00879                      + (__add_at_front ? __nodes_to_add : 0);
00880       std::copy(this->_M_impl._M_start._M_node,
00881             this->_M_impl._M_finish._M_node + 1,
00882             __new_nstart);
00883       _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
00884 
00885       this->_M_impl._M_map = __new_map;
00886       this->_M_impl._M_map_size = __new_map_size;
00887     }
00888 
00889       this->_M_impl._M_start._M_set_node(__new_nstart);
00890       this->_M_impl._M_finish._M_set_node(__new_nstart + __old_num_nodes - 1);
00891     }
00892 
00893   // Overload for deque::iterators, exploiting the "segmented-iterator
00894   // optimization".
00895   template<typename _Tp>
00896     void
00897     fill(const _Deque_iterator<_Tp, _Tp&, _Tp*>& __first,
00898      const _Deque_iterator<_Tp, _Tp&, _Tp*>& __last, const _Tp& __value)
00899     {
00900       typedef typename _Deque_iterator<_Tp, _Tp&, _Tp*>::_Self _Self;
00901 
00902       for (typename _Self::_Map_pointer __node = __first._M_node + 1;
00903            __node < __last._M_node; ++__node)
00904     std::fill(*__node, *__node + _Self::_S_buffer_size(), __value);
00905 
00906       if (__first._M_node != __last._M_node)
00907     {
00908       std::fill(__first._M_cur, __first._M_last, __value);
00909       std::fill(__last._M_first, __last._M_cur, __value);
00910     }
00911       else
00912     std::fill(__first._M_cur, __last._M_cur, __value);
00913     }
00914 
00915   template<typename _Tp>
00916     _Deque_iterator<_Tp, _Tp&, _Tp*>
00917     copy(_Deque_iterator<_Tp, const _Tp&, const _Tp*> __first,
00918      _Deque_iterator<_Tp, const _Tp&, const _Tp*> __last,
00919      _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
00920     {
00921       typedef typename _Deque_iterator<_Tp, _Tp&, _Tp*>::_Self _Self;
00922       typedef typename _Self::difference_type difference_type;
00923 
00924       difference_type __len = __last - __first;
00925       while (__len > 0)
00926     {
00927       const difference_type __clen
00928         = std::min(__len, std::min(__first._M_last - __first._M_cur,
00929                        __result._M_last - __result._M_cur));
00930       std::copy(__first._M_cur, __first._M_cur + __clen, __result._M_cur);
00931       __first += __clen;
00932       __result += __clen;
00933       __len -= __clen;
00934     }
00935       return __result;
00936     }
00937 
00938   template<typename _Tp>
00939     _Deque_iterator<_Tp, _Tp&, _Tp*>
00940     copy_backward(_Deque_iterator<_Tp, const _Tp&, const _Tp*> __first,
00941           _Deque_iterator<_Tp, const _Tp&, const _Tp*> __last,
00942           _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
00943     {
00944       typedef typename _Deque_iterator<_Tp, _Tp&, _Tp*>::_Self _Self;
00945       typedef typename _Self::difference_type difference_type;
00946 
00947       difference_type __len = __last - __first;
00948       while (__len > 0)
00949     {
00950       difference_type __llen = __last._M_cur - __last._M_first;
00951       _Tp* __lend = __last._M_cur;
00952 
00953       difference_type __rlen = __result._M_cur - __result._M_first;
00954       _Tp* __rend = __result._M_cur;
00955 
00956       if (!__llen)
00957         {
00958           __llen = _Self::_S_buffer_size();
00959           __lend = *(__last._M_node - 1) + __llen;
00960         }
00961       if (!__rlen)
00962         {
00963           __rlen = _Self::_S_buffer_size();
00964           __rend = *(__result._M_node - 1) + __rlen;
00965         }
00966 
00967       const difference_type __clen = std::min(__len,
00968                           std::min(__llen, __rlen));
00969       std::copy_backward(__lend - __clen, __lend, __rend);
00970       __last -= __clen;
00971       __result -= __clen;
00972       __len -= __clen;
00973     }
00974       return __result;
00975     }
00976 
00977 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00978   template<typename _Tp>
00979     _Deque_iterator<_Tp, _Tp&, _Tp*>
00980     move(_Deque_iterator<_Tp, const _Tp&, const _Tp*> __first,
00981      _Deque_iterator<_Tp, const _Tp&, const _Tp*> __last,
00982      _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
00983     {
00984       typedef typename _Deque_iterator<_Tp, _Tp&, _Tp*>::_Self _Self;
00985       typedef typename _Self::difference_type difference_type;
00986 
00987       difference_type __len = __last - __first;
00988       while (__len > 0)
00989     {
00990       const difference_type __clen
00991         = std::min(__len, std::min(__first._M_last - __first._M_cur,
00992                        __result._M_last - __result._M_cur));
00993       std::move(__first._M_cur, __first._M_cur + __clen, __result._M_cur);
00994       __first += __clen;
00995       __result += __clen;
00996       __len -= __clen;
00997     }
00998       return __result;
00999     }
01000 
01001   template<typename _Tp>
01002     _Deque_iterator<_Tp, _Tp&, _Tp*>
01003     move_backward(_Deque_iterator<_Tp, const _Tp&, const _Tp*> __first,
01004           _Deque_iterator<_Tp, const _Tp&, const _Tp*> __last,
01005           _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
01006     {
01007       typedef typename _Deque_iterator<_Tp, _Tp&, _Tp*>::_Self _Self;
01008       typedef typename _Self::difference_type difference_type;
01009 
01010       difference_type __len = __last - __first;
01011       while (__len > 0)
01012     {
01013       difference_type __llen = __last._M_cur - __last._M_first;
01014       _Tp* __lend = __last._M_cur;
01015 
01016       difference_type __rlen = __result._M_cur - __result._M_first;
01017       _Tp* __rend = __result._M_cur;
01018 
01019       if (!__llen)
01020         {
01021           __llen = _Self::_S_buffer_size();
01022           __lend = *(__last._M_node - 1) + __llen;
01023         }
01024       if (!__rlen)
01025         {
01026           __rlen = _Self::_S_buffer_size();
01027           __rend = *(__result._M_node - 1) + __rlen;
01028         }
01029 
01030       const difference_type __clen = std::min(__len,
01031                           std::min(__llen, __rlen));
01032       std::move_backward(__lend - __clen, __lend, __rend);
01033       __last -= __clen;
01034       __result -= __clen;
01035       __len -= __clen;
01036     }
01037       return __result;
01038     }
01039 #endif
01040 
01041 _GLIBCXX_END_NESTED_NAMESPACE
01042 
01043 #endif