diff --git a/app/MindWork AI Studio.sln.DotSettings b/app/MindWork AI Studio.sln.DotSettings index 44079898..975924f4 100644 --- a/app/MindWork AI Studio.sln.DotSettings +++ b/app/MindWork AI Studio.sln.DotSettings @@ -2,6 +2,7 @@ AI EDI ERI + FNV GWDG LLM LM diff --git a/app/MindWork AI Studio/Tools/FNVHash.cs b/app/MindWork AI Studio/Tools/FNVHash.cs new file mode 100644 index 00000000..cc47645e --- /dev/null +++ b/app/MindWork AI Studio/Tools/FNVHash.cs @@ -0,0 +1,62 @@ +// ReSharper disable MemberCanBePrivate.Global +namespace AIStudio.Tools; + +/// +/// Implements the Fowler–Noll–Vo hash function for 32-bit and 64-bit hashes. +/// +public static class FNVHash +{ + private const uint FNV_OFFSET_BASIS_32_BIT = 2_166_136_261; + private const ulong FNV_OFFSET_BASIS_64_BIT = 14_695_981_039_346_656_037; + + private const uint FNV_PRIME_32_BIT = 16_777_619; + private const ulong FNV_PRIME_64_BIT = 1_099_511_628_211; + + /// + /// Computes the 32bit FNV-1a hash of a string. + /// + /// The string to hash. + /// The 32bit FNV-1a hash of the string. + public static uint ToFNV32(this string text) => ToFNV32(text.AsSpan()); + + /// + /// Computes the 32bit FNV-1a hash of a string. + /// + /// The string to hash. + /// The 32bit FNV-1a hash of the string. + public static uint ToFNV32(this ReadOnlySpan text) + { + var hash = FNV_OFFSET_BASIS_32_BIT; + foreach (var c in text) + { + hash ^= c; + hash *= FNV_PRIME_32_BIT; + } + + return hash; + } + + /// + /// Computes the 64bit FNV-1a hash of a string. + /// + /// The string to hash. + /// The 64bit FNV-1a hash of the string. + public static ulong ToFNV64(this string text) => ToFNV64(text.AsSpan()); + + /// + /// Computes the 64bit FNV-1a hash of a string. + /// + /// The string to hash. + /// The 64bit FNV-1a hash of the string. + public static ulong ToFNV64(this ReadOnlySpan text) + { + var hash = FNV_OFFSET_BASIS_64_BIT; + foreach (var c in text) + { + hash ^= c; + hash *= FNV_PRIME_64_BIT; + } + + return hash; + } +} \ No newline at end of file