/
hash.c
88 lines (85 loc) · 1.85 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
80
81
82
83
84
85
86
87
88
#include "hash.h"
Hash* funcaoHash(int size, int type, FILE* fp){
Hash *h = NULL;
h = criaHash(size, type);
char *n = (char*)calloc(200, sizeof(char));
int mat;
Aluno* a;
while((fscanf(fp, "%d %[^\n]s", &mat, n)) != EOF){
a = criaAluno(n, mat);
n = (char*)calloc(200, sizeof(char));
insereNaHash(h, a, type);
//break;
}
free(n);
return h;
}
Hash* criaHash(int t, int type){
int i;
Hash *h = NULL;
h = (Hash*)calloc(1, sizeof(Hash)*t);
h->tam = t;
if(type == 0){
h->hash = (Arvore**)calloc(t, sizeof(Arvore*));
for(i = 0; i < t; i++){
h->hash[i] = NULL;
}
} else if(type == 1){
if (h->hashSBB == NULL){
h->hashSBB = (ArvoreSBB**)calloc(t, sizeof(ArvoreSBB*));
for(i = 0; i < t; i++){
inicializaSBB(&h->hashSBB[i]);
}
}
}
return h;
}
//Hash* criaHash()
void insereNaHash(Hash* h, Elemento* x, int type){
int index = x->matricula % h->tam;
if(type == 0){
if(h->hash[index] == NULL){
h->hash[index] = criaArvore(x);
}
else insereElemento(h->hash[index], x);
}
if(type == 1){
if(h->hashSBB == NULL){
fprintf(stderr, "h->hashSBB is null\n");
}
if(h->hashSBB[index] == NULL) {
h->hashSBB[index] = criaArvoreSBB(x);
} else insereSBB(&(h->hashSBB[index]), x);
}
}
void imprime(Hash* h, FILE* fp, int type){
int i;
if(type == 0){
for(i = 0; i<h->tam; i++){
fprintf(fp, "%d:\n", i);
imprimeArvore(h->hash[i], fp);
}
}
if(type == 1){
for(i = 0; i<h->tam; i++){
fprintf(fp, "%d:\n", i);
imprimeSBB(h->hashSBB[i], fp);
}
}
}
void apagaHash(Hash* h, int type){
int i;
if(type == 0){
for(i=0; i<h->tam;i++){
apagaArvore(h->hash[i]);
}
free(h->hash);
}
if(type == 1){
for(i=0; i<h->tam;i++){
apagaSBB(h->hashSBB[i]);
}
free(h->hashSBB);
}
free(h);
}