-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdictionary.c
151 lines (133 loc) · 3.26 KB
/
dictionary.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
// Implements a dictionary's functionality
#include <ctype.h>
#include <stdbool.h>
#include <strings.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "dictionary.h"
// Represents a node in a hash table
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
}
node;
// TODO: Choose number of buckets in hash table
const unsigned int N = 25876; // Looks like a nice number, hash of 45 z's + 1 idk
// Hash table
node *table[N];
// Keep track of how many words there are in our dictionary
int words = 0;
void create_node(int bucket, char *word);
void free_bucket(int bucket);
// Returns true if word is in dictionary, else false
bool check(const char *word)
{
// The idea is the same as in free_bucket(), but instead of calling free;
// We compare the strings
node *cursor = table[hash(word)];
if (cursor != NULL)
{
while (cursor->next != NULL)
{
if (strcasecmp(word, cursor->word) == 0)
{
return true;
}
cursor = cursor->next;
}
if (strcasecmp(word, cursor->word) == 0)
{
return true;
}
}
return false;
}
// Hashes word to a number
unsigned int hash(const char *word)
{
int hash = 0;
for (int i = 0; word[i] != '\0'; i++)
{
hash += (tolower(word[i]) - 'a') * (i + 1);
}
return hash % N;
}
// Create a node in a bucket
void create_node(int bucket, char *word)
{
node *new = malloc(sizeof(node));
strcpy(new->word, word);
if (table[bucket] != NULL)
{
new->next = table[bucket];
}
else
{
new->next = NULL;
}
table[bucket] = new;
}
// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
// Opens the dictionary file
FILE *dicionario = fopen(dictionary, "r");
if (dicionario == NULL)
{
fclose(dicionario);
return false;
}
// Declares a buffer to a word and starts a counter
char palavra[LENGTH + 1];
int c = 0;
// Create nodes and fill them with the words in our dictionary
while (fscanf(dicionario, "%s", palavra) != EOF)
{
create_node(hash(palavra), palavra);
words++;
}
fclose(dicionario);
return true;
}
// Returns number of words in dictionary if loaded, else 0 if not yet loaded
unsigned int size(void)
{
if (words > 0)
{
return words;
}
return 0;
}
// Delete all nodes in a bucket
void free_bucket(int bucket)
{
// Define the cursor as the first node in the bucket
node *cursor = table[bucket];
// If there's no first node, we are done
if (cursor != NULL)
{
// If there's more than 1 nodes, we'll free the atual node, and go to the next;
// Repeating this proccess until there's no next
node *temp = NULL;
while (cursor->next != NULL)
{
temp = cursor->next;
free(cursor);
cursor = temp;
}
// This will free the last node, or, perhaps, the only one
free(cursor);
}
return;
}
// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
for (int i = 0; i < N; i++)
{
free_bucket(i);
}
return true;
}