开发者

double hashtable or double hashing hashtable in vb.net

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 DataI开发者_C百科tem
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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