]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/vector.tcc
Daily bump.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / vector.tcc
CommitLineData
83144cfc
PE
1// Vector implementation (out of line) -*- C++ -*-
2
6323b34e 3// Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
83144cfc
PE
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
83f51799 18// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
83144cfc
PE
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/** @file vector.tcc
57 * This is an internal header file, included by other library headers.
58 * You should not attempt to use it directly.
59 */
60
3d7c150e
BK
61#ifndef _VECTOR_TCC
62#define _VECTOR_TCC 1
83144cfc 63
390e4c0d 64namespace _GLIBCXX_STD
83144cfc 65{
af5fb6ab 66 template<typename _Tp, typename _Alloc>
3971a4d2 67 void
874e7baa 68 vector<_Tp, _Alloc>::
3971a4d2 69 reserve(size_type __n)
83144cfc 70 {
48d1c3c5 71 if (__n > this->max_size())
ba9119ec 72 __throw_length_error(__N("vector::reserve"));
48d1c3c5
BK
73 if (this->capacity() < __n)
74 {
75 const size_type __old_size = size();
bc9053ab 76 pointer __tmp = _M_allocate_and_copy(__n, this->_M_impl._M_start,
03f9ea44 77 this->_M_impl._M_finish);
1985f1cd 78 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
4fd20a8f 79 _M_get_Tp_allocator());
03f9ea44 80 _M_deallocate(this->_M_impl._M_start,
874e7baa
PC
81 this->_M_impl._M_end_of_storage
82 - this->_M_impl._M_start);
03f9ea44
DM
83 this->_M_impl._M_start = __tmp;
84 this->_M_impl._M_finish = __tmp + __old_size;
85 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
48d1c3c5 86 }
3971a4d2 87 }
ed6814f7 88
af5fb6ab 89 template<typename _Tp, typename _Alloc>
874e7baa
PC
90 typename vector<_Tp, _Alloc>::iterator
91 vector<_Tp, _Alloc>::
3971a4d2
PE
92 insert(iterator __position, const value_type& __x)
93 {
43da93a7 94 const size_type __n = __position - begin();
874e7baa
PC
95 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage
96 && __position == end())
97 {
1985f1cd 98 this->_M_impl.construct(this->_M_impl._M_finish, __x);
874e7baa
PC
99 ++this->_M_impl._M_finish;
100 }
83144cfc 101 else
3971a4d2 102 _M_insert_aux(__position, __x);
bc9053ab 103 return iterator(this->_M_impl._M_start + __n);
83144cfc 104 }
ed6814f7 105
af5fb6ab 106 template<typename _Tp, typename _Alloc>
874e7baa
PC
107 typename vector<_Tp, _Alloc>::iterator
108 vector<_Tp, _Alloc>::
3971a4d2 109 erase(iterator __position)
83144cfc 110 {
3971a4d2 111 if (__position + 1 != end())
2f805868 112 std::copy(__position + 1, end(), __position);
03f9ea44 113 --this->_M_impl._M_finish;
1985f1cd 114 this->_M_impl.destroy(this->_M_impl._M_finish);
3971a4d2 115 return __position;
83144cfc 116 }
ed6814f7 117
af5fb6ab 118 template<typename _Tp, typename _Alloc>
874e7baa
PC
119 typename vector<_Tp, _Alloc>::iterator
120 vector<_Tp, _Alloc>::
3971a4d2 121 erase(iterator __first, iterator __last)
83144cfc 122 {
bc9053ab
PC
123 if (__last != end())
124 std::copy(__last, end(), __first);
125 _M_erase_at_end(__first.base() + (end() - __last));
3971a4d2 126 return __first;
83144cfc 127 }
ed6814f7 128
af5fb6ab 129 template<typename _Tp, typename _Alloc>
874e7baa
PC
130 vector<_Tp, _Alloc>&
131 vector<_Tp, _Alloc>::
132 operator=(const vector<_Tp, _Alloc>& __x)
83144cfc 133 {
3971a4d2 134 if (&__x != this)
874e7baa
PC
135 {
136 const size_type __xlen = __x.size();
137 if (__xlen > capacity())
138 {
139 pointer __tmp = _M_allocate_and_copy(__xlen, __x.begin(),
140 __x.end());
1985f1cd 141 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
4fd20a8f 142 _M_get_Tp_allocator());
874e7baa
PC
143 _M_deallocate(this->_M_impl._M_start,
144 this->_M_impl._M_end_of_storage
145 - this->_M_impl._M_start);
146 this->_M_impl._M_start = __tmp;
147 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __xlen;
148 }
149 else if (size() >= __xlen)
150 {
bc9053ab
PC
151 std::_Destroy(std::copy(__x.begin(), __x.end(), begin()),
152 end(), _M_get_Tp_allocator());
874e7baa
PC
153 }
154 else
155 {
bc9053ab 156 std::copy(__x._M_impl._M_start, __x._M_impl._M_start + size(),
874e7baa 157 this->_M_impl._M_start);
bc9053ab
PC
158 std::__uninitialized_copy_a(__x._M_impl._M_start + size(),
159 __x._M_impl._M_finish,
160 this->_M_impl._M_finish,
4fd20a8f 161 _M_get_Tp_allocator());
874e7baa
PC
162 }
163 this->_M_impl._M_finish = this->_M_impl._M_start + __xlen;
164 }
3971a4d2 165 return *this;
83144cfc 166 }
ed6814f7 167
af5fb6ab 168 template<typename _Tp, typename _Alloc>
3971a4d2 169 void
874e7baa 170 vector<_Tp, _Alloc>::
3971a4d2 171 _M_fill_assign(size_t __n, const value_type& __val)
83144cfc 172 {
3971a4d2 173 if (__n > capacity())
874e7baa 174 {
4fd20a8f 175 vector __tmp(__n, __val, _M_get_Tp_allocator());
874e7baa
PC
176 __tmp.swap(*this);
177 }
3971a4d2 178 else if (__n > size())
874e7baa
PC
179 {
180 std::fill(begin(), end(), __val);
1985f1cd
MA
181 std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
182 __n - size(), __val,
4fd20a8f 183 _M_get_Tp_allocator());
368b7a30 184 this->_M_impl._M_finish += __n - size();
874e7baa 185 }
3971a4d2 186 else
bc9053ab 187 _M_erase_at_end(std::fill_n(this->_M_impl._M_start, __n, __val));
83144cfc 188 }
ed6814f7 189
874e7baa
PC
190 template<typename _Tp, typename _Alloc>
191 template<typename _InputIterator>
192 void
193 vector<_Tp, _Alloc>::
194 _M_assign_aux(_InputIterator __first, _InputIterator __last,
6323b34e 195 std::input_iterator_tag)
3971a4d2 196 {
bc9053ab
PC
197 pointer __cur(this->_M_impl._M_start);
198 for (; __first != __last && __cur != this->_M_impl._M_finish;
199 ++__cur, ++__first)
874e7baa
PC
200 *__cur = *__first;
201 if (__first == __last)
bc9053ab 202 _M_erase_at_end(__cur);
874e7baa
PC
203 else
204 insert(end(), __first, __last);
3971a4d2 205 }
874e7baa
PC
206
207 template<typename _Tp, typename _Alloc>
208 template<typename _ForwardIterator>
209 void
43da93a7 210 vector<_Tp, _Alloc>::
874e7baa 211 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
6323b34e 212 std::forward_iterator_tag)
3971a4d2 213 {
43da93a7 214 const size_type __len = std::distance(__first, __last);
874e7baa
PC
215
216 if (__len > capacity())
217 {
218 pointer __tmp(_M_allocate_and_copy(__len, __first, __last));
1985f1cd 219 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
4fd20a8f 220 _M_get_Tp_allocator());
874e7baa
PC
221 _M_deallocate(this->_M_impl._M_start,
222 this->_M_impl._M_end_of_storage
223 - this->_M_impl._M_start);
224 this->_M_impl._M_start = __tmp;
225 this->_M_impl._M_finish = this->_M_impl._M_start + __len;
226 this->_M_impl._M_end_of_storage = this->_M_impl._M_finish;
227 }
228 else if (size() >= __len)
bc9053ab 229 _M_erase_at_end(std::copy(__first, __last, this->_M_impl._M_start));
874e7baa
PC
230 else
231 {
232 _ForwardIterator __mid = __first;
233 std::advance(__mid, size());
234 std::copy(__first, __mid, this->_M_impl._M_start);
1985f1cd
MA
235 this->_M_impl._M_finish =
236 std::__uninitialized_copy_a(__mid, __last,
237 this->_M_impl._M_finish,
4fd20a8f 238 _M_get_Tp_allocator());
874e7baa 239 }
3971a4d2 240 }
ed6814f7 241
af5fb6ab 242 template<typename _Tp, typename _Alloc>
3971a4d2 243 void
43da93a7 244 vector<_Tp, _Alloc>::
3971a4d2
PE
245 _M_insert_aux(iterator __position, const _Tp& __x)
246 {
03f9ea44 247 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
874e7baa 248 {
1985f1cd
MA
249 this->_M_impl.construct(this->_M_impl._M_finish,
250 *(this->_M_impl._M_finish - 1));
874e7baa
PC
251 ++this->_M_impl._M_finish;
252 _Tp __x_copy = __x;
bc9053ab
PC
253 std::copy_backward(__position.base(),
254 this->_M_impl._M_finish - 2,
255 this->_M_impl._M_finish - 1);
874e7baa
PC
256 *__position = __x_copy;
257 }
83144cfc 258 else
874e7baa
PC
259 {
260 const size_type __old_size = size();
996e5395
PC
261 if (__old_size == this->max_size())
262 __throw_length_error(__N("vector::_M_insert_aux"));
263
264 // When sizeof(value_type) == 1 and __old_size > size_type(-1)/2
265 // __len overflows: if we don't notice and _M_allocate doesn't
266 // throw we crash badly later.
267 size_type __len = __old_size != 0 ? 2 * __old_size : 1;
268 if (__len < __old_size)
269 __len = this->max_size();
270
bc9053ab
PC
271 pointer __new_start(this->_M_allocate(__len));
272 pointer __new_finish(__new_start);
874e7baa
PC
273 try
274 {
1985f1cd 275 __new_finish =
bc9053ab
PC
276 std::__uninitialized_copy_a(this->_M_impl._M_start,
277 __position.base(), __new_start,
4fd20a8f 278 _M_get_Tp_allocator());
bc9053ab 279 this->_M_impl.construct(__new_finish, __x);
874e7baa 280 ++__new_finish;
1985f1cd 281 __new_finish =
bc9053ab
PC
282 std::__uninitialized_copy_a(__position.base(),
283 this->_M_impl._M_finish,
1985f1cd 284 __new_finish,
4fd20a8f 285 _M_get_Tp_allocator());
996e5395 286 }
874e7baa
PC
287 catch(...)
288 {
4fd20a8f 289 std::_Destroy(__new_start, __new_finish, _M_get_Tp_allocator());
bc9053ab 290 _M_deallocate(__new_start, __len);
874e7baa
PC
291 __throw_exception_again;
292 }
bc9053ab
PC
293 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
294 _M_get_Tp_allocator());
874e7baa
PC
295 _M_deallocate(this->_M_impl._M_start,
296 this->_M_impl._M_end_of_storage
297 - this->_M_impl._M_start);
bc9053ab
PC
298 this->_M_impl._M_start = __new_start;
299 this->_M_impl._M_finish = __new_finish;
300 this->_M_impl._M_end_of_storage = __new_start + __len;
874e7baa 301 }
83144cfc 302 }
64ebadac 303
af5fb6ab 304 template<typename _Tp, typename _Alloc>
3971a4d2 305 void
43da93a7 306 vector<_Tp, _Alloc>::
3971a4d2
PE
307 _M_fill_insert(iterator __position, size_type __n, const value_type& __x)
308 {
309 if (__n != 0)
874e7baa
PC
310 {
311 if (size_type(this->_M_impl._M_end_of_storage
312 - this->_M_impl._M_finish) >= __n)
313 {
314 value_type __x_copy = __x;
315 const size_type __elems_after = end() - __position;
bc9053ab 316 pointer __old_finish(this->_M_impl._M_finish);
874e7baa
PC
317 if (__elems_after > __n)
318 {
1985f1cd
MA
319 std::__uninitialized_copy_a(this->_M_impl._M_finish - __n,
320 this->_M_impl._M_finish,
321 this->_M_impl._M_finish,
4fd20a8f 322 _M_get_Tp_allocator());
874e7baa 323 this->_M_impl._M_finish += __n;
bc9053ab 324 std::copy_backward(__position.base(), __old_finish - __n,
874e7baa 325 __old_finish);
bc9053ab
PC
326 std::fill(__position.base(), __position.base() + __n,
327 __x_copy);
874e7baa
PC
328 }
329 else
330 {
1985f1cd
MA
331 std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
332 __n - __elems_after,
333 __x_copy,
4fd20a8f 334 _M_get_Tp_allocator());
874e7baa 335 this->_M_impl._M_finish += __n - __elems_after;
bc9053ab 336 std::__uninitialized_copy_a(__position.base(), __old_finish,
1985f1cd 337 this->_M_impl._M_finish,
4fd20a8f 338 _M_get_Tp_allocator());
874e7baa 339 this->_M_impl._M_finish += __elems_after;
bc9053ab 340 std::fill(__position.base(), __old_finish, __x_copy);
874e7baa
PC
341 }
342 }
343 else
344 {
345 const size_type __old_size = size();
996e5395
PC
346 if (this->max_size() - __old_size < __n)
347 __throw_length_error(__N("vector::_M_fill_insert"));
348
349 // See _M_insert_aux above.
350 size_type __len = __old_size + std::max(__old_size, __n);
351 if (__len < __old_size)
352 __len = this->max_size();
353
bc9053ab
PC
354 pointer __new_start(this->_M_allocate(__len));
355 pointer __new_finish(__new_start);
874e7baa
PC
356 try
357 {
1985f1cd 358 __new_finish =
bc9053ab
PC
359 std::__uninitialized_copy_a(this->_M_impl._M_start,
360 __position.base(),
1985f1cd 361 __new_start,
4fd20a8f 362 _M_get_Tp_allocator());
1985f1cd 363 std::__uninitialized_fill_n_a(__new_finish, __n, __x,
4fd20a8f 364 _M_get_Tp_allocator());
368b7a30 365 __new_finish += __n;
1985f1cd 366 __new_finish =
bc9053ab
PC
367 std::__uninitialized_copy_a(__position.base(),
368 this->_M_impl._M_finish,
369 __new_finish,
4fd20a8f 370 _M_get_Tp_allocator());
874e7baa
PC
371 }
372 catch(...)
373 {
1985f1cd 374 std::_Destroy(__new_start, __new_finish,
4fd20a8f 375 _M_get_Tp_allocator());
bc9053ab 376 _M_deallocate(__new_start, __len);
874e7baa
PC
377 __throw_exception_again;
378 }
1985f1cd 379 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
4fd20a8f 380 _M_get_Tp_allocator());
874e7baa
PC
381 _M_deallocate(this->_M_impl._M_start,
382 this->_M_impl._M_end_of_storage
383 - this->_M_impl._M_start);
bc9053ab
PC
384 this->_M_impl._M_start = __new_start;
385 this->_M_impl._M_finish = __new_finish;
386 this->_M_impl._M_end_of_storage = __new_start + __len;
874e7baa
PC
387 }
388 }
3971a4d2 389 }
ed6814f7 390
af5fb6ab 391 template<typename _Tp, typename _Alloc> template<typename _InputIterator>
3971a4d2 392 void
43da93a7 393 vector<_Tp, _Alloc>::
874e7baa 394 _M_range_insert(iterator __pos, _InputIterator __first,
6323b34e 395 _InputIterator __last, std::input_iterator_tag)
3971a4d2 396 {
43da93a7 397 for (; __first != __last; ++__first)
874e7baa
PC
398 {
399 __pos = insert(__pos, *__first);
400 ++__pos;
401 }
3971a4d2 402 }
ed6814f7 403
874e7baa
PC
404 template<typename _Tp, typename _Alloc>
405 template<typename _ForwardIterator>
406 void
43da93a7 407 vector<_Tp, _Alloc>::
996e5395 408 _M_range_insert(iterator __position, _ForwardIterator __first,
6323b34e 409 _ForwardIterator __last, std::forward_iterator_tag)
3971a4d2 410 {
874e7baa
PC
411 if (__first != __last)
412 {
43da93a7 413 const size_type __n = std::distance(__first, __last);
874e7baa
PC
414 if (size_type(this->_M_impl._M_end_of_storage
415 - this->_M_impl._M_finish) >= __n)
416 {
417 const size_type __elems_after = end() - __position;
bc9053ab 418 pointer __old_finish(this->_M_impl._M_finish);
874e7baa
PC
419 if (__elems_after > __n)
420 {
1985f1cd
MA
421 std::__uninitialized_copy_a(this->_M_impl._M_finish - __n,
422 this->_M_impl._M_finish,
423 this->_M_impl._M_finish,
4fd20a8f 424 _M_get_Tp_allocator());
874e7baa 425 this->_M_impl._M_finish += __n;
bc9053ab 426 std::copy_backward(__position.base(), __old_finish - __n,
874e7baa
PC
427 __old_finish);
428 std::copy(__first, __last, __position);
429 }
430 else
431 {
432 _ForwardIterator __mid = __first;
433 std::advance(__mid, __elems_after);
1985f1cd
MA
434 std::__uninitialized_copy_a(__mid, __last,
435 this->_M_impl._M_finish,
4fd20a8f 436 _M_get_Tp_allocator());
874e7baa 437 this->_M_impl._M_finish += __n - __elems_after;
bc9053ab
PC
438 std::__uninitialized_copy_a(__position.base(),
439 __old_finish,
1985f1cd 440 this->_M_impl._M_finish,
4fd20a8f 441 _M_get_Tp_allocator());
874e7baa
PC
442 this->_M_impl._M_finish += __elems_after;
443 std::copy(__first, __mid, __position);
444 }
445 }
446 else
447 {
448 const size_type __old_size = size();
996e5395
PC
449 if (this->max_size() - __old_size < __n)
450 __throw_length_error(__N("vector::_M_range_insert"));
451
452 // See _M_insert_aux above.
453 size_type __len = __old_size + std::max(__old_size, __n);
454 if (__len < __old_size)
455 __len = this->max_size();
456
bc9053ab
PC
457 pointer __new_start(this->_M_allocate(__len));
458 pointer __new_finish(__new_start);
874e7baa
PC
459 try
460 {
1985f1cd 461 __new_finish =
bc9053ab
PC
462 std::__uninitialized_copy_a(this->_M_impl._M_start,
463 __position.base(),
1985f1cd 464 __new_start,
4fd20a8f 465 _M_get_Tp_allocator());
1985f1cd
MA
466 __new_finish =
467 std::__uninitialized_copy_a(__first, __last, __new_finish,
4fd20a8f 468 _M_get_Tp_allocator());
1985f1cd 469 __new_finish =
bc9053ab
PC
470 std::__uninitialized_copy_a(__position.base(),
471 this->_M_impl._M_finish,
1985f1cd 472 __new_finish,
4fd20a8f 473 _M_get_Tp_allocator());
874e7baa
PC
474 }
475 catch(...)
476 {
bc9053ab 477 std::_Destroy(__new_start, __new_finish,
4fd20a8f 478 _M_get_Tp_allocator());
bc9053ab 479 _M_deallocate(__new_start, __len);
874e7baa
PC
480 __throw_exception_again;
481 }
1985f1cd 482 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
4fd20a8f 483 _M_get_Tp_allocator());
874e7baa
PC
484 _M_deallocate(this->_M_impl._M_start,
485 this->_M_impl._M_end_of_storage
486 - this->_M_impl._M_start);
bc9053ab
PC
487 this->_M_impl._M_start = __new_start;
488 this->_M_impl._M_finish = __new_finish;
489 this->_M_impl._M_end_of_storage = __new_start + __len;
874e7baa
PC
490 }
491 }
83144cfc 492 }
390e4c0d 493} // namespace std
83144cfc 494
3d7c150e 495#endif /* _VECTOR_TCC */