// ReSharper disable InconsistentNaming using System; using System.Diagnostics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace Standart.Hash.xxHash { public static partial class xxHash128 { /// /// Compute xxHash for the data byte array /// /// The source of data /// The length of the data for hashing /// The seed number /// hash public static unsafe uint128 ComputeHash(byte[] data, int length, ulong seed = 0) { Debug.Assert(data != null); Debug.Assert(length >= 0); Debug.Assert(length <= data.Length); fixed (byte* ptr = &data[0]) { return UnsafeComputeHash(ptr, length, seed); } } /// /// Compute xxHash for the data byte span /// /// The source of data /// The length of the data for hashing /// The seed number /// hash public static unsafe uint128 ComputeHash(Span data, int length, ulong seed = 0) { Debug.Assert(data != null); Debug.Assert(length >= 0); Debug.Assert(length <= data.Length); fixed (byte* ptr = &data[0]) { return UnsafeComputeHash(ptr, length, seed); } } /// /// Compute xxHash for the data byte span /// /// The source of data /// The length of the data for hashing /// The seed number /// hash public static unsafe uint128 ComputeHash(ReadOnlySpan data, int length, ulong seed = 0) { Debug.Assert(data != null); Debug.Assert(length >= 0); Debug.Assert(length <= data.Length); fixed (byte* ptr = &data[0]) { return UnsafeComputeHash(ptr, length, seed); } } /// /// Compute xxHash for the string /// /// The source of data /// The seed number /// hash public static unsafe uint128 ComputeHash(string str, ulong seed = 0) { Debug.Assert(str != null); fixed (char* c = str) { byte* ptr = (byte*) c; int length = str.Length; return UnsafeComputeHash(ptr, length, seed); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static unsafe uint128 UnsafeComputeHash(byte* input, int len, ulong seed) { fixed (byte* secret = &XXH3_SECRET[0]) { return XXH3_128bits_internal(input, len, seed, secret, XXH3_SECRET_DEFAULT_SIZE); } } } [StructLayout(LayoutKind.Sequential)] public struct uint128 { public ulong low64; public ulong high64; } }