* Agrego qsort para ordenar en el BS (tengo que revisar, algo falla un poquito)
unsigned long int pos;
} t_BlockSortDecode;
unsigned long int pos;
} t_BlockSortDecode;
+char es_menor(char *data, t_BlockSort *bs, int i, int j);
+
int _compare(const void *d1, const void *d2) {
t_BlockSortDecode *s1, *s2;
int _compare(const void *d1, const void *d2) {
t_BlockSortDecode *s1, *s2;
return (s1->c - s2->c);
}
return (s1->c - s2->c);
}
+int __compare(const void *d1, const void *d2) {
+ t_BlockSortData *s1, *s2;
+
+ s1 = (t_BlockSortData *)d1;
+ s2 = (t_BlockSortData *)d2;
+
+ return es_menor(s1->bs->data, s1->bs, s1->pos_inicial, s2->pos_inicial);
+}
+
char es_menor(char *data, t_BlockSort *bs, int i, int j)
{
unsigned long int pi, pj, k;
for(k=0; k<bs->len; k++) {
char es_menor(char *data, t_BlockSort *bs, int i, int j)
{
unsigned long int pi, pj, k;
for(k=0; k<bs->len; k++) {
- pi = (bs->array[i].pos_inicial+k)%bs->len;
- pj = (bs->array[j].pos_inicial+k)%bs->len;
+ 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 0;
if (data[pi] < data[pj]) return 1;
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].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].bs = bs;
}
}
void ordenar_array(char *data, t_BlockSort *bs)
{
}
}
void ordenar_array(char *data, t_BlockSort *bs)
{
- unsigned long int i, j, min;
+ /*unsigned long int i, j, min;*/
- for(i=0; i<bs->len; i++) {
+/* for(i=0; i<bs->len; i++) {
min = -1;
for(j=0; j<bs->len; j++) {
if (bs->array[j].ord) continue;
min = -1;
for(j=0; j<bs->len; j++) {
if (bs->array[j].ord) continue;
bs->array[min].pos_orden = i;
bs->ord[i] = min;
bs->array[min].pos_orden = i;
bs->ord[i] = min;
+ }*/
+ qsort(bs->array, bs->len, sizeof(t_BlockSortData), __compare);
}
int generar_salida(char *data, t_BlockSort *bs, char *salida)
}
int generar_salida(char *data, t_BlockSort *bs, char *salida)
unsigned long int i, k;
k=-1;
for(i=0; i<bs->len; i++) {
unsigned long int i, k;
k=-1;
for(i=0; i<bs->len; i++) {
- salida[i] = data[bs->array[bs->ord[i]].pos_final];
- if (bs->array[bs->ord[i]].pos_inicial == 0) k = i;
+ salida[i] = data[bs->array[i].pos_final];
+ if (bs->array[i].ord == 1) k = i;
l = bs->len;
/* Hack para pedasos menores a la pagina */
if (leido < bs->len) bs->len = leido;
l = bs->len;
/* Hack para pedasos menores a la pagina */
if (leido < bs->len) bs->len = leido;
generar_array(in, bs);
ordenar_array(in, bs);
(*k) = generar_salida(in, bs, out);
generar_array(in, bs);
ordenar_array(in, bs);
(*k) = generar_salida(in, bs, out);
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
+typedef struct _bs_t_ t_BlockSort;
+
typedef struct _bs_data_t_ {
unsigned long int pos_inicial;
unsigned long int pos_final;
unsigned long int pos_orden;
char ord; /* indica si esta ordenada */
typedef struct _bs_data_t_ {
unsigned long int pos_inicial;
unsigned long int pos_final;
unsigned long int pos_orden;
char ord; /* indica si esta ordenada */
+
+ /* Guardo el puntero al padre */
+ t_BlockSort *bs;
+struct _bs_t_ {
+ char *data;
t_BlockSortData *array;
unsigned long int *ord;
unsigned long int len;
t_BlockSortData *array;
unsigned long int *ord;
unsigned long int len;
/** Inicializa un BlockSorting
*
/** Inicializa un BlockSorting
*
{
char *data;
char *salida;
{
char *data;
char *salida;
- unsigned long int len, i, k;
+ unsigned long int len, i, k, total;
FILE *fp;
char c;
t_BlockSort *bs;
FILE *fp;
char c;
t_BlockSort *bs;
-/* if (argc != 3) {
- printf("Modo de uso : %s <archivo datos> <tamaƱo pagina>\n", argv[0]);
- return 0;
+ if (argc == 3) {
+ fp = fopen(argv[1], "r");
+ len = atoi(argv[2]);
+ } else if (argc == 2) {
+ fp = stdin; /*fopen(argv[1], "r");*/
+ len = atoi(argv[1]);
+ } else {
+ printf("no, no\n");
+ return 1;
-*/
- fp = stdin; /*fopen(argv[1], "r");*/
- len = atoi(argv[1]);
data = malloc(sizeof(char)*len);
salida = malloc(sizeof(char)*(len+1));
data = malloc(sizeof(char)*len);
salida = malloc(sizeof(char)*(len+1));
salida[len] = '\0';
bs = bs_create(len);
salida[len] = '\0';
bs = bs_create(len);
- while ((c = fgetc(fp)) != EOF) {
+ c = fgetc(fp);
+ total = 0;
+ while (!feof(fp)) {
- while ((c!=EOF) && (i < len)) {
+ while ((!feof(fp)) && (i < len)) {
data[i++] = c;
c = fgetc(fp);
data[i++] = c;
c = fgetc(fp);
}
bs_solve(data, salida, bs, &k, i);
/* XXX ACA SALIDA DEBERIA PASAR A LA SIGUIENTE ETAPA XXX */
}
bs_solve(data, salida, bs, &k, i);
/* XXX ACA SALIDA DEBERIA PASAR A LA SIGUIENTE ETAPA XXX */
- /*printf("%s -> %ld\n", salida, k);*/
- printf("%s", salida);
+ if (argc == 3)
+ printf("%s -> %ld\n", salida, k);
+ else
+ printf("%s", salida);
}
fclose(fp);
bs_destroy(bs);
}
fclose(fp);
bs_destroy(bs);
+ printf("Total bytes : %ld\n", total);
free(data);
free(salida);
return 0;
free(data);
free(salida);
return 0;