| | | 1 | | //======================================================================= |
| | | 2 | | // SwiftTicket.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 | | |
| | | 10 | | namespace SwiftCollections; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Identifies an allocation in its originating ticket map by index and generation. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <remarks> |
| | | 16 | | /// A <see cref="SwiftTicket"/> consists of an integer index and an unsigned generation value. |
| | | 17 | | /// This combination helps detect stale references when slots are reused. Tickets carry no collection identity; |
| | | 18 | | /// use them only with their originating map. Generation counters are finite. |
| | | 19 | | ///</remarks> |
| | | 20 | | public readonly struct SwiftTicket : IEquatable<SwiftTicket> |
| | | 21 | | { |
| | | 22 | | /// <summary> |
| | | 23 | | /// Gets the zero-based index associated with this instance. |
| | | 24 | | /// </summary> |
| | | 25 | | public readonly int Index; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Gets the generation number associated with this instance. |
| | | 29 | | /// </summary> |
| | | 30 | | public readonly uint Generation; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Initializes a new instance of the SwiftTicket class with the specified index and generation values. |
| | | 34 | | /// </summary> |
| | | 35 | | /// <param name="index"> |
| | | 36 | | /// The zero-based index that identifies the ticket. |
| | | 37 | | /// Must be greater than or equal to 0. |
| | | 38 | | /// </param> |
| | | 39 | | /// <param name="generation"> |
| | | 40 | | /// The generation value associated with the ticket. |
| | | 41 | | /// Used to distinguish between different versions of the same index. |
| | | 42 | | /// </param> |
| | | 43 | | public SwiftTicket(int index, uint generation) |
| | | 44 | | { |
| | 620 | 45 | | Index = index; |
| | 620 | 46 | | Generation = generation; |
| | 620 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <inheritdoc/> |
| | | 50 | | public bool Equals(SwiftTicket other) |
| | 27 | 51 | | => Index == other.Index && Generation == other.Generation; |
| | | 52 | | |
| | | 53 | | /// <inheritdoc/> |
| | | 54 | | public override bool Equals(object? obj) |
| | 2 | 55 | | => obj is SwiftTicket h && Equals(h); |
| | | 56 | | |
| | | 57 | | /// <inheritdoc/> |
| | | 58 | | public override int GetHashCode() |
| | 2 | 59 | | => HashCode.Combine(Index, Generation); |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Returns a string that represents the current ticket, including its index and generation values. |
| | | 63 | | /// </summary> |
| | | 64 | | /// <returns>A string in the format "Ticket({Index}:{Generation})" that identifies the ticket by its index and gener |
| | | 65 | | public override string ToString() |
| | 1 | 66 | | => $"Ticket({Index}:{Generation})"; |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Determines whether two SwiftTicket instances are equal. |
| | | 70 | | /// </summary> |
| | 2 | 71 | | public static bool operator ==(SwiftTicket left, SwiftTicket right) => left.Equals(right); |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Determines whether two SwiftTicket instances are not equal. |
| | | 75 | | /// </summary> |
| | 1 | 76 | | public static bool operator !=(SwiftTicket left, SwiftTicket right) => !(left == right); |
| | | 77 | | } |