summaryrefslogtreecommitdiff
path: root/lib/HTree.cc
blob: 985d6878ec5641acf78cf15e46962aa039d0c632 (plain)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdio.h>
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
#include "HTree.h"

ostream & PrintString(ostream & os, char *str)
{
	char t[2];

	t[1] = '\0';
	while (*str) {
		if (*str >= 33) {
			t[0] = *str;
			os << t;
		} else {
			os << "\\" << ((unsigned int) (((unsigned char) *str)));
		}
		str++;
	}

	return os;
}

HTree::HTree(int n_freq, char *n_objet)
{
	freq = n_freq;
	objet = strdup(n_objet);
	left = right = NULL;
}

HTree::HTree(HTree * n_left, HTree * n_right)
{
	left = n_left;
	right = n_right;
	freq = n_left->freq + n_right->freq;
	objet = NULL;
}

HTree::~HTree()
{
	if (left)
		delete left;

	if (right)
		delete right;

	if (objet)
		free(objet);
}

ostream & HTree::Trace(ostream & os, int d)
{
	static char cmpr[MAX_HDEPTH + 1];
	static int tsize;
	static int rsize;
	static int dsize;

	if (!d)
		tsize = rsize = dsize = 0;

	if (objet) {
		cmpr[d] = '\0';
		PrintString(os, objet);
		os << " (" << freq << ") = " << cmpr << endl;
		tsize += freq * strlen(cmpr);
		rsize += freq * strlen(objet);
		dsize += ((strlen(cmpr) >> 3) + (strlen(cmpr) & 7 ? 1 : 0)) + strlen(objet) + 2;
	} else {
		if (left) {
			cmpr[d] = '0';
			left->Trace(os, d + 1);
		}
		if (right) {
			cmpr[d] = '1';
			right->Trace(os, d + 1);
		}
	}

	if (!d) {
		os << _("Bitstream length           : ") << tsize << _(" bits (= ") << ((tsize >> 3) +
											(tsize & 7 ? 1 : 0)) <<
			_(" bytes)\n");
		os << _("Real size input            : ") << (rsize << 3) << _(" bits (= ") << rsize << _(" bytes)\n");
		os << _("Size squeezed by           : ") << 100.0 - 100.0 * tsize / (rsize << 3) << _(" percents\n");
		os << _("Dictionnary size           : ") << (dsize << 3) << _(" bits (= ") << dsize << _(" bytes)\n");
		os << _("Total bitstream length     : ") << tsize + (dsize << 3) << _(" bits (= ") << ((tsize >> 3) +
												       (tsize & 7 ? 1 :
													0) +
												       dsize) <<
			_(" bytes)\n");
		os << _("Real gain (4 bytes header) : ") << 100.0 - 100.0 * ((tsize >> 3) + (tsize & 7 ? 1 : 0) +
									     dsize + 4) / rsize << _(" percents\n");
	}
	return os;
}

int HTree::ReadFreq(void)
{
	return freq;
}

char *HTree::ReadObj(void)
{
	return objet;
}