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())
243 typedef typename _Is_integer
<_InputIterator
>::_Integral _Integral
;
244 _M_initialize_aux(__first
, __last
, _Integral());
247 template <class _Integer
>
248 void _M_initialize_aux(_Integer __n
, _Integer __value
, __true_type
)
250 _M_start
= _M_allocate(__n
);
251 _M_end_of_storage
= _M_start
+ __n
;
252 _M_finish
= uninitialized_fill_n(_M_start
, __n
, __value
);
255 template<class _InputIterator
>
257 _M_initialize_aux(_InputIterator __first
, _InputIterator __last
, __false_type
)
259 typedef typename iterator_traits
<_InputIterator
>::iterator_category _IterCategory
;
260 _M_range_initialize(__first
, __last
, _IterCategory());
264 { _Destroy(_M_start
, _M_finish
); }
266 vector
<_Tp
, _Alloc
>& operator=(const vector
<_Tp
, _Alloc
>& __x
);
267 void reserve(size_type __n
) {
268 if (capacity() < __n
) {
269 const size_type __old_size
= size();
270 pointer __tmp
= _M_allocate_and_copy(__n
, _M_start
, _M_finish
);
271 _Destroy(_M_start
, _M_finish
);
272 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
274 _M_finish
= __tmp
+ __old_size
;
275 _M_end_of_storage
= _M_start
+ __n
;
279 // assign(), a generalized assignment member function. Two
280 // versions: one that takes a count, and one that takes a range.
281 // The range version is a member template, so we dispatch on whether
282 // or not the type is an integer.
284 void assign(size_type __n
, const _Tp
& __val
) { _M_fill_assign(__n
, __val
); }
285 void _M_fill_assign(size_type __n
, const _Tp
& __val
);
287 template<class _InputIterator
>
289 assign(_InputIterator __first
, _InputIterator __last
)
291 typedef typename _Is_integer
<_InputIterator
>::_Integral _Integral
;
292 _M_assign_dispatch(__first
, __last
, _Integral());
295 template<class _Integer
>
297 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
298 { _M_fill_assign((size_type
) __n
, (_Tp
) __val
); }
300 template<class _InputIter
>
302 _M_assign_dispatch(_InputIter __first
, _InputIter __last
, __false_type
)
304 typedef typename iterator_traits
<_InputIter
>::iterator_category _IterCategory
;
305 _M_assign_aux(__first
, __last
, _IterCategory());
308 template <class _InputIterator
>
309 void _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
312 template <class _ForwardIterator
>
313 void _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
314 forward_iterator_tag
);
316 reference
front() { return *begin(); }
317 const_reference
front() const { return *begin(); }
318 reference
back() { return *(end() - 1); }
319 const_reference
back() const { return *(end() - 1); }
322 push_back(const _Tp
& __x
)
324 if (_M_finish
!= _M_end_of_storage
) {
325 _Construct(_M_finish
, __x
);
329 _M_insert_aux(end(), __x
);
335 if (_M_finish
!= _M_end_of_storage
) {
336 _Construct(_M_finish
);
340 _M_insert_aux(end());
344 swap(vector
<_Tp
, _Alloc
>& __x
)
346 std::swap(_M_start
, __x
._M_start
);
347 std::swap(_M_finish
, __x
._M_finish
);
348 std::swap(_M_end_of_storage
, __x
._M_end_of_storage
);
352 insert(iterator __position
, const _Tp
& __x
)
354 size_type __n
= __position
- begin();
355 if (_M_finish
!= _M_end_of_storage
&& __position
== end()) {
356 _Construct(_M_finish
, __x
);
360 _M_insert_aux(iterator(__position
), __x
);
361 return begin() + __n
;
365 insert(iterator __position
)
367 size_type __n
= __position
- begin();
368 if (_M_finish
!= _M_end_of_storage
&& __position
== end()) {
369 _Construct(_M_finish
);
373 _M_insert_aux(iterator(__position
));
374 return begin() + __n
;
377 // Check whether it's an integral type. If so, it's not an iterator.
378 template<class _InputIterator
>
380 insert(iterator __pos
, _InputIterator __first
, _InputIterator __last
)
382 typedef typename _Is_integer
<_InputIterator
>::_Integral _Integral
;
383 _M_insert_dispatch(__pos
, __first
, __last
, _Integral());
386 template <class _Integer
>
388 _M_insert_dispatch(iterator __pos
, _Integer __n
, _Integer __val
, __true_type
)
389 { _M_fill_insert(__pos
, static_cast<size_type
>(__n
), static_cast<_Tp
>(__val
)); }
391 template<class _InputIterator
>
393 _M_insert_dispatch(iterator __pos
,
394 _InputIterator __first
, _InputIterator __last
,
397 typedef typename iterator_traits
<_InputIterator
>::iterator_category _IterCategory
;
398 _M_range_insert(__pos
, __first
, __last
, _IterCategory());
401 void insert (iterator __pos
, size_type __n
, const _Tp
& __x
)
402 { _M_fill_insert(__pos
, __n
, __x
); }
404 void _M_fill_insert (iterator __pos
, size_type __n
, const _Tp
& __x
);
410 iterator
erase(iterator __position
) {
411 if (__position
+ 1 != end())
412 copy(__position
+ 1, end(), __position
);
417 iterator
erase(iterator __first
, iterator __last
) {
418 iterator
__i(copy(__last
, end(), __first
));
419 _Destroy(__i
, end());
420 _M_finish
= _M_finish
- (__last
- __first
);
424 void resize(size_type __new_size
, const _Tp
& __x
) {
425 if (__new_size
< size())
426 erase(begin() + __new_size
, end());
428 insert(end(), __new_size
- size(), __x
);
430 void resize(size_type __new_size
) { resize(__new_size
, _Tp()); }
431 void clear() { erase(begin(), end()); }
435 template <class _ForwardIterator
>
436 pointer
_M_allocate_and_copy(size_type __n
, _ForwardIterator __first
,
437 _ForwardIterator __last
)
439 pointer __result
= _M_allocate(__n
);
441 uninitialized_copy(__first
, __last
, __result
);
444 __STL_UNWIND(_M_deallocate(__result
, __n
));
447 template <class _InputIterator
>
448 void _M_range_initialize(_InputIterator __first
,
449 _InputIterator __last
, input_iterator_tag
)
451 for ( ; __first
!= __last
; ++__first
)
455 // This function is only called by the constructor.
456 template <class _ForwardIterator
>
457 void _M_range_initialize(_ForwardIterator __first
,
458 _ForwardIterator __last
, forward_iterator_tag
)
461 distance(__first
, __last
, __n
);
462 _M_start
= _M_allocate(__n
);
463 _M_end_of_storage
= _M_start
+ __n
;
464 _M_finish
= uninitialized_copy(__first
, __last
, _M_start
);
467 template <class _InputIterator
>
468 void _M_range_insert(iterator __pos
,
469 _InputIterator __first
, _InputIterator __last
,
472 template <class _ForwardIterator
>
473 void _M_range_insert(iterator __pos
,
474 _ForwardIterator __first
, _ForwardIterator __last
,
475 forward_iterator_tag
);
478 template <class _Tp
, class _Alloc
>
480 operator==(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
482 return __x
.size() == __y
.size() &&
483 equal(__x
.begin(), __x
.end(), __y
.begin());
486 template <class _Tp
, class _Alloc
>
488 operator<(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
)
490 return lexicographical_compare(__x
.begin(), __x
.end(),
491 __y
.begin(), __y
.end());
494 template <class _Tp
, class _Alloc
>
495 inline void swap(vector
<_Tp
, _Alloc
>& __x
, vector
<_Tp
, _Alloc
>& __y
)
500 template <class _Tp
, class _Alloc
>
502 operator!=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
) {
503 return !(__x
== __y
);
506 template <class _Tp
, class _Alloc
>
508 operator>(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
) {
512 template <class _Tp
, class _Alloc
>
514 operator<=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
) {
518 template <class _Tp
, class _Alloc
>
520 operator>=(const vector
<_Tp
, _Alloc
>& __x
, const vector
<_Tp
, _Alloc
>& __y
) {
524 template <class _Tp
, class _Alloc
>
526 vector
<_Tp
,_Alloc
>::operator=(const vector
<_Tp
, _Alloc
>& __x
)
529 const size_type __xlen
= __x
.size();
530 if (__xlen
> capacity()) {
531 pointer __tmp
= _M_allocate_and_copy(__xlen
, __x
.begin(), __x
.end());
532 _Destroy(_M_start
, _M_finish
);
533 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
535 _M_end_of_storage
= _M_start
+ __xlen
;
537 else if (size() >= __xlen
) {
538 iterator
__i(copy(__x
.begin(), __x
.end(), begin()));
539 _Destroy(__i
, end());
542 copy(__x
.begin(), __x
.begin() + size(), _M_start
);
543 uninitialized_copy(__x
.begin() + size(), __x
.end(), _M_finish
);
545 _M_finish
= _M_start
+ __xlen
;
550 template <class _Tp
, class _Alloc
>
551 void vector
<_Tp
, _Alloc
>::_M_fill_assign(size_t __n
, const value_type
& __val
)
553 if (__n
> capacity()) {
554 vector
<_Tp
, _Alloc
> __tmp(__n
, __val
, get_allocator());
557 else if (__n
> size()) {
558 fill(begin(), end(), __val
);
559 _M_finish
= uninitialized_fill_n(_M_finish
, __n
- size(), __val
);
562 erase(fill_n(begin(), __n
, __val
), end());
565 template <class _Tp
, class _Alloc
> template <class _InputIter
>
566 void vector
<_Tp
, _Alloc
>::_M_assign_aux(_InputIter __first
, _InputIter __last
,
567 input_iterator_tag
) {
568 iterator
__cur(begin());
569 for ( ; __first
!= __last
&& __cur
!= end(); ++__cur
, ++__first
)
571 if (__first
== __last
)
574 insert(end(), __first
, __last
);
577 template <class _Tp
, class _Alloc
> template <class _ForwardIter
>
579 vector
<_Tp
, _Alloc
>::_M_assign_aux(_ForwardIter __first
, _ForwardIter __last
,
580 forward_iterator_tag
) {
582 distance(__first
, __last
, __len
);
584 if (__len
> capacity()) {
585 pointer
__tmp(_M_allocate_and_copy(__len
, __first
, __last
));
586 _Destroy(_M_start
, _M_finish
);
587 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
589 _M_end_of_storage
= _M_finish
= _M_start
+ __len
;
591 else if (size() >= __len
) {
592 iterator
__new_finish(copy(__first
, __last
, _M_start
));
593 _Destroy(__new_finish
, end());
594 _M_finish
= __new_finish
.base();
597 _ForwardIter __mid
= __first
;
598 advance(__mid
, size());
599 copy(__first
, __mid
, _M_start
);
600 _M_finish
= uninitialized_copy(__mid
, __last
, _M_finish
);
604 template <class _Tp
, class _Alloc
>
606 vector
<_Tp
, _Alloc
>::_M_insert_aux(iterator __position
, const _Tp
& __x
)
608 if (_M_finish
!= _M_end_of_storage
) {
609 _Construct(_M_finish
, *(_M_finish
- 1));
612 copy_backward(__position
, iterator(_M_finish
- 2), iterator(_M_finish
- 1));
613 *__position
= __x_copy
;
616 const size_type __old_size
= size();
617 const size_type __len
= __old_size
!= 0 ? 2 * __old_size
: 1;
618 iterator
__new_start(_M_allocate(__len
));
619 iterator
__new_finish(__new_start
);
621 __new_finish
= uninitialized_copy(iterator(_M_start
), __position
,
623 _Construct(__new_finish
.base(), __x
);
625 __new_finish
= uninitialized_copy(__position
, iterator(_M_finish
),
628 __STL_UNWIND((_Destroy(__new_start
,__new_finish
),
629 _M_deallocate(__new_start
.base(),__len
)));
630 _Destroy(begin(), end());
631 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
632 _M_start
= __new_start
.base();
633 _M_finish
= __new_finish
.base();
634 _M_end_of_storage
= __new_start
.base() + __len
;
638 template <class _Tp
, class _Alloc
>
640 vector
<_Tp
, _Alloc
>::_M_insert_aux(iterator __position
)
642 if (_M_finish
!= _M_end_of_storage
) {
643 _Construct(_M_finish
, *(_M_finish
- 1));
645 copy_backward(__position
, iterator(_M_finish
- 2),
646 iterator(_M_finish
- 1));
650 const size_type __old_size
= size();
651 const size_type __len
= __old_size
!= 0 ? 2 * __old_size
: 1;
652 pointer __new_start
= _M_allocate(__len
);
653 pointer __new_finish
= __new_start
;
655 __new_finish
= uninitialized_copy(iterator(_M_start
), __position
,
657 _Construct(__new_finish
);
659 __new_finish
= uninitialized_copy(__position
, iterator(_M_finish
),
662 __STL_UNWIND((_Destroy(__new_start
,__new_finish
),
663 _M_deallocate(__new_start
,__len
)));
664 _Destroy(begin(), end());
665 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
666 _M_start
= __new_start
;
667 _M_finish
= __new_finish
;
668 _M_end_of_storage
= __new_start
+ __len
;
672 template <class _Tp
, class _Alloc
>
673 void vector
<_Tp
, _Alloc
>::_M_fill_insert(iterator __position
, size_type __n
,
677 if (size_type(_M_end_of_storage
- _M_finish
) >= __n
) {
679 const size_type __elems_after
= end() - __position
;
680 iterator
__old_finish(_M_finish
);
681 if (__elems_after
> __n
) {
682 uninitialized_copy(_M_finish
- __n
, _M_finish
, _M_finish
);
684 copy_backward(__position
, __old_finish
- __n
, __old_finish
);
685 fill(__position
, __position
+ __n
, __x_copy
);
688 uninitialized_fill_n(_M_finish
, __n
- __elems_after
, __x_copy
);
689 _M_finish
+= __n
- __elems_after
;
690 uninitialized_copy(__position
, __old_finish
, _M_finish
);
691 _M_finish
+= __elems_after
;
692 fill(__position
, __old_finish
, __x_copy
);
696 const size_type __old_size
= size();
697 const size_type __len
= __old_size
+ max(__old_size
, __n
);
698 iterator
__new_start(_M_allocate(__len
));
699 iterator
__new_finish(__new_start
);
701 __new_finish
= uninitialized_copy(begin(), __position
, __new_start
);
702 __new_finish
= uninitialized_fill_n(__new_finish
, __n
, __x
);
704 = uninitialized_copy(__position
, end(), __new_finish
);
706 __STL_UNWIND((_Destroy(__new_start
,__new_finish
),
707 _M_deallocate(__new_start
.base(),__len
)));
708 _Destroy(_M_start
, _M_finish
);
709 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
710 _M_start
= __new_start
.base();
711 _M_finish
= __new_finish
.base();
712 _M_end_of_storage
= __new_start
.base() + __len
;
717 template <class _Tp
, class _Alloc
> template <class _InputIterator
>
719 vector
<_Tp
, _Alloc
>::_M_range_insert(iterator __pos
,
720 _InputIterator __first
,
721 _InputIterator __last
,
724 for ( ; __first
!= __last
; ++__first
) {
725 __pos
= insert(__pos
, *__first
);
730 template <class _Tp
, class _Alloc
> template <class _ForwardIterator
>
732 vector
<_Tp
, _Alloc
>::_M_range_insert(iterator __position
,
733 _ForwardIterator __first
,
734 _ForwardIterator __last
,
735 forward_iterator_tag
)
737 if (__first
!= __last
) {
739 distance(__first
, __last
, __n
);
740 if (size_type(_M_end_of_storage
- _M_finish
) >= __n
) {
741 const size_type __elems_after
= end() - __position
;
742 iterator
__old_finish(_M_finish
);
743 if (__elems_after
> __n
) {
744 uninitialized_copy(_M_finish
- __n
, _M_finish
, _M_finish
);
746 copy_backward(__position
, __old_finish
- __n
, __old_finish
);
747 copy(__first
, __last
, __position
);
750 _ForwardIterator __mid
= __first
;
751 advance(__mid
, __elems_after
);
752 uninitialized_copy(__mid
, __last
, _M_finish
);
753 _M_finish
+= __n
- __elems_after
;
754 uninitialized_copy(__position
, __old_finish
, _M_finish
);
755 _M_finish
+= __elems_after
;
756 copy(__first
, __mid
, __position
);
760 const size_type __old_size
= size();
761 const size_type __len
= __old_size
+ max(__old_size
, __n
);
762 iterator
__new_start(_M_allocate(__len
));
763 iterator
__new_finish(__new_start
);
765 __new_finish
= uninitialized_copy(iterator(_M_start
),
766 __position
, __new_start
);
767 __new_finish
= uninitialized_copy(__first
, __last
, __new_finish
);
769 = uninitialized_copy(__position
, iterator(_M_finish
), __new_finish
);
771 __STL_UNWIND((_Destroy(__new_start
,__new_finish
),
772 _M_deallocate(__new_start
.base(),__len
)));
773 _Destroy(_M_start
, _M_finish
);
774 _M_deallocate(_M_start
, _M_end_of_storage
- _M_start
);
775 _M_start
= __new_start
.base();
776 _M_finish
= __new_finish
.base();
777 _M_end_of_storage
= __new_start
.base() + __len
;
784 #endif /* __SGI_STL_INTERNAL_VECTOR_H */