简体   繁体   English

重载 VB.NET 中的 Equals / GetHashCode 以使用对象作为字典键

[英]Overload Equals / GetHashCode in VB.NET to Use Objects as Dictionary Keys

I have a Dictionary whose keys are Excel Range objects (no, this is not negotiable), defined as follows (the type CellProp is an object that contains various cell properties):我有一个字典,它的键是 Excel Range 对象(不,这是不可协商的),定义如下(CellProp 类型是一个 object,它包含各种单元格属性):

Dim dic As New Dictionary(Of Excel.Range, CellProp)(New RangeComparer())

Because the keys are objects, I need to overload the Equals/GetHashCode functions.因为键是对象,所以我需要重载 Equals/GetHashCode 函数。 My current implementation is as follows:我目前的实现如下:

Class RangeComparer
Implements IEqualityComparer(Of Excel.Range)
Public Overloads Function Equals(ByVal x As Excel.Range, ByVal y As Excel.Range) As Boolean Implements IEqualityComparer(Of Excel.Range).Equals
    If x.Address(External:=True) = y.Address(External:=True) Then
        Return True
    Else
        Return False
    End If
End Function
Public Overloads Function GetHashCode(ByVal obj As Excel.Range) As Integer Implements IEqualityComparer(Of Excel.Range).GetHashCode
      Return obj.Count.GetHashCode
    End Function
End Class

However, this can be pretty slow to execute when adding many cells (ie hundreds) to the Dictionary at once.但是,当一次向字典中添加许多单元格(即数百个)时,执行起来可能会很慢。 Most importantly, is there a faster way to do this?最重要的是,有没有更快的方法来做到这一点? Secondarily, why does getting the hash code for the Range's Count property seem to work (albeit slowly)?其次,为什么获取 Range 的 Count 属性的 hash 代码似乎有效(尽管速度很慢)?

You may want to do a little research into how hash codes work .您可能想稍微研究一下 hash 代码的工作原理 Hash codes are 100% arbitrary and definable by the programmer. Hash 代码是 100% 任意的,可由程序员定义。 The only thing that matters is that if two instances are not the same, then their hash codes should be different .唯一重要的是,如果两个实例不相同,那么它们的 hash 代码应该不同 If you have a collection where almost all hash codes are identical (ie, count = 1), then your dictionary still works just fine, but it degrades into a linear search, which is very inefficient.如果您有一个集合,其中几乎所有 hash 代码都相同(即计数 = 1),那么您的字典仍然可以正常工作,但它会退化为线性搜索,效率非常低。 This is because almost all of the instances are generating hash collisions, so there is no benefit provided by the hashing into buckets.这是因为几乎所有实例都产生了 hash 次冲突,因此散列到桶中没有任何好处。

For instance, another hash code algorithm you could try is generating one from the names of the cells, which should have a lot fewer hash collisions:例如,您可以尝试的另一种 hash 代码算法是从单元格的名称生成一个代码算法,它应该有更少的 hash 冲突:

Public Overloads Function GetHashCode(ByVal obj As Excel.Range) _
    As Integer Implements IEqualityComparer(Of Excel.Range).GetHashCode

  Return obj.Address(External:=True).GetHashCode

End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM