Example #1
0
        public static byte[] EncodeBytes(byte[] source)
        {
            var coder = new HuffmanCoder <byte>();

            coder.Build(source);
            var encoded = coder.Encode(source);

            var freq = coder.GetFrequencies();

            return(BitConverter.GetBytes((short)(freq.Length))
                   .Concat(ToByteArray(freq))
                   .Concat(ToByteArray(encoded))
                   .ToArray());
        }
        static byte[] SaveHuffman(int[] seq, string name)
        {
            var seqdiff = seq.Distinct().Diff((a, b) => b - a).ToArray();
            var coder   = new HuffmanCoder <int>();

            coder.Build(seqdiff);
            var encoded = coder.Encode(seqdiff);
            var freq    = coder.GetFrequencies();
            var seqhuff = BitConverter.GetBytes((short)(freq.Length))
                          .Concat(HuffmanHelper.ToByteArray(freq))
                          .Concat(HuffmanHelper.ToByteArray(encoded))
                          .ToArray();

            seqhuff.WriteToGzip(name + "-huff");
            return(seqhuff);
        }
Example #3
0
        public static byte[] DecodeBytes(byte[] encoded)
        {
            var count = BitConverter.ToInt16(encoded, 0);
            var freq  = new KeyValuePair <byte, int> [count];
            int pos   = 2;

            for (int i = 0; i < count; i++)
            {
                var element  = encoded[pos];
                var elemFreq = BitConverter.ToInt32(encoded, pos + 1);
                freq[i] = new KeyValuePair <byte, int>(element, elemFreq);
                pos    += 5;
            }
            var bits = new BitArray(encoded.Skip(pos).ToArray());

            var coder = new HuffmanCoder <byte>();

            coder.Build(freq);
            return(coder.Decode(bits));
        }