Advertisement
Tamjow

aadscomp

May 25th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.39 KB | None | 0 0
  1. // datacomp.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <vector>
  7. #include <string>
  8. using namespace std;
  9.  
  10. const int dictsize = 9;
  11. string dictionary = "AAAAAAAA";
  12. string text = "ABABCDABCABCDCADABCA";
  13. string textcpy = text;
  14. string lookahead;
  15. vector <string> subsets;
  16. vector <string> compressed;
  17.  
  18. void substrings(string str) {
  19.     subsets.erase(subsets.begin(), subsets.end());
  20.     if (str.length() > 1) {
  21.         for (int i = 0; i <= str.length(); i++) {
  22.             string s1 = str.substr(0, i); //get a substring of s from from the start to index i
  23.             subsets.push_back(s1);
  24.         }
  25.     }
  26.     else {
  27.         subsets.push_back(str);
  28.     }
  29.     if (subsets.size() > 1) {
  30.         for (int i = 1;i < subsets.size();i++) {
  31.             subsets[i - 1] = subsets[i]; //since there's an empty space at the start shift all values left
  32.         }
  33.     }
  34.     subsets.erase(subsets.begin() + 4);//delete last entry because now it is empty
  35. }
  36.  
  37. void compress() {
  38.     if (textcpy.length() >= 4) {
  39.         lookahead = textcpy.substr(0, 4);
  40.     }
  41.     else {
  42.         lookahead = textcpy.substr(0, textcpy.length());
  43.     }
  44.     substrings(lookahead);
  45.     string output = "(";
  46.     int maxlen = 0;
  47.     int maxidx = -1;
  48.     for (int i = 0; i < subsets.size(); i++) {
  49.         size_t index = dictionary.rfind(subsets[i]); //store index of found substring
  50.                                                      //cout << "loop" << endl;
  51.         if (dictionary.rfind(subsets[i]) != string::npos) { //if there is a match do this
  52.                                                             //cout << "ifentered" << endl;
  53.             if (subsets[i].length()>maxlen) { //execute if substring is the longest so far
  54.                 maxlen = subsets[i].length();
  55.                 maxidx = index;
  56.             }
  57.             //cout << "index of found: " << 7 - index << " length of found: " << subsets[i].length() << endl;
  58.  
  59.         }
  60.     }
  61.  
  62.     if (maxlen > 0) {//if match is found
  63.         cout << "dict: " << dictionary << endl;
  64.         //cout << "text: " << textcpy << endl;
  65.         cout << "lookahead: " << lookahead << endl;
  66.         maxidx = 7 - maxidx;//reverse index since indexing in example is right to left
  67.         output += "0,";
  68.         output += to_string(maxidx);
  69.         output += ",";
  70.         output += to_string(maxlen);
  71.         output += ")";
  72.         cout << output << endl;
  73.         compressed.push_back(output);
  74.  
  75.         for (int i = 1;i <= maxlen;i++) {//shift the dictionary to the left by the length amount
  76.             for (int i = 1;i < dictionary.length();i++) {
  77.                 dictionary[i - 1] = dictionary[i]; //since there's an empty space at the start shift all values left
  78.             }
  79.            
  80.         }
  81.         dictionary.erase(8 - maxlen, maxlen);//remove as many ending characters as length of match
  82.         dictionary.append(lookahead, 0, maxlen);
  83.         textcpy.erase(0, maxlen);
  84.  
  85.     }
  86.     else {
  87.         cout << "dict: " << dictionary << endl;
  88.         //cout << "text: " << textcpy << endl;
  89.         cout << "lookahead: " << lookahead << endl;
  90.         output += "1,";
  91.         output += subsets[0];
  92.         output += ")";
  93.         cout << output << endl; //if no match then cout new char
  94.         compressed.push_back(output);
  95.  
  96.         for (int i = 1;i < dictionary.length();i++) {
  97.             dictionary[i - 1] = dictionary[i]; //since there's an empty space at the start shift all values left
  98.         }
  99.         dictionary.erase(7);//remove last character
  100.         dictionary.append(lookahead, 0, 1); //add the new char
  101.         textcpy.erase(0, 1);
  102.     }
  103. }
  104.  
  105.  
  106. int main(void)
  107. {
  108.  
  109.     cout << "dict: " << dictionary << endl;
  110.     cout << "text: " << textcpy << endl;
  111.     cout << "lookahead: " << lookahead << endl;
  112.     while (textcpy.length() >= 4) {
  113.         compress();
  114.     }
  115.  
  116.     system("pause");
  117.     return 0;
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement