4 /* Block Sorting Optimizado en memoria! */
6 typedef struct _bs_decode_t_ {
11 char es_menor(char *data, t_BlockSort *bs, int i, int j);
13 int _compare(const void *d1, const void *d2) {
14 t_BlockSortDecode *s1, *s2;
16 s1 = (t_BlockSortDecode *)d1;
17 s2 = (t_BlockSortDecode *)d2;
19 return (s1->c - s2->c);
22 int __compare(const void *d1, const void *d2) {
23 t_BlockSortData *s1, *s2;
25 s1 = (t_BlockSortData *)d1;
26 s2 = (t_BlockSortData *)d2;
28 return es_menor(s1->bs->data, s1->bs, s1->pos_inicial, s2->pos_inicial);
31 char es_menor(char *data, t_BlockSort *bs, int i, int j)
33 unsigned long int pi, pj, k;
35 for(k=0; k<bs->len; k++) {
39 if (data[pi] > data[pj]) return 0;
40 if (data[pi] < data[pj]) return 1;
46 void generar_array(char *data, t_BlockSort *bs)
49 for(i=0; i<bs->len; i++) {
50 bs->array[i].pos_inicial = i;
51 bs->array[i].pos_final = (i+bs->len-1)%bs->len;
52 bs->array[i].pos_orden = 0;
53 bs->array[i].ord = (i==1)?1:0;
58 void ordenar_array(char *data, t_BlockSort *bs)
60 /*unsigned long int i, j, min;*/
62 /* for(i=0; i<bs->len; i++) {
64 for(j=0; j<bs->len; j++) {
65 if (bs->array[j].ord) continue;
66 if ((min==-1) || (es_menor(data, bs, j, min)))
70 bs->array[min].ord = 1;
71 bs->array[min].pos_orden = i;
75 qsort(bs->array, bs->len, sizeof(t_BlockSortData), __compare);
78 int generar_salida(char *data, t_BlockSort *bs, char *salida)
80 unsigned long int i, k;
82 for(i=0; i<bs->len; i++) {
83 salida[i] = data[bs->array[i].pos_final];
84 if (bs->array[i].ord == 1) k = i;
89 void bs_solve(char *in, char *out, t_BlockSort *bs, unsigned long int *k, unsigned int leido)
93 /* Hack para pedasos menores a la pagina */
94 if (leido < bs->len) bs->len = leido;
97 generar_array(in, bs);
98 ordenar_array(in, bs);
99 (*k) = generar_salida(in, bs, out);
104 void bs_restore(char *dst, char *c, unsigned long int k, unsigned long int len)
106 unsigned long int i, current;
107 t_BlockSortDecode *in;
109 in = malloc(sizeof(t_BlockSortDecode)*len);
111 for(i=0; i<len; i++) {
116 qsort(in, len, sizeof(t_BlockSortDecode), _compare);
121 dst[i++] = in[current].c;
122 current = in[current].pos;
123 } while (current != k);
127 t_BlockSort *bs_create(unsigned long int len)
131 tmp = malloc(sizeof(t_BlockSort));
132 tmp->array = malloc(sizeof(t_BlockSortData)*len);
133 tmp->ord = malloc(sizeof(unsigned long int)*len);
140 void bs_destroy(t_BlockSort *bs)