mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2025-04-28 08:19:47 +00:00
Add FNVHash class for 32-bit and 64-bit hash computations
This commit is contained in:
parent
55c472d819
commit
3506d3b12c
@ -2,6 +2,7 @@
|
|||||||
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=AI/@EntryIndexedValue">AI</s:String>
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=AI/@EntryIndexedValue">AI</s:String>
|
||||||
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=EDI/@EntryIndexedValue">EDI</s:String>
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=EDI/@EntryIndexedValue">EDI</s:String>
|
||||||
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=ERI/@EntryIndexedValue">ERI</s:String>
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=ERI/@EntryIndexedValue">ERI</s:String>
|
||||||
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=FNV/@EntryIndexedValue">FNV</s:String>
|
||||||
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=GWDG/@EntryIndexedValue">GWDG</s:String>
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=GWDG/@EntryIndexedValue">GWDG</s:String>
|
||||||
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=LLM/@EntryIndexedValue">LLM</s:String>
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=LLM/@EntryIndexedValue">LLM</s:String>
|
||||||
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=LM/@EntryIndexedValue">LM</s:String>
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=LM/@EntryIndexedValue">LM</s:String>
|
||||||
|
62
app/MindWork AI Studio/Tools/FNVHash.cs
Normal file
62
app/MindWork AI Studio/Tools/FNVHash.cs
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
// ReSharper disable MemberCanBePrivate.Global
|
||||||
|
namespace AIStudio.Tools;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Implements the Fowler–Noll–Vo hash function for 32-bit and 64-bit hashes.
|
||||||
|
/// </summary>
|
||||||
|
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;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Computes the 32bit FNV-1a hash of a string.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="text">The string to hash.</param>
|
||||||
|
/// <returns>The 32bit FNV-1a hash of the string.</returns>
|
||||||
|
public static uint ToFNV32(this string text) => ToFNV32(text.AsSpan());
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Computes the 32bit FNV-1a hash of a string.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="text">The string to hash.</param>
|
||||||
|
/// <returns>The 32bit FNV-1a hash of the string.</returns>
|
||||||
|
public static uint ToFNV32(this ReadOnlySpan<char> text)
|
||||||
|
{
|
||||||
|
var hash = FNV_OFFSET_BASIS_32_BIT;
|
||||||
|
foreach (var c in text)
|
||||||
|
{
|
||||||
|
hash ^= c;
|
||||||
|
hash *= FNV_PRIME_32_BIT;
|
||||||
|
}
|
||||||
|
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Computes the 64bit FNV-1a hash of a string.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="text">The string to hash.</param>
|
||||||
|
/// <returns>The 64bit FNV-1a hash of the string.</returns>
|
||||||
|
public static ulong ToFNV64(this string text) => ToFNV64(text.AsSpan());
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Computes the 64bit FNV-1a hash of a string.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="text">The string to hash.</param>
|
||||||
|
/// <returns>The 64bit FNV-1a hash of the string.</returns>
|
||||||
|
public static ulong ToFNV64(this ReadOnlySpan<char> text)
|
||||||
|
{
|
||||||
|
var hash = FNV_OFFSET_BASIS_64_BIT;
|
||||||
|
foreach (var c in text)
|
||||||
|
{
|
||||||
|
hash ^= c;
|
||||||
|
hash *= FNV_PRIME_64_BIT;
|
||||||
|
}
|
||||||
|
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user