Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <math.h>
- #include <sstream>
- #include <string.h>
- #include <unistd.h>
- using namespace std;
- struct nodes {
- public:
- bool x = 0;
- nodes * left = nullptr;
- nodes * right = nullptr;
- nodes(bool x): x(x), left(nullptr), right(nullptr) {};
- } *tail, *head;
- stringstream out;
- uint64_t TIMES_DEEP = 0, outsize = 0;
- string large_num;
- void parsebst(nodes * node, int32_t p, string current_num, bool t);
- void printbst();
- nodes * front(nodes*& node, uint64_t entry);
- int pack(fstream& fout);
- void parsebst(nodes * node, int32_t p, string current_num, bool t)
- {
- if (node->left == nullptr && node->right == nullptr)
- return;
- uint8_t counter = 0;
- if (node->right != nullptr) {
- if (p < 0)
- {
- if (p > 3)
- cout << "." << flush;
- p = abs(p);
- current_num += to_string(t);
- for (int g = 0 ; g < 2 ; g++)
- {
- large_num += to_string(p%2);
- p >>= 1;
- }
- large_num += current_num;
- current_num = "";
- }
- p++;
- current_num += (node->x) ? "1" : "0";
- parsebst(node->right, p, current_num, 0);
- }
- if (node->left != nullptr)
- {
- if (p > 0)
- {
- if (p < -3)
- cout << "." << flush;
- p = abs(p);
- current_num += to_string(t);
- for (int g = 0 ; g < 2 ; g++)
- {
- large_num += to_string(p%2);
- p >>= 1;
- }
- large_num += current_num;
- current_num = "";
- }
- p--;
- current_num += (node->x) ? "1" : "0";
- parsebst(node->left, p, current_num, 1);
- }
- }
- void printbst() {
- while (large_num.length() > 7)
- {
- if (strtoull(large_num.substr(0,7).c_str(),NULL,2) == 0)
- out << (char)('\0');
- else
- out << (char)strtoull(large_num.substr(0,8).c_str(),NULL,2);
- large_num = large_num.substr(7);
- }
- large_num += "00000000";
- if (strtoull(large_num.c_str(),NULL,2) == 0)
- out << (char)('\0');
- else
- out << (char)strtoull(large_num.c_str(),NULL,2);
- large_num.clear();
- return;
- }
- nodes * erase_node(nodes*& node){
- if (node->left != nullptr)
- node = erase_node(node->left);
- else if (node->right != nullptr)
- node = erase_node(node->right);
- free(node);
- return NULL;
- }
- nodes * front(nodes*& node, uint64_t entry){
- if (entry == 0)
- return node;
- TIMES_DEEP++;
- if (node->left != nullptr)
- front(node->left, entry);
- else if (node->right != nullptr)
- front(node->right,entry);
- if (entry%4 >= 2) {
- node->left = new nodes(entry%2);
- entry >>= 2;
- front(node->left, entry);
- }
- else if (entry%4 <= 1) {
- node->right = new nodes(entry%2);
- entry >>= 2;
- front(node->right, entry);
- }
- return node;
- }
- int pack(fstream& fout) {
- TIMES_DEEP = 0;
- if (head->left != nullptr)
- parsebst(head, 0, "", 0);
- else if (head->right != nullptr)
- parsebst(head, 0, "", 1);
- printbst();
- out << "|x|";
- outsize += out.str().length();
- fout << out.str();
- out.str("");
- return 1;
- }
- int main(int argc, char * argv[])
- {
- fstream fin (argv[2], std::ios::in | std::ios::binary);
- stringstream sstring;
- stringstream sstring_out;
- sstring << fin.rdbuf();
- string g = "", h = sstring.str();
- sstring.str("");
- fstream fout (argv[3], std::ios::out | std::ios::trunc | std::ios::binary);
- tail = new nodes(0);
- uint64_t d = 1, cnter = 0, place = 0;
- string thx = "thank you! so much!!";
- string f = "";
- cout << "Input size: " << (h.length()) << endl << flush;
- uint64_t file_size = h.length();
- uint64_t TIMES_OVER = 90;
- for (uint32_t a = 1; a <= h.length() ; a++)
- {
- d <<= 8;
- place++;
- if (a%3000 == 0 || a == h.length())
- printf("%.0f%%\t\t%.0f%% \t\r",(double)a/(double)h.length()*100,(double)outsize/(double)(a)*100);
- if (a%6 == 5 || a == h.length())
- {
- if (h[a-1] != '\0')
- d += (uint8_t)h[a-1];
- }
- else
- continue;
- if (head == nullptr)
- head = new nodes(0);
- *tail = &head;
- front(tail, d);
- d = 1;
- if (TIMES_DEEP >= TIMES_OVER || a == h.length())
- {
- pack(fout);
- erase_node(head);
- }
- }
- printf("%.0f%%\t\t%.0f%% \t\r",(double)place/(double)h.length()*100,(double)outsize/(double)place*100);
- cout << "\nOutput size: " << outsize << endl << flush;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement