Can some on recommend what hashing function I could use for two points, (x,y) co ordinates?
Can some on recommend what hashing function I could use for two points, (x,y) co ordinates?
What data type would x and y be?
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
x and y would be ints
I think it depends more on the distribution of points.
For example, if the points are random and fairly sparse then it might be good to just sort by x or y-coordinate. Also if the points tend to fall in a line then you could sort by the axis parallel to that line.
If the points tend to group together or form perpendicular lines, though, that could be rather inefficient. You'd have to try something else, and knowing where the data comes from is probably the best way to know how to hash it well.
You could also represent the coordinates as a long int, specifically:
and use a standard hash function on that.Code:long int coord = ((long int)x << 16) + y;
Consider this post signed
it would be for the game of life, so not sure under which category it falls under
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
In the past, I've used a 64-to-32-bit downmixing function for this. See the section on this page called "64 bit to 32 bit Hash Functions". You pack your two 32-bit coordinates into a single 64-bit long value, pass it to this function, and out comes a 32-bit hash result.
Another possibility would be the 3-way 32-bit mixer on the same page in section "Robert Jenkins' 96 bit Mix Function" -- it takes three inputs. Two of those inputs would be your coordinates, the third input is some random (but fixed) value.
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}