-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathHash2.cpp
190 lines (164 loc) · 4.98 KB
/
Hash2.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
///////////////////////////////////////////////////////////////////////////////
// Title : Hash2
// Date : March 20, 2002
// Author : Kristina Klinkner
// Description : creates a hash table of symbols and their index 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 "Hash2.h"
///////////////////////////////////////////////////////////////////////////////
HashTable2::HashTable2()
{
for(int i=0;i<HASHSIZE2;i++)
m_data[i] = NULL;
}
HashTable2::~HashTable2()
{
for (int i =0;i<HASHSIZE2;i++)
{
if (m_data[i] !=NULL)
{
HashTable2Entry* temp1;
HashTable2Entry* temp2 = m_data[i]->m_nextPtr;
while(temp2!=NULL)
{
temp1 = temp2;
temp2 = temp2->m_nextPtr;
//delete hash entries in list
if(temp1->m_string)
delete temp1->m_string;
delete temp1;
temp1 = NULL;
}
//delete node for main hash entry
delete m_data[i];
m_data[i] = NULL;
}
}
}
int HashTable2::Hash(ulong key)
{
int hashValue = key % HASHSIZE2;
return hashValue;
}
////////////////////////////////////////////////////////////////
//Function: HashTable2::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 string and index
////////////////////////////////////////////////////////////////
void HashTable2::Insert(char* string, int index)
{
if(string == NULL)
{
cerr << "Cannot insert null pointer into Hash Table\n";
exit(1);
}
char* tempstring = NULL;
tempstring = new char[strlen(string) + 1];
if (tempstring == NULL)
{
cerr << "Out of memory." << endl;
exit(1);
}
strcpy(tempstring, string);
ulong key = CreateKey(string);
int hashValue = Hash(key);
//create a new HashTable2Entry and put it at the front of the list
if((hashValue >= 0) && (hashValue < HASHSIZE2))
{
HashTable2Entry* temp1 = NULL;
temp1 = new HashTable2Entry;
if (temp1 == NULL)
{
cerr << "Out of memory." << endl;
exit(1);
}
temp1->m_string = tempstring;
temp1->m_index = index;
HashTable2Entry* temp2;
temp2 = m_data[hashValue];
m_data[hashValue] = temp1;
temp1->m_nextPtr = temp2;
}
else
cerr << "Invalid hash value "<<endl;
}
/////////////////////////////////////////////////////////////////
//Function: HashTable2::WhichIndex
//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
////////////////////////////////////////////////////////////////
int HashTable2::WhichIndex(char* string)
{
int index;
if (string == '\0')
{
cerr << "Cannot check matching state for empty string\n";
exit(1);
}
ulong currentKey = CreateKey(string);
int hashValue = Hash(currentKey);
index = -1;
//traverse list
HashTable2Entry* temp = m_data[hashValue];
while((temp != NULL) && (index == -1))
{
if(strcmp(string,(temp->m_string))==0)
index = temp->m_index;
temp = temp->m_nextPtr;
}
if (index == -1)
{
cerr << "HashTable2::WhichIndex: String or symbol not in table.\n"
<< "A string/history has been encountered in the data which has"
<< "not been recorded in the set of states. "
<< "See 'ReadMe' file for details";
exit(1);
}
return index;
}
ulong HashTable2::CreateKey(char* string)
{
ulong key = 0;
const char *ptr = string;
while(*ptr)
key += (key <<5) + *ptr++;
return key;
}
void HashTable2::Print()
{
HashTable2Entry* temp;
for (int i = 0; i < HASHSIZE2; i++)
{
temp = m_data[i];
while(temp!= NULL)
{
cout << temp->m_string<< "\t" << temp->m_index << endl;
temp = temp->m_nextPtr;
}
}
}