-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_symbol_codes_from_tree.c
79 lines (72 loc) · 2.58 KB
/
build_symbol_codes_from_tree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "build_symbol_codes_from_tree.h"
#include "consts.h"
void buildSymbolCodesFromTreeUtil(
unsigned char **table,
HuffmanNode *tree,
unsigned char *leafPathCreatedSoFar,
size_t leafPathCreatedSoFarIndex
);
unsigned char **buildSymbolCodesFromTree(HuffmanNode *tree) {
unsigned char **table = (unsigned char**) calloc(ASCII_SYMBOLS, sizeof(unsigned char*));
if (table == NULL) {
fprintf(stderr, "failed to allocate space for huffman table at file %s:%d\n", __FILE__, __LINE__);
return NULL;
}
for (size_t i = 0; i < ASCII_SYMBOLS; i++) {
table[i] = (unsigned char*) calloc(ASCII_SYMBOLS, sizeof(unsigned char));
if (table[i] == NULL) {
fprintf(stderr, "failed to allocate space for table line at file %s:%d\n", __FILE__, __LINE__);
destroyHuffmanTable(table);
return NULL;
}
}
unsigned char *symbolCodeBuilt = (unsigned char*) calloc(ASCII_SYMBOLS, sizeof(unsigned char));
if (symbolCodeBuilt == NULL) {
fprintf(stderr, "failed to allocate space for compressed codes buffer at file %s:%d\n", __FILE__, __LINE__);
destroyHuffmanTable(table);
return NULL;
}
size_t symbolCodeBuiltIndex = 0;
buildSymbolCodesFromTreeUtil(table, tree, symbolCodeBuilt, symbolCodeBuiltIndex);
free(symbolCodeBuilt);
return table;
}
void destroyHuffmanTable(unsigned char **table) {
for (int i = 0; i < ASCII_SYMBOLS; i++) {
free(table[i]);
}
free(table);
}
void printHuffmanTable(unsigned char **table) {
for (int i = 0; i < ASCII_SYMBOLS; i++) {
if (table[i][0] != '\0') {
printf("[%d]->[%s]\n", i, table[i]);
}
}
}
void buildSymbolCodesFromTreeUtil(
unsigned char **table,
HuffmanNode *tree,
unsigned char *leafPathCreatedSoFar,
size_t leafPathCreatedSoFarIndex
) {
uint8_t currentNodeIsALeaf = tree->left == NULL && tree->right == NULL;
if (currentNodeIsALeaf) {
leafPathCreatedSoFar[leafPathCreatedSoFarIndex] = '\0';
unsigned char currentLeafSymbol = tree->byte;
size_t oneExtraByteForStringNullTerminator = 1;
memcpy(table[currentLeafSymbol], leafPathCreatedSoFar, leafPathCreatedSoFarIndex + oneExtraByteForStringNullTerminator);
}
if (tree->left != NULL) {
leafPathCreatedSoFar[leafPathCreatedSoFarIndex] = '0';
buildSymbolCodesFromTreeUtil(table, tree->left, leafPathCreatedSoFar, leafPathCreatedSoFarIndex + 1);
}
if (tree->right != NULL) {
leafPathCreatedSoFar[leafPathCreatedSoFarIndex] = '1';
buildSymbolCodesFromTreeUtil(table, tree->right, leafPathCreatedSoFar, leafPathCreatedSoFarIndex + 1);
}
}