var comparer = ...
var s1 = new HashSet<int[]>(new[] { new[] { 1, 2 }, new[] { 3, 4 } }, comparer);
var s2 = new HashSet<int[]>(new[] { new[] { 1, 2 }, new[] { 3, 4 } }, comparer);
(기본값?) 비교 자 s1.Equals (s2)가 참이되도록 HashSet에 연결할 수 있습니까? StructuralComparisons.StructuralEqualityComparer가 있지만 HashSet에는 일반 IEqualityComparer <>이 필요합니다.int 배열의 해시 집합에 대한 구조적 비교
UPDATE : 이제까지 일할 수있는 것처럼
가 보이지 않습니다. phoog에 의해 제안 내가 할 가장 가까운 HashSet.SetEquals를 사용하고 StructuralComparisons.StructuralEqualityComparer의 래퍼를 연결하는 것입니다
internal class GenericStructuralComparer<T> : IEqualityComparer<T>
{
static GenericStructuralComparer<T> _instance;
public static IEqualityComparer<T> Instance
{
get { return _instance ?? (_instance = new GenericStructuralComparer<T>()); }
}
public bool Equals(T x, T y)
{
return StructuralComparisons.StructuralEqualityComparer.Equals(x, y);
}
public int GetHashCode(T obj)
{
return StructuralComparisons.StructuralEqualityComparer.GetHashCode(obj);
}
}
public static IEqualityComparer<T> StructuralComparer<T>()
{
return GenericStructuralComparer<T>.Instance;
}
그리고
var comparer = StructuralComparer<int[]>();
var s1 = new HashSet<int[]>(new[] { new[] { 1, 2 }, new[] { 3, 4 } }, comparer);
var s2 = new HashSet<int[]>(new[] { new[] { 1, 2 }, new[] { 3, 4 } }, comparer);
s1.SetEquals(s2); // True
내가 모르는 무슨을 하나의 내장하지만 하나를 구현하기가 상대적으로 쉽다. 내가 보는 가장 큰 문제점은'GetHashCode()'가'O (n)'이 될 것이라는 것입니다. – CodesInChaos
'Enumerable.SequenceEqual'이 당신을 대신 할 것입니까? 주문 문제가 있을지 모르겠다. 아마도 .. – Servy
'StructuralComparisons.StructuralEqualityComparer'에 위임하는 래퍼 클래스에'IEqualityComparer'을 구현할 수 있습니다. –
phoog