shushus

Спринт 9. Особенности future.

Mar 6th, 2023 (edited)
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. #include <functional>
  2. #include <future>
  3. #include <sstream>
  4. #include <iostream>
  5. #include <iterator>
  6. #include <vector>
  7. #include <map>
  8. #include <set>
  9. #include <string>
  10.  
  11. using namespace std;
  12.  
  13. struct Stats {
  14.     map<string, int> word_frequences;
  15.  
  16.     void operator+=(const Stats& other) {
  17.         for (const auto& [word, frequency] : other.word_frequences) {
  18.             word_frequences[word] += frequency;
  19.         }
  20.     }
  21. };
  22.  
  23. using KeyWords = set<string, less<>>;
  24.  
  25. vector<string_view> SplitKeyWords(string_view text) {
  26.    
  27.     vector<string_view> result;
  28.    
  29.     while (true) {
  30.         const auto space = text.find(' ');
  31.        
  32.         if (!text.empty() && space) {
  33.             result.push_back(text.substr(0, space));
  34.         }
  35.        
  36.         if (text.npos != space) {
  37.             text.remove_prefix(1 + space);
  38.         } else {
  39.             break;
  40.         }
  41.     }
  42.    
  43.     return result;
  44. }
  45.  
  46. Stats SearchStr(const KeyWords& key_words,
  47.                 string_view str) {
  48.    
  49.     Stats result;
  50.    
  51.     for (const string_view s : SplitKeyWords(str)) {
  52.         if (key_words.count(s)) {
  53.             ++result.word_frequences[string(s)];
  54.         }
  55.     }
  56.    
  57.     return result;
  58. }
  59.  
  60. Stats SearchPart(const KeyWords& key_words,
  61.                  vector<string> part) {
  62.    
  63.     Stats result;
  64.    
  65.     for (const string& p : part) {
  66.         result += SearchStr(key_words, p);
  67.     }
  68.    
  69.     return result;
  70. }
  71.  
  72. Stats ExploreKeyWords(const KeyWords& key_words,
  73.                       istream& input) {
  74.    
  75.     const size_t PART_SIZE = 5000;
  76.    
  77.      
  78.     vector<future<Stats>> stats;
  79.     vector<string> part;
  80.  
  81.     for (string str; getline(input, str);) {
  82.        
  83.         part.push_back(move(str));
  84.        
  85.         if (PART_SIZE <= part.size()) {
  86.            
  87.             stats.push_back(async(SearchPart,
  88.                                   ref(key_words),
  89.                                   move(part)));
  90.         }
  91.     }
  92.  
  93.     Stats result;
  94.     if (part.size()) {
  95.         result += SearchPart(key_words,
  96.                              move(part));
  97.     }
  98.  
  99.     for (auto& stat : stats) {
  100.         result += stat.get();
  101.     }
  102.  
  103.     return result;
  104. }
  105.  
  106.  
  107. int main() {
  108.     const KeyWords key_words = {"yangle", "rocks", "sucks", "all"};
  109.  
  110.     stringstream ss;
  111.     ss << "this new yangle service really rocks\n";
  112.     ss << "It sucks when yangle isn't available\n";
  113.     ss << "10 reasons why yangle is the best IT company\n";
  114.     ss << "yangle rocks others suck\n";
  115.     ss << "Goondex really sucks, but yangle rocks. Use yangle\n";
  116.  
  117.     for (const auto& [word, frequency] : ExploreKeyWords(key_words, ss).word_frequences) {
  118.         cout << word << " " << frequency << endl;
  119.     }
  120.  
  121.     return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment