Boost logo

Boost Users :

Subject: [Boost-users] [boost-users] Cartesian product
From: Christoph Heindl (christoph.heindl_at_[hidden])
Date: 2011-10-26 10:46:39


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


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