Move generators to ComputeSharp.Core.SourceGenerators
このコミットが含まれているのは:
1 親
f1918c8348
コミット
59cd5a9ce3
18ファイル変更
+489
-12
検索条件に一致するファイルはありません
@@ -1,167 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using ComputeSharp.SourceGenerators.Extensions;
|
||||
using ComputeSharp.SourceGenerators.Models;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
|
||||
|
||||
namespace ComputeSharp.SourceGenerators;
|
||||
|
||||
/// <summary>
|
||||
/// A source generator creating constructors for types annotated with <see cref="AutoConstructorAttribute"/>.
|
||||
/// </summary>
|
||||
[Generator(LanguageNames.CSharp)]
|
||||
public sealed partial class AutoConstructorGenerator : IIncrementalGenerator
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public void Initialize(IncrementalGeneratorInitializationContext context)
|
||||
{
|
||||
// Get all declared struct symbols with the [AutoConstructor] attribute
|
||||
IncrementalValuesProvider<INamedTypeSymbol> structDeclarations =
|
||||
context.SyntaxProvider
|
||||
.CreateSyntaxProvider(
|
||||
static (node, token) => node is StructDeclarationSyntax structDeclaration,
|
||||
static (context, token) => (INamedTypeSymbol?)context.SemanticModel.GetDeclaredSymbol(context.Node, token))
|
||||
.Where(static symbol => symbol is not null &&
|
||||
symbol.GetAttributes().Any(static a => a.AttributeClass?.ToDisplayString() == typeof(AutoConstructorAttribute).FullName))!;
|
||||
|
||||
// Get the type hierarchy and fields info
|
||||
IncrementalValuesProvider<(HierarchyInfo Left, ImmutableArray<ParameterInfo> Right)> constructorInfo =
|
||||
structDeclarations
|
||||
.Select(static (item, token) => (Hierarchy: HierarchyInfo.From(item), Parameters: Ctor.GetData(item)))
|
||||
.Where(static item => !item.Parameters.IsEmpty)
|
||||
.WithComparers(HierarchyInfo.Comparer.Default, EqualityComparer<ParameterInfo>.Default.ForImmutableArray());
|
||||
|
||||
// Generate the constructors
|
||||
context.RegisterSourceOutput(constructorInfo, static (context, item) =>
|
||||
{
|
||||
CompilationUnitSyntax compilationUnit = Ctor.GetSyntax(item.Left, item.Right);
|
||||
|
||||
context.AddSource($"{item.Left.FilenameHint}.Ctor", compilationUnit.ToFullString());
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A helper with all logic to generate the constructor declarations.
|
||||
/// </summary>
|
||||
private static class Ctor
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the sequence of <see cref="ParameterInfo"/> items for all fields in the input type.
|
||||
/// </summary>
|
||||
/// <param name="structDeclarationSymbol">The <see cref="INamedTypeSymbol"/> being inspected.</param>
|
||||
/// <returns>The sequence of <see cref="ParameterInfo"/> items for all fields in <paramref name="structDeclarationSymbol"/>.</returns>
|
||||
public static ImmutableArray<ParameterInfo> GetData(INamedTypeSymbol structDeclarationSymbol)
|
||||
{
|
||||
return (
|
||||
from fieldSymbol in structDeclarationSymbol.GetMembers().OfType<IFieldSymbol>()
|
||||
where fieldSymbol is { IsConst: false, IsStatic: false, IsFixedSizeBuffer: false }
|
||||
let typeName = fieldSymbol.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)
|
||||
select new ParameterInfo(typeName, fieldSymbol.Name)).ToImmutableArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="CompilationUnitSyntax"/> instance for a given type and sequence of parameters.
|
||||
/// </summary>
|
||||
/// <param name="hierarchyInfo">The type hierarchy info.</param>
|
||||
/// <param name="parameters">The sequence of parameters to initialize.</param>
|
||||
/// <returns>A <see cref="CompilationUnitSyntax"/> instance for a specified type.</returns>
|
||||
public static CompilationUnitSyntax GetSyntax(HierarchyInfo hierarchyInfo, ImmutableArray<ParameterInfo> parameters)
|
||||
{
|
||||
// Create the constructor declaration for the type. This will
|
||||
// produce a constructor with simple initialization of all variables:
|
||||
//
|
||||
// public MyType(Foo a, Bar b, Baz c, ...)
|
||||
// {
|
||||
// this.a = a;
|
||||
// this.b = b;
|
||||
// this.c = c;
|
||||
// ...
|
||||
// }
|
||||
ConstructorDeclarationSyntax constructorDeclaration =
|
||||
ConstructorDeclaration(hierarchyInfo.Names[0])
|
||||
.AddModifiers(Token(SyntaxKind.PublicKeyword))
|
||||
.AddParameterListParameters(parameters.Select(field => Parameter(Identifier(field.Name)).WithType(IdentifierName(field.Type))).ToArray())
|
||||
.AddBodyStatements(parameters.Select(field => ParseStatement($"this.{field.Name} = {field.Name};")).ToArray());
|
||||
|
||||
// Add the unsafe modifier, if needed
|
||||
if (parameters.Any(static param => param.Type.Contains("*")))
|
||||
{
|
||||
constructorDeclaration = constructorDeclaration.AddModifiers(Token(SyntaxKind.UnsafeKeyword));
|
||||
}
|
||||
|
||||
// Constructor attributes
|
||||
AttributeListSyntax[] attributes =
|
||||
{
|
||||
AttributeList(SingletonSeparatedList(
|
||||
Attribute(IdentifierName("global::System.CodeDom.Compiler.GeneratedCode")).AddArgumentListArguments(
|
||||
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(AutoConstructorGenerator).FullName))),
|
||||
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(AutoConstructorGenerator).Assembly.GetName().Version.ToString())))))),
|
||||
AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.DebuggerNonUserCode")))),
|
||||
AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage"))))
|
||||
};
|
||||
|
||||
// Create the partial shader type declaration with the given method implementation.
|
||||
// This code produces a struct declaration as follows:
|
||||
//
|
||||
// partial struct <SHADER_TYPE>
|
||||
// {
|
||||
// <METHOD>
|
||||
// }
|
||||
StructDeclarationSyntax structDeclarationSyntax =
|
||||
StructDeclaration(hierarchyInfo.Names[0])
|
||||
.AddModifiers(Token(SyntaxKind.PartialKeyword))
|
||||
.AddMembers(constructorDeclaration.AddAttributeLists(attributes));
|
||||
|
||||
TypeDeclarationSyntax typeDeclarationSyntax = structDeclarationSyntax;
|
||||
|
||||
// Add all parent types in ascending order, if any
|
||||
foreach (string parentType in hierarchyInfo.Names.AsSpan().Slice(1))
|
||||
{
|
||||
typeDeclarationSyntax =
|
||||
ClassDeclaration(parentType)
|
||||
.AddModifiers(Token(SyntaxKind.PartialKeyword))
|
||||
.AddMembers(typeDeclarationSyntax);
|
||||
}
|
||||
|
||||
if (hierarchyInfo.Namespace is "")
|
||||
{
|
||||
// If there is no namespace, attach the pragma directly to the declared type,
|
||||
// and skip the namespace declaration. This will produce code as follows:
|
||||
//
|
||||
// #pragma warning disable
|
||||
//
|
||||
// <TYPE_HIERARCHY>
|
||||
return
|
||||
CompilationUnit().AddMembers(
|
||||
typeDeclarationSyntax
|
||||
.WithModifiers(TokenList(Token(
|
||||
TriviaList(Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))),
|
||||
SyntaxKind.PartialKeyword,
|
||||
TriviaList()))))
|
||||
.NormalizeWhitespace(eol: "\n");
|
||||
}
|
||||
|
||||
// Create the compilation unit with disabled warnings, target namespace and generated type.
|
||||
// This will produce code as follows:
|
||||
//
|
||||
// #pragma warning disable
|
||||
//
|
||||
// namespace <NAMESPACE>;
|
||||
//
|
||||
// <TYPE_HIERARCHY>
|
||||
return
|
||||
CompilationUnit().AddMembers(
|
||||
FileScopedNamespaceDeclaration(IdentifierName(hierarchyInfo.Namespace))
|
||||
.AddMembers(typeDeclarationSyntax)
|
||||
.WithNamespaceKeyword(Token(TriviaList(
|
||||
Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))),
|
||||
SyntaxKind.NamespaceKeyword,
|
||||
TriviaList())))
|
||||
.NormalizeWhitespace(eol: "\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,11 +11,12 @@
|
||||
<DefineConstants>$(DefineConstants);SOURCE_GENERATOR</DefineConstants>
|
||||
</PropertyGroup>
|
||||
|
||||
<!--The source generator cannot contain package references or project references, so instead we're just
|
||||
manually linking the source files from ComputeSharp\Core in here. We only need them to extract metadata
|
||||
and to match members based on full name, so the types being in the generator assembly are not an issue.-->
|
||||
<!-- Same setup as ComputeSharp.Core.SourceGenerators, just with also APIs from ComputeSharp -->
|
||||
<ItemGroup>
|
||||
|
||||
<!-- ComputeSharp.Core.SourceGenerator APIs -->
|
||||
<Compile Include="..\ComputeSharp.Core.SourceGenerators\Helpers\HashCode.cs" Link="Helpers\HashCode.cs" />
|
||||
|
||||
<!-- ComputeSharp.Core APIs -->
|
||||
<Compile Include="..\ComputeSharp.Core\Attributes\AutoConstructorAttribute.cs" Link="ComputeSharp.Core\Attributes\AutoConstructorAttribute.cs" />
|
||||
<Compile Include="..\ComputeSharp.Core\Exceptions\InvalidExecutionContextException.cs" Link="ComputeSharp.Core\Exceptions\InvalidExecutionContextException.cs" />
|
||||
@@ -91,12 +92,6 @@
|
||||
<AdditionalFiles Include="AnalyzerReleases.Unshipped.md" />
|
||||
</ItemGroup>
|
||||
|
||||
<!-- Pregenerated file with global using directives -->
|
||||
<ItemGroup>
|
||||
<Compile Remove="EmbeddedResources\GlobalUsingDirectives.cs" />
|
||||
<EmbeddedResource Include="EmbeddedResources\GlobalUsingDirectives.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="..\ComputeSharp.Dynamic\Libraries\x64\dxcompiler.dll" Link="ComputeSharp\Libraries\x64\dxcompiler.dll" />
|
||||
<EmbeddedResource Include="..\ComputeSharp.Dynamic\Libraries\x64\dxil.dll" Link="ComputeSharp\Libraries\x64\dxil.dll" />
|
||||
|
||||
@@ -1,95 +0,0 @@
|
||||
global using bool2 = global::ComputeSharp.Bool2;
|
||||
global using bool3 = global::ComputeSharp.Bool3;
|
||||
global using bool4 = global::ComputeSharp.Bool4;
|
||||
global using bool1x1 = global::ComputeSharp.Bool1x1;
|
||||
global using bool1x2 = global::ComputeSharp.Bool1x2;
|
||||
global using bool1x3 = global::ComputeSharp.Bool1x3;
|
||||
global using bool1x4 = global::ComputeSharp.Bool1x4;
|
||||
global using bool2x1 = global::ComputeSharp.Bool2x1;
|
||||
global using bool2x2 = global::ComputeSharp.Bool2x2;
|
||||
global using bool2x3 = global::ComputeSharp.Bool2x3;
|
||||
global using bool2x4 = global::ComputeSharp.Bool2x4;
|
||||
global using bool3x1 = global::ComputeSharp.Bool3x1;
|
||||
global using bool3x2 = global::ComputeSharp.Bool3x2;
|
||||
global using bool3x3 = global::ComputeSharp.Bool3x3;
|
||||
global using bool3x4 = global::ComputeSharp.Bool3x4;
|
||||
global using bool4x1 = global::ComputeSharp.Bool4x1;
|
||||
global using bool4x2 = global::ComputeSharp.Bool4x2;
|
||||
global using bool4x3 = global::ComputeSharp.Bool4x3;
|
||||
global using bool4x4 = global::ComputeSharp.Bool4x4;
|
||||
global using double2 = global::ComputeSharp.Double2;
|
||||
global using double3 = global::ComputeSharp.Double3;
|
||||
global using double4 = global::ComputeSharp.Double4;
|
||||
global using double1x1 = global::ComputeSharp.Double1x1;
|
||||
global using double1x2 = global::ComputeSharp.Double1x2;
|
||||
global using double1x3 = global::ComputeSharp.Double1x3;
|
||||
global using double1x4 = global::ComputeSharp.Double1x4;
|
||||
global using double2x1 = global::ComputeSharp.Double2x1;
|
||||
global using double2x2 = global::ComputeSharp.Double2x2;
|
||||
global using double2x3 = global::ComputeSharp.Double2x3;
|
||||
global using double2x4 = global::ComputeSharp.Double2x4;
|
||||
global using double3x1 = global::ComputeSharp.Double3x1;
|
||||
global using double3x2 = global::ComputeSharp.Double3x2;
|
||||
global using double3x3 = global::ComputeSharp.Double3x3;
|
||||
global using double3x4 = global::ComputeSharp.Double3x4;
|
||||
global using double4x1 = global::ComputeSharp.Double4x1;
|
||||
global using double4x2 = global::ComputeSharp.Double4x2;
|
||||
global using double4x3 = global::ComputeSharp.Double4x3;
|
||||
global using double4x4 = global::ComputeSharp.Double4x4;
|
||||
global using float2 = global::ComputeSharp.Float2;
|
||||
global using float3 = global::ComputeSharp.Float3;
|
||||
global using float4 = global::ComputeSharp.Float4;
|
||||
global using float1x1 = global::ComputeSharp.Float1x1;
|
||||
global using float1x2 = global::ComputeSharp.Float1x2;
|
||||
global using float1x3 = global::ComputeSharp.Float1x3;
|
||||
global using float1x4 = global::ComputeSharp.Float1x4;
|
||||
global using float2x1 = global::ComputeSharp.Float2x1;
|
||||
global using float2x2 = global::ComputeSharp.Float2x2;
|
||||
global using float2x3 = global::ComputeSharp.Float2x3;
|
||||
global using float2x4 = global::ComputeSharp.Float2x4;
|
||||
global using float3x1 = global::ComputeSharp.Float3x1;
|
||||
global using float3x2 = global::ComputeSharp.Float3x2;
|
||||
global using float3x3 = global::ComputeSharp.Float3x3;
|
||||
global using float3x4 = global::ComputeSharp.Float3x4;
|
||||
global using float4x1 = global::ComputeSharp.Float4x1;
|
||||
global using float4x2 = global::ComputeSharp.Float4x2;
|
||||
global using float4x3 = global::ComputeSharp.Float4x3;
|
||||
global using float4x4 = global::ComputeSharp.Float4x4;
|
||||
global using int2 = global::ComputeSharp.Int2;
|
||||
global using int3 = global::ComputeSharp.Int3;
|
||||
global using int4 = global::ComputeSharp.Int4;
|
||||
global using int1x1 = global::ComputeSharp.Int1x1;
|
||||
global using int1x2 = global::ComputeSharp.Int1x2;
|
||||
global using int1x3 = global::ComputeSharp.Int1x3;
|
||||
global using int1x4 = global::ComputeSharp.Int1x4;
|
||||
global using int2x1 = global::ComputeSharp.Int2x1;
|
||||
global using int2x2 = global::ComputeSharp.Int2x2;
|
||||
global using int2x3 = global::ComputeSharp.Int2x3;
|
||||
global using int2x4 = global::ComputeSharp.Int2x4;
|
||||
global using int3x1 = global::ComputeSharp.Int3x1;
|
||||
global using int3x2 = global::ComputeSharp.Int3x2;
|
||||
global using int3x3 = global::ComputeSharp.Int3x3;
|
||||
global using int3x4 = global::ComputeSharp.Int3x4;
|
||||
global using int4x1 = global::ComputeSharp.Int4x1;
|
||||
global using int4x2 = global::ComputeSharp.Int4x2;
|
||||
global using int4x3 = global::ComputeSharp.Int4x3;
|
||||
global using int4x4 = global::ComputeSharp.Int4x4;
|
||||
global using uint2 = global::ComputeSharp.UInt2;
|
||||
global using uint3 = global::ComputeSharp.UInt3;
|
||||
global using uint4 = global::ComputeSharp.UInt4;
|
||||
global using uint1x1 = global::ComputeSharp.UInt1x1;
|
||||
global using uint1x2 = global::ComputeSharp.UInt1x2;
|
||||
global using uint1x3 = global::ComputeSharp.UInt1x3;
|
||||
global using uint1x4 = global::ComputeSharp.UInt1x4;
|
||||
global using uint2x1 = global::ComputeSharp.UInt2x1;
|
||||
global using uint2x2 = global::ComputeSharp.UInt2x2;
|
||||
global using uint2x3 = global::ComputeSharp.UInt2x3;
|
||||
global using uint2x4 = global::ComputeSharp.UInt2x4;
|
||||
global using uint3x1 = global::ComputeSharp.UInt3x1;
|
||||
global using uint3x2 = global::ComputeSharp.UInt3x2;
|
||||
global using uint3x3 = global::ComputeSharp.UInt3x3;
|
||||
global using uint3x4 = global::ComputeSharp.UInt3x4;
|
||||
global using uint4x1 = global::ComputeSharp.UInt4x1;
|
||||
global using uint4x2 = global::ComputeSharp.UInt4x2;
|
||||
global using uint4x3 = global::ComputeSharp.UInt4x3;
|
||||
global using uint4x4 = global::ComputeSharp.UInt4x4;
|
||||
@@ -1,28 +0,0 @@
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace ComputeSharp.SourceGenerators;
|
||||
|
||||
/// <summary>
|
||||
/// A source generator to create global using directives for the HLSL primitive types.
|
||||
/// </summary>
|
||||
[Generator(LanguageNames.CSharp)]
|
||||
public sealed class GlobalUsingDirectivesGenerator : IIncrementalGenerator
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public void Initialize(IncrementalGeneratorInitializationContext context)
|
||||
{
|
||||
context.RegisterPostInitializationOutput(static context =>
|
||||
{
|
||||
const string filename = "ComputeSharp.SourceGenerators.EmbeddedResources.GlobalUsingDirectives.cs";
|
||||
|
||||
using Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(filename);
|
||||
using StreamReader reader = new(stream);
|
||||
|
||||
string globalUsingDirectives = reader.ReadToEnd();
|
||||
|
||||
context.AddSource("GlobalUsingDirectives", globalUsingDirectives);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,493 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
#pragma warning disable CS0809
|
||||
|
||||
namespace System;
|
||||
|
||||
/// <summary>
|
||||
/// A polyfill type that mirrors some methods from <see cref="HashCode"/> on .NET 6.
|
||||
/// </summary>
|
||||
internal struct HashCode
|
||||
{
|
||||
private const uint Prime1 = 2654435761U;
|
||||
private const uint Prime2 = 2246822519U;
|
||||
private const uint Prime3 = 3266489917U;
|
||||
private const uint Prime4 = 668265263U;
|
||||
private const uint Prime5 = 374761393U;
|
||||
|
||||
private static readonly uint seed = GenerateGlobalSeed();
|
||||
|
||||
private uint v1, v2, v3, v4;
|
||||
private uint queue1, queue2, queue3;
|
||||
private uint length;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the default seed.
|
||||
/// </summary>
|
||||
/// <returns>A random seed.</returns>
|
||||
private static unsafe uint GenerateGlobalSeed()
|
||||
{
|
||||
byte[] bytes = new byte[4];
|
||||
|
||||
RandomNumberGenerator.Create().GetBytes(bytes);
|
||||
|
||||
return BitConverter.ToUInt32(bytes, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Combines a value into a hash code.
|
||||
/// </summary>
|
||||
/// <typeparam name="T1">The type of the value to combine into the hash code.</typeparam>
|
||||
/// <param name="value">The value to combine into the hash code.</param>
|
||||
/// <returns>The hash code that represents the value.</returns>
|
||||
public static int Combine<T1>(T1 value)
|
||||
{
|
||||
uint hc1 = (uint)(value?.GetHashCode() ?? 0);
|
||||
uint hash = MixEmptyState();
|
||||
|
||||
hash += 4;
|
||||
hash = QueueRound(hash, hc1);
|
||||
hash = MixFinal(hash);
|
||||
|
||||
return (int)hash;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Combines two values into a hash code.
|
||||
/// </summary>
|
||||
/// <typeparam name="T1">The type of the first value to combine into the hash code.</typeparam>
|
||||
/// <typeparam name="T2">The type of the second value to combine into the hash code.</typeparam>
|
||||
/// <param name="value1">The first value to combine into the hash code.</param>
|
||||
/// <param name="value2">The second value to combine into the hash code.</param>
|
||||
/// <returns>The hash code that represents the values.</returns>
|
||||
public static int Combine<T1, T2>(T1 value1, T2 value2)
|
||||
{
|
||||
uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
|
||||
uint hc2 = (uint)(value2?.GetHashCode() ?? 0);
|
||||
uint hash = MixEmptyState();
|
||||
|
||||
hash += 8;
|
||||
hash = QueueRound(hash, hc1);
|
||||
hash = QueueRound(hash, hc2);
|
||||
hash = MixFinal(hash);
|
||||
|
||||
return (int)hash;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Combines three values into a hash code.
|
||||
/// </summary>
|
||||
/// <typeparam name="T1">The type of the first value to combine into the hash code.</typeparam>
|
||||
/// <typeparam name="T2">The type of the second value to combine into the hash code.</typeparam>
|
||||
/// <typeparam name="T3">The type of the third value to combine into the hash code.</typeparam>
|
||||
/// <param name="value1">The first value to combine into the hash code.</param>
|
||||
/// <param name="value2">The second value to combine into the hash code.</param>
|
||||
/// <param name="value3">The third value to combine into the hash code.</param>
|
||||
/// <returns>The hash code that represents the values.</returns>
|
||||
public static int Combine<T1, T2, T3>(T1 value1, T2 value2, T3 value3)
|
||||
{
|
||||
uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
|
||||
uint hc2 = (uint)(value2?.GetHashCode() ?? 0);
|
||||
uint hc3 = (uint)(value3?.GetHashCode() ?? 0);
|
||||
uint hash = MixEmptyState();
|
||||
|
||||
hash += 12;
|
||||
hash = QueueRound(hash, hc1);
|
||||
hash = QueueRound(hash, hc2);
|
||||
hash = QueueRound(hash, hc3);
|
||||
hash = MixFinal(hash);
|
||||
|
||||
return (int)hash;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Combines four values into a hash code.
|
||||
/// </summary>
|
||||
/// <typeparam name="T1">The type of the first value to combine into the hash code.</typeparam>
|
||||
/// <typeparam name="T2">The type of the second value to combine into the hash code.</typeparam>
|
||||
/// <typeparam name="T3">The type of the third value to combine into the hash code.</typeparam>
|
||||
/// <typeparam name="T4">The type of the fourth value to combine into the hash code.</typeparam>
|
||||
/// <param name="value1">The first value to combine into the hash code.</param>
|
||||
/// <param name="value2">The second value to combine into the hash code.</param>
|
||||
/// <param name="value3">The third value to combine into the hash code.</param>
|
||||
/// <param name="value4">The fourth value to combine into the hash code.</param>
|
||||
/// <returns>The hash code that represents the values.</returns>
|
||||
public static int Combine<T1, T2, T3, T4>(T1 value1, T2 value2, T3 value3, T4 value4)
|
||||
{
|
||||
uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
|
||||
uint hc2 = (uint)(value2?.GetHashCode() ?? 0);
|
||||
uint hc3 = (uint)(value3?.GetHashCode() ?? 0);
|
||||
uint hc4 = (uint)(value4?.GetHashCode() ?? 0);
|
||||
|
||||
Initialize(out uint v1, out uint v2, out uint v3, out uint v4);
|
||||
|
||||
v1 = Round(v1, hc1);
|
||||
v2 = Round(v2, hc2);
|
||||
v3 = Round(v3, hc3);
|
||||
v4 = Round(v4, hc4);
|
||||
|
||||
uint hash = MixState(v1, v2, v3, v4);
|
||||
|
||||
hash += 16;
|
||||
hash = MixFinal(hash);
|
||||
|
||||
return (int)hash;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Combines five values into a hash code.
|
||||
/// </summary>
|
||||
/// <typeparam name="T1">The type of the first value to combine into the hash code.</typeparam>
|
||||
/// <typeparam name="T2">The type of the second value to combine into the hash code.</typeparam>
|
||||
/// <typeparam name="T3">The type of the third value to combine into the hash code.</typeparam>
|
||||
/// <typeparam name="T4">The type of the fourth value to combine into the hash code.</typeparam>
|
||||
/// <typeparam name="T5">The type of the fifth value to combine into the hash code.</typeparam>
|
||||
/// <param name="value1">The first value to combine into the hash code.</param>
|
||||
/// <param name="value2">The second value to combine into the hash code.</param>
|
||||
/// <param name="value3">The third value to combine into the hash code.</param>
|
||||
/// <param name="value4">The fourth value to combine into the hash code.</param>
|
||||
/// <param name="value5">The fifth value to combine into the hash code.</param>
|
||||
/// <returns>The hash code that represents the values.</returns>
|
||||
public static int Combine<T1, T2, T3, T4, T5>(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5)
|
||||
{
|
||||
uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
|
||||
uint hc2 = (uint)(value2?.GetHashCode() ?? 0);
|
||||
uint hc3 = (uint)(value3?.GetHashCode() ?? 0);
|
||||
uint hc4 = (uint)(value4?.GetHashCode() ?? 0);
|
||||
uint hc5 = (uint)(value5?.GetHashCode() ?? 0);
|
||||
|
||||
Initialize(out uint v1, out uint v2, out uint v3, out uint v4);
|
||||
|
||||
v1 = Round(v1, hc1);
|
||||
v2 = Round(v2, hc2);
|
||||
v3 = Round(v3, hc3);
|
||||
v4 = Round(v4, hc4);
|
||||
|
||||
uint hash = MixState(v1, v2, v3, v4);
|
||||
|
||||
hash += 20;
|
||||
hash = QueueRound(hash, hc5);
|
||||
hash = MixFinal(hash);
|
||||
|
||||
return (int)hash;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Combines six values into a hash code.
|
||||
/// </summary>
|
||||
/// <typeparam name="T1">The type of the first value to combine into the hash code.</typeparam>
|
||||
/// <typeparam name="T2">The type of the second value to combine into the hash code.</typeparam>
|
||||
/// <typeparam name="T3">The type of the third value to combine into the hash code.</typeparam>
|
||||
/// <typeparam name="T4">The type of the fourth value to combine into the hash code.</typeparam>
|
||||
/// <typeparam name="T5">The type of the fifth value to combine into the hash code.</typeparam>
|
||||
/// <typeparam name="T6">The type of the sixth value to combine into the hash code.</typeparam>
|
||||
/// <param name="value1">The first value to combine into the hash code.</param>
|
||||
/// <param name="value2">The second value to combine into the hash code.</param>
|
||||
/// <param name="value3">The third value to combine into the hash code.</param>
|
||||
/// <param name="value4">The fourth value to combine into the hash code.</param>
|
||||
/// <param name="value5">The fifth value to combine into the hash code.</param>
|
||||
/// <param name="value6">The sixth value to combine into the hash code.</param>
|
||||
/// <returns>The hash code that represents the values.</returns>
|
||||
public static int Combine<T1, T2, T3, T4, T5, T6>(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6)
|
||||
{
|
||||
uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
|
||||
uint hc2 = (uint)(value2?.GetHashCode() ?? 0);
|
||||
uint hc3 = (uint)(value3?.GetHashCode() ?? 0);
|
||||
uint hc4 = (uint)(value4?.GetHashCode() ?? 0);
|
||||
uint hc5 = (uint)(value5?.GetHashCode() ?? 0);
|
||||
uint hc6 = (uint)(value6?.GetHashCode() ?? 0);
|
||||
|
||||
Initialize(out uint v1, out uint v2, out uint v3, out uint v4);
|
||||
|
||||
v1 = Round(v1, hc1);
|
||||
v2 = Round(v2, hc2);
|
||||
v3 = Round(v3, hc3);
|
||||
v4 = Round(v4, hc4);
|
||||
|
||||
uint hash = MixState(v1, v2, v3, v4);
|
||||
|
||||
hash += 24;
|
||||
hash = QueueRound(hash, hc5);
|
||||
hash = QueueRound(hash, hc6);
|
||||
hash = MixFinal(hash);
|
||||
|
||||
return (int)hash;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Combines seven values into a hash code.
|
||||
/// </summary>
|
||||
/// <typeparam name="T1">The type of the first value to combine into the hash code.</typeparam>
|
||||
/// <typeparam name="T2">The type of the second value to combine into the hash code.</typeparam>
|
||||
/// <typeparam name="T3">The type of the third value to combine into the hash code.</typeparam>
|
||||
/// <typeparam name="T4">The type of the fourth value to combine into the hash code.</typeparam>
|
||||
/// <typeparam name="T5">The type of the fifth value to combine into the hash code.</typeparam>
|
||||
/// <typeparam name="T6">The type of the sixth value to combine into the hash code.</typeparam>
|
||||
/// <typeparam name="T7">The type of the seventh value to combine into the hash code.</typeparam>
|
||||
/// <param name="value1">The first value to combine into the hash code.</param>
|
||||
/// <param name="value2">The second value to combine into the hash code.</param>
|
||||
/// <param name="value3">The third value to combine into the hash code.</param>
|
||||
/// <param name="value4">The fourth value to combine into the hash code.</param>
|
||||
/// <param name="value5">The fifth value to combine into the hash code.</param>
|
||||
/// <param name="value6">The sixth value to combine into the hash code.</param>
|
||||
/// <param name="value7">The seventh value to combine into the hash code.</param>
|
||||
/// <returns>The hash code that represents the values.</returns>
|
||||
public static int Combine<T1, T2, T3, T4, T5, T6, T7>(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7)
|
||||
{
|
||||
uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
|
||||
uint hc2 = (uint)(value2?.GetHashCode() ?? 0);
|
||||
uint hc3 = (uint)(value3?.GetHashCode() ?? 0);
|
||||
uint hc4 = (uint)(value4?.GetHashCode() ?? 0);
|
||||
uint hc5 = (uint)(value5?.GetHashCode() ?? 0);
|
||||
uint hc6 = (uint)(value6?.GetHashCode() ?? 0);
|
||||
uint hc7 = (uint)(value7?.GetHashCode() ?? 0);
|
||||
|
||||
Initialize(out uint v1, out uint v2, out uint v3, out uint v4);
|
||||
|
||||
v1 = Round(v1, hc1);
|
||||
v2 = Round(v2, hc2);
|
||||
v3 = Round(v3, hc3);
|
||||
v4 = Round(v4, hc4);
|
||||
|
||||
uint hash = MixState(v1, v2, v3, v4);
|
||||
|
||||
hash += 28;
|
||||
hash = QueueRound(hash, hc5);
|
||||
hash = QueueRound(hash, hc6);
|
||||
hash = QueueRound(hash, hc7);
|
||||
hash = MixFinal(hash);
|
||||
|
||||
return (int)hash;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Combines eight values into a hash code.
|
||||
/// </summary>
|
||||
/// <typeparam name="T1">The type of the first value to combine into the hash code.</typeparam>
|
||||
/// <typeparam name="T2">The type of the second value to combine into the hash code.</typeparam>
|
||||
/// <typeparam name="T3">The type of the third value to combine into the hash code.</typeparam>
|
||||
/// <typeparam name="T4">The type of the fourth value to combine into the hash code.</typeparam>
|
||||
/// <typeparam name="T5">The type of the fifth value to combine into the hash code.</typeparam>
|
||||
/// <typeparam name="T6">The type of the sixth value to combine into the hash code.</typeparam>
|
||||
/// <typeparam name="T7">The type of the seventh value to combine into the hash code.</typeparam>
|
||||
/// <typeparam name="T8">The type of the eigth value to combine into the hash code.</typeparam>
|
||||
/// <param name="value1">The first value to combine into the hash code.</param>
|
||||
/// <param name="value2">The second value to combine into the hash code.</param>
|
||||
/// <param name="value3">The third value to combine into the hash code.</param>
|
||||
/// <param name="value4">The fourth value to combine into the hash code.</param>
|
||||
/// <param name="value5">The fifth value to combine into the hash code.</param>
|
||||
/// <param name="value6">The sixth value to combine into the hash code.</param>
|
||||
/// <param name="value7">The seventh value to combine into the hash code.</param>
|
||||
/// <param name="value8">The eigth value to combine into the hash code.</param>
|
||||
/// <returns>The hash code that represents the values.</returns>
|
||||
public static int Combine<T1, T2, T3, T4, T5, T6, T7, T8>(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7, T8 value8)
|
||||
{
|
||||
uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
|
||||
uint hc2 = (uint)(value2?.GetHashCode() ?? 0);
|
||||
uint hc3 = (uint)(value3?.GetHashCode() ?? 0);
|
||||
uint hc4 = (uint)(value4?.GetHashCode() ?? 0);
|
||||
uint hc5 = (uint)(value5?.GetHashCode() ?? 0);
|
||||
uint hc6 = (uint)(value6?.GetHashCode() ?? 0);
|
||||
uint hc7 = (uint)(value7?.GetHashCode() ?? 0);
|
||||
uint hc8 = (uint)(value8?.GetHashCode() ?? 0);
|
||||
|
||||
Initialize(out uint v1, out uint v2, out uint v3, out uint v4);
|
||||
|
||||
v1 = Round(v1, hc1);
|
||||
v2 = Round(v2, hc2);
|
||||
v3 = Round(v3, hc3);
|
||||
v4 = Round(v4, hc4);
|
||||
|
||||
v1 = Round(v1, hc5);
|
||||
v2 = Round(v2, hc6);
|
||||
v3 = Round(v3, hc7);
|
||||
v4 = Round(v4, hc8);
|
||||
|
||||
uint hash = MixState(v1, v2, v3, v4);
|
||||
|
||||
hash += 32;
|
||||
hash = MixFinal(hash);
|
||||
|
||||
return (int)hash;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a single value to the current hash.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the value to add into the hash code.</typeparam>
|
||||
/// <param name="value">The value to add into the hash code.</param>
|
||||
public void Add<T>(T value)
|
||||
{
|
||||
Add(value?.GetHashCode() ?? 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a single value to the current hash.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the value to add into the hash code.</typeparam>
|
||||
/// <param name="value">The value to add into the hash code.</param>
|
||||
/// <param name="comparer">The <see cref="IEqualityComparer{T}"/> instance to use.</param>
|
||||
public void Add<T>(T value, IEqualityComparer<T>? comparer)
|
||||
{
|
||||
Add(value is null ? 0 : (comparer?.GetHashCode(value) ?? value.GetHashCode()));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a span of bytes to the hash code.
|
||||
/// </summary>
|
||||
/// <param name="value">The span.</param>
|
||||
public void AddBytes(ReadOnlySpan<byte> value)
|
||||
{
|
||||
ref byte pos = ref MemoryMarshal.GetReference(value);
|
||||
ref byte end = ref Unsafe.Add(ref pos, value.Length);
|
||||
|
||||
while ((nint)Unsafe.ByteOffset(ref pos, ref end) >= sizeof(int))
|
||||
{
|
||||
Add(Unsafe.ReadUnaligned<int>(ref pos));
|
||||
pos = ref Unsafe.Add(ref pos, sizeof(int));
|
||||
}
|
||||
|
||||
while (Unsafe.IsAddressLessThan(ref pos, ref end))
|
||||
{
|
||||
Add((int)pos);
|
||||
pos = ref Unsafe.Add(ref pos, 1);
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void Initialize(out uint v1, out uint v2, out uint v3, out uint v4)
|
||||
{
|
||||
v1 = seed + Prime1 + Prime2;
|
||||
v2 = seed + Prime2;
|
||||
v3 = seed;
|
||||
v4 = seed - Prime1;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static uint Round(uint hash, uint input)
|
||||
{
|
||||
return RotateLeft(hash + input * Prime2, 13) * Prime1;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static uint QueueRound(uint hash, uint queuedValue)
|
||||
{
|
||||
return RotateLeft(hash + queuedValue * Prime3, 17) * Prime4;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static uint MixState(uint v1, uint v2, uint v3, uint v4)
|
||||
{
|
||||
return RotateLeft(v1, 1) + RotateLeft(v2, 7) + RotateLeft(v3, 12) + RotateLeft(v4, 18);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static uint MixEmptyState()
|
||||
{
|
||||
return seed + Prime5;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static uint MixFinal(uint hash)
|
||||
{
|
||||
hash ^= hash >> 15;
|
||||
hash *= Prime2;
|
||||
hash ^= hash >> 13;
|
||||
hash *= Prime3;
|
||||
hash ^= hash >> 16;
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
private void Add(int value)
|
||||
{
|
||||
uint val = (uint)value;
|
||||
uint previousLength = length++;
|
||||
uint position = previousLength % 4;
|
||||
|
||||
if (position == 0)
|
||||
{
|
||||
queue1 = val;
|
||||
}
|
||||
else if (position == 1)
|
||||
{
|
||||
queue2 = val;
|
||||
}
|
||||
else if (position == 2)
|
||||
{
|
||||
queue3 = val;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (previousLength == 3)
|
||||
{
|
||||
Initialize(out v1, out v2, out v3, out v4);
|
||||
}
|
||||
|
||||
v1 = Round(v1, queue1);
|
||||
v2 = Round(v2, queue2);
|
||||
v3 = Round(v3, queue3);
|
||||
v4 = Round(v4, val);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the resulting hashcode from the current instance.
|
||||
/// </summary>
|
||||
/// <returns>The resulting hashcode from the current instance.</returns>
|
||||
public int ToHashCode()
|
||||
{
|
||||
uint length = this.length;
|
||||
uint position = length % 4;
|
||||
uint hash = length < 4 ? MixEmptyState() : MixState(v1, v2, v3, v4);
|
||||
|
||||
hash += length * 4;
|
||||
|
||||
if (position > 0)
|
||||
{
|
||||
hash = QueueRound(hash, queue1);
|
||||
|
||||
if (position > 1)
|
||||
{
|
||||
hash = QueueRound(hash, queue2);
|
||||
|
||||
if (position > 2)
|
||||
{
|
||||
hash = QueueRound(hash, queue3);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
hash = MixFinal(hash);
|
||||
|
||||
return (int)hash;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
[Obsolete("HashCode is a mutable struct and should not be compared with other HashCodes. Use ToHashCode to retrieve the computed hash code.", error: true)]
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
public override int GetHashCode() => throw new NotSupportedException();
|
||||
|
||||
/// <inheritdoc/>
|
||||
[Obsolete("HashCode is a mutable struct and should not be compared with other HashCodes.", error: true)]
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
public override bool Equals(object? obj) => throw new NotSupportedException();
|
||||
|
||||
/// <summary>
|
||||
/// Rotates the specified value left by the specified number of bits.
|
||||
/// Similar in behavior to the x86 instruction ROL.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to rotate.</param>
|
||||
/// <param name="offset">The number of bits to rotate by.
|
||||
/// Any value outside the range [0..31] is treated as congruent mod 32.</param>
|
||||
/// <returns>The rotated value.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static uint RotateLeft(uint value, int offset)
|
||||
{
|
||||
return (value << offset) | (value >> (32 - offset));
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする