Advertisement
AyratT

3.Особенности future 100500 попытка

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