◆ replace_if()

template<typename T , typename P >
void boost::simd::replace_if ( T *  first,
T *  last,
P const &  p,
T const &  new_val 
)

Replaces all elements for which predicate p returns true by new_value in the range [first, last).

Parameters
firstBeginning of the range of elements
lastEnd of the range of elements
punary predicate which returns true if the element value should be replaced.
new_valReplacement value
Requirement
  • first, last and out must be pointer to Vectorizable type.
  • new_val must be a scalar value convertible to pointee type of first.
Example:
#include <boost/simd/algorithm/replace_if.hpp>
#include <boost/simd/pack.hpp>
#include <iostream>
#include <vector>
namespace bs = boost::simd;
struct p
{
template < typename T >
auto operator()(const T & t) const -> decltype((t >= 3.5f)&&(t < 5.0f))
{
return (t >= 3.5f)&&(t < 5.0f);
}
};
int main()
{
namespace bs = boost::simd;
std::vector<float> d{2.0f, 2.5f, 4.0f, 3.5f, 4.0f, 4.0f, 5.0f, 4.0f};
d[7] = 42.0f;
bs::replace_if( d.data(),d.data()+7, p(), 0.0f);
std::cout << "d = (" ;
for( auto e : d) std::cout << e << " ";
std::cout << ") " << std::endl;
std::vector<float> d1{4.0f, 2.5f, 4.0f, 3.5f, 4.0f, 4.0f, 5.0f, 4.0f};
bs::replace_if( d1.data()+1,d1.data()+8, p(), 0.0f);
std::cout << "d1 = (" ;
for( auto e : d1) std::cout << e << " ";
std::cout << ") " << std::endl;
return 0;
}
possible output:
d = (2 2.5 0 0 0 0 5 42 )
d1 = (4 2.5 0 0 0 0 5 0 )