gpt4 book ai didi

c# - 在 GetHashCode 实现中处理集合

转载 作者:太空宇宙 更新时间:2023-11-03 10:34:05 25 4
gpt4 key购买 nike

我正在努力根据此答案中的 HashCode 结构实现 GetHashCode() here .由于我的 Equals 方法将考虑使用 Enumerable.SequenceEqual() 的集合,因此我需要在我的 GetHashCode() 实现中包含这些集合。

作为起点,我使用 Jon Skeet 的嵌入式 GetHashCode() 实现来测试 HashCode 结构实现的输出。使用下面的以下测试,这按预期工作 -

private class MyObjectEmbeddedGetHashCode
{
public int x;
public string y;
public DateTimeOffset z;

public List<string> collection;

public override int GetHashCode()
{
unchecked
{
int hash = 17;

hash = hash * 31 + x.GetHashCode();
hash = hash * 31 + y.GetHashCode();
hash = hash * 31 + z.GetHashCode();

return hash;
}
}
}

private class MyObjectUsingHashCodeStruct
{
public int x;
public string y;
public DateTimeOffset z;

public List<string> collection;

public override int GetHashCode()
{
return HashCode.Start
.Hash(x)
.Hash(y)
.Hash(z);
}
}

[Test]
public void GetHashCode_CollectionExcluded()
{
DateTimeOffset now = DateTimeOffset.Now;

MyObjectEmbeddedGetHashCode a = new MyObjectEmbeddedGetHashCode()
{
x = 1,
y = "Fizz",
z = now,
collection = new List<string>()
{
"Foo",
"Bar",
"Baz"
}
};

MyObjectUsingHashCodeStruct b = new MyObjectUsingHashCodeStruct()
{
x = 1,
y = "Fizz",
z = now,
collection = new List<string>()
{
"Foo",
"Bar",
"Baz"
}
};

Console.WriteLine("MyObject::GetHashCode(): {0}", a.GetHashCode());
Console.WriteLine("MyObjectEx::GetHashCode(): {0}", b.GetHashCode());

Assert.AreEqual(a.GetHashCode(), b.GetHashCode());
}

下一步是在 GetHashCode() 计算中考虑集合。这需要对 MyObjectEmbeddedGetHashCode 中的 GetHashCode() 实现进行少量添加。

public override int GetHashCode()
{
unchecked
{
int hash = 17;

hash = hash * 31 + x.GetHashCode();
hash = hash * 31 + y.GetHashCode();
hash = hash * 31 + z.GetHashCode();

int collectionHash = 17;

foreach (var item in collection)
{
collectionHash = collectionHash * 31 + item.GetHashCode();
}

hash = hash * 31 + collectionHash;

return hash;
}
}

但是,这在 HashCode 结构中有点困难。在此示例中,当将类型 List 的集合传递到 Hash 方法时,T 是 List,因此尝试将 obj 转换为 ICollection 或 IEnumberable 不起作用。我可以成功转换为 IEnumerable,但它会导致装箱,我发现我不得不担心排除实现 IEnumerable 的字符串等类型。

在这种情况下,有没有办法可靠地将 obj 转换为 ICollection 或 IEnumerable?

public struct HashCode
{
private readonly int hashCode;

public HashCode(int hashCode)
{
this.hashCode = hashCode;
}

public static HashCode Start
{
get { return new HashCode(17); }
}

public static implicit operator int(HashCode hashCode)
{
return hashCode.GetHashCode();
}

public HashCode Hash<T>(T obj)
{
// I am able to detect if obj implements one of the lower level
// collection interfaces. However, I am not able to cast obj to
// one of them since T in this case is defined as List<string>,
// so using as to cast obj to ICollection<T> or IEnumberable<T>
// doesn't work.
var isGenericICollection = obj.GetType().GetInterfaces().Any(
x => x.IsGenericType &&
x.GetGenericTypeDefinition() == typeof(ICollection<>));

var c = EqualityComparer<T>.Default;

// This works but using IEnumerable causes boxing.
// var h = c.Equals(obj, default(T)) ? 0 : ( !(obj is string) && (obj is IEnumerable) ? GetCollectionHashCode(obj as IEnumerable) : obj.GetHashCode());

var h = c.Equals(obj, default(T)) ? 0 : obj.GetHashCode();
unchecked { h += this.hashCode * 31; }
return new HashCode(h);
}

public override int GetHashCode()
{
return this.hashCode;
}
}

最佳答案

您可以通过多种方式解决收集问题:

  1. 使用非通用接口(interface),例如ICollectionIEnumerable .
  2. Hash() 添加重载方法,例如Hash<T>(IEnumerable<T> list) { ... }

也就是说,恕我直言,最好只留下 struct HashCode单独并将特定于集合的代码放入您的实际 GetHashCode()方法。例如:

public override int GetHashCode()
{
HashCode hash = HashCode.Start
.Hash(x)
.Hash(y)
.Hash(z);

foreach (var item in collection)
{
hash = hash.Hash(item);
}

return hash;
}

如果您确实想要 struct HashCode 的全功能版本类型,在我看来,您引用的同一页面似乎有一个:https://stackoverflow.com/a/2575444/3538012

成员的命名不同,但与struct HashCode基本相同。类型,但对其他复杂类型有重载(如我上面的建议#2)。您可以使用它,或者只是将那里的技术应用到您的 struct HashCode 的实现中,保留其中使用的命名约定。

关于c# - 在 GetHashCode 实现中处理集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28546239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com