Ejemplo n.º 1
0
Archivo: LZW.cs Proyecto: zzhi/Algs4Net
        /// <summary>
        /// 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.</summary>
        ///
        public void Compress()
        {
            string    inputChars = input.ReadString();
            TST <int> st         = new TST <int>();

            for (int i = 0; i < R; i++)
            {
                st.Put("" + (char)i, i);
            }
            int code = R + 1; // R is codeword for EOF

            while (inputChars.Length > 0)
            {
                string s = st.LongestPrefixOf(inputChars); // Find max prefix match s.
                output.Write(st[s], W);                    // Print s's encoding.
                int t = s.Length;
                if (t < inputChars.Length && code < L)     // Add s to symbol table.
                {
                    st.Put(inputChars.Substring(0, t + 1), code++);
                }
                inputChars = inputChars.Substring(t);    // Scan past s in input.
            }
            output.Write(R, W);
            output.Close();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 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.</summary>
        ///
        public void Compress()
        {
            // read the input
            string s = input.ReadString();

            char[] inputChars = s.ToCharArray();

            // tabulate frequency counts
            int[] freq = new int[R];
            for (int i = 0; i < inputChars.Length; i++)
            {
                freq[inputChars[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
            output.Write(inputChars.Length);

            // use Huffman code to encode input
            for (int i = 0; i < inputChars.Length; i++)
            {
                string code = st[inputChars[i]];
                for (int j = 0; j < code.Length; j++)
                {
                    if (code[j] == '0')
                    {
                        output.Write(false);
                    }
                    else if (code[j] == '1')
                    {
                        output.Write(true);
                    }
                    else
                    {
                        throw new InvalidOperationException("Illegal state");
                    }
                }
            }

            // close output stream
            output.Close();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 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.</summary>
        ///
        public void Compress()
        {
            string s = input.ReadString();
            int    N = s.Length;

            output.Write(N);

            // Write two-bit code for char.
            for (int i = 0; i < N; i++)
            {
                int d = DNA.ToIndex(s[i]);
                output.Write(d, 2);
            }
            output.Close();
            input.Close();
        }