namespace Canon.Core.GrammarParser;
///
/// LR语法中的一个项目集规范族
/// 也就是自动机中的一个状态
///
public class LrState : IEquatable
{
///
/// 项目集规范族
///
public required HashSet Expressions { get; init; }
///
/// 自动机的迁移规则
///
public Dictionary Transformer { get; } = [];
public bool Equals(LrState? other)
{
if (other is null)
{
return false;
}
if (Expressions.Count != other.Expressions.Count)
{
return false;
}
// 如果两个集合的大小相等,且一个是另一个的子集,那么两个集合相等。
return Expressions.IsSubsetOf(other.Expressions);
}
public override bool Equals(object? obj)
{
if (obj is not LrState other)
{
return false;
}
return Equals(other);
}
public override int GetHashCode()
{
int hash = 0;
foreach (Expression expression in Expressions)
{
hash ^= expression.GetHashCode();
}
return hash;
}
public static bool operator ==(LrState a, LrState b)
{
return a.Equals(b);
}
public static bool operator !=(LrState a, LrState b)
{
return !a.Equals(b);
}
}