SHARE
TWEET

Untitled

a guest Jan 12th, 2017 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. // Created by Tomáš on 20.12.16.
  3. //
  4. #include <set>
  5. #include <thread>
  6. #include <vector>
  7. #include "Controller.h"
  8.  
  9. template <typename TimePoint>
  10. std::chrono::milliseconds to_ms(TimePoint tp) {
  11.     return std::chrono::duration_cast<std::chrono::milliseconds>(tp);
  12. }
  13.  
  14. Controller::Controller(std::vector<std::string> vector) {
  15.     arguments = vector;
  16. }
  17.  
  18. void Controller::control() {
  19.     auto start = std::chrono::high_resolution_clock::now(); //timer start
  20.     seq_or_para = seqOrParCounting(); //seq is true, para is false
  21.     if (seq_or_para) {
  22.         seqControl(); //call function for seq word counter
  23.     } else {
  24.         paraControl(); //call function for seq word counter
  25.     }
  26.     printResults(); //print results from map
  27.     auto end = std::chrono::high_resolution_clock::now(); // timer end
  28.     std::cout << "Needed " << to_ms(end - start).count() << " ms to finish.\n";
  29. }
  30.  
  31. bool Controller::seqOrParCounting() {
  32.     if("p" == arguments.front()){
  33.         return false;
  34.     }
  35.     else if("s" == arguments.front()){
  36.         return true;
  37.     } else{
  38.         throw "BAD argument, choose between p for parallel counting and s for sequential!";
  39.     }
  40.  
  41. }
  42.  
  43. std::string Controller::validateWord(std::string word) {
  44.     char chars[] = ".,?!:;-_\""; // add char
  45.     for (unsigned int i = 0; i < strlen(chars); ++i) {
  46.         word.erase(std::remove(word.begin(), word.end(), chars[i]), word.end()); // deleting specific(chars[]) chars from string
  47.         std::transform(word.begin(), word.end(), word.begin(), ::tolower); // transfrom uppercase to lowercase
  48.     }
  49.     return word;
  50. }
  51.  
  52. void Controller::seqControl() { // function for handling sequence reading
  53.     while (arguments.size() != 1) {
  54.         readFile(arguments.back(), this, true);
  55.         arguments.pop_back();
  56.     }
  57. }
  58.  
  59. void Controller::printResults() {
  60.     if (map.size() == 0) {
  61.         std::cout << "\nThere are no words to print." << std::endl;
  62.     } else {
  63.         std::cout << "\nWords and their occurrence:" << std::endl;
  64.         for (std::string word : set) {
  65.             std::cout << word << ": " << map.get(word) << std::endl; // print key and value
  66.         }
  67.     }
  68. }
  69.  
  70. void readFile(std::string path, Controller * controller, bool seq_or_para){
  71.     Map mapForThread;
  72.     std::vector<std::string> setForThread;
  73.     std::ifstream file;
  74.     file.open(path);
  75.     if (!file.is_open()) { //if file wasnt opened sucessfully
  76.         std::cout << "File on path: " + path + " cannot be open." << std::endl;
  77.     } else {
  78.         std::string word;
  79.         while (file >> word) { // read words from file
  80.             word = controller->validateWord(word);
  81.             if (word != "") {
  82.                 if(seq_or_para){
  83.                     if (!controller->map.putWithoutLock(word)) { //without mutex
  84.                         controller->set.insert(word);
  85.                     }
  86.                 }else{
  87.                     if (!mapForThread.putWithoutLock(word)){
  88.                         setForThread.push_back(word);
  89.                     }
  90.                 }
  91.             }
  92.         }
  93.         file.close();
  94.         if(!seq_or_para){
  95.             controller->map.putWithLock(&mapForThread,setForThread,&controller->set);
  96.         }
  97.     }
  98. }
  99.  
  100. void Controller::paraControl() { //handler for para word counting
  101.     std::vector<std::thread> threads;
  102.     while (arguments.size() != 1) {
  103.         std::thread thread(&readFile, arguments.back(), this, false); //create new thread
  104.         arguments.pop_back();
  105.         threads.push_back(std::move(thread));
  106.     }
  107.     for (unsigned int i=0; i<threads.size(); ++i)
  108.     {
  109.         if (threads[i].joinable())
  110.             threads.at(i).join(); //wait until thread job is done
  111.     }
  112. }
  113.  
  114.  
  115.  
  116.  
  117. //
  118. // Created by Tomáš on 21.12.16.
  119. //
  120.  
  121. #include <set>
  122. #include <vector>
  123. #include "Map.h"
  124.  
  125. void Map::putWithLock(Map * map1, std::vector<std::string> vector, std::set<std::string> * set) {
  126.     // take all data from thread`s map and put them to global map
  127.     mtx.lock();
  128.     for(auto a:vector){
  129.         auto it = map.find(a);
  130.         if(it == map.end()){
  131.             map[a] = map1->map[a];
  132.             set->insert(a);
  133.         }else{
  134.             map[a] += map1->map[a];
  135.         }
  136.     }
  137.     mtx.unlock();
  138. }
  139.  
  140. bool Map::putWithoutLock(std::string key) {
  141.     auto it = map.find(key);
  142.     map[key]++;
  143.     return it != map.end();
  144. }
  145.  
  146. int Map::get(std::string key) {
  147.     return map[key];
  148.  
  149. }
  150.  
  151. unsigned long Map::size() {
  152.     return map.size();
  153. }
  154.  
  155. Map::Map() {
  156.  
  157. }
  158.  
  159. Map::~Map() {
  160.  
  161. }
RAW Paste Data
Top