日期:2014-05-17 浏览次数:21014 次
'哈希结构 '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