#ifndef MIGRAPH_GUARD_MIGRAPHLIB_SHAPE_FOR_EACH_HPP #define MIGRAPH_GUARD_MIGRAPHLIB_SHAPE_FOR_EACH_HPP #include #include #include namespace migraph { inline namespace MIGRAPH_INLINE_NS { template void shape_for_each(const migraph::shape& s, F f) { // Ensure calls to f use const ref to vector auto call = [&f](const std::vector& i) { f(i); }; std::vector indices(s.lens().size()); for(std::size_t i = 0; i < s.elements(); i++) { std::transform(s.strides().begin(), s.strides().end(), s.lens().begin(), indices.begin(), [&](std::size_t stride, std::size_t len) { assert(len > 0 and stride > 0); return (i / stride) % len; }); call(indices); } } } // inline namespace MIGRAPH_INLINE_NS } // namespace migraph #endif