Esempio 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();
    }
        public void Compress()
        {
            char cnt = (char)0;
            bool b, old = false;

            while (!BinaryStdIn.IsEmpty())
            {
                b = BinaryStdIn.ReadBoolean();
                if (b != old)
                {
                    BinaryStdOut.Write(cnt);
                    cnt = (char)0;
                    old = !old;
                }
                else
                {
                    if (cnt == 255)
                    {
                        BinaryStdOut.Write(cnt);
                        cnt = (char)0;
                        BinaryStdOut.Write(cnt);
                    }
                }
                cnt++;
            }
            BinaryStdOut.Write(cnt);
        }
Esempio n. 3
0
        public void Expand()
        {
            string[] st = new string[L];
            int      i; // 下一个待补全的编码值

            for (i = 0; i < R; i++)
            {
                st[i] = "" + i;
            }
            st[i++] = " ";//EOF
            int    codeword = BinaryStdIn.ReadInt(W);
            string val      = st[codeword];

            while (true)
            {
                BinaryStdOut.Write(val); //输出当前字符串
                codeword = BinaryStdIn.ReadInt(W);
                if (codeword == R)
                {
                    break;
                }
                string s = st[codeword]; //获取下一个编码
                if (i == codeword)
                {
                    s = val + val[0]; //根据上一个字符串首字母得到编码的字符串
                }
                if (i < L)
                {
                    st[i++] = val + s[0]; //为编译表添加新的条目
                }
                val = s;
            }
        }
Esempio n. 4
0
        public static void compress()
        {
            int num  = 0;
            int num2 = 0;

            while (!BinaryStdIn.IsEmpty)
            {
                int num3 = BinaryStdIn.readBoolean() ? 1 : 0;
                if (num3 != num2)
                {
                    BinaryStdOut.write((char)num, 8);
                    num  = 1;
                    num2 = ((num2 != 0) ? 0 : 1);
                }
                else
                {
                    if (num == 255)
                    {
                        BinaryStdOut.write((char)num, 8);
                        num = 0;
                        BinaryStdOut.write((char)num, 8);
                    }
                    num = (int)((ushort)(num + 1));
                }
            }
            BinaryStdOut.write((char)num, 8);
            BinaryStdOut.close();
        }
Esempio n. 5
0
 /**/
 public static void main(string[] strarr)
 {
     while (!BinaryStdIn.IsEmpty)
     {
         int ch = (int)BinaryStdIn.readChar();
         BinaryStdOut.write((char)ch);
     }
     BinaryStdOut.flush();
 }
Esempio n. 6
0
 /**
  * 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.
  */
 public static void expand() { 
     boolean b = false; 
     while (!BinaryStdIn.isEmpty()) {
         int run = BinaryStdIn.readInt(LG_R);
         for (int i = 0; i < run; i++)
             BinaryStdOut.write(b);
         b = !b;
     }
     BinaryStdOut.close();
 }
Esempio n. 7
0
 /**
  * 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.
  */
 public static void expand() {
     Alphabet DNA = Alphabet.DNA;
     int n = BinaryStdIn.readInt();
     // Read two bits; write char. 
     for (int i = 0; i < n; i++) {
         char c = BinaryStdIn.readChar(2);
         BinaryStdOut.write(DNA.toChar(c), 8);
     }
     BinaryStdOut.close();
 }
Esempio n. 8
0
 // write bitstring-encoded trie to standard output
 private static void writeTrie(Node x) {
     if (x.isLeaf()) {
         BinaryStdOut.write(true);
         BinaryStdOut.write(x.ch, 8);
         return;
     }
     BinaryStdOut.write(false);
     writeTrie(x.left);
     writeTrie(x.right);
 }
    /**/
    public static void main(string[] strarr)
    {
        string    str       = strarr[0];
        BinaryOut binaryOut = new BinaryOut(str);
        BinaryIn  binaryIn  = new BinaryIn();

        while (!binaryIn.IsEmpty)
        {
            int ch = (int)binaryIn.readChar();
            binaryOut.write((char)ch);
        }
        binaryOut.flush();
    }

    static BinaryOut()
    {
        BinaryOut.s_assertionsDisabled = !ClassLiteral <BinaryOut> .Value.desiredAssertionStatus();
    }
}

