Exemplo n.º 1
0
Arquivo: RLE.cs Projeto: danmoka/Codik
        public string Encode(string sourceText, out double compressionRatio)
        {
            var  answer = new StringBuilder();
            uint blockСounter, charCounter, maxCharCounter;

            blockСounter = charCounter = maxCharCounter = 1;

            for (int i = 1; i < sourceText.Length; i++)
            {
                if (sourceText[i - 1] == sourceText[i])
                {
                    charCounter++;
                }
                else
                {
                    answer.Append(string.Format("({0},{1})", sourceText[i - 1], charCounter));
                    if (charCounter > maxCharCounter)
                    {
                        maxCharCounter = charCounter;
                    }
                    blockСounter++;
                    charCounter = 1;
                }
            }

            answer.Append(string.Format("({0},{1})", sourceText[sourceText.Length - 1], charCounter));
            double charSize = BitHacks.GetRealSizeForNumber(char.MaxValue);

            compressionRatio = (sourceText.Length * 16) /
                               (blockСounter * (BitHacks.GetRealSizeForNumber(maxCharCounter) + charSize));

            return(answer.ToString());
        }
Exemplo n.º 2
0
        public string Encode(string sourceText, out double compressionRatio)
        {
            var codeWords = CreateCodeWords(GetFrequencyDictionary(sourceText), this._numberSystem);
            var code      = new StringBuilder();

            foreach (var symbol in sourceText)
            {
                code.Append(codeWords[symbol]);
            }

            compressionRatio = ((double)sourceText.Length * BitHacks.GetRealSizeForNumber(char.MaxValue))
                               / ((double)BitHacks.GetRealSizeForNumber((uint)Math.Min(codeWords.Count, _numberSystem) - 1)
                                  * code.Length);
            code.Append(codeWords.ToSrtingExtension());
            return(code.ToString());
        }
Exemplo n.º 3
0
        public string Encode(string sourceText, out double compressionRatio)
        {
            var dictionary = new Dictionary <string, int>
            {
                { string.Empty, 0 }
            };

            var answer       = new StringBuilder();
            var buffer       = string.Empty;
            int blockCounter = 0;

            foreach (var symbol in sourceText)
            {
                if (dictionary.ContainsKey(buffer + symbol))
                {
                    buffer += symbol;
                }
                else
                {
                    answer.Append(string.Format("({0},{1})", dictionary[buffer], symbol));
                    dictionary.Add(buffer + symbol, dictionary.Count);
                    buffer = string.Empty;
                    blockCounter++;
                }
            }

            answer.Append(string.Format("({0},{1})", dictionary[buffer], '$'));
            blockCounter++;

            double charSize = BitHacks.GetRealSizeForNumber(char.MaxValue);

            compressionRatio = (sourceText.Length * charSize) /
                               (blockCounter * (charSize + BitHacks.GetRealSizeForNumber((uint)dictionary.Count)));

            return(answer.ToString());
        }