1 // Raw memory manipulators -*- C++ -*-
3 // Copyright (C) 2001-2013 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
28 * Hewlett-Packard Company
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
39 * Copyright (c) 1996,1997
40 * Silicon Graphics Computer Systems, Inc.
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
51 /** @file bits/stl_uninitialized.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{memory}
56 #ifndef _STL_UNINITIALIZED_H
57 #define _STL_UNINITIALIZED_H 1
59 namespace std
_GLIBCXX_VISIBILITY(default)
61 _GLIBCXX_BEGIN_NAMESPACE_VERSION
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 typedef __gnu_cxx::__alloc_traits
<_Allocator
> __traits
;
243 for (; __first
!= __last
; ++__first
, ++__cur
)
244 __traits::construct(__alloc
, std::__addressof(*__cur
), *__first
);
249 std::_Destroy(__result
, __cur
, __alloc
);
250 __throw_exception_again
;
254 template<typename _InputIterator
, typename _ForwardIterator
, typename _Tp
>
255 inline _ForwardIterator
256 __uninitialized_copy_a(_InputIterator __first
, _InputIterator __last
,
257 _ForwardIterator __result
, allocator
<_Tp
>&)
258 { return std::uninitialized_copy(__first
, __last
, __result
); }
260 template<typename _InputIterator
, typename _ForwardIterator
,
262 inline _ForwardIterator
263 __uninitialized_move_a(_InputIterator __first
, _InputIterator __last
,
264 _ForwardIterator __result
, _Allocator
& __alloc
)
266 return std::__uninitialized_copy_a(_GLIBCXX_MAKE_MOVE_ITERATOR(__first
),
267 _GLIBCXX_MAKE_MOVE_ITERATOR(__last
),
271 template<typename _InputIterator
, typename _ForwardIterator
,
273 inline _ForwardIterator
274 __uninitialized_move_if_noexcept_a(_InputIterator __first
,
275 _InputIterator __last
,
276 _ForwardIterator __result
,
279 return std::__uninitialized_copy_a
280 (_GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(__first
),
281 _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(__last
), __result
, __alloc
);
284 template<typename _ForwardIterator
, typename _Tp
, typename _Allocator
>
286 __uninitialized_fill_a(_ForwardIterator __first
, _ForwardIterator __last
,
287 const _Tp
& __x
, _Allocator
& __alloc
)
289 _ForwardIterator __cur
= __first
;
292 typedef __gnu_cxx::__alloc_traits
<_Allocator
> __traits
;
293 for (; __cur
!= __last
; ++__cur
)
294 __traits::construct(__alloc
, std::__addressof(*__cur
), __x
);
298 std::_Destroy(__first
, __cur
, __alloc
);
299 __throw_exception_again
;
303 template<typename _ForwardIterator
, typename _Tp
, typename _Tp2
>
305 __uninitialized_fill_a(_ForwardIterator __first
, _ForwardIterator __last
,
306 const _Tp
& __x
, allocator
<_Tp2
>&)
307 { std::uninitialized_fill(__first
, __last
, __x
); }
309 template<typename _ForwardIterator
, typename _Size
, typename _Tp
,
312 __uninitialized_fill_n_a(_ForwardIterator __first
, _Size __n
,
313 const _Tp
& __x
, _Allocator
& __alloc
)
315 _ForwardIterator __cur
= __first
;
318 typedef __gnu_cxx::__alloc_traits
<_Allocator
> __traits
;
319 for (; __n
> 0; --__n
, ++__cur
)
320 __traits::construct(__alloc
, std::__addressof(*__cur
), __x
);
324 std::_Destroy(__first
, __cur
, __alloc
);
325 __throw_exception_again
;
329 template<typename _ForwardIterator
, typename _Size
, typename _Tp
,
332 __uninitialized_fill_n_a(_ForwardIterator __first
, _Size __n
,
333 const _Tp
& __x
, allocator
<_Tp2
>&)
334 { std::uninitialized_fill_n(__first
, __n
, __x
); }
337 // Extensions: __uninitialized_copy_move, __uninitialized_move_copy,
338 // __uninitialized_fill_move, __uninitialized_move_fill.
339 // All of these algorithms take a user-supplied allocator, which is used
340 // for construction and destruction.
342 // __uninitialized_copy_move
343 // Copies [first1, last1) into [result, result + (last1 - first1)), and
344 // move [first2, last2) into
345 // [result, result + (last1 - first1) + (last2 - first2)).
346 template<typename _InputIterator1
, typename _InputIterator2
,
347 typename _ForwardIterator
, typename _Allocator
>
348 inline _ForwardIterator
349 __uninitialized_copy_move(_InputIterator1 __first1
,
350 _InputIterator1 __last1
,
351 _InputIterator2 __first2
,
352 _InputIterator2 __last2
,
353 _ForwardIterator __result
,
356 _ForwardIterator __mid
= std::__uninitialized_copy_a(__first1
, __last1
,
361 return std::__uninitialized_move_a(__first2
, __last2
, __mid
, __alloc
);
365 std::_Destroy(__result
, __mid
, __alloc
);
366 __throw_exception_again
;
370 // __uninitialized_move_copy
371 // Moves [first1, last1) into [result, result + (last1 - first1)), and
372 // copies [first2, last2) into
373 // [result, result + (last1 - first1) + (last2 - first2)).
374 template<typename _InputIterator1
, typename _InputIterator2
,
375 typename _ForwardIterator
, typename _Allocator
>
376 inline _ForwardIterator
377 __uninitialized_move_copy(_InputIterator1 __first1
,
378 _InputIterator1 __last1
,
379 _InputIterator2 __first2
,
380 _InputIterator2 __last2
,
381 _ForwardIterator __result
,
384 _ForwardIterator __mid
= std::__uninitialized_move_a(__first1
, __last1
,
389 return std::__uninitialized_copy_a(__first2
, __last2
, __mid
, __alloc
);
393 std::_Destroy(__result
, __mid
, __alloc
);
394 __throw_exception_again
;
398 // __uninitialized_fill_move
399 // Fills [result, mid) with x, and moves [first, last) into
400 // [mid, mid + (last - first)).
401 template<typename _ForwardIterator
, typename _Tp
, typename _InputIterator
,
403 inline _ForwardIterator
404 __uninitialized_fill_move(_ForwardIterator __result
, _ForwardIterator __mid
,
405 const _Tp
& __x
, _InputIterator __first
,
406 _InputIterator __last
, _Allocator
& __alloc
)
408 std::__uninitialized_fill_a(__result
, __mid
, __x
, __alloc
);
411 return std::__uninitialized_move_a(__first
, __last
, __mid
, __alloc
);
415 std::_Destroy(__result
, __mid
, __alloc
);
416 __throw_exception_again
;
420 // __uninitialized_move_fill
421 // Moves [first1, last1) into [first2, first2 + (last1 - first1)), and
422 // fills [first2 + (last1 - first1), last2) with x.
423 template<typename _InputIterator
, typename _ForwardIterator
, typename _Tp
,
426 __uninitialized_move_fill(_InputIterator __first1
, _InputIterator __last1
,
427 _ForwardIterator __first2
,
428 _ForwardIterator __last2
, const _Tp
& __x
,
431 _ForwardIterator __mid2
= std::__uninitialized_move_a(__first1
, __last1
,
436 std::__uninitialized_fill_a(__mid2
, __last2
, __x
, __alloc
);
440 std::_Destroy(__first2
, __mid2
, __alloc
);
441 __throw_exception_again
;
445 #if __cplusplus >= 201103L
446 // Extensions: __uninitialized_default, __uninitialized_default_n,
447 // __uninitialized_default_a, __uninitialized_default_n_a.
449 template<bool _TrivialValueType
>
450 struct __uninitialized_default_1
452 template<typename _ForwardIterator
>
454 __uninit_default(_ForwardIterator __first
, _ForwardIterator __last
)
456 _ForwardIterator __cur
= __first
;
459 for (; __cur
!= __last
; ++__cur
)
460 std::_Construct(std::__addressof(*__cur
));
464 std::_Destroy(__first
, __cur
);
465 __throw_exception_again
;
471 struct __uninitialized_default_1
<true>
473 template<typename _ForwardIterator
>
475 __uninit_default(_ForwardIterator __first
, _ForwardIterator __last
)
477 typedef typename iterator_traits
<_ForwardIterator
>::value_type
480 std::fill(__first
, __last
, _ValueType());
484 template<bool _TrivialValueType
>
485 struct __uninitialized_default_n_1
487 template<typename _ForwardIterator
, typename _Size
>
489 __uninit_default_n(_ForwardIterator __first
, _Size __n
)
491 _ForwardIterator __cur
= __first
;
494 for (; __n
> 0; --__n
, ++__cur
)
495 std::_Construct(std::__addressof(*__cur
));
499 std::_Destroy(__first
, __cur
);
500 __throw_exception_again
;
506 struct __uninitialized_default_n_1
<true>
508 template<typename _ForwardIterator
, typename _Size
>
510 __uninit_default_n(_ForwardIterator __first
, _Size __n
)
512 typedef typename iterator_traits
<_ForwardIterator
>::value_type
515 std::fill_n(__first
, __n
, _ValueType());
519 // __uninitialized_default
520 // Fills [first, last) with std::distance(first, last) default
521 // constructed value_types(s).
522 template<typename _ForwardIterator
>
524 __uninitialized_default(_ForwardIterator __first
,
525 _ForwardIterator __last
)
527 typedef typename iterator_traits
<_ForwardIterator
>::value_type
530 std::__uninitialized_default_1
<__is_trivial(_ValueType
)>::
531 __uninit_default(__first
, __last
);
534 // __uninitialized_default_n
535 // Fills [first, first + n) with n default constructed value_type(s).
536 template<typename _ForwardIterator
, typename _Size
>
538 __uninitialized_default_n(_ForwardIterator __first
, _Size __n
)
540 typedef typename iterator_traits
<_ForwardIterator
>::value_type
543 std::__uninitialized_default_n_1
<__is_trivial(_ValueType
)>::
544 __uninit_default_n(__first
, __n
);
548 // __uninitialized_default_a
549 // Fills [first, last) with std::distance(first, last) default
550 // constructed value_types(s), constructed with the allocator alloc.
551 template<typename _ForwardIterator
, typename _Allocator
>
553 __uninitialized_default_a(_ForwardIterator __first
,
554 _ForwardIterator __last
,
557 _ForwardIterator __cur
= __first
;
560 typedef __gnu_cxx::__alloc_traits
<_Allocator
> __traits
;
561 for (; __cur
!= __last
; ++__cur
)
562 __traits::construct(__alloc
, std::__addressof(*__cur
));
566 std::_Destroy(__first
, __cur
, __alloc
);
567 __throw_exception_again
;
571 template<typename _ForwardIterator
, typename _Tp
>
573 __uninitialized_default_a(_ForwardIterator __first
,
574 _ForwardIterator __last
,
576 { std::__uninitialized_default(__first
, __last
); }
579 // __uninitialized_default_n_a
580 // Fills [first, first + n) with n default constructed value_types(s),
581 // constructed with the allocator alloc.
582 template<typename _ForwardIterator
, typename _Size
, typename _Allocator
>
584 __uninitialized_default_n_a(_ForwardIterator __first
, _Size __n
,
587 _ForwardIterator __cur
= __first
;
590 typedef __gnu_cxx::__alloc_traits
<_Allocator
> __traits
;
591 for (; __n
> 0; --__n
, ++__cur
)
592 __traits::construct(__alloc
, std::__addressof(*__cur
));
596 std::_Destroy(__first
, __cur
, __alloc
);
597 __throw_exception_again
;
601 template<typename _ForwardIterator
, typename _Size
, typename _Tp
>
603 __uninitialized_default_n_a(_ForwardIterator __first
, _Size __n
,
605 { std::__uninitialized_default_n(__first
, __n
); }
608 template<typename _InputIterator
, typename _Size
,
609 typename _ForwardIterator
>
611 __uninitialized_copy_n(_InputIterator __first
, _Size __n
,
612 _ForwardIterator __result
, input_iterator_tag
)
614 _ForwardIterator __cur
= __result
;
617 for (; __n
> 0; --__n
, ++__first
, ++__cur
)
618 std::_Construct(std::__addressof(*__cur
), *__first
);
623 std::_Destroy(__result
, __cur
);
624 __throw_exception_again
;
628 template<typename _RandomAccessIterator
, typename _Size
,
629 typename _ForwardIterator
>
630 inline _ForwardIterator
631 __uninitialized_copy_n(_RandomAccessIterator __first
, _Size __n
,
632 _ForwardIterator __result
,
633 random_access_iterator_tag
)
634 { return std::uninitialized_copy(__first
, __first
+ __n
, __result
); }
637 * @brief Copies the range [first,first+n) into result.
638 * @param __first An input iterator.
639 * @param __n The number of elements to copy.
640 * @param __result An output iterator.
641 * @return __result + __n
643 * Like copy_n(), but does not require an initialized output range.
645 template<typename _InputIterator
, typename _Size
, typename _ForwardIterator
>
646 inline _ForwardIterator
647 uninitialized_copy_n(_InputIterator __first
, _Size __n
,
648 _ForwardIterator __result
)
649 { return std::__uninitialized_copy_n(__first
, __n
, __result
,
650 std::__iterator_category(__first
)); }
653 _GLIBCXX_END_NAMESPACE_VERSION
656 #endif /* _STL_UNINITIALIZED_H */