24
24
#include <thrust/detail/config.h>
25
#include <thrust/detail/execution_policy.h>
29
31
/*! \addtogroup scattering
34
/*! \p scatter copies elements from a source range into an output array
35
* according to a map. For each iterator \c i in the range [\p first, \p last),
36
* the value \c *i is assigned to <tt>output[*(map + (i - first))]</tt>. The
37
* output iterator must permit random access. If the same index
38
* appears more than once in the range <tt>[map, map + (last - first))</tt>,
39
* the result is undefined.
41
* \param first Beginning of the sequence of values to scatter.
42
* \param last End of the sequence of values to scatter.
43
* \param map Beginning of the sequence of output indices.
44
* \param output Destination of the source elements.
46
* \tparam InputIterator1 must be a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a> and \c InputIterator1's \c value_type must be convertible to \c RandomAccessIterator's \c value_type.
47
* \tparam InputIterator2 must be a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a> and \c InputIterator2's \c value_type must be convertible to \c RandomAccessIterator's \c difference_type.
48
* \tparam RandomAccessIterator must be a model of <a href="http://www.sgi.com/tech/stl/RandomAccessIterator.html">Random Access iterator</a>.
37
/*! \p scatter copies elements from a source range into an output array
38
* according to a map. For each iterator \c i in the range [\p first, \p last),
39
* the value \c *i is assigned to <tt>output[*(map + (i - first))]</tt>. The
40
* output iterator must permit random access. If the same index
41
* appears more than once in the range <tt>[map, map + (last - first))</tt>,
42
* the result is undefined.
44
* The algorithm's execution is parallelized as determined by \p exec.
46
* \param exec The execution policy to use for parallelization.
47
* \param first Beginning of the sequence of values to scatter.
48
* \param last End of the sequence of values to scatter.
49
* \param map Beginning of the sequence of output indices.
50
* \param result Destination of the source elements.
52
* \tparam DerivedPolicy The name of the derived execution policy.
53
* \tparam InputIterator1 must be a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a> and \c InputIterator1's \c value_type must be convertible to \c RandomAccessIterator's \c value_type.
54
* \tparam InputIterator2 must be a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a> and \c InputIterator2's \c value_type must be convertible to \c RandomAccessIterator's \c difference_type.
55
* \tparam RandomAccessIterator must be a model of <a href="http://www.sgi.com/tech/stl/RandomAccessIterator.html">Random Access iterator</a>.
57
* \pre The iterator `result + i` shall not refer to any element referenced by any iterator `j` in the range `[first,last)` for all iterators `i` in the range `[map,map + (last - first))`.
59
* \pre The iterator `result + i` shall not refer to any element referenced by any iterator `j` in the range `[map,map + (last - first))` for all iterators `i` in the range `[map,map + (last - first))`.
61
* \pre The expression `result[*i]` shall be valid for all iterators in the range `[map,map + (last - first))`.
63
* The following code snippet demonstrates how to use \p scatter to
64
* reorder a range using the \p thrust::device execution policy for parallelization:
67
* #include <thrust/scatter.h>
68
* #include <thrust/device_vector.h>
69
* #include <thrust/execution_policy.h>
71
* // mark even indices with a 1; odd indices with a 0
72
* int values[10] = {1, 0, 1, 0, 1, 0, 1, 0, 1, 0};
73
* thrust::device_vector<int> d_values(values, values + 10);
75
* // scatter all even indices into the first half of the
76
* // range, and odd indices vice versa
77
* int map[10] = {0, 5, 1, 6, 2, 7, 3, 8, 4, 9};
78
* thrust::device_vector<int> d_map(map, map + 10);
80
* thrust::device_vector<int> d_output(10);
81
* thrust::scatter(thrust::device,
82
* d_values.begin(), d_values.end(),
83
* d_map.begin(), d_output.begin());
84
* // d_output is now {1, 1, 1, 1, 1, 0, 0, 0, 0, 0}
87
* \note \p scatter is the inverse of thrust::gather.
89
template<typename DerivedPolicy,
90
typename InputIterator1,
91
typename InputIterator2,
92
typename RandomAccessIterator>
93
void scatter(const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
97
RandomAccessIterator result);
100
/*! \p scatter copies elements from a source range into an output array
101
* according to a map. For each iterator \c i in the range [\p first, \p last),
102
* the value \c *i is assigned to <tt>output[*(map + (i - first))]</tt>. The
103
* output iterator must permit random access. If the same index
104
* appears more than once in the range <tt>[map, map + (last - first))</tt>,
105
* the result is undefined.
107
* \param first Beginning of the sequence of values to scatter.
108
* \param last End of the sequence of values to scatter.
109
* \param map Beginning of the sequence of output indices.
110
* \param result Destination of the source elements.
112
* \tparam InputIterator1 must be a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a> and \c InputIterator1's \c value_type must be convertible to \c RandomAccessIterator's \c value_type.
113
* \tparam InputIterator2 must be a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a> and \c InputIterator2's \c value_type must be convertible to \c RandomAccessIterator's \c difference_type.
114
* \tparam RandomAccessIterator must be a model of <a href="http://www.sgi.com/tech/stl/RandomAccessIterator.html">Random Access iterator</a>.
116
* \pre The iterator `result + i` shall not refer to any element referenced by any iterator `j` in the range `[first,last)` for all iterators `i` in the range `[map,map + (last - first))`.
118
* \pre The iterator `result + i` shall not refer to any element referenced by any iterator `j` in the range `[map,map + (last - first))` for all iterators `i` in the range `[map,map + (last - first))`.
120
* \pre The expression `result[*i]` shall be valid for all iterators in the range `[map,map + (last - first))`.
50
122
* The following code snippet demonstrates how to use \p scatter to
51
123
* reorder a range.
77
149
void scatter(InputIterator1 first,
78
150
InputIterator1 last,
79
151
InputIterator2 map,
80
RandomAccessIterator output);
83
/*! \p scatter_if conditionally copies elements from a source range into an
84
* output array according to a map. For each iterator \c i in the
85
* range <tt>[first, last)</tt> such that <tt>*(stencil + (i - first))</tt> is
86
* true, the value \c *i is assigned to <tt>output[*(map + (i - first))]</tt>.
87
* The output iterator must permit random access. If the same index
88
* appears more than once in the range <tt>[map, map + (last - first))</tt>
89
* the result is undefined.
91
* \param first Beginning of the sequence of values to scatter.
92
* \param last End of the sequence of values to scatter.
93
* \param map Beginning of the sequence of output indices.
94
* \param stencil Beginning of the sequence of predicate values.
95
* \param output Beginning of the destination range.
97
* \tparam InputIterator1 must be a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a> and \c InputIterator1's \c value_type must be convertible to \c RandomAccessIterator's \c value_type.
98
* \tparam InputIterator2 must be a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a> and \c InputIterator2's \c value_type must be convertible to \c RandomAccessIterator's \c difference_type.
99
* \tparam InputIterator3 must be a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a> and \c InputIterator3's \c value_type must be convertible to \c bool.
100
* \tparam RandomAccessIterator must be a model of <a href="http://www.sgi.com/tech/stl/RandomAccessIterator.html">Random Access iterator</a>.
152
RandomAccessIterator result);
155
/*! \p scatter_if conditionally copies elements from a source range into an
156
* output array according to a map. For each iterator \c i in the
157
* range <tt>[first, last)</tt> such that <tt>*(stencil + (i - first))</tt> is
158
* true, the value \c *i is assigned to <tt>output[*(map + (i - first))]</tt>.
159
* The output iterator must permit random access. If the same index
160
* appears more than once in the range <tt>[map, map + (last - first))</tt>
161
* the result is undefined.
163
* The algorithm's execution is parallelized as determined by \p exec.
165
* \param exec The execution policy to use for parallelization.
166
* \param first Beginning of the sequence of values to scatter.
167
* \param last End of the sequence of values to scatter.
168
* \param map Beginning of the sequence of output indices.
169
* \param stencil Beginning of the sequence of predicate values.
170
* \param output Beginning of the destination range.
172
* \tparam DerivedPolicy The name of the derived execution policy.
173
* \tparam InputIterator1 must be a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a> and \c InputIterator1's \c value_type must be convertible to \c RandomAccessIterator's \c value_type.
174
* \tparam InputIterator2 must be a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a> and \c InputIterator2's \c value_type must be convertible to \c RandomAccessIterator's \c difference_type.
175
* \tparam InputIterator3 must be a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a> and \c InputIterator3's \c value_type must be convertible to \c bool.
176
* \tparam RandomAccessIterator must be a model of <a href="http://www.sgi.com/tech/stl/RandomAccessIterator.html">Random Access iterator</a>.
178
* \pre The iterator `result + i` shall not refer to any element referenced by any iterator `j` in the range `[first,last)` for all iterators `i` in the range `[map,map + (last - first))`.
180
* \pre The iterator `result + i` shall not refer to any element referenced by any iterator `j` in the range `[map,map + (last - first))` for all iterators `i` in the range `[map,map + (last - first))`.
182
* \pre The iterator `result + i` shall not refer to any element referenced by any iterator `j` in the range `[stencil,stencil + (last - first))` for all iterators `i` in the range `[map,map + (last - first))`.
184
* \pre The expression `result[*i]` shall be valid for all iterators `i` in the range `[map,map + (last - first))` for which the following condition holds: `*(stencil + i) != false`.
187
* #include <thrust/scatter.h>
188
* #include <thrust/execution_policy.h>
190
* int V[8] = {10, 20, 30, 40, 50, 60, 70, 80};
191
* int M[8] = {0, 5, 1, 6, 2, 7, 3, 4};
192
* int S[8] = {1, 0, 1, 0, 1, 0, 1, 0};
193
* int D[8] = {0, 0, 0, 0, 0, 0, 0, 0};
195
* thrust::scatter_if(thrust::host, V, V + 8, M, S, D);
197
* // D contains [10, 30, 50, 70, 0, 0, 0, 0];
200
* \note \p scatter_if is the inverse of thrust::gather_if.
202
template<typename DerivedPolicy,
203
typename InputIterator1,
204
typename InputIterator2,
205
typename InputIterator3,
206
typename RandomAccessIterator>
207
void scatter_if(const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
208
InputIterator1 first,
211
InputIterator3 stencil,
212
RandomAccessIterator output);
215
/*! \p scatter_if conditionally copies elements from a source range into an
216
* output array according to a map. For each iterator \c i in the
217
* range <tt>[first, last)</tt> such that <tt>*(stencil + (i - first))</tt> is
218
* true, the value \c *i is assigned to <tt>output[*(map + (i - first))]</tt>.
219
* The output iterator must permit random access. If the same index
220
* appears more than once in the range <tt>[map, map + (last - first))</tt>
221
* the result is undefined.
223
* \param first Beginning of the sequence of values to scatter.
224
* \param last End of the sequence of values to scatter.
225
* \param map Beginning of the sequence of output indices.
226
* \param stencil Beginning of the sequence of predicate values.
227
* \param output Beginning of the destination range.
229
* \tparam InputIterator1 must be a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a> and \c InputIterator1's \c value_type must be convertible to \c RandomAccessIterator's \c value_type.
230
* \tparam InputIterator2 must be a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a> and \c InputIterator2's \c value_type must be convertible to \c RandomAccessIterator's \c difference_type.
231
* \tparam InputIterator3 must be a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a> and \c InputIterator3's \c value_type must be convertible to \c bool.
232
* \tparam RandomAccessIterator must be a model of <a href="http://www.sgi.com/tech/stl/RandomAccessIterator.html">Random Access iterator</a>.
234
* \pre The iterator `result + i` shall not refer to any element referenced by any iterator `j` in the range `[first,last)` for all iterators `i` in the range `[map,map + (last - first))`.
236
* \pre The iterator `result + i` shall not refer to any element referenced by any iterator `j` in the range `[map,map + (last - first))` for all iterators `i` in the range `[map,map + (last - first))`.
238
* \pre The iterator `result + i` shall not refer to any element referenced by any iterator `j` in the range `[stencil,stencil + (last - first))` for all iterators `i` in the range `[map,map + (last - first))`.
240
* \pre The expression `result[*i]` shall be valid for all iterators `i` in the range `[map,map + (last - first))` for which the following condition holds: `*(stencil + i) != false`.
103
243
* #include <thrust/scatter.h>
123
263
InputIterator2 map,
124
264
InputIterator3 stencil,
125
265
RandomAccessIterator output);
268
/*! \p scatter_if conditionally copies elements from a source range into an
269
* output array according to a map. For each iterator \c i in the
270
* range <tt>[first, last)</tt> such that <tt>pred(*(stencil + (i - first)))</tt> is
271
* \c true, the value \c *i is assigned to <tt>output[*(map + (i - first))]</tt>.
272
* The output iterator must permit random access. If the same index
273
* appears more than once in the range <tt>[map, map + (last - first))</tt>
274
* the result is undefined.
276
* The algorithm's execution is parallelized as determined by \p exec.
278
* \param exec The execution policy to use for parallelization.
279
* \param first Beginning of the sequence of values to scatter.
280
* \param last End of the sequence of values to scatter.
281
* \param map Beginning of the sequence of output indices.
282
* \param stencil Beginning of the sequence of predicate values.
283
* \param output Beginning of the destination range.
284
* \param pred Predicate to apply to the stencil values.
286
* \tparam DerivedPolicy The name of the derived execution policy.
287
* \tparam InputIterator1 must be a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a> and \c InputIterator1's \c value_type must be convertible to \c RandomAccessIterator's \c value_type.
288
* \tparam InputIterator2 must be a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a> and \c InputIterator2's \c value_type must be convertible to \c RandomAccessIterator's \c difference_type.
289
* \tparam InputIterator3 must be a model of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a> and \c InputIterator3's \c value_type must be convertible to \c Predicate's \c argument_type.
290
* \tparam RandomAccessIterator must be a model of <a href="http://www.sgi.com/tech/stl/RandomAccessIterator.html">Random Access iterator</a>.
291
* \tparam Predicate must be a model of <a href="http://www.sgi.com/tech/stl/Predicate.html">Predicate</a>.
293
* \pre The iterator `result + i` shall not refer to any element referenced by any iterator `j` in the range `[first,last)` for all iterators `i` in the range `[map,map + (last - first))`.
295
* \pre The iterator `result + i` shall not refer to any element referenced by any iterator `j` in the range `[map,map + (last - first))` for all iterators `i` in the range `[map,map + (last - first))`.
297
* \pre The iterator `result + i` shall not refer to any element referenced by any iterator `j` in the range `[stencil,stencil + (last - first))` for all iterators `i` in the range `[map,map + (last - first))`.
299
* \pre The expression `result[*i]` shall be valid for all iterators `i` in the range `[map,map + (last - first))` for which the following condition holds: `pred(*(stencil + i)) != false`.
302
* #include <thrust/scatter.h>
303
* #include <thrust/execution_policy.h>
307
* __host__ __device__
308
* bool operator()(int x)
310
* return (x % 2) == 0;
316
* int V[8] = {10, 20, 30, 40, 50, 60, 70, 80};
317
* int M[8] = {0, 5, 1, 6, 2, 7, 3, 4};
318
* int S[8] = {2, 1, 2, 1, 2, 1, 2, 1};
319
* int D[8] = {0, 0, 0, 0, 0, 0, 0, 0};
322
* thrust::scatter_if(thrust::host, V, V + 8, M, S, D, pred);
324
* // D contains [10, 30, 50, 70, 0, 0, 0, 0];
327
* \note \p scatter_if is the inverse of thrust::gather_if.
329
template<typename DerivedPolicy,
330
typename InputIterator1,
331
typename InputIterator2,
332
typename InputIterator3,
333
typename RandomAccessIterator,
335
void scatter_if(const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
336
InputIterator1 first,
339
InputIterator3 stencil,
340
RandomAccessIterator output,
128
344
/*! \p scatter_if conditionally copies elements from a source range into an