コード例 #1
0
        public void BuildCodes()
        {
            HuffmanCodes.Clear();
            HuffmanTreeNode Root = HuffmanTreeList.First();

            BuildBinaryCodesInternal(Root, Root.BinaryCode);
        }
コード例 #2
0
        public long Decode(BitArray EncodedData, byte[] DecodedData)
        {
            long WrittenData = 0;

            HuffmanTreeNode CurrentNode = HuffmanTreeList.First();

            foreach (bool EncodedByte in EncodedData)
            {
                CurrentNode = EncodedByte ? CurrentNode.Right ?? CurrentNode : CurrentNode.Left ?? CurrentNode;

                if (CurrentNode.Name != null)
                {
                    DecodedData[WrittenData] = (byte)CurrentNode.Name.Value;
                    WrittenData++;
                    CurrentNode = HuffmanTreeList.First();
                }
            }

            return(WrittenData - 1);
        }
コード例 #3
0
        private void BuildBinaryCodesInternal(HuffmanTreeNode Node, List <bool> Code)
        {
            Node.BinaryCode = new List <bool>(Code);
            if (Node.Name != null)
            {
                HuffmanCodes.Add(Node.Name.Value, Node.BinaryCode);
            }

            if (Node.Left != null)
            {
                var LeftList = new List <bool>(Code);
                LeftList.Add(false);
                BuildBinaryCodesInternal(Node.Left, LeftList);
            }
            if (Node.Right != null)
            {
                var RightList = new List <bool>(Code);
                RightList.Add(true);
                BuildBinaryCodesInternal(Node.Right, RightList);
            }
        }