=========================================================================== * Restore the heap property by moving down the tree starting at node k, * exchanging a node with the smallest of its two sons if necessary, stopping * when the heap property is re-established (each father smaller than its * two sons). */
(s, tree, k)
| 449 | * two sons). |
| 450 | */ |
| 451 | local void pqdownheap(s, tree, k) |
| 452 | deflate_state *s; |
| 453 | ct_data *tree; /* the tree to restore */ |
| 454 | int k; /* node to move down */ |
| 455 | { |
| 456 | int v = s->heap[k]; |
| 457 | int j = k << 1; /* left son of k */ |
| 458 | while (j <= s->heap_len) { |
| 459 | /* Set j to the smallest of the two sons: */ |
| 460 | if (j < s->heap_len && |
| 461 | smaller(tree, s->heap[j+1], s->heap[j], s->depth)) { |
| 462 | j++; |
| 463 | } |
| 464 | /* Exit if v is smaller than both sons */ |
| 465 | if (smaller(tree, v, s->heap[j], s->depth)) break; |
| 466 | |
| 467 | /* Exchange v with the smallest son */ |
| 468 | s->heap[k] = s->heap[j]; k = j; |
| 469 | |
| 470 | /* And continue down the tree, setting j to the left son of k */ |
| 471 | j <<= 1; |
| 472 | } |
| 473 | s->heap[k] = v; |
| 474 | } |
| 475 | |
| 476 | /* =========================================================================== |
| 477 | * Compute the optimal bit lengths for a tree and update the total bit length |