Merge from mainline (168000:168310).
[official-gcc/graphite-test-results.git] / libstdc++-v3 / include / bits / stl_uninitialized.h
blobfd9f24361730cff9a55f2ea0fdcc46144d06e8d2
1 // Raw memory manipulators -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 // 2009, 2010
5 // Free Software Foundation, Inc.
6 //
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
11 // any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 // <http://www.gnu.org/licenses/>.
29 * Copyright (c) 1994
30 * Hewlett-Packard Company
32 * Permission to use, copy, modify, distribute and sell this software
33 * and its documentation for any purpose is hereby granted without fee,
34 * provided that the above copyright notice appear in all copies and
35 * that both that copyright notice and this permission notice appear
36 * in supporting documentation. Hewlett-Packard Company makes no
37 * representations about the suitability of this software for any
38 * purpose. It is provided "as is" without express or implied warranty.
41 * Copyright (c) 1996,1997
42 * Silicon Graphics Computer Systems, Inc.
44 * Permission to use, copy, modify, distribute and sell this software
45 * and its documentation for any purpose is hereby granted without fee,
46 * provided that the above copyright notice appear in all copies and
47 * that both that copyright notice and this permission notice appear
48 * in supporting documentation. Silicon Graphics makes no
49 * representations about the suitability of this software for any
50 * purpose. It is provided "as is" without express or implied warranty.
53 /** @file bits/stl_uninitialized.h
54 * This is an internal header file, included by other library headers.
55 * Do not attempt to use it directly. @headername{memory}
58 #ifndef _STL_UNINITIALIZED_H
59 #define _STL_UNINITIALIZED_H 1
61 _GLIBCXX_BEGIN_NAMESPACE(std)
63 template<bool _TrivialValueTypes>
64 struct __uninitialized_copy
66 template<typename _InputIterator, typename _ForwardIterator>
67 static _ForwardIterator
68 __uninit_copy(_InputIterator __first, _InputIterator __last,
69 _ForwardIterator __result)
71 _ForwardIterator __cur = __result;
72 __try
74 for (; __first != __last; ++__first, ++__cur)
75 std::_Construct(std::__addressof(*__cur), *__first);
76 return __cur;
78 __catch(...)
80 std::_Destroy(__result, __cur);
81 __throw_exception_again;
86 template<>
87 struct __uninitialized_copy<true>
89 template<typename _InputIterator, typename _ForwardIterator>
90 static _ForwardIterator
91 __uninit_copy(_InputIterator __first, _InputIterator __last,
92 _ForwardIterator __result)
93 { return std::copy(__first, __last, __result); }
96 /**
97 * @brief Copies the range [first,last) into result.
98 * @param first An input iterator.
99 * @param last An input iterator.
100 * @param result An output iterator.
101 * @return result + (first - last)
103 * Like copy(), but does not require an initialized output range.
105 template<typename _InputIterator, typename _ForwardIterator>
106 inline _ForwardIterator
107 uninitialized_copy(_InputIterator __first, _InputIterator __last,
108 _ForwardIterator __result)
110 typedef typename iterator_traits<_InputIterator>::value_type
111 _ValueType1;
112 typedef typename iterator_traits<_ForwardIterator>::value_type
113 _ValueType2;
115 return std::__uninitialized_copy<(__is_trivial(_ValueType1)
116 && __is_trivial(_ValueType2))>::
117 __uninit_copy(__first, __last, __result);
121 template<bool _TrivialValueType>
122 struct __uninitialized_fill
124 template<typename _ForwardIterator, typename _Tp>
125 static void
126 __uninit_fill(_ForwardIterator __first, _ForwardIterator __last,
127 const _Tp& __x)
129 _ForwardIterator __cur = __first;
130 __try
132 for (; __cur != __last; ++__cur)
133 std::_Construct(std::__addressof(*__cur), __x);
135 __catch(...)
137 std::_Destroy(__first, __cur);
138 __throw_exception_again;
143 template<>
144 struct __uninitialized_fill<true>
146 template<typename _ForwardIterator, typename _Tp>
147 static void
148 __uninit_fill(_ForwardIterator __first, _ForwardIterator __last,
149 const _Tp& __x)
150 { std::fill(__first, __last, __x); }
154 * @brief Copies the value x into the range [first,last).
155 * @param first An input iterator.
156 * @param last An input iterator.
157 * @param x The source value.
158 * @return Nothing.
160 * Like fill(), but does not require an initialized output range.
162 template<typename _ForwardIterator, typename _Tp>
163 inline void
164 uninitialized_fill(_ForwardIterator __first, _ForwardIterator __last,
165 const _Tp& __x)
167 typedef typename iterator_traits<_ForwardIterator>::value_type
168 _ValueType;
170 std::__uninitialized_fill<__is_trivial(_ValueType)>::
171 __uninit_fill(__first, __last, __x);
175 template<bool _TrivialValueType>
176 struct __uninitialized_fill_n
178 template<typename _ForwardIterator, typename _Size, typename _Tp>
179 static void
180 __uninit_fill_n(_ForwardIterator __first, _Size __n,
181 const _Tp& __x)
183 _ForwardIterator __cur = __first;
184 __try
186 for (; __n > 0; --__n, ++__cur)
187 std::_Construct(std::__addressof(*__cur), __x);
189 __catch(...)
191 std::_Destroy(__first, __cur);
192 __throw_exception_again;
197 template<>
198 struct __uninitialized_fill_n<true>
200 template<typename _ForwardIterator, typename _Size, typename _Tp>
201 static void
202 __uninit_fill_n(_ForwardIterator __first, _Size __n,
203 const _Tp& __x)
204 { std::fill_n(__first, __n, __x); }
208 * @brief Copies the value x into the range [first,first+n).
209 * @param first An input iterator.
210 * @param n The number of copies to make.
211 * @param x The source value.
212 * @return Nothing.
214 * Like fill_n(), but does not require an initialized output range.
216 template<typename _ForwardIterator, typename _Size, typename _Tp>
217 inline void
218 uninitialized_fill_n(_ForwardIterator __first, _Size __n, const _Tp& __x)
220 typedef typename iterator_traits<_ForwardIterator>::value_type
221 _ValueType;
223 std::__uninitialized_fill_n<__is_trivial(_ValueType)>::
224 __uninit_fill_n(__first, __n, __x);
227 // Extensions: versions of uninitialized_copy, uninitialized_fill,
228 // and uninitialized_fill_n that take an allocator parameter.
229 // We dispatch back to the standard versions when we're given the
230 // default allocator. For nondefault allocators we do not use
231 // any of the POD optimizations.
233 template<typename _InputIterator, typename _ForwardIterator,
234 typename _Allocator>
235 _ForwardIterator
236 __uninitialized_copy_a(_InputIterator __first, _InputIterator __last,
237 _ForwardIterator __result, _Allocator& __alloc)
239 _ForwardIterator __cur = __result;
240 __try
242 for (; __first != __last; ++__first, ++__cur)
243 __alloc.construct(std::__addressof(*__cur), *__first);
244 return __cur;
246 __catch(...)
248 std::_Destroy(__result, __cur, __alloc);
249 __throw_exception_again;
253 template<typename _InputIterator, typename _ForwardIterator, typename _Tp>
254 inline _ForwardIterator
255 __uninitialized_copy_a(_InputIterator __first, _InputIterator __last,
256 _ForwardIterator __result, allocator<_Tp>&)
257 { return std::uninitialized_copy(__first, __last, __result); }
259 template<typename _InputIterator, typename _ForwardIterator,
260 typename _Allocator>
261 inline _ForwardIterator
262 __uninitialized_move_a(_InputIterator __first, _InputIterator __last,
263 _ForwardIterator __result, _Allocator& __alloc)
265 return std::__uninitialized_copy_a(_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
266 _GLIBCXX_MAKE_MOVE_ITERATOR(__last),
267 __result, __alloc);
270 template<typename _ForwardIterator, typename _Tp, typename _Allocator>
271 void
272 __uninitialized_fill_a(_ForwardIterator __first, _ForwardIterator __last,
273 const _Tp& __x, _Allocator& __alloc)
275 _ForwardIterator __cur = __first;
276 __try
278 for (; __cur != __last; ++__cur)
279 __alloc.construct(std::__addressof(*__cur), __x);
281 __catch(...)
283 std::_Destroy(__first, __cur, __alloc);
284 __throw_exception_again;
288 template<typename _ForwardIterator, typename _Tp, typename _Tp2>
289 inline void
290 __uninitialized_fill_a(_ForwardIterator __first, _ForwardIterator __last,
291 const _Tp& __x, allocator<_Tp2>&)
292 { std::uninitialized_fill(__first, __last, __x); }
294 template<typename _ForwardIterator, typename _Size, typename _Tp,
295 typename _Allocator>
296 void
297 __uninitialized_fill_n_a(_ForwardIterator __first, _Size __n,
298 const _Tp& __x, _Allocator& __alloc)
300 _ForwardIterator __cur = __first;
301 __try
303 for (; __n > 0; --__n, ++__cur)
304 __alloc.construct(std::__addressof(*__cur), __x);
306 __catch(...)
308 std::_Destroy(__first, __cur, __alloc);
309 __throw_exception_again;
313 template<typename _ForwardIterator, typename _Size, typename _Tp,
314 typename _Tp2>
315 inline void
316 __uninitialized_fill_n_a(_ForwardIterator __first, _Size __n,
317 const _Tp& __x, allocator<_Tp2>&)
318 { std::uninitialized_fill_n(__first, __n, __x); }
321 // Extensions: __uninitialized_copy_move, __uninitialized_move_copy,
322 // __uninitialized_fill_move, __uninitialized_move_fill.
323 // All of these algorithms take a user-supplied allocator, which is used
324 // for construction and destruction.
326 // __uninitialized_copy_move
327 // Copies [first1, last1) into [result, result + (last1 - first1)), and
328 // move [first2, last2) into
329 // [result, result + (last1 - first1) + (last2 - first2)).
330 template<typename _InputIterator1, typename _InputIterator2,
331 typename _ForwardIterator, typename _Allocator>
332 inline _ForwardIterator
333 __uninitialized_copy_move(_InputIterator1 __first1,
334 _InputIterator1 __last1,
335 _InputIterator2 __first2,
336 _InputIterator2 __last2,
337 _ForwardIterator __result,
338 _Allocator& __alloc)
340 _ForwardIterator __mid = std::__uninitialized_copy_a(__first1, __last1,
341 __result,
342 __alloc);
343 __try
345 return std::__uninitialized_move_a(__first2, __last2, __mid, __alloc);
347 __catch(...)
349 std::_Destroy(__result, __mid, __alloc);
350 __throw_exception_again;
354 // __uninitialized_move_copy
355 // Moves [first1, last1) into [result, result + (last1 - first1)), and
356 // copies [first2, last2) into
357 // [result, result + (last1 - first1) + (last2 - first2)).
358 template<typename _InputIterator1, typename _InputIterator2,
359 typename _ForwardIterator, typename _Allocator>
360 inline _ForwardIterator
361 __uninitialized_move_copy(_InputIterator1 __first1,
362 _InputIterator1 __last1,
363 _InputIterator2 __first2,
364 _InputIterator2 __last2,
365 _ForwardIterator __result,
366 _Allocator& __alloc)
368 _ForwardIterator __mid = std::__uninitialized_move_a(__first1, __last1,
369 __result,
370 __alloc);
371 __try
373 return std::__uninitialized_copy_a(__first2, __last2, __mid, __alloc);
375 __catch(...)
377 std::_Destroy(__result, __mid, __alloc);
378 __throw_exception_again;
382 // __uninitialized_fill_move
383 // Fills [result, mid) with x, and moves [first, last) into
384 // [mid, mid + (last - first)).
385 template<typename _ForwardIterator, typename _Tp, typename _InputIterator,
386 typename _Allocator>
387 inline _ForwardIterator
388 __uninitialized_fill_move(_ForwardIterator __result, _ForwardIterator __mid,
389 const _Tp& __x, _InputIterator __first,
390 _InputIterator __last, _Allocator& __alloc)
392 std::__uninitialized_fill_a(__result, __mid, __x, __alloc);
393 __try
395 return std::__uninitialized_move_a(__first, __last, __mid, __alloc);
397 __catch(...)
399 std::_Destroy(__result, __mid, __alloc);
400 __throw_exception_again;
404 // __uninitialized_move_fill
405 // Moves [first1, last1) into [first2, first2 + (last1 - first1)), and
406 // fills [first2 + (last1 - first1), last2) with x.
407 template<typename _InputIterator, typename _ForwardIterator, typename _Tp,
408 typename _Allocator>
409 inline void
410 __uninitialized_move_fill(_InputIterator __first1, _InputIterator __last1,
411 _ForwardIterator __first2,
412 _ForwardIterator __last2, const _Tp& __x,
413 _Allocator& __alloc)
415 _ForwardIterator __mid2 = std::__uninitialized_move_a(__first1, __last1,
416 __first2,
417 __alloc);
418 __try
420 std::__uninitialized_fill_a(__mid2, __last2, __x, __alloc);
422 __catch(...)
424 std::_Destroy(__first2, __mid2, __alloc);
425 __throw_exception_again;
429 #ifdef __GXX_EXPERIMENTAL_CXX0X__
430 // Extensions: __uninitialized_default, __uninitialized_default_n,
431 // __uninitialized_default_a, __uninitialized_default_n_a.
433 template<bool _TrivialValueType>
434 struct __uninitialized_default_1
436 template<typename _ForwardIterator>
437 static void
438 __uninit_default(_ForwardIterator __first, _ForwardIterator __last)
440 _ForwardIterator __cur = __first;
441 __try
443 for (; __cur != __last; ++__cur)
444 std::_Construct(std::__addressof(*__cur));
446 __catch(...)
448 std::_Destroy(__first, __cur);
449 __throw_exception_again;
454 template<>
455 struct __uninitialized_default_1<true>
457 template<typename _ForwardIterator>
458 static void
459 __uninit_default(_ForwardIterator __first, _ForwardIterator __last)
461 typedef typename iterator_traits<_ForwardIterator>::value_type
462 _ValueType;
464 std::fill(__first, __last, _ValueType());
468 template<bool _TrivialValueType>
469 struct __uninitialized_default_n_1
471 template<typename _ForwardIterator, typename _Size>
472 static void
473 __uninit_default_n(_ForwardIterator __first, _Size __n)
475 _ForwardIterator __cur = __first;
476 __try
478 for (; __n > 0; --__n, ++__cur)
479 std::_Construct(std::__addressof(*__cur));
481 __catch(...)
483 std::_Destroy(__first, __cur);
484 __throw_exception_again;
489 template<>
490 struct __uninitialized_default_n_1<true>
492 template<typename _ForwardIterator, typename _Size>
493 static void
494 __uninit_default_n(_ForwardIterator __first, _Size __n)
496 typedef typename iterator_traits<_ForwardIterator>::value_type
497 _ValueType;
499 std::fill_n(__first, __n, _ValueType());
503 // __uninitialized_default
504 // Fills [first, last) with std::distance(first, last) default
505 // constructed value_types(s).
506 template<typename _ForwardIterator>
507 inline void
508 __uninitialized_default(_ForwardIterator __first,
509 _ForwardIterator __last)
511 typedef typename iterator_traits<_ForwardIterator>::value_type
512 _ValueType;
514 std::__uninitialized_default_1<__is_trivial(_ValueType)>::
515 __uninit_default(__first, __last);
518 // __uninitialized_default_n
519 // Fills [first, first + n) with n default constructed value_type(s).
520 template<typename _ForwardIterator, typename _Size>
521 inline void
522 __uninitialized_default_n(_ForwardIterator __first, _Size __n)
524 typedef typename iterator_traits<_ForwardIterator>::value_type
525 _ValueType;
527 std::__uninitialized_default_n_1<__is_trivial(_ValueType)>::
528 __uninit_default_n(__first, __n);
532 // __uninitialized_default_a
533 // Fills [first, last) with std::distance(first, last) default
534 // constructed value_types(s), constructed with the allocator alloc.
535 template<typename _ForwardIterator, typename _Allocator>
536 void
537 __uninitialized_default_a(_ForwardIterator __first,
538 _ForwardIterator __last,
539 _Allocator& __alloc)
541 _ForwardIterator __cur = __first;
542 __try
544 for (; __cur != __last; ++__cur)
545 __alloc.construct(std::__addressof(*__cur));
547 __catch(...)
549 std::_Destroy(__first, __cur, __alloc);
550 __throw_exception_again;
554 template<typename _ForwardIterator, typename _Tp>
555 inline void
556 __uninitialized_default_a(_ForwardIterator __first,
557 _ForwardIterator __last,
558 allocator<_Tp>&)
559 { std::__uninitialized_default(__first, __last); }
562 // __uninitialized_default_n_a
563 // Fills [first, first + n) with n default constructed value_types(s),
564 // constructed with the allocator alloc.
565 template<typename _ForwardIterator, typename _Size, typename _Allocator>
566 void
567 __uninitialized_default_n_a(_ForwardIterator __first, _Size __n,
568 _Allocator& __alloc)
570 _ForwardIterator __cur = __first;
571 __try
573 for (; __n > 0; --__n, ++__cur)
574 __alloc.construct(std::__addressof(*__cur));
576 __catch(...)
578 std::_Destroy(__first, __cur, __alloc);
579 __throw_exception_again;
583 template<typename _ForwardIterator, typename _Size, typename _Tp>
584 inline void
585 __uninitialized_default_n_a(_ForwardIterator __first, _Size __n,
586 allocator<_Tp>&)
587 { std::__uninitialized_default_n(__first, __n); }
590 template<typename _InputIterator, typename _Size,
591 typename _ForwardIterator>
592 _ForwardIterator
593 __uninitialized_copy_n(_InputIterator __first, _Size __n,
594 _ForwardIterator __result, input_iterator_tag)
596 _ForwardIterator __cur = __result;
597 __try
599 for (; __n > 0; --__n, ++__first, ++__cur)
600 std::_Construct(std::__addressof(*__cur), *__first);
601 return __cur;
603 __catch(...)
605 std::_Destroy(__result, __cur);
606 __throw_exception_again;
610 template<typename _RandomAccessIterator, typename _Size,
611 typename _ForwardIterator>
612 inline _ForwardIterator
613 __uninitialized_copy_n(_RandomAccessIterator __first, _Size __n,
614 _ForwardIterator __result,
615 random_access_iterator_tag)
616 { return std::uninitialized_copy(__first, __first + __n, __result); }
619 * @brief Copies the range [first,first+n) into result.
620 * @param first An input iterator.
621 * @param n The number of elements to copy.
622 * @param result An output iterator.
623 * @return result + n
625 * Like copy_n(), but does not require an initialized output range.
627 template<typename _InputIterator, typename _Size, typename _ForwardIterator>
628 inline _ForwardIterator
629 uninitialized_copy_n(_InputIterator __first, _Size __n,
630 _ForwardIterator __result)
631 { return std::__uninitialized_copy_n(__first, __n, __result,
632 std::__iterator_category(__first)); }
633 #endif
635 _GLIBCXX_END_NAMESPACE
637 #endif /* _STL_UNINITIALIZED_H */