Streamline equality comparison of generation models
このコミットが含まれているのは:
1 親
6e247639ed
コミット
6b0c1161c8
19ファイル変更
+249
-607
検索条件に一致するファイルはありません
@@ -15,6 +15,7 @@
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Extensions\ImmutableArrayExtensions.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Extensions\IncrementalValuesProviderExtensions.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Extensions\ISymbolExtensions.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Helpers\Comparer{T,TSelf}.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Helpers\HashCode.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Helpers\SemanticModelProvider.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Models\DiagnosticInfo.cs" />
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// 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;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ComputeSharp.SourceGeneration.Helpers;
|
||||
|
||||
/// <summary>
|
||||
/// A base <see cref="IEqualityComparer{T}"/> implementation for <typeparamref name="T"/> instances.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to compare.</typeparam>
|
||||
/// <typeparam name="TSelf">The concrete comparer type.</typeparam>
|
||||
internal abstract class Comparer<T, TSelf> : IEqualityComparer<T>
|
||||
where TSelf : Comparer<T, TSelf>, new()
|
||||
{
|
||||
/// <summary>
|
||||
/// The singleton <typeparamref name="TSelf"/> instance.
|
||||
/// </summary>
|
||||
public static TSelf Default { get; } = new();
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool Equals(T? x, T? y)
|
||||
{
|
||||
if (x is null && y is null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (x is null || y is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ReferenceEquals(x, y))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return AreEqual(x, y);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int GetHashCode(T obj)
|
||||
{
|
||||
HashCode hashCode = default;
|
||||
|
||||
AddToHashCode(ref hashCode, obj);
|
||||
|
||||
return hashCode.ToHashCode();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the current instance to an incremental <see cref="HashCode"/> value.
|
||||
/// </summary>
|
||||
/// <param name="hashCode">The target <see cref="HashCode"/> value.</param>
|
||||
/// <param name="obj">The <typeparamref name="T"/> instance being inspected.</param>
|
||||
protected abstract void AddToHashCode(ref HashCode hashCode, T obj);
|
||||
|
||||
/// <summary>
|
||||
/// Compares two <typeparamref name="T"/> instances for equality.
|
||||
/// </summary>
|
||||
/// <param name="x">The first <typeparamref name="T"/> instance to compare.</param>
|
||||
/// <param name="y">The second <typeparamref name="T"/> instance to compare.</param>
|
||||
/// <returns>Whether or not <paramref name="x"/> and <paramref name="y"/> are equal.</returns>
|
||||
protected abstract bool AreEqual(T x, T y);
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using ComputeSharp.SourceGeneration.Helpers;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace ComputeSharp.SourceGeneration.Models;
|
||||
@@ -15,49 +16,25 @@ internal sealed record DiagnosticInfo(DiagnosticDescriptor Descriptor, params ob
|
||||
/// <summary>
|
||||
/// An <see cref="IEqualityComparer{T}"/> implementation for <see cref="DiagnosticInfo"/>.
|
||||
/// </summary>
|
||||
public sealed class Comparer : IEqualityComparer<DiagnosticInfo>
|
||||
public sealed class Comparer : Comparer<DiagnosticInfo, Comparer>
|
||||
{
|
||||
/// <summary>
|
||||
/// The singleton <see cref="Comparer"/> instance.
|
||||
/// </summary>
|
||||
public static Comparer Default { get; } = new();
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool Equals(DiagnosticInfo? x, DiagnosticInfo? y)
|
||||
protected override void AddToHashCode(ref HashCode hashCode, DiagnosticInfo obj)
|
||||
{
|
||||
if (x is null && y is null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (x is null || y is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ReferenceEquals(x, y))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return
|
||||
x.Descriptor.Equals(y.Descriptor) &&
|
||||
x.Args.SequenceEqual(y.Args);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int GetHashCode(DiagnosticInfo obj)
|
||||
{
|
||||
HashCode hashCode = default;
|
||||
|
||||
hashCode.Add(obj.Descriptor);
|
||||
|
||||
|
||||
foreach (object arg in obj.Args)
|
||||
{
|
||||
hashCode.Add(arg);
|
||||
}
|
||||
}
|
||||
|
||||
return hashCode.ToHashCode();
|
||||
/// <inheritdoc/>
|
||||
protected override bool AreEqual(DiagnosticInfo x, DiagnosticInfo y)
|
||||
{
|
||||
return
|
||||
x.Descriptor.Equals(y.Descriptor) &&
|
||||
x.Args.SequenceEqual(y.Args);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using ComputeSharp.SourceGeneration.Extensions;
|
||||
using ComputeSharp.SourceGeneration.Helpers;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using static Microsoft.CodeAnalysis.SymbolDisplayTypeQualificationStyle;
|
||||
|
||||
@@ -42,49 +43,25 @@ internal sealed record HierarchyInfo(string FilenameHint, string MetadataName, s
|
||||
/// <summary>
|
||||
/// An <see cref="IEqualityComparer{T}"/> implementation for <see cref="HierarchyInfo"/>.
|
||||
/// </summary>
|
||||
public sealed class Comparer : IEqualityComparer<HierarchyInfo>
|
||||
public sealed class Comparer : Comparer<HierarchyInfo, Comparer>
|
||||
{
|
||||
/// <summary>
|
||||
/// The singleton <see cref="Comparer"/> instance.
|
||||
/// </summary>
|
||||
public static Comparer Default { get; } = new();
|
||||
/// <inheritdoc/>
|
||||
protected override void AddToHashCode(ref HashCode hashCode, HierarchyInfo obj)
|
||||
{
|
||||
hashCode.Add(obj.FilenameHint);
|
||||
hashCode.Add(obj.MetadataName);
|
||||
hashCode.Add(obj.Namespace);
|
||||
hashCode.AddRange(obj.Names);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool Equals(HierarchyInfo? x, HierarchyInfo? y)
|
||||
protected override bool AreEqual(HierarchyInfo x, HierarchyInfo y)
|
||||
{
|
||||
if (x is null && y is null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (x is null || y is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ReferenceEquals(x, y))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return
|
||||
x.FilenameHint == y.FilenameHint &&
|
||||
x.MetadataName == y.MetadataName &&
|
||||
x.Namespace == y.Namespace &&
|
||||
x.Names.SequenceEqual(y.Names);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int GetHashCode(HierarchyInfo obj)
|
||||
{
|
||||
HashCode hashCode = default;
|
||||
|
||||
hashCode.Add(obj.FilenameHint);
|
||||
hashCode.Add(obj.MetadataName);
|
||||
hashCode.Add(obj.Namespace);
|
||||
hashCode.AddRange(obj.Names);
|
||||
|
||||
return hashCode.ToHashCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする