Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <locale>
  4. #include <string>
  5. #include <list>
  6. #include <codecvt>
  7. #include <thread>
  8. #include <future>
  9. #include <vector>
  10.  
  11. const int W=2;
  12.  
  13. int grep(std::string filename, std::wstring word) {
  14.     std::locale loc("pl_PL.UTF-8");
  15.     std::wfstream file(filename);
  16.     file.imbue(loc);
  17.     std::wstring line;
  18.     unsigned int count = 0;
  19.     while (getline(file, line)) {
  20.         for (auto pos = line.find(word,0);
  21.              pos != std::string::npos;
  22.              pos = line.find(word, pos+1))
  23.             count++;
  24.     }
  25.     return count;
  26. }
  27.  
  28. void f(std::vector<std::string> filename, std::wstring word, std::promise<unsigned int> &count_promise)
  29. {
  30.     int pom=0;
  31.     for(auto f:filename)
  32.         pom+=grep(f,word);
  33.     count_promise.set_value(pom);
  34. }
  35.  
  36. int main() {
  37.     std::ios::sync_with_stdio(false);
  38.     std::locale loc("pl_PL.UTF-8");
  39.     std::wcout.imbue(loc);
  40.     std::wcin.imbue(loc);
  41.  
  42.     std::wstring word;
  43.     std::getline(std::wcin, word);
  44.  
  45.     std::wstring s_file_count;
  46.     std::getline(std::wcin, s_file_count);
  47.     int file_count = std::stoi(s_file_count);
  48.  
  49.     std::list<std::string> filenames{};
  50.  
  51.     std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter;
  52.  
  53.     for (int file_num = 0; file_num < file_count; file_num++) {
  54.         std::wstring w_filename;
  55.         std::getline(std::wcin, w_filename);
  56.         std::string s_filename = converter.to_bytes(w_filename);
  57.         filenames.push_back(s_filename);
  58.     }
  59.  
  60.  
  61.  
  62.  
  63.     std::promise<unsigned int> count_promise[W];
  64.     std::future<unsigned int> count_future[W];
  65.     std::vector<std::string> zlecenie[W];
  66.     std::thread watki[W];
  67.  
  68.     int i=0;
  69.     for(auto filename:filenames)
  70.     {
  71.         zlecenie[i%W].push_back(filename);
  72.         count_future[i] = count_promise[i].get_future();
  73.         i++;
  74.     }
  75.  
  76.  
  77.     for(i=0;i<std::min(W,file_count);i++)
  78.         watki[i] = std::thread { [zlecenie, word, &count_promise, i] { f(zlecenie[i], word, count_promise[i]); } };
  79.  
  80.     int count = 0;
  81.     for(i=0;i<std::min(W,file_count);i++)
  82.         count+=count_future[i].get();
  83.  
  84.     for(i=0;i<std::min(W,file_count);i++)
  85.         watki[i].join();
  86. //    for (auto filename : filenames) {
  87. //        count += grep(filename, word);
  88. //    }
  89.  
  90.     std::wcout << count << std::endl;
  91. }
  92. /*
  93. ./grep-seq
  94. s
  95. 1
  96. ../data-txt/burza.txt
  97. /*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement