is anyone aware of generalized transform, for_each?

E

er

hi,

is anyone aware of a library that generalizes transform, for_each?
e.g.

transform(
vec1.begin(),
vec1.end(),
vec2.begin(),
vec3.begin(),
vec4.begin(),
back_inserter(vec5),
op
)//where op is a 4-ary function
 
J

John Harrison

er said:
hi,

is anyone aware of a library that generalizes transform, for_each?
e.g.

transform(
vec1.begin(),
vec1.end(),
vec2.begin(),
vec3.begin(),
vec4.begin(),
back_inserter(vec5),
op
)//where op is a 4-ary function

I've just written one,

template <class II, class OI, class F4>
OI transform(II first, II last, II first2, II first3, II first4, OI
dest, F4 func)
{
while (first != last)
{
*dest = func(*first, *first2, *first3, *first4);
++first;
++first2;
++first3;
++first4;
++dest;
}
return dest;
}

That will be $8000 please.

john
 
R

red floyd

John said:
I've just written one,

template <class II, class OI, class F4>
OI transform(II first, II last, II first2, II first3, II first4, OI
dest, F4 func)

template <typename II1, typename II2, typename II3, typename II4,
typename OI, typename F4>
OI transform(II1 first, II1 last, II2 first2, II3 first3, II4 first4,
OI dest, F4 func)
{
while (first != last)
{
*dest = func(*first, *first2, *first3, *first4);
++first;
++first2;
++first3;
++first4;
++dest;
}
return dest;
}

Which allows for merging of different iterator types (list, vector,
set...) as well as different types for the values (int, float, double...)
 
E

er

template <typename II1, typename II2, typename II3, typename II4,
typename OI, typename F4>
OI transform(II1 first, II1 last, II2 first2, II3 first3, II4 first4,
OI dest, F4 func)


Which allows for merging of different iterator types (list, vector,
set...) as well as different types for the values (int, float, double...)

yes, thank you both, but this is generalized in type, not in
dimension. i should have made that clear in the first post, and
replace 4 by an arbitrary N. i'm aware that this is no trivial
task...hence my posting here.
 
J

Jeff F

er said:
hi,

is anyone aware of a library that generalizes transform, for_each?
e.g.

transform(
vec1.begin(),
vec1.end(),
vec2.begin(),
vec3.begin(),
vec4.begin(),
back_inserter(vec5),
op
)//where op is a 4-ary function

See http://www.boost.org/libs/iterator/doc/zip_iterator.html. Quote:

std::vector<double>::const_iterator beg1 = vect_of_doubles.begin();
std::vector<double>::const_iterator end1 = vect_of_doubles.end();
std::vector<int>::const_iterator beg2 = vect_of_ints.begin();
std::vector<int>::const_iterator end2 = vect_of_ints.end();

std::for_each(beg1, end1, func_0());
std::for_each(beg2, end2, func_1());
These two iterations can now be replaced with a single one as follows:

std::for_each(
boost::make_zip_iterator(
boost::make_tuple(beg1, beg2)
),
boost::make_zip_iterator(
boost::make_tuple(end1, end2)
),
zip_func()
);
struct zip_func :
public std::unary_function<const boost::tuple<const double&, const int&>&,
void>
{
void operator()(const boost::tuple<const double&, const int&>& t) const
{
m_f0(t.get<0>());
m_f1(t.get<1>());
}

private:
func_0 m_f0;
func_1 m_f1;
};
Jeff Flinn
 
E

er

Seehttp://www.boost.org/libs/iterator/doc/zip_iterator.html. Quote:

std::vector<double>::const_iterator beg1 = vect_of_doubles.begin();
std::vector<double>::const_iterator end1 = vect_of_doubles.end();
std::vector<int>::const_iterator beg2 = vect_of_ints.begin();
std::vector<int>::const_iterator end2 = vect_of_ints.end();

std::for_each(beg1, end1, func_0());
std::for_each(beg2, end2, func_1());
These two iterations can now be replaced with a single one as follows:

std::for_each(
boost::make_zip_iterator(
boost::make_tuple(beg1, beg2)
),
boost::make_zip_iterator(
boost::make_tuple(end1, end2)
),
zip_func()
);
struct zip_func :
public std::unary_function<const boost::tuple<const double&, const int&>&,
void>
{
void operator()(const boost::tuple<const double&, const int&>& t) const
{
m_f0(t.get<0>());
m_f1(t.get<1>());
}

private:
func_0 m_f0;
func_1 m_f1;};

Jeff Flinn

thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,647
Latest member
NelleMacy9

Latest Threads

Top