1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 type huffmanEncoder
struct {
17 type literalNode
struct {
23 // The sum of the leaves in this tree
26 // The number of literals to the left of this item at this level
29 // The right child of this chain in the previous level.
33 type levelInfo
struct {
34 // Our level. for better printing
37 // The most recent chain generated for this level
40 // The frequency of the next character to add to this level
43 // The frequency of the next pair (from level below) to add to this level.
44 // Only valid if the "needed" value of the next lower level is 0.
47 // The number of chains remaining to generate for this level before moving
48 // up to the next level
51 // The levelInfo for level+1
54 // The levelInfo for level-1
58 func maxNode() literalNode
{ return literalNode
{math
.MaxUint16
, math
.MaxInt32
} }
60 func newHuffmanEncoder(size
int) *huffmanEncoder
{
61 return &huffmanEncoder
{make([]uint8, size
), make([]uint16, size
)}
64 // Generates a HuffmanCode corresponding to the fixed literal table
65 func generateFixedLiteralEncoding() *huffmanEncoder
{
66 h
:= newHuffmanEncoder(maxLit
)
67 codeBits
:= h
.codeBits
70 for ch
= 0; ch
< maxLit
; ch
++ {
75 // size 8, 000110000 .. 10111111
80 // size 9, 110010000 .. 111111111
85 // size 7, 0000000 .. 0010111
90 // size 8, 11000000 .. 11000111
95 code
[ch
] = reverseBits(bits
, size
)
100 func generateFixedOffsetEncoding() *huffmanEncoder
{
101 h
:= newHuffmanEncoder(30)
102 codeBits
:= h
.codeBits
104 for ch
:= uint16(0); ch
< 30; ch
++ {
106 code
[ch
] = reverseBits(ch
, 5)
111 var fixedLiteralEncoding
*huffmanEncoder
= generateFixedLiteralEncoding()
112 var fixedOffsetEncoding
*huffmanEncoder
= generateFixedOffsetEncoding()
114 func (h
*huffmanEncoder
) bitLength(freq
[]int32) int64 {
116 for i
, f
:= range freq
{
118 total
+= int64(f
) * int64(h
.codeBits
[i
])
124 // Generate elements in the chain using an iterative algorithm.
125 func (h
*huffmanEncoder
) generateChains(top
*levelInfo
, list
[]literalNode
) {
132 if l
.nextPairFreq
== math
.MaxInt32
&& l
.nextCharFreq
== math
.MaxInt32
{
133 // We've run out of both leafs and pairs.
134 // End all calculations for this level.
135 // To m sure we never come back to this level or any lower level,
136 // set nextPairFreq impossibly large.
140 l
.nextPairFreq
= math
.MaxInt32
144 prevFreq
:= l
.lastChain
.freq
145 if l
.nextCharFreq
< l
.nextPairFreq
{
146 // The next item on this row is a leaf node.
147 n
:= l
.lastChain
.leafCount
+ 1
148 l
.lastChain
= &chain
{l
.nextCharFreq
, n
, l
.lastChain
.up
}
149 l
.nextCharFreq
= list
[n
].freq
151 // The next item on this row is a pair from the previous row.
152 // nextPairFreq isn't valid until we generate two
153 // more values in the level below
154 l
.lastChain
= &chain
{l
.nextPairFreq
, l
.lastChain
.leafCount
, l
.down
.lastChain
}
158 if l
.needed
--; l
.needed
== 0 {
159 // We've done everything we need to do for this level.
160 // Continue calculating one level up. Fill in nextPairFreq
161 // of that level with the sum of the two nodes we've just calculated on
168 up
.nextPairFreq
= prevFreq
+ l
.lastChain
.freq
171 // If we stole from below, move down temporarily to replenish it.
172 for l
.down
.needed
> 0 {
179 // Return the number of literals assigned to each bit size in the Huffman encoding
181 // This method is only called when list.length >= 3
182 // The cases of 0, 1, and 2 literals are handled by special case code.
184 // list An array of the literals with non-zero frequencies
185 // and their associated frequencies. The array is in order of increasing
186 // frequency, and has as its last element a special element with frequency
188 // maxBits The maximum number of bits that should be used to encode any literal.
189 // return An integer array in which array[i] indicates the number of literals
190 // that should be encoded in i bits.
191 func (h
*huffmanEncoder
) bitCounts(list
[]literalNode
, maxBits
int32) []int32 {
192 n
:= int32(len(list
))
196 // The tree can't have greater depth than n - 1, no matter what. This
197 // saves a little bit of work in some small cases
198 maxBits
= minInt32(maxBits
, n
-1)
200 // Create information about each of the levels.
201 // A bogus "Level 0" whose sole purpose is so that
202 // level1.prev.needed==0. This makes level1.nextPairFreq
203 // be a legitimate value that never gets chosen.
204 top
:= &levelInfo
{needed
: 0}
205 chain2
:= &chain
{list
[1].freq
, 2, new(chain
)}
206 for level
:= int32(1); level
<= maxBits
; level
++ {
207 // For every level, the first two items are the first two characters.
208 // We initialize the levels as if we had already figured this out.
212 nextCharFreq
: list
[2].freq
,
213 nextPairFreq
: list
[0].freq
+ list
[1].freq
,
218 top
.nextPairFreq
= math
.MaxInt32
222 // We need a total of 2*n - 2 items at top level and have already generated 2.
227 if l
.nextPairFreq
== math
.MaxInt32
&& l
.nextCharFreq
== math
.MaxInt32
{
228 // We've run out of both leafs and pairs.
229 // End all calculations for this level.
230 // To m sure we never come back to this level or any lower level,
231 // set nextPairFreq impossibly large.
235 l
.nextPairFreq
= math
.MaxInt32
239 prevFreq
:= l
.lastChain
.freq
240 if l
.nextCharFreq
< l
.nextPairFreq
{
241 // The next item on this row is a leaf node.
242 n
:= l
.lastChain
.leafCount
+ 1
243 l
.lastChain
= &chain
{l
.nextCharFreq
, n
, l
.lastChain
.up
}
244 l
.nextCharFreq
= list
[n
].freq
246 // The next item on this row is a pair from the previous row.
247 // nextPairFreq isn't valid until we generate two
248 // more values in the level below
249 l
.lastChain
= &chain
{l
.nextPairFreq
, l
.lastChain
.leafCount
, l
.down
.lastChain
}
253 if l
.needed
--; l
.needed
== 0 {
254 // We've done everything we need to do for this level.
255 // Continue calculating one level up. Fill in nextPairFreq
256 // of that level with the sum of the two nodes we've just calculated on
263 up
.nextPairFreq
= prevFreq
+ l
.lastChain
.freq
266 // If we stole from below, move down temporarily to replenish it.
267 for l
.down
.needed
> 0 {
273 // Somethings is wrong if at the end, the top level is null or hasn't used
274 // all of the leaves.
275 if top
.lastChain
.leafCount
!= n
{
276 panic("top.lastChain.leafCount != n")
279 bitCount
:= make([]int32, maxBits
+1)
281 for chain
:= top
.lastChain
; chain
.up
!= nil; chain
= chain
.up
{
282 // chain.leafCount gives the number of literals requiring at least "bits"
284 bitCount
[bits
] = chain
.leafCount
- chain
.up
.leafCount
290 // Look at the leaves and assign them a bit count and an encoding as specified
292 func (h
*huffmanEncoder
) assignEncodingAndSize(bitCount
[]int32, list
[]literalNode
) {
294 for n
, bits
:= range bitCount
{
296 if n
== 0 || bits
== 0 {
299 // The literals list[len(list)-bits] .. list[len(list)-bits]
300 // are encoded using "bits" bits, and get the values
301 // code, code + 1, .... The code values are
302 // assigned in literal order (not frequency order).
303 chunk
:= list
[len(list
)-int(bits
):]
305 for _
, node
:= range chunk
{
306 h
.codeBits
[node
.literal
] = uint8(n
)
307 h
.code
[node
.literal
] = reverseBits(code
, uint8(n
))
310 list
= list
[0 : len(list
)-int(bits
)]
314 // Update this Huffman Code object to be the minimum code for the specified frequency count.
316 // freq An array of frequencies, in which frequency[i] gives the frequency of literal i.
317 // maxBits The maximum number of bits to use for any literal.
318 func (h
*huffmanEncoder
) generate(freq
[]int32, maxBits
int32) {
319 list
:= make([]literalNode
, len(freq
)+1)
320 // Number of non-zero literals
322 // Set list to be the set of all non-zero literals and their frequencies
323 for i
, f
:= range freq
{
325 list
[count
] = literalNode
{uint16(i
), f
}
331 // If freq[] is shorter than codeBits[], fill rest of codeBits[] with zeros
332 h
.codeBits
= h
.codeBits
[0:len(freq
)]
335 // Handle the small cases here, because they are awkward for the general case code. With
336 // two or fewer literals, everything has bit length 1.
337 for i
, node
:= range list
{
338 // "list" is in order of increasing literal value.
339 h
.codeBits
[node
.literal
] = 1
340 h
.code
[node
.literal
] = uint16(i
)
346 // Get the number of literals for each bit count
347 bitCount
:= h
.bitCounts(list
, maxBits
)
348 // And do the assignment
349 h
.assignEncodingAndSize(bitCount
, list
)
352 type literalNodeSorter
struct {
354 less
func(i
, j
int) bool
357 func (s literalNodeSorter
) Len() int { return len(s
.a
) }
359 func (s literalNodeSorter
) Less(i
, j
int) bool {
363 func (s literalNodeSorter
) Swap(i
, j
int) { s
.a
[i
], s
.a
[j
] = s
.a
[j
], s
.a
[i
] }
365 func sortByFreq(a
[]literalNode
) {
366 s
:= &literalNodeSorter
{a
, func(i
, j
int) bool { return a
[i
].freq
< a
[j
].freq
}}
370 func sortByLiteral(a
[]literalNode
) {
371 s
:= &literalNodeSorter
{a
, func(i
, j
int) bool { return a
[i
].literal
< a
[j
].literal
}}