Advertisement
Guest User

Untitled

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