2014-04-28 20:22:22 +08:00
|
|
|
|
namespace Antlr4.Runtime.Sharpen
|
2013-02-24 13:13:40 +08:00
|
|
|
|
{
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2014-04-28 21:25:21 +08:00
|
|
|
|
internal class SequenceEqualityComparer<T> : EqualityComparer<IEnumerable<T>>
|
2013-02-24 13:13:40 +08:00
|
|
|
|
{
|
|
|
|
|
private static readonly SequenceEqualityComparer<T> _default = new SequenceEqualityComparer<T>();
|
|
|
|
|
|
|
|
|
|
private readonly IEqualityComparer<T> _elementEqualityComparer = EqualityComparer<T>.Default;
|
|
|
|
|
|
|
|
|
|
public SequenceEqualityComparer()
|
|
|
|
|
: this(null)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SequenceEqualityComparer(IEqualityComparer<T> elementComparer)
|
|
|
|
|
{
|
|
|
|
|
_elementEqualityComparer = elementComparer ?? EqualityComparer<T>.Default;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public new static SequenceEqualityComparer<T> Default
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _default;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool Equals(IEnumerable<T> x, IEnumerable<T> y)
|
|
|
|
|
{
|
|
|
|
|
if (x == y)
|
|
|
|
|
return true;
|
|
|
|
|
if (x == null || y == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return x.SequenceEqual(y, _elementEqualityComparer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int GetHashCode(IEnumerable<T> obj)
|
|
|
|
|
{
|
|
|
|
|
if (obj == null)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
int hashCode = 1;
|
|
|
|
|
foreach (T element in obj)
|
|
|
|
|
hashCode = 31 * hashCode + _elementEqualityComparer.GetHashCode(element);
|
|
|
|
|
|
|
|
|
|
return hashCode;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|