I would like to implement some geometrical algorithms with numerical robustness.
For this, a system-wide delta
is used for geometric equality. equals()
for points is implemented with a distance computation using the delta
for approximate equality.
I would like to be able to use regular java Collections, e.g Set
. But I can't come up with a reasonable hashCode()
implementation.
My guess is that an implementation for an efficient HashSet
use would result in a space partitioning with "soft" borders. Points with distance less that delta
to the partition border should be able to be classified in up to eight (for 3D) adjacent regions at the same time. Points close enough to be considered equal on behalf of their distance but lying on different sides of the partition would otherwise be "misclassified".
This is what I can't get my head around. hashCode()
is like putting items in buckets with a single item ending up in a single bucket, while I would need to put it in up to eight.
What would be a resonable solution? Am I abusing the purpose of hashCode()
? And what would be the most reasonable solution still using hashCode()
:)
EDIT: Thank you, I had an intuition that there was something wrong with the idea but could not put my finger on it. You made the matter very clear
Please let me extend my question to : if I'm fine with slower HashSet
operation (which is not a showstopper), I could have hashCode()
return 1
as there is no correct implementation in my case, what dire consequences would there be (it terms of geometric computations), if I do implement equals()
dropping the transitivity requirement?
EDIT I have found this post, highlighting the issues with missing transitivity and and this post, which is closely related to this one.