Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include "cHashTable.h"
- #include "cStudent.cpp"
- #include "StopWatch.cpp"
- #include "Generator.cpp"
- #include "Counter.h"
- #include "cTerm.h"
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <iterator>
- #include <vector>
- #include <sstream>
- #include <thread>
- #include <future>
- int CountWords(std::string strString)
- {
- int nSpaces = 0;
- unsigned int i = 0;
- // Skip over spaces at the beginning of the word
- while (isspace(strString.at(i)))
- i++;
- for (; i < strString.length(); i++)
- {
- if (isspace(strString.at(i)))
- {
- nSpaces++;
- // Skip over duplicate spaces & if a NULL character is found, we're at the end of the string
- while (isspace(strString.at(i++)))
- if (strString.at(i) == '\0')
- nSpaces--;
- }
- }
- // The number of words = the number of spaces + 1
- return nSpaces + 1;
- }
- void FindTerm(int i, cHashTable<cTerm> *mHashTable, std::vector<std::string> newVec, int size, std::vector<std::string> finded)
- {
- int countFinded = 0;
- if ((i + mHashTable->GetLongestTerm()) <= size)
- {
- string searchedWord = "";
- int number = 0;
- for (string s : newVec)
- {
- if (searchedWord == "")
- searchedWord += s;
- else
- searchedWord += " " + s;
- if (std::find(finded.begin(), finded.end(), searchedWord) == finded.end())
- {
- char * item = new char[searchedWord.length() + 1];
- std::strcpy(item, searchedWord.c_str());
- cTerm *s = new cTerm(item);
- if (mHashTable->Find(*s))
- {
- finded.push_back(searchedWord);
- countFinded++;
- //i += number;
- //countFinded += number;
- //delete[]item;
- //break;
- }
- delete[]item;
- }
- //number++;
- }
- newVec = vector<string>();
- }
- else
- {
- string searchedWord = "";
- int number = 0;
- for (string s : newVec)
- {
- if (searchedWord == "")
- searchedWord += s;
- else
- searchedWord += " " + s;
- if (std::find(finded.begin(), finded.end(), searchedWord) == finded.end())
- {
- char * item = new char[searchedWord.length() + 1];
- std::strcpy(item, searchedWord.c_str());
- cTerm *s = new cTerm(item);
- if (mHashTable->Find(*s))
- {
- finded.push_back(searchedWord);
- countFinded++;
- //i += number;
- //countFinded += number;
- //delete[]item;
- //break;
- }
- delete[]item;
- }
- //number++;
- }
- newVec = vector<string>();
- //break;
- }
- Counter::AddMultiple(countFinded);
- }
- int main(int argc, char* argv[])
- {
- unsigned int avgItemSize = 58;
- unsigned int itemCount = 4000;
- cHashTable<cTerm> *mHashTable = new cHashTable<cTerm>(30, itemCount);
- int index = 0;
- int inserted = 0;
- StopWatch watch = StopWatch();
- string line;
- ifstream myfile("data.txt");
- bool phase1 = true;
- watch.Start();
- if (myfile.is_open())
- {
- while (!myfile.eof())
- {
- // phase 1, split words in line as separate records in hashtable (cvicenie 5 -> not split)
- // read line with S
- // phase 2 if A, split line and add records (cvicenie 5 -> nerozdelovat)
- // 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)
- // read lin with F
- getline(myfile, line);
- if (phase1)
- {
- if (line == "S")
- {
- phase1 = false;
- mHashTable->Print();
- printf("Count of Inserted: %d, Index: %d\n", inserted, (index - 1));
- }
- else
- {
- char * item = new char[line.length() + 1];
- std::strcpy(item, line.c_str());
- cTerm *s = new cTerm(item);
- s->SetId(index);
- if (mHashTable->GetLongestTerm() < CountWords(line))
- {
- mHashTable->SetLongestTerm(CountWords(line));
- }
- if (mHashTable->Insert(*s))
- {
- index++;
- inserted++;
- }
- delete[]item;
- }
- }
- else
- {
- if (line == "F")
- {
- // break;
- }
- else
- {
- std::string str2 = line.substr(0, 1);
- if (str2 == "A") // add
- {
- bool firstIgnore = true;
- std::string str3 = line.substr(2);
- char * item = new char[str3.length() + 1];
- std::strcpy(item, str3.c_str());
- cTerm *s = new cTerm(item);
- s->SetId(index);
- if (mHashTable->GetLongestTerm() < CountWords(str3))
- {
- mHashTable->SetLongestTerm(CountWords(str3));
- }
- if (mHashTable->Insert(*s))
- {
- index++;
- inserted++;
- }
- else
- {
- }
- delete[]item;
- }
- else if (str2 == "Q") // find
- {
- Counter::Reset();
- int countFinded = 0;
- istringstream buf(line);
- istream_iterator<std::string> beg(buf), end;
- std::vector<std::string> tokens(beg, end);
- std::vector<std::string> finded;
- //std:thread t[10000];
- for (int i = 1; i <= tokens.size(); i++)
- {
- vector<string>::const_iterator first = tokens.begin() + i;
- if ((i + mHashTable->GetLongestTerm()) <= tokens.size())
- {
- int sizeOfLast = tokens.size();
- vector<string>::const_iterator last = tokens.begin() + i + mHashTable->GetLongestTerm();
- vector<string> newVec(first, last);
- FindTerm(i, mHashTable, newVec, sizeOfLast, finded);
- }
- else
- {
- int sizeOfLast = tokens.size();
- vector<string>::const_iterator last = tokens.begin() + sizeOfLast;
- vector<string> newVec(first, last);
- FindTerm(i, mHashTable, newVec, sizeOfLast, finded);
- }
- //t[i] = std::thread(FindTerm , i, mHashTable , tokens, sizeOfLast);
- //t[i].join();
- }
- //for (int i = 1; i <= tokens.size(); i++)
- //{
- //t[i].join();
- //t[i].~thread();
- //}
- countFinded = Counter::Get();
- printf("Count of finded terms: %d\n", countFinded);
- }
- else if (str2 == "D") // delete
- {
- std::string str3 = line.substr(2);
- char * item = new char[str3.length() + 1];
- std::strcpy(item, str3.c_str());
- cTerm *s = new cTerm(item);
- if (mHashTable->Delete(*s))
- {
- inserted--;
- }
- delete[] item;
- }
- }
- }
- }
- myfile.close();
- }
- else cout << "Unable to open file";
- mHashTable->Print();
- delete mHashTable;
- printf("Count of Inserted: %d, Index: %d\n", inserted, (index - 1));
- watch.Stop();
- watch.ShowTime();
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement