| | | 1 | | //======================================================================= |
| | | 2 | | // GridChangeStamp.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.Runtime.CompilerServices; |
| | | 10 | | using SwiftCollections.Utility; |
| | | 11 | | |
| | | 12 | | namespace GridForge.Grids; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Identifies the committed order and logical cause of a GridForge world mutation. |
| | | 16 | | /// </summary> |
| | | 17 | | public readonly struct GridChangeStamp : IEquatable<GridChangeStamp> |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// The world-local commit order. Zero is reserved for an invalid or default value. |
| | | 21 | | /// </summary> |
| | | 22 | | public readonly ulong Sequence; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// The world-local logical cause shared by notifications emitted for one mutation. |
| | | 26 | | /// </summary> |
| | | 27 | | public readonly ulong CauseId; |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Whether this value identifies a committed world mutation. |
| | | 31 | | /// </summary> |
| | | 32 | | public bool IsValid |
| | | 33 | | { |
| | | 34 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 4 | 35 | | get => Sequence != 0 && CauseId != 0; |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Initializes a change stamp. |
| | | 40 | | /// </summary> |
| | | 41 | | public GridChangeStamp(ulong sequence, ulong causeId) |
| | | 42 | | { |
| | 3499 | 43 | | Sequence = sequence; |
| | 3499 | 44 | | CauseId = causeId; |
| | 3499 | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <inheritdoc /> |
| | | 48 | | public bool Equals(GridChangeStamp other) => |
| | 6 | 49 | | Sequence == other.Sequence && CauseId == other.CauseId; |
| | | 50 | | |
| | | 51 | | /// <inheritdoc /> |
| | 3 | 52 | | public override bool Equals(object? obj) => obj is GridChangeStamp other && Equals(other); |
| | | 53 | | |
| | | 54 | | /// <inheritdoc /> |
| | 4 | 55 | | public override int GetHashCode() => SwiftHashTools.CombineHashCodes(Sequence, CauseId); |
| | | 56 | | |
| | | 57 | | /// <inheritdoc /> |
| | 1 | 58 | | public override string ToString() => $"{Sequence}:{CauseId}"; |
| | | 59 | | |
| | | 60 | | /// <inheritdoc /> |
| | 1 | 61 | | public static bool operator ==(GridChangeStamp left, GridChangeStamp right) => left.Equals(right); |
| | | 62 | | |
| | | 63 | | /// <inheritdoc /> |
| | 1 | 64 | | public static bool operator !=(GridChangeStamp left, GridChangeStamp right) => !left.Equals(right); |
| | | 65 | | } |