2 /* Block Sorting Optimizado en memoria! */
8 typedef struct _bs_data_t_ {
9 unsigned long int pos_inicial;
10 unsigned long int pos_final;
11 unsigned long int pos_orden;
12 char ord; /* indice si esta ordenada */
15 typedef struct _bs_t_ {
16 t_BlockSortData *array;
17 unsigned long int *ord;
18 unsigned long int len;
21 char es_menor(char *data, t_BlockSort *bs, int i, int j)
23 unsigned long int pi, pj, k;
25 for(k=0; k<bs->len; k++) {
26 pi = (bs->array[i].pos_inicial+k)%bs->len;
27 pj = (bs->array[j].pos_inicial+k)%bs->len;
29 if (data[pi] > data[pj]) return 0;
30 if (data[pi] < data[pj]) return 1;
36 void generar_array(char *data, t_BlockSort *bs)
39 for(i=0; i<bs->len; i++) {
40 bs->array[i].pos_inicial = i;
41 bs->array[i].pos_final = (i+bs->len-1)%bs->len;
42 bs->array[i].pos_orden = 0;
47 void ordenar_array(char *data, t_BlockSort *bs)
49 unsigned long int i, j, min;
51 for(i=0; i<bs->len; i++) {
53 for(j=0; j<bs->len; j++) {
54 if (bs->array[j].ord) continue;
55 if ((min==-1) || (es_menor(data, bs, j, min)))
59 bs->array[min].ord = 1;
60 bs->array[min].pos_orden = i;
66 int generar_salida(char *data, t_BlockSort *bs, char *salida)
68 unsigned long int i, k;
70 for(i=0; i<bs->len; i++) {
71 salida[i] = data[bs->array[bs->ord[i]].pos_final];
72 if (bs->array[bs->ord[i]].pos_inicial == 0) k = i;
77 void bs_solve(char *in, char *out, t_BlockSort *bs, unsigned long int *k, unsigned int leido)
81 /* Hack para pedasos menores a la pagina */
82 if (leido < bs->len) bs->len = leido;
84 generar_array(in, bs);
85 ordenar_array(in, bs);
86 (*k) = generar_salida(in, bs, out);
91 t_BlockSort *bs_create(unsigned int len)
95 tmp = malloc(sizeof(t_BlockSort));
96 tmp->array = malloc(sizeof(t_BlockSortData)*len);
97 tmp->ord = malloc(sizeof(unsigned long int)*len);
104 void bs_destroy(t_BlockSort *bs)
111 int main(int argc, char *argv[])
115 unsigned long int len, i, k;
121 printf("Modo de uso : %s <archivo datos> <tamaƱo pagina>\n", argv[0]);
125 fp = fopen(argv[1], "r");
128 data = malloc(sizeof(char)*len);
129 salida = malloc(sizeof(char)*len);
133 while ((c = fgetc(fp)) != EOF) {
135 while ((c!=EOF) && (i < len)) {
139 bs_solve(data, salida, bs, &k, i);
141 /* XXX ACA SALIDA DEBERIA PASAR A LA SIGUIENTE ETAPA XXX */