mirror of
https://github.com/golang/go.git
synced 2024-09-30 14:57:10 +00:00
parent
b6d0a22dc5
commit
5fca0bca61
@ -31,10 +31,10 @@ const (
|
|||||||
|
|
||||||
// The number of extra bits needed by length code X - LENGTH_CODES_START.
|
// The number of extra bits needed by length code X - LENGTH_CODES_START.
|
||||||
var lengthExtraBits = []int8{
|
var lengthExtraBits = []int8{
|
||||||
/* 257 */0, 0, 0,
|
/* 257 */ 0, 0, 0,
|
||||||
/* 260 */0, 0, 0, 0, 0, 1, 1, 1, 1, 2,
|
/* 260 */ 0, 0, 0, 0, 0, 1, 1, 1, 1, 2,
|
||||||
/* 270 */2, 2, 2, 3, 3, 3, 3, 4, 4, 4,
|
/* 270 */ 2, 2, 2, 3, 3, 3, 3, 4, 4, 4,
|
||||||
/* 280 */4, 5, 5, 5, 5, 0,
|
/* 280 */ 4, 5, 5, 5, 5, 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
// The length indicated by length code X - LENGTH_CODES_START.
|
// The length indicated by length code X - LENGTH_CODES_START.
|
||||||
|
@ -10,57 +10,57 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type huffmanEncoder struct {
|
type huffmanEncoder struct {
|
||||||
codeBits []uint8;
|
codeBits []uint8;
|
||||||
code []uint16;
|
code []uint16;
|
||||||
}
|
}
|
||||||
|
|
||||||
type literalNode struct {
|
type literalNode struct {
|
||||||
literal uint16;
|
literal uint16;
|
||||||
freq int32;
|
freq int32;
|
||||||
}
|
}
|
||||||
|
|
||||||
type chain struct {
|
type chain struct {
|
||||||
// The sum of the leaves in this tree
|
// The sum of the leaves in this tree
|
||||||
freq int32;
|
freq int32;
|
||||||
|
|
||||||
// The number of literals to the left of this item at this level
|
// The number of literals to the left of this item at this level
|
||||||
leafCount int32;
|
leafCount int32;
|
||||||
|
|
||||||
// The right child of this chain in the previous level.
|
// The right child of this chain in the previous level.
|
||||||
up *chain;
|
up *chain;
|
||||||
}
|
}
|
||||||
|
|
||||||
type levelInfo struct {
|
type levelInfo struct {
|
||||||
// Our level. for better printing
|
// Our level. for better printing
|
||||||
level int32;
|
level int32;
|
||||||
|
|
||||||
// The most recent chain generated for this level
|
// The most recent chain generated for this level
|
||||||
lastChain *chain;
|
lastChain *chain;
|
||||||
|
|
||||||
// The frequency of the next character to add to this level
|
// The frequency of the next character to add to this level
|
||||||
nextCharFreq int32;
|
nextCharFreq int32;
|
||||||
|
|
||||||
// The frequency of the next pair (from level below) to add to this level.
|
// The frequency of the next pair (from level below) to add to this level.
|
||||||
// Only valid if the "needed" value of the next lower level is 0.
|
// Only valid if the "needed" value of the next lower level is 0.
|
||||||
nextPairFreq int32;
|
nextPairFreq int32;
|
||||||
|
|
||||||
// The number of chains remaining to generate for this level before moving
|
// The number of chains remaining to generate for this level before moving
|
||||||
// up to the next level
|
// up to the next level
|
||||||
needed int32;
|
needed int32;
|
||||||
|
|
||||||
// The levelInfo for level+1
|
// The levelInfo for level+1
|
||||||
up *levelInfo;
|
up *levelInfo;
|
||||||
|
|
||||||
// The levelInfo for level-1
|
// The levelInfo for level-1
|
||||||
down *levelInfo;
|
down *levelInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
func maxNode() literalNode {
|
func maxNode() literalNode {
|
||||||
return literalNode{ math.MaxUint16, math.MaxInt32 };
|
return literalNode{math.MaxUint16, math.MaxInt32};
|
||||||
}
|
}
|
||||||
|
|
||||||
func newHuffmanEncoder(size int) *huffmanEncoder {
|
func newHuffmanEncoder(size int) *huffmanEncoder {
|
||||||
return &huffmanEncoder { make([]uint8, size), make([]uint16, size) };
|
return &huffmanEncoder{make([]uint8, size), make([]uint16, size)};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generates a HuffmanCode corresponding to the fixed literal table
|
// Generates a HuffmanCode corresponding to the fixed literal table
|
||||||
@ -73,18 +73,25 @@ func generateFixedLiteralEncoding() *huffmanEncoder {
|
|||||||
var bits uint16;
|
var bits uint16;
|
||||||
var size uint8;
|
var size uint8;
|
||||||
switch {
|
switch {
|
||||||
case ch < 144:
|
case ch < 144:
|
||||||
// size 8, 000110000 .. 10111111
|
// size 8, 000110000 .. 10111111
|
||||||
bits = ch + 48; size = 8; break;
|
bits = ch+48;
|
||||||
case ch < 256:
|
size = 8;
|
||||||
// size 9, 110010000 .. 111111111
|
break;
|
||||||
bits = ch + 400 - 144; size = 9; break;
|
case ch < 256:
|
||||||
case ch < 280:
|
// size 9, 110010000 .. 111111111
|
||||||
// size 7, 0000000 .. 0010111
|
bits = ch+400-144;
|
||||||
bits = ch - 256; size = 7; break;
|
size = 9;
|
||||||
default:
|
break;
|
||||||
// size 8, 11000000 .. 11000111
|
case ch < 280:
|
||||||
bits = ch + 192 - 280; size = 8;
|
// size 7, 0000000 .. 0010111
|
||||||
|
bits = ch-256;
|
||||||
|
size = 7;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
// size 8, 11000000 .. 11000111
|
||||||
|
bits = ch+192-280;
|
||||||
|
size = 8;
|
||||||
}
|
}
|
||||||
codeBits[ch] = size;
|
codeBits[ch] = size;
|
||||||
code[ch] = reverseBits(bits, size);
|
code[ch] = reverseBits(bits, size);
|
||||||
@ -103,14 +110,14 @@ func generateFixedOffsetEncoding() *huffmanEncoder {
|
|||||||
return h;
|
return h;
|
||||||
}
|
}
|
||||||
|
|
||||||
var fixedLiteralEncoding *huffmanEncoder = generateFixedLiteralEncoding();
|
var fixedLiteralEncoding *huffmanEncoder = generateFixedLiteralEncoding()
|
||||||
var fixedOffsetEncoding *huffmanEncoder = generateFixedOffsetEncoding();
|
var fixedOffsetEncoding *huffmanEncoder = generateFixedOffsetEncoding()
|
||||||
|
|
||||||
func (h *huffmanEncoder) bitLength(freq []int32) int64 {
|
func (h *huffmanEncoder) bitLength(freq []int32) int64 {
|
||||||
var total int64;
|
var total int64;
|
||||||
for i, f := range freq {
|
for i, f := range freq {
|
||||||
if f != 0 {
|
if f != 0 {
|
||||||
total += int64(f) * int64(h.codeBits[i]);
|
total += int64(f)*int64(h.codeBits[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return total;
|
return total;
|
||||||
@ -119,7 +126,7 @@ func (h *huffmanEncoder) bitLength(freq []int32) int64 {
|
|||||||
// Generate elements in the chain using an iterative algorithm.
|
// Generate elements in the chain using an iterative algorithm.
|
||||||
func (h *huffmanEncoder) generateChains(top *levelInfo, list []literalNode) {
|
func (h *huffmanEncoder) generateChains(top *levelInfo, list []literalNode) {
|
||||||
n := len(list);
|
n := len(list);
|
||||||
list = list[0:n+1];
|
list = list[0 : n+1];
|
||||||
list[n] = maxNode();
|
list[n] = maxNode();
|
||||||
|
|
||||||
l := top;
|
l := top;
|
||||||
@ -140,13 +147,13 @@ func (h *huffmanEncoder) generateChains(top *levelInfo, list []literalNode) {
|
|||||||
if l.nextCharFreq < l.nextPairFreq {
|
if l.nextCharFreq < l.nextPairFreq {
|
||||||
// The next item on this row is a leaf node.
|
// The next item on this row is a leaf node.
|
||||||
n := l.lastChain.leafCount + 1;
|
n := l.lastChain.leafCount + 1;
|
||||||
l.lastChain = &chain{ l.nextCharFreq, n, l.lastChain.up };
|
l.lastChain = &chain{l.nextCharFreq, n, l.lastChain.up};
|
||||||
l.nextCharFreq = list[n].freq;
|
l.nextCharFreq = list[n].freq;
|
||||||
} else {
|
} else {
|
||||||
// The next item on this row is a pair from the previous row.
|
// The next item on this row is a pair from the previous row.
|
||||||
// nextPairFreq isn't valid until we generate two
|
// nextPairFreq isn't valid until we generate two
|
||||||
// more values in the level below
|
// more values in the level below
|
||||||
l.lastChain = &chain{ l.nextPairFreq, l.lastChain.leafCount, l.down.lastChain };
|
l.lastChain = &chain{l.nextPairFreq, l.lastChain.leafCount, l.down.lastChain};
|
||||||
l.down.needed = 2;
|
l.down.needed = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -185,19 +192,19 @@ func (h *huffmanEncoder) generateChains(top *levelInfo, list []literalNode) {
|
|||||||
// that should be encoded in i bits.
|
// that should be encoded in i bits.
|
||||||
func (h *huffmanEncoder) bitCounts(list []literalNode, maxBits int32) []int32 {
|
func (h *huffmanEncoder) bitCounts(list []literalNode, maxBits int32) []int32 {
|
||||||
n := int32(len(list));
|
n := int32(len(list));
|
||||||
list = list[0:n+1];
|
list = list[0 : n+1];
|
||||||
list[n] = maxNode();
|
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
|
// saves a little bit of work in some small cases
|
||||||
maxBits = minInt32(maxBits, n - 1);
|
maxBits = minInt32(maxBits, n-1);
|
||||||
|
|
||||||
// Create information about each of the levels.
|
// Create information about each of the levels.
|
||||||
// A bogus "Level 0" whose sole purpose is so that
|
// A bogus "Level 0" whose sole purpose is so that
|
||||||
// level1.prev.needed==0. This makes level1.nextPairFreq
|
// level1.prev.needed==0. This makes level1.nextPairFreq
|
||||||
// be a legitimate value that never gets chosen.
|
// be a legitimate value that never gets chosen.
|
||||||
top := &levelInfo{needed: 0};
|
top := &levelInfo{needed: 0};
|
||||||
chain2 := &chain{ list[1].freq, 2, new(chain) };
|
chain2 := &chain{list[1].freq, 2, new(chain)};
|
||||||
for level := int32(1); level <= maxBits; level++ {
|
for level := int32(1); level <= maxBits; level++ {
|
||||||
// For every level, the first two items are the first two characters.
|
// For every level, the first two items are the first two characters.
|
||||||
// We initialize the levels as if we had already figured this out.
|
// We initialize the levels as if we had already figured this out.
|
||||||
@ -235,13 +242,13 @@ func (h *huffmanEncoder) bitCounts(list []literalNode, maxBits int32) []int32 {
|
|||||||
if l.nextCharFreq < l.nextPairFreq {
|
if l.nextCharFreq < l.nextPairFreq {
|
||||||
// The next item on this row is a leaf node.
|
// The next item on this row is a leaf node.
|
||||||
n := l.lastChain.leafCount + 1;
|
n := l.lastChain.leafCount + 1;
|
||||||
l.lastChain = &chain{ l.nextCharFreq, n, l.lastChain.up };
|
l.lastChain = &chain{l.nextCharFreq, n, l.lastChain.up};
|
||||||
l.nextCharFreq = list[n].freq;
|
l.nextCharFreq = list[n].freq;
|
||||||
} else {
|
} else {
|
||||||
// The next item on this row is a pair from the previous row.
|
// The next item on this row is a pair from the previous row.
|
||||||
// nextPairFreq isn't valid until we generate two
|
// nextPairFreq isn't valid until we generate two
|
||||||
// more values in the level below
|
// more values in the level below
|
||||||
l.lastChain = &chain{ l.nextPairFreq, l.lastChain.leafCount, l.down.lastChain };
|
l.lastChain = &chain{l.nextPairFreq, l.lastChain.leafCount, l.down.lastChain};
|
||||||
l.down.needed = 2;
|
l.down.needed = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -272,7 +279,7 @@ func (h *huffmanEncoder) bitCounts(list []literalNode, maxBits int32) []int32 {
|
|||||||
panic("top.lastChain.leafCount != n");
|
panic("top.lastChain.leafCount != n");
|
||||||
}
|
}
|
||||||
|
|
||||||
bitCount := make([]int32, maxBits + 1);
|
bitCount := make([]int32, maxBits+1);
|
||||||
bits := 1;
|
bits := 1;
|
||||||
for chain := top.lastChain; chain.up != nil; chain = chain.up {
|
for chain := top.lastChain; chain.up != nil; chain = chain.up {
|
||||||
// chain.leafCount gives the number of literals requiring at least "bits"
|
// chain.leafCount gives the number of literals requiring at least "bits"
|
||||||
@ -296,14 +303,14 @@ func (h *huffmanEncoder) assignEncodingAndSize(bitCount []int32, list []literalN
|
|||||||
// are encoded using "bits" bits, and get the values
|
// are encoded using "bits" bits, and get the values
|
||||||
// code, code + 1, .... The code values are
|
// code, code + 1, .... The code values are
|
||||||
// assigned in literal order (not frequency order).
|
// assigned in literal order (not frequency order).
|
||||||
chunk := list[len(list)-int(bits):len(list)];
|
chunk := list[len(list)-int(bits) : len(list)];
|
||||||
sortByLiteral(chunk);
|
sortByLiteral(chunk);
|
||||||
for _, node := range chunk {
|
for _, node := range chunk {
|
||||||
h.codeBits[node.literal] = uint8(n);
|
h.codeBits[node.literal] = uint8(n);
|
||||||
h.code[node.literal] = reverseBits(code, uint8(n));
|
h.code[node.literal] = reverseBits(code, uint8(n));
|
||||||
code++;
|
code++;
|
||||||
}
|
}
|
||||||
list = list[0:len(list)-int(bits)];
|
list = list[0 : len(list)-int(bits)];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -312,7 +319,7 @@ func (h *huffmanEncoder) assignEncodingAndSize(bitCount []int32, list []literalN
|
|||||||
// freq An array of frequencies, in which frequency[i] gives the frequency of literal i.
|
// freq An array of frequencies, in which frequency[i] gives the frequency of literal i.
|
||||||
// maxBits The maximum number of bits to use for any literal.
|
// maxBits The maximum number of bits to use for any literal.
|
||||||
func (h *huffmanEncoder) generate(freq []int32, maxBits int32) {
|
func (h *huffmanEncoder) generate(freq []int32, maxBits int32) {
|
||||||
list := make([]literalNode, len(freq) + 1);
|
list := make([]literalNode, len(freq)+1);
|
||||||
// Number of non-zero literals
|
// Number of non-zero literals
|
||||||
count := 0;
|
count := 0;
|
||||||
// Set list to be the set of all non-zero literals and their frequencies
|
// Set list to be the set of all non-zero literals and their frequencies
|
||||||
@ -335,7 +342,7 @@ func (h *huffmanEncoder) generate(freq []int32, maxBits int32) {
|
|||||||
h.codeBits[node.literal] = 1;
|
h.codeBits[node.literal] = 1;
|
||||||
h.code[node.literal] = uint16(i);
|
h.code[node.literal] = uint16(i);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
sortByFreq(list);
|
sortByFreq(list);
|
||||||
|
|
||||||
@ -346,8 +353,8 @@ func (h *huffmanEncoder) generate(freq []int32, maxBits int32) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type literalNodeSorter struct {
|
type literalNodeSorter struct {
|
||||||
a []literalNode;
|
a []literalNode;
|
||||||
less func(i,j int) bool;
|
less func(i, j int) bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s literalNodeSorter) Len() int {
|
func (s literalNodeSorter) Len() int {
|
||||||
@ -358,16 +365,16 @@ func (s literalNodeSorter) Less(i, j int) bool {
|
|||||||
return s.less(i, j);
|
return s.less(i, j);
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s literalNodeSorter) Swap(i,j int) {
|
func (s literalNodeSorter) Swap(i, j int) {
|
||||||
s.a[i], s.a[j] = s.a[j], s.a[i];
|
s.a[i], s.a[j] = s.a[j], s.a[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
func sortByFreq(a []literalNode) {
|
func sortByFreq(a []literalNode) {
|
||||||
s := &literalNodeSorter { a, func(i, j int) bool { return a[i].freq < a[j].freq; }};
|
s := &literalNodeSorter{a, func(i, j int) bool { return a[i].freq < a[j].freq }};
|
||||||
sort.Sort(s);
|
sort.Sort(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
func sortByLiteral(a []literalNode) {
|
func sortByLiteral(a []literalNode) {
|
||||||
s := &literalNodeSorter{ a, func(i, j int) bool { return a[i].literal < a[j].literal; }};
|
s := &literalNodeSorter{a, func(i, j int) bool { return a[i].literal < a[j].literal }};
|
||||||
sort.Sort(s);
|
sort.Sort(s);
|
||||||
}
|
}
|
||||||
|
@ -609,8 +609,6 @@ func (f *inflater) inflater(r io.Reader, w io.Writer) os.Error {
|
|||||||
func NewInflater(r io.Reader) io.ReadCloser {
|
func NewInflater(r io.Reader) io.ReadCloser {
|
||||||
var f inflater;
|
var f inflater;
|
||||||
pr, pw := io.Pipe();
|
pr, pw := io.Pipe();
|
||||||
go func() {
|
go func() { pw.CloseWithError(f.inflater(r, pw)) }();
|
||||||
pw.CloseWithError(f.inflater(r, pw));
|
|
||||||
}();
|
|
||||||
return pr;
|
return pr;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user