From e4c6f521e2511e7edf28673b33e83b06c59aeee7 Mon Sep 17 00:00:00 2001 From: amulware Date: Wed, 16 Mar 2016 08:11:13 +0100 Subject: [PATCH] improved GetHashCode override of all float and double based Vector/Matrix/Quaternion/Box types --- Source/OpenTK/Math/Box2.cs | 19 +++++++++++++------ Source/OpenTK/Math/Box2d.cs | 13 ++++++++++--- Source/OpenTK/Math/Matrix2.cs | 5 ++++- Source/OpenTK/Math/Matrix2d.cs | 5 ++++- Source/OpenTK/Math/Matrix2x3.cs | 5 ++++- Source/OpenTK/Math/Matrix2x3d.cs | 5 ++++- Source/OpenTK/Math/Matrix2x4.cs | 5 ++++- Source/OpenTK/Math/Matrix2x4d.cs | 5 ++++- Source/OpenTK/Math/Matrix3.cs | 8 +++++++- Source/OpenTK/Math/Matrix3d.cs | 8 +++++++- Source/OpenTK/Math/Matrix3x2.cs | 8 +++++++- Source/OpenTK/Math/Matrix3x2d.cs | 8 +++++++- Source/OpenTK/Math/Matrix3x4.cs | 8 +++++++- Source/OpenTK/Math/Matrix3x4d.cs | 8 +++++++- Source/OpenTK/Math/Matrix4.cs | 9 ++++++++- Source/OpenTK/Math/Matrix4d.cs | 9 ++++++++- Source/OpenTK/Math/Matrix4x2.cs | 9 ++++++++- Source/OpenTK/Math/Matrix4x2d.cs | 9 ++++++++- Source/OpenTK/Math/Matrix4x3.cs | 9 ++++++++- Source/OpenTK/Math/Matrix4x3d.cs | 9 ++++++++- Source/OpenTK/Math/Quaternion.cs | 5 ++++- Source/OpenTK/Math/Quaterniond.cs | 6 +++++- Source/OpenTK/Math/Vector2.cs | 5 ++++- Source/OpenTK/Math/Vector2d.cs | 5 ++++- Source/OpenTK/Math/Vector3.cs | 8 +++++++- Source/OpenTK/Math/Vector3d.cs | 8 +++++++- Source/OpenTK/Math/Vector4.cs | 9 ++++++++- Source/OpenTK/Math/Vector4d.cs | 9 ++++++++- 28 files changed, 184 insertions(+), 35 deletions(-) diff --git a/Source/OpenTK/Math/Box2.cs b/Source/OpenTK/Math/Box2.cs index 418a7c7..bdb6ca5 100644 --- a/Source/OpenTK/Math/Box2.cs +++ b/Source/OpenTK/Math/Box2.cs @@ -12,7 +12,7 @@ namespace OpenTK /// Defines a 2d box (rectangle). /// [StructLayout(LayoutKind.Sequential)] - public struct Box2 + public struct Box2 : IEquatable { /// /// The left boundary of the structure. @@ -190,15 +190,22 @@ namespace OpenTK return obj is Box2 && Equals((Box2) obj); } - /// - /// Gets the hash code for this Box2. - /// - /// + ///// + ///// Gets the hash code for this Box2. + ///// public override int GetHashCode() { - return Left.GetHashCode() ^ Right.GetHashCode() ^ Top.GetHashCode() ^ Bottom.GetHashCode(); + unchecked + { + var hashCode = this.Left.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Right.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Top.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Bottom.GetHashCode(); + return hashCode; + } } + private static string listSeparator = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator; /// /// Returns a describing the current instance. diff --git a/Source/OpenTK/Math/Box2d.cs b/Source/OpenTK/Math/Box2d.cs index 78fe467..d2de82f 100644 --- a/Source/OpenTK/Math/Box2d.cs +++ b/Source/OpenTK/Math/Box2d.cs @@ -12,8 +12,9 @@ namespace OpenTK /// Defines a 2d box (rectangle). /// [StructLayout(LayoutKind.Sequential)] - public struct Box2d + public struct Box2d : IEquatable { + /// /// The left boundary of the structure. /// @@ -193,10 +194,16 @@ namespace OpenTK /// /// Gets the hash code for this Box2d. /// - /// public override int GetHashCode() { - return Left.GetHashCode() ^ Right.GetHashCode() ^ Top.GetHashCode() ^ Bottom.GetHashCode(); + unchecked + { + var hashCode = this.Left.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Right.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Top.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Bottom.GetHashCode(); + return hashCode; + } } private static string listSeparator = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator; diff --git a/Source/OpenTK/Math/Matrix2.cs b/Source/OpenTK/Math/Matrix2.cs index 0a0cb38..5185606 100644 --- a/Source/OpenTK/Math/Matrix2.cs +++ b/Source/OpenTK/Math/Matrix2.cs @@ -721,7 +721,10 @@ namespace OpenTK /// A System.Int32 containing the unique hashcode for this instance. public override int GetHashCode() { - return Row0.GetHashCode() ^ Row1.GetHashCode(); + unchecked + { + return (this.Row0.GetHashCode() * 397) ^ this.Row1.GetHashCode(); + } } #endregion diff --git a/Source/OpenTK/Math/Matrix2d.cs b/Source/OpenTK/Math/Matrix2d.cs index 999f105..01fbcb2 100644 --- a/Source/OpenTK/Math/Matrix2d.cs +++ b/Source/OpenTK/Math/Matrix2d.cs @@ -721,7 +721,10 @@ namespace OpenTK /// A System.Int32 containing the unique hashcode for this instance. public override int GetHashCode() { - return Row0.GetHashCode() ^ Row1.GetHashCode(); + unchecked + { + return (this.Row0.GetHashCode() * 397) ^ this.Row1.GetHashCode(); + } } #endregion diff --git a/Source/OpenTK/Math/Matrix2x3.cs b/Source/OpenTK/Math/Matrix2x3.cs index bd2236c..6b5f5ea 100644 --- a/Source/OpenTK/Math/Matrix2x3.cs +++ b/Source/OpenTK/Math/Matrix2x3.cs @@ -679,7 +679,10 @@ namespace OpenTK /// A System.Int32 containing the unique hashcode for this instance. public override int GetHashCode() { - return Row0.GetHashCode() ^ Row1.GetHashCode(); + unchecked + { + return (this.Row0.GetHashCode() * 397) ^ this.Row1.GetHashCode(); + } } #endregion diff --git a/Source/OpenTK/Math/Matrix2x3d.cs b/Source/OpenTK/Math/Matrix2x3d.cs index 30a59eb..994c75d 100644 --- a/Source/OpenTK/Math/Matrix2x3d.cs +++ b/Source/OpenTK/Math/Matrix2x3d.cs @@ -679,7 +679,10 @@ namespace OpenTK /// A System.Int32 containing the unique hashcode for this instance. public override int GetHashCode() { - return Row0.GetHashCode() ^ Row1.GetHashCode(); + unchecked + { + return (this.Row0.GetHashCode() * 397) ^ this.Row1.GetHashCode(); + } } #endregion diff --git a/Source/OpenTK/Math/Matrix2x4.cs b/Source/OpenTK/Math/Matrix2x4.cs index c3afecd..303f95f 100644 --- a/Source/OpenTK/Math/Matrix2x4.cs +++ b/Source/OpenTK/Math/Matrix2x4.cs @@ -716,7 +716,10 @@ namespace OpenTK /// A System.Int32 containing the unique hashcode for this instance. public override int GetHashCode() { - return Row0.GetHashCode() ^ Row1.GetHashCode(); + unchecked + { + return (this.Row0.GetHashCode() * 397) ^ this.Row1.GetHashCode(); + } } #endregion diff --git a/Source/OpenTK/Math/Matrix2x4d.cs b/Source/OpenTK/Math/Matrix2x4d.cs index deb2b1c..e01ed5a 100644 --- a/Source/OpenTK/Math/Matrix2x4d.cs +++ b/Source/OpenTK/Math/Matrix2x4d.cs @@ -716,7 +716,10 @@ namespace OpenTK /// A System.Int32 containing the unique hashcode for this instance. public override int GetHashCode() { - return Row0.GetHashCode() ^ Row1.GetHashCode(); + unchecked + { + return (this.Row0.GetHashCode() * 397) ^ this.Row1.GetHashCode(); + } } #endregion diff --git a/Source/OpenTK/Math/Matrix3.cs b/Source/OpenTK/Math/Matrix3.cs index 9d720c8..954eb55 100644 --- a/Source/OpenTK/Math/Matrix3.cs +++ b/Source/OpenTK/Math/Matrix3.cs @@ -960,7 +960,13 @@ namespace OpenTK /// A System.Int32 containing the unique hashcode for this instance. public override int GetHashCode() { - return Row0.GetHashCode() ^ Row1.GetHashCode() ^ Row2.GetHashCode(); + unchecked + { + var hashCode = this.Row0.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Row1.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Row2.GetHashCode(); + return hashCode; + } } #endregion diff --git a/Source/OpenTK/Math/Matrix3d.cs b/Source/OpenTK/Math/Matrix3d.cs index ca270b8..5c3c1da 100644 --- a/Source/OpenTK/Math/Matrix3d.cs +++ b/Source/OpenTK/Math/Matrix3d.cs @@ -951,7 +951,13 @@ namespace OpenTK /// A System.Int32 containing the unique hashcode for this instance. public override int GetHashCode() { - return Row0.GetHashCode() ^ Row1.GetHashCode() ^ Row2.GetHashCode(); + unchecked + { + var hashCode = this.Row0.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Row1.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Row2.GetHashCode(); + return hashCode; + } } #endregion diff --git a/Source/OpenTK/Math/Matrix3x2.cs b/Source/OpenTK/Math/Matrix3x2.cs index 639f093..dd7b7aa 100644 --- a/Source/OpenTK/Math/Matrix3x2.cs +++ b/Source/OpenTK/Math/Matrix3x2.cs @@ -690,7 +690,13 @@ namespace OpenTK /// A System.Int32 containing the unique hashcode for this instance. public override int GetHashCode() { - return Row0.GetHashCode() ^ Row1.GetHashCode() ^ Row2.GetHashCode(); + unchecked + { + var hashCode = this.Row0.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Row1.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Row2.GetHashCode(); + return hashCode; + } } #endregion diff --git a/Source/OpenTK/Math/Matrix3x2d.cs b/Source/OpenTK/Math/Matrix3x2d.cs index 8f1804e..d977471 100644 --- a/Source/OpenTK/Math/Matrix3x2d.cs +++ b/Source/OpenTK/Math/Matrix3x2d.cs @@ -690,7 +690,13 @@ namespace OpenTK /// A System.Int32 containing the unique hashcode for this instance. public override int GetHashCode() { - return Row0.GetHashCode() ^ Row1.GetHashCode() ^ Row2.GetHashCode(); + unchecked + { + var hashCode = this.Row0.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Row1.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Row2.GetHashCode(); + return hashCode; + } } #endregion diff --git a/Source/OpenTK/Math/Matrix3x4.cs b/Source/OpenTK/Math/Matrix3x4.cs index 7d251d5..d9c843c 100644 --- a/Source/OpenTK/Math/Matrix3x4.cs +++ b/Source/OpenTK/Math/Matrix3x4.cs @@ -954,7 +954,13 @@ namespace OpenTK /// A System.Int32 containing the unique hashcode for this instance. public override int GetHashCode() { - return Row0.GetHashCode() ^ Row1.GetHashCode() ^ Row2.GetHashCode(); + unchecked + { + var hashCode = this.Row0.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Row1.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Row2.GetHashCode(); + return hashCode; + } } #endregion diff --git a/Source/OpenTK/Math/Matrix3x4d.cs b/Source/OpenTK/Math/Matrix3x4d.cs index fa37cfc..44beb2c 100644 --- a/Source/OpenTK/Math/Matrix3x4d.cs +++ b/Source/OpenTK/Math/Matrix3x4d.cs @@ -954,7 +954,13 @@ namespace OpenTK /// A System.Int32 containing the unique hashcode for this instance. public override int GetHashCode() { - return Row0.GetHashCode() ^ Row1.GetHashCode() ^ Row2.GetHashCode(); + unchecked + { + var hashCode = this.Row0.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Row1.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Row2.GetHashCode(); + return hashCode; + } } #endregion diff --git a/Source/OpenTK/Math/Matrix4.cs b/Source/OpenTK/Math/Matrix4.cs index 945f367..d62598c 100644 --- a/Source/OpenTK/Math/Matrix4.cs +++ b/Source/OpenTK/Math/Matrix4.cs @@ -1715,7 +1715,14 @@ namespace OpenTK /// A System.Int32 containing the unique hashcode for this instance. public override int GetHashCode() { - return Row0.GetHashCode() ^ Row1.GetHashCode() ^ Row2.GetHashCode() ^ Row3.GetHashCode(); + unchecked + { + var hashCode = this.Row0.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Row1.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Row2.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Row3.GetHashCode(); + return hashCode; + } } #endregion diff --git a/Source/OpenTK/Math/Matrix4d.cs b/Source/OpenTK/Math/Matrix4d.cs index 8a2a213..db194b3 100644 --- a/Source/OpenTK/Math/Matrix4d.cs +++ b/Source/OpenTK/Math/Matrix4d.cs @@ -1665,7 +1665,14 @@ namespace OpenTK /// A System.Int32 containing the unique hashcode for this instance. public override int GetHashCode() { - return Row0.GetHashCode() ^ Row1.GetHashCode() ^ Row2.GetHashCode() ^ Row3.GetHashCode(); + unchecked + { + var hashCode = this.Row0.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Row1.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Row2.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Row3.GetHashCode(); + return hashCode; + } } #endregion diff --git a/Source/OpenTK/Math/Matrix4x2.cs b/Source/OpenTK/Math/Matrix4x2.cs index d548efd..bd61880 100644 --- a/Source/OpenTK/Math/Matrix4x2.cs +++ b/Source/OpenTK/Math/Matrix4x2.cs @@ -739,7 +739,14 @@ namespace OpenTK /// A System.Int32 containing the unique hashcode for this instance. public override int GetHashCode() { - return Row0.GetHashCode() ^ Row1.GetHashCode() ^ Row2.GetHashCode() ^ Row3.GetHashCode(); + unchecked + { + var hashCode = this.Row0.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Row1.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Row2.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Row3.GetHashCode(); + return hashCode; + } } #endregion diff --git a/Source/OpenTK/Math/Matrix4x2d.cs b/Source/OpenTK/Math/Matrix4x2d.cs index bf494a1..638294b 100644 --- a/Source/OpenTK/Math/Matrix4x2d.cs +++ b/Source/OpenTK/Math/Matrix4x2d.cs @@ -739,7 +739,14 @@ namespace OpenTK /// A System.Int32 containing the unique hashcode for this instance. public override int GetHashCode() { - return Row0.GetHashCode() ^ Row1.GetHashCode() ^ Row2.GetHashCode() ^ Row3.GetHashCode(); + unchecked + { + var hashCode = this.Row0.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Row1.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Row2.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Row3.GetHashCode(); + return hashCode; + } } #endregion diff --git a/Source/OpenTK/Math/Matrix4x3.cs b/Source/OpenTK/Math/Matrix4x3.cs index e7fd678..792ceda 100644 --- a/Source/OpenTK/Math/Matrix4x3.cs +++ b/Source/OpenTK/Math/Matrix4x3.cs @@ -962,7 +962,14 @@ namespace OpenTK /// A System.Int32 containing the unique hashcode for this instance. public override int GetHashCode() { - return Row0.GetHashCode() ^ Row1.GetHashCode() ^ Row2.GetHashCode(); + unchecked + { + var hashCode = this.Row0.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Row1.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Row2.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Row3.GetHashCode(); + return hashCode; + } } #endregion diff --git a/Source/OpenTK/Math/Matrix4x3d.cs b/Source/OpenTK/Math/Matrix4x3d.cs index 908477f..c74d901 100644 --- a/Source/OpenTK/Math/Matrix4x3d.cs +++ b/Source/OpenTK/Math/Matrix4x3d.cs @@ -962,7 +962,14 @@ namespace OpenTK /// A System.Int32 containing the unique hashcode for this instance. public override int GetHashCode() { - return Row0.GetHashCode() ^ Row1.GetHashCode() ^ Row2.GetHashCode(); + unchecked + { + var hashCode = this.Row0.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Row1.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Row2.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Row3.GetHashCode(); + return hashCode; + } } #endregion diff --git a/Source/OpenTK/Math/Quaternion.cs b/Source/OpenTK/Math/Quaternion.cs index 8d6b837..9f6f724 100644 --- a/Source/OpenTK/Math/Quaternion.cs +++ b/Source/OpenTK/Math/Quaternion.cs @@ -857,7 +857,10 @@ namespace OpenTK /// A hash code formed from the bitwise XOR of this objects members. public override int GetHashCode() { - return Xyz.GetHashCode() ^ W.GetHashCode(); + unchecked + { + return (this.xyz.GetHashCode() * 397) ^ this.w.GetHashCode(); + } } #endregion diff --git a/Source/OpenTK/Math/Quaterniond.cs b/Source/OpenTK/Math/Quaterniond.cs index be553a2..de3160d 100644 --- a/Source/OpenTK/Math/Quaterniond.cs +++ b/Source/OpenTK/Math/Quaterniond.cs @@ -854,9 +854,13 @@ namespace OpenTK /// Provides the hash code for this object. /// /// A hash code formed from the bitwise XOR of this objects members. + public override int GetHashCode() { - return Xyz.GetHashCode() ^ W.GetHashCode(); + unchecked + { + return (this.xyz.GetHashCode() * 397) ^ this.w.GetHashCode(); + } } #endregion diff --git a/Source/OpenTK/Math/Vector2.cs b/Source/OpenTK/Math/Vector2.cs index 4d1064a..d7f8142 100644 --- a/Source/OpenTK/Math/Vector2.cs +++ b/Source/OpenTK/Math/Vector2.cs @@ -1137,7 +1137,10 @@ namespace OpenTK /// A System.Int32 containing the unique hashcode for this instance. public override int GetHashCode() { - return X.GetHashCode() ^ Y.GetHashCode(); + unchecked + { + return (this.X.GetHashCode() * 397) ^ this.Y.GetHashCode(); + } } #endregion diff --git a/Source/OpenTK/Math/Vector2d.cs b/Source/OpenTK/Math/Vector2d.cs index 8c3846c..3993aaf 100644 --- a/Source/OpenTK/Math/Vector2d.cs +++ b/Source/OpenTK/Math/Vector2d.cs @@ -1015,7 +1015,10 @@ namespace OpenTK /// A System.Int32 containing the unique hashcode for this instance. public override int GetHashCode() { - return X.GetHashCode() ^ Y.GetHashCode(); + unchecked + { + return (this.X.GetHashCode() * 397) ^ this.Y.GetHashCode(); + } } #endregion diff --git a/Source/OpenTK/Math/Vector3.cs b/Source/OpenTK/Math/Vector3.cs index fa79e39..158767b 100644 --- a/Source/OpenTK/Math/Vector3.cs +++ b/Source/OpenTK/Math/Vector3.cs @@ -1629,7 +1629,13 @@ namespace OpenTK /// A System.Int32 containing the unique hashcode for this instance. public override int GetHashCode() { - return X.GetHashCode() ^ Y.GetHashCode() ^ Z.GetHashCode(); + unchecked + { + var hashCode = this.X.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Y.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Z.GetHashCode(); + return hashCode; + } } #endregion diff --git a/Source/OpenTK/Math/Vector3d.cs b/Source/OpenTK/Math/Vector3d.cs index fa3dd95..556b9ab 100644 --- a/Source/OpenTK/Math/Vector3d.cs +++ b/Source/OpenTK/Math/Vector3d.cs @@ -1466,7 +1466,13 @@ namespace OpenTK /// A System.Int32 containing the unique hashcode for this instance. public override int GetHashCode() { - return X.GetHashCode() ^ Y.GetHashCode() ^ Z.GetHashCode(); + unchecked + { + var hashCode = this.X.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Y.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Z.GetHashCode(); + return hashCode; + } } #endregion diff --git a/Source/OpenTK/Math/Vector4.cs b/Source/OpenTK/Math/Vector4.cs index 4b9fdd0..a821031 100644 --- a/Source/OpenTK/Math/Vector4.cs +++ b/Source/OpenTK/Math/Vector4.cs @@ -1657,7 +1657,14 @@ namespace OpenTK /// A System.Int32 containing the unique hashcode for this instance. public override int GetHashCode() { - return X.GetHashCode() ^ Y.GetHashCode() ^ Z.GetHashCode() ^ W.GetHashCode(); + unchecked + { + var hashCode = this.X.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Y.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Z.GetHashCode(); + hashCode = (hashCode * 397) ^ this.W.GetHashCode(); + return hashCode; + } } #endregion diff --git a/Source/OpenTK/Math/Vector4d.cs b/Source/OpenTK/Math/Vector4d.cs index b158005..facc329 100644 --- a/Source/OpenTK/Math/Vector4d.cs +++ b/Source/OpenTK/Math/Vector4d.cs @@ -1613,7 +1613,14 @@ namespace OpenTK /// A System.Int32 containing the unique hashcode for this instance. public override int GetHashCode() { - return X.GetHashCode() ^ Y.GetHashCode() ^ Z.GetHashCode() ^ W.GetHashCode(); + unchecked + { + var hashCode = this.X.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Y.GetHashCode(); + hashCode = (hashCode * 397) ^ this.Z.GetHashCode(); + hashCode = (hashCode * 397) ^ this.W.GetHashCode(); + return hashCode; + } } #endregion -- 2.7.4