1

For a 2D point, it could be:- x_value * 10^((int)log_10(max_y_value)) + y_value

What would it be for a Point in N Dimensional space?

Thanks!

5
  • 3
    What do you mean by "uniquely"? You do know that hash(codes) aren't really supposed to be unique? Nov 12, 2011 at 21:56
  • It has to be unique. To be precise, I want a 1-1 mapping between a N-D point and a discrete integer space i.e (long data type). The goal is to fit as many points as possible.
    – user855
    Nov 12, 2011 at 22:05
  • 1
    I don't think you're grasping the point here. It is impossible to reduce a N-component X-bitsized point to a Y-bitsized "hash" without getting collisions, where Y < N*X. If you have a 3-dimensional point, where each dimensional value is 32-bit, you have 96 bits. If you want to reduce that to a 64-bit hashcode, you're going to get collisions. Basically, it cannot be considered unique Nov 12, 2011 at 22:09
  • 2
    The only way to get a true 1-1 mapping is to ensure the size of the hashcode is equal to or larger than the size of whatever it is you're calculating the hashcode of, and then just reinterpreting the bytes in memory from your structure into the hashcode. Nov 12, 2011 at 22:11
  • @LasseV.Karlsen In which case it is not a hash. Nov 14, 2011 at 2:40

3 Answers 3

2

The typical way to calculate hashcodes of structures is that you pick 2 prime numbers, relatively small, such as 23 and 37, let's call those P1 and P2, and then you calculate the hashcode of a multi-component structure as follows:

hashcode = P1
foreach component in structure
    hashcode = hashcode * P2
    hashcode = hashcode + hashcode-of-component

The "hashcode-of-component" is either the component value itself, if it is an integer or something that can be reinterpreted as an integer (meaning: memory bytes converted to an integer), or the hashcode of that structure.

In the case of a point in N dimensional space:

hashcode = P1
count from 1 to N
    hashcode = hashcode * P2
    hashcode = hashcode + component[counting-index]

assuming that each dimensional value of the point can be reinterpreted into an integer

1

Reposting my answer from here:

There's a spatial hash function described in Optimized Spatial Hashing for Collision Detection of Deformable Objects. They use the hash function

hash(x,y,z) = ( x p1 xor y p2 xor z p3) mod n

where p1, p2, p3 are large prime numbers, in our case 73856093, 19349663, 83492791, respectively. The value n is the hash table size.

In the paper, x, y, and z are the discretized coordinates; you could probably also use the binary values of your floats.

0

Assuming for a moment what you actually want is a single unique value for the 3-dimensional point, not a hash, what you probably want is a 3-dimensional hilbert curve. A Hilbert Curve lets you translate between n-dimensional points and points on a line in a way that generally does a good job of preserving locality.

Of course, exactly what the best option is depends on your needs; a simpler option would be to represent each coordinate as an n-bit number, and the composite value as the 3n-bit concatenation of the three numbers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.