◆ generate_n()

template<typename T , typename Size , typename Gen >
const T* boost::simd::generate_n ( T *  first,
Size  n,
Gen  g 
)

Assigns values, generated by given function object g, to the first n elements in the range beginning at first.

Parameters
firstBeginning of the range of elements
nnumber of the elements to generate
gunary function object to generate_n elements
Requirement
  • first, last and out must be pointer to Vectorizable type.
  • f must be a polymorphic unary function object, i.e callable on generic types.
  • If n <= 0 does nothing
Example:
#include <boost/simd/algorithm/generate_n.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_n( d.data(),7, g() );
std::cout << "d = (" ;
for( auto e : d) std::cout << e << " ";
std::cout << ") " << std::endl;
bs::generate_n( d.data(),7, g(2.0f, 0.5f) );
std::cout << "d = (" ;
for( auto e : d) std::cout << e << " ";
std::cout << ") " << std::endl;
bs::generate_n( d.data()+1,8, g() );
std::cout << "d = (" ;
for( auto e : d) std::cout << e << " ";
std::cout << ") " << std::endl;
bs::generate_n( d.data()+1,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 )
Returns
Pointer to the element past the last element generated.