Skip to main content

How do I access fields of multidimensional array stored in 1-d array?

1 reply [Last post]
imagero
Offline
Joined: 2003-11-18
Points: 0

I need to access fields of multidimensional array stored in 1-d array.
Array is created at runtime and may have up to 15 dimensions.
So to access say 2d field at (x, y) I would write offset = x + y * w;
For 3d (x, y, z) offset = x + y * w + z * w * h;
How do I compute it for n dimensions?

BTW Can anyone explain me what is dope vector? And how to implement it in java?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
imagero
Offline
Joined: 2003-11-18
Points: 0

No replies after 9 days... What's wrong with this forum (and java)? In Adobe forum is at least one guy (emploee) who gives answers (not always useful answers though, but it is definitely better as nothing).
Well, after some thinking about this problem, I came to following code:
public class MDArray {:
int [] dims;
int [] multipliers;

protected MDArray(int[] dims) {
this.dims = dims;
multipliers = new int[dims.length];
for(int i = 0; i < dims.length; i++) {
int m = 1;
for(int j = i; j < dims.length; j++) {
m *= dims[j];
}
multipliers[i] = m;
}
}

int getOffset(int [] coords) {
int offset = 0;
for(int i = 0; i < multipliers.length; i++) {
offset += coords[i] * multipliers[i];
}
return offset;
}
}