我如何访问使用 Z 顺序以 O(1) 时间复杂度存储在数组中的数据?我需要通过坐标快速访问每个元素。有没有比使用 while 移位位更快的方法来访问这些数据?
一种方法是使用查找表(我有静态大小的数据)
编辑:
我现在的一个想法是使用 y*SIZE+x 按顺序存储叶子
编辑2:
我正在 std::bitset 中的四叉树中讲述位。我正在尝试检查某些数据是否可用。大小为 128*128 的矩阵。所以我可以跳过暴力矩阵搜索空数据。
可以通过以下代码计算z阶曲线值:
uint32_t calcZOrder(uint16_t xPos, uint16_t yPos)
{
static const uint32_t MASKS[] = {0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF};
static const uint32_t SHIFTS[] = {1, 2, 4, 8};
uint32_t x = xPos; // Interleave lower 16 bits of x and y, so the bits of x
uint32_t y = yPos; // are in the even positions and bits from y in the odd;
x = (x | (x << SHIFTS[3])) & MASKS[3];
x = (x | (x << SHIFTS[2])) & MASKS[2];
x = (x | (x << SHIFTS[1])) & MASKS[1];
x = (x | (x << SHIFTS[0])) & MASKS[0];
y = (y | (y << SHIFTS[3])) & MASKS[3];
y = (y | (y << SHIFTS[2])) & MASKS[2];
y = (y | (y << SHIFTS[1])) & MASKS[1];
y = (y | (y << SHIFTS[0])) & MASKS[0];
const uint32_t result = x | (y << 1);
return result;
}
它是从这里获取的Bit Twiddling Hacks
从 128x128 数组(或任何其他大小)中,您可以轻松计算任何位置的 z 顺序曲线值。例如:
xPos = 2, yPos = 3 -> z order curve value = 7
示例代码的最大数组大小为 65536*65536。为了方便起见,只需使用 2 的幂,在这种情况下,最大浪费的空间约为。 3/4
重现 Wiki 结果https://en.wikipedia.org/wiki/Z-order_curve
#include <stdio.h>
static const unsigned int B[] = {0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF};
static const unsigned int S[] = {1, 2, 4, 8};
unsigned int zorder2D(unsigned x, unsigned y){
x = (x | (x << S[3])) & B[3];
x = (x | (x << S[2])) & B[2];
x = (x | (x << S[1])) & B[1];
x = (x | (x << S[0])) & B[0];
y = (y | (y << S[3])) & B[3];
y = (y | (y << S[2])) & B[2];
y = (y | (y << S[1])) & B[1];
y = (y | (y << S[0])) & B[0];
return x | (y << 1);
}
int main()
{
const unsigned nx=8,ny=8;
unsigned res[ny][nx];
for(unsigned y=0; y<ny; y++){
for(unsigned x=0; x<nx; x++){
res[y][x] = zorder2D(x,y);
printf("yx=%d %d z=%d\n",y,x,res[y][x]);
}
}
for(unsigned y=0; y<ny; y++){
for(unsigned x=0; x<nx; x++){
printf("%-4d",res[y][x]);
}
printf("\n");
}
return 0;
}