52 lines
1.3 KiB
C#
52 lines
1.3 KiB
C#
using System.Collections;
|
|
|
|
namespace PkmnLib.Plugin.Gen7.SourceGen;
|
|
|
|
/// <summary>
|
|
/// An immutable array wrapper with sequence-based equality, so that incremental generator pipeline
|
|
/// values containing collections cache correctly.
|
|
/// </summary>
|
|
public readonly struct EquatableArray<T> : IEquatable<EquatableArray<T>>, IReadOnlyList<T> where T : IEquatable<T>
|
|
{
|
|
private readonly T[]? _array;
|
|
|
|
public EquatableArray(T[] array)
|
|
{
|
|
_array = array;
|
|
}
|
|
|
|
public int Count => _array?.Length ?? 0;
|
|
|
|
public T this[int index] => _array![index];
|
|
|
|
public bool Equals(EquatableArray<T> other)
|
|
{
|
|
if (Count != other.Count)
|
|
return false;
|
|
for (var i = 0; i < Count; i++)
|
|
{
|
|
if (!this[i].Equals(other[i]))
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public override bool Equals(object? obj) => obj is EquatableArray<T> other && Equals(other);
|
|
|
|
public override int GetHashCode()
|
|
{
|
|
unchecked
|
|
{
|
|
var hash = 17;
|
|
for (var i = 0; i < Count; i++)
|
|
{
|
|
hash = hash * 31 + this[i].GetHashCode();
|
|
}
|
|
return hash;
|
|
}
|
|
}
|
|
|
|
public IEnumerator<T> GetEnumerator() => ((IEnumerable<T>)(_array ?? [])).GetEnumerator();
|
|
|
|
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
|
} |