improved GetHashCode override of all float and double based Vector/Matrix/Quaternion...
authoramulware <amul@amulware.net>
Wed, 16 Mar 2016 07:11:13 +0000 (08:11 +0100)
committeramulware <amul@amulware.net>
Wed, 16 Mar 2016 07:11:13 +0000 (08:11 +0100)
28 files changed:
Source/OpenTK/Math/Box2.cs
Source/OpenTK/Math/Box2d.cs
Source/OpenTK/Math/Matrix2.cs
Source/OpenTK/Math/Matrix2d.cs
Source/OpenTK/Math/Matrix2x3.cs
Source/OpenTK/Math/Matrix2x3d.cs
Source/OpenTK/Math/Matrix2x4.cs
Source/OpenTK/Math/Matrix2x4d.cs
Source/OpenTK/Math/Matrix3.cs
Source/OpenTK/Math/Matrix3d.cs
Source/OpenTK/Math/Matrix3x2.cs
Source/OpenTK/Math/Matrix3x2d.cs
Source/OpenTK/Math/Matrix3x4.cs
Source/OpenTK/Math/Matrix3x4d.cs
Source/OpenTK/Math/Matrix4.cs
Source/OpenTK/Math/Matrix4d.cs
Source/OpenTK/Math/Matrix4x2.cs
Source/OpenTK/Math/Matrix4x2d.cs
Source/OpenTK/Math/Matrix4x3.cs
Source/OpenTK/Math/Matrix4x3d.cs
Source/OpenTK/Math/Quaternion.cs
Source/OpenTK/Math/Quaterniond.cs
Source/OpenTK/Math/Vector2.cs
Source/OpenTK/Math/Vector2d.cs
Source/OpenTK/Math/Vector3.cs
Source/OpenTK/Math/Vector3d.cs
Source/OpenTK/Math/Vector4.cs
Source/OpenTK/Math/Vector4d.cs

index 418a7c7..bdb6ca5 100644 (file)
@@ -12,7 +12,7 @@ namespace OpenTK
     /// Defines a 2d box (rectangle).
     /// </summary>
     [StructLayout(LayoutKind.Sequential)]
-    public struct Box2
+    public struct Box2 : IEquatable<Box2>
     {
         /// <summary>
         /// The left boundary of the structure.
@@ -190,15 +190,22 @@ namespace OpenTK
             return obj is Box2 && Equals((Box2) obj);
         }
 
-        /// <summary>
-        /// Gets the hash code for this Box2.
-        /// </summary>
-        /// <returns></returns>
+        ///// <summary>
+        ///// Gets the hash code for this Box2.
+        ///// </summary>
         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;
         /// <summary>
         /// Returns a <see cref="System.String"/> describing the current instance.
index 78fe467..d2de82f 100644 (file)
@@ -12,8 +12,9 @@ namespace OpenTK
     /// Defines a 2d box (rectangle).
     /// </summary>
     [StructLayout(LayoutKind.Sequential)]
