-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgram.c
More file actions
68 lines (61 loc) · 1.56 KB
/
gram.c
File metadata and controls
68 lines (61 loc) · 1.56 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "wordCounter.h"
void initHashTable(Gram **table) {
for (int i = 0; i < HASH_SIZE; i++) {
table[i] = 0;
}
}
Gram *lookupWord(Gram **table, char *word) {
Gram *entry = table[hashString(word)];
while (entry != NULL) {
if (strcmp(entry->word, word) == 0) {
return entry;
} else {
entry = entry->next;
}
}
return NULL;
}
int addWord(Gram **table, char *word) {
Gram *entry = lookupWord(table, word);
if (entry == NULL) {
int idx = hashString(word);
Gram *old_entry = table[idx];
Gram *new_entry = malloc(sizeof(Gram));
new_entry->word = malloc(sizeof(char) * (strlen(word)+1));
strcpy(new_entry->word, word);
new_entry->count = 1;
new_entry->next = old_entry;
table[idx] = new_entry;
} else {
entry->count++;
}
return -1;
}
Gram **findMax(Gram **table) {
Gram **maxtable=malloc(sizeof(Gram*)*MAX_TABLE_SIZE);
for (int i = 0; i < MAX_TABLE_SIZE; i++) {
maxtable[i] = NULL;
}
for (int i = 0; i < HASH_SIZE; i++) {
for (Gram *entry = table[i]; entry != NULL; entry=(entry->next)) {
int j = MAX_TABLE_SIZE-1;
while(j > 0 &&
(maxtable[j] == NULL ||
(maxtable[j]->count) < (entry->count))) {
maxtable[j] = maxtable[j-1];
j--;
}
if (j < MAX_TABLE_SIZE-1) {
if (maxtable[j] == NULL || (maxtable[j]->count) < (entry->count)) {
maxtable[j] = entry;
} else {
maxtable[j+1] = entry;
}
}
}
}
return maxtable;
}