public override bool Equals(object obj) { if (obj == null) { return(false); } if (obj == this) { return(true); } if (!(obj.GetType() == GetType())) { return(false); } bool equals = false; encoding encode = new encoding('\0', "0"); encode = (encoding)obj; if (this.character == encode.character) { equals = true; } return(equals); }
public void Encode(BinaryTreeNode <E> p, LinkedList <encoding> encode) { if (p != null) { encoding += "0"; Encode(p.Right, encode); if (p.isLeaf()) { encoding encoder = new encoding(p.Data.ToString(), encoding); encode.AddFirst(encoder); } encoding += "1"; Encode(p.Left, encode); if (encoding.Length >= 1) { encoding = encoding.Remove(encoding.Length - 1); } } else { //Should remove the last character from the encoding table if the traversal back tracks to a node it already visted encoding = encoding.Remove(encoding.Length - 1); } }
static void compress(LinkedList <encoding> encoder, String filepath, StreamReader sr) { //checking to see if the file is there if (File.Exists(filepath)) { File.Delete(filepath); } FileStream filestream = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write); BinaryWriter bw = new BinaryWriter(filestream); //Variables byte b = 00000000; byte reset = 00000000; int read = sr.Read(); int position = 7; encoding en = new encoding('\0', "\0"); while (read != -1) { //searching for the encoding string en = new encoding((char)read, "\0"); LinkedListNode <encoding> node = encoder.Find(en); String encode = node.Value.encode; for (int i = 0; i < encode.Length; i++) { if (encode[i] == '1') { //turning on bits b = (byte)(b | (byte)(Math.Pow(2, position))); position--; //check to see if we're at the end of the byte if (position < 0) { bw.Write(b); b = (byte)(b & reset); position = 7; } } else { position--; if (position < 0) { bw.Write(b); b = (byte)(b & reset); position = 7; } } } read = sr.Read(); } bw.Write(b); bw.Close(); }