xref: /openbsd-src/gnu/gcc/libstdc++-v3/include/bits/vector.tcc (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1*404b540aSrobert // Vector implementation (out of line) -*- C++ -*-
2*404b540aSrobert 
3*404b540aSrobert // Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4*404b540aSrobert //
5*404b540aSrobert // This file is part of the GNU ISO C++ Library.  This library is free
6*404b540aSrobert // software; you can redistribute it and/or modify it under the
7*404b540aSrobert // terms of the GNU General Public License as published by the
8*404b540aSrobert // Free Software Foundation; either version 2, or (at your option)
9*404b540aSrobert // any later version.
10*404b540aSrobert 
11*404b540aSrobert // This library is distributed in the hope that it will be useful,
12*404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*404b540aSrobert // GNU General Public License for more details.
15*404b540aSrobert 
16*404b540aSrobert // You should have received a copy of the GNU General Public License along
17*404b540aSrobert // with this library; see the file COPYING.  If not, write to the Free
18*404b540aSrobert // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19*404b540aSrobert // USA.
20*404b540aSrobert 
21*404b540aSrobert // As a special exception, you may use this file as part of a free software
22*404b540aSrobert // library without restriction.  Specifically, if other files instantiate
23*404b540aSrobert // templates or use macros or inline functions from this file, or you compile
24*404b540aSrobert // this file and link it with other files to produce an executable, this
25*404b540aSrobert // file does not by itself cause the resulting executable to be covered by
26*404b540aSrobert // the GNU General Public License.  This exception does not however
27*404b540aSrobert // invalidate any other reasons why the executable file might be covered by
28*404b540aSrobert // the GNU General Public License.
29*404b540aSrobert 
30*404b540aSrobert /*
31*404b540aSrobert  *
32*404b540aSrobert  * Copyright (c) 1994
33*404b540aSrobert  * Hewlett-Packard Company
34*404b540aSrobert  *
35*404b540aSrobert  * Permission to use, copy, modify, distribute and sell this software
36*404b540aSrobert  * and its documentation for any purpose is hereby granted without fee,
37*404b540aSrobert  * provided that the above copyright notice appear in all copies and
38*404b540aSrobert  * that both that copyright notice and this permission notice appear
39*404b540aSrobert  * in supporting documentation.  Hewlett-Packard Company makes no
40*404b540aSrobert  * representations about the suitability of this software for any
41*404b540aSrobert  * purpose.  It is provided "as is" without express or implied warranty.
42*404b540aSrobert  *
43*404b540aSrobert  *
44*404b540aSrobert  * Copyright (c) 1996
45*404b540aSrobert  * Silicon Graphics Computer Systems, Inc.
46*404b540aSrobert  *
47*404b540aSrobert  * Permission to use, copy, modify, distribute and sell this software
48*404b540aSrobert  * and its documentation for any purpose is hereby granted without fee,
49*404b540aSrobert  * provided that the above copyright notice appear in all copies and
50*404b540aSrobert  * that both that copyright notice and this permission notice appear
51*404b540aSrobert  * in supporting documentation.  Silicon Graphics makes no
52*404b540aSrobert  * representations about the suitability of this  software for any
53*404b540aSrobert  * purpose.  It is provided "as is" without express or implied warranty.
54*404b540aSrobert  */
55*404b540aSrobert 
56*404b540aSrobert /** @file vector.tcc
57*404b540aSrobert  *  This is an internal header file, included by other library headers.
58*404b540aSrobert  *  You should not attempt to use it directly.
59*404b540aSrobert  */
60*404b540aSrobert 
61*404b540aSrobert #ifndef _VECTOR_TCC
62*404b540aSrobert #define _VECTOR_TCC 1
63*404b540aSrobert 
_GLIBCXX_BEGIN_NESTED_NAMESPACE(std,_GLIBCXX_STD)64*404b540aSrobert _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD)
65*404b540aSrobert 
66*404b540aSrobert   template<typename _Tp, typename _Alloc>
67*404b540aSrobert     void
68*404b540aSrobert     vector<_Tp, _Alloc>::
69*404b540aSrobert     reserve(size_type __n)
70*404b540aSrobert     {
71*404b540aSrobert       if (__n > this->max_size())
72*404b540aSrobert 	__throw_length_error(__N("vector::reserve"));
73*404b540aSrobert       if (this->capacity() < __n)
74*404b540aSrobert 	{
75*404b540aSrobert 	  const size_type __old_size = size();
76*404b540aSrobert 	  pointer __tmp = _M_allocate_and_copy(__n, this->_M_impl._M_start,
77*404b540aSrobert 					       this->_M_impl._M_finish);
78*404b540aSrobert 	  std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
79*404b540aSrobert 			_M_get_Tp_allocator());
80*404b540aSrobert 	  _M_deallocate(this->_M_impl._M_start,
81*404b540aSrobert 			this->_M_impl._M_end_of_storage
82*404b540aSrobert 			- this->_M_impl._M_start);
83*404b540aSrobert 	  this->_M_impl._M_start = __tmp;
84*404b540aSrobert 	  this->_M_impl._M_finish = __tmp + __old_size;
85*404b540aSrobert 	  this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
86*404b540aSrobert 	}
87*404b540aSrobert     }
88*404b540aSrobert 
89*404b540aSrobert   template<typename _Tp, typename _Alloc>
90*404b540aSrobert     typename vector<_Tp, _Alloc>::iterator
91*404b540aSrobert     vector<_Tp, _Alloc>::
insert(iterator __position,const value_type & __x)92*404b540aSrobert     insert(iterator __position, const value_type& __x)
93*404b540aSrobert     {
94*404b540aSrobert       const size_type __n = __position - begin();
95*404b540aSrobert       if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage
96*404b540aSrobert 	  && __position == end())
97*404b540aSrobert 	{
98*404b540aSrobert 	  this->_M_impl.construct(this->_M_impl._M_finish, __x);
99*404b540aSrobert 	  ++this->_M_impl._M_finish;
100*404b540aSrobert 	}
101*404b540aSrobert       else
102*404b540aSrobert         _M_insert_aux(__position, __x);
103*404b540aSrobert       return iterator(this->_M_impl._M_start + __n);
104*404b540aSrobert     }
105*404b540aSrobert 
106*404b540aSrobert   template<typename _Tp, typename _Alloc>
107*404b540aSrobert     typename vector<_Tp, _Alloc>::iterator
108*404b540aSrobert     vector<_Tp, _Alloc>::
erase(iterator __position)109*404b540aSrobert     erase(iterator __position)
110*404b540aSrobert     {
111*404b540aSrobert       if (__position + 1 != end())
112*404b540aSrobert         std::copy(__position + 1, end(), __position);
113*404b540aSrobert       --this->_M_impl._M_finish;
114*404b540aSrobert       this->_M_impl.destroy(this->_M_impl._M_finish);
115*404b540aSrobert       return __position;
116*404b540aSrobert     }
117*404b540aSrobert 
118*404b540aSrobert   template<typename _Tp, typename _Alloc>
119*404b540aSrobert     typename vector<_Tp, _Alloc>::iterator
120*404b540aSrobert     vector<_Tp, _Alloc>::
erase(iterator __first,iterator __last)121*404b540aSrobert     erase(iterator __first, iterator __last)
122*404b540aSrobert     {
123*404b540aSrobert       if (__last != end())
124*404b540aSrobert 	std::copy(__last, end(), __first);
125*404b540aSrobert       _M_erase_at_end(__first.base() + (end() - __last));
126*404b540aSrobert       return __first;
127*404b540aSrobert     }
128*404b540aSrobert 
129*404b540aSrobert   template<typename _Tp, typename _Alloc>
130*404b540aSrobert     vector<_Tp, _Alloc>&
131*404b540aSrobert     vector<_Tp, _Alloc>::
operator =(const vector<_Tp,_Alloc> & __x)132*404b540aSrobert     operator=(const vector<_Tp, _Alloc>& __x)
133*404b540aSrobert     {
134*404b540aSrobert       if (&__x != this)
135*404b540aSrobert 	{
136*404b540aSrobert 	  const size_type __xlen = __x.size();
137*404b540aSrobert 	  if (__xlen > capacity())
138*404b540aSrobert 	    {
139*404b540aSrobert 	      pointer __tmp = _M_allocate_and_copy(__xlen, __x.begin(),
140*404b540aSrobert 						   __x.end());
141*404b540aSrobert 	      std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
142*404b540aSrobert 			    _M_get_Tp_allocator());
143*404b540aSrobert 	      _M_deallocate(this->_M_impl._M_start,
144*404b540aSrobert 			    this->_M_impl._M_end_of_storage
145*404b540aSrobert 			    - this->_M_impl._M_start);
146*404b540aSrobert 	      this->_M_impl._M_start = __tmp;
147*404b540aSrobert 	      this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __xlen;
148*404b540aSrobert 	    }
149*404b540aSrobert 	  else if (size() >= __xlen)
150*404b540aSrobert 	    {
151*404b540aSrobert 	      std::_Destroy(std::copy(__x.begin(), __x.end(), begin()),
152*404b540aSrobert 			    end(), _M_get_Tp_allocator());
153*404b540aSrobert 	    }
154*404b540aSrobert 	  else
155*404b540aSrobert 	    {
156*404b540aSrobert 	      std::copy(__x._M_impl._M_start, __x._M_impl._M_start + size(),
157*404b540aSrobert 			this->_M_impl._M_start);
158*404b540aSrobert 	      std::__uninitialized_copy_a(__x._M_impl._M_start + size(),
159*404b540aSrobert 					  __x._M_impl._M_finish,
160*404b540aSrobert 					  this->_M_impl._M_finish,
161*404b540aSrobert 					  _M_get_Tp_allocator());
162*404b540aSrobert 	    }
163*404b540aSrobert 	  this->_M_impl._M_finish = this->_M_impl._M_start + __xlen;
164*404b540aSrobert 	}
165*404b540aSrobert       return *this;
166*404b540aSrobert     }
167*404b540aSrobert 
168*404b540aSrobert   template<typename _Tp, typename _Alloc>
169*404b540aSrobert     void
170*404b540aSrobert     vector<_Tp, _Alloc>::
_M_fill_assign(size_t __n,const value_type & __val)171*404b540aSrobert     _M_fill_assign(size_t __n, const value_type& __val)
172*404b540aSrobert     {
173*404b540aSrobert       if (__n > capacity())
174*404b540aSrobert 	{
175*404b540aSrobert 	  vector __tmp(__n, __val, _M_get_Tp_allocator());
176*404b540aSrobert 	  __tmp.swap(*this);
177*404b540aSrobert 	}
178*404b540aSrobert       else if (__n > size())
179*404b540aSrobert 	{
180*404b540aSrobert 	  std::fill(begin(), end(), __val);
181*404b540aSrobert 	  std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
182*404b540aSrobert 					__n - size(), __val,
183*404b540aSrobert 					_M_get_Tp_allocator());
184*404b540aSrobert 	  this->_M_impl._M_finish += __n - size();
185*404b540aSrobert 	}
186*404b540aSrobert       else
187*404b540aSrobert         _M_erase_at_end(std::fill_n(this->_M_impl._M_start, __n, __val));
188*404b540aSrobert     }
189*404b540aSrobert 
190*404b540aSrobert   template<typename _Tp, typename _Alloc>
191*404b540aSrobert     template<typename _InputIterator>
192*404b540aSrobert       void
193*404b540aSrobert       vector<_Tp, _Alloc>::
_M_assign_aux(_InputIterator __first,_InputIterator __last,std::input_iterator_tag)194*404b540aSrobert       _M_assign_aux(_InputIterator __first, _InputIterator __last,
195*404b540aSrobert 		    std::input_iterator_tag)
196*404b540aSrobert       {
197*404b540aSrobert 	pointer __cur(this->_M_impl._M_start);
198*404b540aSrobert 	for (; __first != __last && __cur != this->_M_impl._M_finish;
199*404b540aSrobert 	     ++__cur, ++__first)
200*404b540aSrobert 	  *__cur = *__first;
201*404b540aSrobert 	if (__first == __last)
202*404b540aSrobert 	  _M_erase_at_end(__cur);
203*404b540aSrobert 	else
204*404b540aSrobert 	  insert(end(), __first, __last);
205*404b540aSrobert       }
206*404b540aSrobert 
207*404b540aSrobert   template<typename _Tp, typename _Alloc>
208*404b540aSrobert     template<typename _ForwardIterator>
209*404b540aSrobert       void
210*404b540aSrobert       vector<_Tp, _Alloc>::
_M_assign_aux(_ForwardIterator __first,_ForwardIterator __last,std::forward_iterator_tag)211*404b540aSrobert       _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
212*404b540aSrobert 		    std::forward_iterator_tag)
213*404b540aSrobert       {
214*404b540aSrobert 	const size_type __len = std::distance(__first, __last);
215*404b540aSrobert 
216*404b540aSrobert 	if (__len > capacity())
217*404b540aSrobert 	  {
218*404b540aSrobert 	    pointer __tmp(_M_allocate_and_copy(__len, __first, __last));
219*404b540aSrobert 	    std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
220*404b540aSrobert 			  _M_get_Tp_allocator());
221*404b540aSrobert 	    _M_deallocate(this->_M_impl._M_start,
222*404b540aSrobert 			  this->_M_impl._M_end_of_storage
223*404b540aSrobert 			  - this->_M_impl._M_start);
224*404b540aSrobert 	    this->_M_impl._M_start = __tmp;
225*404b540aSrobert 	    this->_M_impl._M_finish = this->_M_impl._M_start + __len;
226*404b540aSrobert 	    this->_M_impl._M_end_of_storage = this->_M_impl._M_finish;
227*404b540aSrobert 	  }
228*404b540aSrobert 	else if (size() >= __len)
229*404b540aSrobert 	  _M_erase_at_end(std::copy(__first, __last, this->_M_impl._M_start));
230*404b540aSrobert 	else
231*404b540aSrobert 	  {
232*404b540aSrobert 	    _ForwardIterator __mid = __first;
233*404b540aSrobert 	    std::advance(__mid, size());
234*404b540aSrobert 	    std::copy(__first, __mid, this->_M_impl._M_start);
235*404b540aSrobert 	    this->_M_impl._M_finish =
236*404b540aSrobert 	      std::__uninitialized_copy_a(__mid, __last,
237*404b540aSrobert 					  this->_M_impl._M_finish,
238*404b540aSrobert 					  _M_get_Tp_allocator());
239*404b540aSrobert 	  }
240*404b540aSrobert       }
241*404b540aSrobert 
242*404b540aSrobert   template<typename _Tp, typename _Alloc>
243*404b540aSrobert     void
244*404b540aSrobert     vector<_Tp, _Alloc>::
_M_insert_aux(iterator __position,const _Tp & __x)245*404b540aSrobert     _M_insert_aux(iterator __position, const _Tp& __x)
246*404b540aSrobert     {
247*404b540aSrobert       if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
248*404b540aSrobert 	{
249*404b540aSrobert 	  this->_M_impl.construct(this->_M_impl._M_finish,
250*404b540aSrobert 				  *(this->_M_impl._M_finish - 1));
251*404b540aSrobert 	  ++this->_M_impl._M_finish;
252*404b540aSrobert 	  _Tp __x_copy = __x;
253*404b540aSrobert 	  std::copy_backward(__position.base(),
254*404b540aSrobert 			     this->_M_impl._M_finish - 2,
255*404b540aSrobert 			     this->_M_impl._M_finish - 1);
256*404b540aSrobert 	  *__position = __x_copy;
257*404b540aSrobert 	}
258*404b540aSrobert       else
259*404b540aSrobert 	{
260*404b540aSrobert 	  const size_type __old_size = size();
261*404b540aSrobert 	  if (__old_size == this->max_size())
262*404b540aSrobert 	    __throw_length_error(__N("vector::_M_insert_aux"));
263*404b540aSrobert 
264*404b540aSrobert 	  // When sizeof(value_type) == 1 and __old_size > size_type(-1)/2
265*404b540aSrobert 	  // __len overflows: if we don't notice and _M_allocate doesn't
266*404b540aSrobert 	  // throw we crash badly later.
267*404b540aSrobert 	  size_type __len = __old_size != 0 ? 2 * __old_size : 1;
268*404b540aSrobert 	  if (__len < __old_size)
269*404b540aSrobert 	    __len = this->max_size();
270*404b540aSrobert 
271*404b540aSrobert 	  pointer __new_start(this->_M_allocate(__len));
272*404b540aSrobert 	  pointer __new_finish(__new_start);
273*404b540aSrobert 	  try
274*404b540aSrobert 	    {
275*404b540aSrobert 	      __new_finish =
276*404b540aSrobert 		std::__uninitialized_copy_a(this->_M_impl._M_start,
277*404b540aSrobert 					    __position.base(), __new_start,
278*404b540aSrobert 					    _M_get_Tp_allocator());
279*404b540aSrobert 	      this->_M_impl.construct(__new_finish, __x);
280*404b540aSrobert 	      ++__new_finish;
281*404b540aSrobert 	      __new_finish =
282*404b540aSrobert 		std::__uninitialized_copy_a(__position.base(),
283*404b540aSrobert 					    this->_M_impl._M_finish,
284*404b540aSrobert 					    __new_finish,
285*404b540aSrobert 					    _M_get_Tp_allocator());
286*404b540aSrobert 	    }
287*404b540aSrobert 	  catch(...)
288*404b540aSrobert 	    {
289*404b540aSrobert 	      std::_Destroy(__new_start, __new_finish, _M_get_Tp_allocator());
290*404b540aSrobert 	      _M_deallocate(__new_start, __len);
291*404b540aSrobert 	      __throw_exception_again;
292*404b540aSrobert 	    }
293*404b540aSrobert 	  std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
294*404b540aSrobert 			_M_get_Tp_allocator());
295*404b540aSrobert 	  _M_deallocate(this->_M_impl._M_start,
296*404b540aSrobert 			this->_M_impl._M_end_of_storage
297*404b540aSrobert 			- this->_M_impl._M_start);
298*404b540aSrobert 	  this->_M_impl._M_start = __new_start;
299*404b540aSrobert 	  this->_M_impl._M_finish = __new_finish;
300*404b540aSrobert 	  this->_M_impl._M_end_of_storage = __new_start + __len;
301*404b540aSrobert 	}
302*404b540aSrobert     }
303*404b540aSrobert 
304*404b540aSrobert   template<typename _Tp, typename _Alloc>
305*404b540aSrobert     void
306*404b540aSrobert     vector<_Tp, _Alloc>::
_M_fill_insert(iterator __position,size_type __n,const value_type & __x)307*404b540aSrobert     _M_fill_insert(iterator __position, size_type __n, const value_type& __x)
308*404b540aSrobert     {
309*404b540aSrobert       if (__n != 0)
310*404b540aSrobert 	{
311*404b540aSrobert 	  if (size_type(this->_M_impl._M_end_of_storage
312*404b540aSrobert 			- this->_M_impl._M_finish) >= __n)
313*404b540aSrobert 	    {
314*404b540aSrobert 	      value_type __x_copy = __x;
315*404b540aSrobert 	      const size_type __elems_after = end() - __position;
316*404b540aSrobert 	      pointer __old_finish(this->_M_impl._M_finish);
317*404b540aSrobert 	      if (__elems_after > __n)
318*404b540aSrobert 		{
319*404b540aSrobert 		  std::__uninitialized_copy_a(this->_M_impl._M_finish - __n,
320*404b540aSrobert 					      this->_M_impl._M_finish,
321*404b540aSrobert 					      this->_M_impl._M_finish,
322*404b540aSrobert 					      _M_get_Tp_allocator());
323*404b540aSrobert 		  this->_M_impl._M_finish += __n;
324*404b540aSrobert 		  std::copy_backward(__position.base(), __old_finish - __n,
325*404b540aSrobert 				     __old_finish);
326*404b540aSrobert 		  std::fill(__position.base(), __position.base() + __n,
327*404b540aSrobert 			    __x_copy);
328*404b540aSrobert 		}
329*404b540aSrobert 	      else
330*404b540aSrobert 		{
331*404b540aSrobert 		  std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
332*404b540aSrobert 						__n - __elems_after,
333*404b540aSrobert 						__x_copy,
334*404b540aSrobert 						_M_get_Tp_allocator());
335*404b540aSrobert 		  this->_M_impl._M_finish += __n - __elems_after;
336*404b540aSrobert 		  std::__uninitialized_copy_a(__position.base(), __old_finish,
337*404b540aSrobert 					      this->_M_impl._M_finish,
338*404b540aSrobert 					      _M_get_Tp_allocator());
339*404b540aSrobert 		  this->_M_impl._M_finish += __elems_after;
340*404b540aSrobert 		  std::fill(__position.base(), __old_finish, __x_copy);
341*404b540aSrobert 		}
342*404b540aSrobert 	    }
343*404b540aSrobert 	  else
344*404b540aSrobert 	    {
345*404b540aSrobert 	      const size_type __old_size = size();
346*404b540aSrobert 	      if (this->max_size() - __old_size < __n)
347*404b540aSrobert 		__throw_length_error(__N("vector::_M_fill_insert"));
348*404b540aSrobert 
349*404b540aSrobert 	      // See _M_insert_aux above.
350*404b540aSrobert 	      size_type __len = __old_size + std::max(__old_size, __n);
351*404b540aSrobert 	      if (__len < __old_size)
352*404b540aSrobert 		__len = this->max_size();
353*404b540aSrobert 
354*404b540aSrobert 	      pointer __new_start(this->_M_allocate(__len));
355*404b540aSrobert 	      pointer __new_finish(__new_start);
356*404b540aSrobert 	      try
357*404b540aSrobert 		{
358*404b540aSrobert 		  __new_finish =
359*404b540aSrobert 		    std::__uninitialized_copy_a(this->_M_impl._M_start,
360*404b540aSrobert 						__position.base(),
361*404b540aSrobert 						__new_start,
362*404b540aSrobert 						_M_get_Tp_allocator());
363*404b540aSrobert 		  std::__uninitialized_fill_n_a(__new_finish, __n, __x,
364*404b540aSrobert 						_M_get_Tp_allocator());
365*404b540aSrobert 		  __new_finish += __n;
366*404b540aSrobert 		  __new_finish =
367*404b540aSrobert 		    std::__uninitialized_copy_a(__position.base(),
368*404b540aSrobert 						this->_M_impl._M_finish,
369*404b540aSrobert 						__new_finish,
370*404b540aSrobert 						_M_get_Tp_allocator());
371*404b540aSrobert 		}
372*404b540aSrobert 	      catch(...)
373*404b540aSrobert 		{
374*404b540aSrobert 		  std::_Destroy(__new_start, __new_finish,
375*404b540aSrobert 				_M_get_Tp_allocator());
376*404b540aSrobert 		  _M_deallocate(__new_start, __len);
377*404b540aSrobert 		  __throw_exception_again;
378*404b540aSrobert 		}
379*404b540aSrobert 	      std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
380*404b540aSrobert 			    _M_get_Tp_allocator());
381*404b540aSrobert 	      _M_deallocate(this->_M_impl._M_start,
382*404b540aSrobert 			    this->_M_impl._M_end_of_storage
383*404b540aSrobert 			    - this->_M_impl._M_start);
384*404b540aSrobert 	      this->_M_impl._M_start = __new_start;
385*404b540aSrobert 	      this->_M_impl._M_finish = __new_finish;
386*404b540aSrobert 	      this->_M_impl._M_end_of_storage = __new_start + __len;
387*404b540aSrobert 	    }
388*404b540aSrobert 	}
389*404b540aSrobert     }
390*404b540aSrobert 
391*404b540aSrobert   template<typename _Tp, typename _Alloc> template<typename _InputIterator>
392*404b540aSrobert     void
393*404b540aSrobert     vector<_Tp, _Alloc>::
_M_range_insert(iterator __pos,_InputIterator __first,_InputIterator __last,std::input_iterator_tag)394*404b540aSrobert     _M_range_insert(iterator __pos, _InputIterator __first,
395*404b540aSrobert 		    _InputIterator __last, std::input_iterator_tag)
396*404b540aSrobert     {
397*404b540aSrobert       for (; __first != __last; ++__first)
398*404b540aSrobert 	{
399*404b540aSrobert 	  __pos = insert(__pos, *__first);
400*404b540aSrobert 	  ++__pos;
401*404b540aSrobert 	}
402*404b540aSrobert     }
403*404b540aSrobert 
404*404b540aSrobert   template<typename _Tp, typename _Alloc>
405*404b540aSrobert     template<typename _ForwardIterator>
406*404b540aSrobert       void
407*404b540aSrobert       vector<_Tp, _Alloc>::
_M_range_insert(iterator __position,_ForwardIterator __first,_ForwardIterator __last,std::forward_iterator_tag)408*404b540aSrobert       _M_range_insert(iterator __position, _ForwardIterator __first,
409*404b540aSrobert 		      _ForwardIterator __last, std::forward_iterator_tag)
410*404b540aSrobert       {
411*404b540aSrobert 	if (__first != __last)
412*404b540aSrobert 	  {
413*404b540aSrobert 	    const size_type __n = std::distance(__first, __last);
414*404b540aSrobert 	    if (size_type(this->_M_impl._M_end_of_storage
415*404b540aSrobert 			  - this->_M_impl._M_finish) >= __n)
416*404b540aSrobert 	      {
417*404b540aSrobert 		const size_type __elems_after = end() - __position;
418*404b540aSrobert 		pointer __old_finish(this->_M_impl._M_finish);
419*404b540aSrobert 		if (__elems_after > __n)
420*404b540aSrobert 		  {
421*404b540aSrobert 		    std::__uninitialized_copy_a(this->_M_impl._M_finish - __n,
422*404b540aSrobert 						this->_M_impl._M_finish,
423*404b540aSrobert 						this->_M_impl._M_finish,
424*404b540aSrobert 						_M_get_Tp_allocator());
425*404b540aSrobert 		    this->_M_impl._M_finish += __n;
426*404b540aSrobert 		    std::copy_backward(__position.base(), __old_finish - __n,
427*404b540aSrobert 				       __old_finish);
428*404b540aSrobert 		    std::copy(__first, __last, __position);
429*404b540aSrobert 		  }
430*404b540aSrobert 		else
431*404b540aSrobert 		  {
432*404b540aSrobert 		    _ForwardIterator __mid = __first;
433*404b540aSrobert 		    std::advance(__mid, __elems_after);
434*404b540aSrobert 		    std::__uninitialized_copy_a(__mid, __last,
435*404b540aSrobert 						this->_M_impl._M_finish,
436*404b540aSrobert 						_M_get_Tp_allocator());
437*404b540aSrobert 		    this->_M_impl._M_finish += __n - __elems_after;
438*404b540aSrobert 		    std::__uninitialized_copy_a(__position.base(),
439*404b540aSrobert 						__old_finish,
440*404b540aSrobert 						this->_M_impl._M_finish,
441*404b540aSrobert 						_M_get_Tp_allocator());
442*404b540aSrobert 		    this->_M_impl._M_finish += __elems_after;
443*404b540aSrobert 		    std::copy(__first, __mid, __position);
444*404b540aSrobert 		  }
445*404b540aSrobert 	      }
446*404b540aSrobert 	    else
447*404b540aSrobert 	      {
448*404b540aSrobert 		const size_type __old_size = size();
449*404b540aSrobert 		if (this->max_size() - __old_size < __n)
450*404b540aSrobert 		  __throw_length_error(__N("vector::_M_range_insert"));
451*404b540aSrobert 
452*404b540aSrobert 		// See _M_insert_aux above.
453*404b540aSrobert 		size_type __len = __old_size + std::max(__old_size, __n);
454*404b540aSrobert 		if (__len < __old_size)
455*404b540aSrobert 		  __len = this->max_size();
456*404b540aSrobert 
457*404b540aSrobert 		pointer __new_start(this->_M_allocate(__len));
458*404b540aSrobert 		pointer __new_finish(__new_start);
459*404b540aSrobert 		try
460*404b540aSrobert 		  {
461*404b540aSrobert 		    __new_finish =
462*404b540aSrobert 		      std::__uninitialized_copy_a(this->_M_impl._M_start,
463*404b540aSrobert 						  __position.base(),
464*404b540aSrobert 						  __new_start,
465*404b540aSrobert 						  _M_get_Tp_allocator());
466*404b540aSrobert 		    __new_finish =
467*404b540aSrobert 		      std::__uninitialized_copy_a(__first, __last, __new_finish,
468*404b540aSrobert 						  _M_get_Tp_allocator());
469*404b540aSrobert 		    __new_finish =
470*404b540aSrobert 		      std::__uninitialized_copy_a(__position.base(),
471*404b540aSrobert 						  this->_M_impl._M_finish,
472*404b540aSrobert 						  __new_finish,
473*404b540aSrobert 						  _M_get_Tp_allocator());
474*404b540aSrobert 		  }
475*404b540aSrobert 		catch(...)
476*404b540aSrobert 		  {
477*404b540aSrobert 		    std::_Destroy(__new_start, __new_finish,
478*404b540aSrobert 				  _M_get_Tp_allocator());
479*404b540aSrobert 		    _M_deallocate(__new_start, __len);
480*404b540aSrobert 		    __throw_exception_again;
481*404b540aSrobert 		  }
482*404b540aSrobert 		std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
483*404b540aSrobert 			      _M_get_Tp_allocator());
484*404b540aSrobert 		_M_deallocate(this->_M_impl._M_start,
485*404b540aSrobert 			      this->_M_impl._M_end_of_storage
486*404b540aSrobert 			      - this->_M_impl._M_start);
487*404b540aSrobert 		this->_M_impl._M_start = __new_start;
488*404b540aSrobert 		this->_M_impl._M_finish = __new_finish;
489*404b540aSrobert 		this->_M_impl._M_end_of_storage = __new_start + __len;
490*404b540aSrobert 	      }
491*404b540aSrobert 	  }
492*404b540aSrobert       }
493*404b540aSrobert 
494*404b540aSrobert _GLIBCXX_END_NESTED_NAMESPACE
495*404b540aSrobert 
496*404b540aSrobert #endif /* _VECTOR_TCC */
497