private static HuffmanCoder CreateFromText(string text) { var leafNodes = text.ToCharArray() .GroupBy(c => c) .Select(g => new TreeNode(g.Key, ((double)g.Count()) / text.Length)); return(HuffmanCoder.Create(leafNodes)); }
public static byte[] Encode(string text) { var encoder = HuffmanCoder.CreateFromText(text); var signature = encoder.ToSignature(); var contentLength = text.Length; var content = text.SelectMany(c => encoder.EncodingTable[c]); var headerBytes = Encoding.UTF8.GetBytes($"{signature}\n{contentLength}\n"); var contentBytes = Utilities.BoolsToBytes(content); return(headerBytes.Concat(contentBytes).ToArray()); }
private static HuffmanCoder CreateFromSignature(string signature) { var leafNodes = signature.Split("|^").Select(s => { var ss = s.Split(",^"); var c = Utilities.StrToChar(ss[0]); var probValue = double.Parse(ss[1]); return(new TreeNode(c, probValue)); }); return(HuffmanCoder.Create(leafNodes)); }
public static string Decode(byte[] encodedBytes) { using (var ms = new MemoryStream(encodedBytes)) using (var sr = new StreamReader(ms)) { var signature = sr.ReadLine(); var contentLength = sr.ReadLine(); var byteCount = Encoding.UTF8.GetByteCount($"{signature}\n{contentLength}\n"); ms.Position = byteCount; var encodedContent = new byte[ms.Length - ms.Position]; ms.Read(encodedContent, 0, encodedContent.Length); var huffman = HuffmanCoder.CreateFromSignature(signature); return(huffman.Decode(encodedContent, int.Parse(contentLength))); } }
static void Main(string[] args) { var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); var inputFile = Path.Combine(path, "ulysses.txt"); var encodedOutputFile = Path.Combine(path, "ulysses-encoded.txt"); var decodedOutputFile = Path.Combine(path, "ulysses-decoded.txt"); var ulysses = File.ReadAllText(inputFile); // Encode the input file var encoded = HuffmanCoder.Encode(ulysses); File.WriteAllBytes(encodedOutputFile, encoded); // Read in the output file and ensure it can be decoded var decoded = HuffmanCoder.Decode(File.ReadAllBytes(encodedOutputFile)); File.WriteAllText(decodedOutputFile, decoded); // Returns true yay var same = string.Equals(ulysses, decoded); }