vb.net 中的双哈希表或双哈希哈希表

发布于 2024-10-19 13:05:00 字数 2317 浏览 6 评论 0原文

我试图在 vb.net 中创建一个双重散列哈希表,但遇到一些我不知道如何解决的错误。希望你们能帮助我。我遇到的问题是在任何有 dbnull.value 或 mod= 的地方,我在编辑器中遇到错误,但我不知道如何修复它们。把这段代码放在VB中看看我的意思。 这是代码:

Public Class DoubleHashing

Class DataItem
    Private data As Integer

    Public Sub New(ByVal i As Integer)
        data = i
    End Sub
    Public Function getKey() As Integer
        Return data
    End Function

End Class

Private hashArray() As DataItem
Private arraySize As Integer
Private bufItem As DataItem

Public Sub New(ByVal size As Integer)

    arraySize = size
    hashArray(arraySize) = New DataItem(arraySize)
    Dim bufItem(-1) As DataItem
End Sub

Public Function hashFunc1(ByVal key As Integer) As Integer
    Return key Mod arraySize
End Function

Public Function hashFunc2(ByVal key As Integer) As Integer
    Return 6 - key Mod 6
End Function


Public Sub insert(ByVal key As Integer, ByVal item As DataItem)
    Dim hashVal As Integer = hashFunc1(key) ' hash the key
    Dim stepSize As Integer = hashFunc2(key) ' get step size

    ' until empty cell or -1
    While hashArray(hashVal) <> DBNull.Value & hashArray(hashVal).getKey() <> -1
        hashVal += stepSize ' add the step
        hashVal mod= arraySize ' for wraparound
    End While
    hashArray(hashVal) = item ' insert item

End Sub

Public Function delete(ByVal key As Integer) As DataItem
    Dim hashVal As Integer = hashFunc1(key)
    Dim stepSize As Integer = hashFunc2(key) ' get step size

    While hashArray(hashVal) <> DBNull.Value
        If hashArray(hashVal).getKey() = key Then
            Dim temp As DataItem = hashArray(hashVal) ' save item
            hashArray(hashVal) = bufItem '  delete item
            Return temp '  return item
        End If

        hashVal += stepSize ' add the step
        hashVal mod= arraySize '  for wraparound
    End While

    Return DBNull.Value
End Function

Public Function find(ByVal key As Integer) As DataItem
    Dim hashVal As Integer = hashFunc1(key)
    Dim stepSize As Integer = hashFunc2(key)

    While hashArray(hashVal) <> DBNull.Value
        If hashArray(hashVal).getKey() = key Then
            Return hashArray(hashVal)
        End If

        hashVal += stepSize
        hashVal mod= arraySize
    End While

    Return DBNull.Value
End Function

结束类

im trying to creat a double hashing hashtable in vb.net and i am getting a few error that i don't know how to solve. hopefuly you guys can help me out. the problems im having have are anywhere i have dbnull.value or mod= i get errors in the editor and i dont' know how to fix them. put this code in vb to see what i mean.
here is the code:

Public Class DoubleHashing

Class DataItem
    Private data As Integer

    Public Sub New(ByVal i As Integer)
        data = i
    End Sub
    Public Function getKey() As Integer
        Return data
    End Function

End Class

Private hashArray() As DataItem
Private arraySize As Integer
Private bufItem As DataItem

Public Sub New(ByVal size As Integer)

    arraySize = size
    hashArray(arraySize) = New DataItem(arraySize)
    Dim bufItem(-1) As DataItem
End Sub

Public Function hashFunc1(ByVal key As Integer) As Integer
    Return key Mod arraySize
End Function

Public Function hashFunc2(ByVal key As Integer) As Integer
    Return 6 - key Mod 6
End Function


Public Sub insert(ByVal key As Integer, ByVal item As DataItem)
    Dim hashVal As Integer = hashFunc1(key) ' hash the key
    Dim stepSize As Integer = hashFunc2(key) ' get step size

    ' until empty cell or -1
    While hashArray(hashVal) <> DBNull.Value & hashArray(hashVal).getKey() <> -1
        hashVal += stepSize ' add the step
        hashVal mod= arraySize ' for wraparound
    End While
    hashArray(hashVal) = item ' insert item

End Sub

Public Function delete(ByVal key As Integer) As DataItem
    Dim hashVal As Integer = hashFunc1(key)
    Dim stepSize As Integer = hashFunc2(key) ' get step size

    While hashArray(hashVal) <> DBNull.Value
        If hashArray(hashVal).getKey() = key Then
            Dim temp As DataItem = hashArray(hashVal) ' save item
            hashArray(hashVal) = bufItem '  delete item
            Return temp '  return item
        End If

        hashVal += stepSize ' add the step
        hashVal mod= arraySize '  for wraparound
    End While

    Return DBNull.Value
End Function

Public Function find(ByVal key As Integer) As DataItem
    Dim hashVal As Integer = hashFunc1(key)
    Dim stepSize As Integer = hashFunc2(key)

    While hashArray(hashVal) <> DBNull.Value
        If hashArray(hashVal).getKey() = key Then
            Return hashArray(hashVal)
        End If

        hashVal += stepSize
        hashVal mod= arraySize
    End While

    Return DBNull.Value
End Function

End Class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风透绣罗衣 2024-10-26 13:05:00

通常第二个哈希函数绝不能返回 0。

我不认为 DBNull.Value 继承自 DataItem。此外,您的数组被初始化为某个大小的数组,其中包含对 Nothing 的引用。

Normally the second hash function must never return 0.

I don't think DBNull.Value inherits from DataItem. Also, your array is initialized to an array of some size containing references to Nothing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文