VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 1935
ClientLeft = 60
ClientTop = 345
ClientWidth = 3600
LinkTopic = "Form1"
ScaleHeight = 1935
ScaleWidth = 3600
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton Command3
Caption = "与COLLECTION对象运行效率比较"
Height = 495
Left = 960
TabIndex = 2
Top = 1320
Width = 1575
End
Begin VB.CommandButton Command2
Caption = "哈希表遍历测试"
Height = 495
Left = 960
TabIndex = 1
Top = 720
Width = 1575
End
Begin VB.CommandButton Command1
Caption = "哈希表"
Height = 495
Left = 960
TabIndex = 0
Top = 120
Width = 1575
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Command1_Click()
'
Dim cHash As clsHashLK
Dim i As Long
Set cHash = New clsHashLK
cHash.AlloMem 7000
For i = 1 To 2500
cHash.Add i, i * 10 + i
Next i
For i = 1 To 2500
cHash.Add i, -(i * 10 + i)
Next i
Debug.Print cHash.Item(11)
Debug.Print cHash.Item(-27500)
Debug.Print cHash.Item(5500)
Debug.Print cHash.IsKeyExist(1), cHash.IsKeyExist(2200)
Set cHash = Nothing
End Sub
Private Sub Command2_Click()
'
Dim cHash As clsHashLK
Dim i As Long
Dim datOne As Long, keyOne As Long, blEndTrav As Boolean
Dim strOne As String, lngOne As Long
Set cHash = New clsHashLK
For i = 1 To 15
cHash.Add i, i * 2
Next i
blEndTrav = False
cHash.startTraversal
datOne = cHash.NextItem(lngOne, strOne, keyOne, blEndTrav)
i = 0
Do Until blEndTrav
Debug.Print keyOne; "->"; datOne,
i = i + 1: If i Mod 5 = 0 Then Debug.Print ""
datOne = cHash.NextItem(lngOne, strOne, keyOne, blEndTrav)
Loop
Debug.Print ""
Set cHash = Nothing
End Sub
Private Sub Command3_Click()
'
Command3.Enabled = False
Dim cHash As clsHashLK
Dim col As Collection
Dim datOne As Long, blEndTrav As Boolean
Dim sngTimer As Single
Dim i As Long
sngTimer = Timer
Set cHash = New clsHashLK
cHash.AlloMem 70000
For i = 1 To 50000
cHash.Add i, i * 10 + i
Next i
Debug.Print "哈希表插入数据结束,耗时:"; Timer - sngTimer; "秒"
sngTimer = Timer
Set col = New Collection
For i = 1 To 50000
col.Add i, CStr(i * 10 + i)
Next i
Debug.Print "COLLECTION插入数据结束,耗时:"; Timer - sngTimer; "秒"
sngTimer = Timer
For i = 1 To 50000
datOne = cHash.Item(i * 10 + i)
Next i
Debug.Print "哈希表按键访问数据结束,耗时:"; Timer - sngTimer; "秒"
sngTimer = Timer
With col
For i = 1 To 50000
datOne = .Item(CStr(i * 10 + i))
Next i
End With
Debug.Print "COLLECTION按键访问数据结束,耗时:"; Timer - sngTimer; "秒"
sngTimer = Timer
cHash.startTraversal
datOne = cHash.NextData(blEndTrav)
i = 1
Do Until blEndTrav
datOne = cHash.NextData(blEndTrav)
i = i + 1
Loop
Debug.Print "哈希表遍历数据结束,耗时:"; Timer - sngTimer; "秒", i
sngTimer = Timer
With col
For i = 1 To 50000
datOne = .Item(i)
Next i
End With
Debug.Print "COLLECTION遍历数据结束,耗时:"; Timer - sngTimer; "秒", i
Set col = Nothing
Set cHash = Nothing
Command3.Enabled = True
End Sub
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "clsHashLK"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
Private Type Hs_DataType
Key As Long
Data As Long
DataLong As Long
DataString As String
Used As Byte
End Type
Private lMem() As Hs_DataType, lMemCount As Long, lMemUsedCount As Long
Private lMem2() As Hs_DataType, lMemCount2 As Long, lMemUsedCount2 As Long
Private mTravIdxCur