use stdlib functions with indexing instead of Iterators?

Viewed 62

I want to use std::partial_sum on Eigen::VectorXd but I couldn't really find Iterators in Eigen data structures at all. Is there some sort of quick Iterator wrapper that uses classic indexing under the hood? Or what would be a good way to do this instead of re-implementing partial sum with indexing?

2 Answers

You can always get the raw pointer to your data and use it in conjunction with the size.

std::partial_sum(array.data(), array.data() + array.rows() * array.cols(), output.data());

There are 2D iterators available in Eigen though, if you want to iterate over one dimension first and then the other one for each element.

This has been implemented in October 2018, and will be part of the 3.4 release. You can already use them with the trunk version, e.g.,

void foo(Eigen::VectorXd& in_out)
{
    std::partial_sum(in_out.begin(), in_out.end(), in_out.begin());
}

Godbolt demonstration (showing near equivalent code to std::vector): https://godbolt.org/z/bFxcCA

See this (merged) pull request, if you are interested in details: https://bitbucket.org/eigen/eigen/pull-requests/519/

Related