Esempio n. 1
0
        /// <summary>
        /// Reads a sequence of bits that represents a Huffman-compressed message from
        /// standard input; expands them; and writes the results to standard output.</summary>
        ///
        public void Expand()
        {
            // read in Huffman trie from input stream
            Node root = readTrie();

            // number of bytes to write
            int length = input.ReadInt();

            // decode using the Huffman trie
            for (int i = 0; i < length; i++)
            {
                Node x = root;
                while (!x.IsLeaf)
                {
                    bool bit = input.ReadBoolean();
                    if (bit)
                    {
                        x = x.right;
                    }
                    else
                    {
                        x = x.left;
                    }
                }
                output.Write(x.ch, 8);
            }
            output.Close();
        }
Esempio n. 2
0
File: LZW.cs Progetto: zzhi/Algs4Net
        /// <summary>
        /// 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.</summary>
        ///
        public 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 = input.ReadInt(W);

            if (codeword == R)
            {
                return;                    // expanded message is empty string
            }
            string val = st[codeword];

            while (true)
            {
                output.Write(val);
                codeword = input.ReadInt(W);
                if (codeword == R)
                {
                    break;
                }
                string s = st[codeword];
                if (i == codeword)
                {
                    s = val + val[0];          // special case hack
                }
                if (i < L)
                {
                    st[i++] = val + s[0];
                }
                val = s;
            }
            output.Close();
        }
Esempio n. 3
0
        /// <summary>
        /// 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.</summary>
        ///
        public void Expand()
        {
            int N = input.ReadInt();

            // Read two bits; write char.
            for (int i = 0; i < N; i++)
            {
                char c = input.ReadChar(2);
                output.Write(DNA.ToChar(c), 8);
            }
            output.Close();
            input.Close();
        }
Esempio n. 4
0
        /// <summary>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.</summary>
        ///
        public void Expand()
        {
            bool b = false;

            while (!input.IsEmpty)
            {
                int run = input.ReadInt(LG_R);
                for (int i = 0; i < run; i++)
                {
                    output.Write(b);
                }
                b = !b;
            }
            output.Close();
        }