Exemple #1
0
        /// <summary>
        /// Кодирование блока слов
        /// </summary>
        /// <param name="words">Блок слов определённого размера</param>
        /// <returns>Закодированый блок представленный
        /// списком битов с добавленной длиной блока</returns>
        public List <bool> EncodeBlock(List <string> words)
        {
            List <bool> encodedSource = new List <bool>();

            HuffmanAdaptiveTree huffmanAdaptiveTree = new HuffmanAdaptiveTree();

            //Кодируем блок
            encodedSource = huffmanAdaptiveTree.Encode(words);
            //Добавляем перед закодированной последовательностью её длину
            encodedSource.InsertRange(0, BitOperations.Int32ToListBool(encodedSource.Count));
            //возращаем закодированный блок
            return(encodedSource);
        }
Exemple #2
0
        /// <summary>
        /// Декодирование блока
        /// </summary>
        /// <param name="bits">Последовательность бит, в которой содержиться закодированный блок</param>
        /// <param name="indexStart">Индекс начала блока вместе с его длинной</param>
        /// <param name="indexEnd">Индекс конца блока</param>
        /// <returns>Декодированный блок текста</returns>
        public StringBuilder DecodeBlock(BitArray bits, int indexStart, out int indexEnd)
        {
            int           index     = indexStart;
            StringBuilder textBlock = new StringBuilder();
            //извлекаем длину закодированного блока
            int lenBlock = BitOperations.GetInt32FromBitArray(index, bits);

            index += sizeByte * sizeof(int);
            HuffmanAdaptiveTree huffmanAdaptiveTree = new HuffmanAdaptiveTree();

            //декодируем блок
            textBlock = huffmanAdaptiveTree.DecodeBlock(bits, index, index + lenBlock);
            //изменяем индекс конца блока
            indexEnd = index + lenBlock;

            return(textBlock);
        }