Skip to content

Commit

Permalink
Use HashCode from Microsoft.Bcl.HashCode
Browse files Browse the repository at this point in the history
  • Loading branch information
Bouke committed Jul 16, 2024

Verified

This commit was signed with the committer’s verified signature.
Bouke Bouke Haarsma
1 parent 09e9928 commit 1abb573
Showing 6 changed files with 3 additions and 12 deletions.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@
<PackageVersion Include="System.Memory" Version="4.5.5" />
<PackageVersion Include="ThisAssembly.Resources" Version="1.4.3" />
<PackageVersion Include="xunit" Version="2.8.1" />
<PackageVersion Include="Microsoft.Bcl.HashCode" Version="1.1.1" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' != 'netcoreapp3.1' ">
1 change: 1 addition & 0 deletions Source/SuperLinq.Async/SuperLinq.Async.csproj
Original file line number Diff line number Diff line change
@@ -125,6 +125,7 @@
</ItemGroup>

<ItemGroup Condition="$(TargetFramework) == 'netstandard2.0'">
<PackageReference Include="Microsoft.Bcl.HashCode" />
<PackageReference Include="System.Memory" PrivateAssets="All" />
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>SuperLinq.Async.Test</_Parameter1>
4 changes: 0 additions & 4 deletions Source/SuperLinq/KeyValuePairEqualityComparer.cs
Original file line number Diff line number Diff line change
@@ -48,12 +48,8 @@ public bool Equals(KeyValuePair<TKey, TValue> x, KeyValuePair<TKey, TValue> y) =
&& _valueComparer.Equals(x.Value, y.Value);

public int GetHashCode(KeyValuePair<TKey, TValue> obj) =>
#if NETCOREAPP
HashCode.Combine(
_keyComparer.GetHashCode(obj.Key!),
_valueComparer.GetHashCode(obj.Value!));
#else
_keyComparer.GetHashCode(obj.Key) * 397 ^ _valueComparer.GetHashCode(obj.Value);
#endif
}
}
1 change: 1 addition & 0 deletions Source/SuperLinq/SuperLinq.csproj
Original file line number Diff line number Diff line change
@@ -164,6 +164,7 @@
</ItemGroup>

<ItemGroup Condition="$(TargetFramework) == 'netstandard2.0'">
<PackageReference Include="Microsoft.Bcl.HashCode" />
<PackageReference Include="System.Memory" PrivateAssets="All" />
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>SuperLinq.Test</_Parameter1>
4 changes: 0 additions & 4 deletions Source/SuperLinq/ValueTupleEqualityComparer.cs
Original file line number Diff line number Diff line change
@@ -87,12 +87,8 @@ public bool Equals(
&& _comparer2.Equals(x.Item2, y.Item2);

public int GetHashCode((T1, T2) obj) =>
#if NETCOREAPP
HashCode.Combine(
_comparer1.GetHashCode(obj.Item1!),
_comparer2.GetHashCode(obj.Item2!));
#else
_comparer1.GetHashCode(obj.Item1) * 397 ^ _comparer2.GetHashCode(obj.Item2);
#endif
}
}
4 changes: 0 additions & 4 deletions Tests/SuperLinq.Test/ValueTupleEqualityComparerTest.cs
Original file line number Diff line number Diff line change
@@ -79,11 +79,7 @@ public void ValueTupleEqualityComparerWithTwoTypeArgsShouldGetHashCode()
var comparer = ValueTupleEqualityComparer.Create<int, int>(comparer1: null, comparer2: null);
ValueTuple<int, int> first = new(1, 2);
var firstHashCode = comparer.GetHashCode(first);
#if NETCOREAPP
var expectedHashCode = HashCode.Combine(1.GetHashCode(), 2.GetHashCode());
#else
var expectedHashCode = 1.GetHashCode() * 397 ^ 2.GetHashCode();
#endif
Assert.Equal(expectedHashCode, firstHashCode);
}
}

0 comments on commit 1abb573

Please sign in to comment.