| | | 1 | | //======================================================================= |
| | | 2 | | // ChronicleHash.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | using System.Globalization; |
| | | 10 | | |
| | | 11 | | namespace Chronicler.Hashing; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Fixed-width deterministic hash value produced by a Chronicler record-hash pass. |
| | | 15 | | /// </summary> |
| | | 16 | | public readonly struct ChronicleHash : IEquatable<ChronicleHash> |
| | | 17 | | { |
| | | 18 | | /// <summary>Creates a hash from two 64-bit components.</summary> |
| | | 19 | | /// <param name="low">The component emitted last in the hexadecimal representation.</param> |
| | | 20 | | /// <param name="high">The component emitted first in the hexadecimal representation.</param> |
| | | 21 | | public ChronicleHash(ulong low, ulong high) |
| | | 22 | | { |
| | 110880 | 23 | | Low = low; |
| | 110880 | 24 | | High = high; |
| | 110880 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary>Gets the low component.</summary> |
| | | 28 | | /// <value>The final 16 hexadecimal digits in <see cref="ToString"/>.</value> |
| | | 29 | | public ulong Low { get; } |
| | | 30 | | |
| | | 31 | | /// <summary>Gets the high component.</summary> |
| | | 32 | | /// <value>The first 16 hexadecimal digits in <see cref="ToString"/>.</value> |
| | | 33 | | public ulong High { get; } |
| | | 34 | | |
| | | 35 | | /// <summary>Tests exact hash equality.</summary> |
| | | 36 | | /// <param name="other">The hash to compare by component.</param> |
| | | 37 | | /// <returns>True when both components match.</returns> |
| | | 38 | | public bool Equals(ChronicleHash other) |
| | | 39 | | { |
| | 165 | 40 | | return Low == other.Low && High == other.High; |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary>Tests equality with a boxed hash.</summary> |
| | | 44 | | /// <param name="obj">The object to compare. Null or another type compares unequal.</param> |
| | | 45 | | /// <returns>True when the object has this value type and both components match.</returns> |
| | | 46 | | public override bool Equals(object? obj) |
| | | 47 | | { |
| | 29 | 48 | | return obj is ChronicleHash other && Equals(other); |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary>Hashes the hash for use in collections.</summary> |
| | | 52 | | /// <returns>A deterministic 32-bit mix of both components.</returns> |
| | | 53 | | public override int GetHashCode() |
| | | 54 | | { |
| | | 55 | | unchecked |
| | | 56 | | { |
| | 2 | 57 | | ulong mixed = Low ^ (High * 0x9e3779b97f4a7c15UL); |
| | 2 | 58 | | mixed ^= mixed >> 33; |
| | 2 | 59 | | mixed *= 0xff51afd7ed558ccdUL; |
| | 2 | 60 | | mixed ^= mixed >> 33; |
| | 2 | 61 | | return (int)(mixed ^ (mixed >> 32)); |
| | | 62 | | } |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary>Formats the hash for display or comparison.</summary> |
| | | 66 | | /// <returns>Exactly 32 lowercase hexadecimal digits, high component first, with leading zeroes and no separators.</ |
| | | 67 | | public override string ToString() |
| | | 68 | | { |
| | 7 | 69 | | return High.ToString("x16", CultureInfo.InvariantCulture) |
| | 7 | 70 | | + Low.ToString("x16", CultureInfo.InvariantCulture); |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary>Tests whether the left hash exactly equals the right hash.</summary> |
| | | 74 | | /// <param name="left">The first operand.</param> |
| | | 75 | | /// <param name="right">The second operand of the comparison.</param> |
| | | 76 | | /// <returns>True when the left operand exactly equals the right operand.</returns> |
| | | 77 | | public static bool operator ==(ChronicleHash left, ChronicleHash right) |
| | | 78 | | { |
| | 1 | 79 | | return left.Equals(right); |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary>Tests whether the left hash differs from the right hash.</summary> |
| | | 83 | | /// <param name="left">The first operand.</param> |
| | | 84 | | /// <param name="right">The second operand of the comparison.</param> |
| | | 85 | | /// <returns>True when the left operand differs from the right operand.</returns> |
| | | 86 | | public static bool operator !=(ChronicleHash left, ChronicleHash right) |
| | | 87 | | { |
| | 20 | 88 | | return !left.Equals(right); |
| | | 89 | | } |
| | | 90 | | } |