forked from danigfavero/LAREIRA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.c
79 lines (72 loc) · 1.79 KB
/
hash.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
#include "lista.h"
/*
typedef struct TabSim{
int size;
Lista* listas;
}TabSim;
*/
int hash(char* s, int mod){
//Definindo como será o hash das palavras
long long int out = 0;
for(int i = 0; s[i] != '\0'; i++){
out = out*257 + (int) s[i];
out %= mod;
}
return out;
}
TabSim Tcria(int tam){
//Criando uma tabela com tam listas
TabSim tabela;
tabela.listas = malloc(tam*sizeof(Lista));
tabela.size = tam;
for(int i = 0; i < tam; i++){
tabela.listas[i] = Lcria();
}
return tabela;
}
void Tdestroi(TabSim t){
//Percorre a tabela destruindo as listas dela
for(int i = 0; i < t.size; i++){
Ldestroi(t.listas[i]);
}
free(t.listas);
return;
}
int Tvazia(TabSim t, int tamanho){
for(int i = 0; i < tamanho; i++){
Elo* crawler = t.listas[i].cabec;
if(crawler != NULL) return 0;
}
return 1;
}
int Tinsere(TabSim t, Elemento* val){
//Calcula o hash do elemento, que é em qual lista ele será inserido
int hash_val = hash(val->nome, t.size);
//printf("Hash do elemento %s: %d\n", val->nome, hash_val);
return Linsere(t.listas[hash_val], val)==NULL?0:1;
}
Elemento* Tbusca(TabSim t, char *n){
int hash_val = hash(n, t.size);
//printf("Estamos buscando o elemento %s que tem hash %d\n", n, hash_val);
return Lbusca(t.listas[hash_val], n);
}
int Tretira(TabSim t, char *n){
int hash_val = hash(n, t.size);
Elo* crawler = t.listas[hash_val].cabec; //Elemento* out = NULL;
Elemento* ee;
while(crawler != NULL){
ee = (Elemento* ) crawler->val;
//printf("Fazendo uma busca pelo %s para tentar elimina-lo\n", n);
if(crawler->val != NULL && stringsIguais(ee->nome, n)){
//printf("Achei o %s e to eliminando\n", n);
crawler->val = NULL;
//free(crawler->val);
//free(crawler);
//puts("eliminei\n");
return 1;
}
//last = crawler;
crawler = crawler->next;
}
return 0;
}