-    public struct Box2d
+    public struct Box2d : IEquatable<Box2d>
     {
+
         /// <summary>
         /// The left boundary of the structure.
         /// </summary>
@@ -193,10 +194,16 @@ namespace OpenTK
         /// <summary>
         /// Gets the hash code for this Box2d.
         /// </summary>
-        /// <returns></returns>
         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;
index 0a0cb38..5185606 100644 (file)
@@ -721,7 +721,10 @@ namespace OpenTK
         /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
         public override int GetHashCode()
         {
-            return Row0.GetHashCode() ^ Row1.GetHashCode();
+            unchecked
+            {
+                return (this.Row0.GetHashCode() * 397) ^ this.Row1.GetHashCode();
+            }
         }
 
         #endregion
index 999f105..01fbcb2 100644 (file)
@@ -721,7 +721,10 @@ namespace OpenTK
         /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
         public override int GetHashCode()
         {
-            return Row0.GetHashCode() ^ Row1.GetHashCode();
+            unchecked
+            {
+                return (this.Row0.GetHashCode() * 397) ^ this.Row1.GetHashCode();
+            }
         }
 
         #endregion
index bd2236c..6b5f5ea 100644 (file)
@@ -679,7 +679,10 @@ namespace OpenTK
         /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
         public override int GetHashCode()
         {
-            return Row0.GetHashCode() ^ Row1.GetHashCode();
+            unchecked
+            {
+                return (this.Row0.GetHashCode() * 397) ^ this.Row1.GetHashCode();
+            }
         }
 
         #endregion
index 30a59eb..994c75d 100644 (file)
@@ -679,7 +679,10 @@ namespace OpenTK
         /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
         public override int GetHashCode()
         {
-            return Row0.GetHashCode() ^ Row1.GetHashCode();
+            unchecked
+            {
+                return (this.Row0.GetHashCode() * 397) ^ this.Row1.GetHashCode();
+            }
         }
 
         #endregion
index c3afecd..303f95f 100644 (file)
@@ -716,7 +716,10 @@ namespace OpenTK
         /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
         public override int GetHashCode()
         {
-            return Row0.GetHashCode() ^ Row1.GetHashCode();
+            unchecked
+            {
+                return (this.Row0.GetHashCode() * 397) ^ this.Row1.GetHashCode();
+            }
         }
 
         #endregion
index deb2b1c..e01ed5a 100644 (file)
@@ -716,7 +716,10 @@ namespace OpenTK
         /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
         public override int GetHashCode()
         {
-            return Row0.GetHashCode() ^ Row1.GetHashCode();
+            unchecked
+            {
+                return (this.Row0.GetHashCode() * 397) ^ this.Row1.GetHashCode();
+            }
         }
 
         #endregion
index 9d720c8..954eb55 100644 (file)
@@ -960,7 +960,13 @@ namespace OpenTK
         /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
         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
index ca270b8..5c3c1da 100644 (file)
@@ -951,7 +951,13 @@ namespace OpenTK
         /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
         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
index 639f093..dd7b7aa 100644 (file)
@@ -690,7 +690,13 @@ namespace OpenTK
         /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
         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
index 8f1804e..d977471 100644 (file)
@@ -690,7 +690,13 @@ namespace OpenTK
         /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
         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
index 7d251d5..d9c843c 100644 (file)
@@ -954,7 +954,13 @@ namespace OpenTK
         /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
         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
index fa37cfc..44beb2c 100644 (file)
@@ -954,7 +954,13 @@ namespace OpenTK
         /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
         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
index 945f367..d62598c 100644 (file)
@@ -1715,7 +1715,14 @@ namespace OpenTK
         /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
         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
index 8a2a213..db194b3 100644 (file)
@@ -1665,7 +1665,14 @@ namespace OpenTK
         /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
         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
index d548efd..bd61880 100644 (file)
@@ -739,7 +739,14 @@ namespace OpenTK
         /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
         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
index bf494a1..638294b 100644 (file)
@@ -739,7 +739,14 @@ namespace OpenTK
         /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
         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
index e7fd678..792ceda 100644 (file)
@@ -962,7 +962,14 @@ namespace OpenTK
         /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
         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
index 908477f..c74d901 100644 (file)
@@ -962,7 +962,14 @@ namespace OpenTK
         /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
         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
index 8d6b837..9f6f724 100644 (file)
@@ -857,7 +857,10 @@ namespace OpenTK
         /// <returns>A hash code formed from the bitwise XOR of this objects members.</returns>
         public override int GetHashCode()
         {
-            return Xyz.GetHashCode() ^ W.GetHashCode();
+            unchecked
+            {
+                return (this.xyz.GetHashCode() * 397) ^ this.w.GetHashCode();
+            }
         }
 
         #endregion
index be553a2..de3160d 100644 (file)
@@ -854,9 +854,13 @@ namespace OpenTK
         /// Provides the hash code for this object. 
         /// </summary>
         /// <returns>A hash code formed from the bitwise XOR of this objects members.</returns>
+
         public override int GetHashCode()
         {
-            return Xyz.GetHashCode() ^ W.GetHashCode();
+            unchecked
+            {
+                return (this.xyz.GetHashCode() * 397) ^ this.w.GetHashCode();
+            }
         }
 
         #endregion
index 4d1064a..d7f8142 100644 (file)
@@ -1137,7 +1137,10 @@ namespace OpenTK
         /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
         public override int GetHashCode()
         {
-            return X.GetHashCode() ^ Y.GetHashCode();
+            unchecked
+            {
+                return (this.X.GetHashCode() * 397) ^ this.Y.GetHashCode();
+            }
         }
 
         #endregion
index 8c3846c..3993aaf 100644 (file)
@@ -1015,7 +1015,10 @@ namespace OpenTK
         /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
         public override int GetHashCode()
         {
-            return X.GetHashCode() ^ Y.GetHashCode();
+            unchecked
+            {
+                return (this.X.GetHashCode() * 397) ^ this.Y.GetHashCode();
+            }
         }
 
         #endregion
index fa79e39..158767b 100644 (file)
@@ -1629,7 +1629,13 @@ namespace OpenTK
         /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
         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
index fa3dd95..556b9ab 100644 (file)
@@ -1466,7 +1466,13 @@ namespace OpenTK
         /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
         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
index 4b9fdd0..a821031 100644 (file)
@@ -1657,7 +1657,14 @@ namespace OpenTK
         /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
         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
index b158005..facc329 100644 (file)
@@ -1613,7 +1613,14 @@ namespace OpenTK
         /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
         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