コード例 #1
0
 public HuffmanTreeNode(HuffmanTreeNode left, HuffmanTreeNode right)
 {
     Left        = left;
     Right       = right;
     Left.Parent = Right.Parent = this;
     if (ByteCount == null)
     {
         ByteCount = new ByteCount();
     }
     ByteCount.Count = Left.ByteCount.Count + Right.ByteCount.Count;
 }
コード例 #2
0
        public HuffmanTree(ByteCount[] bytesFreqs)
        {
            var nodes = new List <HuffmanTreeNode>();

            for (int i = 0; i < bytesFreqs.Length; i++)
            {
                nodes.Add(new HuffmanTreeNode
                {
                    ByteCount = new ByteCount
                    {
                        Byte  = bytesFreqs[i].Byte,
                        Count = bytesFreqs[i].Count
                    }
                });
            }

            HuffmanTreeNode left, right;

            while (nodes.Count > 1)
            {
                int min    = nodes[0].ByteCount.Count;
                int minInd = 0;
                for (int j = 1; j < nodes.Count; j++)
                {
                    if (min > nodes[j].ByteCount.Count)
                    {
                        min    = nodes[j].ByteCount.Count;
                        minInd = j;
                    }
                }
                left = nodes[minInd];
                nodes.RemoveAt(minInd);

                min    = nodes[0].ByteCount.Count;
                minInd = 0;
                for (int j = 1; j < nodes.Count; j++)
                {
                    if (min > nodes[j].ByteCount.Count)
                    {
                        min    = nodes[j].ByteCount.Count;
                        minInd = j;
                    }
                }
                right = nodes[minInd];
                nodes.RemoveAt(minInd);

                nodes.Add(new HuffmanTreeNode(left, right));
            }

            Root = nodes[0];
            CalculateBytes();
        }
コード例 #3
0
     private void TraverseTree(HuffmanTreeNode node, int length, int value)
     {
         if (node.Left != null)
         {
             length++;
             TraverseTree(node.Left, length, value);
             value |= (1 << (length - 1));
             TraverseTree(node.Right, length, value);
         }
         else
         {
             CompressedBytes[node.ByteCount.Byte] = new CompressedByte {
                 Length = length, Value = value
             }
         };
     }
 }
コード例 #4
0
ファイル: Utils.cs プロジェクト: bradparks/Freaky-Sources
        public static byte GetValue(HuffmanTreeNode root, byte[] bytes, ref int bitPos)
        {
            int curBytePos      = bitPos / 8;
            int curBitInBytePos = bitPos % 8;
            int bit;
            var curNode = root;

            while (curNode.Left != null)
            {
                bit = bytes[curBytePos] & (128 >> curBitInBytePos);
                if (bit == 0)
                {
                    curNode = curNode.Left;
                }
                else
                {
                    curNode = curNode.Right;
                }
                curBytePos     += (curBitInBytePos + 1) / 8;
                curBitInBytePos = (curBitInBytePos + 1) % 8;
            }
            bitPos = curBytePos * 8 + curBitInBytePos;
            return(curNode.ByteCount.Byte);
        }
コード例 #5
0
ファイル: Huffman.cs プロジェクト: KvanTTT/Freaky-Sources
        public HuffmanTree(ByteCount[] bytesFreqs)
        {
            var nodes = new List<HuffmanTreeNode>();
            for (int i = 0; i < bytesFreqs.Length; i++)
                nodes.Add(new HuffmanTreeNode
                {
                    ByteCount = new ByteCount
                    {
                        Byte = bytesFreqs[i].Byte,
                        Count = bytesFreqs[i].Count
                    }
                });

            HuffmanTreeNode left, right;
            while (nodes.Count > 1)
            {
                int min = nodes[0].ByteCount.Count;
                int minInd = 0;
                for (int j = 1; j < nodes.Count; j++)
                    if (min > nodes[j].ByteCount.Count)
                    {
                        min = nodes[j].ByteCount.Count;
                        minInd = j;
                    }
                left = nodes[minInd];
                nodes.RemoveAt(minInd);

                min = nodes[0].ByteCount.Count;
                minInd = 0;
                for (int j = 1; j < nodes.Count; j++)
                    if (min > nodes[j].ByteCount.Count)
                    {
                        min = nodes[j].ByteCount.Count;
                        minInd = j;
                    }
                right = nodes[minInd];
                nodes.RemoveAt(minInd);

                nodes.Add(new HuffmanTreeNode(left, right));
            }

            Root = nodes[0];
            CalculateBytes();
        }
コード例 #6
0
ファイル: Huffman.cs プロジェクト: KvanTTT/Freaky-Sources
 public HuffmanTreeNode(HuffmanTreeNode left, HuffmanTreeNode right)
 {
     Left = left;
     Right = right;
     Left.Parent = Right.Parent = this;
     if (ByteCount == null)
         ByteCount = new ByteCount();
     ByteCount.Count = Left.ByteCount.Count + Right.ByteCount.Count;
 }
コード例 #7
0
ファイル: Huffman.cs プロジェクト: KvanTTT/Freaky-Sources
 private void TraverseTree(HuffmanTreeNode node, int length, int value)
 {
     if (node.Left != null)
     {
         length++;
         TraverseTree(node.Left, length, value);
         value |= (1 << (length - 1));
         TraverseTree(node.Right, length, value);
     }
     else
         CompressedBytes[node.ByteCount.Byte] = new CompressedByte { Length = length, Value = value };
 }
コード例 #8
0
ファイル: Utils.cs プロジェクト: KvanTTT/Freaky-Sources
 public static byte GetValue(HuffmanTreeNode root, byte[] bytes, ref int bitPos)
 {
     int curBytePos = bitPos / 8;
     int curBitInBytePos = bitPos % 8;
     int bit;
     var curNode = root;
     while (curNode.Left != null)
     {
         bit = bytes[curBytePos] & (128 >> curBitInBytePos);
         if (bit == 0)
             curNode = curNode.Left;
         else
             curNode = curNode.Right;
         curBytePos += (curBitInBytePos + 1) / 8;
         curBitInBytePos = (curBitInBytePos + 1) % 8;
     }
     bitPos = curBytePos * 8 + curBitInBytePos;
     return curNode.ByteCount.Byte;
 }