Stream Compaction
template <typename DerivedPolicy, typename InputIterator, typename OutputIterator, typename Predicate> _CCCL_HOST_DEVICE OutputIterator copy_if(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, InputIterator first, InputIterator last, OutputIterator result, Predicate pred);
template <typename InputIterator, typename OutputIterator, typename Predicate> OutputIterator copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred);
template <typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator, typename Predicate> _CCCL_HOST_DEVICE OutputIterator copy_if(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, InputIterator1 first, InputIterator1 last, InputIterator2 stencil, OutputIterator result, Predicate pred);
template <typename InputIterator1, typename InputIterator2, typename OutputIterator, typename Predicate> OutputIterator copy_if(InputIterator1 first, InputIterator1 last, InputIterator2 stencil, OutputIterator result, Predicate pred);
template <typename DerivedPolicy, typename ForwardIterator, typename T> _CCCL_HOST_DEVICE ForwardIterator remove(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, ForwardIterator first, ForwardIterator last, const T & value);
template <typename ForwardIterator, typename T> ForwardIterator remove(ForwardIterator first, ForwardIterator last, const T & value);
template <typename DerivedPolicy, typename InputIterator, typename OutputIterator, typename T> _CCCL_HOST_DEVICE OutputIterator remove_copy(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, InputIterator first, InputIterator last, OutputIterator result, const T & value);
template <typename InputIterator, typename OutputIterator, typename T> OutputIterator remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T & value);
template <typename DerivedPolicy, typename ForwardIterator, typename Predicate> _CCCL_HOST_DEVICE ForwardIterator remove_if(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, ForwardIterator first, ForwardIterator last, Predicate pred);
template <typename ForwardIterator, typename Predicate> ForwardIterator remove_if(ForwardIterator first, ForwardIterator last, Predicate pred);
template <typename DerivedPolicy, typename InputIterator, typename OutputIterator, typename Predicate> _CCCL_HOST_DEVICE OutputIterator remove_copy_if(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, InputIterator first, InputIterator last, OutputIterator result, Predicate pred);
template <typename InputIterator, typename OutputIterator, typename Predicate> OutputIterator remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred);
template <typename DerivedPolicy, typename ForwardIterator, typename InputIterator, typename Predicate> _CCCL_HOST_DEVICE ForwardIterator remove_if(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, ForwardIterator first, ForwardIterator last, InputIterator stencil, Predicate pred);
template <typename ForwardIterator, typename InputIterator, typename Predicate> ForwardIterator remove_if(ForwardIterator first, ForwardIterator last, InputIterator stencil, Predicate pred);
template <typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator, typename Predicate> _CCCL_HOST_DEVICE OutputIterator remove_copy_if(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, InputIterator1 first, InputIterator1 last, InputIterator2 stencil, OutputIterator result, Predicate pred);
template <typename InputIterator1, typename InputIterator2, typename OutputIterator, typename Predicate> OutputIterator remove_copy_if(InputIterator1 first, InputIterator1 last, InputIterator2 stencil, OutputIterator result, Predicate pred);
template <typename DerivedPolicy, typename ForwardIterator> _CCCL_HOST_DEVICE ForwardIterator unique(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, ForwardIterator first, ForwardIterator last);
template <typename ForwardIterator> ForwardIterator unique(ForwardIterator first, ForwardIterator last);
template <typename DerivedPolicy, typename ForwardIterator, typename BinaryPredicate> _CCCL_HOST_DEVICE ForwardIterator unique(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, ForwardIterator first, ForwardIterator last, BinaryPredicate binary_pred);
template <typename ForwardIterator, typename BinaryPredicate> ForwardIterator unique(ForwardIterator first, ForwardIterator last, BinaryPredicate binary_pred);
template <typename DerivedPolicy, typename InputIterator, typename OutputIterator> _CCCL_HOST_DEVICE OutputIterator unique_copy(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, InputIterator first, InputIterator last, OutputIterator result);
template <typename InputIterator, typename OutputIterator> OutputIterator unique_copy(InputIterator first, InputIterator last, OutputIterator result);
template <typename DerivedPolicy, typename InputIterator, typename OutputIterator, typename BinaryPredicate> _CCCL_HOST_DEVICE OutputIterator unique_copy(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate binary_pred);
template <typename InputIterator, typename OutputIterator, typename BinaryPredicate> OutputIterator unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate binary_pred);
template <typename DerivedPolicy, typename ForwardIterator1, typename ForwardIterator2> _CCCL_HOST_DEVICE thrust::pair< ForwardIterator1, ForwardIterator2 > unique_by_key(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, ForwardIterator1 keys_first, ForwardIterator1 keys_last, ForwardIterator2 values_first);
template <typename ForwardIterator1, typename ForwardIterator2> thrust::pair< ForwardIterator1, ForwardIterator2 > unique_by_key(ForwardIterator1 keys_first, ForwardIterator1 keys_last, ForwardIterator2 values_first);
template <typename DerivedPolicy, typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate> _CCCL_HOST_DEVICE thrust::pair< ForwardIterator1, ForwardIterator2 > unique_by_key(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, ForwardIterator1 keys_first, ForwardIterator1 keys_last, ForwardIterator2 values_first, BinaryPredicate binary_pred);
template <typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate> thrust::pair< ForwardIterator1, ForwardIterator2 > unique_by_key(ForwardIterator1 keys_first, ForwardIterator1 keys_last, ForwardIterator2 values_first, BinaryPredicate binary_pred);
template <typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator1, typename OutputIterator2> _CCCL_HOST_DEVICE thrust::pair< OutputIterator1, OutputIterator2 > unique_by_key_copy(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, InputIterator1 keys_first, InputIterator1 keys_last, InputIterator2 values_first, OutputIterator1 keys_result, OutputIterator2 values_result);
template <typename InputIterator1, typename InputIterator2, typename OutputIterator1, typename OutputIterator2> thrust::pair< OutputIterator1, OutputIterator2 > unique_by_key_copy(InputIterator1 keys_first, InputIterator1 keys_last, InputIterator2 values_first, OutputIterator1 keys_result, OutputIterator2 values_result);
template <typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator1, typename OutputIterator2, typename BinaryPredicate> _CCCL_HOST_DEVICE thrust::pair< OutputIterator1, OutputIterator2 > unique_by_key_copy(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, InputIterator1 keys_first, InputIterator1 keys_last, InputIterator2 values_first, OutputIterator1 keys_result, OutputIterator2 values_result, BinaryPredicate binary_pred);
template <typename InputIterator1, typename InputIterator2, typename OutputIterator1, typename OutputIterator2, typename BinaryPredicate> thrust::pair< OutputIterator1, OutputIterator2 > unique_by_key_copy(InputIterator1 keys_first, InputIterator1 keys_last, InputIterator2 values_first, OutputIterator1 keys_result, OutputIterator2 values_result, BinaryPredicate binary_pred);
template <typename DerivedPolicy, typename ForwardIterator, typename BinaryPredicate> _CCCL_HOST_DEVICE thrust::iterator_traits< ForwardIterator >::difference_type unique_count(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, ForwardIterator first, ForwardIterator last, BinaryPredicate binary_pred);
template <typename DerivedPolicy, typename ForwardIterator> _CCCL_HOST_DEVICE thrust::iterator_traits< ForwardIterator >::difference_type unique_count(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, ForwardIterator first, ForwardIterator last);
template <typename ForwardIterator, typename BinaryPredicate> _CCCL_HOST_DEVICE thrust::iterator_traits< ForwardIterator >::difference_type unique_count(ForwardIterator first, ForwardIterator last, BinaryPredicate binary_pred);
template <typename ForwardIterator> _CCCL_HOST_DEVICE thrust::iterator_traits< ForwardIterator >::difference_type unique_count(ForwardIterator first, ForwardIterator last);
Functions
Function copy_if
template <typename DerivedPolicy, typename InputIterator, typename OutputIterator, typename Predicate> _CCCL_HOST_DEVICE OutputIterator copy_if(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, 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 result
, except that any element which causes pred
to be false
is not copied. copy_if
is stable, meaning that the relative order of elements that are copied is unchanged.
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.
The algorithm’s execution is parallelized as determined by system
.
The following code snippet demonstrates how to use copy_if
to perform stream compaction to copy even numbers to an output range using the thrust::host
parallelization policy:
#include <thrust/copy.h>
#include <thrust/execution_policy.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(thrust::host, V, V + N, result, is_even());
// V remains {-2, 0, -1, 0, 1, 2}
// result is now {-2, 0, 0, 2}
Template Parameters:
DerivedPolicy
The name of the derived execution policy.InputIterator
is a model of Input Iterator, andInputIterator's
value_type
is convertible toPredicate's
argument_type
.OutputIterator
is a model of Output Iterator.Predicate
is a model of Predicate.
Function Parameters:
exec
The execution policy to use for parallelization.first
The beginning of the sequence from which to copy.last
The end of the sequence from which to copy.result
The beginning of the sequence into which to copy.pred
The predicate to test on every value of the range[first, last)
.
Preconditions: The ranges [first, last)
and [result, result + (last - first))
shall not overlap.
Returns: result + n
, where n
is equal to the number of times pred
evaluated to true
in the range [first, last)
.
See: remove_copy_if
Function copy_if
template <typename InputIterator, typename OutputIterator, typename Predicate> OutputIterator 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 result
, except that any element which causes pred
to false
is not copied. copy_if
is stable, meaning that the relative order of elements that are copied is unchanged.
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.
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}
Template Parameters:
InputIterator
is a model of Input Iterator, andInputIterator's
value_type
is convertible toPredicate's
argument_type
.OutputIterator
is a model of Output Iterator.Predicate
is a model of Predicate.
Function Parameters:
first
The beginning of the sequence from which to copy.last
The end of the sequence from which to copy.result
The beginning of the sequence into which to copy.pred
The predicate to test on every value of the range[first, last)
.
Preconditions: The ranges [first, last)
and [result, result + (last - first))
shall not overlap.
Returns: result + n
, where n
is equal to the number of times pred
evaluated to true
in the range [first, last)
.
See: remove_copy_if
Function copy_if
template <typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator, typename Predicate> _CCCL_HOST_DEVICE OutputIterator copy_if(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, 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. copy_if
is stable, meaning that the relative order of elements that are copied is unchanged.
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.
The algorithm’s execution is parallelized as determined by exec
.
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 using the thrust::host
execution policy:
#include <thrust/copy.h>
#include <thrust/execution_policy.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(thrust::host, 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}
Template Parameters:
DerivedPolicy
The name of the derived execution policy.InputIterator1
is a model of Input Iterator.InputIterator2
is a model of Input Iterator, andInputIterator2's
value_type
is convertible toPredicate's
argument_type
.OutputIterator
is a model of Output Iterator.Predicate
is a model of Predicate.
Function Parameters:
exec
The execution policy to use for parallelization.first
The beginning of the sequence from which to copy.last
The end of the sequence from which to copy.stencil
The beginning of the stencil sequence.result
The beginning of the sequence into which to copy.pred
The predicate to test on every value of the range[stencil, stencil + (last-first))
.
Preconditions:
- The ranges
[first, last)
and[result, result + (last - first))
shall not overlap. - The ranges
[stencil, stencil + (last - first))
and[result, result + (last - first))
shall not overlap.
Returns: result + n
, where n
is equal to the number of times pred
evaluated to true
in the range [stencil, stencil + (last-first))
.
See: remove_copy_if
Function copy_if
template <typename InputIterator1, typename InputIterator2, typename OutputIterator, typename Predicate> OutputIterator 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. copy_if
is stable, meaning that the relative order of elements that are copied is unchanged.
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.
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}
Template Parameters:
InputIterator1
is a model of Input Iterator.InputIterator2
is a model of Input Iterator, andInputIterator2's
value_type
is convertible toPredicate's
argument_type
.OutputIterator
is a model of Output Iterator.Predicate
is a model of Predicate.
Function Parameters:
first
The beginning of the sequence from which to copy.last
The end of the sequence from which to copy.stencil
The beginning of the stencil sequence.result
The beginning of the sequence into which to copy.pred
The predicate to test on every value of the range[stencil, stencil + (last-first))
.
Preconditions:
- The ranges
[first, last)
and[result, result + (last - first))
shall not overlap. - The ranges
[stencil, stencil + (last - first))
and[result, result + (last - first))
shall not overlap.
Returns: result + n
, where n
is equal to the number of times pred
evaluated to true
in the range [stencil, stencil + (last-first))
.
See: remove_copy_if
Function remove
template <typename DerivedPolicy, typename ForwardIterator, typename T> _CCCL_HOST_DEVICE ForwardIterator remove(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, 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.
The algorithm’s execution is parallelized as determined by exec
.
The following code snippet demonstrates how to use remove
to remove a number of interest from a range using the thrust::host
execution policy for parallelization:
#include <thrust/remove.h>
#include <thrust/execution_policy.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())
.
Template Parameters:
DerivedPolicy
The name of the derived execution policy.ForwardIterator
is a model of Forward Iterator, andForwardIterator
is mutable.T
is a model of Equality Comparable, and objects of typeT
can be compared for equality with objects ofForwardIterator's
value_type
.
Function Parameters:
exec
The execution policy to use for parallelization.first
The beginning of the range of interest.last
The end of the range of interest.value
The 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
.
See:
- https://en.cppreference.com/w/cpp/algorithm/remove
- remove_if
- remove_copy
- remove_copy_if
Function remove
template <typename ForwardIterator, typename T> ForwardIterator 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.
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())
.
Template Parameters:
ForwardIterator
is a model of Forward Iterator, andForwardIterator
is mutable.T
is a model of Equality Comparable, and objects of typeT
can be compared for equality with objects ofForwardIterator's
value_type
.
Function Parameters:
first
The beginning of the range of interest.last
The end of the range of interest.value
The 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
.
See:
- https://en.cppreference.com/w/cpp/algorithm/remove
- remove_if
- remove_copy
- remove_copy_if
Function remove_copy
template <typename DerivedPolicy, typename InputIterator, typename OutputIterator, typename T> _CCCL_HOST_DEVICE OutputIterator remove_copy(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, 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)
.
The algorithm’s execution is parallelized as determined by exec
.
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 using the thrust::host
execution policy for parallelization:
#include <thrust/remove.h>
#include <thrust/execution_policy.h>
...
const int N = 6;
int V[N] = {-2, 0, -1, 0, 1, 2};
int result[N-2];
thrust::remove_copy(thrust::host, V, V + N, result, 0);
// V remains {-2, 0, -1, 0, 1, 2}
// result is now {-2, -1, 1, 2}
Template Parameters:
DerivedPolicy
The name of the derived execution policy.InputIterator
is a model of Input Iterator, andInputIterator's
value_type
is convertible to a type inOutputIterator's
set ofvalue_types
.OutputIterator
is a model of Output Iterator.T
is a model of Equality Comparable, and objects of typeT
can be compared for equality with objects ofInputIterator's
value_type
.
Function Parameters:
exec
The execution policy to use for parallelization.first
The beginning of the range of interest.last
The end of the range of interest.result
The resulting range is copied to the sequence beginning at this location.value
The value to omit from the copied range.
Preconditions: The range [first, last)
shall not overlap the range [result, result + (last - first))
.
Returns: An OutputIterator pointing to the end of the resulting range of elements which are not equal to value
.
See:
- https://en.cppreference.com/w/cpp/algorithm/remove_copy
- remove
- remove_if
- remove_copy_if
Function remove_copy
template <typename InputIterator, typename OutputIterator, typename T> OutputIterator 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)
.
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}
Template Parameters:
InputIterator
is a model of Input Iterator, andInputIterator's
value_type
is convertible to a type inOutputIterator's
set ofvalue_types
.OutputIterator
is a model of Output Iterator.T
is a model of Equality Comparable, and objects of typeT
can be compared for equality with objects ofInputIterator's
value_type
.
Function Parameters:
first
The beginning of the range of interest.last
The end of the range of interest.result
The resulting range is copied to the sequence beginning at this location.value
The value to omit from the copied range.
Preconditions: The range [first, last)
shall not overlap the range [result, result + (last - first))
.
Returns: An OutputIterator pointing to the end of the resulting range of elements which are not equal to value
.
See:
- https://en.cppreference.com/w/cpp/algorithm/remove_copy
- remove
- remove_if
- remove_copy_if
Function remove_if
template <typename DerivedPolicy, typename ForwardIterator, typename Predicate> _CCCL_HOST_DEVICE ForwardIterator remove_if(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, 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.
The algorithm’s execution is parallelized as determined by exec
.
The following code snippet demonstrates how to use remove_if
to remove all even numbers from an array of integers using the thrust::host
execution policy for parallelization:
#include <thrust/remove.h>
#include <thrust/execution_policy.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(thrust::host, 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())
.
Template Parameters:
DerivedPolicy
The name of the derived execution policy.ForwardIterator
is a model of Forward Iterator,ForwardIterator
is mutable, andForwardIterator's
value_type
is convertible toPredicate's
argument_type
.Predicate
is a model of Predicate.
Function Parameters:
exec
The execution policy to use for parallelization.first
The beginning of the range of interest.last
The end of the range of interest.pred
A predicate to evaluate for each element of the range[first,last)
. Elements for whichpred
evaluates totrue
are removed from the sequence.
Returns: A ForwardIterator pointing to the end of the resulting range of elements for which pred
evaluated to true
.
See:
- https://en.cppreference.com/w/cpp/algorithm/remove
- remove
- remove_copy
- remove_copy_if
Function remove_if
template <typename ForwardIterator, typename Predicate> ForwardIterator 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.
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())
.
Template Parameters:
ForwardIterator
is a model of Forward Iterator,ForwardIterator
is mutable, andForwardIterator's
value_type
is convertible toPredicate's
argument_type
.Predicate
is a model of Predicate.
Function Parameters:
first
The beginning of the range of interest.last
The end of the range of interest.pred
A predicate to evaluate for each element of the range[first,last)
. Elements for whichpred
evaluates totrue
are removed from the sequence.
Returns: A ForwardIterator pointing to the end of the resulting range of elements for which pred
evaluated to true
.
See:
- https://en.cppreference.com/w/cpp/algorithm/remove
- remove
- remove_copy
- remove_copy_if
Function remove_copy_if
template <typename DerivedPolicy, typename InputIterator, typename OutputIterator, typename Predicate> _CCCL_HOST_DEVICE OutputIterator remove_copy_if(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, 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)
.
The algorithm’s execution is parallelized as determined by exec
.
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 using the thrust::host
execution policy for parallelization:
#include <thrust/remove.h>
#include <thrust/execution_policy.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(thrust::host, V, V + N, result, is_even());
// V remains {-2, 0, -1, 0, 1, 2}
// result is now {-1, 1}
Template Parameters:
DerivedPolicy
The name of the derived execution policy.InputIterator
is a model of Input Iterator,InputIterator's
value_type
is convertible to a type inOutputIterator's
set ofvalue_types
, andInputIterator's
value_type
is convertible toPredicate's
argument_type
.OutputIterator
is a model of Output Iterator.Predicate
is a model of Predicate.
Function Parameters:
exec
The execution policy to use for parallelization.first
The beginning of the range of interest.last
The end of the range of interest.result
The resulting range is copied to the sequence beginning at this location.pred
A predicate to evaluate for each element of the range[first,last)
. Elements for whichpred
evaluates tofalse
are not copied to the resulting sequence.
Preconditions: The range [first, last)
shall not overlap the range [result, result + (last - first))
.
Returns: An OutputIterator pointing to the end of the resulting range.
See:
- https://en.cppreference.com/w/cpp/algorithm/remove_copy
- remove
- remove_copy
- remove_if
Function remove_copy_if
template <typename InputIterator, typename OutputIterator, typename Predicate> OutputIterator 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)
.
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}
Template Parameters:
InputIterator
is a model of Input Iterator,InputIterator's
value_type
is convertible to a type inOutputIterator's
set ofvalue_types
, andInputIterator's
value_type
is convertible toPredicate's
argument_type
.OutputIterator
is a model of Output Iterator.Predicate
is a model of Predicate.
Function Parameters:
first
The beginning of the range of interest.last
The end of the range of interest.result
The resulting range is copied to the sequence beginning at this location.pred
A predicate to evaluate for each element of the range[first,last)
. Elements for whichpred
evaluates tofalse
are not copied to the resulting sequence.
Preconditions: The range [first, last)
shall not overlap the range [result, result + (last - first))
.
Returns: An OutputIterator pointing to the end of the resulting range.
See:
- https://en.cppreference.com/w/cpp/algorithm/remove_copy
- remove
- remove_copy
- remove_if
Function remove_if
template <typename DerivedPolicy, typename ForwardIterator, typename InputIterator, typename Predicate> _CCCL_HOST_DEVICE ForwardIterator remove_if(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, 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.
The algorithm’s execution is parallelized as determined by exec
.
#include <thrust/remove.h>
#include <thrust/execution_policy.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_if(thrust::host, A, A + N, S, thrust::identity<int>());
// The first three values of A are now {1, 5, 7}
// Values beyond new_end are unspecified
The range
[first, last)
is not permitted to overlap with the range [stencil, stencil + (last - first))
.
https://en.cppreference.com/w/cpp/algorithm/remove
remove
remove_copy
remove_copy_if
</code>
Template Parameters:
DerivedPolicy
The name of the derived execution policy.ForwardIterator
is a model of Forward Iterator andForwardIterator
is mutable.InputIterator
is a model of Input Iterator, andInputIterator's
value_type
is convertible toPredicate's
argument_type
.Predicate
is a model of Predicate.
Function Parameters:
exec
The execution policy to use for parallelization.first
The beginning of the range of interest.last
The end of the range of interest.stencil
The beginning of the stencil sequence.pred
A predicate to evaluate for each element of the range[stencil, stencil + (last - first))
. Elements for whichpred
evaluates totrue
are removed from the sequence[first, last)
Preconditions:
- The range
[first, last)
shall not overlap the range[result, result + (last - first))
. -
The range
[stencil, stencil + (last - first))
shall not overlap the range[result, result + (last
- first)). The following code snippet demonstrates how to use
remove_if
to remove specific elements from an array of integers using thethrust::host
execution policy for parallelization:
</code>
Returns: A ForwardIterator pointing to the end of the resulting range of elements for which pred
evaluated to true
.
Function remove_if
template <typename ForwardIterator, typename InputIterator, typename Predicate> ForwardIterator 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.
#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_if(A, A + N, S, thrust::identity<int>());
// The first three values of A are now {1, 5, 7}
// Values beyond new_end are unspecified
The range
[first, last)
is not permitted to overlap with the range [stencil, stencil + (last - first))
.
https://en.cppreference.com/w/cpp/algorithm/remove
remove
remove_copy
remove_copy_if
</code>
Template Parameters:
ForwardIterator
is a model of Forward Iterator andForwardIterator
is mutable.InputIterator
is a model of Input Iterator, andInputIterator's
value_type
is convertible toPredicate's
argument_type
.Predicate
is a model of Predicate.
Function Parameters:
first
The beginning of the range of interest.last
The end of the range of interest.stencil
The beginning of the stencil sequence.pred
A predicate to evaluate for each element of the range[stencil, stencil + (last - first))
. Elements for whichpred
evaluates totrue
are removed from the sequence[first, last)
Preconditions:
- The range
[first, last)
shall not overlap the range[result, result + (last - first))
. -
The range
[stencil, stencil + (last - first))
shall not overlap the range[result, result + (last
- first)). The following code snippet demonstrates how to use
remove_if
to remove specific elements from an array of integers.
</code>
Returns: A ForwardIterator pointing to the end of the resulting range of elements for which pred
evaluated to true
.
Function remove_copy_if
template <typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator, typename Predicate> _CCCL_HOST_DEVICE OutputIterator remove_copy_if(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, 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)
.
The algorithm’s execution policy is parallelized as determined by exec
.
#include <thrust/remove.h>
#include <thrust/execution_policy.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(thrust::host, V, V + N, S, result, thrust::identity<int>());
// V remains {-2, 0, -1, 0, 1, 2}
// result is now {-1, 1}
https://en.cppreference.com/w/cpp/algorithm/remove_copy
remove
remove_copy
remove_if
copy_if
</code>
Template Parameters:
DerivedPolicy
The name of the derived execution policy.InputIterator1
is a model of Input Iterator,InputIterator1's
value_type
is convertible to a type inOutputIterator's
set ofvalue_types
.InputIterator2
is a model of Input Iterator, andInputIterator2's
value_type
is convertible toPredicate's
argument_type
.OutputIterator
is a model of Output Iterator.Predicate
is a model of Predicate.
Function Parameters:
exec
The execution policy to use for parallelization.first
The beginning of the range of interest.last
The end of the range of interest.stencil
The beginning of the stencil sequence.result
The resulting range is copied to the sequence beginning at this location.pred
A predicate to evaluate for each element of the range[first,last)
. Elements for whichpred
evaluates tofalse
are not copied to the resulting sequence.
Preconditions: The range [stencil, stencil + (last - first))
shall not overlap the range [result, result + (last
- first)). 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 using thethrust::host
execution policy for parallelization.
</code>
Returns: An OutputIterator pointing to the end of the resulting range.
Function remove_copy_if
template <typename InputIterator1, typename InputIterator2, typename OutputIterator, typename Predicate> OutputIterator 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)
.
#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}
https://en.cppreference.com/w/cpp/algorithm/remove_copy
remove
remove_copy
remove_if
copy_if
</code>
Template Parameters:
InputIterator1
is a model of Input Iterator,InputIterator1's
value_type
is convertible to a type inOutputIterator's
set ofvalue_types
.InputIterator2
is a model of Input Iterator, andInputIterator2's
value_type
is convertible toPredicate's
argument_type
.OutputIterator
is a model of Output Iterator.Predicate
is a model of Predicate.
Function Parameters:
first
The beginning of the range of interest.last
The end of the range of interest.stencil
The beginning of the stencil sequence.result
The resulting range is copied to the sequence beginning at this location.pred
A predicate to evaluate for each element of the range[first,last)
. Elements for whichpred
evaluates tofalse
are not copied to the resulting sequence.
Preconditions: The range [stencil, stencil + (last - first))
shall not overlap the range [result, result + (last
- first)). 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.
</code>
Returns: An OutputIterator pointing to the end of the resulting range.
Function unique
template <typename DerivedPolicy, typename ForwardIterator> _CCCL_HOST_DEVICE ForwardIterator unique(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, 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.
The algorithm’s execution is parallelized as determined by exec
.
The following code snippet demonstrates how to use unique
to compact a sequence of numbers 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};
int *new_end = thrust::unique(thrust::host, A, A + N);
// The first four values of A are now {1, 3, 2, 1}
// Values beyond new_end are unspecified.
Template Parameters:
DerivedPolicy
The name of the derived execution policy.ForwardIterator
is a model of Forward Iterator, andForwardIterator
is mutable, andForwardIterator's
value_type
is a model of Equality Comparable.
Function Parameters:
exec
The execution policy to use for parallelization.first
The beginning of the input range.last
The end of the input range.
Returns: The end of the unique range [first, new_last)
.
See:
Function unique
template <typename ForwardIterator> ForwardIterator 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.
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.
Template Parameters: ForwardIterator
: is a model of Forward Iterator, and ForwardIterator
is mutable, and ForwardIterator's
value_type
is a model of Equality Comparable.
Function Parameters:
first
The beginning of the input range.last
The end of the input range.
Returns: The end of the unique range [first, new_last)
.
See:
Function unique
template <typename DerivedPolicy, typename ForwardIterator, typename BinaryPredicate> _CCCL_HOST_DEVICE ForwardIterator unique(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, 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.
The algorithm’s execution is parallelized as determined by exec
.
The following code snippet demonstrates how to use unique
to compact a sequence of numbers 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};
int *new_end = thrust::unique(thrust::host, 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.
Template Parameters:
DerivedPolicy
The name of the derived execution policy.ForwardIterator
is a model of Forward Iterator, andForwardIterator
is mutable, andForwardIterator's
value_type
is convertible toBinaryPredicate's
first_argument_type
and toBinaryPredicate's
second_argument_type
.BinaryPredicate
is a model of Binary Predicate.
Function Parameters:
exec
The execution policy to use for parallelization.first
The beginning of the input range.last
The end of the input range.binary_pred
The binary predicate used to determine equality.
Returns: The end of the unique range [first, new_last)
See:
Function unique
template <typename ForwardIterator, typename BinaryPredicate> ForwardIterator 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.
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.
Template Parameters:
ForwardIterator
is a model of Forward Iterator, andForwardIterator
is mutable, andForwardIterator's
value_type
is convertible toBinaryPredicate's
first_argument_type
and toBinaryPredicate's
second_argument_type
.BinaryPredicate
is a model of Binary Predicate.
Function Parameters:
first
The beginning of the input range.last
The end of the input range.binary_pred
The binary predicate used to determine equality.
Returns: The end of the unique range [first, new_last)
See:
Function unique_copy
template <typename DerivedPolicy, typename InputIterator, typename OutputIterator> _CCCL_HOST_DEVICE OutputIterator unique_copy(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, InputIterator first, InputIterator last, OutputIterator result);
unique_copy
copies elements from the range [first, last)
to a range beginning with result
, 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.
The algorithm’s execution is parallelized as determined by exec
.
The following code snippet demonstrates how to use unique_copy
to compact a sequence of numbers 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};
int B[N];
int *result_end = thrust::unique_copy(thrust::host, A, A + N, B);
// The first four values of B are now {1, 3, 2, 1} and (result_end - B) is 4
// Values beyond result_end are unspecified
Template Parameters:
DerivedPolicy
The name of the derived execution policy.InputIterator
is a model of Input Iterator, andInputIterator's
value_type
is a model of Equality Comparable.OutputIterator
is a model of Output Iterator and andInputIterator's
value_type
is convertible toOutputIterator's
value_type
.
Function Parameters:
exec
The execution policy to use for parallelization.first
The beginning of the input range.last
The end of the input range.result
The beginning of the output range.
Preconditions: The range [first,last)
and the range [result, result + (last - first))
shall not overlap.
Returns: The end of the unique range [result, result_end)
.
See:
Function unique_copy
template <typename InputIterator, typename OutputIterator> OutputIterator unique_copy(InputIterator first, InputIterator last, OutputIterator result);
unique_copy
copies elements from the range [first, last)
to a range beginning with result
, 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.
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 *result_end = thrust::unique_copy(A, A + N, B);
// The first four values of B are now {1, 3, 2, 1} and (result_end - B) is 4
// Values beyond result_end are unspecified
Template Parameters:
InputIterator
is a model of Input Iterator, andInputIterator's
value_type
is a model of Equality Comparable.OutputIterator
is a model of Output Iterator and andInputIterator's
value_type
is convertible toOutputIterator's
value_type
.
Function Parameters:
first
The beginning of the input range.last
The end of the input range.result
The beginning of the output range.
Preconditions: The range [first,last)
and the range [result, result + (last - first))
shall not overlap.
Returns: The end of the unique range [result, result_end)
.
See:
Function unique_copy
template <typename DerivedPolicy, typename InputIterator, typename OutputIterator, typename BinaryPredicate> _CCCL_HOST_DEVICE OutputIterator unique_copy(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate binary_pred);
unique_copy
copies elements from the range [first, last)
to a range beginning with result
, 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.
The algorithm’s execution is parallelized as determined by exec
.
The following code snippet demonstrates how to use unique_copy
to compact a sequence of numbers 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};
int B[N];
int *result_end = thrust::unique_copy(thrust::host, A, A + N, B, thrust::equal_to<int>());
// The first four values of B are now {1, 3, 2, 1} and (result_end - B) is 4
// Values beyond result_end are unspecified.
Template Parameters:
DerivedPolicy
The name of the derived execution policy.InputIterator
is a model of Input Iterator, andInputIterator's
value_type
is a model of Equality Comparable.OutputIterator
is a model of Output Iterator and andInputIterator's
value_type
is convertible toOutputIterator's
value_type
.BinaryPredicate
is a model of Binary Predicate.
Function Parameters:
exec
The execution policy to use for parallelization.first
The beginning of the input range.last
The end of the input range.result
The beginning of the output range.binary_pred
The binary predicate used to determine equality.
Preconditions: The range [first,last)
and the range [result, result + (last - first))
shall not overlap.
Returns: The end of the unique range [result, result_end)
.
See:
Function unique_copy
template <typename InputIterator, typename OutputIterator, typename BinaryPredicate> OutputIterator unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate binary_pred);
unique_copy
copies elements from the range [first, last)
to a range beginning with result
, 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.
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 *result_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 (result_end - B) is 4
// Values beyond result_end are unspecified.
Template Parameters:
InputIterator
is a model of Input Iterator, andInputIterator's
value_type
is a model of Equality Comparable.OutputIterator
is a model of Output Iterator and andInputIterator's
value_type
is convertible toOutputIterator's
value_type
.BinaryPredicate
is a model of Binary Predicate.
Function Parameters:
first
The beginning of the input range.last
The end of the input range.result
The beginning of the output range.binary_pred
The binary predicate used to determine equality.
Preconditions: The range [first,last)
and the range [result, result + (last - first))
shall not overlap.
Returns: The end of the unique range [result, result_end)
.
See:
Function unique_by_key
template <typename DerivedPolicy, typename ForwardIterator1, typename ForwardIterator2> _CCCL_HOST_DEVICE thrust::pair< ForwardIterator1, ForwardIterator2 > unique_by_key(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, 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.
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;
new_end = thrust::unique_by_key(thrust::host, 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.
Template Parameters:
DerivedPolicy
The name of the derived execution policy.ForwardIterator1
is a model of Forward Iterator, andForwardIterator1
is mutable, andForwardIterator's
value_type
is a model of Equality Comparable.ForwardIterator2
is a model of Forward Iterator, andForwardIterator2
is mutable.
Function 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.
Preconditions: The range [keys_first, keys_last)
and the range [values_first, values_first + (keys_last - keys_first))
shall not overlap.
Returns: A pair of iterators at end of the ranges [key_first, keys_new_last)
and [values_first, values_new_last)
.
See:
- unique
- unique_by_key_copy
- reduce_by_key
Function unique_by_key
template <typename ForwardIterator1, typename ForwardIterator2> thrust::pair< ForwardIterator1, ForwardIterator2 > 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.
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.
Template Parameters:
ForwardIterator1
is a model of Forward Iterator, andForwardIterator1
is mutable, andForwardIterator's
value_type
is a model of Equality Comparable.ForwardIterator2
is a model of Forward Iterator, andForwardIterator2
is mutable.
Function Parameters:
keys_first
The beginning of the key range.keys_last
The end of the key range.values_first
The beginning of the value range.
Preconditions: The range [keys_first, keys_last)
and the range [values_first, values_first + (keys_last - keys_first))
shall not overlap.
Returns: A pair of iterators at end of the ranges [key_first, keys_new_last)
and [values_first, values_new_last)
.
See:
- unique
- unique_by_key_copy
- reduce_by_key
Function unique_by_key
template <typename DerivedPolicy, typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate> _CCCL_HOST_DEVICE thrust::pair< ForwardIterator1, ForwardIterator2 > 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, 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.
Template Parameters:
DerivedPolicy
The name of the derived execution policy.ForwardIterator1
is a model of Forward Iterator, andForwardIterator1
is mutable, andForwardIterator's
value_type
is a model of Equality Comparable.ForwardIterator2
is a model of Forward Iterator, andForwardIterator2
is mutable.BinaryPredicate
is a model of Binary Predicate.
Function 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.
Preconditions: The range [keys_first, keys_last)
and the range [values_first, values_first + (keys_last - keys_first))
shall not overlap.
Returns: The end of the unique range [first, new_last)
.
See:
- unique
- unique_by_key_copy
- reduce_by_key
Function unique_by_key
template <typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate> thrust::pair< ForwardIterator1, ForwardIterator2 > 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.
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.
Template Parameters:
ForwardIterator1
is a model of Forward Iterator, andForwardIterator1
is mutable, andForwardIterator's
value_type
is a model of Equality Comparable.ForwardIterator2
is a model of Forward Iterator, andForwardIterator2
is mutable.BinaryPredicate
is a model of Binary Predicate.
Function Parameters:
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.
Preconditions: The range [keys_first, keys_last)
and the range [values_first, values_first + (keys_last - keys_first))
shall not overlap.
Returns: The end of the unique range [first, new_last)
.
See:
- unique
- unique_by_key_copy
- reduce_by_key
Function unique_by_key_copy
template <typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator1, typename OutputIterator2> _CCCL_HOST_DEVICE thrust::pair< OutputIterator1, OutputIterator2 > unique_by_key_copy(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, InputIterator1 keys_first, InputIterator1 keys_last, InputIterator2 values_first, OutputIterator1 keys_result, OutputIterator2 values_result);
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_result
and the corresponding values from the range [values_first, values_first + (keys_last - keys_first))
are copied to a range beginning with values_result
.
This version of unique_by_key_copy
uses operator==
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_copy
to compact a sequence of key/value pairs and with equal keys 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}; // 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(thrust::host, 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.
Template Parameters:
DerivedPolicy
The name of the derived execution policy.InputIterator1
is a model of Input Iterator,InputIterator2
is a model of Input Iterator,OutputIterator1
is a model of Output Iterator and andInputIterator1's
value_type
is convertible toOutputIterator1's
value_type
.OutputIterator2
is a model of Output Iterator and andInputIterator2's
value_type
is convertible toOutputIterator2's
value_type
.
Function Parameters:
exec
The execution policy to use for parallelization.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_result
The beginning of the output key range.values_result
The beginning of the output value range.
Preconditions: The input ranges shall not overlap either output range.
Returns: A pair of iterators at end of the ranges [keys_result, keys_result_last)
and [values_result, values_result_last)
.
See:
- unique_copy
- unique_by_key
- reduce_by_key
Function unique_by_key_copy
template <typename InputIterator1, typename InputIterator2, typename OutputIterator1, typename OutputIterator2> thrust::pair< OutputIterator1, OutputIterator2 > unique_by_key_copy(InputIterator1 keys_first, InputIterator1 keys_last, InputIterator2 values_first, OutputIterator1 keys_result, OutputIterator2 values_result);
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_result
and the corresponding values from the range [values_first, values_first + (keys_last - keys_first))
are copied to a range beginning with values_result
.
This version of unique_by_key_copy
uses operator==
to test for equality and project1st
to reduce values with equal keys.
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.
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's
value_type
is convertible toOutputIterator1's
value_type
.OutputIterator2
is a model of Output Iterator and andInputIterator2's
value_type
is convertible toOutputIterator2's
value_type
.
Function 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_result
The beginning of the output key range.values_result
The beginning of the output value range.
Preconditions: The input ranges shall not overlap either output range.
Returns: A pair of iterators at end of the ranges [keys_result, keys_result_last)
and [values_result, values_result_last)
.
See:
- unique_copy
- unique_by_key
- reduce_by_key
Function unique_by_key_copy
template <typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename OutputIterator1, typename OutputIterator2, typename BinaryPredicate> _CCCL_HOST_DEVICE thrust::pair< OutputIterator1, OutputIterator2 > unique_by_key_copy(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, InputIterator1 keys_first, InputIterator1 keys_last, InputIterator2 values_first, OutputIterator1 keys_result, OutputIterator2 values_result, 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_result
and the corresponding values from the range [values_first, values_first + (keys_last - keys_first))
are copied to a range beginning with values_result
.
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.
The algorithm’s execution is parallelized as determined by exec
.
The following code snippet demonstrates how to use unique_by_key_copy
to compact a sequence of key/value pairs and with equal keys 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}; // 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(thrust::host, 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.
Template Parameters:
DerivedPolicy
The name of the derived execution policy.InputIterator1
is a model of Input Iterator,InputIterator2
is a model of Input Iterator,OutputIterator1
is a model of Output Iterator and andInputIterator1's
value_type
is convertible toOutputIterator1's
value_type
.OutputIterator2
is a model of Output Iterator and andInputIterator2's
value_type
is convertible toOutputIterator2's
value_type
.BinaryPredicate
is a model of Binary Predicate.
Function Parameters:
exec
The execution policy to use for parallelization.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_result
The beginning of the output key range.values_result
The beginning of the output value range.binary_pred
The binary predicate used to determine equality.
Preconditions: The input ranges shall not overlap either output range.
Returns: A pair of iterators at end of the ranges [keys_result, keys_result_last)
and [values_result, values_result_last)
.
See:
- unique_copy
- unique_by_key
- reduce_by_key
Function unique_by_key_copy
template <typename InputIterator1, typename InputIterator2, typename OutputIterator1, typename OutputIterator2, typename BinaryPredicate> thrust::pair< OutputIterator1, OutputIterator2 > unique_by_key_copy(InputIterator1 keys_first, InputIterator1 keys_last, InputIterator2 values_first, OutputIterator1 keys_result, OutputIterator2 values_result, 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_result
and the corresponding values from the range [values_first, values_first + (keys_last - keys_first))
are copied to a range beginning with values_result
.
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.
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.
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's
value_type
is convertible toOutputIterator1's
value_type
.OutputIterator2
is a model of Output Iterator and andInputIterator2's
value_type
is convertible toOutputIterator2's
value_type
.BinaryPredicate
is a model of Binary Predicate.
Function 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_result
The beginning of the output key range.values_result
The beginning of the output value range.binary_pred
The binary predicate used to determine equality.
Preconditions: The input ranges shall not overlap either output range.
Returns: A pair of iterators at end of the ranges [keys_result, keys_result_last)
and [values_result, values_result_last)
.
See:
- unique_copy
- unique_by_key
- reduce_by_key
Function unique_count
template <typename DerivedPolicy, typename ForwardIterator, typename BinaryPredicate> _CCCL_HOST_DEVICE thrust::iterator_traits< ForwardIterator >::difference_type unique_count(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, ForwardIterator first, ForwardIterator last, BinaryPredicate binary_pred);
unique_count
counts runs of equal elements in the range [first, last)
with the same value,
This version of unique_count
uses the function object binary_pred
to test for equality.
The algorithm’s execution is parallelized as determined by exec
.
The following code snippet demonstrates how to use unique_count
to determine a number of runs of equal elements 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};
int count = thrust::unique_count(thrust::host, A, A + N, thrust::equal_to<int>());
// count is now 4
Template Parameters:
DerivedPolicy
The name of the derived execution policy.ForwardIterator
is a model of Forward Iterator, andForwardIterator's
value_type
is convertible toBinaryPredicate's
first_argument_type
and toBinaryPredicate's
second_argument_type
.BinaryPredicate
is a model of Binary Predicate.
Function Parameters:
exec
The execution policy to use for parallelization.first
The beginning of the input range.last
The end of the input range.binary_pred
The binary predicate used to determine equality.
Returns: The number of runs of equal elements in [first, new_last)
See:
- unique_copy
- unique_by_key_copy
- reduce_by_key_copy
Function unique_count
template <typename DerivedPolicy, typename ForwardIterator> _CCCL_HOST_DEVICE thrust::iterator_traits< ForwardIterator >::difference_type unique_count(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, ForwardIterator first, ForwardIterator last);
unique_count
counts runs of equal elements in the range [first, last)
with the same value,
This version of unique_count
uses operator==
to test for equality.
The algorithm’s execution is parallelized as determined by exec
.
The following code snippet demonstrates how to use unique_count
to determine the number of runs of equal elements 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};
int count = thrust::unique_count(thrust::host, A, A + N);
// count is now 4
Template Parameters:
DerivedPolicy
The name of the derived execution policy.ForwardIterator
is a model of Forward Iterator, andForwardIterator's
value_type
is convertible toBinaryPredicate's
first_argument_type
and toBinaryPredicate's
second_argument_type
.BinaryPredicate
is a model of Binary Predicate.
Function Parameters:
exec
The execution policy to use for parallelization.first
The beginning of the input range.last
The end of the input range.binary_pred
The binary predicate used to determine equality.
Returns: The number of runs of equal elements in [first, new_last)
See:
- unique_copy
- unique_by_key_copy
- reduce_by_key_copy
Function unique_count
template <typename ForwardIterator, typename BinaryPredicate> _CCCL_HOST_DEVICE thrust::iterator_traits< ForwardIterator >::difference_type unique_count(ForwardIterator first, ForwardIterator last, BinaryPredicate binary_pred);
unique_count
counts runs of equal elements in the range [first, last)
with the same value,
This version of unique_count
uses the function object binary_pred
to test for equality.
The following code snippet demonstrates how to use unique_count
to determine the number of runs of equal elements:
#include <thrust/unique.h>
#include <thrust/execution_policy.h>
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1};
int count = thrust::unique_count(A, A + N, thrust::equal_to<int>());
// count is now 4
Template Parameters:
DerivedPolicy
The name of the derived execution policy.ForwardIterator
is a model of Forward Iterator, andForwardIterator's
value_type
is convertible toBinaryPredicate's
first_argument_type
and toBinaryPredicate's
second_argument_type
.BinaryPredicate
is a model of Binary Predicate.
Function Parameters:
exec
The execution policy to use for parallelization.first
The beginning of the input range.last
The end of the input range.binary_pred
The binary predicate used to determine equality.
Returns: The number of runs of equal elements in [first, new_last)
See:
- unique_copy
- unique_by_key_copy
- reduce_by_key_copy
Function unique_count
template <typename ForwardIterator> _CCCL_HOST_DEVICE thrust::iterator_traits< ForwardIterator >::difference_type unique_count(ForwardIterator first, ForwardIterator last);
unique_count
counts runs of equal elements in the range [first, last)
with the same value,
This version of unique_count
uses operator==
to test for equality.
The following code snippet demonstrates how to use unique_count
to determine the number of runs of equal elements:
#include <thrust/unique.h>
#include <thrust/execution_policy.h>
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1};
int count = thrust::unique_count(thrust::host, A, A + N);
// count is now 4
Template Parameters:
DerivedPolicy
The name of the derived execution policy.ForwardIterator
is a model of Forward Iterator, andForwardIterator's
value_type
is convertible toBinaryPredicate's
first_argument_type
and toBinaryPredicate's
second_argument_type
.BinaryPredicate
is a model of Binary Predicate.
Function Parameters:
exec
The execution policy to use for parallelization.first
The beginning of the input range.last
The end of the input range.binary_pred
The binary predicate used to determine equality.
Returns: The number of runs of equal elements in [first, new_last)
See:
- unique_copy
- unique_by_key_copy
- reduce_by_key_copy