public sealed class BinaryStdIn
{
    private static BufferedInputStream @in;
    private const int EOF = -1;
    private static int buffer;
    private static int N;


    public static bool IsEmpty
    {
        return(BinaryStdIn.buffer == -1);
    }


    public static bool readBoolean()
    {
        if (BinaryStdIn.IsEmpty)
        {
            string arg_11_0 = "Reading from empty input stream";

            throw new RuntimeException(arg_11_0);
        }
        BinaryStdIn.N--;
        int result = ((BinaryStdIn.buffer >> BinaryStdIn.N & 1) == 1) ? 1 : 0;

        if (BinaryStdIn.N == 0)
        {
            BinaryStdIn.fillBuffer();
        }
        return(result != 0);
    }

    public static string readString()
    {
        if (BinaryStdIn.IsEmpty)
        {
            string arg_11_0 = "Reading from empty input stream";

            throw new RuntimeException(arg_11_0);
        }
        StringBuilder stringBuilder = new StringBuilder();

        while (!BinaryStdIn.IsEmpty)
        {
            int c = (int)BinaryStdIn.readChar();
            stringBuilder.append((char)c);
        }
        return(stringBuilder.toString());
    }

    public static int readInt()
    {
        int num = 0;

        for (int i = 0; i < 4; i++)
        {
            int num2 = (int)BinaryStdIn.readChar();
            num <<= 8;
            num  |= num2;
        }
        return(num);
    }

    public static char readChar(int i)
    {
        if (i < 1 || i > 16)
        {
            string arg_28_0 = new StringBuilder().append("Illegal value of r = ").append(i).toString();

            throw new RuntimeException(arg_28_0);
        }
        if (i == 8)
        {
            return(BinaryStdIn.readChar());
        }
        int num = 0;

        for (int j = 0; j < i; j++)
        {
            num = (int)((ushort)(num << 1));
            int num2 = BinaryStdIn.readBoolean() ? 1 : 0;
            if (num2 != 0)
            {
                num = (int)((ushort)(num | 1));
            }
        }
        return((char)num);
    }

    public static char readChar()
    {
        if (BinaryStdIn.IsEmpty)
        {
            string arg_11_0 = "Reading from empty input stream";

            throw new RuntimeException(arg_11_0);
        }
        int num;

        if (BinaryStdIn.N == 8)
        {
            num = BinaryStdIn.buffer;
            BinaryStdIn.fillBuffer();
            return((char)(num & 255));
        }
        num   = BinaryStdIn.buffer;
        num <<= 8 - BinaryStdIn.N;
        int n = BinaryStdIn.N;

        BinaryStdIn.fillBuffer();
        if (BinaryStdIn.IsEmpty)
        {
            string arg_62_0 = "Reading from empty input stream";

            throw new RuntimeException(arg_62_0);
        }
        BinaryStdIn.N = n;
        num          |= (int)((uint)BinaryStdIn.buffer >> BinaryStdIn.N);
        return((char)(num & 255));
    }

    public static int readInt(int i)
    {
        if (i < 1 || i > 32)
        {
            string arg_28_0 = new StringBuilder().append("Illegal value of r = ").append(i).toString();

            throw new RuntimeException(arg_28_0);
        }
        if (i == 32)
        {
            return(BinaryStdIn.readInt());
        }
        int num = 0;

        for (int j = 0; j < i; j++)
        {
            num <<= 1;
            int num2 = BinaryStdIn.readBoolean() ? 1 : 0;
            if (num2 != 0)
            {
                num |= 1;
            }
        }
        return(num);
    }

    private static void fillBuffer()
    {
        try
        {
            BinaryStdIn.buffer = [email protected]();
            BinaryStdIn.N      = 8;
        }
        catch (IOException arg_19_0)
        {
            goto IL_1D;
        }
        return;

IL_1D:
        [email protected]("EOF");
        BinaryStdIn.buffer = -1;
        BinaryStdIn.N      = -1;
    }

    public static long readLong()
    {
        long num = 0L;

        for (int i = 0; i < 8; i++)
        {
            int num2 = (int)BinaryStdIn.readChar();
            num <<= 8;
            num  |= (long)num2;
        }
        return(num);
    }

