-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashtable.c
More file actions
118 lines (96 loc) · 1.73 KB
/
hashtable.c
File metadata and controls
118 lines (96 loc) · 1.73 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
#include <stdbool.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define WORD_LEN 256
#define TABLE_SIZE 676
typedef struct node
{
char word[WORD_LEN + 1];
struct node *next;
} node;
int djb2(const char *str)
{
unsigned long hash = 3;
int c;
while (c = *str++)
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash % 676;
}
void init_table(node *table[])
{
for (int i = 0; i < TABLE_SIZE; i++)
table[i] = NULL;
}
long int load(const char *dict_name, node *table[])
{
FILE *dict;
if ((dict = fopen(dict_name, "r")) == NULL)
return false;
long int total_words = 0;
char word[WORD_LEN];
while (fscanf(dict, "%s", word) != EOF)
{
int index = djb2(word);
node *n = malloc(sizeof(node));
for (int i = 0; word[i] != '\0'; i++)
word[i] = tolower(word[i]);
n->next = NULL;
strcpy(n->word, word);
if (table[index] == NULL)
{
table[index] = n;
}
else
{
n->next = table[index];
table[index] = n;
}
total_words++;
}
return total_words;
}
bool find(node *table[], const char *str)
{
node *n = table[djb2(str)];
while (n != NULL)
{
if (strcmp(str, n->word) == 0)
return true;
n = n->next;
}
return false;
}
void free_list(node *head)
{
struct node *tmp;
while (head != NULL)
{
tmp = head;
head = head->next;
free(tmp);
}
}
double similarity(node *table[], char *str)
{
node *n = table[djb2(str)];
// double
// while (n != NULL)
}
void unload(node *table[])
{
for (int i = 0; i < TABLE_SIZE; i++)
{
free_list(table[i]);
}
}
void print_branch(node *ptr)
{
node *tmp = ptr;
while (tmp != NULL)
{
printf("%s\n", tmp->word);
tmp = tmp->next;
}
}