Currently, boost::fusion::for_each iterates over the elements of a single sequence. I am trying to create a function which will work in a similar way but with many sequences and will iterate over all possible combinations between sequences.
For example if I have three sequences S1, S2, S3, I would like to create a functor like this
struct my_functor {
template <class x, class y, class z>
void operator()(x& el1, y& el2, z& el3) {...}
}
and then call
for_each(s1, s2, s3, my_functor()) // applies the functor to all combinations of elements of s1, s2, s3
where s1, s2, s3 are instances of S1, S2, S3.
I started by writing code for the general case (any number of sequences) but found it too hard. So I decided to start with only two sequences and take it from there. I have managed to get it done when I have two sequences (assuming fusion::vectors for simplicity) like this:
//for_each.hpp
#include <boost/fusion/include/mpl.hpp>
#include <boost/fusion/include/at_c.hpp>
#include <boost/fusion/include/vector.hpp>
#include <boost/fusion/include/back.hpp>
#include <boost/mpl/size.hpp>
template <class Seq1, class Seq2, int i1, int i2, class F>
struct my_call {
static void apply(Seq1& seq1, Seq2& seq2, F& f) {
f(boost::fusion::at_c<i1>(seq1), boost::fusion::at_c<i2>(seq2)); // apply functor for a given pair of ints
my_call<Seq1, Seq2, i1, i2+1, F>::apply(seq1, seq2, f); // increase second int by 1 and apply functor again
}
};
// terminal condition for 2nd sequence
template <class Seq1, class Seq2, int i1, class F>
struct my_call<Seq1, Seq2, i1, boost::mpl::size<Seq2>::type::value - 1, F> {
static void apply(Seq1& seq1, Seq2& seq2, F& f) {
f(boost::fusion::at_c<i1>(seq1), boost::fusion::back(seq2));
my_call<Seq1, Seq2, i1+1, 0, F>::apply(seq1, seq2, f); // reset 2nd int and increase 1st by 1
}
};
// terminal condition for both sequences
template <class Seq1, class Seq2, class F>
struct my_call<Seq1, Seq2, boost::mpl::size<Seq2>::type::value - 1, boost::mpl::size<Seq2>::type::value - 1, F> {
static void apply(Seq1& seq1, Seq2& seq2, F& f) {
f(boost::fusion::back(seq1), boost::fusion::back(seq2));
}
};
// the actual function
template <class Seq1, class Seq2, class F>
void for_each(Seq1& seq1, Seq2& seq2, F& f) {
my_call<Seq1, Seq2, 0, 0, F>::apply(seq1, seq2, f);
}
and the main as
//main.cpp
#include "for_each.hpp"
#include <iostream>
struct myf {
template <class X, class Y>
void operator()(X& x, Y& y) {
std::cout << x + y << std::endl;
}
};
int main() {
boost::fusion::vector<int, double> x(1, 2.5);
boost::fusion::vector<double, int> y(2, 5);
myf F;
for_each(x, y, F);
return 0;
}
My main (no pun intended) problem is generalising the above to get it working with any number of sequences. Any suggestions are very welcome! Thanks