private pqdownheap ( short tree, int k ) : void | ||
tree | short | |
k | int | |
return | void |
internal void pqdownheap(short[] tree, int k)
{
int v = heap[k];
int j = k << 1; // left son of k
while (j <= heap_len)
{
// Set j to the smallest of the two sons:
if (j < heap_len && _IsSmaller(tree, heap[j + 1], heap[j], depth))
{
j++;
}
// Exit if v is smaller than both sons
if (_IsSmaller(tree, v, heap[j], depth))
break;
// Exchange v with the smallest son
heap[k] = heap[j]; k = j;
// And continue down the tree, setting j to the left son of k
j <<= 1;
}
heap[k] = v;
}
internal void build_tree(DeflateManager s) { short[] tree = dyn_tree; short[] stree = staticTree.treeCodes; int elems = staticTree.elems; int max_code = -1; s.heap_len = 0; s.heap_max = HEAP_SIZE; for (int j = 0; j < elems; j++) { if (tree[j * 2] != 0) { max_code = (s.heap[++s.heap_len] = j); s.depth[j] = 0; } else { tree[j * 2 + 1] = 0; } } int node; while (s.heap_len < 2) { node = (s.heap[++s.heap_len] = ((max_code < 2) ? (++max_code) : 0)); tree[node * 2] = 1; s.depth[node] = 0; s.opt_len--; if (stree != null) { s.static_len -= stree[node * 2 + 1]; } } this.max_code = max_code; for (int j = s.heap_len / 2; j >= 1; j--) { s.pqdownheap(tree, j); } node = elems; do { int j = s.heap[1]; s.heap[1] = s.heap[s.heap_len--]; s.pqdownheap(tree, 1); int i = s.heap[1]; s.heap[--s.heap_max] = j; s.heap[--s.heap_max] = i; tree[node * 2] = (short)(tree[j * 2] + tree[i * 2]); s.depth[node] = (sbyte)(Math.Max((byte)s.depth[j], (byte)s.depth[i]) + 1); tree[j * 2 + 1] = (tree[i * 2 + 1] = (short)node); s.heap[1] = node++; s.pqdownheap(tree, 1); }while (s.heap_len >= 2); s.heap[--s.heap_max] = s.heap[1]; gen_bitlen(s); gen_codes(tree, max_code, s.bl_count); }