/
main.c
939 lines (826 loc) · 32.6 KB
/
main.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define TAMANHO_ARQUIVO 11
#define MAXIMO_VALOR_APONTADOR 10
#define ARQUIVO "hash_cc.bin"
FILE *arquivo_hashing;
// Estrutura de registro
typedef struct REGISTRO {
int chave;
char nome[20];
int idade;
//guarda índice para o pai para acertar apontador, caso tenha que ser relocado
int index_father;
// apontador para salto
int apontador;
} registro;
// Tabela Hashing
registro tabela_hash[TAMANHO_ARQUIVO], tabela_h, tabela_jump;
// Estrutura da lista encadeada
struct Node {
int chave;
char nome[20];
int idade;
// ponteiro para próximo da lista
struct Node *prox;
};
// definindo o tipo
typedef struct Node node;
// PROTÓTIPOS
void reinserir_chave(int chave, char nome[], int idade);
void inserir_chave(int chave, char nome[], int idade);
void limpa_apontador_pai(int pai);
void limpa_registro(int indice);
void media_acessos();
void remover(int chave);
void limpa_registro(int indice);
void remove_registro(int indice);
/*
* Função de Hashing h1
*/
int h1(int chave) {
// fprintf(stderr, "h1 = %d\n", chave % TAMANHO_ARQUIVO);
return (chave % TAMANHO_ARQUIVO);
}
/*
* Função de Hashing h2
*/
int h2(int chave) {
// double chao;
// double chao;
int chao = (int) (chave / TAMANHO_ARQUIVO);
// fprintf(stderr, "Chão: %d\n", chao);
// chao = floor(chave / TAMANHO_ARQUIVO);
// max{chave/TAMANHO_ARQUIVO, 1}
if (chao > 1)
return chao;
else
return 1;
}
/**
* Inicia lista encadeada
* @param LISTA
*/
void inicia(node *LISTA) {
LISTA->prox = NULL;
}
/**
* Aloca um novo nó
* @return node
*/
node *aloca() {
node *novo = (node *) malloc(sizeof (node));
if (!novo) {
// printf("Sem memoria disponivel!\n");
exit(1);
} else {
return novo;
}
}
/**
* Verifica se a lista está vazia
* @param LISTA
* @return 1 se vazia
* @return 0 se NÃO vazia
*/
int vazia(node *LISTA) {
if (LISTA->prox == NULL)
return 1;
else
return 0;
}
/**
* Insere no final da lista encadeada
* @param LISTA
* @param chave
* @param nome
* @param idade
*/
void insereFim(node *LISTA, int chave, char nome[20], int idade) {
node *novo = aloca();
novo->chave = chave;
strcpy(novo->nome, nome);
novo->idade = idade;
novo->prox = NULL;
if (vazia(LISTA))
LISTA->prox = novo;
else {
node *tmp = LISTA->prox;
// loop até o final da lista
while (tmp->prox != NULL)
tmp = tmp->prox;
tmp->prox = novo;
}
}
/**
* exibe conteúdo da lista
* @param LISTA
*/
void exibe(node *LISTA) {
if (vazia(LISTA)) {
// printf("Lista vazia!\n\n");
return;
}
node *tmp;
tmp = LISTA->prox;
// printf("Lista:");
while (tmp != NULL) {
// printf("Chave: %d %s %d\n", tmp->chave, tmp->nome, tmp->idade);
tmp = tmp->prox;
}
}
/**
* Retorna elemento da lista pesquisado pela chave
* @param chave
* @return
*/
node *elem_lista(node *LISTA, int chave) {
if (vazia(LISTA)) {
printf("Lista vazia!\n\n");
}
node *tmp;
tmp = LISTA->prox;
// printf("Lista:");
while (tmp != NULL) {
if (tmp->chave == chave)
return tmp;
tmp = tmp->prox;
}
return NULL;
}
/**
* Reinsere os elementos da lista
* @param LISTA
*/
void reinsere_lista(node *LISTA) {
if (vazia(LISTA)) {
// printf("Lista vazia!\n\n");
}
node *tmp;
tmp = LISTA->prox;
// printf("Lista:");
while (tmp != NULL) {
reinserir_chave(tmp->chave, tmp->nome, tmp->idade);
tmp = tmp->prox;
}
}
// Inicializa o registro
void inicializar() {
// inicializa chave
tabela_h.chave = -1;
// inicializa nome
strcpy(tabela_h.nome, "");
// strlcpy(tabela_h.nome, "", 20);
tabela_h.index_father = -1;
// inicializa idade
tabela_h.idade = -1;
// inicializar apontador
tabela_h.apontador = -1;
}
// Cria o arquivo
void criar_arquivo() {
int i;
// arquivo binário
if ((arquivo_hashing = fopen(ARQUIVO, "wb")) == NULL) {
// fprintf(stderr, "\nErro ao abrir arquivo!");
// printf("\nErro ao abrir arquivo!");
return;
}
// fprintf(stderr, "arquivo criado com sucesso!\n");
// coloca o arquivo no inicio antes de começar a inserção dos registros
rewind(arquivo_hashing);
// Cria tabela hashing e insere no arquivo
for (i = 0; i < TAMANHO_ARQUIVO; i++) {
// acerta posição para inserção do registro no arquivo
fseek(arquivo_hashing, ((i + 1) * sizeof (struct REGISTRO)), SEEK_SET);
// escreve os registros no arquivo
if (fwrite(&tabela_h, sizeof (struct REGISTRO), 1, arquivo_hashing) != 1) {
// fprintf(stderr, "\nErro na escrita do arquivo!");
break;
}
}
// fecha o arquivo
fclose(arquivo_hashing);
}
// Imprimir arquivo
void imprimir() {
int i;
// abre o arquivo para leitura
if ((arquivo_hashing = fopen(ARQUIVO, "rb")) == NULL) {
printf("\nErro ao abrir arquivo!");
return;
}
// posiciona a leitura no início do arquivo
rewind(arquivo_hashing);
fseek(arquivo_hashing, (sizeof (struct REGISTRO)), SEEK_SET);
for (i = 0; i < TAMANHO_ARQUIVO; i++) {
fread(&tabela_h, sizeof (struct REGISTRO), 1, arquivo_hashing);
if (tabela_h.chave == -1) {
printf("%d: vazio\n", i);
} else {
if (tabela_h.apontador == -1)
printf("%d: %d %s %d nulo\n", i, tabela_h.chave, tabela_h.nome, tabela_h.idade);
else
printf("%d: %d %s %d %d\n", i, tabela_h.chave, tabela_h.nome, tabela_h.idade, tabela_h.apontador);
// printf("%d: %d %s %d -> %d|fater:%d\n", i, tabela_h.chave, tabela_h.nome, tabela_h.idade, tabela_h.apontador, tabela_h.index_father);
}
}
// fecha o arquivo
fclose(arquivo_hashing);
}
/**
* Consultar existência da chave na tabela
* @param chave
* @param retorna_indice retorna indice da busca
* @return 1 se existir a chave na tabela
* @return 0 se não existir a chave na tabela
* @retureturn índice (caso o parâmetro retorna_indice = 1)
*/
int consultar(int chave, int retorna_indice) {
int indice = h1(chave);
//tem ou não sucessores | saída do loop
bool chain, exit = false;
if ((arquivo_hashing = fopen(ARQUIVO, "rb")) == NULL) {
// fprintf(stderr, "\nErro ao abrir arquivo!");
}
rewind(arquivo_hashing);
// posiciona para leitura
// fseek(arquivo_hashing, ((indice + 1) * sizeof (struct REGISTRO)), SEEK_SET);
fseek(arquivo_hashing, ((indice + 1) * sizeof (struct REGISTRO)), SEEK_SET);
// le a posição na tabela hash
fread(&tabela_h, sizeof (struct REGISTRO), 1, arquivo_hashing);
// arquivo vazio
if (tabela_h.chave == -1) {
if (retorna_indice == 1)
return -1; //não encontrou pois o arquivo está vazio - retorna o próprio índice
else {
fprintf(stderr, "chave nao encontrada: %d\n", chave);
return 0;
}
}
// encontrou no home adress
if (tabela_h.chave == chave) {
if (retorna_indice == 1)
return indice;
else {
fprintf(stderr, "chave: %d\n%s\n%d\n", chave, tabela_h.nome, tabela_h.idade);
return 1;
}
}// colisão
else {
// tem sucessores?
if (tabela_h.apontador != -1) {
int salto = h2(tabela_h.chave) * tabela_h.apontador;
int go_to_index = indice + salto;
if (go_to_index >= TAMANHO_ARQUIVO)
go_to_index -= TAMANHO_ARQUIVO;
// indice = go_to_index;
// loop para verificar todas as posições do arquivo a procura da chave
while (go_to_index != indice && exit == false) {
// salta no arquivo pra achar a chave em outras posições
fseek(arquivo_hashing, ((go_to_index + 1) * sizeof (struct REGISTRO)), SEEK_SET);
fread(&tabela_h, sizeof (struct REGISTRO), 1, arquivo_hashing);
// encontrou
if (tabela_h.chave == chave) {
if (retorna_indice == 1)
return go_to_index;
else {
printf("chave: %d\n%s\n%d\n", chave, tabela_h.nome, tabela_h.idade);
return 1;
}
} else {
if (tabela_h.apontador != -1) {
int index_aux = go_to_index; // salva o indice original
// tem sucessores
/***************** FALTA IMPLEMENTAR **************/
while (tabela_h.apontador != -1 && tabela_h.chave != chave) {
salto = h2(tabela_h.chave) * tabela_h.apontador;
go_to_index = index_aux + salto;
index_aux = go_to_index;
if (go_to_index >= TAMANHO_ARQUIVO)
go_to_index -= TAMANHO_ARQUIVO;
fseek(arquivo_hashing, ((go_to_index + 1) * sizeof (struct REGISTRO)), SEEK_SET);
fread(&tabela_h, sizeof (struct REGISTRO), 1, arquivo_hashing);
index_aux = go_to_index;
}
if (tabela_h.chave == chave) {
// saiu do loop encontrando a chave
if (retorna_indice == 1)
return go_to_index;
else {
fprintf(stderr, "chave: %d\n%s\n%d\n", chave, tabela_h.nome, tabela_h.idade);
return 1;
}
} else {
// saiu do loop sem encontrar a chave
if (retorna_indice == 1)
return -1;
else {
fprintf(stderr, "chave nao encontrada: %d\n", chave);
return 0;
}
}
} else {
// não tem mais sucessore.A chave não se encontra no arquivo
exit = true;
if (retorna_indice == 1)
return -1;
else {
fprintf(stderr, "chave nao encontrada: %d\n", chave);
return 0;
}
}
}
}
} else {
//não sucessores, logo
if (retorna_indice == 1)
return -1;
else {
fprintf(stderr, "chave nao encontrada: %d\n", chave);
return 0;
}
}
}
fclose(arquivo_hashing);
// existe chave
return 1;
}
/**
* Inserir registro na tabela hash
* @param nome
* @param idade
* @param chave
*/
void inserir_chave(int chave, char nome[20], int idade) {
int aux, indice = 0, salto, count_jumps = 0, go_to_index, index_aux, index_before_jump;
registro register_aux, register_jump;
indice = (int) h1(chave);
// se já não existir, insere
// if (!check_key(chave)) {
// não existe
if (consultar(chave, 1) == -1) {
register_aux.chave = chave;
strcpy(register_aux.nome, nome);
register_aux.index_father = -1;
register_aux.idade = idade;
register_aux.apontador = -1;
if ((arquivo_hashing = fopen(ARQUIVO, "rb+")) == NULL) {
printf("\nErro ao abrir arquivo!");
return;
}
rewind(arquivo_hashing);
fseek(arquivo_hashing, ((indice + 1) * sizeof (struct REGISTRO)), SEEK_SET);
fread(&tabela_h, sizeof (struct REGISTRO), 1, arquivo_hashing);
// SEM colisão
if (tabela_h.chave == -1) {
fseek(arquivo_hashing, ((indice + 1) * sizeof (struct REGISTRO)), SEEK_SET);
fwrite(®ister_aux, sizeof (struct REGISTRO), 1, arquivo_hashing);
}//
else {
// verificar se o que está na posição está no seu home adress
if (h1(tabela_h.chave) == indice) {
// está no homeAdress
if (tabela_h.apontador == -1) {
//NÃO tem sucessor. Salta para o sucessor
salto = h2(tabela_h.chave);
fseek(arquivo_hashing, ((salto + indice + 1) * sizeof (struct REGISTRO)), SEEK_SET);
fread(&tabela_jump, sizeof (struct REGISTRO), 1, arquivo_hashing);
index_aux = indice; //salva o índice atual
// CASO BASE
if (tabela_jump.chave == -1) {
// salva o novo registro
fseek(arquivo_hashing, ((salto + indice + 1) * sizeof (struct REGISTRO)), SEEK_SET);
// salva o indice do pai
register_aux.index_father = indice;
fwrite(®ister_aux, sizeof (struct REGISTRO), 1, arquivo_hashing);
// lê registro para salvar apontador para sucessor
fseek(arquivo_hashing, ((indice + 1) * sizeof (struct REGISTRO)), SEEK_SET);
fread(&tabela_h, sizeof (struct REGISTRO), 1, arquivo_hashing);
count_jumps++;
tabela_h.apontador = count_jumps;
// salva apontador para sucessor
fseek(arquivo_hashing, ((indice + 1) * sizeof (struct REGISTRO)), SEEK_SET);
fwrite(&tabela_h, sizeof (struct REGISTRO), 1, arquivo_hashing);
}
// posição ocupada - pular de h2 em h2 até achar uma disponível
if (tabela_jump.chave != -1) {
// index_aux++;
go_to_index = salto + index_aux;
// loop de busca de posição vazia
do {
fseek(arquivo_hashing, ((go_to_index + 1) * sizeof (struct REGISTRO)), SEEK_SET);
fread(&tabela_jump, sizeof (struct REGISTRO), 1, arquivo_hashing);
count_jumps++;
if (tabela_jump.chave != -1) {
// posição ocupada, procurar nova posição
// count_jumps++; //acerta apontador de saltos do home adress
// index_aux++;
index_aux = go_to_index + salto;
// go_to_index = salto + index_aux;
go_to_index = go_to_index + salto;
if (go_to_index >= TAMANHO_ARQUIVO)
go_to_index -= TAMANHO_ARQUIVO;
} else {
// salva o registro
fseek(arquivo_hashing, ((go_to_index + 1) * sizeof (struct REGISTRO)), SEEK_SET);
// salva o indice do pai
register_aux.index_father = indice;
fwrite(®ister_aux, sizeof (struct REGISTRO), 1, arquivo_hashing);
// acertando o apontador de sucessores
tabela_h.apontador = count_jumps; //acerta o apontador de saltos
fseek(arquivo_hashing, ((indice + 1) * sizeof (struct REGISTRO)), SEEK_SET);
fwrite(&tabela_h, sizeof (struct REGISTRO), 1, arquivo_hashing);
}
} while (tabela_jump.chave != -1 && go_to_index != indice);
}
} else {
// TEM SUCESSORE(S). É uma cadeia
salto = (h2(tabela_h.chave) * tabela_h.apontador) + indice;
if (salto >= TAMANHO_ARQUIVO)
salto -= TAMANHO_ARQUIVO;
// guarda o indice atual para saber onde acertar o apontador
int new_index = salto;
fseek(arquivo_hashing, ((salto + 1) * sizeof (struct REGISTRO)), SEEK_SET);
fread(&tabela_jump, sizeof (struct REGISTRO), 1, arquivo_hashing);
count_jumps++;
while (tabela_jump.apontador != -1 && new_index != indice) {
salto = (h2(tabela_jump.chave) * tabela_jump.apontador) + new_index;
if (salto >= TAMANHO_ARQUIVO)
salto -= TAMANHO_ARQUIVO;
new_index = salto;
// salva o índice anterior ao salto
index_before_jump = new_index;
count_jumps++; //salva o número de saltos p/ register_aux
fseek(arquivo_hashing, ((new_index + 1) * sizeof (struct REGISTRO)), SEEK_SET);
fread(&tabela_jump, sizeof (struct REGISTRO), 1, arquivo_hashing);
}
// saiu pq achou o último da cadeia
if (new_index != indice) {
// salta pelo h2 até achar uma vaga ou terminar o arquivo
index_before_jump = new_index; // salva o índice a ser atualizado
salto = h2(tabela_jump.chave) + new_index;
int index_h2 = h2(tabela_jump.chave);
do {
fseek(arquivo_hashing, ((salto + 1) * sizeof (struct REGISTRO)), SEEK_SET);
fread(&tabela_jump, sizeof (struct REGISTRO), 1, arquivo_hashing);
if (tabela_jump.chave == -1) {
fseek(arquivo_hashing, ((salto + 1) * sizeof (struct REGISTRO)), SEEK_SET);
// salva o indice do pai
register_aux.index_father = index_before_jump;
// register_aux.index_father = indice;
fwrite(®ister_aux, sizeof (struct REGISTRO), 1, arquivo_hashing);
// acertar o apontador
fseek(arquivo_hashing, ((index_before_jump + 1) * sizeof (struct REGISTRO)), SEEK_SET);
fread(&tabela_jump, sizeof (struct REGISTRO), 1, arquivo_hashing);
tabela_jump.apontador = count_jumps;
// salva saltos para pai
fseek(arquivo_hashing, ((index_before_jump + 1) * sizeof (struct REGISTRO)), SEEK_SET);
fwrite(&tabela_jump, sizeof (struct REGISTRO), 1, arquivo_hashing);
register_aux = tabela_jump;
fseek(arquivo_hashing, ((index_before_jump + 1) * sizeof (struct REGISTRO)), SEEK_SET);
// fwrite(&tabela_jump, sizeof (struct REGISTRO), 1, arquivo_hashing);
fwrite(®ister_aux, sizeof (struct REGISTRO), 1, arquivo_hashing);
// fclose(arquivo_hashing);
// previnir loop infinito
return;
} else {
// saltando de h2 em h2 até encontrar uma posição
new_index = salto;
salto += index_h2;
if (salto >= TAMANHO_ARQUIVO)
salto -= TAMANHO_ARQUIVO;
}
count_jumps++;
} while (tabela_jump.apontador != -1 || salto != indice);
}
}
}// NÃO ESTÁ NO HOME ADRESS
else {
// retira ele e os seus sucessores e os reinsere
int pai = tabela_h.index_father;
// inicializa lista
node *LISTA = (node *) malloc(sizeof (node));
if (!LISTA) {
// fprintf(stderr, "Sem memória disponivel!\n");
exit(1);
} else {
inicia(LISTA);
}
// NÃO TEM SUCESSORES - APENAS 1 A SER REINSERIDO
if (tabela_h.apontador == -1) {
int key = tabela_h.chave, age = tabela_h.idade;
char name[20];
strcpy(name, tabela_h.nome);
// salva na lista para reinserção
insereFim(LISTA, tabela_h.chave, tabela_h.nome, tabela_h.idade);
// insere no lugar deste o registro
fseek(arquivo_hashing, ((indice + 1) * sizeof (struct REGISTRO)), SEEK_SET);
fwrite(®ister_aux, sizeof (struct REGISTRO), 1, arquivo_hashing);
fclose(arquivo_hashing);
// ACERTA APONTADOR DO PAI
limpa_apontador_pai(pai);
// reinsere o elemento
reinserir_chave(key, name, age);
// REINICIA A LISTA
inicia(LISTA);
return;
}// tem que reinserir todos até o final da cadeia
else {
int jump_to;
// LOOP até encontrar o final da cadeia
while (tabela_h.apontador != -1) {
// salva na lista para reinserção
insereFim(LISTA, tabela_h.chave, tabela_h.nome, tabela_h.idade);
// calcula salto para o próximo para inserir
jump_to = (h2(tabela_h.chave) * tabela_h.apontador) + indice;
if (jump_to >= TAMANHO_ARQUIVO)
jump_to -= TAMANHO_ARQUIVO;
// MARCAR POSIÇÃO COMO VÁLIDA
limpa_registro(indice);
// lê o próximo da cadeia
fseek(arquivo_hashing, ((jump_to + 1) * sizeof (struct REGISTRO)), SEEK_SET);
fread(&tabela_h, sizeof (struct REGISTRO), 1, arquivo_hashing);
}
// insere o último da cadeia
insereFim(LISTA, tabela_h.chave, tabela_h.nome, tabela_h.idade);
// MARCAR POSIÇÃO COMO VÁLIDA
limpa_registro(jump_to);
// após inserir antigos na lista, insere novo
fseek(arquivo_hashing, ((indice + 1) * sizeof (struct REGISTRO)), SEEK_SET);
fwrite(®ister_aux, sizeof (struct REGISTRO), 1, arquivo_hashing);
fclose(arquivo_hashing);
// ACERTA APONTADOR DO PAI
limpa_apontador_pai(pai);
//reinsere os da lista
reinsere_lista(LISTA);
// reinicia a lista
inicia(LISTA);
}
// exibe(LISTA);
}
}
fclose(arquivo_hashing);
} else {
fprintf(stderr, "Chave ja existente\n");
}
// imprimir()
}
void reinserir_chave(int chave, char nome[20], int idade) {
inserir_chave(chave, nome, idade);
// fclose(arquivo_hashing);
}
/**
* limpa o registro do índice passado como parâmetro
* @param indice
*/
void limpa_registro(int indice) {
registro register_blackout;
register_blackout.apontador = -1;
register_blackout.chave = -1;
strcpy(register_blackout.nome, "");
register_blackout.idade = -1;
register_blackout.index_father = -1;
fseek(arquivo_hashing, ((indice + 1) * sizeof (struct REGISTRO)), SEEK_SET);
fwrite(®ister_blackout, sizeof (struct REGISTRO), 1, arquivo_hashing);
}
void remove_registro(int indice) {
registro register_blackout;
register_blackout.apontador = -1;
register_blackout.chave = -1;
strcpy(register_blackout.nome, "");
register_blackout.idade = -1;
register_blackout.index_father = -1;
if ((arquivo_hashing = fopen(ARQUIVO, "rb+")) == NULL) {
printf("\nErro ao abrir arquivo!");
return;
}
fseek(arquivo_hashing, ((indice + 1) * sizeof (struct REGISTRO)), SEEK_SET);
fwrite(®ister_blackout, sizeof (struct REGISTRO), 1, arquivo_hashing);
fclose(arquivo_hashing);
}
/**
* limpa referência do elemento anterior na cadeia
* @param pai
*/
void limpa_apontador_pai(int pai) {
registro register_aux, update_register;
if ((arquivo_hashing = fopen(ARQUIVO, "rb+")) == NULL) {
printf("\nErro ao abrir arquivo!");
return;
}
fseek(arquivo_hashing, ((pai + 1) * sizeof (struct REGISTRO)), SEEK_SET);
fread(®ister_aux, sizeof (struct REGISTRO), 1, arquivo_hashing);
// limpa apontador do pai
update_register.chave = register_aux.chave;
strcpy(update_register.nome, register_aux.nome);
update_register.idade = register_aux.idade;
update_register.index_father = register_aux.index_father;
update_register.apontador = -1;
fseek(arquivo_hashing, ((pai + 1) * sizeof (struct REGISTRO)), SEEK_SET);
fwrite(&update_register, sizeof (struct REGISTRO), 1, arquivo_hashing);
fclose(arquivo_hashing);
}
/**
* Calcula média de acessos as chaves inseridas
*/
void media_acessos() {
float count_jumps = 0, qtd_chaves = 0;
// int count_jumps = 0, qtd_chaves = 0;
node *LISTA = (node *) malloc(sizeof (node));
if (!LISTA) {
exit(1);
} else {
inicia(LISTA);
}
if ((arquivo_hashing = fopen(ARQUIVO, "rb+")) == NULL) {
printf("\nErro ao abrir arquivo!");
return;
}
// int i;
// contar as chaves inseridas, salvar em uma lista e consultar até o final da lista
for (int i = 0; i < TAMANHO_ARQUIVO; i++) {
fseek(arquivo_hashing, ((i + 1) * sizeof (struct REGISTRO)), SEEK_SET);
fread(&tabela_h, sizeof (struct REGISTRO), 1, arquivo_hashing);
// insere todas as chaves do arquivo na lista
if (tabela_h.chave != -1) {
insereFim(LISTA, tabela_h.chave, tabela_h.nome, tabela_h.idade);
qtd_chaves++;
}
}
if (vazia(LISTA)) {
// return;
}
node *tmp;
tmp = LISTA->prox;
//loop para contar os acessos
while (tmp != NULL) {
int indice_elemento = consultar(tmp->chave, 1);
int exit = 0;
// retirar depois
// printf("Chave: %d %s %d\n", tmp->chave, tmp->nome, tmp->idade);
// consultar todos os pais
while (exit == 0) {
fseek(arquivo_hashing, (indice_elemento + 1) * sizeof (struct REGISTRO), SEEK_SET);
fread(&tabela_h, sizeof (struct REGISTRO), 1, arquivo_hashing);
// fprintf(stderr, "jumps %f\n", count_jumps);
//saída do loop dos pais
if (tabela_h.index_father == -1) {
exit = 1;
// fprintf(stderr, "cj %d", count_jumps);
} else
indice_elemento = tabela_h.index_father;
// incrementa contador
count_jumps++;
}
// seleciona próximo da lista
tmp = tmp->prox;
}
if (qtd_chaves != 0) {
float media_acessos = count_jumps / qtd_chaves;
fprintf(stderr, "%.1f", media_acessos);
} else {
fprintf(stderr, "0.0");
}
// media = acessos / TAMANHO_ARQUIVO
fclose(arquivo_hashing);
}
/**
* Remove chave
* @param chave a ser removida
* @return
*/
void remover(int chave) {
int salto, index_aux, pai_index;
registro cleaner;
cleaner.apontador = -1;
cleaner.chave = -1;
cleaner.idade = -1;
cleaner.index_father = -1;
strcpy(cleaner.nome, "");
// inicializa lista
node *LISTA = (node *) malloc(sizeof (node));
if (!LISTA) {
// fprintf(stderr, "Sem memória disponivel!\n");
exit(1);
} else {
inicia(LISTA);
}
// arquivo binário
if ((arquivo_hashing = fopen(ARQUIVO, "rb+")) == NULL) {
// fprintf(stderr, "\nErro ao abrir arquivo!");
// printf("\nErro ao abrir arquivo!");
return;
}
// consultar índice a ser removido
int indice_remove = consultar(chave, 1);
// pode ser 0 até TAMANHO_ARQUIVO
if (indice_remove != -1) {
// lê o registro da posição a ser removido
fseek(arquivo_hashing, ((indice_remove + 1) * sizeof (struct REGISTRO)), SEEK_SET);
fread(&tabela_h, sizeof (struct REGISTRO), 1, arquivo_hashing);
// verifica se corresponde a chave a ser removida
if (tabela_h.chave == chave) {
// salva índice para pai
pai_index = tabela_h.index_father;
// salva índice original
index_aux = indice_remove;
salto = h2(tabela_h.chave) * tabela_h.apontador + index_aux;
if (salto >= TAMANHO_ARQUIVO)
salto -= TAMANHO_ARQUIVO;
while (tabela_h.apontador != -1) {
fseek(arquivo_hashing, ((salto + 1) * sizeof (struct REGISTRO)), SEEK_SET);
fread(&tabela_h, sizeof (struct REGISTRO), 1, arquivo_hashing);
insereFim(LISTA, tabela_h.chave, tabela_h.nome, tabela_h.idade);
index_aux = salto;
salto = h2(tabela_h.chave) * tabela_h.apontador + salto;
if (salto >= TAMANHO_ARQUIVO)
salto -= TAMANHO_ARQUIVO;
// limpa_registro(index_aux);
remove_registro(index_aux);
}
// remove índice
// limpa_registro(indice_remove);
remove_registro(indice_remove);
limpa_apontador_pai(pai_index);
reinsere_lista(LISTA);
}
}
}
/**
* Função principal
*
* @param argc
* @param argv
* @return
*/
int main(int argc, char *argv[]) {
char nome[20], operacao;
int idade, chave;
// Criação de lista de reinserção de elementos
node *LISTA = (node *) malloc(sizeof (node));
if (!LISTA) {
// fprintf(stderr, "memória insuficiente para criar lista \n");
} else {
inicia(LISTA);
}
// Criação de arquivo
if ((arquivo_hashing = fopen(ARQUIVO, "rb")) == NULL) {
// inicializa tabela hash
inicializar();
// cria arquivo para inserção da tabela hash
criar_arquivo();
}
// Verifica qual operação será executada
scanf("%c", &operacao);
do {
switch (operacao) {
case 'i':
scanf("%d", &chave);
// scanf lendo string com espaços em branco
scanf(" %[^\n]s", &nome);
// fflush(stdin);
scanf("%d", &idade);
inserir_chave(chave, nome, idade);
break;
case 'c':
// fprintf(stderr, "CONSULTAR\n");
scanf("%d", &chave);
consultar(chave, 0);
break;
case 'r':
// fprintf(stderr, "REMOVER\n");
scanf("%d", &chave);
int result = consultar(chave, 1);
if (result != -1) {
// fprintf(stderr, "indíce para remover: %d\n", result);
// remoção
remover(chave);
} else {
fprintf(stderr, "chave nao encontrada: %d", chave);
}
break;
case 'p':
// fprintf(stderr, "IMPRIMIR\n");
imprimir();
break;
case 'm':
// fprintf(stderr, "MEDIA DE ACESSOS\n");
media_acessos();
break;
case 'e':
return 0;
break; //exit
default:
break;
}
// limpa caracter do buffer antes do próximo loop
operacao = getchar();
} while (operacao != 'e');
// fclose(arquivo_hashing);
return 0;
}