MCPcopy Create free account
hub / github.com/InteractiveComputerGraphics/SPlisHSPlasH / build_tree

Function build_tree

extern/zlib/src/trees.c:625–704  ·  view source on GitHub ↗

=========================================================================== * Construct one Huffman tree and assigns the code bit strings and lengths. * Update the total bit length for the current block. * IN assertion: the field freq is set for all tree elements. * OUT assertions: the fields len and code are set to the optimal bit length * and corresponding code. The length opt_len is up

Source from the content-addressed store, hash-verified

623 * also updated if stree is not null. The field max_code is set.
624 */
625local void build_tree(deflate_state *s, tree_desc *desc) {
626 ct_data *tree = desc->dyn_tree;
627 const ct_data *stree = desc->stat_desc->static_tree;
628 int elems = desc->stat_desc->elems;
629 int n, m; /* iterate over heap elements */
630 int max_code = -1; /* largest code with non zero frequency */
631 int node; /* new node being created */
632
633 /* Construct the initial heap, with least frequent element in
634 * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n + 1].
635 * heap[0] is not used.
636 */
637 s->heap_len = 0, s->heap_max = HEAP_SIZE;
638
639 for (n = 0; n < elems; n++) {
640 if (tree[n].Freq != 0) {
641 s->heap[++(s->heap_len)] = max_code = n;
642 s->depth[n] = 0;
643 } else {
644 tree[n].Len = 0;
645 }
646 }
647
648 /* The pkzip format requires that at least one distance code exists,
649 * and that at least one bit should be sent even if there is only one
650 * possible code. So to avoid special checks later on we force at least
651 * two codes of non zero frequency.
652 */
653 while (s->heap_len < 2) {
654 node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0);
655 tree[node].Freq = 1;
656 s->depth[node] = 0;
657 s->opt_len--; if (stree) s->static_len -= stree[node].Len;
658 /* node is 0 or 1 so it does not have extra bits */
659 }
660 desc->max_code = max_code;
661
662 /* The elements heap[heap_len/2 + 1 .. heap_len] are leaves of the tree,
663 * establish sub-heaps of increasing lengths:
664 */
665 for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n);
666
667 /* Construct the Huffman tree by repeatedly combining the least two
668 * frequent nodes.
669 */
670 node = elems; /* next internal node of the tree */
671 do {
672 pqremove(s, tree, n); /* n = node of least frequency */
673 m = s->heap[SMALLEST]; /* m = node of next least frequency */
674
675 s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */
676 s->heap[--(s->heap_max)] = m;
677
678 /* Create a new node father of n and m */
679 tree[node].Freq = tree[n].Freq + tree[m].Freq;
680 s->depth[node] = (uch)((s->depth[n] >= s->depth[m] ?
681 s->depth[n] : s->depth[m]) + 1);
682 tree[n].Dad = tree[m].Dad = (ush)node;

Callers 2

build_bl_treeFunction · 0.85
trees.cFile · 0.85

Calls 3

pqdownheapFunction · 0.85
gen_bitlenFunction · 0.85
gen_codesFunction · 0.85

Tested by

no test coverage detected