=========================================================================== * Generate the codes for a given tree and bit counts (which need not be * optimal). * IN assertion: the array bl_count contains the bit length statistics for * the given tree and the field len is set for all tree elements. * OUT assertion: the field code is set for all tree elements of non * zero code length. */
| 200 | * zero code length. |
| 201 | */ |
| 202 | local void gen_codes(ct_data *tree, int max_code, ushf *bl_count) { |
| 203 | ush next_code[MAX_BITS+1]; /* next code value for each bit length */ |
| 204 | unsigned code = 0; /* running code value */ |
| 205 | int bits; /* bit index */ |
| 206 | int n; /* code index */ |
| 207 | |
| 208 | /* The distribution counts are first used to generate the code values |
| 209 | * without bit reversal. |
| 210 | */ |
| 211 | for (bits = 1; bits <= MAX_BITS; bits++) { |
| 212 | code = (code + bl_count[bits - 1]) << 1; |
| 213 | next_code[bits] = (ush)code; |
| 214 | } |
| 215 | /* Check that the bit counts in bl_count are consistent. The last code |
| 216 | * must be all ones. |
| 217 | */ |
| 218 | Assert (code + bl_count[MAX_BITS] - 1 == (1 << MAX_BITS) - 1, |
| 219 | "inconsistent bit counts"); |
| 220 | Tracev((stderr,"\ngen_codes: max_code %d ", max_code)); |
| 221 | |
| 222 | for (n = 0; n <= max_code; n++) { |
| 223 | int len = tree[n].Len; |
| 224 | if (len == 0) continue; |
| 225 | /* Now reverse the bits */ |
| 226 | tree[n].Code = (ush)bi_reverse(next_code[len]++, len); |
| 227 | |
| 228 | Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ", |
| 229 | n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len] - 1)); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | #ifdef GEN_TREES_H |
| 234 | local void gen_trees_header(void); |
no test coverage detected