-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSymTab.c
261 lines (228 loc) · 6.18 KB
/
SymTab.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
/**
* @file SymTab.c
* @author Dakota Kallas
* @brief A symbol table class that interacts with tables
* storing information using sepereate chaining hashing.
* @version 0.1
* @date 2022-09-12
*
* @copyright Copyright (c) 2022
*
*/
#include "SymTab.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int hash(SymTab *table, char *name);
/**
* @brief Create a SymTab object
*
* @param size is an estimate of the number of items that will be stored in the symbol table
* @return SymTab*
*/
SymTab *createSymTab(int size)
{
int primes[25] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
// Find the lowest prime number from the current number of items estimated to be in the list
for (int i = 0; i < 25; i++)
{
if (size <= primes[i])
{
size = primes[i];
break;
}
}
SymTab *table = malloc(sizeof(SymTab));
table->size = size;
table->current = NULL;
table->contents = (SymEntry **)malloc(size * sizeof(SymEntry *));
for (int i = 0; i < size; i++)
{
table->contents[i] = NULL;
}
return table;
}
/**
* @brief initialize the current pointer of the SymTab object
*
* @param table represents the table we are iterating through
* @if the symbol table is empty, return 0
* otherwise set current to the "first" (name, attribute) pair in the symbol table and return 1
*/
int startIterator(SymTab *table)
{
for (int i = 0; i < table->size; i++)
{
if (table->contents[i] != NULL)
{
table->current = table->contents[i];
return 1;
}
}
return 0;
}
/**
* @brief Find if a Name exists in a SymTab object
*
* @param table represents the Symbol Table
* @param name represents the name we are searching for
* @if name is in the symbol table, set current to reference the (name, attribute)
* pair and return 1 otherwise do not change current and return 0
*/
int findName(SymTab *table, char *name)
{
int hashValue = hash(table, name);
SymEntry *currentEntry = table->contents[hashValue];
while (currentEntry != NULL)
{
if (strcmp(currentEntry->name, name) == 0)
{
table->current = currentEntry;
return 1;
}
currentEntry = currentEntry->next;
}
return 0;
}
/**
* @brief if name is not in the symbol table, a copy of name is added to the symbol table
* with a NULL attribute, set current to reference the new (name, attribute) pair
*
* @param table represents the SymTab we are inserting into
* @param name represents the string name we are trying to insert
* @return 1 if the name was not already in the table, otherwise return 0
*/
int enterName(SymTab *table, char *name)
{
// Check to see if the name is already in the table
if (findName(table, name) == 1)
{
return 0;
}
int hashValue = hash(table, name);
SymEntry *entry = malloc(sizeof(SymEntry));
entry->name = strdup(name);
entry->next = table->contents[hashValue];
entry->attribute = NULL;
table->contents[hashValue] = entry;
table->current = entry;
return 1;
}
/**
* @brief a hasing function used to determine where to store/find a name in the SymTab object
*
* @param table represents the table we are hasing for
* @param name is the string that we are hasing
* @return int value of the hash
*/
int hash(SymTab *table, char *name)
{
int i = 0;
for (int j = 0; name[j]; j++)
i += name[j];
return i % table->size;
}
/**
* @brief determine if the current value of the table is not NULL
*
* @param table represents the table we are checking the current value of
* @return 1 if current is not NULL, otherwise return 0
*/
int hasCurrent(SymTab *table)
{
if (table->current != NULL)
{
return 1;
}
return 0;
}
/**
* @brief Get the Current SymEntry object's name
* PRE: hasCurrent() == 1
*
* @param table represents the table we are pulling the name from
* @return char* that represents the current SymEntry's name
*/
char *getCurrentName(SymTab *table)
{
return table->current->name;
}
/**
* @brief Get the Current SymEntry object's attribute
* PRE: hasCurrent() == 1
*
* @param table represents the table we are pulling the attribute from
* @return void* that represents the current SymEntry's attribute
*/
void *getCurrentAttr(SymTab *table)
{
return table->current->attribute;
}
/**
* @brief Set the Current SymEntry object's attribute
* PRE: hashCurrent() == 1
*
* @param table represents the table we are setting the attribute in
* @param attr represents what we are changing the attribute to
*/
void setCurrentAttr(SymTab *table, void *attr)
{
table->current->attribute = attr;
}
/**
* @brief interate to the next SymEntry in the SymTab current array
*
* @param table represents the table we are iterating through
* @return if all (name, attribute) pairs have been visited since the last call
* to startIterator, return 0 otherwise set current to the "next" (name, attribute)
* pair and return 1
*/
int nextEntry(SymTab *table)
{
if (!table->current)
{
return 0;
}
if (table->current->next != NULL)
{
table->current = table->current->next;
return 1;
}
int hashValue = hash(table, table->current->name) + 1;
for (int i = hashValue; i < table->size; i++)
{
SymEntry *currentEntry = table->contents[i];
if (currentEntry != NULL)
{
table->current = currentEntry;
return 1;
}
}
return 0;
}
/**
* @brief recover space created by the symbol table functions no functions should use
* the symbol table after it is destroyed
*
* @param table represents the SymTab object to be destroyed
*/
void destroySymTab(SymTab *table)
{
if (startIterator(table) == 0)
{
free(table->contents);
free(table);
return;
}
SymEntry *currentEntry = table->current;
SymEntry *nextSymEntry = NULL;
while (nextEntry(table) != 0)
{
nextSymEntry = table->current;
free(currentEntry->name);
free(currentEntry);
currentEntry = nextSymEntry;
}
free(table->contents);
free(table);
}