    private BinaryStdIn()
    {
    }

    public static void close()
    {
        IOException ex;

        try
        {
            [email protected]();
        }
        catch (IOException arg_0F_0)
        {
            ex = ByteCodeHelper.MapException <IOException>(arg_0F_0, ByteCodeHelper.MapFlags.NoRemapping);
            goto IL_19;
        }
        return;

IL_19:
        IOException @this = ex;

        Throwable.instancehelper_printStackTrace(@this);
        string arg_2F_0 = "Could not close BinaryStdIn";

        throw new RuntimeException(arg_2F_0);
    }

    public static short readShort()
    {
        int num = 0;

        for (int i = 0; i < 2; i++)
        {
            int num2 = (int)BinaryStdIn.readChar();
            num = (int)((short)(num << 8));
            num = (int)((short)(num | num2));
        }
        return((short)num);
    }

    public static double readDouble()
    {
        DoubleConverter doubleConverter;

        return(DoubleConverter.ToDouble(BinaryStdIn.readLong(), ref doubleConverter));
    }

    public static float readFloat()
    {
        FloatConverter floatConverter;

        return(FloatConverter.ToFloat(BinaryStdIn.readInt(), ref floatConverter));
    }

    public static byte readByte()
    {
        int num = (int)BinaryStdIn.readChar();

        return((byte)((sbyte)(num & 255)));
    }

    /**//**/
    public static void main(string[] strarr)
    {
        while (!BinaryStdIn.IsEmpty)
        {
            int ch = (int)BinaryStdIn.readChar();
            BinaryStdOut.write((char)ch);
        }
        BinaryStdOut.flush();
    }

    static BinaryStdIn()
    {
        BufferedInputStream.__ <clinit>();
        BinaryStdIn.@in = new BufferedInputStream(System.@in);
        BinaryStdIn.fillBuffer();
    }
}

public sealed class BinaryStdOut
{
    private static BufferedOutputStream @out;
    private static int buffer;
    private static int N;
    //[Modifiers(Modifiers.Static | Modifiers.Final | Modifiers.Synthetic)]
    internal static bool s_assertionsDisabled;



    public static void write(int i)
    {
        BinaryStdOut.writeByte((int)((uint)i >> 24 & 255u));
        BinaryStdOut.writeByte((int)((uint)i >> 16 & 255u));
        BinaryStdOut.writeByte((int)((uint)i >> 8 & 255u));
        BinaryStdOut.writeByte((int)((uint)i >> 0 & 255u));
    }

    public static void write(int i1, int i2)
    {
        if (i2 == 32)
        {
            BinaryStdOut.write(i1);
            return;
        }
        if (i2 < 1 || i2 > 32)
        {
            string arg_36_0 = new StringBuilder().append("Illegal value for r = ").append(i2).toString();

            throw new RuntimeException(arg_36_0);
        }
        if (i1 < 0 || i1 >= 1 << i2)
        {
            string arg_78_0 = new StringBuilder().append("Illegal ").append(i2).append("-bit char = ").append(i1).toString();

            throw new RuntimeException(arg_78_0);
        }
        for (int j = 0; j < i2; j++)
        {
            int num = (((uint)i1 >> i2 - j - 1 & 1u) == 1u) ? 1 : 0;
            BinaryStdOut.writeBit(num != 0);
        }
    }

    public static void close()
    {
        BinaryStdOut.flush();
        IOException ex;

        try
        {
            [email protected]();
        }
        catch (IOException arg_14_0)
        {
            ex = ByteCodeHelper.MapException <IOException>(arg_14_0, ByteCodeHelper.MapFlags.NoRemapping);
            goto IL_1E;
        }
        return;

IL_1E:
        IOException @this = ex;

        Throwable.instancehelper_printStackTrace(@this);
    }

    public static void write(char ch, int i)
    {
        if (i == 8)
        {
            BinaryStdOut.write(ch);
            return;
        }
        if (i < 1 || i > 16)
        {
            string arg_37_0 = new StringBuilder().append("Illegal value for r = ").append(i).toString();

            throw new RuntimeException(arg_37_0);
        }
        if (ch < '\0' || (int)ch >= 1 << i)
        {
            string arg_79_0 = new StringBuilder().append("Illegal ").append(i).append("-bit char = ").append(ch).toString();

            throw new RuntimeException(arg_79_0);
        }
        for (int j = 0; j < i; j++)
        {
            int num = ((ch >> (i - j - 1 & 31) & '\u0001') == '\u0001') ? 1 : 0;
            BinaryStdOut.writeBit(num != 0);
        }
    }

    public static void write(bool b)
    {
        BinaryStdOut.writeBit(b);
    }

    public static void write(string str)
    {
        for (int i = 0; i < java.lang.String.instancehelper_length(str); i++)
        {
            BinaryStdOut.write(java.lang.String.instancehelper_charAt(str, i));
        }
    }

    public static void write(char ch)
    {
        if (ch < '\0' || ch >= 'Ā')
        {
            string arg_2D_0 = new StringBuilder().append("Illegal 8-bit char = ").append(ch).toString();

            throw new RuntimeException(arg_2D_0);
        }
        BinaryStdOut.writeByte((int)ch);
    }

    public static void flush()
    {
        BinaryStdOut.clearBuffer();
        IOException ex;

        try
        {
            [email protected]();
        }
        catch (IOException arg_14_0)
        {
            ex = ByteCodeHelper.MapException <IOException>(arg_14_0, ByteCodeHelper.MapFlags.NoRemapping);
            goto IL_1E;
        }
        return;

IL_1E:
        IOException @this = ex;

        Throwable.instancehelper_printStackTrace(@this);
    }

    private static void clearBuffer()
    {
        if (BinaryStdOut.N == 0)
        {
            return;
        }
        if (BinaryStdOut.N > 0)
        {
            BinaryStdOut.buffer <<= 8 - BinaryStdOut.N;
        }
        IOException ex;

        try
        {
            [email protected](BinaryStdOut.buffer);
        }
        catch (IOException arg_39_0)
        {
            ex = ByteCodeHelper.MapException <IOException>(arg_39_0, ByteCodeHelper.MapFlags.NoRemapping);
            goto IL_43;
        }
        goto IL_4F;
IL_43:
        IOException @this = ex;

        Throwable.instancehelper_printStackTrace(@this);
IL_4F:
        BinaryStdOut.N      = 0;
        BinaryStdOut.buffer = 0;
    }

    private static void writeBit(bool flag)
    {
        BinaryStdOut.buffer <<= 1;
        if (flag)
        {
            BinaryStdOut.buffer |= 1;
        }
        BinaryStdOut.N++;
        if (BinaryStdOut.N == 8)
        {
            BinaryStdOut.clearBuffer();
        }
    }

    private static void writeByte(int num)
    {
        if (!BinaryStdOut.s_assertionsDisabled && (num < 0 || num >= 256))
        {
            throw new AssertionError();
        }
        if (BinaryStdOut.N == 0)
        {
            IOException ex;
            try
            {
                [email protected](num);
            }
            catch (IOException arg_35_0)
            {
                ex = ByteCodeHelper.MapException <IOException>(arg_35_0, ByteCodeHelper.MapFlags.NoRemapping);
                goto IL_3F;
            }
            return;

IL_3F:
            IOException @this = ex;
            Throwable.instancehelper_printStackTrace(@this);
            return;
        }
        for (int i = 0; i < 8; i++)
        {
            int num2 = (((uint)num >> 8 - i - 1 & 1u) == 1u) ? 1 : 0;
            BinaryStdOut.writeBit(num2 != 0);
        }
    }

    public static void write(long l)
    {
        BinaryStdOut.writeByte((int)((ulong)l >> 56 & (ulong)255));
        BinaryStdOut.writeByte((int)((ulong)l >> 48 & (ulong)255));
        BinaryStdOut.writeByte((int)((ulong)l >> 40 & (ulong)255));
        BinaryStdOut.writeByte((int)((ulong)l >> 32 & (ulong)255));
        BinaryStdOut.writeByte((int)((ulong)l >> 24 & (ulong)255));
        BinaryStdOut.writeByte((int)((ulong)l >> 16 & (ulong)255));
        BinaryStdOut.writeByte((int)((ulong)l >> 8 & (ulong)255));
        BinaryStdOut.writeByte((int)((ulong)l >> 0 & (ulong)255));
    }

