-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathHash.cpp
253 lines (212 loc) · 6.41 KB
/
Hash.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
///////////////////////////////////////////////////////////////////////////////
// Title : Hash
// Date : March 20, 2002
// Author : Kristina Klinkner
// Description : creates a hash table of pointers to strings and their
// states for use with CSSR
///////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2002 Kristina Klinkner
// This file is part of CSSR
//
// CSSR is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// CSSR is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with CSSR; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//////////////////////////////////////////////////////////////////////////////
#include "Hash.h"
///////////////////////////////////////////////////////////////////////////////
HashTable::HashTable()
{
for(int i=0;i<HASHSIZE;i++)
{
m_data[i] = NULL;
}
}
HashTable::~HashTable()
{
for (int i =0;i<HASHSIZE;i++)
{
if (m_data[i] !=NULL)
{
HashTableEntry* temp1;
HashTableEntry* temp2 = m_data[i]->m_nextPtr;
while(temp2!=NULL)
{
temp1 = temp2;
temp2 = temp2->m_nextPtr;
//delete hash entries in list
delete temp1;
temp1 = NULL;
}
//delete node for main hash entry
delete m_data[i];
m_data[i] = NULL;
}
}
}
int HashTable::Hash(ulong key)
{
int hashValue = key % HASHSIZE;
return hashValue;
}
////////////////////////////////////////////////////////////////
//Function: HashTable::Insert
//Purpose: inserts a new element in the hash table, if there is
// already an element at the appropriate index, puts new
// element at the front of the list.
//In parameter: new element and it's parent state
////////////////////////////////////////////////////////////////
void HashTable::Insert(StringElem* elem, State* state)
{
if(elem == NULL)
{
cerr << "Cannot insert null pointer into Hash Table\n";
exit(1);
}
ulong key = CreateKey(elem->m_string);
int hashValue = Hash(key);
//create a new HashTableEntry and put it at the front of the list
if((hashValue >= 0) && (hashValue < HASHSIZE))
{
HashTableEntry* temp1 = new HashTableEntry;
if (temp1 == NULL)
{
cerr << "Out of memory." << endl;
exit(1);
}
temp1->m_stringPtr = elem;
temp1->m_statePtr = state;
HashTableEntry* temp2;
temp2 = m_data[hashValue];
m_data[hashValue] = temp1;
temp1->m_nextPtr = temp2;
}
else
cerr << "Invalid hash value "<<endl;
}
/////////////////////////////////////////////////////////////////
//Function: Hash::Which State
//Purpose: checks to see which state string is in; returns
// a pointer to the state
//In parameter: string to check
//Return value: pointer to address of appropriate state
////////////////////////////////////////////////////////////////
State* HashTable::WhichState(char* string)
{
if (string == '\0')
{
cerr << "Cannot check matching state for empty string\n";
exit(1);
}
ulong currentKey = CreateKey(string);
int hashValue = Hash(currentKey);
State* statePtr = NULL;
//traverse list
HashTableEntry* temp = m_data[hashValue];
while((temp != NULL) && (statePtr == NULL))
{
if(strcmp(string,(temp->m_stringPtr->m_string))==0)
statePtr = temp->m_statePtr;
temp = temp->m_nextPtr;
}
return statePtr;
}
/////////////////////////////////////////////////////////////////
//Function: Hash::Which StateNumber
//Purpose: checks to see which state string is in; returns the
// number of the state
//In parameter: string to check
//Return value: assigned number of appropriate state
////////////////////////////////////////////////////////////////
int HashTable::WhichStateNumber(char* string)
{
if (string == '\0')
{
cerr << "Cannot check matching state for empty string\n";
exit(1);
}
ulong currentKey = CreateKey(string);
int hashValue = Hash(currentKey);
State* statePtr = NULL;
//traverse list
HashTableEntry* temp = m_data[hashValue];
while((temp != NULL) && (statePtr == NULL))
{
if(strcmp(string,(temp->m_stringPtr->m_string))==0)
statePtr = temp->m_statePtr;
temp = temp->m_nextPtr;
}
if(statePtr)
return statePtr->getNumber();
else
return NULL_STATE;
}
ulong HashTable::CreateKey(char* string)
{
ulong key = 0;
const char *ptr = string;
while(*ptr)
key += (key <<5) + *ptr++;
return key;
}
void HashTable::Print()
{
HashTableEntry* temp;
for (int i = 0; i < HASHSIZE; i++)
{
temp = m_data[i];
while(temp!= NULL)
{
cout << temp->m_stringPtr->m_string<<endl;
temp = temp->m_nextPtr;
}
}
}
void HashTable::RemoveString(char* string)
{
if (string == '\0')
{
cerr << "Cannot check matching state for empty string\n";
exit(1);
}
ulong currentKey = CreateKey(string);
int hashValue = Hash(currentKey);
State* statePtr = NULL;
StringElem* stringPtr = NULL;
//traverse list
HashTableEntry* temp = m_data[hashValue];
HashTableEntry* temp2;
while((temp != NULL) && (strcmp(string,(temp->m_stringPtr->m_string))!=0))
{
temp2 = temp;
temp = temp->m_nextPtr;
}
if(temp!=NULL)
{
//when proper pointers have been found
stringPtr = temp->m_stringPtr;
statePtr = temp->m_statePtr;
//remove string from state
statePtr->RemoveString(stringPtr);
//remove hash table entry
if(temp == m_data[hashValue])
m_data[hashValue] = temp->m_nextPtr;
else
temp2->m_nextPtr = temp->m_nextPtr;
//remove entry from table
delete temp;
}
return;
}