I have a custom object that we'll call "MyObject" It has three major properties called X,Y, and Z that determine whether it's unique or not. I have a HashSet containing 400,000 "MyObject"s in a HashSet. My original solution for generating a unique hashcode was simple and fast.
return Convert.ToInt32(X * 76 + Y * 100 + Z * 23);
However, the integer generated from this was not unique enough. With the current HashCode, these two points match, even though the Y is slightly different.
X: 392598.200000000190 Y: 4935367.900000000400
X: 392598.200000000190 Y: 4935367.900580000100
What I've tried:
double value = (X * 101 + Y * 89 + Z * 56);
return value.GetHashCode();
- Extremely accurate, with 1 - 10,000 records, it only takes a few seconds to compute the differences. However, with 400,000 records, it bogs down hard. I let it run for 17 hours, and it still hadn't returned my results.
- Converting to a string, then getting hashcode of the string. Precise, but uselessly slow.
Increasing the multipliers of X, Y and Z. The number generated becomes too large. I tried using the method used here: http://msdn.microsoft.com/en-us/library/system.object.gethashcode.aspx
return ((int)value ^ (int)(value >> 32));
However it doesn't allow for anymore integers. I also worry that even if I increase the size, it may becomes uselessly slow like my other solutions.
I can't do additional checks if it matches, since 390,000 of 400,000 records will likely match
What is the best solution? Or is there a way to make my two already precise operations significantly faster? I was thinking about removing all zeros from the values after the decimal place until it meets a non-zero, and then using my original logic, ie (45.0002030 would become 45.2030)