Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- None 1.0
- Deflate 0.50
- Filtered 0.34
- BZip2 0.11
- Lzma 0.06
- 1-100, 110-160
- 0,99,8,50
- 00 63 08 32
- 299 = 100101100 = (in blocks of 7): 0000010 0101100
- 1 0101100 (leading one since continuation)
- 0 0000010 (leading zero as no more)
- AC 02
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- static class Program
- {
- static void Main()
- {
- var data = new List<int>();
- data.AddRange(Enumerable.Range(1, 100));
- data.AddRange(Enumerable.Range(110, 51));
- int[] arr = data.ToArray(), arr2;
- using (MemoryStream ms = new MemoryStream())
- {
- Encode(ms, arr);
- ShowRaw(ms.GetBuffer(), (int)ms.Length);
- ms.Position = 0; // rewind to read it...
- arr2 = Decode(ms);
- }
- }
- static void ShowRaw(byte[] buffer, int len)
- {
- for (int i = 0; i < len; i++)
- {
- Console.Write(buffer[i].ToString("X2"));
- }
- Console.WriteLine();
- }
- static int[] Decode(Stream stream)
- {
- var list = new List<int>();
- uint skip, take;
- int last = 0;
- while (TryDecodeUInt32(stream, out skip)
- && TryDecodeUInt32(stream, out take))
- {
- last += (int)skip+1;
- for(uint i = 0 ; i <= take ; i++) {
- list.Add(last++);
- }
- }
- return list.ToArray();
- }
- static int Encode(Stream stream, int[] data)
- {
- if (data.Length == 0) return 0;
- byte[] buffer = new byte[10];
- int last = -1, len = 0;
- for (int i = 0; i < data.Length; )
- {
- int gap = data[i] - 2 - last, size = 0;
- while (++i < data.Length && data[i] == data[i - 1] + 1) size++;
- last = data[i - 1];
- len += EncodeUInt32((uint)gap, buffer, stream)
- + EncodeUInt32((uint)size, buffer, stream);
- }
- return len;
- }
- public static int EncodeUInt32(uint value, byte[] buffer, Stream stream)
- {
- int count = 0, index = 0;
- do
- {
- buffer[index++] = (byte)((value & 0x7F) | 0x80);
- value >>= 7;
- count++;
- } while (value != 0);
- buffer[index - 1] &= 0x7F;
- stream.Write(buffer, 0, count);
- return count;
- }
- public static bool TryDecodeUInt32(Stream source, out uint value)
- {
- int b = source.ReadByte();
- if (b < 0)
- {
- value = 0;
- return false;
- }
- if ((b & 0x80) == 0)
- {
- // single-byte
- value = (uint)b;
- return true;
- }
- int shift = 7;
- value = (uint)(b & 0x7F);
- bool keepGoing;
- int i = 0;
- do
- {
- b = source.ReadByte();
- if (b < 0) throw new EndOfStreamException();
- i++;
- keepGoing = (b & 0x80) != 0;
- value |= ((uint)(b & 0x7F)) << shift;
- shift += 7;
- } while (keepGoing && i < 4);
- if (keepGoing && i == 4)
- {
- throw new OverflowException();
- }
- return true;
- }
- }
- void
- compress(int *out, int *in)
- {
- while(*in) {
- *out++ = *in;
- while(in[0]+1 == in[1])
- ++in;
- *out++ = *in++;
- }
- *out++ = 0;
- }
Add Comment
Please, Sign In to add comment