Advertisement
giGii

future

Jan 3rd, 2023
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1. #include <algorithm>
  2. #include <functional>
  3. #include <iostream>
  4. #include <sstream>
  5. #include <execution>
  6. #include <future>
  7. #include <numeric>
  8. #include <vector>
  9. #include <map>
  10. #include <set>
  11. #include <string>
  12.  
  13. using namespace std;
  14.  
  15. const int SIZE_OF_PART = 5000;
  16.  
  17. struct Stats {
  18.     map<string, int> word_frequences;
  19.  
  20.     void operator+=(const Stats& other) {
  21.         for (const auto& [word, frequency] : other.word_frequences) {
  22.             word_frequences[word] += frequency;
  23.         }
  24.  
  25.         // for_each(execution::par,
  26.         //          other.word_frequences.begin(), other.word_frequences.end(),
  27.         //          [this](auto& item) { this->word_frequences[item.first] += item.second; });
  28.     }
  29. };
  30.  
  31. using KeyWords = set<string, less<>>;
  32.  
  33. Stats ExploreKeyWords(const KeyWords& key_words, istream& input) {
  34.  
  35.     vector<vector<string>> parts_of_stream;
  36.  
  37.  
  38.     // разделю поток на части по SIZE_OF_PART слов
  39.     string word;
  40.     int count = SIZE_OF_PART;
  41.     while (!input.eof()) {
  42.  
  43.         vector<string> tmp;
  44.        
  45.         while (count) {
  46.  
  47.             input >> word;
  48.             tmp.push_back(move(word));
  49.  
  50.             --count;
  51.         }
  52.  
  53.         parts_of_stream.push_back(move(tmp));
  54.  
  55.         count = SIZE_OF_PART;
  56.  
  57.     }
  58.  
  59.     auto counter = [&key_words](const vector<string>& words) {
  60.         Stats st;
  61.        
  62.         // for_each(execution::par,
  63.         //          words.begin(), words.end(),
  64.         //          [&st, &key_words](const string& word) {
  65.         //             if (key_words.count(word)) {
  66.         //                 ++st.word_frequences[word];
  67.         //             }
  68.         //          }
  69.         //         );
  70.  
  71.         for (const string& w : words) {
  72.             if (key_words.count(w)) {
  73.                 ++st.word_frequences[w];
  74.             }
  75.         }
  76.  
  77.         return st;
  78.     };
  79.  
  80.     vector<future<Stats>> threads;
  81.     for (const vector<string>& part : parts_of_stream) {
  82.         // асинхронный запуск подсчетов для каждой из частей потока, разделенного на векторы слов
  83.         threads.push_back(async(counter, cref(part)));
  84.     }
  85.  
  86.     Stats result;
  87.  
  88.     for (future<Stats>& pre : threads) {
  89.         result += pre.get();
  90.     }
  91.  
  92.     return result;
  93. }
  94.  
  95. int main() {
  96.     const KeyWords key_words = {"yangle", "rocks", "sucks", "all"};
  97.  
  98.     stringstream ss;
  99.     ss << "this new yangle service really rocks\n";
  100.     ss << "It sucks when yangle isn't available\n";
  101.     ss << "10 reasons why yangle is the best IT company\n";
  102.     ss << "yangle rocks others suck\n";
  103.     ss << "Goondex really sucks, but yangle rocks. Use yangle\n";
  104.  
  105.     for (const auto& [word, frequency] : ExploreKeyWords(key_words, ss).word_frequences) {
  106.         cout << word << " " << frequency << endl;
  107.     }
  108.  
  109.  
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement