MCPcopy Create free account
hub / github.com/F-Stack/f-stack / gen_codes

Function gen_codes

freebsd/contrib/zlib/trees.c:572–605  ·  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

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