⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 huffmantreegen.cs

📁 PDF文件格式解析库源代码
💻 CS
字号:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using QiHe.CodeLib;

namespace QiHe.CodeLib.Compress
{
    public partial class HuffmanTree
    {
        public static HuffmanTree FromSymbolWeights(MultiSet<int> symbolSet, int lengthLimit)
        {
            if (symbolSet.Count == 0)
            {
                HuffmanTree tree = new HuffmanTree(new int[] { 0 });
                tree.SymbolWeights = new int[] { };
                return tree;
            }
            int maximumSymbol = Algorithm.Maximum(symbolSet.Items);
            int[] symbolWeights = new int[maximumSymbol + 1];
            for (int symbol = 0; symbol <= maximumSymbol; symbol++)
            {
                if (symbolSet.Contains(symbol))
                {
                    symbolWeights[symbol] = symbolSet.GetOccur(symbol);
                }
            }
            return FromSymbolWeights(symbolWeights, lengthLimit);
        }

        public static int Round = 0;
        public static int round = 300;

        public int WeightedPathLength;
        public int[] SymbolWeights;

        public static HuffmanTree FromSymbolWeights(int[] symbolWeights, int lengthLimit)
        {
            //int[] codeLengths = HuffmanAlgorithm.ComputeOptimalCodeLengths(symbolWeights);
            int[] codeLengths = PackageMergeAlgorithm.Solve(symbolWeights, lengthLimit);
            //Round++;
            //round++;
            //if (Round < 100)
            //{
            //    int wpl = CalculateWeightedPathLength(symbolWeights, codeLengths);
            //    string time = String.Format("Round_{0:D2}_{1:D3}", Round, round);
            //    TraceTool.MultiColPage.Debug.Send(time + "\t" + wpl);
            //    TraceTool.Debug.SendValue(time, symbolWeights);
            //    TraceTool.Debug.SendValue(time, codeLengths);
            //}
            HuffmanTree tree = new HuffmanTree(codeLengths);
            tree.SymbolWeights = symbolWeights;
            tree.WeightedPathLength = CalculateWeightedPathLength(symbolWeights, codeLengths);
            return tree;
        }

        public static int CalculateWeightedPathLength(int[] symbolWeights, int[] codeLengths)
        {
            int sum = 0;
            for (int symbol = 0; symbol < symbolWeights.Length; symbol++)
            {
                sum += symbolWeights[symbol] * codeLengths[symbol];
            }
            return sum;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -