diff --git a/src/ComputeSharp.SourceGenerators/Extensions/HashCodeExtensions.cs b/src/ComputeSharp.SourceGenerators/Extensions/HashCodeExtensions.cs
index 31c57b6c..7d486566 100644
--- a/src/ComputeSharp.SourceGenerators/Extensions/HashCodeExtensions.cs
+++ b/src/ComputeSharp.SourceGenerators/Extensions/HashCodeExtensions.cs
@@ -34,7 +34,7 @@ internal static class HashCodeExtensions
{
foreach (T item in items)
{
- hashCode.Add(comparer.GetHashCode(item));
+ hashCode.Add(item, comparer);
}
}
}
diff --git a/src/ComputeSharp.SourceGenerators/Helpers/HashCode.cs b/src/ComputeSharp.SourceGenerators/Helpers/HashCode.cs
index 19030b40..94ea71f0 100644
--- a/src/ComputeSharp.SourceGenerators/Helpers/HashCode.cs
+++ b/src/ComputeSharp.SourceGenerators/Helpers/HashCode.cs
@@ -1,6 +1,5 @@
using System.Collections.Generic;
using System.ComponentModel;
-using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
@@ -30,7 +29,6 @@ internal struct HashCode
/// Initializes the default seed.
///
/// A random seed.
- [Pure]
private static unsafe uint GenerateGlobalSeed()
{
byte[] bytes = new byte[4];
@@ -40,9 +38,15 @@ internal struct HashCode
return BitConverter.ToUInt32(bytes, 0);
}
- public static int Combine(T1 value1)
+ ///
+ /// Combines a value into a hash code.
+ ///
+ /// The type of the value to combine into the hash code.
+ /// The value to combine into the hash code.
+ /// The hash code that represents the value.
+ public static int Combine(T1 value)
{
- uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
+ uint hc1 = (uint)(value?.GetHashCode() ?? 0);
uint hash = MixEmptyState();
hash += 4;
@@ -52,6 +56,14 @@ internal struct HashCode
return (int)hash;
}
+ ///
+ /// Combines two values into a hash code.
+ ///
+ /// The type of the first value to combine into the hash code.
+ /// The type of the second value to combine into the hash code.
+ /// The first value to combine into the hash code.
+ /// The second value to combine into the hash code.
+ /// The hash code that represents the values.
public static int Combine(T1 value1, T2 value2)
{
uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
@@ -66,6 +78,16 @@ internal struct HashCode
return (int)hash;
}
+ ///
+ /// Combines three values into a hash code.
+ ///
+ /// The type of the first value to combine into the hash code.
+ /// The type of the second value to combine into the hash code.
+ /// The type of the third value to combine into the hash code.
+ /// The first value to combine into the hash code.
+ /// The second value to combine into the hash code.
+ /// The third value to combine into the hash code.
+ /// The hash code that represents the values.
public static int Combine(T1 value1, T2 value2, T3 value3)
{
uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
@@ -82,6 +104,18 @@ internal struct HashCode
return (int)hash;
}
+ ///
+ /// Combines four values into a hash code.
+ ///
+ /// The type of the first value to combine into the hash code.
+ /// The type of the second value to combine into the hash code.
+ /// The type of the third value to combine into the hash code.
+ /// The type of the fourth value to combine into the hash code.
+ /// The first value to combine into the hash code.
+ /// The second value to combine into the hash code.
+ /// The third value to combine into the hash code.
+ /// The fourth value to combine into the hash code.
+ /// The hash code that represents the values.
public static int Combine(T1 value1, T2 value2, T3 value3, T4 value4)
{
uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
@@ -104,6 +138,20 @@ internal struct HashCode
return (int)hash;
}
+ ///
+ /// Combines five values into a hash code.
+ ///
+ /// The type of the first value to combine into the hash code.
+ /// The type of the second value to combine into the hash code.
+ /// The type of the third value to combine into the hash code.
+ /// The type of the fourth value to combine into the hash code.
+ /// The type of the fifth value to combine into the hash code.
+ /// The first value to combine into the hash code.
+ /// The second value to combine into the hash code.
+ /// The third value to combine into the hash code.
+ /// The fourth value to combine into the hash code.
+ /// The fifth value to combine into the hash code.
+ /// The hash code that represents the values.
public static int Combine(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5)
{
uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
@@ -128,6 +176,22 @@ internal struct HashCode
return (int)hash;
}
+ ///
+ /// Combines six values into a hash code.
+ ///
+ /// The type of the first value to combine into the hash code.
+ /// The type of the second value to combine into the hash code.
+ /// The type of the third value to combine into the hash code.
+ /// The type of the fourth value to combine into the hash code.
+ /// The type of the fifth value to combine into the hash code.
+ /// The type of the sixth value to combine into the hash code.
+ /// The first value to combine into the hash code.
+ /// The second value to combine into the hash code.
+ /// The third value to combine into the hash code.
+ /// The fourth value to combine into the hash code.
+ /// The fifth value to combine into the hash code.
+ /// The sixth value to combine into the hash code.
+ /// The hash code that represents the values.
public static int Combine(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6)
{
uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
@@ -154,6 +218,24 @@ internal struct HashCode
return (int)hash;
}
+ ///
+ /// Combines seven values into a hash code.
+ ///
+ /// The type of the first value to combine into the hash code.
+ /// The type of the second value to combine into the hash code.
+ /// The type of the third value to combine into the hash code.
+ /// The type of the fourth value to combine into the hash code.
+ /// The type of the fifth value to combine into the hash code.
+ /// The type of the sixth value to combine into the hash code.
+ /// The type of the seventh value to combine into the hash code.
+ /// The first value to combine into the hash code.
+ /// The second value to combine into the hash code.
+ /// The third value to combine into the hash code.
+ /// The fourth value to combine into the hash code.
+ /// The fifth value to combine into the hash code.
+ /// The sixth value to combine into the hash code.
+ /// The seventh value to combine into the hash code.
+ /// The hash code that represents the values.
public static int Combine(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7)
{
uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
@@ -182,6 +264,26 @@ internal struct HashCode
return (int)hash;
}
+ ///
+ /// Combines eight values into a hash code.
+ ///
+ /// The type of the first value to combine into the hash code.
+ /// The type of the second value to combine into the hash code.
+ /// The type of the third value to combine into the hash code.
+ /// The type of the fourth value to combine into the hash code.
+ /// The type of the fifth value to combine into the hash code.
+ /// The type of the sixth value to combine into the hash code.
+ /// The type of the seventh value to combine into the hash code.
+ /// The type of the eigth value to combine into the hash code.
+ /// The first value to combine into the hash code.
+ /// The second value to combine into the hash code.
+ /// The third value to combine into the hash code.
+ /// The fourth value to combine into the hash code.
+ /// The fifth value to combine into the hash code.
+ /// The sixth value to combine into the hash code.
+ /// The seventh value to combine into the hash code.
+ /// The eigth value to combine into the hash code.
+ /// The hash code that represents the values.
public static int Combine(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7, T8 value8)
{
uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
@@ -213,6 +315,49 @@ internal struct HashCode
return (int)hash;
}
+ ///
+ /// Adds a single value to the current hash.
+ ///
+ /// The type of the value to add into the hash code.
+ /// The value to add into the hash code.
+ public void Add(T value)
+ {
+ Add(value?.GetHashCode() ?? 0);
+ }
+
+ ///
+ /// Adds a single value to the current hash.
+ ///
+ /// The type of the value to add into the hash code.
+ /// The value to add into the hash code.
+ /// The instance to use.
+ public void Add(T value, IEqualityComparer? comparer)
+ {
+ Add(value is null ? 0 : (comparer?.GetHashCode(value) ?? value.GetHashCode()));
+ }
+
+ ///
+ /// Adds a span of bytes to the hash code.
+ ///
+ /// The span.
+ public void AddBytes(ReadOnlySpan 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(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)
{
@@ -240,6 +385,7 @@ internal struct HashCode
return RotateLeft(v1, 1) + RotateLeft(v2, 7) + RotateLeft(v3, 12) + RotateLeft(v4, 18);
}
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint MixEmptyState()
{
return seed + Prime5;
@@ -257,40 +403,6 @@ internal struct HashCode
return hash;
}
- public void Add(T value)
- {
- Add(value?.GetHashCode() ?? 0);
- }
-
- public void Add(T value, IEqualityComparer? comparer)
- {
- Add(value is null ? 0 : (comparer?.GetHashCode(value) ?? value.GetHashCode()));
- }
-
- /// Adds a span of bytes to the hash code.
- /// The span.
- ///
- /// This method does not guarantee that the result of adding a span of bytes will match
- /// the result of adding the same bytes individually.
- ///
- public void AddBytes(ReadOnlySpan 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(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);
- }
- }
-
private void Add(int value)
{
uint val = (uint)value;
@@ -323,6 +435,10 @@ internal struct HashCode
}
}
+ ///
+ /// Gets the resulting hashcode from the current instance.
+ ///
+ /// The resulting hashcode from the current instance.
public int ToHashCode()
{
uint length = this.length;