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 && smaller(tree, heap[j + 1], heap[j], depth))
{
j++;
}
// Exit if v is smaller than both sons
if (smaller(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(Deflate s) { short[] array = dyn_tree; short[] static_tree = stat_desc.static_tree; int elems = stat_desc.elems; int num = -1; s.heap_len = 0; s.heap_max = HEAP_SIZE; for (int i = 0; i < elems; i++) { if (array[i * 2] != 0) { num = (s.heap[++s.heap_len] = i); s.depth[i] = 0; } else { array[i * 2 + 1] = 0; } } int num2; while (s.heap_len < 2) { num2 = (s.heap[++s.heap_len] = ((num < 2) ? (++num) : 0)); array[num2 * 2] = 1; s.depth[num2] = 0; s.opt_len--; if (static_tree != null) { s.static_len -= static_tree[num2 * 2 + 1]; } } max_code = num; for (int i = s.heap_len / 2; i >= 1; i--) { s.pqdownheap(array, i); } num2 = elems; do { int i = s.heap[1]; s.heap[1] = s.heap[s.heap_len--]; s.pqdownheap(array, 1); int num3 = s.heap[1]; s.heap[--s.heap_max] = i; s.heap[--s.heap_max] = num3; array[num2 * 2] = (short)(array[i * 2] + array[num3 * 2]); s.depth[num2] = (byte)(Math.Max(s.depth[i], s.depth[num3]) + 1); array[i * 2 + 1] = (array[num3 * 2 + 1] = (short)num2); s.heap[1] = num2++; s.pqdownheap(array, 1); }while (s.heap_len >= 2); s.heap[--s.heap_max] = s.heap[1]; gen_bitlen(s); gen_codes(array, num, s.bl_count); }