Example #1
0
        public void Compress()
        {
            string inputChars = input.ReadString();
            var    st         = new TrieSymbolTable <int>();

            // initialize symbol table
            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);
                output.Write(st[s], W);
                int t = s.Length;
                if (t < inputChars.Length && code < L)
                {
                    // Add s to symbol table
                    st.Put(inputChars.Substring(0, t + 1), code++);
                }
                // Scan past s in input
                inputChars = inputChars.Substring(t);
            }
            output.Write(R, W);
            output.Close();
        }