fork(2) download
  1. //#define FSE 1
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. #include <vector>
  6. #include <numeric>
  7. #include <algorithm>
  8. #ifdef FSE
  9. #include "fse.h"
  10. #endif
  11. using namespace std;
  12.  
  13. enum {
  14. kUncomprSize = 250000,
  15. kUncomprBytes = kUncomprSize * sizeof(uint32_t),
  16. };
  17.  
  18. using Buffer = vector<uint32_t>;
  19.  
  20. unsigned int_log(uint32_t x)
  21. {
  22. return 32 - __builtin_clz(x);
  23. }
  24.  
  25. template<unsigned L>
  26. unsigned elias(const Buffer& deltas)
  27. {
  28. unsigned compr_size = 0;
  29. unsigned top = int_log(*max_element(begin(deltas), end(deltas)));
  30. unsigned bottom = top + 1 - (1 << L);
  31.  
  32. for (auto x: deltas)
  33. {
  34. auto il = max(int_log(x), bottom);
  35. compr_size += L;
  36. compr_size += il - 1;
  37. }
  38.  
  39. return compr_size / 8;
  40. }
  41.  
  42. template<unsigned LSB>
  43. unsigned golomb(const Buffer& deltas)
  44. {
  45. unsigned compr_size = 0;
  46.  
  47. for (auto x: deltas)
  48. {
  49. auto il = int_log(x);
  50. compr_size += LSB;
  51. compr_size += (il > LSB)? 2 * (il - LSB) - 1: 1;
  52. }
  53.  
  54. return compr_size / 8;
  55. }
  56.  
  57. #ifdef FSE
  58. template<unsigned MSB>
  59. unsigned fse(const Buffer& deltas)
  60. {
  61. vector<char> compressible(deltas.size());
  62. transform(begin(deltas), end(deltas), begin(compressible), [](uint32_t x)
  63. {
  64. auto il = int_log(x);
  65. auto res = x << (32 - il);
  66. res >>= 32 - MSB;
  67. res += il << MSB;
  68. return static_cast<char>(res);
  69. });
  70. vector<char> compressed(2 * deltas.size());
  71.  
  72. auto compr_res = FSE_compress(
  73. compressed.data(), compressed.size(),
  74. compressible.data(), compressible.size()
  75. );
  76. if (FSE_isError(compr_res))
  77. {
  78. std::cout << "FSE error: " << FSE_getErrorName(compr_res) << "\n";
  79. }
  80. unsigned compr_size = 8 * static_cast<unsigned>(compr_res);
  81.  
  82. for (auto x: deltas)
  83. {
  84. auto il = int_log(x);
  85. compr_size += (il > MSB)? il - MSB - 1: 0;
  86. }
  87.  
  88. return compr_size / 8;
  89. }
  90. #endif
  91.  
  92. int main() {
  93. Buffer uncompr(kUncomprSize);
  94. Buffer deltas(kUncomprSize);
  95. ifstream in("/dev/random", ios_base::in | ios_base::binary);
  96. in.read(reinterpret_cast<char*>(uncompr.data()), kUncomprBytes);
  97. sort(begin(uncompr), end(uncompr));
  98. adjacent_difference(begin(uncompr), end(uncompr), begin(deltas));
  99.  
  100. std::cout << "Elias4: " << elias<4>(deltas) << '\n';
  101. std::cout << "Elias3: " << elias<3>(deltas) << '\n';
  102. std::cout << "Elias2: " << elias<2>(deltas) << '\n';
  103. std::cout << "Golomb12: " << golomb<12>(deltas) << '\n';
  104. std::cout << "Golomb13: " << golomb<13>(deltas) << '\n';
  105. std::cout << "Golomb14: " << golomb<14>(deltas) << '\n';
  106. std::cout << "Golomb15: " << golomb<15>(deltas) << '\n';
  107. #ifdef FSE
  108. std::cout << "FSE1: " << fse<1>(deltas) << '\n';
  109. std::cout << "FSE2: " << fse<2>(deltas) << '\n';
  110. std::cout << "FSE3: " << fse<3>(deltas) << '\n';
  111. #endif
  112. }
Time limit exceeded #stdin #stdout 5s 5232KB
stdin
Standard input is empty
stdout
Standard output is empty