@@ -90,13 +90,24 @@ func newHuffmanTree(lengths []uint8) (huffmanTree, error) {
90
90
91
91
// First we sort the code length assignments by ascending code length,
92
92
// using the symbol value to break ties.
93
- pairs := huffmanSymbolLengthPairs ( make ([]huffmanSymbolLengthPair , len (lengths ) ))
93
+ pairs := make ([]huffmanSymbolLengthPair , len (lengths ))
94
94
for i , length := range lengths {
95
95
pairs [i ].value = uint16 (i )
96
96
pairs [i ].length = length
97
97
}
98
98
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
+ })
100
111
101
112
// Now we assign codes to the symbols, starting with the longest code.
102
113
// We keep the codes packed into a uint32, at the most-significant end.
@@ -105,7 +116,7 @@ func newHuffmanTree(lengths []uint8) (huffmanTree, error) {
105
116
code := uint32 (0 )
106
117
length := uint8 (32 )
107
118
108
- codes := huffmanCodes ( make ([]huffmanCode , len (lengths ) ))
119
+ codes := make ([]huffmanCode , len (lengths ))
109
120
for i := len (pairs ) - 1 ; i >= 0 ; i -- {
110
121
if length > pairs [i ].length {
111
122
length = pairs [i ].length
@@ -120,7 +131,9 @@ func newHuffmanTree(lengths []uint8) (huffmanTree, error) {
120
131
121
132
// Now we can sort by the code so that the left half of each branch are
122
133
// 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
+ })
124
137
125
138
t .nodes = make ([]huffmanNode , len (codes ))
126
139
_ , err := buildHuffmanNode (& t , codes , 0 )
@@ -133,52 +146,13 @@ type huffmanSymbolLengthPair struct {
133
146
length uint8
134
147
}
135
148
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
-
160
149
// huffmanCode contains a symbol, its code and code length.
161
150
type huffmanCode struct {
162
151
code uint32
163
152
codeLen uint8
164
153
value uint16
165
154
}
166
155
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
-
182
156
// buildHuffmanNode takes a slice of sorted huffmanCodes and builds a node in
183
157
// the Huffman tree at the given level. It returns the index of the newly
184
158
// constructed node.
0 commit comments