-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionary.c
More file actions
144 lines (117 loc) · 2.9 KB
/
dictionary.c
File metadata and controls
144 lines (117 loc) · 2.9 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
// Implements a dictionary's functionality
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "dictionary.h"
// Represents a node in a hash table
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
}
node;
// Number of buckets in hash table
const unsigned int N = 26;
// Guardando o tamanho para retornar na função SIZE
unsigned int SIZE = 0;
// Hash table
node *table[N];
// Returns true if word is in dictionary, else false
bool check(const char *word)
{
//Obtendo o hash value
int h = hash(word);
//Criando cursor para aquela linha
node *cursor = table[h];
while (cursor != NULL)
{
//comparando as palavras
if (strcasecmp(word, cursor->word) == 0)
{
//Iguais retorna TRUE
return true;
}
//Movendo o cursor conforme video explicativo
cursor = cursor->next;
}
return false;
}
// Hashes word to a number
unsigned int hash(const char *word)
{
//Função HASH copiada de: http://www.cse.yorku.ca/~oz/hash.html
unsigned long hash = 5381;
int c = 0;
while (c == *word++)
{
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
}
return hash % N;
}
// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
//Abrindo o dicionário
FILE *input = fopen(dictionary, "r");
if (input == NULL)
{
return false;
}
//Variável para onde vai a palavra
char word[LENGTH + 1];
//Leitura do dicionario para achar a palavra até o fim do arquivo (EOF = fim do arquivo)
while (fscanf(input, "%s", word) != EOF)
{
//Alocando memório para o novo nó
node *n = malloc(sizeof(node));
if (n == NULL)
{
return false;
}
//Copiando a palavra
strcpy(n->word, word);
//Obtendo o hash value
int h = hash(word);
//Verificando se já tem alguma palavra nesse nó
if (table[h] == NULL)
{
table[h] = n;
n->next = NULL;
}
else
{
n->next = table[h];
table[h] = n;
}
SIZE++;
}
//Fechando o dicionário
fclose(input);
return true;
}
// Returns number of words in dictionary if loaded, else 0 if not yet loaded
unsigned int size(void)
{
return SIZE;
}
// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
//Varrendo todos os buckets
for (int i = 0 ; i < N ; i++)
{
//Lendo até o último nó
while (table[i] != NULL)
{
//Salvando o link a ser excluido
node *temp = table[i];
//salvando o proximo link
table[i] = table[i]->next;
//liberando a memoria
free(temp);
}
}
return true;
}