thrust::unique_by_key

Defined in thrust/unique.h

template<typename DerivedPolicy, typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
thrust::pair<ForwardIterator1, ForwardIterator2> thrust::unique_by_key(const thrust::detail::execution_policy_base<DerivedPolicy> &exec, ForwardIterator1 keys_first, ForwardIterator1 keys_last, ForwardIterator2 values_first, BinaryPredicate binary_pred)

unique_by_key is a generalization of unique to key-value pairs. For each group of consecutive keys in the range [keys_first, keys_last) that are equal, unique_by_key removes all but the first element of the group. Similarly, the corresponding values in the range [values_first, values_first + (keys_last - keys_first)) are also removed.

This version of unique_by_key uses the function object binary_pred to test for equality and project1st to reduce values with equal keys.

The algorithm’s execution is parallelized as determined by exec.

The following code snippet demonstrates how to use unique_by_key to compact a sequence of key/value pairs to remove consecutive duplicates using the thrust::host execution policy for parallelization:

#include <thrust/unique.h>
#include <thrust/execution_policy.h>
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1}; // keys
int B[N] = {9, 8, 7, 6, 5, 4, 3}; // values

thrust::pair<int*,int*> new_end;
thrust::equal_to<int> binary_pred;
new_end = thrust::unique_by_key(thrust::host, A, A + N, B, binary_pred);

// The first four keys in A are now {1, 3, 2, 1} and new_end.first - A is 4.
// The first four values in B are now {9, 8, 5, 3} and new_end.second - B is 4.

See also

unique

See also

unique_by_key_copy

See also

reduce_by_key

Parameters
  • exec – The execution policy to use for parallelization.

  • keys_first – The beginning of the key range.

  • keys_last – The end of the key range.

  • values_first – The beginning of the value range.

  • binary_pred – The binary predicate used to determine equality.

Template Parameters
  • DerivedPolicy – The name of the derived execution policy.

  • ForwardIterator1 – is a model of Forward Iterator, and ForwardIterator1 is mutable, and ForwardIterator's value_type is a model of Equality Comparable.

  • ForwardIterator2 – is a model of Forward Iterator, and ForwardIterator2 is mutable.

  • BinaryPredicate – is a model of Binary Predicate.

Returns

The end of the unique range [first, new_last).

Pre

The range [keys_first, keys_last) and the range [values_first, values_first + (keys_last - keys_first)) shall not overlap.