X-Git-Url: https://git.llucax.com/z.facultad/75.06/jacu.git/blobdiff_plain/69611d873aae9e98aa5be48e3f1ce9730ad8b6de..79c3f2200fb945762a764093f545ce61edf691c8:/otros/blocksorting/bs.c diff --git a/otros/blocksorting/bs.c b/otros/blocksorting/bs.c index e5dd16f..13b7824 100644 --- a/otros/blocksorting/bs.c +++ b/otros/blocksorting/bs.c @@ -5,20 +5,40 @@ #include #include -typedef struct _bs_t_ { +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; -char es_menor(char *data, t_BlockSort *array, int n, int i, int j) +typedef struct _bs_decode_t_ { + char c; + unsigned long int pos; +} t_BlockSortDecode; + +int _compare(const void *d1, const void *d2) { + t_BlockSortDecode *s1, *s2; + + s1 = (t_BlockSortDecode *)d1; + s2 = (t_BlockSortDecode *)d2; + + return (s1->c - s2->c); +} + +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; if (data[pi] > data[pj]) return 0; if (data[pi] < data[pj]) return 1; @@ -27,60 +47,102 @@ char es_menor(char *data, t_BlockSort *array, int n, int i, int j) return 0; } -void generar_array(char *data, t_BlockSort *array, unsigned long int n) +void generar_array(char *data, t_BlockSort *bs) { int i; - for(i=0; ilen; 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 = 0; } } -void ordenar_array(char *data, t_BlockSort *array, unsigned long int n) +void ordenar_array(char *data, t_BlockSort *bs) { unsigned long int i, j, min; - for(i=0; ilen; i++) { min = -1; - for(j=0; jlen; j++) { + if (bs->array[j].ord) continue; + if ((min==-1) || (es_menor(data, bs, j, min))) min = j; } - array[min].ord = 1; - array[min].pos_orden = i; + bs->array[min].ord = 1; + bs->array[min].pos_orden = i; + + bs->ord[i] = min; } } -int generar_salida(char *data, t_BlockSort *array, char *salida, int n) +int generar_salida(char *data, t_BlockSort *bs, char *salida) { - unsigned long int i, j, k; + 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; } - return k; } -void block_sorting(char *in, char *out, unsigned long int *k, unsigned long int len) +void bs_solve(char *in, char *out, t_BlockSort *bs, unsigned long int *k, unsigned int leido) +{ + unsigned int l; + 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); + + bs->len = l; +} + +void bs_restore(char *dst, char *c, unsigned long int k, unsigned long int len) { - t_BlockSort *array; + unsigned long int i, current; + t_BlockSortDecode *in; - array = malloc(sizeof(t_BlockSort)*len); + in = malloc(sizeof(t_BlockSortDecode)*len); - generar_array(in, array, len); - ordenar_array(in, array, len); - (*k) = generar_salida(in, array, out, len); + for(i=0; iarray = malloc(sizeof(t_BlockSortData)*len); + tmp->ord = malloc(sizeof(unsigned long int)*len); + tmp->len = len; - free(array); + return tmp; +} + + +void bs_destroy(t_BlockSort *bs) +{ + free(bs->array); + free(bs->ord); + free(bs); } int main(int argc, char *argv[]) @@ -89,7 +151,9 @@ int main(int argc, char *argv[]) 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; @@ -99,17 +163,27 @@ int main(int argc, char *argv[]) len = atoi(argv[2]); data = malloc(sizeof(char)*len); - salida = malloc(sizeof(char)*len); - memset(salida, 0, len*sizeof(char)); + salida = malloc(sizeof(char)*(len+1)); - for(i=0; i %ld\n", salida, k); + } + fclose(fp); + bs_destroy(bs); - printf("Salida = (%s) con k=%ld\n", salida, k); + free(data); + free(salida); return 0; }