Replace all raw string comparisons with StringKey, source generator that creates cache keys for all names for Gen7 plugin
All checks were successful
Build / Build (push) Successful in 2m21s
All checks were successful
Build / Build (push) Successful in 2m21s
This commit is contained in:
52
Plugins/PkmnLib.Plugin.Gen7.SourceGen/EquatableArray.cs
Normal file
52
Plugins/PkmnLib.Plugin.Gen7.SourceGen/EquatableArray.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
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();
|
||||
}
|
||||
Reference in New Issue
Block a user