|
4 | 4 | namespaceNexusMods.Sdk.Hashes; |
5 | 5 |
|
6 | 6 | /// <summary> |
7 | | -///FNV Non-Cryptographic Hash Algorithm. |
| 7 | +///Hasher using 16-bit FNV1a algorithm. |
8 | 8 | /// </summary> |
9 | 9 | [PublicAPI] |
10 | 10 | [SuppressMessage("ReSharper","InconsistentNaming")] |
11 | | -publicstaticclassFNV1a |
| 11 | +publicclassFNV1a16Hasher:IStringHasher<ushort,FNV1a16Hasher> |
| 12 | +{ |
| 13 | +publicstaticushortHash(ReadOnlySpan<byte>input)=>MixToShort(FNV1a32Hasher.Hash(input)); |
| 14 | +publicstaticushortHash(ReadOnlySpan<char>input)=>MixToShort(FNV1a32Hasher.Hash(input)); |
| 15 | + |
| 16 | +privatestaticushortMixToShort(uinthash) |
| 17 | +{ |
| 18 | +return(ushort)((hash>>16)^(hash&0xFFFF)); |
| 19 | +} |
| 20 | +} |
| 21 | + |
| 22 | +/// <summary> |
| 23 | +/// Hasher using 32-bit FNV1a algorithm. |
| 24 | +/// </summary> |
| 25 | +[PublicAPI] |
| 26 | +[SuppressMessage("ReSharper","InconsistentNaming")] |
| 27 | +publicclassFNV1a32Hasher:IStringHasher<uint,FNV1a32Hasher> |
12 | 28 | { |
13 | 29 | // https://datatracker.ietf.org/doc/html/draft-eastlake-fnv-35#name-fnv-constants |
14 | | -privateconstuintPrime32=0x01000193; |
15 | | -privateconstuintOffset32=0x811C9DC5; |
| 30 | +privateconstuintPrime=0x01000193; |
| 31 | +privateconstuintOffset=0x811C9DC5; |
16 | 32 |
|
17 | | -publicstaticuintHash32(ReadOnlySpan<char>input) |
| 33 | +publicstaticuintHash(ReadOnlySpan<byte>input) |
18 | 34 | { |
19 | | -varhash=Offset32; |
| 35 | +varhash=Offset; |
20 | 36 | for(vari=0;i<input.Length;i++) |
21 | 37 | { |
22 | 38 | varcurrent=input[i]; |
23 | 39 | hash^=current; |
24 | | -hash*=Prime32; |
| 40 | +hash*=Prime; |
25 | 41 | } |
26 | 42 |
|
27 | 43 | returnhash; |
28 | 44 | } |
29 | 45 |
|
30 | | -publicstaticuintHash32(ReadOnlySpan<byte>input) |
| 46 | +publicstaticuintHash(ReadOnlySpan<char>input) |
31 | 47 | { |
32 | | -varhash=Offset32; |
| 48 | +varhash=Offset; |
33 | 49 | for(vari=0;i<input.Length;i++) |
34 | 50 | { |
35 | 51 | varcurrent=input[i]; |
36 | 52 | hash^=current; |
37 | | -hash*=Prime32; |
| 53 | +hash*=Prime; |
38 | 54 | } |
39 | 55 |
|
40 | 56 | returnhash; |
41 | 57 | } |
42 | | - |
43 | | -publicstaticushortHash16(ReadOnlySpan<char>input)=>MixToShort(Hash32(input)); |
44 | | -publicstaticushortHash16(ReadOnlySpan<byte>input)=>MixToShort(Hash32(input)); |
45 | | - |
46 | | -/// <summary> |
47 | | -/// Mixes a 32-bit hash into a 16-bit unsigned short by XOR-ing the higher and lower 16 bits. |
48 | | -/// </summary> |
49 | | -privatestaticushortMixToShort(uinthash) |
50 | | -{ |
51 | | -return(ushort)((hash>>16)^(hash&0xFFFF)); |
52 | | -} |
53 | 58 | } |
54 | 59 |
|
55 | 60 | /// <summary> |
56 | | -///String hash poolusing32-bit FNV1ahashes. |
| 61 | +///Hasherusing64-bit FNV1aalgorithm. |
57 | 62 | /// </summary> |
58 | 63 | [PublicAPI] |
59 | 64 | [SuppressMessage("ReSharper","InconsistentNaming")] |
60 | | -publicclassFNV1a32Pool:AStringHashPool<uint> |
| 65 | +publicclassFNV1a64Hasher:IStringHasher<ulong,FNV1a64Hasher> |
61 | 66 | { |
62 | | -publicFNV1a32Pool(stringname):base(name){} |
| 67 | +// https://datatracker.ietf.org/doc/html/draft-eastlake-fnv-35#name-fnv-constants |
| 68 | +privateconstulongPrime=0x00000100_000001B3; |
| 69 | +privateconstulongOffset=0xCBF29CE4_84222325; |
63 | 70 |
|
64 | | -protectedoverrideuintHash(stringinput)=>FNV1a.Hash32(input); |
65 | | -} |
| 71 | +publicstaticulongHash(ReadOnlySpan<byte>input) |
| 72 | +{ |
| 73 | +varhash=Offset; |
| 74 | +for(vari=0;i<input.Length;i++) |
| 75 | +{ |
| 76 | +varcurrent=input[i]; |
| 77 | +hash^=current; |
| 78 | +hash*=Prime; |
| 79 | +} |
66 | 80 |
|
67 | | -/// <summary> |
68 | | -/// String hash pool using 16-bit FNV1a hashes. |
69 | | -/// </summary> |
70 | | -[PublicAPI] |
71 | | -[SuppressMessage("ReSharper","InconsistentNaming")] |
72 | | -publicclassFNV1a16Pool:AStringHashPool<ushort> |
73 | | -{ |
74 | | -publicFNV1a16Pool(stringname):base(name){} |
| 81 | +returnhash; |
| 82 | +} |
| 83 | + |
| 84 | +publicstaticulongHash(ReadOnlySpan<char>input) |
| 85 | +{ |
| 86 | +varhash=Offset; |
| 87 | +for(vari=0;i<input.Length;i++) |
| 88 | +{ |
| 89 | +varcurrent=input[i]; |
| 90 | +hash^=current; |
| 91 | +hash*=Prime; |
| 92 | +} |
75 | 93 |
|
76 | | -protectedoverrideushortHash(stringinput)=>FNV1a.Hash16(input); |
| 94 | +returnhash; |
| 95 | +} |
77 | 96 | } |