-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
296 lines (258 loc) · 6.5 KB
/
util.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
/*******************************************************
* Util.c
*
* Library of useful functions (tries)
* Brittany Deventer
********************************************************/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "util.h"
#define ALPHA 27
int main (void)
{
insert("by");
insert("don't");
insert("bee");
insert("do");
search("bee");
if(search("bee") == true)
{
printf("found bee!\n");
}
}
/**
* Setup trie structure and necessary functions for use in load/check functions.
* Sources:
* http://programminggeeks.com/c-code-for-radix-tree-or-trie/
*/
// define node structure
typedef struct node
{
bool word;
struct node *children[ALPHA];
} node;
//declare root node
struct node *root = NULL;
/*Creates a new node*/
struct node* create_node()
{
//malloc some space for this node
struct node *p = (struct node*)malloc(sizeof(struct node));
//make all elements of children array == NULL
for(int i=0; i<ALPHA; i++)
{
p->children[i] = NULL;
}
//initiate word to false
p->word = false;
return p;
}
/*Inserts keys (strings from dictionary) into the trie*/
void insert(char key[])
{
int length = strlen(key);
int index;
int level;
//creat root node
if(root == NULL)
{
root = create_node(); //root = p
}
struct node *p = root; //now pointer p is pointing to the root node
//iterate through the word
for(level = 0; level < length; level++)
{
printf("%c\n", key[level]);
//at each level, find index to the corresponding character
if(key[level] != '\'')
{
index = tolower(key[level]) - 'a';
}
else //if character is an apostrophe
{
index = 26;
}
//if index is NULL
if(p->children[index] == NULL)
{
//create a new node within this index
p->children[index] = create_node();
}
//change to the next level (this will make sure that even if index has already been made, we still move to the next level
p = p->children[index];
}
}
/***
* Search function for use in check function.
****/
bool search(char key[])
{
struct node *p = root;
int length = strlen(key);
int level;
int index;
for(level = 0; level < length; level ++)
{
printf("%c\n", key[level]);
//at each level, find index to the corresponding character
if(key[level] == '\0') //if end of word
{
p->word = true;
}
else if(key[level] != '\'') //if regular alphabetic character
{
index = tolower(key[level]) - 'a';
}
else //if character is an apostrophe
{
index = 26;
}
//if index isn't NULL
if(p->children[index] != NULL)
{
//go to the next node
p = p->children[index];
}
//otherwise, reached end of word
else
{
//leave the for loop b/c the word has reached NULL
break;
}
}
//if truly reached end of word
if(key[level] == '\0')
{
return true;
}
else
{
return false;
}
}
/**
* Unloads dictionary from memory. Returns true if successful else false.
*/
bool unload(void)
{
free(d);
if(!free)
{
return false;
}
free(p);
if(!free)
{
return false;
}
free(root);
if(!free)
{
return false;
}
free(s);
if(!free)
{
return false;
}
return true;
printf("UNLOAD complete\n");
}
/**
* Free's each node within a trie dictionary
*/
void freeTrie(node* current)
{
current = root;
int count = 0;
for(int i = ALHPA, i >= ALPHA, i--)
{
//iterate to the end of the trie ... level 27
if(current->children[i] == NULL)
{
count++;
node *temp = current; // point temp at whats to be freed
current = current->children[i-1];// point current to the index one to the left (26, 25...)
free(temp);// free what's stored at temp
}
else
{
counter = counter->children[index];
}
}
}
/**
* Free's each node within a trie dictionary
*/
bool freeTrie(node* current)
{
int count = 0;
int p = ALPHA-1;
printf("where's the problem?\n");
//start in the last index of root node
for(int i = ALPHA; i > 0; i--)
{
//points to last index in root node
current = current->children[p];
if (current->children[p] != NULL)
{
//Once at the
for(int n = 0; n < p; n++)
{
printf("here?\n");
//still pointing at
if(current->children[] != NULL)
{
count++;
current = current->children[i-1];
}
// Get to the bottom of the branch
//when you find the end of the branch
else
{
//node *temp = current; // point temp at whats to be freed
current = root;
// free what's stored at temp
//free(temp);
}
}
} //need to get to the end of each root index then return to the next root index one to the left
}
if(root == NULL)
{
//free(root);
return true;
}
free(current);
return false;
}
/*****************
* Notes:
* 10/28: trie seems to be working!! Even the apostrophe has been taken care of. Don't know if these arguments correspond to those in speller.c
* 10/29: search function seems to be working!! (have not checked with gdb)
*****************/
// we must have found a whole word
else if (index > 0)
{
// terminate current word
word[index] = '\0';
// update counter
words++;
// check word's spelling
getrusage(RUSAGE_SELF, &before);
bool misspelled = !check(word);
getrusage(RUSAGE_SELF, &after);
// update benchmark
time_check += calculate(&before, &after);
// print word if misspelled
if (misspelled)
{
printf("%s\n", word);
misspellings++;
}
// prepare for next word
index = 0;
}