4 Persistable = 0 'NotPersistable
5 DataBindingBehavior = 0 'vbNone
6 DataSourceBehavior = 0 'vbNone
7 MTSTransactionMode = 0 'NotAnMTSObject
9 Attribute VB_Name = "CHash"
10 Attribute VB_GlobalNameSpace = False
11 Attribute VB_Creatable = True
12 Attribute VB_PredeclaredId = False
13 Attribute VB_Exposed = False
16 ' hash table, array Method
18 Private Node As CNode ' class for allocating nodes
20 Private NextNode() As Long ' next node
21 Private key() As Variant ' keys
22 Private rec() As Variant ' record
24 Private HashTableSize As Long ' Size of HashTable
25 Private HashTable() As Long ' HashTable(0..HashTableSize-1)
26 Private GrowthFactor As Single ' growth factor
28 Private Function Hash(ByVal KeyVal As Variant) As Long
34 ' Compute hash value based on KeyVal.
36 Hash = KeyVal Mod HashTableSize
39 Public Sub Insert(ByVal KeyVal As Variant, ByRef RecVal As Variant)
41 ' KeyVal key of node to insert
42 ' RecVal record associated with key
44 ' Inserts record RecVal with key KeyVal.
50 ' allocate node and insert in table
52 ' insert node at beginning of list
55 If p > UBound(key) Then
56 ReDim Preserve NextNode(1 To UBound(NextNode) * GrowthFactor)
57 ReDim Preserve key(1 To UBound(key) * GrowthFactor)
58 ReDim Preserve rec(1 To UBound(rec) * GrowthFactor)
60 p0 = HashTable(bucket)
68 Public Sub Delete(ByVal KeyVal As Variant)
70 ' KeyVal key of node to delete
72 ' Deletes record with key KeyVal.
80 ' delete node containing key from table
87 If key(p) = KeyVal Then Exit Do
91 If p = 0 Then Raise errKeyNotFound, "CHash.Delete"
93 ' p designates node to delete, remove it from list
95 ' not first node, p0 points to previous node
96 NextNode(p0) = NextNode(p)
99 HashTable(bucket) = NextNode(p)
106 Public Function Find(ByVal KeyVal As Variant) As Variant
108 ' KeyVal key of node to delete
110 ' record associated with key
112 ' Finds record with key KeyVal
118 ' find node containing key
120 p = HashTable(Hash(KeyVal))
122 If key(p) = KeyVal Then Exit Do
126 If p = 0 Then Raise errKeyNotFound, "CHash.Find"
132 ByVal TableSizeVal As Long, _
133 ByVal InitialAllocVal As Long, _
134 ByVal GrowthFactorVal As Single)
137 HashTableSize = TableSizeVal
138 GrowthFactor = GrowthFactorVal
140 ' initialize hash table
141 ReDim HashTable(0 To TableSizeVal - 1)
144 ReDim key(1 To InitialAllocVal)
145 ReDim NextNode(1 To InitialAllocVal)
146 ReDim rec(1 To InitialAllocVal)
148 Node.Init InitialAllocVal, GrowthFactorVal
151 Public Sub Class_Terminate()
153 ' terminate hash table
154 ' chained nodes are deleted automatically,
155 ' as they're no longer referenced