Example #1
0
    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();
    }
Example #2
0
    /**
     * 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();
    } 
Example #3
0
    /**
     * 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();
    } 
Example #4
0
    /**
     * 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();
    }