re PR libstdc++/16505 ([3.4 only] std::uninitialized_fill_n() incorrect signature)
[gcc.git] / libstdc++-v3 / include / bits / vector.tcc
1 // Vector implementation (out of line) -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004 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 /** @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
61 #ifndef _VECTOR_TCC
62 #define _VECTOR_TCC 1
63
64 namespace _GLIBCXX_STD
65 {
66 template<typename _Tp, typename _Alloc>
67 void
68 vector<_Tp, _Alloc>::
69 reserve(size_type __n)
70 {
71 if (__n > this->max_size())
72 __throw_length_error(__N("vector::reserve"));
73 if (this->capacity() < __n)
74 {
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;
86 }
87 }
88
89 template<typename _Tp, typename _Alloc>
90 typename vector<_Tp, _Alloc>::iterator
91 vector<_Tp, _Alloc>::
92 insert(iterator __position, const value_type& __x)
93 {
94 const size_type __n = __position - begin();
95 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage
96 && __position == end())
97 {
98 std::_Construct(this->_M_impl._M_finish, __x);
99 ++this->_M_impl._M_finish;
100 }
101 else
102 _M_insert_aux(__position, __x);
103 return begin() + __n;
104 }
105
106 template<typename _Tp, typename _Alloc>
107 typename vector<_Tp, _Alloc>::iterator
108 vector<_Tp, _Alloc>::
109 erase(iterator __position)
110 {
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);
115 return __position;
116 }
117
118 template<typename _Tp, typename _Alloc>
119 typename vector<_Tp, _Alloc>::iterator
120 vector<_Tp, _Alloc>::
121 erase(iterator __first, iterator __last)
122 {
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);
126 return __first;
127 }
128
129 template<typename _Tp, typename _Alloc>
130 vector<_Tp, _Alloc>&
131 vector<_Tp, _Alloc>::
132 operator=(const vector<_Tp, _Alloc>& __x)
133 {
134 if (&__x != this)
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());
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;
148 }
149 else if (size() >= __xlen)
150 {
151 iterator __i(copy(__x.begin(), __x.end(), begin()));
152 std::_Destroy(__i, end());
153 }
154 else
155 {
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);
160 }
161 this->_M_impl._M_finish = this->_M_impl._M_start + __xlen;
162 }
163 return *this;
164 }
165
166 template<typename _Tp, typename _Alloc>
167 void
168 vector<_Tp, _Alloc>::
169 _M_fill_assign(size_t __n, const value_type& __val)
170 {
171 if (__n > capacity())
172 {
173 vector __tmp(__n, __val, get_allocator());
174 __tmp.swap(*this);
175 }
176 else if (__n > size())
177 {
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();
182 }
183 else
184 erase(fill_n(begin(), __n, __val), end());
185 }
186
187 template<typename _Tp, typename _Alloc>
188 template<typename _InputIterator>
189 void
190 vector<_Tp, _Alloc>::
191 _M_assign_aux(_InputIterator __first, _InputIterator __last,
192 input_iterator_tag)
193 {
194 iterator __cur(begin());
195 for (; __first != __last && __cur != end(); ++__cur, ++__first)
196 *__cur = *__first;
197 if (__first == __last)
198 erase(__cur, end());
199 else
200 insert(end(), __first, __last);
201 }
202
203 template<typename _Tp, typename _Alloc>
204 template<typename _ForwardIterator>
205 void
206 vector<_Tp, _Alloc>::
207 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
208 forward_iterator_tag)
209 {
210 const size_type __len = std::distance(__first, __last);
211
212 if (__len > capacity())
213 {
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;
222 }
223 else if (size() >= __len)
224 {
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();
229 }
230 else
231 {
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,
236 __last,
237 this->_M_impl.
238 _M_finish);
239 }
240 }
241
242 template<typename _Tp, typename _Alloc>
243 void
244 vector<_Tp, _Alloc>::
245 _M_insert_aux(iterator __position, const _Tp& __x)
246 {
247 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
248 {
249 std::_Construct(this->_M_impl._M_finish,
250 *(this->_M_impl._M_finish - 1));
251 ++this->_M_impl._M_finish;
252 _Tp __x_copy = __x;
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;
257 }
258 else
259 {
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);
264 try
265 {
266 __new_finish = std::uninitialized_copy(iterator(this->
267 _M_impl._M_start),
268 __position,
269 __new_start);
270 std::_Construct(__new_finish.base(), __x);
271 ++__new_finish;
272 __new_finish = std::uninitialized_copy(__position,
273 iterator(this->_M_impl.
274 _M_finish),
275 __new_finish);
276 }
277 catch(...)
278 {
279 std::_Destroy(__new_start,__new_finish);
280 _M_deallocate(__new_start.base(),__len);
281 __throw_exception_again;
282 }
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;
290 }
291 }
292
293 template<typename _Tp, typename _Alloc>
294 void
295 vector<_Tp, _Alloc>::
296 _M_fill_insert(iterator __position, size_type __n, const value_type& __x)
297 {
298 if (__n != 0)
299 {
300 if (size_type(this->_M_impl._M_end_of_storage
301 - this->_M_impl._M_finish) >= __n)
302 {
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)
307 {
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,
313 __old_finish);
314 std::fill(__position, __position + __n, __x_copy);
315 }
316 else
317 {
318 std::uninitialized_fill_n(this->_M_impl._M_finish,
319 __n - __elems_after,
320 __x_copy);
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);
326 }
327 }
328 else
329 {
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);
334 try
335 {
336 __new_finish = std::uninitialized_copy(begin(), __position,
337 __new_start);
338 std::uninitialized_fill_n(__new_finish, __n, __x);
339 __new_finish += __n;
340 __new_finish = std::uninitialized_copy(__position, end(),
341 __new_finish);
342 }
343 catch(...)
344 {
345 std::_Destroy(__new_start, __new_finish);
346 _M_deallocate(__new_start.base(), __len);
347 __throw_exception_again;
348 }
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;
356 }
357 }
358 }
359
360 template<typename _Tp, typename _Alloc> template<typename _InputIterator>
361 void
362 vector<_Tp, _Alloc>::
363 _M_range_insert(iterator __pos, _InputIterator __first,
364 _InputIterator __last, input_iterator_tag)
365 {
366 for (; __first != __last; ++__first)
367 {
368 __pos = insert(__pos, *__first);
369 ++__pos;
370 }
371 }
372
373 template<typename _Tp, typename _Alloc>
374 template<typename _ForwardIterator>
375 void
376 vector<_Tp, _Alloc>::
377 _M_range_insert(iterator __position,_ForwardIterator __first,
378 _ForwardIterator __last, forward_iterator_tag)
379 {
380 if (__first != __last)
381 {
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)
385 {
386 const size_type __elems_after = end() - __position;
387 iterator __old_finish(this->_M_impl._M_finish);
388 if (__elems_after > __n)
389 {
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,
395 __old_finish);
396 std::copy(__first, __last, __position);
397 }
398 else
399 {
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);
409 }
410 }
411 else
412 {
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);
417 try
418 {
419 __new_finish = std::uninitialized_copy(iterator(this->
420 _M_impl.
421 _M_start),
422 __position,
423 __new_start);
424 __new_finish = std::uninitialized_copy(__first, __last,
425 __new_finish);
426 __new_finish = std::uninitialized_copy(__position,
427 iterator(this->
428 _M_impl.
429 _M_finish),
430 __new_finish);
431 }
432 catch(...)
433 {
434 std::_Destroy(__new_start,__new_finish);
435 _M_deallocate(__new_start.base(), __len);
436 __throw_exception_again;
437 }
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;
445 }
446 }
447 }
448 } // namespace std
449
450 #endif /* _VECTOR_TCC */