1 // Raw memory manipulators -*- C++ -*-
3 // Copyright (C) 2001-2014 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
114 #if __cplusplus < 201103L
115 const bool __assignable
= true;
117 // trivial types can have deleted assignment
118 typedef typename iterator_traits
<_InputIterator
>::reference _RefType
;
119 const bool __assignable
= is_assignable
<_ValueType1
, _RefType
>::value
;
122 return std::__uninitialized_copy
<__is_trivial(_ValueType1
)
123 && __is_trivial(_ValueType2
)
125 __uninit_copy(__first
, __last
, __result
);
129 template<bool _TrivialValueType
>
130 struct __uninitialized_fill
132 template<typename _ForwardIterator
, typename _Tp
>
134 __uninit_fill(_ForwardIterator __first
, _ForwardIterator __last
,
137 _ForwardIterator __cur
= __first
;
140 for (; __cur
!= __last
; ++__cur
)
141 std::_Construct(std::__addressof(*__cur
), __x
);
145 std::_Destroy(__first
, __cur
);
146 __throw_exception_again
;
152 struct __uninitialized_fill
<true>
154 template<typename _ForwardIterator
, typename _Tp
>
156 __uninit_fill(_ForwardIterator __first
, _ForwardIterator __last
,
158 { std::fill(__first
, __last
, __x
); }
162 * @brief Copies the value x into the range [first,last).
163 * @param __first An input iterator.
164 * @param __last An input iterator.
165 * @param __x The source value.
168 * Like fill(), but does not require an initialized output range.
170 template<typename _ForwardIterator
, typename _Tp
>
172 uninitialized_fill(_ForwardIterator __first
, _ForwardIterator __last
,
175 typedef typename iterator_traits
<_ForwardIterator
>::value_type
177 #if __cplusplus < 201103L
178 const bool __assignable
= true;
180 // trivial types can have deleted assignment
181 const bool __assignable
= is_copy_assignable
<_ValueType
>::value
;
184 std::__uninitialized_fill
<__is_trivial(_ValueType
) && __assignable
>::
185 __uninit_fill(__first
, __last
, __x
);
189 template<bool _TrivialValueType
>
190 struct __uninitialized_fill_n
192 template<typename _ForwardIterator
, typename _Size
, typename _Tp
>
194 __uninit_fill_n(_ForwardIterator __first
, _Size __n
,
197 _ForwardIterator __cur
= __first
;
200 for (; __n
> 0; --__n
, ++__cur
)
201 std::_Construct(std::__addressof(*__cur
), __x
);
205 std::_Destroy(__first
, __cur
);
206 __throw_exception_again
;
212 struct __uninitialized_fill_n
<true>
214 template<typename _ForwardIterator
, typename _Size
, typename _Tp
>
216 __uninit_fill_n(_ForwardIterator __first
, _Size __n
,
218 { std::fill_n(__first
, __n
, __x
); }
222 * @brief Copies the value x into the range [first,first+n).
223 * @param __first An input iterator.
224 * @param __n The number of copies to make.
225 * @param __x The source value.
228 * Like fill_n(), but does not require an initialized output range.
230 template<typename _ForwardIterator
, typename _Size
, typename _Tp
>
232 uninitialized_fill_n(_ForwardIterator __first
, _Size __n
, const _Tp
& __x
)
234 typedef typename iterator_traits
<_ForwardIterator
>::value_type
236 #if __cplusplus < 201103L
237 const bool __assignable
= true;
239 // trivial types can have deleted assignment
240 const bool __assignable
= is_copy_assignable
<_ValueType
>::value
;
243 std::__uninitialized_fill_n
<__is_trivial(_ValueType
) && __assignable
>::
244 __uninit_fill_n(__first
, __n
, __x
);
247 // Extensions: versions of uninitialized_copy, uninitialized_fill,
248 // and uninitialized_fill_n that take an allocator parameter.
249 // We dispatch back to the standard versions when we're given the
250 // default allocator. For nondefault allocators we do not use
251 // any of the POD optimizations.
253 template<typename _InputIterator
, typename _ForwardIterator
,
256 __uninitialized_copy_a(_InputIterator __first
, _InputIterator __last
,
257 _ForwardIterator __result
, _Allocator
& __alloc
)
259 _ForwardIterator __cur
= __result
;
262 typedef __gnu_cxx::__alloc_traits
<_Allocator
> __traits
;
263 for (; __first
!= __last
; ++__first
, ++__cur
)
264 __traits::construct(__alloc
, std::__addressof(*__cur
), *__first
);
269 std::_Destroy(__result
, __cur
, __alloc
);
270 __throw_exception_again
;
274 template<typename _InputIterator
, typename _ForwardIterator
, typename _Tp
>
275 inline _ForwardIterator
276 __uninitialized_copy_a(_InputIterator __first
, _InputIterator __last
,
277 _ForwardIterator __result
, allocator
<_Tp
>&)
278 { return std::uninitialized_copy(__first
, __last
, __result
); }
280 template<typename _InputIterator
, typename _ForwardIterator
,
282 inline _ForwardIterator
283 __uninitialized_move_a(_InputIterator __first
, _InputIterator __last
,
284 _ForwardIterator __result
, _Allocator
& __alloc
)
286 return std::__uninitialized_copy_a(_GLIBCXX_MAKE_MOVE_ITERATOR(__first
),
287 _GLIBCXX_MAKE_MOVE_ITERATOR(__last
),
291 template<typename _InputIterator
, typename _ForwardIterator
,
293 inline _ForwardIterator
294 __uninitialized_move_if_noexcept_a(_InputIterator __first
,
295 _InputIterator __last
,
296 _ForwardIterator __result
,
299 return std::__uninitialized_copy_a
300 (_GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(__first
),
301 _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(__last
), __result
, __alloc
);
304 template<typename _ForwardIterator
, typename _Tp
, typename _Allocator
>
306 __uninitialized_fill_a(_ForwardIterator __first
, _ForwardIterator __last
,
307 const _Tp
& __x
, _Allocator
& __alloc
)
309 _ForwardIterator __cur
= __first
;
312 typedef __gnu_cxx::__alloc_traits
<_Allocator
> __traits
;
313 for (; __cur
!= __last
; ++__cur
)
314 __traits::construct(__alloc
, std::__addressof(*__cur
), __x
);
318 std::_Destroy(__first
, __cur
, __alloc
);
319 __throw_exception_again
;
323 template<typename _ForwardIterator
, typename _Tp
, typename _Tp2
>
325 __uninitialized_fill_a(_ForwardIterator __first
, _ForwardIterator __last
,
326 const _Tp
& __x
, allocator
<_Tp2
>&)
327 { std::uninitialized_fill(__first
, __last
, __x
); }
329 template<typename _ForwardIterator
, typename _Size
, typename _Tp
,
332 __uninitialized_fill_n_a(_ForwardIterator __first
, _Size __n
,
333 const _Tp
& __x
, _Allocator
& __alloc
)
335 _ForwardIterator __cur
= __first
;
338 typedef __gnu_cxx::__alloc_traits
<_Allocator
> __traits
;
339 for (; __n
> 0; --__n
, ++__cur
)
340 __traits::construct(__alloc
, std::__addressof(*__cur
), __x
);
344 std::_Destroy(__first
, __cur
, __alloc
);
345 __throw_exception_again
;
349 template<typename _ForwardIterator
, typename _Size
, typename _Tp
,
352 __uninitialized_fill_n_a(_ForwardIterator __first
, _Size __n
,
353 const _Tp
& __x
, allocator
<_Tp2
>&)
354 { std::uninitialized_fill_n(__first
, __n
, __x
); }
357 // Extensions: __uninitialized_copy_move, __uninitialized_move_copy,
358 // __uninitialized_fill_move, __uninitialized_move_fill.
359 // All of these algorithms take a user-supplied allocator, which is used
360 // for construction and destruction.
362 // __uninitialized_copy_move
363 // Copies [first1, last1) into [result, result + (last1 - first1)), and
364 // move [first2, last2) into
365 // [result, result + (last1 - first1) + (last2 - first2)).
366 template<typename _InputIterator1
, typename _InputIterator2
,
367 typename _ForwardIterator
, typename _Allocator
>
368 inline _ForwardIterator
369 __uninitialized_copy_move(_InputIterator1 __first1
,
370 _InputIterator1 __last1
,
371 _InputIterator2 __first2
,
372 _InputIterator2 __last2
,
373 _ForwardIterator __result
,
376 _ForwardIterator __mid
= std::__uninitialized_copy_a(__first1
, __last1
,
381 return std::__uninitialized_move_a(__first2
, __last2
, __mid
, __alloc
);
385 std::_Destroy(__result
, __mid
, __alloc
);
386 __throw_exception_again
;
390 // __uninitialized_move_copy
391 // Moves [first1, last1) into [result, result + (last1 - first1)), and
392 // copies [first2, last2) into
393 // [result, result + (last1 - first1) + (last2 - first2)).
394 template<typename _InputIterator1
, typename _InputIterator2
,
395 typename _ForwardIterator
, typename _Allocator
>
396 inline _ForwardIterator
397 __uninitialized_move_copy(_InputIterator1 __first1
,
398 _InputIterator1 __last1
,
399 _InputIterator2 __first2
,
400 _InputIterator2 __last2
,
401 _ForwardIterator __result
,
404 _ForwardIterator __mid
= std::__uninitialized_move_a(__first1
, __last1
,
409 return std::__uninitialized_copy_a(__first2
, __last2
, __mid
, __alloc
);
413 std::_Destroy(__result
, __mid
, __alloc
);
414 __throw_exception_again
;
418 // __uninitialized_fill_move
419 // Fills [result, mid) with x, and moves [first, last) into
420 // [mid, mid + (last - first)).
421 template<typename _ForwardIterator
, typename _Tp
, typename _InputIterator
,
423 inline _ForwardIterator
424 __uninitialized_fill_move(_ForwardIterator __result
, _ForwardIterator __mid
,
425 const _Tp
& __x
, _InputIterator __first
,
426 _InputIterator __last
, _Allocator
& __alloc
)
428 std::__uninitialized_fill_a(__result
, __mid
, __x
, __alloc
);
431 return std::__uninitialized_move_a(__first
, __last
, __mid
, __alloc
);
435 std::_Destroy(__result
, __mid
, __alloc
);
436 __throw_exception_again
;
440 // __uninitialized_move_fill
441 // Moves [first1, last1) into [first2, first2 + (last1 - first1)), and
442 // fills [first2 + (last1 - first1), last2) with x.
443 template<typename _InputIterator
, typename _ForwardIterator
, typename _Tp
,
446 __uninitialized_move_fill(_InputIterator __first1
, _InputIterator __last1
,
447 _ForwardIterator __first2
,
448 _ForwardIterator __last2
, const _Tp
& __x
,
451 _ForwardIterator __mid2
= std::__uninitialized_move_a(__first1
, __last1
,
456 std::__uninitialized_fill_a(__mid2
, __last2
, __x
, __alloc
);
460 std::_Destroy(__first2
, __mid2
, __alloc
);
461 __throw_exception_again
;
465 #if __cplusplus >= 201103L
466 // Extensions: __uninitialized_default, __uninitialized_default_n,
467 // __uninitialized_default_a, __uninitialized_default_n_a.
469 template<bool _TrivialValueType
>
470 struct __uninitialized_default_1
472 template<typename _ForwardIterator
>
474 __uninit_default(_ForwardIterator __first
, _ForwardIterator __last
)
476 _ForwardIterator __cur
= __first
;
479 for (; __cur
!= __last
; ++__cur
)
480 std::_Construct(std::__addressof(*__cur
));
484 std::_Destroy(__first
, __cur
);
485 __throw_exception_again
;
491 struct __uninitialized_default_1
<true>
493 template<typename _ForwardIterator
>
495 __uninit_default(_ForwardIterator __first
, _ForwardIterator __last
)
497 typedef typename iterator_traits
<_ForwardIterator
>::value_type
500 std::fill(__first
, __last
, _ValueType());
504 template<bool _TrivialValueType
>
505 struct __uninitialized_default_n_1
507 template<typename _ForwardIterator
, typename _Size
>
509 __uninit_default_n(_ForwardIterator __first
, _Size __n
)
511 _ForwardIterator __cur
= __first
;
514 for (; __n
> 0; --__n
, ++__cur
)
515 std::_Construct(std::__addressof(*__cur
));
519 std::_Destroy(__first
, __cur
);
520 __throw_exception_again
;
526 struct __uninitialized_default_n_1
<true>
528 template<typename _ForwardIterator
, typename _Size
>
530 __uninit_default_n(_ForwardIterator __first
, _Size __n
)
532 typedef typename iterator_traits
<_ForwardIterator
>::value_type
535 std::fill_n(__first
, __n
, _ValueType());
539 // __uninitialized_default
540 // Fills [first, last) with std::distance(first, last) default
541 // constructed value_types(s).
542 template<typename _ForwardIterator
>
544 __uninitialized_default(_ForwardIterator __first
,
545 _ForwardIterator __last
)
547 typedef typename iterator_traits
<_ForwardIterator
>::value_type
549 // trivial types can have deleted assignment
550 const bool __assignable
= is_copy_assignable
<_ValueType
>::value
;
552 std::__uninitialized_default_1
<__is_trivial(_ValueType
)
554 __uninit_default(__first
, __last
);
557 // __uninitialized_default_n
558 // Fills [first, first + n) with n default constructed value_type(s).
559 template<typename _ForwardIterator
, typename _Size
>
561 __uninitialized_default_n(_ForwardIterator __first
, _Size __n
)
563 typedef typename iterator_traits
<_ForwardIterator
>::value_type
565 // trivial types can have deleted assignment
566 const bool __assignable
= is_copy_assignable
<_ValueType
>::value
;
568 std::__uninitialized_default_n_1
<__is_trivial(_ValueType
)
570 __uninit_default_n(__first
, __n
);
574 // __uninitialized_default_a
575 // Fills [first, last) with std::distance(first, last) default
576 // constructed value_types(s), constructed with the allocator alloc.
577 template<typename _ForwardIterator
, typename _Allocator
>
579 __uninitialized_default_a(_ForwardIterator __first
,
580 _ForwardIterator __last
,
583 _ForwardIterator __cur
= __first
;
586 typedef __gnu_cxx::__alloc_traits
<_Allocator
> __traits
;
587 for (; __cur
!= __last
; ++__cur
)
588 __traits::construct(__alloc
, std::__addressof(*__cur
));
592 std::_Destroy(__first
, __cur
, __alloc
);
593 __throw_exception_again
;
597 template<typename _ForwardIterator
, typename _Tp
>
599 __uninitialized_default_a(_ForwardIterator __first
,
600 _ForwardIterator __last
,
602 { std::__uninitialized_default(__first
, __last
); }
605 // __uninitialized_default_n_a
606 // Fills [first, first + n) with n default constructed value_types(s),
607 // constructed with the allocator alloc.
608 template<typename _ForwardIterator
, typename _Size
, typename _Allocator
>
610 __uninitialized_default_n_a(_ForwardIterator __first
, _Size __n
,
613 _ForwardIterator __cur
= __first
;
616 typedef __gnu_cxx::__alloc_traits
<_Allocator
> __traits
;
617 for (; __n
> 0; --__n
, ++__cur
)
618 __traits::construct(__alloc
, std::__addressof(*__cur
));
622 std::_Destroy(__first
, __cur
, __alloc
);
623 __throw_exception_again
;
627 template<typename _ForwardIterator
, typename _Size
, typename _Tp
>
629 __uninitialized_default_n_a(_ForwardIterator __first
, _Size __n
,
631 { std::__uninitialized_default_n(__first
, __n
); }
634 template<typename _InputIterator
, typename _Size
,
635 typename _ForwardIterator
>
637 __uninitialized_copy_n(_InputIterator __first
, _Size __n
,
638 _ForwardIterator __result
, input_iterator_tag
)
640 _ForwardIterator __cur
= __result
;
643 for (; __n
> 0; --__n
, ++__first
, ++__cur
)
644 std::_Construct(std::__addressof(*__cur
), *__first
);
649 std::_Destroy(__result
, __cur
);
650 __throw_exception_again
;
654 template<typename _RandomAccessIterator
, typename _Size
,
655 typename _ForwardIterator
>
656 inline _ForwardIterator
657 __uninitialized_copy_n(_RandomAccessIterator __first
, _Size __n
,
658 _ForwardIterator __result
,
659 random_access_iterator_tag
)
660 { return std::uninitialized_copy(__first
, __first
+ __n
, __result
); }
663 * @brief Copies the range [first,first+n) into result.
664 * @param __first An input iterator.
665 * @param __n The number of elements to copy.
666 * @param __result An output iterator.
667 * @return __result + __n
669 * Like copy_n(), but does not require an initialized output range.
671 template<typename _InputIterator
, typename _Size
, typename _ForwardIterator
>
672 inline _ForwardIterator
673 uninitialized_copy_n(_InputIterator __first
, _Size __n
,
674 _ForwardIterator __result
)
675 { return std::__uninitialized_copy_n(__first
, __n
, __result
,
676 std::__iterator_category(__first
)); }
679 _GLIBCXX_END_NAMESPACE_VERSION
682 #endif /* _STL_UNINITIALIZED_H */