
On 10/26/11 09:46, Christoph Heindl wrote:
Hi,
I'm on search for an algorithm that produces the cartesian product for n-dimension. The coordinates of each dimension are not limited to numbers.
Ideally I'd like to write something along the lines of
std::vector<int> d0; // (assume d0 = [0, 3, 2]); std::vector<bool> d1; // (assume d1 = [true]); std::vectorstd::string d2; // (assume d2 = ["hello", "world"])
typedef boost::fusion::vector< std::vector<int>, std::vector<bool>, std::vectorstd::string > tt;
tt space(d0, d1, d2);
boost::cartesian_product<tt> cp(space);
BOOST_FOREACH(const boost::cartesian_product<tt>::value_type &t, cp) { // Invoked for every possible combination of the three dimension. // t is boost::tuple
or similar } I think that something similar can be accomplished using boost.fusion, but I cannot derive the algorithm. What I have found so far is a meta cartesian product [1].
Has anyone such an algorithm lying around? Any help would be greatly appreciated.
Best regards, Christoph
The index_stack_simple.cpp upload here: http://www.datasimfinancial.com/forum/viewtopic.php?t=416#984 shows how to do this where values in the containers are all same type. It works by simply keeping a vector of loop indices or iterators. During the operator++, it checks which which iterator to increment, and if that is at the end, then it moves to the next iterator. Dereferencing all the iterators should give 1 element in the cross product. I would think that simply changing the vector of loop indices to a fusion vector of those indices would enable the same to be done containers of different types. HTH.