Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <functional>
- #include <future>
- #include <sstream>
- #include <iostream>
- #include <iterator>
- #include <vector>
- #include <map>
- #include <set>
- #include <string>
- using namespace std;
- struct Stats {
- map<string, int> word_frequences;
- void operator+=(const Stats& other) {
- for (const auto& [word, frequency] : other.word_frequences) {
- word_frequences[word] += frequency;
- }
- }
- };
- using KeyWords = set<string, less<>>;
- vector<string_view> SplitKeyWords(string_view text) {
- vector<string_view> result;
- while (true) {
- const auto space = text.find(' ');
- if (!text.empty() && space) {
- result.push_back(text.substr(0, space));
- }
- if (text.npos != space) {
- text.remove_prefix(1 + space);
- } else {
- break;
- }
- }
- return result;
- }
- Stats SearchStr(const KeyWords& key_words,
- string_view str) {
- Stats result;
- for (const string_view s : SplitKeyWords(str)) {
- if (key_words.count(s)) {
- ++result.word_frequences[string(s)];
- }
- }
- return result;
- }
- Stats SearchPart(const KeyWords& key_words,
- vector<string> part) {
- Stats result;
- for (const string& p : part) {
- result += SearchStr(key_words, p);
- }
- return result;
- }
- Stats ExploreKeyWords(const KeyWords& key_words,
- istream& input) {
- const size_t PART_SIZE = 5000;
- vector<future<Stats>> stats;
- vector<string> part;
- for (string str; getline(input, str);) {
- part.push_back(move(str));
- if (PART_SIZE <= part.size()) {
- stats.push_back(async(SearchPart,
- ref(key_words),
- move(part)));
- }
- }
- Stats result;
- if (part.size()) {
- result += SearchPart(key_words,
- move(part));
- }
- for (auto& stat : stats) {
- result += stat.get();
- }
- return result;
- }
- int main() {
- const KeyWords key_words = {"yangle", "rocks", "sucks", "all"};
- stringstream ss;
- ss << "this new yangle service really rocks\n";
- ss << "It sucks when yangle isn't available\n";
- ss << "10 reasons why yangle is the best IT company\n";
- ss << "yangle rocks others suck\n";
- ss << "Goondex really sucks, but yangle rocks. Use yangle\n";
- for (const auto& [word, frequency] : ExploreKeyWords(key_words, ss).word_frequences) {
- cout << word << " " << frequency << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment