日期:2014-05-17  浏览次数:21014 次

ASP哈希表类,散分求拍砖,求指导,求引玉
VBScript code

'哈希结构
'HashTable(i,0,0) = 元素数量
'HashTable(i,j,0) = 元素Key,key必须为数字
'HashTable(i,j,1) = 元素对象
Class HashTable
        Private mvHashTable(1024,1024,1)
        Private Sub Class_Initialize()
                Dim i
                For i = 1 To 1024
                        mvHashTable(i,0,0) = 0
                Next
        End Sub 
        '添加键值
        Public Sub Add(key,value)
                Dim index,iCount
                index = key Mod 1024
                
                '取索引位置的数量
                iCount = mvHashTable(index,0,0)
                
                '当前元素位置
                iCount = iCount + 1
                
                '重置计数
                mvHashTable(index,0,0) = iCount
                
                '存储对象
                mvHashTable(index,iCount,0) = key
                if isObject(value) Then
                        Set mvHashTable(index,iCount,1) = value
                Else
                        mvHashTable(index,iCount,1) = value
                End if
        End Sub
        
        '根据key取值,字符串或对象
        Public Function Find(Key)
                Dim index,iCount,i
                index = Key Mod 1024
                iCount = mvHashTable(index,0,0)

                '遍列获取对象
                For i = 1 To iCount
                        if CStr(mvHashTable(index,i,0)) = CStr(Key) Then
                                if isObject(mvHashTable(index,i,1)) Then
                                        Set Find = mvHashTable(index,i,1)
                                Else
                                        Find = mvHashTable(index,i,1)
                                End if
                                Exit Function
                        End if
                Next
        End Function

        '根据key取值,数组
        Public Sub FindArray(Key,ByRef arr)
                Dim index,iCount,iBound
                Dim i,j
                index = Key Mod 1024
                iCount = mvHashTable(index,0,0)

                '遍列获取对象
                For i = 1 To iCount
                        if CStr(mvHashTable(index,i,0)) = CStr(Key) Then
                                iBound = Ubound(mvHashTable(index,i,1))
                                Redim arr(iBound)
                                For j = 0 To iBound
                                        if isObject(mvHashTable(index,i,1)) Then
                                                Set arr(j) = mvHashTable(index,i,1)(j)
                                        Else
                                                arr(j) = mvHashTable(index,i,1)(j)
                                        End if
                                Next
                                Exit Sub
                        End if
                Next
        End Sub
        
        '清除所有
        Public Sub Clear()
                Dim i
                For i = 1 To 1024
                        mvHashTable(i,0,0) = 0
                Next
        End Sub
End Class




'测试代码
Dim ar(5),br()
ar(0) = "a"
ar(1) = "ab"
ar(2) = "abc"
ar(3) = "abcd"
ar(4) = "abcde"
ar(5) = "abcdef"

Set Ht = New HashTable
Ht.Add 100,"123"
Ht.Add 200,ar

a = ht.Find(100)

Response.Write a

ht.FindArray 200,br

For i = 0 To Ubound(br)
Response.Write br(i) & "<br>"
Next


------解决方案--------------------
每天回帖即可获得10分可用分!
等高手前来~~~~
------解决方案--------------------
也可以用 Scripting.Dictionary, ASP也可以用.Net中的ArrayList
------解决方案--------------------
帮顶,学习,,
------解决方案-----------------