X-Git-Url: https://git.llucax.com/z.facultad/75.06/jacu.git/blobdiff_plain/79c3f2200fb945762a764093f545ce61edf691c8..86626b07ef6c182f6594bc0b398b0065cefe4aed:/otros/blocksorting/bs.c diff --git a/otros/blocksorting/bs.c b/otros/blocksorting/bs.c index 13b7824..b7caa54 100644 --- a/otros/blocksorting/bs.c +++ b/otros/blocksorting/bs.c @@ -1,28 +1,15 @@ -/* Block Sorting Optimizado en memoria! */ - -#include -#include -#include +#include "bs.h" -typedef struct _bs_data_t_ { - unsigned long int pos_inicial; - unsigned long int pos_final; - unsigned long int pos_orden; - char ord; /* indice si esta ordenada */ -} t_BlockSortData; - -typedef struct _bs_t_ { - t_BlockSortData *array; - unsigned long int *ord; - unsigned long int len; -} t_BlockSort; +/* Block Sorting Optimizado en memoria! */ typedef struct _bs_decode_t_ { char c; 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; @@ -32,13 +19,22 @@ int _compare(const void *d1, const void *d2) { 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; klen; 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; @@ -54,15 +50,16 @@ void generar_array(char *data, t_BlockSort *bs) 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 = 0; + bs->array[i].ord = (i==1)?1:0; + bs->array[i].bs = bs; } } void ordenar_array(char *data, t_BlockSort *bs) { - unsigned long int i, j, min; + /*unsigned long int i, j, min;*/ - for(i=0; ilen; i++) { +/* for(i=0; ilen; i++) { min = -1; for(j=0; jlen; j++) { if (bs->array[j].ord) continue; @@ -74,7 +71,8 @@ void ordenar_array(char *data, t_BlockSort *bs) 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) @@ -82,8 +80,8 @@ int generar_salida(char *data, t_BlockSort *bs, char *salida) unsigned long int i, k; k=-1; for(i=0; ilen; 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; } return k; } @@ -94,7 +92,8 @@ void bs_solve(char *in, char *out, t_BlockSort *bs, unsigned long int *k, unsign l = bs->len; /* Hack para pedasos menores a la pagina */ if (leido < bs->len) bs->len = leido; - + bs->data = in; + generar_array(in, bs); ordenar_array(in, bs); (*k) = generar_salida(in, bs, out); @@ -125,7 +124,7 @@ void bs_restore(char *dst, char *c, unsigned long int k, unsigned long int len) free(in); } -t_BlockSort *bs_create(unsigned int len) +t_BlockSort *bs_create(unsigned long int len) { t_BlockSort *tmp; @@ -145,46 +144,3 @@ void bs_destroy(t_BlockSort *bs) free(bs); } -int main(int argc, char *argv[]) -{ - char *data; - char *salida; - unsigned long int len, i, k; - FILE *fp; - char c; - t_BlockSort *bs; - - if (argc != 3) { - printf("Modo de uso : %s \n", argv[0]); - return 0; - } - - fp = fopen(argv[1], "r"); - len = atoi(argv[2]); - - data = malloc(sizeof(char)*len); - salida = malloc(sizeof(char)*(len+1)); - - salida[len] = '\0'; - bs = bs_create(len); - - while ((c = fgetc(fp)) != EOF) { - i = 0; - while ((c!=EOF) && (i < len)) { - data[i++] = c; - c = fgetc(fp); - } - bs_solve(data, salida, bs, &k, i); - - /* XXX ACA SALIDA DEBERIA PASAR A LA SIGUIENTE ETAPA XXX */ - printf("%s -> %ld\n", salida, k); - } - fclose(fp); - bs_destroy(bs); - - free(data); - free(salida); - return 0; -} - -