huffman.cpp

// Huffman coding
// 
// Simplified code as example for the huffman coding.
//
// by feyd//godX.de
 
#include <vector>
#include <stdio.h>
#include <math.h>
 
class CHuffman
{
    struct Histogram
    {
        int nCount;
        size_t nLink;
        int nBits;
        int nCode;
    };
    struct BitHistogram
    {
        int nBase;
        int nCount;
    };
 
public:
    CHuffman(size_t nSymbols) : m_nSymbols(nSymbols), m_histogram(nSymbols*2) {}
    virtual ~CHuffman() {}
 
    void BuildTree(const std::vector<int>& codes)
    {
        // Clear histogram
        for(size_t n=0; n<m_histogram.size(); n++)
        {
            m_histogram[n].nLink = 0;
            m_histogram[n].nCount = 0;
            m_histogram[n].nBits = 0;
        }
 
        // Build histogram
        for(size_t n=0; n<codes.size(); n++)
        {
            if(codes[n]>=m_nSymbols)
            {
                printf("Error not enough symbols\r\n");
                return;
            }
            m_histogram[codes[n]].nCount++;
        }
 
        // Build tree (slow, good point to optimize ;) )
        size_t nMaxSymbol = m_nSymbols;
        while(true)
        {
            size_t nSmallest1 = nMaxSymbol;
            size_t nSmallest2 = nMaxSymbol;
            for(size_t n=0; n<nMaxSymbol; n++)
            {
                if(m_histogram[n].nLink!=0 || m_histogram[n].nCount==0)
                    continue;
                if(nSmallest1<nMaxSymbol && nSmallest2<nMaxSymbol)
                {
                    if(m_histogram[nSmallest1].nCount>m_histogram[nSmallest2].nCount)
                    {
                        if(m_histogram[n].nCount<m_histogram[nSmallest1].nCount)
                            nSmallest1 = n;
                    } else {
                        if(m_histogram[n].nCount<m_histogram[nSmallest2].nCount)
                            nSmallest2 = n;
                    }
                } else {
                    nSmallest1 = nSmallest2;
                    nSmallest2 = n;
                }
            }
            if(nSmallest1==nMaxSymbol)
                break;
            m_histogram[nSmallest1].nLink = nMaxSymbol;
            m_histogram[nSmallest2].nLink = nMaxSymbol;
            m_histogram[nMaxSymbol].nCount = m_histogram[nSmallest1].nCount+m_histogram[nSmallest2].nCount;
            nMaxSymbol++;
        }
 
        BuildBinaryCodes();
    }
 
    void PrintCode(int nCode)
    {
        for(int n=m_histogram[nCode].nBits-1; n>=0; n--)
            printf("%d", (m_histogram[nCode].nCode>>n)&1);
    }
 
    void PrintHistogram()
    {
        printf(" Tables:\r\n");
        for(size_t n=0; n<m_nSymbols; n++)
        {
            if(m_histogram[n].nCount==0)
                continue;
            printf(" %5d (%c). %5d codes / %2d bits ", (int)n, (n<32 || n>=128)?'.':(char)n, m_histogram[n].nCount, m_histogram[n].nBits);
            PrintCode(n);
            printf("\r\n");
        }
        printf("\r\n");
    }
 
    void Encode(const std::vector<int>& codes)
    {
        printf(" Encoded:\r\n");
        printf(" ");
        float fSymbolSizeIn = log((float)m_nSymbols)/log(2.0f);
        int nBitsOut = 0;
        for(size_t n=0; n<codes.size(); n++)
        {       
            printf(" ");
            PrintCode(codes[n]);
            nBitsOut+= m_histogram[codes[n]].nBits;
        }
        printf("\r\n\r\n");
        printf(" Bits in: %.0fx%d=%.0f Bits out: %d = %3.3f bits/code entropy\r\n", fSymbolSizeIn, (int)codes.size(), fSymbolSizeIn*(float)codes.size(), nBitsOut, (float)nBitsOut/(float)codes.size());
 
        // Theoretical entropy
        int nMaxCount = 0;
        for(size_t n=0; n<m_nSymbols; n++)
            nMaxCount+=m_histogram[n].nCount;
 
        float fBits = 0;
        for(size_t n=0; n<m_nSymbols; n++)
        {       
            if(m_histogram[n].nCount==0)
                continue;
 
            fBits += (float)m_histogram[n].nCount*(log((float)m_histogram[n].nCount/(float)nMaxCount)/-log(2.0f));
        }
        printf(" Theoretical lower bound for entropy is: %3.3f bits = %3.3f bits/code\r\n", fBits, fBits/(float)codes.size());
    }
protected:
    void BuildBinaryCodes()
    {
        // Build bit histogram
        BitHistogram nBitCounts[32];
        for(size_t n=0; n<32; n++)
            nBitCounts[n].nCount = 0;
 
        // Build bit lengths and insert them into the bit histogram
        for(size_t n=0; n<m_nSymbols; n++)
        {
            if(m_histogram[n].nCount==0)
                continue;
            size_t nPos = n;
            while(true)
            {
                nPos = m_histogram[nPos].nLink;
                if(nPos==0)
                    break;
                m_histogram[n].nBits++;
            }
            nBitCounts[m_histogram[n].nBits].nCount++;
        }
 
        // Build codes bases
        int nCurrentBase = 0;
        for(size_t n=0; n<32; n++)
        {
            nBitCounts[n].nBase = nCurrentBase;
            nCurrentBase += nBitCounts[n].nCount;
            nCurrentBase<<=1;
        }
 
        // Assign binary codes to every symbol
        for(size_t n=0; n<m_nSymbols; n++)
        {
            if(m_histogram[n].nCount==0)
                continue;
            m_histogram[n].nCode = nBitCounts[m_histogram[n].nBits].nBase;
            nBitCounts[m_histogram[n].nBits].nBase++;
        }
    }
 
    size_t m_nSymbols;
    std::vector<Histogram> m_histogram;
};
 
// Application entry point (from libc)
int main(int argc, const char* argv[])
{
    printf("Huffman test...\r\n\r\n");
 
    if(argc<2)
    {
        printf("Not enough parameters.\r\n");
        return -1;
    }
    std::vector<int> codes;
    for(size_t n=0; n<strlen(argv[1]); n++)
        codes.push_back(argv[1][n]);
 
    CHuffman huffman(256);
    huffman.BuildTree(codes);
    huffman.PrintHistogram();
    huffman.Encode(codes);
    return 0;
}
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203