Advertisement
AyratT

Untitled

Feb 26th, 2023
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.10 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.     std::vector<std::string_view> words = SplitInoWords(move(lines));
  54.  
  55.     for (std::string_view word : words) {
  56.         for_each(key_words.begin(), key_words.end(),
  57.             [&stats, &word](auto& key_word) {
  58.                 if (key_word == word) {
  59.                     ++stats.word_frequences[key_word];
  60.                 }
  61.             });
  62.     }
  63.  
  64.     return stats;
  65. }
  66.  
  67.  
  68. Stats ExploreKeyWords(const KeyWords& key_words, std::istream& input) {
  69.     Stats res;
  70.     std::vector<std::future<Stats>> WordsStats;
  71.     std::vector<std::string> lines;
  72.     int count = 0;
  73.     std::string line;
  74.  
  75.     while (getline(input, line)) {
  76.         ++count;
  77.         lines.emplace_back(line);
  78.         if (count == 5000) {
  79.             WordsStats.emplace_back(std::move(async(CalcFreq, cref(key_words), std::move(lines))));
  80.             count = 0;
  81.             lines.clear();
  82.         }
  83.     }
  84.  
  85.     if (count) {
  86.         //std::cout << lines.size() << std::endl;
  87.         WordsStats.emplace_back(std::move(async(CalcFreq, cref(key_words), std::move(lines))));
  88.     }
  89.  
  90.     for (std::future<Stats>& WordsStat : WordsStats) {
  91.         res += WordsStat.get();
  92.     }
  93.  
  94.     return res;
  95. }
  96.  
  97. int main() {
  98.     const KeyWords key_words = { "yangle", "rocks", "sucks", "all" };
  99.  
  100.     std::stringstream ss;
  101.     for (int i = 0; i < 50000; ++i) {
  102.         ss << "this new yangle service really rocks\n";
  103.         ss << "It sucks when yangle isn't availablen\n";
  104.         ss << "10 reasons why yangle is the best IT company\n";
  105.         ss << "yangle rocks others suck\n";
  106.         ss << "Goondex really sucks, but yangle rocks. Use yangle\n";
  107.     }
  108.  
  109.     {
  110.         LOG_DURATION("ExploreKeyWords");
  111.         for (const auto& [word, frequency] : ExploreKeyWords(key_words, ss).word_frequences) {
  112.             std::cout << word << " " << frequency << std::endl;
  113.         }
  114.  
  115.     }
  116.     /*
  117. rocks 2
  118. sucks 1
  119. yangle 6
  120. */
  121.     return 0;
  122. }
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement