thrust
Functions
Stream Compaction
Reordering

Functions

template<typename InputIterator , typename OutputIterator , typename Predicate >
OutputIterator thrust::copy_if (InputIterator first, InputIterator last, OutputIterator result, Predicate pred)
template<typename InputIterator1 , typename InputIterator2 , typename OutputIterator , typename Predicate >
OutputIterator thrust::copy_if (InputIterator1 first, InputIterator1 last, InputIterator2 stencil, OutputIterator result, Predicate pred)
template<typename ForwardIterator , typename T >
ForwardIterator thrust::remove (ForwardIterator first, ForwardIterator last, const T &value)
template<typename InputIterator , typename OutputIterator , typename T >
OutputIterator thrust::remove_copy (InputIterator first, InputIterator last, OutputIterator result, const T &value)
template<typename ForwardIterator , typename Predicate >
ForwardIterator thrust::remove_if (ForwardIterator first, ForwardIterator last, Predicate pred)
template<typename InputIterator , typename OutputIterator , typename Predicate >
OutputIterator thrust::remove_copy_if (InputIterator first, InputIterator last, OutputIterator result, Predicate pred)
template<typename ForwardIterator , typename InputIterator , typename Predicate >
ForwardIterator thrust::remove_if (ForwardIterator first, ForwardIterator last, InputIterator stencil, Predicate pred)
template<typename InputIterator1 , typename InputIterator2 , typename OutputIterator , typename Predicate >
OutputIterator thrust::remove_copy_if (InputIterator1 first, InputIterator1 last, InputIterator2 stencil, OutputIterator result, Predicate pred)
template<typename ForwardIterator >
ForwardIterator thrust::unique (ForwardIterator first, ForwardIterator last)
template<typename ForwardIterator , typename BinaryPredicate >
ForwardIterator thrust::unique (ForwardIterator first, ForwardIterator last, BinaryPredicate binary_pred)
template<typename InputIterator , typename OutputIterator >
OutputIterator thrust::unique_copy (InputIterator first, InputIterator last, OutputIterator output)
template<typename InputIterator , typename OutputIterator , typename BinaryPredicate >
OutputIterator thrust::unique_copy (InputIterator first, InputIterator last, OutputIterator output, BinaryPredicate binary_pred)
template<typename ForwardIterator1 , typename ForwardIterator2 >
thrust::pair< ForwardIterator1,
ForwardIterator2 > 
thrust::unique_by_key (ForwardIterator1 keys_first, ForwardIterator1 keys_last, ForwardIterator2 values_first)
template<typename ForwardIterator1 , typename ForwardIterator2 , typename BinaryPredicate >
thrust::pair< ForwardIterator1,
ForwardIterator2 > 
thrust::unique_by_key (ForwardIterator1 keys_first, ForwardIterator1 keys_last, ForwardIterator2 values_first, BinaryPredicate binary_pred)
template<typename InputIterator1 , typename InputIterator2 , typename OutputIterator1 , typename OutputIterator2 >
thrust::pair< OutputIterator1,
OutputIterator2 > 
thrust::unique_by_key_copy (InputIterator1 keys_first, InputIterator1 keys_last, InputIterator2 values_first, OutputIterator1 keys_output, OutputIterator2 values_output)
template<typename InputIterator1 , typename InputIterator2 , typename OutputIterator1 , typename OutputIterator2 , typename BinaryPredicate >
thrust::pair< OutputIterator1,
OutputIterator2 > 
thrust::unique_by_key_copy (InputIterator1 keys_first, InputIterator1 keys_last, InputIterator2 values_first, OutputIterator1 keys_output, OutputIterator2 values_output, BinaryPredicate binary_pred)

Function Documentation

template<typename InputIterator , typename OutputIterator , typename Predicate >
OutputIterator thrust::copy_if ( InputIterator  first,
InputIterator  last,
OutputIterator  result,
Predicate  pred 
)

This version of copy_if copies elements from the range [first,last) to a range beginning at \ presult, except that any element which causes pred to be pred to be false is not copied.

More precisely, for every integer n such that 0 <= n < last-first, copy_if performs the assignment *result = *(first+n) and result is advanced one position if pred(*(first+n)). Otherwise, no assignment occurs and result is not advanced.

Parameters:
firstThe beginning of the sequence from which to copy.
lastThe end of the sequence from which to copy.
resultThe beginning of the sequence into which to copy.
predThe predicate to test on every value of the range [first, last).
Returns:
result + n, where n is equal to the number of times pred evaluated to true in the range [first, last).
Template Parameters:
InputIteratoris a model of Input Iterator, and InputIterator's value_type is convertible to Predicate's argument_type.
OutputIteratoris a model of Output Iterator.
Predicateis a model of Predicate.

The following code snippet demonstrates how to use copy_if to perform stream compaction to copy even numbers to an output range.

  #include <thrust/copy.h>
  ...
  struct is_even
  {
    __host__ __device__
    bool operator()(const int x)
    {
      return (x % 2) == 0;
    }
  };
  ...
  const int N = 6;
  int V[N] = {-2, 0, -1, 0, 1, 2};
  int result[4];

  thrust::copy_if(V, V + N, result, is_even());

  // V remains {-2, 0, -1, 0, 1, 2}
  // result is now {-2, 0, 0, 2}
See also:
remove_copy_if
template<typename InputIterator1 , typename InputIterator2 , typename OutputIterator , typename Predicate >
OutputIterator thrust::copy_if ( InputIterator1  first,
InputIterator1  last,
InputIterator2  stencil,
OutputIterator  result,
Predicate  pred 
)

This version of copy_if copies elements from the range [first,last) to a range beginning at result, except that any element whose corresponding stencil element causes pred to be false is not copied.

More precisely, for every integer n such that 0 <= n < last-first, copy_if performs the assignment *result = *(first+n) and result is advanced one position if pred(*(stencil+n)). Otherwise, no assignment occurs and result is not advanced.

Parameters:
firstThe beginning of the sequence from which to copy.
lastThe end of the sequence from which to copy.
stencilThe beginning of the stencil sequence.
resultThe beginning of the sequence into which to copy.
predThe predicate to test on every value of the range [stencil, stencil + (last-first)).
Returns:
result + n, where n is equal to the number of times pred evaluated to true in the range [stencil, stencil + (last-first)).
Template Parameters:
InputIterator1is a model of Input Iterator.
InputIterator2is a model of Input Iterator, and InputIterator2's value_type is convertible to Predicate's argument_type.
OutputIteratoris a model of Output Iterator.
Predicateis a model of Predicate.

The following code snippet demonstrates how to use copy_if to perform stream compaction to copy numbers to an output range when corresponding stencil elements are even:

  #include <thrust/copy.h>
  ...
  struct is_even
  {
    __host__ __device__
    bool operator()(const int x)
    {
      return (x % 2) == 0;
    }
  };
  ...
  int N = 6;
  int data[N]    = { 0, 1,  2, 3, 4, 5};
  int stencil[N] = {-2, 0, -1, 0, 1, 2};
  int result[4];

  thrust::copy_if(data, data + N, stencil, result, is_even());

  // data remains    = { 0, 1,  2, 3, 4, 5};
  // stencil remains = {-2, 0, -1, 0, 1, 2};
  // result is now     { 0, 1,  3, 5}
See also:
remove_copy_if
template<typename ForwardIterator , typename T >
ForwardIterator thrust::remove ( ForwardIterator  first,
ForwardIterator  last,
const T &  value 
)

remove removes from the range [first, last) all elements that are equal to value. That is, remove returns an iterator new_last such that the range [first, new_last) contains no elements equal to value. The iterators in the range [new_first,last) are all still dereferenceable, but the elements that they point to are unspecified. remove is stable, meaning that the relative order of elements that are not equal to value is unchanged.

