5 ms·
C++ Cartesian Products
- 16s 16y agoOP here. I've been fascinated by CPs for some time. In particular, efficient ways in which to generate them. So I placed my source code on github that demonstrates a few ways I've done it in the past. My hope is that others can offer suggestions and insight into different (especially faster) approaches to generate them.
- _delirium 16y agoThe approach of hardcoding a set of nested loops per each possible count of input sets is probably okay for most common use-cases, but wouldn't a more general algorithm be better? You'd either need to generate N nested loops via template metaprogramming, or else use an algorithm that didn't explicitly have the nested loops.
- 16s 16y agoI've thought of doing something more general (see my while loops), and I've looked for other bits of source code that use a general approach, but haven't found any in C++. Maybe I've just over-looked it You're right, most people hard code the for loops they need. I call that the "intuitive approach". It works and it makes sense I think and performs OK. In fact, whenever I come up with new ways to do CPs I always compare their performance to the hard coded loops.