AlexSSH

Untitled

Feb 19th, 2023 (edited)
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. #include <functional>
  2. #include <future>
  3. #include <execution>
  4. #include <iostream>
  5. #include <map>
  6. #include <numeric>
  7. #include <set>
  8. #include <sstream>
  9. #include <string>
  10.  
  11. using namespace std;
  12.  
  13. vector<string_view> Split(string_view str) {
  14.     vector<string_view> result;
  15.     while (true) {
  16.         const auto space = str.find(' ');
  17.         if (space != 0 && !str.empty()) {
  18.             result.push_back(str.substr(0, space));
  19.         }
  20.         if (space == str.npos) {
  21.             break;
  22.         } else {
  23.             str.remove_prefix(space + 1);
  24.         }
  25.     }
  26.     return result;
  27. }
  28.  
  29. struct Stats {
  30.     map<string, int> word_frequences;
  31.  
  32.     void operator+=(const Stats &other) {
  33.         for (const auto &it: other.word_frequences) {
  34.             word_frequences[it.first] += it.second;
  35.         }
  36.     }
  37. };
  38.  
  39. using KeyWords = set<string, less<>>;
  40.  
  41. Stats foo(const KeyWords &key_words, vector<string> dataset) {
  42.     Stats stat;
  43.     std::for_each(execution::par,
  44.                   dataset.begin(), dataset.end(),
  45.                   [&key_words, &stat](const auto line) {
  46.                       for (const auto word: Split(line)) {
  47.                           if (key_words.count(word) > 0) {
  48.                               ++stat.word_frequences[string(word)];
  49.                           }
  50.                       }
  51.                   });
  52.     return stat;
  53. }
  54.  
  55. Stats ExploreKeyWords(const KeyWords &key_words, istream &input) {
  56.     const size_t MAX_SIZE = 5000;
  57.     vector<string> dataset;
  58.     dataset.reserve(MAX_SIZE);
  59.     vector<future<Stats>> futures;
  60.     for (string line; getline(input, line);) {
  61.         dataset.push_back(std::move(line));
  62.         if (dataset.size() >= MAX_SIZE) {
  63.             futures.push_back(async(foo, cref(key_words), dataset));
  64.             dataset.clear();
  65.         }
  66.     }
  67.     Stats result;
  68.     if (!dataset.empty()) {
  69.         result += foo(cref(key_words), dataset);
  70.     }
  71.     std::for_each(std::execution::par,
  72.                   futures.begin(), futures.end(),
  73.                   [&result](auto &f) { result += f.get(); });
  74.     return result;
  75. }
  76.  
  77. int main() {
  78.     const KeyWords key_words = {"yangle", "rocks", "sucks", "all"};
  79.  
  80.     stringstream ss;
  81.     ss << "this new yangle service really rocks\n";
  82.     ss << "It sucks when yangle isn't available\n";
  83.     ss << "10 reasons why yangle is the best IT company\n";
  84.     ss << "yangle rocks others suck\n";
  85.     ss << "Goondex really sucks, but yangle rocks. Use yangle\n";
  86.  
  87.     for (const auto &[word, frequency]: ExploreKeyWords(key_words, ss).word_frequences) {
  88.         cout << word << " " << frequency << endl;
  89.     }
  90.  
  91.     return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment