From 8e061d8c2c5f160be8c04ceb53b9176a15dcc5c5 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Mon, 9 Sep 2019 16:44:56 -0400 Subject: [PATCH] Replace HashHelpers.Combine with HashCode.Combine in netcoreapp projects (dotnet/corefx#40935) Commit migrated from https://github.com/dotnet/corefx/commit/8c221ca346b64e1e80ab4bde240b0f479bd203fc --- .../src/System.Drawing.Primitives.csproj | 3 --- .../src/System/Drawing/Color.cs | 4 +--- .../src/System/Drawing/Point.cs | 1 - .../src/System/Drawing/PointF.cs | 1 - .../src/System/Drawing/Rectangle.cs | 1 - .../src/System/Drawing/RectangleF.cs | 1 - .../src/System/Drawing/Size.cs | 1 - .../src/System/Drawing/SizeF.cs | 1 - .../System.Memory/src/System.Memory.csproj | 3 --- .../src/System/SequencePosition.cs | 3 +-- .../src/System.Numerics.Vectors.csproj | 3 --- .../src/System/Numerics/Vector2.cs | 5 +---- .../src/System/Numerics/Vector3.cs | 6 +----- .../src/System/Numerics/Vector4.cs | 7 +------ .../tests/GenericVectorTests.cs | 18 +++--------------- .../tests/GenericVectorTests.tt | 18 +++--------------- 16 files changed, 11 insertions(+), 65 deletions(-) diff --git a/src/libraries/System.Drawing.Primitives/src/System.Drawing.Primitives.csproj b/src/libraries/System.Drawing.Primitives/src/System.Drawing.Primitives.csproj index 17511ebd76b72..c9719838eb3c8 100644 --- a/src/libraries/System.Drawing.Primitives/src/System.Drawing.Primitives.csproj +++ b/src/libraries/System.Drawing.Primitives/src/System.Drawing.Primitives.csproj @@ -41,9 +41,6 @@ System\Drawing\KnownColorTable.cs - - Common\System\Numerics\Hashing\HashHelpers.cs - System\Drawing\SystemColors.cs diff --git a/src/libraries/System.Drawing.Primitives/src/System/Drawing/Color.cs b/src/libraries/System.Drawing.Primitives/src/System/Drawing/Color.cs index 21731bd162529..4f702e84fd6fd 100644 --- a/src/libraries/System.Drawing.Primitives/src/System/Drawing/Color.cs +++ b/src/libraries/System.Drawing.Primitives/src/System/Drawing/Color.cs @@ -3,7 +3,6 @@ // See the LICENSE file in the project root for more information. using System.Diagnostics; -using System.Numerics.Hashing; using System.Runtime.CompilerServices; namespace System.Drawing @@ -580,8 +579,7 @@ public override int GetHashCode() if (name != null & !IsKnownColor) return name.GetHashCode(); - return HashHelpers.Combine( - HashHelpers.Combine(value.GetHashCode(), state.GetHashCode()), knownColor.GetHashCode()); + return HashCode.Combine(value.GetHashCode(), state.GetHashCode(), knownColor.GetHashCode()); } } } diff --git a/src/libraries/System.Drawing.Primitives/src/System/Drawing/Point.cs b/src/libraries/System.Drawing.Primitives/src/System/Drawing/Point.cs index 525e52ca7a07d..c8e92c14ab327 100644 --- a/src/libraries/System.Drawing.Primitives/src/System/Drawing/Point.cs +++ b/src/libraries/System.Drawing.Primitives/src/System/Drawing/Point.cs @@ -3,7 +3,6 @@ // See the LICENSE file in the project root for more information. using System.ComponentModel; -using System.Numerics.Hashing; namespace System.Drawing { diff --git a/src/libraries/System.Drawing.Primitives/src/System/Drawing/PointF.cs b/src/libraries/System.Drawing.Primitives/src/System/Drawing/PointF.cs index 9cad5fcb4605e..6d469f4ec61a7 100644 --- a/src/libraries/System.Drawing.Primitives/src/System/Drawing/PointF.cs +++ b/src/libraries/System.Drawing.Primitives/src/System/Drawing/PointF.cs @@ -3,7 +3,6 @@ // See the LICENSE file in the project root for more information. using System.ComponentModel; -using System.Numerics.Hashing; namespace System.Drawing { diff --git a/src/libraries/System.Drawing.Primitives/src/System/Drawing/Rectangle.cs b/src/libraries/System.Drawing.Primitives/src/System/Drawing/Rectangle.cs index adae4d1700bd5..9680fc20c3238 100644 --- a/src/libraries/System.Drawing.Primitives/src/System/Drawing/Rectangle.cs +++ b/src/libraries/System.Drawing.Primitives/src/System/Drawing/Rectangle.cs @@ -3,7 +3,6 @@ // See the LICENSE file in the project root for more information. using System.ComponentModel; -using System.Numerics.Hashing; namespace System.Drawing { diff --git a/src/libraries/System.Drawing.Primitives/src/System/Drawing/RectangleF.cs b/src/libraries/System.Drawing.Primitives/src/System/Drawing/RectangleF.cs index 9844bffea6fb5..e649effa8ab59 100644 --- a/src/libraries/System.Drawing.Primitives/src/System/Drawing/RectangleF.cs +++ b/src/libraries/System.Drawing.Primitives/src/System/Drawing/RectangleF.cs @@ -3,7 +3,6 @@ // See the LICENSE file in the project root for more information. using System.ComponentModel; -using System.Numerics.Hashing; namespace System.Drawing { diff --git a/src/libraries/System.Drawing.Primitives/src/System/Drawing/Size.cs b/src/libraries/System.Drawing.Primitives/src/System/Drawing/Size.cs index e69c62716e7fa..1a315cbd451d8 100644 --- a/src/libraries/System.Drawing.Primitives/src/System/Drawing/Size.cs +++ b/src/libraries/System.Drawing.Primitives/src/System/Drawing/Size.cs @@ -3,7 +3,6 @@ // See the LICENSE file in the project root for more information. using System.ComponentModel; -using System.Numerics.Hashing; namespace System.Drawing { diff --git a/src/libraries/System.Drawing.Primitives/src/System/Drawing/SizeF.cs b/src/libraries/System.Drawing.Primitives/src/System/Drawing/SizeF.cs index ed0699f5f7bbb..5d4cb5eb83072 100644 --- a/src/libraries/System.Drawing.Primitives/src/System/Drawing/SizeF.cs +++ b/src/libraries/System.Drawing.Primitives/src/System/Drawing/SizeF.cs @@ -3,7 +3,6 @@ // See the LICENSE file in the project root for more information. using System.ComponentModel; -using System.Numerics.Hashing; namespace System.Drawing { diff --git a/src/libraries/System.Memory/src/System.Memory.csproj b/src/libraries/System.Memory/src/System.Memory.csproj index d52e5fd1c6a60..1d6654e5ab31e 100644 --- a/src/libraries/System.Memory/src/System.Memory.csproj +++ b/src/libraries/System.Memory/src/System.Memory.csproj @@ -33,9 +33,6 @@ - - Common\System\Collections\HashHelpers.cs - Common\System\Buffers\ArrayBufferWriter.cs diff --git a/src/libraries/System.Memory/src/System/SequencePosition.cs b/src/libraries/System.Memory/src/System/SequencePosition.cs index 6eedc0d7b6607..13aeabe34ec7e 100644 --- a/src/libraries/System.Memory/src/System/SequencePosition.cs +++ b/src/libraries/System.Memory/src/System/SequencePosition.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System.Numerics.Hashing; using System.ComponentModel; namespace System @@ -52,6 +51,6 @@ public SequencePosition(object? @object, int integer) /// [EditorBrowsable(EditorBrowsableState.Never)] - public override int GetHashCode() => HashHelpers.Combine(_object?.GetHashCode() ?? 0, _integer); + public override int GetHashCode() => HashCode.Combine(_object?.GetHashCode() ?? 0, _integer); } } diff --git a/src/libraries/System.Numerics.Vectors/src/System.Numerics.Vectors.csproj b/src/libraries/System.Numerics.Vectors/src/System.Numerics.Vectors.csproj index 6636f75c8dca7..fd74b536fddc2 100644 --- a/src/libraries/System.Numerics.Vectors/src/System.Numerics.Vectors.csproj +++ b/src/libraries/System.Numerics.Vectors/src/System.Numerics.Vectors.csproj @@ -10,9 +10,6 @@ - - Common\System\Numerics\Hashing\HashHelpers.cs - System\Runtime\CompilerServices\IntrinsicAttribute.cs diff --git a/src/libraries/System.Numerics.Vectors/src/System/Numerics/Vector2.cs b/src/libraries/System.Numerics.Vectors/src/System/Numerics/Vector2.cs index 1b6e5240794b2..868dba3b6236a 100644 --- a/src/libraries/System.Numerics.Vectors/src/System/Numerics/Vector2.cs +++ b/src/libraries/System.Numerics.Vectors/src/System/Numerics/Vector2.cs @@ -3,7 +3,6 @@ // See the LICENSE file in the project root for more information. using System.Globalization; -using System.Numerics.Hashing; using System.Runtime.CompilerServices; using System.Text; @@ -54,9 +53,7 @@ public static Vector2 One /// The hash code. public override readonly int GetHashCode() { - int hash = this.X.GetHashCode(); - hash = HashHelpers.Combine(hash, this.Y.GetHashCode()); - return hash; + return HashCode.Combine(this.X.GetHashCode(), this.Y.GetHashCode()); } /// diff --git a/src/libraries/System.Numerics.Vectors/src/System/Numerics/Vector3.cs b/src/libraries/System.Numerics.Vectors/src/System/Numerics/Vector3.cs index 2bc838c01480c..5e1994dce0541 100644 --- a/src/libraries/System.Numerics.Vectors/src/System/Numerics/Vector3.cs +++ b/src/libraries/System.Numerics.Vectors/src/System/Numerics/Vector3.cs @@ -3,7 +3,6 @@ // See the LICENSE file in the project root for more information. using System.Globalization; -using System.Numerics.Hashing; using System.Runtime.CompilerServices; using System.Text; @@ -59,10 +58,7 @@ public static Vector3 One /// The hash code. public override readonly int GetHashCode() { - int hash = this.X.GetHashCode(); - hash = HashHelpers.Combine(hash, this.Y.GetHashCode()); - hash = HashHelpers.Combine(hash, this.Z.GetHashCode()); - return hash; + return HashCode.Combine(this.X.GetHashCode(), this.Y.GetHashCode(), this.Z.GetHashCode()); } /// diff --git a/src/libraries/System.Numerics.Vectors/src/System/Numerics/Vector4.cs b/src/libraries/System.Numerics.Vectors/src/System/Numerics/Vector4.cs index dc19850fca318..4f6b3439cd9a3 100644 --- a/src/libraries/System.Numerics.Vectors/src/System/Numerics/Vector4.cs +++ b/src/libraries/System.Numerics.Vectors/src/System/Numerics/Vector4.cs @@ -3,7 +3,6 @@ // See the LICENSE file in the project root for more information. using System.Globalization; -using System.Numerics.Hashing; using System.Runtime.CompilerServices; using System.Text; @@ -62,11 +61,7 @@ public static Vector4 One /// The hash code. public override readonly int GetHashCode() { - int hash = this.X.GetHashCode(); - hash = HashHelpers.Combine(hash, this.Y.GetHashCode()); - hash = HashHelpers.Combine(hash, this.Z.GetHashCode()); - hash = HashHelpers.Combine(hash, this.W.GetHashCode()); - return hash; + return HashCode.Combine(this.X.GetHashCode(), this.Y.GetHashCode(), this.Z.GetHashCode(), this.W.GetHashCode()); } /// diff --git a/src/libraries/System.Numerics.Vectors/tests/GenericVectorTests.cs b/src/libraries/System.Numerics.Vectors/tests/GenericVectorTests.cs index e1f56f1cb37ad..906fb9efe56aa 100644 --- a/src/libraries/System.Numerics.Vectors/tests/GenericVectorTests.cs +++ b/src/libraries/System.Numerics.Vectors/tests/GenericVectorTests.cs @@ -466,21 +466,9 @@ private void TestEqualsVector() where T : struct public void GetHashCodeDouble() { TestGetHashCode(); } private void TestGetHashCode() where T : struct { - T[] values1 = GenerateRandomValuesForVector(); - Vector v1 = new Vector(values1); - int hash = v1.GetHashCode(); - - int expected = 0; - for (int g = 0; g < Vector.Count; g++) - { - unchecked - { - uint shift5 = ((uint)expected << 5) | ((uint)expected >> 27); - expected = ((int)shift5 + expected) ^ v1[g].GetHashCode(); - } - } - - Assert.Equal(expected, hash); + T[] values = GenerateRandomValuesForVector(); + Vector v = new Vector(values); + Assert.Equal(v.GetHashCode(), v.GetHashCode()); } [Fact] diff --git a/src/libraries/System.Numerics.Vectors/tests/GenericVectorTests.tt b/src/libraries/System.Numerics.Vectors/tests/GenericVectorTests.tt index 0de0b74d1ed98..507763bb5f555 100644 --- a/src/libraries/System.Numerics.Vectors/tests/GenericVectorTests.tt +++ b/src/libraries/System.Numerics.Vectors/tests/GenericVectorTests.tt @@ -328,21 +328,9 @@ namespace System.Numerics.Tests #> private void TestGetHashCode() where T : struct { - T[] values1 = GenerateRandomValuesForVector(); - Vector v1 = new Vector(values1); - int hash = v1.GetHashCode(); - - int expected = 0; - for (int g = 0; g < Vector.Count; g++) - { - unchecked - { - uint shift5 = ((uint)expected << 5) | ((uint)expected >> 27); - expected = ((int)shift5 + expected) ^ v1[g].GetHashCode(); - } - } - - Assert.Equal(expected, hash); + T[] values = GenerateRandomValuesForVector(); + Vector v = new Vector(values); + Assert.Equal(v.GetHashCode(), v.GetHashCode()); } <#