Example #1
0
            /// <summary>Построение дерева Хаффмана из таблицы частот.</summary>
            internal HuffmanTree(FrequencyTable frequencyTable)
            {
                ushort Length = (ushort)frequencyTable.FoundBytes.Length;

                this.frequencyTable = frequencyTable;
                Leafs = new TreeNode[Length];
                if (Length > 1) // один корень
                {
                    for (ushort i = 0; i < Length; ++i)
                    {
                        Leafs[i] = new TreeNode
                        {
                            ByteValue = frequencyTable.FoundBytes[i],
                            Value     = frequencyTable.Frequency[i]
                        };
                    }
                    OrphanNodes.AddRange(Leafs);
                    RootNode = BuildTree();
                }
                else
                {
                    var TempNode = new TreeNode();
                    TempNode.ByteValue = frequencyTable.FoundBytes[0];
                    TempNode.Value     = frequencyTable.Frequency[0];
                    RootNode           = new TreeNode();
                    RootNode.Left      = RootNode.Right = TempNode;
                }
                OrphanNodes.Clear();
                OrphanNodes = null;
            }
Example #2
0
        /// <summary> записать шапук </summary>
        private void WriteHeader(Stream St, FrequencyTable frequencyTable, long OriginalSize,
                                 Byte version, Byte ComplementsBits)
        {
            FileHeader Header = new FileHeader(version, frequencyTable, ref OriginalSize,
                                               ComplementsBits);

            BinFormat.Serialize(St, Header);
        }
Example #3
0
        /// <summary> Сканирование на наличие повторяющихся байтов и в соответствии с ними построение таблицы частот. </summary>
        /// <param name="DataSource">Поток бит.</param>
        private FrequencyTable BuildFrequencyTable(Stream DataSource)
        {
            long           OriginalPosition = DataSource.Position;
            FrequencyTable frequencyTable   = new FrequencyTable();

            IsByteExist = new bool[256];

            Byte bTemp;

            // Подсчет байтов и сохранение их
            for (long i = 0; i < DataSource.Length; ++i)
            {
                bTemp = (Byte)DataSource.ReadByte();
                if (IsByteExist[bTemp]) //Если байт был найден, иначе новый
                {
                    AmountList[ByteLocation[bTemp]] = (uint)AmountList[ByteLocation[bTemp]] + 1;
                }
                else
                {
                    IsByteExist[bTemp]  = true; //найден
                    ByteLocation[bTemp] = (Byte)BytesList.Count;
                    AmountList.Add(1u);
                    BytesList.Add(bTemp);
                }
            }
            int ArraySize = BytesList.Count;

            frequencyTable.FoundBytes = new byte[ArraySize];
            frequencyTable.Frequency  = new uint[ArraySize];
            short ArraysSize = (short)ArraySize;

            for (short i = 0; i < ArraysSize; ++i)
            {
                frequencyTable.FoundBytes[i] = (Byte)BytesList[i];
                frequencyTable.Frequency[i]  = (uint)AmountList[i];
            }
            SortArrays(frequencyTable.Frequency, frequencyTable.FoundBytes, ArraysSize);

            //очистка ресурсов
            IsByteExist = null;
            BytesList.Clear();
            AmountList.Clear();
            DataSource.Seek(OriginalPosition, SeekOrigin.Begin);
            return(frequencyTable);
        }
Example #4
0
 /// <summary>конструктор </summary>
 public FileHeader(Byte ver, FrequencyTable T, ref long OrgSize,
                   byte BitsToFill)
 {
     version = ver; frequencyTable = T; OriginalSize = OrgSize; ComplementsBits = BitsToFill;
 }