-
Notifications
You must be signed in to change notification settings - Fork 34
/
hash.cpp
285 lines (242 loc) · 6.23 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/***********************************************************************
Copyright (C) 1991,
Virginia Polytechnic Institute & State University
This program was originally written by Mr. Hyung K. Lee
under the supervision of Dr. Dong S. Ha, in the Bradley
Department of Electrical Engineering, VPI&SU, in 1991.
This program is released for research use only. This program,
or any derivative thereof, may not be reproduced nor used
for any commercial product without the written permission
of the authors.
For detailed information, please contact to
Dr. Dong S. Ha
Bradley Department of Electrical Engineering
Virginia Polytechnic Institute & State University
Blacksburg, VA 24061
Ph.: (540) 231-4942
Fax: (540) 231-3362
E-Mail: [email protected]
Web: http://www.ee.vt.edu/ha
REFERENCE:
H. K. Lee and D. S. Ha, "On the Generation of Test Patterns
for Combinational Circuits," Technical Report No. 12_93,
Dep't of Electrical Eng., Virginia Polytechnic Institute
and State University.
***********************************************************************/
/**************************** HISTORY **********************************
atalanta: version 1.0 H. K. Lee, 8/15/1991
atalanta: version 1.1 H. K. Lee, 10/5/1992
atalanta: version 2.0 H. K. Lee, 6/30/1997
***********************************************************************/
/*------------------------------------------------------------------
filename hash.c
contains hash fuctions needed for a symbol table
keyvalue() returns key-value of a given symbol.
InitHash() initializes hash arrays into NULL.
FindHash() finds a symbol for a given hash table.
InsertHash() adds a symbol to the given hash table.
Find_and_Insert_Hash() searches and inserts
a symbol to the hash table.
CheckHash() checks a given hash entry is right or not.
ReHash() rehashes the given entry if it is not correct.
--------------------------------------------------------------------*/
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
/*#ifdef WIN32*/
#include <string.h> //for windows
// #else
// #include <strings.h> //for linux
// #endif
#include "error.h"
#include "hash.h"
#include "parameter.h"
#include "define.h"
#include "macro.h"
#define EDIGIT 4
#define BASIS ('Z'-'A'+19)
#define NOT_ALLOCATED (-1) /* index of non-allocated gate */
#define is_white_space(c) (c==' ' || c=='\n'|| c=='\t')
#define is_delimiter(c) (c=='=' || c==',' || c=='(' || c==')')
#define is_valid(c) ((c>='0' && c<='9') || \
(c>='A' && c<='Z') || \
(c>='a' && c<='z') || \
(c=='_') || \
(c=='.'))
/*------keyvalue---------------------------------------------------
computes the key value of a given string.
-------------------------------------------------------------------*/
int keyvalue(char s[])
{
register char c;
register int i, j, val = 0;
int multi = 1;
i = 0;
while ((c = s[i]) != '\0')
{
if (c >= '0' && c <= '9')
{
j = c - '0' + 1;
}
else if (c >= 'a' && c <= 'z')
{
j = c - 'a' + 11;
}
else if (c >= 'A' && c <= 'Z')
{
j = c - 'A' + 11;
}
else if (c == '_')
{
j = 'Z' - 'A' + 12;
}
else if (c == '#')
{
j = 'Z' - 'A' + 13;
}
else if (c == '@')
{
j = 'Z' - 'A' + 14;
}
else if (c == '$')
{
j = 'Z' - 'A' + 15;
}
else if (c == '/')
{
j = 'Z' - 'A' + 16;
}
else if (c == '<' || c == '>')
{
j = 'Z' - 'A' + 17;
}
else if (c == '[' || c == ']')
{
j = 'Z' - 'A' + 18;
}
else
{
j = 'Z' - 'A' + 18;
}
val += j * multi;
i++;
if (i mod EDIGIT == 0)
{
multi = 1;
}
else
{
multi *= BASIS;
}
}
return(val);
}
/*------InitHash--------------------------------------------------*/
void InitHash(HASHPTR pArrSymbolTable[], register int iSize)
{
while (--iSize >= 0)
pArrSymbolTable[iSize] = NULL;
}
/*------FindHash----------------------------------------------------
search the given hash table to find a given string.
inputs: symbol; string to be examined
index; index number of the gate array
outputs: returns the hash pointer. If not found, returns NULL.
remark: if key==0, computes keyvalue, else uses given key value
------------------------------------------------------------------*/
HASHPTR FindHash(HASHPTR pArrSymbolTable[], int iSize, char symbol[], int key)
{
struct HASH *hp;
int h;
/* symbol coding */
if (key == 0)
{
key = keyvalue(symbol);
}
/* hash function: h(val) = val mod size */
h = key mod iSize;
/* check whether the symbol is hashed */
hp = pArrSymbolTable[h];
while (hp != NULL)
{
if (key == hp->key)
{
if (strcmp(hp->symbol, symbol) == 0)
{
break;
}
}
hp = hp->next;
}
return(hp);
}
/*------astrcpy: allocate and copy a string -----------------*/
char * astrcpy(char *d, char *s)
{
int length;
length = strlen(s);
if ((d = MALLOC(char, length + 1)) != NULL)
{
strcpy(d, s);
}
return(d);
}
/*------ hashalloc: allocates and initializes a type HASH ------*/
HASHPTR hashalloc()
{
HASHPTR hash;
if ((hash = MALLOC(HASHTYPE, 1)) != NULL)
{
hash->key = 0;
/* hash->index=0; */
hash->gate = NULL;
hash->symbol = NULL;
hash->next = NULL;
}
return(hash);
}
/*------InsertHash-------------------------------------------------
adds a symbol with index at the symbol table.
A new symbol is added at the top of the linked list.
inputs: symbol; string to be examined
index; index number
outputs: symbol table pointer of the symbol
------------------------------------------------------------------*/
HASHPTR InsertHash(HASHPTR table[], int size, char symbol[], int key)
{
register struct HASH *hp;
register int h;
/* symbol coding */
if (key == 0)
{
key = keyvalue(symbol);
}
h = key mod size;
if ((hp = hashalloc()) == NULL)
{
printFatalError(MEMORYERROR);
}
hp->key = key;
hp->next = table[h];
if ((hp->symbol = astrcpy(hp->symbol, symbol)) == NULL)
{
printFatalError(MEMORYERROR);
}
table[h] = hp;
return(hp);
}
/*------Find_and_Insert_Hash--------------------------------------*/
HASHPTR Find_and_Insert_Hash(HASHPTR table[], int size, char symbol[], int key)
{
struct HASH *hp;
if (key == 0)
{
key = keyvalue(symbol);
}
if ((hp = FindHash(table, size, symbol, key)) == NULL)
{
hp = InsertHash(table, size, symbol, key);
}
return(hp);
}