System.Drawing.Point
has a really, really bad GetHashCode
method if you intend to use it to describes 'pixels' in a Image/Bitmap: it is just XOR between the X and Y coordinates.
So for a image with, say, 2000x2000 size, it has an absurd number of colisions, since only the numbers in the diagonal are going to have a decent hash.
It's quite easy to create a decent GetHashCode
method using unchecked multiplication, as some people already mentioned here.
But what can I do to use this improved GetHashCode
method in a HashSet
?
I know I could create my own class/struct MyPoint
and implement it using this improved methods, but then I'd break all other pieces of code in my project that use a System.Drawing.Point
.
Is it possible to "overwrite" the method from System.Drawing.Point
using some sort of extension method or the like? Or to "tell" the HashSet
to use another function instead of the GetHashCode
?
Currently I'm using a SortedSet<System.Drawing.Point>
with a custom IComparer<Point>
to store my points. When I want to know if the set contains a Point I call BinarySearch
. It's faster than a HashSet<System.Drawing.Point>.Contains
method in a set with 10000 colisions, but it's no as fast as HashSet
with a good hash could be.