Exemplo n.º 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();
    }
Exemplo n.º 2
0
    void TSTStart()
    {
        string[] strs = txt.text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
        // build symbol table from standard input
        TST <int> st = new TST <int>();

        for (int i = 0; i < strs.Length; i++)
        {
            string key = strs[i];
            st.Put(key, i);
        }

        // print results
        if (st.size() < 100)
        {
            print("keys(\"\"):");
            foreach (string key in st.keys())
            {
                print(key + " " + st.Get(key));
            }
        }

        print("longestPrefixOf(\"shellsort\"):");
        print(st.longestPrefixOf("shellsort"));


        print("longestPrefixOf(\"shell\"):");
        print(st.longestPrefixOf("shell"));


        print("keysWithPrefix(\"shor\"):");
        foreach (string s in st.keysWithPrefix("shor"))
        {
            print(s);
        }


        print("keysThatMatch(\".he.l.\"):");
        foreach (string s in st.keysThatMatch(".he.l."))
        {
            print(s);
        }
    }
Exemplo n.º 3
0
Arquivo: LZW.cs Projeto: PajLe/paa-lab
        public LZW(string inputFileName, string outputFileName)
        {
            _fileToDecode = outputFileName;

            using (BinaryReader br = new BinaryReader(File.Open(inputFileName, FileMode.Open)))
            {
                using (BinaryWriter bw = new BinaryWriter(File.Open(outputFileName, FileMode.Create)))
                {
                    TST <uint>    st         = new TST <uint>();
                    StringBuilder inputChars = new StringBuilder();
                    //br.BaseStream.Position = 0;
                    while (br.BaseStream.Position != br.BaseStream.Length)
                    {
                        inputChars.Append(Convert.ToChar(br.ReadByte()));
                    }
                    string input = inputChars.ToString();
                    _inputLength = input.Length;

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

                    int inputStartIndex = 0;
                    while (_inputLength > inputStartIndex)
                    {
                        string s = st.longestPrefixOf(input, inputStartIndex); // Find max prefix match s.

                        bw.WriteIntBits(st.get(s), W);                         // Print s's code.
                        int t = s.Length;
                        if (t < _inputLength - inputStartIndex && code < L)
                        {
                            st.put(input.Substring(inputStartIndex, t + 1), code++); // Add s to symbol table.
                        }
                        inputStartIndex += t;                                        // instead of the slow substring operation
                    }

                    bw.WriteIntBits(R, W);
                }
            }
        }