Reputation: 9963
vector<bool> working_lattice(box.rect.length * box.rect.height * box.rect.width);
How do I access working_lattice[1][5][3]
using the style of declaration above?
Upvotes: 3
Views: 4989
Reputation: 123
i.e considering this one:
A[R][S][T]
assuming that its base address is addr_base_A
,
so you hope that you can get the address of one specific element A[i][j][k]
,
the answer I think is: S*T*i + T*j + k + addr_base_A
.
Hope this helps :)
Upvotes: 1
Reputation: 3276
You need to access it as
(i * length * height) + (j * height) + k
So in your case
working_lattice[(i * box.rect.length * box.rect.height) + (j * box.rect.height) + k);
or
working_lattice[(1 * box.rect.length * box.rect.height) + (5 * box.rect.height) + 3);
EDIT: Since you mentioned x, y, z elsewhere
working_lattice[(x * box.rect.length * box.rect.height) + (y * box.rect.height) + z);
Upvotes: 5
Reputation: 28856
This depends on whether you're using row-major or column-major ordering. Row-major is more typical in C/C++, but you can do either if you're doing it manually.
In row-major ordering, to get to the i, j, k'th element, you need to go through box.rect.height * box.rect.width * i
elements to get up to the i
th row, plus box.rect.width * j
elements to get to the j
th column of that row, plus k
to get back to the k
th element depthwise. To be super-explicit:
const size_t n_x = box.rect.length;
const size_t n_y = box.rect.height;
const size_t n_z = box.rect.width;
working_lattice[1 * n_x * n_z + 5 * n_z + 3]
This is obviously pretty annoying, so you might want to define an inline function or something to help out.
Upvotes: 3