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
1 parent 22a97ef commit dbec67e
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 18 deletions.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<PackageVersion Include="coverlet.msbuild" Version="6.0.2" />
<PackageVersion Include="DocFx.App" Version="2.77.0" />
<PackageVersion Include="GitHubActionsTestLogger" Version="2.4.1" />
<PackageVersion Include="Microsoft.Bcl.HashCode" Version="1.1.1" />
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.10.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="3.3.4" />
Expand Down
13 changes: 7 additions & 6 deletions Source/SuperLinq.Async/SuperLinq.Async.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,13 @@
<ProjectReference Include="..\SuperLinq\SuperLinq.csproj" />
</ItemGroup>

<ItemGroup Condition="$(TargetFramework) == 'netstandard2.0'">
<PackageReference Include="System.Memory" PrivateAssets="All" />
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>SuperLinq.Async.Test</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
<ItemGroup Condition="$(TargetFramework) == 'netstandard2.0'">
<PackageReference Include="Microsoft.Bcl.HashCode" />
<PackageReference Include="System.Memory" />
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>SuperLinq.Async.Test</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

<PropertyGroup Label="SourceGenerator">
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
Expand Down
4 changes: 0 additions & 4 deletions Source/SuperLinq/KeyValuePairEqualityComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
4 changes: 0 additions & 4 deletions Source/SuperLinq/ValueTupleEqualityComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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 dbec67e

Please sign in to comment.