Implement comparers for all model types
このコミットが含まれているのは:
1 親
5210c1ecd9
コミット
ed75dd4b5a
6ファイル変更
+223
-8
検索条件に一致するファイルはありません
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace ComputeSharp.SourceGenerators.Extensions;
|
||||
@@ -21,4 +22,19 @@ internal static class HashCodeExtensions
|
||||
hashCode.Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds all items from a given <see cref="ImmutableArray{T}"/> instance to an hashcode.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to hash.</typeparam>
|
||||
/// <param name="hashCode">The target <see cref="HashCode"/> instance.</param>
|
||||
/// <param name="comparer">A comparer to get hashcodes for <typeparamref name="T"/> items.</param>
|
||||
/// <param name="items">The input items to hash.</param>
|
||||
public static void AddRange<T>(this ref HashCode hashCode, ImmutableArray<T> items, IEqualityComparer<T> comparer)
|
||||
{
|
||||
foreach (T item in items)
|
||||
{
|
||||
hashCode.Add(comparer.GetHashCode(item));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace ComputeSharp.SourceGenerators.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="ImmutableArray{T}"/>.
|
||||
/// </summary>
|
||||
internal static class ImmutableArrayExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Compares two sequences for equality.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The items to compare.</typeparam>
|
||||
/// <param name="left">The left sequence of items.</param>
|
||||
/// <param name="right">The right sequence of items.</param>
|
||||
/// <returns>Whether or not <paramref name="left"/> equals <paramref name="right"/>.</returns>
|
||||
public static bool SequenceEqual<T>(this ImmutableArray<T> left, ImmutableArray<T> right)
|
||||
where T : IEquatable<T>
|
||||
{
|
||||
return left.AsSpan().SequenceEqual(right.AsSpan());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compares two sequences for equality.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The items to compare.</typeparam>
|
||||
/// <param name="left">The left sequence of items.</param>
|
||||
/// <param name="right">The right sequence of items.</param>
|
||||
/// <param name="comparer">The <see cref="IEqualityComparer{T}"/> instance used to compare <typeparamref name="T"/> items.</param>
|
||||
/// <returns>Whether or not <paramref name="left"/> equals <paramref name="right"/>.</returns>
|
||||
public static bool SequenceEqual<T>(this ImmutableArray<T> left, ImmutableArray<T> right, IEqualityComparer<T> comparer)
|
||||
{
|
||||
if (left.Length != right.Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < left.Length; i++)
|
||||
{
|
||||
if (!comparer.Equals(left[i], right[i]))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using ComputeSharp.SourceGenerators.Extensions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace ComputeSharp.SourceGenerators.Models;
|
||||
@@ -16,4 +18,56 @@ internal sealed record DispatchDataInfo(
|
||||
Type Type,
|
||||
ImmutableArray<FieldInfo> FieldInfos,
|
||||
int ResourceCount,
|
||||
int Root32BitConstantCount);
|
||||
int Root32BitConstantCount)
|
||||
{
|
||||
/// <summary>
|
||||
/// An <see cref="IEqualityComparer{T}"/> implementation for <see cref="DispatchDataInfo"/>.
|
||||
/// </summary>
|
||||
public sealed class Comparer : IEqualityComparer<DispatchDataInfo>
|
||||
{
|
||||
/// <summary>
|
||||
/// The singleton <see cref="Comparer"/> instance.
|
||||
/// </summary>
|
||||
public static Comparer Default { get; } = new();
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool Equals(DispatchDataInfo? x, DispatchDataInfo? 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
|
||||
HierarchyInfo.Comparer.Default.Equals(x.Hierarchy, y.Hierarchy) &&
|
||||
x.Type == y.Type &&
|
||||
x.FieldInfos.SequenceEqual(y.FieldInfos, FieldInfo.Comparer.Default) &&
|
||||
x.ResourceCount == y.ResourceCount &&
|
||||
x.Root32BitConstantCount == y.Root32BitConstantCount;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int GetHashCode(DispatchDataInfo obj)
|
||||
{
|
||||
HashCode hashCode = default;
|
||||
|
||||
hashCode.Add(HierarchyInfo.Comparer.Default.GetHashCode(obj.Hierarchy));
|
||||
hashCode.Add(obj.Type);
|
||||
hashCode.AddRange(obj.FieldInfos, FieldInfo.Comparer.Default);
|
||||
hashCode.Add(obj.ResourceCount);
|
||||
hashCode.Add(obj.Root32BitConstantCount);
|
||||
|
||||
return hashCode.ToHashCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,7 @@
|
||||
using System.Collections.Immutable;
|
||||
using ComputeSharp.SourceGenerators.Extensions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace ComputeSharp.SourceGenerators.Models;
|
||||
|
||||
@@ -39,4 +42,96 @@ internal abstract record FieldInfo
|
||||
int Rows,
|
||||
int Columns,
|
||||
ImmutableArray<int> Offsets) : FieldInfo;
|
||||
|
||||
/// <summary>
|
||||
/// An <see cref="IEqualityComparer{T}"/> implementation for <see cref="FieldInfo"/>.
|
||||
/// </summary>
|
||||
public sealed class Comparer : IEqualityComparer<FieldInfo>
|
||||
{
|
||||
/// <summary>
|
||||
/// The singleton <see cref="Comparer"/> instance.
|
||||
/// </summary>
|
||||
public static Comparer Default { get; } = new();
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool Equals(FieldInfo? x, FieldInfo? 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;
|
||||
}
|
||||
|
||||
if (x is Resource resourceX && y is Resource resourceY)
|
||||
{
|
||||
return
|
||||
resourceX.FieldName == resourceY.FieldName &&
|
||||
resourceX.TypeName == resourceY.TypeName &&
|
||||
resourceX.Offset == resourceY.Offset;
|
||||
}
|
||||
else if (x is Primitive primitiveX && y is Primitive primitiveY)
|
||||
{
|
||||
return
|
||||
primitiveX.FieldPath.SequenceEqual(primitiveY.FieldPath) &&
|
||||
primitiveX.TypeName == primitiveY.TypeName &&
|
||||
primitiveX.Offset == primitiveY.Offset;
|
||||
}
|
||||
else if (x is NonLinearMatrix matrixX && y is NonLinearMatrix matrixY)
|
||||
{
|
||||
return
|
||||
matrixX.FieldPath.SequenceEqual(matrixY.FieldPath) &&
|
||||
matrixX.TypeName == matrixY.TypeName &&
|
||||
matrixX.ElementName == matrixY.ElementName &&
|
||||
matrixX.Rows == matrixY.Rows &&
|
||||
matrixY.Columns == matrixY.Columns &&
|
||||
matrixX.Offsets.SequenceEqual(matrixY.Offsets);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int GetHashCode(FieldInfo obj)
|
||||
{
|
||||
switch (obj)
|
||||
{
|
||||
case Resource resource:
|
||||
return HashCode.Combine(resource.FieldName, resource.TypeName, resource.Offset);
|
||||
case Primitive primitive:
|
||||
{
|
||||
HashCode hashCode = default;
|
||||
|
||||
hashCode.AddRange(primitive.FieldPath);
|
||||
hashCode.Add(primitive.TypeName);
|
||||
hashCode.Add(primitive.Offset);
|
||||
|
||||
return hashCode.ToHashCode();
|
||||
}
|
||||
case NonLinearMatrix matrix:
|
||||
{
|
||||
HashCode hashCode = default;
|
||||
|
||||
hashCode.AddRange(matrix.FieldPath);
|
||||
hashCode.Add(matrix.TypeName);
|
||||
hashCode.Add(matrix.ElementName);
|
||||
hashCode.Add(matrix.Rows);
|
||||
hashCode.Add(matrix.Columns);
|
||||
hashCode.AddRange(matrix.Offsets);
|
||||
|
||||
return hashCode.ToHashCode();
|
||||
}
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -70,7 +70,7 @@ internal sealed record HierarchyInfo(string FilenameHint, string Namespace, Immu
|
||||
return
|
||||
x.FilenameHint == y.FilenameHint &&
|
||||
x.Namespace == y.Namespace &&
|
||||
x.Names.AsSpan().SequenceEqual(y.Names.AsSpan());
|
||||
x.Names.SequenceEqual(y.Names);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
|
||||
@@ -77,10 +77,10 @@ internal sealed record HlslSourceInfo(
|
||||
x.EntryPoint == y.EntryPoint &&
|
||||
x.ImplicitTextureType == y.ImplicitTextureType &&
|
||||
x.IsSamplerUsed == y.IsSamplerUsed &&
|
||||
x.DefinedTypes.AsSpan().SequenceEqual(y.DefinedTypes.AsSpan()) &&
|
||||
x.DefinedConstants.AsSpan().SequenceEqual(y.DefinedTypes.AsSpan()) &&
|
||||
x.MethodSignatures.AsSpan().SequenceEqual(y.MethodSignatures.AsSpan()) &&
|
||||
x.Delegates.AsSpan().SequenceEqual(y.Delegates.AsSpan());
|
||||
x.DefinedTypes.SequenceEqual(y.DefinedTypes) &&
|
||||
x.DefinedConstants.SequenceEqual(y.DefinedTypes) &&
|
||||
x.MethodSignatures.SequenceEqual(y.MethodSignatures) &&
|
||||
x.Delegates.SequenceEqual(y.Delegates);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする