using System;
using System.Collections.Generic;
using System.Collections.Immutable;
namespace Vellum.Generator
{
///
/// Holds an array that an incremental generator step can hand on, because it compares by its items.
///
public readonly struct EquatableArray : IEquatable>
{
private readonly ImmutableArray items;
public EquatableArray(ImmutableArray items)
{
this.items = items;
}
///
/// Gets the items, which are empty for a default array.
///
public ImmutableArray Items
{
get
{
if (items.IsDefault)
{
return ImmutableArray.Empty;
}
return items;
}
}
public bool Equals(EquatableArray other)
{
ImmutableArray mine = Items;
ImmutableArray theirs = other.Items;
if (mine.Length != theirs.Length)
{
return false;
}
for (int i = 0; i < mine.Length; ++i)
{
if (!EqualityComparer.Default.Equals(mine[i], theirs[i]))
{
return false;
}
}
return true;
}
public override bool Equals(object obj)
{
return obj is EquatableArray other && Equals(other);
}
public override int GetHashCode()
{
int returnMe = 17;
foreach (ItemType item in Items)
{
returnMe = returnMe * 31 + EqualityComparer.Default.GetHashCode(item);
}
return returnMe;
}
}
}