Skip to content

Commit

Permalink
use HashCode in CASCKeyComparer
Browse files Browse the repository at this point in the history
  • Loading branch information
ZingBallyhoo committed Nov 10, 2024
1 parent 0bfd791 commit cc8286f
Showing 1 changed file with 8 additions and 30 deletions.
38 changes: 8 additions & 30 deletions TACTLib/Core/Key/CASCKeyComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

namespace TACTLib.Core.Key {
public class CASCKeyComparer : IEqualityComparer<TruncatedKey>, IEqualityComparer<FullKey> {
private const uint FnvPrime32 = 0x1000193;
private const uint FnvOffset32 = 0x811C9DC5;

/// <summary>Static instance</summary>
public static readonly CASCKeyComparer Instance = new CASCKeyComparer();

Expand All @@ -21,35 +18,16 @@ private static bool Equals(ReadOnlySpan<byte> spanA, ReadOnlySpan<byte> spanB) {
return spanA.SequenceEqual(spanB);
}

public unsafe int GetHashCode(TruncatedKey obj) {
var hash = FnvOffset32;
var ptr = (uint*) &obj;

for (var i = 0; i < 2; i++) {
hash ^= ptr[i];
hash *= FnvPrime32;
}

var hashPtr = (byte*) &hash;
var b = *((byte*)ptr + 8);
for (var i = 0; i < 4; ++i) {
hashPtr[i] ^= b;
}

return unchecked((int) hash);
public int GetHashCode(TruncatedKey obj) {
var h = new HashCode();
h.AddBytes(obj);
return h.ToHashCode();
}

public unsafe int GetHashCode(FullKey obj) {
var hash = FnvOffset32;

var ptr = (uint*) &obj;

for (var i = 0; i < 4; i++) {
hash ^= ptr[i];
hash *= FnvPrime32;
}

return unchecked((int) hash);
public int GetHashCode(FullKey obj) {
var h = new HashCode();
h.AddBytes(obj);
return h.ToHashCode();
}
}
}

0 comments on commit cc8286f

Please sign in to comment.