Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.97 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. using namespace std;
  14.  
  15. struct Stats
  16. {
  17.     map<string, int> word_frequences;
  18.  
  19.     void operator +=(const Stats& other)
  20.     {
  21.         for (const auto& kvp : other.word_frequences)
  22.         {
  23.             word_frequences[kvp.first] += kvp.second;
  24.         }
  25.     }
  26. };
  27.  
  28. const vector<string_view> SplitIntoWordsView(const string& s)
  29. {
  30.     string_view str = s;
  31.     vector<string_view> result;
  32.     size_t pos = 0;
  33.     const size_t pos_end = str.npos;
  34.     while (true)
  35.     {
  36.         size_t space = str.find(' ', pos);
  37.  
  38.         string_view word = str.substr(pos, space - pos);
  39.  
  40.         result.push_back(word);
  41.  
  42.         if (space == pos_end)
  43.         {
  44.             break;
  45.         }
  46.         else
  47.         {
  48.             pos = space + 1;
  49.         }
  50.     }
  51.  
  52.     return result;
  53. }
  54.  
  55. Stats ExploreLine(const set<string>& key_words, const string& line)
  56. {
  57.     const auto& words = SplitIntoWordsView(line);
  58.  
  59.     Stats lineStats;
  60.     for (string_view key : key_words)
  61.     {
  62.         int* word_freq = nullptr;
  63.  
  64.         bool first = true;
  65.  
  66.         for (string_view word : words)
  67.         {
  68.             if (word == key)
  69.             {
  70.                 if (first)
  71.                 {
  72.                     string k = static_cast<string>(key);
  73.                     word_freq = &lineStats.word_frequences[k];
  74.  
  75.                     first = false;
  76.                 }
  77.  
  78.                 (*word_freq)++;
  79.             }
  80.         }
  81.     }
  82.  
  83.     return lineStats;
  84. }
  85.  
  86. Stats ExploreKeyWordsSingleThread(const set<string>& key_words, istream& input)
  87. {
  88.     Stats result;
  89.     for (string line; getline(input, line);)
  90.     {
  91.         result += ExploreLine(key_words, line);
  92.     }
  93.     return result;
  94. }
  95.  
  96. Stats ExploreKeyWords(const set<string>& key_words, istream& input, size_t threadCount = 4)
  97. {
  98.     vector<stringstream> streams;
  99.     streams.reserve(threadCount);
  100.  
  101.     for(size_t i=0;i<threadCount;++i)
  102.     {
  103.         stringstream ss;
  104.         streams.push_back(move(ss));
  105.     }
  106.  
  107.     int streamNum = 0;
  108.     for (string line; getline(input, line);)
  109.     {
  110.         streams[streamNum] << line << "\n";
  111.         streamNum = (streamNum + 1) % threadCount;
  112.     }
  113.  
  114.     vector<future<Stats>> futures;
  115.     futures.reserve(threadCount);
  116.  
  117.     for (auto& substream : streams)
  118.     {
  119.         futures.push_back(async([&key_words, &substream]
  120.         {
  121.             return ExploreKeyWordsSingleThread(
  122.                     key_words, substream
  123.             );
  124.         }));
  125.     }
  126.  
  127.     Stats result;
  128.     for (auto& _future : futures)
  129.     {
  130.         result += _future.get();
  131.     }
  132.  
  133.     return result;
  134. }
  135.  
  136. void TestBasic()
  137. {
  138.     const set<string> key_words =
  139.     { "yangle", "rocks", "sucks", "all" };
  140.  
  141.     stringstream ss;
  142.     ss << "this new yangle service really rocks\n";
  143.     ss << "It sucks when yangle isn't available\n";
  144.     ss << "10 reasons why yangle is the best IT company\n";
  145.     ss << "yangle rocks others suck\n";
  146.     ss << "Goondex really sucks, but yangle rocks. Use yangle\n";
  147.  
  148.     const auto stats = ExploreKeyWords(key_words, ss);
  149.     const map<string, int> expected =
  150.     {
  151.     { "yangle", 6 },
  152.     { "rocks", 2 },
  153.     { "sucks", 1 } };
  154.     ASSERT_EQUAL(stats.word_frequences, expected);
  155. }
  156.  
  157. int main()
  158. {
  159.     TestRunner tr;
  160.     RUN_TEST(tr, TestBasic);
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement