| | | 1 | | using System; |
| | | 2 | | |
| | | 3 | | namespace SwiftCollections; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Represents a value type handle that uniquely identifies an object or resource by index and generation. |
| | | 7 | | /// </summary> |
| | | 8 | | /// <remarks> |
| | | 9 | | /// A <see cref="SwiftHandle"/> consists of an integer index and an unsigned generation value. |
| | | 10 | | /// This combination helps detect stale or invalid references when resources are recycled. |
| | | 11 | | ///</remarks> |
| | | 12 | | public readonly struct SwiftHandle : IEquatable<SwiftHandle> |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Gets the zero-based index associated with this instance. |
| | | 16 | | /// </summary> |
| | | 17 | | public readonly int Index; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Gets the generation number associated with this instance. |
| | | 21 | | /// </summary> |
| | | 22 | | public readonly uint Generation; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Initializes a new instance of the SwiftHandle class with the specified index and generation values. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="index"> |
| | | 28 | | /// The zero-based index that identifies the handle. |
| | | 29 | | /// Must be greater than or equal to 0. |
| | | 30 | | /// </param> |
| | | 31 | | /// <param name="generation"> |
| | | 32 | | /// The generation value associated with the handle. |
| | | 33 | | /// Used to distinguish between different versions of the same index. |
| | | 34 | | /// </param> |
| | | 35 | | public SwiftHandle(int index, uint generation) |
| | | 36 | | { |
| | 350 | 37 | | Index = index; |
| | 350 | 38 | | Generation = generation; |
| | 350 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <inheritdoc/> |
| | | 42 | | public bool Equals(SwiftHandle other) |
| | 4 | 43 | | => Index == other.Index && Generation == other.Generation; |
| | | 44 | | |
| | | 45 | | /// <inheritdoc/> |
| | | 46 | | public override bool Equals(object? obj) |
| | 1 | 47 | | => obj is SwiftHandle h && Equals(h); |
| | | 48 | | |
| | | 49 | | /// <inheritdoc/> |
| | | 50 | | public override int GetHashCode() |
| | 2 | 51 | | => HashCode.Combine(Index, Generation); |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Returns a string that represents the current handle, including its index and generation values. |
| | | 55 | | /// </summary> |
| | | 56 | | /// <returns>A string in the format "Handle({Index}:{Generation})" that identifies the handle by its index and gener |
| | | 57 | | public override string ToString() |
| | 1 | 58 | | => $"Handle({Index}:{Generation})"; |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Determines whether two SwiftHandle instances are equal. |
| | | 62 | | /// </summary> |
| | 2 | 63 | | public static bool operator ==(SwiftHandle left, SwiftHandle right) => left.Equals(right); |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Determines whether two SwiftHandle instances are not equal. |
| | | 67 | | /// </summary> |
| | 1 | 68 | | public static bool operator !=(SwiftHandle left, SwiftHandle right) => !(left == right); |
| | | 69 | | } |