#include "bs.h"
#include <stdlib.h>
+#include <ctype.h>
/* Block Sorting Optimizado en memoria! */
typedef struct _bs_decode_t_ {
char c;
- unsigned long int pos;
+ Uint32 sig;
} t_BlockSortDecode;
char es_menor(char *data, t_BlockSort *bs, int i, int j);
s1 = (t_BlockSortDecode *)d1;
s2 = (t_BlockSortDecode *)d2;
+ /* Hack si es el mismo caracter, dejo como mejor la de menor sig */
+ if (s1->c == s2->c) {
+ return (s1->sig - s2->sig);
+ }
+
return (s1->c - s2->c);
}
s1 = (t_BlockSortData *)d1;
s2 = (t_BlockSortData *)d2;
- return es_menor(s1->bs->data, s1->bs, s1->pos_inicial, s2->pos_inicial);
+ 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;
+ Uint32 pi, pj, k;
for(k=0; k<bs->len; k++) {
pi = (i+k)%bs->len;
qsort(bs->array, bs->len, sizeof(t_BlockSortData), __compare);
}
-void print_(char *data, unsigned long int pos, unsigned long int len)
-{
- unsigned long int i;
-
- for(i=0; i<len; i++)
- printf("%c", data[(pos+i)%len]);
- printf("\n");
-}
-
int generar_salida(char *data, t_BlockSort *bs, char *salida)
{
- unsigned long int i, k;
+ Uint32 i, k;
char *out;
- /* Dejo lugar para guardar el k y el tamaño de este bloque */
- out = salida + sizeof(unsigned long int)*2;
+ /* Dejo lugar para guardar el K */
+ out = salida + sizeof(Uint32);
k=-1;
for(i=0; i<bs->len; i++) {
- /* print_(data, bs->array[i].pos_inicial, bs->len); */
out[i] = data[bs->array[i].pos_final];
if (bs->array[i].ord == 1) k = i;
}
return k;
}
-void bs_solve(char *in, char *out, t_BlockSort *bs, unsigned long int *k, unsigned int leido)
+void bs_solve(char *in, char *out, t_BlockSort *bs, Uint32 *k, Uint32 leido)
{
unsigned int l;
l = bs->len;
ordenar_array(in, bs);
(*k) = generar_salida(in, bs, out);
- /* Guardo el k y el tamaño en el array */
- memcpy(out, &leido, sizeof(unsigned long int));
- memcpy(out+sizeof(unsigned long int), k, sizeof(unsigned long int));
-
+ /* Guardo el k y el tamaño en el array */
+ memcpy(out, k, sizeof(Uint32));
bs->len = l;
}
-void bs_restore(char *dst, char *c, unsigned long int k, unsigned long int len)
+void bs_restore(char *dst, char *c, Uint32 k, Uint32 len)
{
- unsigned long int i, current;
+ Uint32 i, current;
t_BlockSortDecode *in;
in = malloc(sizeof(t_BlockSortDecode)*len);
for(i=0; i<len; i++) {
in[i].c = c[i];
- in[i].pos = i;
+ in[i].sig = i;
}
qsort(in, len, sizeof(t_BlockSortDecode), _compare);
i=0;
do {
dst[i++] = in[current].c;
- current = in[current].pos;
+ current = in[current].sig;
} while (current != k);
free(in);
}
-t_BlockSort *bs_create(unsigned long int len)
+t_BlockSort *bs_create(Uint32 len)
{
t_BlockSort *tmp;
free(bs);
}
+
+void bs_EOL(char *data, Uint32 pagesize, Uint32 *j)
+{
+ /* Trato de agregar 4 espacios antes y 4 despues de un \n */
+ int i = (*j);
+
+ /* Verifico poder hacerlo */
+ if ((i+9) >= pagesize) return; /* No pude! */
+
+ data[i++] = ' ';
+ data[i++] = ' ';
+ data[i++] = ' ';
+ data[i++] = ' ';
+ data[i++] = ' ';
+ data[i++] = ' ';
+ data[i++] = ' ';
+ data[i++] = ' ';
+ data[i++] = ' ';
+
+ (*j) += 9;
+}
+
+int bs_readblock(FILE *fp, char *data, Uint32 pagesize)
+{
+ Uint32 i=0;
+ char c;
+ while ((!feof(fp)) && (i < pagesize)) {
+ c = fgetc(fp);
+/* if (c != '\n')*/
+ if (c == '\0') {
+ /* Debo encodear el \0 para que no complique */
+ data[i++] = 0x00;
+ data[i++] = 0xFF;
+ }
+ if (isupper(c)) {
+ data[i++] = '\0';
+ data[i++] = tolower(c);
+ } else {
+ data[i++] = c;
+ }
+/* else
+ bs_EOL(data, pagesize, &i);*/
+ }
+
+ /* Saco un EOF que lee de mas */
+ if (i<pagesize) i--;
+
+ return i;
+}
+