1 Attribute VB_Name = "Hash"
4 ' hash table algorithm, object method
6 Private hashTableSize As Long ' size of hashTable
7 Private hashTable() As CHash ' hashTable(0..hashTableSize-1)
9 Public Function Hash(ByVal KeyVal As Variant) As Long
15 ' Compute hash value based on KeyVal.
17 Hash = KeyVal Mod hashTableSize
20 Public Sub Insert(ByVal KeyVal As Variant, ByRef RecVal As Variant)
22 ' KeyVal key of node to insert
23 ' RecVal record associated with key
25 ' Inserts record RecVal with key KeyVal.
31 ' allocate node and insert in table
33 ' insert node at beginning of list
36 Set p0 = hashTable(bucket)
37 Set hashTable(bucket) = p
44 Public Sub Delete(ByVal KeyVal As Variant)
46 ' KeyVal key of node to delete
48 ' Deletes record with key KeyVal.
56 ' delete node containing key from table
61 Set p = hashTable(bucket)
62 Do While Not p Is Nothing
63 If p.key = KeyVal Then Exit Do
67 If p Is Nothing Then Raise errKeyNotFound, "Hash.Delete"
69 ' p designates node to delete, remove it from list
70 If Not p0 Is Nothing Then
71 ' not first node, p0 points to previous node
75 Set hashTable(bucket) = p.Nxt
78 ' p will be automatically freed on return, as it's no longer referenced
82 Public Function Find(ByVal KeyVal As Variant) As Variant
84 ' KeyVal key of node to delete
86 ' record associated with key
88 ' Finds record with key KeyVal
94 ' find node containing key
96 Set p = hashTable(Hash(KeyVal))
97 Do While Not p Is Nothing
98 If p.key = KeyVal Then Exit Do
102 If p Is Nothing Then Raise errKeyNotFound, "Hash.Find"
104 ' copy data fields to user
109 Public Sub Init(ByVal tableSize As Long)
111 ' tableSize size of hashtable
113 ' initialize hash table
115 hashTableSize = tableSize
116 ReDim hashTable(0 To tableSize - 1)
121 ' terminate hash table
122 ' chained nodes are deleted automatically,
123 ' as they're no longer referenced