pi = (i+k)%bs->len;
pj = (j+k)%bs->len;
- if (data[pi] > data[pj]) return 0;
- if (data[pi] < data[pj]) return 1;
+ if (data[pi] > data[pj]) return 1; /* El primero es mayor */
+ if (data[pi] < data[pj]) return -1; /* El primero es menor */
}
+ /* Son iguales! */
return 0;
}
for(i=0; i<bs->len; i++) {
bs->array[i].pos_inicial = i;
bs->array[i].pos_final = (i+bs->len-1)%bs->len;
- bs->array[i].pos_orden = 0;
- bs->array[i].ord = (i==1)?1:0;
+ bs->array[i].ord = (i==0)?1:0;
bs->array[i].bs = bs;
}
}
void ordenar_array(char *data, t_BlockSort *bs)
{
- /*unsigned long int i, j, min;*/
-
-/* for(i=0; i<bs->len; i++) {
- min = -1;
- for(j=0; j<bs->len; j++) {
- if (bs->array[j].ord) continue;
- if ((min==-1) || (es_menor(data, bs, j, min)))
- min = j;
- }
+ qsort(bs->array, bs->len, sizeof(t_BlockSortData), __compare);
+}
- bs->array[min].ord = 1;
- bs->array[min].pos_orden = i;
+void print_(char *data, unsigned long int pos, unsigned long int len)
+{
+ unsigned long int i;
- bs->ord[i] = min;
- }*/
- qsort(bs->array, bs->len, sizeof(t_BlockSortData), __compare);
+ for(i=0; i<len; i++)
+ printf("%c", data[(pos+i)%len]);
+ printf("\n");
}
int generar_salida(char *data, t_BlockSort *bs, char *salida)
unsigned long int i, k;
k=-1;
for(i=0; i<bs->len; i++) {
+ /* print_(data, bs->array[i].pos_inicial, bs->len); */
salida[i] = data[bs->array[i].pos_final];
if (bs->array[i].ord == 1) k = i;
}
tmp = malloc(sizeof(t_BlockSort));
tmp->array = malloc(sizeof(t_BlockSortData)*len);
- tmp->ord = malloc(sizeof(unsigned long int)*len);
tmp->len = len;
return tmp;
void bs_destroy(t_BlockSort *bs)
{
free(bs->array);
- free(bs->ord);
free(bs);
}
{
char *data;
char *salida;
+ char *orig;
unsigned long int len, i, k, total;
FILE *fp;
char c;
data = malloc(sizeof(char)*len);
salida = malloc(sizeof(char)*(len+1));
+ orig = malloc(sizeof(char)*(len+1));
salida[len] = '\0';
+ orig[len] = '\0';
+
bs = bs_create(len);
c = fgetc(fp);
bs_solve(data, salida, bs, &k, i);
/* XXX ACA SALIDA DEBERIA PASAR A LA SIGUIENTE ETAPA XXX */
- if (argc == 3)
- printf("%s -> %ld\n", salida, k);
- else
+ if (argc == 3) {
+ printf("BS : %s -> %ld\n", salida, k);
+ bs_restore(orig, salida, k, i);
+ printf("Orig : %s\n", orig);
+ } else
printf("%s", salida);
}
fclose(fp);
printf("Total bytes : %ld\n", total);
free(data);
free(salida);
+ free(orig);
return 0;
}