    private BinaryStdOut()
    {
    }

    public static void write(byte b)
    {
        int num = (int)((sbyte)b);

        BinaryStdOut.writeByte(num & 255);
    }

    public static void write(double d)
    {
        DoubleConverter doubleConverter;

        BinaryStdOut.write(DoubleConverter.ToLong(d, ref doubleConverter));
    }

    public static void write(float f)
    {
        FloatConverter floatConverter;

        BinaryStdOut.write(FloatConverter.ToInt(f, ref floatConverter));
    }

    public static void write(short s)
    {
        BinaryStdOut.writeByte((int)((uint)s >> 8 & 255u));
        BinaryStdOut.writeByte((int)((uint)s >> 0 & 255u));
    }

    public static void write(string str, int i)
    {
        for (int j = 0; j < java.lang.String.instancehelper_length(str); j++)
        {
            BinaryStdOut.write(java.lang.String.instancehelper_charAt(str, j), i);
        }
    }

    /**//**/
    public static void main(string[] strarr)
    {
        int num = Integer.parseInt(strarr[0]);

        for (int i = 0; i < num; i++)
        {
            BinaryStdOut.write(i);
        }
        BinaryStdOut.flush();
    }
 private static void Writetrie(Node x)
 {
     if (x.isLeaf())
     {
         BinaryStdOut.Write(true);
         BinaryStdOut.Write(x.Ch);
         return;
     }
     BinaryStdOut.Write(false);
     Writetrie(x.Left);
     Writetrie(x.Right);
 }
        public void Expand()
        {
            Alphabet DNA = new Alphabet("ACTG");
            int      w   = DNA.LgR();
            int      n   = BinaryStdIn.ReadInt();

            for (int i = 0; i < n; i++)
            {
                var c = BinaryStdIn.ReadChar(w);
                BinaryStdOut.Write(DNA.ToChar(c));
            }
        }
Esempio n. 12
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();
    } 
        public void Compress()
        {
            Alphabet DNA = new Alphabet("ACTG");
            string   s   = BinaryStdIn.ReadString();
            int      n   = s.Length;

            BinaryStdOut.Write(n);
            for (int i = 0; i < n; i++)
            {
                //将字符用双位编码代码来表示
                int d = DNA.ToIndex(s[i]);
                BinaryStdOut.Write(d, DNA.LgR());
            }
        }
        public void Expand()
        {
            bool b = false;

            while (!BinaryStdIn.IsEmpty())
            {
                char cnt = BinaryStdIn.ReadChar();
                for (int i = 0; i < cnt; i++)
                {
                    BinaryStdOut.Write(b);
                }
                b = !b;
            }
        }
Esempio n. 15
0
        public static void expand()
        {
            int num = 0;

            while (!BinaryStdIn.IsEmpty)
            {
                int num2 = BinaryStdIn.readInt(8);
                for (int i = 0; i < num2; i++)
                {
                    BinaryStdOut.write(num != 0);
                }
                num = ((num != 0) ? 0 : 1);
            }
            BinaryStdOut.close();
        }
Esempio n. 16
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();
    } 
Esempio n. 17
0
    /**
     * Reads a sequence of bits that represents a Huffman-compressed message from
     * standard input; expands them; and writes the results to standard output.
     */
    public static void expand() {

        // read in Huffman trie from input stream
        Node root = readTrie(); 

        // number of bytes to write
        int length = BinaryStdIn.readInt();

        // decode using the Huffman trie
        for (int i = 0; i < length; i++) {
            Node x = root;
            while (!x.isLeaf()) {
                boolean bit = BinaryStdIn.readBoolean();
                if (bit) x = x.right;
                else     x = x.left;
            }
            BinaryStdOut.write(x.ch, 8);
        }
        BinaryStdOut.close();
    }
