1 // Raw memory manipulators -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
34 * Hewlett-Packard Company
36 * Permission to use, copy, modify, distribute and sell this software
37 * and its documentation for any purpose is hereby granted without fee,
38 * provided that the above copyright notice appear in all copies and
39 * that both that copyright notice and this permission notice appear
40 * in supporting documentation. Hewlett-Packard Company makes no
41 * representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied warranty.
45 * Copyright (c) 1996,1997
46 * Silicon Graphics Computer Systems, Inc.
48 * Permission to use, copy, modify, distribute and sell this software
49 * and its documentation for any purpose is hereby granted without fee,
50 * provided that the above copyright notice appear in all copies and
51 * that both that copyright notice and this permission notice appear
52 * in supporting documentation. Silicon Graphics makes no
53 * representations about the suitability of this software for any
54 * purpose. It is provided "as is" without express or implied warranty.
57 /** @file stl_uninitialized.h
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
62 #ifndef _STL_UNINITIALIZED_H
63 #define _STL_UNINITIALIZED_H 1
65 _GLIBCXX_BEGIN_NAMESPACE(std
)
68 struct __uninitialized_copy
70 template<typename _InputIterator
, typename _ForwardIterator
>
71 static _ForwardIterator
72 uninitialized_copy(_InputIterator __first
, _InputIterator __last
,
73 _ForwardIterator __result
)
75 _ForwardIterator __cur
= __result
;
78 for (; __first
!= __last
; ++__first
, ++__cur
)
79 ::new(static_cast<void*>(&*__cur
)) typename
80 iterator_traits
<_ForwardIterator
>::value_type(*__first
);
85 std::_Destroy(__result
, __cur
);
86 __throw_exception_again
;
92 struct __uninitialized_copy
<true>
94 template<typename _InputIterator
, typename _ForwardIterator
>
95 static _ForwardIterator
96 uninitialized_copy(_InputIterator __first
, _InputIterator __last
,
97 _ForwardIterator __result
)
98 { return std::copy(__first
, __last
, __result
); }
102 * @brief Copies the range [first,last) into result.
103 * @param first An input iterator.
104 * @param last An input iterator.
105 * @param result An output iterator.
106 * @return result + (first - last)
108 * Like copy(), but does not require an initialized output range.
110 template<typename _InputIterator
, typename _ForwardIterator
>
111 inline _ForwardIterator
112 uninitialized_copy(_InputIterator __first
, _InputIterator __last
,
113 _ForwardIterator __result
)
115 typedef typename iterator_traits
<_InputIterator
>::value_type
117 typedef typename iterator_traits
<_ForwardIterator
>::value_type
120 return std::__uninitialized_copy
<(__is_pod(_ValueType1
)
121 && __is_pod(_ValueType2
))>::
122 uninitialized_copy(__first
, __last
, __result
);
127 struct __uninitialized_fill
129 template<typename _ForwardIterator
, typename _Tp
>
131 uninitialized_fill(_ForwardIterator __first
,
132 _ForwardIterator __last
, const _Tp
& __x
)
134 _ForwardIterator __cur
= __first
;
137 for (; __cur
!= __last
; ++__cur
)
138 std::_Construct(&*__cur
, __x
);
142 std::_Destroy(__first
, __cur
);
143 __throw_exception_again
;
149 struct __uninitialized_fill
<true>
151 template<typename _ForwardIterator
, typename _Tp
>
153 uninitialized_fill(_ForwardIterator __first
,
154 _ForwardIterator __last
, const _Tp
& __x
)
155 { std::fill(__first
, __last
, __x
); }
159 * @brief Copies the value x into the range [first,last).
160 * @param first An input iterator.
161 * @param last An input iterator.
162 * @param x The source value.
165 * Like fill(), but does not require an initialized output range.
167 template<typename _ForwardIterator
, typename _Tp
>
169 uninitialized_fill(_ForwardIterator __first
, _ForwardIterator __last
,
172 typedef typename iterator_traits
<_ForwardIterator
>::value_type
175 std::__uninitialized_fill
<__is_pod(_ValueType
)>::
176 uninitialized_fill(__first
, __last
, __x
);
181 struct __uninitialized_fill_n
183 template<typename _ForwardIterator
, typename _Size
, typename _Tp
>
185 uninitialized_fill_n(_ForwardIterator __first
, _Size __n
,
188 _ForwardIterator __cur
= __first
;
191 for (; __n
> 0; --__n
, ++__cur
)
192 std::_Construct(&*__cur
, __x
);
196 std::_Destroy(__first
, __cur
);
197 __throw_exception_again
;
203 struct __uninitialized_fill_n
<true>
205 template<typename _ForwardIterator
, typename _Size
, typename _Tp
>
207 uninitialized_fill_n(_ForwardIterator __first
, _Size __n
,
209 { std::fill_n(__first
, __n
, __x
); }
213 * @brief Copies the value x into the range [first,first+n).
214 * @param first An input iterator.
215 * @param n The number of copies to make.
216 * @param x The source value.
219 * Like fill_n(), but does not require an initialized output range.
221 template<typename _ForwardIterator
, typename _Size
, typename _Tp
>
223 uninitialized_fill_n(_ForwardIterator __first
, _Size __n
, const _Tp
& __x
)
225 typedef typename iterator_traits
<_ForwardIterator
>::value_type
228 std::__uninitialized_fill_n
<__is_pod(_ValueType
)>::
229 uninitialized_fill_n(__first
, __n
, __x
);
232 // Extensions: versions of uninitialized_copy, uninitialized_fill,
233 // and uninitialized_fill_n that take an allocator parameter.
234 // We dispatch back to the standard versions when we're given the
235 // default allocator. For nondefault allocators we do not use
236 // any of the POD optimizations.
238 template<typename _InputIterator
, typename _ForwardIterator
,
241 __uninitialized_copy_a(_InputIterator __first
, _InputIterator __last
,
242 _ForwardIterator __result
, _Allocator
& __alloc
)
244 _ForwardIterator __cur
= __result
;
247 for (; __first
!= __last
; ++__first
, ++__cur
)
248 __alloc
.construct(&*__cur
, *__first
);
253 std::_Destroy(__result
, __cur
, __alloc
);
254 __throw_exception_again
;
258 template<typename _InputIterator
, typename _ForwardIterator
, typename _Tp
>
259 inline _ForwardIterator
260 __uninitialized_copy_a(_InputIterator __first
, _InputIterator __last
,
261 _ForwardIterator __result
, allocator
<_Tp
>&)
262 { return std::uninitialized_copy(__first
, __last
, __result
); }
264 template<typename _InputIterator
, typename _ForwardIterator
,
266 inline _ForwardIterator
267 __uninitialized_move_a(_InputIterator __first
, _InputIterator __last
,
268 _ForwardIterator __result
, _Allocator
& __alloc
)
270 return std::__uninitialized_copy_a(_GLIBCXX_MAKE_MOVE_ITERATOR(__first
),
271 _GLIBCXX_MAKE_MOVE_ITERATOR(__last
),
275 template<typename _ForwardIterator
, typename _Tp
, typename _Allocator
>
277 __uninitialized_fill_a(_ForwardIterator __first
, _ForwardIterator __last
,
278 const _Tp
& __x
, _Allocator
& __alloc
)
280 _ForwardIterator __cur
= __first
;
283 for (; __cur
!= __last
; ++__cur
)
284 __alloc
.construct(&*__cur
, __x
);
288 std::_Destroy(__first
, __cur
, __alloc
);
289 __throw_exception_again
;
293 template<typename _ForwardIterator
, typename _Tp
, typename _Tp2
>
295 __uninitialized_fill_a(_ForwardIterator __first
, _ForwardIterator __last
,
296 const _Tp
& __x
, allocator
<_Tp2
>&)
297 { std::uninitialized_fill(__first
, __last
, __x
); }
299 template<typename _ForwardIterator
, typename _Size
, typename _Tp
,
302 __uninitialized_fill_n_a(_ForwardIterator __first
, _Size __n
,
303 const _Tp
& __x
, _Allocator
& __alloc
)
305 _ForwardIterator __cur
= __first
;
308 for (; __n
> 0; --__n
, ++__cur
)
309 __alloc
.construct(&*__cur
, __x
);
313 std::_Destroy(__first
, __cur
, __alloc
);
314 __throw_exception_again
;
318 template<typename _ForwardIterator
, typename _Size
, typename _Tp
,
321 __uninitialized_fill_n_a(_ForwardIterator __first
, _Size __n
,
322 const _Tp
& __x
, allocator
<_Tp2
>&)
323 { std::uninitialized_fill_n(__first
, __n
, __x
); }
326 // Extensions: __uninitialized_copy_move, __uninitialized_move_copy,
327 // __uninitialized_fill_move, __uninitialized_move_fill.
328 // All of these algorithms take a user-supplied allocator, which is used
329 // for construction and destruction.
331 // __uninitialized_copy_move
332 // Copies [first1, last1) into [result, result + (last1 - first1)), and
333 // move [first2, last2) into
334 // [result, result + (last1 - first1) + (last2 - first2)).
335 template<typename _InputIterator1
, typename _InputIterator2
,
336 typename _ForwardIterator
, typename _Allocator
>
337 inline _ForwardIterator
338 __uninitialized_copy_move(_InputIterator1 __first1
,
339 _InputIterator1 __last1
,
340 _InputIterator2 __first2
,
341 _InputIterator2 __last2
,
342 _ForwardIterator __result
,
345 _ForwardIterator __mid
= std::__uninitialized_copy_a(__first1
, __last1
,
350 return std::__uninitialized_move_a(__first2
, __last2
, __mid
, __alloc
);
354 std::_Destroy(__result
, __mid
, __alloc
);
355 __throw_exception_again
;
359 // __uninitialized_move_copy
360 // Moves [first1, last1) into [result, result + (last1 - first1)), and
361 // copies [first2, last2) into
362 // [result, result + (last1 - first1) + (last2 - first2)).
363 template<typename _InputIterator1
, typename _InputIterator2
,
364 typename _ForwardIterator
, typename _Allocator
>
365 inline _ForwardIterator
366 __uninitialized_move_copy(_InputIterator1 __first1
,
367 _InputIterator1 __last1
,
368 _InputIterator2 __first2
,
369 _InputIterator2 __last2
,
370 _ForwardIterator __result
,
373 _ForwardIterator __mid
= std::__uninitialized_move_a(__first1
, __last1
,
378 return std::__uninitialized_copy_a(__first2
, __last2
, __mid
, __alloc
);
382 std::_Destroy(__result
, __mid
, __alloc
);
383 __throw_exception_again
;
387 // __uninitialized_fill_move
388 // Fills [result, mid) with x, and moves [first, last) into
389 // [mid, mid + (last - first)).
390 template<typename _ForwardIterator
, typename _Tp
, typename _InputIterator
,
392 inline _ForwardIterator
393 __uninitialized_fill_move(_ForwardIterator __result
, _ForwardIterator __mid
,
394 const _Tp
& __x
, _InputIterator __first
,
395 _InputIterator __last
, _Allocator
& __alloc
)
397 std::__uninitialized_fill_a(__result
, __mid
, __x
, __alloc
);
400 return std::__uninitialized_move_a(__first
, __last
, __mid
, __alloc
);
404 std::_Destroy(__result
, __mid
, __alloc
);
405 __throw_exception_again
;
409 // __uninitialized_move_fill
410 // Moves [first1, last1) into [first2, first2 + (last1 - first1)), and
411 // fills [first2 + (last1 - first1), last2) with x.
412 template<typename _InputIterator
, typename _ForwardIterator
, typename _Tp
,
415 __uninitialized_move_fill(_InputIterator __first1
, _InputIterator __last1
,
416 _ForwardIterator __first2
,
417 _ForwardIterator __last2
, const _Tp
& __x
,
420 _ForwardIterator __mid2
= std::__uninitialized_move_a(__first1
, __last1
,
425 std::__uninitialized_fill_a(__mid2
, __last2
, __x
, __alloc
);
429 std::_Destroy(__first2
, __mid2
, __alloc
);
430 __throw_exception_again
;
434 #ifdef __GXX_EXPERIMENTAL_CXX0X__
435 template<typename _InputIterator
, typename _Size
,
436 typename _ForwardIterator
>
438 __uninitialized_copy_n(_InputIterator __first
, _Size __n
,
439 _ForwardIterator __result
, input_iterator_tag
)
441 _ForwardIterator __cur
= __result
;
444 for (; __n
> 0; --__n
, ++__first
, ++__cur
)
445 ::new(static_cast<void*>(&*__cur
)) typename
446 iterator_traits
<_ForwardIterator
>::value_type(*__first
);
451 std::_Destroy(__result
, __cur
);
452 __throw_exception_again
;
456 template<typename _RandomAccessIterator
, typename _Size
,
457 typename _ForwardIterator
>
458 inline _ForwardIterator
459 __uninitialized_copy_n(_RandomAccessIterator __first
, _Size __n
,
460 _ForwardIterator __result
,
461 random_access_iterator_tag
)
462 { return std::uninitialized_copy(__first
, __first
+ __n
, __result
); }
465 * @brief Copies the range [first,first+n) into result.
466 * @param first An input iterator.
467 * @param n The number of elements to copy.
468 * @param result An output iterator.
471 * Like copy_n(), but does not require an initialized output range.
473 template<typename _InputIterator
, typename _Size
, typename _ForwardIterator
>
474 inline _ForwardIterator
475 uninitialized_copy_n(_InputIterator __first
, _Size __n
,
476 _ForwardIterator __result
)
477 { return std::__uninitialized_copy_n(__first
, __n
, __result
,
478 std::__iterator_category(__first
)); }
481 _GLIBCXX_END_NAMESPACE
483 #endif /* _STL_UNINITIALIZED_H */