c_locale.cc: Fix throw messages to use the __N marker.
[gcc.git] / libstdc++-v3 / include / bits / vector.tcc
1 // Vector implementation (out of line) -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003 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 __gnu_norm
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_start,
78 this->_M_finish);
79 std::_Destroy(this->_M_start, this->_M_finish);
80 _M_deallocate(this->_M_start,
81 this->_M_end_of_storage - this->_M_start);
82 this->_M_start = __tmp;
83 this->_M_finish = __tmp + __old_size;
84 this->_M_end_of_storage = this->_M_start + __n;
85 }
86 }
87
88 template<typename _Tp, typename _Alloc>
89 typename vector<_Tp,_Alloc>::iterator
90 vector<_Tp,_Alloc>::
91 insert(iterator __position, const value_type& __x)
92 {
93 size_type __n = __position - begin();
94 if (this->_M_finish != this->_M_end_of_storage && __position == end())
95 {
96 std::_Construct(this->_M_finish, __x);
97 ++this->_M_finish;
98 }
99 else
100 _M_insert_aux(__position, __x);
101 return begin() + __n;
102 }
103
104 template<typename _Tp, typename _Alloc>
105 typename vector<_Tp,_Alloc>::iterator
106 vector<_Tp,_Alloc>::
107 erase(iterator __position)
108 {
109 if (__position + 1 != end())
110 std::copy(__position + 1, end(), __position);
111 --this->_M_finish;
112 std::_Destroy(this->_M_finish);
113 return __position;
114 }
115
116 template<typename _Tp, typename _Alloc>
117 typename vector<_Tp,_Alloc>::iterator
118 vector<_Tp,_Alloc>::
119 erase(iterator __first, iterator __last)
120 {
121 iterator __i(copy(__last, end(), __first));
122 std::_Destroy(__i, end());
123 this->_M_finish = this->_M_finish - (__last - __first);
124 return __first;
125 }
126
127 template<typename _Tp, typename _Alloc>
128 vector<_Tp,_Alloc>&
129 vector<_Tp,_Alloc>::
130 operator=(const vector<_Tp,_Alloc>& __x)
131 {
132 if (&__x != this)
133 {
134 const size_type __xlen = __x.size();
135 if (__xlen > capacity())
136 {
137 pointer __tmp = _M_allocate_and_copy(__xlen, __x.begin(), __x.end());
138 std::_Destroy(this->_M_start, this->_M_finish);
139 _M_deallocate(this->_M_start,
140 this->_M_end_of_storage - this->_M_start);
141 this->_M_start = __tmp;
142 this->_M_end_of_storage = this->_M_start + __xlen;
143 }
144 else if (size() >= __xlen)
145 {
146 iterator __i(copy(__x.begin(), __x.end(), begin()));
147 std::_Destroy(__i, end());
148 }
149 else
150 {
151 std::copy(__x.begin(), __x.begin() + size(), this->_M_start);
152 std::uninitialized_copy(__x.begin() + size(), __x.end(), this->_M_finish);
153 }
154 this->_M_finish = this->_M_start + __xlen;
155 }
156 return *this;
157 }
158
159 template<typename _Tp, typename _Alloc>
160 void
161 vector<_Tp,_Alloc>::
162 _M_fill_assign(size_t __n, const value_type& __val)
163 {
164 if (__n > capacity())
165 {
166 vector __tmp(__n, __val, get_allocator());
167 __tmp.swap(*this);
168 }
169 else if (__n > size())
170 {
171 std::fill(begin(), end(), __val);
172 this->_M_finish
173 = std::uninitialized_fill_n(this->_M_finish, __n - size(), __val);
174 }
175 else
176 erase(fill_n(begin(), __n, __val), end());
177 }
178
179 template<typename _Tp, typename _Alloc> template<typename _InputIterator>
180 void
181 vector<_Tp,_Alloc>::
182 _M_assign_aux(_InputIterator __first, _InputIterator __last, input_iterator_tag)
183 {
184 iterator __cur(begin());
185 for ( ; __first != __last && __cur != end(); ++__cur, ++__first)
186 *__cur = *__first;
187 if (__first == __last)
188 erase(__cur, end());
189 else
190 insert(end(), __first, __last);
191 }
192
193 template<typename _Tp, typename _Alloc> template<typename _ForwardIterator>
194 void
195 vector<_Tp,_Alloc>::
196 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
197 forward_iterator_tag)
198 {
199 size_type __len = std::distance(__first, __last);
200
201 if (__len > capacity())
202 {
203 pointer __tmp(_M_allocate_and_copy(__len, __first, __last));
204 std::_Destroy(this->_M_start, this->_M_finish);
205 _M_deallocate(this->_M_start,
206 this->_M_end_of_storage - this->_M_start);
207 this->_M_start = __tmp;
208 this->_M_end_of_storage = this->_M_finish = this->_M_start + __len;
209 }
210 else if (size() >= __len)
211 {
212 iterator __new_finish(copy(__first, __last, this->_M_start));
213 std::_Destroy(__new_finish, end());
214 this->_M_finish = __new_finish.base();
215 }
216 else
217 {
218 _ForwardIterator __mid = __first;
219 std::advance(__mid, size());
220 std::copy(__first, __mid, this->_M_start);
221 this->_M_finish = std::uninitialized_copy(__mid, __last, this->_M_finish);
222 }
223 }
224
225 template<typename _Tp, typename _Alloc>
226 void
227 vector<_Tp,_Alloc>::
228 _M_insert_aux(iterator __position, const _Tp& __x)
229 {
230 if (this->_M_finish != this->_M_end_of_storage)
231 {
232 std::_Construct(this->_M_finish, *(this->_M_finish - 1));
233 ++this->_M_finish;
234 _Tp __x_copy = __x;
235 std::copy_backward(__position,
236 iterator(this->_M_finish-2),
237 iterator(this->_M_finish-1));
238 *__position = __x_copy;
239 }
240 else
241 {
242 const size_type __old_size = size();
243 const size_type __len = __old_size != 0 ? 2 * __old_size : 1;
244 iterator __new_start(this->_M_allocate(__len));
245 iterator __new_finish(__new_start);
246 try
247 {
248 __new_finish = std::uninitialized_copy(iterator(this->_M_start),
249 __position,
250 __new_start);
251 std::_Construct(__new_finish.base(), __x);
252 ++__new_finish;
253 __new_finish = std::uninitialized_copy(__position,
254 iterator(this->_M_finish),
255 __new_finish);
256 }
257 catch(...)
258 {
259 std::_Destroy(__new_start,__new_finish);
260 _M_deallocate(__new_start.base(),__len);
261 __throw_exception_again;
262 }
263 std::_Destroy(begin(), end());
264 _M_deallocate(this->_M_start,
265 this->_M_end_of_storage - this->_M_start);
266 this->_M_start = __new_start.base();
267 this->_M_finish = __new_finish.base();
268 this->_M_end_of_storage = __new_start.base() + __len;
269 }
270 }
271
272 #ifdef _GLIBCXX_DEPRECATED
273 template<typename _Tp, typename _Alloc>
274 void
275 vector<_Tp,_Alloc>::
276 _M_insert_aux(iterator __position)
277 {
278 if (_M_finish != _M_end_of_storage)
279 {
280 std::_Construct(_M_finish, *(_M_finish - 1));
281 ++_M_finish;
282 std::copy_backward(__position, iterator(_M_finish - 2),
283 iterator(_M_finish - 1));
284 *__position = value_type();
285 }
286 else
287 {
288 const size_type __old_size = size();
289 const size_type __len = __old_size != 0 ? 2 * __old_size : 1;
290 pointer __new_start = _M_allocate(__len);
291 pointer __new_finish = __new_start;
292 try
293 {
294 __new_finish = std::uninitialized_copy(iterator(_M_start), __position,
295 __new_start);
296 std::_Construct(__new_finish);
297 ++__new_finish;
298 __new_finish = std::uninitialized_copy(__position, iterator(_M_finish),
299 __new_finish);
300 }
301 catch(...)
302 {
303 std::_Destroy(__new_start,__new_finish);
304 _M_deallocate(__new_start,__len);
305 __throw_exception_again;
306 }
307 std::_Destroy(begin(), end());
308 _M_deallocate(_M_start, _M_end_of_storage - _M_start);
309 _M_start = __new_start;
310 _M_finish = __new_finish;
311 _M_end_of_storage = __new_start + __len;
312 }
313 }
314 #endif
315
316 template<typename _Tp, typename _Alloc>
317 void
318 vector<_Tp,_Alloc>::
319 _M_fill_insert(iterator __position, size_type __n, const value_type& __x)
320 {
321 if (__n != 0)
322 {
323 if (size_type(this->_M_end_of_storage - this->_M_finish) >= __n)
324 {
325 value_type __x_copy = __x;
326 const size_type __elems_after = end() - __position;
327 iterator __old_finish(this->_M_finish);
328 if (__elems_after > __n)
329 {
330 std::uninitialized_copy(this->_M_finish - __n,
331 this->_M_finish,
332 this->_M_finish);
333 this->_M_finish += __n;
334 std::copy_backward(__position, __old_finish - __n, __old_finish);
335 std::fill(__position, __position + __n, __x_copy);
336 }
337 else
338 {
339 std::uninitialized_fill_n(this->_M_finish,
340 __n - __elems_after,
341 __x_copy);
342 this->_M_finish += __n - __elems_after;
343 std::uninitialized_copy(__position, __old_finish, this->_M_finish);
344 this->_M_finish += __elems_after;
345 std::fill(__position, __old_finish, __x_copy);
346 }
347 }
348 else
349 {
350 const size_type __old_size = size();
351 const size_type __len = __old_size + std::max(__old_size, __n);
352 iterator __new_start(this->_M_allocate(__len));
353 iterator __new_finish(__new_start);
354 try
355 {
356 __new_finish = std::uninitialized_copy(begin(), __position,
357 __new_start);
358 __new_finish = std::uninitialized_fill_n(__new_finish, __n, __x);
359 __new_finish = std::uninitialized_copy(__position, end(),
360 __new_finish);
361 }
362 catch(...)
363 {
364 std::_Destroy(__new_start,__new_finish);
365 _M_deallocate(__new_start.base(),__len);
366 __throw_exception_again;
367 }
368 std::_Destroy(this->_M_start, this->_M_finish);
369 _M_deallocate(this->_M_start,
370 this->_M_end_of_storage - this->_M_start);
371 this->_M_start = __new_start.base();
372 this->_M_finish = __new_finish.base();
373 this->_M_end_of_storage = __new_start.base() + __len;
374 }
375 }
376 }
377
378 template<typename _Tp, typename _Alloc> template<typename _InputIterator>
379 void
380 vector<_Tp,_Alloc>::
381 _M_range_insert(iterator __pos,
382 _InputIterator __first, _InputIterator __last,
383 input_iterator_tag)
384 {
385 for ( ; __first != __last; ++__first)
386 {
387 __pos = insert(__pos, *__first);
388 ++__pos;
389 }
390 }
391
392 template<typename _Tp, typename _Alloc> template<typename _ForwardIterator>
393 void
394 vector<_Tp,_Alloc>::
395 _M_range_insert(iterator __position,_ForwardIterator __first,
396 _ForwardIterator __last, forward_iterator_tag)
397 {
398 if (__first != __last)
399 {
400 size_type __n = std::distance(__first, __last);
401 if (size_type(this->_M_end_of_storage - this->_M_finish) >= __n)
402 {
403 const size_type __elems_after = end() - __position;
404 iterator __old_finish(this->_M_finish);
405 if (__elems_after > __n)
406 {
407 std::uninitialized_copy(this->_M_finish - __n,
408 this->_M_finish,
409 this->_M_finish);
410 this->_M_finish += __n;
411 std::copy_backward(__position, __old_finish - __n, __old_finish);
412 std::copy(__first, __last, __position);
413 }
414 else
415 {
416 _ForwardIterator __mid = __first;
417 std::advance(__mid, __elems_after);
418 std::uninitialized_copy(__mid, __last, this->_M_finish);
419 this->_M_finish += __n - __elems_after;
420 std::uninitialized_copy(__position, __old_finish, this->_M_finish);
421 this->_M_finish += __elems_after;
422 std::copy(__first, __mid, __position);
423 }
424 }
425 else
426 {
427 const size_type __old_size = size();
428 const size_type __len = __old_size + std::max(__old_size, __n);
429 iterator __new_start(this->_M_allocate(__len));
430 iterator __new_finish(__new_start);
431 try
432 {
433 __new_finish = std::uninitialized_copy(iterator(this->_M_start),
434 __position, __new_start);
435 __new_finish = std::uninitialized_copy(__first, __last,
436 __new_finish);
437 __new_finish = std::uninitialized_copy(__position,
438 iterator(this->_M_finish),
439 __new_finish);
440 }
441 catch(...)
442 {
443 std::_Destroy(__new_start,__new_finish);
444 _M_deallocate(__new_start.base(), __len);
445 __throw_exception_again;
446 }
447 std::_Destroy(this->_M_start, this->_M_finish);
448 _M_deallocate(this->_M_start,
449 this->_M_end_of_storage - this->_M_start);
450 this->_M_start = __new_start.base();
451 this->_M_finish = __new_finish.base();
452 this->_M_end_of_storage = __new_start.base() + __len;
453 }
454 }
455 }
456 } // namespace __gnu_norm
457
458 #endif /* _VECTOR_TCC */