Move common extensions to shared generation project
このコミットが含まれているのは:
1 親
ae717681c4
コミット
9a51331910
53ファイル変更
+90
-650
検索条件に一致するファイルはありません
@@ -1,8 +1,8 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using ComputeSharp.Core.SourceGenerators.Extensions;
|
||||
using ComputeSharp.Core.SourceGenerators.Models;
|
||||
using ComputeSharp.SourceGeneration.Extensions;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
|
||||
@@ -74,6 +74,9 @@
|
||||
<EmbeddedResource Include="EmbeddedResources\GlobalUsingDirectives.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<!-- Shared project with common helpers -->
|
||||
<Import Project="..\ComputeSharp.SourceGeneration\ComputeSharp.SourceGeneration.projitems" Label="Shared" />
|
||||
|
||||
<!-- Shared project with .NET Standard 2.0 polyfills -->
|
||||
<Import Project="..\ComputeSharp.Core.NetStandard\ComputeSharp.Core.NetStandard.projitems" Label="Shared" />
|
||||
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace ComputeSharp.Core.SourceGenerators.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="HashCode"/>.
|
||||
/// </summary>
|
||||
internal static class HashCodeExtensions
|
||||
{
|
||||
/// <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="items">The input items to hash.</param>
|
||||
public static void AddRange<T>(this ref HashCode hashCode, ImmutableArray<T> items)
|
||||
{
|
||||
foreach (T item in items)
|
||||
{
|
||||
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(item, comparer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace ComputeSharp.Core.SourceGenerators.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="IEqualityComparer{T}"/>.
|
||||
/// </summary>
|
||||
internal static class IEqualityComparerExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets an <see cref="IEqualityComparer{T}"/> for an <see cref="ImmutableArray{T}"/> sequence.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to compare.</typeparam>
|
||||
/// <param name="comparer">The compare for individual <typeparamref name="T"/> items.</param>
|
||||
public static IEqualityComparer<ImmutableArray<T>> ForImmutableArray<T>(this IEqualityComparer<T> comparer)
|
||||
{
|
||||
return new Comparer<T>(comparer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An <see cref="IEqualityComparer{T}"/> implementation for an <see cref="ImmutableArray{T}"/> value.
|
||||
/// </summary>
|
||||
public sealed class Comparer<T> : IEqualityComparer<ImmutableArray<T>>
|
||||
{
|
||||
/// <summary>
|
||||
/// The <typeparamref name="T"/> comparer.
|
||||
/// </summary>
|
||||
private readonly IEqualityComparer<T> comparer;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Comparer{T}"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="comparer">The <typeparamref name="T"/> comparer.</param>
|
||||
public Comparer(IEqualityComparer<T> comparer)
|
||||
{
|
||||
this.comparer = comparer;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool Equals(ImmutableArray<T> x, ImmutableArray<T> y)
|
||||
{
|
||||
return
|
||||
x == y ||
|
||||
x.SequenceEqual(y, this.comparer);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int GetHashCode(ImmutableArray<T> obj)
|
||||
{
|
||||
HashCode hashCode = default;
|
||||
|
||||
hashCode.AddRange(obj, this.comparer);
|
||||
|
||||
return hashCode.ToHashCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace ComputeSharp.Core.SourceGenerators.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="ISymbol"/> types.
|
||||
/// </summary>
|
||||
internal static class ISymbolExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a valid filename for a target symbol and generator type.
|
||||
/// </summary>
|
||||
/// <param name="symbol">The symbol being processed.</param>
|
||||
/// <returns>A filename in the form "<SYMBOL_FULLNAME>"</returns>
|
||||
public static string GetGeneratedFileName(this INamedTypeSymbol symbol)
|
||||
{
|
||||
return symbol.GetFullMetadataName().Replace('`', '-').Replace('+', '.');
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the full metadata name for a given <see cref="ITypeSymbol"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="symbol">The input <see cref="ITypeSymbol"/> instance.</param>
|
||||
/// <returns>The full metadata name for <paramref name="symbol"/>.</returns>
|
||||
private static string GetFullMetadataName(this ITypeSymbol symbol)
|
||||
{
|
||||
static StringBuilder BuildFrom(ISymbol? symbol, StringBuilder builder)
|
||||
{
|
||||
return symbol switch
|
||||
{
|
||||
INamespaceSymbol ns when ns.IsGlobalNamespace => builder,
|
||||
INamespaceSymbol ns when ns.ContainingNamespace is { IsGlobalNamespace: false }
|
||||
=> BuildFrom(ns.ContainingNamespace, builder.Insert(0, $".{ns.MetadataName}")),
|
||||
ITypeSymbol ts when ts.ContainingType is ISymbol containingType
|
||||
=> BuildFrom(containingType, builder.Insert(0, $"+{ts.MetadataName}")),
|
||||
ITypeSymbol ts when ts.ContainingNamespace is ISymbol containingNamespace and not INamespaceSymbol { IsGlobalNamespace: true }
|
||||
=> BuildFrom(containingNamespace, builder.Insert(0, $".{ts.MetadataName}")),
|
||||
ISymbol => BuildFrom(symbol.ContainingSymbol, builder.Insert(0, symbol.MetadataName)),
|
||||
_ => builder
|
||||
};
|
||||
}
|
||||
|
||||
return BuildFrom(symbol, new StringBuilder(256)).ToString();
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace ComputeSharp.Core.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;
|
||||
}
|
||||
}
|
||||
-71
@@ -1,71 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace ComputeSharp.Core.SourceGenerators.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="IncrementalValuesProvider{TValues}"/>.
|
||||
/// </summary>
|
||||
internal static class IncrementalValuesProviderExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="IncrementalValuesProvider{TValues}"/> instance with a gven pair of comparers.
|
||||
/// </summary>
|
||||
/// <typeparam name="TLeft">The type of left items in each tuple.</typeparam>
|
||||
/// <typeparam name="TRight">The type of right items in each tuple.</typeparam>
|
||||
/// <param name="source">The input <see cref="IncrementalValuesProvider{TValues}"/> instance.</param>
|
||||
/// <param name="comparerLeft">An <see cref="IEqualityComparer{T}"/> instance for <typeparamref name="TLeft"/> items.</param>
|
||||
/// <param name="comparerRight">An <see cref="IEqualityComparer{T}"/> instance for <typeparamref name="TRight"/> items.</param>
|
||||
/// <returns>An <see cref="IncrementalValuesProvider{TValues}"/> with the specified comparers applied to each item.</returns>
|
||||
public static IncrementalValuesProvider<(TLeft Left, TRight Right)> WithComparers<TLeft, TRight>(
|
||||
this IncrementalValuesProvider<(TLeft Left, TRight Right)> source,
|
||||
IEqualityComparer<TLeft> comparerLeft,
|
||||
IEqualityComparer<TRight> comparerRight)
|
||||
{
|
||||
return source.WithComparer(new Comparer<TLeft, TRight>(comparerLeft, comparerRight));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An <see cref="IEqualityComparer{T}"/> implementation for a value tuple.
|
||||
/// </summary>
|
||||
public sealed class Comparer<TLeft, TRight> : IEqualityComparer<(TLeft Left, TRight Right)>
|
||||
{
|
||||
/// <summary>
|
||||
/// The <typeparamref name="TLeft"/> comparer.
|
||||
/// </summary>
|
||||
private readonly IEqualityComparer<TLeft> comparerLeft;
|
||||
|
||||
/// <summary>
|
||||
/// The <typeparamref name="TRight"/> comparer.
|
||||
/// </summary>
|
||||
private readonly IEqualityComparer<TRight> comparerRight;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Comparer{TLeft, TRight}"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="comparerLeft">The <typeparamref name="TLeft"/> comparer.</param>
|
||||
/// <param name="comparerRight">The <typeparamref name="TRight"/> comparer.</param>
|
||||
public Comparer(IEqualityComparer<TLeft> comparerLeft, IEqualityComparer<TRight> comparerRight)
|
||||
{
|
||||
this.comparerLeft = comparerLeft;
|
||||
this.comparerRight = comparerRight;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool Equals((TLeft Left, TRight Right) x, (TLeft Left, TRight Right) y)
|
||||
{
|
||||
return
|
||||
this.comparerLeft.Equals(x.Left, y.Left) &&
|
||||
this.comparerRight.Equals(x.Right, y.Right);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int GetHashCode((TLeft Left, TRight Right) obj)
|
||||
{
|
||||
return HashCode.Combine(
|
||||
this.comparerLeft.GetHashCode(obj.Left),
|
||||
this.comparerRight.GetHashCode(obj.Right));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using ComputeSharp.Core.SourceGenerators.Extensions;
|
||||
using ComputeSharp.SourceGeneration.Extensions;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using static Microsoft.CodeAnalysis.SymbolDisplayTypeQualificationStyle;
|
||||
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする