C++ では、それぞれ min[n] から max[n] までの範囲の任意の範囲で n 次元配列を反復処理し、縦座標を ord[n] 全体でそれぞれ維持したいと考えています。
すなわち。次の一般的な解決策:
for (int x = 0; x < 10; x++)
for (int y = 3; y < 20; y++)
for (int z = -2; z < 5; z++)
...
doSomething(x, y, z ...)
フォームの:
int min[n] {0, 3, -2 ...}
int max[n] {10, 20, 5 ...}
int ord[n] {0, 0, 0 ...};
int maxIterations = (max[0] - min[0]) * (max[1] - min[1]) * ....
for (int iteration = 0; iteration < maxIterations; iteration++)
doSomething(ord)
iterate(n, ord, min, max)
私が考えることができる iterate() の最速のアルゴリズムは次のとおりです。
inline void iterate(int dimensions, int* ordinates, int* minimums, int* maximums)
{
// iterate over dimensions in reverse...
for (int dimension = dimensions - 1; dimension >= 0; dimension--)
{
if (ordinates[dimension] < maximums[dimension])
{
// If this dimension can handle another increment... then done.
ordinates[dimension]++;
break;
}
// Otherwise, reset this dimension and bubble up to the next dimension to take a look
ordinates[dimension] = minimums[dimension];
}
}
これにより、必要に応じて各縦座標がインクリメントおよびリセットされ、コールスタックや計算が回避されます。
より高速なアルゴリズムはありますか?