Esempio n. 18
0
    public static void expand()
    {
        string[] array = new string[4096];
        int      i;

        for (i = 0; i < 256; i++)
        {
            array[i] = new StringBuilder().append("").append((char)i).toString();
        }
        string[] arg_44_0 = array;
        int      arg_44_1 = i;

        i++;
        arg_44_0[arg_44_1] = "";
        int    num  = BinaryStdIn.readInt(12);
        string text = array[num];

        while (true)
        {
            BinaryStdOut.write(text);
            num = BinaryStdIn.readInt(12);
            if (num == 256)
            {
                break;
            }
            string text2 = array[num];
            if (i == num)
            {
                text2 = new StringBuilder().append(text).append(java.lang.String.instancehelper_charAt(text, 0)).toString();
            }
            if (i < 4096)
            {
                string[] arg_BE_0 = array;
                int      arg_BE_1 = i;
                i++;
                arg_BE_0[arg_BE_1] = new StringBuilder().append(text).append(java.lang.String.instancehelper_charAt(text2, 0)).toString();
            }
            text = text2;
        }
        BinaryStdOut.close();
    }
Esempio n. 19
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();
    }
Esempio n. 20
0
 /**
  * Reads a sequence of bits from standard input; compresses
  * them using run-length coding with 8-bit run lengths; and writes the
  * results to standard output.
  */
 public static void compress() { 
     char run = 0; 
     boolean old = false;
     while (!BinaryStdIn.isEmpty()) { 
         boolean b = BinaryStdIn.readBoolean();
         if (b != old) {
             BinaryStdOut.write(run, LG_R);
             run = 1;
             old = !old;
         }
         else { 
             if (run == R-1) { 
                 BinaryStdOut.write(run, LG_R);
                 run = 0;
                 BinaryStdOut.write(run, LG_R);
             }
             run++;
         } 
     } 
     BinaryStdOut.write(run, LG_R);
     BinaryStdOut.close();
 }
        public void Expand()
        {
            Node root = ReadTrie();
            int  n    = BinaryStdIn.ReadInt();

            for (int i = 0; i < n; i++)
            {
                var x = root;
                while (!x.isLeaf())
                {
                    if (BinaryStdIn.ReadBoolean())
                    {
                        x = x.Right;
                    }
                    else
                    {
                        x = x.Left;
                    }
                }
                BinaryStdOut.Write(x.Ch);
            }
        }
Esempio n. 22
0
        public void Compress()
        {
            string    input = BinaryStdIn.ReadString();
            TST <int> st    = new TST <int>();

            for (int i = 0; i < R; i++)
            {
                st.Put("" + i, i);
            }
            int code = R + 1; //标记结束

            while (input.Length > 0)
            {
                string s = st.LongestPrefixOf(input); //匹配最长前缀
                BinaryStdOut.Write(st.Get(s), W);
                int t = s.Length;
                if (t < input.Length && code < L) //将s加入符号表
                {
                    st.Put(input.Substring(0, t + 1), code++);
                }
                input = input.Substring(t);
            }
            BinaryStdOut.Write(R, W);
        }
Esempio n. 23
0
    /**
     * 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.
     */
    public static 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 = BinaryStdIn.readInt(W);
        if (codeword == R) return;           // expanded message is empty string
        String val = st[codeword];

        while (true) {
            BinaryStdOut.write(val);
            codeword = BinaryStdIn.readInt(W);
            if (codeword == R) break;
            String s = st[codeword];
            if (i == codeword) s = val + val.charAt(0);   // special case hack
            if (i < L) st[i++] = val + s.charAt(0);
            val = s;
        }
        BinaryStdOut.close();
    }
        public void Compress()
        {
            //读取输入
            string s = BinaryStdIn.ReadString();

            char[] input = s.ToCharArray();
            //统计频率
            int[] freq = new int[R];
            for (int i = 0; i < input.Length; i++)
            {
                freq[input[i]]++;
            }
            Node root = BuildTrie(freq);

            //构造编译表
            string[] st = BuildCode(root);
            Writetrie(root);
            //打印总字符数
            BinaryStdOut.Write(input.Length);

            for (int i = 0; i < input.Length; i++)
            {
                string code = st[input[i]];
                for (int j = 0; j < code.Length; j++)
                {
                    if (code[j] == '1')
                    {
                        BinaryStdOut.Write(true);
                    }
                    else
                    {
                        BinaryStdOut.Write(false);
                    }
                }
            }
        }