/* =========================================================================== * 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); }
void ZipDeflate::build_tree(zip_tree_desc* desc) { zip_ct_data* tree(desc->dyn_tree); const zip_ct_data* stree(desc->stat_desc->static_tree); const int* extra(desc->stat_desc->extra_bits); int elems(desc->stat_desc->elems), base(desc->stat_desc->extra_base), max_length(desc->stat_desc->max_length), n, m, max_code = -1, node, h, bits, xbits, overflow(0); WORD next_code[MAX_BITS + 1], code(0), f; heap_len = 0, heap_max = HEAP_SIZE; for(n = 0; n < elems; n++) {if(tree[n].fc.freq != 0) {heap[++(heap_len)] = max_code = n; depth[n] = 0;} else tree[n].dl.len = 0;} while(heap_len < 2) { node = heap[++(heap_len)] = (max_code < 2 ? ++max_code : 0); tree[node].fc.freq = 1; depth[node] = 0; opt_len--; if(stree) static_len -= stree[node].dl.len; } desc->max_code = max_code; for(n = heap_len / 2; n >= 1; n--) pqdownheap(tree, n); node = elems; do { n = heap[1]; heap[1] = heap[heap_len--]; pqdownheap(tree, 1); m = heap[1]; heap[--(heap_max)] = n; heap[--(heap_max)] = m; tree[node].fc.freq = tree[n].fc.freq + tree[m].fc.freq; depth[node] = (BYTE)((depth[n] >= depth[m] ? depth[n] : depth[m]) + 1); tree[n].dl.dad = tree[m].dl.dad = (WORD)node; heap[1] = node++; pqdownheap(tree, 1); } while (heap_len >= 2); heap[--(heap_max)] = heap[1]; max_code = desc->max_code; for(bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0; tree[heap[heap_max]].dl.len = 0; for(h = heap_max + 1; h < HEAP_SIZE; h++) { n = heap[h]; bits = tree[tree[n].dl.dad].dl.len + 1; if(bits > max_length) bits = max_length, overflow++; tree[n].dl.len = (WORD)bits; if(n > max_code) continue; bl_count[bits]++; xbits = 0; if(n >= base) xbits = extra[n-base]; f = tree[n].fc.freq; opt_len += (DWORD)f * (bits + xbits); if(stree) static_len += (DWORD)f * (stree[n].dl.len + xbits); } if(overflow == 0) return; do { bits = max_length-1; while(bl_count[bits] == 0) bits--; bl_count[bits]--; bl_count[bits + 1] += 2; bl_count[max_length]--; overflow -= 2; } while(overflow > 0); for(bits = max_length; bits != 0; bits--) { n = bl_count[bits]; while(n) { m = heap[--h]; if(m > max_code) continue; if((UINT)tree[m].dl.len != (UINT)bits) { opt_len += ((long)bits - (long)tree[m].dl.len) *(long)tree[m].fc.freq; tree[m].dl.len = (WORD)bits; } n--; } } for(bits = 1; bits <= MAX_BITS; bits++) { next_code[bits] = code = (code + bl_count[bits - 1]) << 1; } for(n = 0; n <= max_code; n++) { int len = tree[n].dl.len; if(len == 0) continue; UINT code(next_code[len]++), res = 0; do { res |= code & 1; code >>= 1, res <<= 1; } while(--len > 0); tree[n].fc.code = (res >> 1); } }
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 ); }