1 // Raw memory manipulators -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
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 std::_Construct(&*__cur
, *__first
);
84 std::_Destroy(__result
, __cur
);
85 __throw_exception_again
;
91 struct __uninitialized_copy
<true>
93 template<typename _InputIterator
, typename _ForwardIterator
>
94 static _ForwardIterator
95 uninitialized_copy(_InputIterator __first
, _InputIterator __last
,
96 _ForwardIterator __result
)
97 { return std::copy(__first
, __last
, __result
); }
101 * @brief Copies the range [first,last) into result.
102 * @param first An input iterator.
103 * @param last An input iterator.
104 * @param result An output iterator.
105 * @return result + (first - last)
107 * Like copy(), but does not require an initialized output range.
109 template<typename _InputIterator
, typename _ForwardIterator
>
110 inline _ForwardIterator
111 uninitialized_copy(_InputIterator __first
, _InputIterator __last
,
112 _ForwardIterator __result
)
114 typedef typename iterator_traits
<_InputIterator
>::value_type
116 typedef typename iterator_traits
<_ForwardIterator
>::value_type
119 return std::__uninitialized_copy
<(__is_pod(_ValueType1
)
120 && __is_pod(_ValueType2
))>::
121 uninitialized_copy(__first
, __last
, __result
);
126 struct __uninitialized_fill
128 template<typename _ForwardIterator
, typename _Tp
>
130 uninitialized_fill(_ForwardIterator __first
,
131 _ForwardIterator __last
, const _Tp
& __x
)
133 _ForwardIterator __cur
= __first
;
136 for (; __cur
!= __last
; ++__cur
)
137 std::_Construct(&*__cur
, __x
);
141 std::_Destroy(__first
, __cur
);
142 __throw_exception_again
;
148 struct __uninitialized_fill
<true>
150 template<typename _ForwardIterator
, typename _Tp
>
152 uninitialized_fill(_ForwardIterator __first
,
153 _ForwardIterator __last
, const _Tp
& __x
)
154 { std::fill(__first
, __last
, __x
); }
158 * @brief Copies the value x into the range [first,last).
159 * @param first An input iterator.
160 * @param last An input iterator.
161 * @param x The source value.
164 * Like fill(), but does not require an initialized output range.
166 template<typename _ForwardIterator
, typename _Tp
>
168 uninitialized_fill(_ForwardIterator __first
, _ForwardIterator __last
,
171 typedef typename iterator_traits
<_ForwardIterator
>::value_type
174 std::__uninitialized_fill
<__is_pod(_ValueType
)>::
175 uninitialized_fill(__first
, __last
, __x
);
180 struct __uninitialized_fill_n
182 template<typename _ForwardIterator
, typename _Size
, typename _Tp
>
184 uninitialized_fill_n(_ForwardIterator __first
, _Size __n
,
187 _ForwardIterator __cur
= __first
;
190 for (; __n
> 0; --__n
, ++__cur
)
191 std::_Construct(&*__cur
, __x
);
195 std::_Destroy(__first
, __cur
);
196 __throw_exception_again
;
202 struct __uninitialized_fill_n
<true>
204 template<typename _ForwardIterator
, typename _Size
, typename _Tp
>
206 uninitialized_fill_n(_ForwardIterator __first
, _Size __n
,
208 { std::fill_n(__first
, __n
, __x
); }
212 * @brief Copies the value x into the range [first,first+n).
213 * @param first An input iterator.
214 * @param n The number of copies to make.
215 * @param x The source value.
218 * Like fill_n(), but does not require an initialized output range.
220 template<typename _ForwardIterator
, typename _Size
, typename _Tp
>
222 uninitialized_fill_n(_ForwardIterator __first
, _Size __n
, const _Tp
& __x
)
224 typedef typename iterator_traits
<_ForwardIterator
>::value_type
227 std::__uninitialized_fill_n
<__is_pod(_ValueType
)>::
228 uninitialized_fill_n(__first
, __n
, __x
);
231 // Extensions: versions of uninitialized_copy, uninitialized_fill,
232 // and uninitialized_fill_n that take an allocator parameter.
233 // We dispatch back to the standard versions when we're given the
234 // default allocator. For nondefault allocators we do not use
235 // any of the POD optimizations.
237 template<typename _InputIterator
, typename _ForwardIterator
,
240 __uninitialized_copy_a(_InputIterator __first
, _InputIterator __last
,
241 _ForwardIterator __result
, _Allocator
& __alloc
)
243 _ForwardIterator __cur
= __result
;
246 for (; __first
!= __last
; ++__first
, ++__cur
)
247 __alloc
.construct(&*__cur
, *__first
);
252 std::_Destroy(__result
, __cur
, __alloc
);
253 __throw_exception_again
;
257 template<typename _InputIterator
, typename _ForwardIterator
, typename _Tp
>
258 inline _ForwardIterator
259 __uninitialized_copy_a(_InputIterator __first
, _InputIterator __last
,
260 _ForwardIterator __result
, allocator
<_Tp
>&)
261 { return std::uninitialized_copy(__first
, __last
, __result
); }
263 template<typename _ForwardIterator
, typename _Tp
, typename _Allocator
>
265 __uninitialized_fill_a(_ForwardIterator __first
, _ForwardIterator __last
,
266 const _Tp
& __x
, _Allocator
& __alloc
)
268 _ForwardIterator __cur
= __first
;
271 for (; __cur
!= __last
; ++__cur
)
272 __alloc
.construct(&*__cur
, __x
);
276 std::_Destroy(__first
, __cur
, __alloc
);
277 __throw_exception_again
;
281 template<typename _ForwardIterator
, typename _Tp
, typename _Tp2
>
283 __uninitialized_fill_a(_ForwardIterator __first
, _ForwardIterator __last
,
284 const _Tp
& __x
, allocator
<_Tp2
>&)
285 { std::uninitialized_fill(__first
, __last
, __x
); }
287 template<typename _ForwardIterator
, typename _Size
, typename _Tp
,
290 __uninitialized_fill_n_a(_ForwardIterator __first
, _Size __n
,
291 const _Tp
& __x
, _Allocator
& __alloc
)
293 _ForwardIterator __cur
= __first
;
296 for (; __n
> 0; --__n
, ++__cur
)
297 __alloc
.construct(&*__cur
, __x
);
301 std::_Destroy(__first
, __cur
, __alloc
);
302 __throw_exception_again
;
306 template<typename _ForwardIterator
, typename _Size
, typename _Tp
,
309 __uninitialized_fill_n_a(_ForwardIterator __first
, _Size __n
,
310 const _Tp
& __x
, allocator
<_Tp2
>&)
311 { std::uninitialized_fill_n(__first
, __n
, __x
); }
314 // Extensions: __uninitialized_copy_copy, __uninitialized_copy_fill,
315 // __uninitialized_fill_copy. All of these algorithms take a user-
316 // supplied allocator, which is used for construction and destruction.
318 // __uninitialized_copy_copy
319 // Copies [first1, last1) into [result, result + (last1 - first1)), and
320 // copies [first2, last2) into
321 // [result, result + (last1 - first1) + (last2 - first2)).
323 template<typename _InputIterator1
, typename _InputIterator2
,
324 typename _ForwardIterator
, typename _Allocator
>
325 inline _ForwardIterator
326 __uninitialized_copy_copy(_InputIterator1 __first1
,
327 _InputIterator1 __last1
,
328 _InputIterator2 __first2
,
329 _InputIterator2 __last2
,
330 _ForwardIterator __result
,
333 _ForwardIterator __mid
= std::__uninitialized_copy_a(__first1
, __last1
,
338 return std::__uninitialized_copy_a(__first2
, __last2
, __mid
, __alloc
);
342 std::_Destroy(__result
, __mid
, __alloc
);
343 __throw_exception_again
;
347 // __uninitialized_fill_copy
348 // Fills [result, mid) with x, and copies [first, last) into
349 // [mid, mid + (last - first)).
350 template<typename _ForwardIterator
, typename _Tp
, typename _InputIterator
,
352 inline _ForwardIterator
353 __uninitialized_fill_copy(_ForwardIterator __result
, _ForwardIterator __mid
,
354 const _Tp
& __x
, _InputIterator __first
,
355 _InputIterator __last
, _Allocator
& __alloc
)
357 std::__uninitialized_fill_a(__result
, __mid
, __x
, __alloc
);
360 return std::__uninitialized_copy_a(__first
, __last
, __mid
, __alloc
);
364 std::_Destroy(__result
, __mid
, __alloc
);
365 __throw_exception_again
;
369 // __uninitialized_copy_fill
370 // Copies [first1, last1) into [first2, first2 + (last1 - first1)), and
371 // fills [first2 + (last1 - first1), last2) with x.
372 template<typename _InputIterator
, typename _ForwardIterator
, typename _Tp
,
375 __uninitialized_copy_fill(_InputIterator __first1
, _InputIterator __last1
,
376 _ForwardIterator __first2
,
377 _ForwardIterator __last2
, const _Tp
& __x
,
380 _ForwardIterator __mid2
= std::__uninitialized_copy_a(__first1
, __last1
,
385 std::__uninitialized_fill_a(__mid2
, __last2
, __x
, __alloc
);
389 std::_Destroy(__first2
, __mid2
, __alloc
);
390 __throw_exception_again
;
394 _GLIBCXX_END_NAMESPACE
396 #endif /* _STL_UNINITIALIZED_H */