migrate from govendor to dep
This commit is contained in:
+43
-62
@@ -9,14 +9,17 @@ import (
|
||||
"sort"
|
||||
)
|
||||
|
||||
type hcode uint32
|
||||
// hcode is a huffman code with a bit code and bit length.
|
||||
type hcode struct {
|
||||
code, len uint16
|
||||
}
|
||||
|
||||
type huffmanEncoder struct {
|
||||
codes []hcode
|
||||
freqcache []literalNode
|
||||
bitCount [17]int32
|
||||
lns literalNodeSorter
|
||||
lfs literalFreqSorter
|
||||
lns byLiteral // stored to avoid repeated allocation in generate
|
||||
lfs byFreq // stored to avoid repeated allocation in generate
|
||||
}
|
||||
|
||||
type literalNode struct {
|
||||
@@ -44,34 +47,16 @@ type levelInfo struct {
|
||||
needed int32
|
||||
}
|
||||
|
||||
func (h hcode) codeBits() (code uint16, bits uint8) {
|
||||
return uint16(h), uint8(h >> 16)
|
||||
}
|
||||
|
||||
func (h *hcode) set(code uint16, bits uint8) {
|
||||
*h = hcode(code) | hcode(uint32(bits)<<16)
|
||||
}
|
||||
|
||||
func (h *hcode) setBits(bits uint8) {
|
||||
*h = hcode(*h&0xffff) | hcode(uint32(bits)<<16)
|
||||
}
|
||||
|
||||
func toCode(code uint16, bits uint8) hcode {
|
||||
return hcode(code) | hcode(uint32(bits)<<16)
|
||||
}
|
||||
|
||||
func (h hcode) code() (code uint16) {
|
||||
return uint16(h)
|
||||
}
|
||||
|
||||
func (h hcode) bits() (bits uint) {
|
||||
return uint(h >> 16)
|
||||
// set sets the code and length of an hcode.
|
||||
func (h *hcode) set(code uint16, length uint16) {
|
||||
h.len = length
|
||||
h.code = code
|
||||
}
|
||||
|
||||
func maxNode() literalNode { return literalNode{math.MaxUint16, math.MaxInt32} }
|
||||
|
||||
func newHuffmanEncoder(size int) *huffmanEncoder {
|
||||
return &huffmanEncoder{codes: make([]hcode, size), freqcache: nil}
|
||||
return &huffmanEncoder{codes: make([]hcode, size)}
|
||||
}
|
||||
|
||||
// Generates a HuffmanCode corresponding to the fixed literal table
|
||||
@@ -81,7 +66,7 @@ func generateFixedLiteralEncoding() *huffmanEncoder {
|
||||
var ch uint16
|
||||
for ch = 0; ch < maxNumLit; ch++ {
|
||||
var bits uint16
|
||||
var size uint8
|
||||
var size uint16
|
||||
switch {
|
||||
case ch < 144:
|
||||
// size 8, 000110000 .. 10111111
|
||||
@@ -103,7 +88,7 @@ func generateFixedLiteralEncoding() *huffmanEncoder {
|
||||
bits = ch + 192 - 280
|
||||
size = 8
|
||||
}
|
||||
codes[ch] = toCode(reverseBits(bits, size), size)
|
||||
codes[ch] = hcode{code: reverseBits(bits, byte(size)), len: size}
|
||||
}
|
||||
return h
|
||||
}
|
||||
@@ -111,8 +96,8 @@ func generateFixedLiteralEncoding() *huffmanEncoder {
|
||||
func generateFixedOffsetEncoding() *huffmanEncoder {
|
||||
h := newHuffmanEncoder(30)
|
||||
codes := h.codes
|
||||
for ch := uint16(0); ch < 30; ch++ {
|
||||
codes[ch] = toCode(reverseBits(ch, 5), 5)
|
||||
for ch := range codes {
|
||||
codes[ch] = hcode{code: reverseBits(uint16(ch), 5), len: 5}
|
||||
}
|
||||
return h
|
||||
}
|
||||
@@ -120,11 +105,11 @@ func generateFixedOffsetEncoding() *huffmanEncoder {
|
||||
var fixedLiteralEncoding *huffmanEncoder = generateFixedLiteralEncoding()
|
||||
var fixedOffsetEncoding *huffmanEncoder = generateFixedOffsetEncoding()
|
||||
|
||||
func (h *huffmanEncoder) bitLength(freq []int32) int64 {
|
||||
var total int64
|
||||
func (h *huffmanEncoder) bitLength(freq []int32) int {
|
||||
var total int
|
||||
for i, f := range freq {
|
||||
if f != 0 {
|
||||
total += int64(f) * int64(h.codes[i].bits())
|
||||
total += int(f) * int(h.codes[i].len)
|
||||
}
|
||||
}
|
||||
return total
|
||||
@@ -138,7 +123,7 @@ const maxBitsLimit = 16
|
||||
// The cases of 0, 1, and 2 literals are handled by special case code.
|
||||
//
|
||||
// list An array of the literals with non-zero frequencies
|
||||
// and their associated frequencies. The array is in order of increasing
|
||||
// and their associated frequencies. The array is in order of increasing
|
||||
// frequency, and has as its last element a special element with frequency
|
||||
// MaxInt32
|
||||
// maxBits The maximum number of bits that should be used to encode any literal.
|
||||
@@ -153,7 +138,7 @@ func (h *huffmanEncoder) bitCounts(list []literalNode, maxBits int32) []int32 {
|
||||
list = list[0 : n+1]
|
||||
list[n] = maxNode()
|
||||
|
||||
// The tree can't have greater depth than n - 1, no matter what. This
|
||||
// The tree can't have greater depth than n - 1, no matter what. This
|
||||
// saves a little bit of work in some small cases
|
||||
if maxBits > n-1 {
|
||||
maxBits = n - 1
|
||||
@@ -222,7 +207,7 @@ func (h *huffmanEncoder) bitCounts(list []literalNode, maxBits int32) []int32 {
|
||||
|
||||
if l.needed--; l.needed == 0 {
|
||||
// We've done everything we need to do for this level.
|
||||
// Continue calculating one level up. Fill in nextPairFreq
|
||||
// Continue calculating one level up. Fill in nextPairFreq
|
||||
// of that level with the sum of the two nodes we've just calculated on
|
||||
// this level.
|
||||
if l.level == maxBits {
|
||||
@@ -246,7 +231,6 @@ func (h *huffmanEncoder) bitCounts(list []literalNode, maxBits int32) []int32 {
|
||||
}
|
||||
|
||||
bitCount := h.bitCount[:maxBits+1]
|
||||
//make([]int32, maxBits+1)
|
||||
bits := 1
|
||||
counts := &leafCounts[maxBits]
|
||||
for level := maxBits; level > 0; level-- {
|
||||
@@ -273,9 +257,9 @@ func (h *huffmanEncoder) assignEncodingAndSize(bitCount []int32, list []literalN
|
||||
// assigned in literal order (not frequency order).
|
||||
chunk := list[len(list)-int(bits):]
|
||||
|
||||
h.lns.Sort(chunk)
|
||||
h.lns.sort(chunk)
|
||||
for _, node := range chunk {
|
||||
h.codes[node.literal] = toCode(reverseBits(code, uint8(n)), uint8(n))
|
||||
h.codes[node.literal] = hcode{code: reverseBits(code, uint8(n)), len: uint16(n)}
|
||||
code++
|
||||
}
|
||||
list = list[0 : len(list)-int(bits)]
|
||||
@@ -288,7 +272,10 @@ func (h *huffmanEncoder) assignEncodingAndSize(bitCount []int32, list []literalN
|
||||
// maxBits The maximum number of bits to use for any literal.
|
||||
func (h *huffmanEncoder) generate(freq []int32, maxBits int32) {
|
||||
if h.freqcache == nil {
|
||||
h.freqcache = make([]literalNode, 300)
|
||||
// Allocate a reusable buffer with the longest possible frequency table.
|
||||
// Possible lengths are codegenCodeCount, offsetCodeCount and maxNumLit.
|
||||
// The largest of these is maxNumLit, so we allocate for that case.
|
||||
h.freqcache = make([]literalNode, maxNumLit+1)
|
||||
}
|
||||
list := h.freqcache[:len(freq)+1]
|
||||
// Number of non-zero literals
|
||||
@@ -300,28 +287,22 @@ func (h *huffmanEncoder) generate(freq []int32, maxBits int32) {
|
||||
count++
|
||||
} else {
|
||||
list[count] = literalNode{}
|
||||
//h.codeBits[i] = 0
|
||||
h.codes[i].setBits(0)
|
||||
h.codes[i].len = 0
|
||||
}
|
||||
}
|
||||
list[len(freq)] = literalNode{}
|
||||
// If freq[] is shorter than codeBits[], fill rest of codeBits[] with zeros
|
||||
// FIXME: Doesn't do what it says on the tin (klauspost)
|
||||
//h.codeBits = h.codeBits[0:len(freq)]
|
||||
|
||||
list = list[0:count]
|
||||
list = list[:count]
|
||||
if count <= 2 {
|
||||
// Handle the small cases here, because they are awkward for the general case code. With
|
||||
// Handle the small cases here, because they are awkward for the general case code. With
|
||||
// two or fewer literals, everything has bit length 1.
|
||||
for i, node := range list {
|
||||
// "list" is in order of increasing literal value.
|
||||
h.codes[node.literal].set(uint16(i), 1)
|
||||
//h.codeBits[node.literal] = 1
|
||||
//h.code[node.literal] = uint16(i)
|
||||
}
|
||||
return
|
||||
}
|
||||
h.lfs.Sort(list)
|
||||
h.lfs.sort(list)
|
||||
|
||||
// Get the number of literals for each bit count
|
||||
bitCount := h.bitCounts(list, maxBits)
|
||||
@@ -329,35 +310,35 @@ func (h *huffmanEncoder) generate(freq []int32, maxBits int32) {
|
||||
h.assignEncodingAndSize(bitCount, list)
|
||||
}
|
||||
|
||||
type literalNodeSorter []literalNode
|
||||
type byLiteral []literalNode
|
||||
|
||||
func (s *literalNodeSorter) Sort(a []literalNode) {
|
||||
*s = literalNodeSorter(a)
|
||||
func (s *byLiteral) sort(a []literalNode) {
|
||||
*s = byLiteral(a)
|
||||
sort.Sort(s)
|
||||
}
|
||||
|
||||
func (s literalNodeSorter) Len() int { return len(s) }
|
||||
func (s byLiteral) Len() int { return len(s) }
|
||||
|
||||
func (s literalNodeSorter) Less(i, j int) bool {
|
||||
func (s byLiteral) Less(i, j int) bool {
|
||||
return s[i].literal < s[j].literal
|
||||
}
|
||||
|
||||
func (s literalNodeSorter) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||
func (s byLiteral) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||
|
||||
type literalFreqSorter []literalNode
|
||||
type byFreq []literalNode
|
||||
|
||||
func (s *literalFreqSorter) Sort(a []literalNode) {
|
||||
*s = literalFreqSorter(a)
|
||||
func (s *byFreq) sort(a []literalNode) {
|
||||
*s = byFreq(a)
|
||||
sort.Sort(s)
|
||||
}
|
||||
|
||||
func (s literalFreqSorter) Len() int { return len(s) }
|
||||
func (s byFreq) Len() int { return len(s) }
|
||||
|
||||
func (s literalFreqSorter) Less(i, j int) bool {
|
||||
func (s byFreq) Less(i, j int) bool {
|
||||
if s[i].freq == s[j].freq {
|
||||
return s[i].literal < s[j].literal
|
||||
}
|
||||
return s[i].freq < s[j].freq
|
||||
}
|
||||
|
||||
func (s literalFreqSorter) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||
func (s byFreq) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||
|
||||
Reference in New Issue
Block a user