Advertisement
thexiv

C++ Compression

Dec 27th, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <algorithm>
  4. #include <bitset>
  5. #include <sstream>
  6. #include <strings.h>
  7. #include <cstring>
  8.  
  9. using namespace std;
  10.  
  11. long dissolve(bitset<64>);
  12. void collect(fstream&, fstream&);
  13. bool resolve(fstream&, fstream&);
  14. bitset<64> recollect(bitset<64> p, int y, bool);
  15. bitset<64> side = 0;
  16. bitset<8> set_64 = 0;
  17. bitset<8> mid_rn = 0;
  18. int cv = 0, st = 0;
  19. const int dd = 8;
  20. int ll = 7;
  21.  
  22. bitset<64> recollect(bitset<64> p, int y, bool check) {
  23.     if (y == 64)
  24.         return p;
  25.     bitset<64> n = 0, z = p;
  26.     n[0] = p[0];
  27.     int t = 1;
  28.     side[y] = check;
  29.     while (t < y) {
  30.         n[t] = (p[t] ^ p[t-1] == 1) ? 0 : 1;
  31.         t++;
  32.     }
  33.  
  34.     if (t < 64)
  35.         n[t] = check;
  36.  
  37.     if (y%dd == 7)
  38.         st++;
  39.  
  40.     if (y%dd == 7 && n[t-1] != set_64[st]) {
  41.         cv++;
  42.         for (int i = 0 ; i < cv%dd ; i++)
  43.             st--;
  44.         if (st < 1) {
  45.             st = 1;
  46.             cv = 0;
  47.         }
  48.         n = recollect(n,(st*8-1),~side[st*8-1]);
  49.     }
  50.     else if (y%dd == 7 && n[t-1] == set_64[st])
  51.         n = recollect(n,++y,side[y]);
  52.     cv = 0;
  53.     int c = 1, j = 0;
  54.     while (c - 1 < dd) {
  55.         if (n[c*dd-1] == set_64[c-1])
  56.             j++;
  57.         c++;
  58.     }
  59.     c = 0;
  60.     cout << j;
  61.     cout << " H" << flush;
  62.  
  63.     if (j < dd) {
  64.         if (st%2 == 0)
  65.             n = recollect(n,++ll,side[ll]);
  66.         else
  67.             n =  recollect(n,++ll,~side[ll]);
  68.     }
  69.  
  70.     return n;
  71. }
  72.  
  73. bool resolve(fstream& fin, fstream& fout) {
  74.     stringstream q;
  75.     q << fin.rdbuf();
  76.     string j = q.str();
  77.     q.str("");
  78.     int x = 0, k = 0;
  79.     bool check = 1;
  80.     bitset<64> y = 0, z = 0;
  81.     while (x + 1 < j.length()) {
  82.         z = (int)(j[x]);
  83.         set_64 = (int)(j[++x]);
  84.         y = recollect(z, dd, 0);
  85.         ll = dd;
  86.         for (int i = 0 ; i < 64 ; i++) {
  87.             k = (k << 1) + y[i];
  88.             if (i%8 == 0) {
  89.                 fout << (char)k;
  90.                 k = 0;
  91.             }
  92.         }
  93.         x++;
  94.     }
  95.     fout << (char)k;
  96. }
  97.  
  98. long dissolve(bitset<64> p) {
  99.     int j = dd, a = 63;
  100.     bitset<64> n = 0;
  101.     int c = 0;
  102.     while (c != 8) {
  103.         int t = 1;
  104.         n = 0;
  105.         n[0] = p[0];
  106.         while (t < a - 1) {
  107.             n[t] = (p[t] ^ p[t-1] == 1) ? 0 : 1;
  108.             t++;
  109.         }
  110.         if (a%8 == 7) {
  111.             mid_rn[c] = n[a];
  112.             c++;
  113.         }
  114.         a--;
  115.         p = n;
  116.     }
  117.     return p.to_ulong();
  118. }
  119.  
  120. void collect(fstream& fin, fstream& fout) {
  121.     stringstream k;
  122.     k << fin.rdbuf();
  123.     string j = k.str();
  124.     k.str("");
  125.  
  126.     bitset<64> lk = 0;
  127.     long long x = 0, t = 0;
  128.     while (x < j.length()) {
  129.         while (x < j.length() && x % dd != 0) {
  130.             lk = (lk.to_ulong() << 8);
  131.             lk = lk.to_ulong() + (long)(j[x++]);
  132.         }
  133.         mid_rn = 0;
  134.         long s = dissolve(lk);
  135.         for (int z = 0 ; z < 8 ; z++)
  136.             set_64[z] = mid_rn[7-z];
  137.         fout << (char)(s) << (char)(set_64.to_ulong());
  138.         x++;
  139.     }
  140.     return;
  141. }
  142.  
  143. int main(int argc, char * argv[]) {
  144.     fstream fin, fout;
  145.     int i = 0, j = 3;
  146.     bool x = 0;
  147.     if (argc > 3) {
  148.         if (strcmp("-c", argv[1]) == 0) {
  149.             i = 2;
  150.         }
  151.         else if (strcmp("-d", argv[1]) == 0) {
  152.             i = 2;
  153.             x = 1;
  154.         }
  155.         else {
  156.             i = 3;
  157.             j = 1;
  158.         }
  159.     }
  160.     else if (argc <= 3)
  161.         printf("Syntax Error: %s -c <input> <output>", argv[0]);
  162.     fin.open(argv[i], ios::in | ios::binary);
  163.     fout.open(argv[j], ios::out | ios::binary);
  164.     if (!x)
  165.         collect(fin, fout);
  166.     else
  167.         resolve(fin, fout);
  168.     return 0;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement