xtensor icon indicating copy to clipboard operation
xtensor copied to clipboard

usability issues with xt::view copying

Open lednakashim opened this issue 2 years ago • 1 comments

Somewhat confused as to which views can be implicitly copied and which can't.

Does this look like a usability bug to you folks?

https://godbolt.org/z/zqfbjvYb6

#include <xtensor/xarray.hpp>
#include <xtensor/xview.hpp>

int main()
{
    using array_t = xt::xarray<int, xt::layout_type::dynamic>;
    array_t array = {{1,2,3},{4,5,6}};
    // works
    auto view1 = xt::view(array,xt::range(0, 1, 1),xt::range(0, 1, 1));
    array_t copy1 = view1;// works
    // doesn't work?
    std::vector<decltype(xt::range(0, 1, 1))> ranges = {xt::range(0, 1, 1),xt::range(0, 1, 1)};
    auto view2 = xt::view(array,ranges);
    array_t copy2 = view2;// won't copy?
    return 0;
}

But in principle we could loop over view2 and copy the data?

additionally, a regular copy doesn't work, but we have to do a std::transform ?

  array_t copy;
  copy.resize(array.shape());
  // std::copy(a.crbegin(), a.crend(), b.begin());//doesn't work???
  std::transform(view1.cbegin(), view1.cend(), copy.begin(), [](auto &&v) { return v; }); 

Additionally for std::transform to work view1 can't be of a const xarray.

lednakashim avatar Aug 14 '22 21:08 lednakashim

Actually the issue is that std::vector<decltype(xt::range(0, 1, 1))> cannot be used as a slice argument of xt::view. This latter requires to know the number fo slices and their type at build time, which is not possible with a std::vector. Prefer the usage of strided_view in your case.

However, the compiler should notify the error upon view construction, not when trying to assign it.

JohanMabille avatar Aug 22 '22 09:08 JohanMabille