1 // Raw memory manipulators -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
29 * Hewlett-Packard Company
31 * Permission to use, copy, modify, distribute and sell this software
32 * and its documentation for any purpose is hereby granted without fee,
33 * provided that the above copyright notice appear in all copies and
34 * that both that copyright notice and this permission notice appear
35 * in supporting documentation. Hewlett-Packard Company makes no
36 * representations about the suitability of this software for any
37 * purpose. It is provided "as is" without express or implied warranty.
40 * Copyright (c) 1996,1997
41 * Silicon Graphics Computer Systems, Inc.
43 * Permission to use, copy, modify, distribute and sell this software
44 * and its documentation for any purpose is hereby granted without fee,
45 * provided that the above copyright notice appear in all copies and
46 * that both that copyright notice and this permission notice appear
47 * in supporting documentation. Silicon Graphics makes no
48 * representations about the suitability of this software for any
49 * purpose. It is provided "as is" without express or implied warranty.
52 /** @file stl_uninitialized.h
53 * This is an internal header file, included by other library headers.
54 * You should not attempt to use it directly.
57 #ifndef _STL_UNINITIALIZED_H
58 #define _STL_UNINITIALIZED_H 1
60 _GLIBCXX_BEGIN_NAMESPACE(std)
63 struct __uninitialized_copy
65 template<typename _InputIterator, typename _ForwardIterator>
66 static _ForwardIterator
67 uninitialized_copy(_InputIterator __first, _InputIterator __last,
68 _ForwardIterator __result)
70 _ForwardIterator __cur = __result;
73 for (; __first != __last; ++__first, ++__cur)
74 std::_Construct(&*__cur, *__first);
79 std::_Destroy(__result, __cur);
80 __throw_exception_again;
86 struct __uninitialized_copy<true>
88 template<typename _InputIterator, typename _ForwardIterator>
89 static _ForwardIterator
90 uninitialized_copy(_InputIterator __first, _InputIterator __last,
91 _ForwardIterator __result)
92 { return std::copy(__first, __last, __result); }
96 * @brief Copies the range [first,last) into result.
97 * @param first An input iterator.
98 * @param last An input iterator.
99 * @param result An output iterator.
100 * @return result + (first - last)
102 * Like copy(), but does not require an initialized output range.
104 template<typename _InputIterator, typename _ForwardIterator>
105 inline _ForwardIterator
106 uninitialized_copy(_InputIterator __first, _InputIterator __last,
107 _ForwardIterator __result)
109 typedef typename iterator_traits<_InputIterator>::value_type
111 typedef typename iterator_traits<_ForwardIterator>::value_type
114 return std::__uninitialized_copy<(__is_pod(_ValueType1)
115 && __is_pod(_ValueType2))>::
116 uninitialized_copy(__first, __last, __result);
121 struct __uninitialized_fill
123 template<typename _ForwardIterator, typename _Tp>
125 uninitialized_fill(_ForwardIterator __first,
126 _ForwardIterator __last, const _Tp& __x)
128 _ForwardIterator __cur = __first;
131 for (; __cur != __last; ++__cur)
132 std::_Construct(&*__cur, __x);
136 std::_Destroy(__first, __cur);
137 __throw_exception_again;
143 struct __uninitialized_fill<true>
145 template<typename _ForwardIterator, typename _Tp>
147 uninitialized_fill(_ForwardIterator __first,
148 _ForwardIterator __last, const _Tp& __x)
149 { std::fill(__first, __last, __x); }
153 * @brief Copies the value x into the range [first,last).
154 * @param first An input iterator.
155 * @param last An input iterator.
156 * @param x The source value.
159 * Like fill(), but does not require an initialized output range.
161 template<typename _ForwardIterator, typename _Tp>
163 uninitialized_fill(_ForwardIterator __first, _ForwardIterator __last,
166 typedef typename iterator_traits<_ForwardIterator>::value_type
169 std::__uninitialized_fill<__is_pod(_ValueType)>::
170 uninitialized_fill(__first, __last, __x);
175 struct __uninitialized_construct_range_dispatch
177 template<typename _ForwardIterator, typename _Tp>
179 __ucr(_ForwardIterator __first, _ForwardIterator __last,
182 if(__first == __last)
185 _ForwardIterator __cur = __first;
188 std::_Construct(&*__first, _GLIBCXX_MOVE(__value));
189 _ForwardIterator __prev = __cur;
191 for(; __cur != __last; ++__cur, ++__prev)
192 std::_Construct(&*__cur, _GLIBCXX_MOVE(*__prev));
193 __value = _GLIBCXX_MOVE(*__prev);
197 std::_Destroy(__first, __cur);
198 __throw_exception_again;
204 struct __uninitialized_construct_range_dispatch<true>
206 template<typename _ForwardIterator, typename _Tp>
208 __ucr(_ForwardIterator, _ForwardIterator, _Tp&) { }
211 // Constructs objects in the range [first, last).
212 // Note that while these new objects will take valid values,
213 // their exact value is not defined. In particular they may
216 // While __value may altered during this algorithm, it will have
217 // the same value when the algorithm finishes, unless one of the
218 // constructions throws.
220 // Requirements: _ForwardIterator::value_type(_Tp&&) is valid.
221 template<typename _ForwardIterator, typename _Tp>
223 __uninitialized_construct_range(_ForwardIterator __first,
224 _ForwardIterator __last,
227 typedef typename std::iterator_traits<_ForwardIterator>::value_type
230 std::__uninitialized_construct_range_dispatch<
231 __has_trivial_constructor(_ValueType)>::
232 __ucr(__first, __last, __value);
237 struct __uninitialized_fill_n
239 template<typename _ForwardIterator, typename _Size, typename _Tp>
241 uninitialized_fill_n(_ForwardIterator __first, _Size __n,
244 _ForwardIterator __cur = __first;
247 for (; __n > 0; --__n, ++__cur)
248 std::_Construct(&*__cur, __x);
252 std::_Destroy(__first, __cur);
253 __throw_exception_again;
259 struct __uninitialized_fill_n<true>
261 template<typename _ForwardIterator, typename _Size, typename _Tp>
263 uninitialized_fill_n(_ForwardIterator __first, _Size __n,
265 { std::fill_n(__first, __n, __x); }
269 * @brief Copies the value x into the range [first,first+n).
270 * @param first An input iterator.
271 * @param n The number of copies to make.
272 * @param x The source value.
275 * Like fill_n(), but does not require an initialized output range.
277 template<typename _ForwardIterator, typename _Size, typename _Tp>
279 uninitialized_fill_n(_ForwardIterator __first, _Size __n, const _Tp& __x)
281 typedef typename iterator_traits<_ForwardIterator>::value_type
284 std::__uninitialized_fill_n<__is_pod(_ValueType)>::
285 uninitialized_fill_n(__first, __n, __x);
288 // Extensions: versions of uninitialized_copy, uninitialized_fill,
289 // and uninitialized_fill_n that take an allocator parameter.
290 // We dispatch back to the standard versions when we're given the
291 // default allocator. For nondefault allocators we do not use
292 // any of the POD optimizations.
294 template<typename _InputIterator, typename _ForwardIterator,
297 __uninitialized_copy_a(_InputIterator __first, _InputIterator __last,
298 _ForwardIterator __result, _Allocator& __alloc)
300 _ForwardIterator __cur = __result;
303 for (; __first != __last; ++__first, ++__cur)
304 __alloc.construct(&*__cur, *__first);
309 std::_Destroy(__result, __cur, __alloc);
310 __throw_exception_again;
314 template<typename _InputIterator, typename _ForwardIterator, typename _Tp>
315 inline _ForwardIterator
316 __uninitialized_copy_a(_InputIterator __first, _InputIterator __last,
317 _ForwardIterator __result, allocator<_Tp>&)
318 { return std::uninitialized_copy(__first, __last, __result); }
320 template<typename _InputIterator, typename _ForwardIterator,
322 inline _ForwardIterator
323 __uninitialized_move_a(_InputIterator __first, _InputIterator __last,
324 _ForwardIterator __result, _Allocator& __alloc)
326 return std::__uninitialized_copy_a(_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
327 _GLIBCXX_MAKE_MOVE_ITERATOR(__last),
331 template<typename _ForwardIterator, typename _Tp, typename _Allocator>
333 __uninitialized_fill_a(_ForwardIterator __first, _ForwardIterator __last,
334 const _Tp& __x, _Allocator& __alloc)
336 _ForwardIterator __cur = __first;
339 for (; __cur != __last; ++__cur)
340 __alloc.construct(&*__cur, __x);
344 std::_Destroy(__first, __cur, __alloc);
345 __throw_exception_again;
349 template<typename _ForwardIterator, typename _Tp, typename _Tp2>
351 __uninitialized_fill_a(_ForwardIterator __first, _ForwardIterator __last,
352 const _Tp& __x, allocator<_Tp2>&)
353 { std::uninitialized_fill(__first, __last, __x); }
355 template<typename _ForwardIterator, typename _Size, typename _Tp,
358 __uninitialized_fill_n_a(_ForwardIterator __first, _Size __n,
359 const _Tp& __x, _Allocator& __alloc)
361 _ForwardIterator __cur = __first;
364 for (; __n > 0; --__n, ++__cur)
365 __alloc.construct(&*__cur, __x);
369 std::_Destroy(__first, __cur, __alloc);
370 __throw_exception_again;
374 template<typename _ForwardIterator, typename _Size, typename _Tp,
377 __uninitialized_fill_n_a(_ForwardIterator __first, _Size __n,
378 const _Tp& __x, allocator<_Tp2>&)
379 { std::uninitialized_fill_n(__first, __n, __x); }
382 // Extensions: __uninitialized_copy_move, __uninitialized_move_copy,
383 // __uninitialized_fill_move, __uninitialized_move_fill.
384 // All of these algorithms take a user-supplied allocator, which is used
385 // for construction and destruction.
387 // __uninitialized_copy_move
388 // Copies [first1, last1) into [result, result + (last1 - first1)), and
389 // move [first2, last2) into
390 // [result, result + (last1 - first1) + (last2 - first2)).
391 template<typename _InputIterator1, typename _InputIterator2,
392 typename _ForwardIterator, typename _Allocator>
393 inline _ForwardIterator
394 __uninitialized_copy_move(_InputIterator1 __first1,
395 _InputIterator1 __last1,
396 _InputIterator2 __first2,
397 _InputIterator2 __last2,
398 _ForwardIterator __result,
401 _ForwardIterator __mid = std::__uninitialized_copy_a(__first1, __last1,
406 return std::__uninitialized_move_a(__first2, __last2, __mid, __alloc);
410 std::_Destroy(__result, __mid, __alloc);
411 __throw_exception_again;
415 // __uninitialized_move_copy
416 // Moves [first1, last1) into [result, result + (last1 - first1)), and
417 // copies [first2, last2) into
418 // [result, result + (last1 - first1) + (last2 - first2)).
419 template<typename _InputIterator1, typename _InputIterator2,
420 typename _ForwardIterator, typename _Allocator>
421 inline _ForwardIterator
422 __uninitialized_move_copy(_InputIterator1 __first1,
423 _InputIterator1 __last1,
424 _InputIterator2 __first2,
425 _InputIterator2 __last2,
426 _ForwardIterator __result,
429 _ForwardIterator __mid = std::__uninitialized_move_a(__first1, __last1,
434 return std::__uninitialized_copy_a(__first2, __last2, __mid, __alloc);
438 std::_Destroy(__result, __mid, __alloc);
439 __throw_exception_again;
443 // __uninitialized_fill_move
444 // Fills [result, mid) with x, and moves [first, last) into
445 // [mid, mid + (last - first)).
446 template<typename _ForwardIterator, typename _Tp, typename _InputIterator,
448 inline _ForwardIterator
449 __uninitialized_fill_move(_ForwardIterator __result, _ForwardIterator __mid,
450 const _Tp& __x, _InputIterator __first,
451 _InputIterator __last, _Allocator& __alloc)
453 std::__uninitialized_fill_a(__result, __mid, __x, __alloc);
456 return std::__uninitialized_move_a(__first, __last, __mid, __alloc);
460 std::_Destroy(__result, __mid, __alloc);
461 __throw_exception_again;
465 // __uninitialized_move_fill
466 // Moves [first1, last1) into [first2, first2 + (last1 - first1)), and
467 // fills [first2 + (last1 - first1), last2) with x.
468 template<typename _InputIterator, typename _ForwardIterator, typename _Tp,
471 __uninitialized_move_fill(_InputIterator __first1, _InputIterator __last1,
472 _ForwardIterator __first2,
473 _ForwardIterator __last2, const _Tp& __x,
476 _ForwardIterator __mid2 = std::__uninitialized_move_a(__first1, __last1,
481 std::__uninitialized_fill_a(__mid2, __last2, __x, __alloc);
485 std::_Destroy(__first2, __mid2, __alloc);
486 __throw_exception_again;
490 #ifdef __GXX_EXPERIMENTAL_CXX0X__
491 template<typename _InputIterator, typename _Size,
492 typename _ForwardIterator>
494 __uninitialized_copy_n(_InputIterator __first, _Size __n,
495 _ForwardIterator __result, input_iterator_tag)
497 _ForwardIterator __cur = __result;
500 for (; __n > 0; --__n, ++__first, ++__cur)
501 ::new(static_cast<void*>(&*__cur)) typename
502 iterator_traits<_ForwardIterator>::value_type(*__first);
507 std::_Destroy(__result, __cur);
508 __throw_exception_again;
512 template<typename _RandomAccessIterator, typename _Size,
513 typename _ForwardIterator>
514 inline _ForwardIterator
515 __uninitialized_copy_n(_RandomAccessIterator __first, _Size __n,
516 _ForwardIterator __result,
517 random_access_iterator_tag)
518 { return std::uninitialized_copy(__first, __first + __n, __result); }
521 * @brief Copies the range [first,first+n) into result.
522 * @param first An input iterator.
523 * @param n The number of elements to copy.
524 * @param result An output iterator.
527 * Like copy_n(), but does not require an initialized output range.
529 template<typename _InputIterator, typename _Size, typename _ForwardIterator>
530 inline _ForwardIterator
531 uninitialized_copy_n(_InputIterator __first, _Size __n,
532 _ForwardIterator __result)
533 { return std::__uninitialized_copy_n(__first, __n, __result,
534 std::__iterator_category(__first)); }
537 _GLIBCXX_END_NAMESPACE
539 #endif /* _STL_UNINITIALIZED_H */