Parameters:
firstThe beginning of the range of interest.
lastThe end of the range of interest.
valueThe value to remove from the range [first, last). Elements which are equal to value are removed from the sequence.
Returns:
A ForwardIterator pointing to the end of the resulting range of elements which are not equal to value.
Template Parameters:
ForwardIteratoris a model of Forward Iterator, and ForwardIterator is mutable.
Tis a model of Equality Comparable, and objects of type T can be compared for equality with objects of ForwardIterator's value_type.

The following code snippet demonstrates how to use remove to remove a number of interest from a range.

  #include <thrust/remove.h>
  ...
  const int N = 6;
  int A[N] = {3, 1, 4, 1, 5, 9};
  int *new_end = thrust::remove(A, A + N, 1);
  // The first four values of A are now {3, 4, 5, 9}
  // Values beyond new_end are unspecified
Note:
The meaning of "removal" is somewhat subtle. remove does not destroy any iterators, and does not change the distance between first and last. (There's no way that it could do anything of the sort.) So, for example, if V is a device_vector, remove(V.begin(), V.end(), 0) does not change V.size(): V will contain just as many elements as it did before. remove returns an iterator that points to the end of the resulting range after elements have been removed from it; it follows that the elements after that iterator are of no interest, and may be discarded. If you are removing elements from a Sequence, you may simply erase them. That is, a reasonable way of removing elements from a Sequence is S.erase(remove(S.begin(), S.end(), x), S.end()).
See also:
http://www.sgi.com/tech/stl/remove.html
remove_if
remove_copy
remove_copy_if
template<typename InputIterator , typename OutputIterator , typename T >
OutputIterator thrust::remove_copy ( InputIterator  first,
InputIterator  last,
OutputIterator  result,
const T &  value 
)

remove_copy copies elements that are not equal to value from the range [first, last) to a range beginning at result. The return value is the end of the resulting range. This operation is stable, meaning that the relative order of the elements that are copied is the same as in the range [first, last).

Parameters:
firstThe beginning of the range of interest.
lastThe end of the range of interest.
resultThe resulting range is copied to the sequence beginning at this location.
valueThe value to omit from the copied range.
Returns:
An OutputIterator pointing to the end of the resulting range of elements which are not equal to value.
Template Parameters:
InputIteratoris a model of Input Iterator, and InputIterator's value_type is convertible to a type in OutputIterator's set of value_types.
OutputIteratoris a model of Output Iterator.
Tis a model of Equality Comparable, and objects of type T can be compared for equality with objects of InputIterator's value_type.

The following code snippet demonstrates how to use remove_copy to copy a sequence of numbers to an output range while omitting a value of interest.

  #include <thrust/remove.h>
  ...
  const int N = 6;
  int V[N] = {-2, 0, -1, 0, 1, 2};
  int result[N-2];
  thrust::remove_copy(V, V + N, result, 0);
  // V remains {-2, 0, -1, 0, 1, 2}
  // result is now {-2, -1, 1, 2}
See also:
http://www.sgi.com/tech/stl/remove_copy.html
remove
remove_if
remove_copy_if
template<typename InputIterator , typename OutputIterator , typename Predicate >
OutputIterator thrust::remove_copy_if ( InputIterator  first,
InputIterator  last,
OutputIterator  result,
Predicate  pred 
)

remove_copy_if copies elements from the range [first,last) to a range beginning at result, except that elements for which pred is true are not copied. The return value is the end of the resulting range. This operation is stable, meaning that the relative order of the elements that are copied is the same as the range [first,last).

Parameters:
firstThe beginning of the range of interest.
lastThe end of the range of interest.
resultThe resulting range is copied to the sequence beginning at this location.
predA predicate to evaluate for each element of the range [first,last). Elements for which pred evaluates to false are not copied to the resulting sequence.
Returns:
An OutputIterator pointing to the end of the resulting range.
Template Parameters:
InputIteratoris a model of Input Iterator, InputIterator's value_type is convertible to a type in OutputIterator's set of value_types, and InputIterator's value_type is convertible to Predicate's argument_type.
OutputIteratoris a model of Output Iterator.
Predicateis a model of Predicate.

The following code snippet demonstrates how to use remove_copy_if to copy a sequence of numbers to an output range while omitting even numbers.

  #include <thrust/remove.h>
  ...
  struct is_even
  {
    __host__ __device__
    bool operator()(const int x)
    {
      return (x % 2) == 0;
    }
  };
  ...
  const int N = 6;
  int V[N] = {-2, 0, -1, 0, 1, 2};
  int result[2];
  thrust::remove_copy_if(V, V + N, result, is_even());
  // V remains {-2, 0, -1, 0, 1, 2}
  // result is now {-1, 1}
See also:
http://www.sgi.com/tech/stl/remove_copy_if.html
remove
remove_copy
remove_if
template<typename InputIterator1 , typename InputIterator2 , typename OutputIterator , typename Predicate >
OutputIterator thrust::remove_copy_if ( InputIterator1  first,
InputIterator1  last,
InputIterator2  stencil,
OutputIterator  result,
Predicate  pred 
)

remove_copy_if copies elements from the range [first,last) to a range beginning at result, except that elements for which pred of the corresponding stencil value is true are not copied. The return value is the end of the resulting range. This operation is stable, meaning that the relative order of the elements that are copied is the same as the range [first,last).

Parameters:
firstThe beginning of the range of interest.
lastThe end of the range of interest.
stencilThe beginning of the stencil sequence.
resultThe resulting range is copied to the sequence beginning at this location.
predA predicate to evaluate for each element of the range [first,last). Elements for which pred evaluates to false are not copied to the resulting sequence.
Returns:
An OutputIterator pointing to the end of the resulting range.
Template Parameters:
InputIterator1is a model of Input Iterator, InputIterator1's value_type is convertible to a type in OutputIterator's set of value_types.
InputIterator2is a model of Input Iterator, and InputIterator2's value_type is convertible to Predicate's argument_type.
OutputIteratoris a model of Output Iterator.
Predicateis a model of Predicate.

The following code snippet demonstrates how to use remove_copy_if to copy a sequence of numbers to an output range while omitting specific elements.

  #include <thrust/remove.h>
  ...
  const int N = 6;
  int V[N] = {-2, 0, -1, 0, 1, 2};
  int S[N] = { 1, 1,  0, 1, 0, 1};
  int result[2];
  thrust::remove_copy_if(V, V + N, S, result, thrust::identity<int>());
  // V remains {-2, 0, -1, 0, 1, 2}
  // result is now {-1, 1}
See also:
http://www.sgi.com/tech/stl/remove_copy_if.html
remove
remove_copy
remove_if
copy_if
template<typename ForwardIterator , typename Predicate >
ForwardIterator thrust::remove_if ( ForwardIterator  first,
ForwardIterator  last,
Predicate  pred 
)

remove_if removes from the range [first, last) every element x such that pred(x) is true. That is, remove_if returns an iterator new_last such that the range [first,new_last) contains no elements for which pred is true. The iterators in the range [new_last,last) are all still dereferenceable, but the elements that they point to are unspecified. remove_if is stable, meaning that the relative order of elements that are not removed is unchanged.

Parameters:
firstThe beginning of the range of interest.
lastThe end of the range of interest.
predA predicate to evaluate for each element of the range [first,last). Elements for which pred evaluates to false are removed from the sequence.
Returns:
A ForwardIterator pointing to the end of the resulting range of elements for which pred evaluated to true.
Template Parameters:
ForwardIteratoris a model of Forward Iterator, ForwardIterator is mutable, and ForwardIterator's value_type is convertible to Predicate's argument_type.
Predicateis a model of Predicate.

The following code snippet demonstrates how to use remove_if to remove all even numbers from an array of integers.

  #include <thrust/remove.h>
  ...
  struct is_even
  {
    __host__ __device__
    bool operator()(const int x)
    {
      return (x % 2) == 0;
    }
  };
  ...
  const int N = 6;
  int A[N] = {1, 4, 2, 8, 5, 7};
  int *new_end = thrust::remove_if(A, A + N, is_even());
  // The first three values of A are now {1, 5, 7}
  // Values beyond new_end are unspecified
Note:
The meaning of "removal" is somewhat subtle. remove_if does not destroy any iterators, and does not change the distance between first and last. (There's no way that it could do anything of the sort.) So, for example, if V is a device_vector, remove_if(V.begin(), V.end(), pred) does not change V.size(): V will contain just as many elements as it did before. remove_if returns an iterator that points to the end of the resulting range after elements have been removed from it; it follows that the elements after that iterator are of no interest, and may be discarded. If you are removing elements from a Sequence, you may simply erase them. That is, a reasonable way of removing elements from a Sequence is S.erase(remove_if(S.begin(), S.end(), pred), S.end()).
See also:
http://www.sgi.com/tech/stl/remove_if.html
remove
remove_copy
remove_copy_if
template<typename ForwardIterator , typename InputIterator , typename Predicate >
ForwardIterator thrust::remove_if ( ForwardIterator  first,
ForwardIterator  last,
InputIterator  stencil,
Predicate  pred 
)

remove_if removes from the range [first, last) every element x such that pred(x) is true. That is, remove_if returns an iterator new_last such that the range [first, new_last) contains no elements for which pred of the corresponding stencil value is true. The iterators in the range [new_last,last) are all still dereferenceable, but the elements that they point to are unspecified. remove_if is stable, meaning that the relative order of elements that are not removed is unchanged.

Parameters:
firstThe beginning of the range of interest.
lastThe end of the range of interest.
stencilThe beginning of the stencil sequence.
predA predicate to evaluate for each element of the range [stencil, stencil + (last - first)). Elements for which pred evaluates to false are removed from the sequence [first, last)
Returns:
A ForwardIterator pointing to the end of the resulting range of elements for which pred evaluated to true.
Template Parameters:
ForwardIteratoris a model of Forward Iterator and ForwardIterator is mutable.
InputIteratoris a model of Input Iterator, and InputIterator's value_type is convertible to Predicate's argument_type.
Predicateis a model of Predicate.

The following code snippet demonstrates how to use remove_if to remove specific elements from an array of integers.

  #include <thrust/remove.h>
  ...
  const int N = 6;
  int A[N] = {1, 4, 2, 8, 5, 7};
  int S[N] = {0, 1, 1, 1, 0, 0};

  int *new_end = thrust::remove(A, A + N, S, thrust::identity<int>());
  // The first three values of A are now {1, 5, 7}
  // Values beyond new_end are unspecified
Note:
The range [first, last) is not permitted to overlap with the range [stencil, stencil + (last - first)).
See also:
http://www.sgi.com/tech/stl/remove_if.html
remove
remove_copy
remove_copy_if
template<typename ForwardIterator >
ForwardIterator thrust::unique ( ForwardIterator  first,
ForwardIterator  last 
)

For each group of consecutive elements in the range [first, last) with the same value, unique removes all but the first element of the group. The return value is an iterator new_last such that no two consecutive elements in the range [first, new_last) are equal. The iterators in the range [new_last, last) are all still dereferenceable, but the elements that they point to are unspecified. unique is stable, meaning that the relative order of elements that are not removed is unchanged.

This version of unique uses operator== to test for equality.

Parameters:
firstThe beginning of the input range.
lastThe end of the input range.
Returns:
The end of the unique range [first, new_last).
Template Parameters:
ForwardIteratoris a model of Forward Iterator, and ForwardIterator is mutable, and ForwardIterator's value_type is a model of Equality Comparable.

The following code snippet demonstrates how to use unique to compact a sequence of numbers to remove consecutive duplicates.

  #include <thrust/unique.h>
  ...
  const int N = 7;
  int A[N] = {1, 3, 3, 3, 2, 2, 1};
  int *new_end = thrust::unique(A, A + N);
  // The first four values of A are now {1, 3, 2, 1}
  // Values beyond new_end are unspecified.
See also:
http://www.sgi.com/tech/stl/unique.html
unique_copy
template<typename ForwardIterator , typename BinaryPredicate >
ForwardIterator thrust::unique ( ForwardIterator  first,
ForwardIterator  last,
BinaryPredicate  binary_pred 
)

For each group of consecutive elements in the range [first, last) with the same value, unique removes all but the first element of the group. The return value is an iterator new_last such that no two consecutive elements in the range [first, new_last) are equal. The iterators in the range [new_last, last) are all still dereferenceable, but the elements that they point to are unspecified. unique is stable, meaning that the relative order of elements that are not removed is unchanged.

This version of unique uses the function object binary_pred to test for equality.

Parameters:
firstThe beginning of the input range.
lastThe end of the input range.
binary_predThe binary predicate used to determine equality.
Returns:
The end of the unique range [first, new_last)
Template Parameters:
ForwardIteratoris a model of Forward Iterator, and ForwardIterator is mutable, and ForwardIterator's value_type is convertible to BinaryPredicate's first_argument_type and to BinaryPredicate's second_argument_type.
BinaryPredicateis a model of Binary Predicate.

The following code snippet demonstrates how to use unique to compact a sequence of numbers to remove consecutive duplicates.

  #include <thrust/unique.h>
  ...
  const int N = 7;
  int A[N] = {1, 3, 3, 3, 2, 2, 1};
  int *new_end = thrust::unique(A, A + N, thrust::equal_to<int>());
  // The first four values of A are now {1, 3, 2, 1}
  // Values beyond new_end are unspecified.
See also:
http://www.sgi.com/tech/stl/unique.html
unique_copy
template<typename ForwardIterator1 , typename ForwardIterator2 >
thrust::pair<ForwardIterator1,ForwardIterator2> thrust::unique_by_key ( ForwardIterator1  keys_first,
ForwardIterator1  keys_last,
ForwardIterator2  values_first 
)

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.

The return value is a pair of iterators (new_keys_last,new_values_last) such that no two consecutive elements in the range [keys_first, new_keys_last) are equal.

This version of unique_by_key uses operator== to test for equality and project1st to reduce values with equal keys.

Parameters:
keys_firstThe beginning of the key range.
keys_lastThe end of the key range.
values_firstThe beginning of the value range.
Returns:
A pair of iterators at end of the ranges [key_first, keys_new_last) and [values_first, values_new_last).
Template Parameters:
ForwardIterator1is a model of Forward Iterator, and ForwardIterator1 is mutable, and ForwardIterator's value_type is a model of Equality Comparable.
ForwardIterator2is a model of Forward Iterator, and ForwardIterator2 is mutable.

The following code snippet demonstrates how to use unique_by_key to compact a sequence of key/value pairs to remove consecutive duplicates.

  #include <thrust/unique.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;
  new_end = thrust::unique_by_key(A, A + N, B);

  // 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
unique_by_key_copy
reduce_by_key
template<typename ForwardIterator1 , typename ForwardIterator2 , typename BinaryPredicate >
thrust::pair<ForwardIterator1,ForwardIterator2> thrust::unique_by_key ( 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.

Parameters:
keys_firstThe beginning of the key range.
keys_lastThe end of the key range.
values_firstThe beginning of the value range.
binary_predThe binary predicate used to determine equality.
Returns:
The end of the unique range [first, new_last).
Template Parameters:
ForwardIterator1is a model of Forward Iterator, and ForwardIterator1 is mutable, and ForwardIterator's value_type is a model of Equality Comparable.
ForwardIterator2is a model of Forward Iterator, and ForwardIterator2 is mutable.
BinaryPredicateis a model of Binary Predicate.

The following code snippet demonstrates how to use unique_by_key to compact a sequence of key/value pairs to remove consecutive duplicates.

  #include <thrust/unique.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(keys, keys + N, values, 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
unique_by_key_copy
reduce_by_key
template<typename InputIterator1 , typename InputIterator2 , typename OutputIterator1 , typename OutputIterator2 >
thrust::pair<OutputIterator1,OutputIterator2> thrust::unique_by_key_copy ( InputIterator1  keys_first,
InputIterator1  keys_last,
InputIterator2  values_first,
OutputIterator1  keys_output,
OutputIterator2  values_output 
)

unique_by_key_copy is a generalization of unique_copy to key-value pairs. For each group of consecutive keys in the range [keys_first, keys_last) that are equal, unique_by_key_copy copies the first element of the group to a range beginning with keys_output and the corresponding values from the range [values_first, values_first + (keys_last - keys_first)) are copied to a range beginning with values_output.

This version of unique_by_key_copy uses operator== to test for equality and project1st to reduce values with equal keys.

Parameters:
keys_firstThe beginning of the input key range.
keys_lastThe end of the input key range.
values_firstThe beginning of the input value range.
keys_outputThe beginning of the output key range.
values_outputThe beginning of the output value range.
Returns:
A pair of iterators at end of the ranges [keys_output, keys_output_last) and [values_output, values_output_last).
Template Parameters:
InputIterator1is a model of Input Iterator,
InputIterator2is a model of Input Iterator,
OutputIterator1is a model of Output Iterator and and InputIterator1's value_type is convertible to OutputIterator1's value_type.
OutputIterator2is a model of Output Iterator and and InputIterator2's value_type is convertible to OutputIterator2's value_type.

The following code snippet demonstrates how to use unique_by_key_copy to compact a sequence of key/value pairs and with equal keys.

  #include <thrust/unique.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;
  new_end = thrust::unique_by_key_copy(A, A + N, B, C, D);

  // 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, 8, 5, 3} and new_end.second - D is 4.
See also:
unique_copy
unique_by_key
reduce_by_key
template<typename InputIterator1 , typename InputIterator2 , typename OutputIterator1 , typename OutputIterator2 , typename BinaryPredicate >
thrust::pair<OutputIterator1,OutputIterator2> thrust::unique_by_key_copy ( InputIterator1  keys_first,
InputIterator1  keys_last,
InputIterator2  values_first,
OutputIterator1  keys_output,
OutputIterator2  values_output,
BinaryPredicate  binary_pred 
)

unique_by_key_copy is a generalization of unique_copy to key-value pairs. For each group of consecutive keys in the range [keys_first, keys_last) that are equal, unique_by_key_copy copies the first element of the group to a range beginning with keys_output and the corresponding values from the range [values_first, values_first + (keys_last - keys_first)) are copied to a range beginning with values_output.

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

Parameters:
keys_firstThe beginning of the input key range.
keys_lastThe end of the input key range.
values_firstThe beginning of the input value range.
keys_outputThe beginning of the output key range.
values_outputThe beginning of the output value range.
binary_predThe binary predicate used to determine equality.
Returns:
A pair of iterators at end of the ranges [keys_output, keys_output_last) and [values_output, values_output_last).
Template Parameters:
InputIterator1is a model of Input Iterator,
InputIterator2is a model of Input Iterator,
OutputIterator1is a model of Output Iterator and and InputIterator1's value_type is convertible to OutputIterator1's value_type.
OutputIterator2is a model of Output Iterator and and InputIterator2's value_type is convertible to OutputIterator2's value_type.
BinaryPredicateis a model of Binary Predicate.

The following code snippet demonstrates how to use unique_by_key_copy to compact a sequence of key/value pairs and with equal keys.

  #include <thrust/unique.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;
  new_end = thrust::unique_by_key_copy(A, A + N, B, C, D, binary_pred);

  // 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, 8, 5, 3} and new_end.second - D is 4.
See also:
unique_copy
unique_by_key
reduce_by_key
template<typename InputIterator , typename OutputIterator >
OutputIterator thrust::unique_copy ( InputIterator  first,
InputIterator  last,
OutputIterator  output 
)

unique_copy copies elements from the range [first, last) to a range beginning with output, except that in a consecutive group of duplicate elements only the first one is copied. The return value is the end of the range to which the elements are copied.

The reason there are two different versions of unique_copy is that there are two different definitions of what it means for a consecutive group of elements to be duplicates. In the first version, the test is simple equality: the elements in a range [f, l) are duplicates if, for every iterator i in the range, either i == f or else *i == *(i-1). In the second, the test is an arbitrary BinaryPredicate binary_pred: the elements in [f, l) are duplicates if, for every iterator i in the range, either i == f or else binary_pred(*i, *(i-1)) is true.

This version of unique_copy uses operator== to test for equality.

Parameters:
firstThe beginning of the input range.
lastThe end of the input range.
outputThe beginning of the output range.
Returns:
The end of the unique range [output, output_end).
Template Parameters:
InputIteratoris a model of Input Iterator, and InputIterator's value_type is a model of Equality Comparable.
OutputIteratoris a model of Output Iterator and and InputIterator's value_type is convertible to OutputIterator's value_type.

The following code snippet demonstrates how to use unique_copy to compact a sequence of numbers to remove consecutive duplicates.

  #include <thrust/unique.h>
  ...
  const int N = 7;
  int A[N] = {1, 3, 3, 3, 2, 2, 1};
  int B[N];
  int * output_end = thrust::unique_copy(A, A + N, B);
  // The first four values of B are now {1, 3, 2, 1} and (output_end - B) is 4
  // Values beyond output_end are unspecified
See also:
unique
http://www.sgi.com/tech/stl/unique_copy.html
template<typename InputIterator , typename OutputIterator , typename BinaryPredicate >
OutputIterator thrust::unique_copy ( InputIterator  first,
InputIterator  last,
OutputIterator  output,
BinaryPredicate  binary_pred 
)

unique_copy copies elements from the range [first, last) to a range beginning with output, except that in a consecutive group of duplicate elements only the first one is copied. The return value is the end of the range to which the elements are copied.

This version of unique_copy uses the function object binary_pred to test for equality.

Parameters:
firstThe beginning of the input range.
lastThe end of the input range.
outputThe beginning of the output range.
binary_predThe binary predicate used to determine equality.
Returns:
The end of the unique range [output, output_end).
Template Parameters:
InputIteratoris a model of Input Iterator, and InputIterator's value_type is a model of Equality Comparable.
OutputIteratoris a model of Output Iterator and and InputIterator's value_type is convertible to OutputIterator's value_type.
BinaryPredicateis a model of Binary Predicate.

The following code snippet demonstrates how to use unique_copy to compact a sequence of numbers to remove consecutive duplicates.

  #include <thrust/unique.h>
  ...
  const int N = 7;
  int A[N] = {1, 3, 3, 3, 2, 2, 1};
  int B[N];
  int *output_end = thrust::unique_copy(A, A + N, B, thrust::equal_to<int>());
  // The first four values of B are now {1, 3, 2, 1} and (output_end - B) is 4
  // Values beyond output_end are unspecified.
See also:
unique
http://www.sgi.com/tech/stl/unique_copy.html