Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.16 KB | None | 0 0
  1. #include "test_runner.h"
  2. #include "profile.h"
  3.  
  4. #include <map>
  5. #include <string>
  6. #include <sstream>
  7. #include <string_view>
  8. #include <vector>
  9. #include <iterator>
  10. #include <algorithm>
  11. #include <functional>
  12. #include <future>
  13. #include <numeric>
  14. using namespace std;
  15.  
  16. struct Stats
  17. {
  18.     map<string, int> word_frequences;
  19.  
  20.     void operator +=(const Stats& other)
  21.     {
  22.         for (const auto& kvp : other.word_frequences)
  23.         {
  24.             word_frequences[kvp.first] += kvp.second;
  25.         }
  26.     }
  27. };
  28.  
  29. const map<string, int> SplitIntoWords(const string& str)
  30. {
  31.     map<string, int> result;
  32.     auto str_begin = begin(str);
  33.     const auto str_end = end(str);
  34.     while (true)
  35.     {
  36.         auto it = find(str_begin , str_end , ' ');
  37.  
  38.         result[string(str_begin , it)]++;
  39.  
  40.         if (it == str_end)
  41.         {
  42.             break;
  43.         }
  44.         else
  45.         {
  46.             str_begin = it + 1;
  47.         }
  48.     }
  49.     return result;
  50. }
  51.  
  52.  
  53. Stats ExploreLine(const set<string>& key_words, const string& line)
  54. {
  55.     //const auto& words = SplitIntoWordsView(line);
  56.     const auto& words = SplitIntoWords(line);
  57.  
  58.     Stats lineStats;
  59.     for (const string& key : key_words)
  60.     {
  61.         if (words.count(key) > 0)
  62.         {
  63.             lineStats.word_frequences[key] = words.at(key);
  64.         }
  65.     }
  66.  
  67.     return lineStats;
  68. }
  69.  
  70. Stats ExploreKeyWordsSingleThread(const set<string>& key_words, istream& input)
  71. {
  72.     Stats result;
  73.     for (string line; getline(input, line);)
  74.     {
  75.         result += ExploreLine(key_words, line);
  76.     }
  77.     return result;
  78. }
  79.  
  80.  
  81. Stats ExploreKeyWords(const set<string>& key_words, istream& input,
  82.         size_t threadCount = 8)
  83. {
  84.     vector<stringstream> streams;
  85.     streams.reserve(threadCount);
  86.  
  87.     for (size_t i = 0; i < threadCount; ++i)
  88.     {
  89.         stringstream ss;
  90.         streams.push_back(stringstream{});
  91.     }
  92.  
  93.     size_t streamNum = 0;
  94.     for (string line; getline(input, line);)
  95.     {
  96.         streams[streamNum++] << line << "\n";
  97.  
  98.         if(streamNum==threadCount)
  99.         {
  100.             streamNum=0;
  101.         }
  102.     }
  103.  
  104.     vector<future<Stats>> futures;
  105.     futures.reserve(threadCount);
  106.  
  107.  
  108.     for (auto& substream : streams)
  109.     {
  110.         futures.push_back(
  111.                 async(ExploreKeyWordsSingleThread, ref(key_words),
  112.                         ref(substream)));
  113.     }
  114.  
  115.     Stats result;
  116.     for (auto& _future : futures)
  117.     {
  118.         result += _future.get();
  119.     }
  120.  
  121.     return result;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement