-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwf_table.c
More file actions
246 lines (235 loc) · 6.41 KB
/
wf_table.c
File metadata and controls
246 lines (235 loc) · 6.41 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "wf_table.h"
#include "debugger.h"
unsigned long hash_func(char *word){
/** credit to djb2 algorithm by dan bernstein */
unsigned long hash = 5381;
int c = *word;
while (c) {
hash = ((hash << 5) + hash) + c;
word++;
c = *word;
}
return hash;
}
int hash_insert(wf_table *table, char *word) {
unsigned long hash = hash_func(word);
int idx = (int)((hash) % table->no_rows);
wf_item *row = table->data[idx];
/** if the row is empty, then just put it as the head */
if (!row) {
wf_item *entry = malloc(sizeof(wf_item));
if (!entry){
return EXIT_FAILURE;
}
entry->word = word;
entry->count = 1;
entry->freq = -1;
entry->hashcode = hash;
entry->next = 0;
table->data[idx] = entry;
table->no_words += 1;
table->no_entries += 1;
} else {
wf_item *ptr = row;
wf_item *prev = 0;
while (ptr) {
if (strcmp(ptr->word, word)==0) {
ptr->count += 1;
/** word was found in the table, but we do not need to store the duplicate word in the heap */
free(word);
break;
} else {
prev = ptr;
ptr = ptr->next;
}
}
if (!ptr) {
wf_item *entry = malloc(sizeof(wf_item));
if (!entry){
return EXIT_FAILURE;
}
entry->word = word;
entry->count = 1;
entry->freq = -1;
entry->hashcode = hash;
entry->next = 0;
prev->next = entry;
table->no_entries += 1;
}
table->no_words += 1;
}
/** now check if we have to rehash due to exceeding y */
double y_prime = (double)table->no_entries/table->no_rows;
if (y_prime > table->y) {
if (hash_rehash(table)==EXIT_FAILURE) {
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
int hash_rehash(wf_table *table){
int new_no_rows = 2 * table->no_rows;
wf_item **new_data = malloc(sizeof(wf_item *)*new_no_rows);
if (!new_data){
return EXIT_FAILURE;
}
for (int i = 0; i < new_no_rows; i += 1) {
new_data[i] = 0;
}
for (int i = 0; i < table->no_rows; i += 1) {
wf_item *row = (table->data)[i];
if (!row) {
continue;
} else {
wf_item *ptr = row;
wf_item *next_it = 0;
while (ptr) {
next_it = ptr->next;
int new_idx = ptr->hashcode % new_no_rows;
ptr->next = new_data[new_idx];
new_data[new_idx] = ptr;
ptr = next_it;
}
}
}
/** free the old table since it won't be used anymore, and replace it with the new table */
free(table->data);
table->no_rows = new_no_rows;
table->data = new_data;
return EXIT_SUCCESS;
}
/**
int hash_comp_freq(wf_table *table) {
for (int i = 0; i < table->no_rows; i += 1) {
wf_item *row = (table->data)[i];
if (!row) {
continue;
}
else {
wf_item *ptr = row;
while (ptr) {
ptr->freq = (double)ptr->count/table->no_words;
ptr = ptr->next;
}
}
}
return EXIT_SUCCESS;
}
*/
/** method based on the list portion of wf_table */
int hash_comp_freq(wf_table *table) {
for (int i = 0; i < table->no_entries; i += 1) {
wf_item *entry = table->list[i];
entry->freq = (double)entry->count/(double)table->no_words;
}
return EXIT_SUCCESS;
}
/**
int hash_destroy(wf_table *table) {
free(table->file_name);
for (int i = 0; i < table->no_rows; i += 1) {
wf_item *row = table->data[i];
if (!row) {
continue;
}
else {
wf_item *ptr = row;
wf_item *next_it = 0;
while(ptr) {
next_it = ptr->next;
free(ptr->word);
free(ptr);
ptr = next_it;
}
}
}
free(table->list);
free(table->data);
free(table);
return EXIT_SUCCESS;
}
*/
/** method based on assumption that the actual hashtable was freed up, and all is left is the list */
int hash_destroy(wf_table *table) {
for (int i = 0; i < table->no_entries; i += 1) {
wf_item *entry = table->list[i];
free(entry->word);
free(entry);
}
free(table->list);
free(table->file_name);
free(table);
return EXIT_SUCCESS;
}
wf_table *hash_create_table(char *file_name, int no_rows, double y) {
wf_table *table = malloc(sizeof(wf_table));
if (!table) {
return 0;
}
table->list = 0;
table->no_rows = no_rows;
table->file_name = file_name;
table->no_words = 0;
table->no_entries = 0;
table->y = y;
table->next = 0;
table->data = malloc(sizeof(wf_item *) * no_rows);
if (!table->data) {
free(table);
return 0;
}
for (int i = 0; i < no_rows; i += 1) {
table->data[i] = 0;
}
return table;
}
double hash_get(wf_table *table, char *word) {
unsigned long hash = hash_func(word);
int idx = (int)(hash % table->no_rows);
wf_item *row = table->data[idx];
if (!row) {
return 0;
}
else {
wf_item *ptr = row;
while (ptr) {
if (strcmp(ptr->word, word)==0) {
return ptr->freq;
}
ptr = ptr->next;
}
return 0;
}
}
int hash_lexical_list(wf_table *table) {
table->list = malloc(sizeof(wf_item *) * table->no_entries);
if (!table->list) {
return EXIT_FAILURE;
}
int j = 0;
for (int i = 0; i < table->no_rows; i += 1) {
wf_item *row = table->data[i];
if (!row) {
continue;
}
else {
wf_item *ptr = row;
while (ptr) {
table->list[j] = ptr;
j += 1;
ptr = ptr->next;
}
}
}
qsort((void *)(table->list), table->no_entries, sizeof(wf_item *), wf_item_comparator);
return EXIT_SUCCESS;
}
int wf_item_comparator(const void *wf_1_void, const void *wf_2_void) {
wf_item *wf_1 = *(wf_item **) wf_1_void;
wf_item *wf_2 = *(wf_item **) wf_2_void;
return strcmp(wf_1->word, wf_2->word);
}