By Daniel · January 5, 2009
Although GM doesn't provide native support for 3D arrays, there are numerous ways of simulating them. The standard technique is to "flatten" the three dimensions by converting three indices into one:
array[ a, b, c ] => array[ a*bLimit*cLimit + b*cLimit + c ]
For more space, you can store data in 2D arrays instead of 1D arrays. Remember that GM places a limit of 32,000 on each index, so a 2D array can potentially hold 32,0002 = 1,024,000,000 values. The simple, standard approach looks like this:
array[ a, b, c ] => array[ a, b*cLimit + c ]
So how can we convert three indices into two in a way that preserves this balance between dimensions (or allows us to choose the limits with more flexibility)? Well, we can expand one index into two using the following technique, which is essentially the reverse of flattening:
array[ i ] => array[ i div dimL, i mod dimL ]
array[ a, b, c ] => array[ (a*bLimit*cLimit + b*cLimit + c) div 32000, (a*bLimit*cLimit + b*cLimit + c) mod 32000 ]
The code used to reduce three indices to two is admittedly somewhat sophisticated, so I'd recommend making two scripts for the two resulting indices. In the example, I named them "idx1" and "idx2". Once you have this, the conversion can be simplified to this:
array[ a, b, c ] => array[ idx1(a, b, c), idx2(a, b, c) ]
array[ idx1(a, b, c), idx2(a, b, c) ] = <value>;
Included is a simple example showing how these ideas can be implemented. For simplicity, I gave each dimension a limit of 1,000 (even though only the maximum is 1,007, and only 4 is needed).
Happy flattening,
~ Daniel
Categories: Data processing
There are no comments to display.
You must be signed in to post comments.