Ejemplo n.º 1
0
        public byte[] Compress(string input)
        {
            var frequencies = _frequencyCalculator.GetFrequencies(input).ToList();

            _huffmanTree.Build(frequencies);

            var blob = new Blob();

            _pathCache = new Dictionary <char, string>();

            foreach (var character in input)
            {
                _pathCache.TryGetValue(character, out var path);

                if (path == null)
                {
                    path = _huffmanTree.GetPath(character);

                    _pathCache.Add(character, path);

                    blob.Append(path);
                }
                else
                {
                    blob.Append(path);
                }
            }

            var data = new CompressedData
            {
                Data           = blob.ToByteArray(),
                Frequencies    = frequencies,
                OriginalLength = input.Length
            };

            return(data.Save());
        }
Ejemplo n.º 2
0
        private void BuildPathCache(List <CharacterFrequency> frequencies)
        {
            _pathCache = new string[(int)Math.Pow(256, Constants.CharSizeInBytes)];

            frequencies.ForEach(f => _pathCache[f.Character] = _huffmanTree.GetPath(f.Character));
        }