This repository was archived by the owner on Oct 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdictionary.c
More file actions
223 lines (187 loc) · 4.24 KB
/
dictionary.c
File metadata and controls
223 lines (187 loc) · 4.24 KB
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
/**
* dictionary.c
*
* Computer Science 50
* Problem Set 5
*
* Implements a dictionary's functionality.
*/
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "dictionary.h"
#define SIZE 150000
typedef struct list_node{
char word[LENGTH+1];
struct list_node *next;
} list_node;
list_node **dict;
unsigned int dictionary_size = 0;
/*
* MurmurHash2 by Austin Appleby
*/
unsigned int MurmurHash2 ( char * key, unsigned int len)
{
const unsigned int m = 0x5bd1e995;
const unsigned int seed = 0;
const int r = 24;
unsigned int h = seed ^ len;
const unsigned char * data = (const unsigned char *)key;
unsigned int k;
while (len >= 4)
{
k = data[0];
k |= data[1] << 8;
k |= data[2] << 16;
k |= data[3] << 24;
k *= m;
k ^= k >> r;
k *= m;
h *= m;
h ^= k;
data += 4;
len -= 4;
}
switch (len)
{
case 3:
h ^= data[2] << 16;
case 2:
h ^= data[1] << 8;
case 1:
h ^= data[0];
h *= m;
};
h ^= h >> 13;
h *= m;
h ^= h >> 15;
return h;
}
/**
* Returns true if word is in dictionary else false.
*/
bool check(const char* word)
{
// get length + copy
size_t word_len = strlen(word);
char *needle = malloc(sizeof(char) * word_len);
for (int i = 0; i < word_len; ++i) {
*(needle + i) = tolower(*(word + i));
}
unsigned int hash = MurmurHash2(needle, word_len) % SIZE;
list_node *l_list = *(dict + hash);
while(l_list) {
if(strcmp(l_list->word, needle) == 0) {
free(needle);
return true;
} else {
l_list = l_list->next;
}
}
free(needle);
return false;
}
/*
* Creates a new node for a linked list
*/
bool createNode(const char *buff, list_node **current) {
// create new
list_node *new_node = malloc(sizeof(list_node));
if(!new_node) {
return false;
}
// copy
strcpy(new_node->word, buff);
new_node->next = NULL;
// add either new or to the last
if(*current == NULL) {
*current = new_node;
} else {
(*current)->next = new_node;
}
++dictionary_size;
return true;
}
/*
* Loads dictionary into memory. Returns true if successful else false.
*/
bool load(const char* dictionary)
{
FILE *fp = fopen(dictionary, "r");
if(!fp) {
perror("Can't open dictionary");
return false;
}
char buff[LENGTH+1];
unsigned int hash;
dict = calloc(SIZE, sizeof(list_node *));
list_node **bucket = NULL;
list_node *l_list = NULL;
while((fscanf(fp, " %s", buff)) != EOF) {
// get bucket index
hash = MurmurHash2(buff, strlen(buff)) % SIZE;
bucket = dict + hash;
l_list = *bucket;
// bucket is empty
if(l_list == NULL) {
// try to create and fill out a node
if(!createNode(buff, bucket)) {
unload();
fclose(fp);
return false;
}
}
// bucket is not empty
else
{
// find last node
while(l_list->next) {
l_list = l_list->next;
}
// try to create and fill out a node
if(!createNode(buff, &l_list)) {
unload();
fclose(fp);
return false;
}
}
}
fclose(fp);
return true;
}
/**
* Returns number of words in dictionary if loaded else 0 if not yet loaded.
*/
unsigned int size(void)
{
if(!dictionary_size) {
return 0;
}
return dictionary_size;
}
/**
* Unloads dictionary from memory. Returns true if successful else false.
*/
bool unload(void)
{
if(!dictionary_size) {
return false;
}
list_node *current, *next;
for (unsigned int i = 0; i < SIZE; ++i) {
current = *(dict + i);
if(current == NULL) {
continue;
} else {
while(current != NULL) {
next = current->next;
free(current);
current = next;
}
}
}
free(dict);
return true;
}