stl_bvector.h: Change calls to 3-argument distance() into standard 2-argument version.
[official-gcc.git] / libstdc++-v3 / include / bits / stl_uninitialized.h
blobc51ba58c079f30c90851ac9004539a1369b7f2fc
1 // Raw memory manipulators -*- C++ -*-
3 // Copyright (C) 2001 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
32 * Copyright (c) 1994
33 * Hewlett-Packard Company
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Hewlett-Packard Company makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
44 * Copyright (c) 1996,1997
45 * Silicon Graphics Computer Systems, Inc.
47 * Permission to use, copy, modify, distribute and sell this software
48 * and its documentation for any purpose is hereby granted without fee,
49 * provided that the above copyright notice appear in all copies and
50 * that both that copyright notice and this permission notice appear
51 * in supporting documentation. Silicon Graphics makes no
52 * representations about the suitability of this software for any
53 * purpose. It is provided "as is" without express or implied warranty.
56 /** @file stl_uninitialized.h
57 * This is an internal header file, included by other library headers.
58 * You should not attempt to use it directly.
61 #ifndef _CPP_BITS_STL_UNINITIALIZED_H
62 #define _CPP_BITS_STL_UNINITIALIZED_H 1
64 #include <bits/std_cstring.h>
66 namespace std
69 // uninitialized_copy
71 template<typename _InputIter, typename _ForwardIter>
72 inline _ForwardIter
73 __uninitialized_copy_aux(_InputIter __first, _InputIter __last,
74 _ForwardIter __result,
75 __true_type)
76 { return copy(__first, __last, __result); }
78 template<typename _InputIter, typename _ForwardIter>
79 _ForwardIter
80 __uninitialized_copy_aux(_InputIter __first, _InputIter __last,
81 _ForwardIter __result,
82 __false_type)
84 _ForwardIter __cur = __result;
85 try {
86 for ( ; __first != __last; ++__first, ++__cur)
87 _Construct(&*__cur, *__first);
88 return __cur;
90 catch(...)
92 _Destroy(__result, __cur);
93 __throw_exception_again;
97 /**
98 * @brief Copies the range [first,last) into result.
99 * @param first An input iterator.
100 * @param last An input iterator.
101 * @param result An output iterator.
102 * @return result + (first - last)
104 * Like copy(), but does not require an initialized output range.
106 template<typename _InputIter, typename _ForwardIter>
107 inline _ForwardIter
108 uninitialized_copy(_InputIter __first, _InputIter __last, _ForwardIter __result)
110 typedef typename iterator_traits<_InputIter>::value_type _ValueType;
111 typedef typename __type_traits<_ValueType>::is_POD_type _Is_POD;
112 return __uninitialized_copy_aux(__first, __last, __result, _Is_POD());
115 inline char*
116 uninitialized_copy(const char* __first, const char* __last, char* __result)
118 memmove(__result, __first, __last - __first);
119 return __result + (__last - __first);
122 inline wchar_t*
123 uninitialized_copy(const wchar_t* __first, const wchar_t* __last,
124 wchar_t* __result)
126 memmove(__result, __first, sizeof(wchar_t) * (__last - __first));
127 return __result + (__last - __first);
130 // uninitialized_copy_n (not part of the C++ standard)
132 template<typename _InputIter, typename _Size, typename _ForwardIter>
133 pair<_InputIter, _ForwardIter>
134 __uninitialized_copy_n(_InputIter __first, _Size __count,
135 _ForwardIter __result,
136 input_iterator_tag)
138 _ForwardIter __cur = __result;
139 try {
140 for ( ; __count > 0 ; --__count, ++__first, ++__cur)
141 _Construct(&*__cur, *__first);
142 return pair<_InputIter, _ForwardIter>(__first, __cur);
144 catch(...)
146 _Destroy(__result, __cur);
147 __throw_exception_again;
151 template<typename _RandomAccessIter, typename _Size, typename _ForwardIter>
152 inline pair<_RandomAccessIter, _ForwardIter>
153 __uninitialized_copy_n(_RandomAccessIter __first, _Size __count,
154 _ForwardIter __result,
155 random_access_iterator_tag)
157 _RandomAccessIter __last = __first + __count;
158 return pair<_RandomAccessIter, _ForwardIter>(
159 __last,
160 uninitialized_copy(__first, __last, __result));
163 template<typename _InputIter, typename _Size, typename _ForwardIter>
164 inline pair<_InputIter, _ForwardIter>
165 __uninitialized_copy_n(_InputIter __first, _Size __count,
166 _ForwardIter __result) {
167 return __uninitialized_copy_n(__first, __count, __result,
168 __iterator_category(__first));
172 * @brief Copies the range [first,last) into result.
173 * @param first An input iterator.
174 * @param last An input iterator.
175 * @param result An output iterator.
176 * @return result + (first - last)
178 * Like copy(), but does not require an initialized output range.
180 template<typename _InputIter, typename _Size, typename _ForwardIter>
181 inline pair<_InputIter, _ForwardIter>
182 uninitialized_copy_n(_InputIter __first, _Size __count,
183 _ForwardIter __result) {
184 return __uninitialized_copy_n(__first, __count, __result,
185 __iterator_category(__first));
188 // Valid if copy construction is equivalent to assignment, and if the
189 // destructor is trivial.
190 template<typename _ForwardIter, typename _Tp>
191 inline void
192 __uninitialized_fill_aux(_ForwardIter __first, _ForwardIter __last,
193 const _Tp& __x, __true_type)
194 { fill(__first, __last, __x); }
196 template<typename _ForwardIter, typename _Tp>
197 void
198 __uninitialized_fill_aux(_ForwardIter __first, _ForwardIter __last,
199 const _Tp& __x, __false_type)
201 _ForwardIter __cur = __first;
202 try {
203 for ( ; __cur != __last; ++__cur)
204 _Construct(&*__cur, __x);
206 catch(...)
208 _Destroy(__first, __cur);
209 __throw_exception_again;
214 * @brief Copies the value x into the range [first,last).
215 * @param first An input iterator.
216 * @param last An input iterator.
217 * @param x The source value.
218 * @return Nothing.
220 * Like fill(), but does not require an initialized output range.
222 template<typename _ForwardIter, typename _Tp>
223 inline void
224 uninitialized_fill(_ForwardIter __first, _ForwardIter __last, const _Tp& __x)
226 typedef typename iterator_traits<_ForwardIter>::value_type _ValueType;
227 typedef typename __type_traits<_ValueType>::is_POD_type _Is_POD;
228 __uninitialized_fill_aux(__first, __last, __x, _Is_POD());
231 // Valid if copy construction is equivalent to assignment, and if the
232 // destructor is trivial.
233 template<typename _ForwardIter, typename _Size, typename _Tp>
234 inline _ForwardIter
235 __uninitialized_fill_n_aux(_ForwardIter __first, _Size __n,
236 const _Tp& __x, __true_type)
238 return fill_n(__first, __n, __x);
241 template<typename _ForwardIter, typename _Size, typename _Tp>
242 _ForwardIter
243 __uninitialized_fill_n_aux(_ForwardIter __first, _Size __n,
244 const _Tp& __x, __false_type)
246 _ForwardIter __cur = __first;
247 try {
248 for ( ; __n > 0; --__n, ++__cur)
249 _Construct(&*__cur, __x);
250 return __cur;
252 catch(...)
254 _Destroy(__first, __cur);
255 __throw_exception_again;
260 * @brief Copies the value x into the range [first,first+n).
261 * @param first An input iterator.
262 * @param n The number of copies to make.
263 * @param x The source value.
264 * @return first+n
266 * Like fill_n(), but does not require an initialized output range.
268 template<typename _ForwardIter, typename _Size, typename _Tp>
269 inline _ForwardIter
270 uninitialized_fill_n(_ForwardIter __first, _Size __n, const _Tp& __x)
272 typedef typename iterator_traits<_ForwardIter>::value_type _ValueType;
273 typedef typename __type_traits<_ValueType>::is_POD_type _Is_POD;
274 return __uninitialized_fill_n_aux(__first, __n, __x, _Is_POD());
277 // Extensions: __uninitialized_copy_copy, __uninitialized_copy_fill,
278 // __uninitialized_fill_copy.
280 // __uninitialized_copy_copy
281 // Copies [first1, last1) into [result, result + (last1 - first1)), and
282 // copies [first2, last2) into
283 // [result, result + (last1 - first1) + (last2 - first2)).
285 template<typename _InputIter1, typename _InputIter2, typename _ForwardIter>
286 inline _ForwardIter
287 __uninitialized_copy_copy(_InputIter1 __first1, _InputIter1 __last1,
288 _InputIter2 __first2, _InputIter2 __last2,
289 _ForwardIter __result)
291 _ForwardIter __mid = uninitialized_copy(__first1, __last1, __result);
292 try {
293 return uninitialized_copy(__first2, __last2, __mid);
295 catch(...)
297 _Destroy(__result, __mid);
298 __throw_exception_again;
302 // __uninitialized_fill_copy
303 // Fills [result, mid) with x, and copies [first, last) into
304 // [mid, mid + (last - first)).
305 template<typename _ForwardIter, typename _Tp, typename _InputIter>
306 inline _ForwardIter
307 __uninitialized_fill_copy(_ForwardIter __result, _ForwardIter __mid,
308 const _Tp& __x,
309 _InputIter __first, _InputIter __last)
311 uninitialized_fill(__result, __mid, __x);
312 try {
313 return uninitialized_copy(__first, __last, __mid);
315 catch(...)
317 _Destroy(__result, __mid);
318 __throw_exception_again;
322 // __uninitialized_copy_fill
323 // Copies [first1, last1) into [first2, first2 + (last1 - first1)), and
324 // fills [first2 + (last1 - first1), last2) with x.
325 template<typename _InputIter, typename _ForwardIter, typename _Tp>
326 inline void
327 __uninitialized_copy_fill(_InputIter __first1, _InputIter __last1,
328 _ForwardIter __first2, _ForwardIter __last2,
329 const _Tp& __x)
331 _ForwardIter __mid2 = uninitialized_copy(__first1, __last1, __first2);
332 try {
333 uninitialized_fill(__mid2, __last2, __x);
335 catch(...)
337 _Destroy(__first2, __mid2);
338 __throw_exception_again;
342 } // namespace std
344 #endif /* _CPP_BITS_STL_UNINITIALIZED_H */
346 // Local Variables:
347 // mode:C++
348 // End: