Advertisement
AyratT

3.Особенности future

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