1#include <config.h>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63#include "bzlib_private.h"
64
65
66#define WEIGHTOF(zz0) ((zz0) & 0xffffff00)
67#define DEPTHOF(zz1) ((zz1) & 0x000000ff)
68#define MYMAX(zz2,zz3) ((zz2) > (zz3) ? (zz2) : (zz3))
69
70#define ADDWEIGHTS(zw1,zw2) \
71 (WEIGHTOF(zw1)+WEIGHTOF(zw2)) | \
72 (1 + MYMAX(DEPTHOF(zw1),DEPTHOF(zw2)))
73
74#define UPHEAP(z) \
75{ \
76 Int32 zz, tmp; \
77 zz = z; tmp = heap[zz]; \
78 while (weight[tmp] < weight[heap[zz >> 1]]) { \
79 heap[zz] = heap[zz >> 1]; \
80 zz >>= 1; \
81 } \
82 heap[zz] = tmp; \
83}
84
85#define DOWNHEAP(z) \
86{ \
87 Int32 zz, yy, tmp; \
88 zz = z; tmp = heap[zz]; \
89 while (True) { \
90 yy = zz << 1; \
91 if (yy > nHeap) break; \
92 if (yy < nHeap && \
93 weight[heap[yy+1]] < weight[heap[yy]]) \
94 yy++; \
95 if (weight[tmp] < weight[heap[yy]]) break; \
96 heap[zz] = heap[yy]; \
97 zz = yy; \
98 } \
99 heap[zz] = tmp; \
100}
101
102
103
104void BZ2_hbMakeCodeLengths ( UChar *len,
105 Int32 *freq,
106 Int32 alphaSize,
107 Int32 maxLen )
108{
109
110
111
112
113 Int32 nNodes, nHeap, n1, n2, i, j, k;
114 Bool tooLong;
115
116 Int32 heap [ BZ_MAX_ALPHA_SIZE + 2 ];
117 Int32 weight [ BZ_MAX_ALPHA_SIZE * 2 ];
118 Int32 parent [ BZ_MAX_ALPHA_SIZE * 2 ];
119
120 for (i = 0; i < alphaSize; i++)
121 weight[i+1] = (freq[i] == 0 ? 1 : freq[i]) << 8;
122
123 while (True) {
124
125 nNodes = alphaSize;
126 nHeap = 0;
127
128 heap[0] = 0;
129 weight[0] = 0;
130 parent[0] = -2;
131
132 for (i = 1; i <= alphaSize; i++) {
133 parent[i] = -1;
134 nHeap++;
135 heap[nHeap] = i;
136 UPHEAP(nHeap);
137 }
138
139 AssertH( nHeap < (BZ_MAX_ALPHA_SIZE+2), 2001 );
140
141 while (nHeap > 1) {
142 n1 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);
143 n2 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);
144 nNodes++;
145 parent[n1] = parent[n2] = nNodes;
146 weight[nNodes] = ADDWEIGHTS(weight[n1], weight[n2]);
147 parent[nNodes] = -1;
148 nHeap++;
149 heap[nHeap] = nNodes;
150 UPHEAP(nHeap);
151 }
152
153 AssertH( nNodes < (BZ_MAX_ALPHA_SIZE * 2), 2002 );
154
155 tooLong = False;
156 for (i = 1; i <= alphaSize; i++) {
157 j = 0;
158 k = i;
159 while (parent[k] >= 0) { k = parent[k]; j++; }
160 len[i-1] = j;
161 if (j > maxLen) tooLong = True;
162 }
163
164 if (! tooLong) break;
165
166 for (i = 1; i < alphaSize; i++) {
167 j = weight[i] >> 8;
168 j = 1 + (j / 2);
169 weight[i] = j << 8;
170 }
171 }
172}
173
174
175
176void BZ2_hbAssignCodes ( Int32 *code,
177 UChar *length,
178 Int32 minLen,
179 Int32 maxLen,
180 Int32 alphaSize )
181{
182 Int32 n, vec, i;
183
184 vec = 0;
185 for (n = minLen; n <= maxLen; n++) {
186 for (i = 0; i < alphaSize; i++)
187 if (length[i] == n) { code[i] = vec; vec++; };
188 vec <<= 1;
189 }
190}
191
192
193
194void BZ2_hbCreateDecodeTables ( Int32 *limit,
195 Int32 *base,
196 Int32 *perm,
197 UChar *length,
198 Int32 minLen,
199 Int32 maxLen,
200 Int32 alphaSize )
201{
202 Int32 pp, i, j, vec;
203
204 pp = 0;
205 for (i = minLen; i <= maxLen; i++)
206 for (j = 0; j < alphaSize; j++)
207 if (length[j] == i) { perm[pp] = j; pp++; };
208
209 for (i = 0; i < BZ_MAX_CODE_LEN; i++) base[i] = 0;
210 for (i = 0; i < alphaSize; i++) base[length[i]+1]++;
211
212 for (i = 1; i < BZ_MAX_CODE_LEN; i++) base[i] += base[i-1];
213
214 for (i = 0; i < BZ_MAX_CODE_LEN; i++) limit[i] = 0;
215 vec = 0;
216
217 for (i = minLen; i <= maxLen; i++) {
218 vec += (base[i+1] - base[i]);
219 limit[i] = vec-1;
220 vec <<= 1;
221 }
222 for (i = minLen + 1; i <= maxLen; i++)
223 base[i] = ((limit[i-1] + 1) << 1) - base[i];
224}
225
226
227
228
229
230