2 * Portions Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
5 * Hewlett-Packard Company
7 * Copyright (c) 1996,1997
8 * Silicon Graphics Computer Systems, Inc.
11 * Moscow Center for SPARC Technology
16 * This material is provided "as is", with absolutely no warranty expressed
17 * or implied. Any use is at your own risk.
19 * Permission to use or copy this software for any purpose is hereby granted
20 * without fee, provided the above notices are retained on all copies.
21 * Permission to modify the code and to distribute modified code is granted,
22 * provided the above notices are retained, and a notice that the code was
23 * modified is included with the above copyright notice.
27 /* NOTE: This is an internal header file, included by other STL headers.
28 * You should not attempt to use it directly.
31 #ifndef _STLP_INTERNAL_UNINITIALIZED_H
32 #define _STLP_INTERNAL_UNINITIALIZED_H
34 #ifndef _STLP_INTERNAL_CSTRING
35 # include <stl/_cstring.h>
38 #ifndef _STLP_INTERNAL_ALGOBASE_H
39 # include <stl/_algobase.h>
42 #ifndef _STLP_INTERNAL_CONSTRUCT_H
43 # include <stl/_construct.h>
48 _STLP_MOVE_TO_PRIV_NAMESPACE
52 template <class _InputIter, class _OutputIter, class _Distance>
53 inline _OutputIter __ucopy(_InputIter __first, _InputIter __last,
54 _OutputIter __result, _Distance*) {
55 _OutputIter __cur = __result;
57 for ( ; __first != __last; ++__first, ++__cur)
58 _Param_Construct(&*__cur, *__first);
61 _STLP_UNWIND(_STLP_STD::_Destroy_Range(__result, __cur))
62 _STLP_RET_AFTER_THROW(__cur)
65 template <class _InputIter, class _OutputIter, class _Distance>
66 inline _OutputIter __ucopy(_InputIter __first, _InputIter __last,
67 _OutputIter __result, const input_iterator_tag &, _Distance* __d)
68 { return __ucopy(__first, __last, __result, __d); }
70 #if defined (_STLP_NONTEMPL_BASE_MATCH_BUG)
71 template <class _InputIter, class _OutputIter, class _Distance>
72 inline _OutputIter __ucopy(_InputIter __first, _InputIter __last,
73 _OutputIter __result, const forward_iterator_tag &, _Distance* __d)
74 { return __ucopy(__first, __last, __result, __d); }
76 template <class _InputIter, class _OutputIter, class _Distance>
77 inline _OutputIter __ucopy(_InputIter __first, _InputIter __last,
78 _OutputIter __result, const bidirectional_iterator_tag &, _Distance* __d)
79 { return __ucopy(__first, __last, __result, __d); }
82 template <class _RandomAccessIter, class _OutputIter, class _Distance>
83 inline _OutputIter __ucopy(_RandomAccessIter __first, _RandomAccessIter __last,
84 _OutputIter __result, const random_access_iterator_tag &, _Distance*) {
85 _OutputIter __cur = __result;
87 for (_Distance __n = __last - __first; __n > 0; --__n) {
88 _Param_Construct(&*__cur, *__first);
94 _STLP_UNWIND(_STLP_STD::_Destroy_Range(__result, __cur))
95 _STLP_RET_AFTER_THROW(__cur)
99 template <class _RandomAccessIter, class _OutputIter>
100 inline _OutputIter __ucopy(_RandomAccessIter __first, _RandomAccessIter __last, _OutputIter __result)
101 { return __ucopy(__first, __last, __result, random_access_iterator_tag(), (ptrdiff_t*)0); }
104 __ucopy_trivial(const void* __first, const void* __last, void* __result) {
105 //dums: this version can use memcpy (__copy_trivial can't)
106 return (__last == __first) ? __result :
107 ((char*)memcpy(__result, __first, ((const char*)__last - (const char*)__first))) +
108 ((const char*)__last - (const char*)__first);
111 template <class _InputIter, class _OutputIter>
112 inline _OutputIter __ucopy_ptrs(_InputIter __first, _InputIter __last, _OutputIter __result,
113 const __false_type& /*TrivialUCopy*/)
114 { return __ucopy(__first, __last, __result, random_access_iterator_tag(), (ptrdiff_t*)0); }
116 template <class _InputIter, class _OutputIter>
117 inline _OutputIter __ucopy_ptrs(_InputIter __first, _InputIter __last, _OutputIter __result,
118 const __true_type& /*TrivialUCopy*/) {
119 // we know they all pointers, so this cast is OK
120 // return (_OutputIter)__copy_trivial(&(*__first), &(*__last), &(*__result));
121 return (_OutputIter)__ucopy_trivial(__first, __last, __result);
124 template <class _InputIter, class _OutputIter>
125 inline _OutputIter __ucopy_aux(_InputIter __first, _InputIter __last, _OutputIter __result,
126 const __true_type& /*BothPtrType*/) {
127 return __ucopy_ptrs(__first, __last, __result,
128 _UseTrivialUCopy(_STLP_VALUE_TYPE(__first, _InputIter),
129 _STLP_VALUE_TYPE(__result, _OutputIter))._Answer());
132 template <class _InputIter, class _OutputIter>
133 inline _OutputIter __ucopy_aux(_InputIter __first, _InputIter __last, _OutputIter __result,
134 const __false_type& /*BothPtrType*/) {
135 return __ucopy(__first, __last, __result,
136 _STLP_ITERATOR_CATEGORY(__first, _InputIter),
137 _STLP_DISTANCE_TYPE(__first, _InputIter));
140 _STLP_MOVE_TO_STD_NAMESPACE
142 template <class _InputIter, class _ForwardIter>
144 uninitialized_copy(_InputIter __first, _InputIter __last, _ForwardIter __result)
145 { return _STLP_PRIV __ucopy_aux(__first, __last, __result, _BothPtrType< _InputIter, _ForwardIter>::_Answer()); }
148 uninitialized_copy(const char* __first, const char* __last, char* __result)
149 { return (char*)_STLP_PRIV __ucopy_trivial(__first, __last, __result); }
151 # if defined (_STLP_HAS_WCHAR_T) // dwa 8/15/97
153 uninitialized_copy(const wchar_t* __first, const wchar_t* __last, wchar_t* __result)
154 { return (wchar_t*)_STLP_PRIV __ucopy_trivial (__first, __last, __result); }
157 // uninitialized_copy_n (not part of the C++ standard)
158 _STLP_MOVE_TO_PRIV_NAMESPACE
160 template <class _InputIter, class _Size, class _ForwardIter>
162 pair<_InputIter, _ForwardIter>
163 __ucopy_n(_InputIter __first, _Size __count, _ForwardIter __result,
164 const input_iterator_tag &) {
165 _ForwardIter __cur = __result;
167 for ( ; __count > 0 ; --__count, ++__first, ++__cur)
168 _Param_Construct(&*__cur, *__first);
169 return pair<_InputIter, _ForwardIter>(__first, __cur);
171 _STLP_UNWIND(_STLP_STD::_Destroy_Range(__result, __cur))
172 _STLP_RET_AFTER_THROW((pair<_InputIter, _ForwardIter>(__first, __cur)))
175 # if defined (_STLP_NONTEMPL_BASE_MATCH_BUG)
176 template <class _InputIter, class _Size, class _ForwardIterator>
177 inline pair<_InputIter, _ForwardIterator>
178 __ucopy_n(_InputIter __first, _Size __count,
179 _ForwardIterator __result,
180 const forward_iterator_tag &)
181 { return __ucopy_n(__first, __count, __result, input_iterator_tag()); }
183 template <class _InputIter, class _Size, class _ForwardIterator>
184 inline pair<_InputIter, _ForwardIterator>
185 __ucopy_n(_InputIter __first, _Size __count,
186 _ForwardIterator __result,
187 const bidirectional_iterator_tag &)
188 { return __ucopy_n(__first, __count, __result, input_iterator_tag()); }
191 template <class _RandomAccessIter, class _Size, class _ForwardIter>
192 inline pair<_RandomAccessIter, _ForwardIter>
193 __ucopy_n(_RandomAccessIter __first, _Size __count, _ForwardIter __result,
194 const random_access_iterator_tag &) {
195 _RandomAccessIter __last = __first + __count;
196 return pair<_RandomAccessIter, _ForwardIter>(__last, uninitialized_copy(__first, __last, __result));
199 // This is used internally in <rope> , which is extension itself.
200 template <class _InputIter, class _Size, class _ForwardIter>
201 inline pair<_InputIter, _ForwardIter>
202 __ucopy_n(_InputIter __first, _Size __count, _ForwardIter __result)
203 { return _STLP_PRIV __ucopy_n(__first, __count, __result, _STLP_ITERATOR_CATEGORY(__first, _InputIter)); }
205 #if !defined (_STLP_NO_EXTENSIONS)
207 _STLP_MOVE_TO_STD_NAMESPACE
209 template <class _InputIter, class _Size, class _ForwardIter>
210 inline pair<_InputIter, _ForwardIter>
211 uninitialized_copy_n(_InputIter __first, _Size __count, _ForwardIter __result)
212 { return _STLP_PRIV __ucopy_n(__first, __count, __result); }
214 _STLP_MOVE_TO_PRIV_NAMESPACE
218 template <class _ForwardIter, class _Tp, class _Distance>
219 inline void __ufill(_ForwardIter __first, _ForwardIter __last, const _Tp& __x, _Distance*) {
220 _ForwardIter __cur = __first;
222 for ( ; __cur != __last; ++__cur)
223 _Param_Construct(&*__cur, __x);
225 _STLP_UNWIND(_STLP_STD::_Destroy_Range(__first, __cur))
228 template <class _ForwardIter, class _Tp, class _Distance>
229 inline void __ufill(_ForwardIter __first, _ForwardIter __last,
230 const _Tp& __x, const input_iterator_tag &, _Distance* __d)
231 { __ufill(__first, __last, __x, __d); }
233 #if defined (_STLP_NONTEMPL_BASE_MATCH_BUG)
234 template <class _ForwardIter, class _Tp, class _Distance>
235 inline void __ufill(_ForwardIter __first, _ForwardIter __last,
236 const _Tp& __x, const forward_iterator_tag &, _Distance* __d)
237 { __ufill(__first, __last, __x, __d); }
239 template <class _ForwardIter, class _Tp, class _Distance>
240 inline void __ufill(_ForwardIter __first, _ForwardIter __last,
241 const _Tp& __x, const bidirectional_iterator_tag &, _Distance* __d)
242 { __ufill(__first, __last, __x, __d); }
245 template <class _ForwardIter, class _Tp, class _Distance>
246 inline void __ufill(_ForwardIter __first, _ForwardIter __last,
247 const _Tp& __x, const random_access_iterator_tag &, _Distance*) {
248 _ForwardIter __cur = __first;
250 for (_Distance __n = __last - __first; __n > 0; --__n, ++__cur)
251 _Param_Construct(&*__cur, __x);
253 _STLP_UNWIND(_STLP_STD::_Destroy_Range(__first, __cur))
256 _STLP_MOVE_TO_STD_NAMESPACE
258 template <class _ForwardIter, class _Tp>
259 inline void uninitialized_fill(_ForwardIter __first, _ForwardIter __last, const _Tp& __x) {
260 _STLP_PRIV __ufill(__first, __last, __x,
261 _STLP_ITERATOR_CATEGORY(__first, _ForwardIter),
262 _STLP_DISTANCE_TYPE(__first, _ForwardIter));
265 // Specialization: for one-byte types we can use memset.
266 inline void uninitialized_fill(unsigned char* __first, unsigned char* __last,
267 const unsigned char& __val) {
268 unsigned char __tmp = __val;
269 memset(__first, __tmp, __last - __first);
271 #if !defined (_STLP_NO_SIGNED_BUILTINS)
272 inline void uninitialized_fill(signed char* __first, signed char* __last,
273 const signed char& __val) {
274 signed char __tmp = __val;
275 memset(__first, __STATIC_CAST(unsigned char,__tmp), __last - __first);
278 inline void uninitialized_fill(char* __first, char* __last, const char& __val) {
280 memset(__first, __STATIC_CAST(unsigned char,__tmp), __last - __first);
283 _STLP_MOVE_TO_PRIV_NAMESPACE
285 template <class _ForwardIter, class _Size, class _Tp>
286 inline _ForwardIter __ufill_n(_ForwardIter __first, _Size __n, const _Tp& __x) {
287 _ForwardIter __cur = __first;
289 for ( ; __n > 0; --__n, ++__cur)
290 _Param_Construct(&*__cur, __x);
292 _STLP_UNWIND(_STLP_STD::_Destroy_Range(__first, __cur))
296 template <class _ForwardIter, class _Size, class _Tp>
297 inline _ForwardIter __ufill_n(_ForwardIter __first, _Size __n, const _Tp& __x,
298 const input_iterator_tag &)
299 { return __ufill_n(__first, __n, __x); }
301 #if defined (_STLP_NONTEMPL_BASE_MATCH_BUG)
302 template <class _ForwardIter, class _Size, class _Tp>
303 inline _ForwardIter __ufill_n(_ForwardIter __first, _Size __n, const _Tp& __x,
304 const forward_iterator_tag &)
305 { return __ufill_n(__first, __n, __x); }
307 template <class _ForwardIter, class _Size, class _Tp>
308 inline _ForwardIter __ufill_n(_ForwardIter __first, _Size __n, const _Tp& __x,
309 const bidirectional_iterator_tag &)
310 { return __ufill_n(__first, __n, __x); }
313 template <class _ForwardIter, class _Size, class _Tp>
314 inline _ForwardIter __uninitialized_fill_n(_ForwardIter __first, _Size __n, const _Tp& __x) {
315 _ForwardIter __last = __first + __n;
316 __ufill(__first, __last, __x, random_access_iterator_tag(), (ptrdiff_t*)0);
320 template <class _ForwardIter, class _Size, class _Tp>
321 inline _ForwardIter __ufill_n(_ForwardIter __first, _Size __n, const _Tp& __x,
322 const random_access_iterator_tag &)
323 { return __uninitialized_fill_n(__first, __n, __x); }
325 /* __uninitialized_init is an internal algo to init a range with a value
326 * built using default constructor. It is only called with pointer as
329 template <class _ForwardIter, class _Size, class _Tp>
330 inline _ForwardIter __uinit_aux_aux(_ForwardIter __first, _Size __n, const _Tp& __val,
331 const __false_type& /*_HasDefaultZero*/)
332 { return __uninitialized_fill_n(__first, __n, __val); }
334 template <class _ForwardIter, class _Size, class _Tp>
335 inline _ForwardIter __uinit_aux_aux(_ForwardIter __first, _Size __n, const _Tp& /*__val */,
336 const __true_type& /*_HasDefaultZero*/) {
337 memset((unsigned char*)__first, 0, __n * sizeof(_Tp));
338 return __first + __n;
341 template <class _ForwardIter, class _Size, class _Tp>
342 inline _ForwardIter __uinit_aux(_ForwardIter __first, _Size __n, const _Tp&,
343 const __true_type& /*_TrivialInit*/)
344 { return __first + __n; }
346 template <class _ForwardIter, class _Size, class _Tp>
347 inline _ForwardIter __uinit_aux(_ForwardIter __first, _Size __n, const _Tp& __val,
348 const __false_type& /*_TrivialInit*/)
349 { return __uinit_aux_aux(__first, __n, __val, _HasDefaultZeroValue(__first)._Answer()); }
351 template <class _ForwardIter, class _Size, class _Tp>
352 inline _ForwardIter __uninitialized_init(_ForwardIter __first, _Size __n, const _Tp& __val)
353 { return __uinit_aux(__first, __n, __val, _UseTrivialInit(__first)._Answer()); }
355 _STLP_MOVE_TO_STD_NAMESPACE
357 template <class _ForwardIter, class _Size, class _Tp>
359 uninitialized_fill_n(_ForwardIter __first, _Size __n, const _Tp& __x)
360 { _STLP_PRIV __ufill_n(__first, __n, __x, _STLP_ITERATOR_CATEGORY(__first, _ForwardIter)); }
362 // Extensions: __uninitialized_copy_copy, __uninitialized_copy_fill,
363 // __uninitialized_fill_copy.
365 // __uninitialized_copy_copy
366 // Copies [first1, last1) into [result, result + (last1 - first1)), and
367 // copies [first2, last2) into
368 // [result + (last1 - first1), result + (last1 - first1) + (last2 - first2)).
370 _STLP_MOVE_TO_PRIV_NAMESPACE
372 template <class _InputIter1, class _InputIter2, class _ForwardIter>
374 __uninitialized_copy_copy(_InputIter1 __first1, _InputIter1 __last1,
375 _InputIter2 __first2, _InputIter2 __last2,
376 _ForwardIter __result)
377 { return uninitialized_copy(__first2, __last2, uninitialized_copy(__first1, __last1, __result)); }
379 // __uninitialized_fill_copy
380 // Fills [result, mid) with x, and copies [first, last) into
381 // [mid, mid + (last - first)).
382 template <class _ForwardIter, class _Tp, class _InputIter>
384 __uninitialized_fill_copy(_ForwardIter __result, _ForwardIter __mid, const _Tp& __x,
385 _InputIter __first, _InputIter __last) {
386 uninitialized_fill(__result, __mid, __x);
388 return uninitialized_copy(__first, __last, __mid);
390 _STLP_UNWIND(_STLP_STD::_Destroy_Range(__result, __mid))
391 _STLP_RET_AFTER_THROW(__result)
394 // __uninitialized_copy_fill
395 // Copies [first1, last1) into [first2, first2 + (last1 - first1)), and
396 // fills [first2 + (last1 - first1), last2) with x.
397 template <class _Iter, class _Tp>
399 __uninitialized_copy_fill(_Iter __first1, _Iter __last1, _Iter __first2, _Iter __last2,
401 _Iter __mid2 = uninitialized_copy(__first1, __last1, __first2);
403 uninitialized_fill(__mid2, __last2, __x);
405 _STLP_UNWIND(_STLP_STD::_Destroy_Range(__first2, __mid2))
408 /* __uninitialized_move:
409 * This function is used internaly and only with pointers as iterators.
411 template <class _InputIter, class _ForwardIter, class _TrivialUCpy>
413 __uninitialized_move(_InputIter __first, _InputIter __last, _ForwardIter __result,
414 _TrivialUCpy __trivial_ucpy, const __false_type& /*_Movable*/)
415 { return __ucopy_ptrs(__first, __last, __result, __trivial_ucpy); }
417 template <class _InputIter, class _ForwardIter, class _TrivialUCpy>
420 __uninitialized_move(_InputIter __first, _InputIter __last, _ForwardIter __result,
421 _TrivialUCpy , const __true_type& /*_Movable*/) {
422 //Move constructor should not throw so we do not need to take care of exceptions here.
423 for (ptrdiff_t __n = __last - __first ; __n > 0; --__n) {
424 _Move_Construct(&*__result, *__first);
425 ++__first; ++__result;
430 _STLP_MOVE_TO_STD_NAMESPACE
434 #endif /* _STLP_INTERNAL_UNINITIALIZED_H */