algo.h: Add "GPL plus runtime exception" comment block, this time for real.
[gcc.git] / libstdc++-v3 / include / bits / stl_vector.h
1 // Vector implementation -*- C++ -*-
2
3 // Copyright (C) 2001 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
10
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.
15
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,
19 // USA.
20
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.
29
30 /*
31 *
32 * Copyright (c) 1994
33 * Hewlett-Packard Company
34 *
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.
42 *
43 *
44 * Copyright (c) 1996
45 * Silicon Graphics Computer Systems, Inc.
46 *
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.
54 */
55
56 /* NOTE: This is an internal header file, included by other STL headers.
57 * You should not attempt to use it directly.
58 */
59
60 #ifndef __SGI_STL_INTERNAL_VECTOR_H
61 #define __SGI_STL_INTERNAL_VECTOR_H
62
63 #include <bits/stl_iterator_base_funcs.h>
64 #include <bits/functexcept.h>
65 #include <bits/concept_check.h>
66
67 namespace std
68 {
69
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
74 // allocators.
75
76 // Base class for ordinary allocators.
77 template <class _Tp, class _Allocator, bool _IsStatic>
78 class _Vector_alloc_base {
79 public:
80 typedef typename _Alloc_traits<_Tp, _Allocator>::allocator_type
81 allocator_type;
82 allocator_type get_allocator() const { return _M_data_allocator; }
83
84 _Vector_alloc_base(const allocator_type& __a)
85 : _M_data_allocator(__a), _M_start(0), _M_finish(0), _M_end_of_storage(0)
86 {}
87
88 protected:
89 allocator_type _M_data_allocator;
90 _Tp* _M_start;
91 _Tp* _M_finish;
92 _Tp* _M_end_of_storage;
93
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); }
98 };
99
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> {
104 public:
105 typedef typename _Alloc_traits<_Tp, _Allocator>::allocator_type
106 allocator_type;
107 allocator_type get_allocator() const { return allocator_type(); }
108
109 _Vector_alloc_base(const allocator_type&)
110 : _M_start(0), _M_finish(0), _M_end_of_storage(0)
111 {}
112
113 protected:
114 _Tp* _M_start;
115 _Tp* _M_finish;
116 _Tp* _M_end_of_storage;
117
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);}
123 };
124
125 template <class _Tp, class _Alloc>
126 struct _Vector_base
127 : public _Vector_alloc_base<_Tp, _Alloc,
128 _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
129 {
130 typedef _Vector_alloc_base<_Tp, _Alloc,
131 _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
132 _Base;
133 typedef typename _Base::allocator_type allocator_type;
134
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;
140 }
141
142 ~_Vector_base() { _M_deallocate(_M_start, _M_end_of_storage - _M_start); }
143 };
144
145
146 template <class _Tp, class _Alloc = allocator<_Tp> >
147 class vector : protected _Vector_base<_Tp, _Alloc>
148 {
149 // concept requirements
150 __glibcpp_class_requires(_Tp, _SGIAssignableConcept);
151
152 private:
153 typedef _Vector_base<_Tp, _Alloc> _Base;
154 typedef vector<_Tp, _Alloc> vector_type;
155 public:
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;
165
166 typedef typename _Base::allocator_type allocator_type;
167 allocator_type get_allocator() const { return _Base::get_allocator(); }
168
169 typedef reverse_iterator<const_iterator> const_reverse_iterator;
170 typedef reverse_iterator<iterator> reverse_iterator;
171
172 protected:
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;
178
179 protected:
180 void _M_insert_aux(iterator __position, const _Tp& __x);
181 void _M_insert_aux(iterator __position);
182
183 public:
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); }
189
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()); }
198
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()); }
205 bool empty() const
206 { return begin() == end(); }
207
208 reference operator[](size_type __n) { return *(begin() + __n); }
209 const_reference operator[](size_type __n) const { return *(begin() + __n); }
210
211 void _M_range_check(size_type __n) const {
212 if (__n >= this->size())
213 __throw_out_of_range("vector");
214 }
215
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]; }
220
221 explicit vector(const allocator_type& __a = allocator_type())
222 : _Base(__a) {}
223
224 vector(size_type __n, const _Tp& __value,
225 const allocator_type& __a = allocator_type())
226 : _Base(__n, __a)
227 { _M_finish = uninitialized_fill_n(_M_start, __n, __value); }
228
229 explicit vector(size_type __n)
230 : _Base(__n, allocator_type())
231 { _M_finish = uninitialized_fill_n(_M_start, __n, _Tp()); }
232
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); }
236
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());
243 }
244
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);
250 }
251
252 template <class _InputIterator>
253 void _M_initialize_aux(_InputIterator __first, _InputIterator __last,
254 __false_type) {
255 _M_range_initialize(__first, __last, __iterator_category(__first));
256 }
257
258 ~vector() { destroy(_M_start, _M_finish); }
259
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);
267 _M_start = __tmp;
268 _M_finish = __tmp + __old_size;
269 _M_end_of_storage = _M_start + __n;
270 }
271 }
272
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.
277
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);
280
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());
285 }
286
287 template <class _Integer>
288 void _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
289 { _M_fill_assign((size_type) __n, (_Tp) __val); }
290
291 template <class _InputIter>
292 void _M_assign_dispatch(_InputIter __first, _InputIter __last, __false_type)
293 { _M_assign_aux(__first, __last, __iterator_category(__first)); }
294
295 template <class _InputIterator>
296 void _M_assign_aux(_InputIterator __first, _InputIterator __last,
297 input_iterator_tag);
298
299 template <class _ForwardIterator>
300 void _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
301 forward_iterator_tag);
302
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); }
307
308 void push_back(const _Tp& __x) {
309 if (_M_finish != _M_end_of_storage) {
310 construct(_M_finish, __x);
311 ++_M_finish;
312 }
313 else
314 _M_insert_aux(end(), __x);
315 }
316 void push_back() {
317 if (_M_finish != _M_end_of_storage) {
318 construct(_M_finish);
319 ++_M_finish;
320 }
321 else
322 _M_insert_aux(end());
323 }
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);
328 }
329
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);
334 ++_M_finish;
335 }
336 else
337 _M_insert_aux(iterator(__position), __x);
338 return begin() + __n;
339 }
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);
344 ++_M_finish;
345 }
346 else
347 _M_insert_aux(iterator(__position));
348 return begin() + __n;
349 }
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());
355 }
356
357 template <class _Integer>
358 void _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val,
359 __true_type)
360 { _M_fill_insert(__pos, (size_type) __n, (_Tp) __val); }
361
362 template <class _InputIterator>
363 void _M_insert_dispatch(iterator __pos,
364 _InputIterator __first, _InputIterator __last,
365 __false_type) {
366 _M_range_insert(__pos, __first, __last, __iterator_category(__first));
367 }
368
369 void insert (iterator __pos, size_type __n, const _Tp& __x)
370 { _M_fill_insert(__pos, __n, __x); }
371
372 void _M_fill_insert (iterator __pos, size_type __n, const _Tp& __x);
373
374 void pop_back() {
375 --_M_finish;
376 destroy(_M_finish);
377 }
378 iterator erase(iterator __position) {
379 if (__position + 1 != end())
380 copy(__position + 1, end(), __position);
381 --_M_finish;
382 destroy(_M_finish);
383 return __position;
384 }
385 iterator erase(iterator __first, iterator __last) {
386 iterator __i(copy(__last, end(), __first));
387 destroy(__i, end());
388 _M_finish = _M_finish - (__last - __first);
389 return __first;
390 }
391
392 void resize(size_type __new_size, const _Tp& __x) {
393 if (__new_size < size())
394 erase(begin() + __new_size, end());
395 else
396 insert(end(), __new_size - size(), __x);
397 }
398 void resize(size_type __new_size) { resize(__new_size, _Tp()); }
399 void clear() { erase(begin(), end()); }
400
401 protected:
402
403 template <class _ForwardIterator>
404 pointer _M_allocate_and_copy(size_type __n, _ForwardIterator __first,
405 _ForwardIterator __last)
406 {
407 pointer __result = _M_allocate(__n);
408 __STL_TRY {
409 uninitialized_copy(__first, __last, __result);
410 return __result;
411 }
412 __STL_UNWIND(_M_deallocate(__result, __n));
413 }
414
415 template <class _InputIterator>
416 void _M_range_initialize(_InputIterator __first,
417 _InputIterator __last, input_iterator_tag)
418 {
419 for ( ; __first != __last; ++__first)
420 push_back(*__first);
421 }
422
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)
427 {
428 size_type __n = 0;
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);
433 }
434
435 template <class _InputIterator>
436 void _M_range_insert(iterator __pos,
437 _InputIterator __first, _InputIterator __last,
438 input_iterator_tag);
439
440 template <class _ForwardIterator>
441 void _M_range_insert(iterator __pos,
442 _ForwardIterator __first, _ForwardIterator __last,
443 forward_iterator_tag);
444 };
445
446 template <class _Tp, class _Alloc>
447 inline bool
448 operator==(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
449 {
450 return __x.size() == __y.size() &&
451 equal(__x.begin(), __x.end(), __y.begin());
452 }
453
454 template <class _Tp, class _Alloc>
455 inline bool
456 operator<(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
457 {
458 return lexicographical_compare(__x.begin(), __x.end(),
459 __y.begin(), __y.end());
460 }
461
462 template <class _Tp, class _Alloc>
463 inline void swap(vector<_Tp, _Alloc>& __x, vector<_Tp, _Alloc>& __y)
464 {
465 __x.swap(__y);
466 }
467
468 template <class _Tp, class _Alloc>
469 inline bool
470 operator!=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) {
471 return !(__x == __y);
472 }
473
474 template <class _Tp, class _Alloc>
475 inline bool
476 operator>(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) {
477 return __y < __x;
478 }
479
480 template <class _Tp, class _Alloc>
481 inline bool
482 operator<=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) {
483 return !(__y < __x);
484 }
485
486 template <class _Tp, class _Alloc>
487 inline bool
488 operator>=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) {
489 return !(__x < __y);
490 }
491
492 template <class _Tp, class _Alloc>
493 vector<_Tp,_Alloc>&
494 vector<_Tp,_Alloc>::operator=(const vector<_Tp, _Alloc>& __x)
495 {
496 if (&__x != this) {
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);
502 _M_start = __tmp;
503 _M_end_of_storage = _M_start + __xlen;
504 }
505 else if (size() >= __xlen) {
506 iterator __i(copy(__x.begin(), __x.end(), begin()));
507 destroy(__i, end());
508 }
509 else {
510 copy(__x.begin(), __x.begin() + size(), _M_start);
511 uninitialized_copy(__x.begin() + size(), __x.end(), _M_finish);
512 }
513 _M_finish = _M_start + __xlen;
514 }
515 return *this;
516 }
517
518 template <class _Tp, class _Alloc>
519 void vector<_Tp, _Alloc>::_M_fill_assign(size_t __n, const value_type& __val)
520 {
521 if (__n > capacity()) {
522 vector<_Tp, _Alloc> __tmp(__n, __val, get_allocator());
523 __tmp.swap(*this);
524 }
525 else if (__n > size()) {
526 fill(begin(), end(), __val);
527 _M_finish = uninitialized_fill_n(_M_finish, __n - size(), __val);
528 }
529 else
530 erase(fill_n(begin(), __n, __val), end());
531 }
532
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)
538 *__cur = *__first;
539 if (__first == __last)
540 erase(__cur, end());
541 else
542 insert(end(), __first, __last);
543 }
544
545 template <class _Tp, class _Alloc> template <class _ForwardIter>
546 void
547 vector<_Tp, _Alloc>::_M_assign_aux(_ForwardIter __first, _ForwardIter __last,
548 forward_iterator_tag) {
549 size_type __len = 0;
550 distance(__first, __last, __len);
551
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);
556 _M_start = __tmp;
557 _M_end_of_storage = _M_finish = _M_start + __len;
558 }
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();
563 }
564 else {
565 _ForwardIter __mid = __first;
566 advance(__mid, size());
567 copy(__first, __mid, _M_start);
568 _M_finish = uninitialized_copy(__mid, __last, _M_finish);
569 }
570 }
571
572 template <class _Tp, class _Alloc>
573 void
574 vector<_Tp, _Alloc>::_M_insert_aux(iterator __position, const _Tp& __x)
575 {
576 if (_M_finish != _M_end_of_storage) {
577 construct(_M_finish, *(_M_finish - 1));
578 ++_M_finish;
579 _Tp __x_copy = __x;
580 copy_backward(__position, iterator(_M_finish - 2), iterator(_M_finish- 1));
581 *__position = __x_copy;
582 }
583 else {
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);
588 __STL_TRY {
589 __new_finish = uninitialized_copy(iterator(_M_start), __position,
590 __new_start);
591 construct(__new_finish.base(), __x);
592 ++__new_finish;
593 __new_finish = uninitialized_copy(__position, iterator(_M_finish),
594 __new_finish);
595 }
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;
603 }
604 }
605
606 template <class _Tp, class _Alloc>
607 void
608 vector<_Tp, _Alloc>::_M_insert_aux(iterator __position)
609 {
610 if (_M_finish != _M_end_of_storage) {
611 construct(_M_finish, *(_M_finish - 1));
612 ++_M_finish;
613 copy_backward(__position, iterator(_M_finish - 2),
614 iterator(_M_finish - 1));
615 *__position = _Tp();
616 }
617 else {
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;
622 __STL_TRY {
623 __new_finish = uninitialized_copy(iterator(_M_start), __position,
624 __new_start);
625 construct(__new_finish);
626 ++__new_finish;
627 __new_finish = uninitialized_copy(__position, iterator(_M_finish),
628 __new_finish);
629 }
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;
637 }
638 }
639
640 template <class _Tp, class _Alloc>
641 void vector<_Tp, _Alloc>::_M_fill_insert(iterator __position, size_type __n,
642 const _Tp& __x)
643 {
644 if (__n != 0) {
645 if (size_type(_M_end_of_storage - _M_finish) >= __n) {
646 _Tp __x_copy = __x;
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);
651 _M_finish += __n;
652 copy_backward(__position, __old_finish - __n, __old_finish);
653 fill(__position, __position + __n, __x_copy);
654 }
655 else {
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);
661 }
662 }
663 else {
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);
668 __STL_TRY {
669 __new_finish = uninitialized_copy(begin(), __position, __new_start);
670 __new_finish = uninitialized_fill_n(__new_finish, __n, __x);
671 __new_finish
672 = uninitialized_copy(__position, end(), __new_finish);
673 }
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;
681 }
682 }
683 }
684
685 template <class _Tp, class _Alloc> template <class _InputIterator>
686 void
687 vector<_Tp, _Alloc>::_M_range_insert(iterator __pos,
688 _InputIterator __first,
689 _InputIterator __last,
690 input_iterator_tag)
691 {
692 for ( ; __first != __last; ++__first) {
693 __pos = insert(__pos, *__first);
694 ++__pos;
695 }
696 }
697
698 template <class _Tp, class _Alloc> template <class _ForwardIterator>
699 void
700 vector<_Tp, _Alloc>::_M_range_insert(iterator __position,
701 _ForwardIterator __first,
702 _ForwardIterator __last,
703 forward_iterator_tag)
704 {
705 if (__first != __last) {
706 size_type __n = 0;
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);
713 _M_finish += __n;
714 copy_backward(__position, __old_finish - __n, __old_finish);
715 copy(__first, __last, __position);
716 }
717 else {
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);
725 }
726 }
727 else {
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);
732 __STL_TRY {
733 __new_finish = uninitialized_copy(iterator(_M_start),
734 __position, __new_start);
735 __new_finish = uninitialized_copy(__first, __last, __new_finish);
736 __new_finish
737 = uninitialized_copy(__position, iterator(_M_finish), __new_finish);
738 }
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;
746 }
747 }
748 }
749
750 } // namespace std
751
752 #endif /* __SGI_STL_INTERNAL_VECTOR_H */
753
754 // Local Variables:
755 // mode:C++
756 // End: