#include <stdlib.h>
#include <stdio.h>
-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)
+char es_menor(char *data, t_BlockSort *bs, int i, int j)
{
unsigned long int pi, pj, k;
- for(k=0; k<n; k++) {
- pi = (array[i].pos_inicial+k)%n;
- pj = (array[j].pos_inicial+k)%n;
+ 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;
if (data[pi] > data[pj]) return 0;
if (data[pi] < data[pj]) return 1;
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; i<n; i++) {
- array[i].pos_inicial = i;
- array[i].pos_final = (i+n-1)%n;
- array[i].pos_orden = 0;
- array[i].ord = 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 = 0;
}
}
-void ordenar_array(char *data, t_BlockSort *array, t_BlockSort *ord, unsigned long int n)
+void ordenar_array(char *data, t_BlockSort *bs)
{
unsigned long int i, j, min;
- for(i=0; i<n; i++) {
+ for(i=0; i<bs->len; i++) {
min = -1;
- for(j=0; j<n; j++) {
- if (array[j].ord) continue;
- if ((min==-1) || (es_menor(data, array, n, j, min)))
+ for(j=0; j<bs->len; 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;
- ord[i] = array[min];
+ 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, k;
k=-1;
- for(i=0; i<n; i++) {
- salida[i] = data[array[i].pos_final];
- if (array[i].pos_inicial == 0) k = i;
+ 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;
}
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;
+}
+
+t_BlockSort *bs_create(unsigned int len)
{
- static t_BlockSort *array = NULL;
- static t_BlockSort *ord = NULL;
+ t_BlockSort *tmp;
- if (!array) array = malloc(sizeof(t_BlockSort)*len);
- if (!ord) ord = malloc(sizeof(t_BlockSort)*len);
+ tmp = malloc(sizeof(t_BlockSort));
+ tmp->array = malloc(sizeof(t_BlockSortData)*len);
+ tmp->ord = malloc(sizeof(unsigned long int)*len);
+ tmp->len = len;
- generar_array(in, array, len);
- ordenar_array(in, array, ord, len);
- (*k) = generar_salida(in, ord, out, len);
+ return tmp;
+}
+
+
+void bs_destroy(t_BlockSort *bs)
+{
+ free(bs->array);
+ free(bs->ord);
+ free(bs);
}
int main(int argc, char *argv[])
unsigned long int len, i, k;
FILE *fp;
char c;
+ t_BlockSort *bs;
if (argc != 3) {
printf("Modo de uso : %s <archivo datos> <tamaƱo pagina>\n", argv[0]);
data = malloc(sizeof(char)*len);
salida = malloc(sizeof(char)*len);
+ bs = bs_create(len);
+
while ((c = fgetc(fp)) != EOF) {
- memset(salida, 0, len*sizeof(char));
i = 0;
while ((c!=EOF) && (i < len)) {
data[i++] = c;
c = fgetc(fp);
}
- block_sorting(data, salida, &k, (i<len)?i:len);
- printf("Salida = (%s) con k=%ld\n", salida, k);
+ bs_solve(data, salida, bs, &k, i);
+
+ /* XXX ACA SALIDA DEBERIA PASAR A LA SIGUIENTE ETAPA XXX */
}
fclose(fp);
+ bs_destroy(bs);
+ free(data);
+ free(salida);
return 0;
}