thrust

◆ inclusive_scan_by_key() [6/6]

template<typename InputIterator1 , typename InputIterator2 , typename OutputIterator , typename BinaryPredicate , typename AssociativeOperator >
OutputIterator thrust::inclusive_scan_by_key ( InputIterator1  first1,
InputIterator1  last1,
InputIterator2  first2,
OutputIterator  result,
BinaryPredicate  binary_pred,
AssociativeOperator  binary_op 
)

inclusive_scan_by_key computes an inclusive key-value or 'segmented' prefix sum operation. The term 'inclusive' means that each result includes the corresponding input operand in the partial sum. The term 'segmented' means that the partial sums are broken into distinct segments. In other words, within each segment a separate inclusive scan operation is computed. Refer to the code sample below for example usage.

This version of inclusive_scan_by_key uses the binary predicate pred to compare adjacent keys. Specifically, consecutive iterators i and i+1 in the range [first1, last1) belong to the same segment if binary_pred(*i, *(i+1)) is true, and belong to different segments otherwise.

This version of inclusive_scan_by_key uses the associative operator binary_op to perform the prefix sum. When the input and output sequences are the same, the scan is performed in-place.

Parameters
first1The beginning of the key sequence.
last1The end of the key sequence.
first2The beginning of the input value sequence.
resultThe beginning of the output value sequence.
binary_predThe binary predicate used to determine equality of keys.
binary_opThe associatve operator used to 'sum' values.
Returns
The end of the output sequence.
Template Parameters
InputIterator1is a model of Input Iterator
InputIterator2is a model of Input Iterator and InputIterator2's value_type is convertible to OutputIterator's value_type.
OutputIteratoris a model of Output Iterator, and if x and y are objects of OutputIterator's value_type, then binary_op(x,y) is defined.
BinaryPredicateis a model of Binary Predicate.
AssociativeOperatoris a model of Binary Function and AssociativeOperator's result_type is convertible to OutputIterator's value_type.
Precondition
first1 may equal result but the range [first1, last1) and the range [result, result + (last1 - first1)) shall not overlap otherwise.
first2 may equal result but the range [first2, first2 + (last1 - first1) and range [result, result + (last1 - first1)) shall not overlap otherwise.

The following code snippet demonstrates how to use inclusive_scan_by_key

#include <thrust/scan.h>
int data[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int keys[10] = {0, 0, 0, 1, 1, 2, 3, 3, 3, 3};
thrust::inclusive_scan_by_key(keys, keys + 10, data, data, binary_pred, binary_op); // in-place scan
// data is now {1, 2, 3, 1, 2, 1, 1, 2, 3, 4};
See also
inclusive_scan
exclusive_scan_by_key