-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenHash.c
299 lines (267 loc) · 8.58 KB
/
openHash.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*=======================================================================*/
/* Trabalho de AED II - Indice remissivo */
/* Nome: Arthur Alexsander Martins Teodoro */
/* MAtrícula: 0022427 Data: 13/09/2016 */
/*=======================================================================*/
/*=======================================================================*/
/* BIBLIOTECAS USADAS */
/*=======================================================================*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "openHash.h"
#include "listadup.h"
/*=======================================================================*/
/* ESTRUTURA CRIADA */
/*=======================================================================*/
struct palavra
{
char plv[33];
Lista ocorrencias;
};
struct hash
{
int tam;
int colisao;
struct palavra **vetor;
};
static int vetRand[32];
/*=======================================================================*/
/*FUNCAO HASH1 - FUNCAO RESPONSAVEL POR DEFINIR POSICAO */
/*IN: PALAVRA OUT: POSICAO */
/*=======================================================================*/
int funcaoHash1(char* palavra, int tam)
{
unsigned long int ascii = 0;
int i;
for(i = 0; i < strlen(palavra); i++)
{
ascii = ascii + (palavra[i] * vetRand[i]);
}
return ascii % tam;
}
/*=======================================================================*/
/*FUNCAO HASH2 - FUNCAO RESPONSAVEL POR DEFINIR POSICAO */
/*IN: PALAVRA OUT: POSICAO */
/*=======================================================================*/
int funcaoHash2(char* palavra, int tam)
{
unsigned int ascii = 0;
int i;
for(i = 1; i <= strlen(palavra); i++)
{
ascii = ascii + (palavra[i-1] * i);
}
return ascii % tam;
}
/*=======================================================================*/
/*FUNCAO HASH2 - FUNCAO RESPONSAVEL POR DEFINIR POSICAO */
/*IN: PALAVRA OUT: POSICAO */
/*=======================================================================*/
int funcaoHash3(char* palavra, int tam)
{
unsigned int ascii = 0;
int i;
for(i = 0; i < strlen(palavra); i++)
{
ascii = ascii + (palavra[i-1] * i + vetRand[i]);
}
return (ascii*ascii+5) % tam;
}
/*=======================================================================*/
/*CRIA HASH - FUNCAO DE CRIACAO DA HASH */
/*IN: VOID OUT: PONTEIRO PARA HASH */
/*=======================================================================*/
Hash criaHash(int tam)
{
srand(time(NULL));
int i;
Hash hash = (Hash) malloc(sizeof(struct hash));
hash->tam = tam;
hash->colisao = 0;
hash->vetor = (Palavra*) malloc(sizeof(Palavra)*tam);
for(i = 0; i < tam; i++)
{
hash->vetor[i] = NULL;
}
if(F != 2)
{
for(i = 0; i < 32; i++)
{
vetRand[i] = rand();
}
}
return hash;
}
/*=======================================================================*/
/*DESTROI HASH - FUNCAO DE DESTRUICAO DA HASH */
/*IN: PONTEIRO PARA HASH OUT: VOID */
/*=======================================================================*/
void destroiHash(Hash hash)
{
if(hash != NULL)
{
int i;
for(i = 0; i < hash->tam; i++)
{
if(hash->vetor[i] != NULL)
{
destroiLista(hash->vetor[i]->ocorrencias);
free(hash->vetor[i]);
}
}
free(hash->vetor);
free(hash);
hash = NULL;
}
return;
}
/*=======================================================================*/
/*INSERE HASH - FUNCAO RESPONSAVEL POR INSERIR PALAVRA */
/*IN: HASH E PALAVRA A INSERIR OUT: PONTEIRO PARA PALAVRA */
/*=======================================================================*/
Palavra insereHash(Hash hash, char* palavra)
{
int posicao;
if(F == 1)
posicao = funcaoHash1(palavra, hash->tam);
else if(F == 2)
posicao = funcaoHash2(palavra, hash->tam);
else if(F == 3)
posicao = funcaoHash3(palavra, hash->tam);
int posicoesVerificadas = 0;
while((hash->vetor[posicao] != NULL))
{
if(C == 1)
posicao = (posicao+1)%hash->tam;
else if(C == 2)
{
posicao = (posicao+3)%hash->tam;
if(hash->vetor[posicao] != NULL)
posicao = (posicao+1)%hash->tam;
}
else if(C == 3)
{
posicao = posicao-1;
if(posicao < 0)
{
posicao = hash->tam-1;
}
else
posicao = posicao%hash->tam;
}
hash->colisao++;
posicoesVerificadas++;
//if(posicoesVerificadas > hash->tam)
// return NULL;
}
hash->vetor[posicao] = (Palavra) malloc(sizeof(struct palavra));
strcpy(hash->vetor[posicao]->plv, palavra);
hash->vetor[posicao]->ocorrencias = criaLista();
return hash->vetor[posicao];
}
/*=======================================================================*/
/*BUSCA HASH - FUNCAO DE BUSCA DA HASH */
/*IN: HASH E PALAVRA A BUSCAR OUT: PONTEIRO PARA PALAVRA */
/*=======================================================================*/
Palavra buscaHash(Hash hash, char* palavra)
{
int posicao;
if(F == 1)
posicao = funcaoHash1(palavra, hash->tam);
else if(F == 2)
posicao = funcaoHash2(palavra, hash->tam);
else
posicao = funcaoHash3(palavra, hash->tam);
int posicoesVerificadas = 0;
while(posicoesVerificadas <= hash->tam)
{
if(hash->vetor[posicao] != NULL && !strcmp(hash->vetor[posicao]->plv,palavra))
{
if(!strcmp(hash->vetor[posicao]->plv,palavra))
return hash->vetor[posicao];
}
if(C == 1)
{
posicao = (posicao+1)%hash->tam;
}
else if(C == 2)
{
posicao = (posicao+3)%hash->tam;
if(hash->vetor[posicao] != NULL)
posicao = (posicao+1)%hash->tam;
}
else if(C == 3)
{
posicao = posicao-1;
if(posicao < 0)
posicao = hash->tam-1;
else
posicao = posicao%hash->tam;
}
posicoesVerificadas++;
}
return NULL;
}
/*=======================================================================*/
/*INSERE OCORRENCIA - FUNCAO RESPONSAVEL POR INSERIR LINHA */
/*IN: HASH, PALAVRA E LINHA OUT: VOID */
/*=======================================================================*/
void insereOcorrencia(Hash hash, char* palavra, int linha)
{
Palavra plv = buscaHash(hash, palavra);
if(plv != NULL)
{
if(!existeLista(plv->ocorrencias, linha))
insereLista(plv->ocorrencias, tamanhoLista(plv->ocorrencias), linha);
}
}
/*=======================================================================*/
/*PRINTA HASH - FUNCAO RESPONSAVEL POR IMPRIMIR HASH */
/*IN: HASH OUT: VOID */
/*=======================================================================*/
void printaHash(Hash hash, FILE* arquivo)
{
int i;
for(i = 0; i < hash->tam; i++)
{
if(hash->vetor[i] != NULL)
{
fprintf(arquivo, "%s - ", hash->vetor[i]->plv);
printaLista(hash->vetor[i]->ocorrencias, arquivo);
}
}
}
/*=======================================================================*/
/*TAMANHO HASH - FUNCAO RESPONSAVEL POR RETORNAR TAMANHO HASH */
/*IN: HASH OUT: INT */
/*=======================================================================*/
int tamanhoHash(Hash hash)
{
return hash->tam;
}
/*=======================================================================*/
/*RETORNA PALAVRA - FUNCAO QUE RETORNA A STRING DA STRUCT */
/*IN: PALAVRA OUT: CHAR* */
/*=======================================================================*/
char* retornaPalavra(Palavra plv)
{
return plv->plv;
}
/*=======================================================================*/
/*RETORNA LISTA - FUNCAO QUE RETORNA A LISTA DA STRUCT */
/*IN: PALAVRA OUT: LISTA */
/*=======================================================================*/
Lista retornaLista(Palavra plv)
{
return plv->ocorrencias;
}
/*=======================================================================*/
/*RETORNA COLISAO - FUNCAO QUE RETORNA QUANT DE COLISAO */
/*IN: HASH OUT: INT */
/*=======================================================================*/
int colisaoHash(Hash hash)
{
return hash->colisao;
}