* remove "\r" nonsense
[mascara-docs.git] / C / sorting.and.searching.cormen.algo / src / vph.txt
blob1f2204f46cda5f19a8444f5e5d49c2cee29708fb
1 Attribute VB_Name = "Hash"
2 Option Explicit
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
10 '   inputs:
11 '       KeyVal                key
12 '   returns:
13 '       hashed value of key
14 '   action:
15 '       Compute hash value based on KeyVal.
17     Hash = KeyVal Mod hashTableSize
18 End Function
20 Public Sub Insert(ByVal KeyVal As Variant, ByRef RecVal As Variant)
21 '   inputs:
22 '       KeyVal                key of node to insert
23 '       RecVal                record associated with key
24 '   action:
25 '       Inserts record RecVal with key KeyVal.
27     Dim p As CHash
28     Dim p0 As CHash
29     Dim bucket As Long
31     ' allocate node and insert in table
33     ' insert node at beginning of list
34     bucket = Hash(KeyVal)
35     Set p = New CHash
36     Set p0 = hashTable(bucket)
37     Set hashTable(bucket) = p
38     Set p.Nxt = p0
39     p.key = KeyVal
40     p.rec = RecVal
41 End Sub
44 Public Sub Delete(ByVal KeyVal As Variant)
45 '   inputs:
46 '       KeyVal                key of node to delete
47 '   action:
48 '       Deletes record with key KeyVal.
49 '   error:
50 '       errKeyNotFound
52     Dim p0 As CHash
53     Dim p As CHash
54     Dim bucket As Long
56    ' delete node containing key from table
58     ' find node
59     Set p0 = Nothing
60     bucket = Hash(KeyVal)
61     Set p = hashTable(bucket)
62     Do While Not p Is Nothing
63         If p.key = KeyVal Then Exit Do
64         Set p0 = p
65         Set p = p.Nxt
66     Loop
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
72         Set p0.Nxt = p.Nxt
73     Else
74         ' first node on chain
75         Set hashTable(bucket) = p.Nxt
76     End If
78     ' p will be automatically freed on return, as it's no longer referenced
80 End Sub
82 Public Function Find(ByVal KeyVal As Variant) As Variant
83 '   inputs:
84 '       KeyVal                key of node to delete
85 '   returns:
86 '       record associated with key
87 '   action:
88 '       Finds record with key KeyVal
89 '   error:
90 '       errKeyNotFound
92     Dim p As CHash
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
99         Set p = p.Nxt
100     Loop
102     If p Is Nothing Then Raise errKeyNotFound, "Hash.Find"
104     ' copy data fields to user
105     Find = p.rec
107 End Function
109 Public Sub Init(ByVal tableSize As Long)
110 '   inputs:
111 '       tableSize               size of hashtable
112 '   action:
113 '       initialize hash table
115     hashTableSize = tableSize
116     ReDim hashTable(0 To tableSize - 1)
117 End Sub
119 Public Sub Term()
120 '   action
121 '       terminate hash table
122 '       chained nodes are deleted automatically,
123 '       as they're no longer referenced
125     Erase hashTable
127 End Sub