Skip to content

Commit b26a4a1

Browse files
joekyodsnet
authored andcommitted
compress/bzip2: use sort.Slice in huffman.go
Change-Id: Ie4d23cdb81473a4c989a977a127479cf825084dc Reviewed-on: https://go-review.googlesource.com/77850 Reviewed-by: Joe Tsai <[email protected]> Run-TryBot: Joe Tsai <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 1102616 commit b26a4a1

File tree

1 file changed

+17
-43
lines changed

1 file changed

+17
-43
lines changed

src/compress/bzip2/huffman.go

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,24 @@ func newHuffmanTree(lengths []uint8) (huffmanTree, error) {
9090

9191
// First we sort the code length assignments by ascending code length,
9292
// using the symbol value to break ties.
93-
pairs := huffmanSymbolLengthPairs(make([]huffmanSymbolLengthPair, len(lengths)))
93+
pairs := make([]huffmanSymbolLengthPair, len(lengths))
9494
for i, length := range lengths {
9595
pairs[i].value = uint16(i)
9696
pairs[i].length = length
9797
}
9898

99-
sort.Sort(pairs)
99+
sort.Slice(pairs, func(i, j int) bool {
100+
if pairs[i].length < pairs[j].length {
101+
return true
102+
}
103+
if pairs[i].length > pairs[j].length {
104+
return false
105+
}
106+
if pairs[i].value < pairs[j].value {
107+
return true
108+
}
109+
return false
110+
})
100111

101112
// Now we assign codes to the symbols, starting with the longest code.
102113
// We keep the codes packed into a uint32, at the most-significant end.
@@ -105,7 +116,7 @@ func newHuffmanTree(lengths []uint8) (huffmanTree, error) {
105116
code := uint32(0)
106117
length := uint8(32)
107118

108-
codes := huffmanCodes(make([]huffmanCode, len(lengths)))
119+
codes := make([]huffmanCode, len(lengths))
109120
for i := len(pairs) - 1; i >= 0; i-- {
110121
if length > pairs[i].length {
111122
length = pairs[i].length
@@ -120,7 +131,9 @@ func newHuffmanTree(lengths []uint8) (huffmanTree, error) {
120131

121132
// Now we can sort by the code so that the left half of each branch are
122133
// grouped together, recursively.
123-
sort.Sort(codes)
134+
sort.Slice(codes, func(i, j int) bool {
135+
return codes[i].code < codes[j].code
136+
})
124137

125138
t.nodes = make([]huffmanNode, len(codes))
126139
_, err := buildHuffmanNode(&t, codes, 0)
@@ -133,52 +146,13 @@ type huffmanSymbolLengthPair struct {
133146
length uint8
134147
}
135148

136-
// huffmanSymbolLengthPair is used to provide an interface for sorting.
137-
type huffmanSymbolLengthPairs []huffmanSymbolLengthPair
138-
139-
func (h huffmanSymbolLengthPairs) Len() int {
140-
return len(h)
141-
}
142-
143-
func (h huffmanSymbolLengthPairs) Less(i, j int) bool {
144-
if h[i].length < h[j].length {
145-
return true
146-
}
147-
if h[i].length > h[j].length {
148-
return false
149-
}
150-
if h[i].value < h[j].value {
151-
return true
152-
}
153-
return false
154-
}
155-
156-
func (h huffmanSymbolLengthPairs) Swap(i, j int) {
157-
h[i], h[j] = h[j], h[i]
158-
}
159-
160149
// huffmanCode contains a symbol, its code and code length.
161150
type huffmanCode struct {
162151
code uint32
163152
codeLen uint8
164153
value uint16
165154
}
166155

167-
// huffmanCodes is used to provide an interface for sorting.
168-
type huffmanCodes []huffmanCode
169-
170-
func (n huffmanCodes) Len() int {
171-
return len(n)
172-
}
173-
174-
func (n huffmanCodes) Less(i, j int) bool {
175-
return n[i].code < n[j].code
176-
}
177-
178-
func (n huffmanCodes) Swap(i, j int) {
179-
n[i], n[j] = n[j], n[i]
180-
}
181-
182156
// buildHuffmanNode takes a slice of sorted huffmanCodes and builds a node in
183157
// the Huffman tree at the given level. It returns the index of the newly
184158
// constructed node.

0 commit comments

Comments
 (0)