1 // Vector implementation -*- C++ -*-
3 // Copyright (C) 2001 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
33 * Hewlett-Packard Company
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Hewlett-Packard Company makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
45 * Silicon Graphics Computer Systems, Inc.
47 * Permission to use, copy, modify, distribute and sell this software
48 * and its documentation for any purpose is hereby granted without fee,
49 * provided that the above copyright notice appear in all copies and
50 * that both that copyright notice and this permission notice appear
51 * in supporting documentation. Silicon Graphics makes no
52 * representations about the suitability of this software for any
53 * purpose. It is provided "as is" without express or implied warranty.
56 /* NOTE: This is an internal header file, included by other STL headers.
57 * You should not attempt to use it directly.
60 #ifndef __SGI_STL_INTERNAL_VECTOR_H
61 #define __SGI_STL_INTERNAL_VECTOR_H
63 #include <bits/stl_iterator_base_funcs.h>
64 #include <bits/functexcept.h>
65 #include <bits/concept_check.h>
70 // The vector base class serves two purposes. First, its constructor
71 // and destructor allocate (but don't initialize) storage. This makes
72 // exception safety easier. Second, the base class encapsulates all of
73 // the differences between SGI-style allocators and standard-conforming
76 // Base class for ordinary allocators.
77 template <class _Tp
, class _Allocator
, bool _IsStatic
>
78 class _Vector_alloc_base
{
80 typedef typename _Alloc_traits
<_Tp
, _Allocator
>::allocator_type
82 allocator_type
get_allocator() const { return _M_data_allocator
; }
84 _Vector_alloc_base(const allocator_type
& __a
)
85 : _M_data_allocator(__a
), _M_start(0), _M_finish(0), _M_end_of_storage(0)
89 allocator_type _M_data_allocator
;
92 _Tp
* _M_end_of_storage
;
94 _Tp
* _M_allocate(size_t __n
)
95 { return _M_data_allocator
.allocate(__n
); }
96 void _M_deallocate(_Tp
* __p
, size_t __n
)
97 { if (__p
) _M_data_allocator
.deallocate(__p
, __n
); }
100 // Specialization for allocators that have the property that we don't
101 // actually have to store an allocator object.
102 template <class _Tp
, class _Allocator
>
103 class _Vector_alloc_base
<_Tp
, _Allocator
, true> {
105 typedef typename _Alloc_traits
<_Tp
, _Allocator
>::allocator_type
107 allocator_type
get_allocator() const { return allocator_type(); }
109 _Vector_alloc_base(const allocator_type
&)
110 : _M_start(0), _M_finish(0), _M_end_of_storage(0)
116 _Tp
* _M_end_of_storage
;
118 typedef typename _Alloc_traits
<_Tp
, _Allocator
>::_Alloc_type _Alloc_type
;
119 _Tp
* _M_allocate(size_t __n
)
120 { return _Alloc_type::allocate(__n
); }
121 void _M_deallocate(_Tp
* __p
, size_t __n
)
122 { _Alloc_type::deallocate(__p
, __n
);}
125 template <class _Tp
, class _Alloc
>
127 : public _Vector_alloc_base
<_Tp
, _Alloc
,
128 _Alloc_traits
<_Tp
, _Alloc
>::_S_instanceless
>
130 typedef _Vector_alloc_base
<_Tp
, _Alloc
,
131 _Alloc_traits
<_Tp
, _Alloc
>::_S_instanceless
>
133 typedef typename
_Base::allocator_type allocator_type
;
135 _Vector_base(const allocator_type
& __a
) : _Base(__a
) {}
136 _Vector_base(size_t __n
, const allocator_type
& __a
) : _Base(__a
) {
137 _M_start
= _M_allocate(__n
);
138 _M_finish
= _M_start
;
139 _M_end_of_storage
= _M_start
+ __n
;
142 ~_Vector_base() { _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
); }
146 template <class _Tp
, class _Alloc
= allocator
<_Tp
> >
147 class vector
: protected _Vector_base
<_Tp
, _Alloc
>
149 // concept requirements
150 __glibcpp_class_requires(_Tp
, _SGIAssignableConcept
);
153 typedef _Vector_base
<_Tp
, _Alloc
> _Base
;
154 typedef vector
<_Tp
, _Alloc
> vector_type
;
156 typedef _Tp value_type
;
157 typedef value_type
* pointer
;
158 typedef const value_type
* const_pointer
;
159 typedef __normal_iterator
<pointer
, vector_type
> iterator
;
160 typedef __normal_iterator
<const_pointer
, vector_type
> const_iterator
;
161 typedef value_type
& reference
;
162 typedef const value_type
& const_reference
;
163 typedef size_t size_type
;
164 typedef ptrdiff_t difference_type
;
166 typedef typename
_Base::allocator_type allocator_type
;
167 allocator_type
get_allocator() const { return _Base::get_allocator(); }
169 typedef reverse_iterator
<const_iterator
> const_reverse_iterator
;
170 typedef reverse_iterator
<iterator
> reverse_iterator
;
173 using _Base::_M_allocate
;
174 using _Base::_M_deallocate
;
175 using _Base::_M_start
;
176 using _Base::_M_finish
;
177 using _Base::_M_end_of_storage
;
180 void _M_insert_aux(iterator __position
, const _Tp
& __x
);
181 void _M_insert_aux(iterator __position
);
184 iterator
begin() { return iterator (_M_start
); }
185 const_iterator
begin() const
186 { return const_iterator (_M_start
); }
187 iterator
end() { return iterator (_M_finish
); }
188 const_iterator
end() const { return const_iterator (_M_finish
); }
190 reverse_iterator
rbegin()
191 { return reverse_iterator(end()); }
192 const_reverse_iterator
rbegin() const
193 { return const_reverse_iterator(end()); }
194 reverse_iterator
rend()
195 { return reverse_iterator(begin()); }
196 const_reverse_iterator
rend() const
197 { return const_reverse_iterator(begin()); }
199 size_type
size() const
200 { return size_type(end() - begin()); }
201 size_type
max_size() const
202 { return size_type(-1) / sizeof(_Tp
); }
203 size_type
capacity() const
204 { return size_type(const_iterator(_M_end_of_storage
) - begin()); }
206 { return begin() == end(); }
208 reference
operator[](size_type __n
) { return *(begin() + __n
); }
209 const_reference
operator[](size_type __n
) const { return *(begin() + __n
); }
211 void _M_range_check(size_type __n
) const {
212 if (__n
>= this->size())
213 __throw_out_of_range("vector");
216 reference
at(size_type __n
)
217 { _M_range_check(__n
); return (*this)[__n
]; }
218 const_reference
at(size_type __n
) const
219 { _M_range_check(__n
); return (*this)[__n
]; }
221 explicit vector(const allocator_type
& __a
= allocator_type())
224 vector(size_type __n
, const _Tp
& __value
,
225 const allocator_type
& __a
= allocator_type())
227 { _M_finish
= uninitialized_fill_n(_M_start
, __n
, __value
); }
229 explicit vector(size_type __n
)
230 : _Base(__n
, allocator_type())
231 { _M_finish
= uninitialized_fill_n(_M_start
, __n
, _Tp()); }
233 vector(const vector
<_Tp
, _Alloc
>& __x
)
234 : _Base(__x
.size(), __x
.get_allocator())
235 { _M_finish
= uninitialized_copy(__x
.begin(), __x
.end(), _M_start
); }
237 // Check whether it's an integral type. If so, it's not an iterator.
238 template <class _InputIterator
>
239 vector(_InputIterator __first
, _InputIterator __last
,
240 const allocator_type
& __a
= allocator_type()) : _Base(__a
) {
241 typedef typename _Is_integer
<_InputIterator
>::_Integral _Integral
;
242 _M_initialize_aux(__first
, __last
, _Integral());
245 template <class _Integer
>
246 void _M_initialize_aux(_Integer __n
, _Integer __value
, __true_type
) {
247 _M_start
= _M_allocate(__n
);
248 _M_end_of_storage
= _M_start
+ __n
;
249 _M_finish
= uninitialized_fill_n(_M_start
, __n
, __value
);
252 template <class _InputIterator
>
253 void _M_initialize_aux(_InputIterator __first
, _InputIterator __last
,
255 _M_range_initialize(__first
, __last
, __iterator_category(__first
));
258 ~vector() { destroy(_M_start
, _M_finish
); }
260 vector
<_Tp
, _Alloc
>& operator=(const vector
<_Tp
, _Alloc
>& __x
);
261 void reserve(size_type __n
) {
262 if (capacity() < __n
) {
263 const size_type __old_size
= size();
264 pointer __tmp
= _M_allocate_and_copy(__n
, _M_start
, _M_finish
);
265 destroy(_M_start
, _M_finish
);
266 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
268 _M_finish
= __tmp
+ __old_size
;
269 _M_end_of_storage
= _M_start
+ __n
;
273 // assign(), a generalized assignment member function. Two
274 // versions: one that takes a count, and one that takes a range.
275 // The range version is a member template, so we dispatch on whether
276 // or not the type is an integer.
278 void assign(size_type __n
, const _Tp
& __val
) { _M_fill_assign(__n
, __val
); }
279 void _M_fill_assign(size_type __n
, const _Tp
& __val
);
281 template <class _InputIterator
>
282 void assign(_InputIterator __first
, _InputIterator __last
) {
283 typedef typename _Is_integer
<_InputIterator
>::_Integral _Integral
;
284 _M_assign_dispatch(__first
, __last
, _Integral());
287 template <class _Integer
>
288 void _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
289 { _M_fill_assign((size_type
) __n
, (_Tp
) __val
); }
291 template <class _InputIter
>
292 void _M_assign_dispatch(_InputIter __first
, _InputIter __last
, __false_type
)
293 { _M_assign_aux(__first
, __last
, __iterator_category(__first
)); }
295 template <class _InputIterator
>
296 void _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
299 template <class _ForwardIterator
>
300 void _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
301 forward_iterator_tag
);
303 reference
front() { return *begin(); }
304 const_reference
front() const { return *begin(); }
305 reference
back() { return *(end() - 1); }
306 const_reference
back() const { return *(end() - 1); }
308 void push_back(const _Tp
& __x
) {
309 if (_M_finish
!= _M_end_of_storage
) {
310 construct(_M_finish
, __x
);
314 _M_insert_aux(end(), __x
);
317 if (_M_finish
!= _M_end_of_storage
) {
318 construct(_M_finish
);
322 _M_insert_aux(end());
324 void swap(vector
<_Tp
, _Alloc
>& __x
) {
325 std::swap(_M_start
, __x
._M_start
);
326 std::swap(_M_finish
, __x
._M_finish
);
327 std::swap(_M_end_of_storage
, __x
._M_end_of_storage
);
330 iterator
insert(iterator __position
, const _Tp
& __x
) {
331 size_type __n
= __position
- begin();
332 if (_M_finish
!= _M_end_of_storage
&& __position
== end()) {
333 construct(_M_finish
, __x
);
337 _M_insert_aux(iterator(__position
), __x
);
338 return begin() + __n
;
340 iterator
insert(iterator __position
) {
341 size_type __n
= __position
- begin();
342 if (_M_finish
!= _M_end_of_storage
&& __position
== end()) {
343 construct(_M_finish
);
347 _M_insert_aux(iterator(__position
));
348 return begin() + __n
;
350 // Check whether it's an integral type. If so, it's not an iterator.
351 template <class _InputIterator
>
352 void insert(iterator __pos
, _InputIterator __first
, _InputIterator __last
) {
353 typedef typename _Is_integer
<_InputIterator
>::_Integral _Integral
;
354 _M_insert_dispatch(__pos
, __first
, __last
, _Integral());
357 template <class _Integer
>
358 void _M_insert_dispatch(iterator __pos
, _Integer __n
, _Integer __val
,
360 { _M_fill_insert(__pos
, (size_type
) __n
, (_Tp
) __val
); }
362 template <class _InputIterator
>
363 void _M_insert_dispatch(iterator __pos
,
364 _InputIterator __first
, _InputIterator __last
,
366 _M_range_insert(__pos
, __first
, __last
, __iterator_category(__first
));
369 void insert (iterator __pos
, size_type __n
, const _Tp
& __x
)
370 { _M_fill_insert(__pos
, __n
, __x
); }
372 void _M_fill_insert (iterator __pos
, size_type __n
, const _Tp
& __x
);
378 iterator
erase(iterator __position
) {
379 if (__position
+ 1 != end())
380 copy(__position
+ 1, end(), __position
);
385 iterator
erase(iterator __first
, iterator __last
) {
386 iterator
__i(copy(__last
, end(), __first
));
388 _M_finish
= _M_finish
- (__last
- __first
);
392 void resize(size_type __new_size
, const _Tp
& __x
) {
393 if (__new_size
< size())
394 erase(begin() + __new_size
, end());
396 insert(end(), __new_size
- size(), __x
);
398 void resize(size_type __new_size
) { resize(__new_size
, _Tp()); }
399 void clear() { erase(begin(), end()); }
403 template <class _ForwardIterator
>
404 pointer
_M_allocate_and_copy(size_type __n
, _ForwardIterator __first
,
405 _ForwardIterator __last
)
407 pointer __result
= _M_allocate(__n
);
409 uninitialized_copy(__first
, __last
, __result
);
412 __STL_UNWIND(_M_deallocate(__result
, __n
));
415 template <class _InputIterator
>
416 void _M_range_initialize(_InputIterator __first
,
417 _InputIterator __last
, input_iterator_tag
)
419 for ( ; __first
!= __last
; ++__first
)
423 // This function is only called by the constructor.
424 template <class _ForwardIterator
>
425 void _M_range_initialize(_ForwardIterator __first
,
426 _ForwardIterator __last
, forward_iterator_tag
)
429 distance(__first
, __last
, __n
);
430 _M_start
= _M_allocate(__n
);
431 _M_end_of_storage
= _M_start
+ __n
;
432 _M_finish
= uninitialized_copy(__first
, __last
, _M_start
);
435 template <class _InputIterator
>
436 void _M_range_insert(iterator __pos
,
437 _InputIterator __first
, _InputIterator __last
,
440 template <class _ForwardIterator
>
441 void _M_range_insert(iterator __pos
,
442 _ForwardIterator __first
, _ForwardIterator __last
,
443 forward_iterator_tag
);
446 template <class _Tp
, class _Alloc
>
448 operator==(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
450 return __x
.size() == __y
.size() &&
451 equal(__x
.begin(), __x
.end(), __y
.begin());
454 template <class _Tp
, class _Alloc
>
456 operator<(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
458 return lexicographical_compare(__x
.begin(), __x
.end(),
459 __y
.begin(), __y
.end());
462 template <class _Tp
, class _Alloc
>
463 inline void swap(vector
<_Tp
, _Alloc
>& __x
, vector
<_Tp
, _Alloc
>& __y
)
468 template <class _Tp
, class _Alloc
>
470 operator!=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
) {
471 return !(__x
== __y
);
474 template <class _Tp
, class _Alloc
>
476 operator>(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
) {
480 template <class _Tp
, class _Alloc
>
482 operator<=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
) {
486 template <class _Tp
, class _Alloc
>
488 operator>=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
) {
492 template <class _Tp
, class _Alloc
>
494 vector
<_Tp
,_Alloc
>::operator=(const vector
<_Tp
, _Alloc
>& __x
)
497 const size_type __xlen
= __x
.size();
498 if (__xlen
> capacity()) {
499 pointer __tmp
= _M_allocate_and_copy(__xlen
, __x
.begin(), __x
.end());
500 destroy(_M_start
, _M_finish
);
501 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
503 _M_end_of_storage
= _M_start
+ __xlen
;
505 else if (size() >= __xlen
) {
506 iterator
__i(copy(__x
.begin(), __x
.end(), begin()));
510 copy(__x
.begin(), __x
.begin() + size(), _M_start
);
511 uninitialized_copy(__x
.begin() + size(), __x
.end(), _M_finish
);
513 _M_finish
= _M_start
+ __xlen
;
518 template <class _Tp
, class _Alloc
>
519 void vector
<_Tp
, _Alloc
>::_M_fill_assign(size_t __n
, const value_type
& __val
)
521 if (__n
> capacity()) {
522 vector
<_Tp
, _Alloc
> __tmp(__n
, __val
, get_allocator());
525 else if (__n
> size()) {
526 fill(begin(), end(), __val
);
527 _M_finish
= uninitialized_fill_n(_M_finish
, __n
- size(), __val
);
530 erase(fill_n(begin(), __n
, __val
), end());
533 template <class _Tp
, class _Alloc
> template <class _InputIter
>
534 void vector
<_Tp
, _Alloc
>::_M_assign_aux(_InputIter __first
, _InputIter __last
,
535 input_iterator_tag
) {
536 iterator
__cur(begin());
537 for ( ; __first
!= __last
&& __cur
!= end(); ++__cur
, ++__first
)
539 if (__first
== __last
)
542 insert(end(), __first
, __last
);
545 template <class _Tp
, class _Alloc
> template <class _ForwardIter
>
547 vector
<_Tp
, _Alloc
>::_M_assign_aux(_ForwardIter __first
, _ForwardIter __last
,
548 forward_iterator_tag
) {
550 distance(__first
, __last
, __len
);
552 if (__len
> capacity()) {
553 pointer
__tmp(_M_allocate_and_copy(__len
, __first
, __last
));
554 destroy(_M_start
, _M_finish
);
555 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
557 _M_end_of_storage
= _M_finish
= _M_start
+ __len
;
559 else if (size() >= __len
) {
560 iterator
__new_finish(copy(__first
, __last
, _M_start
));
561 destroy(__new_finish
, end());
562 _M_finish
= __new_finish
.base();
565 _ForwardIter __mid
= __first
;
566 advance(__mid
, size());
567 copy(__first
, __mid
, _M_start
);
568 _M_finish
= uninitialized_copy(__mid
, __last
, _M_finish
);
572 template <class _Tp
, class _Alloc
>
574 vector
<_Tp
, _Alloc
>::_M_insert_aux(iterator __position
, const _Tp
& __x
)
576 if (_M_finish
!= _M_end_of_storage
) {
577 construct(_M_finish
, *(_M_finish
- 1));
580 copy_backward(__position
, iterator(_M_finish
- 2), iterator(_M_finish
- 1));
581 *__position
= __x_copy
;
584 const size_type __old_size
= size();
585 const size_type __len
= __old_size
!= 0 ? 2 * __old_size
: 1;
586 iterator
__new_start(_M_allocate(__len
));
587 iterator
__new_finish(__new_start
);
589 __new_finish
= uninitialized_copy(iterator(_M_start
), __position
,
591 construct(__new_finish
.base(), __x
);
593 __new_finish
= uninitialized_copy(__position
, iterator(_M_finish
),
596 __STL_UNWIND((destroy(__new_start
,__new_finish
),
597 _M_deallocate(__new_start
.base(),__len
)));
598 destroy(begin(), end());
599 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
600 _M_start
= __new_start
.base();
601 _M_finish
= __new_finish
.base();
602 _M_end_of_storage
= __new_start
.base() + __len
;
606 template <class _Tp
, class _Alloc
>
608 vector
<_Tp
, _Alloc
>::_M_insert_aux(iterator __position
)
610 if (_M_finish
!= _M_end_of_storage
) {
611 construct(_M_finish
, *(_M_finish
- 1));
613 copy_backward(__position
, iterator(_M_finish
- 2),
614 iterator(_M_finish
- 1));
618 const size_type __old_size
= size();
619 const size_type __len
= __old_size
!= 0 ? 2 * __old_size
: 1;
620 pointer __new_start
= _M_allocate(__len
);
621 pointer __new_finish
= __new_start
;
623 __new_finish
= uninitialized_copy(iterator(_M_start
), __position
,
625 construct(__new_finish
);
627 __new_finish
= uninitialized_copy(__position
, iterator(_M_finish
),
630 __STL_UNWIND((destroy(__new_start
,__new_finish
),
631 _M_deallocate(__new_start
,__len
)));
632 destroy(begin(), end());
633 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
634 _M_start
= __new_start
;
635 _M_finish
= __new_finish
;
636 _M_end_of_storage
= __new_start
+ __len
;
640 template <class _Tp
, class _Alloc
>
641 void vector
<_Tp
, _Alloc
>::_M_fill_insert(iterator __position
, size_type __n
,
645 if (size_type(_M_end_of_storage
- _M_finish
) >= __n
) {
647 const size_type __elems_after
= end() - __position
;
648 iterator
__old_finish(_M_finish
);
649 if (__elems_after
> __n
) {
650 uninitialized_copy(_M_finish
- __n
, _M_finish
, _M_finish
);
652 copy_backward(__position
, __old_finish
- __n
, __old_finish
);
653 fill(__position
, __position
+ __n
, __x_copy
);
656 uninitialized_fill_n(_M_finish
, __n
- __elems_after
, __x_copy
);
657 _M_finish
+= __n
- __elems_after
;
658 uninitialized_copy(__position
, __old_finish
, _M_finish
);
659 _M_finish
+= __elems_after
;
660 fill(__position
, __old_finish
, __x_copy
);
664 const size_type __old_size
= size();
665 const size_type __len
= __old_size
+ max(__old_size
, __n
);
666 iterator
__new_start(_M_allocate(__len
));
667 iterator
__new_finish(__new_start
);
669 __new_finish
= uninitialized_copy(begin(), __position
, __new_start
);
670 __new_finish
= uninitialized_fill_n(__new_finish
, __n
, __x
);
672 = uninitialized_copy(__position
, end(), __new_finish
);
674 __STL_UNWIND((destroy(__new_start
,__new_finish
),
675 _M_deallocate(__new_start
.base(),__len
)));
676 destroy(_M_start
, _M_finish
);
677 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
678 _M_start
= __new_start
.base();
679 _M_finish
= __new_finish
.base();
680 _M_end_of_storage
= __new_start
.base() + __len
;
685 template <class _Tp
, class _Alloc
> template <class _InputIterator
>
687 vector
<_Tp
, _Alloc
>::_M_range_insert(iterator __pos
,
688 _InputIterator __first
,
689 _InputIterator __last
,
692 for ( ; __first
!= __last
; ++__first
) {
693 __pos
= insert(__pos
, *__first
);
698 template <class _Tp
, class _Alloc
> template <class _ForwardIterator
>
700 vector
<_Tp
, _Alloc
>::_M_range_insert(iterator __position
,
701 _ForwardIterator __first
,
702 _ForwardIterator __last
,
703 forward_iterator_tag
)
705 if (__first
!= __last
) {
707 distance(__first
, __last
, __n
);
708 if (size_type(_M_end_of_storage
- _M_finish
) >= __n
) {
709 const size_type __elems_after
= end() - __position
;
710 iterator
__old_finish(_M_finish
);
711 if (__elems_after
> __n
) {
712 uninitialized_copy(_M_finish
- __n
, _M_finish
, _M_finish
);
714 copy_backward(__position
, __old_finish
- __n
, __old_finish
);
715 copy(__first
, __last
, __position
);
718 _ForwardIterator __mid
= __first
;
719 advance(__mid
, __elems_after
);
720 uninitialized_copy(__mid
, __last
, _M_finish
);
721 _M_finish
+= __n
- __elems_after
;
722 uninitialized_copy(__position
, __old_finish
, _M_finish
);
723 _M_finish
+= __elems_after
;
724 copy(__first
, __mid
, __position
);
728 const size_type __old_size
= size();
729 const size_type __len
= __old_size
+ max(__old_size
, __n
);
730 iterator
__new_start(_M_allocate(__len
));
731 iterator
__new_finish(__new_start
);
733 __new_finish
= uninitialized_copy(iterator(_M_start
),
734 __position
, __new_start
);
735 __new_finish
= uninitialized_copy(__first
, __last
, __new_finish
);
737 = uninitialized_copy(__position
, iterator(_M_finish
), __new_finish
);
739 __STL_UNWIND((destroy(__new_start
,__new_finish
),
740 _M_deallocate(__new_start
.base(),__len
)));
741 destroy(_M_start
, _M_finish
);
742 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
743 _M_start
= __new_start
.base();
744 _M_finish
= __new_finish
.base();
745 _M_end_of_storage
= __new_start
.base() + __len
;
752 #endif /* __SGI_STL_INTERNAL_VECTOR_H */