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 const 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 std::uninitialized_fill_n(this->_M_impl._M_finish,
180 __n - size(), __val);
181 this->_M_impl._M_finish += __n - size();
184 erase(fill_n(begin(), __n, __val), end());
187 template<typename _Tp, typename _Alloc>
188 template<typename _InputIterator>
190 vector<_Tp, _Alloc>::
191 _M_assign_aux(_InputIterator __first, _InputIterator __last,
194 iterator __cur(begin());
195 for (; __first != __last && __cur != end(); ++__cur, ++__first)
197 if (__first == __last)
200 insert(end(), __first, __last);
203 template<typename _Tp, typename _Alloc>
204 template<typename _ForwardIterator>
206 vector<_Tp, _Alloc>::
207 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
208 forward_iterator_tag)
210 const size_type __len = std::distance(__first, __last);
212 if (__len > capacity())
214 pointer __tmp(_M_allocate_and_copy(__len, __first, __last));
215 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish);
216 _M_deallocate(this->_M_impl._M_start,
217 this->_M_impl._M_end_of_storage
218 - this->_M_impl._M_start);
219 this->_M_impl._M_start = __tmp;
220 this->_M_impl._M_finish = this->_M_impl._M_start + __len;
221 this->_M_impl._M_end_of_storage = this->_M_impl._M_finish;
223 else if (size() >= __len)
225 iterator __new_finish(copy(__first, __last,
226 this->_M_impl._M_start));
227 std::_Destroy(__new_finish, end());
228 this->_M_impl._M_finish = __new_finish.base();
232 _ForwardIterator __mid = __first;
233 std::advance(__mid, size());
234 std::copy(__first, __mid, this->_M_impl._M_start);
235 this->_M_impl._M_finish = std::uninitialized_copy(__mid,
242 template<typename _Tp, typename _Alloc>
244 vector<_Tp, _Alloc>::
245 _M_insert_aux(iterator __position, const _Tp& __x)
247 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
249 std::_Construct(this->_M_impl._M_finish,
250 *(this->_M_impl._M_finish - 1));
251 ++this->_M_impl._M_finish;
253 std::copy_backward(__position,
254 iterator(this->_M_impl._M_finish-2),
255 iterator(this->_M_impl._M_finish-1));
256 *__position = __x_copy;
260 const size_type __old_size = size();
261 const size_type __len = __old_size != 0 ? 2 * __old_size : 1;
262 iterator __new_start(this->_M_allocate(__len));
263 iterator __new_finish(__new_start);
266 __new_finish = std::uninitialized_copy(iterator(this->
270 std::_Construct(__new_finish.base(), __x);
272 __new_finish = std::uninitialized_copy(__position,
273 iterator(this->_M_impl.
279 std::_Destroy(__new_start,__new_finish);
280 _M_deallocate(__new_start.base(),__len);
281 __throw_exception_again;
283 std::_Destroy(begin(), end());
284 _M_deallocate(this->_M_impl._M_start,
285 this->_M_impl._M_end_of_storage
286 - this->_M_impl._M_start);
287 this->_M_impl._M_start = __new_start.base();
288 this->_M_impl._M_finish = __new_finish.base();
289 this->_M_impl._M_end_of_storage = __new_start.base() + __len;
293 template<typename _Tp, typename _Alloc>
295 vector<_Tp, _Alloc>::
296 _M_fill_insert(iterator __position, size_type __n, const value_type& __x)
300 if (size_type(this->_M_impl._M_end_of_storage
301 - this->_M_impl._M_finish) >= __n)
303 value_type __x_copy = __x;
304 const size_type __elems_after = end() - __position;
305 iterator __old_finish(this->_M_impl._M_finish);
306 if (__elems_after > __n)
308 std::uninitialized_copy(this->_M_impl._M_finish - __n,
309 this->_M_impl._M_finish,
310 this->_M_impl._M_finish);
311 this->_M_impl._M_finish += __n;
312 std::copy_backward(__position, __old_finish - __n,
314 std::fill(__position, __position + __n, __x_copy);
318 std::uninitialized_fill_n(this->_M_impl._M_finish,
321 this->_M_impl._M_finish += __n - __elems_after;
322 std::uninitialized_copy(__position, __old_finish,
323 this->_M_impl._M_finish);
324 this->_M_impl._M_finish += __elems_after;
325 std::fill(__position, __old_finish, __x_copy);
330 const size_type __old_size = size();
331 const size_type __len = __old_size + std::max(__old_size, __n);
332 iterator __new_start(this->_M_allocate(__len));
333 iterator __new_finish(__new_start);
336 __new_finish = std::uninitialized_copy(begin(), __position,
338 std::uninitialized_fill_n(__new_finish, __n, __x);
340 __new_finish = std::uninitialized_copy(__position, end(),
345 std::_Destroy(__new_start, __new_finish);
346 _M_deallocate(__new_start.base(), __len);
347 __throw_exception_again;
349 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish);
350 _M_deallocate(this->_M_impl._M_start,
351 this->_M_impl._M_end_of_storage
352 - this->_M_impl._M_start);
353 this->_M_impl._M_start = __new_start.base();
354 this->_M_impl._M_finish = __new_finish.base();
355 this->_M_impl._M_end_of_storage = __new_start.base() + __len;
360 template<typename _Tp, typename _Alloc> template<typename _InputIterator>
362 vector<_Tp, _Alloc>::
363 _M_range_insert(iterator __pos, _InputIterator __first,
364 _InputIterator __last, input_iterator_tag)
366 for (; __first != __last; ++__first)
368 __pos = insert(__pos, *__first);
373 template<typename _Tp, typename _Alloc>
374 template<typename _ForwardIterator>
376 vector<_Tp, _Alloc>::
377 _M_range_insert(iterator __position,_ForwardIterator __first,
378 _ForwardIterator __last, forward_iterator_tag)
380 if (__first != __last)
382 const size_type __n = std::distance(__first, __last);
383 if (size_type(this->_M_impl._M_end_of_storage
384 - this->_M_impl._M_finish) >= __n)
386 const size_type __elems_after = end() - __position;
387 iterator __old_finish(this->_M_impl._M_finish);
388 if (__elems_after > __n)
390 std::uninitialized_copy(this->_M_impl._M_finish - __n,
391 this->_M_impl._M_finish,
392 this->_M_impl._M_finish);
393 this->_M_impl._M_finish += __n;
394 std::copy_backward(__position, __old_finish - __n,
396 std::copy(__first, __last, __position);
400 _ForwardIterator __mid = __first;
401 std::advance(__mid, __elems_after);
402 std::uninitialized_copy(__mid, __last,
403 this->_M_impl._M_finish);
404 this->_M_impl._M_finish += __n - __elems_after;
405 std::uninitialized_copy(__position, __old_finish,
406 this->_M_impl._M_finish);
407 this->_M_impl._M_finish += __elems_after;
408 std::copy(__first, __mid, __position);
413 const size_type __old_size = size();
414 const size_type __len = __old_size + std::max(__old_size, __n);
415 iterator __new_start(this->_M_allocate(__len));
416 iterator __new_finish(__new_start);
419 __new_finish = std::uninitialized_copy(iterator(this->
424 __new_finish = std::uninitialized_copy(__first, __last,
426 __new_finish = std::uninitialized_copy(__position,
434 std::_Destroy(__new_start,__new_finish);
435 _M_deallocate(__new_start.base(), __len);
436 __throw_exception_again;
438 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish);
439 _M_deallocate(this->_M_impl._M_start,
440 this->_M_impl._M_end_of_storage
441 - this->_M_impl._M_start);
442 this->_M_impl._M_start = __new_start.base();
443 this->_M_impl._M_finish = __new_finish.base();
444 this->_M_impl._M_end_of_storage = __new_start.base() + __len;
450 #endif /* _VECTOR_TCC */