Advertisement
Argentum_02

blogExplore

Nov 20th, 2019
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. #include "test_runner.h"
  2. #include "profile.h"
  3.  
  4. #include <map>
  5. #include <string>
  6. #include <future>
  7. #include <functional>
  8. #include <algorithm>
  9. #include <exception>
  10. using namespace std;
  11.  
  12. struct Stats {
  13.   map<string, int> word_frequences;
  14.  
  15.   void operator += (const Stats& other){
  16.       for(auto &it : other.word_frequences){
  17.           if(word_frequences.find(it.first)==word_frequences.end())
  18.           {
  19.               word_frequences[it.first]=0;
  20.           }
  21.           word_frequences[it.first] +=it.second;
  22.       }
  23.   }
  24. };
  25.  
  26.  
  27.  
  28. Stats ExploreLine(const set<string>& key_words, const string& line) {
  29.     Stats res;
  30.     istringstream str(line);
  31.     string space;
  32.     while(!str.eof()){
  33.         str >> space;
  34.         if(key_words.find(space)!=key_words.end()){
  35.             res.word_frequences[space]++;
  36.         }
  37.     }
  38.     return res;
  39. }
  40.  
  41. Stats ThreadExplore(const set<string>& key_words,istream &input){
  42.     Stats res;
  43.     string line;
  44.     while(!input.eof()){
  45.         getline(input,line);
  46.         res+= ExploreLine(key_words,line);
  47.     }
  48.     return res;
  49. }
  50.  
  51. Stats ExploreKeyWordsSingleThread(
  52.   const set<string>& key_words, istream& input
  53. ) {
  54.   Stats result;
  55.   for (string line; getline(input, line); ) {
  56.     result += ExploreLine(key_words, line);
  57.   }
  58.   return result;
  59. }
  60.  
  61. Stats ExploreKeyWords(const set<string>& key_words, istream& input) {
  62.     Stats result;
  63.  
  64.     unsigned concurentThreadsSupported = thread::hardware_concurrency();
  65.     vector<future<Stats>> space(concurentThreadsSupported);
  66.  
  67.         for(int i = 0;i<concurentThreadsSupported;i++){
  68.             space[i]=async([&key_words,&input](){
  69.                 return ThreadExplore(key_words,input);
  70.             });
  71.         }
  72.         for(auto &it:space){
  73.             result+=it.get();
  74.         }
  75.     return result;
  76. }
  77.  
  78. void TestBasic() {
  79.   const set<string> key_words = {"yangle", "rocks", "sucks", "all"};
  80.  
  81.   stringstream ss;
  82.   ss << "this new yangle service really rocks\n";
  83.   ss << "It sucks when yangle isn't available\n";
  84.   ss << "10 reasons why yangle is the best IT company\n";
  85.   ss << "yangle rocks others suck\n";
  86.   ss << "Goondex really sucks, but yangle rocks. Use yangle\n";
  87.  
  88.   const auto stats = ExploreKeyWords(key_words, ss);
  89.   const map<string, int> expected = {
  90.     {"yangle", 6},
  91.     {"rocks", 2},
  92.     {"sucks", 1}
  93.   };
  94.   ASSERT_EQUAL(stats.word_frequences, expected);
  95. }
  96.  
  97. int main() {
  98.   TestRunner tr;
  99.   RUN_TEST(tr, TestBasic);
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement