pqdownheap() приватный Метод

private pqdownheap ( short tree, int k ) : void
tree short
k int
Результат void
Пример #1
0
        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);
        }
Пример #2
0
        internal void build_tree(DeflateManager s)
        {
            short[] array     = dyn_tree;
            short[] treeCodes = staticTree.treeCodes;
            int     elems     = staticTree.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 (treeCodes != null)
                {
                    s.static_len -= treeCodes[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]        = (sbyte)(Math.Max((byte)s.depth[i], (byte)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);
        }
Пример #3
0
        internal void build_tree(DeflateManager s)
        {
            short[] tree      = this.dyn_tree;
            short[] numArray1 = this.staticTree.treeCodes;
            int     num1      = this.staticTree.elems;
            int     max_code  = -1;

            s.heap_len = 0;
            s.heap_max = Tree.HEAP_SIZE;
            for (int index = 0; index < num1; ++index)
            {
                if ((int)tree[index * 2] != 0)
                {
                    s.heap[++s.heap_len] = max_code = index;
                    s.depth[index]       = (sbyte)0;
                }
                else
                {
                    tree[index * 2 + 1] = (short)0;
                }
            }
            while (s.heap_len < 2)
            {
                int[] numArray2 = s.heap;
                int   index1    = ++s.heap_len;
                int   num2;
                if (max_code >= 2)
                {
                    num2 = 0;
                }
                else
                {
                    max_code = num2 = max_code + 1;
                }
                int num3 = num2;
                numArray2[index1] = num2;
                int index2 = num3;
                tree[index2 * 2] = (short)1;
                s.depth[index2]  = (sbyte)0;
                --s.opt_len;
                if (numArray1 != null)
                {
                    s.static_len -= (int)numArray1[index2 * 2 + 1];
                }
            }
            this.max_code = max_code;
            for (int k = s.heap_len / 2; k >= 1; --k)
            {
                s.pqdownheap(tree, k);
            }
            int index3 = num1;

            do
            {
                int index1 = s.heap[1];
                s.heap[1] = s.heap[s.heap_len--];
                s.pqdownheap(tree, 1);
                int index2 = s.heap[1];
                s.heap[--s.heap_max] = index1;
                s.heap[--s.heap_max] = index2;
                tree[index3 * 2]     = (short)((int)tree[index1 * 2] + (int)tree[index2 * 2]);
                s.depth[index3]      = (sbyte)((int)Math.Max((byte)s.depth[index1], (byte)s.depth[index2]) + 1);
                tree[index1 * 2 + 1] = tree[index2 * 2 + 1] = (short)index3;
                s.heap[1]            = index3++;
                s.pqdownheap(tree, 1);
            }while (s.heap_len >= 2);
            s.heap[--s.heap_max] = s.heap[1];
            this.gen_bitlen(s);
            Tree.gen_codes(tree, max_code, s.bl_count);
        }
Пример #4
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.
        internal void  build_tree(DeflateManager s)
        {
            short[] tree = dyn_tree;
            short[] stree = stat_desc.static_tree;
            int     elems = stat_desc.elems;
            int     n, m;          // iterate over heap elements
            int     max_code = -1; // largest code with non zero frequency
            int     node;          // new node being created

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

            for (n = 0; n < elems; n++)
            {
                if (tree[n * 2] != 0)
                {
                    s.heap[++s.heap_len] = max_code = n;
                    s.depth[n]           = 0;
                }
                else
                {
                    tree[n * 2 + 1] = 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 (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];
                }
                // node is 0 or 1 so it does not have extra bits
            }
            this.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 = s.heap_len / 2; n >= 1; n--)
            {
                s.pqdownheap(tree, n);
            }

            // Construct the Huffman tree by repeatedly combining the least two
            // frequent nodes.

            node = elems;             // next internal node of the tree
            do
            {
                // n = node of least frequency
                n         = s.heap[1];
                s.heap[1] = s.heap[s.heap_len--];
                s.pqdownheap(tree, 1);
                m = s.heap[1];                 // m = node of next least frequency

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

                // Create a new node father of n and m
                tree[node * 2]  = (short)(tree[n * 2] + tree[m * 2]);
                s.depth[node]   = (sbyte)(System.Math.Max((byte)s.depth[n], (byte)s.depth[m]) + 1);
                tree[n * 2 + 1] = tree[m * 2 + 1] = (short)node;

                // and insert the new node in the heap
                s.heap[1] = node++;
                s.pqdownheap(tree, 1);
            }while (s.heap_len >= 2);

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

            // At this point, the fields freq and dad are set. We can now
            // generate the bit lengths.

            gen_bitlen(s);

            // The field len is now set, we can generate the bit codes
            gen_codes(tree, max_code, s.bl_count);
        }
 // 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.
 internal void  build_tree(DeflateManager s)
 {
     short[] tree  = dyn_tree;
     short[] stree = staticTree.treeCodes;
     int elems     = staticTree.elems;
     int n, m;            // iterate over heap elements
     int max_code  = -1;  // largest code with non zero frequency
     int node;            // new node being created
                 
     // Construct the initial heap, with least frequent element in
     // heap[1]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
     // heap[0] is not used.
     s.heap_len = 0;
     s.heap_max = HEAP_SIZE;
                 
     for (n = 0; n < elems; n++)
     {
         if (tree[n * 2] != 0)
         {
             s.heap[++s.heap_len] = max_code = n;
             s.depth[n] = 0;
         }
         else
         {
             tree[n * 2 + 1] = 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 (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];
         // node is 0 or 1 so it does not have extra bits
     }
     this.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 = s.heap_len / 2; n >= 1; n--)
         s.pqdownheap(tree, n);
                 
     // Construct the Huffman tree by repeatedly combining the least two
     // frequent nodes.
                 
     node = elems; // next internal node of the tree
     do 
     {
         // n = node of least frequency
         n = s.heap[1];
         s.heap[1] = s.heap[s.heap_len--];
         s.pqdownheap(tree, 1);
         m = s.heap[1]; // m = node of next least frequency
                         
         s.heap[--s.heap_max] = n; // keep the nodes sorted by frequency
         s.heap[--s.heap_max] = m;
                         
         // Create a new node father of n and m
         tree[node * 2] = unchecked((short) (tree[n * 2] + tree[m * 2]));
         s.depth[node] = (sbyte) (System.Math.Max((byte) s.depth[n], (byte) s.depth[m]) + 1);
         tree[n * 2 + 1] = tree[m * 2 + 1] = (short) node;
                         
         // and insert the new node in the heap
         s.heap[1] = node++;
         s.pqdownheap(tree, 1);
     }
     while (s.heap_len >= 2);
                 
     s.heap[--s.heap_max] = s.heap[1];
                 
     // At this point, the fields freq and dad are set. We can now
     // generate the bit lengths.
                 
     gen_bitlen(s);
                 
     // The field len is now set, we can generate the bit codes
     gen_codes(tree, max_code, s.bl_count);
 }
Пример #6
0
        internal void build_tree(DeflateManager s)
        {
            int num2;
            int num5;

            short[] tree      = this.dyn_tree;
            short[] numArray2 = this.stat_desc.static_tree;
            int     elems     = this.stat_desc.elems;
            int     num4      = -1;

            s.heap_len = 0;
            s.heap_max = HEAP_SIZE;
            for (num2 = 0; num2 < elems; num2++)
            {
                if (tree[num2 * 2] != 0)
                {
                    s.heap[++s.heap_len] = num4 = num2;
                    s.depth[num2]        = 0;
                }
                else
                {
                    tree[(num2 * 2) + 1] = 0;
                }
            }
            while (s.heap_len < 2)
            {
                num5           = s.heap[++s.heap_len] = (num4 < 2) ? ++num4 : 0;
                tree[num5 * 2] = 1;
                s.depth[num5]  = 0;
                s.opt_len--;
                if (numArray2 != null)
                {
                    s.static_len -= numArray2[(num5 * 2) + 1];
                }
            }
            this.max_code = num4;
            num2          = s.heap_len / 2;
            while (num2 >= 1)
            {
                s.pqdownheap(tree, num2);
                num2--;
            }
            num5 = elems;
            do
            {
                num2      = s.heap[1];
                s.heap[1] = s.heap[s.heap_len--];
                s.pqdownheap(tree, 1);
                int index = s.heap[1];
                s.heap[--s.heap_max] = num2;
                s.heap[--s.heap_max] = index;
                tree[num5 * 2]       = (short)(tree[num2 * 2] + tree[index * 2]);
                s.depth[num5]        = (sbyte)(Math.Max((byte)s.depth[num2], (byte)s.depth[index]) + 1);
                tree[(num2 * 2) + 1] = tree[(index * 2) + 1] = (short)num5;
                s.heap[1]            = num5++;
                s.pqdownheap(tree, 1);
            }while (s.heap_len >= 2);
            s.heap[--s.heap_max] = s.heap[1];
            this.gen_bitlen(s);
            gen_codes(tree, num4, s.bl_count);
        }