Commit 35753548 by Rudolf

Improve compression time

parent 4ee8062a
Showing with 24 additions and 1 deletions
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "huffman.h" #include "huffman.h"
#include "tree.h" #include "tree.h"
...@@ -170,15 +171,33 @@ static int get_rev_path(struct tree *parent, unsigned char c, int *buf, ...@@ -170,15 +171,33 @@ static int get_rev_path(struct tree *parent, unsigned char c, int *buf,
return -1; return -1;
} }
static int *cpaths[MY_BUF-1];
static int clengths[MY_BUF-1];
static int write_entry(struct BIT_BUFFER *bitbuf, struct tree *parent, static int write_entry(struct BIT_BUFFER *bitbuf, struct tree *parent,
unsigned char c, int *pathbuf) unsigned char c, int *pathbuf)
{ {
int length = get_rev_path(parent, c, pathbuf, 0); int length;
if (cpaths[c]) {
pathbuf = cpaths[c];
length = clengths[c];
}
else {
length = get_rev_path(parent, c, pathbuf, 0);
}
/* The path is in reverse. Now write it out in correct order. */ /* The path is in reverse. Now write it out in correct order. */
for (int i = length-1; i >= 0; i--) { for (int i = length-1; i >= 0; i--) {
bb_write(bitbuf, &pathbuf[i], 1); bb_write(bitbuf, &pathbuf[i], 1);
} }
if (cpaths[c] == NULL) {
cpaths[c] = calloc(length, sizeof(int));
memcpy(cpaths[c], pathbuf, length * sizeof(int));
clengths[c] = length;
}
return 0; return 0;
} }
...@@ -201,6 +220,10 @@ int encode_tree(struct BIT_BUFFER *bitbuf, struct tree *parent, char *buf, ...@@ -201,6 +220,10 @@ int encode_tree(struct BIT_BUFFER *bitbuf, struct tree *parent, char *buf,
free(pathbuf); free(pathbuf);
for (int i = 0; i < MY_BUF; i++) {
free(cpaths[i]);
}
insert_throwaways(bitbuf->fp, 8 - bitbuf->pos); insert_throwaways(bitbuf->fp, 8 - bitbuf->pos);
return 0; return 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment