2
* Copyright 2008-2011 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 C++ implementations of scan functions.
24
#include <thrust/iterator/iterator_traits.h>
26
#include <thrust/detail/type_traits.h>
27
#include <thrust/detail/type_traits/function_traits.h>
28
#include <thrust/detail/type_traits/iterator/is_output_iterator.h>
30
#include <thrust/detail/backend/dereference.h>
41
template<typename InputIterator,
42
typename OutputIterator,
43
typename BinaryFunction>
44
OutputIterator inclusive_scan(InputIterator first,
46
OutputIterator result,
47
BinaryFunction binary_op)
49
// the pseudocode for deducing the type of the temporary used below:
51
// if BinaryFunction is AdaptableBinaryFunction
52
// TemporaryType = AdaptableBinaryFunction::result_type
53
// else if OutputIterator is a "pure" output iterator
54
// TemporaryType = InputIterator::value_type
56
// TemporaryType = OutputIterator::value_type
58
// XXX upon c++0x, TemporaryType needs to be:
59
// result_of<BinaryFunction>::type
61
typedef typename eval_if<
62
has_result_type<BinaryFunction>::value,
63
result_type<BinaryFunction>,
65
is_output_iterator<OutputIterator>::value,
66
thrust::iterator_value<InputIterator>,
67
thrust::iterator_value<OutputIterator>
73
ValueType sum = dereference(first);
75
dereference(result) = sum;
77
for(++first, ++result; first != last; ++first, ++result)
78
dereference(result) = sum = binary_op(sum, dereference(first));
85
template<typename InputIterator,
86
typename OutputIterator,
88
typename BinaryFunction>
89
OutputIterator exclusive_scan(InputIterator first,
91
OutputIterator result,
93
BinaryFunction binary_op)
95
// the pseudocode for deducing the type of the temporary used below:
97
// if BinaryFunction is AdaptableBinaryFunction
98
// TemporaryType = AdaptableBinaryFunction::result_type
99
// else if OutputIterator is a "pure" output iterator
100
// TemporaryType = InputIterator::value_type
102
// TemporaryType = OutputIterator::value_type
104
// XXX upon c++0x, TemporaryType needs to be:
105
// result_of<BinaryFunction>::type
107
typedef typename eval_if<
108
has_result_type<BinaryFunction>::value,
109
result_type<BinaryFunction>,
111
is_output_iterator<OutputIterator>::value,
112
thrust::iterator_value<InputIterator>,
113
thrust::iterator_value<OutputIterator>
119
ValueType tmp = dereference(first); // temporary value allows in-situ scan
120
ValueType sum = init;
123
sum = binary_op(sum, tmp);
125
for(++first, ++result; first != last; ++first, ++result)
127
tmp = dereference(first);
128
dereference(result) = sum;
129
sum = binary_op(sum, tmp);
137
template<typename InputIterator1,
138
typename InputIterator2,
139
typename OutputIterator,
140
typename BinaryPredicate,
141
typename BinaryFunction>
142
OutputIterator inclusive_scan_by_key(InputIterator1 first1,
143
InputIterator1 last1,
144
InputIterator2 first2,
145
OutputIterator result,
146
BinaryPredicate binary_pred,
147
BinaryFunction binary_op)
149
typedef typename thrust::iterator_traits<InputIterator1>::value_type KeyType;
150
typedef typename thrust::iterator_traits<OutputIterator>::value_type ValueType;
154
KeyType prev_key = dereference(first1);
155
ValueType prev_value = dereference(first2);
157
dereference(result) = prev_value;
159
for(++first1, ++first2, ++result;
161
++first1, ++first2, ++result)
163
KeyType key = dereference(first1);
165
if (binary_pred(prev_key, key))
166
dereference(result) = prev_value = binary_op(prev_value, dereference(first2));
168
dereference(result) = prev_value = dereference(first2);
178
template<typename InputIterator1,
179
typename InputIterator2,
180
typename OutputIterator,
182
typename BinaryPredicate,
183
typename BinaryFunction>
184
OutputIterator exclusive_scan_by_key(InputIterator1 first1,
185
InputIterator1 last1,
186
InputIterator2 first2,
187
OutputIterator result,
189
BinaryPredicate binary_pred,
190
BinaryFunction binary_op)
192
typedef typename thrust::iterator_traits<InputIterator1>::value_type KeyType;
193
typedef typename thrust::iterator_traits<OutputIterator>::value_type ValueType;
197
KeyType temp_key = dereference(first1);
198
ValueType temp_value = dereference(first2);
200
ValueType next = init;
203
dereference(result) = next;
205
next = binary_op(next, temp_value);
207
for(++first1, ++first2, ++result;
209
++first1, ++first2, ++result)
211
KeyType key = dereference(first1);
213
// use temp to permit in-place scans
214
temp_value = dereference(first2);
216
if (!binary_pred(temp_key, key))
217
next = init; // reset sum
219
dereference(result) = next;
220
next = binary_op(next, temp_value);
229
} // end namespace cpp
230
} // end namespace backend
231
} // end namespace detail
232
} // end namespace thrust