MCPcopy Create free account
hub / github.com/9chu/LuaSTGPlus / gen_codes

Function gen_codes

ZLib/trees.c:575–607  ·  view source on GitHub ↗

=========================================================================== * 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. */

(tree, max_code, bl_count)

Source from the content-addressed store, hash-verified

573 * zero code length.
574 */
575local void gen_codes (tree, max_code, bl_count)
576 ct_data *tree; /* the tree to decorate */
577 int max_code; /* largest code with non zero frequency */
578 ushf *bl_count; /* number of codes at each bit length */
579{
580 ush next_code[MAX_BITS+1]; /* next code value for each bit length */
581 ush code = 0; /* running code value */
582 int bits; /* bit index */
583 int n; /* code index */
584
585 /* The distribution counts are first used to generate the code values
586 * without bit reversal.
587 */
588 for (bits = 1; bits <= MAX_BITS; bits++) {
589 next_code[bits] = code = (code + bl_count[bits-1]) << 1;
590 }
591 /* Check that the bit counts in bl_count are consistent. The last code
592 * must be all ones.
593 */
594 Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
595 "inconsistent bit counts");
596 Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
597
598 for (n = 0; n <= max_code; n++) {
599 int len = tree[n].Len;
600 if (len == 0) continue;
601 /* Now reverse the bits */
602 tree[n].Code = bi_reverse(next_code[len]++, len);
603
604 Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
605 n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
606 }
607}
608
609/* ===========================================================================
610 * Construct one Huffman tree and assigns the code bit strings and lengths.

Callers 2

tr_static_initFunction · 0.85
build_treeFunction · 0.85

Calls 1

bi_reverseFunction · 0.85

Tested by

no test coverage detected