After seeing the example of xtensor
matrix adaptors of 1D containers, I was wondering if it is also possible to have a resizeable adaptor of STL containers like vectors.
In the process, I would like to also find out and use the type of such an adaptor, instead of using auto
.
Starting with the C-style array adaptor and compute()
function –which multiplies the 2×1 row vector with a 2×1 column and returns the xarray as a 2×2 matrix– I got as far as
using shape_type = std::vector < size_t >;
using adaptor_typev = xt::xarray_adaptor < xt::xbuffer_adaptor < double*,
xt::acquire_ownership >,
xt::layout_type::dynamic,
shape_type >;
unsigned vsize = 2;
std::vector < double > vdata = { 0, 1 };
std::vector < std::size_t > vshape = { vsize };
std::vector < std::size_t > vstrides = { 1 };
adaptor_typev
my_adaptorv = xt::adapt ( vdata.data(),
vsize,
xt::acquire_ownership(),
vshape,
vstrides);
std::cout << "n";
std::cout << "nbefore reshapen";
for ( unsigned i = 0; i < vsize; i++ )
std::cout << vdata [ i ] << " ";
compute ( my_adaptorv );
std::cout << "nafter reshapen";
for ( unsigned i = 0; i < vsize * vsize; i++ )
std::cout << vdata [ i ] << " ";
The output that I get from the C-style xarray_adaptor (from the xtensor website) and the std:vector xarray_adaptor, respectively, is:
before reshape
0 1
after reshape
0 1 0 2
before reshape
0 1
after reshape
0 1 nan -5.11336e-311
==== Program exited with exit code: 3221226356 ====
Time elapsed: 000:00.016 (MM:SS.MS)
The last two values suggest that the resize has not happened, and the non-zero exit code shows all is not well. If I run it in the debugger, it exits with a SIGTRAP at the end of main()
. The top of the call stack says ntdll!RtllsZeroMemory
.
Does that mean that to resize an xarray
adaptor of an std::vector
, I need to resize the vector separately? Or is there a way to do it without trying to become owner of the vector’s data (that does not sound right to begin with)?