Created
November 13, 2017 11:08
-
-
Save anonymous/8c88e9c5e37edbb06524ec4871e13846 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Option Explicit | |
Call Main() | |
Sub Main() | |
Dim hash | |
Set hash = New CHashtable | |
Call hash.Init(1000000) | |
'Set hash = CreateObject("Scripting.Dictionary") | |
'Set hash = CreateObject("System.Collections.Hashtable") | |
Dim time | |
time = Timer() | |
Dim i | |
For i = 1 To 1000000 | |
hash(i) = i | |
If i Mod 10000 = 0 Then | |
Call WriteLine(i) | |
End If | |
Next | |
time = Timer() - time | |
Call WriteLine(FormatNumber(time, 1)) | |
End Sub | |
Class CHashtable | |
Private m_Table | |
Public Sub Init(inSize) | |
ReDim m_Table(inSize) | |
End Sub | |
Public Default Property Get Item(inKey) | |
Item = GetItem(m_Table(CalcHash(inKey)), inKey) | |
End Property | |
Public Property Let Item(inKey, inValue) | |
Call AddItem(m_Table(CalcHash(inKey)), inKey, inValue) | |
End Property | |
Private Function CalcHash(inKey) | |
CalcHash = (inKey And &H7FFFFFFF&) Mod (UBound(m_Table) + 1) | |
End Function | |
Private Function GetItem(inBucket, inKey) | |
If IsEmpty(inBucket) Then | |
GetItem = Empty | |
Exit Function | |
End If | |
Dim i | |
For i = 0 To UBound(inBucket) Step 2 | |
If inBucket(i) = inKey Then | |
GetItem = inBucket(i + 1) | |
Exit Function | |
End If | |
Next | |
GetItem = Empty | |
End Function | |
Private Sub AddItem(outBucket, inKey, inValue) | |
If IsEmpty(outBucket) Then | |
outBucket = Array(inKey, inValue) | |
Exit Sub | |
End If | |
Dim i | |
For i = 0 To UBound(outBucket) Step 2 | |
If outBucket(i) = inKey Then | |
outBucket(i + 1) = inValue | |
Exit Sub | |
End If | |
Next | |
ReDim Preserve outBucket(UBound(outBucket) + 2) | |
outBucket(UBound(outBucket) - 1) = inKey | |
outBucket(UBound(outBucket)) = inValue | |
End Sub | |
End Class | |
Sub WriteLine(inValue) | |
Call WScript.StdOut.WriteLine(inValue) | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment