Ejemplo n.º 1
0
/* ===========================================================================
 * 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 updated; static_len is
 *     also updated if stree is not null. The field max_code is set.
 */
void build_tree(tree_desc near *desc)
    //tree_desc near *desc; /* the tree descriptor */
{
    ct_data near *tree   = desc->dyn_tree;
    ct_data near *stree  = desc->static_tree;
    int elems            = desc->elems;
    int n, m;          /* iterate over heap elements */
    int max_code = -1; /* largest code with non zero frequency */
    int node = elems;  /* next internal node of the tree */

    /* Construct the initial heap, with least frequent element in
     * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
     * heap[0] is not used.
     */
    heap_len = 0, heap_max = HEAP_SIZE;

    for (n = 0; n < elems; n++) {
        if (tree[n].Freq != 0) {
            heap[++heap_len] = max_code = n;
            depth[n] = 0;
        } else {
            tree[n].Len = 0;
        }
    }

    /* The pkzip format requires that at least one distance code exists,
     * and that at least one bit should be sent even if there is only one
     * possible code. So to avoid special checks later on we force at least
     * two codes of non zero frequency.
     */
    while (heap_len < 2) {
        int new_ = heap[++heap_len] = (max_code < 2 ? ++max_code : 0);
        tree[new_].Freq = 1;
        depth[new_] = 0;
        opt_len--; if (stree) static_len -= stree[new_].Len;
        /* new is 0 or 1 so it does not have extra bits */
    }
    desc->max_code = max_code;

    /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
     * establish sub-heaps of increasing lengths:
     */
    for (n = heap_len/2; n >= 1; n--) pqdownheap(tree, n);

    /* Construct the Huffman tree by repeatedly combining the least two
     * frequent nodes.
     */
    do {
        pqremove(tree, n);   /* n = node of least frequency */
        m = heap[SMALLEST];  /* m = node of next least frequency */

        heap[--heap_max] = n; /* keep the nodes sorted by frequency */
        heap[--heap_max] = m;

        /* Create a new node father of n and m */
        tree[node].Freq = (u16)(tree[n].Freq + tree[m].Freq);
        depth[node] = (u8) (max(depth[n], depth[m]) + 1);
        tree[n].Dad = tree[m].Dad = (u16)node;

        /* and insert the new node in the heap */
        heap[SMALLEST] = node++;
        pqdownheap(tree, SMALLEST);

    } while (heap_len >= 2);

    heap[--heap_max] = heap[SMALLEST];

    /* At this point, the fields freq and dad are set. We can now
     * generate the bit lengths.
     */
    gen_bitlen((tree_desc near *)desc);

    /* The field len is now set, we can generate the bit codes */
    gen_codes ((ct_data near *)tree, max_code);
}
Ejemplo n.º 2
0
static void build_tree(
    z_stream&	s,
    tree_desc*	desc )
{
    ct_data* tree         = desc->dyn_tree;
    const ct_data* stree  = desc->stat_desc->static_tree;
    int elems             = desc->stat_desc->elems;
    int max_code		  = -1;
    int n, m;
    int node;

    s.heap_len = 0, s.heap_max = HEAP_SIZE;

    for( n = 0 ; n < elems ; n++ )
    {
        if( tree[ n ].Freq )
        {
            s.heap[ ++s.heap_len ] = max_code = n;
            s.depth[ n ] = 0;
        }
        else
        {
            tree[ n ].Len = 0;
        }
    }

    while( s.heap_len < 2 )
    {
        node = s.heap[ ++s.heap_len ] = ( max_code < 2 ? ++max_code : 0 );
        tree[ node ].Freq = 1;
        s.depth[ node ] = 0;
        s.opt_len--;
        if( stree )
            s.static_len -= stree[ node ].Len;
    }

    desc->max_code = max_code;

    for( n = s.heap_len / 2 ; n >= 1 ; n-- )
        pqdownheap( s, tree, n );

    node = elems;

    do
    {
        pqremove( s, tree, n );
        m = s.heap[ SMALLEST ];

        s.heap[ --s.heap_max ] = n;
        s.heap[ --s.heap_max ] = m;

        tree[ node ].Freq = tree[ n ].Freq + tree[ m ].Freq;
        s.depth[ node ] = (unsigned char) ( ( s.depth[ n ] >= s.depth[ m ] ? s.depth[ n ] : s.depth[ m ] ) + 1 );
        tree[ n ].Dad = tree[ m ].Dad = (unsigned short) node;

        s.heap[ SMALLEST ] = node++;
        pqdownheap( s, tree, SMALLEST );

    }
    while( s.heap_len >= 2 );

    s.heap[ --s.heap_max ] = s.heap[ SMALLEST ];

    gen_bitlen( s, (tree_desc*) desc );
    gen_codes( (ct_data*) tree, max_code, s.bl_count );
}