Skip to main content
gamedev.net
🔒 Locked

Array index trick

Started by sj Apr 14, 2006 at 5:18 AM 8 replies 1.2k views
Original Post
sj
sj
I remember there was a trick by which you could have a one-dimensional array and use it as though it were a two-dimensional array when accessing its elements by using some simple calculation for the index. I just can't remember what the calculation was. It's been a while since the last time I programmed and apparently this is one of the things I have forgotten. It was supposed to be a bit faster to use one-dimensional arrays in place of two-dimensional ones. I'd appreciate it if anyone could refresh my memory. Juha
Evil Steve
Evil Steve
const int width = 8;const int height = 16;int array[width * height];int x = 3;int y = 7;int element = array[x + y * width];
sj
sj
Oh that's it! Somehow I managed to try everything else but that. Thank you.

Juha
jpetrie
jpetrie
You should endevour to figure things out, not guess. Guessing is dangerous because it can lead to situations where things appear to work correctly but actually don't, and you won't discover that until later. Here's how the indexing "trick" works:

Consider an array of 25 elements.
***** ***** ***** ***** *****01234 56789 ...


Conceptually, we can view that as a linear array of 25 (as above) or as a logical "matrix" or "2D array" 5 units per side:

*****01234*****56789(etc, omitted for brevity)


Note how the indices haven't changed. Also note that the first element of row 1 (zero based) is at index 5, or (width * 1). If you'd continue the diagram you'd see that the second row begins at index 10, or (width * 2). From this it should become clear that translating just the "y" part into a linear index is simply (y * width). This works out for accessing row zero (0 * width == 0) as well. Now that you've managed to calculate the correct "row," finding the exact element is simply a matter of adding the "x" index to the already calculated linear index for the row; the final calculation becomes (y * width + x).

Note that the array, obviously, does not need to be considered logically square for this to work out (a 30-element linear array can be interpreted as a 3x10 grid, for example).
riltniqaz
riltniqaz
Quote:
Original post by jpetrie
You should endevour to figure things out, not guess. Guessing is dangerous because it can lead to situations where things appear to work correctly but actually don't, and you won't discover that until later.
An excellent piece of advice, there.
jpetrie
jpetrie
It's still possible to encounter span/stride issues even today, with certain image formats. It's not terribly hard to deal with; span or stride is often measured as total number of bytes or elements per row, or number of extra bytes/elements per row. The calculation becomes some like (x + y * stride) or (x + y * (width + stride)), respectively. As long as you have the documentation about the nature of the structure of the image or data, and are aware of it, its not too bad.
sj
sj
Yes, that is a good piece of advice, jpetrie. I should really get into that habit. My problem is that I really suck at math (a great trait for a programmer ;)) so sometimes I find guessing easier than just doing the brainwork. But thanks, everybody, for your replies.

Juha
Adam Hamilton
Adam Hamilton
I imagine you might get some speed boost by making width a multiple of the cache line but maybe this is limited to certain situations.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.