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

Function gen_bitlen

freebsd/contrib/zlib/trees.c:486–562  ·  view source on GitHub ↗

=========================================================================== * Compute the optimal bit lengths for a tree and update the total bit length * for the current block. * IN assertion: the fields freq and dad are set, heap[heap_max] and * above are the tree nodes sorted by increasing frequency. * OUT assertions: the field len is set to the optimal bit length, the * array bl_c

(s, desc)

Source from the content-addressed store, hash-verified

484 * not null.
485 */
486local void gen_bitlen(s, desc)
487 deflate_state *s;
488 tree_desc *desc; /* the tree descriptor */
489{
490 ct_data *tree = desc->dyn_tree;
491 int max_code = desc->max_code;
492 const ct_data *stree = desc->stat_desc->static_tree;
493 const intf *extra = desc->stat_desc->extra_bits;
494 int base = desc->stat_desc->extra_base;
495 int max_length = desc->stat_desc->max_length;
496 int h; /* heap index */
497 int n, m; /* iterate over the tree elements */
498 int bits; /* bit length */
499 int xbits; /* extra bits */
500 ush f; /* frequency */
501 int overflow = 0; /* number of elements with bit length too large */
502
503 for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0;
504
505 /* In a first pass, compute the optimal bit lengths (which may
506 * overflow in the case of the bit length tree).
507 */
508 tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */
509
510 for (h = s->heap_max+1; h < HEAP_SIZE; h++) {
511 n = s->heap[h];
512 bits = tree[tree[n].Dad].Len + 1;
513 if (bits > max_length) bits = max_length, overflow++;
514 tree[n].Len = (ush)bits;
515 /* We overwrite tree[n].Dad which is no longer needed */
516
517 if (n > max_code) continue; /* not a leaf node */
518
519 s->bl_count[bits]++;
520 xbits = 0;
521 if (n >= base) xbits = extra[n-base];
522 f = tree[n].Freq;
523 s->opt_len += (ulg)f * (unsigned)(bits + xbits);
524 if (stree) s->static_len += (ulg)f * (unsigned)(stree[n].Len + xbits);
525 }
526 if (overflow == 0) return;
527
528 Tracev((stderr,"\nbit length overflow\n"));
529 /* This happens for example on obj2 and pic of the Calgary corpus */
530
531 /* Find the first bit length which could increase: */
532 do {
533 bits = max_length-1;
534 while (s->bl_count[bits] == 0) bits--;
535 s->bl_count[bits]--; /* move one leaf down the tree */
536 s->bl_count[bits+1] += 2; /* move one overflow item as its brother */
537 s->bl_count[max_length]--;
538 /* The brother of the overflow item also moves one step up,
539 * but this does not affect bl_count[max_length]
540 */
541 overflow -= 2;
542 } while (overflow > 0);
543

Callers 1

build_treeFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected