◆ generate()

template<typename T , typename Gen >
void boost::simd::generate ( T *  first,
T *  last,
Gen  g 
)

Assigns each element in range [first, last) a value generated by the successive calls to the given function object g.

Parameters
firstBeginning of the range of elements
lastEnd of the range of elements
gunary function object to generate elements
Requirement
  • first, last and out must be pointer to Vectorizable type.
  • g must be a Polymorphic Unary function object. Its prototype must be T g(boost::simd::as_<T>) as g can be called for scalars and packs and must have a way to discriminate its return type.
Warning
The expectd signature for g is not the one of the standard algorithm.
Example:
#include <boost/simd/algorithm/generate.hpp>
#include <boost/simd/function/enumerate.hpp>
#include <boost/simd/pack.hpp>
#include <iostream>
#include <vector>
namespace bs = boost::simd;
struct g
{
g(float seed = 0.0f, float step = 1.0f)
: i_(seed), step_(step) {}
template < typename T> T operator()(bs::as_<T>)
{
auto z = bs::enumerate<T>(i_, step_);
return z;
}
float i_;
float step_;
};
int main()
{
namespace bs = boost::simd;
std::vector<float> d(8);
d[7] = 42.0f;
bs::generate( d.data(),d.data()+7, g() );
std::cout << "d = (" ;
for( auto e : d) std::cout << e << " ";
std::cout << ") " << std::endl;
bs::generate( d.data(),d.data()+7, g(2.0f, 0.5f) );
std::cout << "d = (" ;
for( auto e : d) std::cout << e << " ";
std::cout << ") " << std::endl;
bs::generate( d.data()+1,d.data()+8, g() );
std::cout << "d = (" ;
for( auto e : d) std::cout << e << " ";
std::cout << ") " << std::endl;
bs::generate( d.data()+1,d.data()+8, g(2.0f, 0.5f) );
std::cout << "d = (" ;
for( auto e : d) std::cout << e << " ";
std::cout << ") " << std::endl;
return 0;
}
Possible output:
d = (0 1 2 3 4 5 6 42 )
d = (2 2.5 3 3.5 4 4.5 5 42 )
d = (2 0 1 2 3 4 5 6 )
d = (2 2 2.5 3 3.5 4 4.5 5 )