1 // Vector implementation (out of line) -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004 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.
57 * This is an internal header file, included by other library headers.
58 * You should not attempt to use it directly.
64 namespace _GLIBCXX_STD
66 template<typename _Tp, typename _Alloc>
69 reserve(size_type __n)
71 if (__n > this->max_size())
72 __throw_length_error(__N("vector::reserve"));
73 if (this->capacity() < __n)
75 const size_type __old_size = size();
76 pointer __tmp = _M_allocate_and_copy(__n,
77 this->_M_impl._M_start,
78 this->_M_impl._M_finish);
79 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
80 this->get_allocator());
81 _M_deallocate(this->_M_impl._M_start,
82 this->_M_impl._M_end_of_storage
83 - this->_M_impl._M_start);
84 this->_M_impl._M_start = __tmp;
85 this->_M_impl._M_finish = __tmp + __old_size;
86 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
90 template<typename _Tp, typename _Alloc>
91 typename vector<_Tp, _Alloc>::iterator
93 insert(iterator __position, const value_type& __x)
95 const size_type __n = __position - begin();
96 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage
97 && __position == end())
99 this->_M_impl.construct(this->_M_impl._M_finish, __x);
100 ++this->_M_impl._M_finish;
103 _M_insert_aux(__position, __x);
104 return begin() + __n;
107 template<typename _Tp, typename _Alloc>
108 typename vector<_Tp, _Alloc>::iterator
109 vector<_Tp, _Alloc>::
110 erase(iterator __position)
112 if (__position + 1 != end())
113 std::copy(__position + 1, end(), __position);
114 --this->_M_impl._M_finish;
115 this->_M_impl.destroy(this->_M_impl._M_finish);
119 template<typename _Tp, typename _Alloc>
120 typename vector<_Tp, _Alloc>::iterator
121 vector<_Tp, _Alloc>::
122 erase(iterator __first, iterator __last)
124 iterator __i(copy(__last, end(), __first));
125 std::_Destroy(__i, end(), this->get_allocator());
126 this->_M_impl._M_finish = this->_M_impl._M_finish - (__last - __first);
130 template<typename _Tp, typename _Alloc>
132 vector<_Tp, _Alloc>::
133 operator=(const vector<_Tp, _Alloc>& __x)
137 const size_type __xlen = __x.size();
138 if (__xlen > capacity())
140 pointer __tmp = _M_allocate_and_copy(__xlen, __x.begin(),
142 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
143 this->get_allocator());
144 _M_deallocate(this->_M_impl._M_start,
145 this->_M_impl._M_end_of_storage
146 - this->_M_impl._M_start);
147 this->_M_impl._M_start = __tmp;
148 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __xlen;
150 else if (size() >= __xlen)
152 iterator __i(copy(__x.begin(), __x.end(), begin()));
153 std::_Destroy(__i, end(), this->get_allocator());
157 std::copy(__x.begin(), __x.begin() + size(),
158 this->_M_impl._M_start);
159 std::__uninitialized_copy_a(__x.begin() + size(),
160 __x.end(), this->_M_impl._M_finish,
161 this->get_allocator());
163 this->_M_impl._M_finish = this->_M_impl._M_start + __xlen;
168 template<typename _Tp, typename _Alloc>
170 vector<_Tp, _Alloc>::
171 _M_fill_assign(size_t __n, const value_type& __val)
173 if (__n > capacity())
175 vector __tmp(__n, __val, get_allocator());
178 else if (__n > size())
180 std::fill(begin(), end(), __val);
181 std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
183 this->get_allocator());
184 this->_M_impl._M_finish += __n - size();
187 erase(fill_n(begin(), __n, __val), end());
190 template<typename _Tp, typename _Alloc>
191 template<typename _InputIterator>
193 vector<_Tp, _Alloc>::
194 _M_assign_aux(_InputIterator __first, _InputIterator __last,
197 iterator __cur(begin());
198 for (; __first != __last && __cur != end(); ++__cur, ++__first)
200 if (__first == __last)
203 insert(end(), __first, __last);
206 template<typename _Tp, typename _Alloc>
207 template<typename _ForwardIterator>
209 vector<_Tp, _Alloc>::
210 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
211 forward_iterator_tag)
213 const size_type __len = std::distance(__first, __last);
215 if (__len > capacity())
217 pointer __tmp(_M_allocate_and_copy(__len, __first, __last));
218 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
219 this->get_allocator());
220 _M_deallocate(this->_M_impl._M_start,
221 this->_M_impl._M_end_of_storage
222 - this->_M_impl._M_start);
223 this->_M_impl._M_start = __tmp;
224 this->_M_impl._M_finish = this->_M_impl._M_start + __len;
225 this->_M_impl._M_end_of_storage = this->_M_impl._M_finish;
227 else if (size() >= __len)
229 iterator __new_finish(copy(__first, __last,
230 this->_M_impl._M_start));
231 std::_Destroy(__new_finish, end(), this->get_allocator());
232 this->_M_impl._M_finish = __new_finish.base();
236 _ForwardIterator __mid = __first;
237 std::advance(__mid, size());
238 std::copy(__first, __mid, this->_M_impl._M_start);
239 this->_M_impl._M_finish =
240 std::__uninitialized_copy_a(__mid, __last,
241 this->_M_impl._M_finish,
242 this->get_allocator());
246 template<typename _Tp, typename _Alloc>
248 vector<_Tp, _Alloc>::
249 _M_insert_aux(iterator __position, const _Tp& __x)
251 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
253 this->_M_impl.construct(this->_M_impl._M_finish,
254 *(this->_M_impl._M_finish - 1));
255 ++this->_M_impl._M_finish;
257 std::copy_backward(__position,
258 iterator(this->_M_impl._M_finish-2),
259 iterator(this->_M_impl._M_finish-1));
260 *__position = __x_copy;
264 const size_type __old_size = size();
265 const size_type __len = __old_size != 0 ? 2 * __old_size : 1;
266 iterator __new_start(this->_M_allocate(__len));
267 iterator __new_finish(__new_start);
271 std::__uninitialized_copy_a(iterator(this->_M_impl._M_start),
274 this->get_allocator());
275 this->_M_impl.construct(__new_finish.base(), __x);
278 std::__uninitialized_copy_a(__position,
279 iterator(this->_M_impl._M_finish),
281 this->get_allocator());
285 std::_Destroy(__new_start, __new_finish, this->get_allocator());
286 _M_deallocate(__new_start.base(),__len);
287 __throw_exception_again;
289 std::_Destroy(begin(), end(), this->get_allocator());
290 _M_deallocate(this->_M_impl._M_start,
291 this->_M_impl._M_end_of_storage
292 - this->_M_impl._M_start);
293 this->_M_impl._M_start = __new_start.base();
294 this->_M_impl._M_finish = __new_finish.base();
295 this->_M_impl._M_end_of_storage = __new_start.base() + __len;
299 template<typename _Tp, typename _Alloc>
301 vector<_Tp, _Alloc>::
302 _M_fill_insert(iterator __position, size_type __n, const value_type& __x)
306 if (size_type(this->_M_impl._M_end_of_storage
307 - this->_M_impl._M_finish) >= __n)
309 value_type __x_copy = __x;
310 const size_type __elems_after = end() - __position;
311 iterator __old_finish(this->_M_impl._M_finish);
312 if (__elems_after > __n)
314 std::__uninitialized_copy_a(this->_M_impl._M_finish - __n,
315 this->_M_impl._M_finish,
316 this->_M_impl._M_finish,
317 this->get_allocator());
318 this->_M_impl._M_finish += __n;
319 std::copy_backward(__position, __old_finish - __n,
321 std::fill(__position, __position + __n, __x_copy);
325 std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
328 this->get_allocator());
329 this->_M_impl._M_finish += __n - __elems_after;
330 std::__uninitialized_copy_a(__position, __old_finish,
331 this->_M_impl._M_finish,
332 this->get_allocator());
333 this->_M_impl._M_finish += __elems_after;
334 std::fill(__position, __old_finish, __x_copy);
339 const size_type __old_size = size();
340 const size_type __len = __old_size + std::max(__old_size, __n);
341 iterator __new_start(this->_M_allocate(__len));
342 iterator __new_finish(__new_start);
346 std::__uninitialized_copy_a(begin(), __position,
348 this->get_allocator());
349 std::__uninitialized_fill_n_a(__new_finish, __n, __x,
350 this->get_allocator());
353 std::__uninitialized_copy_a(__position, end(), __new_finish,
354 this->get_allocator());
358 std::_Destroy(__new_start, __new_finish,
359 this->get_allocator());
360 _M_deallocate(__new_start.base(), __len);
361 __throw_exception_again;
363 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
364 this->get_allocator());
365 _M_deallocate(this->_M_impl._M_start,
366 this->_M_impl._M_end_of_storage
367 - this->_M_impl._M_start);
368 this->_M_impl._M_start = __new_start.base();
369 this->_M_impl._M_finish = __new_finish.base();
370 this->_M_impl._M_end_of_storage = __new_start.base() + __len;
375 template<typename _Tp, typename _Alloc> template<typename _InputIterator>
377 vector<_Tp, _Alloc>::
378 _M_range_insert(iterator __pos, _InputIterator __first,
379 _InputIterator __last, input_iterator_tag)
381 for (; __first != __last; ++__first)
383 __pos = insert(__pos, *__first);
388 template<typename _Tp, typename _Alloc>
389 template<typename _ForwardIterator>
391 vector<_Tp, _Alloc>::
392 _M_range_insert(iterator __position,_ForwardIterator __first,
393 _ForwardIterator __last, forward_iterator_tag)
395 if (__first != __last)
397 const size_type __n = std::distance(__first, __last);
398 if (size_type(this->_M_impl._M_end_of_storage
399 - this->_M_impl._M_finish) >= __n)
401 const size_type __elems_after = end() - __position;
402 iterator __old_finish(this->_M_impl._M_finish);
403 if (__elems_after > __n)
405 std::__uninitialized_copy_a(this->_M_impl._M_finish - __n,
406 this->_M_impl._M_finish,
407 this->_M_impl._M_finish,
408 this->get_allocator());
409 this->_M_impl._M_finish += __n;
410 std::copy_backward(__position, __old_finish - __n,
412 std::copy(__first, __last, __position);
416 _ForwardIterator __mid = __first;
417 std::advance(__mid, __elems_after);
418 std::__uninitialized_copy_a(__mid, __last,
419 this->_M_impl._M_finish,
420 this->get_allocator());
421 this->_M_impl._M_finish += __n - __elems_after;
422 std::__uninitialized_copy_a(__position, __old_finish,
423 this->_M_impl._M_finish,
424 this->get_allocator());
425 this->_M_impl._M_finish += __elems_after;
426 std::copy(__first, __mid, __position);
431 const size_type __old_size = size();
432 const size_type __len = __old_size + std::max(__old_size, __n);
433 iterator __new_start(this->_M_allocate(__len));
434 iterator __new_finish(__new_start);
438 std::__uninitialized_copy_a(iterator(this->_M_impl._M_start),
441 this->get_allocator());
443 std::__uninitialized_copy_a(__first, __last, __new_finish,
444 this->get_allocator());
446 std::__uninitialized_copy_a(__position,
447 iterator(this->_M_impl._M_finish),
449 this->get_allocator());
453 std::_Destroy(__new_start,__new_finish,
454 this->get_allocator());
455 _M_deallocate(__new_start.base(), __len);
456 __throw_exception_again;
458 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
459 this->get_allocator());
460 _M_deallocate(this->_M_impl._M_start,
461 this->_M_impl._M_end_of_storage
462 - this->_M_impl._M_start);
463 this->_M_impl._M_start = __new_start.base();
464 this->_M_impl._M_finish = __new_finish.base();
465 this->_M_impl._M_end_of_storage = __new_start.base() + __len;
471 #endif /* _VECTOR_TCC */