1/*
2 *
3 * Copyright (c) 1994
4 * Hewlett-Packard Company
5 *
6 * Permission to use, copy, modify, distribute and sell this software
7 * and its documentation for any purpose is hereby granted without fee,
8 * provided that the above copyright notice appear in all copies and
9 * that both that copyright notice and this permission notice appear
10 * in supporting documentation.  Hewlett-Packard Company makes no
11 * representations about the suitability of this software for any
12 * purpose.  It is provided "as is" without express or implied warranty.
13 *
14 *
15 * Copyright (c) 1996,1997
16 * Silicon Graphics Computer Systems, Inc.
17 *
18 * Permission to use, copy, modify, distribute and sell this software
19 * and its documentation for any purpose is hereby granted without fee,
20 * provided that the above copyright notice appear in all copies and
21 * that both that copyright notice and this permission notice appear
22 * in supporting documentation.  Silicon Graphics makes no
23 * representations about the suitability of this software for any
24 * purpose.  It is provided "as is" without express or implied warranty.
25 */
26
27/* NOTE: This is an internal header file, included by other STL headers.
28 *   You should not attempt to use it directly.
29 */
30
31#ifndef __SGI_STL_INTERNAL_UNINITIALIZED_H
32#define __SGI_STL_INTERNAL_UNINITIALIZED_H
33
34__STL_BEGIN_NAMESPACE
35
36// uninitialized_copy
37
38// Valid if copy construction is equivalent to assignment, and if the
39//  destructor is trivial.
40template <class _InputIter, class _ForwardIter>
41inline _ForwardIter
42__uninitialized_copy_aux(_InputIter __first, _InputIter __last,
43                         _ForwardIter __result,
44                         __true_type)
45{
46  return copy(__first, __last, __result);
47}
48
49template <class _InputIter, class _ForwardIter>
50_ForwardIter
51__uninitialized_copy_aux(_InputIter __first, _InputIter __last,
52                         _ForwardIter __result,
53                         __false_type)
54{
55  _ForwardIter __cur = __result;
56  __STL_TRY {
57    for ( ; __first != __last; ++__first, ++__cur)
58      construct(&*__cur, *__first);
59    return __cur;
60  }
61  __STL_UNWIND(destroy(__result, __cur));
62}
63
64
65template <class _InputIter, class _ForwardIter, class _Tp>
66inline _ForwardIter
67__uninitialized_copy(_InputIter __first, _InputIter __last,
68                     _ForwardIter __result, _Tp*)
69{
70  typedef typename __type_traits<_Tp>::is_POD_type _Is_POD;
71  return __uninitialized_copy_aux(__first, __last, __result, _Is_POD());
72}
73
74template <class _InputIter, class _ForwardIter>
75inline _ForwardIter
76  uninitialized_copy(_InputIter __first, _InputIter __last,
77                     _ForwardIter __result)
78{
79  return __uninitialized_copy(__first, __last, __result,
80                              __VALUE_TYPE(__result));
81}
82
83inline char* uninitialized_copy(const char* __first, const char* __last,
84                                char* __result) {
85  memmove(__result, __first, __last - __first);
86  return __result + (__last - __first);
87}
88
89inline wchar_t*
90uninitialized_copy(const wchar_t* __first, const wchar_t* __last,
91                   wchar_t* __result)
92{
93  memmove(__result, __first, sizeof(wchar_t) * (__last - __first));
94  return __result + (__last - __first);
95}
96
97// uninitialized_copy_n (not part of the C++ standard)
98
99template <class _InputIter, class _Size, class _ForwardIter>
100pair<_InputIter, _ForwardIter>
101__uninitialized_copy_n(_InputIter __first, _Size __count,
102                       _ForwardIter __result,
103                       input_iterator_tag)
104{
105  _ForwardIter __cur = __result;
106  __STL_TRY {
107    for ( ; __count > 0 ; --__count, ++__first, ++__cur)
108      construct(&*__cur, *__first);
109    return pair<_InputIter, _ForwardIter>(__first, __cur);
110  }
111  __STL_UNWIND(destroy(__result, __cur));
112}
113
114template <class _RandomAccessIter, class _Size, class _ForwardIter>
115inline pair<_RandomAccessIter, _ForwardIter>
116__uninitialized_copy_n(_RandomAccessIter __first, _Size __count,
117                       _ForwardIter __result,
118                       random_access_iterator_tag) {
119  _RandomAccessIter __last = __first + __count;
120  return pair<_RandomAccessIter, _ForwardIter>(
121                 __last,
122                 uninitialized_copy(__first, __last, __result));
123}
124
125template <class _InputIter, class _Size, class _ForwardIter>
126inline pair<_InputIter, _ForwardIter>
127__uninitialized_copy_n(_InputIter __first, _Size __count,
128                     _ForwardIter __result) {
129  return __uninitialized_copy_n(__first, __count, __result,
130                                __ITERATOR_CATEGORY(__first));
131}
132
133template <class _InputIter, class _Size, class _ForwardIter>
134inline pair<_InputIter, _ForwardIter>
135uninitialized_copy_n(_InputIter __first, _Size __count,
136                     _ForwardIter __result) {
137  return __uninitialized_copy_n(__first, __count, __result,
138                                __ITERATOR_CATEGORY(__first));
139}
140
141// Valid if copy construction is equivalent to assignment, and if the
142// destructor is trivial.
143template <class _ForwardIter, class _Tp>
144inline void
145__uninitialized_fill_aux(_ForwardIter __first, _ForwardIter __last,
146                         const _Tp& __x, __true_type)
147{
148  fill(__first, __last, __x);
149}
150
151template <class _ForwardIter, class _Tp>
152void
153__uninitialized_fill_aux(_ForwardIter __first, _ForwardIter __last,
154                         const _Tp& __x, __false_type)
155{
156  _ForwardIter __cur = __first;
157  __STL_TRY {
158    for ( ; __cur != __last; ++__cur)
159      construct(&*__cur, __x);
160  }
161  __STL_UNWIND(destroy(__first, __cur));
162}
163
164template <class _ForwardIter, class _Tp, class _Tp1>
165inline void __uninitialized_fill(_ForwardIter __first,
166                                 _ForwardIter __last, const _Tp& __x, _Tp1*)
167{
168  typedef typename __type_traits<_Tp1>::is_POD_type _Is_POD;
169  __uninitialized_fill_aux(__first, __last, __x, _Is_POD());
170
171}
172
173template <class _ForwardIter, class _Tp>
174inline void uninitialized_fill(_ForwardIter __first,
175                               _ForwardIter __last,
176                               const _Tp& __x)
177{
178  __uninitialized_fill(__first, __last, __x, __VALUE_TYPE(__first));
179}
180
181// Valid if copy construction is equivalent to assignment, and if the
182//  destructor is trivial.
183template <class _ForwardIter, class _Size, class _Tp>
184inline _ForwardIter
185__uninitialized_fill_n_aux(_ForwardIter __first, _Size __n,
186                           const _Tp& __x, __true_type)
187{
188  return fill_n(__first, __n, __x);
189}
190
191template <class _ForwardIter, class _Size, class _Tp>
192_ForwardIter
193__uninitialized_fill_n_aux(_ForwardIter __first, _Size __n,
194                           const _Tp& __x, __false_type)
195{
196  _ForwardIter __cur = __first;
197  __STL_TRY {
198    for ( ; __n > 0; --__n, ++__cur)
199      construct(&*__cur, __x);
200    return __cur;
201  }
202  __STL_UNWIND(destroy(__first, __cur));
203}
204
205template <class _ForwardIter, class _Size, class _Tp, class _Tp1>
206inline _ForwardIter
207__uninitialized_fill_n(_ForwardIter __first, _Size __n, const _Tp& __x, _Tp1*)
208{
209  typedef typename __type_traits<_Tp1>::is_POD_type _Is_POD;
210  return __uninitialized_fill_n_aux(__first, __n, __x, _Is_POD());
211}
212
213template <class _ForwardIter, class _Size, class _Tp>
214inline _ForwardIter
215uninitialized_fill_n(_ForwardIter __first, _Size __n, const _Tp& __x)
216{
217  return __uninitialized_fill_n(__first, __n, __x, __VALUE_TYPE(__first));
218}
219
220// Extensions: __uninitialized_copy_copy, __uninitialized_copy_fill,
221// __uninitialized_fill_copy.
222
223// __uninitialized_copy_copy
224// Copies [first1, last1) into [result, result + (last1 - first1)), and
225//  copies [first2, last2) into
226//  [result, result + (last1 - first1) + (last2 - first2)).
227
228template <class _InputIter1, class _InputIter2, class _ForwardIter>
229inline _ForwardIter
230__uninitialized_copy_copy(_InputIter1 __first1, _InputIter1 __last1,
231                          _InputIter2 __first2, _InputIter2 __last2,
232                          _ForwardIter __result)
233{
234  _ForwardIter __mid = uninitialized_copy(__first1, __last1, __result);
235  __STL_TRY {
236    return uninitialized_copy(__first2, __last2, __mid);
237  }
238  __STL_UNWIND(destroy(__result, __mid));
239}
240
241// __uninitialized_fill_copy
242// Fills [result, mid) with x, and copies [first, last) into
243//  [mid, mid + (last - first)).
244template <class _ForwardIter, class _Tp, class _InputIter>
245inline _ForwardIter
246__uninitialized_fill_copy(_ForwardIter __result, _ForwardIter __mid,
247                          const _Tp& __x,
248                          _InputIter __first, _InputIter __last)
249{
250  uninitialized_fill(__result, __mid, __x);
251  __STL_TRY {
252    return uninitialized_copy(__first, __last, __mid);
253  }
254  __STL_UNWIND(destroy(__result, __mid));
255}
256
257// __uninitialized_copy_fill
258// Copies [first1, last1) into [first2, first2 + (last1 - first1)), and
259//  fills [first2 + (last1 - first1), last2) with x.
260template <class _InputIter, class _ForwardIter, class _Tp>
261inline void
262__uninitialized_copy_fill(_InputIter __first1, _InputIter __last1,
263                          _ForwardIter __first2, _ForwardIter __last2,
264                          const _Tp& __x)
265{
266  _ForwardIter __mid2 = uninitialized_copy(__first1, __last1, __first2);
267  __STL_TRY {
268    uninitialized_fill(__mid2, __last2, __x);
269  }
270  __STL_UNWIND(destroy(__first2, __mid2));
271}
272
273__STL_END_NAMESPACE
274
275#endif /* __SGI_STL_INTERNAL_UNINITIALIZED_H */
276
277// Local Variables:
278// mode:C++
279// End:
280