code_junkie

Best Compression algorithm for a sequence of integers

Nov 12th, 2011
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. None 1.0
  2. Deflate 0.50
  3. Filtered 0.34
  4. BZip2 0.11
  5. Lzma 0.06
  6.  
  7. 1-100, 110-160
  8.  
  9. 0,99,8,50
  10.  
  11. 00 63 08 32
  12.  
  13. 299 = 100101100 = (in blocks of 7): 0000010 0101100
  14.  
  15. 1 0101100 (leading one since continuation)
  16. 0 0000010 (leading zero as no more)
  17.  
  18. AC 02
  19.  
  20. using System;
  21. using System.Collections.Generic;
  22. using System.IO;
  23. using System.Linq;
  24. static class Program
  25. {
  26. static void Main()
  27. {
  28. var data = new List<int>();
  29. data.AddRange(Enumerable.Range(1, 100));
  30. data.AddRange(Enumerable.Range(110, 51));
  31. int[] arr = data.ToArray(), arr2;
  32.  
  33. using (MemoryStream ms = new MemoryStream())
  34. {
  35. Encode(ms, arr);
  36. ShowRaw(ms.GetBuffer(), (int)ms.Length);
  37. ms.Position = 0; // rewind to read it...
  38. arr2 = Decode(ms);
  39. }
  40. }
  41. static void ShowRaw(byte[] buffer, int len)
  42. {
  43. for (int i = 0; i < len; i++)
  44. {
  45. Console.Write(buffer[i].ToString("X2"));
  46. }
  47. Console.WriteLine();
  48. }
  49. static int[] Decode(Stream stream)
  50. {
  51. var list = new List<int>();
  52. uint skip, take;
  53. int last = 0;
  54. while (TryDecodeUInt32(stream, out skip)
  55. && TryDecodeUInt32(stream, out take))
  56. {
  57. last += (int)skip+1;
  58. for(uint i = 0 ; i <= take ; i++) {
  59. list.Add(last++);
  60. }
  61. }
  62. return list.ToArray();
  63. }
  64. static int Encode(Stream stream, int[] data)
  65. {
  66. if (data.Length == 0) return 0;
  67. byte[] buffer = new byte[10];
  68. int last = -1, len = 0;
  69. for (int i = 0; i < data.Length; )
  70. {
  71. int gap = data[i] - 2 - last, size = 0;
  72. while (++i < data.Length && data[i] == data[i - 1] + 1) size++;
  73. last = data[i - 1];
  74. len += EncodeUInt32((uint)gap, buffer, stream)
  75. + EncodeUInt32((uint)size, buffer, stream);
  76. }
  77. return len;
  78. }
  79. public static int EncodeUInt32(uint value, byte[] buffer, Stream stream)
  80. {
  81. int count = 0, index = 0;
  82. do
  83. {
  84. buffer[index++] = (byte)((value & 0x7F) | 0x80);
  85. value >>= 7;
  86. count++;
  87. } while (value != 0);
  88. buffer[index - 1] &= 0x7F;
  89. stream.Write(buffer, 0, count);
  90. return count;
  91. }
  92. public static bool TryDecodeUInt32(Stream source, out uint value)
  93. {
  94. int b = source.ReadByte();
  95. if (b < 0)
  96. {
  97. value = 0;
  98. return false;
  99. }
  100.  
  101. if ((b & 0x80) == 0)
  102. {
  103. // single-byte
  104. value = (uint)b;
  105. return true;
  106. }
  107.  
  108. int shift = 7;
  109.  
  110. value = (uint)(b & 0x7F);
  111. bool keepGoing;
  112. int i = 0;
  113. do
  114. {
  115. b = source.ReadByte();
  116. if (b < 0) throw new EndOfStreamException();
  117. i++;
  118. keepGoing = (b & 0x80) != 0;
  119. value |= ((uint)(b & 0x7F)) << shift;
  120. shift += 7;
  121. } while (keepGoing && i < 4);
  122. if (keepGoing && i == 4)
  123. {
  124. throw new OverflowException();
  125. }
  126. return true;
  127. }
  128. }
  129.  
  130. void
  131. compress(int *out, int *in)
  132. {
  133. while(*in) {
  134. *out++ = *in;
  135. while(in[0]+1 == in[1])
  136. ++in;
  137. *out++ = *in++;
  138. }
  139. *out++ = 0;
  140. }
Add Comment
Please, Sign In to add comment