diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs index 9b5ffd45..86dcf802 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using System.Text; -using ComputeSharp.D2D1.SourceGenerators.Diagnostics; using ComputeSharp.D2D1.SourceGenerators.SyntaxRewriters; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; @@ -24,7 +23,7 @@ partial class ID2D1ShaderGenerator /// /// A helper with all logic to generate the BuildHlslSource method. /// - internal static partial class BuildHlslSource + private static partial class BuildHlslSource { /// /// Gathers all necessary information on a transpiled HLSL source for a given shader type. @@ -33,13 +32,17 @@ partial class ID2D1ShaderGenerator /// The node to process. /// The for . /// The number of inputs for the shader. + /// The indicess of the simple shader inputs. + /// The indicess of the complex shader inputs. /// The resulting diagnostics from the processing operation. /// The HLSL source for the shader. public static string GetHlslSource( Compilation compilation, StructDeclarationSyntax structDeclaration, INamedTypeSymbol structDeclarationSymbol, - out int inputCount, + int inputCount, + ImmutableArray inputSimpleIndices, + ImmutableArray inputComplexIndices, out ImmutableArray diagnostics) { ImmutableArray.Builder builder = ImmutableArray.CreateBuilder(); @@ -62,14 +65,8 @@ partial class ID2D1ShaderGenerator var declaredTypes = GetDeclaredTypes(builder, structDeclarationSymbol, discoveredTypes); var definedConstants = GetDefinedConstants(constantDefinitions); - // Gather the shader metadata - GatherD2D1AttributeInfo( - builder, - structDeclarationSymbol, - out inputCount, - out ImmutableArray inputSimpleIndices, - out ImmutableArray inputComplexIndices, - out bool requiresScenePosition); + // Check whether the scene position is required + bool requiresScenePosition = GetRequiresScenePositionInfo(structDeclarationSymbol); diagnostics = builder.ToImmutable(); @@ -397,100 +394,21 @@ partial class ID2D1ShaderGenerator } /// - /// Extracts the metadata definition for the current shader. + /// Gets whether or not a given shader requires the scene position. /// - /// The collection of produced instances. /// The input instance to process. - /// The number of shader inputs to declare. - /// The indicess of the simple shader inputs. - /// The indicess of the complex shader inputs. - /// Whether the shader requires the scene position. - private static void GatherD2D1AttributeInfo( - ImmutableArray.Builder diagnostics, - INamedTypeSymbol structDeclarationSymbol, - out int inputCount, - out ImmutableArray inputSimpleIndices, - out ImmutableArray inputComplexIndices, - out bool requiresScenePosition) + /// Whether requires the scene position. + private static bool GetRequiresScenePositionInfo(INamedTypeSymbol structDeclarationSymbol) { - // We need a separate local here so we can keep the original value to apply - // diagnostics to, but without returning invalid values to the caller which - // might cause generator errors (eg. -1 would cause other code to just throw). - int rawInputCount = 0; - - inputCount = 0; - requiresScenePosition = false; - - ImmutableArray.Builder inputSimpleIndicesBuilder = ImmutableArray.CreateBuilder(); - ImmutableArray.Builder inputComplexIndicesBuilder = ImmutableArray.CreateBuilder(); - foreach (AttributeData attributeData in structDeclarationSymbol.GetAttributes()) { - switch (attributeData.AttributeClass?.GetFullMetadataName()) + if (attributeData.AttributeClass?.GetFullMetadataName() is "ComputeSharp.D2D1.D2DRequiresScenePositionAttribute") { - case "ComputeSharp.D2D1.D2DInputCountAttribute": - rawInputCount = (int)attributeData.ConstructorArguments[0].Value!; - inputCount = Math.Max(rawInputCount, 0); - break; - case "ComputeSharp.D2D1.D2DInputSimpleAttribute": - inputSimpleIndicesBuilder.Add((int)attributeData.ConstructorArguments[0].Value!); - break; - case "ComputeSharp.D2D1.D2DInputComplexAttribute": - inputComplexIndicesBuilder.Add((int)attributeData.ConstructorArguments[0].Value!); - break; - case "ComputeSharp.D2D1.D2DRequiresScenePositionAttribute": - requiresScenePosition = true; - break; - default: - break; + return true; } } - inputSimpleIndices = inputSimpleIndicesBuilder.ToImmutable(); - inputComplexIndices = inputComplexIndicesBuilder.ToImmutable(); - - // Validate the input count - if (rawInputCount is not (>= 0 and <= 8)) - { - diagnostics.Add(InvalidD2DInputCount, structDeclarationSymbol, structDeclarationSymbol); - - return; - } - - // All simple indices must be in range - if (inputSimpleIndicesBuilder.Concat(inputComplexIndicesBuilder).Any(i => (uint)i >= rawInputCount)) - { - diagnostics.Add(OutOfRangeInputIndex, structDeclarationSymbol, structDeclarationSymbol); - - return; - } - - HashSet inputSimpleIndicesAsSet = new(inputSimpleIndicesBuilder); - HashSet inputComplexIndicesAsSet = new(inputComplexIndicesBuilder); - - // All simple indices must be unique - if (inputSimpleIndicesAsSet.Count != inputSimpleIndicesBuilder.Count) - { - diagnostics.Add(RepeatedD2DInputSimpleIndices, structDeclarationSymbol, structDeclarationSymbol); - - return; - } - - // All complex indices must be unique - if (inputComplexIndicesAsSet.Count != inputComplexIndicesBuilder.Count) - { - diagnostics.Add(RepeatedD2DInputComplexIndices, structDeclarationSymbol, structDeclarationSymbol); - - return; - } - - // Simple and complex indices can't have indices in common - if (inputSimpleIndicesAsSet.Intersect(inputComplexIndicesAsSet).Any()) - { - diagnostics.Add(InvalidSimpleAndComplexIndicesCombination, structDeclarationSymbol, structDeclarationSymbol); - - return; - } + return false; } /// diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputCountMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputCountMethod.Syntax.cs index d0496ef4..7553f9b2 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputCountMethod.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputCountMethod.Syntax.cs @@ -13,7 +13,7 @@ partial class ID2D1ShaderGenerator /// /// A helper with all logic to generate the GetInputCount method. /// - partial class GetInputCount + private static partial class GetInputCount { /// /// Creates a instance for the GetInputCount method. diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.Syntax.cs new file mode 100644 index 00000000..da74241a --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.Syntax.cs @@ -0,0 +1,81 @@ +using System.Collections.Immutable; +using ComputeSharp.D2D1.__Internals; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; + +#pragma warning disable CS0618 + +namespace ComputeSharp.D2D1.SourceGenerators; + +/// +partial class ID2D1ShaderGenerator +{ + /// + partial class GetInputType + { + /// + /// Creates a instance for the GetInputTypeMethod method. + /// + /// The input types for the shader. + /// The resulting instance for the GetInputTypeMethod method. + public static MethodDeclarationSyntax GetSyntax(ImmutableArray inputTypes) + { + // This code produces a method declaration as follows: + // + // readonly uint global::ComputeSharp.D2D1.__Internals.ID2D1Shader.GetInputType(uint index) + // { + // return ; + // } + return + MethodDeclaration(PredefinedType(Token(SyntaxKind.UIntKeyword)), Identifier("GetInputType")) + .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}"))) + .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) + .AddParameterListParameters(Parameter(Identifier("index")).WithType(PredefinedType(Token(SyntaxKind.UIntKeyword)))) + .WithBody(Block(ReturnStatement(GetInputTypesSwitchExpression(inputTypes)))); + } + + /// + /// Gets the expression to get the type of a given shader input from its index. + /// + /// The input types for the shader. + /// The expression to extract the values from . + private static ExpressionSyntax GetInputTypesSwitchExpression(ImmutableArray inputTypes) + { + // If there are no inputs, just return an unspecified value. + if (inputTypes.IsEmpty) + { + return LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0)); + } + + ImmutableArray.Builder switches = ImmutableArray.CreateBuilder(); + + for (int i = 0; i < inputTypes.Length; i++) + { + // Add a switch branch to map a given input to its serialized type, as follows: + // + // 0u => 1u + switches.Add( + SwitchExpressionArm( + ConstantPattern(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal((uint)i))), + LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(inputTypes[i])))); + } + + // Add a default arm + switches.Add( + SwitchExpressionArm( + DiscardPattern(), + LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0u)))); + + // Build a switch expression as follows: + // + // index switch + // { + // + // } + return + SwitchExpression(IdentifierName("index")) + .AddArms(switches.ToArray()); + } + } +} diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.cs new file mode 100644 index 00000000..926345cb --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.cs @@ -0,0 +1,133 @@ +using System; +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Linq; +using ComputeSharp.SourceGeneration.Extensions; +using Microsoft.CodeAnalysis; +using static ComputeSharp.D2D1.SourceGenerators.Diagnostics.DiagnosticDescriptors; + +namespace ComputeSharp.D2D1.SourceGenerators; + +/// +partial class ID2D1ShaderGenerator +{ + /// + /// A helper with all logic to generate the GetInputTypeMethod method. + /// + private static partial class GetInputType + { + /// + /// Extracts the input info for the current shader. + /// + /// The collection of produced instances. + /// The input instance to process. + /// The number of shader inputs to declare. + /// The indicess of the simple shader inputs. + /// The indices of the complex shader inputs. + /// The combined and serialized input types for each available input. + public static void GetInfo( + INamedTypeSymbol structDeclarationSymbol, + out int inputCount, + out ImmutableArray inputSimpleIndices, + out ImmutableArray inputComplexIndices, + out ImmutableArray combinedInputTypes, + out ImmutableArray diagnostics) + { + ImmutableArray.Builder builder = ImmutableArray.CreateBuilder(); + + // We need a separate local here so we can keep the original value to apply + // diagnostics to, but without returning invalid values to the caller which + // might cause generator errors (eg. -1 would cause other code to just throw). + int rawInputCount = 0; + + inputCount = 0; + + ImmutableArray.Builder inputSimpleIndicesBuilder = ImmutableArray.CreateBuilder(); + ImmutableArray.Builder inputComplexIndicesBuilder = ImmutableArray.CreateBuilder(); + + foreach (AttributeData attributeData in structDeclarationSymbol.GetAttributes()) + { + switch (attributeData.AttributeClass?.GetFullMetadataName()) + { + case "ComputeSharp.D2D1.D2DInputCountAttribute": + rawInputCount = (int)attributeData.ConstructorArguments[0].Value!; + inputCount = Math.Max(rawInputCount, 0); + break; + case "ComputeSharp.D2D1.D2DInputSimpleAttribute": + inputSimpleIndicesBuilder.Add((int)attributeData.ConstructorArguments[0].Value!); + break; + case "ComputeSharp.D2D1.D2DInputComplexAttribute": + inputComplexIndicesBuilder.Add((int)attributeData.ConstructorArguments[0].Value!); + break; + default: + break; + } + } + + inputSimpleIndices = inputSimpleIndicesBuilder.ToImmutable(); + inputComplexIndices = inputComplexIndicesBuilder.ToImmutable(); + combinedInputTypes = ImmutableArray.Empty; + + // Validate the input count + if (rawInputCount is not (>= 0 and <= 8)) + { + builder.Add(InvalidD2DInputCount, structDeclarationSymbol, structDeclarationSymbol); + + goto End; + } + + // All simple indices must be in range + if (inputSimpleIndicesBuilder.Concat(inputComplexIndicesBuilder).Any(i => (uint)i >= rawInputCount)) + { + builder.Add(OutOfRangeInputIndex, structDeclarationSymbol, structDeclarationSymbol); + + goto End; + } + + HashSet inputSimpleIndicesAsSet = new(inputSimpleIndicesBuilder); + HashSet inputComplexIndicesAsSet = new(inputComplexIndicesBuilder); + + // All simple indices must be unique + if (inputSimpleIndicesAsSet.Count != inputSimpleIndicesBuilder.Count) + { + builder.Add(RepeatedD2DInputSimpleIndices, structDeclarationSymbol, structDeclarationSymbol); + + goto End; + } + + // All complex indices must be unique + if (inputComplexIndicesAsSet.Count != inputComplexIndicesBuilder.Count) + { + builder.Add(RepeatedD2DInputComplexIndices, structDeclarationSymbol, structDeclarationSymbol); + + goto End; + } + + // Simple and complex indices can't have indices in common + if (inputSimpleIndicesAsSet.Intersect(inputComplexIndicesAsSet).Any()) + { + builder.Add(InvalidSimpleAndComplexIndicesCombination, structDeclarationSymbol, structDeclarationSymbol); + + goto End; + } + + // Build the combined input types list now that inputs have been validated + ImmutableArray.Builder combinedBuilder = ImmutableArray.CreateBuilder(inputCount); + + for (int i = 0; i < inputCount; i++) + { + // The D2D_INPUT_SIMPLE define is optional, and if omitted, the corresponding input + // will automatically be of complex type. So we just need to check whether the i-th + // input is present in the set of explicitly simple inputs. These values will map to + // values from D2D1PixelShaderInputType, which has Simple and Complex as members. + // So, simple inputs map to 0, and complex inputs map to 1. + combinedBuilder.Add(inputSimpleIndicesAsSet.Contains(i) ? 0u : 1u); + } + + combinedInputTypes = combinedBuilder.MoveToImmutable(); + + End: + diagnostics = builder.ToImmutable(); + } + } +} diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs index f616dd12..ca1ba01a 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs @@ -22,7 +22,7 @@ namespace ComputeSharp.D2D1.SourceGenerators; partial class ID2D1ShaderGenerator { /// - internal static partial class LoadBytecode + private static partial class LoadBytecode { /// /// Extracts the shader profile for the current shader. diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index 5851d946..12cd2d3b 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -49,9 +49,9 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator .Where(static item => IsD2D1PixelShaderType(item.Left.Symbol, item.Right)) .Select(static (item, token) => (item.Left.Syntax, item.Left.Symbol, HierarchyInfo.From(item.Left.Symbol))); - // Get the dispatch data, HLSL source and embedded bytecode info. This info is computed on the - // same step as parts are shared in following sub-branches in the incremental generator pipeline. - IncrementalValuesProvider<(HierarchyInfo Hierarchy, Result Dispatch, Result Source, int InputCount)> shaderInfoWithErrors = + // Get the dispatch data, input types, HLSL source and embedded bytecode info. This info is computed + // on the same step as parts are shared in following sub-branches in the incremental generator pipeline. + IncrementalValuesProvider<(HierarchyInfo Hierarchy, Result Dispatch, Result InputTypes, Result Source)> shaderInfoWithErrors = shaderDeclarations .Combine(context.CompilationProvider) .Select(static (item, token) => @@ -66,12 +66,25 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator DispatchDataInfo dispatchDataInfo = new(fieldInfos, root32BitConstantCount); + // Get the input info for GetInputInfo() + GetInputType.GetInfo( + item.Left.Symbol, + out int inputCount, + out ImmutableArray inputSimpleIndices, + out ImmutableArray inputComplexIndices, + out ImmutableArray inputTypes, + out ImmutableArray inputTypesDiagnostics); + + InputTypesInfo inputTypesInfo = new(inputTypes); + // Get HLSL source for BuildHlslSource() string hlslSource = BuildHlslSource.GetHlslSource( item.Right, item.Left.Syntax, item.Left.Symbol, - out int inputCount, + inputCount, + inputSimpleIndices, + inputComplexIndices, out ImmutableArray hlslSourceDiagnostics); token.ThrowIfCancellationRequested(); @@ -92,18 +105,19 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator return ( item.Left.Hierarchy, new Result(dispatchDataInfo, dispatchDataDiagnostics), - new Result(sourceInfo, hlslSourceDiagnostics), - inputCount); + new Result(inputTypesInfo, inputTypesDiagnostics), + new Result(sourceInfo, hlslSourceDiagnostics)); }); // Output the diagnostics context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.Dispatch.Errors)); + context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.InputTypes.Errors)); context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.Source.Errors)); // Filter items to enable caching for the input count methods IncrementalValuesProvider<(HierarchyInfo Hierarchy, int InputCount)> inputCountInfo = shaderInfoWithErrors - .Select(static (item, token) => (item.Hierarchy, item.InputCount)) + .Select(static (item, token) => (item.Hierarchy, item.InputTypes.Value.InputTypes.Length)) .WithComparers(HierarchyInfo.Comparer.Default, EqualityComparer.Default); // Generate the GetInputCount() methods @@ -116,10 +130,25 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator }); // Filter all items to enable caching at a coarse level, and remove diagnostics - IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchDataInfo Dispatch, HlslShaderSourceInfo Source)> shaderInfo = + IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchDataInfo Dispatch, InputTypesInfo InputTypes, HlslShaderSourceInfo Source)> shaderInfo = shaderInfoWithErrors - .Select(static (item, token) => (item.Hierarchy, item.Dispatch.Value, item.Source.Value)) - .WithComparers(HierarchyInfo.Comparer.Default, DispatchDataInfo.Comparer.Default, EqualityComparer.Default); + .Select(static (item, token) => (item.Hierarchy, item.Dispatch.Value, item.InputTypes.Value, item.Source.Value)) + .WithComparers(HierarchyInfo.Comparer.Default, DispatchDataInfo.Comparer.Default, InputTypesInfo.Comparer.Default, EqualityComparer.Default); + + // Get a filtered sequence to enable caching + IncrementalValuesProvider<(HierarchyInfo Hierarchy, InputTypesInfo InputTypes)> inputTypesInfo = + shaderInfo + .Select(static (item, token) => (item.Hierarchy, item.InputTypes)) + .WithComparers(HierarchyInfo.Comparer.Default, InputTypesInfo.Comparer.Default); + + // Generate the GetInputType() methods + context.RegisterSourceOutput(inputTypesInfo, static (context, item) => + { + MethodDeclarationSyntax getInputTypeMethod = GetInputType.GetSyntax(item.InputTypes.InputTypes); + CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, getInputTypeMethod, false); + + context.AddSource($"{item.Hierarchy.FilenameHint}.{nameof(GetInputType)}", compilationUnit.ToFullString()); + }); // Get a filtered sequence to enable caching IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchDataInfo Dispatch)> dispatchDataInfo = diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/InputTypesInfo.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/InputTypesInfo.cs new file mode 100644 index 00000000..0a3c10c2 --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/Models/InputTypesInfo.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Collections.Immutable; +using ComputeSharp.SourceGeneration.Extensions; +using ComputeSharp.SourceGeneration.Helpers; + +namespace ComputeSharp.D2D1.SourceGenerators.Models; + +/// +/// A model representing gathered info on a shader input types. +/// +/// The input types for a given shader. +internal sealed record InputTypesInfo(ImmutableArray InputTypes) +{ + /// + /// An implementation for . + /// + public sealed class Comparer : Comparer + { + /// + protected override void AddToHashCode(ref HashCode hashCode, InputTypesInfo obj) + { + hashCode.AddRange(obj.InputTypes); + } + + /// + protected override bool AreEqual(InputTypesInfo x, InputTypesInfo y) + { + return x.InputTypes.SequenceEqual(y.InputTypes); + } + } +} diff --git a/src/ComputeSharp.SourceGeneration/Extensions/IncrementalValuesProviderExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/IncrementalValuesProviderExtensions.cs index a7d0e73d..f7a26034 100644 --- a/src/ComputeSharp.SourceGeneration/Extensions/IncrementalValuesProviderExtensions.cs +++ b/src/ComputeSharp.SourceGeneration/Extensions/IncrementalValuesProviderExtensions.cs @@ -46,6 +46,29 @@ internal static class IncrementalValuesProviderExtensions return source.WithComparer(new Comparer(comparer1, comparer2, comparer3)); } + /// + /// Creates a new instance with a gven pair of comparers. + /// + /// The type of first items in each tuple. + /// The type of second items in each tuple. + /// The type of third items in each tuple. + /// The type of fourth items in each tuple. + /// The input instance. + /// An instance for items. + /// An instance for items. + /// An instance for items. + /// An instance for items. + /// An with the specified comparers applied to each item. + public static IncrementalValuesProvider<(T1, T2, T3, T4)> WithComparers( + this IncrementalValuesProvider<(T1, T2, T3, T4)> source, + IEqualityComparer comparer1, + IEqualityComparer comparer2, + IEqualityComparer comparer3, + IEqualityComparer comparer4) + { + return source.WithComparer(new Comparer(comparer1, comparer2, comparer3, comparer4)); + } + /// /// An implementation for a value tuple. /// @@ -140,4 +163,65 @@ internal static class IncrementalValuesProviderExtensions this.comparer3.GetHashCode(obj.Item3)); } } + + /// + /// An implementation for a value tuple. + /// + public sealed class Comparer : IEqualityComparer<(T1, T2, T3, T4)> + { + /// + /// The comparer. + /// + private readonly IEqualityComparer comparer1; + + /// + /// The comparer. + /// + private readonly IEqualityComparer comparer2; + + /// + /// The comparer. + /// + private readonly IEqualityComparer comparer3; + + /// + /// The comparer. + /// + private readonly IEqualityComparer comparer4; + + /// + /// Creates a new instance with the specified parameters. + /// + /// The comparer. + /// The comparer. + /// The comparer. + /// The comparer. + public Comparer(IEqualityComparer comparer1, IEqualityComparer comparer2, IEqualityComparer comparer3, IEqualityComparer comparer4) + { + this.comparer1 = comparer1; + this.comparer2 = comparer2; + this.comparer3 = comparer3; + this.comparer4 = comparer4; + } + + /// + public bool Equals((T1, T2, T3, T4) x, (T1, T2, T3, T4) y) + { + return + this.comparer1.Equals(x.Item1, y.Item1) && + this.comparer2.Equals(x.Item2, y.Item2) && + this.comparer3.Equals(x.Item3, y.Item3) && + this.comparer4.Equals(x.Item4, y.Item4); + } + + /// + public int GetHashCode((T1, T2, T3, T4) obj) + { + return HashCode.Combine( + this.comparer1.GetHashCode(obj.Item1), + this.comparer2.GetHashCode(obj.Item2), + this.comparer3.GetHashCode(obj.Item3), + this.comparer4.GetHashCode(obj.Item4)); + } + } }