"src/targets/vscode:/vscode.git/clone" did not exist on "34c2889a4edfa8436358e4b6ce0a61cc372bc417"
shape_for_each.hpp 770 Bytes
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
6
7
8
#ifndef RTG_GUARD_RTGLIB_SHAPE_FOR_EACH_HPP
#define RTG_GUARD_RTGLIB_SHAPE_FOR_EACH_HPP

#include <rtg/shape.hpp>
#include <algorithm>

namespace rtg {

Paul's avatar
Paul committed
9
template <class F>
Paul's avatar
Paul committed
10
11
12
13
14
void shape_for_each(const rtg::shape& s, F f)
{
    // Ensure calls to f use const ref to vector
    auto call = [&f](const std::vector<std::size_t>& i) { f(i); };
    std::vector<std::size_t> indices(s.lens().size());
Paul's avatar
Paul committed
15
16
    for(std::size_t i = 0; i < s.elements(); i++)
    {
Paul's avatar
Paul committed
17
        std::transform(s.strides().begin(),
Paul's avatar
Paul committed
18
19
20
21
                       s.strides().end(),
                       s.lens().begin(),
                       indices.begin(),
                       [&](std::size_t stride, std::size_t len) { return (i / stride) % len; });
Paul's avatar
Paul committed
22
23
24
25
26
27
28
        call(indices);
    }
}

} // namespace rtg

#endif