| | | 1 | | using System; |
| | | 2 | | using System.Globalization; |
| | | 3 | | |
| | | 4 | | namespace Chronicler; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Fixed-width deterministic hash value produced by a Chronicler record-hash pass. |
| | | 8 | | /// </summary> |
| | | 9 | | public readonly struct ChronicleHash : IEquatable<ChronicleHash> |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Creates a new hash value from its two 64-bit lanes. |
| | | 13 | | /// </summary> |
| | | 14 | | public ChronicleHash(ulong low, ulong high) |
| | | 15 | | { |
| | 110666 | 16 | | Low = low; |
| | 110666 | 17 | | High = high; |
| | 110666 | 18 | | } |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Gets the low 64-bit lane. |
| | | 22 | | /// </summary> |
| | | 23 | | public ulong Low { get; } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Gets the high 64-bit lane. |
| | | 27 | | /// </summary> |
| | | 28 | | public ulong High { get; } |
| | | 29 | | |
| | | 30 | | /// <inheritdoc /> |
| | | 31 | | public bool Equals(ChronicleHash other) |
| | | 32 | | { |
| | 47 | 33 | | return Low == other.Low && High == other.High; |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <inheritdoc /> |
| | | 37 | | public override bool Equals(object? obj) |
| | | 38 | | { |
| | 27 | 39 | | return obj is ChronicleHash other && Equals(other); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <inheritdoc /> |
| | | 43 | | public override int GetHashCode() |
| | | 44 | | { |
| | | 45 | | unchecked |
| | | 46 | | { |
| | 2 | 47 | | ulong mixed = Low ^ (High * 0x9e3779b97f4a7c15UL); |
| | 2 | 48 | | mixed ^= mixed >> 33; |
| | 2 | 49 | | mixed *= 0xff51afd7ed558ccdUL; |
| | 2 | 50 | | mixed ^= mixed >> 33; |
| | 2 | 51 | | return (int)(mixed ^ (mixed >> 32)); |
| | | 52 | | } |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Returns a lowercase 32-character hexadecimal hash string as high lane followed by low lane. |
| | | 57 | | /// </summary> |
| | | 58 | | public override string ToString() |
| | | 59 | | { |
| | 4 | 60 | | return High.ToString("x16", CultureInfo.InvariantCulture) |
| | 4 | 61 | | + Low.ToString("x16", CultureInfo.InvariantCulture); |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Compares two hash values for equality. |
| | | 66 | | /// </summary> |
| | | 67 | | public static bool operator ==(ChronicleHash left, ChronicleHash right) |
| | | 68 | | { |
| | 1 | 69 | | return left.Equals(right); |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Compares two hash values for inequality. |
| | | 74 | | /// </summary> |
| | | 75 | | public static bool operator !=(ChronicleHash left, ChronicleHash right) |
| | | 76 | | { |
| | 20 | 77 | | return !left.Equals(right); |
| | | 78 | | } |
| | | 79 | | } |