How could i access data which is being stored using Z-order with O(1) time complexity in array? I need fast access to each of element by their coordinates. I there any faste
You can calculate the z order curve value with the following code:
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;
}
It was taken from here Bit Twiddling Hacks
From you 128x128 array (or any other size) you can calculate easily the z order curve value from any position. For example:
xPos = 2, yPos = 3 -> z order curve value = 7
The max array size for the example code is 65536*65536. Just use a power of 2 for ease, in that case the maximum wasted space is approx. 3/4
Reproduce Wiki results at 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;
}