|
Boost Users : |
Subject: Re: [Boost-users] [boost-users] Cartesian product
From: Larry Evans (cppljevans_at_[hidden])
Date: 2011-10-26 11:34:39
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::vector<std::string> d2; // (assume d2 = ["hello", "world"])
>
> typedef boost::fusion::vector<
> std::vector<int>,
> std::vector<bool>,
> std::vector<std::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<int, bool, std::string> 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
>
> [1] http://organicvectory.com/index.php?option=com_content&view=article&id=75:boostmplcartesianproduct&catid=42:boost&Itemid=78
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.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net