Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include "cHashTable.h"
  3. #include "cStudent.cpp"
  4. #include "StopWatch.cpp"
  5. #include "Generator.cpp"
  6. #include "Counter.h"
  7. #include "cTerm.h"
  8. #include <iostream>
  9. #include <fstream>
  10. #include <string>
  11. #include <iterator>
  12. #include <vector>
  13. #include <sstream>
  14. #include <thread>
  15. #include <future>
  16.  
  17. int CountWords(std::string strString)
  18. {
  19.     int nSpaces = 0;
  20.     unsigned int i = 0;
  21.  
  22.     // Skip over spaces at the beginning of the word
  23.     while (isspace(strString.at(i)))
  24.         i++;
  25.  
  26.     for (; i < strString.length(); i++)
  27.     {
  28.         if (isspace(strString.at(i)))
  29.         {
  30.             nSpaces++;
  31.  
  32.             // Skip over duplicate spaces & if a NULL character is found, we're at the end of the string
  33.             while (isspace(strString.at(i++)))
  34.                 if (strString.at(i) == '\0')
  35.                     nSpaces--;
  36.         }
  37.     }
  38.  
  39.     // The number of words = the number of spaces + 1
  40.     return nSpaces + 1;
  41. }
  42.  
  43. void FindTerm(int i, cHashTable<cTerm> *mHashTable, std::vector<std::string> newVec, int size, std::vector<std::string> finded)
  44. {
  45.  
  46.     int countFinded = 0;
  47.  
  48.     if ((i + mHashTable->GetLongestTerm()) <= size)
  49.     {
  50.         string searchedWord = "";
  51.         int number = 0;
  52.         for (string s : newVec)
  53.         {
  54.  
  55.             if (searchedWord == "")
  56.                 searchedWord += s;
  57.             else
  58.                 searchedWord += " " + s;
  59.  
  60.             if (std::find(finded.begin(), finded.end(), searchedWord) == finded.end())
  61.             {
  62.                 char * item = new char[searchedWord.length() + 1];
  63.                 std::strcpy(item, searchedWord.c_str());
  64.                 cTerm *s = new cTerm(item);
  65.  
  66.                 if (mHashTable->Find(*s))
  67.                 {
  68.                     finded.push_back(searchedWord);
  69.                     countFinded++;
  70.                     //i += number;
  71.                     //countFinded += number;
  72.                     //delete[]item;
  73.                     //break;
  74.                 }
  75.                 delete[]item;
  76.             }
  77.             //number++;
  78.         }
  79.         newVec = vector<string>();
  80.  
  81.     }
  82.     else
  83.     {
  84.         string searchedWord = "";
  85.         int number = 0;
  86.         for (string s : newVec)
  87.         {
  88.  
  89.             if (searchedWord == "")
  90.                 searchedWord += s;
  91.             else
  92.                 searchedWord += " " + s;
  93.  
  94.             if (std::find(finded.begin(), finded.end(), searchedWord) == finded.end())
  95.             {
  96.                 char * item = new char[searchedWord.length() + 1];
  97.                 std::strcpy(item, searchedWord.c_str());
  98.                 cTerm *s = new cTerm(item);
  99.                 if (mHashTable->Find(*s))
  100.                 {
  101.                     finded.push_back(searchedWord);
  102.                     countFinded++;
  103.                     //i += number;
  104.                     //countFinded += number;
  105.                     //delete[]item;
  106.                     //break;
  107.                 }
  108.                 delete[]item;
  109.             }
  110.             //number++;
  111.         }
  112.         newVec = vector<string>();
  113.         //break;
  114.     }
  115.     Counter::AddMultiple(countFinded);
  116. }
  117.  
  118. int main(int argc, char* argv[])
  119. {
  120.     unsigned int avgItemSize = 58;
  121.     unsigned int itemCount = 4000;
  122.  
  123.     cHashTable<cTerm> *mHashTable = new cHashTable<cTerm>(30, itemCount);
  124.  
  125.     int index = 0;
  126.     int inserted = 0;
  127.  
  128.     StopWatch watch = StopWatch();
  129.  
  130.     string line;
  131.     ifstream myfile("data.txt");
  132.     bool phase1 = true;
  133.     watch.Start();
  134.     if (myfile.is_open())
  135.     {
  136.         while (!myfile.eof())
  137.         {
  138.             // phase 1, split words in line as separate records in hashtable (cvicenie 5 -> not split)
  139.             // read line with S
  140.             // phase 2 if A, split line and add records (cvicenie 5 -> nerozdelovat)
  141.             // phase 2 if Q or D, split line and try to find all words in hashtable (cvicenie 5 -> naprogramovat delete, Q aj nad viacslovnymi zaznamami)
  142.             // read lin with F
  143.             getline(myfile, line);
  144.             if (phase1)
  145.             {
  146.                 if (line == "S")
  147.                 {
  148.                     phase1 = false;
  149.                     mHashTable->Print();
  150.                     printf("Count of Inserted: %d, Index: %d\n", inserted, (index - 1));
  151.                 }
  152.                 else
  153.                 {
  154.  
  155.                     char * item = new char[line.length() + 1];
  156.                     std::strcpy(item, line.c_str());
  157.                     cTerm *s = new cTerm(item);
  158.                     s->SetId(index);
  159.  
  160.                     if (mHashTable->GetLongestTerm() < CountWords(line))
  161.                     {
  162.                         mHashTable->SetLongestTerm(CountWords(line));
  163.                     }
  164.  
  165.                     if (mHashTable->Insert(*s))
  166.                     {
  167.                         index++;
  168.                         inserted++;
  169.                     }
  170.                     delete[]item;
  171.  
  172.                 }
  173.             }
  174.             else
  175.             {
  176.                 if (line == "F")
  177.                 {
  178.                     // break;
  179.                 }
  180.                 else
  181.                 {
  182.  
  183.                     std::string str2 = line.substr(0, 1);
  184.                     if (str2 == "A") // add
  185.                     {
  186.                         bool firstIgnore = true;
  187.                         std::string str3 = line.substr(2);
  188.  
  189.                         char * item = new char[str3.length() + 1];
  190.                         std::strcpy(item, str3.c_str());
  191.                         cTerm *s = new cTerm(item);
  192.                         s->SetId(index);
  193.  
  194.                         if (mHashTable->GetLongestTerm() < CountWords(str3))
  195.                         {
  196.                             mHashTable->SetLongestTerm(CountWords(str3));
  197.                         }
  198.  
  199.                         if (mHashTable->Insert(*s))
  200.                         {
  201.                             index++;
  202.                             inserted++;
  203.                         }
  204.                         else
  205.                         {
  206.  
  207.                         }
  208.                         delete[]item;
  209.  
  210.                     }
  211.                     else if (str2 == "Q") // find
  212.                     {
  213.  
  214.                         Counter::Reset();
  215.                         int countFinded = 0;
  216.                         istringstream buf(line);
  217.                         istream_iterator<std::string> beg(buf), end;
  218.                         std::vector<std::string> tokens(beg, end);
  219.  
  220.                         std::vector<std::string> finded;
  221.                         //std:thread t[10000];
  222.                         for (int i = 1; i <= tokens.size(); i++)
  223.                         {
  224.                             vector<string>::const_iterator first = tokens.begin() + i;
  225.                             if ((i + mHashTable->GetLongestTerm()) <= tokens.size())
  226.                             {
  227.                                 int sizeOfLast = tokens.size();
  228.                                 vector<string>::const_iterator last = tokens.begin() + i + mHashTable->GetLongestTerm();
  229.                                 vector<string> newVec(first, last);
  230.                                 FindTerm(i, mHashTable, newVec, sizeOfLast, finded);
  231.                             }
  232.                             else
  233.                             {
  234.                                 int sizeOfLast = tokens.size();
  235.                                 vector<string>::const_iterator last = tokens.begin() + sizeOfLast;
  236.                                 vector<string> newVec(first, last);
  237.                                 FindTerm(i, mHashTable, newVec, sizeOfLast, finded);
  238.                             }
  239.                             //t[i] = std::thread(FindTerm , i, mHashTable , tokens, sizeOfLast);
  240.                             //t[i].join();
  241.                         }
  242.                         //for (int i = 1; i <= tokens.size(); i++)
  243.                         //{
  244.                             //t[i].join();
  245.                             //t[i].~thread();
  246.                         //}
  247.  
  248.                         countFinded = Counter::Get();
  249.                         printf("Count of finded terms: %d\n", countFinded);
  250.                     }
  251.                     else if (str2 == "D") // delete
  252.                     {
  253.                         std::string str3 = line.substr(2);
  254.                         char * item = new char[str3.length() + 1];
  255.                         std::strcpy(item, str3.c_str());
  256.                         cTerm *s = new cTerm(item);
  257.  
  258.                         if (mHashTable->Delete(*s))
  259.                         {
  260.                             inserted--;
  261.                         }
  262.  
  263.                         delete[] item;
  264.                     }
  265.                 }
  266.             }
  267.         }
  268.         myfile.close();
  269.     }
  270.     else cout << "Unable to open file";
  271.     mHashTable->Print();
  272.     delete mHashTable;
  273.  
  274.     printf("Count of Inserted: %d, Index: %d\n", inserted, (index - 1));
  275.     watch.Stop();
  276.     watch.ShowTime();
  277.  
  278.     getchar();
  279.     return 0;
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement