views:

211

answers:

2

i have a std::vector, namely

vector<vector<vector> > > mdata;

i want pass data from my mdata vector to the GSL function

gsl_spline_init(gsl_spline * spline, const double xa[], const double ya[], size_t size);

as ya. i already figured out that i can do things like

gsl_spline_init(spline, &(mgrid.front()), &(mdata[i][j][k].front()), mgrid.size());

this is fine if i want to pass the data from mdata for fixed i,j to gsl_spline_init().

however, now i would need to pass along the first dimension of mdata, so for fixed j,k.

i know that for any two fixed indices, all vectors along the remaining dimensions have the same length, so my vector is a 'regular cube'. so the offset between all the values i need should be the same.

of course i could create a temporary vector

int j = 123;
int k = 321;
vector<double> tmp;
for (int i = 0: i < mdata.size(); i++)
    tmp.push_back(mdata[i][j][k]);
gsl_spline_init(spline, &(mgrid.front()), &(tmp.front()), mgrid.size());

but this seems too complicated. perhaps there is a way to achieve my goal with pointer arithmetic?

any help is greatly appreciated :)

+1  A: 

You really can't do that without redesigning the array consumer function gsl_spline_init() - it relies on the data passed being a contiguous block of data. This is not the case with you three-level vector - not only it is a cube but also each level has a separate buffer allocated on heap.

sharptooth
+1  A: 

This can't be done. Not only with vectors, but even with plain arrays only the last dimension is a contiguous block of data. If gsl_spline_init took an iterator instead of array, you could try to craft some functor to choose appropriate data but I'm not sure it's worth trying. No pointer arithmetic can help you.

Tadeusz Kopec
An array 'int a[5][6][7];' is in contiguous memory and can be traversed serially via pointer.
Martin York
I meant that values a[1][2][0], a[1][2][1], a[1][2][2] and so on, make a contiguous part of memory and they can be passed do the function. Values a[0][1][2], a[1][1][2], a[2][1][2] do not make a contiguous part of memory and they can't be passed to the function.
Tadeusz Kopec