2 * Copyright 2008-2013 NVIDIA Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 * \brief Transforms input ranges using a function object
24 #include <thrust/detail/config.h>
25 #include <thrust/detail/execution_policy.h>
31 /*! \addtogroup algorithms
34 /*! \addtogroup transformations
40 /*! This version of \p transform applies a unary function to each element
41 * of an input sequence and stores the result in the corresponding
42 * position in an output sequence. Specifically, for each iterator
43 * <tt>i</tt> in the range [\p first, \p last) the operation
44 * <tt>op(*i)</tt> is performed and the result is assigned to <tt>*o</tt>,
45 * where <tt>o</tt> is the corresponding output iterator in the range
46 * [\p result, \p result + (\p last - \p first) ). The input and
47 * output sequences may coincide, resulting in an in-place transformation.
49 * The algorithm's execution is parallelized as determined by \p exec.
51 * \param exec The execution policy to use for parallelization.
52 * \param first The beginning of the input sequence.
53 * \param last The end of the input sequence.
54 * \param result The beginning of the output sequence.
55 * \param op The tranformation operation.
56 * \return The end of the output sequence.
58 * \tparam DerivedPolicy The name of the derived execution policy.
59 * \tparam InputIterator is a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a>
60 * and \c InputIterator's \c value_type is convertible to \c UnaryFunction's \c argument_type.
61 * \tparam OutputIterator is a model of <a href="http://www.sgi.com/tech/stl/OutputIterator.html">Output Iterator</a>.
62 * \tparam UnaryFunction is a model of <a href="http://www.sgi.com/tech/stl/UnaryFunction.html">Unary Function</a>
63 * and \c UnaryFunction's \c result_type is convertible to \c OutputIterator's \c value_type.
65 * \pre \p first may equal \p result, but the range <tt>[first, last)</tt> shall not overlap the range <tt>[result, result + (last - first))</tt> otherwise.
67 * The following code snippet demonstrates how to use \p transform to negate a range in-place
68 * using the \p thrust::host execution policy for parallelization:
71 * #include <thrust/transform.h>
72 * #include <thrust/functional.h>
73 * #include <thrust/execution_policy.h>
76 * int data[10] = {-5, 0, 2, -3, 2, 4, 0, -1, 2, 8};
78 * thrust::negate<int> op;
80 * thrust::transform(thrust::host, data, data + 10, data, op); // in-place transformation
82 * // data is now {5, 0, -2, 3, -2, -4, 0, 1, -2, -8};
85 * \see http://www.sgi.com/tech/stl/transform.html
87 template<typename DerivedPolicy,
88 typename InputIterator,
89 typename OutputIterator,
90 typename UnaryFunction>
91 OutputIterator transform(const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
92 InputIterator first, InputIterator last,
93 OutputIterator result,
97 /*! This version of \p transform applies a unary function to each element
98 * of an input sequence and stores the result in the corresponding
99 * position in an output sequence. Specifically, for each iterator
100 * <tt>i</tt> in the range [\p first, \p last) the operation
101 * <tt>op(*i)</tt> is performed and the result is assigned to <tt>*o</tt>,
102 * where <tt>o</tt> is the corresponding output iterator in the range
103 * [\p result, \p result + (\p last - \p first) ). The input and
104 * output sequences may coincide, resulting in an in-place transformation.
106 * \param first The beginning of the input sequence.
107 * \param last The end of the input sequence.
108 * \param result The beginning of the output sequence.
109 * \param op The tranformation operation.
110 * \return The end of the output sequence.
112 * \tparam InputIterator is a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a>
113 * and \c InputIterator's \c value_type is convertible to \c UnaryFunction's \c argument_type.
114 * \tparam OutputIterator is a model of <a href="http://www.sgi.com/tech/stl/OutputIterator.html">Output Iterator</a>.
115 * \tparam UnaryFunction is a model of <a href="http://www.sgi.com/tech/stl/UnaryFunction.html">Unary Function</a>
116 * and \c UnaryFunction's \c result_type is convertible to \c OutputIterator's \c value_type.
118 * \pre \p first may equal \p result, but the range <tt>[first, last)</tt> shall not overlap the range <tt>[result, result + (last - first))</tt> otherwise.
120 * The following code snippet demonstrates how to use \p transform
123 * #include <thrust/transform.h>
124 * #include <thrust/functional.h>
126 * int data[10] = {-5, 0, 2, -3, 2, 4, 0, -1, 2, 8};
128 * thrust::negate<int> op;
130 * thrust::transform(data, data + 10, data, op); // in-place transformation
132 * // data is now {5, 0, -2, 3, -2, -4, 0, 1, -2, -8};
135 * \see http://www.sgi.com/tech/stl/transform.html
137 template<typename InputIterator,
138 typename OutputIterator,
139 typename UnaryFunction>
140 OutputIterator transform(InputIterator first, InputIterator last,
141 OutputIterator result,
145 /*! This version of \p transform applies a binary function to each pair
146 * of elements from two input sequences and stores the result in the
147 * corresponding position in an output sequence. Specifically, for
148 * each iterator <tt>i</tt> in the range [\p first1, \p last1) and
149 * <tt>j = first + (i - first1)</tt> in the range [\p first2, \p last2)
150 * the operation <tt>op(*i,*j)</tt> is performed and the result is
151 * assigned to <tt>*o</tt>, where <tt>o</tt> is the corresponding
152 * output iterator in the range [\p result, \p result + (\p last - \p first) ).
153 * The input and output sequences may coincide, resulting in an
154 * in-place transformation.
156 * The algorithm's execution is parallelized as determined by \p exec.
158 * \param exec The execution policy to use for parallelization.
159 * \param first1 The beginning of the first input sequence.
160 * \param last1 The end of the first input sequence.
161 * \param first2 The beginning of the second input sequence.
162 * \param result The beginning of the output sequence.
163 * \param op The tranformation operation.
164 * \return The end of the output sequence.
166 * \tparam DerivedPolicy The name of the derived execution policy.
167 * \tparam InputIterator1 is a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a>
168 * and \c InputIterator1's \c value_type is convertible to \c BinaryFunction's \c first_argument_type.
169 * \tparam InputIterator2 is a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a>
170 * and \c InputIterator2's \c value_type is convertible to \c BinaryFunction's \c second_argument_type.
171 * \tparam OutputIterator is a model of <a href="http://www.sgi.com/tech/stl/OutputIterator.html">Output Iterator</a>.
172 * \tparam BinaryFunction is a model of <a href="http://www.sgi.com/tech/stl/BinaryFunction.html">Binary Function</a>
173 * and \c BinaryFunction's \c result_type is convertible to \c OutputIterator's \c value_type.
175 * \pre \p first1 may equal \p result, but the range <tt>[first1, last1)</tt> shall not overlap the range <tt>[result, result + (last1 - first1))</tt> otherwise.
176 * \pre \p first2 may equal \p result, but the range <tt>[first2, first2 + (last1 - first1))</tt> shall not overlap the range <tt>[result, result + (last1 - first1))</tt> otherwise.
178 * The following code snippet demonstrates how to use \p transform to compute the sum of two
179 * ranges using the \p thrust::host execution policy for parallelization:
182 * #include <thrust/transform.h>
183 * #include <thrust/functional.h>
184 * #include <thrust/execution_policy.h>
187 * int input1[6] = {-5, 0, 2, 3, 2, 4};
188 * int input2[6] = { 3, 6, -2, 1, 2, 3};
191 * thrust::plus<int> op;
193 * thrust::transform(thrust::host, input1, input1 + 6, input2, output, op);
195 * // output is now {-2, 6, 0, 4, 4, 7};
198 * \see http://www.sgi.com/tech/stl/transform.html
200 template<typename DerivedPolicy,
201 typename InputIterator1,
202 typename InputIterator2,
203 typename OutputIterator,
204 typename BinaryFunction>
205 OutputIterator transform(const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
206 InputIterator1 first1, InputIterator1 last1,
207 InputIterator2 first2,
208 OutputIterator result,
212 /*! This version of \p transform applies a binary function to each pair
213 * of elements from two input sequences and stores the result in the
214 * corresponding position in an output sequence. Specifically, for
215 * each iterator <tt>i</tt> in the range [\p first1, \p last1) and
216 * <tt>j = first + (i - first1)</tt> in the range [\p first2, \p last2)
217 * the operation <tt>op(*i,*j)</tt> is performed and the result is
218 * assigned to <tt>*o</tt>, where <tt>o</tt> is the corresponding
219 * output iterator in the range [\p result, \p result + (\p last - \p first) ).
220 * The input and output sequences may coincide, resulting in an
221 * in-place transformation.
223 * \param first1 The beginning of the first input sequence.
224 * \param last1 The end of the first input sequence.
225 * \param first2 The beginning of the second input sequence.
226 * \param result The beginning of the output sequence.
227 * \param op The tranformation operation.
228 * \return The end of the output sequence.
230 * \tparam InputIterator1 is a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a>
231 * and \c InputIterator1's \c value_type is convertible to \c BinaryFunction's \c first_argument_type.
232 * \tparam InputIterator2 is a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a>
233 * and \c InputIterator2's \c value_type is convertible to \c BinaryFunction's \c second_argument_type.
234 * \tparam OutputIterator is a model of <a href="http://www.sgi.com/tech/stl/OutputIterator.html">Output Iterator</a>.
235 * \tparam BinaryFunction is a model of <a href="http://www.sgi.com/tech/stl/BinaryFunction.html">Binary Function</a>
236 * and \c BinaryFunction's \c result_type is convertible to \c OutputIterator's \c value_type.
238 * \pre \p first1 may equal \p result, but the range <tt>[first1, last1)</tt> shall not overlap the range <tt>[result, result + (last1 - first1))</tt> otherwise.
239 * \pre \p first2 may equal \p result, but the range <tt>[first2, first2 + (last1 - first1))</tt> shall not overlap the range <tt>[result, result + (last1 - first1))</tt> otherwise.
241 * The following code snippet demonstrates how to use \p transform
244 * #include <thrust/transform.h>
245 * #include <thrust/functional.h>
247 * int input1[6] = {-5, 0, 2, 3, 2, 4};
248 * int input2[6] = { 3, 6, -2, 1, 2, 3};
251 * thrust::plus<int> op;
253 * thrust::transform(input1, input1 + 6, input2, output, op);
255 * // output is now {-2, 6, 0, 4, 4, 7};
258 * \see http://www.sgi.com/tech/stl/transform.html
260 template<typename InputIterator1,
261 typename InputIterator2,
262 typename OutputIterator,
263 typename BinaryFunction>
264 OutputIterator transform(InputIterator1 first1, InputIterator1 last1,
265 InputIterator2 first2,
266 OutputIterator result,
270 /*! This version of \p transform_if conditionally applies a unary function
271 * to each element of an input sequence and stores the result in the corresponding
272 * position in an output sequence if the corresponding position in the input sequence
273 * satifies a predicate. Otherwise, the corresponding position in the
274 * output sequence is not modified.
276 * Specifically, for each iterator <tt>i</tt> in the range <tt>[first, last)</tt> the
277 * predicate <tt>pred(*i)</tt> is evaluated. If this predicate
278 * evaluates to \c true, the result of <tt>op(*i)</tt> is assigned to <tt>*o</tt>,
279 * where <tt>o</tt> is the corresponding output iterator in the range
280 * <tt>[result, result + (last - first) )</tt>. Otherwise, <tt>op(*i)</tt> is
281 * not evaluated and no assignment occurs. The input and output sequences may coincide,
282 * resulting in an in-place transformation.
284 * The algorithm's execution is parallelized as determined by \p exec.
286 * \param exec The execution policy to use for parallelization.
287 * \param first The beginning of the input sequence.
288 * \param last The end of the input sequence.
289 * \param result The beginning of the output sequence.
290 * \param op The tranformation operation.
291 * \param pred The predicate operation.
292 * \return The end of the output sequence.
294 * \tparam DerivedPolicy The name of the derived execution policy.
295 * \tparam InputIterator is a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a>,
296 * and \c InputIterator's \c value_type is convertible to \c Predicate's \c argument_type,
297 * and \c InputIterator's \c value_type is convertible to \c UnaryFunction's \c argument_type.
298 * \tparam ForwardIterator is a model of <a href="http://www.sgi.com/tech/stl/ForwardIterator.html">Forward Iterator</a>.
299 * \tparam UnaryFunction is a model of <a href="http://www.sgi.com/tech/stl/UnaryFunction.html">Unary Function</a>
300 * and \c UnaryFunction's \c result_type is convertible to \c OutputIterator's \c value_type.
301 * \tparam Predicate is a model of <a href="http://www.sgi.com/tech/stl/Predicate.html">Predicate</a>.
303 * \pre \p first may equal \p result, but the range <tt>[first, last)</tt> shall not overlap the range <tt>[result, result + (last - first))</tt> otherwise.
305 * The following code snippet demonstrates how to use \p transform_if to negate the odd-valued
306 * elements of a range using the \p thrust::host execution policy for parallelization:
309 * #include <thrust/transform.h>
310 * #include <thrust/functional.h>
311 * #include <thrust/execution_policy.h>
314 * int data[10] = {-5, 0, 2, -3, 2, 4, 0, -1, 2, 8};
318 * __host__ __device__
319 * bool operator()(int x)
325 * thrust::negate<int> op;
326 * thrust::identity<int> identity;
328 * // negate odd elements
329 * thrust::transform_if(thrust::host, data, data + 10, data, op, is_odd()); // in-place transformation
331 * // data is now {5, 0, 2, 3, 2, 4, 0, 1, 2, 8};
334 * \see thrust::transform
336 template<typename DerivedPolicy,
337 typename InputIterator,
338 typename ForwardIterator,
339 typename UnaryFunction,
341 ForwardIterator transform_if(const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
342 InputIterator first, InputIterator last,
343 ForwardIterator result,
348 /*! This version of \p transform_if conditionally applies a unary function
349 * to each element of an input sequence and stores the result in the corresponding
350 * position in an output sequence if the corresponding position in the input sequence
351 * satifies a predicate. Otherwise, the corresponding position in the
352 * output sequence is not modified.
354 * Specifically, for each iterator <tt>i</tt> in the range <tt>[first, last)</tt> the
355 * predicate <tt>pred(*i)</tt> is evaluated. If this predicate
356 * evaluates to \c true, the result of <tt>op(*i)</tt> is assigned to <tt>*o</tt>,
357 * where <tt>o</tt> is the corresponding output iterator in the range
358 * <tt>[result, result + (last - first) )</tt>. Otherwise, <tt>op(*i)</tt> is
359 * not evaluated and no assignment occurs. The input and output sequences may coincide,
360 * resulting in an in-place transformation.
362 * \param first The beginning of the input sequence.
363 * \param last The end of the input sequence.
364 * \param result The beginning of the output sequence.
365 * \param op The tranformation operation.
366 * \param pred The predicate operation.
367 * \return The end of the output sequence.
369 * \tparam InputIterator is a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a>,
370 * and \c InputIterator's \c value_type is convertible to \c Predicate's \c argument_type,
371 * and \c InputIterator's \c value_type is convertible to \c UnaryFunction's \c argument_type.
372 * \tparam ForwardIterator is a model of <a href="http://www.sgi.com/tech/stl/ForwardIterator.html">Forward Iterator</a>.
373 * \tparam UnaryFunction is a model of <a href="http://www.sgi.com/tech/stl/UnaryFunction.html">Unary Function</a>
374 * and \c UnaryFunction's \c result_type is convertible to \c OutputIterator's \c value_type.
375 * \tparam Predicate is a model of <a href="http://www.sgi.com/tech/stl/Predicate.html">Predicate</a>.
377 * \pre \p first may equal \p result, but the range <tt>[first, last)</tt> shall not overlap the range <tt>[result, result + (last - first))</tt> otherwise.
379 * The following code snippet demonstrates how to use \p transform_if:
382 * #include <thrust/transform.h>
383 * #include <thrust/functional.h>
385 * int data[10] = {-5, 0, 2, -3, 2, 4, 0, -1, 2, 8};
389 * __host__ __device__
390 * bool operator()(int x)
396 * thrust::negate<int> op;
397 * thrust::identity<int> identity;
399 * // negate odd elements
400 * thrust::transform_if(data, data + 10, data, op, is_odd()); // in-place transformation
402 * // data is now {5, 0, 2, 3, 2, 4, 0, 1, 2, 8};
405 * \see thrust::transform
407 template<typename InputIterator,
408 typename ForwardIterator,
409 typename UnaryFunction,
411 ForwardIterator transform_if(InputIterator first, InputIterator last,
412 ForwardIterator result,
417 /*! This version of \p transform_if conditionally applies a unary function
418 * to each element of an input sequence and stores the result in the corresponding
419 * position in an output sequence if the corresponding position in a stencil sequence
420 * satisfies a predicate. Otherwise, the corresponding position in the
421 * output sequence is not modified.
423 * Specifically, for each iterator <tt>i</tt> in the range <tt>[first, last)</tt> the
424 * predicate <tt>pred(*s)</tt> is evaluated, where <tt>s</tt> is the corresponding input
425 * iterator in the range <tt>[stencil, stencil + (last - first) )</tt>. If this predicate
426 * evaluates to \c true, the result of <tt>op(*i)</tt> is assigned to <tt>*o</tt>,
427 * where <tt>o</tt> is the corresponding output iterator in the range
428 * <tt>[result, result + (last - first) )</tt>. Otherwise, <tt>op(*i)</tt> is
429 * not evaluated and no assignment occurs. The input and output sequences may coincide,
430 * resulting in an in-place transformation.
432 * The algorithm's execution is parallelized as determined by \p exec.
434 * \param exec The execution policy to use for parallelization.
435 * \param first The beginning of the input sequence.
436 * \param last The end of the input sequence.
437 * \param stencil The beginning of the stencil sequence.
438 * \param result The beginning of the output sequence.
439 * \param op The tranformation operation.
440 * \param pred The predicate operation.
441 * \return The end of the output sequence.
443 * \tparam DerivedPolicy The name of the derived execution policy.
444 * \tparam InputIterator1 is a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a>
445 * and \c InputIterator1's \c value_type is convertible to \c UnaryFunction's \c argument_type.
446 * \tparam InputIterator2 is a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a>
447 * and \c InputIterator2's \c value_type is convertible to \c Predicate's \c argument_type.
448 * \tparam ForwardIterator is a model of <a href="http://www.sgi.com/tech/stl/ForwardIterator.html">Forward Iterator</a>.
449 * \tparam UnaryFunction is a model of <a href="http://www.sgi.com/tech/stl/UnaryFunction.html">Unary Function</a>
450 * and \c UnaryFunction's \c result_type is convertible to \c OutputIterator's \c value_type.
451 * \tparam Predicate is a model of <a href="http://www.sgi.com/tech/stl/Predicate.html">Predicate</a>.
453 * \pre \p first may equal \p result, but the range <tt>[first, last)</tt> shall not overlap the range <tt>[result, result + (last - first))</tt> otherwise.
454 * \pre \p stencil may equal \p result, but the range <tt>[stencil, stencil + (last - first))</tt> shall not overlap the range <tt>[result, result + (last - first))</tt> otherwise.
456 * The following code snippet demonstrates how to use \p transform_if using the \p thrust::host
457 * execution policy for parallelization:
460 * #include <thrust/transform.h>
461 * #include <thrust/functional.h>
462 * #include <thrust/execution_policy.h>
465 * int data[10] = {-5, 0, 2, -3, 2, 4, 0, -1, 2, 8};
466 * int stencil[10] = { 1, 0, 1, 0, 1, 0, 1, 0, 1, 0};
468 * thrust::negate<int> op;
469 * thrust::identity<int> identity;
471 * thrust::transform_if(thrust::host, data, data + 10, stencil, data, op, identity); // in-place transformation
473 * // data is now {5, 0, -2, -3, -2, 4, 0, -1, -2, 8};
476 * \see thrust::transform
478 template<typename DerivedPolicy,
479 typename InputIterator1,
480 typename InputIterator2,
481 typename ForwardIterator,
482 typename UnaryFunction,
484 ForwardIterator transform_if(const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
485 InputIterator1 first, InputIterator1 last,
486 InputIterator2 stencil,
487 ForwardIterator result,
492 /*! This version of \p transform_if conditionally applies a unary function
493 * to each element of an input sequence and stores the result in the corresponding
494 * position in an output sequence if the corresponding position in a stencil sequence
495 * satisfies a predicate. Otherwise, the corresponding position in the
496 * output sequence is not modified.
498 * Specifically, for each iterator <tt>i</tt> in the range <tt>[first, last)</tt> the
499 * predicate <tt>pred(*s)</tt> is evaluated, where <tt>s</tt> is the corresponding input
500 * iterator in the range <tt>[stencil, stencil + (last - first) )</tt>. If this predicate
501 * evaluates to \c true, the result of <tt>op(*i)</tt> is assigned to <tt>*o</tt>,
502 * where <tt>o</tt> is the corresponding output iterator in the range
503 * <tt>[result, result + (last - first) )</tt>. Otherwise, <tt>op(*i)</tt> is
504 * not evaluated and no assignment occurs. The input and output sequences may coincide,
505 * resulting in an in-place transformation.
507 * \param first The beginning of the input sequence.
508 * \param last The end of the input sequence.
509 * \param stencil The beginning of the stencil sequence.
510 * \param result The beginning of the output sequence.
511 * \param op The tranformation operation.
512 * \param pred The predicate operation.
513 * \return The end of the output sequence.
515 * \tparam InputIterator1 is a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a>
516 * and \c InputIterator1's \c value_type is convertible to \c UnaryFunction's \c argument_type.
517 * \tparam InputIterator2 is a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a>
518 * and \c InputIterator2's \c value_type is convertible to \c Predicate's \c argument_type.
519 * \tparam ForwardIterator is a model of <a href="http://www.sgi.com/tech/stl/ForwardIterator.html">Forward Iterator</a>.
520 * \tparam UnaryFunction is a model of <a href="http://www.sgi.com/tech/stl/UnaryFunction.html">Unary Function</a>
521 * and \c UnaryFunction's \c result_type is convertible to \c OutputIterator's \c value_type.
522 * \tparam Predicate is a model of <a href="http://www.sgi.com/tech/stl/Predicate.html">Predicate</a>.
524 * \pre \p first may equal \p result, but the range <tt>[first, last)</tt> shall not overlap the range <tt>[result, result + (last - first))</tt> otherwise.
525 * \pre \p stencil may equal \p result, but the range <tt>[stencil, stencil + (last - first))</tt> shall not overlap the range <tt>[result, result + (last - first))</tt> otherwise.
527 * The following code snippet demonstrates how to use \p transform_if:
530 * #include <thrust/transform.h>
531 * #include <thrust/functional.h>
533 * int data[10] = {-5, 0, 2, -3, 2, 4, 0, -1, 2, 8};
534 * int stencil[10] = { 1, 0, 1, 0, 1, 0, 1, 0, 1, 0};
536 * thrust::negate<int> op;
537 * thrust::identity<int> identity;
539 * thrust::transform_if(data, data + 10, stencil, data, op, identity); // in-place transformation
541 * // data is now {5, 0, -2, -3, -2, 4, 0, -1, -2, 8};
544 * \see thrust::transform
546 template<typename InputIterator1,
547 typename InputIterator2,
548 typename ForwardIterator,
549 typename UnaryFunction,
551 ForwardIterator transform_if(InputIterator1 first, InputIterator1 last,
552 InputIterator2 stencil,
553 ForwardIterator result,
558 /*! This version of \p transform_if conditionally applies a binary function
559 * to each pair of elements from two input sequences and stores the result in the corresponding
560 * position in an output sequence if the corresponding position in a stencil sequence
561 * satifies a predicate. Otherwise, the corresponding position in the
562 * output sequence is not modified.
564 * Specifically, for each iterator <tt>i</tt> in the range <tt>[first1, last1)</tt> and
565 * <tt>j = first2 + (i - first1)</tt> in the range <tt>[first2, first2 + (last1 - first1) )</tt>,
566 * the predicate <tt>pred(*s)</tt> is evaluated, where <tt>s</tt> is the corresponding input
567 * iterator in the range <tt>[stencil, stencil + (last1 - first1) )</tt>. If this predicate
568 * evaluates to \c true, the result of <tt>binary_op(*i,*j)</tt> is assigned to <tt>*o</tt>,
569 * where <tt>o</tt> is the corresponding output iterator in the range
570 * <tt>[result, result + (last1 - first1) )</tt>. Otherwise, <tt>binary_op(*i,*j)</tt> is
571 * not evaluated and no assignment occurs. The input and output sequences may coincide,
572 * resulting in an in-place transformation.
574 * The algorithm's execution is parallelized as determined by \p exec.
576 * \param exec The execution policy to use for parallelization.
577 * \param first1 The beginning of the first input sequence.
578 * \param last1 The end of the first input sequence.
579 * \param first2 The beginning of the second input sequence.
580 * \param stencil The beginning of the stencil sequence.
581 * \param result The beginning of the output sequence.
582 * \param binary_op The transformation operation.
583 * \param pred The predicate operation.
584 * \return The end of the output sequence.
586 * \tparam DerivedPolicy The name of the derived execution policy.
587 * \tparam InputIterator1 is a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a>
588 * and \c InputIterator1's \c value_type is convertible to \c BinaryFunction's \c first_argument_type.
589 * \tparam InputIterator2 is a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a>
590 * and \c InputIterator2's \c value_type is convertible to \c BinaryFunction's \c second_argument_type.
591 * \tparam ForwardIterator is a model of <a href="http://www.sgi.com/tech/stl/ForwardIterator.html">Forward Iterator</a>.
592 * \tparam BinaryFunction is a model of <a href="http://www.sgi.com/tech/stl/BinaryFunction.html">Binary Function</a>
593 * and \c BinaryFunction's \c result_type is convertible to \c OutputIterator's \c value_type.
594 * \tparam Predicate is a model of <a href="http://www.sgi.com/tech/stl/Predicate.html">Predicate</a>.
596 * \pre \p first1 may equal \p result, but the range <tt>[first1, last1)</tt> shall not overlap the range <tt>[result, result + (last1 - first1))</tt> otherwise.
597 * \pre \p first2 may equal \p result, but the range <tt>[first2, first2 + (last1 - first1))</tt> shall not overlap the range <tt>[result, result + (last1 - first1))</tt> otherwise.
598 * \pre \p stencil may equal \p result, but the range <tt>[stencil, stencil + (last1 - first1))</tt> shall not overlap the range <tt>[result, result + (last1 - first1))</tt> otherwise.
600 * The following code snippet demonstrates how to use \p transform_if using the \p thrust::host
601 * execution policy for parallelization:
604 * #include <thrust/transform.h>
605 * #include <thrust/functional.h>
606 * #include <thrust/execution_policy.h>
609 * int input1[6] = {-5, 0, 2, 3, 2, 4};
610 * int input2[6] = { 3, 6, -2, 1, 2, 3};
611 * int stencil[8] = { 1, 0, 1, 0, 1, 0};
614 * thrust::plus<int> op;
615 * thrust::identity<int> identity;
617 * thrust::transform_if(thrust::host, input1, input1 + 6, input2, stencil, output, op, identity);
619 * // output is now {-2, 0, 0, 3, 4, 4};
622 * \see thrust::transform
624 template<typename DerivedPolicy,
625 typename InputIterator1,
626 typename InputIterator2,
627 typename InputIterator3,
628 typename ForwardIterator,
629 typename BinaryFunction,
631 ForwardIterator transform_if(const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
632 InputIterator1 first1, InputIterator1 last1,
633 InputIterator2 first2,
634 InputIterator3 stencil,
635 ForwardIterator result,
636 BinaryFunction binary_op,
640 /*! This version of \p transform_if conditionally applies a binary function
641 * to each pair of elements from two input sequences and stores the result in the corresponding
642 * position in an output sequence if the corresponding position in a stencil sequence
643 * satifies a predicate. Otherwise, the corresponding position in the
644 * output sequence is not modified.
646 * Specifically, for each iterator <tt>i</tt> in the range <tt>[first1, last1)</tt> and
647 * <tt>j = first2 + (i - first1)</tt> in the range <tt>[first2, first2 + (last1 - first1) )</tt>,
648 * the predicate <tt>pred(*s)</tt> is evaluated, where <tt>s</tt> is the corresponding input
649 * iterator in the range <tt>[stencil, stencil + (last1 - first1) )</tt>. If this predicate
650 * evaluates to \c true, the result of <tt>binary_op(*i,*j)</tt> is assigned to <tt>*o</tt>,
651 * where <tt>o</tt> is the corresponding output iterator in the range
652 * <tt>[result, result + (last1 - first1) )</tt>. Otherwise, <tt>binary_op(*i,*j)</tt> is
653 * not evaluated and no assignment occurs. The input and output sequences may coincide,
654 * resulting in an in-place transformation.
656 * \param first1 The beginning of the first input sequence.
657 * \param last1 The end of the first input sequence.
658 * \param first2 The beginning of the second input sequence.
659 * \param stencil The beginning of the stencil sequence.
660 * \param result The beginning of the output sequence.
661 * \param binary_op The transformation operation.
662 * \param pred The predicate operation.
663 * \return The end of the output sequence.
665 * \tparam InputIterator1 is a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a>
666 * and \c InputIterator1's \c value_type is convertible to \c BinaryFunction's \c first_argument_type.
667 * \tparam InputIterator2 is a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a>
668 * and \c InputIterator2's \c value_type is convertible to \c BinaryFunction's \c second_argument_type.
669 * \tparam ForwardIterator is a model of <a href="http://www.sgi.com/tech/stl/ForwardIterator.html">Forward Iterator</a>.
670 * \tparam BinaryFunction is a model of <a href="http://www.sgi.com/tech/stl/BinaryFunction.html">Binary Function</a>
671 * and \c BinaryFunction's \c result_type is convertible to \c OutputIterator's \c value_type.
672 * \tparam Predicate is a model of <a href="http://www.sgi.com/tech/stl/Predicate.html">Predicate</a>.
674 * \pre \p first1 may equal \p result, but the range <tt>[first1, last1)</tt> shall not overlap the range <tt>[result, result + (last1 - first1))</tt> otherwise.
675 * \pre \p first2 may equal \p result, but the range <tt>[first2, first2 + (last1 - first1))</tt> shall not overlap the range <tt>[result, result + (last1 - first1))</tt> otherwise.
676 * \pre \p stencil may equal \p result, but the range <tt>[stencil, stencil + (last1 - first1))</tt> shall not overlap the range <tt>[result, result + (last1 - first1))</tt> otherwise.
678 * The following code snippet demonstrates how to use \p transform_if:
681 * #include <thrust/transform.h>
682 * #include <thrust/functional.h>
684 * int input1[6] = {-5, 0, 2, 3, 2, 4};
685 * int input2[6] = { 3, 6, -2, 1, 2, 3};
686 * int stencil[8] = { 1, 0, 1, 0, 1, 0};
689 * thrust::plus<int> op;
690 * thrust::identity<int> identity;
692 * thrust::transform_if(input1, input1 + 6, input2, stencil, output, op, identity);
694 * // output is now {-2, 0, 0, 3, 4, 4};
697 * \see thrust::transform
699 template<typename InputIterator1,
700 typename InputIterator2,
701 typename InputIterator3,
702 typename ForwardIterator,
703 typename BinaryFunction,
705 ForwardIterator transform_if(InputIterator1 first1, InputIterator1 last1,
706 InputIterator2 first2,
707 InputIterator3 stencil,
708 ForwardIterator result,
709 BinaryFunction binary_op,
713 /*! \} // end transformations
717 } // end namespace thrust
719 #include <thrust/detail/transform.inl>