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 _M_deallocate(this->_M_impl._M_start,
81 this->_M_impl._M_end_of_storage
82 - this->_M_impl._M_start);
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;
89 template<typename _Tp, typename _Alloc>
90 typename vector<_Tp, _Alloc>::iterator
92 insert(iterator __position, const value_type& __x)
94 size_type __n = __position - begin();
95 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage
96 && __position == end())
98 std::_Construct(this->_M_impl._M_finish, __x);
99 ++this->_M_impl._M_finish;
102 _M_insert_aux(__position, __x);
103 return begin() + __n;
106 template<typename _Tp, typename _Alloc>
107 typename vector<_Tp, _Alloc>::iterator
108 vector<_Tp, _Alloc>::
109 erase(iterator __position)
111 if (__position + 1 != end())
112 std::copy(__position + 1, end(), __position);
113 --this->_M_impl._M_finish;
114 std::_Destroy(this->_M_impl._M_finish);
118 template<typename _Tp, typename _Alloc>
119 typename vector<_Tp, _Alloc>::iterator
120 vector<_Tp, _Alloc>::
121 erase(iterator __first, iterator __last)
123 iterator __i(copy(__last, end(), __first));
124 std::_Destroy(__i, end());
125 this->_M_impl._M_finish = this->_M_impl._M_finish - (__last - __first);
129 template<typename _Tp, typename _Alloc>
131 vector<_Tp, _Alloc>::
132 operator=(const vector<_Tp, _Alloc>& __x)
136 const size_type __xlen = __x.size();
137 if (__xlen > capacity())
139 pointer __tmp = _M_allocate_and_copy(__xlen, __x.begin(),
141 std::_Destroy(this->_M_impl._M_start,
142 this->_M_impl._M_finish);
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;
149 else if (size() >= __xlen)
151 iterator __i(copy(__x.begin(), __x.end(), begin()));
152 std::_Destroy(__i, end());
156 std::copy(__x.begin(), __x.begin() + size(),
157 this->_M_impl._M_start);
158 std::uninitialized_copy(__x.begin() + size(),
159 __x.end(), this->_M_impl._M_finish);
161 this->_M_impl._M_finish = this->_M_impl._M_start + __xlen;
166 template<typename _Tp, typename _Alloc>
168 vector<_Tp, _Alloc>::
169 _M_fill_assign(size_t __n, const value_type& __val)
171 if (__n > capacity())
173 vector __tmp(__n, __val, get_allocator());
176 else if (__n > size())
178 std::fill(begin(), end(), __val);
179 this->_M_impl._M_finish = std::uninitialized_fill_n(this->
185 erase(fill_n(begin(), __n, __val), end());
188 template<typename _Tp, typename _Alloc>
189 template<typename _InputIterator>
191 vector<_Tp, _Alloc>::
192 _M_assign_aux(_InputIterator __first, _InputIterator __last,
195 iterator __cur(begin());
196 for ( ; __first != __last && __cur != end(); ++__cur, ++__first)
198 if (__first == __last)
201 insert(end(), __first, __last);
204 template<typename _Tp, typename _Alloc>
205 template<typename _ForwardIterator>
208 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
209 forward_iterator_tag)
211 size_type __len = std::distance(__first, __last);
213 if (__len > capacity())
215 pointer __tmp(_M_allocate_and_copy(__len, __first, __last));
216 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish);
217 _M_deallocate(this->_M_impl._M_start,
218 this->_M_impl._M_end_of_storage
219 - this->_M_impl._M_start);
220 this->_M_impl._M_start = __tmp;
221 this->_M_impl._M_finish = this->_M_impl._M_start + __len;
222 this->_M_impl._M_end_of_storage = this->_M_impl._M_finish;
224 else if (size() >= __len)
226 iterator __new_finish(copy(__first, __last,
227 this->_M_impl._M_start));
228 std::_Destroy(__new_finish, end());
229 this->_M_impl._M_finish = __new_finish.base();
233 _ForwardIterator __mid = __first;
234 std::advance(__mid, size());
235 std::copy(__first, __mid, this->_M_impl._M_start);
236 this->_M_impl._M_finish = std::uninitialized_copy(__mid,
243 template<typename _Tp, typename _Alloc>
246 _M_insert_aux(iterator __position, const _Tp& __x)
248 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
250 std::_Construct(this->_M_impl._M_finish,
251 *(this->_M_impl._M_finish - 1));
252 ++this->_M_impl._M_finish;
254 std::copy_backward(__position,
255 iterator(this->_M_impl._M_finish-2),
256 iterator(this->_M_impl._M_finish-1));
257 *__position = __x_copy;
261 const size_type __old_size = size();
262 const size_type __len = __old_size != 0 ? 2 * __old_size : 1;
263 iterator __new_start(this->_M_allocate(__len));
264 iterator __new_finish(__new_start);
267 __new_finish = std::uninitialized_copy(iterator(this->
271 std::_Construct(__new_finish.base(), __x);
273 __new_finish = std::uninitialized_copy(__position,
274 iterator(this->_M_impl.
280 std::_Destroy(__new_start,__new_finish);
281 _M_deallocate(__new_start.base(),__len);
282 __throw_exception_again;
284 std::_Destroy(begin(), end());
285 _M_deallocate(this->_M_impl._M_start,
286 this->_M_impl._M_end_of_storage
287 - this->_M_impl._M_start);
288 this->_M_impl._M_start = __new_start.base();
289 this->_M_impl._M_finish = __new_finish.base();
290 this->_M_impl._M_end_of_storage = __new_start.base() + __len;
294 template<typename _Tp, typename _Alloc>
297 _M_fill_insert(iterator __position, size_type __n, const value_type& __x)
301 if (size_type(this->_M_impl._M_end_of_storage
302 - this->_M_impl._M_finish) >= __n)
304 value_type __x_copy = __x;
305 const size_type __elems_after = end() - __position;
306 iterator __old_finish(this->_M_impl._M_finish);
307 if (__elems_after > __n)
309 std::uninitialized_copy(this->_M_impl._M_finish - __n,
310 this->_M_impl._M_finish,
311 this->_M_impl._M_finish);
312 this->_M_impl._M_finish += __n;
313 std::copy_backward(__position, __old_finish - __n,
315 std::fill(__position, __position + __n, __x_copy);
319 std::uninitialized_fill_n(this->_M_impl._M_finish,
322 this->_M_impl._M_finish += __n - __elems_after;
323 std::uninitialized_copy(__position, __old_finish,
324 this->_M_impl._M_finish);
325 this->_M_impl._M_finish += __elems_after;
326 std::fill(__position, __old_finish, __x_copy);
331 const size_type __old_size = size();
332 const size_type __len = __old_size + std::max(__old_size, __n);
333 iterator __new_start(this->_M_allocate(__len));
334 iterator __new_finish(__new_start);
337 __new_finish = std::uninitialized_copy(begin(), __position,
339 __new_finish = std::uninitialized_fill_n(__new_finish, __n,
341 __new_finish = std::uninitialized_copy(__position, end(),
346 std::_Destroy(__new_start,__new_finish);
347 _M_deallocate(__new_start.base(),__len);
348 __throw_exception_again;
350 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish);
351 _M_deallocate(this->_M_impl._M_start,
352 this->_M_impl._M_end_of_storage
353 - this->_M_impl._M_start);
354 this->_M_impl._M_start = __new_start.base();
355 this->_M_impl._M_finish = __new_finish.base();
356 this->_M_impl._M_end_of_storage = __new_start.base() + __len;
361 template<typename _Tp, typename _Alloc> template<typename _InputIterator>
364 _M_range_insert(iterator __pos, _InputIterator __first,
365 _InputIterator __last, input_iterator_tag)
367 for ( ; __first != __last; ++__first)
369 __pos = insert(__pos, *__first);
374 template<typename _Tp, typename _Alloc>
375 template<typename _ForwardIterator>
378 _M_range_insert(iterator __position,_ForwardIterator __first,
379 _ForwardIterator __last, forward_iterator_tag)
381 if (__first != __last)
383 size_type __n = std::distance(__first, __last);
384 if (size_type(this->_M_impl._M_end_of_storage
385 - this->_M_impl._M_finish) >= __n)
387 const size_type __elems_after = end() - __position;
388 iterator __old_finish(this->_M_impl._M_finish);
389 if (__elems_after > __n)
391 std::uninitialized_copy(this->_M_impl._M_finish - __n,
392 this->_M_impl._M_finish,
393 this->_M_impl._M_finish);
394 this->_M_impl._M_finish += __n;
395 std::copy_backward(__position, __old_finish - __n,
397 std::copy(__first, __last, __position);
401 _ForwardIterator __mid = __first;
402 std::advance(__mid, __elems_after);
403 std::uninitialized_copy(__mid, __last,
404 this->_M_impl._M_finish);
405 this->_M_impl._M_finish += __n - __elems_after;
406 std::uninitialized_copy(__position, __old_finish,
407 this->_M_impl._M_finish);
408 this->_M_impl._M_finish += __elems_after;
409 std::copy(__first, __mid, __position);
414 const size_type __old_size = size();
415 const size_type __len = __old_size + std::max(__old_size, __n);
416 iterator __new_start(this->_M_allocate(__len));
417 iterator __new_finish(__new_start);
420 __new_finish = std::uninitialized_copy(iterator(this->
425 __new_finish = std::uninitialized_copy(__first, __last,
427 __new_finish = std::uninitialized_copy(__position,
435 std::_Destroy(__new_start,__new_finish);
436 _M_deallocate(__new_start.base(), __len);
437 __throw_exception_again;
439 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish);
440 _M_deallocate(this->_M_impl._M_start,
441 this->_M_impl._M_end_of_storage
442 - this->_M_impl._M_start);
443 this->_M_impl._M_start = __new_start.base();
444 this->_M_impl._M_finish = __new_finish.base();
445 this->_M_impl._M_end_of_storage = __new_start.base() + __len;
451 #endif /* _VECTOR_TCC */