6

我如何访问使用 Z 顺序存储的数据,数组中的时间复杂度为 O(1)?我需要通过坐标快速访问每个元素。我有比使用 while 移位更快的方法来访问这些数据吗?

一种方法是使用查找表(我有静态数据大小)

编辑:

我现在的一个想法是使用 y*SIZE+x 顺序存储叶子

编辑2:

我正在 std::bitset 中的四叉树中记录位。我正在尝试检查是否有一些数据可用。在大小为 128*128 的矩阵中。所以我可以跳过蛮力矩阵搜索空数据。

4

2 回答 2

11

您可以使用以下代码计算 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

于 2013-02-13T12:22:47.557 回答
-1

在https://en.wikipedia.org/wiki/Z-order_curve重现 Wiki 结果

#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;
}
于 2020-09-02T16:09:50.243 回答