4 * Hewlett-Packard Company
6 * Copyright (c) 1996,1997
7 * Silicon Graphics Computer Systems, Inc.
10 * Moscow Center for SPARC Technology
15 * This material is provided "as is", with absolutely no warranty expressed
16 * or implied. Any use is at your own risk.
18 * Permission to use or copy this software for any purpose is hereby granted
19 * without fee, provided the above notices are retained on all copies.
20 * Permission to modify the code and to distribute modified code is granted,
21 * provided the above notices are retained, and a notice that the code was
22 * modified is included with the above copyright notice.
26 /* NOTE: This is an internal header file, included by other STL headers.
27 * You should not attempt to use it directly.
30 #ifndef _STLP_INTERNAL_UNINITIALIZED_H
31 #define _STLP_INTERNAL_UNINITIALIZED_H
33 #ifndef _STLP_INTERNAL_CSTRING
34 # include <stl/_cstring.h>
37 #ifndef _STLP_INTERNAL_ALGOBASE_H
38 # include <stl/_algobase.h>
41 #ifndef _STLP_INTERNAL_CONSTRUCT_H
42 # include <stl/_construct.h>
47 _STLP_MOVE_TO_PRIV_NAMESPACE
51 template <class _InputIter, class _OutputIter, class _Distance>
52 inline _OutputIter __ucopy(_InputIter __first, _InputIter __last,
53 _OutputIter __result, _Distance*) {
54 _OutputIter __cur = __result;
56 for ( ; __first != __last; ++__first, ++__cur)
57 _Param_Construct(&*__cur, *__first);
60 _STLP_UNWIND(_STLP_STD::_Destroy_Range(__result, __cur))
61 _STLP_RET_AFTER_THROW(__cur)
64 template <class _InputIter, class _OutputIter, class _Distance>
65 inline _OutputIter __ucopy(_InputIter __first, _InputIter __last,
66 _OutputIter __result, const input_iterator_tag &, _Distance* __d)
67 { return __ucopy(__first, __last, __result, __d); }
69 #if defined (_STLP_NONTEMPL_BASE_MATCH_BUG)
70 template <class _InputIter, class _OutputIter, class _Distance>
71 inline _OutputIter __ucopy(_InputIter __first, _InputIter __last,
72 _OutputIter __result, const forward_iterator_tag &, _Distance* __d)
73 { return __ucopy(__first, __last, __result, __d); }
75 template <class _InputIter, class _OutputIter, class _Distance>
76 inline _OutputIter __ucopy(_InputIter __first, _InputIter __last,
77 _OutputIter __result, const bidirectional_iterator_tag &, _Distance* __d)
78 { return __ucopy(__first, __last, __result, __d); }
81 template <class _RandomAccessIter, class _OutputIter, class _Distance>
82 inline _OutputIter __ucopy(_RandomAccessIter __first, _RandomAccessIter __last,
83 _OutputIter __result, const random_access_iterator_tag &, _Distance*) {
84 _OutputIter __cur = __result;
86 for (_Distance __n = __last - __first; __n > 0; --__n) {
87 _Param_Construct(&*__cur, *__first);
93 _STLP_UNWIND(_STLP_STD::_Destroy_Range(__result, __cur))
94 _STLP_RET_AFTER_THROW(__cur)
98 template <class _RandomAccessIter, class _OutputIter>
99 inline _OutputIter __ucopy(_RandomAccessIter __first, _RandomAccessIter __last, _OutputIter __result)
100 { return __ucopy(__first, __last, __result, random_access_iterator_tag(), (ptrdiff_t*)0); }
103 __ucopy_trivial(const void* __first, const void* __last, void* __result) {
104 //dums: this version can use memcpy (__copy_trivial can't)
105 return (__last == __first) ? __result :
106 ((char*)memcpy(__result, __first, ((const char*)__last - (const char*)__first))) +
107 ((const char*)__last - (const char*)__first);
110 template <class _InputIter, class _OutputIter>
111 inline _OutputIter __ucopy_ptrs(_InputIter __first, _InputIter __last, _OutputIter __result,
112 const __false_type& /*TrivialUCopy*/)
113 { return __ucopy(__first, __last, __result, random_access_iterator_tag(), (ptrdiff_t*)0); }
115 template <class _InputIter, class _OutputIter>
116 inline _OutputIter __ucopy_ptrs(_InputIter __first, _InputIter __last, _OutputIter __result,
117 const __true_type& /*TrivialUCopy*/) {
118 // we know they all pointers, so this cast is OK
119 // return (_OutputIter)__copy_trivial(&(*__first), &(*__last), &(*__result));
120 return (_OutputIter)__ucopy_trivial(__first, __last, __result);
123 template <class _InputIter, class _OutputIter>
124 inline _OutputIter __ucopy_aux(_InputIter __first, _InputIter __last, _OutputIter __result,
125 const __true_type& /*BothPtrType*/) {
126 return __ucopy_ptrs(__first, __last, __result,
127 _UseTrivialUCopy(_STLP_VALUE_TYPE(__first, _InputIter),
128 _STLP_VALUE_TYPE(__result, _OutputIter))._Answer());
131 template <class _InputIter, class _OutputIter>
132 inline _OutputIter __ucopy_aux(_InputIter __first, _InputIter __last, _OutputIter __result,
133 const __false_type& /*BothPtrType*/) {
134 return __ucopy(__first, __last, __result,
135 _STLP_ITERATOR_CATEGORY(__first, _InputIter),
136 _STLP_DISTANCE_TYPE(__first, _InputIter));
139 _STLP_MOVE_TO_STD_NAMESPACE
141 template <class _InputIter, class _ForwardIter>
143 uninitialized_copy(_InputIter __first, _InputIter __last, _ForwardIter __result)
144 { return _STLP_PRIV __ucopy_aux(__first, __last, __result, _BothPtrType< _InputIter, _ForwardIter>::_Answer()); }
147 uninitialized_copy(const char* __first, const char* __last, char* __result)
148 { return (char*)_STLP_PRIV __ucopy_trivial(__first, __last, __result); }
150 # if defined (_STLP_HAS_WCHAR_T) // dwa 8/15/97
152 uninitialized_copy(const wchar_t* __first, const wchar_t* __last, wchar_t* __result)
153 { return (wchar_t*)_STLP_PRIV __ucopy_trivial (__first, __last, __result); }
156 // uninitialized_copy_n (not part of the C++ standard)
157 _STLP_MOVE_TO_PRIV_NAMESPACE
159 template <class _InputIter, class _Size, class _ForwardIter>
161 pair<_InputIter, _ForwardIter>
162 __ucopy_n(_InputIter __first, _Size __count, _ForwardIter __result,
163 const input_iterator_tag &) {
164 _ForwardIter __cur = __result;
166 for ( ; __count > 0 ; --__count, ++__first, ++__cur)
167 _Param_Construct(&*__cur, *__first);
168 return pair<_InputIter, _ForwardIter>(__first, __cur);
170 _STLP_UNWIND(_STLP_STD::_Destroy_Range(__result, __cur))
171 _STLP_RET_AFTER_THROW((pair<_InputIter, _ForwardIter>(__first, __cur)))
174 # if defined (_STLP_NONTEMPL_BASE_MATCH_BUG)
175 template <class _InputIter, class _Size, class _ForwardIterator>
176 inline pair<_InputIter, _ForwardIterator>
177 __ucopy_n(_InputIter __first, _Size __count,
178 _ForwardIterator __result,
179 const forward_iterator_tag &)
180 { return __ucopy_n(__first, __count, __result, input_iterator_tag()); }
182 template <class _InputIter, class _Size, class _ForwardIterator>
183 inline pair<_InputIter, _ForwardIterator>
184 __ucopy_n(_InputIter __first, _Size __count,
185 _ForwardIterator __result,
186 const bidirectional_iterator_tag &)
187 { return __ucopy_n(__first, __count, __result, input_iterator_tag()); }
190 template <class _RandomAccessIter, class _Size, class _ForwardIter>
191 inline pair<_RandomAccessIter, _ForwardIter>
192 __ucopy_n(_RandomAccessIter __first, _Size __count, _ForwardIter __result,
193 const random_access_iterator_tag &) {
194 _RandomAccessIter __last = __first + __count;
195 return pair<_RandomAccessIter, _ForwardIter>(__last, uninitialized_copy(__first, __last, __result));
198 // This is used internally in <rope> , which is extension itself.
199 template <class _InputIter, class _Size, class _ForwardIter>
200 inline pair<_InputIter, _ForwardIter>
201 __ucopy_n(_InputIter __first, _Size __count, _ForwardIter __result)
202 { return _STLP_PRIV __ucopy_n(__first, __count, __result, _STLP_ITERATOR_CATEGORY(__first, _InputIter)); }
204 #if !defined (_STLP_NO_EXTENSIONS)
206 _STLP_MOVE_TO_STD_NAMESPACE
208 template <class _InputIter, class _Size, class _ForwardIter>
209 inline pair<_InputIter, _ForwardIter>
210 uninitialized_copy_n(_InputIter __first, _Size __count, _ForwardIter __result)
211 { return _STLP_PRIV __ucopy_n(__first, __count, __result); }
213 _STLP_MOVE_TO_PRIV_NAMESPACE
217 template <class _ForwardIter, class _Tp, class _Distance>
218 inline void __ufill(_ForwardIter __first, _ForwardIter __last, const _Tp& __x, _Distance*) {
219 _ForwardIter __cur = __first;
221 for ( ; __cur != __last; ++__cur)
222 _Param_Construct(&*__cur, __x);
224 _STLP_UNWIND(_STLP_STD::_Destroy_Range(__first, __cur))
227 template <class _ForwardIter, class _Tp, class _Distance>
228 inline void __ufill(_ForwardIter __first, _ForwardIter __last,
229 const _Tp& __x, const input_iterator_tag &, _Distance* __d)
230 { __ufill(__first, __last, __x, __d); }
232 #if defined (_STLP_NONTEMPL_BASE_MATCH_BUG)
233 template <class _ForwardIter, class _Tp, class _Distance>
234 inline void __ufill(_ForwardIter __first, _ForwardIter __last,
235 const _Tp& __x, const forward_iterator_tag &, _Distance* __d)
236 { __ufill(__first, __last, __x, __d); }
238 template <class _ForwardIter, class _Tp, class _Distance>
239 inline void __ufill(_ForwardIter __first, _ForwardIter __last,
240 const _Tp& __x, const bidirectional_iterator_tag &, _Distance* __d)
241 { __ufill(__first, __last, __x, __d); }
244 template <class _ForwardIter, class _Tp, class _Distance>
245 inline void __ufill(_ForwardIter __first, _ForwardIter __last,
246 const _Tp& __x, const random_access_iterator_tag &, _Distance*) {
247 _ForwardIter __cur = __first;
249 for (_Distance __n = __last - __first; __n > 0; --__n, ++__cur)
250 _Param_Construct(&*__cur, __x);
252 _STLP_UNWIND(_STLP_STD::_Destroy_Range(__first, __cur))
255 _STLP_MOVE_TO_STD_NAMESPACE
257 template <class _ForwardIter, class _Tp>
258 inline void uninitialized_fill(_ForwardIter __first, _ForwardIter __last, const _Tp& __x) {
259 _STLP_PRIV __ufill(__first, __last, __x,
260 _STLP_ITERATOR_CATEGORY(__first, _ForwardIter),
261 _STLP_DISTANCE_TYPE(__first, _ForwardIter));
264 // Specialization: for one-byte types we can use memset.
265 inline void uninitialized_fill(unsigned char* __first, unsigned char* __last,
266 const unsigned char& __val) {
267 unsigned char __tmp = __val;
268 memset(__first, __tmp, __last - __first);
270 #if !defined (_STLP_NO_SIGNED_BUILTINS)
271 inline void uninitialized_fill(signed char* __first, signed char* __last,
272 const signed char& __val) {
273 signed char __tmp = __val;
274 memset(__first, __STATIC_CAST(unsigned char,__tmp), __last - __first);
277 inline void uninitialized_fill(char* __first, char* __last, const char& __val) {
279 memset(__first, __STATIC_CAST(unsigned char,__tmp), __last - __first);
282 _STLP_MOVE_TO_PRIV_NAMESPACE
284 template <class _ForwardIter, class _Size, class _Tp>
285 inline _ForwardIter __ufill_n(_ForwardIter __first, _Size __n, const _Tp& __x) {
286 _ForwardIter __cur = __first;
288 for ( ; __n > 0; --__n, ++__cur)
289 _Param_Construct(&*__cur, __x);
291 _STLP_UNWIND(_STLP_STD::_Destroy_Range(__first, __cur))
295 template <class _ForwardIter, class _Size, class _Tp>
296 inline _ForwardIter __ufill_n(_ForwardIter __first, _Size __n, const _Tp& __x,
297 const input_iterator_tag &)
298 { return __ufill_n(__first, __n, __x); }
300 #if defined (_STLP_NONTEMPL_BASE_MATCH_BUG)
301 template <class _ForwardIter, class _Size, class _Tp>
302 inline _ForwardIter __ufill_n(_ForwardIter __first, _Size __n, const _Tp& __x,
303 const forward_iterator_tag &)
304 { return __ufill_n(__first, __n, __x); }
306 template <class _ForwardIter, class _Size, class _Tp>
307 inline _ForwardIter __ufill_n(_ForwardIter __first, _Size __n, const _Tp& __x,
308 const bidirectional_iterator_tag &)
309 { return __ufill_n(__first, __n, __x); }
312 template <class _ForwardIter, class _Size, class _Tp>
313 inline _ForwardIter __uninitialized_fill_n(_ForwardIter __first, _Size __n, const _Tp& __x) {
314 _ForwardIter __last = __first + __n;
315 __ufill(__first, __last, __x, random_access_iterator_tag(), (ptrdiff_t*)0);
319 template <class _ForwardIter, class _Size, class _Tp>
320 inline _ForwardIter __ufill_n(_ForwardIter __first, _Size __n, const _Tp& __x,
321 const random_access_iterator_tag &)
322 { return __uninitialized_fill_n(__first, __n, __x); }
324 /* __uninitialized_init is an internal algo to init a range with a value
325 * built using default constructor. It is only called with pointer as
328 template <class _ForwardIter, class _Size, class _Tp>
329 inline _ForwardIter __uinit_aux_aux(_ForwardIter __first, _Size __n, const _Tp& __val,
330 const __false_type& /*_HasDefaultZero*/)
331 { return __uninitialized_fill_n(__first, __n, __val); }
333 template <class _ForwardIter, class _Size, class _Tp>
334 inline _ForwardIter __uinit_aux_aux(_ForwardIter __first, _Size __n, const _Tp& __val,
335 const __true_type& /*_HasDefaultZero*/) {
336 memset((unsigned char*)__first, 0, __n * sizeof(_Tp));
337 return __first + __n;
340 template <class _ForwardIter, class _Size, class _Tp>
341 inline _ForwardIter __uinit_aux(_ForwardIter __first, _Size __n, const _Tp&,
342 const __true_type& /*_TrivialInit*/)
343 { return __first + __n; }
345 template <class _ForwardIter, class _Size, class _Tp>
346 inline _ForwardIter __uinit_aux(_ForwardIter __first, _Size __n, const _Tp& __val,
347 const __false_type& /*_TrivialInit*/)
348 { return __uinit_aux_aux(__first, __n, __val, _HasDefaultZeroValue(__first)._Answer()); }
350 template <class _ForwardIter, class _Size, class _Tp>
351 inline _ForwardIter __uninitialized_init(_ForwardIter __first, _Size __n, const _Tp& __val)
352 { return __uinit_aux(__first, __n, __val, _UseTrivialInit(__first)._Answer()); }
354 _STLP_MOVE_TO_STD_NAMESPACE
356 template <class _ForwardIter, class _Size, class _Tp>
358 uninitialized_fill_n(_ForwardIter __first, _Size __n, const _Tp& __x)
359 { _STLP_PRIV __ufill_n(__first, __n, __x, _STLP_ITERATOR_CATEGORY(__first, _ForwardIter)); }
361 // Extensions: __uninitialized_copy_copy, __uninitialized_copy_fill,
362 // __uninitialized_fill_copy.
364 // __uninitialized_copy_copy
365 // Copies [first1, last1) into [result, result + (last1 - first1)), and
366 // copies [first2, last2) into
367 // [result + (last1 - first1), result + (last1 - first1) + (last2 - first2)).
369 _STLP_MOVE_TO_PRIV_NAMESPACE
371 template <class _InputIter1, class _InputIter2, class _ForwardIter>
373 __uninitialized_copy_copy(_InputIter1 __first1, _InputIter1 __last1,
374 _InputIter2 __first2, _InputIter2 __last2,
375 _ForwardIter __result)
376 { return uninitialized_copy(__first2, __last2, uninitialized_copy(__first1, __last1, __result)); }
378 // __uninitialized_fill_copy
379 // Fills [result, mid) with x, and copies [first, last) into
380 // [mid, mid + (last - first)).
381 template <class _ForwardIter, class _Tp, class _InputIter>
383 __uninitialized_fill_copy(_ForwardIter __result, _ForwardIter __mid, const _Tp& __x,
384 _InputIter __first, _InputIter __last) {
385 uninitialized_fill(__result, __mid, __x);
387 return uninitialized_copy(__first, __last, __mid);
389 _STLP_UNWIND(_STLP_STD::_Destroy_Range(__result, __mid))
390 _STLP_RET_AFTER_THROW(__result)
393 // __uninitialized_copy_fill
394 // Copies [first1, last1) into [first2, first2 + (last1 - first1)), and
395 // fills [first2 + (last1 - first1), last2) with x.
396 template <class _Iter, class _Tp>
398 __uninitialized_copy_fill(_Iter __first1, _Iter __last1, _Iter __first2, _Iter __last2,
400 _Iter __mid2 = uninitialized_copy(__first1, __last1, __first2);
402 uninitialized_fill(__mid2, __last2, __x);
404 _STLP_UNWIND(_STLP_STD::_Destroy_Range(__first2, __mid2))
407 /* __uninitialized_move:
408 * This function is used internaly and only with pointers as iterators.
410 template <class _InputIter, class _ForwardIter, class _TrivialUCpy>
412 __uninitialized_move(_InputIter __first, _InputIter __last, _ForwardIter __result,
413 _TrivialUCpy __trivial_ucpy, const __false_type& /*_Movable*/)
414 { return __ucopy_ptrs(__first, __last, __result, __trivial_ucpy); }
416 template <class _InputIter, class _ForwardIter, class _TrivialUCpy>
419 __uninitialized_move(_InputIter __first, _InputIter __last, _ForwardIter __result,
420 _TrivialUCpy , const __true_type& /*_Movable*/) {
421 //Move constructor should not throw so we do not need to take care of exceptions here.
422 for (ptrdiff_t __n = __last - __first ; __n > 0; --__n) {
423 _Move_Construct(&*__result, *__first);
424 ++__first; ++__result;
429 _STLP_MOVE_TO_STD_NAMESPACE
433 #endif /* _STLP_INTERNAL_UNINITIALIZED_H */