The indexing formula is given by the multiplication of any given dimension value with the product of all the previous dimensions.
Index = xn ( D1 * ... * D{n-1} ) + x{n-1} ( D1 * ... * D{n-2} ) + ... + x2 * D1 + x1
So for 4D
index = x + y * D1 + z * D1 * D2 + t * D1 * D2 * D3;
x = Index % D1;
y = ( ( Index - x ) / D1 ) % D2;
z = ( ( Index - y * D1 - x ) / (D1 * D2) ) % D3;
t = ( ( Index - z * D2 * D1 - y * D1 - x ) / (D1 * D2 * D3) ) % D4;
/* Technically the last modulus is not required,
since that division SHOULD be bounded by D4 anyways... */
The general formula being of the form
xn = ( ( Index - Index( x1, ..., x{n-1} ) ) / Product( D1, ..., D{N-1} ) ) % Dn
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…