|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Collections.Immutable; |
| 5 | +using System.Linq; |
| 6 | + |
| 7 | +namespace Nino.Generator.DTOs; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Immutable array wrapper with value-based equality for use in source generator DTOs. |
| 11 | +/// Enables proper incremental caching by implementing structural equality. |
| 12 | +/// </summary> |
| 13 | +public readonly struct EquatableArray<T> : IEquatable<EquatableArray<T>>, IEnumerable<T> |
| 14 | +{ |
| 15 | + private readonly T[]? _array; |
| 16 | + |
| 17 | + public EquatableArray(IEnumerable<T>? items) |
| 18 | + { |
| 19 | + _array = items?.ToArray(); |
| 20 | + } |
| 21 | + |
| 22 | + public EquatableArray(params T[] items) |
| 23 | + { |
| 24 | + _array = items; |
| 25 | + } |
| 26 | + |
| 27 | + public static EquatableArray<T> Empty => new(Array.Empty<T>()); |
| 28 | + |
| 29 | + public int Length => _array?.Length ?? 0; |
| 30 | + |
| 31 | + public T this[int index] => _array![index]; |
| 32 | + |
| 33 | + public bool Equals(EquatableArray<T> other) |
| 34 | + { |
| 35 | + if (_array is null) return other._array is null; |
| 36 | + if (other._array is null) return false; |
| 37 | + if (_array.Length != other._array.Length) return false; |
| 38 | + |
| 39 | + for (int i = 0; i < _array.Length; i++) |
| 40 | + { |
| 41 | + if (!EqualityComparer<T>.Default.Equals(_array[i], other._array[i])) |
| 42 | + return false; |
| 43 | + } |
| 44 | + |
| 45 | + return true; |
| 46 | + } |
| 47 | + |
| 48 | + public override bool Equals(object? obj) |
| 49 | + { |
| 50 | + return obj is EquatableArray<T> other && Equals(other); |
| 51 | + } |
| 52 | + |
| 53 | + public override int GetHashCode() |
| 54 | + { |
| 55 | + if (_array is null) return 0; |
| 56 | + |
| 57 | + // Simple hash code calculation compatible with .NET Standard 2.0 |
| 58 | + unchecked |
| 59 | + { |
| 60 | + int hash = 17; |
| 61 | + foreach (var item in _array) |
| 62 | + { |
| 63 | + hash = hash * 31 + (item?.GetHashCode() ?? 0); |
| 64 | + } |
| 65 | + return hash; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public IEnumerator<T> GetEnumerator() |
| 70 | + { |
| 71 | + return (_array ?? Array.Empty<T>()).AsEnumerable().GetEnumerator(); |
| 72 | + } |
| 73 | + |
| 74 | + IEnumerator IEnumerable.GetEnumerator() |
| 75 | + { |
| 76 | + return GetEnumerator(); |
| 77 | + } |
| 78 | + |
| 79 | + public static bool operator ==(EquatableArray<T> left, EquatableArray<T> right) |
| 80 | + { |
| 81 | + return left.Equals(right); |
| 82 | + } |
| 83 | + |
| 84 | + public static bool operator !=(EquatableArray<T> left, EquatableArray<T> right) |
| 85 | + { |
| 86 | + return !left.Equals(right); |
| 87 | + } |
| 88 | + |
| 89 | + public static implicit operator EquatableArray<T>(T[] array) |
| 90 | + { |
| 91 | + return new EquatableArray<T>(array); |
| 92 | + } |
| 93 | + |
| 94 | + public static implicit operator EquatableArray<T>(ImmutableArray<T> array) |
| 95 | + { |
| 96 | + return new EquatableArray<T>(array); |
| 97 | + } |
| 98 | + |
| 99 | + public ImmutableArray<T> ToImmutableArray() |
| 100 | + { |
| 101 | + return _array is null ? ImmutableArray<T>.Empty : ImmutableArray.Create(_array); |
| 102 | + } |
| 103 | + |
| 104 | + public T[] ToArray() |
| 105 | + { |
| 106 | + return _array is null ? Array.Empty<T>() : _array.ToArray(); |
| 107 | + } |
| 108 | +} |
0 commit comments