Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace hoff
- {
- public class Hoffman
- {
- const char EMPTY_CHAR = '\0';
- public Hoffman(string text)
- {
- if (string.IsNullOrEmpty(text))
- {
- throw new Exception("empty text");
- }
- var originText = string.Concat(text.Where(u => !char.IsPunctuation(u)).ToList());
- SetDictionary(originText);
- }
- public string GetEncriptedText(string text)
- {
- return string.Concat(GetEncriptedSymbols(text).ToList());
- }
- public string GetDecriptedText(string text)
- {
- return string.Concat(GetDecriptedSymbols(text).ToList());
- }
- public void PrintDictionary()
- {
- foreach (var item in dictionary)
- {
- Console.WriteLine($"{item.Key} - {item.Value}");
- }
- }
- private Dictionary<char, string> dictionary = new Dictionary<char, string>();
- private int dictionaryMinCodeLength => dictionary.OrderBy(u => u.Value.Length).FirstOrDefault().Value.Length;
- private Dictionary<char, string> SetDictionary(string text)
- {
- var mapKeys = text
- .GroupBy(u => u)
- .Select(u => new Point { Key = u.Key, Count = u.Count() })
- .OrderBy(u => u.Count)
- .ThenBy(u => u.Key)
- .ToList();
- while (mapKeys.Count > 1)
- {
- var child1 = mapKeys[0];
- var child2 = mapKeys[1];
- mapKeys.RemoveAt(0);
- mapKeys.RemoveAt(0);
- mapKeys.Add(new Point
- {
- Count = child1.Count + child2.Count,
- LeftPoint = child1,
- RightPoint = child2
- });
- mapKeys = mapKeys
- .OrderBy(u => u.Count)
- .ToList();
- }
- var parentPoint = mapKeys.FirstOrDefault();
- if (parentPoint == null)
- {
- throw new Exception("can't find parent point");
- }
- SetPointCodes(parentPoint);
- GetPointCodes(parentPoint);
- return dictionary;
- }
- private IEnumerable<string> GetEncriptedSymbols(string text)
- {
- foreach (var item in text)
- yield return dictionary[item];
- }
- private IEnumerable<char> GetDecriptedSymbols(string text)
- {
- var tempLength = dictionaryMinCodeLength;
- for (int i = 0; i < text.Length; i++)
- {
- char key = EMPTY_CHAR;
- var valueLength = dictionaryMinCodeLength;
- while (key == EMPTY_CHAR)
- {
- var code = string.Concat(text.Skip(i).Take(valueLength).ToList());
- key = dictionary.FirstOrDefault(u => u.Value == code).Key;
- if (key == EMPTY_CHAR)
- valueLength++;
- }
- i += (valueLength - 1);
- yield return key;
- }
- }
- private void GetPointCodes(Point parentPoint)
- {
- if (parentPoint.Key != EMPTY_CHAR)
- dictionary.Add(parentPoint.Key, parentPoint.Code);
- if (parentPoint.LeftPoint != null)
- GetPointCodes(parentPoint.LeftPoint);
- if (parentPoint.RightPoint != null)
- GetPointCodes(parentPoint.RightPoint);
- }
- private static void SetPointCodes(Point point)
- {
- if (point.LeftPoint != null)
- {
- point.LeftPoint.Code = point.Code + "0";
- SetPointCodes(point.LeftPoint);
- }
- if (point.RightPoint != null)
- {
- point.RightPoint.Code = point.Code + "1";
- SetPointCodes(point.RightPoint);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement