]> git.llucax.com Git - z.facultad/75.06/emufs.git/blob - emufs/external_sort/bufford_test.c
f92916a533458d8b428c9631054dab7de1198c22
[z.facultad/75.06/emufs.git] / emufs / external_sort / bufford_test.c
1
2 #include "bufford.h"
3 #include <stdio.h>
4
5 /*
6 typedef struct
7 {
8         int id;
9         int estado;
10         int nrofac;
11 }
12 RECORD;
13 */
14
15 int cmp(void* x, void* y)
16 {
17         int xx = *(int*)x;
18         int yy = *(int*)y;
19         if (xx > yy) return 1;
20         if (xx < yy) return -1;
21         return 0;
22 }
23
24 void buffer_dump(BUFFORD* b)
25 {
26         int* c;
27         printf("Buffer: ");
28         for (c = bufford_get_min(b); c; c = bufford_get_next(b, c))
29                 printf("%i ", *c);
30         printf("\n");
31 }
32
33 int main(int argc, char* argv[])
34 {
35         FILE* fp;
36         BUFFORD* b = bufford_new(sizeof(int) * 8, sizeof(int), &cmp);
37         if (!b) return 1;
38         fp = fopen(argv[1], "r");
39         while (1) {
40                 int reg;
41                 fscanf(fp, "%i", &reg);
42                 if (feof(fp)) break;
43                 if (reg == 0) continue;
44                 if (!bufford_push(b, &reg)) { /* no hay más lugar */
45                         int new_reg;
46                         int de_nuevo = 1;
47                         printf("No se pudo insertar el valor %i, el buffer está lleno!\n", reg);
48                         buffer_dump(b);
49                         while (de_nuevo) {
50                                 int* poped;
51                                 printf("Debe sacar algún valor: ");
52                                 scanf("%i", &new_reg);
53                                 if ((poped = bufford_pop(b, &new_reg))) {
54                                         printf("Se sacó el valor %i con éxito!\n", *poped);
55                                         free(poped);
56                                         if (!bufford_push(b, &reg)) { /* no hay más lugar */
57                                                 printf("Hubo un error FEO!\n");
58                                                 return -1;
59                                         }
60                                         de_nuevo = 0;
61                                 } else {
62                                         printf("No hay ningún valor menor a %i en el buffer!\n", new_reg);
63                                 }
64                         }
65                 }
66                 printf("Se insertó el valor %i con éxito!\n", reg);
67         }
68         printf("Fin!!!\n");
69         buffer_dump(b);
70         /* vacío el buffer */
71         {
72                 int* c;
73                 int x;
74                 for (c = bufford_pop_min(b); c; c = bufford_pop(b, &x)) {
75                         x = *c;
76                         free(c);
77                         printf("Se sacó el valor %i con éxito!\n", x);
78                         buffer_dump(b);
79                 }
80         }
81         bufford_delete(b);
82         return 0;
83 }
84