Example #1
0
 // 先序读取构造一颗huffman树
 private static HuffmanNode ReadTrie(BitVirtualSteam bvStream, ref int index)
 {
     if (bvStream.ReadBoolean())
     {
         //byte b = BitsToChar(bits, ref index);
         return(new HuffmanNode(bvStream.ReadChar(), 0, null, null));
     }
     return(new HuffmanNode('\0', 0, ReadTrie(bvStream, ref index), ReadTrie(bvStream, ref index)));
 }
Example #2
0
 private static void WriteTrie(BitVirtualSteam bvStream, HuffmanNode x)
 {
     if (x.isLeaf())
     {
         bvStream.Write(true);
         bvStream.Write(x.Ch);
         return;
     }
     bvStream.Write(false);
     WriteTrie(bvStream, x.Left);
     WriteTrie(bvStream, x.Right);
 }
Example #3
0
        // 返回bytes
        public static byte[] Compress(string text)
        {
            BitVirtualSteam bvStream = new BitVirtualSteam();

            char[] chars = text.ToCharArray();
            // 统计频次
            int[] freqs = new int[R];
            for (int i = 0; i < chars.Length; i++)
            {
                freqs[chars[i]]++;
            }
            HuffmanNode root = BuildTrie(freqs);

            // 构造编译表 每个字母 对应一个 code 字符串
            string[] st = new string[R];
            BuildCode(st, root, "");

            List <bool> bits = new List <bool>();

            WriteTrie(bvStream, root);
            bvStream.Write(text.Length);
            // var lenBytes = BitConverter.GetBytes(text.Length);

            // BitArray array = new BitArray(lenBytes);
            // for (int i = 0; i < array.Length; i++)
            // {
            //     bits.Add(array[i]);
            // }

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

            return(bvStream.GetBytes());
        }
Example #4
0
        static void Main(string[] args)
        {
            BitVirtualSteam bvStream = new BitVirtualSteam();

            // 12 bit (0,4096)
            bvStream.Write(4095, 12);
            int v = bvStream.ReadInt(12);

            var    bytes = HuffmanTrie.Compress("ABRACADABRA!");
            string str   = HuffmanTrie.Expand(bytes);

            //HuffmanTrie.Compress("it was the best of times it was the worst of times !");
            byte[] lzwBytes = LZW.Compress("ABRACADABRABRABRA");
            string lzwText  = LZW.Expand(lzwBytes);

            Console.WriteLine("Hello World!");
        }
Example #5
0
        public static string Expand(byte[] bytes)
        {
            int             index    = 0;
            BitArray        bits     = new BitArray(bytes);
            BitVirtualSteam bvStream = new BitVirtualSteam(bytes);
            HuffmanNode     root     = ReadTrie(bvStream, ref index);
            BitArray        lenbs    = new BitArray(32);

            for (int i = 0; i < 32; i++)
            {
                lenbs[i] = bits[index++];
            }
            byte[] lenBytes = new byte[4];
            lenbs.CopyTo(lenBytes, 0);
            //int N = BitConverter.ToInt32(lenBytes);
            int N = bvStream.ReadInt();

            char[] chars = new char[N];
            for (int i = 0; i < N; i++)
            {
                HuffmanNode x = root;
                while (!x.isLeaf())
                {
                    //一直往前读取
                    if (bvStream.ReadBoolean())
                    {
                        x = x.Right;
                    }
                    else
                    {
                        x = x.Left;
                    }
                }
                chars[i] = x.Ch;
            }
            return(string.Join("", chars));
        }