gpt4 book ai didi

c# - 锯齿状阵列与一个大阵列?

转载 作者:太空狗 更新时间:2023-10-29 19:44:11 26 4
gpt4 key购买 nike

不太确定如何问这个问题,但我有 2 种方法(到目前为止)查找数组

选项 1 是:

bool[][][] myJaggegArray;

myJaggegArray = new bool[120][][];
for (int i = 0; i < 120; ++i)
{
if ((i & 0x88) == 0)
{
//only 64 will be set
myJaggegArray[i] = new bool[120][];
for (int j = 0; j < 120; ++j)
{
if ((j & 0x88) == 0)
{
//only 64 will be set
myJaggegArray[i][j] = new bool[60];
}
}
}
}

选项 2 是:

bool[] myArray;
// [998520]
myArray = new bool[(120 | (120 << 7) | (60 << 14))];

这两种方法都很好用,但是是否有另一种(更好的)方法来进行快速查找?如果速度/性能很重要,您会采用哪种方法?

这将用于 chessboard implementation (0x88)主要是

[from][to][dataX]对于选项 1

[(from | (to << 7) | (dataX << 14))]对于选项 2

最佳答案

我建议使用一个大数组,因为有一个大内存块的优点,但我也鼓励为该数组编写一个特殊的访问器。

class MyCustomDataStore
{
bool[] array;
int sizex, sizey, sizez;

MyCustomDataStore(int x, int y, int z) {
array=new bool[x*y*z];
this.sizex = x;
this.sizey = y;
this.sizez = z;
}

bool get(int px, int py, int pz) {
// change the order in whatever way you iterate
return array [ px*sizex*sizey + py*sizey + pz ];
}

}

关于c# - 锯齿状阵列与一个大阵列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16015298/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com