Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <locale>
- #include <string>
- #include <list>
- #include <codecvt>
- #include <thread>
- #include <future>
- #include <vector>
- const int W=2;
- int grep(std::string filename, std::wstring word) {
- std::locale loc("pl_PL.UTF-8");
- std::wfstream file(filename);
- file.imbue(loc);
- std::wstring line;
- unsigned int count = 0;
- while (getline(file, line)) {
- for (auto pos = line.find(word,0);
- pos != std::string::npos;
- pos = line.find(word, pos+1))
- count++;
- }
- return count;
- }
- void f(std::vector<std::string> filename, std::wstring word, std::promise<unsigned int> &count_promise)
- {
- int pom=0;
- for(auto f:filename)
- pom+=grep(f,word);
- count_promise.set_value(pom);
- }
- int main() {
- std::ios::sync_with_stdio(false);
- std::locale loc("pl_PL.UTF-8");
- std::wcout.imbue(loc);
- std::wcin.imbue(loc);
- std::wstring word;
- std::getline(std::wcin, word);
- std::wstring s_file_count;
- std::getline(std::wcin, s_file_count);
- int file_count = std::stoi(s_file_count);
- std::list<std::string> filenames{};
- std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter;
- for (int file_num = 0; file_num < file_count; file_num++) {
- std::wstring w_filename;
- std::getline(std::wcin, w_filename);
- std::string s_filename = converter.to_bytes(w_filename);
- filenames.push_back(s_filename);
- }
- std::promise<unsigned int> count_promise[W];
- std::future<unsigned int> count_future[W];
- std::vector<std::string> zlecenie[W];
- std::thread watki[W];
- int i=0;
- for(auto filename:filenames)
- {
- zlecenie[i%W].push_back(filename);
- count_future[i] = count_promise[i].get_future();
- i++;
- }
- for(i=0;i<std::min(W,file_count);i++)
- watki[i] = std::thread { [zlecenie, word, &count_promise, i] { f(zlecenie[i], word, count_promise[i]); } };
- int count = 0;
- for(i=0;i<std::min(W,file_count);i++)
- count+=count_future[i].get();
- for(i=0;i<std::min(W,file_count);i++)
- watki[i].join();
- // for (auto filename : filenames) {
- // count += grep(filename, word);
- // }
- std::wcout << count << std::endl;
- }
- /*
- ./grep-seq
- s
- 1
- ../data-txt/burza.txt
- /*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement