thrust
Functions
Replacing
Transformations

Functions

template<typename ForwardIterator , typename T >
void thrust::replace (ForwardIterator first, ForwardIterator last, const T &old_value, const T &new_value)
template<typename ForwardIterator , typename Predicate , typename T >
void thrust::replace_if (ForwardIterator first, ForwardIterator last, Predicate pred, const T &new_value)
template<typename ForwardIterator , typename InputIterator , typename Predicate , typename T >
void thrust::replace_if (ForwardIterator first, ForwardIterator last, InputIterator stencil, Predicate pred, const T &new_value)
template<typename InputIterator , typename OutputIterator , typename T >
OutputIterator thrust::replace_copy (InputIterator first, InputIterator last, OutputIterator result, const T &old_value, const T &new_value)
template<typename InputIterator , typename OutputIterator , typename Predicate , typename T >
OutputIterator thrust::replace_copy_if (InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T &new_value)
template<typename InputIterator1 , typename InputIterator2 , typename OutputIterator , typename Predicate , typename T >
OutputIterator thrust::replace_copy_if (InputIterator1 first, InputIterator1 last, InputIterator2 stencil, OutputIterator result, Predicate pred, const T &new_value)

Function Documentation

template<typename ForwardIterator , typename T >
void thrust::replace ( ForwardIterator  first,
ForwardIterator  last,
const T &  old_value,
const T &  new_value 
)

replace replaces every element in the range [first, last) equal to old_value with new_value. That is: for every iterator i, if *i == old_value then it performs the assignment *i = new_value.

Parameters:
firstThe beginning of the sequence of interest.
lastThe end of the sequence of interest.
old_valueThe value to replace.
new_valueThe new value to replace old_value.
Template Parameters:
ForwardIteratoris a model of Forward Iterator, and ForwardIterator is mutable.
Tis a model of Assignable, T is a model of EqualityComparable, objects of T may be compared for equality with objects of ForwardIterator's value_type, and T is convertible to ForwardIterator's value_type.

The following code snippet demonstrates how to use replace to replace a value of interest in a device_vector with another.

  #include <thrust/replace.h>
  #include <thrust/device_vector.h>

  ...
  
  thrust::device_vector<int> A(4);
  A[0] = 1;
  A[1] = 2;
  A[2] = 3;
  A[3] = 1;

  thrust::replace(A.begin(), A.end(), 1, 99);

  // A contains [99, 2, 3, 99]
See also:
http://www.sgi.com/tech/stl/replace.html
replace_if
replace_copy
replace_copy_if
template<typename InputIterator , typename OutputIterator , typename T >
OutputIterator thrust::replace_copy ( InputIterator  first,
InputIterator  last,
OutputIterator  result,
const T &  old_value,
const T &  new_value 
)

replace_copy copies elements from the range [first, last) to the range [result, result + (last-first)), except that any element equal to old_value is not copied; new_value is copied instead.

More precisely, for every integer n such that 0 <= n < last-first, replace_copy performs the assignment *(result+n) = new_value if *(first+n) == old_value, and *(result+n) = *(first+n) otherwise.

Parameters:
firstThe beginning of the sequence to copy from.
lastThe end of the sequence to copy from.
resultThe beginning of the sequence to copy to.
old_valueThe value to replace.
new_valueThe replacement value for which *i == old_value evaluates to true.
Returns:
result + (last-first)
Template Parameters:
InputIteratoris a model of Input Iterator.
OutputIteratoris a model of Output Iterator.
Tis a model of Assignable, T is a model of Equality Comparable, T may be compared for equality with InputIterator's value_type, and T is convertible to OutputIterator's value_type.
  #include <thrust/replace.h>
  #include <thrust/device_vector.h>
  ...
  thrust::device_vector<int> A(4);
  A[0] = 1;
  A[1] = 2;
  A[2] = 3;
  A[3] = 1;

  thrust::device_vector<int> B(4);

  thrust::replace_copy(A.begin(), A.end(), B.begin(), 1, 99);

  // B contains [99, 2, 3, 99]
See also:
http://www.sgi.com/tech/stl/replace_copy.html
copy
replace
replace_if
replace_copy_if
template<typename InputIterator , typename OutputIterator , typename Predicate , typename T >
OutputIterator thrust::replace_copy_if ( InputIterator  first,
InputIterator  last,
OutputIterator  result,
Predicate  pred,
const T &  new_value 
)

replace_copy_if copies elements from the range [first, last) to the range [result, result + (last-first)), except that any element for which pred is true is not copied; new_value is copied instead.

More precisely, for every integer n such that 0 <= n < last-first, replace_copy_if performs the assignment *(result+n) = new_value if pred(*(first+n)), and *(result+n) = *(first+n) otherwise.

Parameters:
firstThe beginning of the sequence to copy from.
lastThe end of the sequence to copy from.
resultThe beginning of the sequence to copy to.
predThe predicate to test on every value of the range [first,last).
new_valueThe replacement value to assign pred(*i) evaluates to true.
Returns:
result + (last-first)
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.
Tis a model of Assignable, and T is convertible to OutputIterator's value_type.
  #include <thrust/replace.h>
  #include <thrust/device_vector.h>

  struct is_less_than_zero
  {
    __host__ __device__
    bool operator()(int x)
    {
      return x < 0;
    }
  };

  ...
  
  thrust::device_vector<int> A(4);
  A[0] =  1;
  A[1] = -3;
  A[2] =  2;
  A[3] = -1;
 
  thrust::device_vector<int> B(4);
  is_less_than_zero pred;

  thrust::replace_copy_if(A.begin(), A.end(), B.begin(), pred, 0);

  // B contains [1, 0, 2, 0]
See also:
http://www.sgi.com/tech/stl/replace_copy_if.html
replace
replace_if
replace_copy
template<typename InputIterator1 , typename InputIterator2 , typename OutputIterator , typename Predicate , typename T >
OutputIterator thrust::replace_copy_if ( InputIterator1  first,
InputIterator1  last,
InputIterator2  stencil,
OutputIterator  result,
Predicate  pred,
const T &  new_value 
)

This version of replace_copy_if copies elements from the range [first, last) to the range [result, result + (last-first)), except that any element whose corresponding stencil element causes pred to be true is not copied; new_value is copied instead.

More precisely, for every integer n such that 0 <= n < last-first, replace_copy_if performs the assignment *(result+n) = new_value if pred(*(stencil+n)), and *(result+n) = *(first+n) otherwise.

Parameters:
firstThe beginning of the sequence to copy from.
lastThe end of the sequence to copy from.
stencilThe beginning of the stencil sequence.
resultThe beginning of the sequence to copy to.
predThe predicate to test on every value of the range [stencil, stencil + (last - first)).
new_valueThe replacement value to assign when pred(*s) evaluates to true.
Returns:
result + (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.
Tis a model of Assignable, and T is convertible to OutputIterator's value_type.
  #include <thrust/replace.h>
  #include <thrust/device_vector.h>

  struct is_less_than_zero
  {
    __host__ __device__
    bool operator()(int x)
    {
      return x < 0;
    }
  };
  
  ...
  
  thrust::device_vector<int> A(4);
  A[0] =  10;
  A[1] =  20;
  A[2] =  30;
  A[3] =  40;

  thrust::device_vector<int> S(4);
  S[0] = -1;
  S[1] =  0;
  S[2] = -1;
  S[3] =  0;

  thrust::device_vector<int> B(4);
  is_less_than_zero pred;

  thrust::replace_if(A.begin(), A.end(), S.begin(), B.begin(), pred, 0);

  // B contains [0, 20, 0, 40]
See also:
replace_copy
replace_if
template<typename ForwardIterator , typename Predicate , typename T >
void thrust::replace_if ( ForwardIterator  first,
ForwardIterator  last,
Predicate  pred,
const T &  new_value 
)

replace_if replaces every element in the range [first, last) for which pred returns true with new_value. That is: for every iterator i, if pred(*i) is true then it performs the assignment *i = new_value.

Parameters:
firstThe beginning of the sequence of interest.
lastThe end of the sequence of interest.
predThe predicate to test on every value of the range [first,last).
new_valueThe new value to replace elements which pred(*i) evaluates 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.
Tis a model of Assignable, and T is convertible to ForwardIterator's value_type.

The following code snippet demonstrates how to use replace_if to replace a device_vector's negative elements with 0.

  #include <thrust/replace.h>
  #include <thrust/device_vector.h>
  ...
  struct is_less_than_zero
  {
    __host__ __device__
    bool operator()(int x)
    {
      return x < 0;
    }
  };

  ...
  
  thrust::device_vector<int> A(4);
  A[0] =  1;
  A[1] = -3;
  A[2] =  2;
  A[3] = -1;

  is_less_than_zero pred;

  thrust::replace_if(A.begin(), A.end(), pred, 0);

  // A contains [1, 0, 2, 0]
See also:
http://www.sgi.com/tech/stl/replace_if.html
replace
replace_copy
replace_copy_if
template<typename ForwardIterator , typename InputIterator , typename Predicate , typename T >
void thrust::replace_if ( ForwardIterator  first,
ForwardIterator  last,
InputIterator  stencil,
Predicate  pred,
const T &  new_value 
)

replace_if replaces every element in the range [first, last) for which pred(*s) returns true with new_value. That is: for every iterator i in the range [first, last), and s in the range [stencil, stencil + (last - first)), if pred(*s) is true then it performs the assignment *i = new_value.

Parameters:
firstThe beginning of the sequence of interest.
lastThe end of the sequence of interest.
stencilThe beginning of the stencil sequence.
predThe predicate to test on every value of the range [first,last).
new_valueThe new value to replace elements which pred(*i) evaluates 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.
Tis a model of Assignable, and T is convertible to ForwardIterator's value_type.

The following code snippet demonstrates how to use replace_if to replace a device_vector's element with 0 when its corresponding stencil element is less than zero.

  #include <thrust/replace.h>
  #include <thrust/device_vector.h>

  struct is_less_than_zero
  {
    __host__ __device__
    bool operator()(int x)
    {
      return x < 0;
    }
  };
  
  ...
  
  thrust::device_vector<int> A(4);
  A[0] =  10;
  A[1] =  20;
  A[2] =  30;
  A[3] =  40;

  thrust::device_vector<int> S(4);
  S[0] = -1;
  S[1] =  0;
  S[2] = -1;
  S[3] =  0;

  is_less_than_zero pred;
  thrust::replace_if(A.begin(), A.end(), S.begin(), pred, 0);

  // A contains [0, 20, 0, 40]
See also:
http://www.sgi.com/tech/stl/replace_if.html
replace
replace_copy
replace_copy_if