It's probably just me, but I didn't quite get that.. you meant that you
make up the larger matrices from 2x2 ones? If that is the case, it is
not a surmountable problem to have nesting when computing the offset in
memory for a cell:
OK, 2x2 sub-blocks.. this means that you will have 4 cells per
sub-block, therefore the formula for addressing indivisual cell wold be
something like this:
struct block
{
int xsize; // matrix width
int ysize; // matrix height
int computeOffset(int x, int y)
{
const int blocksize = 2;
int xblock = x / blocksize;
int yblock = y / blocksize;
x %= blocksize;
y %= blocksize;
// ... at this point (xblock,yblock) is which block..
// ... (x,y) is location within the block
// TODO: this could be made simpler when some assumptions I didn't
// make could be verified.. so as it is..
int offset = (yblock * (xsize/blocksize) + xblock) * blocksize *
blocksize;
offset += y * blocksize + x; // adjust for intra block position
return offset;
}
};
I could be way off mark but that's the basic idea I'd use for something
like that, implementation details withstanding (disclaimer: I might not
implement it like that but I apparently would demonstrate the basic
idea like that
Asserting, range-checking etc. is left as exercise to the reader (maybe
just clip the (x,y), whatever)... also using "int" might not be
recommended but again, I just wanted SOME type to demonstrate the
concept.. I'm used to doing this with power-of-two data mostly (useful
technique for storing spatially local data in memory close together, v.
good for cache easily worth the more complex addressing -- which infact
is cheap when using powers-of-two..) but that might be something
completely different...