public static void compress() { string text = BinaryStdIn.readString(); TST tST = new TST(); int i; for (i = 0; i < 256; i++) { tST.put(new StringBuilder().append("").append((char)i).toString(), Integer.valueOf(i)); } i = 257; while (java.lang.String.instancehelper_length(text) > 0) { string text2 = tST.longestPrefixOf(text); BinaryStdOut.write(((Integer)tST.get(text2)).intValue(), 12); int num = java.lang.String.instancehelper_length(text2); if (num < java.lang.String.instancehelper_length(text) && i < 4096) { TST arg_A5_0 = tST; string arg_A5_1 = java.lang.String.instancehelper_substring(text, 0, num + 1); int arg_A0_0 = i; i++; arg_A5_0.put(arg_A5_1, Integer.valueOf(arg_A0_0)); } text = java.lang.String.instancehelper_substring(text, num); } BinaryStdOut.write(256, 12); BinaryStdOut.close(); }
public static void compress() { int num = 0; int num2 = 0; while (!BinaryStdIn.IsEmpty) { int num3 = BinaryStdIn.readBoolean() ? 1 : 0; if (num3 != num2) { BinaryStdOut.write((char)num, 8); num = 1; num2 = ((num2 != 0) ? 0 : 1); } else { if (num == 255) { BinaryStdOut.write((char)num, 8); num = 0; BinaryStdOut.write((char)num, 8); } num = (int)((ushort)(num + 1)); } } BinaryStdOut.write((char)num, 8); BinaryStdOut.close(); }
/** * Reads a binary sequence from standard input; converts each two bits * to an 8-bit extended ASCII character over the alphabet { A, C, T, G }; * and writes the results to standard output. */ public static void expand() { Alphabet DNA = Alphabet.DNA; int n = BinaryStdIn.readInt(); // Read two bits; write char. for (int i = 0; i < n; i++) { char c = BinaryStdIn.readChar(2); BinaryStdOut.write(DNA.toChar(c), 8); } BinaryStdOut.close(); }
/** * Reads a sequence of bits from standard input (that are encoded * using run-length encoding with 8-bit run lengths); decodes them; * and writes the results to standard output. */ public static void expand() { boolean b = false; while (!BinaryStdIn.isEmpty()) { int run = BinaryStdIn.readInt(LG_R); for (int i = 0; i < run; i++) BinaryStdOut.write(b); b = !b; } BinaryStdOut.close(); }
/** * Reads a sequence of 8-bit extended ASCII characters over the alphabet * { A, C, T, G } from standard input; compresses them using two bits per * character; and writes the results to standard output. */ public static void compress() { Alphabet DNA = Alphabet.DNA; String s = BinaryStdIn.readString(); int n = s.length(); BinaryStdOut.write(n); // Write two-bit code for char. for (int i = 0; i < n; i++) { int d = DNA.toIndex(s.charAt(i)); BinaryStdOut.write(d, 2); } BinaryStdOut.close(); }
public static void expand() { int num = 0; while (!BinaryStdIn.IsEmpty) { int num2 = BinaryStdIn.readInt(8); for (int i = 0; i < num2; i++) { BinaryStdOut.write(num != 0); } num = ((num != 0) ? 0 : 1); } BinaryStdOut.close(); }
/** * Reads a sequence of 8-bit bytes from standard input; compresses * them using LZW compression with 12-bit codewords; and writes the results * to standard output. */ public static void compress() { String input = BinaryStdIn.readString(); TST<Integer> st = new TST<Integer>(); for (int i = 0; i < R; i++) st.put("" + (char) i, i); int code = R+1; // R is codeword for EOF while (input.length() > 0) { String s = st.longestPrefixOf(input); // Find max prefix match s. BinaryStdOut.write(st.get(s), W); // Print s's encoding. int t = s.length(); if (t < input.length() && code < L) // Add s to symbol table. st.put(input.substring(0, t + 1), code++); input = input.substring(t); // Scan past s in input. } BinaryStdOut.write(R, W); BinaryStdOut.close(); }
/** * Reads a sequence of bits that represents a Huffman-compressed message from * standard input; expands them; and writes the results to standard output. */ public static void expand() { // read in Huffman trie from input stream Node root = readTrie(); // number of bytes to write int length = BinaryStdIn.readInt(); // decode using the Huffman trie for (int i = 0; i < length; i++) { Node x = root; while (!x.isLeaf()) { boolean bit = BinaryStdIn.readBoolean(); if (bit) x = x.right; else x = x.left; } BinaryStdOut.write(x.ch, 8); } BinaryStdOut.close(); }
public static void expand() { string[] array = new string[4096]; int i; for (i = 0; i < 256; i++) { array[i] = new StringBuilder().append("").append((char)i).toString(); } string[] arg_44_0 = array; int arg_44_1 = i; i++; arg_44_0[arg_44_1] = ""; int num = BinaryStdIn.readInt(12); string text = array[num]; while (true) { BinaryStdOut.write(text); num = BinaryStdIn.readInt(12); if (num == 256) { break; } string text2 = array[num]; if (i == num) { text2 = new StringBuilder().append(text).append(java.lang.String.instancehelper_charAt(text, 0)).toString(); } if (i < 4096) { string[] arg_BE_0 = array; int arg_BE_1 = i; i++; arg_BE_0[arg_BE_1] = new StringBuilder().append(text).append(java.lang.String.instancehelper_charAt(text2, 0)).toString(); } text = text2; } BinaryStdOut.close(); }
/** * Reads a sequence of 8-bit bytes from standard input; compresses them * using Huffman codes with an 8-bit alphabet; and writes the results * to standard output. */ public static void compress() { // read the input String s = BinaryStdIn.readString(); char[] input = s.toCharArray(); // tabulate frequency counts int[] freq = new int[R]; for (int i = 0; i < input.length; i++) freq[input[i]]++; // build Huffman trie Node root = buildTrie(freq); // build code table String[] st = new String[R]; buildCode(st, root, ""); // print trie for decoder writeTrie(root); // print number of bytes in original uncompressed message BinaryStdOut.write(input.length); // use Huffman code to encode input for (int i = 0; i < input.length; i++) { String code = st[input[i]]; for (int j = 0; j < code.length(); j++) { if (code.charAt(j) == '0') { BinaryStdOut.write(false); } else if (code.charAt(j) == '1') { BinaryStdOut.write(true); } else throw new IllegalStateException("Illegal state"); } } // close output stream BinaryStdOut.close(); }
/** * Reads a sequence of bits from standard input; compresses * them using run-length coding with 8-bit run lengths; and writes the * results to standard output. */ public static void compress() { char run = 0; boolean old = false; while (!BinaryStdIn.isEmpty()) { boolean b = BinaryStdIn.readBoolean(); if (b != old) { BinaryStdOut.write(run, LG_R); run = 1; old = !old; } else { if (run == R-1) { BinaryStdOut.write(run, LG_R); run = 0; BinaryStdOut.write(run, LG_R); } run++; } } BinaryStdOut.write(run, LG_R); BinaryStdOut.close(); }
/** * Reads a sequence of bit encoded using LZW compression with * 12-bit codewords from standard input; expands them; and writes * the results to standard output. */ public static void expand() { String[] st = new String[L]; int i; // next available codeword value // initialize symbol table with all 1-character strings for (i = 0; i < R; i++) st[i] = "" + (char) i; st[i++] = ""; // (unused) lookahead for EOF int codeword = BinaryStdIn.readInt(W); if (codeword == R) return; // expanded message is empty string String val = st[codeword]; while (true) { BinaryStdOut.write(val); codeword = BinaryStdIn.readInt(W); if (codeword == R) break; String s = st[codeword]; if (i == codeword) s = val + val.charAt(0); // special case hack if (i < L) st[i++] = val + s.charAt(0); val = s; } BinaryStdOut.close(); }