hash function providing unique uint from an integer coordinate pair

前端 未结 11 1958
隐瞒了意图╮
隐瞒了意图╮ 2020-12-13 10:09

The problem in general: I have a big 2d point space, sparsely populated with dots. Think of it as a big white canvas sprinkled with black dots. I have to it

11条回答
  •  星月不相逢
    2020-12-13 10:38

    If you can do a = ((y & 0xffff) << 16) | (x & 0xffff) then you could afterward apply a reversible 32-bit mix to a, such as Thomas Wang's

    uint32_t hash( uint32_t a)
        a = (a ^ 61) ^ (a >> 16);
        a = a + (a << 3);
        a = a ^ (a >> 4);
        a = a * 0x27d4eb2d;
        a = a ^ (a >> 15);
        return a;
    }
    

    That way you get a random-looking result rather than high bits from one dimension and low bits from the other.

提交回复
热议问题