abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
huffman.c File Reference
#include "bzlib_private.h"

Go to the source code of this file.

Macros

#define WEIGHTOF(zz0)   ((zz0) & 0xffffff00)
 
#define DEPTHOF(zz1)   ((zz1) & 0x000000ff)
 
#define MYMAX(zz2, zz3)   ((zz2) > (zz3) ? (zz2) : (zz3))
 
#define ADDWEIGHTS(zw1, zw2)
 
#define UPHEAP(z)
 
#define DOWNHEAP(z)
 

Functions

void BZ2_hbMakeCodeLengths (UChar *len, Int32 *freq, Int32 alphaSize, Int32 maxLen)
 
void BZ2_hbAssignCodes (Int32 *code, UChar *length, Int32 minLen, Int32 maxLen, Int32 alphaSize)
 
void BZ2_hbCreateDecodeTables (Int32 *limit, Int32 *base, Int32 *perm, UChar *length, Int32 minLen, Int32 maxLen, Int32 alphaSize)
 

Macro Definition Documentation

#define ADDWEIGHTS (   zw1,
  zw2 
)
Value:
(WEIGHTOF(zw1)+WEIGHTOF(zw2)) | \
(1 + MYMAX(DEPTHOF(zw1),DEPTHOF(zw2)))
#define DEPTHOF(zz1)
Definition: huffman.c:29
#define MYMAX(zz2, zz3)
Definition: huffman.c:30
#define WEIGHTOF(zz0)
Definition: huffman.c:28

Definition at line 32 of file huffman.c.

#define DEPTHOF (   zz1)    ((zz1) & 0x000000ff)

Definition at line 29 of file huffman.c.

#define DOWNHEAP (   z)
Value:
{ \
Int32 zz, yy, tmp; \
zz = z; tmp = heap[zz]; \
while (True) { \
yy = zz << 1; \
if (yy > nHeap) break; \
if (yy < nHeap && \
weight[heap[yy+1]] < weight[heap[yy]]) \
yy++; \
if (weight[tmp] < weight[heap[yy]]) break; \
heap[zz] = heap[yy]; \
zz = yy; \
} \
heap[zz] = tmp; \
}
if(last==0)
Definition: sparse_int.h:34
int Int32
Definition: bzlib_private.h:46
#define True
Definition: bzlib_private.h:51

Definition at line 47 of file huffman.c.

#define MYMAX (   zz2,
  zz3 
)    ((zz2) > (zz3) ? (zz2) : (zz3))

Definition at line 30 of file huffman.c.

#define UPHEAP (   z)
Value:
{ \
Int32 zz, tmp; \
zz = z; tmp = heap[zz]; \
while (weight[tmp] < weight[heap[zz >> 1]]) { \
heap[zz] = heap[zz >> 1]; \
zz >>= 1; \
} \
heap[zz] = tmp; \
}
int Int32
Definition: bzlib_private.h:46

Definition at line 36 of file huffman.c.

#define WEIGHTOF (   zz0)    ((zz0) & 0xffffff00)

Definition at line 28 of file huffman.c.

Function Documentation

void BZ2_hbAssignCodes ( Int32 code,
UChar length,
Int32  minLen,
Int32  maxLen,
Int32  alphaSize 
)

Definition at line 155 of file huffman.c.

160 {
161  Int32 n, vec, i;
162 
163  vec = 0;
164  for (n = minLen; n <= maxLen; n++) {
165  for (i = 0; i < alphaSize; i++)
166  if (length[i] == n) { code[i] = vec; vec++; };
167  vec <<= 1;
168  }
169 }
Definition: inftrees.h:26
int Int32
Definition: bzlib_private.h:46
void BZ2_hbCreateDecodeTables ( Int32 limit,
Int32 base,
Int32 perm,
UChar length,
Int32  minLen,
Int32  maxLen,
Int32  alphaSize 
)

Definition at line 173 of file huffman.c.

