1 // Raw memory manipulators -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
5 // Free Software Foundation, Inc.
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)
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/>.
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
;
74 for (; __first
!= __last
; ++__first
, ++__cur
)
75 std::_Construct(std::__addressof(*__cur
), *__first
);
80 std::_Destroy(__result
, __cur
);
81 __throw_exception_again
;
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
); }
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
112 typedef typename iterator_traits
<_ForwardIterator
>::value_type
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
>
126 __uninit_fill(_ForwardIterator __first
, _ForwardIterator __last
,
129 _ForwardIterator __cur
= __first
;
132 for (; __cur
!= __last
; ++__cur
)
133 std::_Construct(std::__addressof(*__cur
), __x
);
137 std::_Destroy(__first
, __cur
);
138 __throw_exception_again
;
144 struct __uninitialized_fill
<true>
146 template<typename _ForwardIterator
, typename _Tp
>
148 __uninit_fill(_ForwardIterator __first
, _ForwardIterator __last
,
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.
160 * Like fill(), but does not require an initialized output range.
162 template<typename _ForwardIterator
, typename _Tp
>
164 uninitialized_fill(_ForwardIterator __first
, _ForwardIterator __last
,
167 typedef typename iterator_traits
<_ForwardIterator
>::value_type
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
>
180 __uninit_fill_n(_ForwardIterator __first
, _Size __n
,
183 _ForwardIterator __cur
= __first
;
186 for (; __n
> 0; --__n
, ++__cur
)
187 std::_Construct(std::__addressof(*__cur
), __x
);
191 std::_Destroy(__first
, __cur
);
192 __throw_exception_again
;
198 struct __uninitialized_fill_n
<true>
200 template<typename _ForwardIterator
, typename _Size
, typename _Tp
>
202 __uninit_fill_n(_ForwardIterator __first
, _Size __n
,
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.
214 * Like fill_n(), but does not require an initialized output range.
216 template<typename _ForwardIterator
, typename _Size
, typename _Tp
>
218 uninitialized_fill_n(_ForwardIterator __first
, _Size __n
, const _Tp
& __x
)
220 typedef typename iterator_traits
<_ForwardIterator
>::value_type
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
,
236 __uninitialized_copy_a(_InputIterator __first
, _InputIterator __last
,
237 _ForwardIterator __result
, _Allocator
& __alloc
)
239 _ForwardIterator __cur
= __result
;
242 for (; __first
!= __last
; ++__first
, ++__cur
)
243 __alloc
.construct(std::__addressof(*__cur
), *__first
);
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
,
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
),
270 template<typename _ForwardIterator
, typename _Tp
, typename _Allocator
>
272 __uninitialized_fill_a(_ForwardIterator __first
, _ForwardIterator __last
,
273 const _Tp
& __x
, _Allocator
& __alloc
)
275 _ForwardIterator __cur
= __first
;
278 for (; __cur
!= __last
; ++__cur
)
279 __alloc
.construct(std::__addressof(*__cur
), __x
);
283 std::_Destroy(__first
, __cur
, __alloc
);
284 __throw_exception_again
;
288 template<typename _ForwardIterator
, typename _Tp
, typename _Tp2
>
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
,
297 __uninitialized_fill_n_a(_ForwardIterator __first
, _Size __n
,
298 const _Tp
& __x
, _Allocator
& __alloc
)
300 _ForwardIterator __cur
= __first
;
303 for (; __n
> 0; --__n
, ++__cur
)
304 __alloc
.construct(std::__addressof(*__cur
), __x
);
308 std::_Destroy(__first
, __cur
, __alloc
);
309 __throw_exception_again
;
313 template<typename _ForwardIterator
, typename _Size
, typename _Tp
,
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
,
340 _ForwardIterator __mid
= std::__uninitialized_copy_a(__first1
, __last1
,
345 return std::__uninitialized_move_a(__first2
, __last2
, __mid
, __alloc
);
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
,
368 _ForwardIterator __mid
= std::__uninitialized_move_a(__first1
, __last1
,
373 return std::__uninitialized_copy_a(__first2
, __last2
, __mid
, __alloc
);
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
,
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
);
395 return std::__uninitialized_move_a(__first
, __last
, __mid
, __alloc
);
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
,
410 __uninitialized_move_fill(_InputIterator __first1
, _InputIterator __last1
,
411 _ForwardIterator __first2
,
412 _ForwardIterator __last2
, const _Tp
& __x
,
415 _ForwardIterator __mid2
= std::__uninitialized_move_a(__first1
, __last1
,
420 std::__uninitialized_fill_a(__mid2
, __last2
, __x
, __alloc
);
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
>
438 __uninit_default(_ForwardIterator __first
, _ForwardIterator __last
)
440 _ForwardIterator __cur
= __first
;
443 for (; __cur
!= __last
; ++__cur
)
444 std::_Construct(std::__addressof(*__cur
));
448 std::_Destroy(__first
, __cur
);
449 __throw_exception_again
;
455 struct __uninitialized_default_1
<true>
457 template<typename _ForwardIterator
>
459 __uninit_default(_ForwardIterator __first
, _ForwardIterator __last
)
461 typedef typename iterator_traits
<_ForwardIterator
>::value_type
464 std::fill(__first
, __last
, _ValueType());
468 template<bool _TrivialValueType
>
469 struct __uninitialized_default_n_1
471 template<typename _ForwardIterator
, typename _Size
>
473 __uninit_default_n(_ForwardIterator __first
, _Size __n
)
475 _ForwardIterator __cur
= __first
;
478 for (; __n
> 0; --__n
, ++__cur
)
479 std::_Construct(std::__addressof(*__cur
));
483 std::_Destroy(__first
, __cur
);
484 __throw_exception_again
;
490 struct __uninitialized_default_n_1
<true>
492 template<typename _ForwardIterator
, typename _Size
>
494 __uninit_default_n(_ForwardIterator __first
, _Size __n
)
496 typedef typename iterator_traits
<_ForwardIterator
>::value_type
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
>
508 __uninitialized_default(_ForwardIterator __first
,
509 _ForwardIterator __last
)
511 typedef typename iterator_traits
<_ForwardIterator
>::value_type
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
>
522 __uninitialized_default_n(_ForwardIterator __first
, _Size __n
)
524 typedef typename iterator_traits
<_ForwardIterator
>::value_type
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
>
537 __uninitialized_default_a(_ForwardIterator __first
,
538 _ForwardIterator __last
,
541 _ForwardIterator __cur
= __first
;
544 for (; __cur
!= __last
; ++__cur
)
545 __alloc
.construct(std::__addressof(*__cur
));
549 std::_Destroy(__first
, __cur
, __alloc
);
550 __throw_exception_again
;
554 template<typename _ForwardIterator
, typename _Tp
>
556 __uninitialized_default_a(_ForwardIterator __first
,
557 _ForwardIterator __last
,
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
>
567 __uninitialized_default_n_a(_ForwardIterator __first
, _Size __n
,
570 _ForwardIterator __cur
= __first
;
573 for (; __n
> 0; --__n
, ++__cur
)
574 __alloc
.construct(std::__addressof(*__cur
));
578 std::_Destroy(__first
, __cur
, __alloc
);
579 __throw_exception_again
;
583 template<typename _ForwardIterator
, typename _Size
, typename _Tp
>
585 __uninitialized_default_n_a(_ForwardIterator __first
, _Size __n
,
587 { std::__uninitialized_default_n(__first
, __n
); }
590 template<typename _InputIterator
, typename _Size
,
591 typename _ForwardIterator
>
593 __uninitialized_copy_n(_InputIterator __first
, _Size __n
,
594 _ForwardIterator __result
, input_iterator_tag
)
596 _ForwardIterator __cur
= __result
;
599 for (; __n
> 0; --__n
, ++__first
, ++__cur
)
600 std::_Construct(std::__addressof(*__cur
), *__first
);
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.
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
)); }
635 _GLIBCXX_END_NAMESPACE
637 #endif /* _STL_UNINITIALIZED_H */