How to use C++ ranges to implement numpy.ndindex?
views::cartesian_product
is already variadic, you just need to expand a pack into it.
template <typename... Ts>
inline auto NDIndex(Ts ... ds) {
using namespace ranges;
return views::cartesian_product(views::indices(ds)...);
}
// Usage
int main() {
for (const auto[i1, i2] : NDIndex(5, 4)) {
}
for (const auto[i1, i2, i3] : NDIndex(5, 4, 7)) {
}
}
This can be done this way
#include <range/v3/view/indices.hpp>
#include <range/v3/view/cartesian_product.hpp>
template <unsigned... Ind>
constexpr inline auto NDIndex() {
using namespace ranges;
return views::cartesian_product(views::indices(Ind)...);
}
int main() {
for (const auto[i1, i2] : NDIndex<5, 4>()) {
}
for (const auto[i1, i2, i3] : NDIndex<5, 4, 7>()) {
}
}
Live example