i have an arbitrary "reshaping" function which, given a linear sequential array can "reshape" it to 2D or 3D. the order in which the X-dimension, Y-dimension and Z-dimension may all be changed, as well as the size of the X, Y and Z dimensions, may all be changed, at runtime. consequently it is not possible to use static compilers or static allocation.
here is a function that, for the full sequential range, prints out a corresponding (remapped) set of indices:
(xdim, ydim, zdim) = (3, 2, 5)
lims = [xdim, ydim, zdim]
idxs = [0,0,0]
order = [1,0,2]
for idx in range(xdim * ydim * zdim):
new_idx = idxs[0] + idxs[1] * xdim + idxs[2] * xdim * ydim
print new_idx,
for i in range(3):
idxs[order[i]] = idxs[order[i]] + 1
if (idxs[order[i]] != lims[order[i]]):
break
print
idxs[order[i]] = 0
what i actually need is a function which, when it is passed an arbitrary index, returns one (correct) remapped index.
i suppose what i could hypothetically do is have a function that sets up the full array (at startup time), and indexes it. i would however like to see an algorithmic version of the above.