Template Function thrust::reduce_by_key(InputIterator1, InputIterator1, InputIterator2, OutputIterator1, OutputIterator2, BinaryPredicate, BinaryFunction)¶
Function Documentation¶
-
template<typename
InputIterator1, typenameInputIterator2, typenameOutputIterator1, typenameOutputIterator2, typenameBinaryPredicate, typenameBinaryFunction>
thrust::pair<OutputIterator1, OutputIterator2>thrust::reduce_by_key(InputIterator1 keys_first, InputIterator1 keys_last, InputIterator2 values_first, OutputIterator1 keys_output, OutputIterator2 values_output, BinaryPredicate binary_pred, BinaryFunction binary_op) reduce_by_keyis a generalization ofreduceto key-value pairs. For each group of consecutive keys in the range[keys_first, keys_last)that are equal,reduce_by_keycopies the first element of the group to thekeys_output. The corresponding values in the range are reduced using theBinaryFunctionbinary_opand the result copied tovalues_output. Specifically, if consecutive key iteratorsiand(i + 1) are such thatbinary_pred(*i, *(i+1))istrue, then the corresponding values are reduced to a single value withbinary_op.This version of
reduce_by_keyuses the function objectbinary_predto test for equality andbinary_opto reduce values with equal keys.The following code snippet demonstrates how to use
reduce_by_keyto compact a sequence of key/value pairs and sum values with equal keys.- Return
A pair of iterators at end of the ranges
[keys_output, keys_output_last)and[values_output, values_output_last).- Pre
The input ranges shall not overlap either output range.
- Parameters
keys_first: The beginning of the input key range.keys_last: The end of the input key range.values_first: The beginning of the input value range.keys_output: The beginning of the output key range.values_output: The beginning of the output value range.binary_pred: The binary predicate used to determine equality.binary_op: The binary function used to accumulate values.
- Template Parameters
InputIterator1: is a model of Input Iterator,InputIterator2: is a model of Input Iterator,OutputIterator1: is a model of Output Iterator and andInputIterator1'svalue_typeis convertible toOutputIterator1'svalue_type.OutputIterator2: is a model of Output Iterator and andInputIterator2'svalue_typeis convertible toOutputIterator2'svalue_type.BinaryPredicate: is a model of Binary Predicate.BinaryFunction: is a model of Binary Function andBinaryFunction'sresult_typeis convertible toOutputIterator2'svalue_type.
#include <thrust/reduce.h> ... const int N = 7; int A[N] = {1, 3, 3, 3, 2, 2, 1}; // input keys int B[N] = {9, 8, 7, 6, 5, 4, 3}; // input values int C[N]; // output keys int D[N]; // output values thrust::pair<int*,int*> new_end; thrust::equal_to<int> binary_pred; thrust::plus<int> binary_op; new_end = thrust::reduce_by_key(A, A + N, B, C, D, binary_pred, binary_op); // The first four keys in C are now {1, 3, 2, 1} and new_end.first - C is 4. // The first four values in D are now {9, 21, 9, 3} and new_end.second - D is 4.
- See
reduce
- See
unique_copy
- See
unique_by_key
- See
unique_by_key_copy