-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenhash.c
297 lines (258 loc) · 6.28 KB
/
openhash.c
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
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
Author: Kosuke Fujimoto
Date: 12-3-2016
Objective: Open Hasing for symbol table
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "openhash.h"
////////////////////////////////////////////////////////////////////
////// function definitoin ////////
///////////////////////////////////////////////////////////////////
/* function insertNode: return void
arguments:
char *ident: identifier is declared when createNode() is invoked
Summary:
- generate key of hashtable by identifier
- create new node with the ident
- if there is no node in a nth raw in the hashtable, head-> the node
- ex) none -> new node
- else
- new node point to old node
- ex) new node -> old node
error:
if there is same identifier, the new identifier cannot be decared.
*/
void insertNode(char *ident)
{
int index = key(ident);
//printf("index: %d\n",index);
// struct node * newnode = createNode(index,ident);
node * newnode = createNode(index,ident);
// If there is no node in a raw
if(!hashTable[index].head){
newnode->offset=0;
hashTable[index].head = newnode;
hashTable[index].count = 1;
//printf("offset is: %d\n",newnode->offset);
return;
}
if(!IsThereIdent(ident)){
newnode->offset = hashTable[index].head->offset + 4;
newnode->next = (hashTable[index].head);
hashTable[index].head = newnode;
hashTable[index].count++;
//printf("offset is: %d\n",newnode->offset);
}else{
printf("There is already %s. Cannot declare the identifier twice\n", ident);
exit(0);
}
}
/*function setValue : return void
arguments:
- char * ident: target string
- int value : value the selected node would take
Summary:
setValue to the selected node
Algorithm is same as IsThereNode
*/
void setValue(char *ident, int value)
{
int index = key(ident);
// struct node *snode = hashTable[index].head;
node *snode = hashTable[index].head;
if(!snode){
//printf("there is no identifier");
}else{
while(snode != NULL)
{
if(strcmp(ident,snode->ident)==0)
{
//printf("Target %s exists\n",ident);
//printf("Set value %d to %s\n",value, ident);
snode->value = value;
break;
}
if(snode->next==NULL)
break;
snode = snode->next;
}
}
}
/*function setValue : return void
arguments:
- char * ident: target string
- int value : value the selected node would take
Summary:
setValue to the selected node
Algorithm is same as IsThereNode
*/
void setReg(char *ident, int rn)
{
int index = key(ident);
// struct node *snode = hashTable[index].head;
node *snode = hashTable[index].head;
if(!snode){
//printf("there is no identifier");
}else{
while(snode != NULL)
{
if(strcmp(ident,snode->ident)==0)
{
//printf("Target %s exists\n",ident);
//printf("Set value %d to %s\n",value, ident);
snode->rn = rn;
break;
}
if(snode->next==NULL)
break;
snode = snode->next;
}
}
}
/* function createNode : return node
arguments:
- int key : key for hash table
- char * ident : string for identifier
- int value : integer value corresponding to identifier
Summary:
create a node with the arguments above, and return it without next pointer
-> next is done in other function.
*/
//struct node * createNode(int key, char *ident)
node * createNode(int key, char *ident)
{
//struct node *Node;
node *Node;
//Node = (struct node *)malloc(sizeof(struct node));
Node = (node *)malloc(sizeof(node));
Node -> key = key;
strcpy(Node->ident,ident);
Node -> next = NULL;
return Node;
}
/* function IsThereIdent: return char *
argument:
char * target: target for searching from node list
summary:
- get index of target with key function
- search hashlist within the nth raw
- if there is same identifier, return
- else
*/
int IsThereIdent(char *target)
{
int index = key(target);
//struct node *snode = hashTable[index].head;
node *snode = hashTable[index].head;
if(!snode){
//printf("there is no such a identifier");
return 0;
}else{
while(snode != NULL)
{
if(strcmp(target,snode->ident)==0)
{
//printf("Target %s exists\n",target);
return 1;
}
if(snode->next==NULL)
break;
snode = snode->next;
}
}
//printf("There is no target in the %d raw\n", index);
return 0;
}
int getNodeValue(char *ident)
{
int index = key(ident);
//struct node *snode = hashTable[index].head;
node *snode = hashTable[index].head;
if(!snode){
printf("there is no such a identifier\n");
exit(0);
}else{
while(snode != NULL)
{
if(strcmp(ident,snode->ident)==0)
{
//printf("Target %s exists\n",ident);
return snode->value;
}
if(snode->next==NULL)
break;
snode = snode->next;
}
}
printf("%s is not declared \n", ident);
exit(0);
}
int getNodeReg(char *ident)
{
int index = key(ident);
//struct node *snode = hashTable[index].head;
node *snode = hashTable[index].head;
if(!snode){
printf("there is no such a identifier");
exit(0);
}else{
while(snode != NULL)
{
if(strcmp(ident,snode->ident)==0)
{
//printf("Target %s exists\n",ident);
return snode->rn;
}
if(snode->next==NULL)
break;
snode = snode->next;
}
}
printf("%s is not declared \n", ident);
exit(0);
}
int getOffset(char *ident)
{
int index = key(ident);
//struct node *snode = hashTable[index].head;
node *snode = hashTable[index].head;
if(!snode){
printf("there is no such a identifier");
exit(0);
}else{
while(snode != NULL)
{
if(strcmp(ident,snode->ident)==0)
{
//printf("Target %s exists\n",ident);
return snode->offset;
}
if(snode->next==NULL)
break;
snode = snode->next;
}
}
printf("%s is not declared \n", ident);
exit(0);
}
/* function key : return integer
- Add all ASCII value of each character of an identifier
- and return sum of ident % TSIZE
For exmaple
- "a" returns 97 % 19
- "Hello" returns 500 % 6
*/
int key(char * ident)
{
int key=0;
int i =0;
for(i;(int)ident[i]!=0;i++)
{
int ascii = (int)ident[i];
// printf("%d th: ident is %s ASCII is %d\n", i, &ident[i], ascii);
key+=ascii;
}
return key%TSIZE;
}