From 66619ad7d8587555acccf5c5d083446342456429 Mon Sep 17 00:00:00 2001 From: Roman Marusyk Date: Wed, 12 Aug 2020 15:45:15 +0300 Subject: [PATCH] Improve System.Numerics.Matrix3x2 and Matrix4x4 hash codes (#34452) * Improve System.Numerics.Matrix3x2 and Matrix4x4 hash codes * Code review fixes * Code review fixes --- .../tests/Matrix3x2Tests.cs | 6 ++-- .../tests/Matrix4x4Tests.cs | 34 +++++++++++++++++----- .../src/System/Numerics/Matrix3x2.cs | 4 +-- .../src/System/Numerics/Matrix4x4.cs | 30 ++++++++++++++----- .../src/System/Numerics/Vector2.cs | 2 +- .../src/System/Numerics/Vector3.cs | 2 +- .../src/System/Numerics/Vector4.cs | 2 +- 7 files changed, 56 insertions(+), 24 deletions(-) diff --git a/src/libraries/System.Numerics.Vectors/tests/Matrix3x2Tests.cs b/src/libraries/System.Numerics.Vectors/tests/Matrix3x2Tests.cs index b1c626a..9d5f2dc 100644 --- a/src/libraries/System.Numerics.Vectors/tests/Matrix3x2Tests.cs +++ b/src/libraries/System.Numerics.Vectors/tests/Matrix3x2Tests.cs @@ -527,9 +527,9 @@ namespace System.Numerics.Tests public void Matrix3x2GetHashCodeTest() { Matrix3x2 target = GenerateIncrementalMatrixNumber(); - int expected = unchecked(target.M11.GetHashCode() + target.M12.GetHashCode() + - target.M21.GetHashCode() + target.M22.GetHashCode() + - target.M31.GetHashCode() + target.M32.GetHashCode()); + int expected = HashCode.Combine(target.M11, target.M12, + target.M21, target.M22, + target.M31, target.M32); int actual; actual = target.GetHashCode(); diff --git a/src/libraries/System.Numerics.Vectors/tests/Matrix4x4Tests.cs b/src/libraries/System.Numerics.Vectors/tests/Matrix4x4Tests.cs index 1cb2575..2a0c7d8 100644 --- a/src/libraries/System.Numerics.Vectors/tests/Matrix4x4Tests.cs +++ b/src/libraries/System.Numerics.Vectors/tests/Matrix4x4Tests.cs @@ -1609,14 +1609,32 @@ namespace System.Numerics.Tests public void Matrix4x4GetHashCodeTest() { Matrix4x4 target = GenerateIncrementalMatrixNumber(); - int expected = unchecked( - target.M11.GetHashCode() + target.M12.GetHashCode() + target.M13.GetHashCode() + target.M14.GetHashCode() + - target.M21.GetHashCode() + target.M22.GetHashCode() + target.M23.GetHashCode() + target.M24.GetHashCode() + - target.M31.GetHashCode() + target.M32.GetHashCode() + target.M33.GetHashCode() + target.M34.GetHashCode() + - target.M41.GetHashCode() + target.M42.GetHashCode() + target.M43.GetHashCode() + target.M44.GetHashCode()); - int actual; - - actual = target.GetHashCode(); + + HashCode hash = default; + + hash.Add(target.M11); + hash.Add(target.M12); + hash.Add(target.M13); + hash.Add(target.M14); + + hash.Add(target.M21); + hash.Add(target.M22); + hash.Add(target.M23); + hash.Add(target.M24); + + hash.Add(target.M31); + hash.Add(target.M32); + hash.Add(target.M33); + hash.Add(target.M34); + + hash.Add(target.M41); + hash.Add(target.M42); + hash.Add(target.M43); + hash.Add(target.M44); + + int expected = hash.ToHashCode(); + int actual = target.GetHashCode(); + Assert.Equal(expected, actual); } diff --git a/src/libraries/System.Private.CoreLib/src/System/Numerics/Matrix3x2.cs b/src/libraries/System.Private.CoreLib/src/System/Numerics/Matrix3x2.cs index fce8ec1..5f65ddd 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Numerics/Matrix3x2.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Numerics/Matrix3x2.cs @@ -762,9 +762,7 @@ namespace System.Numerics /// The hash code. public override readonly int GetHashCode() { - return unchecked(M11.GetHashCode() + M12.GetHashCode() + - M21.GetHashCode() + M22.GetHashCode() + - M31.GetHashCode() + M32.GetHashCode()); + return HashCode.Combine(M11, M12, M21, M22, M31, M32); } } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Numerics/Matrix4x4.cs b/src/libraries/System.Private.CoreLib/src/System/Numerics/Matrix4x4.cs index c357347..d0099515 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Numerics/Matrix4x4.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Numerics/Matrix4x4.cs @@ -2320,13 +2320,29 @@ namespace System.Numerics /// The hash code. public override readonly int GetHashCode() { - unchecked - { - return M11.GetHashCode() + M12.GetHashCode() + M13.GetHashCode() + M14.GetHashCode() + - M21.GetHashCode() + M22.GetHashCode() + M23.GetHashCode() + M24.GetHashCode() + - M31.GetHashCode() + M32.GetHashCode() + M33.GetHashCode() + M34.GetHashCode() + - M41.GetHashCode() + M42.GetHashCode() + M43.GetHashCode() + M44.GetHashCode(); - } + HashCode hash = default; + + hash.Add(M11); + hash.Add(M12); + hash.Add(M13); + hash.Add(M14); + + hash.Add(M21); + hash.Add(M22); + hash.Add(M23); + hash.Add(M24); + + hash.Add(M31); + hash.Add(M32); + hash.Add(M33); + hash.Add(M34); + + hash.Add(M41); + hash.Add(M42); + hash.Add(M43); + hash.Add(M44); + + return hash.ToHashCode(); } } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Numerics/Vector2.cs b/src/libraries/System.Private.CoreLib/src/System/Numerics/Vector2.cs index b6049a3..7b03544 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Numerics/Vector2.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Numerics/Vector2.cs @@ -53,7 +53,7 @@ namespace System.Numerics /// The hash code. public override readonly int GetHashCode() { - return HashCode.Combine(this.X.GetHashCode(), this.Y.GetHashCode()); + return HashCode.Combine(this.X, this.Y); } /// diff --git a/src/libraries/System.Private.CoreLib/src/System/Numerics/Vector3.cs b/src/libraries/System.Private.CoreLib/src/System/Numerics/Vector3.cs index b64f83b..b156f42 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Numerics/Vector3.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Numerics/Vector3.cs @@ -58,7 +58,7 @@ namespace System.Numerics /// The hash code. public override readonly int GetHashCode() { - return HashCode.Combine(this.X.GetHashCode(), this.Y.GetHashCode(), this.Z.GetHashCode()); + return HashCode.Combine(this.X, this.Y, this.Z); } /// diff --git a/src/libraries/System.Private.CoreLib/src/System/Numerics/Vector4.cs b/src/libraries/System.Private.CoreLib/src/System/Numerics/Vector4.cs index 8b2bf9a..a34c8b0 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Numerics/Vector4.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Numerics/Vector4.cs @@ -61,7 +61,7 @@ namespace System.Numerics /// The hash code. public override readonly int GetHashCode() { - return HashCode.Combine(this.X.GetHashCode(), this.Y.GetHashCode(), this.Z.GetHashCode(), this.W.GetHashCode()); + return HashCode.Combine(this.X, this.Y, this.Z, this.W); } /// -- 2.7.4