2012-08-09 19:09:29 +00:00
|
|
|
#include "huffman.h"
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
static int
|
|
|
|
find_smallest (struct huffman_node **nodes, int count, int different)
|
|
|
|
{
|
2012-09-06 18:55:17 +00:00
|
|
|
// 'real' weights will always be positive.
|
|
|
|
int smallest = -1;
|
2012-08-09 19:09:29 +00:00
|
|
|
int i;
|
|
|
|
|
2012-09-06 18:55:17 +00:00
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
if (i == different) {
|
2012-08-09 19:09:29 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-09-06 18:55:17 +00:00
|
|
|
if (smallest == -1 ||
|
|
|
|
nodes[i]->weight < nodes[smallest]->weight) {
|
2012-08-09 19:09:29 +00:00
|
|
|
smallest = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return smallest;
|
|
|
|
}
|
|
|
|
|
2012-09-06 18:55:17 +00:00
|
|
|
static void
|
|
|
|
shift_nodes (struct huffman_node **nodes, int count, int start)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = start; i + 1 < count; i++) {
|
|
|
|
nodes[i] = nodes[i + 1];
|
|
|
|
}
|
|
|
|
nodes[i] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-09 19:09:29 +00:00
|
|
|
struct huffman_node *
|
|
|
|
huffman_build_tree(void **values, int count)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct huffman_node **nodes;
|
|
|
|
|
|
|
|
|
|
|
|
nodes = malloc (sizeof (struct huffman_node *) * count);
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
struct huffman_node *node =
|
|
|
|
malloc (sizeof (struct huffman_node));
|
|
|
|
|
|
|
|
node->value = values[i];
|
2012-08-10 13:49:13 +00:00
|
|
|
node->weight = i + 1;
|
2012-08-09 19:09:29 +00:00
|
|
|
node->left = NULL;
|
|
|
|
node->right = NULL;
|
|
|
|
|
|
|
|
nodes[i] = node;
|
|
|
|
}
|
|
|
|
|
|
|
|
int tree1;
|
|
|
|
int tree2;
|
2012-09-06 18:55:17 +00:00
|
|
|
for (; count > 1; count--) {
|
2012-08-09 19:09:29 +00:00
|
|
|
struct huffman_node *tmp;
|
|
|
|
|
|
|
|
tree1 = find_smallest (nodes, count, -1);
|
|
|
|
tree2 = find_smallest (nodes, count, tree1);
|
|
|
|
|
2012-09-06 18:55:17 +00:00
|
|
|
tmp = malloc (sizeof (struct huffman_node));
|
|
|
|
tmp->weight = nodes[tree1]->weight + nodes[tree2]->weight;
|
|
|
|
tmp->value = NULL;
|
|
|
|
tmp->left = nodes[tree1];
|
|
|
|
tmp->right = nodes[tree2];
|
2012-08-09 19:09:29 +00:00
|
|
|
|
2012-09-06 18:55:17 +00:00
|
|
|
if (tree1 > tree2) {
|
|
|
|
shift_nodes (nodes, count, tree1);
|
|
|
|
shift_nodes (nodes, count, tree2);
|
|
|
|
} else {
|
|
|
|
shift_nodes (nodes, count, tree2);
|
|
|
|
shift_nodes (nodes, count, tree1);
|
|
|
|
}
|
2012-08-09 19:09:29 +00:00
|
|
|
|
2012-09-06 18:55:17 +00:00
|
|
|
nodes[count - 2] = tmp;
|
2012-08-09 19:09:29 +00:00
|
|
|
}
|
|
|
|
|
2012-09-06 18:55:17 +00:00
|
|
|
return nodes[0];
|
2012-08-09 19:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2012-09-06 14:12:54 +00:00
|
|
|
huffman_lookup (struct huffman_node *tree, unsigned char *bits, int *bits_read,
|
|
|
|
bool print)
|
2012-08-09 19:09:29 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
struct huffman_node *node = tree;
|
|
|
|
|
|
|
|
while (true) {
|
2012-08-10 13:49:13 +00:00
|
|
|
if (node == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-08-09 19:09:29 +00:00
|
|
|
if (node->value != NULL) {
|
|
|
|
return node->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((bits[0] << *bits_read % 8 & 0x80) == 0) {
|
|
|
|
node = node->left;
|
2012-09-06 14:12:54 +00:00
|
|
|
if (print) {
|
|
|
|
putchar ('0');
|
|
|
|
}
|
2012-08-09 19:09:29 +00:00
|
|
|
} else {
|
|
|
|
node = node->right;
|
2012-09-06 14:12:54 +00:00
|
|
|
if (print) {
|
|
|
|
putchar ('1');
|
|
|
|
}
|
2012-08-09 19:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
(*bits_read)++;
|
|
|
|
if (*bits_read % 8 == 0) {
|
|
|
|
bits++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-09-06 14:51:37 +00:00
|
|
|
|
|
|
|
struct stack {
|
|
|
|
struct stack *next;
|
|
|
|
bool val;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
huffman_lookup_driver (struct huffman_node *tree, void *value,
|
|
|
|
struct stack *head, struct stack *cur)
|
|
|
|
{
|
|
|
|
if (tree->value == value) {
|
|
|
|
struct stack *x = head->next;
|
|
|
|
while (x != NULL) {
|
|
|
|
if (x->val) {
|
|
|
|
putchar ('1');
|
|
|
|
} else {
|
|
|
|
putchar ('0');
|
|
|
|
}
|
|
|
|
x = x->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct stack *next = malloc (sizeof (struct stack));
|
|
|
|
next->next = NULL;
|
|
|
|
cur->next = next;
|
|
|
|
|
|
|
|
if (tree->left != NULL) {
|
|
|
|
next->val = false;
|
|
|
|
huffman_lookup_driver (tree->left, value, head, next);
|
|
|
|
}
|
|
|
|
if (tree->right != NULL) {
|
|
|
|
next->val = true;
|
|
|
|
huffman_lookup_driver (tree->right, value, head, next);
|
|
|
|
}
|
|
|
|
|
|
|
|
cur->next = NULL;
|
|
|
|
free (next);
|
|
|
|
}
|
|
|
|
|
|
|
|
// given a value, print its code to stdout.
|
|
|
|
void
|
|
|
|
huffman_reverse_lookup (struct huffman_node *tree, void *value)
|
|
|
|
{
|
|
|
|
struct stack head;
|
|
|
|
head.next = NULL;
|
|
|
|
huffman_lookup_driver (tree, value, &head, &head);
|
|
|
|
}
|