[Mono-list] System.Drawing.Drawing2D

Stefan Maierhofer sm@dhm.at
Mon, 27 Aug 2001 11:20:43 +0200


On Saturday 25 August 2001 01:54, you wrote:
> > > why not something like
> > > public unsafe override int GetHashCode()
> > > {
> > > fixed(float * fp = m) {
> > > int * ip = (int *) fp;
> > > return ip[0] ^ ip[1] ^ ip[2] ^ ip[3];
> > > }
> > > }
> >
> > I think we should avoid unsafe code blocks in the Mono libraries.
>
> though your argument is good i think my way is better, as a lot of real
> matrix and different matrix could return same hashcode with a hash like
> (int)(m0*m1*m2*m3).
> so let me think.....
>
> why not something like this : ? (which is safe)
> -------------------------------------------
> // at the top
> using System.Runtime.InteropServices;
>
> // in your class
>     [StructLayout(LayoutKind.Explicit)]
>     internal struct BitConverter {
> 	[FieldOffset(0)] public float f;
> 	[FieldOffset(0)] public int i;
>     }
>     public override int GetHashCode()
>     {
>         BitConverter b;
>         // compiler is not smart
>         b.i = 0;
>         int h=0;
>         for(int i=0; i<4; i++) {
>             b.f = m[i];
>             h ^= m.i;
> 	}
>         return h;
>     }
>
> _______________________________________________
> Mono-list maillist  -  Mono-list@ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list

I think your implementation will work fine, so I just added it to the Matrix 
class and commited to CVS.

I made one little adjustment:
Your method would return the same hash-code for all translations and 
scalings of form (x,y) where x == y, so I use h ^= b.i >> i instead of h ^= 
b.i which solves the problem.

Stefan.