180 {
181  Int32 pp, i, j, vec;
182 
183  pp = 0;
184  for (i = minLen; i <= maxLen; i++)
185  for (j = 0; j < alphaSize; j++)
186  if (length[j] == i) { perm[pp] = j; pp++; };
187 
188  for (i = 0; i < BZ_MAX_CODE_LEN; i++) base[i] = 0;
189  for (i = 0; i < alphaSize; i++) base[length[i]+1]++;
190 
191  for (i = 1; i < BZ_MAX_CODE_LEN; i++) base[i] += base[i-1];
192 
193  for (i = 0; i < BZ_MAX_CODE_LEN; i++) limit[i] = 0;
194  vec = 0;
195 
196  for (i = minLen; i <= maxLen; i++) {
197  vec += (base[i+1] - base[i]);
198  limit[i] = vec-1;
199  vec <<= 1;
200  }
201  for (i = minLen + 1; i <= maxLen; i++)
202  base[i] = ((limit[i-1] + 1) << 1) - base[i];
203 }
int Int32
Definition: bzlib_private.h:46
#define BZ_MAX_CODE_LEN
void BZ2_hbMakeCodeLengths ( UChar len,
Int32 freq,
Int32  alphaSize,
Int32  maxLen 
)

Definition at line 66 of file huffman.c.

70 {
71  /*--
72  Nodes and heap entries run from 1. Entry 0
73  for both the heap and nodes is a sentinel.
74  --*/
75  Int32 nNodes, nHeap, n1, n2, i, j, k;
76  Bool tooLong;
77 
78  Int32 heap [ BZ_MAX_ALPHA_SIZE + 2 ];
79  Int32 weight [ BZ_MAX_ALPHA_SIZE * 2 ];
80  Int32 parent [ BZ_MAX_ALPHA_SIZE * 2 ];
81 
82  for (i = 0; i < alphaSize; i++)
83  weight[i+1] = (freq[i] == 0 ? 1 : freq[i]) << 8;
84 
85  while (True) {
86 
87  nNodes = alphaSize;
88  nHeap = 0;
89 
90  heap[0] = 0;
91  weight[0] = 0;
92  parent[0] = -2;
93 
94  for (i = 1; i <= alphaSize; i++) {
95  parent[i] = -1;
96  nHeap++;
97  heap[nHeap] = i;
98  UPHEAP(nHeap);
99  }
100 
101  AssertH( nHeap < (BZ_MAX_ALPHA_SIZE+2), 2001 );
102 
103  while (nHeap > 1) {
104  n1 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);
105  n2 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);
106  nNodes++;
107  parent[n1] = parent[n2] = nNodes;
108  weight[nNodes] = ADDWEIGHTS(weight[n1], weight[n2]);
109  parent[nNodes] = -1;
110  nHeap++;
111  heap[nHeap] = nNodes;
112  UPHEAP(nHeap);
113  }
114 
115  AssertH( nNodes < (BZ_MAX_ALPHA_SIZE * 2), 2002 );
116 
117  tooLong = False;
118  for (i = 1; i <= alphaSize; i++) {
119  j = 0;
120  k = i;
121  while (parent[k] >= 0) { k = parent[k]; j++; }
122  len[i-1] = j;
123  if (j > maxLen) tooLong = True;
124  }
125 
126  if (! tooLong) break;
127 
128  /* 17 Oct 04: keep-going condition for the following loop used
129  to be 'i < alphaSize', which missed the last element,
130  theoretically leading to the possibility of the compressor
131  looping. However, this count-scaling step is only needed if
132  one of the generated Huffman code words is longer than
133  maxLen, which up to and including version 1.0.2 was 20 bits,
134  which is extremely unlikely. In version 1.0.3 maxLen was
135  changed to 17 bits, which has minimal effect on compression
136  ratio, but does mean this scaling step is used from time to
137  time, enough to verify that it works.
138 
139  This means that bzip2-1.0.3 and later will only produce
140  Huffman codes with a maximum length of 17 bits. However, in
141  order to preserve backwards compatibility with bitstreams
142  produced by versions pre-1.0.3, the decompressor must still
143  handle lengths of up to 20. */
144 
145  for (i = 1; i <= alphaSize; i++) {
146  j = weight[i] >> 8;
147  j = 1 + (j / 2);
148  weight[i] = j << 8;
149  }
150  }
151 }
#define ADDWEIGHTS(zw1, zw2)
Definition: huffman.c:32
unsigned char Bool
Definition: bzlib_private.h:44
#define AssertH(cond, errcode)
Definition: bzlib_private.h:61
#define UPHEAP(z)
Definition: huffman.c:36
int Int32
Definition: bzlib_private.h:46
#define BZ_MAX_ALPHA_SIZE
#define True
Definition: bzlib_private.h:51
#define False
Definition: bzlib_private.h:52
#define DOWNHEAP(z)
Definition: huffman.c:47